Skip to content

Commit 872bea4

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 da45adc + d936f3a commit 872bea4

19 files changed

+126
-78
lines changed

rust-version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
94b2b15e63c5d2b2a6a0910e3dae554ce9415bf9
1+
0631ea5d73f4a3199c776687b12c20c50a91f0d2

src/diagnostics.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -224,13 +224,14 @@ pub fn report_error<'tcx, 'mir>(
224224
let helps = match e.kind() {
225225
Unsupported(
226226
UnsupportedOpInfo::ThreadLocalStatic(_) |
227-
UnsupportedOpInfo::ReadExternStatic(_)
227+
UnsupportedOpInfo::ReadExternStatic(_) |
228+
UnsupportedOpInfo::PartialPointerOverwrite(_) | // we make memory uninit instead
229+
UnsupportedOpInfo::ReadPointerAsBytes
228230
) =>
229231
panic!("Error should never be raised by Miri: {kind:?}", kind = e.kind()),
230232
Unsupported(
231233
UnsupportedOpInfo::Unsupported(_) |
232-
UnsupportedOpInfo::PartialPointerOverwrite(_) |
233-
UnsupportedOpInfo::ReadPointerAsBytes
234+
UnsupportedOpInfo::PartialPointerCopy(_)
234235
) =>
235236
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"))],
236237
UndefinedBehavior(UndefinedBehaviorInfo::AlignmentCheckFailed { .. })

src/helpers.rs

Lines changed: 1 addition & 1 deletion
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

Lines changed: 15 additions & 0 deletions
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

Lines changed: 4 additions & 4 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 1 deletion
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

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

tests/fail/copy_half_a_pointer.stderr

Lines changed: 14 additions & 0 deletions
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+0x8
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+0x8
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

Lines changed: 1 addition & 2 deletions
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
}

0 commit comments

Comments
 (0)