Skip to content

LBIE: keep the hexa vertex walk on the grid (intermittent hexahedralize crash) - #677

Merged
transfix merged 1 commit into
masterfrom
fix/hexahedralize-improve-crash
Oct 18, 2026
Merged

transfix merged 1 commit into
masterfrom
fix/hexahedralize-improve-crash

Conversation

@transfix

Copy link
Copy Markdown
Owner

Why

CvcCliTest.Regression_MeshImproveMethodsNoCrash and CvcCliTest.HexahedralizeMethodAndImproveFlags failed now and then with an access violation (0xC0000005) in cvc hexahedralize.

Reproduction. On the CLI tests' input (the tetrahedron SDF, sdf -d 16,16,16 -a v2), the release cvc hexahedralize -i sdf.rawiv -o out.off -v 0.0 crashed:

Option Crashes
no --improve 3 of 40 runs
--improve edge-contract 2 of 40
--improve geo-flow 2 of 40

The output always stopped after "finished traverse qef", so the crash is in hex mesh extraction, not in --improve. With OMP_NUM_THREADS=1 it crashed 0 of 80 times. OpenMP wasn't racing, though: its threads only change the heap layout around a bad read.

Cause (AddressSanitizer, /fsanitize=address /Z7, flags it on every run): a 4-byte heap-buffer-overflow read.

READ of size 4 ... LBIE::Octree::getCellValues  octree.cpp:3586
  LBIE::Octree::get_vtx_new                     hexa.cpp:1640
  LBIE::Octree::hexa_adaptive_2                 hexa.cpp:152
  LBIE::Octree::hexahedralize                   hexa.cpp:51

get_vtx_new() moves each hexa vertex onto the isosurface by walking across the finest cells. Where the surface meets the volume's boundary, the walk stepped off the grid in two ways:

  • a vertex on the far face gives a starting cell index of 2^oct_depth;
  • a step past the edge goes to -1 or 2^oct_depth.

xyz2octcell() returns -1 for both, and getCellValues(-1) reads orig_vol out of bounds. The vertex also lands outside the volume.

What

In get_vtx_new():

  • the walk starts in the nearest real cell (indices clamped to [0, 2^oct_depth));
  • when the next cell would be outside the grid, the walk stops on the last cell's boundary.

Vertex and element counts are unchanged on the inputs checked: 334 vertices for the CLI's SDF; 344 vertices / 4374 quads and 64 / 162 for the test spheres.

Tests

  • New LbieMesherTest.HexaVerticesStayInsideAVolumeTheSurfaceLeaves meshes spheres of radius 1.2 and 1.5 in a [-1, 1]³ box, so the surface leaves the box, and checks every vertex stays inside it. Without the fix it fails deterministically, with no sanitizer needed: 44 and 1 coordinates outside, the same on every run. With the fix it passes.
  • Release cvc hexahedralize: 0 crashes in 80 runs without --improve and 0 in 80 with --improve edge-contract (before, about 1 in 15).
  • Under AddressSanitizer: every combination the CLI regression tests use is clean, 26 in all:
    • tetrahedralize, hexahedralize and tetrahedralize2, each with none, geo-flow, edge-contract, joe-liu, minimal-vol and optimization;
    • hexahedralize -m libisocontour and -m fastcontouring;
    • layer-mesh with each improve method.
  • The three CLI mesh cases (Regression_MeshImproveMethodsNoCrash, Regression_LayerMeshImproveMethodsNoCrash, HexahedralizeMethodAndImproveFlags) passed 10 of 10 --gtest_repeat runs. lbie_mesher_test passes 37/37.
  • Full ctest locally (MSVC 14.38 Release, Windows, 4088 cases): all pass except:

Found, not fixed (latent; ASan did not hit them on these inputs)

  • is_min_vertex accepts corners outside the volume. It sets vtx[i] = -1 in an unsigned int array and then checks vtx[i] >= 0, which is always true, so a corner where min_vtx_hexa returned -1 would be accepted as vertex 4294967295.
  • min_vtx_hexa can step to a negative level. It loops while (level == 0 || !is_refined(…, level - 1)). At level 0 that steps to level -1 and calls is_refined(…, -2), which indexes level_id[-2]. It is only reachable when the octree's root is a leaf.
  • Two vertex-array bugs in LBIE_geoframe.h:
    • AddVert initializes refine_edge with int_18 neg1_18 = { { -1 } }, which sets only element 0 to -1 and the other 17 to 0;
    • AddBound_edge doesn't stop after filling an empty slot, so it copies the edge into every remaining slot.

…ze crash)

Octree::get_vtx_new() moves each hexa vertex onto the isosurface by
walking across the finest cells. Where the surface meets the volume's
boundary the walk stepped off the grid: a vertex on the far face gives a
cell index of 2^oct_depth, and stepping past the edge goes to -1 or
2^oct_depth. xyz2octcell() returns -1 for those, and getCellValues(-1)
read orig_vol out of bounds. Release builds crashed about 1 run in 15 on
the CLI tests' tetrahedron SDF (Regression_MeshImproveMethodsNoCrash,
HexahedralizeMethodAndImproveFlags), with or without --improve, and
AddressSanitizer reports the read on every run. The vertex also landed
outside the volume.

The walk now starts in the nearest real cell and stops on the last
cell's boundary when the next cell would be outside the grid.
LbieMesherTest.HexaVerticesStayInsideAVolumeTheSurfaceLeaves meshes two
spheres larger than their box and checks every vertex stays inside it;
it fails without the fix (44 and 1 coordinates outside).
@transfix
transfix merged commit 5e8d668 into master Oct 18, 2026
15 checks passed
@transfix
transfix deleted the fix/hexahedralize-improve-crash branch October 18, 2026 04:25
transfix added a commit that referenced this pull request Oct 24, 2026
… reused vertex 0 (#777)

`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).
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