fix(core): correct get_neighbor_code +boundary detection and level-0 UB - #34
Merged
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What (from the parallel adversarial audit)
The
get_neighbor_code+-direction overflow guard(current | ~dim_mask) == UINT64_MAXonly fires at full resolution, so a coarser cell at the+-domain boundary got a wrapped neighbour code instead of theUINT64_MAXsentinel:+x, or any axis at smallmax_level→ a code sorting above every valid leaf (benign in balance, but wrong contract), and+y/+zatmax_level=21(3D) → wraps to the origin (the carry runs off the top of the 64-bit word), which can makebalance()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
+dirguard → 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.level == 0 → sentinel(a root cell spans the whole domain): also removes a1<<64shift UB inone_dilatedat the maximum level (CORE-2).Validation
[boundary]regression atmax_level=21(far-corner+x/+y/+zand level-0 → sentinel; interior still resolves). Fails on the old code.