Skip to content

Charge deposition skips bounds check #6655

@WeiqunZhang

Description

@WeiqunZhang

Summary

  • WarpXParticleContainer::DepositCharge in Particles/WarpXParticleContainer.cpp:1485-1489 and again in the overload at 1816-1820 tries to guard against depositing into a component that was never allocated.
  • The guard compares rho->nComp() >= icomp - 1, which is always true because icomp is non-negative. The check never fails, so requests for component 1 (used for the “new” charge) succeed even when rho_fp only allocated one component.
  • When do_dive_cleaning is disabled (the default), rho_fp has a single component. The subsequent alias MultiFab rhoi(*rho, amrex::make_alias, icomp*nc, nc); therefore indexes past the end of the allocation and lockAdd writes out-of-bounds. On GPU this manifests as miscomputed charge and intermittent crashes.

Suggested patch

diff --git a/Particles/WarpXParticleContainer.cpp b/Particles/WarpXParticleContainer.cpp
@@
-    WARPX_ALWAYS_ASSERT_WITH_MESSAGE(
-        rho->nComp() >= icomp - 1,
+    WARPX_ALWAYS_ASSERT_WITH_MESSAGE(
+        rho->nComp() >= (icomp + 1) * WarpX::ncomps,
         "Cannot deposit charge in rho component icomp=" + std::to_string(icomp) +
         ": not enough components allocated (" + std::to_string(rho->nComp()) + "!"
     );
@@
-    WARPX_ALWAYS_ASSERT_WITH_MESSAGE(
-        rho->nComp() >= icomp - 1,
+    WARPX_ALWAYS_ASSERT_WITH_MESSAGE(
+        rho->nComp() >= (icomp + 1) * WarpX::ncomps,
         "Cannot deposit charge in rho component icomp=" + std::to_string(icomp) +
         ": not enough components allocated (" + std::to_string(rho->nComp()) + "!"
     );

Prepared by Codex

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions