Skip to content

Commit 52b1360

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 52b1360

File tree

24 files changed

+196
-4
lines changed

24 files changed

+196
-4
lines changed

compiler/rustc_codegen_cranelift/src/base.rs

Lines changed: 10 additions & 0 deletions
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

Lines changed: 5 additions & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 3 additions & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 4 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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 } => {
Lines changed: 73 additions & 0 deletions
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

Lines changed: 2 additions & 0 deletions
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.

0 commit comments

Comments
 (0)