-
Notifications
You must be signed in to change notification settings - Fork 352
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
19 changed files
with
126 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
94b2b15e63c5d2b2a6a0910e3dae554ce9415bf9 | ||
0631ea5d73f4a3199c776687b12c20c50a91f0d2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#![allow(dead_code)] | ||
|
||
// We use packed structs to get around alignment restrictions | ||
#[repr(packed)] | ||
struct Data { | ||
pad: u8, | ||
ptr: &'static i32, | ||
} | ||
|
||
static G: i32 = 0; | ||
|
||
fn main() { | ||
let mut d = Data { pad: 0, ptr: &G }; | ||
|
||
// Get a pointer to the beginning of the Data struct (one u8 byte, then the pointer bytes). | ||
let d_alias = &mut d as *mut _ as *mut *const u8; | ||
unsafe { | ||
let _x = d_alias.read_unaligned(); //~ERROR: unable to copy parts of a pointer | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
error: unsupported operation: unable to copy parts of a pointer from memory at ALLOC+0x8 | ||
--> $DIR/copy_half_a_pointer.rs:LL:CC | ||
| | ||
LL | let _x = d_alias.read_unaligned(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^ unable to copy parts of a pointer from memory at ALLOC+0x8 | ||
| | ||
= help: this is likely not a bug in the program; it indicates that the program performed an operation that the interpreter does not support | ||
= note: backtrace: | ||
= note: inside `main` at $DIR/copy_half_a_pointer.rs:LL:CC | ||
|
||
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace | ||
|
||
error: aborting due to previous error | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#![feature(strict_provenance)] | ||
use std::{mem, ptr}; | ||
|
||
fn t1() { | ||
// If we are careful, we can exploit data layout... | ||
// This is a tricky case since we are transmuting a ScalarPair type to a non-ScalarPair type. | ||
let raw = unsafe { mem::transmute::<&[u8], [*const u8; 2]>(&[42]) }; | ||
let ptr: *const u8 = unsafe { mem::transmute_copy(&raw) }; | ||
assert_eq!(unsafe { *ptr }, 42); | ||
} | ||
|
||
#[cfg(target_pointer_width = "64")] | ||
const PTR_SIZE: usize = 8; | ||
#[cfg(target_pointer_width = "32")] | ||
const PTR_SIZE: usize = 4; | ||
|
||
fn t2() { | ||
let bad = unsafe { mem::transmute::<&[u8], [u8; 2 * PTR_SIZE]>(&[1u8]) }; | ||
let _val = bad[0] + bad[bad.len() - 1]; | ||
} | ||
|
||
fn ptr_integer_array() { | ||
let r = &mut 42; | ||
let _i: [usize; 1] = unsafe { mem::transmute(r) }; | ||
|
||
let _x: [u8; PTR_SIZE] = unsafe { mem::transmute(&0) }; | ||
} | ||
|
||
fn ptr_in_two_halves() { | ||
unsafe { | ||
let ptr = &0 as *const i32; | ||
let arr = [ptr; 2]; | ||
// We want to do a scalar read of a pointer at offset PTR_SIZE/2 into this array. But we | ||
// cannot use a packed struct or `read_unaligned`, as those use the memcpy code path in | ||
// Miri. So instead we shift the entire array by a bit and then the actual read we want to | ||
// do is perfectly aligned. | ||
let mut target_arr = [ptr::null::<i32>(); 3]; | ||
let target = target_arr.as_mut_ptr().cast::<u8>(); | ||
target.add(PTR_SIZE / 2).cast::<[*const i32; 2]>().write_unaligned(arr); | ||
// Now target_arr[1] is a mix of the two `ptr` we had stored in `arr`. | ||
let strange_ptr = target_arr[1]; | ||
// Check that the provenance works out. | ||
assert_eq!(*strange_ptr.with_addr(ptr.addr()), 0); | ||
} | ||
} | ||
|
||
fn main() { | ||
t1(); | ||
t2(); | ||
ptr_integer_array(); | ||
ptr_in_two_halves(); | ||
} |