2018-10-26 11:31:20 +02:00
|
|
|
use std::cell::RefCell;
|
2018-10-16 18:01:50 +02:00
|
|
|
|
2018-10-26 11:31:20 +02:00
|
|
|
use rustc::ty::{self, Ty, 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-10-29 19:48:43 +01:00
|
|
|
MemoryKind, MiriMemoryKind, RangeMap, EvalResult, AllocId,
|
2018-10-24 17:17:44 +02:00
|
|
|
Pointer, PlaceTy,
|
2018-10-16 18:01:50 +02:00
|
|
|
};
|
2018-10-16 11:21:38 +02:00
|
|
|
|
|
|
|
pub type Timestamp = u64;
|
|
|
|
|
|
|
|
/// Information about a potentially mutable borrow
|
|
|
|
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
|
|
|
|
pub enum Mut {
|
2018-10-17 15:15:53 +02:00
|
|
|
/// A unique, mutable reference
|
|
|
|
Uniq(Timestamp),
|
|
|
|
/// Any raw pointer, or a shared borrow with interior mutability
|
|
|
|
Raw,
|
2018-10-16 11:21:38 +02:00
|
|
|
}
|
|
|
|
|
2018-10-16 18:01:50 +02:00
|
|
|
impl Mut {
|
|
|
|
#[inline(always)]
|
2018-10-24 11:39:31 +02:00
|
|
|
pub fn is_raw(self) -> bool {
|
2018-10-16 18:01:50 +02:00
|
|
|
match self {
|
|
|
|
Mut::Raw => true,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
2018-10-24 11:39:31 +02:00
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn is_uniq(self) -> bool {
|
|
|
|
match self {
|
|
|
|
Mut::Uniq(_) => true,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
2018-10-16 18:01:50 +02:00
|
|
|
}
|
|
|
|
|
2018-10-16 11:21:38 +02:00
|
|
|
/// Information about any kind of borrow
|
|
|
|
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
|
|
|
|
pub enum Borrow {
|
2018-10-17 15:15:53 +02:00
|
|
|
/// A mutable borrow, a raw pointer, or a shared borrow with interior mutability
|
|
|
|
Mut(Mut),
|
|
|
|
/// A shared borrow without interior mutability
|
|
|
|
Frz(Timestamp)
|
2018-10-16 11:21:38 +02:00
|
|
|
}
|
|
|
|
|
2018-10-16 18:01:50 +02:00
|
|
|
impl Borrow {
|
|
|
|
#[inline(always)]
|
2018-10-24 11:39:31 +02:00
|
|
|
pub fn is_uniq(self) -> bool {
|
|
|
|
match self {
|
|
|
|
Borrow::Mut(m) => m.is_uniq(),
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn is_frz(self) -> bool {
|
2018-10-16 18:01:50 +02:00
|
|
|
match self {
|
2018-10-24 11:39:31 +02:00
|
|
|
Borrow::Frz(_) => 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 {
|
|
|
|
Borrow::Mut(Mut::Raw)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-16 11:21:38 +02:00
|
|
|
/// An item in the borrow stack
|
|
|
|
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
|
|
|
|
pub enum BorStackItem {
|
2018-10-17 15:15:53 +02:00
|
|
|
/// Defines which references are permitted to mutate *if* the location is not frozen
|
|
|
|
Mut(Mut),
|
|
|
|
/// 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-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,
|
|
|
|
/// Create *
|
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-location state
|
|
|
|
#[derive(Clone, Debug)]
|
2018-10-30 16:46:28 +01:00
|
|
|
pub struct Stack {
|
2018-10-16 18:01:50 +02:00
|
|
|
borrows: Vec<BorStackItem>, // used as a stack
|
|
|
|
frozen_since: Option<Timestamp>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Stack {
|
|
|
|
fn default() -> Self {
|
|
|
|
Stack {
|
2018-10-22 18:01:32 +02:00
|
|
|
borrows: vec![BorStackItem::Mut(Mut::Raw)],
|
2018-10-16 18:01:50 +02:00
|
|
|
frozen_since: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-29 19:48:43 +01:00
|
|
|
impl Stack {
|
|
|
|
#[inline(always)]
|
|
|
|
fn is_frozen(&self) -> bool {
|
|
|
|
self.frozen_since.is_some()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-10-30 16:46:28 +01:00
|
|
|
/// `usage` indicates whether this is being used to read/write (or, equivalently, to
|
2018-10-30 15:08:18 +01:00
|
|
|
/// borrow as &/&mut), or to borrow as raw.
|
2018-10-23 15:59:50 +02:00
|
|
|
/// Returns `Err` if the answer is "no"; otherwise the data says
|
|
|
|
/// what needs to happen to activate this: `None` = nothing,
|
|
|
|
/// `Some(n)` = unfreeze and make item `n` the top item of the stack.
|
2018-10-30 16:46:28 +01:00
|
|
|
fn reactivatable(&self, bor: Borrow, usage: UsageKind) -> Result<Option<usize>, String> {
|
2018-10-30 15:08:18 +01:00
|
|
|
let mut_borrow = match bor {
|
2018-10-19 16:07:40 +02:00
|
|
|
Borrow::Frz(since) =>
|
2018-10-30 15:08:18 +01:00
|
|
|
// The only way to reactivate a `Frz` is if this is already frozen.
|
|
|
|
return match self.frozen_since {
|
2018-10-30 16:46:28 +01:00
|
|
|
_ if usage == UsageKind::Write =>
|
2018-10-30 15:08:18 +01:00
|
|
|
Err(format!("Using a shared borrow for mutation")),
|
|
|
|
None =>
|
|
|
|
Err(format!("Location should be frozen but it is not")),
|
|
|
|
Some(loc) if loc <= since =>
|
|
|
|
Ok(None),
|
|
|
|
Some(loc) =>
|
|
|
|
Err(format!("Location should be frozen since {} but it is only frozen \
|
|
|
|
since {}", since, loc)),
|
|
|
|
},
|
2018-10-30 16:46:28 +01:00
|
|
|
Borrow::Mut(Mut::Raw) if self.is_frozen() && usage != UsageKind::Write =>
|
2018-10-30 15:08:18 +01:00
|
|
|
// Non-mutating access with a raw from a frozen location is a special case: The
|
|
|
|
// shared refs do not mind raw reads, and the raw itself does not assume any
|
2018-10-30 16:46:28 +01:00
|
|
|
// exclusivity. So we do not even require there to be a raw on the stack,
|
|
|
|
// the raw is instead "matched" by the fact that this location is frozen.
|
2018-10-30 15:08:18 +01:00
|
|
|
// This does not break the assumption that an `&mut` we own is
|
|
|
|
// exclusive for reads, because there we have the invariant that
|
|
|
|
// the location is *not* frozen.
|
|
|
|
return Ok(None),
|
|
|
|
Borrow::Mut(mut_borrow) => mut_borrow
|
2018-10-16 18:01:50 +02:00
|
|
|
};
|
2018-10-30 15:08:18 +01:00
|
|
|
// See if we can get there via popping.
|
2018-10-23 15:59:50 +02:00
|
|
|
for (idx, &itm) in self.borrows.iter().enumerate().rev() {
|
2018-10-16 18:01:50 +02:00
|
|
|
match itm {
|
2018-10-23 15:59:50 +02:00
|
|
|
BorStackItem::FnBarrier(_) =>
|
2018-10-30 15:08:18 +01:00
|
|
|
return Err(format!("Trying to reactivate a mutable borrow ({:?}) that lives \
|
|
|
|
behind a barrier", mut_borrow)),
|
|
|
|
BorStackItem::Mut(loc) => {
|
|
|
|
if loc == mut_borrow {
|
|
|
|
// We found it! This is good to know.
|
|
|
|
// Yet, maybe we do not really want to pop?
|
2018-10-30 16:46:28 +01:00
|
|
|
if usage == UsageKind::Read && self.is_frozen() {
|
2018-10-30 15:08:18 +01:00
|
|
|
// Whoever had exclusive access to this location allowed it
|
|
|
|
// to become frozen. That can only happen if they reborrowed
|
|
|
|
// to a shared ref, at which point they gave up on exclusive access.
|
|
|
|
// Hence we allow more reads, entirely ignoring everything above
|
|
|
|
// on the stack (but still making sure it is on the stack).
|
|
|
|
// This does not break the assumption that an `&mut` we own is
|
|
|
|
// exclusive for reads, because there we have the invariant that
|
|
|
|
// the location is *not* frozen.
|
|
|
|
return Ok(None);
|
|
|
|
} else {
|
|
|
|
return Ok(Some(idx));
|
|
|
|
}
|
|
|
|
}
|
2018-10-16 18:01:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-10-22 18:01:32 +02:00
|
|
|
// Nothing to be found.
|
2018-10-30 15:08:18 +01:00
|
|
|
Err(format!("Mutable borrow-to-reactivate ({:?}) does not exist on the stack", mut_borrow))
|
2018-10-23 15:59:50 +02:00
|
|
|
}
|
|
|
|
|
2018-10-30 16:46:28 +01:00
|
|
|
/// Reactive `bor` for this stack. `usage` indicates whether this is being
|
2018-10-30 15:08:18 +01:00
|
|
|
/// used to read/write (or, equivalently, to borrow as &/&mut), or to borrow as raw.
|
2018-10-30 16:46:28 +01:00
|
|
|
fn reactivate(&mut self, bor: Borrow, usage: UsageKind) -> EvalResult<'tcx> {
|
|
|
|
let action = match self.reactivatable(bor, usage) {
|
2018-10-23 15:59:50 +02:00
|
|
|
Ok(action) => action,
|
|
|
|
Err(err) => return err!(MachineError(err)),
|
|
|
|
};
|
2018-10-30 15:08:18 +01:00
|
|
|
// Execute what `reactivatable` told us to do.
|
2018-10-23 15:59:50 +02:00
|
|
|
match action {
|
|
|
|
None => {}, // nothing to do
|
|
|
|
Some(top) => {
|
2018-10-29 19:48:43 +01:00
|
|
|
if self.frozen_since.is_some() {
|
|
|
|
trace!("reactivate: Unfreezing");
|
|
|
|
}
|
2018-10-23 15:59:50 +02:00
|
|
|
self.frozen_since = None;
|
2018-10-29 19:48:43 +01:00
|
|
|
for itm in self.borrows.drain(top+1..).rev() {
|
|
|
|
trace!("reactivate: Popping {:?}", itm);
|
|
|
|
}
|
2018-10-23 15:59:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
2018-10-16 18:01:50 +02:00
|
|
|
}
|
|
|
|
|
2018-10-19 16:07:40 +02:00
|
|
|
/// Initiate `bor`; mostly this means freezing or pushing.
|
2018-10-29 19:48:43 +01:00
|
|
|
/// This operation cannot fail; it is up to the caller to ensure that the precondition
|
|
|
|
/// is met: We cannot push onto frozen stacks.
|
|
|
|
fn initiate(&mut self, bor: Borrow) {
|
2018-10-16 18:01:50 +02:00
|
|
|
match bor {
|
|
|
|
Borrow::Frz(t) => {
|
|
|
|
match self.frozen_since {
|
2018-10-19 16:07:40 +02:00
|
|
|
None => {
|
|
|
|
trace!("initiate: Freezing");
|
|
|
|
self.frozen_since = Some(t);
|
|
|
|
}
|
|
|
|
Some(since) => {
|
|
|
|
trace!("initiate: Already frozen");
|
|
|
|
assert!(since <= t);
|
|
|
|
}
|
2018-10-16 18:01:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Borrow::Mut(m) => {
|
|
|
|
match self.frozen_since {
|
2018-10-19 16:07:40 +02:00
|
|
|
None => {
|
|
|
|
trace!("initiate: Pushing {:?}", bor);
|
|
|
|
self.borrows.push(BorStackItem::Mut(m))
|
|
|
|
}
|
|
|
|
Some(_) if m.is_raw() =>
|
|
|
|
// We only ever initiate right after activating the ref we come from.
|
|
|
|
// If the source ref is fine being frozen, then a raw ref we create
|
|
|
|
// from it is fine with this as well.
|
|
|
|
trace!("initiate: Initiating a raw on a frozen location, not doing a thing"),
|
2018-10-16 18:01:50 +02:00
|
|
|
Some(_) =>
|
2018-10-29 19:48:43 +01:00
|
|
|
bug!("Trying to mutate frozen location")
|
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-10-30 16:46:28 +01:00
|
|
|
/// The single most operation: Make sure that using `ptr` as `usage` is okay,
|
2018-10-29 19:48:43 +01:00
|
|
|
/// and if `new_bor` is present then make that the new current borrow.
|
|
|
|
fn use_and_maybe_re_borrow(
|
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-29 19:48:43 +01:00
|
|
|
new_bor: Option<Borrow>,
|
2018-10-17 15:15:53 +02:00
|
|
|
) -> EvalResult<'tcx> {
|
2018-10-29 19:48:43 +01:00
|
|
|
trace!("use_and_maybe_re_borrow of tag {:?} as {:?}, new {:?}: {:?}, size {}",
|
2018-10-30 16:46:28 +01:00
|
|
|
ptr.tag, usage, new_bor, 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-10-30 16:46:28 +01:00
|
|
|
stack.reactivate(ptr.tag, usage)?;
|
2018-10-29 19:48:43 +01:00
|
|
|
if let Some(new_bor) = new_bor {
|
|
|
|
stack.initiate(new_bor);
|
2018-10-17 15:15:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2018-10-22 18:01:32 +02:00
|
|
|
|
2018-10-29 19:48:43 +01:00
|
|
|
#[inline(always)]
|
|
|
|
pub fn memory_read(
|
|
|
|
&self,
|
|
|
|
ptr: Pointer<Borrow>,
|
|
|
|
size: Size,
|
|
|
|
) -> EvalResult<'tcx> {
|
|
|
|
// Reads behave exactly like the first half of a reborrow-to-shr
|
2018-10-30 16:46:28 +01:00
|
|
|
self.use_and_maybe_re_borrow(ptr, size, UsageKind::Read, None)
|
2018-10-29 19:48:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn memory_written(
|
|
|
|
&mut self,
|
|
|
|
ptr: Pointer<Borrow>,
|
|
|
|
size: Size,
|
|
|
|
) -> EvalResult<'tcx> {
|
|
|
|
// Writes behave exactly like the first half of a reborrow-to-mut
|
2018-10-30 16:46:28 +01:00
|
|
|
self.use_and_maybe_re_borrow(ptr, size, UsageKind::Write, None)
|
2018-10-29 19:48:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn memory_deallocated(
|
|
|
|
&mut self,
|
|
|
|
ptr: Pointer<Borrow>,
|
|
|
|
size: Size,
|
|
|
|
) -> EvalResult<'tcx> {
|
|
|
|
// This is like mutating
|
2018-10-30 16:46:28 +01:00
|
|
|
self.use_and_maybe_re_borrow(ptr, size, UsageKind::Write, None)
|
2018-10-29 19:48:43 +01:00
|
|
|
// FIXME: Error out of there are any barriers?
|
|
|
|
}
|
|
|
|
|
2018-10-22 18:01:32 +02:00
|
|
|
/// Pushes the first borrow to the stacks, must be a mutable one.
|
|
|
|
pub fn first_borrow(
|
|
|
|
&mut self,
|
2018-10-30 10:41:01 +01:00
|
|
|
mut_borrow: Mut,
|
2018-10-22 18:01:32 +02:00
|
|
|
size: Size
|
|
|
|
) {
|
|
|
|
for stack in self.stacks.get_mut().iter_mut(Size::ZERO, size) {
|
|
|
|
assert!(stack.borrows.len() == 1 && stack.frozen_since.is_none());
|
|
|
|
assert_eq!(stack.borrows.pop().unwrap(), BorStackItem::Mut(Mut::Raw));
|
2018-10-30 10:41:01 +01:00
|
|
|
stack.borrows.push(BorStackItem::Mut(mut_borrow));
|
2018-10-22 18:01:32 +02:00
|
|
|
}
|
|
|
|
}
|
2018-10-17 15:15:53 +02:00
|
|
|
}
|
|
|
|
|
2018-10-16 18:01:50 +02:00
|
|
|
pub trait EvalContextExt<'tcx> {
|
|
|
|
fn tag_reference(
|
2018-10-26 11:31:20 +02:00
|
|
|
&mut self,
|
2018-10-16 18:01:50 +02:00
|
|
|
ptr: Pointer<Borrow>,
|
|
|
|
pointee_ty: Ty<'tcx>,
|
|
|
|
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-19 16:07:40 +02:00
|
|
|
|
2018-10-16 18:01:50 +02:00
|
|
|
fn tag_dereference(
|
|
|
|
&self,
|
|
|
|
ptr: Pointer<Borrow>,
|
2018-10-18 16:59:08 +02:00
|
|
|
pointee_ty: Ty<'tcx>,
|
|
|
|
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
|
|
|
|
|
|
|
fn retag(
|
|
|
|
&mut self,
|
|
|
|
fn_entry: bool,
|
|
|
|
place: PlaceTy<'tcx, Borrow>
|
|
|
|
) -> EvalResult<'tcx>;
|
2018-10-16 18:01:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'mir, 'tcx> EvalContextExt<'tcx> for super::MiriEvalContext<'a, 'mir, 'tcx> {
|
2018-10-26 11:31:20 +02:00
|
|
|
/// Called for place-to-value conversion.
|
|
|
|
fn tag_reference(
|
|
|
|
&mut self,
|
|
|
|
ptr: Pointer<Borrow>,
|
2018-10-16 18:01:50 +02:00
|
|
|
pointee_ty: Ty<'tcx>,
|
2018-10-26 11:31:20 +02:00
|
|
|
size: Size,
|
2018-10-30 16:46:28 +01:00
|
|
|
usage: UsageKind,
|
2018-10-26 11:31:20 +02:00
|
|
|
) -> EvalResult<'tcx, Borrow> {
|
2018-10-16 18:01:50 +02:00
|
|
|
let time = self.machine.stacked_borrows.increment_clock();
|
2018-10-30 16:46:28 +01:00
|
|
|
let new_bor = match usage {
|
|
|
|
UsageKind::Write => Borrow::Mut(Mut::Uniq(time)),
|
|
|
|
UsageKind::Read =>
|
2018-10-22 12:51:30 +02:00
|
|
|
// FIXME This does not do enough checking when only part of the data has
|
|
|
|
// interior mutability. When the type is `(i32, Cell<i32>)`, we want the
|
|
|
|
// first field to be frozen but not the second.
|
2018-10-16 18:01:50 +02:00
|
|
|
if self.type_is_freeze(pointee_ty) {
|
|
|
|
Borrow::Frz(time)
|
|
|
|
} else {
|
2018-10-18 16:59:08 +02:00
|
|
|
// Shared reference with interior mutability.
|
2018-10-16 18:01:50 +02:00
|
|
|
Borrow::Mut(Mut::Raw)
|
2018-10-17 15:15:53 +02:00
|
|
|
},
|
2018-10-30 16:46:28 +01:00
|
|
|
UsageKind::Raw => Borrow::Mut(Mut::Raw),
|
2018-10-26 11:31:20 +02:00
|
|
|
};
|
2018-10-17 15:15:53 +02:00
|
|
|
trace!("tag_reference: Creating new reference ({:?}) for {:?} (pointee {}, size {}): {:?}",
|
2018-10-30 16:46:28 +01:00
|
|
|
usage, ptr, pointee_ty, size.bytes(), new_bor);
|
2018-10-16 18:01:50 +02:00
|
|
|
|
|
|
|
// Make sure this reference is not dangling or so
|
2018-10-19 19:51:41 +02:00
|
|
|
self.memory().check_bounds(ptr, size, false)?;
|
2018-10-16 18:01:50 +02:00
|
|
|
|
|
|
|
// Update the stacks. We cannot use `get_mut` becuse this might be immutable
|
|
|
|
// memory.
|
2018-10-19 19:51:41 +02:00
|
|
|
let alloc = self.memory().get(ptr.alloc_id).expect("We checked that the ptr is fine!");
|
2018-10-30 16:46:28 +01:00
|
|
|
alloc.extra.use_and_maybe_re_borrow(ptr, size, usage, Some(new_bor))?;
|
2018-10-16 18:01:50 +02:00
|
|
|
|
|
|
|
Ok(new_bor)
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
ptr: Pointer<Borrow>,
|
2018-10-29 19:48:43 +01:00
|
|
|
pointee_ty: Ty<'tcx>,
|
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-29 19:48:43 +01:00
|
|
|
trace!("tag_reference: Accessing reference ({:?}) for {:?} (pointee {}, size {})",
|
2018-10-30 16:46:28 +01:00
|
|
|
usage, ptr, pointee_ty, size.bytes());
|
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-10-22 18:01:32 +02:00
|
|
|
// Don't use the tag, this is a raw access! Even if there is a tag,
|
|
|
|
// that means transmute happened and we ignore the tag.
|
|
|
|
// Also don't do any further validation, this is raw after all.
|
|
|
|
return Ok(Borrow::Mut(Mut::Raw));
|
|
|
|
}
|
2018-10-30 16:46:28 +01:00
|
|
|
(UsageKind::Write, Borrow::Mut(Mut::Uniq(_))) |
|
|
|
|
(UsageKind::Read, Borrow::Frz(_)) |
|
|
|
|
(UsageKind::Read, Borrow::Mut(Mut::Raw)) => {
|
2018-10-19 16:07:40 +02:00
|
|
|
// Expected combinations. Nothing to do.
|
|
|
|
// FIXME: We probably shouldn't accept this if we got a raw shr without
|
|
|
|
// interior mutability.
|
|
|
|
}
|
2018-10-30 16:46:28 +01:00
|
|
|
(UsageKind::Write, Borrow::Mut(Mut::Raw)) => {
|
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-10-30 16:46:28 +01:00
|
|
|
(UsageKind::Read, Borrow::Mut(Mut::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-10-30 16:46:28 +01:00
|
|
|
(UsageKind::Write, Borrow::Frz(_)) => {
|
2018-10-19 16:07:40 +02:00
|
|
|
// This is just invalid.
|
|
|
|
// 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)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Even if we don't touch the tag, this operation is only okay if we *could*
|
|
|
|
// activate it. Also it must not be dangling.
|
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-10-19 16:07:40 +02:00
|
|
|
let mut stacks = alloc.extra.stacks.borrow_mut();
|
|
|
|
// We need `iter_mut` because `iter` would skip gaps!
|
|
|
|
for stack in stacks.iter_mut(ptr.offset, size) {
|
2018-10-30 15:08:18 +01:00
|
|
|
// Conservatively assume that we will only read.
|
2018-10-30 16:46:28 +01:00
|
|
|
if let Err(err) = stack.reactivatable(ptr.tag, UsageKind::Read) {
|
|
|
|
return err!(MachineError(format!(
|
|
|
|
"Encountered {} reference with non-reactivatable tag: {}",
|
|
|
|
if usage == UsageKind::Write { "mutable" } else { "shared" },
|
|
|
|
err
|
|
|
|
)))
|
2018-10-19 16:07:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// All is good.
|
|
|
|
Ok(ptr.tag)
|
2018-10-16 18:01:50 +02:00
|
|
|
}
|
2018-10-22 18:01:32 +02:00
|
|
|
|
|
|
|
fn tag_new_allocation(
|
|
|
|
&mut self,
|
|
|
|
id: AllocId,
|
|
|
|
kind: MemoryKind<MiriMemoryKind>,
|
|
|
|
) -> Borrow {
|
2018-10-30 10:41:01 +01:00
|
|
|
let mut_borrow = match kind {
|
2018-10-22 18:01:32 +02:00
|
|
|
MemoryKind::Stack => {
|
2018-10-30 15:08:18 +01:00
|
|
|
// 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
|
2018-10-22 18:01:32 +02:00
|
|
|
let time = self.machine.stacked_borrows.increment_clock();
|
|
|
|
Mut::Uniq(time)
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
// Raw for everything else
|
|
|
|
Mut::Raw
|
|
|
|
}
|
|
|
|
};
|
|
|
|
// 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);
|
2018-10-30 10:41:01 +01:00
|
|
|
alloc.extra.first_borrow(mut_borrow, size);
|
|
|
|
Borrow::Mut(mut_borrow)
|
2018-10-22 18:01:32 +02:00
|
|
|
}
|
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.
|
|
|
|
let mutbl = match place.layout.ty.sty {
|
|
|
|
ty::Ref(_, _, mutbl) => mutbl, // go ahead
|
|
|
|
_ => return Ok(()), // don't do a thing
|
|
|
|
};
|
|
|
|
// We want to reborrow the reference stored there. This will call the hooks
|
2018-10-29 19:48:43 +01:00
|
|
|
// above. First deref, which will call `tag_dereference`.
|
2018-10-26 11:31:20 +02:00
|
|
|
// (This is somewhat redundant because validation already did the same thing,
|
|
|
|
// but what can you do.)
|
2018-11-05 08:51:55 +01:00
|
|
|
let val = self.read_immediate(self.place_to_op(place)?)?;
|
2018-10-26 11:31:20 +02:00
|
|
|
let dest = self.ref_to_mplace(val)?;
|
2018-10-29 19:48:43 +01:00
|
|
|
// Now put a new ref into the old place, which will call `tag_reference`.
|
2018-10-26 11:31:20 +02:00
|
|
|
// FIXME: Honor `fn_entry`!
|
|
|
|
let val = self.create_ref(dest, Some(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
|
|
|
}
|