2019-06-24 16:34:38 -05:00
|
|
|
use std::cell::{Cell, RefCell};
|
2019-06-20 14:21:47 -05:00
|
|
|
|
2019-06-24 16:34:38 -05:00
|
|
|
use rustc::mir::interpret::{AllocId, Pointer, InterpResult};
|
|
|
|
use rustc_mir::interpret::Memory;
|
|
|
|
use rustc_target::abi::Size;
|
2019-06-20 14:21:47 -05:00
|
|
|
|
2019-06-24 16:34:38 -05:00
|
|
|
use crate::stacked_borrows::Tag;
|
|
|
|
use crate::Evaluator;
|
|
|
|
|
|
|
|
pub type MemoryExtra = RefCell<GlobalState>;
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Default)]
|
|
|
|
pub struct AllocExtra {
|
|
|
|
base_addr: Cell<Option<u64>>
|
|
|
|
}
|
2019-06-20 14:21:47 -05:00
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub struct GlobalState {
|
2019-06-24 16:34:38 -05:00
|
|
|
/// This is used as a map between the address of each allocation and its `AllocId`.
|
|
|
|
/// It is always sorted
|
2019-06-24 10:03:16 -05:00
|
|
|
pub int_to_ptr_map: Vec<(u64, AllocId)>,
|
2019-06-24 16:34:38 -05:00
|
|
|
/// This is used as a memory address when a new pointer is casted to an integer. It
|
|
|
|
/// is always larger than any address that was previously made part of a block.
|
2019-06-24 10:03:16 -05:00
|
|
|
pub next_base_addr: u64,
|
2019-06-20 14:21:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for GlobalState {
|
2019-06-24 10:03:16 -05:00
|
|
|
// FIXME: Query the page size in the future
|
2019-06-20 14:21:47 -05:00
|
|
|
fn default() -> Self {
|
|
|
|
GlobalState {
|
2019-06-24 10:03:16 -05:00
|
|
|
int_to_ptr_map: Vec::default(),
|
|
|
|
next_base_addr: 2u64.pow(16)
|
2019-06-20 14:21:47 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-06-24 16:34:38 -05:00
|
|
|
|
|
|
|
impl<'mir, 'tcx> GlobalState {
|
|
|
|
pub fn int_to_ptr(
|
2019-06-25 14:07:23 -05:00
|
|
|
int: u64,
|
2019-06-24 16:34:38 -05:00
|
|
|
memory: &Memory<'mir, 'tcx, Evaluator<'tcx>>,
|
|
|
|
) -> InterpResult<'tcx, Pointer<Tag>> {
|
|
|
|
let global_state = memory.extra.intptrcast.borrow();
|
|
|
|
|
2019-06-25 14:07:23 -05:00
|
|
|
match global_state.int_to_ptr_map.binary_search_by_key(&int, |(addr, _)| *addr) {
|
2019-06-24 16:34:38 -05:00
|
|
|
Ok(pos) => {
|
|
|
|
let (_, alloc_id) = global_state.int_to_ptr_map[pos];
|
2019-06-25 14:07:23 -05:00
|
|
|
// `int` is equal to the starting address for an allocation, the offset should be
|
2019-06-24 16:34:38 -05:00
|
|
|
// zero. The pointer is untagged because it was created from a cast
|
|
|
|
Ok(Pointer::new_with_tag(alloc_id, Size::from_bytes(0), Tag::Untagged))
|
|
|
|
},
|
|
|
|
Err(0) => err!(DanglingPointerDeref),
|
|
|
|
Err(pos) => {
|
2019-06-25 14:07:23 -05:00
|
|
|
// This is the largest of the adresses smaller than `int`,
|
2019-06-24 16:34:38 -05:00
|
|
|
// i.e. the greatest lower bound (glb)
|
|
|
|
let (glb, alloc_id) = global_state.int_to_ptr_map[pos - 1];
|
2019-06-25 14:07:23 -05:00
|
|
|
// This never overflows because `int >= glb`
|
|
|
|
let offset = int - glb;
|
2019-06-24 16:34:38 -05:00
|
|
|
// If the offset exceeds the size of the allocation, this access is illegal
|
|
|
|
if offset <= memory.get(alloc_id)?.bytes.len() as u64 {
|
|
|
|
// This pointer is untagged because it was created from a cast
|
|
|
|
Ok(Pointer::new_with_tag(alloc_id, Size::from_bytes(offset), Tag::Untagged))
|
|
|
|
} else {
|
|
|
|
err!(DanglingPointerDeref)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn ptr_to_int(
|
|
|
|
ptr: Pointer<Tag>,
|
|
|
|
memory: &Memory<'mir, 'tcx, Evaluator<'tcx>>,
|
|
|
|
) -> InterpResult<'tcx, u64> {
|
|
|
|
let mut global_state = memory.extra.intptrcast.borrow_mut();
|
|
|
|
|
|
|
|
let alloc = memory.get(ptr.alloc_id)?;
|
|
|
|
|
|
|
|
let base_addr = match alloc.extra.intptrcast.base_addr.get() {
|
|
|
|
Some(base_addr) => base_addr,
|
|
|
|
None => {
|
2019-06-25 14:07:23 -05:00
|
|
|
// This allocation does not have a base address yet, pick one.
|
|
|
|
let base_addr = Self::align_addr(global_state.next_base_addr, alloc.align.bytes());
|
|
|
|
global_state.next_base_addr = base_addr + alloc.bytes.len() as u64;
|
2019-06-24 16:34:38 -05:00
|
|
|
alloc.extra.intptrcast.base_addr.set(Some(base_addr));
|
|
|
|
// Given that `next_base_addr` increases in each allocation, pushing the
|
|
|
|
// corresponding tuple keeps `int_to_ptr_map` sorted
|
2019-06-25 14:07:23 -05:00
|
|
|
global_state.int_to_ptr_map.push((base_addr, ptr.alloc_id));
|
2019-06-24 16:34:38 -05:00
|
|
|
|
|
|
|
base_addr
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-06-28 09:12:11 +02:00
|
|
|
debug_assert_eq!(base_addr % alloc.align.bytes(), 0); // sanity check
|
2019-06-24 16:34:38 -05:00
|
|
|
Ok(base_addr + ptr.offset.bytes())
|
|
|
|
}
|
2019-06-25 14:07:23 -05:00
|
|
|
|
|
|
|
/// Shifts `addr` to make it aligned with `align` by rounding `addr` to the smallest multiple
|
|
|
|
/// of `align` that is strictly larger to `addr`
|
|
|
|
fn align_addr(addr: u64, align: u64) -> u64 {
|
|
|
|
addr + align - addr % align
|
|
|
|
}
|
2019-06-24 16:34:38 -05:00
|
|
|
}
|