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