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
7 use std::fmt::Write;
8 use std::num::NonZeroUsize;
9
10 use either::{Left, Right};
11
12 use rustc_ast::Mutability;
13 use rustc_data_structures::fx::FxHashSet;
14 use rustc_hir as hir;
15 use rustc_middle::mir::interpret::{
16 ExpectedKind, InterpError, InvalidMetaKind, PointerKind, ValidationErrorInfo,
17 ValidationErrorKind, ValidationErrorKind::*,
18 };
19 use rustc_middle::ty;
20 use rustc_middle::ty::layout::{LayoutOf, TyAndLayout};
21 use rustc_span::symbol::{sym, Symbol};
22 use rustc_target::abi::{
23 Abi, FieldIdx, Scalar as ScalarAbi, Size, VariantIdx, Variants, WrappingRange,
24 };
25
26 use std::hash::Hash;
27
28 // for the validation errors
29 use super::UndefinedBehaviorInfo::*;
30 use super::{
31 AllocId, CheckInAllocMsg, GlobalAlloc, ImmTy, Immediate, InterpCx, InterpResult, MPlaceTy,
32 Machine, MemPlaceMeta, OpTy, Pointer, Scalar, ValueVisitor,
33 };
34
35 macro_rules! throw_validation_failure {
36 ($where:expr, $kind: expr) => {{
37 let where_ = &$where;
38 let path = if !where_.is_empty() {
39 let mut path = String::new();
40 write_path(&mut path, where_);
41 Some(path)
42 } else {
43 None
44 };
45
46 throw_ub!(Validation(ValidationErrorInfo { path, kind: $kind }))
47 }};
48 }
49
50 /// If $e throws an error matching the pattern, throw a validation failure.
51 /// Other errors are passed back to the caller, unchanged -- and if they reach the root of
52 /// the visitor, we make sure only validation errors and `InvalidProgram` errors are left.
53 /// This lets you use the patterns as a kind of validation list, asserting which errors
54 /// can possibly happen:
55 ///
56 /// ```ignore(illustrative)
57 /// let v = try_validation!(some_fn(), some_path, {
58 /// Foo | Bar | Baz => { "some failure" },
59 /// });
60 /// ```
61 ///
62 /// The patterns must be of type `UndefinedBehaviorInfo`.
63 /// An additional expected parameter can also be added to the failure message:
64 ///
65 /// ```ignore(illustrative)
66 /// let v = try_validation!(some_fn(), some_path, {
67 /// Foo | Bar | Baz => { "some failure" } expected { "something that wasn't a failure" },
68 /// });
69 /// ```
70 ///
71 /// An additional nicety is that both parameters actually take format args, so you can just write
72 /// the format string in directly:
73 ///
74 /// ```ignore(illustrative)
75 /// let v = try_validation!(some_fn(), some_path, {
76 /// Foo | Bar | Baz => { "{:?}", some_failure } expected { "{}", expected_value },
77 /// });
78 /// ```
79 ///
80 macro_rules! try_validation {
81 ($e:expr, $where:expr,
82 $( $( $p:pat_param )|+ => $kind: expr ),+ $(,)?
83 ) => {{
84 match $e {
85 Ok(x) => x,
86 // We catch the error and turn it into a validation failure. We are okay with
87 // allocation here as this can only slow down builds that fail anyway.
88 Err(e) => match e.into_parts() {
89 $(
90 (InterpError::UndefinedBehavior($($p)|+), _) =>
91 throw_validation_failure!(
92 $where,
93 $kind
94 )
95 ),+,
96 #[allow(unreachable_patterns)]
97 (e, rest) => Err::<!, _>($crate::interpret::InterpErrorInfo::from_parts(e, rest))?,
98 }
99 }
100 }};
101 }
102
103 /// We want to show a nice path to the invalid field for diagnostics,
104 /// but avoid string operations in the happy case where no error happens.
105 /// So we track a `Vec<PathElem>` where `PathElem` contains all the data we
106 /// need to later print something for the user.
107 #[derive(Copy, Clone, Debug)]
108 pub enum PathElem {
109 Field(Symbol),
110 Variant(Symbol),
111 GeneratorState(VariantIdx),
112 CapturedVar(Symbol),
113 ArrayElem(usize),
114 TupleElem(usize),
115 Deref,
116 EnumTag,
117 GeneratorTag,
118 DynDowncast,
119 }
120
121 /// Extra things to check for during validation of CTFE results.
122 pub enum CtfeValidationMode {
123 /// Regular validation, nothing special happening.
124 Regular,
125 /// Validation of a `const`.
126 /// `inner` says if this is an inner, indirect allocation (as opposed to the top-level const
127 /// allocation). Being an inner allocation makes a difference because the top-level allocation
128 /// of a `const` is copied for each use, but the inner allocations are implicitly shared.
129 /// `allow_static_ptrs` says if pointers to statics are permitted (which is the case for promoteds in statics).
130 Const { inner: bool, allow_static_ptrs: bool },
131 }
132
133 /// State for tracking recursive validation of references
134 pub struct RefTracking<T, PATH = ()> {
135 pub seen: FxHashSet<T>,
136 pub todo: Vec<(T, PATH)>,
137 }
138
139 impl<T: Copy + Eq + Hash + std::fmt::Debug, PATH: Default> RefTracking<T, PATH> {
empty() -> Self140 pub fn empty() -> Self {
141 RefTracking { seen: FxHashSet::default(), todo: vec![] }
142 }
new(op: T) -> Self143 pub fn new(op: T) -> Self {
144 let mut ref_tracking_for_consts =
145 RefTracking { seen: FxHashSet::default(), todo: vec![(op, PATH::default())] };
146 ref_tracking_for_consts.seen.insert(op);
147 ref_tracking_for_consts
148 }
149
track(&mut self, op: T, path: impl FnOnce() -> PATH)150 pub fn track(&mut self, op: T, path: impl FnOnce() -> PATH) {
151 if self.seen.insert(op) {
152 trace!("Recursing below ptr {:#?}", op);
153 let path = path();
154 // Remember to come back to this later.
155 self.todo.push((op, path));
156 }
157 }
158 }
159
160 // FIXME make this translatable as well?
161 /// Format a path
write_path(out: &mut String, path: &[PathElem])162 fn write_path(out: &mut String, path: &[PathElem]) {
163 use self::PathElem::*;
164
165 for elem in path.iter() {
166 match elem {
167 Field(name) => write!(out, ".{}", name),
168 EnumTag => write!(out, ".<enum-tag>"),
169 Variant(name) => write!(out, ".<enum-variant({})>", name),
170 GeneratorTag => write!(out, ".<generator-tag>"),
171 GeneratorState(idx) => write!(out, ".<generator-state({})>", idx.index()),
172 CapturedVar(name) => write!(out, ".<captured-var({})>", name),
173 TupleElem(idx) => write!(out, ".{}", idx),
174 ArrayElem(idx) => write!(out, "[{}]", idx),
175 // `.<deref>` does not match Rust syntax, but it is more readable for long paths -- and
176 // some of the other items here also are not Rust syntax. Actually we can't
177 // even use the usual syntax because we are just showing the projections,
178 // not the root.
179 Deref => write!(out, ".<deref>"),
180 DynDowncast => write!(out, ".<dyn-downcast>"),
181 }
182 .unwrap()
183 }
184 }
185
186 struct ValidityVisitor<'rt, 'mir, 'tcx, M: Machine<'mir, 'tcx>> {
187 /// The `path` may be pushed to, but the part that is present when a function
188 /// starts must not be changed! `visit_fields` and `visit_array` rely on
189 /// this stack discipline.
190 path: Vec<PathElem>,
191 ref_tracking: Option<&'rt mut RefTracking<MPlaceTy<'tcx, M::Provenance>, Vec<PathElem>>>,
192 /// `None` indicates this is not validating for CTFE (but for runtime).
193 ctfe_mode: Option<CtfeValidationMode>,
194 ecx: &'rt InterpCx<'mir, 'tcx, M>,
195 }
196
197 impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, 'tcx, M> {
aggregate_field_path_elem(&mut self, layout: TyAndLayout<'tcx>, field: usize) -> PathElem198 fn aggregate_field_path_elem(&mut self, layout: TyAndLayout<'tcx>, field: usize) -> PathElem {
199 // First, check if we are projecting to a variant.
200 match layout.variants {
201 Variants::Multiple { tag_field, .. } => {
202 if tag_field == field {
203 return match layout.ty.kind() {
204 ty::Adt(def, ..) if def.is_enum() => PathElem::EnumTag,
205 ty::Generator(..) => PathElem::GeneratorTag,
206 _ => bug!("non-variant type {:?}", layout.ty),
207 };
208 }
209 }
210 Variants::Single { .. } => {}
211 }
212
213 // Now we know we are projecting to a field, so figure out which one.
214 match layout.ty.kind() {
215 // generators and closures.
216 ty::Closure(def_id, _) | ty::Generator(def_id, _, _) => {
217 let mut name = None;
218 // FIXME this should be more descriptive i.e. CapturePlace instead of CapturedVar
219 // https://github.com/rust-lang/project-rfc-2229/issues/46
220 if let Some(local_def_id) = def_id.as_local() {
221 let captures = self.ecx.tcx.closure_captures(local_def_id);
222 if let Some(captured_place) = captures.get(field) {
223 // Sometimes the index is beyond the number of upvars (seen
224 // for a generator).
225 let var_hir_id = captured_place.get_root_variable();
226 let node = self.ecx.tcx.hir().get(var_hir_id);
227 if let hir::Node::Pat(pat) = node {
228 if let hir::PatKind::Binding(_, _, ident, _) = pat.kind {
229 name = Some(ident.name);
230 }
231 }
232 }
233 }
234
235 PathElem::CapturedVar(name.unwrap_or_else(|| {
236 // Fall back to showing the field index.
237 sym::integer(field)
238 }))
239 }
240
241 // tuples
242 ty::Tuple(_) => PathElem::TupleElem(field),
243
244 // enums
245 ty::Adt(def, ..) if def.is_enum() => {
246 // we might be projecting *to* a variant, or to a field *in* a variant.
247 match layout.variants {
248 Variants::Single { index } => {
249 // Inside a variant
250 PathElem::Field(def.variant(index).fields[FieldIdx::from_usize(field)].name)
251 }
252 Variants::Multiple { .. } => bug!("we handled variants above"),
253 }
254 }
255
256 // other ADTs
257 ty::Adt(def, _) => {
258 PathElem::Field(def.non_enum_variant().fields[FieldIdx::from_usize(field)].name)
259 }
260
261 // arrays/slices
262 ty::Array(..) | ty::Slice(..) => PathElem::ArrayElem(field),
263
264 // dyn traits
265 ty::Dynamic(..) => PathElem::DynDowncast,
266
267 // nothing else has an aggregate layout
268 _ => bug!("aggregate_field_path_elem: got non-aggregate type {:?}", layout.ty),
269 }
270 }
271
with_elem<R>( &mut self, elem: PathElem, f: impl FnOnce(&mut Self) -> InterpResult<'tcx, R>, ) -> InterpResult<'tcx, R>272 fn with_elem<R>(
273 &mut self,
274 elem: PathElem,
275 f: impl FnOnce(&mut Self) -> InterpResult<'tcx, R>,
276 ) -> InterpResult<'tcx, R> {
277 // Remember the old state
278 let path_len = self.path.len();
279 // Record new element
280 self.path.push(elem);
281 // Perform operation
282 let r = f(self)?;
283 // Undo changes
284 self.path.truncate(path_len);
285 // Done
286 Ok(r)
287 }
288
read_immediate( &self, op: &OpTy<'tcx, M::Provenance>, expected: ExpectedKind, ) -> InterpResult<'tcx, ImmTy<'tcx, M::Provenance>>289 fn read_immediate(
290 &self,
291 op: &OpTy<'tcx, M::Provenance>,
292 expected: ExpectedKind,
293 ) -> InterpResult<'tcx, ImmTy<'tcx, M::Provenance>> {
294 Ok(try_validation!(
295 self.ecx.read_immediate(op),
296 self.path,
297 InvalidUninitBytes(None) => Uninit { expected }
298 ))
299 }
300
read_scalar( &self, op: &OpTy<'tcx, M::Provenance>, expected: ExpectedKind, ) -> InterpResult<'tcx, Scalar<M::Provenance>>301 fn read_scalar(
302 &self,
303 op: &OpTy<'tcx, M::Provenance>,
304 expected: ExpectedKind,
305 ) -> InterpResult<'tcx, Scalar<M::Provenance>> {
306 Ok(self.read_immediate(op, expected)?.to_scalar())
307 }
308
check_wide_ptr_meta( &mut self, meta: MemPlaceMeta<M::Provenance>, pointee: TyAndLayout<'tcx>, ) -> InterpResult<'tcx>309 fn check_wide_ptr_meta(
310 &mut self,
311 meta: MemPlaceMeta<M::Provenance>,
312 pointee: TyAndLayout<'tcx>,
313 ) -> InterpResult<'tcx> {
314 let tail = self.ecx.tcx.struct_tail_erasing_lifetimes(pointee.ty, self.ecx.param_env);
315 match tail.kind() {
316 ty::Dynamic(_, _, ty::Dyn) => {
317 let vtable = meta.unwrap_meta().to_pointer(self.ecx)?;
318 // Make sure it is a genuine vtable pointer.
319 let (_ty, _trait) = try_validation!(
320 self.ecx.get_ptr_vtable(vtable),
321 self.path,
322 DanglingIntPointer(..) |
323 InvalidVTablePointer(..) => InvalidVTablePtr { value: format!("{vtable}") }
324 );
325 // FIXME: check if the type/trait match what ty::Dynamic says?
326 }
327 ty::Slice(..) | ty::Str => {
328 let _len = meta.unwrap_meta().to_target_usize(self.ecx)?;
329 // We do not check that `len * elem_size <= isize::MAX`:
330 // that is only required for references, and there it falls out of the
331 // "dereferenceable" check performed by Stacked Borrows.
332 }
333 ty::Foreign(..) => {
334 // Unsized, but not wide.
335 }
336 _ => bug!("Unexpected unsized type tail: {:?}", tail),
337 }
338
339 Ok(())
340 }
341
342 /// Check a reference or `Box`.
check_safe_pointer( &mut self, value: &OpTy<'tcx, M::Provenance>, ptr_kind: PointerKind, ) -> InterpResult<'tcx>343 fn check_safe_pointer(
344 &mut self,
345 value: &OpTy<'tcx, M::Provenance>,
346 ptr_kind: PointerKind,
347 ) -> InterpResult<'tcx> {
348 let place = self.ecx.ref_to_mplace(&self.read_immediate(value, ptr_kind.into())?)?;
349 // Handle wide pointers.
350 // Check metadata early, for better diagnostics
351 if place.layout.is_unsized() {
352 self.check_wide_ptr_meta(place.meta, place.layout)?;
353 }
354 // Make sure this is dereferenceable and all.
355 let size_and_align = try_validation!(
356 self.ecx.size_and_align_of_mplace(&place),
357 self.path,
358 InvalidMeta(msg) => match msg {
359 InvalidMetaKind::SliceTooBig => InvalidMetaSliceTooLarge { ptr_kind },
360 InvalidMetaKind::TooBig => InvalidMetaTooLarge { ptr_kind },
361 }
362 );
363 let (size, align) = size_and_align
364 // for the purpose of validity, consider foreign types to have
365 // alignment and size determined by the layout (size will be 0,
366 // alignment should take attributes into account).
367 .unwrap_or_else(|| (place.layout.size, place.layout.align.abi));
368 // Direct call to `check_ptr_access_align` checks alignment even on CTFE machines.
369 try_validation!(
370 self.ecx.check_ptr_access_align(
371 place.ptr,
372 size,
373 align,
374 CheckInAllocMsg::InboundsTest, // will anyway be replaced by validity message
375 ),
376 self.path,
377 AlignmentCheckFailed { required, has } => UnalignedPtr {
378 ptr_kind,
379 required_bytes: required.bytes(),
380 found_bytes: has.bytes()
381 },
382 DanglingIntPointer(0, _) => NullPtr { ptr_kind },
383 DanglingIntPointer(i, _) => DanglingPtrNoProvenance {
384 ptr_kind,
385 // FIXME this says "null pointer" when null but we need translate
386 pointer: format!("{}", Pointer::<Option<AllocId>>::from_addr_invalid(i))
387 },
388 PointerOutOfBounds { .. } => DanglingPtrOutOfBounds {
389 ptr_kind
390 },
391 // This cannot happen during const-eval (because interning already detects
392 // dangling pointers), but it can happen in Miri.
393 PointerUseAfterFree(..) => DanglingPtrUseAfterFree {
394 ptr_kind,
395 },
396 );
397 // Do not allow pointers to uninhabited types.
398 if place.layout.abi.is_uninhabited() {
399 let ty = place.layout.ty;
400 throw_validation_failure!(self.path, PtrToUninhabited { ptr_kind, ty })
401 }
402 // Recursive checking
403 if let Some(ref_tracking) = self.ref_tracking.as_deref_mut() {
404 // Proceed recursively even for ZST, no reason to skip them!
405 // `!` is a ZST and we want to validate it.
406 if let Ok((alloc_id, _offset, _prov)) = self.ecx.ptr_try_get_alloc_id(place.ptr) {
407 // Let's see what kind of memory this points to.
408 let alloc_kind = self.ecx.tcx.try_get_global_alloc(alloc_id);
409 match alloc_kind {
410 Some(GlobalAlloc::Static(did)) => {
411 // Special handling for pointers to statics (irrespective of their type).
412 assert!(!self.ecx.tcx.is_thread_local_static(did));
413 assert!(self.ecx.tcx.is_static(did));
414 if matches!(
415 self.ctfe_mode,
416 Some(CtfeValidationMode::Const { allow_static_ptrs: false, .. })
417 ) {
418 // See const_eval::machine::MemoryExtra::can_access_statics for why
419 // this check is so important.
420 // This check is reachable when the const just referenced the static,
421 // but never read it (so we never entered `before_access_global`).
422 throw_validation_failure!(self.path, PtrToStatic { ptr_kind });
423 }
424 // We skip recursively checking other statics. These statics must be sound by
425 // themselves, and the only way to get broken statics here is by using
426 // unsafe code.
427 // The reasons we don't check other statics is twofold. For one, in all
428 // sound cases, the static was already validated on its own, and second, we
429 // trigger cycle errors if we try to compute the value of the other static
430 // and that static refers back to us.
431 // We might miss const-invalid data,
432 // but things are still sound otherwise (in particular re: consts
433 // referring to statics).
434 return Ok(());
435 }
436 Some(GlobalAlloc::Memory(alloc)) => {
437 if alloc.inner().mutability == Mutability::Mut
438 && matches!(self.ctfe_mode, Some(CtfeValidationMode::Const { .. }))
439 {
440 // This should be unreachable, but if someone manages to copy a pointer
441 // out of a `static`, then that pointer might point to mutable memory,
442 // and we would catch that here.
443 throw_validation_failure!(self.path, PtrToMut { ptr_kind });
444 }
445 }
446 // Nothing to check for these.
447 None | Some(GlobalAlloc::Function(..) | GlobalAlloc::VTable(..)) => {}
448 }
449 }
450 let path = &self.path;
451 ref_tracking.track(place, || {
452 // We need to clone the path anyway, make sure it gets created
453 // with enough space for the additional `Deref`.
454 let mut new_path = Vec::with_capacity(path.len() + 1);
455 new_path.extend(path);
456 new_path.push(PathElem::Deref);
457 new_path
458 });
459 }
460 Ok(())
461 }
462
463 /// Check if this is a value of primitive type, and if yes check the validity of the value
464 /// at that type. Return `true` if the type is indeed primitive.
try_visit_primitive( &mut self, value: &OpTy<'tcx, M::Provenance>, ) -> InterpResult<'tcx, bool>465 fn try_visit_primitive(
466 &mut self,
467 value: &OpTy<'tcx, M::Provenance>,
468 ) -> InterpResult<'tcx, bool> {
469 // Go over all the primitive types
470 let ty = value.layout.ty;
471 match ty.kind() {
472 ty::Bool => {
473 let value = self.read_scalar(value, ExpectedKind::Bool)?;
474 try_validation!(
475 value.to_bool(),
476 self.path,
477 InvalidBool(..) => ValidationErrorKind::InvalidBool {
478 value: format!("{value:x}"),
479 }
480 );
481 Ok(true)
482 }
483 ty::Char => {
484 let value = self.read_scalar(value, ExpectedKind::Char)?;
485 try_validation!(
486 value.to_char(),
487 self.path,
488 InvalidChar(..) => ValidationErrorKind::InvalidChar {
489 value: format!("{value:x}"),
490 }
491 );
492 Ok(true)
493 }
494 ty::Float(_) | ty::Int(_) | ty::Uint(_) => {
495 // NOTE: Keep this in sync with the array optimization for int/float
496 // types below!
497 let value = self.read_scalar(
498 value,
499 if matches!(ty.kind(), ty::Float(..)) {
500 ExpectedKind::Float
501 } else {
502 ExpectedKind::Int
503 },
504 )?;
505 // As a special exception we *do* match on a `Scalar` here, since we truly want
506 // to know its underlying representation (and *not* cast it to an integer).
507 if matches!(value, Scalar::Ptr(..)) {
508 throw_validation_failure!(
509 self.path,
510 ExpectedNonPtr { value: format!("{value:x}") }
511 )
512 }
513 Ok(true)
514 }
515 ty::RawPtr(..) => {
516 // We are conservative with uninit for integers, but try to
517 // actually enforce the strict rules for raw pointers (mostly because
518 // that lets us re-use `ref_to_mplace`).
519 let place =
520 self.ecx.ref_to_mplace(&self.read_immediate(value, ExpectedKind::RawPtr)?)?;
521 if place.layout.is_unsized() {
522 self.check_wide_ptr_meta(place.meta, place.layout)?;
523 }
524 Ok(true)
525 }
526 ty::Ref(_, ty, mutbl) => {
527 if matches!(self.ctfe_mode, Some(CtfeValidationMode::Const { .. }))
528 && *mutbl == Mutability::Mut
529 {
530 // A mutable reference inside a const? That does not seem right (except if it is
531 // a ZST).
532 let layout = self.ecx.layout_of(*ty)?;
533 if !layout.is_zst() {
534 throw_validation_failure!(self.path, MutableRefInConst);
535 }
536 }
537 self.check_safe_pointer(value, PointerKind::Ref)?;
538 Ok(true)
539 }
540 ty::FnPtr(_sig) => {
541 let value = self.read_scalar(value, ExpectedKind::FnPtr)?;
542
543 // If we check references recursively, also check that this points to a function.
544 if let Some(_) = self.ref_tracking {
545 let ptr = value.to_pointer(self.ecx)?;
546 let _fn = try_validation!(
547 self.ecx.get_ptr_fn(ptr),
548 self.path,
549 DanglingIntPointer(..) |
550 InvalidFunctionPointer(..) => InvalidFnPtr {
551 value: format!("{ptr}"),
552 },
553 );
554 // FIXME: Check if the signature matches
555 } else {
556 // Otherwise (for standalone Miri), we have to still check it to be non-null.
557 if self.ecx.scalar_may_be_null(value)? {
558 throw_validation_failure!(self.path, NullFnPtr);
559 }
560 }
561 Ok(true)
562 }
563 ty::Never => throw_validation_failure!(self.path, NeverVal),
564 ty::Foreign(..) | ty::FnDef(..) => {
565 // Nothing to check.
566 Ok(true)
567 }
568 // The above should be all the primitive types. The rest is compound, we
569 // check them by visiting their fields/variants.
570 ty::Adt(..)
571 | ty::Tuple(..)
572 | ty::Array(..)
573 | ty::Slice(..)
574 | ty::Str
575 | ty::Dynamic(..)
576 | ty::Closure(..)
577 | ty::Generator(..) => Ok(false),
578 // Some types only occur during typechecking, they have no layout.
579 // We should not see them here and we could not check them anyway.
580 ty::Error(_)
581 | ty::Infer(..)
582 | ty::Placeholder(..)
583 | ty::Bound(..)
584 | ty::Param(..)
585 | ty::Alias(..)
586 | ty::GeneratorWitnessMIR(..)
587 | ty::GeneratorWitness(..) => bug!("Encountered invalid type {:?}", ty),
588 }
589 }
590
visit_scalar( &mut self, scalar: Scalar<M::Provenance>, scalar_layout: ScalarAbi, ) -> InterpResult<'tcx>591 fn visit_scalar(
592 &mut self,
593 scalar: Scalar<M::Provenance>,
594 scalar_layout: ScalarAbi,
595 ) -> InterpResult<'tcx> {
596 let size = scalar_layout.size(self.ecx);
597 let valid_range = scalar_layout.valid_range(self.ecx);
598 let WrappingRange { start, end } = valid_range;
599 let max_value = size.unsigned_int_max();
600 assert!(end <= max_value);
601 let bits = match scalar.try_to_int() {
602 Ok(int) => int.assert_bits(size),
603 Err(_) => {
604 // So this is a pointer then, and casting to an int failed.
605 // Can only happen during CTFE.
606 // We support 2 kinds of ranges here: full range, and excluding zero.
607 if start == 1 && end == max_value {
608 // Only null is the niche. So make sure the ptr is NOT null.
609 if self.ecx.scalar_may_be_null(scalar)? {
610 throw_validation_failure!(
611 self.path,
612 NullablePtrOutOfRange { range: valid_range, max_value }
613 )
614 } else {
615 return Ok(());
616 }
617 } else if scalar_layout.is_always_valid(self.ecx) {
618 // Easy. (This is reachable if `enforce_number_validity` is set.)
619 return Ok(());
620 } else {
621 // Conservatively, we reject, because the pointer *could* have a bad
622 // value.
623 throw_validation_failure!(
624 self.path,
625 PtrOutOfRange { range: valid_range, max_value }
626 )
627 }
628 }
629 };
630 // Now compare.
631 if valid_range.contains(bits) {
632 Ok(())
633 } else {
634 throw_validation_failure!(
635 self.path,
636 OutOfRange { value: format!("{bits}"), range: valid_range, max_value }
637 )
638 }
639 }
640 }
641
642 impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValueVisitor<'mir, 'tcx, M>
643 for ValidityVisitor<'rt, 'mir, 'tcx, M>
644 {
645 type V = OpTy<'tcx, M::Provenance>;
646
647 #[inline(always)]
ecx(&self) -> &InterpCx<'mir, 'tcx, M>648 fn ecx(&self) -> &InterpCx<'mir, 'tcx, M> {
649 &self.ecx
650 }
651
read_discriminant( &mut self, op: &OpTy<'tcx, M::Provenance>, ) -> InterpResult<'tcx, VariantIdx>652 fn read_discriminant(
653 &mut self,
654 op: &OpTy<'tcx, M::Provenance>,
655 ) -> InterpResult<'tcx, VariantIdx> {
656 self.with_elem(PathElem::EnumTag, move |this| {
657 Ok(try_validation!(
658 this.ecx.read_discriminant(op),
659 this.path,
660 InvalidTag(val) => InvalidEnumTag {
661 value: format!("{val:x}"),
662 },
663
664 InvalidUninitBytes(None) => UninitEnumTag,
665 )
666 .1)
667 })
668 }
669
670 #[inline]
visit_field( &mut self, old_op: &OpTy<'tcx, M::Provenance>, field: usize, new_op: &OpTy<'tcx, M::Provenance>, ) -> InterpResult<'tcx>671 fn visit_field(
672 &mut self,
673 old_op: &OpTy<'tcx, M::Provenance>,
674 field: usize,
675 new_op: &OpTy<'tcx, M::Provenance>,
676 ) -> InterpResult<'tcx> {
677 let elem = self.aggregate_field_path_elem(old_op.layout, field);
678 self.with_elem(elem, move |this| this.visit_value(new_op))
679 }
680
681 #[inline]
visit_variant( &mut self, old_op: &OpTy<'tcx, M::Provenance>, variant_id: VariantIdx, new_op: &OpTy<'tcx, M::Provenance>, ) -> InterpResult<'tcx>682 fn visit_variant(
683 &mut self,
684 old_op: &OpTy<'tcx, M::Provenance>,
685 variant_id: VariantIdx,
686 new_op: &OpTy<'tcx, M::Provenance>,
687 ) -> InterpResult<'tcx> {
688 let name = match old_op.layout.ty.kind() {
689 ty::Adt(adt, _) => PathElem::Variant(adt.variant(variant_id).name),
690 // Generators also have variants
691 ty::Generator(..) => PathElem::GeneratorState(variant_id),
692 _ => bug!("Unexpected type with variant: {:?}", old_op.layout.ty),
693 };
694 self.with_elem(name, move |this| this.visit_value(new_op))
695 }
696
697 #[inline(always)]
visit_union( &mut self, op: &OpTy<'tcx, M::Provenance>, _fields: NonZeroUsize, ) -> InterpResult<'tcx>698 fn visit_union(
699 &mut self,
700 op: &OpTy<'tcx, M::Provenance>,
701 _fields: NonZeroUsize,
702 ) -> InterpResult<'tcx> {
703 // Special check preventing `UnsafeCell` inside unions in the inner part of constants.
704 if matches!(self.ctfe_mode, Some(CtfeValidationMode::Const { inner: true, .. })) {
705 if !op.layout.ty.is_freeze(*self.ecx.tcx, self.ecx.param_env) {
706 throw_validation_failure!(self.path, UnsafeCell);
707 }
708 }
709 Ok(())
710 }
711
712 #[inline]
visit_box(&mut self, op: &OpTy<'tcx, M::Provenance>) -> InterpResult<'tcx>713 fn visit_box(&mut self, op: &OpTy<'tcx, M::Provenance>) -> InterpResult<'tcx> {
714 self.check_safe_pointer(op, PointerKind::Box)?;
715 Ok(())
716 }
717
718 #[inline]
visit_value(&mut self, op: &OpTy<'tcx, M::Provenance>) -> InterpResult<'tcx>719 fn visit_value(&mut self, op: &OpTy<'tcx, M::Provenance>) -> InterpResult<'tcx> {
720 trace!("visit_value: {:?}, {:?}", *op, op.layout);
721
722 // Check primitive types -- the leaves of our recursive descent.
723 if self.try_visit_primitive(op)? {
724 return Ok(());
725 }
726
727 // Special check preventing `UnsafeCell` in the inner part of constants
728 if let Some(def) = op.layout.ty.ty_adt_def() {
729 if matches!(self.ctfe_mode, Some(CtfeValidationMode::Const { inner: true, .. }))
730 && def.is_unsafe_cell()
731 {
732 throw_validation_failure!(self.path, UnsafeCell);
733 }
734 }
735
736 // Recursively walk the value at its type.
737 self.walk_value(op)?;
738
739 // *After* all of this, check the ABI. We need to check the ABI to handle
740 // types like `NonNull` where the `Scalar` info is more restrictive than what
741 // the fields say (`rustc_layout_scalar_valid_range_start`).
742 // But in most cases, this will just propagate what the fields say,
743 // and then we want the error to point at the field -- so, first recurse,
744 // then check ABI.
745 //
746 // FIXME: We could avoid some redundant checks here. For newtypes wrapping
747 // scalars, we do the same check on every "level" (e.g., first we check
748 // MyNewtype and then the scalar in there).
749 match op.layout.abi {
750 Abi::Uninhabited => {
751 let ty = op.layout.ty;
752 throw_validation_failure!(self.path, UninhabitedVal { ty });
753 }
754 Abi::Scalar(scalar_layout) => {
755 if !scalar_layout.is_uninit_valid() {
756 // There is something to check here.
757 let scalar = self.read_scalar(op, ExpectedKind::InitScalar)?;
758 self.visit_scalar(scalar, scalar_layout)?;
759 }
760 }
761 Abi::ScalarPair(a_layout, b_layout) => {
762 // We can only proceed if *both* scalars need to be initialized.
763 // FIXME: find a way to also check ScalarPair when one side can be uninit but
764 // the other must be init.
765 if !a_layout.is_uninit_valid() && !b_layout.is_uninit_valid() {
766 let (a, b) =
767 self.read_immediate(op, ExpectedKind::InitScalar)?.to_scalar_pair();
768 self.visit_scalar(a, a_layout)?;
769 self.visit_scalar(b, b_layout)?;
770 }
771 }
772 Abi::Vector { .. } => {
773 // No checks here, we assume layout computation gets this right.
774 // (This is harder to check since Miri does not represent these as `Immediate`. We
775 // also cannot use field projections since this might be a newtype around a vector.)
776 }
777 Abi::Aggregate { .. } => {
778 // Nothing to do.
779 }
780 }
781
782 Ok(())
783 }
784
visit_aggregate( &mut self, op: &OpTy<'tcx, M::Provenance>, fields: impl Iterator<Item = InterpResult<'tcx, Self::V>>, ) -> InterpResult<'tcx>785 fn visit_aggregate(
786 &mut self,
787 op: &OpTy<'tcx, M::Provenance>,
788 fields: impl Iterator<Item = InterpResult<'tcx, Self::V>>,
789 ) -> InterpResult<'tcx> {
790 match op.layout.ty.kind() {
791 ty::Str => {
792 let mplace = op.assert_mem_place(); // strings are unsized and hence never immediate
793 let len = mplace.len(self.ecx)?;
794 try_validation!(
795 self.ecx.read_bytes_ptr_strip_provenance(mplace.ptr, Size::from_bytes(len)),
796 self.path,
797 InvalidUninitBytes(..) => { UninitStr },
798 );
799 }
800 ty::Array(tys, ..) | ty::Slice(tys)
801 // This optimization applies for types that can hold arbitrary bytes (such as
802 // integer and floating point types) or for structs or tuples with no fields.
803 // FIXME(wesleywiser) This logic could be extended further to arbitrary structs
804 // or tuples made up of integer/floating point types or inhabited ZSTs with no
805 // padding.
806 if matches!(tys.kind(), ty::Int(..) | ty::Uint(..) | ty::Float(..))
807 =>
808 {
809 // Optimized handling for arrays of integer/float type.
810
811 // This is the length of the array/slice.
812 let len = op.len(self.ecx)?;
813 // This is the element type size.
814 let layout = self.ecx.layout_of(*tys)?;
815 // This is the size in bytes of the whole array. (This checks for overflow.)
816 let size = layout.size * len;
817 // If the size is 0, there is nothing to check.
818 // (`size` can only be 0 of `len` is 0, and empty arrays are always valid.)
819 if size == Size::ZERO {
820 return Ok(());
821 }
822 // Now that we definitely have a non-ZST array, we know it lives in memory.
823 let mplace = match op.as_mplace_or_imm() {
824 Left(mplace) => mplace,
825 Right(imm) => match *imm {
826 Immediate::Uninit =>
827 throw_validation_failure!(self.path, UninitVal),
828 Immediate::Scalar(..) | Immediate::ScalarPair(..) =>
829 bug!("arrays/slices can never have Scalar/ScalarPair layout"),
830 }
831 };
832
833 // Optimization: we just check the entire range at once.
834 // NOTE: Keep this in sync with the handling of integer and float
835 // types above, in `visit_primitive`.
836 // In run-time mode, we accept pointers in here. This is actually more
837 // permissive than a per-element check would be, e.g., we accept
838 // a &[u8] that contains a pointer even though bytewise checking would
839 // reject it. However, that's good: We don't inherently want
840 // to reject those pointers, we just do not have the machinery to
841 // talk about parts of a pointer.
842 // We also accept uninit, for consistency with the slow path.
843 let alloc = self.ecx.get_ptr_alloc(mplace.ptr, size, mplace.align)?.expect("we already excluded size 0");
844
845 match alloc.get_bytes_strip_provenance() {
846 // In the happy case, we needn't check anything else.
847 Ok(_) => {}
848 // Some error happened, try to provide a more detailed description.
849 Err(err) => {
850 // For some errors we might be able to provide extra information.
851 // (This custom logic does not fit the `try_validation!` macro.)
852 match err.kind() {
853 err_ub!(InvalidUninitBytes(Some((_alloc_id, access)))) => {
854 // Some byte was uninitialized, determine which
855 // element that byte belongs to so we can
856 // provide an index.
857 let i = usize::try_from(
858 access.uninit.start.bytes() / layout.size.bytes(),
859 )
860 .unwrap();
861 self.path.push(PathElem::ArrayElem(i));
862
863 throw_validation_failure!(self.path, UninitVal)
864 }
865
866 // Propagate upwards (that will also check for unexpected errors).
867 _ => return Err(err),
868 }
869 }
870 }
871 }
872 // Fast path for arrays and slices of ZSTs. We only need to check a single ZST element
873 // of an array and not all of them, because there's only a single value of a specific
874 // ZST type, so either validation fails for all elements or none.
875 ty::Array(tys, ..) | ty::Slice(tys) if self.ecx.layout_of(*tys)?.is_zst() => {
876 // Validate just the first element (if any).
877 self.walk_aggregate(op, fields.take(1))?
878 }
879 _ => {
880 self.walk_aggregate(op, fields)? // default handler
881 }
882 }
883 Ok(())
884 }
885 }
886
887 impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
validate_operand_internal( &self, op: &OpTy<'tcx, M::Provenance>, path: Vec<PathElem>, ref_tracking: Option<&mut RefTracking<MPlaceTy<'tcx, M::Provenance>, Vec<PathElem>>>, ctfe_mode: Option<CtfeValidationMode>, ) -> InterpResult<'tcx>888 fn validate_operand_internal(
889 &self,
890 op: &OpTy<'tcx, M::Provenance>,
891 path: Vec<PathElem>,
892 ref_tracking: Option<&mut RefTracking<MPlaceTy<'tcx, M::Provenance>, Vec<PathElem>>>,
893 ctfe_mode: Option<CtfeValidationMode>,
894 ) -> InterpResult<'tcx> {
895 trace!("validate_operand_internal: {:?}, {:?}", *op, op.layout.ty);
896
897 // Construct a visitor
898 let mut visitor = ValidityVisitor { path, ref_tracking, ctfe_mode, ecx: self };
899
900 // Run it.
901 match visitor.visit_value(&op) {
902 Ok(()) => Ok(()),
903 // Pass through validation failures.
904 Err(err) if matches!(err.kind(), err_ub!(Validation { .. })) => Err(err),
905 // Complain about any other kind of UB error -- those are bad because we'd like to
906 // report them in a way that shows *where* in the value the issue lies.
907 Err(err) if matches!(err.kind(), InterpError::UndefinedBehavior(_)) => {
908 let (err, backtrace) = err.into_parts();
909 backtrace.print_backtrace();
910 bug!("Unexpected Undefined Behavior error during validation: {err:?}");
911 }
912 // Pass through everything else.
913 Err(err) => Err(err),
914 }
915 }
916
917 /// This function checks the data at `op` to be const-valid.
918 /// `op` is assumed to cover valid memory if it is an indirect operand.
919 /// It will error if the bits at the destination do not match the ones described by the layout.
920 ///
921 /// `ref_tracking` is used to record references that we encounter so that they
922 /// can be checked recursively by an outside driving loop.
923 ///
924 /// `constant` controls whether this must satisfy the rules for constants:
925 /// - no pointers to statics.
926 /// - no `UnsafeCell` or non-ZST `&mut`.
927 #[inline(always)]
const_validate_operand( &self, op: &OpTy<'tcx, M::Provenance>, path: Vec<PathElem>, ref_tracking: &mut RefTracking<MPlaceTy<'tcx, M::Provenance>, Vec<PathElem>>, ctfe_mode: CtfeValidationMode, ) -> InterpResult<'tcx>928 pub fn const_validate_operand(
929 &self,
930 op: &OpTy<'tcx, M::Provenance>,
931 path: Vec<PathElem>,
932 ref_tracking: &mut RefTracking<MPlaceTy<'tcx, M::Provenance>, Vec<PathElem>>,
933 ctfe_mode: CtfeValidationMode,
934 ) -> InterpResult<'tcx> {
935 self.validate_operand_internal(op, path, Some(ref_tracking), Some(ctfe_mode))
936 }
937
938 /// This function checks the data at `op` to be runtime-valid.
939 /// `op` is assumed to cover valid memory if it is an indirect operand.
940 /// It will error if the bits at the destination do not match the ones described by the layout.
941 #[inline(always)]
validate_operand(&self, op: &OpTy<'tcx, M::Provenance>) -> InterpResult<'tcx>942 pub fn validate_operand(&self, op: &OpTy<'tcx, M::Provenance>) -> InterpResult<'tcx> {
943 // Note that we *could* actually be in CTFE here with `-Zextra-const-ub-checks`, but it's
944 // still correct to not use `ctfe_mode`: that mode is for validation of the final constant
945 // value, it rules out things like `UnsafeCell` in awkward places. It also can make checking
946 // recurse through references which, for now, we don't want here, either.
947 self.validate_operand_internal(op, vec![], None, None)
948 }
949 }
950