A MIR transform that checks pointers are aligned

This commit is contained in:
Ben Kimock 2022-11-10 11:37:28 -05:00
parent e216300876
commit 8ccf53332e
34 changed files with 360 additions and 20 deletions

View file

@ -4,6 +4,7 @@
// ignore-emscripten
// ignore-gnux32
// ignore 32-bit platforms (LLVM has a bug with them)
// ignore-debug
// Check that LLVM understands that `Iter` pointer is not null. Issue #37945.

View file

@ -1,5 +1,6 @@
// compile-flags: -Zvirtual-function-elimination -Clto -O -Csymbol-mangling-version=v0
// ignore-32bit
// ignore-debug
// CHECK: @vtable.0 = {{.*}}, !type ![[TYPE0:[0-9]+]], !vcall_visibility ![[VCALL_VIS0:[0-9]+]]
// CHECK: @vtable.1 = {{.*}}, !type ![[TYPE1:[0-9]+]], !vcall_visibility ![[VCALL_VIS0:[0-9]+]]

View file

@ -8,10 +8,10 @@
scope 1 {
debug _x => _1; // in scope 1 at $DIR/inline_into_box_place.rs:+1:9: +1:11
}
+ scope 2 (inlined Vec::<u32>::new) { // at $DIR/inline_into_box_place.rs:7:38: 7:48
+ scope 2 (inlined Vec::<u32>::new) { // at $DIR/inline_into_box_place.rs:8:38: 8:48
+ let mut _3: alloc::raw_vec::RawVec<u32>; // in scope 2 at $SRC_DIR/alloc/src/vec/mod.rs:LL:COL
+ }
+ scope 3 (inlined Box::<Vec<u32>>::new) { // at $DIR/inline_into_box_place.rs:7:29: 7:49
+ scope 3 (inlined Box::<Vec<u32>>::new) { // at $DIR/inline_into_box_place.rs:8:29: 8:49
+ debug x => _2; // in scope 3 at $SRC_DIR/alloc/src/boxed.rs:LL:COL
+ let mut _4: usize; // in scope 3 at $SRC_DIR/alloc/src/boxed.rs:LL:COL
+ let mut _5: usize; // in scope 3 at $SRC_DIR/alloc/src/boxed.rs:LL:COL
@ -28,7 +28,7 @@
+ StorageLive(_3); // scope 2 at $SRC_DIR/alloc/src/vec/mod.rs:LL:COL
+ _3 = const _; // scope 2 at $SRC_DIR/alloc/src/vec/mod.rs:LL:COL
// mir::Constant
- // + span: $DIR/inline_into_box_place.rs:7:38: 7:46
- // + span: $DIR/inline_into_box_place.rs:8:38: 8:46
- // + user_ty: UserType(2)
- // + literal: Const { ty: fn() -> Vec<u32> {Vec::<u32>::new}, val: Value(<ZST>) }
+ // + span: $SRC_DIR/alloc/src/vec/mod.rs:LL:COL
@ -47,7 +47,7 @@
bb1: {
- _1 = Box::<Vec<u32>>::new(move _2) -> [return: bb2, unwind: bb4]; // scope 0 at $DIR/inline_into_box_place.rs:+1:29: +1:49
- // mir::Constant
- // + span: $DIR/inline_into_box_place.rs:7:29: 7:37
- // + span: $DIR/inline_into_box_place.rs:8:29: 8:37
- // + user_ty: UserType(1)
- // + literal: Const { ty: fn(Vec<u32>) -> Box<Vec<u32>> {Box::<Vec<u32>>::new}, val: Value(<ZST>) }
+ StorageDead(_1); // scope 0 at $DIR/inline_into_box_place.rs:+2:1: +2:2

View file

@ -1,5 +1,6 @@
// ignore-endian-big
// ignore-wasm32-bare compiled with panic=abort by default
// ignore-debug MIR alignment checks in std alter the diff, breaking the test
// compile-flags: -Z mir-opt-level=4
// EMIT_MIR inline_into_box_place.main.Inline.diff

View file

@ -11,11 +11,11 @@ else
NM = nm
PANIC_SYMS = panic_bounds_check pad_integral Display Debug
PANIC_SYMS = panic_bounds_check Debug
# Allow for debug_assert!() in debug builds of std.
ifdef NO_DEBUG_ASSERTIONS
PANIC_SYMS += panicking panic_fmt
PANIC_SYMS += panicking panic_fmt pad_integral Display Debug
endif
all: main.rs

View file

@ -0,0 +1,11 @@
// run-fail
// compile-flags: -C debug-assertions
// error-pattern: misaligned pointer dereference: address must be a multiple of 0x4 but is
fn main() {
let mut x = [0u32; 2];
let ptr: *mut u8 = x.as_mut_ptr().cast::<u8>();
unsafe {
*(ptr.add(1).cast::<u32>()) = 42;
}
}