1 //! Check the validity invariant of a given value, and tell the user
2 //! where in the value it got violated.
3 //! In const context, this goes even further and tries to approximate const safety.
4 //! That's useful because it means other passes (e.g. promotion) can rely on `const`s
7 use std
::convert
::TryFrom
;
9 use std
::num
::NonZeroUsize
;
10 use std
::ops
::RangeInclusive
;
12 use rustc_data_structures
::fx
::FxHashSet
;
14 use rustc_middle
::mir
::interpret
::{InterpError, InterpErrorInfo}
;
16 use rustc_middle
::ty
::layout
::TyAndLayout
;
17 use rustc_span
::symbol
::{sym, Symbol}
;
18 use rustc_target
::abi
::{Abi, LayoutOf, Scalar, Size, VariantIdx, Variants}
;
23 CheckInAllocMsg
, GlobalAlloc
, InterpCx
, InterpResult
, MPlaceTy
, Machine
, MemPlaceMeta
, OpTy
,
27 macro_rules
! throw_validation_failure
{
28 ($
where:expr
, { $( $what_fmt:expr ),+ } $
( expected { $( $expected_fmt:expr ),+ }
)?
) => {{
29 let msg
= rustc_middle
::ty
::print
::with_no_trimmed_paths(|| {
30 let mut msg
= String
::new();
31 msg
.push_str("encountered ");
32 write
!(&mut msg
, $
($what_fmt
),+).unwrap();
34 if !where_
.is_empty() {
36 write_path(&mut msg
, where_
);
39 msg
.push_str(", but expected ");
40 write
!(&mut msg
, $
($expected_fmt
),+).unwrap();
45 throw_ub
!(ValidationFailure(msg
))
49 /// If $e throws an error matching the pattern, throw a validation failure.
50 /// Other errors are passed back to the caller, unchanged -- and if they reach the root of
51 /// the visitor, we make sure only validation errors and `InvalidProgram` errors are left.
52 /// This lets you use the patterns as a kind of validation list, asserting which errors
53 /// can possibly happen:
56 /// let v = try_validation!(some_fn(), some_path, {
57 /// Foo | Bar | Baz => { "some failure" },
61 /// An additional expected parameter can also be added to the failure message:
64 /// let v = try_validation!(some_fn(), some_path, {
65 /// Foo | Bar | Baz => { "some failure" } expected { "something that wasn't a failure" },
69 /// An additional nicety is that both parameters actually take format args, so you can just write
70 /// the format string in directly:
73 /// let v = try_validation!(some_fn(), some_path, {
74 /// Foo | Bar | Baz => { "{:?}", some_failure } expected { "{}", expected_value },
78 macro_rules
! try_validation
{
79 ($e
:expr
, $
where:expr
,
80 $
( $
( $p
:pat
)|+ => { $( $what_fmt:expr ),+ } $
( expected { $( $expected_fmt:expr ),+ }
)?
),+ $
(,)?
84 // We catch the error and turn it into a validation failure. We are okay with
85 // allocation here as this can only slow down builds that fail anyway.
86 $
( $
( Err(InterpErrorInfo { kind: $p, .. }
) )|+ =>
87 throw_validation_failure
!(
89 { $( $what_fmt ),+ } $
( expected { $( $expected_fmt ),+ }
)?
92 #[allow(unreachable_patterns)]
93 Err(e
) => Err
::<!, _
>(e
)?
,
98 /// We want to show a nice path to the invalid field for diagnostics,
99 /// but avoid string operations in the happy case where no error happens.
100 /// So we track a `Vec<PathElem>` where `PathElem` contains all the data we
101 /// need to later print something for the user.
102 #[derive(Copy, Clone, Debug)]
106 GeneratorState(VariantIdx
),
116 /// Extra things to check for during validation of CTFE results.
117 pub enum CtfeValidationMode
{
118 /// Regular validation, nothing special happening.
120 /// Validation of a `const`.
121 /// `inner` says if this is an inner, indirect allocation (as opposed to the top-level const
122 /// allocation). Being an inner allocation makes a difference because the top-level allocation
123 /// of a `const` is copied for each use, but the inner allocations are implicitly shared.
124 /// `allow_static_ptrs` says if pointers to statics are permitted (which is the case for promoteds in statics).
125 Const { inner: bool, allow_static_ptrs: bool }
,
128 /// State for tracking recursive validation of references
129 pub struct RefTracking
<T
, PATH
= ()> {
130 pub seen
: FxHashSet
<T
>,
131 pub todo
: Vec
<(T
, PATH
)>,
134 impl<T
: Copy
+ Eq
+ Hash
+ std
::fmt
::Debug
, PATH
: Default
> RefTracking
<T
, PATH
> {
135 pub fn empty() -> Self {
136 RefTracking { seen: FxHashSet::default(), todo: vec![] }
138 pub fn new(op
: T
) -> Self {
139 let mut ref_tracking_for_consts
=
140 RefTracking { seen: FxHashSet::default(), todo: vec![(op, PATH::default())] }
;
141 ref_tracking_for_consts
.seen
.insert(op
);
142 ref_tracking_for_consts
145 pub fn track(&mut self, op
: T
, path
: impl FnOnce() -> PATH
) {
146 if self.seen
.insert(op
) {
147 trace
!("Recursing below ptr {:#?}", op
);
149 // Remember to come back to this later.
150 self.todo
.push((op
, path
));
156 fn write_path(out
: &mut String
, path
: &[PathElem
]) {
157 use self::PathElem
::*;
159 for elem
in path
.iter() {
161 Field(name
) => write
!(out
, ".{}", name
),
162 EnumTag
=> write
!(out
, ".<enum-tag>"),
163 Variant(name
) => write
!(out
, ".<enum-variant({})>", name
),
164 GeneratorTag
=> write
!(out
, ".<generator-tag>"),
165 GeneratorState(idx
) => write
!(out
, ".<generator-state({})>", idx
.index()),
166 CapturedVar(name
) => write
!(out
, ".<captured-var({})>", name
),
167 TupleElem(idx
) => write
!(out
, ".{}", idx
),
168 ArrayElem(idx
) => write
!(out
, "[{}]", idx
),
169 // `.<deref>` does not match Rust syntax, but it is more readable for long paths -- and
170 // some of the other items here also are not Rust syntax. Actually we can't
171 // even use the usual syntax because we are just showing the projections,
173 Deref
=> write
!(out
, ".<deref>"),
174 DynDowncast
=> write
!(out
, ".<dyn-downcast>"),
180 // Test if a range that wraps at overflow contains `test`
181 fn wrapping_range_contains(r
: &RangeInclusive
<u128
>, test
: u128
) -> bool
{
182 let (lo
, hi
) = r
.clone().into_inner();
185 (..=hi
).contains(&test
) || (lo
..).contains(&test
)
192 // Formats such that a sentence like "expected something {}" to mean
193 // "expected something <in the given range>" makes sense.
194 fn wrapping_range_format(r
: &RangeInclusive
<u128
>, max_hi
: u128
) -> String
{
195 let (lo
, hi
) = r
.clone().into_inner();
196 assert
!(hi
<= max_hi
);
198 format
!("less or equal to {}, or greater or equal to {}", hi
, lo
)
200 format
!("equal to {}", lo
)
202 assert
!(hi
< max_hi
, "should not be printing if the range covers everything");
203 format
!("less or equal to {}", hi
)
204 } else if hi
== max_hi
{
205 assert
!(lo
> 0, "should not be printing if the range covers everything");
206 format
!("greater or equal to {}", lo
)
208 format
!("in the range {:?}", r
)
212 struct ValidityVisitor
<'rt
, 'mir
, 'tcx
, M
: Machine
<'mir
, 'tcx
>> {
213 /// The `path` may be pushed to, but the part that is present when a function
214 /// starts must not be changed! `visit_fields` and `visit_array` rely on
215 /// this stack discipline.
217 ref_tracking
: Option
<&'rt
mut RefTracking
<MPlaceTy
<'tcx
, M
::PointerTag
>, Vec
<PathElem
>>>,
218 /// `None` indicates this is not validating for CTFE (but for runtime).
219 ctfe_mode
: Option
<CtfeValidationMode
>,
220 ecx
: &'rt InterpCx
<'mir
, 'tcx
, M
>,
223 impl<'rt
, 'mir
, 'tcx
: 'mir
, M
: Machine
<'mir
, 'tcx
>> ValidityVisitor
<'rt
, 'mir
, 'tcx
, M
> {
224 fn aggregate_field_path_elem(&mut self, layout
: TyAndLayout
<'tcx
>, field
: usize) -> PathElem
{
225 // First, check if we are projecting to a variant.
226 match layout
.variants
{
227 Variants
::Multiple { tag_field, .. }
=> {
228 if tag_field
== field
{
229 return match layout
.ty
.kind() {
230 ty
::Adt(def
, ..) if def
.is_enum() => PathElem
::EnumTag
,
231 ty
::Generator(..) => PathElem
::GeneratorTag
,
232 _
=> bug
!("non-variant type {:?}", layout
.ty
),
236 Variants
::Single { .. }
=> {}
239 // Now we know we are projecting to a field, so figure out which one.
240 match layout
.ty
.kind() {
241 // generators and closures.
242 ty
::Closure(def_id
, _
) | ty
::Generator(def_id
, _
, _
) => {
244 if let Some(def_id
) = def_id
.as_local() {
245 let tables
= self.ecx
.tcx
.typeck(def_id
);
246 if let Some(upvars
) = tables
.closure_captures
.get(&def_id
.to_def_id()) {
247 // Sometimes the index is beyond the number of upvars (seen
249 if let Some((&var_hir_id
, _
)) = upvars
.get_index(field
) {
250 let node
= self.ecx
.tcx
.hir().get(var_hir_id
);
251 if let hir
::Node
::Binding(pat
) = node
{
252 if let hir
::PatKind
::Binding(_
, _
, ident
, _
) = pat
.kind
{
253 name
= Some(ident
.name
);
260 PathElem
::CapturedVar(name
.unwrap_or_else(|| {
261 // Fall back to showing the field index.
267 ty
::Tuple(_
) => PathElem
::TupleElem(field
),
270 ty
::Adt(def
, ..) if def
.is_enum() => {
271 // we might be projecting *to* a variant, or to a field *in* a variant.
272 match layout
.variants
{
273 Variants
::Single { index }
=> {
275 PathElem
::Field(def
.variants
[index
].fields
[field
].ident
.name
)
277 Variants
::Multiple { .. }
=> bug
!("we handled variants above"),
282 ty
::Adt(def
, _
) => PathElem
::Field(def
.non_enum_variant().fields
[field
].ident
.name
),
285 ty
::Array(..) | ty
::Slice(..) => PathElem
::ArrayElem(field
),
288 ty
::Dynamic(..) => PathElem
::DynDowncast
,
290 // nothing else has an aggregate layout
291 _
=> bug
!("aggregate_field_path_elem: got non-aggregate type {:?}", layout
.ty
),
298 f
: impl FnOnce(&mut Self) -> InterpResult
<'tcx
, R
>,
299 ) -> InterpResult
<'tcx
, R
> {
300 // Remember the old state
301 let path_len
= self.path
.len();
302 // Record new element
303 self.path
.push(elem
);
307 self.path
.truncate(path_len
);
312 fn check_wide_ptr_meta(
314 meta
: MemPlaceMeta
<M
::PointerTag
>,
315 pointee
: TyAndLayout
<'tcx
>,
316 ) -> InterpResult
<'tcx
> {
317 let tail
= self.ecx
.tcx
.struct_tail_erasing_lifetimes(pointee
.ty
, self.ecx
.param_env
);
320 let vtable
= meta
.unwrap_meta();
321 // Direct call to `check_ptr_access_align` checks alignment even on CTFE machines.
323 self.ecx
.memory
.check_ptr_access_align(
325 3 * self.ecx
.tcx
.data_layout
.pointer_size
, // drop, size, align
326 Some(self.ecx
.tcx
.data_layout
.pointer_align
.abi
),
327 CheckInAllocMsg
::InboundsTest
,
330 err_ub
!(DanglingIntPointer(..)) |
331 err_ub
!(PointerUseAfterFree(..)) |
332 err_unsup
!(ReadBytesAsPointer
) =>
333 { "dangling vtable pointer in wide pointer" }
,
334 err_ub
!(AlignmentCheckFailed { .. }
) =>
335 { "unaligned vtable pointer in wide pointer" }
,
336 err_ub
!(PointerOutOfBounds { .. }
) =>
337 { "too small vtable" }
,
340 self.ecx
.read_drop_type_from_vtable(vtable
),
342 err_ub
!(DanglingIntPointer(..)) |
343 err_ub
!(InvalidFunctionPointer(..)) |
344 err_unsup
!(ReadBytesAsPointer
) =>
345 { "invalid drop function pointer in vtable (not pointing to a function)" }
,
346 err_ub
!(InvalidDropFn(..)) =>
347 { "invalid drop function pointer in vtable (function has incompatible signature)" }
,
350 self.ecx
.read_size_and_align_from_vtable(vtable
),
352 err_unsup
!(ReadPointerAsBytes
) => { "invalid size or align in vtable" }
,
354 // FIXME: More checks for the vtable.
356 ty
::Slice(..) | ty
::Str
=> {
357 let _len
= try_validation
!(
358 meta
.unwrap_meta().to_machine_usize(self.ecx
),
360 err_unsup
!(ReadPointerAsBytes
) => { "non-integer slice length in wide pointer" }
,
362 // We do not check that `len * elem_size <= isize::MAX`:
363 // that is only required for references, and there it falls out of the
364 // "dereferenceable" check performed by Stacked Borrows.
367 // Unsized, but not wide.
369 _
=> bug
!("Unexpected unsized type tail: {:?}", tail
),
375 /// Check a reference or `Box`.
376 fn check_safe_pointer(
378 value
: OpTy
<'tcx
, M
::PointerTag
>,
380 ) -> InterpResult
<'tcx
> {
381 let value
= self.ecx
.read_immediate(value
)?
;
382 // Handle wide pointers.
383 // Check metadata early, for better diagnostics
384 let place
= try_validation
!(
385 self.ecx
.ref_to_mplace(value
),
387 err_ub
!(InvalidUninitBytes(None
)) => { "uninitialized {}
", kind },
389 if place.layout.is_unsized() {
390 self.check_wide_ptr_meta(place.meta, place.layout)?;
392 // Make sure this is dereferenceable and all.
393 let size_and_align = try_validation!(
394 self.ecx.size_and_align_of_mplace(place),
396 err_ub!(InvalidMeta(msg)) => { "invalid {} metadata: {}", kind
, msg
},
398 let (size
, align
) = size_and_align
399 // for the purpose of validity, consider foreign types to have
400 // alignment and size determined by the layout (size will be 0,
401 // alignment should take attributes into account).
402 .unwrap_or_else(|| (place
.layout
.size
, place
.layout
.align
.abi
));
403 // Direct call to `check_ptr_access_align` checks alignment even on CTFE machines.
404 let ptr
: Option
<_
> = try_validation
!(
405 self.ecx
.memory
.check_ptr_access_align(
409 CheckInAllocMsg
::InboundsTest
,
412 err_ub
!(AlignmentCheckFailed { required, has }
) =>
414 "an unaligned {} (required {} byte alignment but found {})",
419 err_ub
!(DanglingIntPointer(0, _
)) =>
420 { "a NULL {}
", kind },
421 err_ub!(DanglingIntPointer(i, _)) =>
422 { "a dangling {} (address 0x{:x} is unallocated)", kind
, i
},
423 err_ub
!(PointerOutOfBounds { .. }
) =>
424 { "a dangling {}
(going beyond the bounds of its allocation
)", kind },
425 err_unsup!(ReadBytesAsPointer) =>
426 { "a dangling {} (created from integer)", kind
},
427 // This cannot happen during const-eval (because interning already detects
428 // dangling pointers), but it can happen in Miri.
429 err_ub
!(PointerUseAfterFree(..)) =>
430 { "a dangling {}
(use-after
-free
)", kind },
432 // Recursive checking
433 if let Some(ref mut ref_tracking) = self.ref_tracking {
434 if let Some(ptr) = ptr {
436 // Skip validation entirely for some external statics
437 let alloc_kind = self.ecx.tcx.get_global_alloc(ptr.alloc_id);
438 if let Some(GlobalAlloc::Static(did)) = alloc_kind {
439 assert!(!self.ecx.tcx.is_thread_local_static(did));
440 assert!(self.ecx.tcx.is_static(did));
443 Some(CtfeValidationMode::Const { allow_static_ptrs: false, .. })
445 // See const_eval::machine::MemoryExtra::can_access_statics for why
446 // this check is so important.
447 // This check is reachable when the const just referenced the static,
448 // but never read it (so we never entered `before_access_global`).
449 throw_validation_failure!(self.path,
450 { "a {} pointing to a static variable", kind
}
453 // We skip checking other statics. These statics must be sound by
454 // themselves, and the only way to get broken statics here is by using
456 // The reasons we don't check other statics is twofold. For one, in all
457 // sound cases, the static was already validated on its own, and second, we
458 // trigger cycle errors if we try to compute the value of the other static
459 // and that static refers back to us.
460 // We might miss const-invalid data,
461 // but things are still sound otherwise (in particular re: consts
462 // referring to statics).
466 // Proceed recursively even for ZST, no reason to skip them!
467 // `!` is a ZST and we want to validate it.
468 // Normalize before handing `place` to tracking because that will
469 // check for duplicates.
470 let place
= if size
.bytes() > 0 {
471 self.ecx
.force_mplace_ptr(place
).expect("we already bounds-checked")
475 let path
= &self.path
;
476 ref_tracking
.track(place
, || {
477 // We need to clone the path anyway, make sure it gets created
478 // with enough space for the additional `Deref`.
479 let mut new_path
= Vec
::with_capacity(path
.len() + 1);
480 new_path
.clone_from(path
);
481 new_path
.push(PathElem
::Deref
);
488 /// Check if this is a value of primitive type, and if yes check the validity of the value
489 /// at that type. Return `true` if the type is indeed primitive.
490 fn try_visit_primitive(
492 value
: OpTy
<'tcx
, M
::PointerTag
>,
493 ) -> InterpResult
<'tcx
, bool
> {
494 // Go over all the primitive types
495 let ty
= value
.layout
.ty
;
498 let value
= self.ecx
.read_scalar(value
)?
;
502 err_ub
!(InvalidBool(..)) | err_ub
!(InvalidUninitBytes(None
)) =>
503 { "{}
", value } expected { "a boolean" },
508 let value = self.ecx.read_scalar(value)?;
512 err_ub!(InvalidChar(..)) | err_ub!(InvalidUninitBytes(None)) =>
513 { "{}", value
} expected { "a valid unicode scalar value (in `0..=0x10FFFF` but not in `0xD800..=0xDFFF`)" }
,
517 ty
::Float(_
) | ty
::Int(_
) | ty
::Uint(_
) => {
518 let value
= try_validation
!(
519 self.ecx
.read_scalar(value
),
521 err_unsup
!(ReadPointerAsBytes
) => { "read of part of a pointer" }
,
523 // NOTE: Keep this in sync with the array optimization for int/float
525 if self.ctfe_mode
.is_some() {
526 // Integers/floats in CTFE: Must be scalar bits, pointers are dangerous
527 let is_bits
= value
.check_init().map_or(false, |v
| v
.is_bits());
529 throw_validation_failure
!(self.path
,
530 { "{}
", value } expected { "initialized plain (non-pointer) bytes" }
534 // At run-time, for now, we accept *anything* for these types, including
535 // uninit. We should fix that, but let's start low.
540 // We are conservative with uninit for integers, but try to
541 // actually enforce the strict rules for raw pointers (mostly because
542 // that lets us re-use `ref_to_mplace`).
543 let place = try_validation!(
544 self.ecx.ref_to_mplace(self.ecx.read_immediate(value)?),
546 err_ub!(InvalidUninitBytes(None)) => { "uninitialized raw pointer" },
548 if place.layout.is_unsized() {
549 self.check_wide_ptr_meta(place.meta, place.layout)?;
553 ty::Ref(_, ty, mutbl) => {
554 if matches!(self.ctfe_mode, Some(CtfeValidationMode::Const { .. }))
555 && *mutbl == hir::Mutability::Mut
557 // A mutable reference inside a const? That does not seem right (except if it is
559 let layout = self.ecx.layout_of(ty)?;
560 if !layout.is_zst() {
561 throw_validation_failure!(self.path, { "mutable reference in a `const`" });
564 self.check_safe_pointer(value, "reference
")?;
567 ty::Adt(def, ..) if def.is_box() => {
568 self.check_safe_pointer(value, "box")?;
572 let value = self.ecx.read_scalar(value)?;
573 let _fn = try_validation!(
574 value.check_init().and_then(|ptr| self.ecx.memory.get_fn(ptr)),
576 err_ub!(DanglingIntPointer(..)) |
577 err_ub!(InvalidFunctionPointer(..)) |
578 err_ub!(InvalidUninitBytes(None)) |
579 err_unsup!(ReadBytesAsPointer) =>
580 { "{}", value
} expected { "a function pointer" }
,
582 // FIXME: Check if the signature matches
585 ty
::Never
=> throw_validation_failure
!(self.path
, { "a value of the never type `!`" }
),
586 ty
::Foreign(..) | ty
::FnDef(..) => {
590 // The above should be all the primitive types. The rest is compound, we
591 // check them by visiting their fields/variants.
599 | ty
::Generator(..) => Ok(false),
600 // Some types only occur during typechecking, they have no layout.
601 // We should not see them here and we could not check them anyway.
604 | ty
::Placeholder(..)
609 | ty
::GeneratorWitness(..) => bug
!("Encountered invalid type {:?}", ty
),
615 op
: OpTy
<'tcx
, M
::PointerTag
>,
616 scalar_layout
: &Scalar
,
617 ) -> InterpResult
<'tcx
> {
618 let value
= self.ecx
.read_scalar(op
)?
;
619 let valid_range
= &scalar_layout
.valid_range
;
620 let (lo
, hi
) = valid_range
.clone().into_inner();
621 // Determine the allowed range
622 // `max_hi` is as big as the size fits
623 let max_hi
= u128
::MAX
>> (128 - op
.layout
.size
.bits());
624 assert
!(hi
<= max_hi
);
625 // We could also write `(hi + 1) % (max_hi + 1) == lo` but `max_hi + 1` overflows for `u128`
626 if (lo
== 0 && hi
== max_hi
) || (hi
+ 1 == lo
) {
630 // At least one value is excluded. Get the bits.
631 let value
= try_validation
!(
634 err_ub
!(InvalidUninitBytes(None
)) => { "{}
", value }
635 expected { "something {}", wrapping_range_format(valid_range
, max_hi
) },
637 let bits
= match value
.to_bits_or_ptr(op
.layout
.size
, self.ecx
) {
639 if lo
== 1 && hi
== max_hi
{
640 // Only NULL is the niche. So make sure the ptr is NOT NULL.
641 if self.ecx
.memory
.ptr_may_be_null(ptr
) {
642 throw_validation_failure
!(self.path
,
643 { "a potentially NULL pointer" }
645 "something that cannot possibly fail to be {}",
646 wrapping_range_format(valid_range
, max_hi
)
652 // Conservatively, we reject, because the pointer *could* have a bad
654 throw_validation_failure
!(self.path
,
657 "something that cannot possibly fail to be {}",
658 wrapping_range_format(valid_range
, max_hi
)
665 // Now compare. This is slightly subtle because this is a special "wrap-around" range.
666 if wrapping_range_contains(&valid_range
, bits
) {
669 throw_validation_failure
!(self.path
,
671 expected { "something {}", wrapping_range_format(valid_range
, max_hi
) }
677 impl<'rt
, 'mir
, 'tcx
: 'mir
, M
: Machine
<'mir
, 'tcx
>> ValueVisitor
<'mir
, 'tcx
, M
>
678 for ValidityVisitor
<'rt
, 'mir
, 'tcx
, M
>
680 type V
= OpTy
<'tcx
, M
::PointerTag
>;
683 fn ecx(&self) -> &InterpCx
<'mir
, 'tcx
, M
> {
687 fn read_discriminant(
689 op
: OpTy
<'tcx
, M
::PointerTag
>,
690 ) -> InterpResult
<'tcx
, VariantIdx
> {
691 self.with_elem(PathElem
::EnumTag
, move |this
| {
693 this
.ecx
.read_discriminant(op
),
695 err_ub
!(InvalidTag(val
)) =>
696 { "{}
", val } expected { "a valid enum tag" },
697 err_ub!(InvalidUninitBytes(None)) =>
698 { "uninitialized bytes" } expected { "a valid enum tag" },
699 err_unsup!(ReadPointerAsBytes) =>
700 { "a pointer" } expected { "a valid enum tag" },
709 old_op: OpTy<'tcx, M::PointerTag>,
711 new_op: OpTy<'tcx, M::PointerTag>,
712 ) -> InterpResult<'tcx> {
713 let elem = self.aggregate_field_path_elem(old_op.layout, field);
714 self.with_elem(elem, move |this| this.visit_value(new_op))
720 old_op: OpTy<'tcx, M::PointerTag>,
721 variant_id: VariantIdx,
722 new_op: OpTy<'tcx, M::PointerTag>,
723 ) -> InterpResult<'tcx> {
724 let name = match old_op.layout.ty.kind() {
725 ty::Adt(adt, _) => PathElem::Variant(adt.variants[variant_id].ident.name),
726 // Generators also have variants
727 ty::Generator(..) => PathElem::GeneratorState(variant_id),
728 _ => bug!("Unexpected
type with variant
: {:?}
", old_op.layout.ty),
730 self.with_elem(name, move |this| this.visit_value(new_op))
736 _op: OpTy<'tcx, M::PointerTag>,
737 _fields: NonZeroUsize,
738 ) -> InterpResult<'tcx> {
743 fn visit_value(&mut self, op: OpTy<'tcx, M::PointerTag>) -> InterpResult<'tcx> {
744 trace!("visit_value
: {:?}
, {:?}
", *op, op.layout);
746 // Check primitive types -- the leafs of our recursive descend.
747 if self.try_visit_primitive(op)? {
750 // Sanity check: `builtin_deref` does not know any pointers that are not primitive.
751 assert!(op.layout.ty.builtin_deref(true).is_none());
753 // Special check preventing `UnsafeCell` in the inner part of constants
754 if let Some(def) = op.layout.ty.ty_adt_def() {
755 if matches!(self.ctfe_mode, Some(CtfeValidationMode::Const { inner: true, .. }))
756 && Some(def.did) == self.ecx.tcx.lang_items().unsafe_cell_type()
758 throw_validation_failure!(self.path, { "`UnsafeCell` in a `const`" });
762 // Recursively walk the value at its type.
763 self.walk_value(op)?;
765 // *After* all of this, check the ABI. We need to check the ABI to handle
766 // types like `NonNull` where the `Scalar` info is more restrictive than what
767 // the fields say (`rustc_layout_scalar_valid_range_start`).
768 // But in most cases, this will just propagate what the fields say,
769 // and then we want the error to point at the field -- so, first recurse,
772 // FIXME: We could avoid some redundant checks here. For newtypes wrapping
773 // scalars, we do the same check on every "level
" (e.g., first we check
774 // MyNewtype and then the scalar in there).
775 match op.layout.abi {
776 Abi::Uninhabited => {
777 throw_validation_failure!(self.path,
778 { "a value of uninhabited type {:?}", op
.layout
.ty
}
781 Abi
::Scalar(ref scalar_layout
) => {
782 self.visit_scalar(op
, scalar_layout
)?
;
784 Abi
::ScalarPair { .. }
| Abi
::Vector { .. }
=> {
785 // These have fields that we already visited above, so we already checked
786 // all their scalar-level restrictions.
787 // There is also no equivalent to `rustc_layout_scalar_valid_range_start`
788 // that would make skipping them here an issue.
790 Abi
::Aggregate { .. }
=> {
800 op
: OpTy
<'tcx
, M
::PointerTag
>,
801 fields
: impl Iterator
<Item
= InterpResult
<'tcx
, Self::V
>>,
802 ) -> InterpResult
<'tcx
> {
803 match op
.layout
.ty
.kind() {
805 let mplace
= op
.assert_mem_place(self.ecx
); // strings are never immediate
806 let len
= mplace
.len(self.ecx
)?
;
808 self.ecx
.memory
.read_bytes(mplace
.ptr
, Size
::from_bytes(len
)),
810 err_ub
!(InvalidUninitBytes(..)) => { "uninitialized data in `str`" }
,
813 ty
::Array(tys
, ..) | ty
::Slice(tys
)
814 // This optimization applies for types that can hold arbitrary bytes (such as
815 // integer and floating point types) or for structs or tuples with no fields.
816 // FIXME(wesleywiser) This logic could be extended further to arbitrary structs
817 // or tuples made up of integer/floating point types or inhabited ZSTs with no
819 if matches
!(tys
.kind(), ty
::Int(..) | ty
::Uint(..) | ty
::Float(..))
822 // Optimized handling for arrays of integer/float type.
824 // Arrays cannot be immediate, slices are never immediate.
825 let mplace
= op
.assert_mem_place(self.ecx
);
826 // This is the length of the array/slice.
827 let len
= mplace
.len(self.ecx
)?
;
828 // Zero length slices have nothing to be checked.
832 // This is the element type size.
833 let layout
= self.ecx
.layout_of(tys
)?
;
834 // This is the size in bytes of the whole array. (This checks for overflow.)
835 let size
= layout
.size
* len
;
836 // Size is not 0, get a pointer.
837 let ptr
= self.ecx
.force_ptr(mplace
.ptr
)?
;
839 // Optimization: we just check the entire range at once.
840 // NOTE: Keep this in sync with the handling of integer and float
841 // types above, in `visit_primitive`.
842 // In run-time mode, we accept pointers in here. This is actually more
843 // permissive than a per-element check would be, e.g., we accept
844 // an &[u8] that contains a pointer even though bytewise checking would
845 // reject it. However, that's good: We don't inherently want
846 // to reject those pointers, we just do not have the machinery to
847 // talk about parts of a pointer.
848 // We also accept uninit, for consistency with the slow path.
849 match self.ecx
.memory
.get_raw(ptr
.alloc_id
)?
.check_bytes(
853 /*allow_uninit_and_ptr*/ self.ctfe_mode
.is_none(),
855 // In the happy case, we needn't check anything else.
857 // Some error happened, try to provide a more detailed description.
859 // For some errors we might be able to provide extra information.
860 // (This custom logic does not fit the `try_validation!` macro.)
862 err_ub
!(InvalidUninitBytes(Some(access
))) => {
863 // Some byte was uninitialized, determine which
864 // element that byte belongs to so we can
866 let i
= usize::try_from(
867 access
.uninit_ptr
.offset
.bytes() / layout
.size
.bytes(),
870 self.path
.push(PathElem
::ArrayElem(i
));
872 throw_validation_failure
!(self.path
, { "uninitialized bytes" }
)
874 err_unsup
!(ReadPointerAsBytes
) => {
875 throw_validation_failure
!(self.path
, { "a pointer" } expected { "plain (non-pointer) bytes" }
)
878 // Propagate upwards (that will also check for unexpected errors).
879 _
=> return Err(err
),
884 // Fast path for arrays and slices of ZSTs. We only need to check a single ZST element
885 // of an array and not all of them, because there's only a single value of a specific
886 // ZST type, so either validation fails for all elements or none.
887 ty
::Array(tys
, ..) | ty
::Slice(tys
) if self.ecx
.layout_of(tys
)?
.is_zst() => {
888 // Validate just the first element (if any).
889 self.walk_aggregate(op
, fields
.take(1))?
892 self.walk_aggregate(op
, fields
)?
// default handler
899 impl<'mir
, 'tcx
: 'mir
, M
: Machine
<'mir
, 'tcx
>> InterpCx
<'mir
, 'tcx
, M
> {
900 fn validate_operand_internal(
902 op
: OpTy
<'tcx
, M
::PointerTag
>,
904 ref_tracking
: Option
<&mut RefTracking
<MPlaceTy
<'tcx
, M
::PointerTag
>, Vec
<PathElem
>>>,
905 ctfe_mode
: Option
<CtfeValidationMode
>,
906 ) -> InterpResult
<'tcx
> {
907 trace
!("validate_operand_internal: {:?}, {:?}", *op
, op
.layout
.ty
);
909 // Construct a visitor
910 let mut visitor
= ValidityVisitor { path, ref_tracking, ctfe_mode, ecx: self }
;
912 // Try to cast to ptr *once* instead of all the time.
913 let op
= self.force_op_ptr(op
).unwrap_or(op
);
916 match visitor
.visit_value(op
) {
918 // Pass through validation failures.
919 Err(err
) if matches
!(err
.kind
, err_ub
!(ValidationFailure { .. }
)) => Err(err
),
920 // Also pass through InvalidProgram, those just indicate that we could not
921 // validate and each caller will know best what to do with them.
922 Err(err
) if matches
!(err
.kind
, InterpError
::InvalidProgram(_
)) => Err(err
),
923 // Avoid other errors as those do not show *where* in the value the issue lies.
925 err
.print_backtrace();
926 bug
!("Unexpected error during validation: {}", err
);
931 /// This function checks the data at `op` to be const-valid.
932 /// `op` is assumed to cover valid memory if it is an indirect operand.
933 /// It will error if the bits at the destination do not match the ones described by the layout.
935 /// `ref_tracking` is used to record references that we encounter so that they
936 /// can be checked recursively by an outside driving loop.
938 /// `constant` controls whether this must satisfy the rules for constants:
939 /// - no pointers to statics.
940 /// - no `UnsafeCell` or non-ZST `&mut`.
942 pub fn const_validate_operand(
944 op
: OpTy
<'tcx
, M
::PointerTag
>,
946 ref_tracking
: &mut RefTracking
<MPlaceTy
<'tcx
, M
::PointerTag
>, Vec
<PathElem
>>,
947 ctfe_mode
: CtfeValidationMode
,
948 ) -> InterpResult
<'tcx
> {
949 self.validate_operand_internal(op
, path
, Some(ref_tracking
), Some(ctfe_mode
))
952 /// This function checks the data at `op` to be runtime-valid.
953 /// `op` is assumed to cover valid memory if it is an indirect operand.
954 /// It will error if the bits at the destination do not match the ones described by the layout.
956 pub fn validate_operand(&self, op
: OpTy
<'tcx
, M
::PointerTag
>) -> InterpResult
<'tcx
> {
957 self.validate_operand_internal(op
, vec
![], None
, None
)