Skip to content

Commit f81fba1

Browse files
committed
Report the range of uninit bytes in CTFE errors
1 parent f46ce66 commit f81fba1

36 files changed

+133
-46
lines changed

compiler/rustc_const_eval/src/interpret/validity.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ impl<'rt, 'tcx, M: Machine<'tcx>> ValidityVisitor<'rt, 'tcx, M> {
397397
interp_ok(try_validation!(
398398
self.ecx.read_immediate(val),
399399
self.path,
400-
Ub(InvalidUninitBytes(None)) =>
400+
Ub(InvalidUninitBytes(_)) =>
401401
Uninit { expected },
402402
// The `Unsup` cases can only occur during CTFE
403403
Unsup(ReadPointerAsInt(_)) =>

compiler/rustc_middle/src/mir/interpret/allocation.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -700,8 +700,11 @@ impl<Prov: Provenance, Extra, Bytes: AllocBytes> Allocation<Prov, Extra, Bytes>
700700
read_provenance: bool,
701701
) -> AllocResult<Scalar<Prov>> {
702702
// First and foremost, if anything is uninit, bail.
703-
if self.init_mask.is_range_initialized(range).is_err() {
704-
return Err(AllocError::InvalidUninitBytes(None));
703+
if let Err(bad) = self.init_mask.is_range_initialized(range) {
704+
return Err(AllocError::InvalidUninitBytes(Some(BadBytesAccess {
705+
access: range,
706+
bad,
707+
})));
705708
}
706709

707710
// Get the integer part of the result. We HAVE TO check provenance before returning this!

src/tools/miri/tests/fail/function_calls/arg_inplace_observe_after.stderr

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: Undefined Behavior: using uninitialized data, but this operation requires initialized memory
1+
error: Undefined Behavior: reading memory at ALLOC[0x0..0x4], but memory is uninitialized at [0x0..0x4], and this operation requires initialized memory
22
--> tests/fail/function_calls/arg_inplace_observe_after.rs:LL:CC
33
|
44
LL | _observe = non_copy.0;
@@ -9,6 +9,11 @@ LL | _observe = non_copy.0;
99
= note: BACKTRACE:
1010
= note: inside `main` at tests/fail/function_calls/arg_inplace_observe_after.rs:LL:CC
1111

12+
Uninitialized memory occurred at ALLOC[0x0..0x4], in this allocation:
13+
ALLOC (stack variable, size: 4, align: 4) {
14+
__ __ __ __ │ ░░░░
15+
}
16+
1217
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
1318

1419
error: aborting due to 1 previous error

src/tools/miri/tests/fail/function_calls/arg_inplace_observe_during.none.stderr

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: Undefined Behavior: using uninitialized data, but this operation requires initialized memory
1+
error: Undefined Behavior: reading memory at ALLOC[0x0..0x4], but memory is uninitialized at [0x0..0x4], and this operation requires initialized memory
22
--> tests/fail/function_calls/arg_inplace_observe_during.rs:LL:CC
33
|
44
LL | unsafe { ptr.read() };
@@ -14,6 +14,11 @@ note: inside `main`
1414
LL | Call(_unit = change_arg(Move(*ptr), ptr), ReturnTo(after_call), UnwindContinue())
1515
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1616

17+
Uninitialized memory occurred at ALLOC[0x0..0x4], in this allocation:
18+
ALLOC (stack variable, size: 4, align: 4) {
19+
__ __ __ __ │ ░░░░
20+
}
21+
1722
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
1823

1924
error: aborting due to 1 previous error

src/tools/miri/tests/fail/function_calls/return_pointer_aliasing_read.none.stderr

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: Undefined Behavior: using uninitialized data, but this operation requires initialized memory
1+
error: Undefined Behavior: reading memory at ALLOC[0x0..0x4], but memory is uninitialized at [0x0..0x4], and this operation requires initialized memory
22
--> tests/fail/function_calls/return_pointer_aliasing_read.rs:LL:CC
33
|
44
LL | unsafe { ptr.read() };
@@ -14,6 +14,11 @@ note: inside `main`
1414
LL | Call(*ptr = myfun(ptr), ReturnTo(after_call), UnwindContinue())
1515
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1616

17+
Uninitialized memory occurred at ALLOC[0x0..0x4], in this allocation:
18+
ALLOC (stack variable, size: 4, align: 4) {
19+
__ __ __ __ │ ░░░░
20+
}
21+
1722
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
1823

1924
error: aborting due to 1 previous error

src/tools/miri/tests/fail/function_calls/return_pointer_on_unwind.stderr

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ thread 'main' panicked at tests/fail/function_calls/return_pointer_on_unwind.rs:
33
explicit panic
44
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
55
note: in Miri, you may have to set `MIRIFLAGS=-Zmiri-env-forward=RUST_BACKTRACE` for the environment variable to have an effect
6-
error: Undefined Behavior: using uninitialized data, but this operation requires initialized memory
6+
error: Undefined Behavior: reading memory at ALLOC[0x0..0x4], but memory is uninitialized at [0x0..0x4], and this operation requires initialized memory
77
--> tests/fail/function_calls/return_pointer_on_unwind.rs:LL:CC
88
|
99
LL | dbg!(x.0);
@@ -15,6 +15,19 @@ LL | dbg!(x.0);
1515
= note: inside `main` at RUSTLIB/std/src/macros.rs:LL:CC
1616
= note: this error originates in the macro `dbg` (in Nightly builds, run with -Z macro-backtrace for more info)
1717

18+
Uninitialized memory occurred at ALLOC[0x0..0x4], in this allocation:
19+
ALLOC (stack variable, size: 132, align: 4) {
20+
0x00 │ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ │ ░░░░░░░░░░░░░░░░
21+
0x10 │ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ │ ░░░░░░░░░░░░░░░░
22+
0x20 │ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ │ ░░░░░░░░░░░░░░░░
23+
0x30 │ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ │ ░░░░░░░░░░░░░░░░
24+
0x40 │ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ │ ░░░░░░░░░░░░░░░░
25+
0x50 │ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ │ ░░░░░░░░░░░░░░░░
26+
0x60 │ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ │ ░░░░░░░░░░░░░░░░
27+
0x70 │ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ │ ░░░░░░░░░░░░░░░░
28+
0x80 │ __ __ __ __ │ ░░░░
29+
}
30+
1831
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
1932

2033
error: aborting due to 1 previous error

src/tools/miri/tests/fail/intrinsics/ptr_metadata_uninit_slice_data.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use std::intrinsics::mir::*;
99
pub unsafe fn deref_meta(p: *const *const [i32]) -> usize {
1010
mir! {
1111
{
12-
RET = PtrMetadata(*p); //~ ERROR: Undefined Behavior: using uninitialized data
12+
RET = PtrMetadata(*p); //~ ERROR: /Undefined Behavior: .*, but memory is uninitialized/
1313
Return()
1414
}
1515
}

src/tools/miri/tests/fail/intrinsics/ptr_metadata_uninit_slice_data.stderr

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: Undefined Behavior: using uninitialized data, but this operation requires initialized memory
1+
error: Undefined Behavior: reading memory at ALLOC[0x0..0x8], but memory is uninitialized at [0x0..0x8], and this operation requires initialized memory
22
--> tests/fail/intrinsics/ptr_metadata_uninit_slice_data.rs:LL:CC
33
|
44
LL | RET = PtrMetadata(*p);
@@ -14,6 +14,11 @@ note: inside `main`
1414
LL | let _meta = deref_meta(p.as_ptr().cast());
1515
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1616

17+
Uninitialized memory occurred at ALLOC[0x0..0x8], in this allocation:
18+
ALLOC (stack variable, size: 16, align: 8) {
19+
__ __ __ __ __ __ __ __ 04 00 00 00 00 00 00 00 │ ░░░░░░░░........
20+
}
21+
1722
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
1823

1924
error: aborting due to 1 previous error

src/tools/miri/tests/fail/intrinsics/ptr_metadata_uninit_slice_len.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use std::intrinsics::mir::*;
99
pub unsafe fn deref_meta(p: *const *const [i32]) -> usize {
1010
mir! {
1111
{
12-
RET = PtrMetadata(*p); //~ ERROR: Undefined Behavior: using uninitialized data
12+
RET = PtrMetadata(*p); //~ ERROR: /Undefined Behavior: .*, but memory is uninitialized/
1313
Return()
1414
}
1515
}

src/tools/miri/tests/fail/intrinsics/ptr_metadata_uninit_slice_len.stderr

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ LL | (*p.as_mut_ptr().cast::<[*const i32; 2]>())[0] = 4 as *const i32;
1212
= note: BACKTRACE:
1313
= note: inside `main` at tests/fail/intrinsics/ptr_metadata_uninit_slice_len.rs:LL:CC
1414

15-
error: Undefined Behavior: using uninitialized data, but this operation requires initialized memory
15+
error: Undefined Behavior: reading memory at ALLOC[0x8..0x10], but memory is uninitialized at [0x8..0x10], and this operation requires initialized memory
1616
--> tests/fail/intrinsics/ptr_metadata_uninit_slice_len.rs:LL:CC
1717
|
1818
LL | RET = PtrMetadata(*p);
@@ -28,6 +28,11 @@ note: inside `main`
2828
LL | let _meta = deref_meta(p.as_ptr().cast());
2929
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3030

31+
Uninitialized memory occurred at ALLOC[0x8..0x10], in this allocation:
32+
ALLOC (stack variable, size: 16, align: 8) {
33+
╾────0x4[wildcard]────╼ __ __ __ __ __ __ __ __ │ ╾──────╼░░░░░░░░
34+
}
35+
3136
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
3237

3338
error: aborting due to 1 previous error; 1 warning emitted

0 commit comments

Comments
 (0)