2019-06-29 07:15:05 -05:00
|
|
|
//! Implements "Stacked Borrows". See <https://github.com/rust-lang/unsafe-code-guidelines/blob/master/wip/stacked-borrows.md>
|
|
|
|
//! for further information.
|
|
|
|
|
2018-10-26 04:31:20 -05:00
|
|
|
use std::cell::RefCell;
|
2019-05-27 13:04:37 -05:00
|
|
|
use std::collections::{HashMap, HashSet};
|
2018-11-15 11:15:05 -06:00
|
|
|
use std::rc::Rc;
|
2019-04-15 08:36:09 -05:00
|
|
|
use std::fmt;
|
|
|
|
use std::num::NonZeroU64;
|
2018-10-16 11:01:50 -05:00
|
|
|
|
2018-11-05 09:05:17 -06:00
|
|
|
use rustc::ty::{self, layout::Size};
|
2019-04-17 01:42:41 -05:00
|
|
|
use rustc::hir::{MutMutable, MutImmutable};
|
2018-12-12 04:11:20 -06:00
|
|
|
use rustc::mir::RetagKind;
|
2018-10-16 11:01:50 -05:00
|
|
|
|
2018-11-01 02:55:03 -05:00
|
|
|
use crate::{
|
2019-06-08 15:14:47 -05:00
|
|
|
InterpResult, InterpError, MiriEvalContext, HelpersEvalContextExt, Evaluator, MutValueVisitor,
|
2019-06-24 21:47:37 -05:00
|
|
|
MemoryKind, MiriMemoryKind, RangeMap, AllocId, Pointer, Immediate, ImmTy, PlaceTy, MPlaceTy,
|
2018-10-16 11:01:50 -05:00
|
|
|
};
|
2018-10-16 04:21:38 -05:00
|
|
|
|
2019-04-15 08:36:09 -05:00
|
|
|
pub type PtrId = NonZeroU64;
|
2019-04-17 07:57:13 -05:00
|
|
|
pub type CallId = NonZeroU64;
|
2019-06-24 16:34:38 -05:00
|
|
|
pub type AllocExtra = Stacks;
|
2018-10-16 04:21:38 -05:00
|
|
|
|
2019-04-15 08:36:09 -05:00
|
|
|
/// Tracking pointer provenance
|
2019-05-24 03:09:18 -05:00
|
|
|
#[derive(Copy, Clone, Hash, PartialEq, Eq)]
|
2019-04-15 08:36:09 -05:00
|
|
|
pub enum Tag {
|
|
|
|
Tagged(PtrId),
|
|
|
|
Untagged,
|
2018-10-16 04:21:38 -05:00
|
|
|
}
|
|
|
|
|
2019-05-24 03:09:18 -05:00
|
|
|
impl fmt::Debug for Tag {
|
2019-04-15 08:36:09 -05:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2018-10-16 11:01:50 -05:00
|
|
|
match self {
|
2019-05-24 03:09:18 -05:00
|
|
|
Tag::Tagged(id) => write!(f, "<{}>", id),
|
2019-04-15 08:36:09 -05:00
|
|
|
Tag::Untagged => write!(f, "<untagged>"),
|
2018-10-16 11:01:50 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-15 08:36:09 -05:00
|
|
|
/// Indicates which permission is granted (by this item to some pointers)
|
|
|
|
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
|
|
|
|
pub enum Permission {
|
|
|
|
/// Grants unique mutable access.
|
|
|
|
Unique,
|
|
|
|
/// Grants shared mutable access.
|
|
|
|
SharedReadWrite,
|
2019-05-19 06:59:18 -05:00
|
|
|
/// Grants shared read-only access.
|
2019-04-15 08:36:09 -05:00
|
|
|
SharedReadOnly,
|
2019-05-17 07:22:55 -05:00
|
|
|
/// Grants no access, but separates two groups of SharedReadWrite so they are not
|
|
|
|
/// all considered mutually compatible.
|
|
|
|
Disabled,
|
2018-10-30 10:46:28 -05:00
|
|
|
}
|
|
|
|
|
2019-02-15 19:29:38 -06:00
|
|
|
/// An item in the per-location borrow stack.
|
2019-05-24 03:09:18 -05:00
|
|
|
#[derive(Copy, Clone, Hash, PartialEq, Eq)]
|
2019-04-17 07:57:13 -05:00
|
|
|
pub struct Item {
|
|
|
|
/// The permission this item grants.
|
|
|
|
perm: Permission,
|
|
|
|
/// The pointers the permission is granted to.
|
|
|
|
tag: Tag,
|
|
|
|
/// An optional protector, ensuring the item cannot get popped until `CallId` is over.
|
|
|
|
protector: Option<CallId>,
|
2019-04-15 08:36:09 -05:00
|
|
|
}
|
|
|
|
|
2019-05-24 03:09:18 -05:00
|
|
|
impl fmt::Debug for Item {
|
2019-04-15 08:36:09 -05:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2019-05-24 03:09:18 -05:00
|
|
|
write!(f, "[{:?} for {:?}", self.perm, self.tag)?;
|
2019-04-17 07:57:13 -05:00
|
|
|
if let Some(call) = self.protector {
|
|
|
|
write!(f, " (call {})", call)?;
|
2019-04-15 08:36:09 -05:00
|
|
|
}
|
2019-04-17 07:57:13 -05:00
|
|
|
write!(f, "]")?;
|
|
|
|
Ok(())
|
2019-04-15 08:36:09 -05:00
|
|
|
}
|
2018-10-16 04:21:38 -05:00
|
|
|
}
|
2018-10-16 11:01:50 -05:00
|
|
|
|
2019-02-15 19:29:38 -06:00
|
|
|
/// Extra per-location state.
|
2019-01-05 08:26:16 -06:00
|
|
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
2018-11-05 09:05:17 -06:00
|
|
|
pub struct Stack {
|
2019-04-15 08:36:09 -05:00
|
|
|
/// Used *mostly* as a stack; never empty.
|
|
|
|
/// Invariants:
|
2019-04-17 07:57:13 -05:00
|
|
|
/// * Above a `SharedReadOnly` there can only be more `SharedReadOnly`.
|
2019-05-17 06:42:36 -05:00
|
|
|
/// * Except for `Untagged`, no tag occurs in the stack more than once.
|
2019-04-15 08:36:09 -05:00
|
|
|
borrows: Vec<Item>,
|
2018-11-05 09:05:17 -06:00
|
|
|
}
|
|
|
|
|
2019-04-15 08:36:09 -05:00
|
|
|
|
|
|
|
/// Extra per-allocation state.
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub struct Stacks {
|
|
|
|
// Even reading memory can have effects on the stack, so we need a `RefCell` here.
|
|
|
|
stacks: RefCell<RangeMap<Stack>>,
|
|
|
|
// Pointer to global state
|
2019-06-24 16:34:38 -05:00
|
|
|
global: MemoryExtra,
|
2018-11-05 09:05:17 -06:00
|
|
|
}
|
|
|
|
|
2019-04-15 08:36:09 -05:00
|
|
|
/// Extra global state, available to the memory access hooks.
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct GlobalState {
|
2019-05-27 13:04:37 -05:00
|
|
|
/// Next unused pointer ID (tag).
|
2019-04-15 08:36:09 -05:00
|
|
|
next_ptr_id: PtrId,
|
2019-05-27 13:04:37 -05:00
|
|
|
/// Table storing the "base" tag for each allocation.
|
|
|
|
/// The base tag is the one used for the initial pointer.
|
|
|
|
/// We need this in a separate table to handle cyclic statics.
|
|
|
|
base_ptr_ids: HashMap<AllocId, Tag>,
|
|
|
|
/// Next unused call ID (for protectors).
|
2019-04-15 08:36:09 -05:00
|
|
|
next_call_id: CallId,
|
2019-05-27 13:04:37 -05:00
|
|
|
/// Those call IDs corresponding to functions that are still running.
|
2019-04-15 08:36:09 -05:00
|
|
|
active_calls: HashSet<CallId>,
|
2018-10-19 09:07:40 -05:00
|
|
|
}
|
2019-05-27 13:04:37 -05:00
|
|
|
/// Memory extra state gives us interior mutable access to the global state.
|
2019-06-24 16:34:38 -05:00
|
|
|
pub type MemoryExtra = Rc<RefCell<GlobalState>>;
|
2018-10-19 09:07:40 -05:00
|
|
|
|
2019-02-15 19:29:38 -06:00
|
|
|
/// Indicates which kind of access is being performed.
|
2019-05-24 03:09:18 -05:00
|
|
|
#[derive(Copy, Clone, Hash, PartialEq, Eq)]
|
2018-11-21 08:25:47 -06:00
|
|
|
pub enum AccessKind {
|
|
|
|
Read,
|
2019-04-17 01:42:41 -05:00
|
|
|
Write,
|
2019-04-15 08:36:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for AccessKind {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
match self {
|
2019-05-24 03:09:18 -05:00
|
|
|
AccessKind::Read => write!(f, "read access"),
|
|
|
|
AccessKind::Write => write!(f, "write access"),
|
2019-04-15 08:36:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Indicates which kind of reference is being created.
|
2019-04-17 01:42:41 -05:00
|
|
|
/// Used by high-level `reborrow` to compute which permissions to grant to the
|
2019-04-15 08:36:09 -05:00
|
|
|
/// new pointer.
|
2019-05-24 03:09:18 -05:00
|
|
|
#[derive(Copy, Clone, Hash, PartialEq, Eq)]
|
2019-04-15 08:36:09 -05:00
|
|
|
pub enum RefKind {
|
2019-04-17 01:42:41 -05:00
|
|
|
/// `&mut` and `Box`.
|
2019-05-17 06:42:36 -05:00
|
|
|
Unique { two_phase: bool },
|
2019-04-15 08:36:09 -05:00
|
|
|
/// `&` with or without interior mutability.
|
2019-04-17 01:42:41 -05:00
|
|
|
Shared,
|
|
|
|
/// `*mut`/`*const` (raw pointers).
|
|
|
|
Raw { mutable: bool },
|
2019-04-15 08:36:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for RefKind {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
match self {
|
2019-05-17 06:42:36 -05:00
|
|
|
RefKind::Unique { two_phase: false } => write!(f, "unique"),
|
|
|
|
RefKind::Unique { two_phase: true } => write!(f, "unique (two-phase)"),
|
2019-04-17 01:42:41 -05:00
|
|
|
RefKind::Shared => write!(f, "shared"),
|
|
|
|
RefKind::Raw { mutable: true } => write!(f, "raw (mutable)"),
|
|
|
|
RefKind::Raw { mutable: false } => write!(f, "raw (constant)"),
|
2019-04-15 08:36:09 -05:00
|
|
|
}
|
|
|
|
}
|
2018-11-15 11:15:05 -06:00
|
|
|
}
|
|
|
|
|
2019-04-15 08:36:09 -05:00
|
|
|
/// Utilities for initialization and ID generation
|
|
|
|
impl Default for GlobalState {
|
2018-11-15 11:15:05 -06:00
|
|
|
fn default() -> Self {
|
2019-04-15 08:36:09 -05:00
|
|
|
GlobalState {
|
|
|
|
next_ptr_id: NonZeroU64::new(1).unwrap(),
|
2019-05-27 13:04:37 -05:00
|
|
|
base_ptr_ids: HashMap::default(),
|
2019-04-17 07:57:13 -05:00
|
|
|
next_call_id: NonZeroU64::new(1).unwrap(),
|
2018-11-15 11:15:05 -06:00
|
|
|
active_calls: HashSet::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-15 08:36:09 -05:00
|
|
|
impl GlobalState {
|
2019-05-27 13:04:37 -05:00
|
|
|
fn new_ptr(&mut self) -> PtrId {
|
2019-04-15 08:36:09 -05:00
|
|
|
let id = self.next_ptr_id;
|
|
|
|
self.next_ptr_id = NonZeroU64::new(id.get() + 1).unwrap();
|
|
|
|
id
|
|
|
|
}
|
|
|
|
|
2018-11-15 11:15:05 -06:00
|
|
|
pub fn new_call(&mut self) -> CallId {
|
2019-04-15 08:36:09 -05:00
|
|
|
let id = self.next_call_id;
|
2018-11-15 11:15:05 -06:00
|
|
|
trace!("new_call: Assigning ID {}", id);
|
|
|
|
self.active_calls.insert(id);
|
2019-04-17 07:57:13 -05:00
|
|
|
self.next_call_id = NonZeroU64::new(id.get() + 1).unwrap();
|
2018-11-15 11:15:05 -06:00
|
|
|
id
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn end_call(&mut self, id: CallId) {
|
|
|
|
assert!(self.active_calls.remove(&id));
|
|
|
|
}
|
2018-11-15 12:49:00 -06:00
|
|
|
|
|
|
|
fn is_active(&self, id: CallId) -> bool {
|
|
|
|
self.active_calls.contains(&id)
|
|
|
|
}
|
2019-05-27 13:04:37 -05:00
|
|
|
|
|
|
|
pub fn static_base_ptr(&mut self, id: AllocId) -> Tag {
|
|
|
|
self.base_ptr_ids.get(&id).copied().unwrap_or_else(|| {
|
|
|
|
let tag = Tag::Tagged(self.new_ptr());
|
|
|
|
trace!("New allocation {:?} has base tag {:?}", id, tag);
|
|
|
|
self.base_ptr_ids.insert(id, tag);
|
|
|
|
tag
|
|
|
|
})
|
|
|
|
}
|
2018-11-15 11:15:05 -06:00
|
|
|
}
|
|
|
|
|
2019-04-15 08:36:09 -05:00
|
|
|
// # Stacked Borrows Core Begin
|
2018-11-09 03:53:28 -06:00
|
|
|
|
|
|
|
/// We need to make at least the following things true:
|
|
|
|
///
|
2019-04-15 10:06:42 -05:00
|
|
|
/// U1: After creating a `Uniq`, it is at the top.
|
|
|
|
/// U2: If the top is `Uniq`, accesses must be through that `Uniq` or remove it it.
|
2019-04-15 08:36:09 -05:00
|
|
|
/// U3: If an access happens with a `Uniq`, it requires the `Uniq` to be in the stack.
|
2018-11-09 03:53:28 -06:00
|
|
|
///
|
2019-04-15 10:06:42 -05:00
|
|
|
/// F1: After creating a `&`, the parts outside `UnsafeCell` have our `SharedReadOnly` on top.
|
|
|
|
/// F2: If a write access happens, it pops the `SharedReadOnly`. This has three pieces:
|
|
|
|
/// F2a: If a write happens granted by an item below our `SharedReadOnly`, the `SharedReadOnly`
|
|
|
|
/// gets popped.
|
|
|
|
/// F2b: No `SharedReadWrite` or `Unique` will ever be added on top of our `SharedReadOnly`.
|
2019-04-15 08:36:09 -05:00
|
|
|
/// F3: If an access happens with an `&` outside `UnsafeCell`,
|
2019-04-15 10:06:42 -05:00
|
|
|
/// it requires the `SharedReadOnly` to still be in the stack.
|
2019-04-15 08:36:09 -05:00
|
|
|
|
2019-05-17 06:42:36 -05:00
|
|
|
/// Core relation on `Permission` to define which accesses are allowed
|
2019-04-15 08:36:09 -05:00
|
|
|
impl Permission {
|
|
|
|
/// This defines for a given permission, whether it permits the given kind of access.
|
|
|
|
fn grants(self, access: AccessKind) -> bool {
|
2019-05-19 06:59:18 -05:00
|
|
|
// Disabled grants nothing. Otherwise, all items grant read access, and except for SharedReadOnly they grant write access.
|
2019-05-17 07:22:55 -05:00
|
|
|
self != Permission::Disabled && (access == AccessKind::Read || self != Permission::SharedReadOnly)
|
2019-04-15 08:36:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-17 01:42:41 -05:00
|
|
|
/// Core per-location operations: access, dealloc, reborrow.
|
2019-04-15 08:36:09 -05:00
|
|
|
impl<'tcx> Stack {
|
2019-05-15 10:50:43 -05:00
|
|
|
/// Find the item granting the given kind of access to the given tag, and return where
|
2019-05-17 06:42:36 -05:00
|
|
|
/// it is on the stack.
|
|
|
|
fn find_granting(&self, access: AccessKind, tag: Tag) -> Option<usize> {
|
|
|
|
self.borrows.iter()
|
2019-04-15 08:36:09 -05:00
|
|
|
.enumerate() // we also need to know *where* in the stack
|
|
|
|
.rev() // search top-to-bottom
|
|
|
|
// Return permission of first item that grants access.
|
2019-04-15 10:06:42 -05:00
|
|
|
// We require a permission with the right tag, ensuring U3 and F3.
|
2019-04-17 09:25:38 -05:00
|
|
|
.find_map(|(idx, item)|
|
2019-05-17 06:42:36 -05:00
|
|
|
if tag == item.tag && item.perm.grants(access) {
|
|
|
|
Some(idx)
|
2019-04-17 07:57:13 -05:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2019-05-17 06:42:36 -05:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-05-19 05:40:59 -05:00
|
|
|
/// Find the first write-incompatible item above the given one --
|
2019-05-17 07:22:55 -05:00
|
|
|
/// i.e, find the height to which the stack will be truncated when writing to `granting`.
|
2019-05-17 06:42:36 -05:00
|
|
|
fn find_first_write_incompaible(&self, granting: usize) -> usize {
|
|
|
|
let perm = self.borrows[granting].perm;
|
|
|
|
match perm {
|
|
|
|
Permission::SharedReadOnly =>
|
|
|
|
bug!("Cannot use SharedReadOnly for writing"),
|
2019-05-17 07:22:55 -05:00
|
|
|
Permission::Disabled =>
|
|
|
|
bug!("Cannot use Disabled for anything"),
|
2019-05-17 06:42:36 -05:00
|
|
|
Permission::Unique =>
|
|
|
|
// On a write, everything above us is incompatible.
|
2019-05-17 12:42:35 -05:00
|
|
|
granting + 1,
|
2019-05-17 06:42:36 -05:00
|
|
|
Permission::SharedReadWrite => {
|
|
|
|
// The SharedReadWrite *just* above us are compatible, to skip those.
|
2019-05-17 12:42:35 -05:00
|
|
|
let mut idx = granting + 1;
|
2019-05-17 06:42:36 -05:00
|
|
|
while let Some(item) = self.borrows.get(idx) {
|
|
|
|
if item.perm == Permission::SharedReadWrite {
|
|
|
|
// Go on.
|
|
|
|
idx += 1;
|
|
|
|
} else {
|
|
|
|
// Found first incompatible!
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
idx
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-17 07:22:55 -05:00
|
|
|
/// Check if the given item is protected.
|
2019-06-08 15:14:47 -05:00
|
|
|
fn check_protector(item: &Item, tag: Option<Tag>, global: &GlobalState) -> InterpResult<'tcx> {
|
2019-05-17 06:42:36 -05:00
|
|
|
if let Some(call) = item.protector {
|
|
|
|
if global.is_active(call) {
|
|
|
|
if let Some(tag) = tag {
|
|
|
|
return err!(MachineError(format!(
|
2019-05-24 03:09:18 -05:00
|
|
|
"not granting access to tag {:?} because incompatible item is protected: {:?}",
|
2019-05-17 06:42:36 -05:00
|
|
|
tag, item
|
|
|
|
)));
|
|
|
|
} else {
|
|
|
|
return err!(MachineError(format!(
|
2019-05-24 03:09:18 -05:00
|
|
|
"deallocating while item is protected: {:?}", item
|
2019-05-17 06:42:36 -05:00
|
|
|
)));
|
|
|
|
}
|
2019-05-15 10:23:26 -05:00
|
|
|
}
|
|
|
|
}
|
2019-05-17 06:42:36 -05:00
|
|
|
Ok(())
|
2019-04-15 08:36:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Test if a memory `access` using pointer tagged `tag` is granted.
|
|
|
|
/// If yes, return the index of the item that granted it.
|
2019-06-24 16:34:38 -05:00
|
|
|
fn access(
|
2018-11-15 12:49:00 -06:00
|
|
|
&mut self,
|
2019-04-15 08:36:09 -05:00
|
|
|
access: AccessKind,
|
|
|
|
tag: Tag,
|
|
|
|
global: &GlobalState,
|
2019-06-08 15:14:47 -05:00
|
|
|
) -> InterpResult<'tcx> {
|
2019-05-15 10:50:43 -05:00
|
|
|
// Two main steps: Find granting item, remove incompatible items above.
|
2019-04-15 08:36:09 -05:00
|
|
|
|
|
|
|
// Step 1: Find granting item.
|
2019-05-17 06:42:36 -05:00
|
|
|
let granting_idx = self.find_granting(access, tag)
|
2019-04-15 08:36:09 -05:00
|
|
|
.ok_or_else(|| InterpError::MachineError(format!(
|
2019-05-24 03:09:18 -05:00
|
|
|
"no item granting {} to tag {:?} found in borrow stack",
|
2019-04-17 09:25:38 -05:00
|
|
|
access, tag,
|
2019-04-15 08:36:09 -05:00
|
|
|
)))?;
|
2019-04-16 08:26:21 -05:00
|
|
|
|
2019-05-15 10:50:43 -05:00
|
|
|
// Step 2: Remove incompatible items above them. Make sure we do not remove protected
|
|
|
|
// items. Behavior differs for reads and writes.
|
2019-05-17 06:42:36 -05:00
|
|
|
if access == AccessKind::Write {
|
|
|
|
// Remove everything above the write-compatible items, like a proper stack. This makes sure read-only and unique
|
|
|
|
// pointers become invalid on write accesses (ensures F2a, and ensures U2 for write accesses).
|
|
|
|
let first_incompatible_idx = self.find_first_write_incompaible(granting_idx);
|
2019-05-19 05:40:59 -05:00
|
|
|
for item in self.borrows.drain(first_incompatible_idx..).rev() {
|
2019-05-24 03:09:18 -05:00
|
|
|
trace!("access: popping item {:?}", item);
|
2019-05-17 07:22:55 -05:00
|
|
|
Stack::check_protector(&item, Some(tag), global)?;
|
2019-05-17 06:42:36 -05:00
|
|
|
}
|
|
|
|
} else {
|
2019-05-17 07:22:55 -05:00
|
|
|
// On a read, *disable* all `Unique` above the granting item. This ensures U2 for read accesses.
|
|
|
|
// The reason this is not following the stack discipline (by removing the first Unique and
|
|
|
|
// everything on top of it) is that in `let raw = &mut *x as *mut _; let _val = *x;`, the second statement
|
2019-05-17 06:42:36 -05:00
|
|
|
// would pop the `Unique` from the reborrow of the first statement, and subsequently also pop the
|
|
|
|
// `SharedReadWrite` for `raw`.
|
|
|
|
// This pattern occurs a lot in the standard library: create a raw pointer, then also create a shared
|
|
|
|
// reference and use that.
|
2019-05-17 07:22:55 -05:00
|
|
|
// We *disable* instead of removing `Unique` to avoid "connecting" two neighbouring blocks of SRWs.
|
2019-05-17 06:42:36 -05:00
|
|
|
for idx in (granting_idx+1 .. self.borrows.len()).rev() {
|
2019-05-17 07:22:55 -05:00
|
|
|
let item = &mut self.borrows[idx];
|
|
|
|
if item.perm == Permission::Unique {
|
2019-05-24 03:09:18 -05:00
|
|
|
trace!("access: disabling item {:?}", item);
|
2019-05-17 07:22:55 -05:00
|
|
|
Stack::check_protector(item, Some(tag), global)?;
|
|
|
|
item.perm = Permission::Disabled;
|
2018-11-09 03:53:28 -06:00
|
|
|
}
|
|
|
|
}
|
2018-10-23 08:59:50 -05:00
|
|
|
}
|
2019-04-15 08:36:09 -05:00
|
|
|
|
2019-04-17 01:42:41 -05:00
|
|
|
// Done.
|
2019-05-15 10:23:26 -05:00
|
|
|
Ok(())
|
2019-04-17 01:42:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Deallocate a location: Like a write access, but also there must be no
|
2019-05-17 06:42:36 -05:00
|
|
|
/// active protectors at all because we will remove all items.
|
2019-06-24 16:34:38 -05:00
|
|
|
fn dealloc(
|
2019-04-17 01:42:41 -05:00
|
|
|
&mut self,
|
|
|
|
tag: Tag,
|
|
|
|
global: &GlobalState,
|
2019-06-08 15:14:47 -05:00
|
|
|
) -> InterpResult<'tcx> {
|
2019-04-17 01:42:41 -05:00
|
|
|
// Step 1: Find granting item.
|
2019-05-17 06:42:36 -05:00
|
|
|
self.find_granting(AccessKind::Write, tag)
|
2019-04-17 01:42:41 -05:00
|
|
|
.ok_or_else(|| InterpError::MachineError(format!(
|
2019-05-24 03:09:18 -05:00
|
|
|
"no item granting write access for deallocation to tag {:?} found in borrow stack",
|
2019-04-17 09:25:38 -05:00
|
|
|
tag,
|
2019-04-17 01:42:41 -05:00
|
|
|
)))?;
|
|
|
|
|
2019-05-17 06:42:36 -05:00
|
|
|
// Step 2: Remove all items. Also checks for protectors.
|
2019-05-19 05:40:59 -05:00
|
|
|
for item in self.borrows.drain(..).rev() {
|
2019-05-17 07:22:55 -05:00
|
|
|
Stack::check_protector(&item, None, global)?;
|
2018-11-13 10:05:47 -06:00
|
|
|
}
|
2018-11-22 09:26:06 -06:00
|
|
|
|
2019-04-17 01:42:41 -05:00
|
|
|
Ok(())
|
2019-04-15 08:36:09 -05:00
|
|
|
}
|
|
|
|
|
2019-04-15 10:06:42 -05:00
|
|
|
/// Derived a new pointer from one with the given tag.
|
2019-05-11 05:07:25 -05:00
|
|
|
/// `weak` controls whether this operation is weak or strong: weak granting does not act as
|
2019-05-11 04:03:53 -05:00
|
|
|
/// an access, and they add the new item directly on top of the one it is derived
|
2019-04-17 07:57:13 -05:00
|
|
|
/// from instead of all the way at the top of the stack.
|
2019-05-11 04:03:53 -05:00
|
|
|
fn grant(
|
2019-04-15 08:36:09 -05:00
|
|
|
&mut self,
|
|
|
|
derived_from: Tag,
|
2019-04-17 07:57:13 -05:00
|
|
|
new: Item,
|
2019-04-15 08:36:09 -05:00
|
|
|
global: &GlobalState,
|
2019-06-08 15:14:47 -05:00
|
|
|
) -> InterpResult<'tcx> {
|
2019-04-17 01:42:41 -05:00
|
|
|
// Figure out which access `perm` corresponds to.
|
2019-04-17 07:57:13 -05:00
|
|
|
let access = if new.perm.grants(AccessKind::Write) {
|
2019-04-17 09:25:38 -05:00
|
|
|
AccessKind::Write
|
|
|
|
} else {
|
|
|
|
AccessKind::Read
|
|
|
|
};
|
2019-04-17 01:42:41 -05:00
|
|
|
// Now we figure out which item grants our parent (`derived_from`) this kind of access.
|
|
|
|
// We use that to determine where to put the new item.
|
2019-05-17 06:42:36 -05:00
|
|
|
let granting_idx = self.find_granting(access, derived_from)
|
2019-04-15 08:36:09 -05:00
|
|
|
.ok_or_else(|| InterpError::MachineError(format!(
|
2019-05-24 03:09:18 -05:00
|
|
|
"trying to reborrow for {:?}, but parent tag {:?} does not have an appropriate item in the borrow stack", new.perm, derived_from,
|
2019-04-15 08:36:09 -05:00
|
|
|
)))?;
|
|
|
|
|
2019-04-17 07:57:13 -05:00
|
|
|
// Compute where to put the new item.
|
2019-05-17 06:42:36 -05:00
|
|
|
// Either way, we ensure that we insert the new item in a way such that between
|
2019-04-15 10:06:42 -05:00
|
|
|
// `derived_from` and the new one, there are only items *compatible with* `derived_from`.
|
2019-05-17 06:42:36 -05:00
|
|
|
let new_idx = if new.perm == Permission::SharedReadWrite {
|
|
|
|
assert!(access == AccessKind::Write, "this case only makes sense for stack-like accesses");
|
|
|
|
// SharedReadWrite can coexist with "existing loans", meaning they don't act like a write
|
|
|
|
// access. Instead of popping the stack, we insert the item at the place the stack would
|
|
|
|
// be popped to (i.e., we insert it above all the write-compatible items).
|
|
|
|
// This ensures F2b by adding the new item below any potentially existing `SharedReadOnly`.
|
|
|
|
self.find_first_write_incompaible(granting_idx)
|
2019-04-15 08:36:09 -05:00
|
|
|
} else {
|
|
|
|
// A "safe" reborrow for a pointer that actually expects some aliasing guarantees.
|
2019-05-17 06:42:36 -05:00
|
|
|
// Here, creating a reference actually counts as an access.
|
2019-04-15 10:06:42 -05:00
|
|
|
// This ensures F2b for `Unique`, by removing offending `SharedReadOnly`.
|
2019-05-15 10:23:26 -05:00
|
|
|
self.access(access, derived_from, global)?;
|
2019-04-15 08:36:09 -05:00
|
|
|
|
|
|
|
// We insert "as far up as possible": We know only compatible items are remaining
|
|
|
|
// on top of `derived_from`, and we want the new item at the top so that we
|
|
|
|
// get the strongest possible guarantees.
|
2019-04-15 10:06:42 -05:00
|
|
|
// This ensures U1 and F1.
|
2019-04-17 07:57:13 -05:00
|
|
|
self.borrows.len()
|
|
|
|
};
|
2019-04-15 10:06:42 -05:00
|
|
|
|
2019-04-17 07:57:13 -05:00
|
|
|
// Put the new item there. As an optimization, deduplicate if it is equal to one of its new neighbors.
|
|
|
|
if self.borrows[new_idx-1] == new || self.borrows.get(new_idx) == Some(&new) {
|
|
|
|
// Optimization applies, done.
|
2019-05-24 03:09:18 -05:00
|
|
|
trace!("reborrow: avoiding adding redundant item {:?}", new);
|
2019-04-17 07:57:13 -05:00
|
|
|
} else {
|
2019-05-24 03:09:18 -05:00
|
|
|
trace!("reborrow: adding item {:?}", new);
|
2019-04-17 07:57:13 -05:00
|
|
|
self.borrows.insert(new_idx, new);
|
2019-04-15 08:36:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2018-10-16 11:01:50 -05:00
|
|
|
}
|
2019-04-17 01:42:41 -05:00
|
|
|
// # Stacked Borrows Core End
|
2018-10-16 11:01:50 -05:00
|
|
|
|
2019-04-17 07:23:21 -05:00
|
|
|
/// Map per-stack operations to higher-level per-location-range operations.
|
2018-10-17 08:15:53 -05:00
|
|
|
impl<'tcx> Stacks {
|
2019-04-15 08:36:09 -05:00
|
|
|
/// Creates new stack with initial tag.
|
2019-05-27 13:04:37 -05:00
|
|
|
fn new(
|
2018-10-17 08:15:53 -05:00
|
|
|
size: Size,
|
2019-05-27 13:04:37 -05:00
|
|
|
perm: Permission,
|
2019-04-15 08:36:09 -05:00
|
|
|
tag: Tag,
|
2019-06-24 16:34:38 -05:00
|
|
|
extra: MemoryExtra,
|
2019-04-15 08:36:09 -05:00
|
|
|
) -> Self {
|
2019-05-27 13:04:37 -05:00
|
|
|
let item = Item { perm, tag, protector: None };
|
2019-04-15 08:36:09 -05:00
|
|
|
let stack = Stack {
|
|
|
|
borrows: vec![item],
|
|
|
|
};
|
2019-06-20 14:21:47 -05:00
|
|
|
|
2019-04-15 08:36:09 -05:00
|
|
|
Stacks {
|
|
|
|
stacks: RefCell::new(RangeMap::new(size, stack)),
|
2019-06-20 14:21:47 -05:00
|
|
|
global: extra,
|
2018-10-17 08:15:53 -05:00
|
|
|
}
|
2018-11-05 09:05:17 -06:00
|
|
|
}
|
2018-10-17 08:15:53 -05:00
|
|
|
|
2019-04-17 07:23:21 -05:00
|
|
|
/// Call `f` on every stack in the range.
|
2019-06-24 16:34:38 -05:00
|
|
|
fn for_each(
|
2019-04-17 01:42:41 -05:00
|
|
|
&self,
|
|
|
|
ptr: Pointer<Tag>,
|
|
|
|
size: Size,
|
2019-06-08 15:14:47 -05:00
|
|
|
f: impl Fn(&mut Stack, &GlobalState) -> InterpResult<'tcx>,
|
|
|
|
) -> InterpResult<'tcx> {
|
2019-04-15 08:36:09 -05:00
|
|
|
let global = self.global.borrow();
|
2018-11-05 09:05:17 -06:00
|
|
|
let mut stacks = self.stacks.borrow_mut();
|
|
|
|
for stack in stacks.iter_mut(ptr.offset, size) {
|
2019-04-17 07:57:13 -05:00
|
|
|
f(stack, &*global)?;
|
2018-11-05 09:05:17 -06:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-17 01:42:41 -05:00
|
|
|
/// Glue code to connect with Miri Machine Hooks
|
2019-04-15 08:36:09 -05:00
|
|
|
impl Stacks {
|
|
|
|
pub fn new_allocation(
|
2019-05-27 13:04:37 -05:00
|
|
|
id: AllocId,
|
2019-04-15 08:36:09 -05:00
|
|
|
size: Size,
|
2019-06-24 16:34:38 -05:00
|
|
|
extra: MemoryExtra,
|
2019-04-15 08:36:09 -05:00
|
|
|
kind: MemoryKind<MiriMemoryKind>,
|
|
|
|
) -> (Self, Tag) {
|
2019-05-27 13:04:37 -05:00
|
|
|
let (tag, perm) = match kind {
|
|
|
|
MemoryKind::Stack =>
|
|
|
|
// New unique borrow. This tag is not accessible by the program,
|
2019-04-15 08:36:09 -05:00
|
|
|
// so it will only ever be used when using the local directly (i.e.,
|
2019-05-27 13:04:37 -05:00
|
|
|
// not through a pointer). That is, whenever we directly write to a local, this will pop
|
2019-04-15 08:36:09 -05:00
|
|
|
// everything else off the stack, invalidating all previous pointers,
|
2019-05-27 13:04:37 -05:00
|
|
|
// and in particular, *all* raw pointers.
|
|
|
|
(Tag::Tagged(extra.borrow_mut().new_ptr()), Permission::Unique),
|
|
|
|
MemoryKind::Machine(MiriMemoryKind::Static) =>
|
|
|
|
(extra.borrow_mut().static_base_ptr(id), Permission::SharedReadWrite),
|
|
|
|
_ =>
|
|
|
|
(Tag::Untagged, Permission::SharedReadWrite),
|
2018-11-15 11:15:05 -06:00
|
|
|
};
|
2019-05-27 13:04:37 -05:00
|
|
|
let stack = Stacks::new(size, perm, tag, extra);
|
2019-04-15 08:36:09 -05:00
|
|
|
(stack, tag)
|
2018-11-15 06:29:55 -06:00
|
|
|
}
|
2019-06-24 16:34:38 -05:00
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn memory_read<'tcx>(
|
|
|
|
&self,
|
|
|
|
ptr: Pointer<Tag>,
|
|
|
|
size: Size,
|
|
|
|
) -> InterpResult<'tcx> {
|
|
|
|
trace!("read access with tag {:?}: {:?}, size {}", ptr.tag, ptr.erase_tag(), size.bytes());
|
|
|
|
self.for_each(ptr, size, |stack, global| {
|
|
|
|
stack.access(AccessKind::Read, ptr.tag, global)?;
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn memory_written<'tcx>(
|
|
|
|
&mut self,
|
|
|
|
ptr: Pointer<Tag>,
|
|
|
|
size: Size,
|
|
|
|
) -> InterpResult<'tcx> {
|
|
|
|
trace!("write access with tag {:?}: {:?}, size {}", ptr.tag, ptr.erase_tag(), size.bytes());
|
|
|
|
self.for_each(ptr, size, |stack, global| {
|
|
|
|
stack.access(AccessKind::Write, ptr.tag, global)?;
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn memory_deallocated<'tcx>(
|
|
|
|
&mut self,
|
|
|
|
ptr: Pointer<Tag>,
|
|
|
|
size: Size,
|
|
|
|
) -> InterpResult<'tcx> {
|
|
|
|
trace!("deallocation with tag {:?}: {:?}, size {}", ptr.tag, ptr.erase_tag(), size.bytes());
|
|
|
|
self.for_each(ptr, size, |stack, global| {
|
|
|
|
stack.dealloc(ptr.tag, global)
|
|
|
|
})
|
|
|
|
}
|
2019-04-15 08:36:09 -05:00
|
|
|
}
|
2018-11-15 06:29:55 -06:00
|
|
|
|
2019-04-17 07:57:13 -05:00
|
|
|
/// Retagging/reborrowing. There is some policy in here, such as which permissions
|
2019-05-17 06:42:36 -05:00
|
|
|
/// to grant for which references, and when to add protectors.
|
2019-06-13 01:52:04 -05:00
|
|
|
impl<'mir, 'tcx> EvalContextPrivExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
|
|
|
|
trait EvalContextPrivExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
|
2018-12-11 07:18:51 -06:00
|
|
|
fn reborrow(
|
|
|
|
&mut self,
|
2019-04-15 08:36:09 -05:00
|
|
|
place: MPlaceTy<'tcx, Tag>,
|
2018-12-11 07:18:51 -06:00
|
|
|
size: Size,
|
2019-04-17 01:42:41 -05:00
|
|
|
kind: RefKind,
|
2019-04-15 08:36:09 -05:00
|
|
|
new_tag: Tag,
|
2019-04-17 07:57:13 -05:00
|
|
|
protect: bool,
|
2019-06-08 15:14:47 -05:00
|
|
|
) -> InterpResult<'tcx> {
|
2018-12-11 07:18:51 -06:00
|
|
|
let this = self.eval_context_mut();
|
2019-04-17 07:57:13 -05:00
|
|
|
let protector = if protect { Some(this.frame().extra) } else { None };
|
2019-06-23 10:26:12 -05:00
|
|
|
let ptr = this.memory().check_ptr_access(place.ptr, size, place.align)
|
|
|
|
.expect("validity checks should have excluded dangling/unaligned pointer")
|
|
|
|
.expect("we shouldn't get here for ZST");
|
2019-05-24 03:09:18 -05:00
|
|
|
trace!("reborrow: {} reference {:?} derived from {:?} (pointee {}): {:?}, size {}",
|
2019-05-24 06:09:29 -05:00
|
|
|
kind, new_tag, ptr.tag, place.layout.ty, ptr.erase_tag(), size.bytes());
|
2018-12-11 07:18:51 -06:00
|
|
|
|
2019-02-15 19:29:38 -06:00
|
|
|
// Get the allocation. It might not be mutable, so we cannot use `get_mut`.
|
2018-12-11 07:18:51 -06:00
|
|
|
let alloc = this.memory().get(ptr.alloc_id)?;
|
2019-07-03 03:19:55 -05:00
|
|
|
let stacked_borrows = alloc.extra.stacked_borrows.as_ref().expect("we should have Stacked Borrows data");
|
2018-12-11 07:18:51 -06:00
|
|
|
// Update the stacks.
|
2019-04-17 07:57:13 -05:00
|
|
|
// Make sure that raw pointers and mutable shared references are reborrowed "weak":
|
|
|
|
// There could be existing unique pointers reborrowed from them that should remain valid!
|
2019-04-17 01:42:41 -05:00
|
|
|
let perm = match kind {
|
2019-05-17 06:42:36 -05:00
|
|
|
RefKind::Unique { two_phase: false } => Permission::Unique,
|
|
|
|
RefKind::Unique { two_phase: true } => Permission::SharedReadWrite,
|
2019-04-17 01:42:41 -05:00
|
|
|
RefKind::Raw { mutable: true } => Permission::SharedReadWrite,
|
|
|
|
RefKind::Shared | RefKind::Raw { mutable: false } => {
|
|
|
|
// Shared references and *const are a whole different kind of game, the
|
|
|
|
// permission is not uniform across the entire range!
|
|
|
|
// We need a frozen-sensitive reborrow.
|
|
|
|
return this.visit_freeze_sensitive(place, size, |cur_ptr, size, frozen| {
|
|
|
|
// We are only ever `SharedReadOnly` inside the frozen bits.
|
|
|
|
let perm = if frozen { Permission::SharedReadOnly } else { Permission::SharedReadWrite };
|
2019-04-17 07:57:13 -05:00
|
|
|
let item = Item { perm, tag: new_tag, protector };
|
2019-07-03 03:19:55 -05:00
|
|
|
stacked_borrows.for_each(cur_ptr, size, |stack, global| {
|
2019-05-17 06:42:36 -05:00
|
|
|
stack.grant(cur_ptr.tag, item, global)
|
2019-04-17 07:23:21 -05:00
|
|
|
})
|
2019-04-17 01:42:41 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
2019-04-17 07:57:13 -05:00
|
|
|
let item = Item { perm, tag: new_tag, protector };
|
2019-07-03 03:19:55 -05:00
|
|
|
stacked_borrows.for_each(ptr, size, |stack, global| {
|
2019-05-17 06:42:36 -05:00
|
|
|
stack.grant(ptr.tag, item, global)
|
2019-04-17 07:23:21 -05:00
|
|
|
})
|
2018-12-11 07:18:51 -06:00
|
|
|
}
|
|
|
|
|
2019-02-15 19:29:38 -06:00
|
|
|
/// Retags an indidual pointer, returning the retagged version.
|
2018-12-12 04:11:20 -06:00
|
|
|
/// `mutbl` can be `None` to make this a raw pointer.
|
2018-12-11 07:18:51 -06:00
|
|
|
fn retag_reference(
|
|
|
|
&mut self,
|
2019-04-15 08:36:09 -05:00
|
|
|
val: ImmTy<'tcx, Tag>,
|
2019-04-17 01:42:41 -05:00
|
|
|
kind: RefKind,
|
2019-04-17 07:57:13 -05:00
|
|
|
protect: bool,
|
2019-06-08 15:14:47 -05:00
|
|
|
) -> InterpResult<'tcx, Immediate<Tag>> {
|
2018-12-11 07:18:51 -06:00
|
|
|
let this = self.eval_context_mut();
|
|
|
|
// We want a place for where the ptr *points to*, so we get one.
|
|
|
|
let place = this.ref_to_mplace(val)?;
|
|
|
|
let size = this.size_and_align_of_mplace(place)?
|
|
|
|
.map(|(size, _)| size)
|
|
|
|
.unwrap_or_else(|| place.layout.size);
|
|
|
|
if size == Size::ZERO {
|
|
|
|
// Nothing to do for ZSTs.
|
|
|
|
return Ok(*val);
|
|
|
|
}
|
2019-07-06 07:08:37 -05:00
|
|
|
let place = this.force_mplace_ptr(place)?;
|
2018-12-11 07:18:51 -06:00
|
|
|
|
|
|
|
// Compute new borrow.
|
2019-04-17 01:42:41 -05:00
|
|
|
let new_tag = match kind {
|
|
|
|
RefKind::Raw { .. } => Tag::Untagged,
|
2019-06-24 16:34:38 -05:00
|
|
|
_ => Tag::Tagged(this.memory().extra.stacked_borrows.borrow_mut().new_ptr()),
|
2018-12-11 07:18:51 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
// Reborrow.
|
2019-05-17 06:42:36 -05:00
|
|
|
this.reborrow(place, size, kind, new_tag, protect)?;
|
2019-04-15 08:36:09 -05:00
|
|
|
let new_place = place.replace_tag(new_tag);
|
2018-12-11 07:18:51 -06:00
|
|
|
|
2019-02-15 19:29:38 -06:00
|
|
|
// Return new pointer.
|
2018-12-11 07:18:51 -06:00
|
|
|
Ok(new_place.to_ref())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-13 01:52:04 -05:00
|
|
|
impl<'mir, 'tcx> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
|
|
|
|
pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
|
2018-10-24 10:17:44 -05:00
|
|
|
fn retag(
|
|
|
|
&mut self,
|
2018-12-12 04:11:20 -06:00
|
|
|
kind: RetagKind,
|
2019-04-15 08:36:09 -05:00
|
|
|
place: PlaceTy<'tcx, Tag>
|
2019-06-08 15:14:47 -05:00
|
|
|
) -> InterpResult<'tcx> {
|
2018-12-11 07:16:58 -06:00
|
|
|
let this = self.eval_context_mut();
|
2019-04-17 07:57:13 -05:00
|
|
|
// Determine mutability and whether to add a protector.
|
2018-11-21 08:21:41 -06:00
|
|
|
// Cannot use `builtin_deref` because that reports *immutable* for `Box`,
|
|
|
|
// making it useless.
|
2019-04-17 01:42:41 -05:00
|
|
|
fn qualify(ty: ty::Ty<'_>, kind: RetagKind) -> Option<(RefKind, bool)> {
|
2018-11-21 08:21:41 -06:00
|
|
|
match ty.sty {
|
2019-02-15 19:29:38 -06:00
|
|
|
// References are simple.
|
2019-04-17 01:42:41 -05:00
|
|
|
ty::Ref(_, _, MutMutable) =>
|
2019-05-17 06:42:36 -05:00
|
|
|
Some((RefKind::Unique { two_phase: kind == RetagKind::TwoPhase}, kind == RetagKind::FnEntry)),
|
2019-04-17 01:42:41 -05:00
|
|
|
ty::Ref(_, _, MutImmutable) =>
|
|
|
|
Some((RefKind::Shared, kind == RetagKind::FnEntry)),
|
2019-02-15 19:29:38 -06:00
|
|
|
// Raw pointers need to be enabled.
|
2019-04-17 01:42:41 -05:00
|
|
|
ty::RawPtr(tym) if kind == RetagKind::Raw =>
|
|
|
|
Some((RefKind::Raw { mutable: tym.mutbl == MutMutable }, false)),
|
2019-04-17 07:57:13 -05:00
|
|
|
// Boxes do not get a protector: protectors reflect that references outlive the call
|
2019-06-24 16:34:38 -05:00
|
|
|
// they were passed in to; that's just not the case for boxes.
|
2019-05-17 06:42:36 -05:00
|
|
|
ty::Adt(..) if ty.is_box() => Some((RefKind::Unique { two_phase: false }, false)),
|
2018-11-21 08:21:41 -06:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-15 19:29:38 -06:00
|
|
|
// We need a visitor to visit all references. However, that requires
|
2018-11-13 05:48:20 -06:00
|
|
|
// a `MemPlace`, so we have a fast path for reference types that
|
|
|
|
// avoids allocating.
|
2019-04-17 07:57:13 -05:00
|
|
|
if let Some((mutbl, protector)) = qualify(place.layout.ty, kind) {
|
2019-02-15 19:29:38 -06:00
|
|
|
// Fast path.
|
2018-12-11 07:16:58 -06:00
|
|
|
let val = this.read_immediate(this.place_to_op(place)?)?;
|
2019-05-17 06:42:36 -05:00
|
|
|
let val = this.retag_reference(val, mutbl, protector)?;
|
2018-12-11 07:16:58 -06:00
|
|
|
this.write_immediate(val, place)?;
|
2018-11-17 05:35:43 -06:00
|
|
|
return Ok(());
|
|
|
|
}
|
2018-12-11 07:16:58 -06:00
|
|
|
let place = this.force_allocation(place)?;
|
2018-11-13 05:48:20 -06:00
|
|
|
|
2018-12-12 04:11:20 -06:00
|
|
|
let mut visitor = RetagVisitor { ecx: this, kind };
|
2018-11-13 05:48:20 -06:00
|
|
|
visitor.visit_value(place)?;
|
|
|
|
|
2019-02-15 19:29:38 -06:00
|
|
|
// The actual visitor.
|
2019-06-13 01:52:04 -05:00
|
|
|
struct RetagVisitor<'ecx, 'mir, 'tcx> {
|
|
|
|
ecx: &'ecx mut MiriEvalContext<'mir, 'tcx>,
|
2018-12-12 04:11:20 -06:00
|
|
|
kind: RetagKind,
|
2018-11-13 05:48:20 -06:00
|
|
|
}
|
2019-06-13 01:52:04 -05:00
|
|
|
impl<'ecx, 'mir, 'tcx>
|
|
|
|
MutValueVisitor<'mir, 'tcx, Evaluator<'tcx>>
|
2018-11-13 05:48:20 -06:00
|
|
|
for
|
2019-06-13 01:52:04 -05:00
|
|
|
RetagVisitor<'ecx, 'mir, 'tcx>
|
2018-11-13 05:48:20 -06:00
|
|
|
{
|
2019-04-15 08:36:09 -05:00
|
|
|
type V = MPlaceTy<'tcx, Tag>;
|
2018-11-13 05:48:20 -06:00
|
|
|
|
|
|
|
#[inline(always)]
|
2019-06-13 01:52:04 -05:00
|
|
|
fn ecx(&mut self) -> &mut MiriEvalContext<'mir, 'tcx> {
|
2018-11-13 05:48:20 -06:00
|
|
|
&mut self.ecx
|
|
|
|
}
|
|
|
|
|
|
|
|
// Primitives of reference type, that is the one thing we are interested in.
|
2019-06-08 15:14:47 -05:00
|
|
|
fn visit_primitive(&mut self, place: MPlaceTy<'tcx, Tag>) -> InterpResult<'tcx>
|
2018-11-13 05:48:20 -06:00
|
|
|
{
|
2018-11-17 05:33:44 -06:00
|
|
|
// Cannot use `builtin_deref` because that reports *immutable* for `Box`,
|
|
|
|
// making it useless.
|
2019-04-17 07:57:13 -05:00
|
|
|
if let Some((mutbl, protector)) = qualify(place.layout.ty, self.kind) {
|
2018-11-21 08:21:41 -06:00
|
|
|
let val = self.ecx.read_immediate(place.into())?;
|
2018-12-12 04:11:20 -06:00
|
|
|
let val = self.ecx.retag_reference(
|
|
|
|
val,
|
|
|
|
mutbl,
|
2019-05-17 06:42:36 -05:00
|
|
|
protector
|
2018-12-12 04:11:20 -06:00
|
|
|
)?;
|
2018-11-21 08:21:41 -06:00
|
|
|
self.ecx.write_immediate(val, place.into())?;
|
|
|
|
}
|
2018-11-13 05:48:20 -06:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-24 10:17:44 -05:00
|
|
|
Ok(())
|
|
|
|
}
|
2018-10-16 11:01:50 -05:00
|
|
|
}
|