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