Skip to content

Commit b3827e4

Browse files
authored
Rollup merge of #142673 - oli-obk:uninit-read-mem, r=RalfJung
Show the offset, length and memory of uninit read errors r? ``@RalfJung`` I want to improve memory dumps in general. Not sure yet how to do so best within rust diagnostics, but in a perfect world I could generate a dummy in-memory file (that contains the rendered memory dump) that we then can then provide regular rustc `Span`s to. So we'd basically report normal diagnostics for them with squiggly lines and everything.
2 parents 61285e2 + 652ba27 commit b3827e4

File tree

63 files changed

+402
-144
lines changed

Some content is hidden

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

63 files changed

+402
-144
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()),

compiler/rustc_const_eval/src/interpret/validity.rs

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

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

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

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

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

Lines changed: 3 additions & 1 deletion
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
@@ -15,6 +17,6 @@ fn main() {
1517
libc::pthread_cond_destroy(cond.as_mut_ptr());
1618

1719
libc::pthread_cond_destroy(cond.as_mut_ptr());
18-
//~^ ERROR: Undefined Behavior: using uninitialized data, but this operation requires initialized memory
20+
//~^ ERROR: /Undefined Behavior: reading memory .*, but memory is uninitialized/
1921
}
2022
}

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

Lines changed: 4 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,9 @@ 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 DUMP
14+
1215
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
1316

1417
error: aborting due to 1 previous error

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

Lines changed: 3 additions & 1 deletion
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
@@ -13,6 +15,6 @@ fn main() {
1315
libc::pthread_condattr_destroy(attr.as_mut_ptr());
1416

1517
libc::pthread_condattr_destroy(attr.as_mut_ptr());
16-
//~^ ERROR: Undefined Behavior: using uninitialized data, but this operation requires initialized memory
18+
//~^ ERROR: /Undefined Behavior: reading memory .*, but memory is uninitialized/
1719
}
1820
}

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

Lines changed: 4 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,9 @@ 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 DUMP
14+
1215
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
1316

1417
error: aborting due to 1 previous error

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

Lines changed: 3 additions & 1 deletion
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
@@ -16,6 +18,6 @@ fn main() {
1618
libc::pthread_mutex_destroy(mutex.as_mut_ptr());
1719

1820
libc::pthread_mutex_destroy(mutex.as_mut_ptr());
19-
//~^ ERROR: Undefined Behavior: using uninitialized data, but this operation requires initialized memory
21+
//~^ ERROR: /Undefined Behavior: reading memory .*, but memory is uninitialized/
2022
}
2123
}

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

Lines changed: 4 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,9 @@ 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 DUMP
14+
1215
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
1316

1417
error: aborting due to 1 previous error

0 commit comments

Comments
 (0)