Skip to content

Commit 1a1220c

Browse files
committed
validation should only catch UB errors
1 parent cf2c65b commit 1a1220c

File tree

1 file changed

+20
-17
lines changed

1 file changed

+20
-17
lines changed

compiler/rustc_const_eval/src/interpret/validity.rs

+20-17
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ use rustc_target::abi::{Abi, Scalar as ScalarAbi, Size, VariantIdx, Variants, Wr
1919

2020
use std::hash::Hash;
2121

22+
// for the validation errors
23+
use super::UndefinedBehaviorInfo::*;
2224
use super::{
2325
CheckInAllocMsg, GlobalAlloc, ImmTy, Immediate, InterpCx, InterpResult, MPlaceTy, Machine,
2426
MemPlaceMeta, OpTy, Scalar, ValueVisitor,
@@ -59,6 +61,7 @@ macro_rules! throw_validation_failure {
5961
/// });
6062
/// ```
6163
///
64+
/// The patterns must be of type `UndefinedBehaviorInfo`.
6265
/// An additional expected parameter can also be added to the failure message:
6366
///
6467
/// ```
@@ -86,7 +89,7 @@ macro_rules! try_validation {
8689
// allocation here as this can only slow down builds that fail anyway.
8790
Err(e) => match e.kind() {
8891
$(
89-
$($p)|+ =>
92+
InterpError::UndefinedBehavior($($p)|+) =>
9093
throw_validation_failure!(
9194
$where,
9295
{ $( $what_fmt ),+ } $( expected { $( $expected_fmt ),+ } )?
@@ -312,7 +315,7 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, '
312315
Ok(try_validation!(
313316
self.ecx.read_immediate(op),
314317
self.path,
315-
err_ub!(InvalidUninitBytes(None)) => { "uninitialized memory" } expected { "{expected}" }
318+
InvalidUninitBytes(None) => { "uninitialized memory" } expected { "{expected}" }
316319
))
317320
}
318321

@@ -337,8 +340,8 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, '
337340
let (_ty, _trait) = try_validation!(
338341
self.ecx.get_ptr_vtable(vtable),
339342
self.path,
340-
err_ub!(DanglingIntPointer(..)) |
341-
err_ub!(InvalidVTablePointer(..)) =>
343+
DanglingIntPointer(..) |
344+
InvalidVTablePointer(..) =>
342345
{ "{vtable}" } expected { "a vtable pointer" },
343346
);
344347
// FIXME: check if the type/trait match what ty::Dynamic says?
@@ -374,7 +377,7 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, '
374377
let size_and_align = try_validation!(
375378
self.ecx.size_and_align_of_mplace(&place),
376379
self.path,
377-
err_ub!(InvalidMeta(msg)) => { "invalid {} metadata: {}", kind, msg },
380+
InvalidMeta(msg) => { "invalid {} metadata: {}", kind, msg },
378381
);
379382
let (size, align) = size_and_align
380383
// for the purpose of validity, consider foreign types to have
@@ -390,21 +393,21 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, '
390393
CheckInAllocMsg::InboundsTest, // will anyway be replaced by validity message
391394
),
392395
self.path,
393-
err_ub!(AlignmentCheckFailed { required, has }) =>
396+
AlignmentCheckFailed { required, has } =>
394397
{
395398
"an unaligned {kind} (required {} byte alignment but found {})",
396399
required.bytes(),
397400
has.bytes()
398401
},
399-
err_ub!(DanglingIntPointer(0, _)) =>
402+
DanglingIntPointer(0, _) =>
400403
{ "a null {kind}" },
401-
err_ub!(DanglingIntPointer(i, _)) =>
404+
DanglingIntPointer(i, _) =>
402405
{ "a dangling {kind} (address {i:#x} is unallocated)" },
403-
err_ub!(PointerOutOfBounds { .. }) =>
406+
PointerOutOfBounds { .. } =>
404407
{ "a dangling {kind} (going beyond the bounds of its allocation)" },
405408
// This cannot happen during const-eval (because interning already detects
406409
// dangling pointers), but it can happen in Miri.
407-
err_ub!(PointerUseAfterFree(..)) =>
410+
PointerUseAfterFree(..) =>
408411
{ "a dangling {kind} (use-after-free)" },
409412
);
410413
// Do not allow pointers to uninhabited types.
@@ -475,7 +478,7 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, '
475478
try_validation!(
476479
value.to_bool(),
477480
self.path,
478-
err_ub!(InvalidBool(..)) =>
481+
InvalidBool(..) =>
479482
{ "{:x}", value } expected { "a boolean" },
480483
);
481484
Ok(true)
@@ -485,7 +488,7 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, '
485488
try_validation!(
486489
value.to_char(),
487490
self.path,
488-
err_ub!(InvalidChar(..)) =>
491+
InvalidChar(..) =>
489492
{ "{:x}", value } expected { "a valid unicode scalar value (in `0..=0x10FFFF` but not in `0xD800..=0xDFFF`)" },
490493
);
491494
Ok(true)
@@ -544,8 +547,8 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, '
544547
let _fn = try_validation!(
545548
self.ecx.get_ptr_fn(ptr),
546549
self.path,
547-
err_ub!(DanglingIntPointer(..)) |
548-
err_ub!(InvalidFunctionPointer(..)) =>
550+
DanglingIntPointer(..) |
551+
InvalidFunctionPointer(..) =>
549552
{ "{ptr}" } expected { "a function pointer" },
550553
);
551554
// FIXME: Check if the signature matches
@@ -660,9 +663,9 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValueVisitor<'mir, 'tcx, M>
660663
Ok(try_validation!(
661664
this.ecx.read_discriminant(op),
662665
this.path,
663-
err_ub!(InvalidTag(val)) =>
666+
InvalidTag(val) =>
664667
{ "{:x}", val } expected { "a valid enum tag" },
665-
err_ub!(InvalidUninitBytes(None)) =>
668+
InvalidUninitBytes(None) =>
666669
{ "uninitialized bytes" } expected { "a valid enum tag" },
667670
)
668671
.1)
@@ -805,7 +808,7 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValueVisitor<'mir, 'tcx, M>
805808
try_validation!(
806809
self.ecx.read_bytes_ptr_strip_provenance(mplace.ptr, Size::from_bytes(len)),
807810
self.path,
808-
err_ub!(InvalidUninitBytes(..)) => { "uninitialized data in `str`" },
811+
InvalidUninitBytes(..) => { "uninitialized data in `str`" },
809812
);
810813
}
811814
ty::Array(tys, ..) | ty::Slice(tys)

0 commit comments

Comments
 (0)