Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement intptrcast methods #779

Merged
merged 7 commits into from
Jun 26, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/intptrcast.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use std::cell::RefCell;

use rustc::mir::interpret::AllocId;

pub type MemoryState = RefCell<GlobalState>;

#[derive(Clone, Debug)]
pub struct GlobalState {
pub vec: Vec<(u64, AllocId)>,
pvdrz marked this conversation as resolved.
Show resolved Hide resolved
pub addr: u64,
pvdrz marked this conversation as resolved.
Show resolved Hide resolved
}

impl Default for GlobalState {
fn default() -> Self {
GlobalState {
vec: Vec::default(),
addr: 2u64.pow(16)
pvdrz marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
97 changes: 89 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@ mod tls;
mod range_map;
mod mono_hash_map;
mod stacked_borrows;
mod intptrcast;
mod memory;

use std::collections::HashMap;
use std::borrow::Cow;
use std::cell::RefCell;
use std::rc::Rc;

use rand::rngs::StdRng;
Expand All @@ -48,6 +51,7 @@ use crate::range_map::RangeMap;
pub use crate::helpers::{EvalContextExt as HelpersEvalContextExt};
use crate::mono_hash_map::MonoHashMap;
pub use crate::stacked_borrows::{EvalContextExt as StackedBorEvalContextExt};
use crate::memory::AllocExtra;

// Used by priroda.
pub use crate::stacked_borrows::{Tag, Permission, Stack, Stacks, Item};
Expand Down Expand Up @@ -206,6 +210,8 @@ pub fn create_ecx<'mir, 'tcx: 'mir>(
}
}

ecx.memory_mut().extra.seed = config.seed.clone();
pvdrz marked this conversation as resolved.
Show resolved Hide resolved

assert!(args.next().is_none(), "start lang item has more arguments than expected");

Ok(ecx)
Expand Down Expand Up @@ -386,8 +392,8 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'tcx> {
type MemoryKinds = MiriMemoryKind;

type FrameExtra = stacked_borrows::CallId;
type MemoryExtra = stacked_borrows::MemoryState;
type AllocExtra = stacked_borrows::Stacks;
type MemoryExtra = memory::MemoryState;
type AllocExtra = memory::AllocExtra;
type PointerTag = Tag;

type MemoryMap = MonoHashMap<AllocId, (MemoryKind<MiriMemoryKind>, Allocation<Tag, Self::AllocExtra>)>;
Expand Down Expand Up @@ -515,14 +521,14 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'tcx> {
let (extra, base_tag) = Stacks::new_allocation(
pvdrz marked this conversation as resolved.
Show resolved Hide resolved
id,
Size::from_bytes(alloc.bytes.len() as u64),
Rc::clone(&memory.extra),
Rc::clone(&memory.extra.stacked),
kind,
);
if kind != MiriMemoryKind::Static.into() {
assert!(alloc.relocations.is_empty(), "Only statics can come initialized with inner pointers");
// Now we can rely on the inner pointers being static, too.
}
let mut memory_extra = memory.extra.borrow_mut();
let mut memory_extra = memory.extra.stacked.borrow_mut();
let alloc: Allocation<Tag, Self::AllocExtra> = Allocation {
bytes: alloc.bytes,
relocations: Relocations::from_presorted(
Expand All @@ -535,7 +541,10 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'tcx> {
undef_mask: alloc.undef_mask,
align: alloc.align,
mutability: alloc.mutability,
extra,
extra: AllocExtra {
stacks: extra,
base_addr: RefCell::new(None),
},
};
(Cow::Owned(alloc), base_tag)
}
Expand All @@ -545,7 +554,7 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'tcx> {
id: AllocId,
memory: &Memory<'mir, 'tcx, Self>,
) -> Self::PointerTag {
memory.extra.borrow_mut().static_base_ptr(id)
memory.extra.stacked.borrow_mut().static_base_ptr(id)
}

#[inline(always)]
Expand All @@ -570,14 +579,86 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'tcx> {
fn stack_push(
ecx: &mut InterpretCx<'mir, 'tcx, Self>,
) -> InterpResult<'tcx, stacked_borrows::CallId> {
Ok(ecx.memory().extra.borrow_mut().new_call())
Ok(ecx.memory().extra.stacked.borrow_mut().new_call())
}

#[inline(always)]
fn stack_pop(
ecx: &mut InterpretCx<'mir, 'tcx, Self>,
extra: stacked_borrows::CallId,
) -> InterpResult<'tcx> {
Ok(ecx.memory().extra.borrow_mut().end_call(extra))
Ok(ecx.memory().extra.stacked.borrow_mut().end_call(extra))
}

fn int_to_ptr(
int: u64,
memory: &Memory<'mir, 'tcx, Self>,
) -> InterpResult<'tcx, Pointer<Self::PointerTag>> {
if int == 0 {
return err!(InvalidNullPointerUsage);
}

if memory.extra.seed.is_none() {
return err!(ReadBytesAsPointer);
}

let extra = memory.extra.intptrcast.borrow();

match extra.vec.binary_search_by_key(&int, |(int, _)| *int) {
Ok(pos) => {
let (_, alloc_id) = extra.vec[pos];
Ok(Pointer::new_with_tag(alloc_id, Size::from_bytes(0), Tag::Untagged))
pvdrz marked this conversation as resolved.
Show resolved Hide resolved
}
Err(pos) => {
pvdrz marked this conversation as resolved.
Show resolved Hide resolved
if pos > 0 {
let (glb, alloc_id) = extra.vec[pos - 1];
let offset = int - glb;
if offset <= memory.get(alloc_id)?.bytes.len() as u64 {
Ok(Pointer::new_with_tag(alloc_id, Size::from_bytes(offset), Tag::Untagged))
} else {
return err!(DanglingPointerDeref);
}
} else {
return err!(DanglingPointerDeref);
}
pvdrz marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

fn ptr_to_int(
ptr: Pointer<Self::PointerTag>,
memory: &Memory<'mir, 'tcx, Self>,
) -> InterpResult<'tcx, u64> {
if memory.extra.seed.is_none() {
return err!(ReadPointerAsBytes);
}

let mut extra = memory.extra.intptrcast.borrow_mut();

let alloc = memory.get(ptr.alloc_id)?;

let mut base_addr = alloc.extra.base_addr.borrow_mut();

let addr = match *base_addr {
Some(addr) => addr,
None => {
let addr = extra.addr;
extra.addr += alloc.bytes.len() as u64;

*base_addr = Some(addr);

let elem = (addr, ptr.alloc_id);

if let Err(pos) = extra.vec.binary_search(&elem) {
pvdrz marked this conversation as resolved.
Show resolved Hide resolved
extra.vec.insert(pos, elem);
} else {
return err!(Unreachable);
}

addr
}
};

Ok(addr + ptr.offset.bytes())
}
}
60 changes: 60 additions & 0 deletions src/memory.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use std::cell::RefCell;

use rustc_mir::interpret::{Pointer, Allocation, AllocationExtra, InterpResult};
use rustc_target::abi::Size;

use crate::{stacked_borrows, intptrcast};
use crate::stacked_borrows::{Tag, AccessKind};

#[derive(Default, Clone, Debug)]
pub struct MemoryState {
pvdrz marked this conversation as resolved.
Show resolved Hide resolved
pub stacked: stacked_borrows::MemoryState,
pub intptrcast: intptrcast::MemoryState,
pvdrz marked this conversation as resolved.
Show resolved Hide resolved
pub seed: Option<u64>,
pvdrz marked this conversation as resolved.
Show resolved Hide resolved
}

#[derive(Debug, Clone,)]
pub struct AllocExtra {
pub stacks: stacked_borrows::Stacks,
pub base_addr: RefCell<Option<u64>>,
pvdrz marked this conversation as resolved.
Show resolved Hide resolved
}

impl AllocationExtra<Tag> for AllocExtra {
#[inline(always)]
fn memory_read<'tcx>(
alloc: &Allocation<Tag, AllocExtra>,
ptr: Pointer<Tag>,
size: Size,
) -> InterpResult<'tcx> {
trace!("read access with tag {:?}: {:?}, size {}", ptr.tag, ptr.erase_tag(), size.bytes());
alloc.extra.stacks.for_each(ptr, size, |stack, global| {
stack.access(AccessKind::Read, ptr.tag, global)?;
Ok(())
})
}
pvdrz marked this conversation as resolved.
Show resolved Hide resolved

#[inline(always)]
fn memory_written<'tcx>(
alloc: &mut Allocation<Tag, AllocExtra>,
ptr: Pointer<Tag>,
size: Size,
) -> InterpResult<'tcx> {
trace!("write access with tag {:?}: {:?}, size {}", ptr.tag, ptr.erase_tag(), size.bytes());
alloc.extra.stacks.for_each(ptr, size, |stack, global| {
stack.access(AccessKind::Write, ptr.tag, global)?;
Ok(())
})
}

#[inline(always)]
fn memory_deallocated<'tcx>(
alloc: &mut Allocation<Tag, AllocExtra>,
ptr: Pointer<Tag>,
size: Size,
) -> InterpResult<'tcx> {
trace!("deallocation with tag {:?}: {:?}, size {}", ptr.tag, ptr.erase_tag(), size.bytes());
alloc.extra.stacks.for_each(ptr, size, |stack, global| {
stack.dealloc(ptr.tag, global)
})
}
}
10 changes: 10 additions & 0 deletions src/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ impl<'mir, 'tcx> EvalContextExt<'tcx> for super::MiriEvalContext<'mir, 'tcx> {

trace!("ptr_op: {:?} {:?} {:?}", *left, bin_op, *right);

if self.memory().extra.seed.is_some() && bin_op != Offset {
let l_bits = self.force_bits(left.imm.to_scalar()?, left.layout.size)?;
pvdrz marked this conversation as resolved.
Show resolved Hide resolved
let r_bits = self.force_bits(right.imm.to_scalar()?, right.layout.size)?;

let left = ImmTy::from_scalar(Scalar::from_uint(l_bits, left.layout.size), left.layout);
let right = ImmTy::from_scalar(Scalar::from_uint(r_bits, left.layout.size), right.layout);

return self.binary_op(bin_op, left, right);
}

// Operations that support fat pointers
match bin_op {
Eq | Ne => {
Expand Down
Loading