Skip to content

Commit a958314

Browse files
authored
Rollup merge of #69839 - RalfJung:miri-error-cleanup, r=oli-obk
Miri error reform Some time ago we started moving Miri errors into a few distinct categories, but we never classified all the old errors. That's what this PR does. ~~This is on top of #69762; [relative diff](RalfJung/rust@validity-errors...RalfJung:miri-error-cleanup).~~ r? @oli-obk Fixes rust-lang/const-eval#4
2 parents 23b79d8 + e219dd4 commit a958314

28 files changed

+420
-470
lines changed

src/librustc/mir/interpret/allocation.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ pub struct Allocation<Tag = (), Extra = ()> {
4141
/// The size of the allocation. Currently, must always equal `bytes.len()`.
4242
pub size: Size,
4343
/// The alignment of the allocation to detect unaligned reads.
44+
/// (`Align` guarantees that this is a power of two.)
4445
pub align: Align,
4546
/// `true` if the allocation is mutable.
4647
/// Also used by codegen to determine if a static should be put into mutable memory,
@@ -314,7 +315,7 @@ impl<'tcx, Tag: Copy, Extra: AllocationExtra<Tag>> Allocation<Tag, Extra> {
314315
&self.get_bytes(cx, ptr, size_with_null)?[..size]
315316
}
316317
// This includes the case where `offset` is out-of-bounds to begin with.
317-
None => throw_unsup!(UnterminatedCString(ptr.erase_tag())),
318+
None => throw_ub!(UnterminatedCString(ptr.erase_tag())),
318319
})
319320
}
320321

@@ -573,7 +574,7 @@ impl<'tcx, Tag, Extra> Allocation<Tag, Extra> {
573574
fn check_defined(&self, ptr: Pointer<Tag>, size: Size) -> InterpResult<'tcx> {
574575
self.undef_mask
575576
.is_range_defined(ptr.offset, ptr.offset + size)
576-
.or_else(|idx| throw_unsup!(ReadUndefBytes(idx)))
577+
.or_else(|idx| throw_ub!(InvalidUndefBytes(Some(Pointer::new(ptr.alloc_id, idx)))))
577578
}
578579

579580
pub fn mark_definedness(&mut self, ptr: Pointer<Tag>, size: Size, new_state: bool) {

src/librustc/mir/interpret/error.rs

Lines changed: 117 additions & 186 deletions
Large diffs are not rendered by default.

src/librustc/mir/interpret/mod.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,13 @@ pub struct AllocId(pub u64);
161161

162162
impl fmt::Debug for AllocId {
163163
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
164-
write!(fmt, "alloc{}", self.0)
164+
fmt::Display::fmt(self, fmt)
165+
}
166+
}
167+
168+
impl fmt::Display for AllocId {
169+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
170+
write!(f, "alloc{}", self.0)
165171
}
166172
}
167173

@@ -351,12 +357,6 @@ impl<'s> AllocDecodingSession<'s> {
351357
}
352358
}
353359

354-
impl fmt::Display for AllocId {
355-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
356-
write!(f, "{}", self.0)
357-
}
358-
}
359-
360360
/// An allocation in the global (tcx-managed) memory can be either a function pointer,
361361
/// a static, or a "real" allocation with some data in it.
362362
#[derive(Debug, Clone, Eq, PartialEq, Hash, RustcDecodable, RustcEncodable, HashStable)]

src/librustc/mir/interpret/pointer.rs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -213,20 +213,4 @@ impl<'tcx, Tag> Pointer<Tag> {
213213
pub fn erase_tag(self) -> Pointer {
214214
Pointer { alloc_id: self.alloc_id, offset: self.offset, tag: () }
215215
}
216-
217-
/// Test if the pointer is "inbounds" of an allocation of the given size.
218-
/// A pointer is "inbounds" even if its offset is equal to the size; this is
219-
/// a "one-past-the-end" pointer.
220-
#[inline(always)]
221-
pub fn check_inbounds_alloc(
222-
self,
223-
allocation_size: Size,
224-
msg: CheckInAllocMsg,
225-
) -> InterpResult<'tcx, ()> {
226-
if self.offset > allocation_size {
227-
throw_unsup!(PointerOutOfBounds { ptr: self.erase_tag(), msg, allocation_size })
228-
} else {
229-
Ok(())
230-
}
231-
}
232216
}

src/librustc/mir/interpret/value.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -455,18 +455,19 @@ impl<'tcx, Tag> Scalar<Tag> {
455455
}
456456

457457
pub fn to_bool(self) -> InterpResult<'tcx, bool> {
458-
match self {
459-
Scalar::Raw { data: 0, size: 1 } => Ok(false),
460-
Scalar::Raw { data: 1, size: 1 } => Ok(true),
461-
_ => throw_unsup!(InvalidBool),
458+
let val = self.to_u8()?;
459+
match val {
460+
0 => Ok(false),
461+
1 => Ok(true),
462+
_ => throw_ub!(InvalidBool(val)),
462463
}
463464
}
464465

465466
pub fn to_char(self) -> InterpResult<'tcx, char> {
466467
let val = self.to_u32()?;
467468
match ::std::char::from_u32(val) {
468469
Some(c) => Ok(c),
469-
None => throw_unsup!(InvalidChar(val as u128)),
470+
None => throw_ub!(InvalidChar(val)),
470471
}
471472
}
472473

@@ -609,7 +610,7 @@ impl<'tcx, Tag> ScalarMaybeUndef<Tag> {
609610
pub fn not_undef(self) -> InterpResult<'static, Scalar<Tag>> {
610611
match self {
611612
ScalarMaybeUndef::Scalar(scalar) => Ok(scalar),
612-
ScalarMaybeUndef::Undef => throw_unsup!(ReadUndefBytes(Size::ZERO)),
613+
ScalarMaybeUndef::Undef => throw_ub!(InvalidUndefBytes(None)),
613614
}
614615
}
615616

src/librustc_mir/const_eval/machine.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,8 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
240240
Ok(Some(match ecx.load_mir(instance.def, None) {
241241
Ok(body) => *body,
242242
Err(err) => {
243-
if let err_unsup!(NoMirFor(ref path)) = err.kind {
243+
if let err_unsup!(NoMirFor(did)) = err.kind {
244+
let path = ecx.tcx.def_path_str(did);
244245
return Err(ConstEvalErrKind::NeedsRfc(format!(
245246
"calling extern function `{}`",
246247
path

src/librustc_mir/interpret/eval_context.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ pub enum LocalValue<Tag = (), Id = AllocId> {
138138
impl<'tcx, Tag: Copy + 'static> LocalState<'tcx, Tag> {
139139
pub fn access(&self) -> InterpResult<'tcx, Operand<Tag>> {
140140
match self.value {
141-
LocalValue::Dead => throw_unsup!(DeadLocal),
141+
LocalValue::Dead => throw_ub!(DeadLocal),
142142
LocalValue::Uninitialized => {
143143
bug!("The type checker should prevent reading from a never-written local")
144144
}
@@ -152,7 +152,7 @@ impl<'tcx, Tag: Copy + 'static> LocalState<'tcx, Tag> {
152152
&mut self,
153153
) -> InterpResult<'tcx, Result<&mut LocalValue<Tag>, MemPlace<Tag>>> {
154154
match self.value {
155-
LocalValue::Dead => throw_unsup!(DeadLocal),
155+
LocalValue::Dead => throw_ub!(DeadLocal),
156156
LocalValue::Live(Operand::Indirect(mplace)) => Ok(Err(mplace)),
157157
ref mut local @ LocalValue::Live(Operand::Immediate(_))
158158
| ref mut local @ LocalValue::Uninitialized => Ok(Ok(local)),
@@ -326,7 +326,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
326326
if self.tcx.is_mir_available(did) {
327327
Ok(self.tcx.optimized_mir(did).unwrap_read_only())
328328
} else {
329-
throw_unsup!(NoMirFor(self.tcx.def_path_str(def_id)))
329+
throw_unsup!(NoMirFor(def_id))
330330
}
331331
}
332332
_ => Ok(self.tcx.instance_mir(instance)),

src/librustc_mir/interpret/intern.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ pub fn intern_const_alloc_recursive<M: CompileTimeMachine<'mir, 'tcx>>(
327327
if let Err(error) = interned {
328328
// This can happen when e.g. the tag of an enum is not a valid discriminant. We do have
329329
// to read enum discriminants in order to find references in enum variant fields.
330-
if let err_unsup!(ValidationFailure(_)) = error.kind {
330+
if let err_ub!(ValidationFailure(_)) = error.kind {
331331
let err = crate::const_eval::error_to_const_error(&ecx, error);
332332
match err.struct_error(
333333
ecx.tcx,
@@ -390,7 +390,7 @@ pub fn intern_const_alloc_recursive<M: CompileTimeMachine<'mir, 'tcx>>(
390390
}
391391
} else if ecx.memory.dead_alloc_map.contains_key(&alloc_id) {
392392
// dangling pointer
393-
throw_unsup!(ValidationFailure("encountered dangling pointer in final constant".into()))
393+
throw_ub_format!("encountered dangling pointer in final constant")
394394
} else if ecx.tcx.alloc_map.lock().get(alloc_id).is_none() {
395395
// We have hit an `AllocId` that is neither in local or global memory and isn't marked
396396
// as dangling by local memory.

src/librustc_mir/interpret/intrinsics.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
135135
let bits = self.force_bits(val, layout_of.size)?;
136136
let kind = match layout_of.abi {
137137
ty::layout::Abi::Scalar(ref scalar) => scalar.value,
138-
_ => throw_unsup!(TypeNotPrimitive(ty)),
138+
_ => bug!("{} called on invalid type {:?}", intrinsic_name, ty),
139139
};
140140
let (nonzero, intrinsic_name) = match intrinsic_name {
141141
sym::cttz_nonzero => (true, sym::cttz),
@@ -246,9 +246,9 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
246246
let layout = self.layout_of(substs.type_at(0))?;
247247
let r_val = self.force_bits(r.to_scalar()?, layout.size)?;
248248
if let sym::unchecked_shl | sym::unchecked_shr = intrinsic_name {
249-
throw_ub_format!("Overflowing shift by {} in `{}`", r_val, intrinsic_name);
249+
throw_ub_format!("overflowing shift by {} in `{}`", r_val, intrinsic_name);
250250
} else {
251-
throw_ub_format!("Overflow executing `{}`", intrinsic_name);
251+
throw_ub_format!("overflow executing `{}`", intrinsic_name);
252252
}
253253
}
254254
self.write_scalar(val, dest)?;

src/librustc_mir/interpret/machine.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,8 +286,10 @@ pub trait Machine<'mir, 'tcx>: Sized {
286286
int: u64,
287287
) -> InterpResult<'tcx, Pointer<Self::PointerTag>> {
288288
Err((if int == 0 {
289-
err_unsup!(InvalidNullPointerUsage)
289+
// This is UB, seriously.
290+
err_ub!(InvalidIntPointerUsage(0))
290291
} else {
292+
// This is just something we cannot support during const-eval.
291293
err_unsup!(ReadBytesAsPointer)
292294
})
293295
.into())

0 commit comments

Comments
 (0)