Repository navigation
fix(LBIE): hex optimization copied an uninitialized point; refinement reused vertex 0 - #777
Merged
Merged
Conversation
… 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).
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.
Why
CvcCliTest.Regression_MeshImproveMethodsNoCrashandCvcCliTest.HexahedralizeMethodAndImproveFlagswere flaky with an access violation incvc hexahedralize --improve optimization -q 1. On 2026-10-09 (Windows/MSVC, bunny SDF fromcvc 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 afterfinished traverse qef(stderr is unbuffered). That is in hex extraction, before--improveruns: the vertex walk that #677 fixed on 2026-10-18.cvc.exe--improve optimization -q 1--improve noneThe uninitialized read is real, though. It writes garbage instead of crashing. Every
--improve optimizationrun 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_edgeslots were never marked free. Each vertex has 18refine_edgeslots, free when -1, butint_18 neg1_18 = { { -1 } }sets only slot 0. After one edge or face was refined at a vertex,AddRefine_edgevtx_hex/AddRefine_facevtx(andAddRefine_edgevtxfor QUAD) found no free slot and returned vertex 0. All 18 now start at -1. On the bunny:Octree::optimization: the line search could run zero times. It moves the worst corner's vertex down the gradient whilecondition - cond_max > 1e-6, starting fromcondition = cond_max + 1.0f. A flat corner (Jacobian 0) has condition +inf, so that test is NaN (or 0 oncecond_max >= 2^24): the loop never ran, and the uninitializedmmv4was copied into the mesh. Now:mmv4starts at the corner itself, so a search that cannot run leaves the vertex in place;Octree::optimization: it moved the wrong vertex for corners 4..7. The vertex to move wasquads[6h+1][j-4], which is v5, v4, v7, v6 for j = 4..7 (add_hexastores v4..v7 asv5 v4 v7 v6), and corner 4's line search used corner 1's edges. The per-vertex statistics readquads[6h][4]for j == 4, past the end of the quad. Acorner_row/corner_coltable 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.exenext to master'scvc.dll. The header test was compiled once against master'sLBIE_geoframe.h, since that code is inlined into the test.LbieMesherTest.RefinementMakesAVertexPerEdgeAndFaceLbieMesherTest.AdaptiveHexahedraHaveEightDistinctCorners(17³ sphere, r = 1.2)LbieMesherTest.OptimizationLeavesAFlatCornerInPlace(one hexahedron, flat corner)(2.2e22, 0, 2.4e22), then NaNLbieMesherTest.OptimizationMovesTheWorstCornersOwnVertex(corner 6 worst)CvcCliTest.Regression_HexahedralizeOptimizationKeepsVerticesInTheVolumeLocal 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_testwithoutIntegration_*: 163 pass, 1 skipped (PlantLogWritesARun, unrelated).Found, not fixed
The second
--improvepass runs in world units, butoptimization's first step of 0.5 is in grid units.Mesher::qualityImproveruns 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 1ends at 10 instead of 14. It is left for its own change.One flat corner turns
optimizationinto a no-op for the whole mesh, sincecond_maxstays +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_edgecopies 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.