Merge pull request #695 from RalfJung/stacked-borrows-2

Stacked borrows 2 (alpha 1)
This commit is contained in:
Ralf Jung 2019-04-18 10:15:37 +02:00 committed by GitHub
commit ae9e9cb47c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
56 changed files with 788 additions and 753 deletions

View file

@ -1 +1 @@
ee621f42329069c296b4c2066b3743cc4ff0f369
efe2f32a6b8217425f361ec7c206910c611c03ee

View file

@ -13,8 +13,8 @@ pub trait EvalContextExt<'a, 'mir, 'tcx: 'a + 'mir>: crate::MiriEvalContextExt<'
fn find_fn(
&mut self,
instance: ty::Instance<'tcx>,
args: &[OpTy<'tcx, Borrow>],
dest: Option<PlaceTy<'tcx, Borrow>>,
args: &[OpTy<'tcx, Tag>],
dest: Option<PlaceTy<'tcx, Tag>>,
ret: Option<mir::BasicBlock>,
) -> EvalResult<'tcx, Option<&'mir mir::Mir<'tcx>>> {
let this = self.eval_context_mut();
@ -55,8 +55,8 @@ pub trait EvalContextExt<'a, 'mir, 'tcx: 'a + 'mir>: crate::MiriEvalContextExt<'
fn emulate_foreign_item(
&mut self,
def_id: DefId,
args: &[OpTy<'tcx, Borrow>],
dest: Option<PlaceTy<'tcx, Borrow>>,
args: &[OpTy<'tcx, Tag>],
dest: Option<PlaceTy<'tcx, Tag>>,
ret: Option<mir::BasicBlock>,
) -> EvalResult<'tcx> {
let this = self.eval_context_mut();
@ -92,7 +92,7 @@ pub trait EvalContextExt<'a, 'mir, 'tcx: 'a + 'mir>: crate::MiriEvalContextExt<'
} else {
let align = this.tcx.data_layout.pointer_align.abi;
let ptr = this.memory_mut().allocate(Size::from_bytes(size), align, MiriMemoryKind::C.into());
this.write_scalar(Scalar::Ptr(ptr.with_default_tag()), dest)?;
this.write_scalar(Scalar::Ptr(ptr), dest)?;
}
}
"calloc" => {
@ -105,7 +105,7 @@ pub trait EvalContextExt<'a, 'mir, 'tcx: 'a + 'mir>: crate::MiriEvalContextExt<'
} else {
let size = Size::from_bytes(bytes);
let align = this.tcx.data_layout.pointer_align.abi;
let ptr = this.memory_mut().allocate(size, align, MiriMemoryKind::C.into()).with_default_tag();
let ptr = this.memory_mut().allocate(size, align, MiriMemoryKind::C.into());
this.memory_mut().get_mut(ptr.alloc_id)?.write_repeat(tcx, ptr, 0, size)?;
this.write_scalar(Scalar::Ptr(ptr), dest)?;
}
@ -132,7 +132,7 @@ pub trait EvalContextExt<'a, 'mir, 'tcx: 'a + 'mir>: crate::MiriEvalContextExt<'
Align::from_bytes(align).unwrap(),
MiriMemoryKind::C.into()
);
this.write_scalar(Scalar::Ptr(ptr.with_default_tag()), ret.into())?;
this.write_scalar(Scalar::Ptr(ptr), ret.into())?;
}
this.write_null(dest)?;
}
@ -162,8 +162,7 @@ pub trait EvalContextExt<'a, 'mir, 'tcx: 'a + 'mir>: crate::MiriEvalContextExt<'
Size::from_bytes(size),
Align::from_bytes(align).unwrap(),
MiriMemoryKind::Rust.into()
)
.with_default_tag();
);
this.write_scalar(Scalar::Ptr(ptr), dest)?;
}
"__rust_alloc_zeroed" => {
@ -180,8 +179,7 @@ pub trait EvalContextExt<'a, 'mir, 'tcx: 'a + 'mir>: crate::MiriEvalContextExt<'
Size::from_bytes(size),
Align::from_bytes(align).unwrap(),
MiriMemoryKind::Rust.into()
)
.with_default_tag();
);
this.memory_mut()
.get_mut(ptr.alloc_id)?
.write_repeat(tcx, ptr, 0, Size::from_bytes(size))?;
@ -222,7 +220,7 @@ pub trait EvalContextExt<'a, 'mir, 'tcx: 'a + 'mir>: crate::MiriEvalContextExt<'
Align::from_bytes(align).unwrap(),
MiriMemoryKind::Rust.into(),
)?;
this.write_scalar(Scalar::Ptr(new_ptr.with_default_tag()), dest)?;
this.write_scalar(Scalar::Ptr(new_ptr), dest)?;
}
"syscall" => {
@ -428,7 +426,7 @@ pub trait EvalContextExt<'a, 'mir, 'tcx: 'a + 'mir>: crate::MiriEvalContextExt<'
Size::from_bytes((value.len() + 1) as u64),
Align::from_bytes(1).unwrap(),
MiriMemoryKind::Env.into(),
).with_default_tag();
);
{
let alloc = this.memory_mut().get_mut(value_copy.alloc_id)?;
alloc.write_bytes(tcx, value_copy, &value)?;
@ -798,13 +796,13 @@ pub trait EvalContextExt<'a, 'mir, 'tcx: 'a + 'mir>: crate::MiriEvalContextExt<'
Ok(())
}
fn write_null(&mut self, dest: PlaceTy<'tcx, Borrow>) -> EvalResult<'tcx> {
fn write_null(&mut self, dest: PlaceTy<'tcx, Tag>) -> EvalResult<'tcx> {
self.eval_context_mut().write_scalar(Scalar::from_int(0, dest.layout.size), dest)
}
/// Evaluates the scalar at the specified path. Returns Some(val)
/// if the path could be resolved, and None otherwise
fn eval_path_scalar(&mut self, path: &[&str]) -> EvalResult<'tcx, Option<ScalarMaybeUndef<stacked_borrows::Borrow>>> {
fn eval_path_scalar(&mut self, path: &[&str]) -> EvalResult<'tcx, Option<ScalarMaybeUndef<Tag>>> {
let this = self.eval_context_mut();
if let Ok(instance) = this.resolve_path(path) {
let cid = GlobalId {

View file

@ -47,9 +47,9 @@ pub trait EvalContextExt<'a, 'mir, 'tcx: 'a + 'mir>: crate::MiriEvalContextExt<'
/// will be true if this is frozen, false if this is in an `UnsafeCell`.
fn visit_freeze_sensitive(
&self,
place: MPlaceTy<'tcx, Borrow>,
place: MPlaceTy<'tcx, Tag>,
size: Size,
mut action: impl FnMut(Pointer<Borrow>, Size, bool) -> EvalResult<'tcx>,
mut action: impl FnMut(Pointer<Tag>, Size, bool) -> EvalResult<'tcx>,
) -> EvalResult<'tcx> {
let this = self.eval_context_ref();
trace!("visit_frozen(place={:?}, size={:?})", *place, size);
@ -64,7 +64,7 @@ pub trait EvalContextExt<'a, 'mir, 'tcx: 'a + 'mir>: crate::MiriEvalContextExt<'
let mut end_ptr = place.ptr;
// Called when we detected an `UnsafeCell` at the given offset and size.
// Calls `action` and advances `end_ptr`.
let mut unsafe_cell_action = |unsafe_cell_ptr: Scalar<Borrow>, unsafe_cell_size: Size| {
let mut unsafe_cell_action = |unsafe_cell_ptr: Scalar<Tag>, unsafe_cell_size: Size| {
if unsafe_cell_size != Size::ZERO {
debug_assert_eq!(unsafe_cell_ptr.to_ptr().unwrap().alloc_id,
end_ptr.to_ptr().unwrap().alloc_id);
@ -120,7 +120,7 @@ pub trait EvalContextExt<'a, 'mir, 'tcx: 'a + 'mir>: crate::MiriEvalContextExt<'
/// Visiting the memory covered by a `MemPlace`, being aware of
/// whether we are inside an `UnsafeCell` or not.
struct UnsafeCellVisitor<'ecx, 'a, 'mir, 'tcx, F>
where F: FnMut(MPlaceTy<'tcx, Borrow>) -> EvalResult<'tcx>
where F: FnMut(MPlaceTy<'tcx, Tag>) -> EvalResult<'tcx>
{
ecx: &'ecx MiriEvalContext<'a, 'mir, 'tcx>,
unsafe_cell_action: F,
@ -131,9 +131,9 @@ pub trait EvalContextExt<'a, 'mir, 'tcx: 'a + 'mir>: crate::MiriEvalContextExt<'
for
UnsafeCellVisitor<'ecx, 'a, 'mir, 'tcx, F>
where
F: FnMut(MPlaceTy<'tcx, Borrow>) -> EvalResult<'tcx>
F: FnMut(MPlaceTy<'tcx, Tag>) -> EvalResult<'tcx>
{
type V = MPlaceTy<'tcx, Borrow>;
type V = MPlaceTy<'tcx, Tag>;
#[inline(always)]
fn ecx(&self) -> &MiriEvalContext<'a, 'mir, 'tcx> {
@ -141,7 +141,7 @@ pub trait EvalContextExt<'a, 'mir, 'tcx: 'a + 'mir>: crate::MiriEvalContextExt<'
}
// Hook to detect `UnsafeCell`.
fn visit_value(&mut self, v: MPlaceTy<'tcx, Borrow>) -> EvalResult<'tcx>
fn visit_value(&mut self, v: MPlaceTy<'tcx, Tag>) -> EvalResult<'tcx>
{
trace!("UnsafeCellVisitor: {:?} {:?}", *v, v.layout.ty);
let is_unsafe_cell = match v.layout.ty.sty {
@ -163,8 +163,8 @@ pub trait EvalContextExt<'a, 'mir, 'tcx: 'a + 'mir>: crate::MiriEvalContextExt<'
// Make sure we visit aggregrates in increasing offset order.
fn visit_aggregate(
&mut self,
place: MPlaceTy<'tcx, Borrow>,
fields: impl Iterator<Item=EvalResult<'tcx, MPlaceTy<'tcx, Borrow>>>,
place: MPlaceTy<'tcx, Tag>,
fields: impl Iterator<Item=EvalResult<'tcx, MPlaceTy<'tcx, Tag>>>,
) -> EvalResult<'tcx> {
match place.layout.fields {
layout::FieldPlacement::Array { .. } => {
@ -174,7 +174,7 @@ pub trait EvalContextExt<'a, 'mir, 'tcx: 'a + 'mir>: crate::MiriEvalContextExt<'
}
layout::FieldPlacement::Arbitrary { .. } => {
// Gather the subplaces and sort them before visiting.
let mut places = fields.collect::<EvalResult<'tcx, Vec<MPlaceTy<'tcx, Borrow>>>>()?;
let mut places = fields.collect::<EvalResult<'tcx, Vec<MPlaceTy<'tcx, Tag>>>>()?;
places.sort_by_key(|place| place.ptr.get_ptr_offset(self.ecx()));
self.walk_aggregate(place, places.into_iter().map(Ok))
}
@ -186,7 +186,7 @@ pub trait EvalContextExt<'a, 'mir, 'tcx: 'a + 'mir>: crate::MiriEvalContextExt<'
}
// We have to do *something* for unions.
fn visit_union(&mut self, v: MPlaceTy<'tcx, Borrow>) -> EvalResult<'tcx>
fn visit_union(&mut self, v: MPlaceTy<'tcx, Tag>) -> EvalResult<'tcx>
{
// With unions, we fall back to whatever the type says, to hopefully be consistent
// with LLVM IR.
@ -200,7 +200,7 @@ pub trait EvalContextExt<'a, 'mir, 'tcx: 'a + 'mir>: crate::MiriEvalContextExt<'
}
// We should never get to a primitive, but always short-circuit somewhere above.
fn visit_primitive(&mut self, _v: MPlaceTy<'tcx, Borrow>) -> EvalResult<'tcx>
fn visit_primitive(&mut self, _v: MPlaceTy<'tcx, Tag>) -> EvalResult<'tcx>
{
bug!("we should always short-circuit before coming to a primitive")
}

View file

@ -4,7 +4,7 @@ use rustc::ty::layout::{self, LayoutOf, Size};
use rustc::ty;
use crate::{
PlaceTy, OpTy, ImmTy, Immediate, Scalar, ScalarMaybeUndef, Borrow,
PlaceTy, OpTy, ImmTy, Immediate, Scalar, ScalarMaybeUndef, Tag,
OperatorEvalContextExt
};
@ -13,8 +13,8 @@ pub trait EvalContextExt<'a, 'mir, 'tcx: 'a+'mir>: crate::MiriEvalContextExt<'a,
fn call_intrinsic(
&mut self,
instance: ty::Instance<'tcx>,
args: &[OpTy<'tcx, Borrow>],
dest: PlaceTy<'tcx, Borrow>,
args: &[OpTy<'tcx, Tag>],
dest: PlaceTy<'tcx, Tag>,
) -> EvalResult<'tcx> {
let this = self.eval_context_mut();
if this.emulate_intrinsic(instance, args, dest)? {

View file

@ -23,6 +23,7 @@ mod stacked_borrows;
use std::collections::HashMap;
use std::borrow::Cow;
use std::rc::Rc;
use rand::rngs::StdRng;
use rand::SeedableRng;
@ -48,7 +49,7 @@ use crate::mono_hash_map::MonoHashMap;
pub use crate::stacked_borrows::{EvalContextExt as StackedBorEvalContextExt};
// Used by priroda.
pub use crate::stacked_borrows::{Borrow, Stack, Stacks, BorStackItem};
pub use crate::stacked_borrows::{Tag, Permission, Stack, Stacks, Item};
/// Insert rustc arguments at the beginning of the argument list that Miri wants to be
/// set per default, for maximal validation power.
@ -155,7 +156,7 @@ pub fn create_ecx<'a, 'mir: 'a, 'tcx: 'mir>(
// Don't forget `0` terminator.
cmd.push(std::char::from_u32(0).unwrap());
// Collect the pointers to the individual strings.
let mut argvs = Vec::<Pointer<Borrow>>::new();
let mut argvs = Vec::<Pointer<Tag>>::new();
for arg in config.args {
// Add `0` terminator.
let mut arg = arg.into_bytes();
@ -187,7 +188,7 @@ pub fn create_ecx<'a, 'mir: 'a, 'tcx: 'mir>(
Size::from_bytes(cmd_utf16.len() as u64 * 2),
Align::from_bytes(2).unwrap(),
MiriMemoryKind::Env.into(),
).with_default_tag();
);
ecx.machine.cmd_line = Some(cmd_ptr);
// Store the UTF-16 string.
let char_size = Size::from_bytes(2);
@ -214,7 +215,13 @@ pub fn eval_main<'a, 'tcx: 'a>(
main_id: DefId,
config: MiriConfig,
) {
let mut ecx = create_ecx(tcx, main_id, config).expect("couldn't create ecx");
let mut ecx = match create_ecx(tcx, main_id, config) {
Ok(ecx) => ecx,
Err(mut err) => {
err.print_backtrace();
panic!("Miri initialziation error: {}", err.kind)
}
};
// Perform the main execution.
let res: EvalResult = (|| {
@ -310,14 +317,14 @@ impl MayLeak for MiriMemoryKind {
pub struct Evaluator<'tcx> {
/// Environment variables set by `setenv`.
/// Miri does not expose env vars from the host to the emulated program.
pub(crate) env_vars: HashMap<Vec<u8>, Pointer<Borrow>>,
pub(crate) env_vars: HashMap<Vec<u8>, Pointer<Tag>>,
/// Program arguments (`Option` because we can only initialize them after creating the ecx).
/// These are *pointers* to argc/argv because macOS.
/// We also need the full command line as one string because of Windows.
pub(crate) argc: Option<Pointer<Borrow>>,
pub(crate) argv: Option<Pointer<Borrow>>,
pub(crate) cmd_line: Option<Pointer<Borrow>>,
pub(crate) argc: Option<Pointer<Tag>>,
pub(crate) argv: Option<Pointer<Tag>>,
pub(crate) cmd_line: Option<Pointer<Tag>>,
/// Last OS error.
pub(crate) last_error: u32,
@ -328,9 +335,6 @@ pub struct Evaluator<'tcx> {
/// Whether to enforce the validity invariant.
pub(crate) validate: bool,
/// Stacked Borrows state.
pub(crate) stacked_borrows: stacked_borrows::State,
/// The random number generator to use if Miri
/// is running in non-deterministic mode
pub(crate) rng: Option<StdRng>
@ -346,7 +350,6 @@ impl<'tcx> Evaluator<'tcx> {
last_error: 0,
tls: TlsData::default(),
validate,
stacked_borrows: stacked_borrows::State::default(),
rng: seed.map(|s| StdRng::seed_from_u64(s))
}
}
@ -378,9 +381,9 @@ impl<'a, 'mir, 'tcx> Machine<'a, 'mir, 'tcx> for Evaluator<'tcx> {
type FrameExtra = stacked_borrows::CallId;
type MemoryExtra = stacked_borrows::MemoryState;
type AllocExtra = stacked_borrows::Stacks;
type PointerTag = Borrow;
type PointerTag = Tag;
type MemoryMap = MonoHashMap<AllocId, (MemoryKind<MiriMemoryKind>, Allocation<Borrow, Self::AllocExtra>)>;
type MemoryMap = MonoHashMap<AllocId, (MemoryKind<MiriMemoryKind>, Allocation<Tag, Self::AllocExtra>)>;
const STATIC_KIND: Option<MiriMemoryKind> = Some(MiriMemoryKind::MutStatic);
@ -394,8 +397,8 @@ impl<'a, 'mir, 'tcx> Machine<'a, 'mir, 'tcx> for Evaluator<'tcx> {
fn find_fn(
ecx: &mut InterpretCx<'a, 'mir, 'tcx, Self>,
instance: ty::Instance<'tcx>,
args: &[OpTy<'tcx, Borrow>],
dest: Option<PlaceTy<'tcx, Borrow>>,
args: &[OpTy<'tcx, Tag>],
dest: Option<PlaceTy<'tcx, Tag>>,
ret: Option<mir::BasicBlock>,
) -> EvalResult<'tcx, Option<&'mir mir::Mir<'tcx>>> {
ecx.find_fn(instance, args, dest, ret)
@ -405,8 +408,8 @@ impl<'a, 'mir, 'tcx> Machine<'a, 'mir, 'tcx> for Evaluator<'tcx> {
fn call_intrinsic(
ecx: &mut rustc_mir::interpret::InterpretCx<'a, 'mir, 'tcx, Self>,
instance: ty::Instance<'tcx>,
args: &[OpTy<'tcx, Borrow>],
dest: PlaceTy<'tcx, Borrow>,
args: &[OpTy<'tcx, Tag>],
dest: PlaceTy<'tcx, Tag>,
) -> EvalResult<'tcx> {
ecx.call_intrinsic(instance, args, dest)
}
@ -415,15 +418,15 @@ impl<'a, 'mir, 'tcx> Machine<'a, 'mir, 'tcx> for Evaluator<'tcx> {
fn ptr_op(
ecx: &rustc_mir::interpret::InterpretCx<'a, 'mir, 'tcx, Self>,
bin_op: mir::BinOp,
left: ImmTy<'tcx, Borrow>,
right: ImmTy<'tcx, Borrow>,
) -> EvalResult<'tcx, (Scalar<Borrow>, bool)> {
left: ImmTy<'tcx, Tag>,
right: ImmTy<'tcx, Tag>,
) -> EvalResult<'tcx, (Scalar<Tag>, bool)> {
ecx.ptr_op(bin_op, left, right)
}
fn box_alloc(
ecx: &mut InterpretCx<'a, 'mir, 'tcx, Self>,
dest: PlaceTy<'tcx, Borrow>,
dest: PlaceTy<'tcx, Tag>,
) -> EvalResult<'tcx> {
trace!("box_alloc for {:?}", dest.layout.ty);
// Call the `exchange_malloc` lang item.
@ -467,7 +470,7 @@ impl<'a, 'mir, 'tcx> Machine<'a, 'mir, 'tcx> for Evaluator<'tcx> {
def_id: DefId,
tcx: TyCtxtAt<'a, 'tcx, 'tcx>,
memory_extra: &Self::MemoryExtra,
) -> EvalResult<'tcx, Cow<'tcx, Allocation<Borrow, Self::AllocExtra>>> {
) -> EvalResult<'tcx, Cow<'tcx, Allocation<Tag, Self::AllocExtra>>> {
let attrs = tcx.get_attrs(def_id);
let link_name = match attr::first_attr_value_str_by_name(&attrs, "link_name") {
Some(name) => name.as_str(),
@ -479,7 +482,7 @@ impl<'a, 'mir, 'tcx> Machine<'a, 'mir, 'tcx> for Evaluator<'tcx> {
// This should be all-zero, pointer-sized.
let size = tcx.data_layout.pointer_size;
let data = vec![0; size.bytes() as usize];
let extra = AllocationExtra::memory_allocated(size, memory_extra);
let extra = Stacks::new(size, Tag::default(), Rc::clone(memory_extra));
Allocation::from_bytes(&data, tcx.data_layout.pointer_align.abi, extra)
}
_ => return err!(Unimplemented(
@ -499,16 +502,17 @@ impl<'a, 'mir, 'tcx> Machine<'a, 'mir, 'tcx> for Evaluator<'tcx> {
fn adjust_static_allocation<'b>(
alloc: &'b Allocation,
memory_extra: &Self::MemoryExtra,
) -> Cow<'b, Allocation<Borrow, Self::AllocExtra>> {
let extra = AllocationExtra::memory_allocated(
) -> Cow<'b, Allocation<Tag, Self::AllocExtra>> {
let extra = Stacks::new(
Size::from_bytes(alloc.bytes.len() as u64),
memory_extra,
Tag::default(),
Rc::clone(memory_extra),
);
let alloc: Allocation<Borrow, Self::AllocExtra> = Allocation {
let alloc: Allocation<Tag, Self::AllocExtra> = Allocation {
bytes: alloc.bytes.clone(),
relocations: Relocations::from_presorted(
alloc.relocations.iter()
.map(|&(offset, ((), alloc))| (offset, (Borrow::default(), alloc)))
.map(|&(offset, ((), alloc))| (offset, (Tag::default(), alloc)))
.collect()
),
undef_mask: alloc.undef_mask.clone(),
@ -519,46 +523,30 @@ impl<'a, 'mir, 'tcx> Machine<'a, 'mir, 'tcx> for Evaluator<'tcx> {
Cow::Owned(alloc)
}
fn tag_dereference(
ecx: &InterpretCx<'a, 'mir, 'tcx, Self>,
place: MPlaceTy<'tcx, Borrow>,
mutability: Option<hir::Mutability>,
) -> EvalResult<'tcx, Scalar<Borrow>> {
let size = ecx.size_and_align_of_mplace(place)?.map(|(size, _)| size)
// For extern types, just cover what we can.
.unwrap_or_else(|| place.layout.size);
if !ecx.tcx.sess.opts.debugging_opts.mir_emit_retag ||
!Self::enforce_validity(ecx) || size == Size::ZERO
{
// No tracking.
Ok(place.ptr)
} else {
ecx.ptr_dereference(place, size, mutability.into())?;
// We never change the pointer.
Ok(place.ptr)
}
#[inline(always)]
fn new_allocation(
size: Size,
extra: &Self::MemoryExtra,
kind: MemoryKind<MiriMemoryKind>,
) -> (Self::AllocExtra, Self::PointerTag) {
Stacks::new_allocation(size, extra, kind)
}
#[inline(always)]
fn tag_new_allocation(
ecx: &mut InterpretCx<'a, 'mir, 'tcx, Self>,
ptr: Pointer,
kind: MemoryKind<Self::MemoryKinds>,
) -> Pointer<Borrow> {
if !ecx.machine.validate {
// No tracking.
ptr.with_default_tag()
} else {
let tag = ecx.tag_new_allocation(ptr.alloc_id, kind);
Pointer::new_with_tag(ptr.alloc_id, ptr.offset, tag)
}
fn tag_dereference(
_ecx: &InterpretCx<'a, 'mir, 'tcx, Self>,
place: MPlaceTy<'tcx, Tag>,
_mutability: Option<hir::Mutability>,
) -> EvalResult<'tcx, Scalar<Tag>> {
// Nothing happens.
Ok(place.ptr)
}
#[inline(always)]
fn retag(
ecx: &mut InterpretCx<'a, 'mir, 'tcx, Self>,
kind: mir::RetagKind,
place: PlaceTy<'tcx, Borrow>,
place: PlaceTy<'tcx, Tag>,
) -> EvalResult<'tcx> {
if !ecx.tcx.sess.opts.debugging_opts.mir_emit_retag || !Self::enforce_validity(ecx) {
// No tracking, or no retagging. The latter is possible because a dependency of ours

View file

@ -7,39 +7,39 @@ pub trait EvalContextExt<'tcx> {
fn ptr_op(
&self,
bin_op: mir::BinOp,
left: ImmTy<'tcx, Borrow>,
right: ImmTy<'tcx, Borrow>,
) -> EvalResult<'tcx, (Scalar<Borrow>, bool)>;
left: ImmTy<'tcx, Tag>,
right: ImmTy<'tcx, Tag>,
) -> EvalResult<'tcx, (Scalar<Tag>, bool)>;
fn ptr_int_arithmetic(
&self,
bin_op: mir::BinOp,
left: Pointer<Borrow>,
left: Pointer<Tag>,
right: u128,
signed: bool,
) -> EvalResult<'tcx, (Scalar<Borrow>, bool)>;
) -> EvalResult<'tcx, (Scalar<Tag>, bool)>;
fn ptr_eq(
&self,
left: Scalar<Borrow>,
right: Scalar<Borrow>,
left: Scalar<Tag>,
right: Scalar<Tag>,
) -> EvalResult<'tcx, bool>;
fn pointer_offset_inbounds(
&self,
ptr: Scalar<Borrow>,
ptr: Scalar<Tag>,
pointee_ty: Ty<'tcx>,
offset: i64,
) -> EvalResult<'tcx, Scalar<Borrow>>;
) -> EvalResult<'tcx, Scalar<Tag>>;
}
impl<'a, 'mir, 'tcx> EvalContextExt<'tcx> for super::MiriEvalContext<'a, 'mir, 'tcx> {
fn ptr_op(
&self,
bin_op: mir::BinOp,
left: ImmTy<'tcx, Borrow>,
right: ImmTy<'tcx, Borrow>,
) -> EvalResult<'tcx, (Scalar<Borrow>, bool)> {
left: ImmTy<'tcx, Tag>,
right: ImmTy<'tcx, Tag>,
) -> EvalResult<'tcx, (Scalar<Tag>, bool)> {
use rustc::mir::BinOp::*;
trace!("ptr_op: {:?} {:?} {:?}", *left, bin_op, *right);
@ -136,8 +136,8 @@ impl<'a, 'mir, 'tcx> EvalContextExt<'tcx> for super::MiriEvalContext<'a, 'mir, '
fn ptr_eq(
&self,
left: Scalar<Borrow>,
right: Scalar<Borrow>,
left: Scalar<Tag>,
right: Scalar<Tag>,
) -> EvalResult<'tcx, bool> {
let size = self.pointer_size();
Ok(match (left, right) {
@ -233,13 +233,13 @@ impl<'a, 'mir, 'tcx> EvalContextExt<'tcx> for super::MiriEvalContext<'a, 'mir, '
fn ptr_int_arithmetic(
&self,
bin_op: mir::BinOp,
left: Pointer<Borrow>,
left: Pointer<Tag>,
right: u128,
signed: bool,
) -> EvalResult<'tcx, (Scalar<Borrow>, bool)> {
) -> EvalResult<'tcx, (Scalar<Tag>, bool)> {
use rustc::mir::BinOp::*;
fn map_to_primval((res, over): (Pointer<Borrow>, bool)) -> (Scalar<Borrow>, bool) {
fn map_to_primval((res, over): (Pointer<Tag>, bool)) -> (Scalar<Tag>, bool) {
(Scalar::Ptr(res), over)
}
@ -327,10 +327,10 @@ impl<'a, 'mir, 'tcx> EvalContextExt<'tcx> for super::MiriEvalContext<'a, 'mir, '
/// allocation, and all the remaining integers pointers their own allocation.
fn pointer_offset_inbounds(
&self,
ptr: Scalar<Borrow>,
ptr: Scalar<Tag>,
pointee_ty: Ty<'tcx>,
offset: i64,
) -> EvalResult<'tcx, Scalar<Borrow>> {
) -> EvalResult<'tcx, Scalar<Tag>> {
// FIXME: assuming here that type size is less than `i64::max_value()`.
let pointee_size = self.layout_of(pointee_ty)?.size.bytes() as i64;
let offset = offset

File diff suppressed because it is too large Load diff

View file

@ -5,14 +5,14 @@ use rustc::{ty, ty::layout::HasDataLayout, mir};
use crate::{
EvalResult, InterpError, StackPopCleanup,
MPlaceTy, Scalar, Borrow,
MPlaceTy, Scalar, Tag,
};
pub type TlsKey = u128;
#[derive(Copy, Clone, Debug)]
pub struct TlsEntry<'tcx> {
pub(crate) data: Scalar<Borrow>, // Will eventually become a map from thread IDs to `Scalar`s, if we ever support more than one thread.
pub(crate) data: Scalar<Tag>, // Will eventually become a map from thread IDs to `Scalar`s, if we ever support more than one thread.
pub(crate) dtor: Option<ty::Instance<'tcx>>,
}
@ -63,7 +63,7 @@ impl<'tcx> TlsData<'tcx> {
}
}
pub fn load_tls(&mut self, key: TlsKey) -> EvalResult<'tcx, Scalar<Borrow>> {
pub fn load_tls(&mut self, key: TlsKey) -> EvalResult<'tcx, Scalar<Tag>> {
match self.keys.get(&key) {
Some(&TlsEntry { data, .. }) => {
trace!("TLS key {} loaded: {:?}", key, data);
@ -73,7 +73,7 @@ impl<'tcx> TlsData<'tcx> {
}
}
pub fn store_tls(&mut self, key: TlsKey, new_data: Scalar<Borrow>) -> EvalResult<'tcx> {
pub fn store_tls(&mut self, key: TlsKey, new_data: Scalar<Tag>) -> EvalResult<'tcx> {
match self.keys.get_mut(&key) {
Some(&mut TlsEntry { ref mut data, .. }) => {
trace!("TLS key {} stored: {:?}", key, new_data);
@ -106,7 +106,7 @@ impl<'tcx> TlsData<'tcx> {
&mut self,
key: Option<TlsKey>,
cx: &impl HasDataLayout,
) -> Option<(ty::Instance<'tcx>, Scalar<Borrow>, TlsKey)> {
) -> Option<(ty::Instance<'tcx>, Scalar<Tag>, TlsKey)> {
use std::collections::Bound::*;
let thread_local = &mut self.keys;

View file

@ -9,5 +9,5 @@ fn main() {
retarget(&mut target_alias, target);
// now `target_alias` points to the same thing as `target`
*target = 13;
let _val = *target_alias; //~ ERROR does not exist on the borrow stack
let _val = *target_alias; //~ ERROR borrow stack
}

View file

@ -1,6 +1,6 @@
use std::mem;
pub fn safe(_x: &mut i32, _y: &mut i32) {} //~ ERROR barrier
pub fn safe(_x: &mut i32, _y: &mut i32) {} //~ ERROR protect
fn main() {
let mut x = 0;

View file

@ -1,6 +1,6 @@
use std::mem;
pub fn safe(_x: &i32, _y: &mut i32) {} //~ ERROR barrier
pub fn safe(_x: &i32, _y: &mut i32) {} //~ ERROR protect
fn main() {
let mut x = 0;

View file

@ -1,6 +1,6 @@
use std::mem;
pub fn safe(_x: &mut i32, _y: &i32) {} //~ ERROR does not exist on the borrow stack
pub fn safe(_x: &mut i32, _y: &i32) {} //~ ERROR borrow stack
fn main() {
let mut x = 0;

View file

@ -2,7 +2,7 @@ use std::mem;
use std::cell::Cell;
// Make sure &mut UnsafeCell also is exclusive
pub fn safe(_x: &i32, _y: &mut Cell<i32>) {} //~ ERROR barrier
pub fn safe(_x: &i32, _y: &mut Cell<i32>) {} //~ ERROR protect
fn main() {
let mut x = 0;

View file

@ -8,7 +8,7 @@ fn demo_mut_advanced_unique(mut our: Box<i32>) -> i32 {
unknown_code_2();
// We know this will return 5
*our //~ ERROR does not exist on the borrow stack
*our //~ ERROR borrow stack
}
// Now comes the evil context

View file

@ -13,5 +13,5 @@ fn main() {
let v1 = safe::as_mut_slice(&v);
let _v2 = safe::as_mut_slice(&v);
v1[1] = 5;
//~^ ERROR does not exist on the borrow stack
//~^ ERROR borrow stack
}

View file

@ -9,7 +9,7 @@ mod safe {
assert!(mid <= len);
(from_raw_parts_mut(ptr, len - mid), // BUG: should be "mid" instead of "len - mid"
//~^ ERROR does not exist on the borrow stack
//~^ ERROR borrow stack
from_raw_parts_mut(ptr.offset(mid as isize), len - mid))
}
}

View file

@ -1,4 +1,4 @@
// error-pattern: deallocating with active barrier
// error-pattern: deallocating with active protect
fn inner(x: &mut i32, f: fn(&mut i32)) {
// `f` may mutate, but it may not deallocate!

View file

@ -7,7 +7,7 @@ fn main() {
let xref = unsafe { &mut *xraw }; // derived from raw, so using raw is still ok...
callee(xraw);
let _val = *xref; // ...but any use of raw will invalidate our ref.
//~^ ERROR: does not exist on the borrow stack
//~^ ERROR: borrow stack
}
fn callee(xraw: *mut i32) {

View file

@ -7,7 +7,7 @@ fn main() {
let xref = unsafe { &mut *xraw }; // derived from raw, so using raw is still ok...
callee(xraw);
let _val = *xref; // ...but any use of raw will invalidate our ref.
//~^ ERROR: does not exist on the borrow stack
//~^ ERROR: borrow stack
}
fn callee(xraw: *mut i32) {

View file

@ -15,7 +15,7 @@ fn main() {
callee(xref1_sneaky);
// ... though any use of it will invalidate our ref.
let _val = *xref2;
//~^ ERROR: does not exist on the borrow stack
//~^ ERROR: borrow stack
}
fn callee(xref1: usize) {

View file

@ -5,5 +5,5 @@ fn main() {
let xraw = xref1 as *mut _;
let xref2 = unsafe { &mut *xraw };
let _val = unsafe { *xraw }; // use the raw again, this invalidates xref2 *even* with the special read except for uniq refs
let _illegal = *xref2; //~ ERROR does not exist on the borrow stack
let _illegal = *xref2; //~ ERROR borrow stack
}

View file

@ -12,5 +12,5 @@ fn main() {
let _val = *xref; // we can even still use our mutable reference
mem::forget(unsafe { ptr::read(xshr) }); // but after reading through the shared ref
let _val = *xref; // the mutable one is dead and gone
//~^ ERROR does not exist on the borrow stack
//~^ ERROR borrow stack
}

View file

@ -0,0 +1,8 @@
// Creating a shared reference does not leak the data to raw pointers.
fn main() { unsafe {
let x = &mut 0;
let raw = x as *mut _;
let x = &mut *x; // kill `raw`
let _y = &*x; // this should not activate `raw` again
let _val = *raw; //~ ERROR borrow stack
} }

View file

@ -5,5 +5,5 @@ fn main() {
let x : *mut u32 = xref as *const _ as *mut _;
unsafe { *x = 42; } // invalidates shared ref, activates raw
}
let _x = *xref; //~ ERROR is not frozen
let _x = *xref; //~ ERROR borrow stack
}

View file

@ -3,6 +3,6 @@ fn main() {
let target2 = target as *mut _;
drop(&mut *target); // reborrow
// Now make sure our ref is still the only one.
unsafe { *target2 = 13; } //~ ERROR does not exist on the borrow stack
unsafe { *target2 = 13; } //~ ERROR borrow stack
let _val = *target;
}

View file

@ -3,6 +3,6 @@ fn main() {
// Make sure raw ptr with raw tag cannot mutate frozen location without breaking the shared ref.
let r#ref = &target; // freeze
let ptr = r#ref as *const _ as *mut _; // raw ptr, with raw tag
unsafe { *ptr = 42; } //~ ERROR does not exist on the borrow stack
unsafe { *ptr = 42; } //~ ERROR borrow stack
let _val = *r#ref;
}

View file

@ -9,5 +9,5 @@ fn main() {
let ptr = reference as *const _ as *mut i32; // raw ptr, with raw tag
let _mut_ref: &mut i32 = unsafe { mem::transmute(ptr) }; // &mut, with raw tag
// Now we retag, making our ref top-of-stack -- and, in particular, unfreezing.
let _val = *reference; //~ ERROR is not frozen
let _val = *reference; //~ ERROR borrow stack
}

View file

@ -8,7 +8,7 @@ fn main() {
callee(xraw);
// ... though any use of raw value will invalidate our ref.
let _val = *xref;
//~^ ERROR: does not exist on the borrow stack
//~^ ERROR: borrow stack
}
fn callee(xraw: *mut i32) {

View file

@ -2,7 +2,7 @@ fn inner(x: *mut i32, _y: &mut i32) {
// If `x` and `y` alias, retagging is fine with this... but we really
// shouldn't be allowed to use `x` at all because `y` was assumed to be
// unique for the duration of this call.
let _val = unsafe { *x }; //~ ERROR barrier
let _val = unsafe { *x }; //~ ERROR protect
}
fn main() {

View file

@ -2,7 +2,7 @@ fn inner(x: *mut i32, _y: &i32) {
// If `x` and `y` alias, retagging is fine with this... but we really
// shouldn't be allowed to write to `x` at all because `y` was assumed to be
// immutable for the duration of this call.
unsafe { *x = 0 }; //~ ERROR barrier
unsafe { *x = 0 }; //~ ERROR protect
}
fn main() {

View file

@ -5,5 +5,5 @@ fn main() {
let xref = unsafe { &mut *xraw };
let xref_in_mem = Box::new(xref);
let _val = unsafe { *xraw }; // invalidate xref
let _val = *xref_in_mem; //~ ERROR does not exist on the borrow stack
let _val = *xref_in_mem; //~ ERROR borrow stack
}

View file

@ -5,5 +5,5 @@ fn main() {
let xref = unsafe { &*xraw };
let xref_in_mem = Box::new(xref);
unsafe { *xraw = 42 }; // unfreeze
let _val = *xref_in_mem; //~ ERROR is not frozen
let _val = *xref_in_mem; //~ ERROR borrow stack
}

View file

@ -21,7 +21,7 @@ fn unknown_code_1(x: &i32) { unsafe {
} }
fn unknown_code_2() { unsafe {
*LEAK = 7; //~ ERROR barrier
*LEAK = 7; //~ ERROR borrow stack
} }
fn main() {

View file

@ -3,7 +3,7 @@ fn main() {
let y: *const i32 = &x;
x = 1; // this invalidates y by reactivating the lowermost uniq borrow for this local
assert_eq!(unsafe { *y }, 1); //~ ERROR does not exist on the borrow stack
assert_eq!(unsafe { *y }, 1); //~ ERROR borrow stack
assert_eq!(x, 1);
}

View file

@ -6,5 +6,5 @@ fn main() {
let xraw = x as *mut _;
let xref = unsafe { &mut *xraw };
let _val = unsafe { *xraw }; // invalidate xref
foo(xref); //~ ERROR does not exist on the borrow stack
foo(xref); //~ ERROR borrow stack
}

View file

@ -6,5 +6,5 @@ fn main() {
let xraw = x as *mut _;
let xref = unsafe { &*xraw };
unsafe { *xraw = 42 }; // unfreeze
foo(xref); //~ ERROR is not frozen
foo(xref); //~ ERROR borrow stack
}

View file

@ -8,7 +8,7 @@ fn fun1(x: &mut u8) {
fn fun2() {
// Now we use a pointer we are not allowed to use
let _x = unsafe { *PTR }; //~ ERROR does not exist on the borrow stack
let _x = unsafe { *PTR }; //~ ERROR borrow stack
}
fn main() {

View file

@ -3,7 +3,7 @@ fn foo(x: &mut (i32, i32)) -> &mut i32 {
let xraw = x as *mut (i32, i32);
let ret = unsafe { &mut (*xraw).1 };
let _val = unsafe { *xraw }; // invalidate xref
ret //~ ERROR does not exist on the borrow stack
ret //~ ERROR borrow stack
}
fn main() {

View file

@ -3,7 +3,7 @@ fn foo(x: &mut (i32, i32)) -> Option<&mut i32> {
let xraw = x as *mut (i32, i32);
let ret = Some(unsafe { &mut (*xraw).1 });
let _val = unsafe { *xraw }; // invalidate xref
ret //~ ERROR does not exist on the borrow stack
ret //~ ERROR borrow stack
}
fn main() {

View file

@ -3,7 +3,7 @@ fn foo(x: &mut (i32, i32)) -> (&mut i32,) {
let xraw = x as *mut (i32, i32);
let ret = (unsafe { &mut (*xraw).1 },);
let _val = unsafe { *xraw }; // invalidate xref
ret //~ ERROR does not exist on the borrow stack
ret //~ ERROR borrow stack
}
fn main() {

View file

@ -3,7 +3,7 @@ fn foo(x: &mut (i32, i32)) -> &i32 {
let xraw = x as *mut (i32, i32);
let ret = unsafe { &(*xraw).1 };
unsafe { *xraw = (42, 23) }; // unfreeze
ret //~ ERROR is not frozen
ret //~ ERROR borrow stack
}
fn main() {

View file

@ -3,7 +3,7 @@ fn foo(x: &mut (i32, i32)) -> Option<&i32> {
let xraw = x as *mut (i32, i32);
let ret = Some(unsafe { &(*xraw).1 });
unsafe { *xraw = (42, 23) }; // unfreeze
ret //~ ERROR is not frozen
ret //~ ERROR borrow stack
}
fn main() {

View file

@ -3,7 +3,7 @@ fn foo(x: &mut (i32, i32)) -> (&i32,) {
let xraw = x as *mut (i32, i32);
let ret = (unsafe { &(*xraw).1 },);
unsafe { *xraw = (42, 23) }; // unfreeze
ret //~ ERROR is not frozen
ret //~ ERROR borrow stack
}
fn main() {

View file

@ -0,0 +1,14 @@
// We want to test that granting a SharedReadWrite will be added
// *below* an already granted Unique -- so writing to
// the SharedReadWrite will invalidate the Unique.
use std::mem;
use std::cell::Cell;
fn main() { unsafe {
let x = &mut Cell::new(0);
let y: &mut Cell<i32> = mem::transmute(&mut *x); // launder lifetime
let shr_rw = &*x; // thanks to interior mutability this will be a SharedReadWrite
shr_rw.set(1);
y.get_mut(); //~ ERROR borrow stack
} }

View file

@ -0,0 +1,14 @@
// We want to test that granting a SharedReadWrite will be added
// *below* an already granted SharedReadWrite -- so writing to
// the SharedReadWrite will invalidate the SharedReadWrite.
use std::mem;
use std::cell::RefCell;
fn main() { unsafe {
let x = &mut RefCell::new(0);
let y: &i32 = mem::transmute(&*x.borrow()); // launder lifetime
let shr_rw = &*x; // thanks to interior mutability this will be a SharedReadWrite
shr_rw.replace(1);
let _val = *y; //~ ERROR borrow stack
} }

View file

@ -8,9 +8,6 @@ fn main() {
println!("{}", foo(&mut 0));
}
// If we replace the `*const` by `&`, my current dev version of miri
// *does* find the problem, but not for a good reason: It finds it because
// of barriers, and we shouldn't rely on unknown code using barriers.
fn unknown_code(x: *const i32) {
unsafe { *(x as *mut i32) = 7; } //~ ERROR barrier
fn unknown_code(x: &i32) {
unsafe { *(x as *const i32 as *mut i32) = 7; } //~ ERROR borrow stack
}

View file

@ -3,6 +3,6 @@ static X: usize = 5;
#[allow(mutable_transmutes)]
fn main() {
let _x = unsafe {
std::mem::transmute::<&usize, &mut usize>(&X) //~ ERROR mutable reference with frozen tag
std::mem::transmute::<&usize, &mut usize>(&X) //~ ERROR borrow stack
};
}

View file

@ -10,5 +10,5 @@ fn main() {
let _raw: *mut i32 = unsafe { mem::transmute(&mut x[0]) };
// `raw` still carries a tag, so we get another pointer to the same location that does not carry a tag
let raw = (&mut x[1] as *mut i32).wrapping_offset(-1);
unsafe { *raw = 13; } //~ ERROR does not exist on the borrow stack
unsafe { *raw = 13; } //~ ERROR borrow stack
}

View file

@ -4,5 +4,5 @@ fn main() {
let mut x = 42;
let raw = &mut x as *mut i32 as usize as *mut i32;
let _ptr = &mut x;
unsafe { *raw = 13; } //~ ERROR does not exist on the borrow stack
unsafe { *raw = 13; } //~ ERROR borrow stack
}

View file

@ -1,6 +1,6 @@
fn main() {
let v = [1i16, 2];
let x = &v as *const i16;
let x = &v as *const [i16] as *const i16;
let x = x.wrapping_offset(1);
assert_eq!(unsafe { *x }, 2);
}

View file

@ -2,7 +2,7 @@ fn f() -> i32 { 42 }
fn main() {
let v = [1i16, 2];
let x = &v as *const i16;
let x = &v as *const [i16; 2] as *const i16;
let x = unsafe { x.offset(1) };
assert_eq!(unsafe { *x }, 2);

View file

@ -1,6 +1,6 @@
use std::cell::RefCell;
fn lots_of_funny_borrows() {
fn main() {
let c = RefCell::new(42);
{
let s1 = c.borrow();
@ -31,47 +31,3 @@ fn lots_of_funny_borrows() {
let _y: i32 = *s2;
}
}
fn aliasing_mut_and_shr() {
fn inner(rc: &RefCell<i32>, aliasing: &mut i32) {
*aliasing += 4;
let _escape_to_raw = rc as *const _;
*aliasing += 4;
let _shr = &*rc;
*aliasing += 4;
// also turning this into a frozen ref now must work
let aliasing = &*aliasing;
let _val = *aliasing;
let _escape_to_raw = rc as *const _; // this must NOT unfreeze
let _val = *aliasing;
let _shr = &*rc; // this must NOT unfreeze
let _val = *aliasing;
}
let rc = RefCell::new(23);
let mut bmut = rc.borrow_mut();
inner(&rc, &mut *bmut);
drop(bmut);
assert_eq!(*rc.borrow(), 23+12);
}
fn aliasing_frz_and_shr() {
fn inner(rc: &RefCell<i32>, aliasing: &i32) {
let _val = *aliasing;
let _escape_to_raw = rc as *const _; // this must NOT unfreeze
let _val = *aliasing;
let _shr = &*rc; // this must NOT unfreeze
let _val = *aliasing;
}
let rc = RefCell::new(23);
let bshr = rc.borrow();
inner(&rc, &*bshr);
assert_eq!(*rc.borrow(), 23);
}
fn main() {
lots_of_funny_borrows();
aliasing_mut_and_shr();
aliasing_frz_and_shr();
}

View file

@ -22,14 +22,14 @@ struct Ccx {
x: isize
}
fn alloc<'a>(_bcx : &'a Arena) -> &'a Bcx<'a> {
fn alloc<'a>(_bcx : &'a Arena) -> &'a mut Bcx<'a> {
unsafe {
mem::transmute(libc::malloc(mem::size_of::<Bcx<'a>>()
as libc::size_t))
}
}
fn h<'a>(bcx : &'a Bcx<'a>) -> &'a Bcx<'a> {
fn h<'a>(bcx : &'a Bcx<'a>) -> &'a mut Bcx<'a> {
return alloc(bcx.fcx.arena);
}

View file

@ -1,3 +1,5 @@
#![allow(mutable_borrow_reservation_conflict)]
trait S: Sized {
fn tpb(&mut self, _s: Self) {}
}
@ -26,7 +28,21 @@ fn two_phase3(b: bool) {
));
}
/*
#[allow(unreachable_code)]
fn two_phase_raw() {
let x: &mut Vec<i32> = &mut vec![];
x.push(
{
// Unfortunately this does not trigger the problem of creating a
// raw ponter from a pointer that had a two-phase borrow derived from
// it because of the implicit &mut reborrow.
let raw = x as *mut _;
unsafe { *raw = vec![1]; }
return
}
);
}
fn two_phase_overlapping1() {
let mut x = vec![];
let p = &x;
@ -39,7 +55,6 @@ fn two_phase_overlapping2() {
let l = &x;
x.add_assign(x + *l);
}
*/
fn with_interior_mutability() {
use std::cell::Cell;
@ -53,7 +68,6 @@ fn with_interior_mutability() {
let mut x = Cell::new(1);
let l = &x;
#[allow(unknown_lints, mutable_borrow_reservation_conflict)]
x
.do_the_thing({
x.set(3);
@ -68,8 +82,8 @@ fn main() {
two_phase2();
two_phase3(false);
two_phase3(true);
two_phase_raw();
with_interior_mutability();
//FIXME: enable these, or remove them, depending on how https://github.com/rust-lang/rust/issues/56254 gets resolved
//two_phase_overlapping1();
//two_phase_overlapping2();
two_phase_overlapping1();
two_phase_overlapping2();
}

View file

@ -0,0 +1,59 @@
#![feature(maybe_uninit, maybe_uninit_ref)]
use std::mem::MaybeUninit;
use std::cell::Cell;
use std::cell::RefCell;
fn main() {
aliasing_mut_and_shr();
aliasing_frz_and_shr();
into_interior_mutability();
}
fn aliasing_mut_and_shr() {
fn inner(rc: &RefCell<i32>, aliasing: &mut i32) {
*aliasing += 4;
let _escape_to_raw = rc as *const _;
*aliasing += 4;
let _shr = &*rc;
*aliasing += 4;
// also turning this into a frozen ref now must work
let aliasing = &*aliasing;
let _val = *aliasing;
let _escape_to_raw = rc as *const _; // this must NOT unfreeze
let _val = *aliasing;
let _shr = &*rc; // this must NOT unfreeze
let _val = *aliasing;
}
let rc = RefCell::new(23);
let mut bmut = rc.borrow_mut();
inner(&rc, &mut *bmut);
drop(bmut);
assert_eq!(*rc.borrow(), 23+12);
}
fn aliasing_frz_and_shr() {
fn inner(rc: &RefCell<i32>, aliasing: &i32) {
let _val = *aliasing;
let _escape_to_raw = rc as *const _; // this must NOT unfreeze
let _val = *aliasing;
let _shr = &*rc; // this must NOT unfreeze
let _val = *aliasing;
}
let rc = RefCell::new(23);
let bshr = rc.borrow();
inner(&rc, &*bshr);
assert_eq!(*rc.borrow(), 23);
}
// Getting a pointer into a union with interior mutability used to be tricky
// business (https://github.com/rust-lang/miri/issues/615), but it should work
// now.
fn into_interior_mutability() {
let mut x: MaybeUninit<(Cell<u32>, u32)> = MaybeUninit::uninit();
x.as_ptr();
x.write((Cell::new(0), 1));
let ptr = unsafe { x.get_ref() };
assert_eq!(ptr.1, 1);
}

View file

@ -10,6 +10,8 @@ fn main() {
partially_invalidate_mut();
drop_after_sharing();
direct_mut_to_const_raw();
two_raw();
shr_and_raw();
}
// Deref a raw ptr to access a field of a large struct, where the field
@ -123,3 +125,27 @@ fn direct_mut_to_const_raw() {
assert_eq!(*x, 1);
*/
}
// Make sure that we can create two raw pointers from a mutable reference and use them both.
fn two_raw() { unsafe {
let x = &mut 0;
// Given the implicit reborrows, the only reason this currently works is that we
// do not track raw pointers: The creation of `y2` reborrows `x` and thus pops
// `y1` off the stack.
let y1 = x as *mut _;
let y2 = x as *mut _;
*y1 += 2;
*y2 += 1;
} }
// Make sure that creating a *mut does not invalidate existing shared references.
fn shr_and_raw() { /* unsafe {
use std::mem;
// FIXME: This is currently disabled because "as *mut _" incurs a reborrow.
let x = &mut 0;
let y1: &i32 = mem::transmute(&*x); // launder lifetimes
let y2 = x as *mut _;
let _val = *y1;
*y2 += 1;
// TODO: Once this works, add compile-fail test that tries to read from y1 again.
} */ }