Skip to content

fix(core): correct get_neighbor_code +boundary detection and level-0 UB - #34

Merged
nikbott merged 1 commit into
devfrom
fix/neighbor-boundary-overflow
Aug 14, 2026
Merged

nikbott merged 1 commit into
devfrom
fix/neighbor-boundary-overflow

Conversation

@nikbott

@nikbott nikbott commented Aug 14, 2026

Copy link
Copy Markdown
Owner

What (from the parallel adversarial audit)

The get_neighbor_code +-direction overflow guard (current | ~dim_mask) == UINT64_MAX only fires at full resolution, so a coarser cell at the +-domain boundary got a wrapped neighbour code instead of the UINT64_MAX sentinel:

  • +x, or any axis at small max_level → a code sorting above every valid leaf (benign in balance, but wrong contract), and
  • +y/+z at max_level=21 (3D) → wraps to the origin (the carry runs off the top of the 64-bit word), which can make balance() spuriously flag the wrapped-location cell.

Two audit agents disagreed on severity (one called it benign, one high); working the bit-arithmetic shows both are partly right — benign at DIC scales, a real wrap at the envelope edge. Fixed regardless, plus a companion UB.

Fix

  • +dir guard → an at-maximum-aligned-coordinate test: (current & dim_mask) == (dim_mask & domain_mask) & ~(one_dilated-1). Returns the sentinel exactly at the boundary; unchanged for interior cells.
  • Early level == 0 → sentinel (a root cell spans the whole domain): also removes a 1<<64 shift UB in one_dilated at the maximum level (CORE-2).
  • Applied identically in omp / mpi / cuda.

Validation

  • New [boundary] regression at max_level=21 (far-corner +x/+y/+z and level-0 → sentinel; interior still resolves). Fails on the old code.
  • Full omp (18 cases) + mpi (np-driven) suites pass; CUDA byte-identical balance parity re-run on the A2000 — all fixtures pass (the fix touches balance and balance_ref identically, so parity is preserved).
  • clang-format + full pre-commit clean.

The +direction overflow guard '(current | ~dim_mask) == UINT64_MAX' only
fired at full resolution, so a coarser cell at the +domain boundary produced
a wrapped neighbour code -- above every valid leaf, or the origin when the
carry ran off the top of the 64-bit word (3D +y/+z at max_level 21) --
instead of the UINT64_MAX sentinel, risking a spurious balance flag. Replace
it with an at-maximum-aligned-coordinate test.

Also return the sentinel immediately for a level-0 cell (the whole domain has
no neighbour), removing a 1<<64 shift UB in one_dilated at the maximum level.

Fixed identically in omp/mpi/cuda; byte-identical balance parity preserved
(both balance and balance_ref share the primitive). Adds a max_level-21
boundary regression test.
@nikbott
nikbott merged commit 0f99309 into dev Aug 14, 2026
9 checks passed
@nikbott
nikbott deleted the fix/neighbor-boundary-overflow branch August 22, 2026 16:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant