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
8 use std::ops::RangeInclusive;
11 use rustc::ty::layout::{self, LayoutOf, TyLayout, VariantIdx};
12 use rustc_data_structures::fx::FxHashSet;
14 use rustc_span::symbol::{sym, Symbol};
19 CheckInAllocMsg, GlobalAlloc, InterpCx, InterpResult, MPlaceTy, Machine, MemPlaceMeta, OpTy,
23 macro_rules! throw_validation_failure {
24 ($what:expr, $where:expr, $details:expr) => {{
25 let mut msg = format!("encountered {}", $what);
27 if !where_.is_empty() {
29 write_path(&mut msg, where_);
31 write!(&mut msg, ", but expected {}", $details).unwrap();
32 throw_unsup!(ValidationFailure(msg))
34 ($what:expr, $where:expr) => {{
35 let mut msg = format!("encountered {}", $what);
37 if !where_.is_empty() {
39 write_path(&mut msg, where_);
41 throw_unsup!(ValidationFailure(msg))
45 macro_rules! try_validation {
46 ($e:expr, $what:expr, $where:expr, $details:expr) => {{
49 Err(_) => throw_validation_failure!($what, $where, $details),
53 ($e:expr, $what:expr, $where:expr) => {{
56 Err(_) => throw_validation_failure!($what, $where),
61 /// We want to show a nice path to the invalid field for diagnostics,
62 /// but avoid string operations in the happy case where no error happens.
63 /// So we track a `Vec<PathElem>` where `PathElem` contains all the data we
64 /// need to later print something for the user.
65 #[derive(Copy, Clone, Debug)]
69 GeneratorState(VariantIdx),
79 /// State for tracking recursive validation of references
80 pub struct RefTracking<T, PATH = ()> {
81 pub seen: FxHashSet<T>,
82 pub todo: Vec<(T, PATH)>,
85 impl<T: Copy + Eq + Hash + std::fmt::Debug, PATH: Default> RefTracking<T, PATH> {
86 pub fn empty() -> Self {
87 RefTracking { seen: FxHashSet::default(), todo: vec![] }
89 pub fn new(op: T) -> Self {
90 let mut ref_tracking_for_consts =
91 RefTracking { seen: FxHashSet::default(), todo: vec![(op, PATH::default())] };
92 ref_tracking_for_consts.seen.insert(op);
93 ref_tracking_for_consts
96 pub fn track(&mut self, op: T, path: impl FnOnce() -> PATH) {
97 if self.seen.insert(op) {
98 trace!("Recursing below ptr {:#?}", op);
100 // Remember to come back to this later.
101 self.todo.push((op, path));
107 fn write_path(out: &mut String, path: &Vec<PathElem>) {
108 use self::PathElem::*;
110 for elem in path.iter() {
112 Field(name) => write!(out, ".{}", name),
113 EnumTag => write!(out, ".<enum-tag>"),
114 Variant(name) => write!(out, ".<enum-variant({})>", name),
115 GeneratorTag => write!(out, ".<generator-tag>"),
116 GeneratorState(idx) => write!(out, ".<generator-state({})>", idx.index()),
117 CapturedVar(name) => write!(out, ".<captured-var({})>", name),
118 TupleElem(idx) => write!(out, ".{}", idx),
119 ArrayElem(idx) => write!(out, "[{}]", idx),
120 // `.<deref>` does not match Rust syntax, but it is more readable for long paths -- and
121 // some of the other items here also are not Rust syntax. Actually we can't
122 // even use the usual syntax because we are just showing the projections,
124 Deref => write!(out, ".<deref>"),
125 DynDowncast => write!(out, ".<dyn-downcast>"),
131 // Test if a range that wraps at overflow contains `test`
132 fn wrapping_range_contains(r: &RangeInclusive<u128>, test: u128) -> bool {
133 let (lo, hi) = r.clone().into_inner();
136 (..=hi).contains(&test) || (lo..).contains(&test)
143 // Formats such that a sentence like "expected something {}" to mean
144 // "expected something <in the given range>" makes sense.
145 fn wrapping_range_format(r: &RangeInclusive<u128>, max_hi: u128) -> String {
146 let (lo, hi) = r.clone().into_inner();
147 assert!(hi <= max_hi);
149 format!("less or equal to {}, or greater or equal to {}", hi, lo)
151 format!("equal to {}", lo)
153 assert!(hi < max_hi, "should not be printing if the range covers everything");
154 format!("less or equal to {}", hi)
155 } else if hi == max_hi {
156 assert!(lo > 0, "should not be printing if the range covers everything");
157 format!("greater or equal to {}", lo)
159 format!("in the range {:?}", r)
163 struct ValidityVisitor<'rt, 'mir, 'tcx, M: Machine<'mir, 'tcx>> {
164 /// The `path` may be pushed to, but the part that is present when a function
165 /// starts must not be changed! `visit_fields` and `visit_array` rely on
166 /// this stack discipline.
168 ref_tracking_for_consts:
169 Option<&'rt mut RefTracking<MPlaceTy<'tcx, M::PointerTag>, Vec<PathElem>>>,
170 ecx: &'rt InterpCx<'mir, 'tcx, M>,
173 impl<'rt, 'mir, 'tcx, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, 'tcx, M> {
174 fn aggregate_field_path_elem(&mut self, layout: TyLayout<'tcx>, field: usize) -> PathElem {
175 // First, check if we are projecting to a variant.
176 match layout.variants {
177 layout::Variants::Multiple { discr_index, .. } => {
178 if discr_index == field {
179 return match layout.ty.kind {
180 ty::Adt(def, ..) if def.is_enum() => PathElem::EnumTag,
181 ty::Generator(..) => PathElem::GeneratorTag,
182 _ => bug!("non-variant type {:?}", layout.ty),
186 layout::Variants::Single { .. } => {}
189 // Now we know we are projecting to a field, so figure out which one.
190 match layout.ty.kind {
191 // generators and closures.
192 ty::Closure(def_id, _) | ty::Generator(def_id, _, _) => {
194 if def_id.is_local() {
195 let tables = self.ecx.tcx.typeck_tables_of(def_id);
196 if let Some(upvars) = tables.upvar_list.get(&def_id) {
197 // Sometimes the index is beyond the number of upvars (seen
199 if let Some((&var_hir_id, _)) = upvars.get_index(field) {
200 let node = self.ecx.tcx.hir().get(var_hir_id);
201 if let hir::Node::Binding(pat) = node {
202 if let hir::PatKind::Binding(_, _, ident, _) = pat.kind {
203 name = Some(ident.name);
210 PathElem::CapturedVar(name.unwrap_or_else(|| {
211 // Fall back to showing the field index.
217 ty::Tuple(_) => PathElem::TupleElem(field),
220 ty::Adt(def, ..) if def.is_enum() => {
221 // we might be projecting *to* a variant, or to a field *in* a variant.
222 match layout.variants {
223 layout::Variants::Single { index } => {
225 PathElem::Field(def.variants[index].fields[field].ident.name)
227 layout::Variants::Multiple { .. } => bug!("we handled variants above"),
232 ty::Adt(def, _) => PathElem::Field(def.non_enum_variant().fields[field].ident.name),
235 ty::Array(..) | ty::Slice(..) => PathElem::ArrayElem(field),
238 ty::Dynamic(..) => PathElem::DynDowncast,
240 // nothing else has an aggregate layout
241 _ => bug!("aggregate_field_path_elem: got non-aggregate type {:?}", layout.ty),
247 new_op: OpTy<'tcx, M::PointerTag>,
249 ) -> InterpResult<'tcx> {
250 // Remember the old state
251 let path_len = self.path.len();
253 self.path.push(elem);
254 self.visit_value(new_op)?;
256 self.path.truncate(path_len);
260 fn check_wide_ptr_meta(
262 meta: MemPlaceMeta<M::PointerTag>,
263 pointee: TyLayout<'tcx>,
264 ) -> InterpResult<'tcx> {
265 let tail = self.ecx.tcx.struct_tail_erasing_lifetimes(pointee.ty, self.ecx.param_env);
268 let vtable = meta.unwrap_meta();
270 self.ecx.memory.check_ptr_access(
272 3 * self.ecx.tcx.data_layout.pointer_size, // drop, size, align
273 self.ecx.tcx.data_layout.pointer_align.abi,
275 "dangling or unaligned vtable pointer in wide pointer or too small vtable",
279 self.ecx.read_drop_type_from_vtable(vtable),
280 "invalid drop fn in vtable",
284 self.ecx.read_size_and_align_from_vtable(vtable),
285 "invalid size or align in vtable",
288 // FIXME: More checks for the vtable.
290 ty::Slice(..) | ty::Str => {
291 let _len = try_validation!(
292 meta.unwrap_meta().to_machine_usize(self.ecx),
293 "non-integer slice length in wide pointer",
296 // We do not check that `len * elem_size <= isize::MAX`:
297 // that is only required for references, and there it falls out of the
298 // "dereferenceable" check performed by Stacked Borrows.
301 // Unsized, but not wide.
303 _ => bug!("Unexpected unsized type tail: {:?}", tail),
309 /// Check a reference or `Box`.
310 fn check_safe_pointer(
312 value: OpTy<'tcx, M::PointerTag>,
314 ) -> InterpResult<'tcx> {
315 let value = self.ecx.read_immediate(value)?;
316 // Handle wide pointers.
317 // Check metadata early, for better diagnostics
318 let place = try_validation!(self.ecx.ref_to_mplace(value), "undefined pointer", self.path);
319 if place.layout.is_unsized() {
320 self.check_wide_ptr_meta(place.meta, place.layout)?;
322 // Make sure this is dereferenceable and all.
323 let (size, align) = self
325 .size_and_align_of(place.meta, place.layout)?
326 // for the purpose of validity, consider foreign types to have
327 // alignment and size determined by the layout (size will be 0,
328 // alignment should take attributes into account).
329 .unwrap_or_else(|| (place.layout.size, place.layout.align.abi));
330 let ptr: Option<_> = match self.ecx.memory.check_ptr_access_align(
334 CheckInAllocMsg::InboundsTest,
339 "{:?} did not pass access check for size {:?}, align {:?}",
340 place.ptr, size, align
343 err_unsup!(InvalidNullPointerUsage) => {
344 throw_validation_failure!(format_args!("a NULL {}", kind), self.path)
346 err_unsup!(AlignmentCheckFailed { required, has }) => {
347 throw_validation_failure!(
350 (required {} byte alignment but found {})",
358 err_unsup!(ReadBytesAsPointer) => throw_validation_failure!(
359 format_args!("a dangling {} (created from integer)", kind),
362 _ => throw_validation_failure!(
363 format_args!("a dangling {} (not entirely in bounds)", kind),
369 // Recursive checking
370 if let Some(ref mut ref_tracking) = self.ref_tracking_for_consts {
371 if let Some(ptr) = ptr {
373 // Skip validation entirely for some external statics
374 let alloc_kind = self.ecx.tcx.alloc_map.lock().get(ptr.alloc_id);
375 if let Some(GlobalAlloc::Static(did)) = alloc_kind {
376 // `extern static` cannot be validated as they have no body.
377 // FIXME: Statics from other crates are also skipped.
378 // They might be checked at a different type, but for now we
379 // want to avoid recursing too deeply. This is not sound!
380 if !did.is_local() || self.ecx.tcx.is_foreign_item(did) {
385 // Proceed recursively even for ZST, no reason to skip them!
386 // `!` is a ZST and we want to validate it.
387 // Normalize before handing `place` to tracking because that will
388 // check for duplicates.
389 let place = if size.bytes() > 0 {
390 self.ecx.force_mplace_ptr(place).expect("we already bounds-checked")
394 let path = &self.path;
395 ref_tracking.track(place, || {
396 // We need to clone the path anyway, make sure it gets created
397 // with enough space for the additional `Deref`.
398 let mut new_path = Vec::with_capacity(path.len() + 1);
399 new_path.clone_from(path);
400 new_path.push(PathElem::Deref);
407 /// Check if this is a value of primitive type, and if yes check the validity of the value
408 /// at that type. Return `true` if the type is indeed primitive.
409 fn try_visit_primitive(
411 value: OpTy<'tcx, M::PointerTag>,
412 ) -> InterpResult<'tcx, bool> {
413 // Go over all the primitive types
414 let ty = value.layout.ty;
417 let value = self.ecx.read_scalar(value)?;
418 try_validation!(value.to_bool(), value, self.path, "a boolean");
422 let value = self.ecx.read_scalar(value)?;
423 try_validation!(value.to_char(), value, self.path, "a valid unicode codepoint");
426 ty::Float(_) | ty::Int(_) | ty::Uint(_) => {
427 let value = self.ecx.read_scalar(value)?;
428 // NOTE: Keep this in sync with the array optimization for int/float
430 if self.ref_tracking_for_consts.is_some() {
431 // Integers/floats in CTFE: Must be scalar bits, pointers are dangerous
432 let is_bits = value.not_undef().map_or(false, |v| v.is_bits());
434 throw_validation_failure!(
437 "initialized plain (non-pointer) bytes"
441 // At run-time, for now, we accept *anything* for these types, including
442 // undef. We should fix that, but let's start low.
447 // We are conservative with undef for integers, but try to
448 // actually enforce our current rules for raw pointers.
449 let place = try_validation!(
450 self.ecx.ref_to_mplace(self.ecx.read_immediate(value)?),
454 if place.layout.is_unsized() {
455 self.check_wide_ptr_meta(place.meta, place.layout)?;
460 self.check_safe_pointer(value, "reference")?;
463 ty::Adt(def, ..) if def.is_box() => {
464 self.check_safe_pointer(value, "box")?;
468 let value = self.ecx.read_scalar(value)?;
469 let _fn = try_validation!(
470 value.not_undef().and_then(|ptr| self.ecx.memory.get_fn(ptr)),
475 // FIXME: Check if the signature matches
478 ty::Never => throw_validation_failure!("a value of the never type `!`", self.path),
479 ty::Foreign(..) | ty::FnDef(..) => {
483 // The above should be all the (inhabited) primitive types. The rest is compound, we
484 // check them by visiting their fields/variants.
485 // (`Str` UTF-8 check happens in `visit_aggregate`, too.)
493 | ty::Generator(..) => Ok(false),
494 // Some types only occur during typechecking, they have no layout.
495 // We should not see them here and we could not check them anyway.
498 | ty::Placeholder(..)
502 | ty::UnnormalizedProjection(..)
504 | ty::GeneratorWitness(..) => bug!("Encountered invalid type {:?}", ty),
510 op: OpTy<'tcx, M::PointerTag>,
511 scalar_layout: &layout::Scalar,
512 ) -> InterpResult<'tcx> {
513 let value = self.ecx.read_scalar(op)?;
514 let valid_range = &scalar_layout.valid_range;
515 let (lo, hi) = valid_range.clone().into_inner();
516 // Determine the allowed range
517 // `max_hi` is as big as the size fits
518 let max_hi = u128::MAX >> (128 - op.layout.size.bits());
519 assert!(hi <= max_hi);
520 // We could also write `(hi + 1) % (max_hi + 1) == lo` but `max_hi + 1` overflows for `u128`
521 if (lo == 0 && hi == max_hi) || (hi + 1 == lo) {
525 // At least one value is excluded. Get the bits.
526 let value = try_validation!(
530 format_args!("something {}", wrapping_range_format(valid_range, max_hi),)
532 let bits = match value.to_bits_or_ptr(op.layout.size, self.ecx) {
534 if lo == 1 && hi == max_hi {
535 // Only NULL is the niche. So make sure the ptr is NOT NULL.
536 if self.ecx.memory.ptr_may_be_null(ptr) {
537 throw_validation_failure!(
538 "a potentially NULL pointer",
541 "something that cannot possibly fail to be {}",
542 wrapping_range_format(valid_range, max_hi)
548 // Conservatively, we reject, because the pointer *could* have a bad
550 throw_validation_failure!(
554 "something that cannot possibly fail to be {}",
555 wrapping_range_format(valid_range, max_hi)
562 // Now compare. This is slightly subtle because this is a special "wrap-around" range.
563 if wrapping_range_contains(&valid_range, bits) {
566 throw_validation_failure!(
569 format_args!("something {}", wrapping_range_format(valid_range, max_hi))
575 impl<'rt, 'mir, 'tcx, M: Machine<'mir, 'tcx>> ValueVisitor<'mir, 'tcx, M>
576 for ValidityVisitor<'rt, 'mir, 'tcx, M>
578 type V = OpTy<'tcx, M::PointerTag>;
581 fn ecx(&self) -> &InterpCx<'mir, 'tcx, M> {
588 old_op: OpTy<'tcx, M::PointerTag>,
590 new_op: OpTy<'tcx, M::PointerTag>,
591 ) -> InterpResult<'tcx> {
592 let elem = self.aggregate_field_path_elem(old_op.layout, field);
593 self.visit_elem(new_op, elem)
599 old_op: OpTy<'tcx, M::PointerTag>,
600 variant_id: VariantIdx,
601 new_op: OpTy<'tcx, M::PointerTag>,
602 ) -> InterpResult<'tcx> {
603 let name = match old_op.layout.ty.kind {
604 ty::Adt(adt, _) => PathElem::Variant(adt.variants[variant_id].ident.name),
605 // Generators also have variants
606 ty::Generator(..) => PathElem::GeneratorState(variant_id),
607 _ => bug!("Unexpected type with variant: {:?}", old_op.layout.ty),
609 self.visit_elem(new_op, name)
613 fn visit_union(&mut self, op: OpTy<'tcx, M::PointerTag>, fields: usize) -> InterpResult<'tcx> {
614 // Empty unions are not accepted by rustc. But uninhabited enums
615 // claim to be unions, so allow them, too.
616 assert!(op.layout.abi.is_uninhabited() || fields > 0);
621 fn visit_value(&mut self, op: OpTy<'tcx, M::PointerTag>) -> InterpResult<'tcx> {
622 trace!("visit_value: {:?}, {:?}", *op, op.layout);
624 // Check primitive types -- the leafs of our recursive descend.
625 if self.try_visit_primitive(op)? {
628 // Sanity check: `builtin_deref` does not know any pointers that are not primitive.
629 assert!(op.layout.ty.builtin_deref(true).is_none());
631 // Recursively walk the type. Translate some possible errors to something nicer.
632 match self.walk_value(op) {
634 Err(err) => match err.kind {
635 err_ub!(InvalidDiscriminant(val)) => {
636 throw_validation_failure!(val, self.path, "a valid enum discriminant")
638 err_unsup!(ReadPointerAsBytes) => {
639 throw_validation_failure!("a pointer", self.path, "plain (non-pointer) bytes")
641 _ => return Err(err),
645 // *After* all of this, check the ABI. We need to check the ABI to handle
646 // types like `NonNull` where the `Scalar` info is more restrictive than what
647 // the fields say (`rustc_layout_scalar_valid_range_start`).
648 // But in most cases, this will just propagate what the fields say,
649 // and then we want the error to point at the field -- so, first recurse,
652 // FIXME: We could avoid some redundant checks here. For newtypes wrapping
653 // scalars, we do the same check on every "level" (e.g., first we check
654 // MyNewtype and then the scalar in there).
655 match op.layout.abi {
656 layout::Abi::Uninhabited => {
657 throw_validation_failure!(
658 format_args!("a value of uninhabited type {:?}", op.layout.ty),
662 layout::Abi::Scalar(ref scalar_layout) => {
663 self.visit_scalar(op, scalar_layout)?;
665 layout::Abi::ScalarPair { .. } | layout::Abi::Vector { .. } => {
666 // These have fields that we already visited above, so we already checked
667 // all their scalar-level restrictions.
668 // There is also no equivalent to `rustc_layout_scalar_valid_range_start`
669 // that would make skipping them here an issue.
671 layout::Abi::Aggregate { .. } => {
681 op: OpTy<'tcx, M::PointerTag>,
682 fields: impl Iterator<Item = InterpResult<'tcx, Self::V>>,
683 ) -> InterpResult<'tcx> {
684 match op.layout.ty.kind {
686 let mplace = op.assert_mem_place(self.ecx); // strings are never immediate
688 self.ecx.read_str(mplace),
689 "uninitialized or non-UTF-8 data in str",
693 ty::Array(tys, ..) | ty::Slice(tys)
695 // This optimization applies for types that can hold arbitrary bytes (such as
696 // integer and floating point types) or for structs or tuples with no fields.
697 // FIXME(wesleywiser) This logic could be extended further to arbitrary structs
698 // or tuples made up of integer/floating point types or inhabited ZSTs with no
701 ty::Int(..) | ty::Uint(..) | ty::Float(..) => true,
706 // Optimized handling for arrays of integer/float type.
708 // Arrays cannot be immediate, slices are never immediate.
709 let mplace = op.assert_mem_place(self.ecx);
710 // This is the length of the array/slice.
711 let len = mplace.len(self.ecx)?;
712 // Zero length slices have nothing to be checked.
716 // This is the element type size.
717 let layout = self.ecx.layout_of(tys)?;
718 // This is the size in bytes of the whole array.
719 let size = layout.size * len;
720 // Size is not 0, get a pointer.
721 let ptr = self.ecx.force_ptr(mplace.ptr)?;
723 // Optimization: we just check the entire range at once.
724 // NOTE: Keep this in sync with the handling of integer and float
725 // types above, in `visit_primitive`.
726 // In run-time mode, we accept pointers in here. This is actually more
727 // permissive than a per-element check would be, e.g., we accept
728 // an &[u8] that contains a pointer even though bytewise checking would
729 // reject it. However, that's good: We don't inherently want
730 // to reject those pointers, we just do not have the machinery to
731 // talk about parts of a pointer.
732 // We also accept undef, for consistency with the slow path.
733 match self.ecx.memory.get_raw(ptr.alloc_id)?.check_bytes(
737 /*allow_ptr_and_undef*/ self.ref_tracking_for_consts.is_none(),
739 // In the happy case, we needn't check anything else.
741 // Some error happened, try to provide a more detailed description.
743 // For some errors we might be able to provide extra information
745 err_unsup!(ReadUndefBytes(offset)) => {
746 // Some byte was undefined, determine which
747 // element that byte belongs to so we can
749 let i = (offset.bytes() / layout.size.bytes()) as usize;
750 self.path.push(PathElem::ArrayElem(i));
752 throw_validation_failure!("undefined bytes", self.path)
754 // Other errors shouldn't be possible
755 _ => return Err(err),
760 // Fast path for arrays and slices of ZSTs. We only need to check a single ZST element
761 // of an array and not all of them, because there's only a single value of a specific
762 // ZST type, so either validation fails for all elements or none.
763 ty::Array(tys, ..) | ty::Slice(tys) if self.ecx.layout_of(tys)?.is_zst() => {
764 // Validate just the first element
765 self.walk_aggregate(op, fields.take(1))?
768 self.walk_aggregate(op, fields)? // default handler
775 impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
776 /// This function checks the data at `op`. `op` is assumed to cover valid memory if it
777 /// is an indirect operand.
778 /// It will error if the bits at the destination do not match the ones described by the layout.
780 /// `ref_tracking_for_consts` can be `None` to avoid recursive checking below references.
781 /// This also toggles between "run-time" (no recursion) and "compile-time" (with recursion)
782 /// validation (e.g., pointer values are fine in integers at runtime) and various other const
783 /// specific validation checks.
784 pub fn validate_operand(
786 op: OpTy<'tcx, M::PointerTag>,
788 ref_tracking_for_consts: Option<
789 &mut RefTracking<MPlaceTy<'tcx, M::PointerTag>, Vec<PathElem>>,
791 ) -> InterpResult<'tcx> {
792 trace!("validate_operand: {:?}, {:?}", *op, op.layout.ty);
794 // Construct a visitor
795 let mut visitor = ValidityVisitor { path, ref_tracking_for_consts, ecx: self };
797 // Try to cast to ptr *once* instead of all the time.
798 let op = self.force_op_ptr(op).unwrap_or(op);
801 visitor.visit_value(op)