2019-06-30 03:59:42 -05:00
|
|
|
use std::cell::RefCell;
|
|
|
|
use std::cmp::max;
|
2020-03-02 15:36:15 -06:00
|
|
|
use std::collections::hash_map::Entry;
|
2019-06-20 14:21:47 -05:00
|
|
|
|
2020-03-30 04:07:32 -05:00
|
|
|
use log::trace;
|
2019-06-28 03:16:10 -05:00
|
|
|
use rand::Rng;
|
|
|
|
|
2020-03-02 15:36:15 -06:00
|
|
|
use rustc_data_structures::fx::FxHashMap;
|
2020-03-30 04:07:32 -05:00
|
|
|
use rustc_middle::ty::layout::HasDataLayout;
|
2020-02-23 15:35:13 -06:00
|
|
|
use rustc_mir::interpret::{AllocCheck, AllocId, InterpResult, Memory, Machine, Pointer, PointerArithmetic};
|
2019-06-24 16:34:38 -05:00
|
|
|
use rustc_target::abi::Size;
|
2019-06-20 14:21:47 -05:00
|
|
|
|
2019-06-29 06:33:47 -05:00
|
|
|
use crate::{Evaluator, Tag, STACK_ADDR};
|
2019-06-24 16:34:38 -05:00
|
|
|
|
|
|
|
pub type MemoryExtra = RefCell<GlobalState>;
|
|
|
|
|
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-30 03:59:42 -05:00
|
|
|
/// The base address for each allocation. We cannot put that into
|
|
|
|
/// `AllocExtra` because function pointers also have a base address, and
|
|
|
|
/// they do not have an `AllocExtra`.
|
|
|
|
/// This is the inverse of `int_to_ptr_map`.
|
2020-03-02 15:36:15 -06:00
|
|
|
pub base_addr: FxHashMap<AllocId, u64>,
|
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 {
|
|
|
|
fn default() -> Self {
|
|
|
|
GlobalState {
|
2019-06-24 10:03:16 -05:00
|
|
|
int_to_ptr_map: Vec::default(),
|
2020-03-02 15:36:15 -06:00
|
|
|
base_addr: FxHashMap::default(),
|
2019-06-29 06:33:47 -05:00
|
|
|
next_base_addr: STACK_ADDR,
|
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-12-23 05:56:23 -06:00
|
|
|
let pos = global_state.int_to_ptr_map.binary_search_by_key(&int, |(addr, _)| *addr);
|
2019-09-05 11:17:58 -05:00
|
|
|
|
2019-12-23 05:56:23 -06:00
|
|
|
Ok(match pos {
|
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
|
2019-08-03 03:25:55 -05:00
|
|
|
Pointer::new_with_tag(alloc_id, Size::from_bytes(0), Tag::Untagged)
|
2019-12-23 05:56:23 -06:00
|
|
|
}
|
2020-03-08 17:34:54 -05:00
|
|
|
Err(0) => throw_ub!(InvalidIntPointerUsage(int)),
|
2019-06-24 16:34:38 -05:00
|
|
|
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
|
2019-11-08 15:07:52 -06:00
|
|
|
if offset <= memory.get_size_and_align(alloc_id, AllocCheck::MaybeDead)?.0.bytes() {
|
2019-06-24 16:34:38 -05:00
|
|
|
// This pointer is untagged because it was created from a cast
|
2019-08-03 03:25:55 -05:00
|
|
|
Pointer::new_with_tag(alloc_id, Size::from_bytes(offset), Tag::Untagged)
|
2019-06-24 16:34:38 -05:00
|
|
|
} else {
|
2020-03-08 17:34:54 -05:00
|
|
|
throw_ub!(InvalidIntPointerUsage(int))
|
2019-09-05 11:17:58 -05:00
|
|
|
}
|
2019-06-24 16:34:38 -05:00
|
|
|
}
|
2019-08-03 03:25:55 -05:00
|
|
|
})
|
2019-06-24 16:34:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
2019-06-30 03:59:42 -05:00
|
|
|
let global_state = &mut *global_state;
|
2020-02-23 15:35:13 -06:00
|
|
|
let id = Evaluator::canonical_alloc_id(memory, ptr.alloc_id);
|
2019-06-24 16:34:38 -05:00
|
|
|
|
2019-07-03 02:23:31 -05:00
|
|
|
// There is nothing wrong with a raw pointer being cast to an integer only after
|
|
|
|
// it became dangling. Hence `MaybeDead`.
|
2020-02-23 15:35:13 -06:00
|
|
|
let (size, align) = memory.get_size_and_align(id, AllocCheck::MaybeDead)?;
|
2019-06-24 16:34:38 -05:00
|
|
|
|
2020-02-23 15:35:13 -06:00
|
|
|
let base_addr = match global_state.base_addr.entry(id) {
|
2019-06-30 03:59:42 -05:00
|
|
|
Entry::Occupied(entry) => *entry.get(),
|
|
|
|
Entry::Vacant(entry) => {
|
2019-06-25 14:07:23 -05:00
|
|
|
// This allocation does not have a base address yet, pick one.
|
2019-06-28 03:16:10 -05:00
|
|
|
// Leave some space to the previous allocation, to give it some chance to be less aligned.
|
|
|
|
let slack = {
|
2019-07-23 14:38:53 -05:00
|
|
|
let mut rng = memory.extra.rng.borrow_mut();
|
2019-06-28 03:23:29 -05:00
|
|
|
// This means that `(global_state.next_base_addr + slack) % 16` is uniformly distributed.
|
|
|
|
rng.gen_range(0, 16)
|
2019-06-28 03:16:10 -05:00
|
|
|
};
|
|
|
|
// From next_base_addr + slack, round up to adjust for alignment.
|
2019-07-23 16:43:37 -05:00
|
|
|
let base_addr = global_state.next_base_addr.checked_add(slack).unwrap();
|
|
|
|
let base_addr = Self::align_addr(base_addr, align.bytes());
|
2019-06-30 03:59:42 -05:00
|
|
|
entry.insert(base_addr);
|
2019-06-30 14:06:32 -05:00
|
|
|
trace!(
|
|
|
|
"Assigning base address {:#x} to allocation {:?} (slack: {}, align: {})",
|
2019-12-23 05:56:23 -06:00
|
|
|
base_addr,
|
2020-02-23 15:35:13 -06:00
|
|
|
id,
|
2019-12-23 05:56:23 -06:00
|
|
|
slack,
|
|
|
|
align.bytes(),
|
2019-06-30 14:06:32 -05:00
|
|
|
);
|
2019-06-28 03:16:10 -05:00
|
|
|
|
2019-06-30 03:59:42 -05:00
|
|
|
// Remember next base address. If this allocation is zero-sized, leave a gap
|
|
|
|
// of at least 1 to avoid two allocations having the same base address.
|
2019-07-23 16:43:37 -05:00
|
|
|
global_state.next_base_addr = base_addr.checked_add(max(size.bytes(), 1)).unwrap();
|
2019-06-24 16:34:38 -05:00
|
|
|
// Given that `next_base_addr` increases in each allocation, pushing the
|
|
|
|
// corresponding tuple keeps `int_to_ptr_map` sorted
|
2020-02-23 15:35:13 -06:00
|
|
|
global_state.int_to_ptr_map.push((base_addr, id));
|
2019-06-24 16:34:38 -05:00
|
|
|
|
|
|
|
base_addr
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-12-23 05:56:23 -06:00
|
|
|
// Sanity check that the base address is aligned.
|
|
|
|
debug_assert_eq!(base_addr % align.bytes(), 0);
|
2019-07-03 02:32:21 -05:00
|
|
|
// Add offset with the right kind of pointer-overflowing arithmetic.
|
|
|
|
let dl = memory.data_layout();
|
|
|
|
Ok(dl.overflowing_offset(base_addr, ptr.offset.bytes()).0)
|
2019-06-24 16:34:38 -05:00
|
|
|
}
|
2019-06-25 14:07:23 -05:00
|
|
|
|
|
|
|
/// Shifts `addr` to make it aligned with `align` by rounding `addr` to the smallest multiple
|
2019-06-28 03:16:10 -05:00
|
|
|
/// of `align` that is larger or equal to `addr`
|
2019-06-25 14:07:23 -05:00
|
|
|
fn align_addr(addr: u64, align: u64) -> u64 {
|
2019-06-28 03:24:16 -05:00
|
|
|
match addr % align {
|
|
|
|
0 => addr,
|
2019-12-23 05:56:23 -06:00
|
|
|
rem => addr.checked_add(align).unwrap() - rem,
|
2019-06-28 03:16:10 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_align_addr() {
|
|
|
|
assert_eq!(GlobalState::align_addr(37, 4), 40);
|
|
|
|
assert_eq!(GlobalState::align_addr(44, 4), 44);
|
2019-06-25 14:07:23 -05:00
|
|
|
}
|
2019-06-24 16:34:38 -05:00
|
|
|
}
|