Skip to content

Commit 8b8ffa1

Browse files
committed
Insert null checks for pointer dereferences when debug assertions are 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.
1 parent 9d3d57c commit 8b8ffa1

File tree

24 files changed

+196
-4
lines changed

24 files changed

+196
-4
lines changed

compiler/rustc_codegen_cranelift/src/base.rs

+10
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,16 @@ fn codegen_fn_body(fx: &mut FunctionCx<'_, '_, '_>, start_block: Block) {
417417
Some(source_info.span),
418418
);
419419
}
420+
AssertKind::NullPointerDereference => {
421+
let location = fx.get_caller_location(source_info).load_scalar(fx);
422+
423+
codegen_panic_inner(
424+
fx,
425+
rustc_hir::LangItem::PanicNullPointerDereference,
426+
&[location],
427+
Some(source_info.span),
428+
)
429+
}
420430
_ => {
421431
let location = fx.get_caller_location(source_info).load_scalar(fx);
422432

compiler/rustc_codegen_ssa/src/mir/block.rs

+5
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,11 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
713713
// and `#[track_caller]` adds an implicit third argument.
714714
(LangItem::PanicMisalignedPointerDereference, vec![required, found, location])
715715
}
716+
AssertKind::NullPointerDereference => {
717+
// It's `fn panic_null_pointer_dereference()`, and
718+
// `#[track_caller]` adds an implicit third argument.
719+
(LangItem::PanicNullPointerDereference, vec![location])
720+
}
716721
_ => {
717722
// It's `pub fn panic_...()` and `#[track_caller]` adds an implicit argument.
718723
(msg.panic_function(), vec![location])

compiler/rustc_const_eval/src/const_eval/machine.rs

+1
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,7 @@ impl<'tcx> interpret::Machine<'tcx> for CompileTimeMachine<'tcx> {
509509
found: eval_to_int(found)?,
510510
}
511511
}
512+
NullPointerDereference => NullPointerDereference,
512513
};
513514
Err(ConstEvalErrKind::AssertFailure(err)).into()
514515
}

compiler/rustc_hir/src/lang_items.rs

+1
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,7 @@ language_item_table! {
317317
PanicAsyncFnResumedPanic, sym::panic_const_async_fn_resumed_panic, panic_const_async_fn_resumed_panic, Target::Fn, GenericRequirement::None;
318318
PanicAsyncGenFnResumedPanic, sym::panic_const_async_gen_fn_resumed_panic, panic_const_async_gen_fn_resumed_panic, Target::Fn, GenericRequirement::None;
319319
PanicGenFnNonePanic, sym::panic_const_gen_fn_none_panic, panic_const_gen_fn_none_panic, Target::Fn, GenericRequirement::None;
320+
PanicNullPointerDereference, sym::panic_null_pointer_dereference, panic_null_pointer_dereference, Target::Fn, GenericRequirement::None;
320321
/// libstd panic entry point. Necessary for const eval to be able to catch it
321322
BeginPanic, sym::begin_panic, begin_panic_fn, Target::Fn, GenericRequirement::None;
322323

compiler/rustc_middle/messages.ftl

+3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ middle_assert_gen_resume_after_panic = `gen` fn or block cannot be further itera
1717
middle_assert_misaligned_ptr_deref =
1818
misaligned pointer dereference: address must be a multiple of {$required} but is {$found}
1919
20+
middle_assert_null_ptr_deref =
21+
null pointer dereference occurred
22+
2023
middle_assert_op_overflow =
2124
attempt to compute `{$left} {$op} {$right}`, which would overflow
2225

compiler/rustc_middle/src/mir/syntax.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1012,6 +1012,7 @@ pub enum AssertKind<O> {
10121012
ResumedAfterReturn(CoroutineKind),
10131013
ResumedAfterPanic(CoroutineKind),
10141014
MisalignedPointerDereference { required: O, found: O },
1015+
NullPointerDereference,
10151016
}
10161017

10171018
#[derive(Clone, Debug, PartialEq, TyEncodable, TyDecodable, Hash, HashStable)]

compiler/rustc_middle/src/mir/terminator.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ impl<O> AssertKind<O> {
195195
ResumedAfterPanic(CoroutineKind::Desugared(CoroutineDesugaring::Gen, _)) => {
196196
LangItem::PanicGenFnNonePanic
197197
}
198+
NullPointerDereference => LangItem::PanicNullPointerDereference,
198199

199200
BoundsCheck { .. } | MisalignedPointerDereference { .. } => {
200201
bug!("Unexpected AssertKind")
@@ -260,6 +261,7 @@ impl<O> AssertKind<O> {
260261
"\"misaligned pointer dereference: address must be a multiple of {{}} but is {{}}\", {required:?}, {found:?}"
261262
)
262263
}
264+
NullPointerDereference => write!(f, "\"null pointer dereference occured\""),
263265
ResumedAfterReturn(CoroutineKind::Coroutine(_)) => {
264266
write!(f, "\"coroutine resumed after completion\"")
265267
}
@@ -330,7 +332,7 @@ impl<O> AssertKind<O> {
330332
ResumedAfterPanic(CoroutineKind::Coroutine(_)) => {
331333
middle_assert_coroutine_resume_after_panic
332334
}
333-
335+
NullPointerDereference => middle_assert_null_ptr_deref,
334336
MisalignedPointerDereference { .. } => middle_assert_misaligned_ptr_deref,
335337
}
336338
}
@@ -363,7 +365,7 @@ impl<O> AssertKind<O> {
363365
add!("left", format!("{left:#?}"));
364366
add!("right", format!("{right:#?}"));
365367
}
366-
ResumedAfterReturn(_) | ResumedAfterPanic(_) => {}
368+
ResumedAfterReturn(_) | ResumedAfterPanic(_) | NullPointerDereference => {}
367369
MisalignedPointerDereference { required, found } => {
368370
add!("required", format!("{required:#?}"));
369371
add!("found", format!("{found:#?}"));

compiler/rustc_middle/src/mir/visit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,7 @@ macro_rules! make_mir_visitor {
636636
OverflowNeg(op) | DivisionByZero(op) | RemainderByZero(op) => {
637637
self.visit_operand(op, location);
638638
}
639-
ResumedAfterReturn(_) | ResumedAfterPanic(_) => {
639+
ResumedAfterReturn(_) | ResumedAfterPanic(_) | NullPointerDereference => {
640640
// Nothing to visit
641641
}
642642
MisalignedPointerDereference { required, found } => {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
use rustc_index::IndexVec;
2+
use rustc_middle::mir::interpret::Scalar;
3+
use rustc_middle::mir::*;
4+
use rustc_middle::ty::{Ty, TyCtxt};
5+
use rustc_session::Session;
6+
7+
use crate::check_pointers::check_pointers;
8+
9+
pub(super) struct CheckNull;
10+
11+
impl<'tcx> crate::MirPass<'tcx> for CheckNull {
12+
fn is_enabled(&self, sess: &Session) -> bool {
13+
sess.ub_checks()
14+
}
15+
16+
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
17+
check_pointers(tcx, body, &[], insert_null_check);
18+
}
19+
}
20+
21+
fn insert_null_check<'tcx>(
22+
tcx: TyCtxt<'tcx>,
23+
local_decls: &mut IndexVec<Local, LocalDecl<'tcx>>,
24+
block_data: &mut BasicBlockData<'tcx>,
25+
pointer: Place<'tcx>,
26+
_: Ty<'tcx>,
27+
source_info: SourceInfo,
28+
new_block: BasicBlock,
29+
) {
30+
let const_raw_ptr = Ty::new_imm_ptr(tcx, tcx.types.unit);
31+
let rvalue = Rvalue::Cast(CastKind::PtrToPtr, Operand::Copy(pointer), const_raw_ptr);
32+
let thin_ptr = local_decls.push(LocalDecl::with_source_info(const_raw_ptr, source_info)).into();
33+
block_data
34+
.statements
35+
.push(Statement { source_info, kind: StatementKind::Assign(Box::new((thin_ptr, rvalue))) });
36+
37+
// Transmute the pointer to a usize (equivalent to `ptr.addr()`)
38+
let rvalue = Rvalue::Cast(CastKind::Transmute, Operand::Copy(thin_ptr), tcx.types.usize);
39+
let addr = local_decls.push(LocalDecl::with_source_info(tcx.types.usize, source_info)).into();
40+
block_data
41+
.statements
42+
.push(Statement { source_info, kind: StatementKind::Assign(Box::new((addr, rvalue))) });
43+
44+
// Check if the pointer is null.
45+
let is_ok = local_decls.push(LocalDecl::with_source_info(tcx.types.bool, source_info)).into();
46+
let zero = Operand::Constant(Box::new(ConstOperand {
47+
span: source_info.span,
48+
user_ty: None,
49+
const_: Const::Val(ConstValue::Scalar(Scalar::from_target_usize(0, &tcx)), tcx.types.usize),
50+
}));
51+
block_data.statements.push(Statement {
52+
source_info,
53+
kind: StatementKind::Assign(Box::new((
54+
is_ok,
55+
Rvalue::BinaryOp(BinOp::Ne, Box::new((Operand::Copy(addr), zero))),
56+
))),
57+
});
58+
59+
// Set this block's terminator to our assert, continuing to new_block if we pass
60+
block_data.terminator = Some(Terminator {
61+
source_info,
62+
kind: TerminatorKind::Assert {
63+
cond: Operand::Copy(is_ok),
64+
expected: true,
65+
target: new_block,
66+
msg: Box::new(AssertKind::NullPointerDereference),
67+
// This calls panic_misaligned_pointer_dereference, which is #[rustc_nounwind].
68+
// We never want to insert an unwind into unsafe code, because unwinding could
69+
// make a failing UB check turn into much worse UB when we start unwinding.
70+
unwind: UnwindAction::Unreachable,
71+
},
72+
});
73+
}

compiler/rustc_mir_transform/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ declare_passes! {
117117
mod add_subtyping_projections : Subtyper;
118118
mod check_alignment : CheckAlignment;
119119
mod check_const_item_mutation : CheckConstItemMutation;
120+
mod check_null : CheckNull;
120121
mod check_packed_ref : CheckPackedRef;
121122
mod check_undefined_transmutes : CheckUndefinedTransmutes;
122123
// This pass is public to allow external drivers to perform MIR cleanup
@@ -656,6 +657,7 @@ fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
656657
&[
657658
// Add some UB checks before any UB gets optimized away.
658659
&check_alignment::CheckAlignment,
660+
&check_null::CheckNull,
659661
// Before inlining: trim down MIR with passes to reduce inlining work.
660662

661663
// Has to be done before inlining, otherwise actual call will be almost always inlined.

compiler/rustc_monomorphize/src/collector.rs

+3
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,9 @@ impl<'a, 'tcx> MirVisitor<'tcx> for MirUsedCollector<'a, 'tcx> {
814814
mir::AssertKind::MisalignedPointerDereference { .. } => {
815815
push_mono_lang_item(self, LangItem::PanicMisalignedPointerDereference);
816816
}
817+
mir::AssertKind::NullPointerDereference => {
818+
push_mono_lang_item(self, LangItem::PanicNullPointerDereference);
819+
}
817820
_ => {
818821
push_mono_lang_item(self, msg.panic_function());
819822
}

compiler/rustc_smir/src/rustc_smir/convert/mir.rs

+3
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,9 @@ impl<'tcx> Stable<'tcx> for mir::AssertMessage<'tcx> {
484484
found: found.stable(tables),
485485
}
486486
}
487+
AssertKind::NullPointerDereference => {
488+
stable_mir::mir::AssertMessage::NullPointerDereference
489+
}
487490
}
488491
}
489492
}

compiler/rustc_span/src/symbol.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1456,6 +1456,7 @@ symbols! {
14561456
panic_location,
14571457
panic_misaligned_pointer_dereference,
14581458
panic_nounwind,
1459+
panic_null_pointer_dereference,
14591460
panic_runtime,
14601461
panic_str_2015,
14611462
panic_unwind,

compiler/stable_mir/src/mir/body.rs

+2
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ pub enum AssertMessage {
251251
ResumedAfterReturn(CoroutineKind),
252252
ResumedAfterPanic(CoroutineKind),
253253
MisalignedPointerDereference { required: Operand, found: Operand },
254+
NullPointerDereference,
254255
}
255256

256257
impl AssertMessage {
@@ -306,6 +307,7 @@ impl AssertMessage {
306307
AssertMessage::MisalignedPointerDereference { .. } => {
307308
Ok("misaligned pointer dereference")
308309
}
310+
AssertMessage::NullPointerDereference => Ok("null pointer dereference occured"),
309311
}
310312
}
311313
}

compiler/stable_mir/src/mir/pretty.rs

+3
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,9 @@ fn pretty_assert_message<W: Write>(writer: &mut W, msg: &AssertMessage) -> io::R
296296
"\"misaligned pointer dereference: address must be a multiple of {{}} but is {{}}\",{pretty_required}, {pretty_found}"
297297
)
298298
}
299+
AssertMessage::NullPointerDereference => {
300+
write!(writer, "\"null pointer dereference occured.\"")
301+
}
299302
AssertMessage::ResumedAfterReturn(_) | AssertMessage::ResumedAfterPanic(_) => {
300303
write!(writer, "{}", msg.description().unwrap())
301304
}

compiler/stable_mir/src/mir/visit.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,10 @@ pub trait MirVisitor {
426426
| AssertMessage::RemainderByZero(op) => {
427427
self.visit_operand(op, location);
428428
}
429-
AssertMessage::ResumedAfterReturn(_) | AssertMessage::ResumedAfterPanic(_) => { //nothing to visit
429+
AssertMessage::ResumedAfterReturn(_)
430+
| AssertMessage::ResumedAfterPanic(_)
431+
| AssertMessage::NullPointerDereference => {
432+
//nothing to visit
430433
}
431434
AssertMessage::MisalignedPointerDereference { required, found } => {
432435
self.visit_operand(required, location);

library/core/src/panicking.rs

+16
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,22 @@ fn panic_misaligned_pointer_dereference(required: usize, found: usize) -> ! {
291291
)
292292
}
293293

294+
#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never), cold, optimize(size))]
295+
#[cfg_attr(feature = "panic_immediate_abort", inline)]
296+
#[track_caller]
297+
#[cfg_attr(not(bootstrap), lang = "panic_null_pointer_dereference")] // needed by codegen for panic on null pointer deref
298+
#[rustc_nounwind] // `CheckNull` MIR pass requires this function to never unwind
299+
fn panic_null_pointer_dereference() -> ! {
300+
if cfg!(feature = "panic_immediate_abort") {
301+
super::intrinsics::abort()
302+
}
303+
304+
panic_nounwind_fmt(
305+
format_args!("null pointer dereference occured"),
306+
/* force_no_backtrace */ false,
307+
)
308+
}
309+
294310
/// Panics because we cannot unwind out of a function.
295311
///
296312
/// This is a separate function to avoid the codesize impact of each crate containing the string to

src/tools/rust-analyzer/crates/hir-def/src/lang_item.rs

+1
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,7 @@ language_item_table! {
410410
PanicLocation, sym::panic_location, panic_location, Target::Struct, GenericRequirement::None;
411411
PanicImpl, sym::panic_impl, panic_impl, Target::Fn, GenericRequirement::None;
412412
PanicCannotUnwind, sym::panic_cannot_unwind, panic_cannot_unwind, Target::Fn, GenericRequirement::Exact(0);
413+
PanicNullPointerDereference, sym::panic_null_pointer_dereference, panic_null_pointer_dereference, Target::Fn, GenericRequirement::None;
413414
/// libstd panic entry point. Necessary for const eval to be able to catch it
414415
BeginPanic, sym::begin_panic, begin_panic_fn, Target::Fn, GenericRequirement::None;
415416

tests/ui/mir/null/addrof_null.rs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//@ run-pass
2+
//@ compile-flags: -C debug-assertions
3+
4+
struct Field {
5+
a: u32,
6+
}
7+
8+
fn main() {
9+
unsafe {
10+
let ptr: *const Field = std::ptr::null();
11+
let _ptr = core::ptr::addr_of!((*ptr).a);
12+
}
13+
}

tests/ui/mir/null/null_lhs.rs

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//@ run-fail
2+
//@ compile-flags: -C debug-assertions
3+
//@ error-pattern: null pointer dereference occured
4+
5+
fn main() {
6+
let ptr: *mut u32 = std::ptr::null_mut();
7+
unsafe {
8+
*(ptr) = 42;
9+
}
10+
}

tests/ui/mir/null/null_rhs.rs

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//@ run-fail
2+
//@ compile-flags: -C debug-assertions
3+
//@ error-pattern: null pointer dereference occured
4+
5+
fn main() {
6+
let ptr: *mut u32 = std::ptr::null_mut();
7+
unsafe {
8+
let _v = *ptr;
9+
}
10+
}
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//@ run-pass
2+
//@ compile-flags: -C debug-assertions
3+
4+
fn main() {
5+
let ptr: *const u16 = std::ptr::null();
6+
unsafe {
7+
let _ = *ptr;
8+
}
9+
}

tests/ui/mir/null/two_pointers.rs

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//@ run-fail
2+
//@ compile-flags: -C debug-assertions
3+
//@ error-pattern: null pointer dereference occured
4+
5+
fn main() {
6+
let ptr = std::ptr::null();
7+
let mut dest = 0u32;
8+
let dest_ptr = &mut dest as *mut u32;
9+
unsafe {
10+
*dest_ptr = *(ptr);
11+
}
12+
}

tests/ui/mir/null/use_null.rs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//@ run-fail
2+
//@ compile-flags: -C debug-assertions
3+
//@ error-pattern: null pointer dereference occured
4+
5+
fn main() {
6+
let ptr: *const u32 = std::ptr::null();
7+
let _ptr = unsafe { *ptr };
8+
}

0 commit comments

Comments
 (0)