Skip to content

Commit 880ec81

Browse files
authored
Rollup merge of #143327 - RalfJung:miri-type-validity-error, r=oli-obk
miri: improve errors for type validity assertion failures Miri has pretty nice errors for type validity violations, printing which field in the type the problem occurs at and so on. However, we don't see these errors when using e.g. `mem::zeroed` as that uses `assert_zero_valid` to bail out before Miri can detect the UB. Similar to what we did with `@saethlin's` UB checks, I think we should disable such language UB checks in Miri so that we can get better error messages. If we go for this we should probably say this in the intrinsic docs as well so that people don't think they can rely on these intrinsics catching anything. Furthermore, I slightly changed `MaybeUninit::assume_init` so that the `.value` field does not show up in error messages any more. `@rust-lang/miri` what do you think?
2 parents 5f4c7df + 80adf4d commit 880ec81

10 files changed

+27
-62
lines changed

src/intrinsics/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,10 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
457457
throw_machine_stop!(TerminationInfo::Abort(format!("trace/breakpoint trap")))
458458
}
459459

460+
"assert_inhabited" | "assert_zero_valid" | "assert_mem_uninitialized_valid" => {
461+
// Make these a NOP, so we get the better Miri-native error messages.
462+
}
463+
460464
_ => return interp_ok(EmulateItemResult::NotSupported),
461465
}
462466

tests/fail-dep/libc/libc-read-and-uninit-premature-eof.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ fn main() {
2020
let mut buf: MaybeUninit<[u8; 4]> = std::mem::MaybeUninit::uninit();
2121
// Read 4 bytes from a 3-byte file.
2222
assert_eq!(libc::read(fd, buf.as_mut_ptr().cast::<std::ffi::c_void>(), 4), 3);
23-
buf.assume_init(); //~ERROR: Undefined Behavior: constructing invalid value at .value[3]: encountered uninitialized memory, but expected an integer
23+
buf.assume_init(); //~ERROR: encountered uninitialized memory, but expected an integer
2424
assert_eq!(libc::close(fd), 0);
2525
}
2626
remove_file(&path).unwrap();

tests/fail-dep/libc/libc-read-and-uninit-premature-eof.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error: Undefined Behavior: constructing invalid value at .value[3]: encountered uninitialized memory, but expected an integer
1+
error: Undefined Behavior: constructing invalid value at [3]: encountered uninitialized memory, but expected an integer
22
--> tests/fail-dep/libc/libc-read-and-uninit-premature-eof.rs:LL:CC
33
|
4-
LL | ... buf.assume_init();
5-
| ^^^^^^^^^^^^^^^^^ Undefined Behavior occurred here
4+
LL | buf.assume_init();
5+
| ^^^^^^^^^^^^^^^^^ Undefined Behavior occurred here
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
Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
1-
//@normalize-stderr-test: "\|.*::abort\(\).*" -> "| ABORT()"
2-
//@normalize-stderr-test: "\| +\^+" -> "| ^"
3-
//@normalize-stderr-test: "\n +[0-9]+:[^\n]+" -> ""
4-
//@normalize-stderr-test: "\n +at [^\n]+" -> ""
5-
//@error-in-other-file: aborted execution
61
#![feature(never_type)]
72

83
#[allow(deprecated, invalid_value)]
94
fn main() {
10-
let _ = unsafe { std::mem::uninitialized::<!>() };
5+
let _ = unsafe { std::mem::uninitialized::<!>() }; //~ERROR: constructing invalid value
116
}

tests/fail/intrinsics/uninit_uninhabited_type.stderr

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,13 @@
1-
2-
thread 'main' panicked at RUSTLIB/core/src/panicking.rs:LL:CC:
3-
aborted execution: attempted to instantiate uninhabited type `!`
4-
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
5-
note: in Miri, you may have to set `MIRIFLAGS=-Zmiri-env-forward=RUST_BACKTRACE` for the environment variable to have an effect
6-
thread caused non-unwinding panic. aborting.
7-
error: abnormal termination: the program aborted execution
8-
--> RUSTLIB/std/src/sys/pal/PLATFORM/mod.rs:LL:CC
9-
|
10-
LL | ABORT()
11-
| ^ abnormal termination occurred here
12-
|
13-
= note: BACKTRACE:
14-
= note: inside `std::sys::pal::PLATFORM::abort_internal` at RUSTLIB/std/src/sys/pal/PLATFORM/mod.rs:LL:CC
15-
= note: inside `std::panicking::rust_panic_with_hook` at RUSTLIB/std/src/panicking.rs:LL:CC
16-
= note: inside closure at RUSTLIB/std/src/panicking.rs:LL:CC
17-
= note: inside `std::sys::backtrace::__rust_end_short_backtrace::<{closure@std::panicking::begin_panic_handler::{closure#0}}, !>` at RUSTLIB/std/src/sys/backtrace.rs:LL:CC
18-
= note: inside `std::panicking::begin_panic_handler` at RUSTLIB/std/src/panicking.rs:LL:CC
19-
= note: inside `core::panicking::panic_nounwind` at RUSTLIB/core/src/panicking.rs:LL:CC
20-
note: inside `main`
1+
error: Undefined Behavior: constructing invalid value: encountered a value of the never type `!`
212
--> tests/fail/intrinsics/uninit_uninhabited_type.rs:LL:CC
223
|
234
LL | let _ = unsafe { std::mem::uninitialized::<!>() };
24-
| ^
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Undefined Behavior occurred here
6+
|
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
9+
= note: BACKTRACE:
10+
= note: inside `main` at tests/fail/intrinsics/uninit_uninhabited_type.rs:LL:CC
2511

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

tests/fail/intrinsics/zero_fn_ptr.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
1-
//@normalize-stderr-test: "\|.*::abort\(\).*" -> "| ABORT()"
2-
//@normalize-stderr-test: "\| +\^+" -> "| ^"
3-
//@normalize-stderr-test: "\n +[0-9]+:[^\n]+" -> ""
4-
//@normalize-stderr-test: "\n +at [^\n]+" -> ""
5-
//@error-in-other-file: aborted execution
6-
71
#[allow(deprecated, invalid_value)]
82
fn main() {
9-
let _ = unsafe { std::mem::zeroed::<fn()>() };
3+
let _ = unsafe { std::mem::zeroed::<fn()>() }; //~ERROR: constructing invalid value
104
}

tests/fail/intrinsics/zero_fn_ptr.stderr

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,13 @@
1-
2-
thread 'main' panicked at RUSTLIB/core/src/panicking.rs:LL:CC:
3-
aborted execution: attempted to zero-initialize type `fn()`, which is invalid
4-
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
5-
note: in Miri, you may have to set `MIRIFLAGS=-Zmiri-env-forward=RUST_BACKTRACE` for the environment variable to have an effect
6-
thread caused non-unwinding panic. aborting.
7-
error: abnormal termination: the program aborted execution
8-
--> RUSTLIB/std/src/sys/pal/PLATFORM/mod.rs:LL:CC
9-
|
10-
LL | ABORT()
11-
| ^ abnormal termination occurred here
12-
|
13-
= note: BACKTRACE:
14-
= note: inside `std::sys::pal::PLATFORM::abort_internal` at RUSTLIB/std/src/sys/pal/PLATFORM/mod.rs:LL:CC
15-
= note: inside `std::panicking::rust_panic_with_hook` at RUSTLIB/std/src/panicking.rs:LL:CC
16-
= note: inside closure at RUSTLIB/std/src/panicking.rs:LL:CC
17-
= note: inside `std::sys::backtrace::__rust_end_short_backtrace::<{closure@std::panicking::begin_panic_handler::{closure#0}}, !>` at RUSTLIB/std/src/sys/backtrace.rs:LL:CC
18-
= note: inside `std::panicking::begin_panic_handler` at RUSTLIB/std/src/panicking.rs:LL:CC
19-
= note: inside `core::panicking::panic_nounwind` at RUSTLIB/core/src/panicking.rs:LL:CC
20-
note: inside `main`
1+
error: Undefined Behavior: constructing invalid value: encountered a null function pointer
212
--> tests/fail/intrinsics/zero_fn_ptr.rs:LL:CC
223
|
234
LL | let _ = unsafe { std::mem::zeroed::<fn()>() };
24-
| ^
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ Undefined Behavior occurred here
6+
|
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
9+
= note: BACKTRACE:
10+
= note: inside `main` at tests/fail/intrinsics/zero_fn_ptr.rs:LL:CC
2511

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

tests/fail/validity/uninit_float.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: Undefined Behavior: constructing invalid value at .value[0]: encountered uninitialized memory, but expected a floating point number
1+
error: Undefined Behavior: constructing invalid value at [0]: encountered uninitialized memory, but expected a floating point number
22
--> tests/fail/validity/uninit_float.rs:LL:CC
33
|
44
LL | let _val: [f32; 1] = unsafe { std::mem::uninitialized() };

tests/fail/validity/uninit_integer.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: Undefined Behavior: constructing invalid value at .value[0]: encountered uninitialized memory, but expected an integer
1+
error: Undefined Behavior: constructing invalid value at [0]: encountered uninitialized memory, but expected an integer
22
--> tests/fail/validity/uninit_integer.rs:LL:CC
33
|
44
LL | let _val = unsafe { std::mem::MaybeUninit::<[usize; 1]>::uninit().assume_init() };

tests/fail/validity/uninit_raw_ptr.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: Undefined Behavior: constructing invalid value at .value[0]: encountered uninitialized memory, but expected a raw pointer
1+
error: Undefined Behavior: constructing invalid value at [0]: encountered uninitialized memory, but expected a raw pointer
22
--> tests/fail/validity/uninit_raw_ptr.rs:LL:CC
33
|
44
LL | let _val = unsafe { std::mem::MaybeUninit::<[*const u8; 1]>::uninit().assume_init() };

0 commit comments

Comments
 (0)