Skip to content

Commit 1115f04

Browse files
committed
A MIR transform that checks pointers are aligned
1 parent 79d8cff commit 1115f04

16 files changed

+53
-11
lines changed

src/shims/panic.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,34 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
220220
},
221221
)?;
222222
}
223+
MisalignedPointerDereference { required, found } => {
224+
// Forward to `panic_misaligned_pointer_dereference` lang item.
225+
226+
// First arg: required.
227+
let required = this.read_scalar(&this.eval_operand(required, None)?)?;
228+
// Second arg: found.
229+
let found = this.read_scalar(&this.eval_operand(found, None)?)?;
230+
231+
// Call the lang item.
232+
let panic_misaligned_pointer_dereference =
233+
this.tcx.lang_items().panic_misaligned_pointer_dereference_fn().unwrap();
234+
let panic_misaligned_pointer_dereference =
235+
ty::Instance::mono(this.tcx.tcx, panic_misaligned_pointer_dereference);
236+
this.call_function(
237+
panic_misaligned_pointer_dereference,
238+
Abi::Rust,
239+
&[required.into(), found.into()],
240+
None,
241+
StackPopCleanup::Goto {
242+
ret: None,
243+
unwind: match unwind {
244+
Some(cleanup) => StackPopUnwind::Cleanup(cleanup),
245+
None => StackPopUnwind::Skip,
246+
},
247+
},
248+
)?;
249+
}
250+
223251
_ => {
224252
// Forward everything else to `panic` lang item.
225253
this.start_panic(

tests/fail/unaligned_pointers/alignment.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
//@normalize-stderr-test: "\| +\^+" -> "| ^"
2+
//@compile-flags: -Cdebug-assertions=no
23

34
fn main() {
45
// No retry needed, this fails reliably.

tests/fail/unaligned_pointers/atomic_unaligned.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//@compile-flags: -Zmiri-symbolic-alignment-check
1+
//@compile-flags: -Zmiri-symbolic-alignment-check -Cdebug-assertions=no
22
#![feature(core_intrinsics)]
33

44
fn main() {

tests/fail/unaligned_pointers/drop_in_place.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//@compile-flags: -Cdebug-assertions=no
2+
13
#[repr(transparent)]
24
struct HasDrop(u8);
35

tests/fail/unaligned_pointers/dyn_alignment.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// should find the bug even without validation and stacked borrows, but gets masked by optimizations
2-
//@compile-flags: -Zmiri-disable-validation -Zmiri-disable-stacked-borrows -Zmir-opt-level=0
2+
//@compile-flags: -Zmiri-disable-validation -Zmiri-disable-stacked-borrows -Zmir-opt-level=0 -Cdebug-assertions=no
33

44
#[repr(align(256))]
55
#[derive(Debug)]

tests/fail/unaligned_pointers/intptrcast_alignment_check.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//@compile-flags: -Zmiri-symbolic-alignment-check -Zmiri-permissive-provenance
1+
//@compile-flags: -Zmiri-symbolic-alignment-check -Zmiri-permissive-provenance -Cdebug-assertions=no
22
// With the symbolic alignment check, even with intptrcast and without
33
// validation, we want to be *sure* to catch bugs that arise from pointers being
44
// insufficiently aligned. The only way to achieve that is not not let programs

tests/fail/unaligned_pointers/reference_to_packed.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// This should fail even without validation/SB
2-
//@compile-flags: -Zmiri-disable-validation -Zmiri-disable-stacked-borrows
2+
//@compile-flags: -Zmiri-disable-validation -Zmiri-disable-stacked-borrows -Cdebug-assertions=no
33

44
#![allow(dead_code, unused_variables)]
55

tests/fail/unaligned_pointers/unaligned_ptr1.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// This should fail even without validation or Stacked Borrows.
2-
//@compile-flags: -Zmiri-disable-validation -Zmiri-disable-stacked-borrows
2+
//@compile-flags: -Zmiri-disable-validation -Zmiri-disable-stacked-borrows -Cdebug-assertions=no
33

44
fn main() {
55
// Try many times as this might work by chance.

tests/fail/unaligned_pointers/unaligned_ptr2.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// This should fail even without validation or Stacked Borrows.
2-
//@compile-flags: -Zmiri-disable-validation -Zmiri-disable-stacked-borrows
2+
//@compile-flags: -Zmiri-disable-validation -Zmiri-disable-stacked-borrows -Cdebug-assertions=no
33

44
fn main() {
55
// No retry needed, this fails reliably.

tests/fail/unaligned_pointers/unaligned_ptr3.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// This should fail even without validation or Stacked Borrows.
2-
//@compile-flags: -Zmiri-disable-validation -Zmiri-disable-stacked-borrows
2+
//@compile-flags: -Zmiri-disable-validation -Zmiri-disable-stacked-borrows -Cdebug-assertions=no
33

44
fn main() {
55
// Try many times as this might work by chance.

0 commit comments

Comments
 (0)