2018-10-26 11:31:20 +02:00
|
|
|
use std::cell::RefCell;
|
2018-10-16 18:01:50 +02:00
|
|
|
|
2018-11-05 16:05:17 +01:00
|
|
|
use rustc::ty::{self, layout::Size};
|
2018-10-18 16:59:08 +02:00
|
|
|
use rustc::hir;
|
2018-10-16 18:01:50 +02:00
|
|
|
|
2018-11-01 08:55:03 +01:00
|
|
|
use crate::{
|
2018-11-05 16:05:17 +01:00
|
|
|
EvalResult, MiriEvalContext, HelpersEvalContextExt,
|
2018-11-12 08:54:12 +01:00
|
|
|
MemoryKind, MiriMemoryKind, RangeMap, AllocId, Allocation, AllocationExtra,
|
2018-11-07 14:56:25 +01:00
|
|
|
Pointer, MemPlace, Scalar, Immediate, ImmTy, PlaceTy, MPlaceTy,
|
2018-10-16 18:01:50 +02:00
|
|
|
};
|
2018-10-16 11:21:38 +02:00
|
|
|
|
|
|
|
pub type Timestamp = u64;
|
|
|
|
|
2018-11-05 16:05:17 +01:00
|
|
|
/// Information about which kind of borrow was used to create the reference this is tagged
|
|
|
|
/// with.
|
2018-10-16 11:21:38 +02:00
|
|
|
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
|
|
|
|
pub enum Borrow {
|
2018-11-05 16:05:17 +01:00
|
|
|
/// A unique (mutable) reference.
|
|
|
|
Uniq(Timestamp),
|
|
|
|
/// A shared reference. This is also used by raw pointers, which do not track details
|
|
|
|
/// of how or when they were created, hence the timestamp is optional.
|
|
|
|
/// Shr(Some(_)) does NOT mean that the destination of this reference is frozen;
|
|
|
|
/// that depends on the type! Only those parts outside of an `UnsafeCell` are actually
|
|
|
|
/// frozen.
|
|
|
|
Shr(Option<Timestamp>),
|
2018-10-16 11:21:38 +02:00
|
|
|
}
|
|
|
|
|
2018-10-16 18:01:50 +02:00
|
|
|
impl Borrow {
|
|
|
|
#[inline(always)]
|
2018-11-05 16:05:17 +01:00
|
|
|
pub fn is_shr(self) -> bool {
|
2018-10-24 11:39:31 +02:00
|
|
|
match self {
|
2018-11-05 16:05:17 +01:00
|
|
|
Borrow::Shr(_) => true,
|
2018-10-24 11:39:31 +02:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
2018-11-05 16:05:17 +01:00
|
|
|
pub fn is_uniq(self) -> bool {
|
2018-10-16 18:01:50 +02:00
|
|
|
match self {
|
2018-11-05 16:05:17 +01:00
|
|
|
Borrow::Uniq(_) => true,
|
2018-10-16 18:01:50 +02:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-30 16:46:28 +01:00
|
|
|
impl Default for Borrow {
|
|
|
|
fn default() -> Self {
|
2018-11-05 16:05:17 +01:00
|
|
|
Borrow::Shr(None)
|
2018-10-30 16:46:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-05 16:05:17 +01:00
|
|
|
/// An item in the per-location borrow stack
|
2018-10-16 11:21:38 +02:00
|
|
|
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
|
|
|
|
pub enum BorStackItem {
|
2018-11-05 16:05:17 +01:00
|
|
|
/// Indicates the unique reference that may mutate.
|
|
|
|
Uniq(Timestamp),
|
|
|
|
/// Indicates that the location has been shared. Used for raw pointers, but
|
|
|
|
/// also for shared references. The latter *additionally* get frozen
|
|
|
|
/// when there is no `UnsafeCell`.
|
|
|
|
Shr,
|
2018-10-17 15:15:53 +02:00
|
|
|
/// A barrier, tracking the function it belongs to by its index on the call stack
|
|
|
|
#[allow(dead_code)] // for future use
|
|
|
|
FnBarrier(usize)
|
2018-10-16 11:21:38 +02:00
|
|
|
}
|
|
|
|
|
2018-10-30 16:46:28 +01:00
|
|
|
impl BorStackItem {
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn is_fn_barrier(self) -> bool {
|
|
|
|
match self {
|
|
|
|
BorStackItem::FnBarrier(_) => true,
|
|
|
|
_ => false,
|
|
|
|
}
|
2018-10-16 11:21:38 +02:00
|
|
|
}
|
|
|
|
}
|
2018-10-16 18:01:50 +02:00
|
|
|
|
2018-11-05 16:05:17 +01:00
|
|
|
/// Extra per-location state
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub struct Stack {
|
|
|
|
borrows: Vec<BorStackItem>, // used as a stack; never empty
|
|
|
|
frozen_since: Option<Timestamp>, // virtual frozen "item" on top of the stack
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Stack {
|
|
|
|
fn default() -> Self {
|
|
|
|
Stack {
|
|
|
|
borrows: vec![BorStackItem::Shr],
|
|
|
|
frozen_since: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Stack {
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn is_frozen(&self) -> bool {
|
|
|
|
self.frozen_since.is_some()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-30 16:46:28 +01:00
|
|
|
/// What kind of usage of the pointer are we talking about?
|
2018-10-19 16:07:40 +02:00
|
|
|
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
|
2018-10-30 16:46:28 +01:00
|
|
|
pub enum UsageKind {
|
|
|
|
/// Write, or create &mut
|
|
|
|
Write,
|
|
|
|
/// Read, or create &
|
|
|
|
Read,
|
2018-11-05 16:05:17 +01:00
|
|
|
/// Create * (raw ptr)
|
2018-10-19 16:07:40 +02:00
|
|
|
Raw,
|
|
|
|
}
|
|
|
|
|
2018-10-30 16:46:28 +01:00
|
|
|
impl From<Option<hir::Mutability>> for UsageKind {
|
2018-10-19 16:07:40 +02:00
|
|
|
fn from(mutbl: Option<hir::Mutability>) -> Self {
|
|
|
|
match mutbl {
|
2018-10-30 16:46:28 +01:00
|
|
|
None => UsageKind::Raw,
|
|
|
|
Some(hir::MutMutable) => UsageKind::Write,
|
|
|
|
Some(hir::MutImmutable) => UsageKind::Read,
|
2018-10-19 16:07:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-16 18:01:50 +02:00
|
|
|
/// Extra global machine state
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub struct State {
|
2018-10-26 11:31:20 +02:00
|
|
|
clock: Timestamp
|
2018-10-16 18:01:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl State {
|
|
|
|
pub fn new() -> State {
|
2018-10-26 11:31:20 +02:00
|
|
|
State { clock: 0 }
|
2018-10-16 18:01:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Extra per-allocation state
|
|
|
|
#[derive(Clone, Debug, Default)]
|
|
|
|
pub struct Stacks {
|
2018-10-26 11:31:20 +02:00
|
|
|
// Even reading memory can have effects on the stack, so we need a `RefCell` here.
|
2018-10-16 18:01:50 +02:00
|
|
|
stacks: RefCell<RangeMap<Stack>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Core operations
|
|
|
|
impl<'tcx> Stack {
|
2018-10-19 16:07:40 +02:00
|
|
|
/// Check if `bor` could be activated by unfreezing and popping.
|
2018-11-05 16:23:22 +01:00
|
|
|
/// `is_write` indicates whether this is being used to write (or, equivalently, to
|
|
|
|
/// borrow as &mut).
|
2018-11-05 16:05:17 +01:00
|
|
|
/// Returns `Err` if the answer is "no"; otherwise the return value indicates what to
|
|
|
|
/// do: With `Some(n)` you need to unfreeze, and then additionally pop `n` items.
|
2018-11-05 16:23:22 +01:00
|
|
|
fn reactivatable(&self, bor: Borrow, is_write: bool) -> Result<Option<usize>, String> {
|
2018-11-05 16:05:17 +01:00
|
|
|
// Check if we can match the frozen "item". Not possible on writes!
|
2018-11-05 16:23:22 +01:00
|
|
|
if !is_write {
|
2018-11-05 16:05:17 +01:00
|
|
|
// For now, we do NOT check the timestamp. That might be surprising, but
|
|
|
|
// we cannot even notice when a location should be frozen but is not!
|
|
|
|
// Those checks are both done in `tag_dereference`, where we have type information.
|
|
|
|
// Either way, it is crucial that the frozen "item" matches raw pointers:
|
|
|
|
// Reading through a raw should not unfreeze.
|
|
|
|
match (self.frozen_since, bor) {
|
|
|
|
(Some(_), Borrow::Shr(_)) => {
|
|
|
|
return Ok(None)
|
|
|
|
}
|
|
|
|
_ => {},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// See if we can find this borrow.
|
|
|
|
for (idx, &itm) in self.borrows.iter().rev().enumerate() {
|
|
|
|
// Check borrow and stack item for compatibility.
|
|
|
|
match (itm, bor) {
|
|
|
|
(BorStackItem::FnBarrier(_), _) => {
|
|
|
|
return Err(format!("Trying to reactivate a borrow ({:?}) that lives \
|
|
|
|
behind a barrier", bor))
|
|
|
|
}
|
|
|
|
(BorStackItem::Uniq(itm_t), Borrow::Uniq(bor_t)) if itm_t == bor_t => {
|
2018-11-07 21:08:20 +01:00
|
|
|
// Found matching unique item. This is *always* required to use a `Uniq`:
|
|
|
|
// The item must still be on the stack.
|
2018-11-05 16:23:22 +01:00
|
|
|
if !is_write {
|
2018-11-07 21:08:20 +01:00
|
|
|
// As a special case, if we are reading, let us see if it would be
|
|
|
|
// beneficial to pretend we are a raw pointer instead. If
|
|
|
|
// raw pointers are allowed to read while popping *less* than we
|
|
|
|
// would have to pop, there is no reason not to let them do this.
|
2018-11-05 16:23:22 +01:00
|
|
|
match self.reactivatable(Borrow::default(), is_write) {
|
2018-11-07 21:08:20 +01:00
|
|
|
// If we got something better (popping less) that `idx`, use that
|
2018-11-05 16:05:17 +01:00
|
|
|
Ok(None) => return Ok(None),
|
|
|
|
Ok(Some(shr_idx)) if shr_idx <= idx => return Ok(Some(shr_idx)),
|
|
|
|
// Otherwise just go on.
|
|
|
|
_ => {},
|
2018-10-30 15:08:18 +01:00
|
|
|
}
|
|
|
|
}
|
2018-11-05 16:05:17 +01:00
|
|
|
return Ok(Some(idx))
|
2018-10-16 18:01:50 +02:00
|
|
|
}
|
2018-11-05 16:05:17 +01:00
|
|
|
(BorStackItem::Shr, Borrow::Shr(_)) => {
|
|
|
|
// Found matching shared/raw item.
|
|
|
|
return Ok(Some(idx))
|
|
|
|
}
|
|
|
|
// Go on looking.
|
|
|
|
_ => {}
|
2018-10-16 18:01:50 +02:00
|
|
|
}
|
|
|
|
}
|
2018-10-22 18:01:32 +02:00
|
|
|
// Nothing to be found.
|
2018-11-05 16:05:17 +01:00
|
|
|
Err(format!("Borrow-to-reactivate {:?} does not exist on the stack", bor))
|
2018-10-23 15:59:50 +02:00
|
|
|
}
|
|
|
|
|
2018-11-05 16:23:22 +01:00
|
|
|
/// Reactive `bor` for this stack. `is_write` indicates whether this is being
|
|
|
|
/// used to write (or, equivalently, to borrow as &mut).
|
|
|
|
fn reactivate(&mut self, bor: Borrow, is_write: bool) -> EvalResult<'tcx> {
|
|
|
|
let mut pop = match self.reactivatable(bor, is_write) {
|
2018-11-05 16:05:17 +01:00
|
|
|
Ok(None) => return Ok(()),
|
|
|
|
Ok(Some(pop)) => pop,
|
2018-10-23 15:59:50 +02:00
|
|
|
Err(err) => return err!(MachineError(err)),
|
|
|
|
};
|
2018-11-05 16:05:17 +01:00
|
|
|
// Pop what `reactivatable` told us to pop. Always unfreeze.
|
|
|
|
if self.is_frozen() {
|
|
|
|
trace!("reactivate: Unfreezing");
|
|
|
|
}
|
|
|
|
self.frozen_since = None;
|
|
|
|
while pop > 0 {
|
|
|
|
let itm = self.borrows.pop().unwrap();
|
|
|
|
trace!("reactivate: Popping {:?}", itm);
|
|
|
|
pop -= 1;
|
2018-10-23 15:59:50 +02:00
|
|
|
}
|
|
|
|
Ok(())
|
2018-10-16 18:01:50 +02:00
|
|
|
}
|
|
|
|
|
2018-11-05 16:05:17 +01:00
|
|
|
/// Initiate `bor`; mostly this means pushing.
|
2018-10-29 19:48:43 +01:00
|
|
|
/// This operation cannot fail; it is up to the caller to ensure that the precondition
|
2018-11-07 16:56:25 +01:00
|
|
|
/// is met: We cannot push `Uniq` onto frozen stacks.
|
|
|
|
/// Crucially, this makes pushing a `Shr` onto a frozen location a NOP. We do not want
|
|
|
|
/// such a location to get mutably shared this way!
|
2018-10-29 19:48:43 +01:00
|
|
|
fn initiate(&mut self, bor: Borrow) {
|
2018-11-05 16:05:17 +01:00
|
|
|
if let Some(_) = self.frozen_since {
|
2018-11-07 16:56:25 +01:00
|
|
|
// A frozen location, we won't change anything here!
|
2018-11-05 16:05:17 +01:00
|
|
|
match bor {
|
|
|
|
Borrow::Uniq(_) => bug!("Trying to create unique ref to frozen location"),
|
|
|
|
Borrow::Shr(_) => trace!("initiate: New shared ref to frozen location is a NOP"),
|
2018-10-16 18:01:50 +02:00
|
|
|
}
|
2018-11-05 16:05:17 +01:00
|
|
|
} else {
|
|
|
|
// Just push.
|
|
|
|
let itm = match bor {
|
|
|
|
Borrow::Uniq(t) => BorStackItem::Uniq(t),
|
|
|
|
Borrow::Shr(_) if *self.borrows.last().unwrap() == BorStackItem::Shr => {
|
|
|
|
// Optimization: Don't push a Shr onto a Shr.
|
|
|
|
trace!("initiate: New shared ref to already shared location is a NOP");
|
|
|
|
return
|
|
|
|
},
|
|
|
|
Borrow::Shr(_) => BorStackItem::Shr,
|
|
|
|
};
|
|
|
|
trace!("initiate: Pushing {:?}", itm);
|
|
|
|
self.borrows.push(itm)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Check if this location is "frozen enough".
|
|
|
|
fn check_frozen(&self, bor_t: Timestamp) -> EvalResult<'tcx> {
|
|
|
|
let frozen = self.frozen_since.map_or(false, |itm_t| itm_t <= bor_t);
|
|
|
|
if !frozen {
|
|
|
|
err!(MachineError(format!("Location is not frozen long enough")))
|
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Freeze this location, since `bor_t`.
|
|
|
|
fn freeze(&mut self, bor_t: Timestamp) {
|
|
|
|
if let Some(itm_t) = self.frozen_since {
|
|
|
|
assert!(itm_t <= bor_t, "Trying to freeze shorter than it was frozen?");
|
|
|
|
} else {
|
|
|
|
trace!("Freezing");
|
|
|
|
self.frozen_since = Some(bor_t);
|
2018-10-16 18:01:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl State {
|
2018-10-26 11:31:20 +02:00
|
|
|
fn increment_clock(&mut self) -> Timestamp {
|
|
|
|
let val = self.clock;
|
|
|
|
self.clock = val + 1;
|
2018-10-18 16:59:08 +02:00
|
|
|
val
|
2018-10-16 18:01:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-17 15:15:53 +02:00
|
|
|
/// Higher-level operations
|
|
|
|
impl<'tcx> Stacks {
|
2018-11-07 16:56:25 +01:00
|
|
|
/// `ptr` got used, reflect that in the stack.
|
|
|
|
fn reactivate(
|
2018-10-17 15:15:53 +02:00
|
|
|
&self,
|
|
|
|
ptr: Pointer<Borrow>,
|
|
|
|
size: Size,
|
2018-10-30 16:46:28 +01:00
|
|
|
usage: UsageKind,
|
2018-10-17 15:15:53 +02:00
|
|
|
) -> EvalResult<'tcx> {
|
2018-11-07 16:56:25 +01:00
|
|
|
trace!("use_borrow of tag {:?} as {:?}: {:?}, size {}",
|
|
|
|
ptr.tag, usage, ptr, size.bytes());
|
2018-10-17 15:15:53 +02:00
|
|
|
let mut stacks = self.stacks.borrow_mut();
|
|
|
|
for stack in stacks.iter_mut(ptr.offset, size) {
|
2018-11-05 16:23:22 +01:00
|
|
|
stack.reactivate(ptr.tag, usage == UsageKind::Write)?;
|
2018-10-17 15:15:53 +02:00
|
|
|
}
|
2018-11-05 16:05:17 +01:00
|
|
|
Ok(())
|
|
|
|
}
|
2018-10-17 15:15:53 +02:00
|
|
|
|
2018-11-07 16:56:25 +01:00
|
|
|
/// Create a new borrow, the ptr must already have the new tag.
|
|
|
|
/// Also freezes the location if `freeze` is set and the tag is a timestamped `Shr`.
|
|
|
|
fn initiate(
|
2018-11-05 16:05:17 +01:00
|
|
|
&self,
|
|
|
|
ptr: Pointer<Borrow>,
|
|
|
|
size: Size,
|
2018-11-07 16:56:25 +01:00
|
|
|
freeze: bool,
|
|
|
|
) {
|
|
|
|
trace!("reborrow for tag {:?}: {:?}, size {}",
|
|
|
|
ptr.tag, ptr, size.bytes());
|
2018-11-05 16:05:17 +01:00
|
|
|
let mut stacks = self.stacks.borrow_mut();
|
|
|
|
for stack in stacks.iter_mut(ptr.offset, size) {
|
2018-11-07 16:56:25 +01:00
|
|
|
stack.initiate(ptr.tag);
|
|
|
|
if freeze {
|
|
|
|
if let Borrow::Shr(Some(bor_t)) = ptr.tag {
|
|
|
|
stack.freeze(bor_t);
|
|
|
|
}
|
|
|
|
}
|
2018-11-05 16:05:17 +01:00
|
|
|
}
|
2018-10-17 15:15:53 +02:00
|
|
|
}
|
2018-10-22 18:01:32 +02:00
|
|
|
|
2018-11-05 16:05:17 +01:00
|
|
|
/// Check that this stack is fine with being dereferenced
|
|
|
|
fn check_deref(
|
|
|
|
&self,
|
|
|
|
ptr: Pointer<Borrow>,
|
|
|
|
size: Size,
|
2018-11-07 16:56:25 +01:00
|
|
|
frozen: bool,
|
2018-11-05 16:05:17 +01:00
|
|
|
) -> EvalResult<'tcx> {
|
|
|
|
let mut stacks = self.stacks.borrow_mut();
|
|
|
|
// We need `iter_mut` because `iter` would skip gaps!
|
|
|
|
for stack in stacks.iter_mut(ptr.offset, size) {
|
|
|
|
// Conservatively assume we will just read
|
2018-11-05 16:23:22 +01:00
|
|
|
if let Err(err) = stack.reactivatable(ptr.tag, /*is_write*/false) {
|
2018-11-05 16:05:17 +01:00
|
|
|
return err!(MachineError(format!(
|
|
|
|
"Encountered reference with non-reactivatable tag: {}",
|
|
|
|
err
|
|
|
|
)))
|
|
|
|
}
|
2018-11-07 16:56:25 +01:00
|
|
|
// Sometimes we also need to be frozen.
|
2018-11-07 21:08:20 +01:00
|
|
|
// In this case we *both* push `Shr` and then freeze. This means that a `&mut`
|
|
|
|
// to `*const` to `*mut` cast through `&` actually works.
|
2018-11-07 16:56:25 +01:00
|
|
|
if frozen {
|
|
|
|
// Even shared refs can have uniq tags (after transmute). That's not an error
|
|
|
|
// but they do not get any freezing benefits.
|
|
|
|
if let Borrow::Shr(Some(bor_t)) = ptr.tag {
|
|
|
|
stack.check_frozen(bor_t)?;
|
|
|
|
}
|
|
|
|
}
|
2018-11-05 16:05:17 +01:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Hooks and glue
|
2018-11-12 08:54:12 +01:00
|
|
|
impl AllocationExtra<Borrow> for Stacks {
|
2018-10-29 19:48:43 +01:00
|
|
|
#[inline(always)]
|
2018-11-12 08:54:12 +01:00
|
|
|
fn memory_read<'tcx>(
|
|
|
|
alloc: &Allocation<Borrow, Stacks>,
|
2018-10-29 19:48:43 +01:00
|
|
|
ptr: Pointer<Borrow>,
|
|
|
|
size: Size,
|
|
|
|
) -> EvalResult<'tcx> {
|
|
|
|
// Reads behave exactly like the first half of a reborrow-to-shr
|
2018-11-07 16:56:25 +01:00
|
|
|
alloc.extra.reactivate(ptr, size, UsageKind::Read)
|
2018-10-29 19:48:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
2018-11-12 08:54:12 +01:00
|
|
|
fn memory_written<'tcx>(
|
|
|
|
alloc: &mut Allocation<Borrow, Stacks>,
|
2018-10-29 19:48:43 +01:00
|
|
|
ptr: Pointer<Borrow>,
|
|
|
|
size: Size,
|
|
|
|
) -> EvalResult<'tcx> {
|
|
|
|
// Writes behave exactly like the first half of a reborrow-to-mut
|
2018-11-07 16:56:25 +01:00
|
|
|
alloc.extra.reactivate(ptr, size, UsageKind::Read)
|
2018-10-29 19:48:43 +01:00
|
|
|
}
|
|
|
|
|
2018-11-12 08:54:12 +01:00
|
|
|
#[inline(always)]
|
2018-11-14 16:03:38 +01:00
|
|
|
fn memory_deallocated<'tcx>(
|
|
|
|
alloc: &mut Allocation<Borrow, Stacks>,
|
2018-10-29 19:48:43 +01:00
|
|
|
ptr: Pointer<Borrow>,
|
|
|
|
size: Size,
|
|
|
|
) -> EvalResult<'tcx> {
|
|
|
|
// This is like mutating
|
2018-11-07 16:56:25 +01:00
|
|
|
alloc.extra.reactivate(ptr, size, UsageKind::Write)
|
2018-10-29 19:48:43 +01:00
|
|
|
// FIXME: Error out of there are any barriers?
|
|
|
|
}
|
2018-11-14 16:03:38 +01:00
|
|
|
}
|
2018-10-29 19:48:43 +01:00
|
|
|
|
2018-11-14 16:03:38 +01:00
|
|
|
impl<'tcx> Stacks {
|
2018-11-05 16:05:17 +01:00
|
|
|
/// Pushes the first item to the stacks.
|
|
|
|
pub fn first_item(
|
2018-10-22 18:01:32 +02:00
|
|
|
&mut self,
|
2018-11-05 16:05:17 +01:00
|
|
|
itm: BorStackItem,
|
2018-10-22 18:01:32 +02:00
|
|
|
size: Size
|
|
|
|
) {
|
2018-11-05 16:05:17 +01:00
|
|
|
assert!(!itm.is_fn_barrier());
|
2018-10-22 18:01:32 +02:00
|
|
|
for stack in self.stacks.get_mut().iter_mut(Size::ZERO, size) {
|
2018-11-05 16:05:17 +01:00
|
|
|
assert!(stack.borrows.len() == 1);
|
|
|
|
assert_eq!(stack.borrows.pop().unwrap(), BorStackItem::Shr);
|
|
|
|
stack.borrows.push(itm);
|
2018-10-22 18:01:32 +02:00
|
|
|
}
|
|
|
|
}
|
2018-10-17 15:15:53 +02:00
|
|
|
}
|
|
|
|
|
2018-11-05 16:05:17 +01:00
|
|
|
|
|
|
|
|
2018-10-16 18:01:50 +02:00
|
|
|
pub trait EvalContextExt<'tcx> {
|
|
|
|
fn tag_dereference(
|
|
|
|
&self,
|
2018-11-05 16:05:17 +01:00
|
|
|
place: MPlaceTy<'tcx, Borrow>,
|
2018-10-18 16:59:08 +02:00
|
|
|
size: Size,
|
2018-10-30 16:46:28 +01:00
|
|
|
usage: UsageKind,
|
2018-10-16 18:01:50 +02:00
|
|
|
) -> EvalResult<'tcx, Borrow>;
|
2018-10-22 18:01:32 +02:00
|
|
|
|
|
|
|
fn tag_new_allocation(
|
|
|
|
&mut self,
|
|
|
|
id: AllocId,
|
|
|
|
kind: MemoryKind<MiriMemoryKind>,
|
|
|
|
) -> Borrow;
|
2018-10-24 17:17:44 +02:00
|
|
|
|
2018-11-07 14:56:25 +01:00
|
|
|
/// Retag an indidual pointer, returning the retagged version.
|
|
|
|
fn retag_ptr(
|
|
|
|
&mut self,
|
|
|
|
ptr: ImmTy<'tcx, Borrow>,
|
|
|
|
mutbl: hir::Mutability,
|
|
|
|
) -> EvalResult<'tcx, Immediate<Borrow>>;
|
|
|
|
|
2018-10-24 17:17:44 +02:00
|
|
|
fn retag(
|
|
|
|
&mut self,
|
|
|
|
fn_entry: bool,
|
|
|
|
place: PlaceTy<'tcx, Borrow>
|
|
|
|
) -> EvalResult<'tcx>;
|
2018-10-16 18:01:50 +02:00
|
|
|
|
2018-11-07 14:56:25 +01:00
|
|
|
fn escape_to_raw(
|
2018-10-26 11:31:20 +02:00
|
|
|
&mut self,
|
2018-11-05 16:05:17 +01:00
|
|
|
place: MPlaceTy<'tcx, Borrow>,
|
2018-10-26 11:31:20 +02:00
|
|
|
size: Size,
|
2018-11-07 14:56:25 +01:00
|
|
|
) -> EvalResult<'tcx>;
|
|
|
|
}
|
2018-10-16 18:01:50 +02:00
|
|
|
|
2018-11-07 14:56:25 +01:00
|
|
|
impl<'a, 'mir, 'tcx> EvalContextExt<'tcx> for MiriEvalContext<'a, 'mir, 'tcx> {
|
2018-11-07 16:56:25 +01:00
|
|
|
fn tag_new_allocation(
|
|
|
|
&mut self,
|
|
|
|
id: AllocId,
|
|
|
|
kind: MemoryKind<MiriMemoryKind>,
|
|
|
|
) -> Borrow {
|
|
|
|
let time = match kind {
|
|
|
|
MemoryKind::Stack => {
|
|
|
|
// New unique borrow. This `Uniq` is not accessible by the program,
|
|
|
|
// so it will only ever be used when using the local directly (i.e.,
|
|
|
|
// not through a pointer). IOW, whenever we directly use a local this will pop
|
|
|
|
// everything else off the stack, invalidating all previous pointers
|
|
|
|
// and, in particular, *all* raw pointers. This subsumes the explicit
|
|
|
|
// `reset` which the blog post [1] says to perform when accessing a local.
|
|
|
|
//
|
|
|
|
// [1] https://www.ralfj.de/blog/2018/08/07/stacked-borrows.html
|
|
|
|
self.machine.stacked_borrows.increment_clock()
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
// Nothing to do for everything else
|
|
|
|
return Borrow::default()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
// Make this the active borrow for this allocation
|
|
|
|
let alloc = self.memory_mut().get_mut(id).expect("This is a new allocation, it must still exist");
|
|
|
|
let size = Size::from_bytes(alloc.bytes.len() as u64);
|
|
|
|
alloc.extra.first_item(BorStackItem::Uniq(time), size);
|
|
|
|
Borrow::Uniq(time)
|
|
|
|
}
|
|
|
|
|
2018-10-18 16:59:08 +02:00
|
|
|
/// Called for value-to-place conversion.
|
2018-10-19 16:07:40 +02:00
|
|
|
///
|
|
|
|
/// Note that this does NOT mean that all this memory will actually get accessed/referenced!
|
|
|
|
/// We could be in the middle of `&(*var).1`.
|
2018-10-16 18:01:50 +02:00
|
|
|
fn tag_dereference(
|
|
|
|
&self,
|
2018-11-05 16:05:17 +01:00
|
|
|
place: MPlaceTy<'tcx, Borrow>,
|
2018-10-18 16:59:08 +02:00
|
|
|
size: Size,
|
2018-10-30 16:46:28 +01:00
|
|
|
usage: UsageKind,
|
2018-10-16 18:01:50 +02:00
|
|
|
) -> EvalResult<'tcx, Borrow> {
|
2018-11-05 16:05:17 +01:00
|
|
|
trace!("tag_dereference: Accessing reference ({:?}) for {:?} (pointee {})",
|
2018-11-07 14:56:25 +01:00
|
|
|
usage, place.ptr, place.layout.ty);
|
|
|
|
let ptr = place.ptr.to_ptr()?;
|
2018-10-19 16:07:40 +02:00
|
|
|
// In principle we should not have to do anything here. However, with transmutes involved,
|
2018-10-30 16:46:28 +01:00
|
|
|
// it can happen that the tag of `ptr` does not actually match `usage`, and we
|
2018-10-19 16:07:40 +02:00
|
|
|
// should adjust for that.
|
|
|
|
// Notably, the compiler can introduce such transmutes by optimizing away `&[mut]*`.
|
|
|
|
// That can transmute a raw ptr to a (shared/mut) ref, and a mut ref to a shared one.
|
2018-10-30 16:46:28 +01:00
|
|
|
match (usage, ptr.tag) {
|
|
|
|
(UsageKind::Raw, _) => {
|
2018-11-07 16:56:25 +01:00
|
|
|
// Don't use the tag, this is a raw access! They should happen tagless.
|
|
|
|
// This does mean, however, that `&*foo` is *not* a NOP *if* `foo` is a raw ptr.
|
2018-10-22 18:01:32 +02:00
|
|
|
// Also don't do any further validation, this is raw after all.
|
2018-11-05 16:05:17 +01:00
|
|
|
return Ok(Borrow::default());
|
2018-10-22 18:01:32 +02:00
|
|
|
}
|
2018-11-05 16:05:17 +01:00
|
|
|
(UsageKind::Write, Borrow::Uniq(_)) |
|
|
|
|
(UsageKind::Read, Borrow::Shr(_)) => {
|
2018-10-19 16:07:40 +02:00
|
|
|
// Expected combinations. Nothing to do.
|
|
|
|
}
|
2018-11-05 16:05:17 +01:00
|
|
|
(UsageKind::Write, Borrow::Shr(None)) => {
|
2018-10-22 18:01:32 +02:00
|
|
|
// Raw transmuted to mut ref. Keep this as raw access.
|
2018-10-19 16:07:40 +02:00
|
|
|
// We cannot reborrow here; there might be a raw in `&(*var).1` where
|
|
|
|
// `var` is an `&mut`. The other field of the struct might be already frozen,
|
|
|
|
// also using `var`, and that would be okay.
|
|
|
|
}
|
2018-11-05 16:05:17 +01:00
|
|
|
(UsageKind::Read, Borrow::Uniq(_)) => {
|
2018-11-03 11:42:38 +01:00
|
|
|
// A mut got transmuted to shr. Can happen even from compiler transformations:
|
|
|
|
// `&*x` gets optimized to `x` even when `x` is a `&mut`.
|
2018-10-19 16:07:40 +02:00
|
|
|
}
|
2018-11-05 16:05:17 +01:00
|
|
|
(UsageKind::Write, Borrow::Shr(Some(_))) => {
|
|
|
|
// This is just invalid: A shr got transmuted to a mut.
|
2018-10-19 16:07:40 +02:00
|
|
|
// If we ever allow this, we have to consider what we do when a turn a
|
|
|
|
// `Raw`-tagged `&mut` into a raw pointer pointing to a frozen location.
|
|
|
|
// We probably do not want to allow that, but we have to allow
|
|
|
|
// turning a `Raw`-tagged `&` into a raw ptr to a frozen location.
|
|
|
|
return err!(MachineError(format!("Encountered mutable reference with frozen tag {:?}", ptr.tag)))
|
|
|
|
}
|
|
|
|
}
|
2018-11-05 16:05:17 +01:00
|
|
|
|
2018-11-07 16:56:25 +01:00
|
|
|
// Get the allocation
|
2018-10-19 19:51:41 +02:00
|
|
|
self.memory().check_bounds(ptr, size, false)?;
|
|
|
|
let alloc = self.memory().get(ptr.alloc_id).expect("We checked that the ptr is fine!");
|
2018-11-07 16:56:25 +01:00
|
|
|
// If we got here, we do some checking, *but* we leave the tag unchanged.
|
|
|
|
if let Borrow::Shr(Some(_)) = ptr.tag {
|
|
|
|
// We need a frozen-sensitive check
|
|
|
|
self.visit_freeze_sensitive(place, size, |cur_ptr, size, frozen| {
|
|
|
|
alloc.extra.check_deref(cur_ptr, size, frozen)
|
2018-11-05 16:05:17 +01:00
|
|
|
})?;
|
2018-11-07 16:56:25 +01:00
|
|
|
} else {
|
|
|
|
// Just treat this as one big chunk
|
|
|
|
alloc.extra.check_deref(ptr, size, /*frozen*/false)?;
|
2018-10-19 16:07:40 +02:00
|
|
|
}
|
2018-11-05 16:05:17 +01:00
|
|
|
|
|
|
|
// All is good, and do not change the tag
|
2018-10-19 16:07:40 +02:00
|
|
|
Ok(ptr.tag)
|
2018-10-16 18:01:50 +02:00
|
|
|
}
|
2018-10-22 18:01:32 +02:00
|
|
|
|
2018-11-07 16:56:25 +01:00
|
|
|
/// The given place may henceforth be accessed through raw pointers.
|
|
|
|
fn escape_to_raw(
|
2018-10-22 18:01:32 +02:00
|
|
|
&mut self,
|
2018-11-07 16:56:25 +01:00
|
|
|
place: MPlaceTy<'tcx, Borrow>,
|
|
|
|
size: Size,
|
|
|
|
) -> EvalResult<'tcx> {
|
|
|
|
trace!("self: {:?} is now accessible by raw pointers", *place);
|
|
|
|
// Get the allocation
|
|
|
|
let mut ptr = place.ptr.to_ptr()?;
|
|
|
|
self.memory().check_bounds(ptr, size, false)?; // `ptr_dereference` wouldn't do any checks if this is a raw ptr
|
|
|
|
let alloc = self.memory().get(ptr.alloc_id).expect("We checked that the ptr is fine!");
|
|
|
|
// Re-borrow to raw. This is a NOP for shared borrows, but we do not know the borrow
|
|
|
|
// type here and that's also okay. Freezing does not matter here.
|
|
|
|
alloc.extra.reactivate(ptr, size, UsageKind::Raw)?;
|
|
|
|
ptr.tag = Borrow::default();
|
|
|
|
alloc.extra.initiate(ptr, size, /*freeze*/false);
|
|
|
|
Ok(())
|
2018-10-22 18:01:32 +02:00
|
|
|
}
|
2018-10-24 17:17:44 +02:00
|
|
|
|
2018-11-07 14:56:25 +01:00
|
|
|
fn retag_ptr(
|
|
|
|
&mut self,
|
|
|
|
val: ImmTy<'tcx, Borrow>,
|
|
|
|
mutbl: hir::Mutability,
|
|
|
|
) -> EvalResult<'tcx, Immediate<Borrow>> {
|
|
|
|
// We want a place for where the ptr *points to*, so we get one.
|
|
|
|
let place = self.ref_to_mplace(val)?;
|
|
|
|
let size = self.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);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prepare to re-borrow this place.
|
|
|
|
let ptr = place.ptr.to_ptr()?;
|
|
|
|
let time = self.machine.stacked_borrows.increment_clock();
|
|
|
|
let new_bor = match mutbl {
|
|
|
|
hir::MutMutable => Borrow::Uniq(time),
|
|
|
|
hir::MutImmutable => Borrow::Shr(Some(time)),
|
|
|
|
};
|
2018-11-07 16:56:25 +01:00
|
|
|
let new_ptr = Pointer::new_with_tag(ptr.alloc_id, ptr.offset, new_bor);
|
2018-11-07 14:56:25 +01:00
|
|
|
trace!("retag: Creating new reference ({:?}) for {:?} (pointee {}): {:?}",
|
|
|
|
mutbl, ptr, place.layout.ty, new_bor);
|
|
|
|
|
2018-11-07 16:56:25 +01:00
|
|
|
// Get the allocation
|
2018-11-07 14:56:25 +01:00
|
|
|
self.memory().check_bounds(ptr, size, false)?; // `ptr_dereference` wouldn't do any checks if this is a raw ptr
|
|
|
|
let alloc = self.memory().get(ptr.alloc_id).expect("We checked that the ptr is fine!");
|
2018-11-07 16:56:25 +01:00
|
|
|
// Update the stacks. First use old borrow, then initiate new one.
|
|
|
|
alloc.extra.reactivate(ptr, size, Some(mutbl).into())?;
|
|
|
|
if mutbl == hir::MutImmutable {
|
|
|
|
// We need a frozen-sensitive initiate
|
|
|
|
self.visit_freeze_sensitive(place, size, |mut cur_ptr, size, frozen| {
|
|
|
|
cur_ptr.tag = new_bor;
|
|
|
|
Ok(alloc.extra.initiate(cur_ptr, size, frozen))
|
2018-11-07 14:56:25 +01:00
|
|
|
})?;
|
2018-11-07 16:56:25 +01:00
|
|
|
} else {
|
|
|
|
// Just treat this as one big chunk
|
|
|
|
alloc.extra.initiate(new_ptr, size, /*frozen*/false);
|
2018-11-07 14:56:25 +01:00
|
|
|
}
|
|
|
|
|
2018-11-07 16:56:25 +01:00
|
|
|
// Return new ptr
|
|
|
|
let new_place = MemPlace { ptr: Scalar::Ptr(new_ptr), ..*place };
|
2018-11-07 14:56:25 +01:00
|
|
|
Ok(new_place.to_ref())
|
|
|
|
}
|
|
|
|
|
2018-10-24 17:17:44 +02:00
|
|
|
fn retag(
|
|
|
|
&mut self,
|
|
|
|
_fn_entry: bool,
|
2018-10-26 11:31:20 +02:00
|
|
|
place: PlaceTy<'tcx, Borrow>
|
2018-10-24 17:17:44 +02:00
|
|
|
) -> EvalResult<'tcx> {
|
2018-10-26 11:31:20 +02:00
|
|
|
// For now, we only retag if the toplevel type is a reference.
|
|
|
|
// TODO: Recurse into structs and enums, sharing code with validation.
|
2018-11-07 14:56:25 +01:00
|
|
|
// TODO: Honor `fn_entry`.
|
2018-10-26 11:31:20 +02:00
|
|
|
let mutbl = match place.layout.ty.sty {
|
|
|
|
ty::Ref(_, _, mutbl) => mutbl, // go ahead
|
2018-11-07 14:56:25 +01:00
|
|
|
_ => return Ok(()), // do nothing, for now
|
2018-10-26 11:31:20 +02:00
|
|
|
};
|
2018-11-07 14:56:25 +01:00
|
|
|
// Retag the pointer and write it back.
|
2018-11-05 08:51:55 +01:00
|
|
|
let val = self.read_immediate(self.place_to_op(place)?)?;
|
2018-11-07 14:56:25 +01:00
|
|
|
let val = self.retag_ptr(val, mutbl)?;
|
2018-11-05 08:51:55 +01:00
|
|
|
self.write_immediate(val, place)?;
|
2018-10-24 17:17:44 +02:00
|
|
|
Ok(())
|
|
|
|
}
|
2018-10-16 18:01:50 +02:00
|
|
|
}
|