Skip to content

Commit 2590bc6

Browse files
committed
Auto merge of #1614 - RalfJung:raw-retag, r=RalfJung
Stacked Borrows: test raw-ref-to-field with raw ptr tracking Adds a test for rust-lang/rust#78597 (blocked on that landing first)
2 parents 9a2cfbf + 571b48c commit 2590bc6

File tree

6 files changed

+43
-14
lines changed

6 files changed

+43
-14
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ Violations of [Stacked Borrows] found that are likely bugs (but Stacked Borrows
411411
* [TiKV creating overlapping mutable reference and raw pointer](https://github.com/tikv/tikv/pull/7709)
412412
* [Windows `Env` iterator using a raw pointer outside its valid memory area](https://github.com/rust-lang/rust/pull/70479)
413413
* [`VecDeque::iter_mut` creating overlapping mutable references](https://github.com/rust-lang/rust/issues/74029)
414-
* [Standard library `SipHasher` using a raw pointer outside its valid memory area](https://github.com/rust-lang/rust/pull/78484)
414+
* [Various standard library aliasing issues involving raw pointers](https://github.com/rust-lang/rust/pull/78602)
415415

416416
## License
417417

rust-version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
a53fb30e3bf2655b0563da6d561c23cda5f3ec11
1+
5cdf5b882da9e8b7c73b5cadeb7745cb68f6ff63

tests/run-pass/rc.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// compile-flags: -Zmiri-track-raw-pointers
2+
// ignore-windows (FIXME: tracking raw pointers does not work on Windows)
13
#![feature(new_uninit)]
24
#![feature(get_mut_unchecked)]
35

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
fn main() {
2+
ref_raw_int_raw();
3+
}
4+
5+
// Just to make sure that casting a ref to raw, to int and back to raw
6+
// and only then using it works. This rules out ideas like "do escape-to-raw lazily";
7+
// after casting to int and back, we lost the tag that could have let us do that.
8+
fn ref_raw_int_raw() {
9+
let mut x = 3;
10+
let xref = &mut x;
11+
let xraw = xref as *mut i32 as usize as *mut i32;
12+
assert_eq!(unsafe { *xraw }, 3);
13+
}

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

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1+
// compile-flags: -Zmiri-track-raw-pointers
2+
// ignore-windows (FIXME: tracking raw pointers does not work on Windows)
3+
#![feature(raw_ref_macros)]
4+
use std::ptr;
5+
16
// Test various stacked-borrows-related things.
27
fn main() {
38
read_does_not_invalidate1();
49
read_does_not_invalidate2();
5-
ref_raw_int_raw();
610
mut_raw_then_mut_shr();
711
mut_shr_then_mut_raw();
812
mut_raw_mut();
@@ -12,6 +16,7 @@ fn main() {
1216
two_raw();
1317
shr_and_raw();
1418
disjoint_mutable_subborrows();
19+
raw_ref_to_part();
1520
}
1621

1722
// Make sure that reading from an `&mut` does, like reborrowing to `&`,
@@ -37,16 +42,6 @@ fn read_does_not_invalidate2() {
3742
assert_eq!(*foo(&mut (1, 2)), 2);
3843
}
3944

40-
// Just to make sure that casting a ref to raw, to int and back to raw
41-
// and only then using it works. This rules out ideas like "do escape-to-raw lazily";
42-
// after casting to int and back, we lost the tag that could have let us do that.
43-
fn ref_raw_int_raw() {
44-
let mut x = 3;
45-
let xref = &mut x;
46-
let xraw = xref as *mut i32 as usize as *mut i32;
47-
assert_eq!(unsafe { *xraw }, 3);
48-
}
49-
5045
// Escape a mut to raw, then share the same mut and use the share, then the raw.
5146
// That should work.
5247
fn mut_raw_then_mut_shr() {
@@ -162,3 +157,22 @@ fn disjoint_mutable_subborrows() {
162157
a.push_str(" world");
163158
eprintln!("{:?} {:?}", a, b);
164159
}
160+
161+
fn raw_ref_to_part() {
162+
struct Part {
163+
_lame: i32,
164+
}
165+
166+
#[repr(C)]
167+
struct Whole {
168+
part: Part,
169+
extra: i32,
170+
}
171+
172+
let it = Box::new(Whole { part: Part { _lame: 0 }, extra: 42 });
173+
let whole = ptr::raw_mut!(*Box::leak(it));
174+
let part = unsafe { ptr::raw_mut!((*whole).part) };
175+
let typed = unsafe { &mut *(part as *mut Whole) };
176+
assert!(typed.extra == 42);
177+
drop(unsafe { Box::from_raw(whole) });
178+
}

tests/run-pass/thread-local.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ fn main() {
5858
// Initialize the keys we use to check destructor ordering
5959
for (key, global) in KEYS.iter_mut().zip(GLOBALS.iter_mut()) {
6060
*key = create(Some(mem::transmute(dtor as unsafe extern fn(*mut u64))));
61-
set(*key, global as *const _ as *mut _);
61+
set(*key, global as *mut _ as *mut u8);
6262
}
6363

6464
// Initialize cannary

0 commit comments

Comments
 (0)