Skip to content

Commit 9064147

Browse files
authored
Rollup merge of #98979 - RalfJung:more-alloc-range, r=oli-obk
interpret: use AllocRange in UninitByteAccess also use nice new format string syntax in `interpret/error.rs`, and use the `#` flag to add `0x` prefixes where applicable. r? ``@oli-obk``
2 parents 8cc6bb3 + 27b7b3d commit 9064147

File tree

4 files changed

+54
-63
lines changed

4 files changed

+54
-63
lines changed

compiler/rustc_const_eval/src/interpret/validity.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, '
427427
err_ub!(DanglingIntPointer(0, _)) =>
428428
{ "a null {kind}" },
429429
err_ub!(DanglingIntPointer(i, _)) =>
430-
{ "a dangling {kind} (address 0x{i:x} is unallocated)" },
430+
{ "a dangling {kind} (address {i:#x} is unallocated)" },
431431
err_ub!(PointerOutOfBounds { .. }) =>
432432
{ "a dangling {kind} (going beyond the bounds of its allocation)" },
433433
// This cannot happen during const-eval (because interning already detects
@@ -941,7 +941,7 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValueVisitor<'mir, 'tcx, M>
941941
// element that byte belongs to so we can
942942
// provide an index.
943943
let i = usize::try_from(
944-
access.uninit_offset.bytes() / layout.size.bytes(),
944+
access.uninit.start.bytes() / layout.size.bytes(),
945945
)
946946
.unwrap();
947947
self.path.push(PathElem::ArrayElem(i));

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

+12-9
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,11 @@ pub fn alloc_range(start: Size, size: Size) -> AllocRange {
179179
}
180180

181181
impl AllocRange {
182+
#[inline]
183+
pub fn from(r: Range<Size>) -> Self {
184+
alloc_range(r.start, r.end - r.start) // `Size` subtraction (overflow-checked)
185+
}
186+
182187
#[inline(always)]
183188
pub fn end(self) -> Size {
184189
self.start + self.size // This does overflow checking.
@@ -1095,17 +1100,17 @@ impl InitMask {
10951100
/// Returns `Ok(())` if it's initialized. Otherwise returns a range of byte
10961101
/// indexes for the first contiguous span of the uninitialized access.
10971102
#[inline]
1098-
pub fn is_range_initialized(&self, start: Size, end: Size) -> Result<(), Range<Size>> {
1103+
pub fn is_range_initialized(&self, start: Size, end: Size) -> Result<(), AllocRange> {
10991104
if end > self.len {
1100-
return Err(self.len..end);
1105+
return Err(AllocRange::from(self.len..end));
11011106
}
11021107

11031108
let uninit_start = self.find_bit(start, end, false);
11041109

11051110
match uninit_start {
11061111
Some(uninit_start) => {
11071112
let uninit_end = self.find_bit(uninit_start, end, true).unwrap_or(end);
1108-
Err(uninit_start..uninit_end)
1113+
Err(AllocRange::from(uninit_start..uninit_end))
11091114
}
11101115
None => Ok(()),
11111116
}
@@ -1176,19 +1181,17 @@ impl<Tag: Copy, Extra> Allocation<Tag, Extra> {
11761181
///
11771182
/// Returns `Ok(())` if it's initialized. Otherwise returns the range of byte
11781183
/// indexes of the first contiguous uninitialized access.
1179-
fn is_init(&self, range: AllocRange) -> Result<(), Range<Size>> {
1184+
fn is_init(&self, range: AllocRange) -> Result<(), AllocRange> {
11801185
self.init_mask.is_range_initialized(range.start, range.end()) // `Size` addition
11811186
}
11821187

11831188
/// Checks that a range of bytes is initialized. If not, returns the `InvalidUninitBytes`
11841189
/// error which will report the first range of bytes which is uninitialized.
11851190
fn check_init(&self, range: AllocRange) -> AllocResult {
1186-
self.is_init(range).map_err(|idx_range| {
1191+
self.is_init(range).map_err(|uninit_range| {
11871192
AllocError::InvalidUninitBytes(Some(UninitBytesAccess {
1188-
access_offset: range.start,
1189-
access_size: range.size,
1190-
uninit_offset: idx_range.start,
1191-
uninit_size: idx_range.end - idx_range.start, // `Size` subtraction
1193+
access: range,
1194+
uninit: uninit_range,
11921195
}))
11931196
})
11941197
}

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

+39-51
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::{AllocId, ConstAlloc, Pointer, Scalar};
1+
use super::{AllocId, AllocRange, ConstAlloc, Pointer, Scalar};
22

33
use crate::mir::interpret::ConstValue;
44
use crate::ty::{layout, query::TyCtxtAt, tls, FnSig, Ty, ValTree};
@@ -162,9 +162,9 @@ impl fmt::Display for InvalidProgramInfo<'_> {
162162
AlreadyReported(ErrorGuaranteed { .. }) => {
163163
write!(f, "encountered constants with type errors, stopping evaluation")
164164
}
165-
Layout(ref err) => write!(f, "{}", err),
166-
FnAbiAdjustForForeignAbi(ref err) => write!(f, "{}", err),
167-
SizeOfUnsizedType(ty) => write!(f, "size_of called on unsized type `{}`", ty),
165+
Layout(ref err) => write!(f, "{err}"),
166+
FnAbiAdjustForForeignAbi(ref err) => write!(f, "{err}"),
167+
SizeOfUnsizedType(ty) => write!(f, "size_of called on unsized type `{ty}`"),
168168
}
169169
}
170170
}
@@ -205,14 +205,10 @@ impl fmt::Display for CheckInAllocMsg {
205205
/// Details of an access to uninitialized bytes where it is not allowed.
206206
#[derive(Debug)]
207207
pub struct UninitBytesAccess {
208-
/// Location of the original memory access.
209-
pub access_offset: Size,
210-
/// Size of the original memory access.
211-
pub access_size: Size,
212-
/// Location of the first uninitialized byte that was accessed.
213-
pub uninit_offset: Size,
214-
/// Number of consecutive uninitialized bytes that were accessed.
215-
pub uninit_size: Size,
208+
/// Range of the original memory access.
209+
pub access: AllocRange,
210+
/// Range of the uninit memory that was encountered. (Might not be maximal.)
211+
pub uninit: AllocRange,
216212
}
217213

218214
/// Information about a size mismatch.
@@ -308,30 +304,28 @@ impl fmt::Display for UndefinedBehaviorInfo<'_> {
308304
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
309305
use UndefinedBehaviorInfo::*;
310306
match self {
311-
Ub(msg) => write!(f, "{}", msg),
307+
Ub(msg) => write!(f, "{msg}"),
312308
Unreachable => write!(f, "entering unreachable code"),
313309
BoundsCheckFailed { ref len, ref index } => {
314-
write!(f, "indexing out of bounds: the len is {} but the index is {}", len, index)
310+
write!(f, "indexing out of bounds: the len is {len} but the index is {index}")
315311
}
316312
DivisionByZero => write!(f, "dividing by zero"),
317313
RemainderByZero => write!(f, "calculating the remainder with a divisor of zero"),
318314
DivisionOverflow => write!(f, "overflow in signed division (dividing MIN by -1)"),
319315
RemainderOverflow => write!(f, "overflow in signed remainder (dividing MIN by -1)"),
320316
PointerArithOverflow => write!(f, "overflowing in-bounds pointer arithmetic"),
321-
InvalidMeta(msg) => write!(f, "invalid metadata in wide pointer: {}", msg),
317+
InvalidMeta(msg) => write!(f, "invalid metadata in wide pointer: {msg}"),
322318
InvalidVtableDropFn(sig) => write!(
323319
f,
324-
"invalid drop function signature: got {}, expected exactly one argument which must be a pointer type",
325-
sig
320+
"invalid drop function signature: got {sig}, expected exactly one argument which must be a pointer type",
326321
),
327322
InvalidVtableSize => {
328323
write!(f, "invalid vtable: size is bigger than largest supported object")
329324
}
330-
InvalidVtableAlignment(msg) => write!(f, "invalid vtable: alignment {}", msg),
325+
InvalidVtableAlignment(msg) => write!(f, "invalid vtable: alignment {msg}"),
331326
UnterminatedCString(p) => write!(
332327
f,
333-
"reading a null-terminated string starting at {:?} with no null found before end of allocation",
334-
p,
328+
"reading a null-terminated string starting at {p:?} with no null found before end of allocation",
335329
),
336330
PointerUseAfterFree(a) => {
337331
write!(f, "pointer to {a:?} was dereferenced after this allocation got freed")
@@ -359,41 +353,36 @@ impl fmt::Display for UndefinedBehaviorInfo<'_> {
359353
}
360354
AlignmentCheckFailed { required, has } => write!(
361355
f,
362-
"accessing memory with alignment {}, but alignment {} is required",
363-
has.bytes(),
364-
required.bytes()
356+
"accessing memory with alignment {has}, but alignment {required} is required",
357+
has = has.bytes(),
358+
required = required.bytes()
365359
),
366360
WriteToReadOnly(a) => write!(f, "writing to {a:?} which is read-only"),
367361
DerefFunctionPointer(a) => write!(f, "accessing {a:?} which contains a function"),
368362
ValidationFailure { path: None, msg } => {
369-
write!(f, "constructing invalid value: {}", msg)
363+
write!(f, "constructing invalid value: {msg}")
370364
}
371365
ValidationFailure { path: Some(path), msg } => {
372-
write!(f, "constructing invalid value at {}: {}", path, msg)
366+
write!(f, "constructing invalid value at {path}: {msg}")
373367
}
374368
InvalidBool(b) => {
375-
write!(f, "interpreting an invalid 8-bit value as a bool: 0x{:02x}", b)
369+
write!(f, "interpreting an invalid 8-bit value as a bool: 0x{b:02x}")
376370
}
377371
InvalidChar(c) => {
378-
write!(f, "interpreting an invalid 32-bit value as a char: 0x{:08x}", c)
372+
write!(f, "interpreting an invalid 32-bit value as a char: 0x{c:08x}")
379373
}
380-
InvalidTag(val) => write!(f, "enum value has invalid tag: {:x}", val),
374+
InvalidTag(val) => write!(f, "enum value has invalid tag: {val:x}"),
381375
InvalidFunctionPointer(p) => {
382-
write!(f, "using {:?} as function pointer but it does not point to a function", p)
376+
write!(f, "using {p:?} as function pointer but it does not point to a function")
383377
}
384-
InvalidStr(err) => write!(f, "this string is not valid UTF-8: {}", err),
385-
InvalidUninitBytes(Some((alloc, access))) => write!(
378+
InvalidStr(err) => write!(f, "this string is not valid UTF-8: {err}"),
379+
InvalidUninitBytes(Some((alloc, info))) => write!(
386380
f,
387-
"reading {} byte{} of memory starting at {:?}, \
388-
but {} byte{} {} uninitialized starting at {:?}, \
381+
"reading memory at {alloc:?}{access:?}, \
382+
but memory is uninitialized at {uninit:?}, \
389383
and this operation requires initialized memory",
390-
access.access_size.bytes(),
391-
pluralize!(access.access_size.bytes()),
392-
Pointer::new(*alloc, access.access_offset),
393-
access.uninit_size.bytes(),
394-
pluralize!(access.uninit_size.bytes()),
395-
pluralize!("is", access.uninit_size.bytes()),
396-
Pointer::new(*alloc, access.uninit_offset),
384+
access = info.access,
385+
uninit = info.uninit,
397386
),
398387
InvalidUninitBytes(None) => write!(
399388
f,
@@ -402,8 +391,7 @@ impl fmt::Display for UndefinedBehaviorInfo<'_> {
402391
DeadLocal => write!(f, "accessing a dead local variable"),
403392
ScalarSizeMismatch(self::ScalarSizeMismatch { target_size, data_size }) => write!(
404393
f,
405-
"scalar size mismatch: expected {} bytes but got {} bytes instead",
406-
target_size, data_size
394+
"scalar size mismatch: expected {target_size} bytes but got {data_size} bytes instead",
407395
),
408396
UninhabitedEnumVariantWritten => {
409397
write!(f, "writing discriminant of an uninhabited enum")
@@ -437,13 +425,13 @@ impl fmt::Display for UnsupportedOpInfo {
437425
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
438426
use UnsupportedOpInfo::*;
439427
match self {
440-
Unsupported(ref msg) => write!(f, "{}", msg),
428+
Unsupported(ref msg) => write!(f, "{msg}"),
441429
ReadPointerAsBytes => write!(f, "unable to turn pointer into raw bytes"),
442430
PartialPointerOverwrite(ptr) => {
443-
write!(f, "unable to overwrite parts of a pointer in memory at {:?}", ptr)
431+
write!(f, "unable to overwrite parts of a pointer in memory at {ptr:?}")
444432
}
445-
ThreadLocalStatic(did) => write!(f, "cannot access thread local static ({:?})", did),
446-
ReadExternStatic(did) => write!(f, "cannot read from extern static ({:?})", did),
433+
ThreadLocalStatic(did) => write!(f, "cannot access thread local static ({did:?})"),
434+
ReadExternStatic(did) => write!(f, "cannot read from extern static ({did:?})"),
447435
}
448436
}
449437
}
@@ -526,11 +514,11 @@ impl fmt::Display for InterpError<'_> {
526514
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
527515
use InterpError::*;
528516
match *self {
529-
Unsupported(ref msg) => write!(f, "{}", msg),
530-
InvalidProgram(ref msg) => write!(f, "{}", msg),
531-
UndefinedBehavior(ref msg) => write!(f, "{}", msg),
532-
ResourceExhaustion(ref msg) => write!(f, "{}", msg),
533-
MachineStop(ref msg) => write!(f, "{}", msg),
517+
Unsupported(ref msg) => write!(f, "{msg}"),
518+
InvalidProgram(ref msg) => write!(f, "{msg}"),
519+
UndefinedBehavior(ref msg) => write!(f, "{msg}"),
520+
ResourceExhaustion(ref msg) => write!(f, "{msg}"),
521+
MachineStop(ref msg) => write!(f, "{msg}"),
534522
}
535523
}
536524
}

src/test/ui/intrinsics/intrinsic-raw_eq-const-padding.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0080]: evaluation of constant value failed
22
--> $DIR/intrinsic-raw_eq-const-padding.rs:6:5
33
|
44
LL | std::intrinsics::raw_eq(&(1_u8, 2_u16), &(1_u8, 2_u16))
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ reading 4 bytes of memory starting at alloc3, but 1 byte is uninitialized starting at alloc3+0x1, and this operation requires initialized memory
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ reading memory at alloc3[0x0..0x4], but memory is uninitialized at [0x1..0x2], and this operation requires initialized memory
66

77
error: aborting due to previous error
88

0 commit comments

Comments
 (0)