2019-08-30 08:37:02 +02:00
|
|
|
//! Check the validity invariant of a given value, and tell the user
|
|
|
|
|
//! where in the value it got violated.
|
|
|
|
|
//! In const context, this goes even further and tries to approximate const safety.
|
|
|
|
|
//! That's useful because it means other passes (e.g. promotion) can rely on `const`s
|
|
|
|
|
//! to be const-safe.
|
|
|
|
|
|
2024-08-29 19:24:31 +02:00
|
|
|
use std::borrow::Cow;
|
2023-05-17 10:30:14 +00:00
|
|
|
use std::fmt::Write;
|
2024-06-22 16:26:30 +02:00
|
|
|
use std::hash::Hash;
|
2024-01-29 23:59:09 +01:00
|
|
|
use std::num::NonZero;
|
2018-08-17 12:18:02 +02:00
|
|
|
|
2022-11-18 10:18:32 +01:00
|
|
|
use either::{Left, Right};
|
2023-12-16 16:24:25 +01:00
|
|
|
use hir::def::DefKind;
|
2024-10-29 13:37:26 -07:00
|
|
|
use rustc_abi::{
|
|
|
|
|
BackendRepr, FieldIdx, FieldsShape, Scalar as ScalarAbi, Size, VariantIdx, Variants,
|
|
|
|
|
WrappingRange,
|
|
|
|
|
};
|
2022-08-22 21:25:03 -04:00
|
|
|
use rustc_ast::Mutability;
|
2018-08-17 12:18:02 +02:00
|
|
|
use rustc_data_structures::fx::FxHashSet;
|
2020-01-05 02:37:57 +01:00
|
|
|
use rustc_hir as hir;
|
2024-09-15 13:11:05 +02:00
|
|
|
use rustc_middle::bug;
|
2023-05-17 10:30:14 +00:00
|
|
|
use rustc_middle::mir::interpret::ValidationErrorKind::{self, *};
|
|
|
|
|
use rustc_middle::mir::interpret::{
|
2024-10-19 08:49:13 +02:00
|
|
|
ExpectedKind, InterpErrorKind, InvalidMetaKind, Misalignment, PointerKind, Provenance,
|
|
|
|
|
UnsupportedOpInfo, ValidationErrorInfo, alloc_range, interp_ok,
|
2023-05-17 10:30:14 +00:00
|
|
|
};
|
2024-08-29 19:24:31 +02:00
|
|
|
use rustc_middle::ty::layout::{LayoutCx, LayoutOf, TyAndLayout};
|
2024-09-15 21:59:51 +02:00
|
|
|
use rustc_middle::ty::{self, Ty};
|
2019-12-31 20:15:40 +03:00
|
|
|
use rustc_span::symbol::{Symbol, sym};
|
2024-05-22 14:20:23 +10:00
|
|
|
use tracing::trace;
|
2018-08-17 12:18:02 +02:00
|
|
|
|
2024-06-13 21:01:00 +02:00
|
|
|
use super::machine::AllocMap;
|
2018-08-17 12:18:02 +02:00
|
|
|
use super::{
|
2024-11-09 13:13:31 +01:00
|
|
|
AllocId, CheckInAllocMsg, GlobalAlloc, ImmTy, Immediate, InterpCx, InterpResult, MPlaceTy,
|
|
|
|
|
Machine, MemPlaceMeta, PlaceTy, Pointer, Projectable, Scalar, ValueVisitor, err_ub,
|
2024-09-29 11:53:23 +02:00
|
|
|
format_interp_error,
|
2018-08-17 12:18:02 +02:00
|
|
|
};
|
|
|
|
|
|
2024-07-19 11:02:59 +10:00
|
|
|
// for the validation errors
|
|
|
|
|
#[rustfmt::skip]
|
2024-10-19 08:49:13 +02:00
|
|
|
use super::InterpErrorKind::UndefinedBehavior as Ub;
|
|
|
|
|
use super::InterpErrorKind::Unsupported as Unsup;
|
2023-08-02 16:14:36 +02:00
|
|
|
use super::UndefinedBehaviorInfo::*;
|
|
|
|
|
use super::UnsupportedOpInfo::*;
|
|
|
|
|
|
2024-09-29 11:53:23 +02:00
|
|
|
macro_rules! err_validation_failure {
|
2023-05-17 10:30:14 +00:00
|
|
|
($where:expr, $kind: expr) => {{
|
2023-03-22 00:00:00 +00:00
|
|
|
let where_ = &$where;
|
|
|
|
|
let path = if !where_.is_empty() {
|
|
|
|
|
let mut path = String::new();
|
|
|
|
|
write_path(&mut path, where_);
|
|
|
|
|
Some(path)
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
};
|
2023-05-17 10:30:14 +00:00
|
|
|
|
2024-09-29 11:53:23 +02:00
|
|
|
err_ub!(ValidationError(ValidationErrorInfo { path, kind: $kind }))
|
2018-08-17 12:18:02 +02:00
|
|
|
}};
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-29 11:53:23 +02:00
|
|
|
macro_rules! throw_validation_failure {
|
|
|
|
|
($where:expr, $kind: expr) => {
|
|
|
|
|
do yeet err_validation_failure!($where, $kind)
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-06 09:22:52 +02:00
|
|
|
/// If $e throws an error matching the pattern, throw a validation failure.
|
|
|
|
|
/// Other errors are passed back to the caller, unchanged -- and if they reach the root of
|
|
|
|
|
/// the visitor, we make sure only validation errors and `InvalidProgram` errors are left.
|
2020-07-07 11:12:44 -04:00
|
|
|
/// This lets you use the patterns as a kind of validation list, asserting which errors
|
2020-05-06 09:22:52 +02:00
|
|
|
/// can possibly happen:
|
2020-04-23 22:00:06 +10:00
|
|
|
///
|
2022-09-16 20:11:46 +09:00
|
|
|
/// ```ignore(illustrative)
|
2020-05-06 00:07:53 +02:00
|
|
|
/// let v = try_validation!(some_fn(), some_path, {
|
2020-05-01 17:52:42 +10:00
|
|
|
/// Foo | Bar | Baz => { "some failure" },
|
|
|
|
|
/// });
|
2020-04-23 22:00:06 +10:00
|
|
|
/// ```
|
2020-05-01 17:52:42 +10:00
|
|
|
///
|
2022-08-28 11:48:55 -04:00
|
|
|
/// The patterns must be of type `UndefinedBehaviorInfo`.
|
2020-05-01 17:52:42 +10:00
|
|
|
/// An additional expected parameter can also be added to the failure message:
|
|
|
|
|
///
|
2022-09-16 20:11:46 +09:00
|
|
|
/// ```ignore(illustrative)
|
2020-05-06 00:07:53 +02:00
|
|
|
/// let v = try_validation!(some_fn(), some_path, {
|
2020-05-01 17:52:42 +10:00
|
|
|
/// Foo | Bar | Baz => { "some failure" } expected { "something that wasn't a failure" },
|
|
|
|
|
/// });
|
|
|
|
|
/// ```
|
|
|
|
|
///
|
2020-05-01 21:49:42 +10:00
|
|
|
/// An additional nicety is that both parameters actually take format args, so you can just write
|
|
|
|
|
/// the format string in directly:
|
|
|
|
|
///
|
2022-09-16 20:11:46 +09:00
|
|
|
/// ```ignore(illustrative)
|
2020-05-06 00:07:53 +02:00
|
|
|
/// let v = try_validation!(some_fn(), some_path, {
|
2020-05-01 21:49:42 +10:00
|
|
|
/// Foo | Bar | Baz => { "{:?}", some_failure } expected { "{}", expected_value },
|
|
|
|
|
/// });
|
|
|
|
|
/// ```
|
|
|
|
|
///
|
2020-05-06 00:07:53 +02:00
|
|
|
macro_rules! try_validation {
|
|
|
|
|
($e:expr, $where:expr,
|
2023-05-17 10:30:14 +00:00
|
|
|
$( $( $p:pat_param )|+ => $kind: expr ),+ $(,)?
|
2020-05-06 00:07:53 +02:00
|
|
|
) => {{
|
2024-10-19 08:49:13 +02:00
|
|
|
$e.map_err_kind(|e| {
|
2020-04-23 22:52:27 +10:00
|
|
|
// We catch the error and turn it into a validation failure. We are okay with
|
|
|
|
|
// allocation here as this can only slow down builds that fail anyway.
|
2024-10-19 08:49:13 +02:00
|
|
|
match e {
|
2024-09-29 11:53:23 +02:00
|
|
|
$(
|
|
|
|
|
$($p)|+ => {
|
|
|
|
|
err_validation_failure!(
|
|
|
|
|
$where,
|
|
|
|
|
$kind
|
2024-10-19 08:49:13 +02:00
|
|
|
)
|
2024-09-29 11:53:23 +02:00
|
|
|
}
|
|
|
|
|
),+,
|
2024-10-19 08:49:13 +02:00
|
|
|
e => e,
|
2021-02-14 00:00:00 +00:00
|
|
|
}
|
2024-09-29 11:53:23 +02:00
|
|
|
})?
|
2019-12-12 15:23:46 +01:00
|
|
|
}};
|
2018-10-02 17:02:58 +02:00
|
|
|
}
|
|
|
|
|
|
2018-11-12 13:05:20 -05:00
|
|
|
/// We want to show a nice path to the invalid field for diagnostics,
|
2018-08-18 13:46:52 +02:00
|
|
|
/// but avoid string operations in the happy case where no error happens.
|
|
|
|
|
/// So we track a `Vec<PathElem>` where `PathElem` contains all the data we
|
|
|
|
|
/// need to later print something for the user.
|
|
|
|
|
#[derive(Copy, Clone, Debug)]
|
|
|
|
|
pub enum PathElem {
|
|
|
|
|
Field(Symbol),
|
2018-11-07 16:45:07 +01:00
|
|
|
Variant(Symbol),
|
2023-10-19 16:06:43 +00:00
|
|
|
CoroutineState(VariantIdx),
|
2020-02-26 12:00:33 +01:00
|
|
|
CapturedVar(Symbol),
|
2018-08-18 13:46:52 +02:00
|
|
|
ArrayElem(usize),
|
|
|
|
|
TupleElem(usize),
|
|
|
|
|
Deref,
|
2020-02-26 12:00:33 +01:00
|
|
|
EnumTag,
|
2023-10-19 16:06:43 +00:00
|
|
|
CoroutineTag,
|
2018-10-31 18:44:00 +01:00
|
|
|
DynDowncast,
|
2024-08-29 19:24:31 +02:00
|
|
|
Vtable,
|
2018-08-18 13:46:52 +02:00
|
|
|
}
|
|
|
|
|
|
2020-10-24 20:49:17 +02:00
|
|
|
/// Extra things to check for during validation of CTFE results.
|
2023-12-16 16:24:25 +01:00
|
|
|
#[derive(Copy, Clone)]
|
2020-10-24 20:49:17 +02:00
|
|
|
pub enum CtfeValidationMode {
|
2023-12-16 16:24:25 +01:00
|
|
|
/// Validation of a `static`
|
|
|
|
|
Static { mutbl: Mutability },
|
2024-02-12 08:51:41 +01:00
|
|
|
/// Validation of a promoted.
|
|
|
|
|
Promoted,
|
|
|
|
|
/// Validation of a `const`.
|
2023-12-16 16:24:25 +01:00
|
|
|
/// `allow_immutable_unsafe_cell` says whether we allow `UnsafeCell` in immutable memory (which is the
|
|
|
|
|
/// case for the top-level allocation of a `const`, where this is fine because the allocation will be
|
|
|
|
|
/// copied at each use site).
|
2024-02-12 08:51:41 +01:00
|
|
|
Const { allow_immutable_unsafe_cell: bool },
|
2023-12-16 16:24:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl CtfeValidationMode {
|
|
|
|
|
fn allow_immutable_unsafe_cell(self) -> bool {
|
|
|
|
|
match self {
|
|
|
|
|
CtfeValidationMode::Static { .. } => false,
|
2024-02-12 08:51:41 +01:00
|
|
|
CtfeValidationMode::Promoted { .. } => false,
|
2023-12-16 16:24:25 +01:00
|
|
|
CtfeValidationMode::Const { allow_immutable_unsafe_cell, .. } => {
|
|
|
|
|
allow_immutable_unsafe_cell
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-10-24 20:49:17 +02:00
|
|
|
}
|
|
|
|
|
|
2018-10-02 16:06:50 +02:00
|
|
|
/// State for tracking recursive validation of references
|
2019-02-10 14:59:13 +01:00
|
|
|
pub struct RefTracking<T, PATH = ()> {
|
2024-08-02 10:29:52 +02:00
|
|
|
seen: FxHashSet<T>,
|
|
|
|
|
todo: Vec<(T, PATH)>,
|
2018-10-02 16:06:50 +02:00
|
|
|
}
|
|
|
|
|
|
2023-07-25 22:35:07 +02:00
|
|
|
impl<T: Clone + Eq + Hash + std::fmt::Debug, PATH: Default> RefTracking<T, PATH> {
|
2019-02-10 14:59:13 +01:00
|
|
|
pub fn empty() -> Self {
|
|
|
|
|
RefTracking { seen: FxHashSet::default(), todo: vec![] }
|
|
|
|
|
}
|
2024-08-29 08:59:52 +02:00
|
|
|
pub fn new(val: T) -> Self {
|
2019-02-10 14:59:13 +01:00
|
|
|
let mut ref_tracking_for_consts =
|
2024-08-29 08:59:52 +02:00
|
|
|
RefTracking { seen: FxHashSet::default(), todo: vec![(val.clone(), PATH::default())] };
|
|
|
|
|
ref_tracking_for_consts.seen.insert(val);
|
2019-02-10 14:59:13 +01:00
|
|
|
ref_tracking_for_consts
|
|
|
|
|
}
|
2024-08-02 10:29:52 +02:00
|
|
|
pub fn next(&mut self) -> Option<(T, PATH)> {
|
|
|
|
|
self.todo.pop()
|
|
|
|
|
}
|
2019-02-10 14:59:13 +01:00
|
|
|
|
2024-08-29 08:59:52 +02:00
|
|
|
fn track(&mut self, val: T, path: impl FnOnce() -> PATH) {
|
|
|
|
|
if self.seen.insert(val.clone()) {
|
|
|
|
|
trace!("Recursing below ptr {:#?}", val);
|
2019-02-10 14:59:13 +01:00
|
|
|
let path = path();
|
|
|
|
|
// Remember to come back to this later.
|
2024-08-29 08:59:52 +02:00
|
|
|
self.todo.push((val, path));
|
2019-02-10 14:59:13 +01:00
|
|
|
}
|
2018-10-02 16:06:50 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-17 10:30:14 +00:00
|
|
|
// FIXME make this translatable as well?
|
2018-08-18 13:46:52 +02:00
|
|
|
/// Format a path
|
2020-12-30 12:59:07 +01:00
|
|
|
fn write_path(out: &mut String, path: &[PathElem]) {
|
2018-08-18 13:46:52 +02:00
|
|
|
use self::PathElem::*;
|
|
|
|
|
|
|
|
|
|
for elem in path.iter() {
|
|
|
|
|
match elem {
|
2023-07-25 23:17:39 +02:00
|
|
|
Field(name) => write!(out, ".{name}"),
|
2020-02-26 12:00:33 +01:00
|
|
|
EnumTag => write!(out, ".<enum-tag>"),
|
2023-07-25 23:17:39 +02:00
|
|
|
Variant(name) => write!(out, ".<enum-variant({name})>"),
|
2023-10-19 21:46:28 +00:00
|
|
|
CoroutineTag => write!(out, ".<coroutine-tag>"),
|
|
|
|
|
CoroutineState(idx) => write!(out, ".<coroutine-state({})>", idx.index()),
|
2023-07-25 23:17:39 +02:00
|
|
|
CapturedVar(name) => write!(out, ".<captured-var({name})>"),
|
|
|
|
|
TupleElem(idx) => write!(out, ".{idx}"),
|
|
|
|
|
ArrayElem(idx) => write!(out, "[{idx}]"),
|
2020-01-06 11:35:55 +01:00
|
|
|
// `.<deref>` does not match Rust syntax, but it is more readable for long paths -- and
|
2022-11-16 20:34:16 +00:00
|
|
|
// some of the other items here also are not Rust syntax. Actually we can't
|
2018-08-18 13:46:52 +02:00
|
|
|
// even use the usual syntax because we are just showing the projections,
|
|
|
|
|
// not the root.
|
2020-01-06 11:35:55 +01:00
|
|
|
Deref => write!(out, ".<deref>"),
|
2018-10-31 18:44:00 +01:00
|
|
|
DynDowncast => write!(out, ".<dyn-downcast>"),
|
2024-08-29 19:24:31 +02:00
|
|
|
Vtable => write!(out, ".<vtable>"),
|
2018-10-31 18:44:00 +01:00
|
|
|
}
|
|
|
|
|
.unwrap()
|
2018-08-18 13:46:52 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-29 19:24:31 +02:00
|
|
|
/// Represents a set of `Size` values as a sorted list of ranges.
|
|
|
|
|
// These are (offset, length) pairs, and they are sorted and mutually disjoint,
|
|
|
|
|
// and never adjacent (i.e. there's always a gap between two of them).
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
|
pub struct RangeSet(Vec<(Size, Size)>);
|
|
|
|
|
|
|
|
|
|
impl RangeSet {
|
|
|
|
|
fn add_range(&mut self, offset: Size, size: Size) {
|
2024-09-09 14:42:03 +02:00
|
|
|
if size.bytes() == 0 {
|
|
|
|
|
// No need to track empty ranges.
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-08-29 19:24:31 +02:00
|
|
|
let v = &mut self.0;
|
|
|
|
|
// We scan for a partition point where the left partition is all the elements that end
|
|
|
|
|
// strictly before we start. Those are elements that are too "low" to merge with us.
|
|
|
|
|
let idx =
|
|
|
|
|
v.partition_point(|&(other_offset, other_size)| other_offset + other_size < offset);
|
|
|
|
|
// Now we want to either merge with the first element of the second partition, or insert ourselves before that.
|
|
|
|
|
if let Some(&(other_offset, other_size)) = v.get(idx)
|
|
|
|
|
&& offset + size >= other_offset
|
|
|
|
|
{
|
|
|
|
|
// Their end is >= our start (otherwise it would not be in the 2nd partition) and
|
|
|
|
|
// our end is >= their start. This means we can merge the ranges.
|
|
|
|
|
let new_start = other_offset.min(offset);
|
|
|
|
|
let mut new_end = (other_offset + other_size).max(offset + size);
|
|
|
|
|
// We grew to the right, so merge with overlapping/adjacent elements.
|
|
|
|
|
// (We also may have grown to the left, but that can never make us adjacent with
|
|
|
|
|
// anything there since we selected the first such candidate via `partition_point`.)
|
|
|
|
|
let mut scan_right = 1;
|
|
|
|
|
while let Some(&(next_offset, next_size)) = v.get(idx + scan_right)
|
|
|
|
|
&& new_end >= next_offset
|
|
|
|
|
{
|
|
|
|
|
// Increase our size to absorb the next element.
|
|
|
|
|
new_end = new_end.max(next_offset + next_size);
|
|
|
|
|
// Look at the next element.
|
|
|
|
|
scan_right += 1;
|
|
|
|
|
}
|
|
|
|
|
// Update the element we grew.
|
|
|
|
|
v[idx] = (new_start, new_end - new_start);
|
|
|
|
|
// Remove the elements we absorbed (if any).
|
|
|
|
|
if scan_right > 1 {
|
|
|
|
|
drop(v.drain((idx + 1)..(idx + scan_right)));
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// Insert new element.
|
|
|
|
|
v.insert(idx, (offset, size));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-27 08:24:23 +02:00
|
|
|
struct ValidityVisitor<'rt, 'tcx, M: Machine<'tcx>> {
|
2018-10-31 16:46:33 +01:00
|
|
|
/// The `path` may be pushed to, but the part that is present when a function
|
|
|
|
|
/// starts must not be changed! `visit_fields` and `visit_array` rely on
|
|
|
|
|
/// this stack discipline.
|
|
|
|
|
path: Vec<PathElem>,
|
2022-07-18 18:47:31 -04:00
|
|
|
ref_tracking: Option<&'rt mut RefTracking<MPlaceTy<'tcx, M::Provenance>, Vec<PathElem>>>,
|
2020-10-24 20:49:17 +02:00
|
|
|
/// `None` indicates this is not validating for CTFE (but for runtime).
|
|
|
|
|
ctfe_mode: Option<CtfeValidationMode>,
|
2024-08-29 08:59:52 +02:00
|
|
|
ecx: &'rt mut InterpCx<'tcx, M>,
|
|
|
|
|
/// Whether provenance should be reset outside of pointers (emulating the effect of a typed
|
|
|
|
|
/// copy).
|
2024-08-29 19:24:31 +02:00
|
|
|
reset_provenance_and_padding: bool,
|
|
|
|
|
/// This tracks which byte ranges in this value contain data; the remaining bytes are padding.
|
|
|
|
|
/// The ideal representation here would be pointer-length pairs, but to keep things more compact
|
|
|
|
|
/// we only store a (range) set of offsets -- the base pointer is the same throughout the entire
|
|
|
|
|
/// visit, after all.
|
|
|
|
|
/// If this is `Some`, then `reset_provenance_and_padding` must be true (but not vice versa:
|
|
|
|
|
/// we might not track data vs padding bytes if the operand isn't stored in memory anyway).
|
|
|
|
|
data_bytes: Option<RangeSet>,
|
2018-10-31 16:46:33 +01:00
|
|
|
}
|
|
|
|
|
|
2024-05-27 08:24:23 +02:00
|
|
|
impl<'rt, 'tcx, M: Machine<'tcx>> ValidityVisitor<'rt, 'tcx, M> {
|
2020-03-04 14:50:21 +00:00
|
|
|
fn aggregate_field_path_elem(&mut self, layout: TyAndLayout<'tcx>, field: usize) -> PathElem {
|
2020-02-26 12:00:33 +01:00
|
|
|
// First, check if we are projecting to a variant.
|
|
|
|
|
match layout.variants {
|
2020-05-23 13:22:45 +02:00
|
|
|
Variants::Multiple { tag_field, .. } => {
|
|
|
|
|
if tag_field == field {
|
2020-08-03 00:49:11 +02:00
|
|
|
return match layout.ty.kind() {
|
2020-02-26 12:00:33 +01:00
|
|
|
ty::Adt(def, ..) if def.is_enum() => PathElem::EnumTag,
|
2023-10-19 16:06:43 +00:00
|
|
|
ty::Coroutine(..) => PathElem::CoroutineTag,
|
2020-02-26 12:00:33 +01:00
|
|
|
_ => bug!("non-variant type {:?}", layout.ty),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-03-31 18:16:47 +02:00
|
|
|
Variants::Single { .. } => {}
|
2020-02-26 12:00:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Now we know we are projecting to a field, so figure out which one.
|
2020-08-03 00:49:11 +02:00
|
|
|
match layout.ty.kind() {
|
2024-02-11 22:09:28 +00:00
|
|
|
// coroutines, closures, and coroutine-closures all have upvars that may be named.
|
|
|
|
|
ty::Closure(def_id, _) | ty::Coroutine(def_id, _) | ty::CoroutineClosure(def_id, _) => {
|
2018-11-26 20:58:59 +02:00
|
|
|
let mut name = None;
|
2020-12-23 15:38:22 -05:00
|
|
|
// FIXME this should be more descriptive i.e. CapturePlace instead of CapturedVar
|
|
|
|
|
// https://github.com/rust-lang/project-rfc-2229/issues/46
|
|
|
|
|
if let Some(local_def_id) = def_id.as_local() {
|
2022-10-25 17:59:18 +00:00
|
|
|
let captures = self.ecx.tcx.closure_captures(local_def_id);
|
|
|
|
|
if let Some(captured_place) = captures.get(field) {
|
2019-05-04 03:57:46 +03:00
|
|
|
// Sometimes the index is beyond the number of upvars (seen
|
2023-10-19 21:46:28 +00:00
|
|
|
// for a coroutine).
|
2020-12-23 15:38:22 -05:00
|
|
|
let var_hir_id = captured_place.get_root_variable();
|
2023-12-01 05:28:34 -08:00
|
|
|
let node = self.ecx.tcx.hir_node(var_hir_id);
|
2022-06-28 13:15:30 -05:00
|
|
|
if let hir::Node::Pat(pat) = node {
|
2020-12-23 15:38:22 -05:00
|
|
|
if let hir::PatKind::Binding(_, _, ident, _) = pat.kind {
|
|
|
|
|
name = Some(ident.name);
|
2018-11-26 20:58:59 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-10-31 18:44:00 +01:00
|
|
|
}
|
2018-11-26 20:58:59 +02:00
|
|
|
|
2020-02-26 12:00:33 +01:00
|
|
|
PathElem::CapturedVar(name.unwrap_or_else(|| {
|
2018-11-26 20:58:59 +02:00
|
|
|
// Fall back to showing the field index.
|
2019-05-22 19:25:39 +10:00
|
|
|
sym::integer(field)
|
2018-11-26 20:58:59 +02:00
|
|
|
}))
|
2018-10-31 18:44:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// tuples
|
|
|
|
|
ty::Tuple(_) => PathElem::TupleElem(field),
|
|
|
|
|
|
|
|
|
|
// enums
|
|
|
|
|
ty::Adt(def, ..) if def.is_enum() => {
|
2020-02-17 22:59:16 +01:00
|
|
|
// we might be projecting *to* a variant, or to a field *in* a variant.
|
2018-11-01 13:53:21 +01:00
|
|
|
match layout.variants {
|
2020-03-31 18:16:47 +02:00
|
|
|
Variants::Single { index } => {
|
2020-01-03 13:31:56 +01:00
|
|
|
// Inside a variant
|
2023-03-28 23:32:25 -07:00
|
|
|
PathElem::Field(def.variant(index).fields[FieldIdx::from_usize(field)].name)
|
2019-12-22 17:42:04 -05:00
|
|
|
}
|
2020-03-31 18:16:47 +02:00
|
|
|
Variants::Multiple { .. } => bug!("we handled variants above"),
|
2018-11-01 13:53:21 +01:00
|
|
|
}
|
2018-10-31 18:44:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// other ADTs
|
2023-03-28 23:32:25 -07:00
|
|
|
ty::Adt(def, _) => {
|
|
|
|
|
PathElem::Field(def.non_enum_variant().fields[FieldIdx::from_usize(field)].name)
|
|
|
|
|
}
|
2018-10-31 18:44:00 +01:00
|
|
|
|
|
|
|
|
// arrays/slices
|
|
|
|
|
ty::Array(..) | ty::Slice(..) => PathElem::ArrayElem(field),
|
|
|
|
|
|
2024-08-29 19:24:31 +02:00
|
|
|
// dyn* vtables
|
|
|
|
|
ty::Dynamic(_, _, ty::DynKind::DynStar) if field == 1 => PathElem::Vtable,
|
|
|
|
|
|
2018-10-31 18:44:00 +01:00
|
|
|
// dyn traits
|
2024-08-29 19:24:31 +02:00
|
|
|
ty::Dynamic(..) => {
|
|
|
|
|
assert_eq!(field, 0);
|
|
|
|
|
PathElem::DynDowncast
|
|
|
|
|
}
|
2018-10-31 18:44:00 +01:00
|
|
|
|
|
|
|
|
// nothing else has an aggregate layout
|
|
|
|
|
_ => bug!("aggregate_field_path_elem: got non-aggregate type {:?}", layout.ty),
|
2018-11-08 17:06:27 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-05 13:40:27 +02:00
|
|
|
fn with_elem<R>(
|
2018-11-08 17:06:27 +01:00
|
|
|
&mut self,
|
|
|
|
|
elem: PathElem,
|
2020-07-05 13:40:27 +02:00
|
|
|
f: impl FnOnce(&mut Self) -> InterpResult<'tcx, R>,
|
|
|
|
|
) -> InterpResult<'tcx, R> {
|
2018-11-08 17:06:27 +01:00
|
|
|
// Remember the old state
|
|
|
|
|
let path_len = self.path.len();
|
2020-07-05 13:40:27 +02:00
|
|
|
// Record new element
|
2018-10-31 18:44:00 +01:00
|
|
|
self.path.push(elem);
|
2020-07-05 13:40:27 +02:00
|
|
|
// Perform operation
|
|
|
|
|
let r = f(self)?;
|
2018-11-08 17:06:27 +01:00
|
|
|
// Undo changes
|
|
|
|
|
self.path.truncate(path_len);
|
2020-07-05 13:40:27 +02:00
|
|
|
// Done
|
2024-09-29 11:53:23 +02:00
|
|
|
interp_ok(r)
|
2018-10-31 16:46:33 +01:00
|
|
|
}
|
2019-08-25 13:57:46 +02:00
|
|
|
|
2022-08-01 19:05:20 -04:00
|
|
|
fn read_immediate(
|
|
|
|
|
&self,
|
2024-08-29 08:59:52 +02:00
|
|
|
val: &PlaceTy<'tcx, M::Provenance>,
|
2023-05-17 10:30:14 +00:00
|
|
|
expected: ExpectedKind,
|
2022-08-01 19:05:20 -04:00
|
|
|
) -> InterpResult<'tcx, ImmTy<'tcx, M::Provenance>> {
|
2024-09-29 11:53:23 +02:00
|
|
|
interp_ok(try_validation!(
|
2024-08-29 08:59:52 +02:00
|
|
|
self.ecx.read_immediate(val),
|
2022-08-01 19:05:20 -04:00
|
|
|
self.path,
|
2023-08-02 16:14:36 +02:00
|
|
|
Ub(InvalidUninitBytes(None)) =>
|
|
|
|
|
Uninit { expected },
|
|
|
|
|
// The `Unsup` cases can only occur during CTFE
|
|
|
|
|
Unsup(ReadPointerAsInt(_)) =>
|
|
|
|
|
PointerAsInt { expected },
|
|
|
|
|
Unsup(ReadPartialPointer(_)) =>
|
|
|
|
|
PartialPointer,
|
2022-08-01 19:05:20 -04:00
|
|
|
))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn read_scalar(
|
|
|
|
|
&self,
|
2024-08-29 08:59:52 +02:00
|
|
|
val: &PlaceTy<'tcx, M::Provenance>,
|
2023-05-17 10:30:14 +00:00
|
|
|
expected: ExpectedKind,
|
2022-08-01 19:05:20 -04:00
|
|
|
) -> InterpResult<'tcx, Scalar<M::Provenance>> {
|
2024-09-29 11:53:23 +02:00
|
|
|
interp_ok(self.read_immediate(val, expected)?.to_scalar())
|
2024-08-29 08:59:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn deref_pointer(
|
|
|
|
|
&mut self,
|
|
|
|
|
val: &PlaceTy<'tcx, M::Provenance>,
|
|
|
|
|
expected: ExpectedKind,
|
|
|
|
|
) -> InterpResult<'tcx, MPlaceTy<'tcx, M::Provenance>> {
|
|
|
|
|
// Not using `ecx.deref_pointer` since we want to use our `read_immediate` wrapper.
|
|
|
|
|
let imm = self.read_immediate(val, expected)?;
|
|
|
|
|
// Reset provenance: ensure slice tail metadata does not preserve provenance,
|
|
|
|
|
// and ensure all pointers do not preserve partial provenance.
|
2024-08-29 19:24:31 +02:00
|
|
|
if self.reset_provenance_and_padding {
|
2024-10-29 13:37:26 -07:00
|
|
|
if matches!(imm.layout.backend_repr, BackendRepr::Scalar(..)) {
|
2024-08-29 08:59:52 +02:00
|
|
|
// A thin pointer. If it has provenance, we don't have to do anything.
|
|
|
|
|
// If it does not, ensure we clear the provenance in memory.
|
|
|
|
|
if matches!(imm.to_scalar(), Scalar::Int(..)) {
|
|
|
|
|
self.ecx.clear_provenance(val)?;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// A wide pointer. This means we have to worry both about the pointer itself and the
|
|
|
|
|
// metadata. We do the lazy thing and just write back the value we got. Just
|
|
|
|
|
// clearing provenance in a targeted manner would be more efficient, but unless this
|
|
|
|
|
// is a perf hotspot it's just not worth the effort.
|
|
|
|
|
self.ecx.write_immediate_no_validate(*imm, val)?;
|
|
|
|
|
}
|
2024-08-29 19:24:31 +02:00
|
|
|
// The entire thing is data, not padding.
|
|
|
|
|
self.add_data_range_place(val);
|
2024-08-29 08:59:52 +02:00
|
|
|
}
|
|
|
|
|
// Now turn it into a place.
|
|
|
|
|
self.ecx.ref_to_mplace(&imm)
|
2022-08-01 19:05:20 -04:00
|
|
|
}
|
|
|
|
|
|
2019-08-25 13:57:46 +02:00
|
|
|
fn check_wide_ptr_meta(
|
|
|
|
|
&mut self,
|
2022-07-18 18:47:31 -04:00
|
|
|
meta: MemPlaceMeta<M::Provenance>,
|
2020-03-04 14:50:21 +00:00
|
|
|
pointee: TyAndLayout<'tcx>,
|
2019-08-25 13:57:46 +02:00
|
|
|
) -> InterpResult<'tcx> {
|
2024-11-15 13:53:31 +01:00
|
|
|
let tail = self.ecx.tcx.struct_tail_for_codegen(pointee.ty, self.ecx.typing_env());
|
2020-08-03 00:49:11 +02:00
|
|
|
match tail.kind() {
|
2024-04-21 11:35:02 +02:00
|
|
|
ty::Dynamic(data, _, ty::Dyn) => {
|
2022-07-23 10:36:57 -04:00
|
|
|
let vtable = meta.unwrap_meta().to_pointer(self.ecx)?;
|
2024-06-10 17:24:36 +02:00
|
|
|
// Make sure it is a genuine vtable pointer for the right trait.
|
|
|
|
|
try_validation!(
|
|
|
|
|
self.ecx.get_ptr_vtable_ty(vtable, Some(data)),
|
2020-05-06 09:22:52 +02:00
|
|
|
self.path,
|
2024-07-27 18:09:50 +02:00
|
|
|
Ub(DanglingIntPointer{ .. } | InvalidVTablePointer(..)) =>
|
2024-06-10 17:24:36 +02:00
|
|
|
InvalidVTablePtr { value: format!("{vtable}") },
|
2024-09-22 20:00:01 -04:00
|
|
|
Ub(InvalidVTableTrait { vtable_dyn_type, expected_dyn_type }) => {
|
|
|
|
|
InvalidMetaWrongTrait { vtable_dyn_type, expected_dyn_type }
|
2024-06-10 17:24:36 +02:00
|
|
|
},
|
2019-08-25 13:57:46 +02:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
ty::Slice(..) | ty::Str => {
|
2023-02-14 14:31:26 +00:00
|
|
|
let _len = meta.unwrap_meta().to_target_usize(self.ecx)?;
|
2019-08-26 19:48:56 +02:00
|
|
|
// We do not check that `len * elem_size <= isize::MAX`:
|
|
|
|
|
// that is only required for references, and there it falls out of the
|
2019-11-22 18:11:28 +01:00
|
|
|
// "dereferenceable" check performed by Stacked Borrows.
|
2019-08-25 13:57:46 +02:00
|
|
|
}
|
|
|
|
|
ty::Foreign(..) => {
|
|
|
|
|
// Unsized, but not wide.
|
|
|
|
|
}
|
|
|
|
|
_ => bug!("Unexpected unsized type tail: {:?}", tail),
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-29 11:53:23 +02:00
|
|
|
interp_ok(())
|
2019-08-25 13:57:46 +02:00
|
|
|
}
|
2018-10-31 16:46:33 +01:00
|
|
|
|
2020-03-02 13:09:13 +01:00
|
|
|
/// Check a reference or `Box`.
|
2020-03-02 21:17:34 +01:00
|
|
|
fn check_safe_pointer(
|
|
|
|
|
&mut self,
|
2024-08-29 08:59:52 +02:00
|
|
|
value: &PlaceTy<'tcx, M::Provenance>,
|
2023-05-17 10:30:14 +00:00
|
|
|
ptr_kind: PointerKind,
|
2020-03-02 21:17:34 +01:00
|
|
|
) -> InterpResult<'tcx> {
|
2024-08-29 08:59:52 +02:00
|
|
|
let place = self.deref_pointer(value, ptr_kind.into())?;
|
2020-03-02 13:09:13 +01:00
|
|
|
// Handle wide pointers.
|
|
|
|
|
// Check metadata early, for better diagnostics
|
|
|
|
|
if place.layout.is_unsized() {
|
2023-09-04 17:53:38 +02:00
|
|
|
self.check_wide_ptr_meta(place.meta(), place.layout)?;
|
2020-03-02 13:09:13 +01:00
|
|
|
}
|
|
|
|
|
// Make sure this is dereferenceable and all.
|
2020-05-06 13:26:24 +02:00
|
|
|
let size_and_align = try_validation!(
|
2021-02-15 00:00:00 +00:00
|
|
|
self.ecx.size_and_align_of_mplace(&place),
|
2020-05-06 13:26:24 +02:00
|
|
|
self.path,
|
2023-08-02 16:14:36 +02:00
|
|
|
Ub(InvalidMeta(msg)) => match msg {
|
2023-05-17 10:30:14 +00:00
|
|
|
InvalidMetaKind::SliceTooBig => InvalidMetaSliceTooLarge { ptr_kind },
|
|
|
|
|
InvalidMetaKind::TooBig => InvalidMetaTooLarge { ptr_kind },
|
|
|
|
|
}
|
2020-05-06 13:26:24 +02:00
|
|
|
);
|
2020-03-05 23:31:39 +01:00
|
|
|
let (size, align) = size_and_align
|
2020-03-02 13:09:13 +01:00
|
|
|
// for the purpose of validity, consider foreign types to have
|
|
|
|
|
// alignment and size determined by the layout (size will be 0,
|
|
|
|
|
// alignment should take attributes into account).
|
|
|
|
|
.unwrap_or_else(|| (place.layout.size, place.layout.align.abi));
|
2020-05-06 00:07:53 +02:00
|
|
|
// Direct call to `check_ptr_access_align` checks alignment even on CTFE machines.
|
2021-05-16 18:53:20 +02:00
|
|
|
try_validation!(
|
2023-09-26 21:08:21 +02:00
|
|
|
self.ecx.check_ptr_access(
|
2023-09-04 17:53:38 +02:00
|
|
|
place.ptr(),
|
2020-05-06 13:26:24 +02:00
|
|
|
size,
|
2021-05-06 00:16:27 +02:00
|
|
|
CheckInAllocMsg::InboundsTest, // will anyway be replaced by validity message
|
2020-05-06 13:26:24 +02:00
|
|
|
),
|
|
|
|
|
self.path,
|
2024-07-27 18:09:50 +02:00
|
|
|
Ub(DanglingIntPointer { addr: 0, .. }) => NullPtr { ptr_kind },
|
|
|
|
|
Ub(DanglingIntPointer { addr: i, .. }) => DanglingPtrNoProvenance {
|
2023-05-17 10:30:14 +00:00
|
|
|
ptr_kind,
|
|
|
|
|
// FIXME this says "null pointer" when null but we need translate
|
2024-09-26 16:48:32 +02:00
|
|
|
pointer: format!("{}", Pointer::<Option<AllocId>>::from_addr_invalid(i))
|
2023-05-17 10:30:14 +00:00
|
|
|
},
|
2023-08-02 16:14:36 +02:00
|
|
|
Ub(PointerOutOfBounds { .. }) => DanglingPtrOutOfBounds {
|
2023-05-17 10:30:14 +00:00
|
|
|
ptr_kind
|
|
|
|
|
},
|
2023-08-02 16:14:36 +02:00
|
|
|
Ub(PointerUseAfterFree(..)) => DanglingPtrUseAfterFree {
|
2023-05-17 10:30:14 +00:00
|
|
|
ptr_kind,
|
|
|
|
|
},
|
2020-05-06 13:26:24 +02:00
|
|
|
);
|
2023-09-26 21:08:21 +02:00
|
|
|
try_validation!(
|
|
|
|
|
self.ecx.check_ptr_align(
|
|
|
|
|
place.ptr(),
|
|
|
|
|
align,
|
|
|
|
|
),
|
|
|
|
|
self.path,
|
|
|
|
|
Ub(AlignmentCheckFailed(Misalignment { required, has }, _msg)) => UnalignedPtr {
|
|
|
|
|
ptr_kind,
|
|
|
|
|
required_bytes: required.bytes(),
|
|
|
|
|
found_bytes: has.bytes()
|
|
|
|
|
},
|
|
|
|
|
);
|
2024-05-10 16:31:57 +02:00
|
|
|
// Make sure this is non-null. We checked dereferenceability above, but if `size` is zero
|
|
|
|
|
// that does not imply non-null.
|
2024-05-09 12:35:11 +02:00
|
|
|
if self.ecx.scalar_may_be_null(Scalar::from_maybe_pointer(place.ptr(), self.ecx))? {
|
|
|
|
|
throw_validation_failure!(self.path, NullPtr { ptr_kind })
|
|
|
|
|
}
|
2024-08-02 10:29:52 +02:00
|
|
|
// Do not allow references to uninhabited types.
|
2024-10-27 21:34:49 -07:00
|
|
|
if place.layout.is_uninhabited() {
|
2023-05-17 10:30:14 +00:00
|
|
|
let ty = place.layout.ty;
|
|
|
|
|
throw_validation_failure!(self.path, PtrToUninhabited { ptr_kind, ty })
|
2022-05-17 17:32:36 +02:00
|
|
|
}
|
2020-03-02 13:09:13 +01:00
|
|
|
// Recursive checking
|
2022-12-23 15:15:21 +00:00
|
|
|
if let Some(ref_tracking) = self.ref_tracking.as_deref_mut() {
|
2021-05-16 18:53:20 +02:00
|
|
|
// Proceed recursively even for ZST, no reason to skip them!
|
|
|
|
|
// `!` is a ZST and we want to validate it.
|
2024-08-02 10:29:52 +02:00
|
|
|
if let Some(ctfe_mode) = self.ctfe_mode {
|
2024-02-16 09:58:53 +01:00
|
|
|
let mut skip_recursive_check = false;
|
2024-08-02 10:29:52 +02:00
|
|
|
// CTFE imposes restrictions on what references can point to.
|
|
|
|
|
if let Ok((alloc_id, _offset, _prov)) =
|
|
|
|
|
self.ecx.ptr_try_get_alloc_id(place.ptr(), 0)
|
2024-05-09 12:35:11 +02:00
|
|
|
{
|
2024-11-09 13:13:31 +01:00
|
|
|
// Everything should be already interned.
|
|
|
|
|
let Some(global_alloc) = self.ecx.tcx.try_get_global_alloc(alloc_id) else {
|
|
|
|
|
assert!(self.ecx.memory.alloc_map.get(alloc_id).is_none());
|
|
|
|
|
// We can't have *any* references to non-existing allocations in const-eval
|
|
|
|
|
// as the rest of rustc isn't happy with them... so we throw an error, even
|
|
|
|
|
// though for zero-sized references this isn't really UB.
|
|
|
|
|
// A potential future alternative would be to resurrect this as a zero-sized allocation
|
|
|
|
|
// (which codegen will then compile to an aligned dummy pointer anyway).
|
|
|
|
|
throw_validation_failure!(self.path, DanglingPtrUseAfterFree { ptr_kind });
|
|
|
|
|
};
|
|
|
|
|
let (size, _align) =
|
2024-11-15 13:53:31 +01:00
|
|
|
global_alloc.size_and_align(*self.ecx.tcx, self.ecx.typing_env());
|
2024-11-09 13:13:31 +01:00
|
|
|
|
|
|
|
|
if let GlobalAlloc::Static(did) = global_alloc {
|
2024-08-02 10:29:52 +02:00
|
|
|
let DefKind::Static { nested, .. } = self.ecx.tcx.def_kind(did) else {
|
|
|
|
|
bug!()
|
|
|
|
|
};
|
|
|
|
|
// Special handling for pointers to statics (irrespective of their type).
|
|
|
|
|
assert!(!self.ecx.tcx.is_thread_local_static(did));
|
|
|
|
|
assert!(self.ecx.tcx.is_static(did));
|
|
|
|
|
// Mode-specific checks
|
|
|
|
|
match ctfe_mode {
|
|
|
|
|
CtfeValidationMode::Static { .. }
|
|
|
|
|
| CtfeValidationMode::Promoted { .. } => {
|
|
|
|
|
// We skip recursively checking other statics. These statics must be sound by
|
|
|
|
|
// themselves, and the only way to get broken statics here is by using
|
|
|
|
|
// unsafe code.
|
|
|
|
|
// The reasons we don't check other statics is twofold. For one, in all
|
|
|
|
|
// sound cases, the static was already validated on its own, and second, we
|
|
|
|
|
// trigger cycle errors if we try to compute the value of the other static
|
|
|
|
|
// and that static refers back to us (potentially through a promoted).
|
|
|
|
|
// This could miss some UB, but that's fine.
|
|
|
|
|
// We still walk nested allocations, as they are fundamentally part of this validation run.
|
|
|
|
|
// This means we will also recurse into nested statics of *other*
|
|
|
|
|
// statics, even though we do not recurse into other statics directly.
|
|
|
|
|
// That's somewhat inconsistent but harmless.
|
|
|
|
|
skip_recursive_check = !nested;
|
|
|
|
|
}
|
|
|
|
|
CtfeValidationMode::Const { .. } => {
|
|
|
|
|
// We can't recursively validate `extern static`, so we better reject them.
|
|
|
|
|
if self.ecx.tcx.is_foreign_item(did) {
|
|
|
|
|
throw_validation_failure!(self.path, ConstRefToExtern);
|
|
|
|
|
}
|
2024-02-26 18:03:06 +00:00
|
|
|
}
|
|
|
|
|
}
|
2022-08-22 21:25:03 -04:00
|
|
|
}
|
2024-04-02 15:15:48 +00:00
|
|
|
|
2024-08-02 10:29:52 +02:00
|
|
|
// If this allocation has size zero, there is no actual mutability here.
|
|
|
|
|
if size != Size::ZERO {
|
|
|
|
|
// Determine whether this pointer expects to be pointing to something mutable.
|
|
|
|
|
let ptr_expected_mutbl = match ptr_kind {
|
|
|
|
|
PointerKind::Box => Mutability::Mut,
|
|
|
|
|
PointerKind::Ref(mutbl) => {
|
|
|
|
|
// We do not take into account interior mutability here since we cannot know if
|
|
|
|
|
// there really is an `UnsafeCell` inside `Option<UnsafeCell>` -- so we check
|
|
|
|
|
// that in the recursive descent behind this reference (controlled by
|
|
|
|
|
// `allow_immutable_unsafe_cell`).
|
|
|
|
|
mutbl
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
// Determine what it actually points to.
|
2024-11-09 13:13:31 +01:00
|
|
|
let alloc_actual_mutbl =
|
|
|
|
|
global_alloc.mutability(*self.ecx.tcx, self.ecx.param_env);
|
2024-08-02 10:29:52 +02:00
|
|
|
// Mutable pointer to immutable memory is no good.
|
2024-02-29 08:15:22 +01:00
|
|
|
if ptr_expected_mutbl == Mutability::Mut
|
2024-08-02 10:29:52 +02:00
|
|
|
&& alloc_actual_mutbl == Mutability::Not
|
2024-02-29 08:15:22 +01:00
|
|
|
{
|
2024-09-15 13:11:05 +02:00
|
|
|
// This can actually occur with transmutes.
|
2024-08-02 10:29:52 +02:00
|
|
|
throw_validation_failure!(self.path, MutableRefToImmutable);
|
|
|
|
|
}
|
|
|
|
|
// In a const, everything must be completely immutable.
|
|
|
|
|
if matches!(self.ctfe_mode, Some(CtfeValidationMode::Const { .. })) {
|
|
|
|
|
if ptr_expected_mutbl == Mutability::Mut
|
|
|
|
|
|| alloc_actual_mutbl == Mutability::Mut
|
|
|
|
|
{
|
|
|
|
|
throw_validation_failure!(self.path, ConstRefToMutable);
|
|
|
|
|
}
|
2024-02-29 08:15:22 +01:00
|
|
|
}
|
2023-12-16 16:24:25 +01:00
|
|
|
}
|
2020-03-02 13:09:13 +01:00
|
|
|
}
|
2024-02-16 09:58:53 +01:00
|
|
|
// Potentially skip recursive check.
|
|
|
|
|
if skip_recursive_check {
|
2024-09-29 11:53:23 +02:00
|
|
|
return interp_ok(());
|
2024-02-16 09:58:53 +01:00
|
|
|
}
|
2024-08-02 10:29:52 +02:00
|
|
|
} else {
|
|
|
|
|
// This is not CTFE, so it's Miri with recursive checking.
|
|
|
|
|
// FIXME: we do *not* check behind boxes, since creating a new box first creates it uninitialized
|
|
|
|
|
// and then puts the value in there, so briefly we have a box with uninit contents.
|
|
|
|
|
// FIXME: should we also skip `UnsafeCell` behind shared references? Currently that is not
|
|
|
|
|
// needed since validation reads bypass Stacked Borrows and data race checks.
|
|
|
|
|
if matches!(ptr_kind, PointerKind::Box) {
|
2024-09-29 11:53:23 +02:00
|
|
|
return interp_ok(());
|
2024-08-02 10:29:52 +02:00
|
|
|
}
|
2020-03-02 13:09:13 +01:00
|
|
|
}
|
|
|
|
|
let path = &self.path;
|
|
|
|
|
ref_tracking.track(place, || {
|
|
|
|
|
// We need to clone the path anyway, make sure it gets created
|
|
|
|
|
// with enough space for the additional `Deref`.
|
|
|
|
|
let mut new_path = Vec::with_capacity(path.len() + 1);
|
2022-03-22 18:58:23 -04:00
|
|
|
new_path.extend(path);
|
2020-03-02 13:09:13 +01:00
|
|
|
new_path.push(PathElem::Deref);
|
|
|
|
|
new_path
|
|
|
|
|
});
|
|
|
|
|
}
|
2024-09-29 11:53:23 +02:00
|
|
|
interp_ok(())
|
2020-03-02 13:09:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Check if this is a value of primitive type, and if yes check the validity of the value
|
2022-11-16 20:34:16 +00:00
|
|
|
/// at that type. Return `true` if the type is indeed primitive.
|
2023-07-24 11:44:58 +02:00
|
|
|
///
|
|
|
|
|
/// Note that not all of these have `FieldsShape::Primitive`, e.g. wide references.
|
2020-03-02 22:47:28 +01:00
|
|
|
fn try_visit_primitive(
|
|
|
|
|
&mut self,
|
2024-08-29 08:59:52 +02:00
|
|
|
value: &PlaceTy<'tcx, M::Provenance>,
|
2020-03-02 22:47:28 +01:00
|
|
|
) -> InterpResult<'tcx, bool> {
|
2018-10-02 18:07:40 +02:00
|
|
|
// Go over all the primitive types
|
2018-10-08 13:41:16 +02:00
|
|
|
let ty = value.layout.ty;
|
2020-08-03 00:49:11 +02:00
|
|
|
match ty.kind() {
|
2018-10-02 18:07:40 +02:00
|
|
|
ty::Bool => {
|
2024-08-29 08:59:52 +02:00
|
|
|
let scalar = self.read_scalar(value, ExpectedKind::Bool)?;
|
2020-05-06 00:07:53 +02:00
|
|
|
try_validation!(
|
2024-08-29 08:59:52 +02:00
|
|
|
scalar.to_bool(),
|
2020-05-06 00:07:53 +02:00
|
|
|
self.path,
|
2023-08-02 16:14:36 +02:00
|
|
|
Ub(InvalidBool(..)) => ValidationErrorKind::InvalidBool {
|
2024-08-29 08:59:52 +02:00
|
|
|
value: format!("{scalar:x}"),
|
2023-05-17 10:30:14 +00:00
|
|
|
}
|
2020-05-06 00:07:53 +02:00
|
|
|
);
|
2024-08-29 19:24:31 +02:00
|
|
|
if self.reset_provenance_and_padding {
|
2024-08-29 08:59:52 +02:00
|
|
|
self.ecx.clear_provenance(value)?;
|
2024-08-29 19:24:31 +02:00
|
|
|
self.add_data_range_place(value);
|
2024-08-29 08:59:52 +02:00
|
|
|
}
|
2024-09-29 11:53:23 +02:00
|
|
|
interp_ok(true)
|
2018-10-02 18:07:40 +02:00
|
|
|
}
|
2018-08-22 11:54:46 +01:00
|
|
|
ty::Char => {
|
2024-08-29 08:59:52 +02:00
|
|
|
let scalar = self.read_scalar(value, ExpectedKind::Char)?;
|
2020-05-06 00:07:53 +02:00
|
|
|
try_validation!(
|
2024-08-29 08:59:52 +02:00
|
|
|
scalar.to_char(),
|
2020-05-06 00:07:53 +02:00
|
|
|
self.path,
|
2023-08-02 16:14:36 +02:00
|
|
|
Ub(InvalidChar(..)) => ValidationErrorKind::InvalidChar {
|
2024-08-29 08:59:52 +02:00
|
|
|
value: format!("{scalar:x}"),
|
2023-05-17 10:30:14 +00:00
|
|
|
}
|
2020-05-06 00:07:53 +02:00
|
|
|
);
|
2024-08-29 19:24:31 +02:00
|
|
|
if self.reset_provenance_and_padding {
|
2024-08-29 08:59:52 +02:00
|
|
|
self.ecx.clear_provenance(value)?;
|
2024-08-29 19:24:31 +02:00
|
|
|
self.add_data_range_place(value);
|
2024-08-29 08:59:52 +02:00
|
|
|
}
|
2024-09-29 11:53:23 +02:00
|
|
|
interp_ok(true)
|
2018-10-02 18:07:40 +02:00
|
|
|
}
|
2018-10-03 11:38:16 +02:00
|
|
|
ty::Float(_) | ty::Int(_) | ty::Uint(_) => {
|
2018-10-16 08:37:27 +02:00
|
|
|
// NOTE: Keep this in sync with the array optimization for int/float
|
|
|
|
|
// types below!
|
2023-08-02 16:14:36 +02:00
|
|
|
self.read_scalar(
|
2022-08-01 19:05:20 -04:00
|
|
|
value,
|
|
|
|
|
if matches!(ty.kind(), ty::Float(..)) {
|
2023-05-17 10:30:14 +00:00
|
|
|
ExpectedKind::Float
|
2022-08-01 19:05:20 -04:00
|
|
|
} else {
|
2023-05-17 10:30:14 +00:00
|
|
|
ExpectedKind::Int
|
2022-08-01 19:05:20 -04:00
|
|
|
},
|
|
|
|
|
)?;
|
2024-08-29 19:24:31 +02:00
|
|
|
if self.reset_provenance_and_padding {
|
2024-08-29 08:59:52 +02:00
|
|
|
self.ecx.clear_provenance(value)?;
|
2024-08-29 19:24:31 +02:00
|
|
|
self.add_data_range_place(value);
|
2024-08-29 08:59:52 +02:00
|
|
|
}
|
2024-09-29 11:53:23 +02:00
|
|
|
interp_ok(true)
|
2018-10-03 11:38:16 +02:00
|
|
|
}
|
2018-10-19 17:11:23 +02:00
|
|
|
ty::RawPtr(..) => {
|
2024-08-29 08:59:52 +02:00
|
|
|
let place = self.deref_pointer(value, ExpectedKind::RawPtr)?;
|
2019-11-06 08:44:15 +01:00
|
|
|
if place.layout.is_unsized() {
|
2023-09-04 17:53:38 +02:00
|
|
|
self.check_wide_ptr_meta(place.meta(), place.layout)?;
|
2019-08-25 14:26:56 +02:00
|
|
|
}
|
2024-09-29 11:53:23 +02:00
|
|
|
interp_ok(true)
|
2018-10-19 17:11:23 +02:00
|
|
|
}
|
2024-02-16 09:58:53 +01:00
|
|
|
ty::Ref(_, _ty, mutbl) => {
|
|
|
|
|
self.check_safe_pointer(value, PointerKind::Ref(*mutbl))?;
|
2024-09-29 11:53:23 +02:00
|
|
|
interp_ok(true)
|
2020-03-02 13:09:13 +01:00
|
|
|
}
|
2024-08-08 17:18:20 +10:00
|
|
|
ty::FnPtr(..) => {
|
2024-08-29 08:59:52 +02:00
|
|
|
let scalar = self.read_scalar(value, ExpectedKind::FnPtr)?;
|
2022-02-24 19:38:37 -05:00
|
|
|
|
2022-02-22 18:49:12 -05:00
|
|
|
// If we check references recursively, also check that this points to a function.
|
|
|
|
|
if let Some(_) = self.ref_tracking {
|
2024-08-29 08:59:52 +02:00
|
|
|
let ptr = scalar.to_pointer(self.ecx)?;
|
2022-02-22 18:49:12 -05:00
|
|
|
let _fn = try_validation!(
|
2022-04-03 13:05:49 -04:00
|
|
|
self.ecx.get_ptr_fn(ptr),
|
2022-02-22 18:49:12 -05:00
|
|
|
self.path,
|
2024-07-27 18:09:50 +02:00
|
|
|
Ub(DanglingIntPointer{ .. } | InvalidFunctionPointer(..)) =>
|
2023-08-02 16:14:36 +02:00
|
|
|
InvalidFnPtr { value: format!("{ptr}") },
|
2022-02-22 18:49:12 -05:00
|
|
|
);
|
|
|
|
|
// FIXME: Check if the signature matches
|
2022-02-24 19:38:37 -05:00
|
|
|
} else {
|
|
|
|
|
// Otherwise (for standalone Miri), we have to still check it to be non-null.
|
2024-08-29 08:59:52 +02:00
|
|
|
if self.ecx.scalar_may_be_null(scalar)? {
|
2023-05-17 10:30:14 +00:00
|
|
|
throw_validation_failure!(self.path, NullFnPtr);
|
2022-02-24 19:38:37 -05:00
|
|
|
}
|
2022-02-22 18:49:12 -05:00
|
|
|
}
|
2024-08-29 19:24:31 +02:00
|
|
|
if self.reset_provenance_and_padding {
|
2024-08-29 08:59:52 +02:00
|
|
|
// Make sure we do not preserve partial provenance. This matches the thin
|
|
|
|
|
// pointer handling in `deref_pointer`.
|
|
|
|
|
if matches!(scalar, Scalar::Int(..)) {
|
|
|
|
|
self.ecx.clear_provenance(value)?;
|
|
|
|
|
}
|
2024-08-29 19:24:31 +02:00
|
|
|
self.add_data_range_place(value);
|
2024-08-29 08:59:52 +02:00
|
|
|
}
|
2024-09-29 11:53:23 +02:00
|
|
|
interp_ok(true)
|
2020-03-02 13:09:13 +01:00
|
|
|
}
|
2023-05-17 10:30:14 +00:00
|
|
|
ty::Never => throw_validation_failure!(self.path, NeverVal),
|
2020-03-02 13:09:13 +01:00
|
|
|
ty::Foreign(..) | ty::FnDef(..) => {
|
|
|
|
|
// Nothing to check.
|
2024-09-29 11:53:23 +02:00
|
|
|
interp_ok(true)
|
2018-10-02 18:07:40 +02:00
|
|
|
}
|
2020-10-28 10:39:21 +01:00
|
|
|
// The above should be all the primitive types. The rest is compound, we
|
2020-03-02 13:09:13 +01:00
|
|
|
// check them by visiting their fields/variants.
|
|
|
|
|
ty::Adt(..)
|
|
|
|
|
| ty::Tuple(..)
|
|
|
|
|
| ty::Array(..)
|
|
|
|
|
| ty::Slice(..)
|
|
|
|
|
| ty::Str
|
|
|
|
|
| ty::Dynamic(..)
|
|
|
|
|
| ty::Closure(..)
|
2023-02-02 13:57:36 +00:00
|
|
|
| ty::Pat(..)
|
2024-01-24 18:01:56 +00:00
|
|
|
| ty::CoroutineClosure(..)
|
2024-09-29 11:53:23 +02:00
|
|
|
| ty::Coroutine(..) => interp_ok(false),
|
2020-03-02 22:24:23 +01:00
|
|
|
// Some types only occur during typechecking, they have no layout.
|
|
|
|
|
// We should not see them here and we could not check them anyway.
|
2020-05-05 23:02:09 -05:00
|
|
|
ty::Error(_)
|
2020-03-02 13:09:13 +01:00
|
|
|
| ty::Infer(..)
|
|
|
|
|
| ty::Placeholder(..)
|
|
|
|
|
| ty::Bound(..)
|
|
|
|
|
| ty::Param(..)
|
2022-11-27 17:52:17 +00:00
|
|
|
| ty::Alias(..)
|
2023-10-19 16:06:43 +00:00
|
|
|
| ty::CoroutineWitness(..) => bug!("Encountered invalid type {:?}", ty),
|
2018-08-17 12:18:02 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-02 09:33:26 +01:00
|
|
|
fn visit_scalar(
|
|
|
|
|
&mut self,
|
2022-08-01 19:05:20 -04:00
|
|
|
scalar: Scalar<M::Provenance>,
|
2021-08-29 11:06:55 +02:00
|
|
|
scalar_layout: ScalarAbi,
|
2019-06-07 18:56:27 +02:00
|
|
|
) -> InterpResult<'tcx> {
|
2022-05-04 22:47:46 +02:00
|
|
|
let size = scalar_layout.size(self.ecx);
|
2022-03-03 12:02:12 +00:00
|
|
|
let valid_range = scalar_layout.valid_range(self.ecx);
|
2023-07-21 22:35:57 -07:00
|
|
|
let WrappingRange { start, end } = valid_range;
|
2022-05-04 22:47:46 +02:00
|
|
|
let max_value = size.unsigned_int_max();
|
2023-07-21 22:35:57 -07:00
|
|
|
assert!(end <= max_value);
|
2024-06-08 16:13:45 +02:00
|
|
|
let bits = match scalar.try_to_scalar_int() {
|
|
|
|
|
Ok(int) => int.to_bits(size),
|
2021-07-16 09:39:35 +02:00
|
|
|
Err(_) => {
|
|
|
|
|
// So this is a pointer then, and casting to an int failed.
|
|
|
|
|
// Can only happen during CTFE.
|
2023-07-21 22:35:57 -07:00
|
|
|
// We support 2 kinds of ranges here: full range, and excluding zero.
|
|
|
|
|
if start == 1 && end == max_value {
|
|
|
|
|
// Only null is the niche. So make sure the ptr is NOT null.
|
|
|
|
|
if self.ecx.scalar_may_be_null(scalar)? {
|
|
|
|
|
throw_validation_failure!(self.path, NullablePtrOutOfRange {
|
|
|
|
|
range: valid_range,
|
|
|
|
|
max_value
|
|
|
|
|
})
|
|
|
|
|
} else {
|
2024-09-29 11:53:23 +02:00
|
|
|
return interp_ok(());
|
2023-07-21 22:35:57 -07:00
|
|
|
}
|
|
|
|
|
} else if scalar_layout.is_always_valid(self.ecx) {
|
|
|
|
|
// Easy. (This is reachable if `enforce_number_validity` is set.)
|
2024-09-29 11:53:23 +02:00
|
|
|
return interp_ok(());
|
2018-10-03 11:38:16 +02:00
|
|
|
} else {
|
2023-07-21 22:35:57 -07:00
|
|
|
// Conservatively, we reject, because the pointer *could* have a bad
|
|
|
|
|
// value.
|
2023-05-17 10:30:14 +00:00
|
|
|
throw_validation_failure!(self.path, PtrOutOfRange {
|
|
|
|
|
range: valid_range,
|
|
|
|
|
max_value
|
2019-07-30 20:18:50 +05:30
|
|
|
})
|
2018-10-03 11:38:16 +02:00
|
|
|
}
|
2018-10-02 20:05:12 +02:00
|
|
|
}
|
2023-07-21 22:35:57 -07:00
|
|
|
};
|
|
|
|
|
// Now compare.
|
|
|
|
|
if valid_range.contains(bits) {
|
2024-09-29 11:53:23 +02:00
|
|
|
interp_ok(())
|
2023-07-21 22:35:57 -07:00
|
|
|
} else {
|
|
|
|
|
throw_validation_failure!(self.path, OutOfRange {
|
|
|
|
|
value: format!("{bits}"),
|
|
|
|
|
range: valid_range,
|
|
|
|
|
max_value
|
|
|
|
|
})
|
2018-10-02 16:06:50 +02:00
|
|
|
}
|
|
|
|
|
}
|
2023-12-16 16:24:25 +01:00
|
|
|
|
2024-08-29 08:59:52 +02:00
|
|
|
fn in_mutable_memory(&self, val: &PlaceTy<'tcx, M::Provenance>) -> bool {
|
2024-11-09 13:13:31 +01:00
|
|
|
debug_assert!(self.ctfe_mode.is_some());
|
2024-08-29 08:59:52 +02:00
|
|
|
if let Some(mplace) = val.as_mplace_or_local().left() {
|
2023-12-16 16:24:25 +01:00
|
|
|
if let Some(alloc_id) = mplace.ptr().provenance.and_then(|p| p.get_alloc_id()) {
|
2024-11-09 13:13:31 +01:00
|
|
|
let tcx = *self.ecx.tcx;
|
|
|
|
|
// Everything must be already interned.
|
|
|
|
|
let mutbl = tcx.global_alloc(alloc_id).mutability(tcx, self.ecx.param_env);
|
|
|
|
|
if let Some((_, alloc)) = self.ecx.memory.alloc_map.get(alloc_id) {
|
|
|
|
|
assert_eq!(alloc.mutability, mutbl);
|
|
|
|
|
}
|
|
|
|
|
mutbl.is_mut()
|
2024-08-29 08:59:52 +02:00
|
|
|
} else {
|
|
|
|
|
// No memory at all.
|
|
|
|
|
false
|
2023-12-16 16:24:25 +01:00
|
|
|
}
|
2024-08-29 08:59:52 +02:00
|
|
|
} else {
|
|
|
|
|
// A local variable -- definitely mutable.
|
|
|
|
|
true
|
2023-12-16 16:24:25 +01:00
|
|
|
}
|
|
|
|
|
}
|
2024-08-29 19:24:31 +02:00
|
|
|
|
|
|
|
|
/// Add the given pointer-length pair to the "data" range of this visit.
|
|
|
|
|
fn add_data_range(&mut self, ptr: Pointer<Option<M::Provenance>>, size: Size) {
|
|
|
|
|
if let Some(data_bytes) = self.data_bytes.as_mut() {
|
|
|
|
|
// We only have to store the offset, the rest is the same for all pointers here.
|
|
|
|
|
let (_prov, offset) = ptr.into_parts();
|
|
|
|
|
// Add this.
|
|
|
|
|
data_bytes.add_range(offset, size);
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Add the entire given place to the "data" range of this visit.
|
|
|
|
|
fn add_data_range_place(&mut self, place: &PlaceTy<'tcx, M::Provenance>) {
|
|
|
|
|
// Only sized places can be added this way.
|
2024-10-27 21:34:49 -07:00
|
|
|
debug_assert!(place.layout.is_sized());
|
2024-08-29 19:24:31 +02:00
|
|
|
if let Some(data_bytes) = self.data_bytes.as_mut() {
|
|
|
|
|
let offset = Self::data_range_offset(self.ecx, place);
|
|
|
|
|
data_bytes.add_range(offset, place.layout.size);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Convert a place into the offset it starts at, for the purpose of data_range tracking.
|
|
|
|
|
/// Must only be called if `data_bytes` is `Some(_)`.
|
|
|
|
|
fn data_range_offset(ecx: &InterpCx<'tcx, M>, place: &PlaceTy<'tcx, M::Provenance>) -> Size {
|
|
|
|
|
// The presence of `data_bytes` implies that our place is in memory.
|
|
|
|
|
let ptr = ecx
|
|
|
|
|
.place_to_op(place)
|
|
|
|
|
.expect("place must be in memory")
|
|
|
|
|
.as_mplace_or_imm()
|
|
|
|
|
.expect_left("place must be in memory")
|
|
|
|
|
.ptr();
|
|
|
|
|
let (_prov, offset) = ptr.into_parts();
|
|
|
|
|
offset
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn reset_padding(&mut self, place: &PlaceTy<'tcx, M::Provenance>) -> InterpResult<'tcx> {
|
2024-09-29 11:53:23 +02:00
|
|
|
let Some(data_bytes) = self.data_bytes.as_mut() else { return interp_ok(()) };
|
2024-08-29 19:24:31 +02:00
|
|
|
// Our value must be in memory, otherwise we would not have set up `data_bytes`.
|
|
|
|
|
let mplace = self.ecx.force_allocation(place)?;
|
|
|
|
|
// Determine starting offset and size.
|
|
|
|
|
let (_prov, start_offset) = mplace.ptr().into_parts();
|
|
|
|
|
let (size, _align) = self
|
|
|
|
|
.ecx
|
|
|
|
|
.size_and_align_of_mplace(&mplace)?
|
|
|
|
|
.unwrap_or((mplace.layout.size, mplace.layout.align.abi));
|
|
|
|
|
// If there is no padding at all, we can skip the rest: check for
|
|
|
|
|
// a single data range covering the entire value.
|
|
|
|
|
if data_bytes.0 == &[(start_offset, size)] {
|
2024-09-29 11:53:23 +02:00
|
|
|
return interp_ok(());
|
2024-08-29 19:24:31 +02:00
|
|
|
}
|
|
|
|
|
// Get a handle for the allocation. Do this only once, to avoid looking up the same
|
|
|
|
|
// allocation over and over again. (Though to be fair, iterating the value already does
|
|
|
|
|
// exactly that.)
|
|
|
|
|
let Some(mut alloc) = self.ecx.get_ptr_alloc_mut(mplace.ptr(), size)? else {
|
|
|
|
|
// A ZST, no padding to clear.
|
2024-09-29 11:53:23 +02:00
|
|
|
return interp_ok(());
|
2024-08-29 19:24:31 +02:00
|
|
|
};
|
|
|
|
|
// Add a "finalizer" data range at the end, so that the iteration below finds all gaps
|
|
|
|
|
// between ranges.
|
|
|
|
|
data_bytes.0.push((start_offset + size, Size::ZERO));
|
|
|
|
|
// Iterate, and reset gaps.
|
|
|
|
|
let mut padding_cleared_until = start_offset;
|
|
|
|
|
for &(offset, size) in data_bytes.0.iter() {
|
|
|
|
|
assert!(
|
|
|
|
|
offset >= padding_cleared_until,
|
|
|
|
|
"reset_padding on {}: previous field ended at offset {}, next field starts at {} (and has a size of {} bytes)",
|
|
|
|
|
mplace.layout.ty,
|
|
|
|
|
(padding_cleared_until - start_offset).bytes(),
|
|
|
|
|
(offset - start_offset).bytes(),
|
|
|
|
|
size.bytes(),
|
|
|
|
|
);
|
|
|
|
|
if offset > padding_cleared_until {
|
|
|
|
|
// We found padding. Adjust the range to be relative to `alloc`, and make it uninit.
|
|
|
|
|
let padding_start = padding_cleared_until - start_offset;
|
|
|
|
|
let padding_size = offset - padding_cleared_until;
|
|
|
|
|
let range = alloc_range(padding_start, padding_size);
|
|
|
|
|
trace!("reset_padding on {}: resetting padding range {range:?}", mplace.layout.ty);
|
|
|
|
|
alloc.write_uninit(range)?;
|
|
|
|
|
}
|
|
|
|
|
padding_cleared_until = offset + size;
|
|
|
|
|
}
|
|
|
|
|
assert!(padding_cleared_until == start_offset + size);
|
2024-09-29 11:53:23 +02:00
|
|
|
interp_ok(())
|
2024-08-29 19:24:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Computes the data range of this union type:
|
|
|
|
|
/// which bytes are inside a field (i.e., not padding.)
|
|
|
|
|
fn union_data_range<'e>(
|
|
|
|
|
ecx: &'e mut InterpCx<'tcx, M>,
|
|
|
|
|
layout: TyAndLayout<'tcx>,
|
|
|
|
|
) -> Cow<'e, RangeSet> {
|
|
|
|
|
assert!(layout.ty.is_union());
|
2024-10-27 21:34:49 -07:00
|
|
|
assert!(layout.is_sized(), "there are no unsized unions");
|
2024-11-15 13:53:31 +01:00
|
|
|
let layout_cx = LayoutCx::new(*ecx.tcx, ecx.typing_env());
|
2024-08-29 19:24:31 +02:00
|
|
|
return M::cached_union_data_range(ecx, layout.ty, || {
|
|
|
|
|
let mut out = RangeSet(Vec::new());
|
2024-09-09 14:42:03 +02:00
|
|
|
union_data_range_uncached(&layout_cx, layout, Size::ZERO, &mut out);
|
2024-08-29 19:24:31 +02:00
|
|
|
out
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
/// Helper for recursive traversal: add data ranges of the given type to `out`.
|
2024-09-09 14:42:03 +02:00
|
|
|
fn union_data_range_uncached<'tcx>(
|
2024-09-15 21:59:51 +02:00
|
|
|
cx: &LayoutCx<'tcx>,
|
2024-08-29 19:24:31 +02:00
|
|
|
layout: TyAndLayout<'tcx>,
|
|
|
|
|
base_offset: Size,
|
|
|
|
|
out: &mut RangeSet,
|
|
|
|
|
) {
|
2024-09-09 14:42:03 +02:00
|
|
|
// If this is a ZST, we don't contain any data. In particular, this helps us to quickly
|
|
|
|
|
// skip over huge arrays of ZST.
|
|
|
|
|
if layout.is_zst() {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-08-29 19:24:31 +02:00
|
|
|
// Just recursively add all the fields of everything to the output.
|
|
|
|
|
match &layout.fields {
|
|
|
|
|
FieldsShape::Primitive => {
|
|
|
|
|
out.add_range(base_offset, layout.size);
|
|
|
|
|
}
|
|
|
|
|
&FieldsShape::Union(fields) => {
|
2024-09-09 14:42:03 +02:00
|
|
|
// Currently, all fields start at offset 0 (relative to `base_offset`).
|
2024-08-29 19:24:31 +02:00
|
|
|
for field in 0..fields.get() {
|
|
|
|
|
let field = layout.field(cx, field);
|
2024-09-09 14:42:03 +02:00
|
|
|
union_data_range_uncached(cx, field, base_offset, out);
|
2024-08-29 19:24:31 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
&FieldsShape::Array { stride, count } => {
|
|
|
|
|
let elem = layout.field(cx, 0);
|
2024-09-09 14:42:03 +02:00
|
|
|
|
|
|
|
|
// Fast-path for large arrays of simple types that do not contain any padding.
|
2024-10-29 13:37:26 -07:00
|
|
|
if elem.backend_repr.is_scalar() {
|
2024-09-09 14:42:03 +02:00
|
|
|
out.add_range(base_offset, elem.size * count);
|
|
|
|
|
} else {
|
|
|
|
|
for idx in 0..count {
|
|
|
|
|
// This repeats the same computation for every array element... but the alternative
|
|
|
|
|
// is to allocate temporary storage for a dedicated `out` set for the array element,
|
|
|
|
|
// and replicating that N times. Is that better?
|
|
|
|
|
union_data_range_uncached(cx, elem, base_offset + idx * stride, out);
|
|
|
|
|
}
|
2024-08-29 19:24:31 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
FieldsShape::Arbitrary { offsets, .. } => {
|
|
|
|
|
for (field, &offset) in offsets.iter_enumerated() {
|
|
|
|
|
let field = layout.field(cx, field.as_usize());
|
2024-09-09 14:42:03 +02:00
|
|
|
union_data_range_uncached(cx, field, base_offset + offset, out);
|
2024-08-29 19:24:31 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Don't forget potential other variants.
|
|
|
|
|
match &layout.variants {
|
|
|
|
|
Variants::Single { .. } => {
|
|
|
|
|
// Fully handled above.
|
|
|
|
|
}
|
|
|
|
|
Variants::Multiple { variants, .. } => {
|
|
|
|
|
for variant in variants.indices() {
|
|
|
|
|
let variant = layout.for_variant(cx, variant);
|
2024-09-09 14:42:03 +02:00
|
|
|
union_data_range_uncached(cx, variant, base_offset, out);
|
2024-08-29 19:24:31 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-02-17 22:59:16 +01:00
|
|
|
}
|
|
|
|
|
|
2024-05-27 08:24:23 +02:00
|
|
|
impl<'rt, 'tcx, M: Machine<'tcx>> ValueVisitor<'tcx, M> for ValidityVisitor<'rt, 'tcx, M> {
|
2024-08-29 08:59:52 +02:00
|
|
|
type V = PlaceTy<'tcx, M::Provenance>;
|
2020-02-17 22:59:16 +01:00
|
|
|
|
|
|
|
|
#[inline(always)]
|
2024-05-27 08:24:23 +02:00
|
|
|
fn ecx(&self) -> &InterpCx<'tcx, M> {
|
2023-08-01 17:11:00 +02:00
|
|
|
self.ecx
|
2020-02-17 22:59:16 +01:00
|
|
|
}
|
|
|
|
|
|
2020-07-05 16:01:18 +02:00
|
|
|
fn read_discriminant(
|
|
|
|
|
&mut self,
|
2024-08-29 08:59:52 +02:00
|
|
|
val: &PlaceTy<'tcx, M::Provenance>,
|
2020-07-05 16:01:18 +02:00
|
|
|
) -> InterpResult<'tcx, VariantIdx> {
|
2020-07-05 13:40:27 +02:00
|
|
|
self.with_elem(PathElem::EnumTag, move |this| {
|
2024-09-29 11:53:23 +02:00
|
|
|
interp_ok(try_validation!(
|
2024-08-29 08:59:52 +02:00
|
|
|
this.ecx.read_discriminant(val),
|
2020-07-05 13:40:27 +02:00
|
|
|
this.path,
|
2023-08-02 16:14:36 +02:00
|
|
|
Ub(InvalidTag(val)) => InvalidEnumTag {
|
2023-05-17 10:30:14 +00:00
|
|
|
value: format!("{val:x}"),
|
|
|
|
|
},
|
2023-08-02 16:14:36 +02:00
|
|
|
Ub(UninhabitedEnumVariantRead(_)) => UninhabitedEnumVariant,
|
|
|
|
|
// Uninit / bad provenance are not possible since the field was already previously
|
|
|
|
|
// checked at its integer type.
|
2023-07-24 11:44:58 +02:00
|
|
|
))
|
2020-07-05 13:40:27 +02:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-17 22:59:16 +01:00
|
|
|
#[inline]
|
|
|
|
|
fn visit_field(
|
|
|
|
|
&mut self,
|
2024-08-29 08:59:52 +02:00
|
|
|
old_val: &PlaceTy<'tcx, M::Provenance>,
|
2020-02-17 22:59:16 +01:00
|
|
|
field: usize,
|
2024-08-29 08:59:52 +02:00
|
|
|
new_val: &PlaceTy<'tcx, M::Provenance>,
|
2020-02-17 22:59:16 +01:00
|
|
|
) -> InterpResult<'tcx> {
|
2024-08-29 08:59:52 +02:00
|
|
|
let elem = self.aggregate_field_path_elem(old_val.layout, field);
|
|
|
|
|
self.with_elem(elem, move |this| this.visit_value(new_val))
|
2020-02-17 22:59:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
fn visit_variant(
|
|
|
|
|
&mut self,
|
2024-08-29 08:59:52 +02:00
|
|
|
old_val: &PlaceTy<'tcx, M::Provenance>,
|
2020-02-17 22:59:16 +01:00
|
|
|
variant_id: VariantIdx,
|
2024-08-29 08:59:52 +02:00
|
|
|
new_val: &PlaceTy<'tcx, M::Provenance>,
|
2020-02-17 22:59:16 +01:00
|
|
|
) -> InterpResult<'tcx> {
|
2024-08-29 08:59:52 +02:00
|
|
|
let name = match old_val.layout.ty.kind() {
|
2022-03-05 07:28:41 +11:00
|
|
|
ty::Adt(adt, _) => PathElem::Variant(adt.variant(variant_id).name),
|
2023-10-19 16:06:43 +00:00
|
|
|
// Coroutines also have variants
|
|
|
|
|
ty::Coroutine(..) => PathElem::CoroutineState(variant_id),
|
2024-08-29 08:59:52 +02:00
|
|
|
_ => bug!("Unexpected type with variant: {:?}", old_val.layout.ty),
|
2020-02-17 22:59:16 +01:00
|
|
|
};
|
2024-08-29 08:59:52 +02:00
|
|
|
self.with_elem(name, move |this| this.visit_value(new_val))
|
2020-02-17 22:59:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline(always)]
|
2020-04-16 15:15:46 +00:00
|
|
|
fn visit_union(
|
|
|
|
|
&mut self,
|
2024-08-29 08:59:52 +02:00
|
|
|
val: &PlaceTy<'tcx, M::Provenance>,
|
2024-01-29 23:59:09 +01:00
|
|
|
_fields: NonZero<usize>,
|
2020-04-16 15:15:46 +00:00
|
|
|
) -> InterpResult<'tcx> {
|
2023-12-16 16:24:25 +01:00
|
|
|
// Special check for CTFE validation, preventing `UnsafeCell` inside unions in immutable memory.
|
|
|
|
|
if self.ctfe_mode.is_some_and(|c| !c.allow_immutable_unsafe_cell()) {
|
2024-08-29 08:59:52 +02:00
|
|
|
if !val.layout.is_zst() && !val.layout.ty.is_freeze(*self.ecx.tcx, self.ecx.param_env) {
|
|
|
|
|
if !self.in_mutable_memory(val) {
|
2023-12-16 16:24:25 +01:00
|
|
|
throw_validation_failure!(self.path, UnsafeCellInImmutable);
|
|
|
|
|
}
|
2021-10-28 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
}
|
2024-08-29 19:24:31 +02:00
|
|
|
if self.reset_provenance_and_padding
|
|
|
|
|
&& let Some(data_bytes) = self.data_bytes.as_mut()
|
|
|
|
|
{
|
|
|
|
|
let base_offset = Self::data_range_offset(self.ecx, val);
|
|
|
|
|
// Determine and add data range for this union.
|
|
|
|
|
let union_data_range = Self::union_data_range(self.ecx, val.layout);
|
|
|
|
|
for &(offset, size) in union_data_range.0.iter() {
|
|
|
|
|
data_bytes.add_range(base_offset + offset, size);
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-09-29 11:53:23 +02:00
|
|
|
interp_ok(())
|
2020-02-17 22:59:16 +01:00
|
|
|
}
|
|
|
|
|
|
2022-07-03 22:55:25 -04:00
|
|
|
#[inline]
|
2024-03-09 13:05:13 +01:00
|
|
|
fn visit_box(
|
|
|
|
|
&mut self,
|
|
|
|
|
_box_ty: Ty<'tcx>,
|
2024-08-29 08:59:52 +02:00
|
|
|
val: &PlaceTy<'tcx, M::Provenance>,
|
2024-03-09 13:05:13 +01:00
|
|
|
) -> InterpResult<'tcx> {
|
2024-08-29 08:59:52 +02:00
|
|
|
self.check_safe_pointer(val, PointerKind::Box)?;
|
2024-09-29 11:53:23 +02:00
|
|
|
interp_ok(())
|
2022-07-03 22:55:25 -04:00
|
|
|
}
|
|
|
|
|
|
2020-02-17 22:59:16 +01:00
|
|
|
#[inline]
|
2024-08-29 08:59:52 +02:00
|
|
|
fn visit_value(&mut self, val: &PlaceTy<'tcx, M::Provenance>) -> InterpResult<'tcx> {
|
|
|
|
|
trace!("visit_value: {:?}, {:?}", *val, val.layout);
|
2020-02-17 22:59:16 +01:00
|
|
|
|
2022-02-24 19:38:37 -05:00
|
|
|
// Check primitive types -- the leaves of our recursive descent.
|
2024-08-29 08:59:52 +02:00
|
|
|
// This is called even for enum discriminants (which are "fields" of their enum),
|
|
|
|
|
// so for integer-typed discriminants the provenance reset will happen here.
|
2024-05-09 12:35:11 +02:00
|
|
|
// We assume that the Scalar validity range does not restrict these values
|
|
|
|
|
// any further than `try_visit_primitive` does!
|
2024-08-29 08:59:52 +02:00
|
|
|
if self.try_visit_primitive(val)? {
|
2024-09-29 11:53:23 +02:00
|
|
|
return interp_ok(());
|
2020-02-17 22:59:16 +01:00
|
|
|
}
|
|
|
|
|
|
2020-12-20 19:34:29 +01:00
|
|
|
// Special check preventing `UnsafeCell` in the inner part of constants
|
2023-12-16 16:24:25 +01:00
|
|
|
if self.ctfe_mode.is_some_and(|c| !c.allow_immutable_unsafe_cell()) {
|
2024-08-29 08:59:52 +02:00
|
|
|
if !val.layout.is_zst()
|
|
|
|
|
&& let Some(def) = val.layout.ty.ty_adt_def()
|
2022-07-07 10:46:22 +00:00
|
|
|
&& def.is_unsafe_cell()
|
2020-10-24 20:49:17 +02:00
|
|
|
{
|
2024-08-29 08:59:52 +02:00
|
|
|
if !self.in_mutable_memory(val) {
|
2023-12-16 16:24:25 +01:00
|
|
|
throw_validation_failure!(self.path, UnsafeCellInImmutable);
|
|
|
|
|
}
|
2020-10-24 20:49:17 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-24 11:44:58 +02:00
|
|
|
// Recursively walk the value at its type. Apply optimizations for some large types.
|
2024-08-29 08:59:52 +02:00
|
|
|
match val.layout.ty.kind() {
|
2018-10-31 18:44:00 +01:00
|
|
|
ty::Str => {
|
2024-08-29 08:59:52 +02:00
|
|
|
let mplace = val.assert_mem_place(); // strings are unsized and hence never immediate
|
2020-05-12 09:46:41 +02:00
|
|
|
let len = mplace.len(self.ecx)?;
|
2019-12-27 00:38:10 +01:00
|
|
|
try_validation!(
|
2023-09-04 17:53:38 +02:00
|
|
|
self.ecx.read_bytes_ptr_strip_provenance(mplace.ptr(), Size::from_bytes(len)),
|
2020-05-06 00:07:53 +02:00
|
|
|
self.path,
|
2023-08-02 16:14:36 +02:00
|
|
|
Ub(InvalidUninitBytes(..)) => Uninit { expected: ExpectedKind::Str },
|
|
|
|
|
Unsup(ReadPointerAsInt(_)) => PointerAsInt { expected: ExpectedKind::Str }
|
2019-12-27 00:38:10 +01:00
|
|
|
);
|
2018-10-31 18:44:00 +01:00
|
|
|
}
|
2018-10-31 16:46:33 +01:00
|
|
|
ty::Array(tys, ..) | ty::Slice(tys)
|
2024-08-29 08:59:52 +02:00
|
|
|
// This optimization applies for types that can hold arbitrary non-provenance bytes (such as
|
|
|
|
|
// integer and floating point types).
|
|
|
|
|
// FIXME(wesleywiser) This logic could be extended further to arbitrary structs or
|
|
|
|
|
// tuples made up of integer/floating point types or inhabited ZSTs with no padding.
|
2020-10-21 09:26:11 +02:00
|
|
|
if matches!(tys.kind(), ty::Int(..) | ty::Uint(..) | ty::Float(..))
|
|
|
|
|
=>
|
2018-10-31 16:46:33 +01:00
|
|
|
{
|
2023-08-02 16:14:36 +02:00
|
|
|
let expected = if tys.is_integral() { ExpectedKind::Int } else { ExpectedKind::Float };
|
2019-11-02 17:46:11 +01:00
|
|
|
// Optimized handling for arrays of integer/float type.
|
|
|
|
|
|
2018-10-31 16:46:33 +01:00
|
|
|
// This is the length of the array/slice.
|
2024-08-29 08:59:52 +02:00
|
|
|
let len = val.len(self.ecx)?;
|
2018-10-31 16:46:33 +01:00
|
|
|
// This is the element type size.
|
Overhaul `TyS` and `Ty`.
Specifically, change `Ty` from this:
```
pub type Ty<'tcx> = &'tcx TyS<'tcx>;
```
to this
```
pub struct Ty<'tcx>(Interned<'tcx, TyS<'tcx>>);
```
There are two benefits to this.
- It's now a first class type, so we can define methods on it. This
means we can move a lot of methods away from `TyS`, leaving `TyS` as a
barely-used type, which is appropriate given that it's not meant to
be used directly.
- The uniqueness requirement is now explicit, via the `Interned` type.
E.g. the pointer-based `Eq` and `Hash` comes from `Interned`, rather
than via `TyS`, which wasn't obvious at all.
Much of this commit is boring churn. The interesting changes are in
these files:
- compiler/rustc_middle/src/arena.rs
- compiler/rustc_middle/src/mir/visit.rs
- compiler/rustc_middle/src/ty/context.rs
- compiler/rustc_middle/src/ty/mod.rs
Specifically:
- Most mentions of `TyS` are removed. It's very much a dumb struct now;
`Ty` has all the smarts.
- `TyS` now has `crate` visibility instead of `pub`.
- `TyS::make_for_test` is removed in favour of the static `BOOL_TY`,
which just works better with the new structure.
- The `Eq`/`Ord`/`Hash` impls are removed from `TyS`. `Interned`s impls
of `Eq`/`Hash` now suffice. `Ord` is now partly on `Interned`
(pointer-based, for the `Equal` case) and partly on `TyS`
(contents-based, for the other cases).
- There are many tedious sigil adjustments, i.e. adding or removing `*`
or `&`. They seem to be unavoidable.
2022-01-25 14:13:38 +11:00
|
|
|
let layout = self.ecx.layout_of(*tys)?;
|
2020-03-24 16:43:50 +01:00
|
|
|
// This is the size in bytes of the whole array. (This checks for overflow.)
|
|
|
|
|
let size = layout.size * len;
|
2022-07-04 08:48:05 -04:00
|
|
|
// If the size is 0, there is nothing to check.
|
2024-09-09 14:42:03 +02:00
|
|
|
// (`size` can only be 0 if `len` is 0, and empty arrays are always valid.)
|
2022-07-04 08:48:05 -04:00
|
|
|
if size == Size::ZERO {
|
2024-09-29 11:53:23 +02:00
|
|
|
return interp_ok(());
|
2022-07-04 08:48:05 -04:00
|
|
|
}
|
2024-08-29 08:59:52 +02:00
|
|
|
// Now that we definitely have a non-ZST array, we know it lives in memory -- except it may
|
|
|
|
|
// be an uninitialized local variable, those are also "immediate".
|
|
|
|
|
let mplace = match val.to_op(self.ecx)?.as_mplace_or_imm() {
|
2022-11-18 10:18:32 +01:00
|
|
|
Left(mplace) => mplace,
|
|
|
|
|
Right(imm) => match *imm {
|
2022-07-04 08:48:05 -04:00
|
|
|
Immediate::Uninit =>
|
2023-08-02 16:14:36 +02:00
|
|
|
throw_validation_failure!(self.path, Uninit { expected }),
|
2022-07-04 08:48:05 -04:00
|
|
|
Immediate::Scalar(..) | Immediate::ScalarPair(..) =>
|
|
|
|
|
bug!("arrays/slices can never have Scalar/ScalarPair layout"),
|
|
|
|
|
}
|
|
|
|
|
};
|
2018-11-12 13:26:53 +01:00
|
|
|
|
2019-12-27 14:33:22 -05:00
|
|
|
// Optimization: we just check the entire range at once.
|
2018-10-31 16:46:33 +01:00
|
|
|
// NOTE: Keep this in sync with the handling of integer and float
|
|
|
|
|
// types above, in `visit_primitive`.
|
2023-09-05 15:12:18 +02:00
|
|
|
// No need for an alignment check here, this is not an actual memory access.
|
2023-09-26 21:08:21 +02:00
|
|
|
let alloc = self.ecx.get_ptr_alloc(mplace.ptr(), size)?.expect("we already excluded size 0");
|
2021-05-16 18:53:20 +02:00
|
|
|
|
2024-10-19 08:49:13 +02:00
|
|
|
alloc.get_bytes_strip_provenance().map_err_kind(|kind| {
|
2018-10-31 16:46:33 +01:00
|
|
|
// Some error happened, try to provide a more detailed description.
|
2024-09-29 11:53:23 +02:00
|
|
|
// For some errors we might be able to provide extra information.
|
|
|
|
|
// (This custom logic does not fit the `try_validation!` macro.)
|
|
|
|
|
match kind {
|
|
|
|
|
Ub(InvalidUninitBytes(Some((_alloc_id, access)))) | Unsup(ReadPointerAsInt(Some((_alloc_id, access)))) => {
|
|
|
|
|
// Some byte was uninitialized, determine which
|
|
|
|
|
// element that byte belongs to so we can
|
|
|
|
|
// provide an index.
|
|
|
|
|
let i = usize::try_from(
|
|
|
|
|
access.bad.start.bytes() / layout.size.bytes(),
|
|
|
|
|
)
|
|
|
|
|
.unwrap();
|
|
|
|
|
self.path.push(PathElem::ArrayElem(i));
|
2020-07-05 13:40:27 +02:00
|
|
|
|
2024-09-29 11:53:23 +02:00
|
|
|
if matches!(kind, Ub(InvalidUninitBytes(_))) {
|
2024-10-19 08:49:13 +02:00
|
|
|
err_validation_failure!(self.path, Uninit { expected })
|
2024-09-29 11:53:23 +02:00
|
|
|
} else {
|
2024-10-19 08:49:13 +02:00
|
|
|
err_validation_failure!(self.path, PointerAsInt { expected })
|
2024-09-29 11:53:23 +02:00
|
|
|
}
|
2018-08-25 14:36:24 +02:00
|
|
|
}
|
2024-09-29 11:53:23 +02:00
|
|
|
|
|
|
|
|
// Propagate upwards (that will also check for unexpected errors).
|
2024-10-19 08:49:13 +02:00
|
|
|
err => err,
|
2018-08-24 15:27:05 +02:00
|
|
|
}
|
2024-09-29 11:53:23 +02:00
|
|
|
})?;
|
2024-08-29 08:59:52 +02:00
|
|
|
|
|
|
|
|
// Don't forget that these are all non-pointer types, and thus do not preserve
|
|
|
|
|
// provenance.
|
2024-08-29 19:24:31 +02:00
|
|
|
if self.reset_provenance_and_padding {
|
2024-08-29 08:59:52 +02:00
|
|
|
// We can't share this with above as above, we might be looking at read-only memory.
|
|
|
|
|
let mut alloc = self.ecx.get_ptr_alloc_mut(mplace.ptr(), size)?.expect("we already excluded size 0");
|
|
|
|
|
alloc.clear_provenance()?;
|
2024-08-29 19:24:31 +02:00
|
|
|
// Also, mark this as containing data, not padding.
|
|
|
|
|
self.add_data_range(mplace.ptr(), size);
|
2024-08-29 08:59:52 +02:00
|
|
|
}
|
2018-10-31 16:46:33 +01:00
|
|
|
}
|
2020-01-13 17:58:37 +01:00
|
|
|
// Fast path for arrays and slices of ZSTs. We only need to check a single ZST element
|
|
|
|
|
// of an array and not all of them, because there's only a single value of a specific
|
|
|
|
|
// ZST type, so either validation fails for all elements or none.
|
Overhaul `TyS` and `Ty`.
Specifically, change `Ty` from this:
```
pub type Ty<'tcx> = &'tcx TyS<'tcx>;
```
to this
```
pub struct Ty<'tcx>(Interned<'tcx, TyS<'tcx>>);
```
There are two benefits to this.
- It's now a first class type, so we can define methods on it. This
means we can move a lot of methods away from `TyS`, leaving `TyS` as a
barely-used type, which is appropriate given that it's not meant to
be used directly.
- The uniqueness requirement is now explicit, via the `Interned` type.
E.g. the pointer-based `Eq` and `Hash` comes from `Interned`, rather
than via `TyS`, which wasn't obvious at all.
Much of this commit is boring churn. The interesting changes are in
these files:
- compiler/rustc_middle/src/arena.rs
- compiler/rustc_middle/src/mir/visit.rs
- compiler/rustc_middle/src/ty/context.rs
- compiler/rustc_middle/src/ty/mod.rs
Specifically:
- Most mentions of `TyS` are removed. It's very much a dumb struct now;
`Ty` has all the smarts.
- `TyS` now has `crate` visibility instead of `pub`.
- `TyS::make_for_test` is removed in favour of the static `BOOL_TY`,
which just works better with the new structure.
- The `Eq`/`Ord`/`Hash` impls are removed from `TyS`. `Interned`s impls
of `Eq`/`Hash` now suffice. `Ord` is now partly on `Interned`
(pointer-based, for the `Equal` case) and partly on `TyS`
(contents-based, for the other cases).
- There are many tedious sigil adjustments, i.e. adding or removing `*`
or `&`. They seem to be unavoidable.
2022-01-25 14:13:38 +11:00
|
|
|
ty::Array(tys, ..) | ty::Slice(tys) if self.ecx.layout_of(*tys)?.is_zst() => {
|
2020-10-21 09:26:11 +02:00
|
|
|
// Validate just the first element (if any).
|
2024-08-29 08:59:52 +02:00
|
|
|
if val.len(self.ecx)? > 0 {
|
|
|
|
|
self.visit_field(val, 0, &self.ecx.project_index(val, 0)?)?;
|
2023-07-24 11:44:58 +02:00
|
|
|
}
|
2020-01-13 17:58:37 +01:00
|
|
|
}
|
2018-11-02 08:17:40 +01:00
|
|
|
_ => {
|
2024-04-21 11:35:02 +02:00
|
|
|
// default handler
|
|
|
|
|
try_validation!(
|
2024-08-29 08:59:52 +02:00
|
|
|
self.walk_value(val),
|
2024-04-21 11:35:02 +02:00
|
|
|
self.path,
|
|
|
|
|
// It's not great to catch errors here, since we can't give a very good path,
|
|
|
|
|
// but it's better than ICEing.
|
2024-09-22 20:00:01 -04:00
|
|
|
Ub(InvalidVTableTrait { vtable_dyn_type, expected_dyn_type }) => {
|
2024-09-26 16:48:32 +02:00
|
|
|
InvalidMetaWrongTrait { vtable_dyn_type, expected_dyn_type }
|
2024-04-21 11:35:02 +02:00
|
|
|
},
|
|
|
|
|
);
|
2023-07-24 11:44:58 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// *After* all of this, check the ABI. We need to check the ABI to handle
|
|
|
|
|
// types like `NonNull` where the `Scalar` info is more restrictive than what
|
|
|
|
|
// the fields say (`rustc_layout_scalar_valid_range_start`).
|
|
|
|
|
// But in most cases, this will just propagate what the fields say,
|
|
|
|
|
// and then we want the error to point at the field -- so, first recurse,
|
|
|
|
|
// then check ABI.
|
|
|
|
|
//
|
|
|
|
|
// FIXME: We could avoid some redundant checks here. For newtypes wrapping
|
|
|
|
|
// scalars, we do the same check on every "level" (e.g., first we check
|
|
|
|
|
// MyNewtype and then the scalar in there).
|
2024-10-29 13:37:26 -07:00
|
|
|
match val.layout.backend_repr {
|
|
|
|
|
BackendRepr::Uninhabited => {
|
2024-08-29 08:59:52 +02:00
|
|
|
let ty = val.layout.ty;
|
2023-07-24 11:44:58 +02:00
|
|
|
throw_validation_failure!(self.path, UninhabitedVal { ty });
|
|
|
|
|
}
|
2024-10-29 13:37:26 -07:00
|
|
|
BackendRepr::Scalar(scalar_layout) => {
|
2023-07-24 11:44:58 +02:00
|
|
|
if !scalar_layout.is_uninit_valid() {
|
|
|
|
|
// There is something to check here.
|
2024-08-29 08:59:52 +02:00
|
|
|
let scalar = self.read_scalar(val, ExpectedKind::InitScalar)?;
|
2023-07-24 11:44:58 +02:00
|
|
|
self.visit_scalar(scalar, scalar_layout)?;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-10-29 13:37:26 -07:00
|
|
|
BackendRepr::ScalarPair(a_layout, b_layout) => {
|
2023-07-24 11:44:58 +02:00
|
|
|
// We can only proceed if *both* scalars need to be initialized.
|
|
|
|
|
// FIXME: find a way to also check ScalarPair when one side can be uninit but
|
|
|
|
|
// the other must be init.
|
|
|
|
|
if !a_layout.is_uninit_valid() && !b_layout.is_uninit_valid() {
|
|
|
|
|
let (a, b) =
|
2024-08-29 08:59:52 +02:00
|
|
|
self.read_immediate(val, ExpectedKind::InitScalar)?.to_scalar_pair();
|
2023-07-24 11:44:58 +02:00
|
|
|
self.visit_scalar(a, a_layout)?;
|
|
|
|
|
self.visit_scalar(b, b_layout)?;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-10-29 13:37:26 -07:00
|
|
|
BackendRepr::Vector { .. } => {
|
2023-07-24 11:44:58 +02:00
|
|
|
// No checks here, we assume layout computation gets this right.
|
|
|
|
|
// (This is harder to check since Miri does not represent these as `Immediate`. We
|
|
|
|
|
// also cannot use field projections since this might be a newtype around a vector.)
|
|
|
|
|
}
|
2024-10-29 13:37:26 -07:00
|
|
|
BackendRepr::Memory { .. } => {
|
2023-07-24 11:44:58 +02:00
|
|
|
// Nothing to do.
|
2018-11-02 08:17:40 +01:00
|
|
|
}
|
|
|
|
|
}
|
2023-07-24 11:44:58 +02:00
|
|
|
|
2024-09-29 11:53:23 +02:00
|
|
|
interp_ok(())
|
2018-08-17 12:18:02 +02:00
|
|
|
}
|
2018-10-31 16:46:33 +01:00
|
|
|
}
|
2018-08-17 12:18:02 +02:00
|
|
|
|
2024-05-27 08:24:23 +02:00
|
|
|
impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
|
2020-03-05 23:31:39 +01:00
|
|
|
fn validate_operand_internal(
|
2024-08-29 08:59:52 +02:00
|
|
|
&mut self,
|
|
|
|
|
val: &PlaceTy<'tcx, M::Provenance>,
|
2018-10-31 16:46:33 +01:00
|
|
|
path: Vec<PathElem>,
|
2022-07-18 18:47:31 -04:00
|
|
|
ref_tracking: Option<&mut RefTracking<MPlaceTy<'tcx, M::Provenance>, Vec<PathElem>>>,
|
2020-10-24 20:49:17 +02:00
|
|
|
ctfe_mode: Option<CtfeValidationMode>,
|
2024-08-29 19:24:31 +02:00
|
|
|
reset_provenance_and_padding: bool,
|
2019-06-07 18:56:27 +02:00
|
|
|
) -> InterpResult<'tcx> {
|
2024-08-29 08:59:52 +02:00
|
|
|
trace!("validate_operand_internal: {:?}, {:?}", *val, val.layout.ty);
|
2018-08-17 12:18:02 +02:00
|
|
|
|
2024-08-29 08:59:52 +02:00
|
|
|
// Run the visitor.
|
2024-09-29 11:53:23 +02:00
|
|
|
self.run_for_validation(|ecx| {
|
2024-08-29 19:24:31 +02:00
|
|
|
let reset_padding = reset_provenance_and_padding && {
|
|
|
|
|
// Check if `val` is actually stored in memory. If not, padding is not even
|
|
|
|
|
// represented and we need not reset it.
|
|
|
|
|
ecx.place_to_op(val)?.as_mplace_or_imm().is_left()
|
|
|
|
|
};
|
|
|
|
|
let mut v = ValidityVisitor {
|
|
|
|
|
path,
|
|
|
|
|
ref_tracking,
|
|
|
|
|
ctfe_mode,
|
|
|
|
|
ecx,
|
|
|
|
|
reset_provenance_and_padding,
|
|
|
|
|
data_bytes: reset_padding.then_some(RangeSet(Vec::new())),
|
|
|
|
|
};
|
|
|
|
|
v.visit_value(val)?;
|
|
|
|
|
v.reset_padding(val)?;
|
2024-09-29 11:53:23 +02:00
|
|
|
interp_ok(())
|
|
|
|
|
})
|
2024-10-19 08:49:13 +02:00
|
|
|
.map_err_info(|err| {
|
2024-09-29 11:53:23 +02:00
|
|
|
if !matches!(
|
|
|
|
|
err.kind(),
|
|
|
|
|
err_ub!(ValidationError { .. })
|
2024-10-19 08:49:13 +02:00
|
|
|
| InterpErrorKind::InvalidProgram(_)
|
|
|
|
|
| InterpErrorKind::Unsupported(UnsupportedOpInfo::ExternTypeField)
|
2024-09-29 11:53:23 +02:00
|
|
|
) {
|
2024-01-06 13:48:48 +01:00
|
|
|
bug!(
|
|
|
|
|
"Unexpected error during validation: {}",
|
|
|
|
|
format_interp_error(self.tcx.dcx(), err)
|
|
|
|
|
);
|
2020-05-06 09:22:52 +02:00
|
|
|
}
|
2024-09-29 11:53:23 +02:00
|
|
|
err
|
|
|
|
|
})
|
2018-08-17 12:18:02 +02:00
|
|
|
}
|
2020-03-05 23:31:39 +01:00
|
|
|
|
|
|
|
|
/// This function checks the data at `op` to be const-valid.
|
|
|
|
|
/// `op` is assumed to cover valid memory if it is an indirect operand.
|
|
|
|
|
/// It will error if the bits at the destination do not match the ones described by the layout.
|
|
|
|
|
///
|
|
|
|
|
/// `ref_tracking` is used to record references that we encounter so that they
|
|
|
|
|
/// can be checked recursively by an outside driving loop.
|
|
|
|
|
///
|
2020-10-24 20:49:17 +02:00
|
|
|
/// `constant` controls whether this must satisfy the rules for constants:
|
|
|
|
|
/// - no pointers to statics.
|
|
|
|
|
/// - no `UnsafeCell` or non-ZST `&mut`.
|
2020-03-05 23:31:39 +01:00
|
|
|
#[inline(always)]
|
2023-10-10 07:02:45 +00:00
|
|
|
pub(crate) fn const_validate_operand(
|
2024-08-29 08:59:52 +02:00
|
|
|
&mut self,
|
|
|
|
|
val: &PlaceTy<'tcx, M::Provenance>,
|
2020-03-05 23:31:39 +01:00
|
|
|
path: Vec<PathElem>,
|
2022-07-18 18:47:31 -04:00
|
|
|
ref_tracking: &mut RefTracking<MPlaceTy<'tcx, M::Provenance>, Vec<PathElem>>,
|
2020-10-24 20:49:17 +02:00
|
|
|
ctfe_mode: CtfeValidationMode,
|
2020-03-05 23:31:39 +01:00
|
|
|
) -> InterpResult<'tcx> {
|
2024-08-29 08:59:52 +02:00
|
|
|
self.validate_operand_internal(
|
|
|
|
|
val,
|
|
|
|
|
path,
|
|
|
|
|
Some(ref_tracking),
|
|
|
|
|
Some(ctfe_mode),
|
|
|
|
|
/*reset_provenance*/ false,
|
|
|
|
|
)
|
2020-03-05 23:31:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// This function checks the data at `op` to be runtime-valid.
|
|
|
|
|
/// `op` is assumed to cover valid memory if it is an indirect operand.
|
|
|
|
|
/// It will error if the bits at the destination do not match the ones described by the layout.
|
|
|
|
|
#[inline(always)]
|
2024-08-02 10:29:52 +02:00
|
|
|
pub fn validate_operand(
|
2024-08-29 08:59:52 +02:00
|
|
|
&mut self,
|
|
|
|
|
val: &PlaceTy<'tcx, M::Provenance>,
|
2024-08-02 10:29:52 +02:00
|
|
|
recursive: bool,
|
2024-08-29 19:24:31 +02:00
|
|
|
reset_provenance_and_padding: bool,
|
2024-08-02 10:29:52 +02:00
|
|
|
) -> InterpResult<'tcx> {
|
2022-08-07 08:30:03 -04:00
|
|
|
// Note that we *could* actually be in CTFE here with `-Zextra-const-ub-checks`, but it's
|
|
|
|
|
// still correct to not use `ctfe_mode`: that mode is for validation of the final constant
|
2024-08-02 10:29:52 +02:00
|
|
|
// value, it rules out things like `UnsafeCell` in awkward places.
|
|
|
|
|
if !recursive {
|
2024-08-29 19:24:31 +02:00
|
|
|
return self.validate_operand_internal(
|
|
|
|
|
val,
|
|
|
|
|
vec![],
|
|
|
|
|
None,
|
|
|
|
|
None,
|
|
|
|
|
reset_provenance_and_padding,
|
|
|
|
|
);
|
2024-08-02 10:29:52 +02:00
|
|
|
}
|
|
|
|
|
// Do a recursive check.
|
|
|
|
|
let mut ref_tracking = RefTracking::empty();
|
2024-08-29 08:59:52 +02:00
|
|
|
self.validate_operand_internal(
|
|
|
|
|
val,
|
|
|
|
|
vec![],
|
|
|
|
|
Some(&mut ref_tracking),
|
|
|
|
|
None,
|
2024-08-29 19:24:31 +02:00
|
|
|
reset_provenance_and_padding,
|
2024-08-29 08:59:52 +02:00
|
|
|
)?;
|
2024-08-02 10:29:52 +02:00
|
|
|
while let Some((mplace, path)) = ref_tracking.todo.pop() {
|
2024-08-29 08:59:52 +02:00
|
|
|
// Things behind reference do *not* have the provenance reset.
|
|
|
|
|
self.validate_operand_internal(
|
|
|
|
|
&mplace.into(),
|
|
|
|
|
path,
|
|
|
|
|
Some(&mut ref_tracking),
|
|
|
|
|
None,
|
2024-08-29 19:24:31 +02:00
|
|
|
/*reset_provenance_and_padding*/ false,
|
2024-08-29 08:59:52 +02:00
|
|
|
)?;
|
2024-08-02 10:29:52 +02:00
|
|
|
}
|
2024-09-29 11:53:23 +02:00
|
|
|
interp_ok(())
|
2020-03-05 23:31:39 +01:00
|
|
|
}
|
2018-08-17 12:18:02 +02:00
|
|
|
}
|