Skip to content

main.cpp CAMR_USE_MOVING_EB loop: finalize_EB2() every step destroys the EB2 index space live level factories still point to; the next regrid reads freed memory, and geometry only "moves" via regrid_int=1 #112

Closed

Description

@WeiqunZhang

Severity: medium · Category: eb
Locations: Source/main.cpp:133-140, Source/main.cpp:114, Source/EB/embedded_boundaries.cpp:66-69, Source/Utils/CAMR_error.cpp:155-162, Source/CAMR.cpp:325-328, Exec/MovingEBCases/MovingPlane_Shock/inputs:34-43
Based on commit 17997db (line numbers refer to that tree).

The defect

With USE_MOVING_EB = TRUE (Exec/Make.CAMR:15-17 defines CAMR_USE_MOVING_EB) the driver loop is

    initialize_EB2(Geom(maxLevel), maxLevel, maxLevel, cumTime());   // EB2::Build -> IndexSpace::push
    amrptr->coarseTimeStep(stop_time);
    finalize_EB2();                                                  // EB2::Finalize -> IndexSpace::clear
  1. EB2::Finalize() (amrex/Src/EB/AMReX_EB2.cpp:45-49) calls IndexSpace::clear(), which destroys every
    IndexSpaceImp and the EB2::Level objects inside it. Every AmrLevel keeps an EBFArrayBoxFactory whose
    m_parent is a raw const EB2::Level* into that index space (AMReX_EBFabFactory.cpp:22,104). The factory's
    own MultiFabs (flags, volfrac, areafrac, ...) survive, but isAllRegular(), hasEBInfo(), getEBIndexSpace()
    and maxCoarseningLevel() dereference m_parent (AMReX_EBFabFactory.cpp:228-266).
    The next iteration pushes a new index space and calls coarseTimeStep, whose level-0 regrid
    (Amr::timeStep -> regrid(0) -> grid_places, AMReX_Amr.cpp:3088) calls errorEst on the existing
    levels first. CAMR::errorEst (CAMR_error.cpp:155-156) does
    dynamic_cast<EBFArrayBoxFactory const&>(Factory()).isAllRegular() -> m_parent->isAllRegular() on the
    freed level, then amrex::TagCutCells (refine_cut_cells = 1 in the shipped inputs) calls
    factory.hasEBInfo() (AMReX_EBAmrUtil.cpp:21) on it again. From coarse step 2 on, every regrid is a
    use-after-free read (it usually returns the stale bool, so the run appears to work).
  2. The moving geometry only reaches the solver through the regrid. CAMR::buildMetrics (CAMR.cpp:325-328)
    caches volfrac/areafrac/facecent pointers from the level's factory, and the factory is created once per
    AmrLevel construction. Nothing in the loop refreshes the factories: if amr.regrid_int[0] != 1 or
    amr.force_regrid_level_zero is not set, all levels keep the t=0 geometry forever while a fresh index space
    is built and thrown away each step. The shipped MovingPlane_Shock/inputs sets regrid_int = 1 and
    force_regrid_level_zero = 1, which is what makes the case run at all; nothing enforces it.
  3. Minor consequences of the same design: the first loop iteration pushes a second, identical t=0 index space on
    top of the one built at main.cpp:114; newly covered cells keep their old fluid values after the regrid
    FillPatch (CAMR::init(old), CAMR.cpp:442-460, never calls EB_set_covered), so the "covered cells are
    zero" invariant relied on elsewhere no longer holds in this build; and any final writePlotFile/checkPoint
    after the loop runs with dangling factories (harmless today only because EB_set_covered and the vfrac
    derive use the factory's own MultiFabs).

The path is not compiled by any CI job (all workflows build Exec/Sod and Exec/SodPlusSphere only).

Why it matters

Undefined behaviour (heap use-after-free) on every regrid in a moving-EB run; under ASan or a different allocator
it crashes, otherwise it silently reads stale flags. Without the exact regrid settings of the shipped inputs the
geometry does not move at all while the code claims it does.

How to reach it

Exec/MovingEBCases/MovingPlane_Shock, DIM=3 USE_EB=TRUE USE_MOVING_EB=TRUE, shipped inputs
(amr.max_level = 2, CAMR.refine_cut_cells = 1, amr.regrid_int = 1, amr.force_regrid_level_zero = 1).
Step 1 advances on the initial index space; finalize_EB2() frees it; step 2's regrid(0) -> errorEst
dereferences the freed EB2::Level. Build with -fsanitize=address to observe it directly.

Suggested fix

Keep the index space alive until no level references it: build the new space at the start of a step only when
time has advanced (the initial space already describes strt_time), erase the previous space after the step
(by then the forced regrid has rebuilt every level from the new top space), never call EB2::Finalize() in the
loop, and abort up front unless amr.regrid_int = 1 and amr.force_regrid_level_zero = 1 since the scheme
depends on them. A fuller fix would also zero newly covered cells after the regrid FillPatch and treat newly
uncovered cells; that is beyond a driver patch.

--- a/Source/main.cpp
+++ b/Source/main.cpp
@@ -116,6 +116,20 @@
 
   amrptr->init(strt_time, stop_time);
 
+#ifdef CAMR_USE_MOVING_EB
+  {
+    // The geometry is rebuilt at the start of every coarse step; the level
+    // factories only pick it up when every level (level 0 included) is
+    // regridded at the top of that step.
+    int force_regrid_level_zero = 0;
+    amrex::ParmParse ppa("amr");
+    ppa.query("force_regrid_level_zero", force_regrid_level_zero);
+    if (amrptr->regridInt(0) != 1 || !force_regrid_level_zero) {
+      amrex::Abort("CAMR_USE_MOVING_EB requires amr.regrid_int = 1 and amr.force_regrid_level_zero = 1");
+    }
+  }
+#endif
+
   // If we set the regrid_on_restart flag and if we are *not* going to take
   // a time step then we want to go ahead and regrid here.
   if (
@@ -131,12 +145,23 @@
          (amrptr->levelSteps(0) < max_step || max_step < 0) &&
          (amrptr->cumTime() < stop_time || stop_time < 0.0)) {
 #ifdef CAMR_USE_MOVING_EB
-    initialize_EB2(amrptr->Geom(amrptr->maxLevel()), amrptr->maxLevel(), amrptr->maxLevel(), amrptr->cumTime());
+    // Build the geometry for this step's start time.  The existing levels
+    // still point into the previous index space until the regrid inside
+    // coarseTimeStep replaces them, so it is erased only after the step.
+    // The initial index space (built above at strt_time) serves the first
+    // step, which does not regrid.
+    amrex::EB2::IndexSpace const* old_is = nullptr;
+    if (amrptr->cumTime() > strt_time) {
+      old_is = amrex::EB2::TopIndexSpaceIfPresent();
+      initialize_EB2(amrptr->Geom(amrptr->maxLevel()), amrptr->maxLevel(), amrptr->maxLevel(), amrptr->cumTime());
+    }
 #endif
     // Do a timestep
     amrptr->coarseTimeStep(stop_time);
 #ifdef CAMR_USE_MOVING_EB
-    finalize_EB2();
+    if (old_is) {
+      amrex::EB2::IndexSpace::erase(const_cast<amrex::EB2::IndexSpace*>(old_is));
+    }
 #endif
   }
 

(git apply --check OK against 17997db; finalize_EB2 in embedded_boundaries.cpp becomes unused but is left in
place. On a restart the first iteration already has cumTime() > strt_time, so the initial space is erased after
step 1; that is safe because the checkpoint carries level_count[0], which with regrid_int = 1 is 1 at every
checkpoint, so the first step after a restart regrids (amr.regrid_on_restart = 1 guarantees it regardless).)

Activity

  1. added a commit that references this issue on Nov 17, 2026
    ca188fb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions