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

Commit 7f5cfa5

Browse files
committed
port some tests away from flags we want to remove
1 parent b1b3836 commit 7f5cfa5

9 files changed

+26
-36
lines changed
Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
1-
// compile-flags: -Zmiri-allow-ptr-int-transmute
21
// A callee may not read the destination of our `&mut` without us noticing.
32
// Thise code got carefully checked to not introduce any reborrows
43
// that are not explicit in the source. Let's hope the compiler does not break this later!
54

6-
#![feature(untagged_unions)]
7-
85
use std::mem;
96

7+
union HiddenRef {
8+
// We avoid retagging at this type, so shared vs mutable does not matter.
9+
r: &'static i32,
10+
}
11+
1012
fn main() {
1113
let mut x: i32 = 15;
1214
let xref1 = &mut x;
13-
let xref1_sneaky: usize = unsafe { mem::transmute_copy(&xref1) };
15+
let xref1_sneaky: HiddenRef = unsafe { mem::transmute_copy(&xref1) };
1416
// Derived from `xref1`, so using raw value is still ok, ...
1517
let xref2 = &mut *xref1;
1618
callee(xref1_sneaky);
@@ -19,14 +21,8 @@ fn main() {
1921
//~^ ERROR: borrow stack
2022
}
2123

22-
fn callee(xref1: usize) {
23-
// Transmuting through a union to avoid retagging.
24-
union UsizeToRef {
25-
from: usize,
26-
to: &'static mut i32,
27-
}
28-
let xref1 = UsizeToRef { from: xref1 };
24+
fn callee(xref1: HiddenRef) {
2925
// Doing the deref and the transmute (through the union) in the same place expression
3026
// should avoid retagging.
31-
let _val = unsafe { *xref1.to };
27+
let _val = unsafe { *xref1.r };
3228
}

tests/fail/stacked_borrows/illegal_read3.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ LL | let xref2 = &mut *xref1;
1717
help: <TAG> was later invalidated at offsets [0x0..0x4]
1818
--> $DIR/illegal_read3.rs:LL:CC
1919
|
20-
LL | let _val = unsafe { *xref1.to };
21-
| ^^^^^^^^^
20+
LL | let _val = unsafe { *xref1.r };
21+
| ^^^^^^^^
2222
= note: inside `main` at $DIR/illegal_read3.rs:LL:CC
2323

2424
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace

tests/fail/transmute-pair-uninit.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// compile-flags: -Zmiri-allow-uninit-numbers
21
#![feature(core_intrinsics)]
32

43
use std::mem;
@@ -18,6 +17,6 @@ fn main() {
1817
assert_eq!(byte, 0);
1918
}
2019
let v = unsafe { *z.offset(first_undef) };
20+
//~^ ERROR uninitialized
2121
if v == 0 { println!("it is zero"); }
22-
//~^ ERROR this operation requires initialized memory
2322
}

tests/fail/transmute-pair-uninit.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error: Undefined Behavior: using uninitialized data, but this operation requires initialized memory
1+
error: Undefined Behavior: type validation failed: encountered uninitialized bytes, but expected initialized bytes
22
--> $DIR/transmute-pair-uninit.rs:LL:CC
33
|
4-
LL | if v == 0 { println!("it is zero"); }
5-
| ^^^^^^ using uninitialized data, but this operation requires initialized memory
4+
LL | let v = unsafe { *z.offset(first_undef) };
5+
| ^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered uninitialized bytes, but expected initialized bytes
66
|
77
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
88
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information

tests/fail/uninit_byte_read.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
// compile-flags: -Zmiri-allow-uninit-numbers
21
fn main() {
32
let v: Vec<u8> = Vec::with_capacity(10);
4-
let undef = unsafe { *v.get_unchecked(5) };
5-
let x = undef + 1; //~ ERROR this operation requires initialized memory
3+
let undef = unsafe { *v.get_unchecked(5) }; //~ ERROR uninitialized
4+
let x = undef + 1;
65
panic!("this should never print: {}", x);
76
}

tests/fail/uninit_byte_read.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error: Undefined Behavior: using uninitialized data, but this operation requires initialized memory
1+
error: Undefined Behavior: type validation failed: encountered uninitialized bytes, but expected initialized bytes
22
--> $DIR/uninit_byte_read.rs:LL:CC
33
|
4-
LL | let x = undef + 1;
5-
| ^^^^^^^^^ using uninitialized data, but this operation requires initialized memory
4+
LL | let undef = unsafe { *v.get_unchecked(5) };
5+
| ^^^^^^^^^^^^^^^^^^^ type validation failed: encountered uninitialized bytes, but expected initialized bytes
66
|
77
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
88
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information

tests/fail/validity/invalid_enum_tag_256variants_uninit.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Even when uninit numbers are allowed, this enum is not.
12
// compile-flags: -Zmiri-allow-uninit-numbers
23
#![allow(unused, deprecated, invalid_value)]
34

tests/pass/intptrcast.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
// compile-flags: -Zmiri-allow-ptr-int-transmute
2-
3-
// This returns a miri pointer at type usize, if the argument is a proper pointer
1+
// This strips provenance
42
fn transmute_ptr_to_int<T>(x: *const T) -> usize {
53
unsafe { std::mem::transmute(x) }
64
}
@@ -39,7 +37,7 @@ fn transmute() {
3937
// transmuting.
4038
let a: *const i32 = &42;
4139
let b = transmute_ptr_to_int(a) as u8;
42-
let c = a as usize as u8;
40+
let c = a as u8;
4341
assert_eq!(b, c);
4442
}
4543

tests/pass/transmute_fat.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
// Stacked Borrows disallows this becuase the reference is never cast to a raw pointer.
2-
// compile-flags: -Zmiri-disable-stacked-borrows -Zmiri-allow-ptr-int-transmute
2+
// compile-flags: -Zmiri-disable-stacked-borrows
33

44
fn main() {
55
// If we are careful, we can exploit data layout...
66
let raw = unsafe {
7-
std::mem::transmute::<&[u8], [usize; 2]>(&[42])
7+
std::mem::transmute::<&[u8], [*const u8; 2]>(&[42])
88
};
9-
let ptr = raw[0] + raw[1];
10-
// We transmute both ways, to really test allow-ptr-int-transmute.
11-
let ptr: *const u8 = unsafe { std::mem::transmute(ptr) };
12-
// The pointer is one-past-the end, but we decrement it into bounds before using it
13-
assert_eq!(unsafe { *ptr.offset(-1) }, 42);
9+
let ptr: *const u8 = unsafe { std::mem::transmute_copy(&raw) };
10+
assert_eq!(unsafe { *ptr }, 42);
1411
}

0 commit comments

Comments
 (0)