Skip to content

Commit

Permalink
Insert null checks for pointer dereferences when debug assertions are…
Browse files Browse the repository at this point in the history
… enabled

Similar to how the alignment is already checked, this adds a check
for null pointer dereferences in debug mode. It is implemented similarly
to the alignment check as a MirPass.

This is related to a 2025H1 project goal for better UB checks in debug
mode: rust-lang/rust-project-goals#177.
  • Loading branch information
1c3t3a authored and gitbot committed Feb 20, 2025
1 parent 3e9d605 commit c77eb3d
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions core/src/panicking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,22 @@ fn panic_misaligned_pointer_dereference(required: usize, found: usize) -> ! {
)
}

#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never), cold, optimize(size))]
#[cfg_attr(feature = "panic_immediate_abort", inline)]
#[track_caller]
#[cfg_attr(not(bootstrap), lang = "panic_null_pointer_dereference")] // needed by codegen for panic on null pointer deref
#[rustc_nounwind] // `CheckNull` MIR pass requires this function to never unwind
fn panic_null_pointer_dereference() -> ! {
if cfg!(feature = "panic_immediate_abort") {
super::intrinsics::abort()
}

panic_nounwind_fmt(
format_args!("null pointer dereference occured"),
/* force_no_backtrace */ false,
)
}

/// Panics because we cannot unwind out of a function.
///
/// This is a separate function to avoid the codesize impact of each crate containing the string to
Expand Down

0 comments on commit c77eb3d

Please sign in to comment.