Skip to content

Commit 9e70fad

Browse files
committed
Show the memory of uninit reads
1 parent f81fba1 commit 9e70fad

File tree

50 files changed

+350
-126
lines changed

Some content is hidden

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

50 files changed

+350
-126
lines changed

compiler/rustc_const_eval/src/const_eval/error.rs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@ 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::{Provenance, ReportedErrorInfo};
5+
use rustc_middle::mir::interpret::{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-
ErrorHandled, Frame, InterpErrorInfo, InterpErrorKind, MachineStopType, err_inval,
14+
ErrorHandled, Frame, InterpCx, InterpErrorInfo, InterpErrorKind, MachineStopType, err_inval,
1515
err_machine_stop,
1616
};
1717

@@ -135,7 +135,7 @@ pub fn get_span_and_frames<'tcx>(
135135
/// You can use it to add a stacktrace of current execution according to
136136
/// `get_span_and_frames` or just give context on where the const eval error happened.
137137
pub(super) fn report<'tcx, C, F>(
138-
tcx: TyCtxt<'tcx>,
138+
ecx: &InterpCx<'tcx, CompileTimeMachine<'tcx>>,
139139
error: InterpErrorKind<'tcx>,
140140
span: Span,
141141
get_span_and_frames: C,
@@ -145,6 +145,7 @@ where
145145
C: FnOnce() -> (Span, Vec<FrameNote>),
146146
F: FnOnce(&mut Diag<'_>, Span, Vec<FrameNote>),
147147
{
148+
let tcx = ecx.tcx.tcx;
148149
// Special handling for certain errors
149150
match error {
150151
// Don't emit a new diagnostic for these errors, they are already reported elsewhere or
@@ -170,9 +171,24 @@ where
170171
InterpErrorKind::ResourceExhaustion(_) | InterpErrorKind::InvalidProgram(_)
171172
);
172173

174+
if let InterpErrorKind::UndefinedBehavior(UndefinedBehaviorInfo::InvalidUninitBytes(
175+
Some((alloc_id, _access)),
176+
)) = error
177+
{
178+
let bytes = ecx.print_alloc_bytes_for_diagnostics(alloc_id);
179+
let info = ecx.get_alloc_info(alloc_id);
180+
let raw_bytes = errors::RawBytesNote {
181+
size: info.size.bytes(),
182+
align: info.align.bytes(),
183+
bytes,
184+
};
185+
err.subdiagnostic(raw_bytes);
186+
}
187+
173188
error.add_args(&mut err);
174189

175190
mk(&mut err, span, frames);
191+
176192
let g = err.emit();
177193
let reported = if allowed_in_infallible {
178194
ReportedErrorInfo::allowed_in_infallible(g)

compiler/rustc_const_eval/src/const_eval/eval_queries.rs

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

408408
super::report(
409-
*ecx.tcx,
409+
ecx,
410410
error,
411411
DUMMY_SP,
412412
|| super::get_span_and_frames(ecx.tcx, ecx.stack()),
@@ -446,7 +446,7 @@ fn report_validation_error<'tcx>(
446446
errors::RawBytesNote { size: info.size.bytes(), align: info.align.bytes(), bytes };
447447

448448
crate::const_eval::report(
449-
*ecx.tcx,
449+
ecx,
450450
error,
451451
DUMMY_SP,
452452
|| 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: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
//@ignore-target: windows # No pthreads on Windows
2+
//@ normalize-stderr-test: ".*│.*\n" -> ""
3+
//@ normalize-stderr-test: "size: [0-9]+" -> "size: SIZE"
4+
//@ normalize-stderr-test: "align: [0-9]+" -> "align: ALIGN"
5+
//@ normalize-stderr-test: "\[0x[0-9a-z]..0x[0-9a-z]\]" -> "[0xX..0xY]"
26

37
/// Test that destroying a pthread_cond twice fails, even without a check for number validity
48
@@ -15,6 +19,6 @@ fn main() {
1519
libc::pthread_cond_destroy(cond.as_mut_ptr());
1620

1721
libc::pthread_cond_destroy(cond.as_mut_ptr());
18-
//~^ ERROR: Undefined Behavior: using uninitialized data, but this operation requires initialized memory
22+
//~^ ERROR: /Undefined Behavior: .* but memory is uninitialized/
1923
}
2024
}

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

Lines changed: 5 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[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,6 +9,10 @@ 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[0xX..0xY], in this allocation:
13+
ALLOC (stack variable, size: SIZE, align: ALIGN) {
14+
}
15+
1216
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
1317

1418
error: aborting due to 1 previous error

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
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" -> ""
4+
//@ normalize-stderr-test: "size: [0-9]+" -> "size: SIZE"
5+
//@ normalize-stderr-test: "align: [0-9]+" -> "align: ALIGN"
6+
//@ normalize-stderr-test: "\[0x[0-9a-z]..0x[0-9a-z]\]" -> "[0xX..0xY]"
37

48
/// Test that destroying a pthread_condattr twice fails, even without a check for number validity
59
@@ -13,6 +17,6 @@ fn main() {
1317
libc::pthread_condattr_destroy(attr.as_mut_ptr());
1418

1519
libc::pthread_condattr_destroy(attr.as_mut_ptr());
16-
//~^ ERROR: Undefined Behavior: using uninitialized data, but this operation requires initialized memory
20+
//~^ ERROR: /Undefined Behavior: .* but memory is uninitialized/
1721
}
1822
}

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

Lines changed: 5 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[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,6 +9,10 @@ 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[0xX..0xY], in this allocation:
13+
ALLOC (stack variable, size: SIZE, align: ALIGN) {
14+
}
15+
1216
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
1317

1418
error: aborting due to 1 previous error

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
//@ignore-target: windows # No pthreads on Windows
2+
//@ normalize-stderr-test: ".*│.*\n" -> ""
3+
//@ normalize-stderr-test: "size: [0-9]+" -> "size: SIZE"
4+
//@ normalize-stderr-test: "align: [0-9]+" -> "align: ALIGN"
5+
//@ normalize-stderr-test: "\[0x[0-9a-z]..0x[0-9a-z]\]" -> "[0xX..0xY]"
26

37
/// Test that destroying a pthread_mutex twice fails, even without a check for number validity
48
@@ -16,6 +20,6 @@ fn main() {
1620
libc::pthread_mutex_destroy(mutex.as_mut_ptr());
1721

1822
libc::pthread_mutex_destroy(mutex.as_mut_ptr());
19-
//~^ ERROR: Undefined Behavior: using uninitialized data, but this operation requires initialized memory
23+
//~^ ERROR: /Undefined Behavior: .* but memory is uninitialized/
2024
}
2125
}

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

Lines changed: 5 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[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,6 +9,10 @@ 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[0xX..0xY], in this allocation:
13+
ALLOC (stack variable, size: SIZE, align: ALIGN) {
14+
}
15+
1216
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
1317

1418
error: aborting due to 1 previous error

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
//@ignore-target: windows # No pthreads on Windows
2+
//@ normalize-stderr-test: ".*│.*\n" -> ""
3+
//@ normalize-stderr-test: "size: [0-9]+" -> "size: SIZE"
4+
//@ normalize-stderr-test: "align: [0-9]+" -> "align: ALIGN"
5+
//@ normalize-stderr-test: "\[0x[0-9a-z]..0x[0-9a-z]\]" -> "[0xX..0xY]"
26

37
/// Test that destroying a pthread_mutexattr twice fails, even without a check for number validity
48
@@ -12,6 +16,6 @@ fn main() {
1216
libc::pthread_mutexattr_destroy(attr.as_mut_ptr());
1317

1418
libc::pthread_mutexattr_destroy(attr.as_mut_ptr());
15-
//~^ ERROR: Undefined Behavior: using uninitialized data, but this operation requires initialized memory
19+
//~^ ERROR: /Undefined Behavior: .* but memory is uninitialized/
1620
}
1721
}

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

Lines changed: 5 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[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,6 +9,10 @@ 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[0xX..0xY], in this allocation:
13+
ALLOC (stack variable, size: SIZE, align: ALIGN) {
14+
}
15+
1216
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
1317

1418
error: aborting due to 1 previous error

0 commit comments

Comments
 (0)