Skip to content

Commit 3361eab

Browse files
committed
Auto merge of #2183 - RalfJung:better-provenance-control, r=RalfJung
adjust for better provenance control This is the Miri side of rust-lang/rust#97684.
2 parents cd73c86 + 84edb76 commit 3361eab

29 files changed

+138
-110
lines changed

rust-version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
4e725bad73747a4c93a3ac53106e4b4006edc665
1+
9d20fd109809f20c049d6895a5be27a1fbd39daa

rustup-toolchain

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ else
3030
NEW_COMMIT="$1"
3131
fi
3232
echo "$NEW_COMMIT" > rust-version
33-
shift
33+
shift || true # don't fail if shifting fails
3434

3535
# Check if we already are at that commit.
3636
CUR_COMMIT=$(rustc +miri --version -v 2>/dev/null | egrep "^commit-hash: " | cut -d " " -f 2)

src/diagnostics.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,9 +229,16 @@ pub fn report_error<'tcx, 'mir>(
229229
};
230230
#[rustfmt::skip]
231231
let helps = match e.kind() {
232-
Unsupported(UnsupportedOpInfo::ThreadLocalStatic(_) | UnsupportedOpInfo::ReadExternStatic(_)) =>
232+
Unsupported(
233+
UnsupportedOpInfo::ThreadLocalStatic(_) |
234+
UnsupportedOpInfo::ReadExternStatic(_)
235+
) =>
233236
panic!("Error should never be raised by Miri: {:?}", e.kind()),
234-
Unsupported(_) =>
237+
Unsupported(
238+
UnsupportedOpInfo::Unsupported(_) |
239+
UnsupportedOpInfo::PartialPointerOverwrite(_) |
240+
UnsupportedOpInfo::ReadPointerAsBytes
241+
) =>
235242
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"))],
236243
UndefinedBehavior(UndefinedBehaviorInfo::AlignmentCheckFailed { .. })
237244
if ecx.machine.check_alignment == AlignmentCheck::Symbolic
@@ -245,7 +252,8 @@ pub fn report_error<'tcx, 'mir>(
245252
(None, format!("this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior")),
246253
(None, format!("see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information")),
247254
],
248-
_ => vec![],
255+
InvalidProgram(_) | ResourceExhaustion(_) | MachineStop(_) =>
256+
vec![],
249257
};
250258
(Some(title), helps)
251259
}

src/helpers.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -681,7 +681,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
681681
// FIXME: We are re-getting the allocation each time around the loop.
682682
// Would be nice if we could somehow "extend" an existing AllocRange.
683683
let alloc = this.get_ptr_alloc(ptr.offset(len, this)?, size1, Align::ONE)?.unwrap(); // not a ZST, so we will get a result
684-
let byte = alloc.read_scalar(alloc_range(Size::ZERO, size1))?.to_u8()?;
684+
let byte = alloc.read_integer(Size::ZERO, size1)?.to_u8()?;
685685
if byte == 0 {
686686
break;
687687
} else {
@@ -703,7 +703,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
703703
// FIXME: We are re-getting the allocation each time around the loop.
704704
// Would be nice if we could somehow "extend" an existing AllocRange.
705705
let alloc = this.get_ptr_alloc(ptr, size2, align2)?.unwrap(); // not a ZST, so we will get a result
706-
let wchar = alloc.read_scalar(alloc_range(Size::ZERO, size2))?.to_u16()?;
706+
let wchar = alloc.read_integer(Size::ZERO, size2)?.to_u16()?;
707707
if wchar == 0 {
708708
break;
709709
} else {

tests/fail/branchless-select-i128-pointer.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ fn main() {
99
for &my_bool in &[true, false] {
1010
let mask = -(my_bool as TwoPtrs); // false -> 0, true -> -1 aka !0
1111
// This is branchless code to select one or the other pointer.
12-
// For now, Miri brafs on it, but if this code ever passes we better make sure it behaves correctly.
12+
// However, it drops provenance when transmuting to TwoPtrs, so this is UB.
1313
let val = unsafe {
14-
transmute::<_, &str>(
15-
!mask & transmute::<_, TwoPtrs>("false !") | mask & transmute::<_, TwoPtrs>("true !"), //~ERROR encountered (potentially part of) a pointer, but expected plain (non-pointer) bytes
14+
transmute::<_, &str>( //~ERROR type validation failed: encountered a dangling reference
15+
!mask & transmute::<_, TwoPtrs>("false !") | mask & transmute::<_, TwoPtrs>("true !"),
1616
)
1717
};
1818
println!("{}", val);

tests/fail/branchless-select-i128-pointer.stderr

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
error: Undefined Behavior: type validation failed: encountered (potentially part of) a pointer, but expected plain (non-pointer) bytes
1+
error: Undefined Behavior: type validation failed: encountered a dangling reference (address $HEX is unallocated)
22
--> $DIR/branchless-select-i128-pointer.rs:LL:CC
33
|
4-
LL | !mask & transmute::<_, TwoPtrs>("false !") | mask & transmute::<_, TwoPtrs>("true !"),
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered (potentially part of) a pointer, but expected plain (non-pointer) bytes
4+
LL | / transmute::<_, &str>(
5+
LL | | !mask & transmute::<_, TwoPtrs>("false !") | mask & transmute::<_, TwoPtrs>("true !"),
6+
LL | | )
7+
| |_____________^ type validation failed: encountered a dangling reference (address $HEX is unallocated)
68
|
79
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
810
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information

tests/fail/pointer_partial_overwrite.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// compile-flags: -Zmiri-disable-alignment-check -Zmiri-disable-stacked-borrows -Zmiri-disable-validation
33

44
// Test what happens when we overwrite parts of a pointer.
5-
// Also see <https://github.com/rust-lang/rust/issues/87184>.
5+
// Also see <https://github.com/rust-lang/miri/issues/2181>.
66

77
fn main() {
88
let mut p = &42;

tests/fail/pointer_partial_read.rs

Lines changed: 0 additions & 9 deletions
This file was deleted.

tests/fail/pointer_partial_read.stderr

Lines changed: 0 additions & 14 deletions
This file was deleted.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// compile-flags: -Zmiri-permissive-provenance -Zmiri-disable-stacked-borrows
2+
#![feature(strict_provenance)]
3+
4+
use std::mem;
5+
6+
// This is the example from
7+
// <https://github.com/rust-lang/unsafe-code-guidelines/issues/286#issuecomment-1085144431>.
8+
9+
unsafe fn deref(left: *const u8, right: *const u8) {
10+
let left_int: usize = mem::transmute(left);
11+
let right_int: usize = mem::transmute(right);
12+
if left_int == right_int {
13+
// The compiler is allowed to replace `left_int` by `right_int` here...
14+
let left_ptr: *const u8 = mem::transmute(left_int);
15+
// ...which however means here it could be dereferencing the wrong pointer.
16+
let _val = *left_ptr; //~ERROR dereferencing pointer failed
17+
}
18+
}
19+
20+
fn main() {
21+
let ptr1 = &0u8 as *const u8;
22+
let ptr2 = &1u8 as *const u8;
23+
unsafe {
24+
// Two pointers with the same address but different provenance.
25+
deref(ptr1, ptr2.with_addr(ptr1.addr()));
26+
}
27+
}

0 commit comments

Comments
 (0)