vulkano-rs / vulkano

Safe and rich Rust wrapper around the Vulkan API
Apache License 2.0
4.48k stars 433 forks source link

slice::from_raw_parts abort-panics while creating Instance with Debug Utils messenger #2494

Closed RA3236 closed 6 months ago

RA3236 commented 6 months ago

Issue

Creating an instance with a debug messenger callback attempts to create a slice from a raw pointer, but it instead panics (on my system) with the following message:

thread 'main' panicked at library\core\src\panicking.rs:156:5:
unsafe precondition(s) violated: slice::from_raw_parts requires the pointer to be aligned and non-null, and the total size of the slice not to exceed `isize::MAX`
stack backtrace:
   0: std::panicking::begin_panic_handler
             at /rustc/7d3702e472b99be0f5de6608dd87af1df8f99428/library\std\src\panicking.rs:645
   1: core::panicking::panic_nounwind_fmt::runtime
             at /rustc/7d3702e472b99be0f5de6608dd87af1df8f99428/library\core\src\panicking.rs:110
   2: core::panicking::panic_nounwind_fmt
             at /rustc/7d3702e472b99be0f5de6608dd87af1df8f99428/library\core\src\panicking.rs:123
   3: core::panicking::panic_nounwind
             at /rustc/7d3702e472b99be0f5de6608dd87af1df8f99428/library\core\src\panicking.rs:156
   4: core::slice::raw::from_raw_parts::precondition_check
             at /rustc/7d3702e472b99be0f5de6608dd87af1df8f99428\library\core\src\intrinsics.rs:2766
   5: core::slice::raw::from_raw_parts<ash::vk::definitions::DebugUtilsLabelEXT>
             at /rustc/7d3702e472b99be0f5de6608dd87af1df8f99428\library\core\src\slice\raw.rs:98
   6: vulkano::instance::debug::trampoline::closure$0
             at C:\Users\SNIP\.cargo\registry\src\index.crates.io-6f17d22bba15001f\vulkano-0.34.1\src\instance\debug.rs:351
   7: core::ops::function::FnOnce::call_once<vulkano::instance::debug::trampoline::closure_env$0,tuple$<> >
             at /rustc/7d3702e472b99be0f5de6608dd87af1df8f99428\library\core\src\ops\function.rs:250
   8: core::panic::unwind_safe::impl$25::call_once<tuple$<>,vulkano::instance::debug::trampoline::closure_env$0>
             at /rustc/7d3702e472b99be0f5de6608dd87af1df8f99428\library\core\src\panic\unwind_safe.rs:272
   9: std::panicking::try::do_call<core::panic::unwind_safe::AssertUnwindSafe<vulkano::instance::debug::trampoline::closure_env$0>,tuple$<> >
             at /rustc/7d3702e472b99be0f5de6608dd87af1df8f99428\library\std\src\panicking.rs:552
  10: vulkano::instance::impl$24::from
  11: std::panicking::try<tuple$<>,core::panic::unwind_safe::AssertUnwindSafe<vulkano::instance::debug::trampoline::closure_env$0> >
             at /rustc/7d3702e472b99be0f5de6608dd87af1df8f99428\library\std\src\panicking.rs:516
  12: std::panic::catch_unwind<core::panic::unwind_safe::AssertUnwindSafe<vulkano::instance::debug::trampoline::closure_env$0>,tuple$<> >
             at /rustc/7d3702e472b99be0f5de6608dd87af1df8f99428\library\std\src\panic.rs:146
  13: vulkano::instance::debug::trampoline
             at C:\Users\SNIP\.cargo\registry\src\index.crates.io-6f17d22bba15001f\vulkano-0.34.1\src\instance\debug.rs:328
  14: vkResetEvent
  15: vkResetEvent
  16: vkResetEvent
  17: vkResetEvent
  18: vkResetEvent
  19: vkResetEvent
  20: vkResetEvent
  21: vulkano::instance::Instance::new_unchecked
             at C:\Users\SNIP\.cargo\registry\src\index.crates.io-6f17d22bba15001f\vulkano-0.34.1\src\instance\mod.rs:515
  22: vulkano::instance::Instance::new
             at C:\Users\SNIP\.cargo\registry\src\index.crates.io-6f17d22bba15001f\vulkano-0.34.1\src\instance\mod.rs:307
  23: testing::main
             at .\src\main.rs:87
  24: core::ops::function::FnOnce::call_once<enum2$<core::result::Result<tuple$<>,anyhow::Error> > (*)(),tuple$<> >
             at /rustc/7d3702e472b99be0f5de6608dd87af1df8f99428\library\core\src\ops\function.rs:250
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
thread caused non-unwinding panic. aborting.
error: process didn't exit successfully: `target\debug\testing.exe` (exit code: 0xc0000409, STATUS_STACK_BUFFER_OVERRUN)

This also occurs with the example debug.rs in the examples folder (with the 0.34.X branch).

In src/instance/debug.rs:

// line 344
let callback_data = DebugUtilsMessengerCallbackData {
    message_id_name: p_message_id_name
        .as_ref()
        .map(|p_message_id_name| CStr::from_ptr(p_message_id_name).to_str().unwrap()),
    message_id_number,
    message: CStr::from_ptr(p_message).to_str().unwrap(),
    /// OFFENDING LINES
    queue_labels: DebugUtilsMessengerCallbackLabelIter(
        slice::from_raw_parts(p_queue_labels, queue_label_count as usize).iter(),
    ),
    ///
    cmd_buf_labels: DebugUtilsMessengerCallbackLabelIter(
        slice::from_raw_parts(p_cmd_buf_labels, cmd_buf_label_count as usize).iter(),
    ),
    objects: DebugUtilsMessengerCallbackObjectNameInfoIter(
        slice::from_raw_parts(p_objects, object_count as usize).iter(),
    ),
};

This is likely to be a driver bug.

marc0246 commented 6 months ago

This is already fixed by #2490.

RA3236 commented 6 months ago

My apologies, thank you.