Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 18c17fa

Browse files
committed
Extend the null check to borrowed places and remove AsmOutput places
1 parent f0ba4d7 commit 18c17fa

File tree

5 files changed

+32
-6
lines changed

5 files changed

+32
-6
lines changed

compiler/rustc_mir_transform/src/check_alignment.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ impl<'tcx> crate::MirPass<'tcx> for CheckAlignment {
2020
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
2121
// Skip trivially aligned place types.
2222
let excluded_pointees = [tcx.types.bool, tcx.types.i8, tcx.types.u8];
23-
check_pointers(tcx, body, &excluded_pointees, insert_alignment_check);
23+
check_pointers(tcx, body, &excluded_pointees, insert_alignment_check, false);
2424
}
2525
}
2626

compiler/rustc_mir_transform/src/check_null.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ impl<'tcx> crate::MirPass<'tcx> for CheckNull {
1414
}
1515

1616
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
17-
check_pointers(tcx, body, &[], insert_null_check);
17+
check_pointers(tcx, body, &[], insert_null_check, true);
1818
}
1919
}
2020

compiler/rustc_mir_transform/src/check_pointers.rs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ pub(crate) fn check_pointers<'a, 'tcx, F>(
3535
body: &mut Body<'tcx>,
3636
excluded_pointees: &'a [Ty<'tcx>],
3737
on_finding: F,
38+
check_for_borrows: bool,
3839
) where
3940
F: Fn(
4041
/* tcx: */ TyCtxt<'tcx>,
@@ -66,7 +67,13 @@ pub(crate) fn check_pointers<'a, 'tcx, F>(
6667
let statement = &basic_blocks[block].statements[statement_index];
6768
let source_info = statement.source_info;
6869

69-
let mut finder = PointerFinder::new(tcx, local_decls, typing_env, excluded_pointees);
70+
let mut finder = PointerFinder::new(
71+
tcx,
72+
local_decls,
73+
typing_env,
74+
excluded_pointees,
75+
check_for_borrows,
76+
);
7077
finder.visit_statement(statement, location);
7178

7279
for (local, ty) in finder.into_found_pointers() {
@@ -110,6 +117,7 @@ struct PointerFinder<'a, 'tcx> {
110117
typing_env: ty::TypingEnv<'tcx>,
111118
pointers: Vec<(Place<'tcx>, Ty<'tcx>)>,
112119
excluded_pointees: &'a [Ty<'tcx>],
120+
check_for_borrows: bool,
113121
}
114122

115123
impl<'a, 'tcx> PointerFinder<'a, 'tcx> {
@@ -118,8 +126,16 @@ impl<'a, 'tcx> PointerFinder<'a, 'tcx> {
118126
local_decls: &'a mut LocalDecls<'tcx>,
119127
typing_env: ty::TypingEnv<'tcx>,
120128
excluded_pointees: &'a [Ty<'tcx>],
129+
check_for_borrows: bool,
121130
) -> Self {
122-
PointerFinder { tcx, local_decls, typing_env, excluded_pointees, pointers: Vec::new() }
131+
PointerFinder {
132+
tcx,
133+
local_decls,
134+
typing_env,
135+
excluded_pointees,
136+
pointers: Vec::new(),
137+
check_for_borrows,
138+
}
123139
}
124140

125141
fn into_found_pointers(self) -> Vec<(Place<'tcx>, Ty<'tcx>)> {
@@ -134,14 +150,16 @@ impl<'a, 'tcx> Visitor<'tcx> for PointerFinder<'a, 'tcx> {
134150
match context {
135151
PlaceContext::MutatingUse(
136152
MutatingUseContext::Store
137-
| MutatingUseContext::AsmOutput
138153
| MutatingUseContext::Call
139154
| MutatingUseContext::Yield
140155
| MutatingUseContext::Drop,
141156
) => {}
142157
PlaceContext::NonMutatingUse(
143158
NonMutatingUseContext::Copy | NonMutatingUseContext::Move,
144159
) => {}
160+
PlaceContext::MutatingUse(MutatingUseContext::Borrow)
161+
| PlaceContext::NonMutatingUse(NonMutatingUseContext::SharedBorrow)
162+
if self.check_for_borrows => {}
145163
_ => {
146164
return;
147165
}
Lines changed: 8 additions & 0 deletions
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: *mut u32 = std::ptr::null_mut();
7+
let _ptr: &mut u32 = unsafe { &mut *ptr };
8+
}

tests/ui/mir/null/use_null.rs renamed to tests/ui/mir/null/borrowed_null.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44

55
fn main() {
66
let ptr: *const u32 = std::ptr::null();
7-
let _ptr = unsafe { *ptr };
7+
let _ptr: &u32 = unsafe { &*ptr };
88
}

0 commit comments

Comments
 (0)