parry icon indicating copy to clipboard operation
parry copied to clipboard

3D Voxels example crash

Open ThierryBerger opened this issue 6 months ago • 1 comments

I encountered this crash on voxels example, it reproduces on mac + PopOS after a few seconds:

The crash happens at https://github.com/dimforge/parry/blob/025c77d2ee438dde2c9dfc8cd81f60cceaab35e4/src/shape/voxels.rs#L858

rel_key.z is somehow -1, so when cast to u32, that leads to an overflowing multiplication.

The point_key.z in argument is under the min_dimension.z:

voxel_key = [
    126,
    0,
    -1,
]
self.domain_mins = [
    0,
    -11,
    0,
]
Crash

    Finished `dev` profile [optimized + debuginfo] target(s) in 0.16s
     Running `target/debug/all_examples3`
2025-06-12T07:57:42.318205Z  INFO bevy_render::renderer: AdapterInfo { name: "AMD Radeon RX 7600 (RADV NAVI33)", vendor: 4098, device: 29824, device_type: DiscreteGpu, driver: "radv", driver_info: "Mesa 24.0.3-1pop1~1711635559~22.04~7a9f319", backend: Vulkan }
2025-06-12T07:57:42.375048Z  INFO bevy_winit::system: Creating new window "Rapier: 3D demos" (0v1#4294967296)
2025-06-12T07:57:42.375078Z  INFO winit::platform_impl::linux::x11::window: Guessed window scale factor: 1

[/home/tb/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/parry3d-0.21.1/src/shape/voxels.rs:858:9] rel_key = [
    [
        126,
        11,
        -1,
    ],
]

thread 'main' panicked at /home/tb/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/parry3d-0.21.1/src/shape/voxels.rs:858:9:
attempt to add with overflow
stack backtrace:
   0: __rustc::rust_begin_unwind
             at /rustc/17067e9ac6d7ecb70e50f92c1944e545188d2359/library/std/src/panicking.rs:697:5
   1: core::panicking::panic_fmt
             at /rustc/17067e9ac6d7ecb70e50f92c1944e545188d2359/library/core/src/panicking.rs:75:14
   2: core::panicking::panic_const::panic_const_add_overflow
             at /rustc/17067e9ac6d7ecb70e50f92c1944e545188d2359/library/core/src/panicking.rs:178:21
   3: parry3d::shape::voxels::Voxels::linear_index
             at /home/tb/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/parry3d-0.21.1/src/shape/voxels.rs:858:9
   4: parry3d::query::contact_manifolds::contact_manifolds_voxels_shape::CanonicalVoxelShape::from_voxel
             at /home/tb/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/parry3d-0.21.1/src/query/contact_manifolds/contact_manifolds_voxels_shape.rs:355:17
   5: parry3d::query::contact_manifolds::contact_manifolds_voxels_shape::contact_manifolds_voxels_shape
             at /home/tb/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/parry3d-0.21.1/src/query/contact_manifolds/contact_manifolds_voxels_shape.rs:145:26
   6: parry3d::query::contact_manifolds::contact_manifolds_voxels_shape::contact_manifolds_voxels_shape_shapes
             at /home/tb/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/parry3d-0.21.1/src/query/contact_manifolds/contact_manifolds_voxels_shape.rs:87:9
   7: <parry3d::query::default_query_dispatcher::DefaultQueryDispatcher as parry3d::query::query_dispatcher::PersistentQueryDispatcher<ManifoldData,ContactData>>::contact_manifolds
             at /home/tb/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/parry3d-0.21.1/src/query/default_query_dispatcher.rs:555:21
   8: rapier3d::geometry::narrow_phase::NarrowPhase::compute_contacts::{{closure}}
             at ./crates/rapier3d/../../src/geometry/narrow_phase.rs:960:25
   9: <core::slice::iter::IterMut<T> as core::iter::traits::iterator::Iterator>::for_each
             at /home/tb/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/slice/iter/macros.rs:277:21
  10: rapier3d::geometry::narrow_phase::NarrowPhase::compute_contacts
             at ./crates/rapier3d/../../src/geometry/narrow_phase.rs:820:9
  11: rapier3d::pipeline::physics_pipeline::PhysicsPipeline::detect_collisions
             at ./crates/rapier3d/../../src/pipeline/physics_pipeline.rs:150:9
  12: rapier3d::pipeline::physics_pipeline::PhysicsPipeline::step
             at ./crates/rapier3d/../../src/pipeline/physics_pipeline.rs:627:13
  13: rapier_testbed3d::harness::Harness::step_with_graphics
             at ./crates/rapier_testbed3d/../../src_testbed/harness/mod.rs:235:9
  14: rapier_testbed3d::testbed::update_testbed
             at ./crates/rapier_testbed3d/../../src_testbed/testbed.rs:1436:17
  15: core::ops::function::FnMut::call_mut
             at /home/tb/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/function.rs:166:5
  16: core::ops::function::impls::<impl core::ops::function::FnMut<A> for &mut F>::call_mut
             at /home/tb/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/function.rs:294:13
  17: <Func as bevy_ecs::system::function_system::SystemParamFunction<fn(F0,F1,F2,F3,F4,F5,F6,F7,F8,F9,F10,F11,F12,F13) .> Out>>::run::call_inner
             at /home/tb/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/bevy_ecs-0.15.2/src/system/function_system.rs:1002:21
  18: <Func as bevy_ecs::system::function_system::SystemParamFunction<fn(F0,F1,F2,F3,F4,F5,F6,F7,F8,F9,F10,F11,F12,F13) .> Out>>::run
             at /home/tb/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/bevy_ecs-0.15.2/src/system/function_system.rs:1005:17
  19: <bevy_ecs::system::function_system::FunctionSystem<Marker,F> as bevy_ecs::system::system::System>::run_unsafe
             at /home/tb/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/bevy_ecs-0.15.2/src/system/function_system.rs:800:19
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
Encountered a panic in system `rapier_testbed3d::testbed::update_testbed`!
Encountered a panic in system `bevy_app::main_schedule::Main::run_main`!

ThierryBerger avatar Jun 12 '25 08:06 ThierryBerger

Thank you for reporting. What’s happening here is that CanonicalVoxelShape::from_voxels calls linear_index with an out-of-bounds voxels. The fact that voxel is out-of-bounds is legitimate here as it is part of the internal edges resolution logic.

The bug isn’t too critical as it is essentially just using a very weird contact manifold cache key when we hit this out-of-bounds case (and it won’t crash in release mode).

I think the fix is to compute the CanonicalVoxelShape::workspace_key differently. It would be a custom version of linear_index but with the voxels shape domain dilated by 1.

sebcrozet avatar Jun 12 '25 09:06 sebcrozet