Skip to content

MOL_umeth_eb: slopetmp on The_Arena is freed while the Riemann kernels reading it are still in flight #120

Closed

Description

@WeiqunZhang

Severity: low · Category: gpu-parallel
Locations: Source/Hydro/MOL/MOL_umeth_eb.cpp:68-70, Source/Hydro/MOL/MOL_umeth_eb.cpp:133-148, Source/Hydro/MOL/MOL_umeth_eb.cpp:171-191
Based on commit 17997db (line numbers refer to that tree).

The defect

MOL_umeth_eb allocates its slope scratch as a default-arena FAB (FArrayBox slopetmp; slopetmp.resize(bxg_ii,QVAR);, lines 68-70). The slopes are read by the mol_riemann_x/y/z kernels
launched at lines 95-110, 133-148 and 171-187; the function returns at line 191 without any
Gpu::streamSynchronize(). On GPU the FAB destructor calls arena()->streamOrderedFree(dptr, stream)
(amrex/Src/Base/AMReX_BaseFab.H:1848), which for a non-stream-ordered arena such as The_Arena() is an
immediate free(pt) (amrex/Src/Base/AMReX_Arena.cpp:130-136). The block goes back on the CArena free
list while the last Riemann kernel may still be reading it.

The non-EB twin MOL_umeth (MOL_umeth.cpp:55-58) uses the same plain-arena FAB but ends with
Gpu::streamSynchronize() (line 141), and every other per-box scratch FAB in the EB hydro path
(qxm/qxp, qec, divc, redistwgt, flux_tmp, dm_as_fine, the Godunov EB scratch) is on
The_Async_Arena(). slopetmp in the EB MOL path is the one exception.

Why it matters

At this commit no wrong answer is reachable, and it is important to say so: the MFIter loop is
#pragma omp parallel if (amrex::Gpu::notInLaunchRegion()) (CAMR_construct_hydro_source.cpp:78-80),
so one host thread runs the loop in GPU builds; the only later The_Arena allocation inside the same
iteration is adjust_fluxes_eb::divu (Hydro_adjust_fluxes.cpp:74), which is written by kernels on the
same stream (stream-ordered after the Riemann kernels), and adjust_fluxes_eb ends with
Gpu::streamSynchronize() (line 174) before the next iteration can allocate on another stream.

The hazard is latent: it becomes a use-after-free (wrong left/right states, NaN fluxes, nondeterministic
results) as soon as any of those three accidental protections goes away -- removing the sync in
adjust_fluxes_eb (it exists there for its own divu), enabling host threads with GPU streams, or adding
a host-side write into a fresh The_Arena block between line 191 and that sync. This is the same class
of defect as closed #95 (freed while redistribution kernels still read them), one step earlier in the
same iteration.

How to reach it

Any EB build with CAMR.do_mol = 1 and a cut box (Exec/SodPlusSphere, Exec/DoubleRamp,
Exec/ReReTest inputs; DIM = 2 or 3, USE_EB = TRUE, USE_CUDA/USE_HIP = TRUE). No observable failure at
17997db (see above); the fix is preventive.

Suggested fix

Allocate slopetmp on the async arena like the rest of the EB scratch (stream-ordered free, no sync
needed). Diff checked with git apply --check (audit/notes/T6-scratch/009-slopetmp-async.diff):

--- a/Source/Hydro/MOL/MOL_umeth_eb.cpp
+++ b/Source/Hydro/MOL/MOL_umeth_eb.cpp
@@ -65,8 +65,9 @@
     // ****************************************************************
     // Slopes -- we will compute divc on bxg2 so need slopes on bxg3
     // ****************************************************************
-    FArrayBox slopetmp;
-    slopetmp.resize(bxg_ii,QVAR);
+    // Async arena: the Riemann kernels that read the slopes are still in
+    // flight when this function returns and nothing here synchronizes.
+    FArrayBox slopetmp(bxg_ii, QVAR, amrex::The_Async_Arena());
     auto const& slope = slopetmp.array();
 
     AMREX_D_TERM(auto const& fx_arr = flux_tmp[0];,

Activity

  1. added a commit that references this issue on Nov 17, 2026
    e082e76
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