2019-06-30 03:59:42 -05:00
|
|
|
use std::cell::RefCell;
|
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;
|
|
|
|
|
2022-05-22 15:22:05 -05:00
|
|
|
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
|
2021-05-16 04:28:01 -05:00
|
|
|
use rustc_target::abi::{HasDataLayout, Size};
|
2019-06-20 14:21:47 -05:00
|
|
|
|
2020-06-27 06:19:35 -05:00
|
|
|
use crate::*;
|
2019-06-24 16:34:38 -05:00
|
|
|
|
2022-05-22 15:22:05 -05:00
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
|
|
|
pub enum ProvenanceMode {
|
2022-06-26 20:14:18 -05:00
|
|
|
/// We support `expose_addr`/`from_exposed_addr` via "wildcard" provenance.
|
|
|
|
/// However, we want on `from_exposed_addr` to alert the user of the precision loss.
|
|
|
|
Default,
|
|
|
|
/// Like `Default`, but without the warning.
|
2022-05-22 15:22:05 -05:00
|
|
|
Permissive,
|
2022-06-26 20:14:18 -05:00
|
|
|
/// We error on `from_exposed_addr`, ensuring no precision loss.
|
2022-05-22 15:22:05 -05:00
|
|
|
Strict,
|
|
|
|
}
|
|
|
|
|
2022-04-03 15:12:52 -05:00
|
|
|
pub type GlobalState = RefCell<GlobalStateInner>;
|
2019-06-24 16:34:38 -05:00
|
|
|
|
2019-06-20 14:21:47 -05:00
|
|
|
#[derive(Clone, Debug)]
|
2022-04-03 15:12:52 -05:00
|
|
|
pub struct GlobalStateInner {
|
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
|
2022-04-01 13:10:24 -05:00
|
|
|
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`.
|
2022-04-01 13:10:24 -05:00
|
|
|
base_addr: FxHashMap<AllocId, u64>,
|
2022-05-22 15:22:05 -05:00
|
|
|
/// Whether an allocation has been exposed or not. This cannot be put
|
|
|
|
/// into `AllocExtra` for the same reason as `base_addr`.
|
|
|
|
exposed: FxHashSet<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.
|
2022-04-01 13:10:24 -05:00
|
|
|
next_base_addr: u64,
|
2022-05-22 15:22:05 -05:00
|
|
|
/// The provenance to use for int2ptr casts
|
|
|
|
provenance_mode: ProvenanceMode,
|
2019-06-20 14:21:47 -05:00
|
|
|
}
|
|
|
|
|
2022-04-03 15:12:52 -05:00
|
|
|
impl GlobalStateInner {
|
2022-04-01 13:10:24 -05:00
|
|
|
pub fn new(config: &MiriConfig) -> Self {
|
2022-04-03 15:12:52 -05:00
|
|
|
GlobalStateInner {
|
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(),
|
2022-05-22 15:22:05 -05:00
|
|
|
exposed: FxHashSet::default(),
|
2019-06-29 06:33:47 -05:00
|
|
|
next_base_addr: STACK_ADDR,
|
2022-05-22 15:22:05 -05:00
|
|
|
provenance_mode: config.provenance_mode,
|
2019-06-20 14:21:47 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-06-24 16:34:38 -05:00
|
|
|
|
2022-04-03 15:12:52 -05:00
|
|
|
impl<'mir, 'tcx> GlobalStateInner {
|
2022-05-22 15:22:05 -05:00
|
|
|
// Returns the exposed `AllocId` that corresponds to the specified addr,
|
|
|
|
// or `None` if the addr is out of bounds
|
|
|
|
fn alloc_id_from_addr(ecx: &MiriEvalContext<'mir, 'tcx>, addr: u64) -> Option<AllocId> {
|
2022-04-03 15:12:52 -05:00
|
|
|
let global_state = ecx.machine.intptrcast.borrow();
|
2022-05-22 15:22:05 -05:00
|
|
|
assert!(global_state.provenance_mode != ProvenanceMode::Strict);
|
2022-04-01 13:10:24 -05:00
|
|
|
|
|
|
|
let pos = global_state.int_to_ptr_map.binary_search_by_key(&addr, |(addr, _)| *addr);
|
2022-05-22 15:22:05 -05:00
|
|
|
|
2022-06-26 20:14:18 -05:00
|
|
|
// Determine the in-bounds provenance for this pointer.
|
|
|
|
// (This is only called on an actual access, so in-bounds is the only possible kind of provenance.)
|
2021-07-15 13:33:08 -05:00
|
|
|
let alloc_id = match pos {
|
|
|
|
Ok(pos) => Some(global_state.int_to_ptr_map[pos].1),
|
|
|
|
Err(0) => None,
|
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];
|
2021-07-15 13:33:08 -05:00
|
|
|
// This never overflows because `addr >= glb`
|
|
|
|
let offset = addr - glb;
|
|
|
|
// If the offset exceeds the size of the allocation, don't use this `alloc_id`.
|
2022-05-22 15:22:05 -05:00
|
|
|
|
2021-07-15 13:33:08 -05:00
|
|
|
if offset
|
2022-04-03 15:12:52 -05:00
|
|
|
<= ecx
|
|
|
|
.get_alloc_size_and_align(alloc_id, AllocCheck::MaybeDead)
|
|
|
|
.unwrap()
|
|
|
|
.0
|
|
|
|
.bytes()
|
2021-07-15 13:33:08 -05:00
|
|
|
{
|
|
|
|
Some(alloc_id)
|
2019-06-24 16:34:38 -05:00
|
|
|
} else {
|
2021-07-15 13:33:08 -05:00
|
|
|
None
|
2019-09-05 11:17:58 -05:00
|
|
|
}
|
2019-06-24 16:34:38 -05:00
|
|
|
}
|
2022-05-22 15:22:05 -05:00
|
|
|
}?;
|
|
|
|
|
2022-06-26 20:14:18 -05:00
|
|
|
// We only use this provenance if it has been exposed.
|
|
|
|
if global_state.exposed.contains(&alloc_id) { Some(alloc_id) } else { None }
|
2022-05-22 15:22:05 -05:00
|
|
|
}
|
|
|
|
|
2022-06-24 15:45:22 -05:00
|
|
|
pub fn expose_ptr(ecx: &mut MiriEvalContext<'mir, 'tcx>, alloc_id: AllocId, sb: SbTag) {
|
|
|
|
let global_state = ecx.machine.intptrcast.get_mut();
|
2022-06-26 20:14:18 -05:00
|
|
|
// In strict mode, we don't need this, so we can save some cycles by not tracking it.
|
|
|
|
if global_state.provenance_mode != ProvenanceMode::Strict {
|
2022-06-24 17:02:25 -05:00
|
|
|
trace!("Exposing allocation id {alloc_id:?}");
|
2022-05-22 15:22:05 -05:00
|
|
|
global_state.exposed.insert(alloc_id);
|
2022-06-24 15:45:22 -05:00
|
|
|
if ecx.machine.stacked_borrows.is_some() {
|
|
|
|
ecx.expose_tag(alloc_id, sb);
|
|
|
|
}
|
2022-05-22 15:22:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn ptr_from_addr_transmute(
|
|
|
|
ecx: &MiriEvalContext<'mir, 'tcx>,
|
|
|
|
addr: u64,
|
|
|
|
) -> Pointer<Option<Tag>> {
|
|
|
|
trace!("Transmuting 0x{:x} to a pointer", addr);
|
|
|
|
|
2022-06-26 20:14:18 -05:00
|
|
|
let provenance = if ecx.machine.allow_ptr_int_transmute {
|
|
|
|
// When we allow transmutes, treat them like casts: generating a wildcard pointer.
|
|
|
|
Some(Tag::Wildcard)
|
2022-05-23 08:44:27 -05:00
|
|
|
} else {
|
2022-06-26 20:14:18 -05:00
|
|
|
// Usually, we consider transmuted pointers to be "invalid" (`None` provenance).
|
|
|
|
None
|
|
|
|
};
|
|
|
|
Pointer::new(provenance, Size::from_bytes(addr))
|
2022-05-22 15:22:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn ptr_from_addr_cast(
|
|
|
|
ecx: &MiriEvalContext<'mir, 'tcx>,
|
|
|
|
addr: u64,
|
2022-06-26 20:14:18 -05:00
|
|
|
) -> InterpResult<'tcx, Pointer<Option<Tag>>> {
|
2022-05-22 15:22:05 -05:00
|
|
|
trace!("Casting 0x{:x} to a pointer", addr);
|
|
|
|
|
|
|
|
let global_state = ecx.machine.intptrcast.borrow();
|
|
|
|
|
2022-05-23 02:17:04 -05:00
|
|
|
match global_state.provenance_mode {
|
2022-06-26 20:14:18 -05:00
|
|
|
ProvenanceMode::Default => {
|
|
|
|
// The first time this happens, print a warning.
|
|
|
|
use std::sync::atomic::{AtomicBool, Ordering};
|
|
|
|
static FIRST_WARNING: AtomicBool = AtomicBool::new(true);
|
|
|
|
if FIRST_WARNING.swap(false, Ordering::Relaxed) {
|
|
|
|
register_diagnostic(NonHaltingDiagnostic::Int2Ptr);
|
|
|
|
}
|
2022-05-23 02:17:04 -05:00
|
|
|
}
|
|
|
|
ProvenanceMode::Strict => {
|
2022-06-26 20:14:18 -05:00
|
|
|
throw_unsup_format!(
|
|
|
|
"integer-to-pointer casts and `from_exposed_addr` are not supported with `-Zmiri-strict-provenance`; use `with_addr` instead"
|
|
|
|
)
|
2022-05-23 02:17:04 -05:00
|
|
|
}
|
2022-06-26 20:14:18 -05:00
|
|
|
ProvenanceMode::Permissive => {}
|
2022-05-22 15:22:05 -05:00
|
|
|
}
|
2022-06-26 20:14:18 -05:00
|
|
|
|
|
|
|
// This is how wildcard pointers are born.
|
|
|
|
Ok(Pointer::new(Some(Tag::Wildcard), Size::from_bytes(addr)))
|
2019-06-24 16:34:38 -05:00
|
|
|
}
|
|
|
|
|
2022-04-03 15:12:52 -05:00
|
|
|
fn alloc_base_addr(ecx: &MiriEvalContext<'mir, 'tcx>, alloc_id: AllocId) -> u64 {
|
|
|
|
let mut global_state = ecx.machine.intptrcast.borrow_mut();
|
2019-06-30 03:59:42 -05:00
|
|
|
let global_state = &mut *global_state;
|
2019-06-24 16:34:38 -05:00
|
|
|
|
2021-07-15 13:33:08 -05:00
|
|
|
match global_state.base_addr.entry(alloc_id) {
|
2019-06-30 03:59:42 -05:00
|
|
|
Entry::Occupied(entry) => *entry.get(),
|
|
|
|
Entry::Vacant(entry) => {
|
2021-07-15 13:33:08 -05:00
|
|
|
// There is nothing wrong with a raw pointer being cast to an integer only after
|
|
|
|
// it became dangling. Hence `MaybeDead`.
|
|
|
|
let (size, align) =
|
2022-04-03 15:12:52 -05:00
|
|
|
ecx.get_alloc_size_and_align(alloc_id, AllocCheck::MaybeDead).unwrap();
|
2021-07-15 13:33:08 -05:00
|
|
|
|
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 = {
|
2022-04-03 15:12:52 -05:00
|
|
|
let mut rng = ecx.machine.rng.borrow_mut();
|
2019-06-28 03:23:29 -05:00
|
|
|
// This means that `(global_state.next_base_addr + slack) % 16` is uniformly distributed.
|
2021-04-04 04:45:09 -05:00
|
|
|
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!(
|
2021-07-15 13:33:08 -05:00
|
|
|
"Assigning base address {:#x} to allocation {:?} (size: {}, align: {}, slack: {})",
|
2019-12-23 05:56:23 -06:00
|
|
|
base_addr,
|
2021-07-15 13:33:08 -05:00
|
|
|
alloc_id,
|
|
|
|
size.bytes(),
|
2019-12-23 05:56:23 -06:00
|
|
|
align.bytes(),
|
2021-07-15 13:33:08 -05:00
|
|
|
slack,
|
2019-06-30 14:06:32 -05:00
|
|
|
);
|
2019-06-28 03:16:10 -05:00
|
|
|
|
2021-12-03 14:57:37 -06:00
|
|
|
// Remember next base address. Leave a gap of at least 1 to avoid two zero-sized allocations
|
|
|
|
// having the same base address, and to avoid ambiguous provenance for the address between two
|
2021-12-05 19:33:20 -06:00
|
|
|
// allocations (also see https://github.com/rust-lang/unsafe-code-guidelines/issues/313).
|
|
|
|
let size_plus_1 = size.bytes().checked_add(1).unwrap();
|
|
|
|
global_state.next_base_addr = base_addr.checked_add(size_plus_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
|
2021-07-15 13:33:08 -05:00
|
|
|
global_state.int_to_ptr_map.push((base_addr, alloc_id));
|
2019-06-24 16:34:38 -05:00
|
|
|
|
|
|
|
base_addr
|
|
|
|
}
|
2021-07-15 13:33:08 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Convert a relative (tcx) pointer to an absolute address.
|
2022-04-03 15:12:52 -05:00
|
|
|
pub fn rel_ptr_to_addr(ecx: &MiriEvalContext<'mir, 'tcx>, ptr: Pointer<AllocId>) -> u64 {
|
2022-04-18 11:38:26 -05:00
|
|
|
let (alloc_id, offset) = ptr.into_parts(); // offset is relative (AllocId provenance)
|
2022-04-03 15:12:52 -05:00
|
|
|
let base_addr = GlobalStateInner::alloc_base_addr(ecx, alloc_id);
|
2019-06-24 16:34:38 -05:00
|
|
|
|
2019-07-03 02:32:21 -05:00
|
|
|
// Add offset with the right kind of pointer-overflowing arithmetic.
|
2022-04-03 15:12:52 -05:00
|
|
|
let dl = ecx.data_layout();
|
2021-07-15 13:33:08 -05:00
|
|
|
dl.overflowing_offset(base_addr, offset.bytes()).0
|
|
|
|
}
|
|
|
|
|
2022-06-26 20:14:18 -05:00
|
|
|
/// Whena pointer is used for a memory access, this computes where in which allocation the
|
|
|
|
/// access is going.
|
2022-05-22 15:22:05 -05:00
|
|
|
pub fn abs_ptr_to_rel(
|
|
|
|
ecx: &MiriEvalContext<'mir, 'tcx>,
|
|
|
|
ptr: Pointer<Tag>,
|
|
|
|
) -> Option<(AllocId, Size)> {
|
2022-04-18 11:38:26 -05:00
|
|
|
let (tag, addr) = ptr.into_parts(); // addr is absolute (Tag provenance)
|
2022-05-22 15:22:05 -05:00
|
|
|
|
2022-06-24 18:45:35 -05:00
|
|
|
let alloc_id = if let Tag::Concrete { alloc_id, .. } = tag {
|
|
|
|
alloc_id
|
2022-05-22 15:22:05 -05:00
|
|
|
} else {
|
2022-05-23 02:17:04 -05:00
|
|
|
// A wildcard pointer.
|
2022-05-22 15:22:05 -05:00
|
|
|
GlobalStateInner::alloc_id_from_addr(ecx, addr.bytes())?
|
|
|
|
};
|
|
|
|
|
|
|
|
let base_addr = GlobalStateInner::alloc_base_addr(ecx, alloc_id);
|
2021-07-15 13:33:08 -05:00
|
|
|
|
|
|
|
// Wrapping "addr - base_addr"
|
2022-04-03 15:12:52 -05:00
|
|
|
let dl = ecx.data_layout();
|
2021-07-15 13:33:08 -05:00
|
|
|
let neg_base_addr = (base_addr as i64).wrapping_neg();
|
2022-05-22 15:22:05 -05:00
|
|
|
Some((
|
|
|
|
alloc_id,
|
|
|
|
Size::from_bytes(dl.overflowing_signed_offset(addr.bytes(), neg_base_addr).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() {
|
2022-04-03 15:12:52 -05:00
|
|
|
assert_eq!(GlobalStateInner::align_addr(37, 4), 40);
|
|
|
|
assert_eq!(GlobalStateInner::align_addr(44, 4), 44);
|
2019-06-25 14:07:23 -05:00
|
|
|
}
|
2019-06-24 16:34:38 -05:00
|
|
|
}
|