Skip to content

fix(LBIE): hex optimization copied an uninitialized point; refinement reused vertex 0 - #777

Merged
transfix merged 1 commit into
masterfrom
fix/lbie-hexa-uninit-crash
Oct 24, 2026
Merged

transfix merged 1 commit into
masterfrom
fix/lbie-hexa-uninit-crash

Conversation

@transfix

Copy link
Copy Markdown
Owner

Why

CvcCliTest.Regression_MeshImproveMethodsNoCrash and CvcCliTest.HexahedralizeMethodAndImproveFlags were flaky with an access violation in cvc hexahedralize --improve optimization -q 1. On 2026-10-09 (Windows/MSVC, bunny SDF from cvc sdf -d 16,16,16 -a v2) it crashed 9 of 400 runs on a RelWithDebInfo build, 7 of 400 on a Release build and 0 of 400 on another. That layout-dependence pointed at an uninitialized read.

The crash was #677's, and master no longer has it. Rerunning the same loop on the pre-#677 RelWithDebInfo cvc.exe, 2 of 400 runs crashed, and both stopped after finished traverse qef (stderr is unbuffered). That is in hex extraction, before --improve runs: the vertex walk that #677 fixed on 2026-10-18.

cvc.exe --improve optimization -q 1 --improve none
pre-#677 RelWithDebInfo 2 crashes / 400
master 82a3901, Release 0 / 2000
this branch, Release 0 / 2000 0 / 400

The uninitialized read is real, though. It writes garbage instead of crashing. Every --improve optimization run on master writes vertices that are not points of the volume. Which garbage you get depends on the stack: (-2.6e13, 0.03, -1.9e13) from the October build, (1.2501, 0, 0) from a current Release build (7 of 200 vertices outside the volume), (7.4e24, 5.8e-43, 7.9e24) in a test binary, then NaN once the next pass reads them.

What

geoframe::AddVert (LBIE_geoframe.h): refine_edge slots were never marked free. Each vertex has 18 refine_edge slots, free when -1, but int_18 neg1_18 = { { -1 } } sets only slot 0. After one edge or face was refined at a vertex, AddRefine_edgevtx_hex / AddRefine_facevtx (and AddRefine_edgevtx for QUAD) found no free slot and returned vertex 0. All 18 now start at -1. On the bunny:

bunny adaptive hex mesh master this branch
vertices 200 592
hexahedra with a repeated vertex (vertex 0) 338 of 351 0
corners with NaN / +inf condition number 1731 / 302 0 / 0
worst finite condition number 46341 169

Octree::optimization: the line search could run zero times. It moves the worst corner's vertex down the gradient while condition - cond_max > 1e-6, starting from condition = cond_max + 1.0f. A flat corner (Jacobian 0) has condition +inf, so that test is NaN (or 0 once cond_max >= 2^24): the loop never ran, and the uninitialized mmv4 was copied into the mesh. Now:

  • mmv4 starts at the corner itself, so a search that cannot run leaves the vertex in place;
  • the step halving stops at 0, where it used to loop forever if no step qualified;
  • a non-finite position (from a zero-length edge's NaN gradient) is never written.

Octree::optimization: it moved the wrong vertex for corners 4..7. The vertex to move was quads[6h+1][j-4], which is v5, v4, v7, v6 for j = 4..7 (add_hexa stores v4..v7 as v5 v4 v7 v6), and corner 4's line search used corner 1's edges. The per-vertex statistics read quads[6h][4] for j == 4, past the end of the quad. A corner_row/corner_col table now maps each corner to its quad and slot.

Both LBIE files are under cvc-mesher/.clang-format (DisableFormat), so they keep their legacy style.

Tests

Each new test fails against master and passes on this branch. I checked the library tests by running the new lbie_mesher_test.exe next to master's cvc.dll. The header test was compiled once against master's LBIE_geoframe.h, since that code is inlined into the test.

test master
LbieMesherTest.RefinementMakesAVertexPerEdgeAndFace second edge and face both return vertex 0
LbieMesherTest.AdaptiveHexahedraHaveEightDistinctCorners (17³ sphere, r = 1.2) 702 of 729 hexahedra repeat a vertex
LbieMesherTest.OptimizationLeavesAFlatCornerInPlace (one hexahedron, flat corner) vertex 0 → (2.2e22, 0, 2.4e22), then NaN
LbieMesherTest.OptimizationMovesTheWorstCornersOwnVertex (corner 6 worst) v6 stays put, v7 gets v6's new position
CvcCliTest.Regression_HexahedralizeOptimizationKeepsVerticesInTheVolume 11 of 334 written vertices outside the volume

Local runs on Windows/MSVC 14.44, Release, shared:

  • lbie_mesher_test: 43/43 pass.
  • algorithm_test: 10/10 pass.
  • geometry_test: 111/111 pass.
  • cvc_cli_test without Integration_*: 163 pass, 1 skipped (PlantLogWritesARun, unrelated).

Found, not fixed

  • The second --improve pass runs in world units, but optimization's first step of 0.5 is in grid units. Mesher::qualityImprove runs after the vertices are mapped to world coordinates. On the bunny (about 0.15 across) that pass now throws one vertex to (0.24, 0.32, -0.75), outside the volume, and flattens 2 corners (worst condition number 1832). Before this PR the zero-trip search hid it.

    Scaling the first step by the corner's shortest edge fixes the bunny (worst 20.6, no flat corners, no vertex outside the extracted mesh's bounding box). It is mixed elsewhere: on a 17³ sphere of radius 1.2 the grid-only pass ends at 38 instead of 14, though -q 1 ends at 10 instead of 14. It is left for its own change.

  • One flat corner turns optimization into a no-op for the whole mesh, since cond_max stays +inf. The CLI tests' tetrahedron SDF has 6 such corners after extraction. Choosing the worst corner among the finite ones would let the rest improve.

  • geoframe::AddBound_edge copies the edge into every remaining empty slot instead of stopping after the first, so only one boundary edge per vertex is ever recorded. LBIE: keep the hexa vertex walk on the grid (intermittent hexahedralize crash) #677 noted this too.

… reused vertex 0

`cvc hexahedralize --improve optimization` exited 0 but wrote garbage vertices. The
cause is an uninitialized read in Octree::optimization, fed by degenerate hexahedra
that geoframe::AddVert's refine_edge initialization produced.

Refinement. AddVert gives each vertex 18 refine_edge slots, free when -1, but
initialized them with `int_18 neg1_18 = { { -1 } }`, which sets only slot 0 and
zeroes the rest. Once one edge or face was refined at a vertex, AddRefine_edgevtx_hex
and AddRefine_facevtx (and AddRefine_edgevtx, for QUAD) found no free slot and
returned vertex 0 instead of a new vertex. In the bunny's adaptive hex mesh (`cvc sdf
-d 16,16,16 -a v2`) 338 of 351 hexahedra had vertex 0 in place of a corner; 1731
corners had a NaN condition number and 302 an infinite one. All 18 slots now start
at -1: the mesh has 592 vertices instead of 200, no hexahedron repeats a vertex, and
every corner is finite (worst condition number 169).

Optimization. Octree::optimization moves the vertex of the worst corner down the
gradient until that corner is no worse than cond_max. A flat corner (Jacobian 0) has
condition +inf, so cond_max + 1.0f - cond_max is NaN (or 0 once cond_max >= 2^24),
the line search never ran, and its result mmv4 went into the mesh uninitialized.
What came out depended on the stack: (-2.6e13, 0.03, -1.9e13) from an October
RelWithDebInfo build, (1.2501, 0, 0) from a current Release cvc.exe, (7.4e24,
5.8e-43, 7.9e24) in a test binary, then NaN once the next pass used them. mmv4 now
starts at the corner itself, the step halving ends at 0 (it looped forever when no
step qualified), and a non-finite position is never written.

The same routine moved the wrong vertex for corners 4..7: it read quads[6h+1][j-4],
which is v5, v4, v7, v6 for j = 4..7, and corner 4's line search used corner 1's
edges. Its per-vertex statistics read quads[6h][4] for j == 4, past the end of the
quad. A table of each corner's quad and slot replaces both.

The intermittent access violation the CLI tests hit
(Regression_MeshImproveMethodsNoCrash, HexahedralizeMethodAndImproveFlags) is not
here: it was the hexa vertex walk fixed by #677. On a pre-#677 build, both crashes in 400 runs of the bunny command
stopped after "finished traverse qef", before optimization runs. Current master
crashes 0 times in 2000.

Tests (lbie_mesher_test, cvc_cli_test), each failing against master's cvc.dll /
cvc.exe or header and passing here:
- RefinementMakesAVertexPerEdgeAndFace: a second edge and a face refined at one vertex
  get new vertices (master returns 0 for both).
- AdaptiveHexahedraHaveEightDistinctCorners: a 17^3 sphere of radius 1.2 (master: 702
  of 729 hexahedra repeat a vertex).
- OptimizationLeavesAFlatCornerInPlace: one hexahedron with a flat corner comes back
  unchanged (master wrote (2.2e22, 0, 2.4e22) into vertex 0, then NaN).
- OptimizationMovesTheWorstCornersOwnVertex: with corner 6 worst, v6 moves and v7
  does not (master wrote v6's new position onto v7).
- Regression_HexahedralizeOptimizationKeepsVerticesInTheVolume: every vertex the CLI
  writes lies in the volume (master: 11 of 334 outside).
@transfix
transfix merged commit 391b46b into master Oct 24, 2026
24 checks passed
@transfix
transfix deleted the fix/lbie-hexa-uninit-crash branch October 24, 2026 06:00
transfix added a commit that referenced this pull request Oct 24, 2026
Carries the master merge (#776 squash, #774, #775, #777, #780, #782,
#784, #785) up the H1 stack.
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