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