Skip to content

Commit c71b240

Browse files
committed
Auto merge of #863 - RalfJung:deref-checks, r=RalfJung
adjust tests for eager pointer checks on deref The Miri side of rust-lang/rust#63075. Fixes #447.
2 parents 868da2a + 8a103cf commit c71b240

8 files changed

+21
-36
lines changed

rust-version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
00ee1b47f42129a0a6e33510578fbcf07c1e5382
1+
1cdcea920e56a5d0587307a4c9cf8fff5c77c4bc

src/operator.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ impl<'mir, 'tcx> EvalContextExt<'tcx> for super::MiriEvalContext<'mir, 'tcx> {
3535
#[inline]
3636
fn pointer_inbounds(&self, ptr: Pointer<Tag>) -> InterpResult<'tcx> {
3737
let (size, _align) = self.memory().get_size_and_align(ptr.alloc_id, AllocCheck::Live)?;
38-
ptr.check_in_alloc(size, CheckInAllocMsg::InboundsTest)
38+
ptr.check_inbounds_alloc(size, CheckInAllocMsg::InboundsTest)
3939
}
4040

4141
fn binary_ptr_op(
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// This should fail even without validation.
2+
// compile-flags: -Zmiri-disable-validation
3+
4+
fn main() {
5+
let x = 2usize as *const u32;
6+
let _y = unsafe { &*x as *const u32 }; //~ ERROR dangling pointer was dereferenced
7+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Deref a raw ptr to access a field of a large struct, where the field
2+
// is allocated but not the entire struct is.
3+
fn main() {
4+
let x = (1, 13);
5+
let xptr = &x as *const _ as *const (i32, i32, i32);
6+
let val = unsafe { (*xptr).1 }; //~ ERROR pointer must be in-bounds at offset 12, but is outside bounds of allocation
7+
assert_eq!(val, 13);
8+
}
Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
1-
// Validation makes this fail in the wrong place
2-
// compile-flags: -Zmiri-disable-validation
3-
41
// Even with intptrcast and without validation, we want to be *sure* to catch bugs
52
// that arise from pointers being insufficiently aligned. The only way to achieve
63
// that is not not let programs exploit integer information for alignment, so here
74
// we test that this is indeed the case.
85
fn main() {
96
let x = &mut [0u8; 3];
107
let base_addr = x as *mut _ as usize;
11-
let u16_ref = unsafe { if base_addr % 2 == 0 {
12-
&mut *(base_addr as *mut u16)
13-
} else {
14-
&mut *((base_addr+1) as *mut u16)
15-
} };
16-
*u16_ref = 2; //~ ERROR tried to access memory with alignment 1, but alignment 2 is required
8+
let base_addr_aligned = if base_addr % 2 == 0 { base_addr } else { base_addr+1 };
9+
let u16_ptr = base_addr_aligned as *mut u16;
10+
unsafe { *u16_ptr = 2; } //~ ERROR tried to access memory with alignment 1, but alignment 2 is required
1711
println!("{:?}", x);
1812
}

tests/compile-fail/storage_dead_dangling.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ fn fill(v: &mut i32) {
88
}
99

1010
fn evil() {
11-
let v = unsafe { &mut *(LEAK as *mut i32) };
12-
let _x = *v; //~ ERROR dangling pointer was dereferenced
11+
unsafe { &mut *(LEAK as *mut i32) }; //~ ERROR dangling pointer was dereferenced
1312
}
1413

1514
fn main() {

tests/run-pass/ref-invalid-ptr.rs

Lines changed: 0 additions & 12 deletions
This file was deleted.

tests/run-pass/stacked-borrows/stacked-borrows.rs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// Test various stacked-borrows-related things.
22
fn main() {
3-
deref_partially_dangling_raw();
43
read_does_not_invalidate1();
54
read_does_not_invalidate2();
65
ref_raw_int_raw();
@@ -14,16 +13,6 @@ fn main() {
1413
shr_and_raw();
1514
}
1615

17-
// Deref a raw ptr to access a field of a large struct, where the field
18-
// is allocated but not the entire struct is.
19-
// For now, we want to allow this.
20-
fn deref_partially_dangling_raw() {
21-
let x = (1, 13);
22-
let xptr = &x as *const _ as *const (i32, i32, i32);
23-
let val = unsafe { (*xptr).1 };
24-
assert_eq!(val, 13);
25-
}
26-
2716
// Make sure that reading from an `&mut` does, like reborrowing to `&`,
2817
// NOT invalidate other reborrows.
2918
fn read_does_not_invalidate1() {

0 commit comments

Comments
 (0)