Skip to content

Commit 0da5f76

Browse files
committed
Auto merge of #2516 - RalfJung:read-pointer-as-bytes, r=RalfJung
Adjust for supporting more implicit ptr-to-int transmutation This is the Miri side of rust-lang/rust#101101. Fixes #2456.
2 parents 95b315d + 0113f9e commit 0da5f76

32 files changed

+159
-103
lines changed

rust-version

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
94b2b15e63c5d2b2a6a0910e3dae554ce9415bf9
1+
4fd4de7ea358ad6fc28c5780533ea8ccc09e1006

src/diagnostics.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -226,13 +226,14 @@ pub fn report_error<'tcx, 'mir>(
226226
let helps = match e.kind() {
227227
Unsupported(
228228
UnsupportedOpInfo::ThreadLocalStatic(_) |
229-
UnsupportedOpInfo::ReadExternStatic(_)
229+
UnsupportedOpInfo::ReadExternStatic(_) |
230+
UnsupportedOpInfo::PartialPointerOverwrite(_) | // we make memory uninit instead
231+
UnsupportedOpInfo::ReadPointerAsBytes
230232
) =>
231233
panic!("Error should never be raised by Miri: {kind:?}", kind = e.kind()),
232234
Unsupported(
233235
UnsupportedOpInfo::Unsupported(_) |
234-
UnsupportedOpInfo::PartialPointerOverwrite(_) |
235-
UnsupportedOpInfo::ReadPointerAsBytes
236+
UnsupportedOpInfo::PartialPointerCopy(_)
236237
) =>
237238
vec![(None, format!("this is likely not a bug in the program; it indicates that the program performed an operation that the interpreter does not support"))],
238239
UndefinedBehavior(UndefinedBehaviorInfo::AlignmentCheckFailed { .. })

src/helpers.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -757,7 +757,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
757757
}
758758

759759
// Step 2: get the bytes.
760-
this.read_bytes_ptr(ptr, len)
760+
this.read_bytes_ptr_strip_provenance(ptr, len)
761761
}
762762

763763
fn read_wide_str(&self, mut ptr: Pointer<Option<Provenance>>) -> InterpResult<'tcx, Vec<u16>> {

src/machine.rs

+15
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,21 @@ impl interpret::Provenance for Provenance {
205205
Provenance::Wildcard => None,
206206
}
207207
}
208+
209+
fn join(left: Option<Self>, right: Option<Self>) -> Option<Self> {
210+
match (left, right) {
211+
// If both are the *same* concrete tag, that is the result.
212+
(
213+
Some(Provenance::Concrete { alloc_id: left_alloc, sb: left_sb }),
214+
Some(Provenance::Concrete { alloc_id: right_alloc, sb: right_sb }),
215+
) if left_alloc == right_alloc && left_sb == right_sb => left,
216+
// If one side is a wildcard, the best possible outcome is that it is equal to the other
217+
// one, and we use that.
218+
(Some(Provenance::Wildcard), o) | (o, Some(Provenance::Wildcard)) => o,
219+
// Otherwise, fall back to `None`.
220+
_ => None,
221+
}
222+
}
208223
}
209224

210225
impl fmt::Debug for ProvenanceExtra {

src/shims/foreign_items.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -560,8 +560,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
560560
let n = Size::from_bytes(this.read_scalar(n)?.to_machine_usize(this)?);
561561

562562
let result = {
563-
let left_bytes = this.read_bytes_ptr(left, n)?;
564-
let right_bytes = this.read_bytes_ptr(right, n)?;
563+
let left_bytes = this.read_bytes_ptr_strip_provenance(left, n)?;
564+
let right_bytes = this.read_bytes_ptr_strip_provenance(right, n)?;
565565

566566
use std::cmp::Ordering::*;
567567
match left_bytes.cmp(right_bytes) {
@@ -583,7 +583,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
583583
let val = val as u8;
584584

585585
if let Some(idx) = this
586-
.read_bytes_ptr(ptr, Size::from_bytes(num))?
586+
.read_bytes_ptr_strip_provenance(ptr, Size::from_bytes(num))?
587587
.iter()
588588
.rev()
589589
.position(|&c| c == val)
@@ -606,7 +606,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
606606
let val = val as u8;
607607

608608
let idx = this
609-
.read_bytes_ptr(ptr, Size::from_bytes(num))?
609+
.read_bytes_ptr_strip_provenance(ptr, Size::from_bytes(num))?
610610
.iter()
611611
.position(|&c| c == val);
612612
if let Some(idx) = idx {

src/shims/unix/fs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -761,7 +761,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
761761
let communicate = this.machine.communicate();
762762

763763
if let Some(file_descriptor) = this.machine.file_handler.handles.get(&fd) {
764-
let bytes = this.read_bytes_ptr(buf, Size::from_bytes(count))?;
764+
let bytes = this.read_bytes_ptr_strip_provenance(buf, Size::from_bytes(count))?;
765765
let result =
766766
file_descriptor.write(communicate, bytes)?.map(|c| i64::try_from(c).unwrap());
767767
this.try_unwrap_io_result(result)

src/shims/windows/dlsym.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
7878
// stdout/stderr
7979
use std::io::{self, Write};
8080

81-
let buf_cont = this.read_bytes_ptr(buf, Size::from_bytes(u64::from(n)))?;
81+
let buf_cont =
82+
this.read_bytes_ptr_strip_provenance(buf, Size::from_bytes(u64::from(n)))?;
8283
let res = if this.machine.mute_stdout_stderr {
8384
Ok(buf_cont.len())
8485
} else if handle == -11 {

tests/fail/copy_half_a_pointer.rs

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//@normalize-stderr-test: "\+0x[48]" -> "+HALF_PTR"
2+
#![allow(dead_code)]
3+
4+
// We use packed structs to get around alignment restrictions
5+
#[repr(packed)]
6+
struct Data {
7+
pad: u8,
8+
ptr: &'static i32,
9+
}
10+
11+
static G: i32 = 0;
12+
13+
fn main() {
14+
let mut d = Data { pad: 0, ptr: &G };
15+
16+
// Get a pointer to the beginning of the Data struct (one u8 byte, then the pointer bytes).
17+
let d_alias = &mut d as *mut _ as *mut *const u8;
18+
unsafe {
19+
let _x = d_alias.read_unaligned(); //~ERROR: unable to copy parts of a pointer
20+
}
21+
}

tests/fail/copy_half_a_pointer.stderr

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error: unsupported operation: unable to copy parts of a pointer from memory at ALLOC+HALF_PTR
2+
--> $DIR/copy_half_a_pointer.rs:LL:CC
3+
|
4+
LL | let _x = d_alias.read_unaligned();
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^ unable to copy parts of a pointer from memory at ALLOC+HALF_PTR
6+
|
7+
= help: this is likely not a bug in the program; it indicates that the program performed an operation that the interpreter does not support
8+
= note: BACKTRACE:
9+
= note: inside `main` at $DIR/copy_half_a_pointer.rs:LL:CC
10+
11+
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
12+
13+
error: aborting due to previous error
14+

tests/fail/intrinsics/raw_eq_on_ptr.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,5 @@ extern "rust-intrinsic" {
66

77
fn main() {
88
let x = &0;
9-
// FIXME: the error message is not great (should be UB rather than 'unsupported')
10-
unsafe { raw_eq(&x, &x) }; //~ERROR: unsupported operation
9+
unsafe { raw_eq(&x, &x) }; //~ERROR: `raw_eq` on bytes with provenance
1110
}

tests/fail/intrinsics/raw_eq_on_ptr.stderr

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
error: unsupported operation: unable to turn pointer into raw bytes
1+
error: Undefined Behavior: `raw_eq` on bytes with provenance
22
--> $DIR/raw_eq_on_ptr.rs:LL:CC
33
|
44
LL | unsafe { raw_eq(&x, &x) };
5-
| ^^^^^^^^^^^^^^ unable to turn pointer into raw bytes
5+
| ^^^^^^^^^^^^^^ `raw_eq` on bytes with provenance
66
|
7-
= help: this is likely not a bug in the program; it indicates that the program performed an operation that the interpreter does not support
7+
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
8+
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
89
= note: BACKTRACE:
910
= note: inside `main` at $DIR/raw_eq_on_ptr.rs:LL:CC
1011

tests/fail/invalid_int.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#![allow(invalid_value)]
12
// Validation makes this fail in the wrong place
23
// Make sure we find these even with many checks disabled.
34
//@compile-flags: -Zmiri-disable-alignment-check -Zmiri-disable-stacked-borrows -Zmiri-disable-validation

tests/fail/reading_half_a_pointer.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ fn main() {
2424
// starts 1 byte to the right, so using it would actually be wrong!
2525
let d_alias = &mut w.data as *mut _ as *mut *const u8;
2626
unsafe {
27-
let _x = *d_alias; //~ ERROR: unable to turn pointer into raw bytes
27+
let x = *d_alias;
28+
let _val = *x; //~ERROR: is a dangling pointer (it has no provenance)
2829
}
2930
}

tests/fail/reading_half_a_pointer.stderr

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
error: unsupported operation: unable to turn pointer into raw bytes
1+
error: Undefined Behavior: dereferencing pointer failed: $HEX[noalloc] is a dangling pointer (it has no provenance)
22
--> $DIR/reading_half_a_pointer.rs:LL:CC
33
|
4-
LL | let _x = *d_alias;
5-
| ^^^^^^^^ unable to turn pointer into raw bytes
4+
LL | let _val = *x;
5+
| ^^ dereferencing pointer failed: $HEX[noalloc] is a dangling pointer (it has no provenance)
66
|
7-
= help: this is likely not a bug in the program; it indicates that the program performed an operation that the interpreter does not support
7+
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
8+
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
89
= note: BACKTRACE:
910
= note: inside `main` at $DIR/reading_half_a_pointer.rs:LL:CC
1011

tests/fail/transmute_fat1.rs

-13
This file was deleted.

tests/fail/transmute_fat1.stderr

-15
This file was deleted.

tests/fail/validity/invalid_bool_uninit.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
union MyUninit {
44
init: (),
5-
uninit: bool,
5+
uninit: [bool; 1],
66
}
77

88
fn main() {
9-
let _b = unsafe { MyUninit { init: () }.uninit }; //~ ERROR: uninitialized
9+
let _b = unsafe { MyUninit { init: () }.uninit }; //~ ERROR: constructing invalid value
1010
}

tests/fail/validity/invalid_bool_uninit.stderr

+2-2
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: constructing invalid value at [0]: encountered uninitialized memory, but expected a boolean
22
--> $DIR/invalid_bool_uninit.rs:LL:CC
33
|
44
LL | let _b = unsafe { MyUninit { init: () }.uninit };
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ using uninitialized data, but this operation requires initialized memory
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at [0]: encountered uninitialized memory, but expected a boolean
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_char_uninit.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
union MyUninit {
44
init: (),
5-
uninit: char,
5+
uninit: [char; 1],
66
}
77

88
fn main() {
9-
let _b = unsafe { MyUninit { init: () }.uninit }; //~ ERROR: uninitialized
9+
let _b = unsafe { MyUninit { init: () }.uninit }; //~ ERROR: constructing invalid value
1010
}

tests/fail/validity/invalid_char_uninit.stderr

+2-2
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: constructing invalid value at [0]: encountered uninitialized memory, but expected a unicode scalar value
22
--> $DIR/invalid_char_uninit.rs:LL:CC
33
|
44
LL | let _b = unsafe { MyUninit { init: () }.uninit };
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ using uninitialized data, but this operation requires initialized memory
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at [0]: encountered uninitialized memory, but expected a unicode scalar value
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_fnptr_uninit.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
union MyUninit {
44
init: (),
5-
uninit: fn(),
5+
uninit: [fn(); 1],
66
}
77

88
fn main() {
9-
let _b = unsafe { MyUninit { init: () }.uninit }; //~ ERROR: uninitialized
9+
let _b = unsafe { MyUninit { init: () }.uninit }; //~ ERROR: constructing invalid value
1010
}

tests/fail/validity/invalid_fnptr_uninit.stderr

+2-2
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: constructing invalid value at [0]: encountered uninitialized memory, but expected a function pointer
22
--> $DIR/invalid_fnptr_uninit.rs:LL:CC
33
|
44
LL | let _b = unsafe { MyUninit { init: () }.uninit };
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ using uninitialized data, but this operation requires initialized memory
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at [0]: encountered uninitialized memory, but expected a function pointer
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/ptr_integer_array_transmute.rs

-4
This file was deleted.

tests/fail/validity/ptr_integer_array_transmute.stderr

-15
This file was deleted.

tests/fail/validity/uninit_float.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
#![allow(deprecated)]
1+
#![allow(deprecated, invalid_value)]
22
// This test is adapted from https://github.com/rust-lang/miri/issues/1340#issue-600900312.
33

44
fn main() {
55
// Deliberately using `mem::uninitialized` to make sure that despite all the mitigations, we consider this UB.
6-
let _val: f32 = unsafe { std::mem::uninitialized() };
6+
// The array avoids a `Scalar` layout which detects uninit without even doing validation.
7+
let _val: [f32; 1] = unsafe { std::mem::uninitialized() };
78
//~^ ERROR: uninitialized
89
}

tests/fail/validity/uninit_float.stderr

+3-3
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: constructing invalid value at .value[0]: encountered uninitialized bytes
22
--> $DIR/uninit_float.rs:LL:CC
33
|
4-
LL | let _val: f32 = unsafe { std::mem::uninitialized() };
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^ using uninitialized data, but this operation requires initialized memory
4+
LL | let _val: [f32; 1] = unsafe { std::mem::uninitialized() };
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at .value[0]: encountered uninitialized 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/uninit_integer.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
#![allow(invalid_value)]
12
// This test is from https://github.com/rust-lang/miri/issues/1340#issue-600900312.
23

34
fn main() {
4-
let _val = unsafe { std::mem::MaybeUninit::<usize>::uninit().assume_init() };
5+
// The array avoids a `Scalar` layout which detects uninit without even doing validation.
6+
let _val = unsafe { std::mem::MaybeUninit::<[usize; 1]>::uninit().assume_init() };
57
//~^ ERROR: uninitialized
68
}

tests/fail/validity/uninit_integer.stderr

+3-3
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: constructing invalid value at .value[0]: encountered uninitialized bytes
22
--> $DIR/uninit_integer.rs:LL:CC
33
|
4-
LL | let _val = unsafe { std::mem::MaybeUninit::<usize>::uninit().assume_init() };
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ using uninitialized data, but this operation requires initialized memory
4+
LL | let _val = unsafe { std::mem::MaybeUninit::<[usize; 1]>::uninit().assume_init() };
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at .value[0]: encountered uninitialized 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/uninit_raw_ptr.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
#![allow(invalid_value)]
2+
13
fn main() {
2-
let _val = unsafe { std::mem::MaybeUninit::<*const u8>::uninit().assume_init() };
4+
// The array avoids a `Scalar` layout which detects uninit without even doing validation.
5+
let _val = unsafe { std::mem::MaybeUninit::<[*const u8; 1]>::uninit().assume_init() };
36
//~^ ERROR: uninitialized
47
}

tests/fail/validity/uninit_raw_ptr.stderr

+3-3
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: constructing invalid value at .value[0]: encountered uninitialized memory, but expected a raw pointer
22
--> $DIR/uninit_raw_ptr.rs:LL:CC
33
|
4-
LL | let _val = unsafe { std::mem::MaybeUninit::<*const u8>::uninit().assume_init() };
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ using uninitialized data, but this operation requires initialized memory
4+
LL | let _val = unsafe { std::mem::MaybeUninit::<[*const u8; 1]>::uninit().assume_init() };
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at .value[0]: encountered uninitialized memory, but expected a raw pointer
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/pass/transmute_fat.rs

-10
This file was deleted.

0 commit comments

Comments
 (0)