Repository navigation
Description
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
EB2::Finalize()(amrex/Src/EB/AMReX_EB2.cpp:45-49) callsIndexSpace::clear(), which destroys every
IndexSpaceImpand theEB2::Levelobjects inside it. EveryAmrLevelkeeps anEBFArrayBoxFactorywhose
m_parentis a rawconst EB2::Level*into that index space (AMReX_EBFabFactory.cpp:22,104). The factory's
own MultiFabs (flags, volfrac, areafrac, ...) survive, butisAllRegular(),hasEBInfo(),getEBIndexSpace()
andmaxCoarseningLevel()dereferencem_parent(AMReX_EBFabFactory.cpp:228-266).
The next iteration pushes a new index space and callscoarseTimeStep, whose level-0 regrid
(Amr::timeStep->regrid(0)->grid_places,AMReX_Amr.cpp:3088) callserrorEston 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, thenamrex::TagCutCells(refine_cut_cells = 1in 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).- The moving geometry only reaches the solver through the regrid.
CAMR::buildMetrics(CAMR.cpp:325-328)
cachesvolfrac/areafrac/facecentpointers from the level's factory, and the factory is created once per
AmrLevelconstruction. Nothing in the loop refreshes the factories: ifamr.regrid_int[0] != 1or
amr.force_regrid_level_zerois not set, all levels keep the t=0 geometry forever while a fresh index space
is built and thrown away each step. The shippedMovingPlane_Shock/inputssetsregrid_int = 1and
force_regrid_level_zero = 1, which is what makes the case run at all; nothing enforces it. - Minor consequences of the same design: the first loop iteration pushes a second, identical t=0 index space on
top of the one built atmain.cpp:114; newly covered cells keep their old fluid values after the regrid
FillPatch (CAMR::init(old),CAMR.cpp:442-460, never callsEB_set_covered), so the "covered cells are
zero" invariant relied on elsewhere no longer holds in this build; and any finalwritePlotFile/checkPoint
after the loop runs with dangling factories (harmless today only becauseEB_set_coveredand thevfrac
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
fix issues 111 112 113 114 115 116 (AMReX-Fluids#123)