rustc_const_eval/interpret/
validity.rs

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
5//! to be const-safe.
6
7use std::borrow::Cow;
8use std::fmt::Write;
9use std::hash::Hash;
10use std::num::NonZero;
11
12use either::{Left, Right};
13use hir::def::DefKind;
14use rustc_abi::{
15    BackendRepr, FieldIdx, FieldsShape, Scalar as ScalarAbi, Size, VariantIdx, Variants,
16    WrappingRange,
17};
18use rustc_ast::Mutability;
19use rustc_data_structures::fx::FxHashSet;
20use rustc_hir as hir;
21use rustc_middle::bug;
22use rustc_middle::mir::interpret::ValidationErrorKind::{self, *};
23use rustc_middle::mir::interpret::{
24    ExpectedKind, InterpErrorKind, InvalidMetaKind, Misalignment, PointerKind, Provenance,
25    UnsupportedOpInfo, ValidationErrorInfo, alloc_range, interp_ok,
26};
27use rustc_middle::ty::layout::{LayoutCx, LayoutOf, TyAndLayout};
28use rustc_middle::ty::{self, Ty};
29use rustc_span::{Symbol, sym};
30use tracing::trace;
31
32use super::machine::AllocMap;
33use super::{
34    AllocId, CheckInAllocMsg, GlobalAlloc, ImmTy, Immediate, InterpCx, InterpResult, MPlaceTy,
35    Machine, MemPlaceMeta, PlaceTy, Pointer, Projectable, Scalar, ValueVisitor, err_ub,
36    format_interp_error,
37};
38
39// for the validation errors
40#[rustfmt::skip]
41use super::InterpErrorKind::UndefinedBehavior as Ub;
42use super::InterpErrorKind::Unsupported as Unsup;
43use super::UndefinedBehaviorInfo::*;
44use super::UnsupportedOpInfo::*;
45
46macro_rules! err_validation_failure {
47    ($where:expr, $kind: expr) => {{
48        let where_ = &$where;
49        let path = if !where_.is_empty() {
50            let mut path = String::new();
51            write_path(&mut path, where_);
52            Some(path)
53        } else {
54            None
55        };
56
57        err_ub!(ValidationError(ValidationErrorInfo { path, kind: $kind }))
58    }};
59}
60
61macro_rules! throw_validation_failure {
62    ($where:expr, $kind: expr) => {
63        do yeet err_validation_failure!($where, $kind)
64    };
65}
66
67/// If $e throws an error matching the pattern, throw a validation failure.
68/// Other errors are passed back to the caller, unchanged -- and if they reach the root of
69/// the visitor, we make sure only validation errors and `InvalidProgram` errors are left.
70/// This lets you use the patterns as a kind of validation list, asserting which errors
71/// can possibly happen:
72///
73/// ```ignore(illustrative)
74/// let v = try_validation!(some_fn(), some_path, {
75///     Foo | Bar | Baz => { "some failure" },
76/// });
77/// ```
78///
79/// The patterns must be of type `UndefinedBehaviorInfo`.
80/// An additional expected parameter can also be added to the failure message:
81///
82/// ```ignore(illustrative)
83/// let v = try_validation!(some_fn(), some_path, {
84///     Foo | Bar | Baz => { "some failure" } expected { "something that wasn't a failure" },
85/// });
86/// ```
87///
88/// An additional nicety is that both parameters actually take format args, so you can just write
89/// the format string in directly:
90///
91/// ```ignore(illustrative)
92/// let v = try_validation!(some_fn(), some_path, {
93///     Foo | Bar | Baz => { "{:?}", some_failure } expected { "{}", expected_value },
94/// });
95/// ```
96///
97macro_rules! try_validation {
98    ($e:expr, $where:expr,
99    $( $( $p:pat_param )|+ => $kind: expr ),+ $(,)?
100    ) => {{
101        $e.map_err_kind(|e| {
102            // We catch the error and turn it into a validation failure. We are okay with
103            // allocation here as this can only slow down builds that fail anyway.
104            match e {
105                $(
106                    $($p)|+ => {
107                        err_validation_failure!(
108                            $where,
109                            $kind
110                        )
111                    }
112                ),+,
113                e => e,
114            }
115        })?
116    }};
117}
118
119/// We want to show a nice path to the invalid field for diagnostics,
120/// but avoid string operations in the happy case where no error happens.
121/// So we track a `Vec<PathElem>` where `PathElem` contains all the data we
122/// need to later print something for the user.
123#[derive(Copy, Clone, Debug)]
124pub enum PathElem {
125    Field(Symbol),
126    Variant(Symbol),
127    CoroutineState(VariantIdx),
128    CapturedVar(Symbol),
129    ArrayElem(usize),
130    TupleElem(usize),
131    Deref,
132    EnumTag,
133    CoroutineTag,
134    DynDowncast,
135    Vtable,
136}
137
138/// Extra things to check for during validation of CTFE results.
139#[derive(Copy, Clone)]
140pub enum CtfeValidationMode {
141    /// Validation of a `static`
142    Static { mutbl: Mutability },
143    /// Validation of a promoted.
144    Promoted,
145    /// Validation of a `const`.
146    /// `allow_immutable_unsafe_cell` says whether we allow `UnsafeCell` in immutable memory (which is the
147    /// case for the top-level allocation of a `const`, where this is fine because the allocation will be
148    /// copied at each use site).
149    Const { allow_immutable_unsafe_cell: bool },
150}
151
152impl CtfeValidationMode {
153    fn allow_immutable_unsafe_cell(self) -> bool {
154        match self {
155            CtfeValidationMode::Static { .. } => false,
156            CtfeValidationMode::Promoted { .. } => false,
157            CtfeValidationMode::Const { allow_immutable_unsafe_cell, .. } => {
158                allow_immutable_unsafe_cell
159            }
160        }
161    }
162}
163
164/// State for tracking recursive validation of references
165pub struct RefTracking<T, PATH = ()> {
166    seen: FxHashSet<T>,
167    todo: Vec<(T, PATH)>,
168}
169
170impl<T: Clone + Eq + Hash + std::fmt::Debug, PATH: Default> RefTracking<T, PATH> {
171    pub fn empty() -> Self {
172        RefTracking { seen: FxHashSet::default(), todo: vec![] }
173    }
174    pub fn new(val: T) -> Self {
175        let mut ref_tracking_for_consts =
176            RefTracking { seen: FxHashSet::default(), todo: vec![(val.clone(), PATH::default())] };
177        ref_tracking_for_consts.seen.insert(val);
178        ref_tracking_for_consts
179    }
180    pub fn next(&mut self) -> Option<(T, PATH)> {
181        self.todo.pop()
182    }
183
184    fn track(&mut self, val: T, path: impl FnOnce() -> PATH) {
185        if self.seen.insert(val.clone()) {
186            trace!("Recursing below ptr {:#?}", val);
187            let path = path();
188            // Remember to come back to this later.
189            self.todo.push((val, path));
190        }
191    }
192}
193
194// FIXME make this translatable as well?
195/// Format a path
196fn write_path(out: &mut String, path: &[PathElem]) {
197    use self::PathElem::*;
198
199    for elem in path.iter() {
200        match elem {
201            Field(name) => write!(out, ".{name}"),
202            EnumTag => write!(out, ".<enum-tag>"),
203            Variant(name) => write!(out, ".<enum-variant({name})>"),
204            CoroutineTag => write!(out, ".<coroutine-tag>"),
205            CoroutineState(idx) => write!(out, ".<coroutine-state({})>", idx.index()),
206            CapturedVar(name) => write!(out, ".<captured-var({name})>"),
207            TupleElem(idx) => write!(out, ".{idx}"),
208            ArrayElem(idx) => write!(out, "[{idx}]"),
209            // `.<deref>` does not match Rust syntax, but it is more readable for long paths -- and
210            // some of the other items here also are not Rust syntax. Actually we can't
211            // even use the usual syntax because we are just showing the projections,
212            // not the root.
213            Deref => write!(out, ".<deref>"),
214            DynDowncast => write!(out, ".<dyn-downcast>"),
215            Vtable => write!(out, ".<vtable>"),
216        }
217        .unwrap()
218    }
219}
220
221/// Represents a set of `Size` values as a sorted list of ranges.
222// These are (offset, length) pairs, and they are sorted and mutually disjoint,
223// and never adjacent (i.e. there's always a gap between two of them).
224#[derive(Debug, Clone)]
225pub struct RangeSet(Vec<(Size, Size)>);
226
227impl RangeSet {
228    fn add_range(&mut self, offset: Size, size: Size) {
229        if size.bytes() == 0 {
230            // No need to track empty ranges.
231            return;
232        }
233        let v = &mut self.0;
234        // We scan for a partition point where the left partition is all the elements that end
235        // strictly before we start. Those are elements that are too "low" to merge with us.
236        let idx =
237            v.partition_point(|&(other_offset, other_size)| other_offset + other_size < offset);
238        // Now we want to either merge with the first element of the second partition, or insert ourselves before that.
239        if let Some(&(other_offset, other_size)) = v.get(idx)
240            && offset + size >= other_offset
241        {
242            // Their end is >= our start (otherwise it would not be in the 2nd partition) and
243            // our end is >= their start. This means we can merge the ranges.
244            let new_start = other_offset.min(offset);
245            let mut new_end = (other_offset + other_size).max(offset + size);
246            // We grew to the right, so merge with overlapping/adjacent elements.
247            // (We also may have grown to the left, but that can never make us adjacent with
248            // anything there since we selected the first such candidate via `partition_point`.)
249            let mut scan_right = 1;
250            while let Some(&(next_offset, next_size)) = v.get(idx + scan_right)
251                && new_end >= next_offset
252            {
253                // Increase our size to absorb the next element.
254                new_end = new_end.max(next_offset + next_size);
255                // Look at the next element.
256                scan_right += 1;
257            }
258            // Update the element we grew.
259            v[idx] = (new_start, new_end - new_start);
260            // Remove the elements we absorbed (if any).
261            if scan_right > 1 {
262                drop(v.drain((idx + 1)..(idx + scan_right)));
263            }
264        } else {
265            // Insert new element.
266            v.insert(idx, (offset, size));
267        }
268    }
269}
270
271struct ValidityVisitor<'rt, 'tcx, M: Machine<'tcx>> {
272    /// The `path` may be pushed to, but the part that is present when a function
273    /// starts must not be changed!  `visit_fields` and `visit_array` rely on
274    /// this stack discipline.
275    path: Vec<PathElem>,
276    ref_tracking: Option<&'rt mut RefTracking<MPlaceTy<'tcx, M::Provenance>, Vec<PathElem>>>,
277    /// `None` indicates this is not validating for CTFE (but for runtime).
278    ctfe_mode: Option<CtfeValidationMode>,
279    ecx: &'rt mut InterpCx<'tcx, M>,
280    /// Whether provenance should be reset outside of pointers (emulating the effect of a typed
281    /// copy).
282    reset_provenance_and_padding: bool,
283    /// This tracks which byte ranges in this value contain data; the remaining bytes are padding.
284    /// The ideal representation here would be pointer-length pairs, but to keep things more compact
285    /// we only store a (range) set of offsets -- the base pointer is the same throughout the entire
286    /// visit, after all.
287    /// If this is `Some`, then `reset_provenance_and_padding` must be true (but not vice versa:
288    /// we might not track data vs padding bytes if the operand isn't stored in memory anyway).
289    data_bytes: Option<RangeSet>,
290}
291
292impl<'rt, 'tcx, M: Machine<'tcx>> ValidityVisitor<'rt, 'tcx, M> {
293    fn aggregate_field_path_elem(&mut self, layout: TyAndLayout<'tcx>, field: usize) -> PathElem {
294        // First, check if we are projecting to a variant.
295        match layout.variants {
296            Variants::Multiple { tag_field, .. } => {
297                if tag_field == field {
298                    return match layout.ty.kind() {
299                        ty::Adt(def, ..) if def.is_enum() => PathElem::EnumTag,
300                        ty::Coroutine(..) => PathElem::CoroutineTag,
301                        _ => bug!("non-variant type {:?}", layout.ty),
302                    };
303                }
304            }
305            Variants::Single { .. } | Variants::Empty => {}
306        }
307
308        // Now we know we are projecting to a field, so figure out which one.
309        match layout.ty.kind() {
310            // coroutines, closures, and coroutine-closures all have upvars that may be named.
311            ty::Closure(def_id, _) | ty::Coroutine(def_id, _) | ty::CoroutineClosure(def_id, _) => {
312                let mut name = None;
313                // FIXME this should be more descriptive i.e. CapturePlace instead of CapturedVar
314                // https://github.com/rust-lang/project-rfc-2229/issues/46
315                if let Some(local_def_id) = def_id.as_local() {
316                    let captures = self.ecx.tcx.closure_captures(local_def_id);
317                    if let Some(captured_place) = captures.get(field) {
318                        // Sometimes the index is beyond the number of upvars (seen
319                        // for a coroutine).
320                        let var_hir_id = captured_place.get_root_variable();
321                        let node = self.ecx.tcx.hir_node(var_hir_id);
322                        if let hir::Node::Pat(pat) = node {
323                            if let hir::PatKind::Binding(_, _, ident, _) = pat.kind {
324                                name = Some(ident.name);
325                            }
326                        }
327                    }
328                }
329
330                PathElem::CapturedVar(name.unwrap_or_else(|| {
331                    // Fall back to showing the field index.
332                    sym::integer(field)
333                }))
334            }
335
336            // tuples
337            ty::Tuple(_) => PathElem::TupleElem(field),
338
339            // enums
340            ty::Adt(def, ..) if def.is_enum() => {
341                // we might be projecting *to* a variant, or to a field *in* a variant.
342                match layout.variants {
343                    Variants::Single { index } => {
344                        // Inside a variant
345                        PathElem::Field(def.variant(index).fields[FieldIdx::from_usize(field)].name)
346                    }
347                    Variants::Empty => panic!("there is no field in Variants::Empty types"),
348                    Variants::Multiple { .. } => bug!("we handled variants above"),
349                }
350            }
351
352            // other ADTs
353            ty::Adt(def, _) => {
354                PathElem::Field(def.non_enum_variant().fields[FieldIdx::from_usize(field)].name)
355            }
356
357            // arrays/slices
358            ty::Array(..) | ty::Slice(..) => PathElem::ArrayElem(field),
359
360            // dyn* vtables
361            ty::Dynamic(_, _, ty::DynKind::DynStar) if field == 1 => PathElem::Vtable,
362
363            // dyn traits
364            ty::Dynamic(..) => {
365                assert_eq!(field, 0);
366                PathElem::DynDowncast
367            }
368
369            // nothing else has an aggregate layout
370            _ => bug!("aggregate_field_path_elem: got non-aggregate type {:?}", layout.ty),
371        }
372    }
373
374    fn with_elem<R>(
375        &mut self,
376        elem: PathElem,
377        f: impl FnOnce(&mut Self) -> InterpResult<'tcx, R>,
378    ) -> InterpResult<'tcx, R> {
379        // Remember the old state
380        let path_len = self.path.len();
381        // Record new element
382        self.path.push(elem);
383        // Perform operation
384        let r = f(self)?;
385        // Undo changes
386        self.path.truncate(path_len);
387        // Done
388        interp_ok(r)
389    }
390
391    fn read_immediate(
392        &self,
393        val: &PlaceTy<'tcx, M::Provenance>,
394        expected: ExpectedKind,
395    ) -> InterpResult<'tcx, ImmTy<'tcx, M::Provenance>> {
396        interp_ok(try_validation!(
397            self.ecx.read_immediate(val),
398            self.path,
399            Ub(InvalidUninitBytes(None)) =>
400                Uninit { expected },
401            // The `Unsup` cases can only occur during CTFE
402            Unsup(ReadPointerAsInt(_)) =>
403                PointerAsInt { expected },
404            Unsup(ReadPartialPointer(_)) =>
405                PartialPointer,
406        ))
407    }
408
409    fn read_scalar(
410        &self,
411        val: &PlaceTy<'tcx, M::Provenance>,
412        expected: ExpectedKind,
413    ) -> InterpResult<'tcx, Scalar<M::Provenance>> {
414        interp_ok(self.read_immediate(val, expected)?.to_scalar())
415    }
416
417    fn deref_pointer(
418        &mut self,
419        val: &PlaceTy<'tcx, M::Provenance>,
420        expected: ExpectedKind,
421    ) -> InterpResult<'tcx, MPlaceTy<'tcx, M::Provenance>> {
422        // Not using `ecx.deref_pointer` since we want to use our `read_immediate` wrapper.
423        let imm = self.read_immediate(val, expected)?;
424        // Reset provenance: ensure slice tail metadata does not preserve provenance,
425        // and ensure all pointers do not preserve partial provenance.
426        if self.reset_provenance_and_padding {
427            if matches!(imm.layout.backend_repr, BackendRepr::Scalar(..)) {
428                // A thin pointer. If it has provenance, we don't have to do anything.
429                // If it does not, ensure we clear the provenance in memory.
430                if matches!(imm.to_scalar(), Scalar::Int(..)) {
431                    self.ecx.clear_provenance(val)?;
432                }
433            } else {
434                // A wide pointer. This means we have to worry both about the pointer itself and the
435                // metadata. We do the lazy thing and just write back the value we got. Just
436                // clearing provenance in a targeted manner would be more efficient, but unless this
437                // is a perf hotspot it's just not worth the effort.
438                self.ecx.write_immediate_no_validate(*imm, val)?;
439            }
440            // The entire thing is data, not padding.
441            self.add_data_range_place(val);
442        }
443        // Now turn it into a place.
444        self.ecx.ref_to_mplace(&imm)
445    }
446
447    fn check_wide_ptr_meta(
448        &mut self,
449        meta: MemPlaceMeta<M::Provenance>,
450        pointee: TyAndLayout<'tcx>,
451    ) -> InterpResult<'tcx> {
452        let tail = self.ecx.tcx.struct_tail_for_codegen(pointee.ty, self.ecx.typing_env);
453        match tail.kind() {
454            ty::Dynamic(data, _, ty::Dyn) => {
455                let vtable = meta.unwrap_meta().to_pointer(self.ecx)?;
456                // Make sure it is a genuine vtable pointer for the right trait.
457                try_validation!(
458                    self.ecx.get_ptr_vtable_ty(vtable, Some(data)),
459                    self.path,
460                    Ub(DanglingIntPointer{ .. } | InvalidVTablePointer(..)) =>
461                        InvalidVTablePtr { value: format!("{vtable}") },
462                    Ub(InvalidVTableTrait { vtable_dyn_type, expected_dyn_type }) => {
463                        InvalidMetaWrongTrait { vtable_dyn_type, expected_dyn_type }
464                    },
465                );
466            }
467            ty::Slice(..) | ty::Str => {
468                let _len = meta.unwrap_meta().to_target_usize(self.ecx)?;
469                // We do not check that `len * elem_size <= isize::MAX`:
470                // that is only required for references, and there it falls out of the
471                // "dereferenceable" check performed by Stacked Borrows.
472            }
473            ty::Foreign(..) => {
474                // Unsized, but not wide.
475            }
476            _ => bug!("Unexpected unsized type tail: {:?}", tail),
477        }
478
479        interp_ok(())
480    }
481
482    /// Check a reference or `Box`.
483    fn check_safe_pointer(
484        &mut self,
485        value: &PlaceTy<'tcx, M::Provenance>,
486        ptr_kind: PointerKind,
487    ) -> InterpResult<'tcx> {
488        let place = self.deref_pointer(value, ptr_kind.into())?;
489        // Handle wide pointers.
490        // Check metadata early, for better diagnostics
491        if place.layout.is_unsized() {
492            self.check_wide_ptr_meta(place.meta(), place.layout)?;
493        }
494        // Make sure this is dereferenceable and all.
495        let size_and_align = try_validation!(
496            self.ecx.size_and_align_of_mplace(&place),
497            self.path,
498            Ub(InvalidMeta(msg)) => match msg {
499                InvalidMetaKind::SliceTooBig => InvalidMetaSliceTooLarge { ptr_kind },
500                InvalidMetaKind::TooBig => InvalidMetaTooLarge { ptr_kind },
501            }
502        );
503        let (size, align) = size_and_align
504            // for the purpose of validity, consider foreign types to have
505            // alignment and size determined by the layout (size will be 0,
506            // alignment should take attributes into account).
507            .unwrap_or_else(|| (place.layout.size, place.layout.align.abi));
508        // Direct call to `check_ptr_access_align` checks alignment even on CTFE machines.
509        try_validation!(
510            self.ecx.check_ptr_access(
511                place.ptr(),
512                size,
513                CheckInAllocMsg::InboundsTest, // will anyway be replaced by validity message
514            ),
515            self.path,
516            Ub(DanglingIntPointer { addr: 0, .. }) => NullPtr { ptr_kind },
517            Ub(DanglingIntPointer { addr: i, .. }) => DanglingPtrNoProvenance {
518                ptr_kind,
519                // FIXME this says "null pointer" when null but we need translate
520                pointer: format!("{}", Pointer::<Option<AllocId>>::from_addr_invalid(i))
521            },
522            Ub(PointerOutOfBounds { .. }) => DanglingPtrOutOfBounds {
523                ptr_kind
524            },
525            Ub(PointerUseAfterFree(..)) => DanglingPtrUseAfterFree {
526                ptr_kind,
527            },
528        );
529        try_validation!(
530            self.ecx.check_ptr_align(
531                place.ptr(),
532                align,
533            ),
534            self.path,
535            Ub(AlignmentCheckFailed(Misalignment { required, has }, _msg)) => UnalignedPtr {
536                ptr_kind,
537                required_bytes: required.bytes(),
538                found_bytes: has.bytes()
539            },
540        );
541        // Make sure this is non-null. We checked dereferenceability above, but if `size` is zero
542        // that does not imply non-null.
543        if self.ecx.scalar_may_be_null(Scalar::from_maybe_pointer(place.ptr(), self.ecx))? {
544            throw_validation_failure!(self.path, NullPtr { ptr_kind })
545        }
546        // Do not allow references to uninhabited types.
547        if place.layout.is_uninhabited() {
548            let ty = place.layout.ty;
549            throw_validation_failure!(self.path, PtrToUninhabited { ptr_kind, ty })
550        }
551        // Recursive checking
552        if let Some(ref_tracking) = self.ref_tracking.as_deref_mut() {
553            // Proceed recursively even for ZST, no reason to skip them!
554            // `!` is a ZST and we want to validate it.
555            if let Some(ctfe_mode) = self.ctfe_mode {
556                let mut skip_recursive_check = false;
557                // CTFE imposes restrictions on what references can point to.
558                if let Ok((alloc_id, _offset, _prov)) =
559                    self.ecx.ptr_try_get_alloc_id(place.ptr(), 0)
560                {
561                    // Everything should be already interned.
562                    let Some(global_alloc) = self.ecx.tcx.try_get_global_alloc(alloc_id) else {
563                        assert!(self.ecx.memory.alloc_map.get(alloc_id).is_none());
564                        // We can't have *any* references to non-existing allocations in const-eval
565                        // as the rest of rustc isn't happy with them... so we throw an error, even
566                        // though for zero-sized references this isn't really UB.
567                        // A potential future alternative would be to resurrect this as a zero-sized allocation
568                        // (which codegen will then compile to an aligned dummy pointer anyway).
569                        throw_validation_failure!(self.path, DanglingPtrUseAfterFree { ptr_kind });
570                    };
571                    let (size, _align) =
572                        global_alloc.size_and_align(*self.ecx.tcx, self.ecx.typing_env);
573
574                    if let GlobalAlloc::Static(did) = global_alloc {
575                        let DefKind::Static { nested, .. } = self.ecx.tcx.def_kind(did) else {
576                            bug!()
577                        };
578                        // Special handling for pointers to statics (irrespective of their type).
579                        assert!(!self.ecx.tcx.is_thread_local_static(did));
580                        assert!(self.ecx.tcx.is_static(did));
581                        // Mode-specific checks
582                        match ctfe_mode {
583                            CtfeValidationMode::Static { .. }
584                            | CtfeValidationMode::Promoted { .. } => {
585                                // We skip recursively checking other statics. These statics must be sound by
586                                // themselves, and the only way to get broken statics here is by using
587                                // unsafe code.
588                                // The reasons we don't check other statics is twofold. For one, in all
589                                // sound cases, the static was already validated on its own, and second, we
590                                // trigger cycle errors if we try to compute the value of the other static
591                                // and that static refers back to us (potentially through a promoted).
592                                // This could miss some UB, but that's fine.
593                                // We still walk nested allocations, as they are fundamentally part of this validation run.
594                                // This means we will also recurse into nested statics of *other*
595                                // statics, even though we do not recurse into other statics directly.
596                                // That's somewhat inconsistent but harmless.
597                                skip_recursive_check = !nested;
598                            }
599                            CtfeValidationMode::Const { .. } => {
600                                // We can't recursively validate `extern static`, so we better reject them.
601                                if self.ecx.tcx.is_foreign_item(did) {
602                                    throw_validation_failure!(self.path, ConstRefToExtern);
603                                }
604                            }
605                        }
606                    }
607
608                    // If this allocation has size zero, there is no actual mutability here.
609                    if size != Size::ZERO {
610                        // Determine whether this pointer expects to be pointing to something mutable.
611                        let ptr_expected_mutbl = match ptr_kind {
612                            PointerKind::Box => Mutability::Mut,
613                            PointerKind::Ref(mutbl) => {
614                                // We do not take into account interior mutability here since we cannot know if
615                                // there really is an `UnsafeCell` inside `Option<UnsafeCell>` -- so we check
616                                // that in the recursive descent behind this reference (controlled by
617                                // `allow_immutable_unsafe_cell`).
618                                mutbl
619                            }
620                        };
621                        // Determine what it actually points to.
622                        let alloc_actual_mutbl =
623                            global_alloc.mutability(*self.ecx.tcx, self.ecx.typing_env);
624                        // Mutable pointer to immutable memory is no good.
625                        if ptr_expected_mutbl == Mutability::Mut
626                            && alloc_actual_mutbl == Mutability::Not
627                        {
628                            // This can actually occur with transmutes.
629                            throw_validation_failure!(self.path, MutableRefToImmutable);
630                        }
631                        // In a const, everything must be completely immutable.
632                        if matches!(self.ctfe_mode, Some(CtfeValidationMode::Const { .. })) {
633                            if ptr_expected_mutbl == Mutability::Mut
634                                || alloc_actual_mutbl == Mutability::Mut
635                            {
636                                throw_validation_failure!(self.path, ConstRefToMutable);
637                            }
638                        }
639                    }
640                }
641                // Potentially skip recursive check.
642                if skip_recursive_check {
643                    return interp_ok(());
644                }
645            } else {
646                // This is not CTFE, so it's Miri with recursive checking.
647                // FIXME: we do *not* check behind boxes, since creating a new box first creates it uninitialized
648                // and then puts the value in there, so briefly we have a box with uninit contents.
649                // FIXME: should we also skip `UnsafeCell` behind shared references? Currently that is not
650                // needed since validation reads bypass Stacked Borrows and data race checks.
651                if matches!(ptr_kind, PointerKind::Box) {
652                    return interp_ok(());
653                }
654            }
655            let path = &self.path;
656            ref_tracking.track(place, || {
657                // We need to clone the path anyway, make sure it gets created
658                // with enough space for the additional `Deref`.
659                let mut new_path = Vec::with_capacity(path.len() + 1);
660                new_path.extend(path);
661                new_path.push(PathElem::Deref);
662                new_path
663            });
664        }
665        interp_ok(())
666    }
667
668    /// Check if this is a value of primitive type, and if yes check the validity of the value
669    /// at that type. Return `true` if the type is indeed primitive.
670    ///
671    /// Note that not all of these have `FieldsShape::Primitive`, e.g. wide references.
672    fn try_visit_primitive(
673        &mut self,
674        value: &PlaceTy<'tcx, M::Provenance>,
675    ) -> InterpResult<'tcx, bool> {
676        // Go over all the primitive types
677        let ty = value.layout.ty;
678        match ty.kind() {
679            ty::Bool => {
680                let scalar = self.read_scalar(value, ExpectedKind::Bool)?;
681                try_validation!(
682                    scalar.to_bool(),
683                    self.path,
684                    Ub(InvalidBool(..)) => ValidationErrorKind::InvalidBool {
685                        value: format!("{scalar:x}"),
686                    }
687                );
688                if self.reset_provenance_and_padding {
689                    self.ecx.clear_provenance(value)?;
690                    self.add_data_range_place(value);
691                }
692                interp_ok(true)
693            }
694            ty::Char => {
695                let scalar = self.read_scalar(value, ExpectedKind::Char)?;
696                try_validation!(
697                    scalar.to_char(),
698                    self.path,
699                    Ub(InvalidChar(..)) => ValidationErrorKind::InvalidChar {
700                        value: format!("{scalar:x}"),
701                    }
702                );
703                if self.reset_provenance_and_padding {
704                    self.ecx.clear_provenance(value)?;
705                    self.add_data_range_place(value);
706                }
707                interp_ok(true)
708            }
709            ty::Float(_) | ty::Int(_) | ty::Uint(_) => {
710                // NOTE: Keep this in sync with the array optimization for int/float
711                // types below!
712                self.read_scalar(
713                    value,
714                    if matches!(ty.kind(), ty::Float(..)) {
715                        ExpectedKind::Float
716                    } else {
717                        ExpectedKind::Int
718                    },
719                )?;
720                if self.reset_provenance_and_padding {
721                    self.ecx.clear_provenance(value)?;
722                    self.add_data_range_place(value);
723                }
724                interp_ok(true)
725            }
726            ty::RawPtr(..) => {
727                let place = self.deref_pointer(value, ExpectedKind::RawPtr)?;
728                if place.layout.is_unsized() {
729                    self.check_wide_ptr_meta(place.meta(), place.layout)?;
730                }
731                interp_ok(true)
732            }
733            ty::Ref(_, _ty, mutbl) => {
734                self.check_safe_pointer(value, PointerKind::Ref(*mutbl))?;
735                interp_ok(true)
736            }
737            ty::FnPtr(..) => {
738                let scalar = self.read_scalar(value, ExpectedKind::FnPtr)?;
739
740                // If we check references recursively, also check that this points to a function.
741                if let Some(_) = self.ref_tracking {
742                    let ptr = scalar.to_pointer(self.ecx)?;
743                    let _fn = try_validation!(
744                        self.ecx.get_ptr_fn(ptr),
745                        self.path,
746                        Ub(DanglingIntPointer{ .. } | InvalidFunctionPointer(..)) =>
747                            InvalidFnPtr { value: format!("{ptr}") },
748                    );
749                    // FIXME: Check if the signature matches
750                } else {
751                    // Otherwise (for standalone Miri), we have to still check it to be non-null.
752                    if self.ecx.scalar_may_be_null(scalar)? {
753                        throw_validation_failure!(self.path, NullFnPtr);
754                    }
755                }
756                if self.reset_provenance_and_padding {
757                    // Make sure we do not preserve partial provenance. This matches the thin
758                    // pointer handling in `deref_pointer`.
759                    if matches!(scalar, Scalar::Int(..)) {
760                        self.ecx.clear_provenance(value)?;
761                    }
762                    self.add_data_range_place(value);
763                }
764                interp_ok(true)
765            }
766            ty::Never => throw_validation_failure!(self.path, NeverVal),
767            ty::Foreign(..) | ty::FnDef(..) => {
768                // Nothing to check.
769                interp_ok(true)
770            }
771            ty::UnsafeBinder(_) => todo!("FIXME(unsafe_binder)"),
772            // The above should be all the primitive types. The rest is compound, we
773            // check them by visiting their fields/variants.
774            ty::Adt(..)
775            | ty::Tuple(..)
776            | ty::Array(..)
777            | ty::Slice(..)
778            | ty::Str
779            | ty::Dynamic(..)
780            | ty::Closure(..)
781            | ty::Pat(..)
782            | ty::CoroutineClosure(..)
783            | ty::Coroutine(..) => interp_ok(false),
784            // Some types only occur during typechecking, they have no layout.
785            // We should not see them here and we could not check them anyway.
786            ty::Error(_)
787            | ty::Infer(..)
788            | ty::Placeholder(..)
789            | ty::Bound(..)
790            | ty::Param(..)
791            | ty::Alias(..)
792            | ty::CoroutineWitness(..) => bug!("Encountered invalid type {:?}", ty),
793        }
794    }
795
796    fn visit_scalar(
797        &mut self,
798        scalar: Scalar<M::Provenance>,
799        scalar_layout: ScalarAbi,
800    ) -> InterpResult<'tcx> {
801        let size = scalar_layout.size(self.ecx);
802        let valid_range = scalar_layout.valid_range(self.ecx);
803        let WrappingRange { start, end } = valid_range;
804        let max_value = size.unsigned_int_max();
805        assert!(end <= max_value);
806        let bits = match scalar.try_to_scalar_int() {
807            Ok(int) => int.to_bits(size),
808            Err(_) => {
809                // So this is a pointer then, and casting to an int failed.
810                // Can only happen during CTFE.
811                // We support 2 kinds of ranges here: full range, and excluding zero.
812                if start == 1 && end == max_value {
813                    // Only null is the niche. So make sure the ptr is NOT null.
814                    if self.ecx.scalar_may_be_null(scalar)? {
815                        throw_validation_failure!(
816                            self.path,
817                            NullablePtrOutOfRange { range: valid_range, max_value }
818                        )
819                    } else {
820                        return interp_ok(());
821                    }
822                } else if scalar_layout.is_always_valid(self.ecx) {
823                    // Easy. (This is reachable if `enforce_number_validity` is set.)
824                    return interp_ok(());
825                } else {
826                    // Conservatively, we reject, because the pointer *could* have a bad
827                    // value.
828                    throw_validation_failure!(
829                        self.path,
830                        PtrOutOfRange { range: valid_range, max_value }
831                    )
832                }
833            }
834        };
835        // Now compare.
836        if valid_range.contains(bits) {
837            interp_ok(())
838        } else {
839            throw_validation_failure!(
840                self.path,
841                OutOfRange { value: format!("{bits}"), range: valid_range, max_value }
842            )
843        }
844    }
845
846    fn in_mutable_memory(&self, val: &PlaceTy<'tcx, M::Provenance>) -> bool {
847        debug_assert!(self.ctfe_mode.is_some());
848        if let Some(mplace) = val.as_mplace_or_local().left() {
849            if let Some(alloc_id) = mplace.ptr().provenance.and_then(|p| p.get_alloc_id()) {
850                let tcx = *self.ecx.tcx;
851                // Everything must be already interned.
852                let mutbl = tcx.global_alloc(alloc_id).mutability(tcx, self.ecx.typing_env);
853                if let Some((_, alloc)) = self.ecx.memory.alloc_map.get(alloc_id) {
854                    assert_eq!(alloc.mutability, mutbl);
855                }
856                mutbl.is_mut()
857            } else {
858                // No memory at all.
859                false
860            }
861        } else {
862            // A local variable -- definitely mutable.
863            true
864        }
865    }
866
867    /// Add the given pointer-length pair to the "data" range of this visit.
868    fn add_data_range(&mut self, ptr: Pointer<Option<M::Provenance>>, size: Size) {
869        if let Some(data_bytes) = self.data_bytes.as_mut() {
870            // We only have to store the offset, the rest is the same for all pointers here.
871            let (_prov, offset) = ptr.into_parts();
872            // Add this.
873            data_bytes.add_range(offset, size);
874        };
875    }
876
877    /// Add the entire given place to the "data" range of this visit.
878    fn add_data_range_place(&mut self, place: &PlaceTy<'tcx, M::Provenance>) {
879        // Only sized places can be added this way.
880        debug_assert!(place.layout.is_sized());
881        if let Some(data_bytes) = self.data_bytes.as_mut() {
882            let offset = Self::data_range_offset(self.ecx, place);
883            data_bytes.add_range(offset, place.layout.size);
884        }
885    }
886
887    /// Convert a place into the offset it starts at, for the purpose of data_range tracking.
888    /// Must only be called if `data_bytes` is `Some(_)`.
889    fn data_range_offset(ecx: &InterpCx<'tcx, M>, place: &PlaceTy<'tcx, M::Provenance>) -> Size {
890        // The presence of `data_bytes` implies that our place is in memory.
891        let ptr = ecx
892            .place_to_op(place)
893            .expect("place must be in memory")
894            .as_mplace_or_imm()
895            .expect_left("place must be in memory")
896            .ptr();
897        let (_prov, offset) = ptr.into_parts();
898        offset
899    }
900
901    fn reset_padding(&mut self, place: &PlaceTy<'tcx, M::Provenance>) -> InterpResult<'tcx> {
902        let Some(data_bytes) = self.data_bytes.as_mut() else { return interp_ok(()) };
903        // Our value must be in memory, otherwise we would not have set up `data_bytes`.
904        let mplace = self.ecx.force_allocation(place)?;
905        // Determine starting offset and size.
906        let (_prov, start_offset) = mplace.ptr().into_parts();
907        let (size, _align) = self
908            .ecx
909            .size_and_align_of_mplace(&mplace)?
910            .unwrap_or((mplace.layout.size, mplace.layout.align.abi));
911        // If there is no padding at all, we can skip the rest: check for
912        // a single data range covering the entire value.
913        if data_bytes.0 == &[(start_offset, size)] {
914            return interp_ok(());
915        }
916        // Get a handle for the allocation. Do this only once, to avoid looking up the same
917        // allocation over and over again. (Though to be fair, iterating the value already does
918        // exactly that.)
919        let Some(mut alloc) = self.ecx.get_ptr_alloc_mut(mplace.ptr(), size)? else {
920            // A ZST, no padding to clear.
921            return interp_ok(());
922        };
923        // Add a "finalizer" data range at the end, so that the iteration below finds all gaps
924        // between ranges.
925        data_bytes.0.push((start_offset + size, Size::ZERO));
926        // Iterate, and reset gaps.
927        let mut padding_cleared_until = start_offset;
928        for &(offset, size) in data_bytes.0.iter() {
929            assert!(
930                offset >= padding_cleared_until,
931                "reset_padding on {}: previous field ended at offset {}, next field starts at {} (and has a size of {} bytes)",
932                mplace.layout.ty,
933                (padding_cleared_until - start_offset).bytes(),
934                (offset - start_offset).bytes(),
935                size.bytes(),
936            );
937            if offset > padding_cleared_until {
938                // We found padding. Adjust the range to be relative to `alloc`, and make it uninit.
939                let padding_start = padding_cleared_until - start_offset;
940                let padding_size = offset - padding_cleared_until;
941                let range = alloc_range(padding_start, padding_size);
942                trace!("reset_padding on {}: resetting padding range {range:?}", mplace.layout.ty);
943                alloc.write_uninit(range)?;
944            }
945            padding_cleared_until = offset + size;
946        }
947        assert!(padding_cleared_until == start_offset + size);
948        interp_ok(())
949    }
950
951    /// Computes the data range of this union type:
952    /// which bytes are inside a field (i.e., not padding.)
953    fn union_data_range<'e>(
954        ecx: &'e mut InterpCx<'tcx, M>,
955        layout: TyAndLayout<'tcx>,
956    ) -> Cow<'e, RangeSet> {
957        assert!(layout.ty.is_union());
958        assert!(layout.is_sized(), "there are no unsized unions");
959        let layout_cx = LayoutCx::new(*ecx.tcx, ecx.typing_env);
960        return M::cached_union_data_range(ecx, layout.ty, || {
961            let mut out = RangeSet(Vec::new());
962            union_data_range_uncached(&layout_cx, layout, Size::ZERO, &mut out);
963            out
964        });
965
966        /// Helper for recursive traversal: add data ranges of the given type to `out`.
967        fn union_data_range_uncached<'tcx>(
968            cx: &LayoutCx<'tcx>,
969            layout: TyAndLayout<'tcx>,
970            base_offset: Size,
971            out: &mut RangeSet,
972        ) {
973            // If this is a ZST, we don't contain any data. In particular, this helps us to quickly
974            // skip over huge arrays of ZST.
975            if layout.is_zst() {
976                return;
977            }
978            // Just recursively add all the fields of everything to the output.
979            match &layout.fields {
980                FieldsShape::Primitive => {
981                    out.add_range(base_offset, layout.size);
982                }
983                &FieldsShape::Union(fields) => {
984                    // Currently, all fields start at offset 0 (relative to `base_offset`).
985                    for field in 0..fields.get() {
986                        let field = layout.field(cx, field);
987                        union_data_range_uncached(cx, field, base_offset, out);
988                    }
989                }
990                &FieldsShape::Array { stride, count } => {
991                    let elem = layout.field(cx, 0);
992
993                    // Fast-path for large arrays of simple types that do not contain any padding.
994                    if elem.backend_repr.is_scalar() {
995                        out.add_range(base_offset, elem.size * count);
996                    } else {
997                        for idx in 0..count {
998                            // This repeats the same computation for every array element... but the alternative
999                            // is to allocate temporary storage for a dedicated `out` set for the array element,
1000                            // and replicating that N times. Is that better?
1001                            union_data_range_uncached(cx, elem, base_offset + idx * stride, out);
1002                        }
1003                    }
1004                }
1005                FieldsShape::Arbitrary { offsets, .. } => {
1006                    for (field, &offset) in offsets.iter_enumerated() {
1007                        let field = layout.field(cx, field.as_usize());
1008                        union_data_range_uncached(cx, field, base_offset + offset, out);
1009                    }
1010                }
1011            }
1012            // Don't forget potential other variants.
1013            match &layout.variants {
1014                Variants::Single { .. } | Variants::Empty => {
1015                    // Fully handled above.
1016                }
1017                Variants::Multiple { variants, .. } => {
1018                    for variant in variants.indices() {
1019                        let variant = layout.for_variant(cx, variant);
1020                        union_data_range_uncached(cx, variant, base_offset, out);
1021                    }
1022                }
1023            }
1024        }
1025    }
1026}
1027
1028impl<'rt, 'tcx, M: Machine<'tcx>> ValueVisitor<'tcx, M> for ValidityVisitor<'rt, 'tcx, M> {
1029    type V = PlaceTy<'tcx, M::Provenance>;
1030
1031    #[inline(always)]
1032    fn ecx(&self) -> &InterpCx<'tcx, M> {
1033        self.ecx
1034    }
1035
1036    fn read_discriminant(
1037        &mut self,
1038        val: &PlaceTy<'tcx, M::Provenance>,
1039    ) -> InterpResult<'tcx, VariantIdx> {
1040        self.with_elem(PathElem::EnumTag, move |this| {
1041            interp_ok(try_validation!(
1042                this.ecx.read_discriminant(val),
1043                this.path,
1044                Ub(InvalidTag(val)) => InvalidEnumTag {
1045                    value: format!("{val:x}"),
1046                },
1047                Ub(UninhabitedEnumVariantRead(_)) => UninhabitedEnumVariant,
1048                // Uninit / bad provenance are not possible since the field was already previously
1049                // checked at its integer type.
1050            ))
1051        })
1052    }
1053
1054    #[inline]
1055    fn visit_field(
1056        &mut self,
1057        old_val: &PlaceTy<'tcx, M::Provenance>,
1058        field: usize,
1059        new_val: &PlaceTy<'tcx, M::Provenance>,
1060    ) -> InterpResult<'tcx> {
1061        let elem = self.aggregate_field_path_elem(old_val.layout, field);
1062        self.with_elem(elem, move |this| this.visit_value(new_val))
1063    }
1064
1065    #[inline]
1066    fn visit_variant(
1067        &mut self,
1068        old_val: &PlaceTy<'tcx, M::Provenance>,
1069        variant_id: VariantIdx,
1070        new_val: &PlaceTy<'tcx, M::Provenance>,
1071    ) -> InterpResult<'tcx> {
1072        let name = match old_val.layout.ty.kind() {
1073            ty::Adt(adt, _) => PathElem::Variant(adt.variant(variant_id).name),
1074            // Coroutines also have variants
1075            ty::Coroutine(..) => PathElem::CoroutineState(variant_id),
1076            _ => bug!("Unexpected type with variant: {:?}", old_val.layout.ty),
1077        };
1078        self.with_elem(name, move |this| this.visit_value(new_val))
1079    }
1080
1081    #[inline(always)]
1082    fn visit_union(
1083        &mut self,
1084        val: &PlaceTy<'tcx, M::Provenance>,
1085        _fields: NonZero<usize>,
1086    ) -> InterpResult<'tcx> {
1087        // Special check for CTFE validation, preventing `UnsafeCell` inside unions in immutable memory.
1088        if self.ctfe_mode.is_some_and(|c| !c.allow_immutable_unsafe_cell()) {
1089            if !val.layout.is_zst() && !val.layout.ty.is_freeze(*self.ecx.tcx, self.ecx.typing_env)
1090            {
1091                if !self.in_mutable_memory(val) {
1092                    throw_validation_failure!(self.path, UnsafeCellInImmutable);
1093                }
1094            }
1095        }
1096        if self.reset_provenance_and_padding
1097            && let Some(data_bytes) = self.data_bytes.as_mut()
1098        {
1099            let base_offset = Self::data_range_offset(self.ecx, val);
1100            // Determine and add data range for this union.
1101            let union_data_range = Self::union_data_range(self.ecx, val.layout);
1102            for &(offset, size) in union_data_range.0.iter() {
1103                data_bytes.add_range(base_offset + offset, size);
1104            }
1105        }
1106        interp_ok(())
1107    }
1108
1109    #[inline]
1110    fn visit_box(
1111        &mut self,
1112        _box_ty: Ty<'tcx>,
1113        val: &PlaceTy<'tcx, M::Provenance>,
1114    ) -> InterpResult<'tcx> {
1115        self.check_safe_pointer(val, PointerKind::Box)?;
1116        interp_ok(())
1117    }
1118
1119    #[inline]
1120    fn visit_value(&mut self, val: &PlaceTy<'tcx, M::Provenance>) -> InterpResult<'tcx> {
1121        trace!("visit_value: {:?}, {:?}", *val, val.layout);
1122
1123        // Check primitive types -- the leaves of our recursive descent.
1124        // This is called even for enum discriminants (which are "fields" of their enum),
1125        // so for integer-typed discriminants the provenance reset will happen here.
1126        // We assume that the Scalar validity range does not restrict these values
1127        // any further than `try_visit_primitive` does!
1128        if self.try_visit_primitive(val)? {
1129            return interp_ok(());
1130        }
1131
1132        // Special check preventing `UnsafeCell` in the inner part of constants
1133        if self.ctfe_mode.is_some_and(|c| !c.allow_immutable_unsafe_cell()) {
1134            if !val.layout.is_zst()
1135                && let Some(def) = val.layout.ty.ty_adt_def()
1136                && def.is_unsafe_cell()
1137            {
1138                if !self.in_mutable_memory(val) {
1139                    throw_validation_failure!(self.path, UnsafeCellInImmutable);
1140                }
1141            }
1142        }
1143
1144        // Recursively walk the value at its type. Apply optimizations for some large types.
1145        match val.layout.ty.kind() {
1146            ty::Str => {
1147                let mplace = val.assert_mem_place(); // strings are unsized and hence never immediate
1148                let len = mplace.len(self.ecx)?;
1149                try_validation!(
1150                    self.ecx.read_bytes_ptr_strip_provenance(mplace.ptr(), Size::from_bytes(len)),
1151                    self.path,
1152                    Ub(InvalidUninitBytes(..)) => Uninit { expected: ExpectedKind::Str },
1153                    Unsup(ReadPointerAsInt(_)) => PointerAsInt { expected: ExpectedKind::Str }
1154                );
1155            }
1156            ty::Array(tys, ..) | ty::Slice(tys)
1157                // This optimization applies for types that can hold arbitrary non-provenance bytes (such as
1158                // integer and floating point types).
1159                // FIXME(wesleywiser) This logic could be extended further to arbitrary structs or
1160                // tuples made up of integer/floating point types or inhabited ZSTs with no padding.
1161                if matches!(tys.kind(), ty::Int(..) | ty::Uint(..) | ty::Float(..))
1162                =>
1163            {
1164                let expected = if tys.is_integral() { ExpectedKind::Int } else { ExpectedKind::Float };
1165                // Optimized handling for arrays of integer/float type.
1166
1167                // This is the length of the array/slice.
1168                let len = val.len(self.ecx)?;
1169                // This is the element type size.
1170                let layout = self.ecx.layout_of(*tys)?;
1171                // This is the size in bytes of the whole array. (This checks for overflow.)
1172                let size = layout.size * len;
1173                // If the size is 0, there is nothing to check.
1174                // (`size` can only be 0 if `len` is 0, and empty arrays are always valid.)
1175                if size == Size::ZERO {
1176                    return interp_ok(());
1177                }
1178                // Now that we definitely have a non-ZST array, we know it lives in memory -- except it may
1179                // be an uninitialized local variable, those are also "immediate".
1180                let mplace = match val.to_op(self.ecx)?.as_mplace_or_imm() {
1181                    Left(mplace) => mplace,
1182                    Right(imm) => match *imm {
1183                        Immediate::Uninit =>
1184                            throw_validation_failure!(self.path, Uninit { expected }),
1185                        Immediate::Scalar(..) | Immediate::ScalarPair(..) =>
1186                            bug!("arrays/slices can never have Scalar/ScalarPair layout"),
1187                    }
1188                };
1189
1190                // Optimization: we just check the entire range at once.
1191                // NOTE: Keep this in sync with the handling of integer and float
1192                // types above, in `visit_primitive`.
1193                // No need for an alignment check here, this is not an actual memory access.
1194                let alloc = self.ecx.get_ptr_alloc(mplace.ptr(), size)?.expect("we already excluded size 0");
1195
1196                alloc.get_bytes_strip_provenance().map_err_kind(|kind| {
1197                    // Some error happened, try to provide a more detailed description.
1198                    // For some errors we might be able to provide extra information.
1199                    // (This custom logic does not fit the `try_validation!` macro.)
1200                    match kind {
1201                        Ub(InvalidUninitBytes(Some((_alloc_id, access)))) | Unsup(ReadPointerAsInt(Some((_alloc_id, access)))) => {
1202                            // Some byte was uninitialized, determine which
1203                            // element that byte belongs to so we can
1204                            // provide an index.
1205                            let i = usize::try_from(
1206                                access.bad.start.bytes() / layout.size.bytes(),
1207                            )
1208                            .unwrap();
1209                            self.path.push(PathElem::ArrayElem(i));
1210
1211                            if matches!(kind, Ub(InvalidUninitBytes(_))) {
1212                                err_validation_failure!(self.path, Uninit { expected })
1213                            } else {
1214                                err_validation_failure!(self.path, PointerAsInt { expected })
1215                            }
1216                        }
1217
1218                        // Propagate upwards (that will also check for unexpected errors).
1219                        err => err,
1220                    }
1221                })?;
1222
1223                // Don't forget that these are all non-pointer types, and thus do not preserve
1224                // provenance.
1225                if self.reset_provenance_and_padding {
1226                    // We can't share this with above as above, we might be looking at read-only memory.
1227                    let mut alloc = self.ecx.get_ptr_alloc_mut(mplace.ptr(), size)?.expect("we already excluded size 0");
1228                    alloc.clear_provenance()?;
1229                    // Also, mark this as containing data, not padding.
1230                    self.add_data_range(mplace.ptr(), size);
1231                }
1232            }
1233            // Fast path for arrays and slices of ZSTs. We only need to check a single ZST element
1234            // of an array and not all of them, because there's only a single value of a specific
1235            // ZST type, so either validation fails for all elements or none.
1236            ty::Array(tys, ..) | ty::Slice(tys) if self.ecx.layout_of(*tys)?.is_zst() => {
1237                // Validate just the first element (if any).
1238                if val.len(self.ecx)? > 0 {
1239                    self.visit_field(val, 0, &self.ecx.project_index(val, 0)?)?;
1240                }
1241            }
1242            ty::Pat(base, pat) => {
1243                // First check that the base type is valid
1244                self.visit_value(&val.transmute(self.ecx.layout_of(*base)?, self.ecx)?)?;
1245                // When you extend this match, make sure to also add tests to
1246                // tests/ui/type/pattern_types/validity.rs((
1247                match **pat {
1248                    // Range patterns are precisely reflected into `valid_range` and thus
1249                    // handled fully by `visit_scalar` (called below).
1250                    ty::PatternKind::Range { .. } => {},
1251                }
1252            }
1253            _ => {
1254                // default handler
1255                try_validation!(
1256                    self.walk_value(val),
1257                    self.path,
1258                    // It's not great to catch errors here, since we can't give a very good path,
1259                    // but it's better than ICEing.
1260                    Ub(InvalidVTableTrait { vtable_dyn_type, expected_dyn_type }) => {
1261                        InvalidMetaWrongTrait { vtable_dyn_type, expected_dyn_type }
1262                    },
1263                );
1264            }
1265        }
1266
1267        // *After* all of this, check the ABI. We need to check the ABI to handle
1268        // types like `NonNull` where the `Scalar` info is more restrictive than what
1269        // the fields say (`rustc_layout_scalar_valid_range_start`).
1270        // But in most cases, this will just propagate what the fields say,
1271        // and then we want the error to point at the field -- so, first recurse,
1272        // then check ABI.
1273        //
1274        // FIXME: We could avoid some redundant checks here. For newtypes wrapping
1275        // scalars, we do the same check on every "level" (e.g., first we check
1276        // MyNewtype and then the scalar in there).
1277        match val.layout.backend_repr {
1278            BackendRepr::Uninhabited => {
1279                let ty = val.layout.ty;
1280                throw_validation_failure!(self.path, UninhabitedVal { ty });
1281            }
1282            BackendRepr::Scalar(scalar_layout) => {
1283                if !scalar_layout.is_uninit_valid() {
1284                    // There is something to check here.
1285                    let scalar = self.read_scalar(val, ExpectedKind::InitScalar)?;
1286                    self.visit_scalar(scalar, scalar_layout)?;
1287                }
1288            }
1289            BackendRepr::ScalarPair(a_layout, b_layout) => {
1290                // We can only proceed if *both* scalars need to be initialized.
1291                // FIXME: find a way to also check ScalarPair when one side can be uninit but
1292                // the other must be init.
1293                if !a_layout.is_uninit_valid() && !b_layout.is_uninit_valid() {
1294                    let (a, b) =
1295                        self.read_immediate(val, ExpectedKind::InitScalar)?.to_scalar_pair();
1296                    self.visit_scalar(a, a_layout)?;
1297                    self.visit_scalar(b, b_layout)?;
1298                }
1299            }
1300            BackendRepr::Vector { .. } => {
1301                // No checks here, we assume layout computation gets this right.
1302                // (This is harder to check since Miri does not represent these as `Immediate`. We
1303                // also cannot use field projections since this might be a newtype around a vector.)
1304            }
1305            BackendRepr::Memory { .. } => {
1306                // Nothing to do.
1307            }
1308        }
1309
1310        interp_ok(())
1311    }
1312}
1313
1314impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
1315    fn validate_operand_internal(
1316        &mut self,
1317        val: &PlaceTy<'tcx, M::Provenance>,
1318        path: Vec<PathElem>,
1319        ref_tracking: Option<&mut RefTracking<MPlaceTy<'tcx, M::Provenance>, Vec<PathElem>>>,
1320        ctfe_mode: Option<CtfeValidationMode>,
1321        reset_provenance_and_padding: bool,
1322    ) -> InterpResult<'tcx> {
1323        trace!("validate_operand_internal: {:?}, {:?}", *val, val.layout.ty);
1324
1325        // Run the visitor.
1326        self.run_for_validation(|ecx| {
1327            let reset_padding = reset_provenance_and_padding && {
1328                // Check if `val` is actually stored in memory. If not, padding is not even
1329                // represented and we need not reset it.
1330                ecx.place_to_op(val)?.as_mplace_or_imm().is_left()
1331            };
1332            let mut v = ValidityVisitor {
1333                path,
1334                ref_tracking,
1335                ctfe_mode,
1336                ecx,
1337                reset_provenance_and_padding,
1338                data_bytes: reset_padding.then_some(RangeSet(Vec::new())),
1339            };
1340            v.visit_value(val)?;
1341            v.reset_padding(val)?;
1342            interp_ok(())
1343        })
1344        .map_err_info(|err| {
1345            if !matches!(
1346                err.kind(),
1347                err_ub!(ValidationError { .. })
1348                    | InterpErrorKind::InvalidProgram(_)
1349                    | InterpErrorKind::Unsupported(UnsupportedOpInfo::ExternTypeField)
1350            ) {
1351                bug!(
1352                    "Unexpected error during validation: {}",
1353                    format_interp_error(self.tcx.dcx(), err)
1354                );
1355            }
1356            err
1357        })
1358    }
1359
1360    /// This function checks the data at `op` to be const-valid.
1361    /// `op` is assumed to cover valid memory if it is an indirect operand.
1362    /// It will error if the bits at the destination do not match the ones described by the layout.
1363    ///
1364    /// `ref_tracking` is used to record references that we encounter so that they
1365    /// can be checked recursively by an outside driving loop.
1366    ///
1367    /// `constant` controls whether this must satisfy the rules for constants:
1368    /// - no pointers to statics.
1369    /// - no `UnsafeCell` or non-ZST `&mut`.
1370    #[inline(always)]
1371    pub(crate) fn const_validate_operand(
1372        &mut self,
1373        val: &PlaceTy<'tcx, M::Provenance>,
1374        path: Vec<PathElem>,
1375        ref_tracking: &mut RefTracking<MPlaceTy<'tcx, M::Provenance>, Vec<PathElem>>,
1376        ctfe_mode: CtfeValidationMode,
1377    ) -> InterpResult<'tcx> {
1378        self.validate_operand_internal(
1379            val,
1380            path,
1381            Some(ref_tracking),
1382            Some(ctfe_mode),
1383            /*reset_provenance*/ false,
1384        )
1385    }
1386
1387    /// This function checks the data at `op` to be runtime-valid.
1388    /// `op` is assumed to cover valid memory if it is an indirect operand.
1389    /// It will error if the bits at the destination do not match the ones described by the layout.
1390    #[inline(always)]
1391    pub fn validate_operand(
1392        &mut self,
1393        val: &PlaceTy<'tcx, M::Provenance>,
1394        recursive: bool,
1395        reset_provenance_and_padding: bool,
1396    ) -> InterpResult<'tcx> {
1397        // Note that we *could* actually be in CTFE here with `-Zextra-const-ub-checks`, but it's
1398        // still correct to not use `ctfe_mode`: that mode is for validation of the final constant
1399        // value, it rules out things like `UnsafeCell` in awkward places.
1400        if !recursive {
1401            return self.validate_operand_internal(
1402                val,
1403                vec![],
1404                None,
1405                None,
1406                reset_provenance_and_padding,
1407            );
1408        }
1409        // Do a recursive check.
1410        let mut ref_tracking = RefTracking::empty();
1411        self.validate_operand_internal(
1412            val,
1413            vec![],
1414            Some(&mut ref_tracking),
1415            None,
1416            reset_provenance_and_padding,
1417        )?;
1418        while let Some((mplace, path)) = ref_tracking.todo.pop() {
1419            // Things behind reference do *not* have the provenance reset.
1420            self.validate_operand_internal(
1421                &mplace.into(),
1422                path,
1423                Some(&mut ref_tracking),
1424                None,
1425                /*reset_provenance_and_padding*/ false,
1426            )?;
1427        }
1428        interp_ok(())
1429    }
1430}