Fix segfault on a mesh with convex inertia that no geom references - #3432
Fix segfault on a mesh with convex inertia that no geom references#3432VihaanAgarwal wants to merge 1 commit into
Conversation
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
|
Hey, I get the suspicion. I did use a coding agent to help me navigate the codebase, but I wasn't just submitting whatever it generated. It took me a while to track this down: The test segfaults on This is my first time working in a C++ codebase this large, so if the fix itself or the test placement is off, just let me know and I'll redo it. |
|
? |
|
Yeah, I use Claude Code. It’s 2026. If there’s an issue with the actual fix, I’m happy to address it. |
needhull_ was only set while iterating geoms, so a mesh whose inertia is computed from its convex hull never got one unless some geom pointed at it. mjCMesh::ComputeVolume and ComputeInertia then read graph_[1] and GraphFaces() off a null pointer and the compiler crashed. Whether a mesh needs its hull for inertia is a property of the mesh, not of any geom, so move that condition out of the geom loop and apply it to every mesh. Fixes google-deepmind#3431
977745b to
95f0540
Compare
Fixes #3431.
What happens
A mesh declared with
inertia="convex"that no geom references segfaults the compiler:mujoco.MjModel.from_xml_path("orphan.xml")exits with SIGSEGV on 3.10.0 and on current main.Root cause
mjCModel::TryCompiledecides which meshes need a convex hull by walkinggeoms_, and one of theconditions in that loop is
geoms_[i]->mesh->spec.inertia == mjMESH_INERTIA_CONVEX. That conditionis a property of the mesh, but it is only ever evaluated for meshes some geom points at. An
unreferenced mesh is never visited, so
needhull_stays false,mjCMesh::CompileskipsMakeGraph, andgraph_stays null.ComputeVolumeandComputeInertiathen doon that null pointer, which is the crash. Note the mesh here has faces, so the
face_.empty()fallback that would otherwise build the hull does not fire either.
Fix
Move the convex-inertia condition out of the geom loop and apply it to every mesh, so a mesh that
computes its inertia from its hull always gets one. The geom loop keeps the conditions that really
are about geoms (
contype,conaffinity, membership in a contact pair).The reporter asked for "a warning about unused meshes, or silence" — this gives silence, and it
matches the intent the original condition already expressed.
Test
MjCMeshTest.UnreferencedConvexInertiaMeshintest/user/user_mesh_test.cccompiles the modelabove. It dies with SIGSEGV on main and passes with this change.
Ran on macOS arm64, Release build:
user_mesh_test71/71 passuser_objects_test90/90,user_recompile_test320/320,user_flex_test59/59,user_composite_test4/4 all passuser_model_testhas one failure (MujocoTest.ResolvePluginMissingInstanceThrowsError) anduser_api_testsegfaults, both of which reproduce identically on unmodified main here, so theyare local/environmental and not from this change.
Changelog entry added under Compiler -> Bug fixes.