Skip to content

CAMR::clean_state runs computeTemp over S.nGrow() ghost cells that were never filled when state_nghost > 0 #113

Closed

Description

@WeiqunZhang

Severity: low · Category: correctness
Locations: Source/CAMR.cpp:1283-1292 (clean_state), Source/CAMR.cpp:707, Source/CAMR.cpp:718, Source/CAMR_advance.cpp:83, Source/CAMR_advance.cpp:161, Source/CAMR_advance.cpp:168
Based on commit 17997db (line numbers refer to that tree).

The defect

clean_state(S) derives the ghost width it works on from the MultiFab itself:

int ng = S.nGrow();
computeTemp(S,ng);          // CAMR.cpp:1290-1291

computeTemp -> reset_internal_energy -> CAMR_reset_internal_e then runs on
growntilebox(ng) and evaluates 1/rho, rho*e and EOS::REY2T there.

That is right for Sborder (expand_state FillPatches exactly numGrow() ghosts
before calling clean_state), but the same routine is also applied to the
state data itself -- get_old_data(State_Type) at CAMR_advance.cpp:83,
S_new at CAMR_advance.cpp:161,168, and the coarse/fine new data in
post_timestep (CAMR.cpp:707,718). State data has state_nghost
(CAMR.state_nghost, _cpp_parameters:14) ghost cells, and nothing in CAMR
ever fills them: initData only setVal(0)s them, the advance only writes the
valid region (Saxpy(..., 0)), and swapTimeLevels hands over a freshly
allocated, uninitialised new_data. With state_nghost = 0 (the default) the
grown box equals the valid box and the problem is invisible; with any positive
value computeTemp reads and "repairs" garbage.

Why it matters

With CAMR.state_nghost = 1 in a non-EB build the very first coarse step aborts:
clean_state(get_old_data(...)) sees rho = 0 in the (never written) ghost
ring, CAMR_reset_internal_e computes rhoInv = 1/0 = inf and
Up = 0 * inf = NaN, which raises FE_INVALID -- and every Exec/*/inputs
sets amrex.fpe_trap_invalid = 1, so the run dies with SIGFPE. Without the
trap, the ghost ring of the old data holds NaN temperatures for that one call; the
MultiFab::Copy(S_new, Sborder, ..., S_new.nGrow()) at CAMR_advance.cpp:113 then overwrites the
state ghosts with FillPatched values every step, so the damage does not propagate into S_new
(refutation pass). The defect is the one-off uninitialised read and FPE at the first step. In EB builds the rho > 0 guards in
CAMR_reset_internal_e / computeTemp hide the crash but the pass over
unfilled cells is still an uninitialised read.

How to reach it

Exec/Sod, DIM=2, USE_EB=FALSE (the checked-in GNUmakefile), inputs-x plus
CAMR.state_nghost = 1. Crashes in the first call to CAMR::advance.
Any other Exec case with CAMR.state_nghost > 0 reads unfilled ghost cells the
same way.

Suggested fix

Give clean_state an explicit ghost count. Callers that clean state data pass
0; expand_state passes the ng it just FillPatched. git apply --check
passes on 17997db.

--- a/Source/CAMR.H
+++ b/Source/CAMR.H
@@ -388,7 +388,11 @@
   static void error_setup ();
 
   void expand_state(amrex::MultiFab& S, const amrex::Real time, const int ng);
-  void clean_state(amrex::MultiFab& S);
+
+  // Floor density, renormalize species and recompute T on the valid region
+  // plus ng ghost cells.  ng must not exceed the number of ghost cells that
+  // have actually been filled (0 for state data, numGrow() for Sborder).
+  void clean_state(amrex::MultiFab& S, int ng);
 #ifdef CAMR_USE_MOVING_EB
   void ZeroingOutForPlotting(amrex::MultiFab& S);
 #endif
--- a/Source/CAMR.cpp
+++ b/Source/CAMR.cpp
@@ -704,7 +704,7 @@
     // Clean up any aberrant state data generated by the reflux and average-down,
     // and then update quantities like temperature to be consistent.
     amrex::MultiFab& S_new_crse = get_new_data(State_Type);
-    clean_state(S_new_crse);
+    clean_state(S_new_crse, 0);
 
 #ifdef AMREX_USE_EB
     // If we redistribute then the ML redistribution algorithm may change
@@ -715,7 +715,7 @@
     if ( level < finest_level && (redistribution_type != "NoRedist") ) {
         CAMR& fine_level = getLevel(level + 1);
         amrex::MultiFab& S_new_fine = fine_level.get_new_data(State_Type);
-        clean_state(S_new_fine);
+        clean_state(S_new_fine, 0);
     }
 #endif
 
@@ -1276,18 +1276,21 @@
 
     AmrLevel::FillPatch(*this,S,ng,time,State_Type,0,S.nComp());
 
-    clean_state(S);
+    clean_state(S, ng);
 }
 
 void
-CAMR::clean_state(amrex::MultiFab& S)
+CAMR::clean_state(amrex::MultiFab& S, int ng)
 {
+  AMREX_ALWAYS_ASSERT(S.nGrow() >= ng);
+
   // Enforce a minimum density.
   enforce_min_density(S);
 
   normalize_species(S);
 
-  int ng = S.nGrow();
+  // Only touch ghost cells the caller has actually filled: the state data
+  // itself carries state_nghost unfilled ghost cells.
   computeTemp(S,ng);
 }
 
--- a/Source/CAMR_advance.cpp
+++ b/Source/CAMR_advance.cpp
@@ -80,7 +80,7 @@
     // trusted to respect the consistency between certain state variables
     // (e.g. UEINT and UEDEN) that we demand in every zone.
 
-    clean_state(get_old_data(State_Type));
+    clean_state(get_old_data(State_Type), 0);
 
     MultiFab& S_old = get_old_data(State_Type);
     amrex::ignore_unused(S_old);
@@ -158,14 +158,14 @@
     }
 
     // Sync up state after old sources and hydro source.
-    clean_state(S_new);
+    clean_state(S_new, 0);
 
 
     // "new source" is actually the correction to the old source we've already added
     for (int n = 0; n < src_list.size(); ++n) {
         construct_new_source(src_list[n], time, dt);
         MultiFab::Saxpy(S_new, dt, *new_sources[src_list[n]], 0, 0, NVAR, 0);
-        clean_state(S_new);
+        clean_state(S_new, 0);
     }
 
     Sborder.clear();

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