Repository navigation
Description
Severity: medium · Category: gpu-parallel, memory-ub · Fix order: 15 of 21 — fix this 15th.
Filenames are numbered in reverse fix order: 001 = fix last, 021 = fix first. This file is 007.
Locations: Tutorials/HIT/TurbulentForcing_def.H:152, Tutorials/HIT/TurbulentForcing_def.H:358
Based on commit 9bf664bf (line numbers refer to that tree).
Both defects sit in TurbulentForcing::init_turbulent_forcing and both concern the same ~4.9 MB stack buffer tmp: nothing constrains the mode indices that write into it, and the GPU copy out of it is never synchronized before its stack frame dies.
The defect
Tutorials/HIT/TurbulentForcing_def.H:152 — No check that nmodes*zstep (loop upper bound nzmodes) stays below array_size=33; the mode loops write FTX(kx,ky,kz) etc. out of bounds for high-aspect domains or large nmodes.
Tutorials/HIT/TurbulentForcing_def.H:358 — Gpu::htod_memcpy_async copies from the ~4.9MB stack array tmp with no Gpu::streamSynchronize before init_turbulent_forcing returns and tmp goes out of scope.
Why it matters
F042: Any run with AMREX_USE_FAST_FORCE and the default forcing parameters: the FPZZ write lands at offset 644688 in a stack array of 610929 Reals (33^3=35937 per slab, base 1635937=574992, plus 641089=69696), i.e. 33759 Reals ~= 264KB past the end of the ~4.7MB stack array tmp -- stack corruption, and in practice a crash.
F041: SYCL (oneAPI) GPU build of the HIT tutorial: amrex htod_memcpy_async is q.submit(memcpy) — fully asynchronous — so the copy can read tmp's stack frame after the function returns and the frame is reused; forcedata ends up containing garbage, silently corrupting all turbulent forcing. (CUDA/HIP only work by relying on pageable-source staging semantics.)
Suggested fix
F042: validate the input rather than trust it — turb.nmodes arrives via pp.get and is never compared against the compile-time array_size = 33, while the loops run inclusively to nmodes*step. Put an AMREX_ALWAYS_ASSERT (unconditional even in optimized builds, unlike AMREX_ASSERT) right after xstep/ystep/zstep. Maintainer's call: assert, or promote array_size to a managed runtime value of nmodes*max_step+1, which lifts the 33 limit and shrinks tmp from 4.9 MB of stack to tens of KB — but then NS_getForce.cpp:344-346 must build its Array4s from the same value, so both files change together.
F041: call Gpu::htod_memcpy, which is htod_memcpy_async plus Gpu::streamSynchronize (amrex/Src/Base/AMReX_GpuDevice.H:488). On SYCL the async form is a bare q.submit(h.memcpy), so the host source must outlive the queue command; tmp does not, and a caller-side sync cannot rescue a dead frame.
Also fix lines 113-114, which print nxmodes in place of nymodes/nzmodes — the one diagnostic that would have exposed the overrun.
For Tutorials/HIT/TurbulentForcing_def.H:152 (F042):
--- a/Tutorials/HIT/TurbulentForcing_def.H
+++ b/Tutorials/HIT/TurbulentForcing_def.H
@@ -104,6 +104,11 @@
int nxmodes = nmodes*(int)(0.5+Lx/Lmin);
int nymodes = nmodes*(int)(0.5+Ly/Lmin);
int nzmodes = nmodes*(int)(0.5+Lz/Lmin);
+
+ // Mode indices run 0:n?modes, so they must fit in the 0:array_size-1
+ // extent of the forcing arrays.
+ AMREX_ALWAYS_ASSERT_WITH_MESSAGE(std::max(nxmodes,std::max(nymodes,nzmodes)) < array_size,
+ "TurbulentForcing: too many modes; reduce turb.nmodes or increase array_size");
if (verbose)
{Fail fast before the mode loops write past the flat tmp array. nxmodes/nymodes/nzmodes are exactly nmodes*xstep/ystep/zstep, so this also covers the symmetry-breaking loop (kz<zstep) and NS_getForce.cpp's device loops, which index forcedata with the same bounds. Maintainer decides: assert, or make array_size in TurbulentForcing_params.H large/dynamic enough for high-aspect domains.
For Tutorials/HIT/TurbulentForcing_def.H:358 (F041):
--- a/Tutorials/HIT/TurbulentForcing_def.H
+++ b/Tutorials/HIT/TurbulentForcing_def.H
@@ -355,7 +355,7 @@
if (Gpu::inLaunchRegion())
{
forcedata = static_cast<Real*>(The_Arena()->alloc(tmp_size*sizeof(Real)));
- Gpu::htod_memcpy_async(forcedata, tmp, tmp_size*sizeof(Real));
+ Gpu::htod_memcpy(forcedata, tmp, tmp_size*sizeof(Real));
}
else
#endifUses the synchronous variant (AMReX_GpuDevice.H:487-493 = htod_memcpy_async + Gpu::streamSynchronize), so the copy finishes before the stack array tmp dies at function exit. Needed because the SYCL path (GpuDevice.H:424-426) is a bare q.submit(memcpy) with no wait. One-time init, so the sync costs nothing.
Diff(s) are against 9bf664bf, written from the current source and verified only with git apply --check — never compiled, never run, never applied to the tree. Treat them as precise intent, not tested patches.
Verification evidence
F042 — confirmed (one verifier lens)
Lens 1 (refutation attempt): nzmodes = nmodes*(int)(0.5+Lz/Lmin) (line 106) is never checked against array_size=33; loop 'kz <= nzmodes; kz += zstep' (line 142). With nmodes=4, Lz/Lx=16: kz=64 at kx=ky=0 has kappa=4/Lx <= kappaMax=4/Lx+1e-8, so FTX(0,0,64) writes offset 641089=69696 into a 35937-element slab; for FPZZ (base 574992) that is 644688 > tmp_size=610929 — past the stack array (~264KB, not the claimed ~26KB, but OOB stands). Code explicitly supports Lz>Lx (asserts only Lx==Ly, 'Lz can be longer'). NS_getForce.cpp:363 device loops (kz <= nmodeszstep, same kappa filter) OOB-read the same forcedata indices. nmodes>=33 on a cubic box also overruns.
F041 — confirmed (one verifier lens)
Lens 1 (refutation attempt): Line 358: Gpu::htod_memcpy_async(forcedata, tmp, tmp_size*sizeof(Real)); is the last statement in the GPU branch and the function returns at line 366, ending the lifetime of the ~4.9MB stack array tmp. amrex/Src/Base/AMReX_GpuDevice.H:424-426 implements the SYCL path as q.submit(&{ h.memcpy(p_d,p_h,sz); }) with no wait — the host source must outlive the command. No streamSynchronize exists in the function; a caller sync cannot save a dead stack frame. The synchronous htod_memcpy (GpuDevice.H:488, adds Gpu::streamSynchronize) is the correct call. oneAPI/SYCL is a supported IAMR build (oneAPI CI commits b5817ee). CUDA/HIP escape only via pageable-source staging semantics, as claimed.
Based on commit 9bf664bf, which is also the tree the audit verified against. From an automated audit of Source/, Tutorials/ and Util/. Audit finding ids: F042, F041. Reviewer unit(s): Tutorials-HIT. Nothing here was compiled or run — the failure scenarios are code reasoning, so the reaching configuration above is the cheapest way to confirm or refute it.
Activity
Fix issues AMReX-Fluids#179, AMReX-Fluids#181-AMReX-Fluids#185, AMReX…