Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle null pointer in fmt_labels #389

Merged
merged 1 commit into from
May 3, 2024

Conversation

patowen
Copy link
Collaborator

@patowen patowen commented May 3, 2024

Rust 1.78.0 introduced some debug assertions for unsafe preconditions. One such precondition, which we have inadvertently violated for a while was that slice::from_raw_parts does not accept null pointers, even when the size is 0.

An except from the Rust docs:

data must be non-null and aligned even for zero-length slices. One reason for this is that enum layout optimizations may rely on references (including slices of any length) being aligned and non-null to distinguish them from other data. You can obtain a pointer that is usable as data for zero-length slices using NonNull::dangling.

While another option could have been to replace ptr with NonNull::dangling if it was null, simply handling this case separately seems like a simpler approach.

This is the error that appeared when I tried running Hypermine in debug mode:

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/9b00956e56009bab2aa15d7bff10916599e3d6d6/library\std\src\panicking.rs:645 
   1: core::panicking::panic_nounwind_fmt::runtime
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library\core\src\panicking.rs:110
   2: core::panicking::panic_nounwind_fmt
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library\core\src\panicking.rs:123
   3: core::panicking::panic_nounwind
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library\core\src\panicking.rs:156
   4: client::graphics::core::messenger_callback::fmt_labels
             at .\core.rs:133
   5: client::graphics::core::messenger_callback
             at .\core.rs:151
   6: vkDestroyDescriptorPool
   7: vkDestroyDescriptorPool
   8: vkDestroyDescriptorPool
   9: vkDestroyDescriptorPool
  10: vkDestroyDescriptorPool
  11: vkDestroyDescriptorPool
  12: client::graphics::core::Core::new
             at .\core.rs:94
  13: client::main
             at E:\Gamma\git\rust\hypermine\client\src\main.rs:64
  14: core::ops::function::FnOnce::call_once
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6\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.

Copy link
Owner

@Ralith Ralith left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, thanks!

@Ralith Ralith merged commit 50cf2ff into Ralith:master May 3, 2024
6 checks passed
@patowen patowen deleted the fix-from-raw-parts branch May 3, 2024 23:51
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.

2 participants