Skip to content

Commit 154f820

Browse files
committed
Show the memory of uninit reads
1 parent 12eedaf commit 154f820

File tree

53 files changed

+338
-158
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+338
-158
lines changed

compiler/rustc_const_eval/src/const_eval/error.rs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@ use std::mem;
22

33
use rustc_errors::{Diag, DiagArgName, DiagArgValue, DiagMessage, IntoDiagArg};
44
use rustc_middle::mir::AssertKind;
5-
use rustc_middle::mir::interpret::{AllocId, Provenance, ReportedErrorInfo};
5+
use rustc_middle::mir::interpret::{AllocId, Provenance, ReportedErrorInfo, UndefinedBehaviorInfo};
66
use rustc_middle::query::TyCtxtAt;
7+
use rustc_middle::ty::ConstInt;
78
use rustc_middle::ty::layout::LayoutError;
8-
use rustc_middle::ty::{ConstInt, TyCtxt};
99
use rustc_span::{Span, Symbol};
1010

1111
use super::CompileTimeMachine;
1212
use crate::errors::{self, FrameNote, ReportErrorExt};
1313
use crate::interpret::{
14-
CtfeProvenance, ErrorHandled, Frame, InterpErrorInfo, InterpErrorKind, MachineStopType,
15-
Pointer, err_inval, err_machine_stop,
14+
CtfeProvenance, ErrorHandled, Frame, InterpCx, InterpErrorInfo, InterpErrorKind,
15+
MachineStopType, Pointer, err_inval, err_machine_stop,
1616
};
1717

1818
/// The CTFE machine has some custom error kinds.
@@ -163,7 +163,7 @@ pub fn get_span_and_frames<'tcx>(
163163
/// You can use it to add a stacktrace of current execution according to
164164
/// `get_span_and_frames` or just give context on where the const eval error happened.
165165
pub(super) fn report<'tcx, C, F>(
166-
tcx: TyCtxt<'tcx>,
166+
ecx: &InterpCx<'tcx, CompileTimeMachine<'tcx>>,
167167
error: InterpErrorKind<'tcx>,
168168
span: Span,
169169
get_span_and_frames: C,
@@ -173,6 +173,7 @@ where
173173
C: FnOnce() -> (Span, Vec<FrameNote>),
174174
F: FnOnce(&mut Diag<'_>, Span, Vec<FrameNote>),
175175
{
176+
let tcx = ecx.tcx.tcx;
176177
// Special handling for certain errors
177178
match error {
178179
// Don't emit a new diagnostic for these errors, they are already reported elsewhere or
@@ -198,6 +199,20 @@ where
198199
InterpErrorKind::ResourceExhaustion(_) | InterpErrorKind::InvalidProgram(_)
199200
);
200201

202+
if let InterpErrorKind::UndefinedBehavior(UndefinedBehaviorInfo::InvalidUninitBytes(
203+
Some((alloc_id, _access)),
204+
)) = error
205+
{
206+
let bytes = ecx.print_alloc_bytes_for_diagnostics(alloc_id);
207+
let info = ecx.get_alloc_info(alloc_id);
208+
let raw_bytes = errors::RawBytesNote {
209+
size: info.size.bytes(),
210+
align: info.align.bytes(),
211+
bytes,
212+
};
213+
err.subdiagnostic(raw_bytes);
214+
}
215+
201216
error.add_args(&mut err);
202217

203218
mk(&mut err, span, frames);

compiler/rustc_const_eval/src/const_eval/eval_queries.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ fn report_eval_error<'tcx>(
411411
let instance = with_no_trimmed_paths!(cid.instance.to_string());
412412

413413
super::report(
414-
*ecx.tcx,
414+
ecx,
415415
error,
416416
DUMMY_SP,
417417
|| super::get_span_and_frames(ecx.tcx, ecx.stack()),
@@ -451,7 +451,7 @@ fn report_validation_error<'tcx>(
451451
errors::RawBytesNote { size: info.size.bytes(), align: info.align.bytes(), bytes };
452452

453453
crate::const_eval::report(
454-
*ecx.tcx,
454+
ecx,
455455
error,
456456
DUMMY_SP,
457457
|| crate::const_eval::get_span_and_frames(ecx.tcx, ecx.stack()),

src/tools/miri/tests/fail-dep/concurrency/libc_pthread_cond_double_destroy.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
//@ignore-target: windows # No pthreads on Windows
2+
//@ normalize-stderr-test: "(\n)ALLOC \(.*\) \{\n(.*\n)*\}(\n)" -> "${1}ALLOC DUMP${3}"
3+
//@ normalize-stderr-test: "\[0x[0-9a-z]..0x[0-9a-z]\]" -> "[0xX..0xY]"
24

35
/// Test that destroying a pthread_cond twice fails, even without a check for number validity
46

src/tools/miri/tests/fail-dep/concurrency/libc_pthread_cond_double_destroy.stderr

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: Undefined Behavior: reading memory at ALLOC[0x0..0x4], but memory is uninitialized at [0x0..0x4], and this operation requires initialized memory
1+
error: Undefined Behavior: reading memory at ALLOC[0xX..0xY], but memory is uninitialized at [0xX..0xY], and this operation requires initialized memory
22
--> tests/fail-dep/concurrency/libc_pthread_cond_double_destroy.rs:LL:CC
33
|
44
LL | libc::pthread_cond_destroy(cond.as_mut_ptr());
@@ -9,12 +9,8 @@ LL | libc::pthread_cond_destroy(cond.as_mut_ptr());
99
= note: BACKTRACE:
1010
= note: inside `main` at tests/fail-dep/concurrency/libc_pthread_cond_double_destroy.rs:LL:CC
1111

12-
Uninitialized memory occurred at ALLOC[0x0..0x4], in this allocation:
13-
ALLOC (stack variable, size: 48, align: 8) {
14-
0x00 │ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ │ ░░░░░░░░░░░░░░░░
15-
0x10 │ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ │ ░░░░░░░░░░░░░░░░
16-
0x20 │ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ │ ░░░░░░░░░░░░░░░░
17-
}
12+
Uninitialized memory occurred at ALLOC[0xX..0xY], in this allocation:
13+
ALLOC DUMP
1814

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

src/tools/miri/tests/fail-dep/concurrency/libc_pthread_condattr_double_destroy.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//@ignore-target: windows # No pthreads on Windows
22
//@ignore-target: apple # Our macOS condattr don't have any fields so we do not notice this.
3+
//@ normalize-stderr-test: "(\n)ALLOC \(.*\) \{\n(.*\n)*\}(\n)" -> "${1}ALLOC DUMP${3}"
4+
//@ normalize-stderr-test: "\[0x[0-9a-z]..0x[0-9a-z]\]" -> "[0xX..0xY]"
35

46
/// Test that destroying a pthread_condattr twice fails, even without a check for number validity
57

src/tools/miri/tests/fail-dep/concurrency/libc_pthread_condattr_double_destroy.stderr

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: Undefined Behavior: reading memory at ALLOC[0x0..0x4], but memory is uninitialized at [0x0..0x4], and this operation requires initialized memory
1+
error: Undefined Behavior: reading memory at ALLOC[0xX..0xY], but memory is uninitialized at [0xX..0xY], and this operation requires initialized memory
22
--> tests/fail-dep/concurrency/libc_pthread_condattr_double_destroy.rs:LL:CC
33
|
44
LL | libc::pthread_condattr_destroy(attr.as_mut_ptr());
@@ -9,10 +9,8 @@ LL | libc::pthread_condattr_destroy(attr.as_mut_ptr());
99
= note: BACKTRACE:
1010
= note: inside `main` at tests/fail-dep/concurrency/libc_pthread_condattr_double_destroy.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-
}
12+
Uninitialized memory occurred at ALLOC[0xX..0xY], in this allocation:
13+
ALLOC DUMP
1614

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

src/tools/miri/tests/fail-dep/concurrency/libc_pthread_mutex_double_destroy.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
//@ignore-target: windows # No pthreads on Windows
2+
//@ normalize-stderr-test: "(\n)ALLOC \(.*\) \{\n(.*\n)*\}(\n)" -> "${1}ALLOC DUMP${3}"
3+
//@ normalize-stderr-test: "\[0x[0-9a-z]..0x[0-9a-z]\]" -> "[0xX..0xY]"
24

35
/// Test that destroying a pthread_mutex twice fails, even without a check for number validity
46

src/tools/miri/tests/fail-dep/concurrency/libc_pthread_mutex_double_destroy.stderr

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: Undefined Behavior: reading memory at ALLOC[0x0..0x4], but memory is uninitialized at [0x0..0x4], and this operation requires initialized memory
1+
error: Undefined Behavior: reading memory at ALLOC[0xX..0xY], but memory is uninitialized at [0xX..0xY], and this operation requires initialized memory
22
--> tests/fail-dep/concurrency/libc_pthread_mutex_double_destroy.rs:LL:CC
33
|
44
LL | libc::pthread_mutex_destroy(mutex.as_mut_ptr());
@@ -9,12 +9,8 @@ LL | libc::pthread_mutex_destroy(mutex.as_mut_ptr());
99
= note: BACKTRACE:
1010
= note: inside `main` at tests/fail-dep/concurrency/libc_pthread_mutex_double_destroy.rs:LL:CC
1111

12-
Uninitialized memory occurred at ALLOC[0x0..0x4], in this allocation:
13-
ALLOC (stack variable, size: 40, align: 8) {
14-
0x00 │ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ │ ░░░░░░░░░░░░░░░░
15-
0x10 │ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ │ ░░░░░░░░░░░░░░░░
16-
0x20 │ __ __ __ __ __ __ __ __ │ ░░░░░░░░
17-
}
12+
Uninitialized memory occurred at ALLOC[0xX..0xY], in this allocation:
13+
ALLOC DUMP
1814

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

src/tools/miri/tests/fail-dep/concurrency/libc_pthread_mutexattr_double_destroy.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
//@ignore-target: windows # No pthreads on Windows
2+
//@ normalize-stderr-test: "(\n)ALLOC \(.*\) \{\n(.*\n)*\}(\n)" -> "${1}ALLOC DUMP${3}"
3+
//@ normalize-stderr-test: "\[0x[0-9a-z]..0x[0-9a-z]\]" -> "[0xX..0xY]"
24

35
/// Test that destroying a pthread_mutexattr twice fails, even without a check for number validity
46

src/tools/miri/tests/fail-dep/concurrency/libc_pthread_mutexattr_double_destroy.stderr

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: Undefined Behavior: reading memory at ALLOC[0x0..0x4], but memory is uninitialized at [0x0..0x4], and this operation requires initialized memory
1+
error: Undefined Behavior: reading memory at ALLOC[0xX..0xY], but memory is uninitialized at [0xX..0xY], and this operation requires initialized memory
22
--> tests/fail-dep/concurrency/libc_pthread_mutexattr_double_destroy.rs:LL:CC
33
|
44
LL | libc::pthread_mutexattr_destroy(attr.as_mut_ptr());
@@ -9,10 +9,8 @@ LL | libc::pthread_mutexattr_destroy(attr.as_mut_ptr());
99
= note: BACKTRACE:
1010
= note: inside `main` at tests/fail-dep/concurrency/libc_pthread_mutexattr_double_destroy.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-
}
12+
Uninitialized memory occurred at ALLOC[0xX..0xY], in this allocation:
13+
ALLOC DUMP
1614

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

0 commit comments

Comments
 (0)