2018-08-22 16:52:01 -03:00
|
|
|
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
|
//
|
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
2018-08-18 11:53:15 +02:00
|
|
|
//! The memory subsystem.
|
|
|
|
|
//!
|
|
|
|
|
//! Generally, we use `Pointer` to denote memory addresses. However, some operations
|
|
|
|
|
//! have a "size"-like parameter, and they take `Scalar` for the address because
|
|
|
|
|
//! if the size is 0, then the pointer can also be a (properly aligned, non-NULL)
|
|
|
|
|
//! integer. It is crucial that these operations call `check_align` *before*
|
|
|
|
|
//! short-circuiting the empty case!
|
|
|
|
|
|
2018-05-18 16:06:20 +02:00
|
|
|
use std::collections::VecDeque;
|
2018-04-26 09:18:19 +02:00
|
|
|
use std::ptr;
|
2016-03-05 00:48:23 -06:00
|
|
|
|
2018-08-23 21:22:27 +02:00
|
|
|
use rustc::ty::{self, Instance, query::TyCtxtAt};
|
|
|
|
|
use rustc::ty::layout::{self, Align, TargetDataLayout, Size, HasDataLayout};
|
2018-09-30 13:09:26 +02:00
|
|
|
use rustc::mir::interpret::{Pointer, AllocId, Allocation, ConstValue, GlobalId,
|
2018-08-26 20:42:52 +02:00
|
|
|
EvalResult, Scalar, EvalErrorKind, AllocType, PointerArithmetic,
|
|
|
|
|
truncate};
|
2018-08-16 11:38:16 +02:00
|
|
|
pub use rustc::mir::interpret::{write_target_uint, read_target_uint};
|
2018-08-05 00:01:11 +02:00
|
|
|
use rustc_data_structures::fx::{FxHashSet, FxHashMap};
|
2018-06-08 03:47:26 +01:00
|
|
|
|
|
|
|
|
use syntax::ast::Mutability;
|
2017-12-12 17:14:49 +01:00
|
|
|
|
2018-09-30 13:09:26 +02:00
|
|
|
use super::{Machine, ScalarMaybeUndef};
|
2016-04-04 20:33:41 -06:00
|
|
|
|
2018-08-23 19:04:33 +02:00
|
|
|
#[derive(Debug, PartialEq, Eq, Copy, Clone, Hash)]
|
2017-08-09 14:53:22 +02:00
|
|
|
pub enum MemoryKind<T> {
|
2017-07-13 16:58:36 +02:00
|
|
|
/// Error if deallocated except during a stack pop
|
2017-07-12 14:51:47 +02:00
|
|
|
Stack,
|
2017-07-28 16:48:43 +02:00
|
|
|
/// Additional memory kinds a machine wishes to distinguish from the builtin ones
|
|
|
|
|
Machine(T),
|
2016-04-04 20:33:41 -06:00
|
|
|
}
|
|
|
|
|
|
2018-09-20 10:12:21 +02:00
|
|
|
// `Memory` has to depend on the `Machine` because some of its operations
|
|
|
|
|
// (e.g. `get`) call a `Machine` hook.
|
|
|
|
|
pub struct Memory<'a, 'mir, 'tcx: 'a + 'mir, M: Machine<'a, 'mir, 'tcx>> {
|
2017-07-21 17:25:30 +02:00
|
|
|
/// Additional data required by the Machine
|
|
|
|
|
pub data: M::MemoryData,
|
2017-05-25 22:38:07 -07:00
|
|
|
|
2018-08-23 19:04:33 +02:00
|
|
|
/// Allocations local to this instance of the miri engine. The kind
|
|
|
|
|
/// helps ensure that the same mechanism is used for allocation and
|
2018-08-26 12:59:59 +02:00
|
|
|
/// deallocation. When an allocation is not found here, it is a
|
|
|
|
|
/// static and looked up in the `tcx` for read access. Writing to
|
|
|
|
|
/// a static creates a copy here, in the machine.
|
2018-08-23 19:04:33 +02:00
|
|
|
alloc_map: FxHashMap<AllocId, (MemoryKind<M::MemoryKinds>, Allocation)>,
|
2017-02-10 13:35:45 -08:00
|
|
|
|
2018-09-15 16:34:30 +02:00
|
|
|
/// To be able to compare pointers with NULL, and to check alignment for accesses
|
|
|
|
|
/// to ZSTs (where pointers may dangle), we keep track of the size even for allocations
|
|
|
|
|
/// that do not exist any more.
|
|
|
|
|
dead_alloc_map: FxHashMap<AllocId, (Size, Align)>,
|
|
|
|
|
|
2018-09-20 10:22:11 +02:00
|
|
|
/// Lets us implement `HasDataLayout`, which is awfully convenient.
|
|
|
|
|
pub(super) tcx: TyCtxtAt<'a, 'tcx, 'tcx>,
|
2016-03-23 21:40:58 -06:00
|
|
|
}
|
|
|
|
|
|
2018-09-20 10:12:21 +02:00
|
|
|
impl<'b, 'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> HasDataLayout
|
|
|
|
|
for &'b Memory<'a, 'mir, 'tcx, M>
|
|
|
|
|
{
|
2018-08-23 21:22:27 +02:00
|
|
|
#[inline]
|
|
|
|
|
fn data_layout(&self) -> &TargetDataLayout {
|
|
|
|
|
&self.tcx.data_layout
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-09-20 10:12:21 +02:00
|
|
|
impl<'a, 'b, 'c, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> HasDataLayout
|
2018-08-26 20:42:52 +02:00
|
|
|
for &'b &'c mut Memory<'a, 'mir, 'tcx, M>
|
|
|
|
|
{
|
|
|
|
|
#[inline]
|
|
|
|
|
fn data_layout(&self) -> &TargetDataLayout {
|
|
|
|
|
&self.tcx.data_layout
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-08-23 21:22:27 +02:00
|
|
|
|
2018-09-20 10:12:21 +02:00
|
|
|
// FIXME: Really we shouldnt clone memory, ever. Snapshot machinery should instad
|
|
|
|
|
// carefully copy only the reachable parts.
|
|
|
|
|
impl<'a, 'mir, 'tcx: 'a + 'mir, M: Machine<'a, 'mir, 'tcx>>
|
|
|
|
|
Clone for Memory<'a, 'mir, 'tcx, M>
|
|
|
|
|
where M::MemoryData: Clone
|
|
|
|
|
{
|
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
|
Memory {
|
|
|
|
|
data: self.data.clone(),
|
|
|
|
|
alloc_map: self.alloc_map.clone(),
|
|
|
|
|
dead_alloc_map: self.dead_alloc_map.clone(),
|
|
|
|
|
tcx: self.tcx,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
|
2018-02-06 18:33:59 +01:00
|
|
|
pub fn new(tcx: TyCtxtAt<'a, 'tcx, 'tcx>, data: M::MemoryData) -> Self {
|
2016-09-22 15:22:00 +02:00
|
|
|
Memory {
|
2017-07-21 17:25:30 +02:00
|
|
|
data,
|
2018-03-23 10:32:27 +01:00
|
|
|
alloc_map: FxHashMap::default(),
|
2018-09-15 16:34:30 +02:00
|
|
|
dead_alloc_map: FxHashMap::default(),
|
2017-12-06 09:25:29 +01:00
|
|
|
tcx,
|
2016-09-22 15:22:00 +02:00
|
|
|
}
|
2016-03-05 00:48:23 -06:00
|
|
|
}
|
|
|
|
|
|
2018-05-21 00:37:44 +02:00
|
|
|
pub fn create_fn_alloc(&mut self, instance: Instance<'tcx>) -> Pointer {
|
2018-05-21 00:46:06 +02:00
|
|
|
self.tcx.alloc_map.lock().create_fn_alloc(instance).into()
|
2016-06-08 13:43:34 +02:00
|
|
|
}
|
|
|
|
|
|
2018-08-23 19:04:33 +02:00
|
|
|
pub fn allocate_static_bytes(&mut self, bytes: &[u8]) -> Pointer {
|
2018-05-21 00:46:06 +02:00
|
|
|
self.tcx.allocate_bytes(bytes).into()
|
2017-02-10 13:35:33 -08:00
|
|
|
}
|
|
|
|
|
|
2018-08-23 19:04:33 +02:00
|
|
|
pub fn allocate_with(
|
2017-07-28 16:48:43 +02:00
|
|
|
&mut self,
|
2018-04-26 09:18:19 +02:00
|
|
|
alloc: Allocation,
|
2018-06-08 03:47:26 +01:00
|
|
|
kind: MemoryKind<M::MemoryKinds>,
|
2018-04-26 09:18:19 +02:00
|
|
|
) -> EvalResult<'tcx, AllocId> {
|
2018-05-02 06:03:06 +02:00
|
|
|
let id = self.tcx.alloc_map.lock().reserve();
|
2018-08-23 19:04:33 +02:00
|
|
|
self.alloc_map.insert(id, (kind, alloc));
|
2018-04-26 09:18:19 +02:00
|
|
|
Ok(id)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn allocate(
|
|
|
|
|
&mut self,
|
2018-05-19 16:37:29 +02:00
|
|
|
size: Size,
|
2018-04-26 09:18:19 +02:00
|
|
|
align: Align,
|
2018-06-08 03:47:26 +01:00
|
|
|
kind: MemoryKind<M::MemoryKinds>,
|
2018-05-21 00:37:44 +02:00
|
|
|
) -> EvalResult<'tcx, Pointer> {
|
2018-08-23 19:04:33 +02:00
|
|
|
self.allocate_with(Allocation::undef(size, align), kind).map(Pointer::from)
|
2016-03-05 00:48:23 -06:00
|
|
|
}
|
|
|
|
|
|
2017-07-28 16:48:43 +02:00
|
|
|
pub fn reallocate(
|
|
|
|
|
&mut self,
|
2018-05-21 00:37:44 +02:00
|
|
|
ptr: Pointer,
|
2018-05-19 16:37:29 +02:00
|
|
|
old_size: Size,
|
2017-12-17 08:47:22 +02:00
|
|
|
old_align: Align,
|
2018-05-19 16:37:29 +02:00
|
|
|
new_size: Size,
|
2017-12-17 08:47:22 +02:00
|
|
|
new_align: Align,
|
2017-08-09 14:53:22 +02:00
|
|
|
kind: MemoryKind<M::MemoryKinds>,
|
2018-05-21 00:37:44 +02:00
|
|
|
) -> EvalResult<'tcx, Pointer> {
|
2018-05-19 16:37:29 +02:00
|
|
|
if ptr.offset.bytes() != 0 {
|
2017-08-02 16:59:01 +02:00
|
|
|
return err!(ReallocateNonBasePtr);
|
2016-04-06 17:29:56 -06:00
|
|
|
}
|
|
|
|
|
|
2017-07-10 13:34:54 -07:00
|
|
|
// For simplicities' sake, we implement reallocate as "alloc, copy, dealloc"
|
2018-06-08 03:47:26 +01:00
|
|
|
let new_ptr = self.allocate(new_size, new_align, kind)?;
|
2017-08-10 08:48:38 -07:00
|
|
|
self.copy(
|
|
|
|
|
ptr.into(),
|
2017-12-17 08:47:22 +02:00
|
|
|
old_align,
|
2017-08-10 08:48:38 -07:00
|
|
|
new_ptr.into(),
|
2017-12-17 08:47:22 +02:00
|
|
|
new_align,
|
|
|
|
|
old_size.min(new_size),
|
2018-08-23 19:04:33 +02:00
|
|
|
/*nonoverlapping*/ true,
|
2017-08-10 08:48:38 -07:00
|
|
|
)?;
|
2017-07-12 14:51:47 +02:00
|
|
|
self.deallocate(ptr, Some((old_size, old_align)), kind)?;
|
2016-06-13 11:24:01 +02:00
|
|
|
|
2017-07-10 13:34:54 -07:00
|
|
|
Ok(new_ptr)
|
2016-04-06 17:29:56 -06:00
|
|
|
}
|
|
|
|
|
|
2018-08-23 19:04:33 +02:00
|
|
|
/// Deallocate a local, or do nothing if that local has been made into a static
|
2018-05-21 00:37:44 +02:00
|
|
|
pub fn deallocate_local(&mut self, ptr: Pointer) -> EvalResult<'tcx> {
|
2018-08-23 19:04:33 +02:00
|
|
|
// The allocation might be already removed by static interning.
|
|
|
|
|
// This can only really happen in the CTFE instance, not in miri.
|
|
|
|
|
if self.alloc_map.contains_key(&ptr.alloc_id) {
|
|
|
|
|
self.deallocate(ptr, None, MemoryKind::Stack)
|
|
|
|
|
} else {
|
|
|
|
|
Ok(())
|
2017-12-06 09:25:29 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-28 16:48:43 +02:00
|
|
|
pub fn deallocate(
|
|
|
|
|
&mut self,
|
2018-05-21 00:37:44 +02:00
|
|
|
ptr: Pointer,
|
2018-05-19 16:37:29 +02:00
|
|
|
size_and_align: Option<(Size, Align)>,
|
2017-08-09 14:53:22 +02:00
|
|
|
kind: MemoryKind<M::MemoryKinds>,
|
2017-07-28 16:48:43 +02:00
|
|
|
) -> EvalResult<'tcx> {
|
2018-09-15 16:34:30 +02:00
|
|
|
debug!("deallocating: {}", ptr.alloc_id);
|
|
|
|
|
|
2018-05-19 16:37:29 +02:00
|
|
|
if ptr.offset.bytes() != 0 {
|
2017-08-02 16:59:01 +02:00
|
|
|
return err!(DeallocateNonBasePtr);
|
2016-11-17 14:48:34 +01:00
|
|
|
}
|
2016-04-07 03:02:02 -06:00
|
|
|
|
2018-08-23 19:04:33 +02:00
|
|
|
let (alloc_kind, alloc) = match self.alloc_map.remove(&ptr.alloc_id) {
|
2017-12-06 09:25:29 +01:00
|
|
|
Some(alloc) => alloc,
|
2018-06-08 03:47:26 +01:00
|
|
|
None => {
|
2018-08-23 19:04:33 +02:00
|
|
|
// Deallocating static memory -- always an error
|
2018-05-02 06:03:06 +02:00
|
|
|
return match self.tcx.alloc_map.lock().get(ptr.alloc_id) {
|
|
|
|
|
Some(AllocType::Function(..)) => err!(DeallocatedWrongMemoryKind(
|
|
|
|
|
"function".to_string(),
|
|
|
|
|
format!("{:?}", kind),
|
|
|
|
|
)),
|
|
|
|
|
Some(AllocType::Static(..)) |
|
|
|
|
|
Some(AllocType::Memory(..)) => err!(DeallocatedWrongMemoryKind(
|
|
|
|
|
"static".to_string(),
|
|
|
|
|
format!("{:?}", kind),
|
|
|
|
|
)),
|
|
|
|
|
None => err!(DoubleFree)
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-08-08 13:06:14 +02:00
|
|
|
};
|
|
|
|
|
|
2017-12-06 09:25:29 +01:00
|
|
|
if alloc_kind != kind {
|
2017-08-10 08:48:38 -07:00
|
|
|
return err!(DeallocatedWrongMemoryKind(
|
2017-12-06 09:25:29 +01:00
|
|
|
format!("{:?}", alloc_kind),
|
2017-08-10 08:48:38 -07:00
|
|
|
format!("{:?}", kind),
|
|
|
|
|
));
|
2017-07-12 14:51:47 +02:00
|
|
|
}
|
|
|
|
|
if let Some((size, align)) = size_and_align {
|
2018-05-19 16:37:29 +02:00
|
|
|
if size.bytes() != alloc.bytes.len() as u64 || align != alloc.align {
|
2018-08-22 16:59:14 -03:00
|
|
|
let bytes = Size::from_bytes(alloc.bytes.len() as u64);
|
|
|
|
|
return err!(IncorrectAllocationInformation(size,
|
|
|
|
|
bytes,
|
|
|
|
|
align,
|
|
|
|
|
alloc.align));
|
2017-07-03 16:47:58 -07:00
|
|
|
}
|
2016-04-07 03:02:02 -06:00
|
|
|
}
|
2017-07-03 16:47:58 -07:00
|
|
|
|
2018-09-15 16:34:30 +02:00
|
|
|
// Don't forget to remember size and align of this now-dead allocation
|
|
|
|
|
let old = self.dead_alloc_map.insert(
|
|
|
|
|
ptr.alloc_id,
|
|
|
|
|
(Size::from_bytes(alloc.bytes.len() as u64), alloc.align)
|
|
|
|
|
);
|
|
|
|
|
if old.is_some() {
|
|
|
|
|
bug!("Nothing can be deallocated twice");
|
|
|
|
|
}
|
2016-04-07 03:02:02 -06:00
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
2016-06-23 09:59:16 +02:00
|
|
|
|
2018-09-15 16:34:30 +02:00
|
|
|
/// Check that the pointer is aligned AND non-NULL. This supports ZSTs in two ways:
|
|
|
|
|
/// You can pass a scalar, and a `Pointer` does not have to actually still be allocated.
|
2018-05-21 00:30:00 +02:00
|
|
|
pub fn check_align(&self, ptr: Scalar, required_align: Align) -> EvalResult<'tcx> {
|
2017-08-25 14:41:59 +02:00
|
|
|
// Check non-NULL/Undef, extract offset
|
2018-05-21 00:30:00 +02:00
|
|
|
let (offset, alloc_align) = match ptr {
|
2018-05-20 23:43:16 +02:00
|
|
|
Scalar::Ptr(ptr) => {
|
2018-09-15 16:34:30 +02:00
|
|
|
let (size, align) = self.get_size_and_align(ptr.alloc_id)?;
|
|
|
|
|
// check this is not NULL -- which we can ensure only if this is in-bounds
|
|
|
|
|
// of some (potentially dead) allocation.
|
|
|
|
|
if ptr.offset > size {
|
|
|
|
|
return err!(PointerOutOfBounds {
|
|
|
|
|
ptr,
|
|
|
|
|
access: true,
|
|
|
|
|
allocation_size: size,
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
// keep data for alignment check
|
|
|
|
|
(ptr.offset.bytes(), align)
|
2017-08-10 08:48:38 -07:00
|
|
|
}
|
2018-07-24 18:28:53 +02:00
|
|
|
Scalar::Bits { bits, size } => {
|
|
|
|
|
assert_eq!(size as u64, self.pointer_size().bytes());
|
2018-08-30 11:39:40 +02:00
|
|
|
assert!(bits < (1u128 << self.pointer_size().bits()));
|
2018-09-15 16:34:30 +02:00
|
|
|
// check this is not NULL
|
2018-08-30 11:39:40 +02:00
|
|
|
if bits == 0 {
|
2017-08-02 16:59:01 +02:00
|
|
|
return err!(InvalidNullPointerUsage);
|
2017-07-03 14:16:11 +02:00
|
|
|
}
|
2018-08-30 11:39:40 +02:00
|
|
|
// the "base address" is 0 and hence always aligned
|
|
|
|
|
(bits as u64, required_align)
|
2017-08-10 08:48:38 -07:00
|
|
|
}
|
2017-01-31 10:36:27 +01:00
|
|
|
};
|
2017-08-25 14:41:59 +02:00
|
|
|
// Check alignment
|
2017-12-17 08:47:22 +02:00
|
|
|
if alloc_align.abi() < required_align.abi() {
|
2017-08-25 14:41:59 +02:00
|
|
|
return err!(AlignmentCheckFailed {
|
2018-05-19 16:37:29 +02:00
|
|
|
has: alloc_align,
|
|
|
|
|
required: required_align,
|
2017-08-25 14:41:59 +02:00
|
|
|
});
|
|
|
|
|
}
|
2017-12-17 08:47:22 +02:00
|
|
|
if offset % required_align.abi() == 0 {
|
2016-07-22 16:35:39 +02:00
|
|
|
Ok(())
|
|
|
|
|
} else {
|
2018-05-19 16:37:29 +02:00
|
|
|
let has = offset % required_align.abi();
|
2017-08-02 16:59:01 +02:00
|
|
|
err!(AlignmentCheckFailed {
|
2018-05-19 16:37:29 +02:00
|
|
|
has: Align::from_bytes(has, has).unwrap(),
|
|
|
|
|
required: required_align,
|
2016-07-22 16:35:39 +02:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-01-30 09:44:52 +01:00
|
|
|
|
2018-08-13 16:14:22 +02:00
|
|
|
/// Check if the pointer is "in-bounds". Notice that a pointer pointing at the end
|
|
|
|
|
/// of an allocation (i.e., at the first *inaccessible* location) *is* considered
|
2018-08-28 17:49:24 +02:00
|
|
|
/// in-bounds! This follows C's/LLVM's rules. The `access` boolean is just used
|
|
|
|
|
/// for the error message.
|
|
|
|
|
/// If you want to check bounds before doing a memory access, be sure to
|
|
|
|
|
/// check the pointer one past the end of your access, then everything will
|
|
|
|
|
/// work out exactly.
|
2018-10-02 17:02:58 +02:00
|
|
|
pub fn check_bounds_ptr(&self, ptr: Pointer, access: bool) -> EvalResult<'tcx> {
|
2017-06-04 19:31:34 -07:00
|
|
|
let alloc = self.get(ptr.alloc_id)?;
|
|
|
|
|
let allocation_size = alloc.bytes.len() as u64;
|
2018-05-19 16:37:29 +02:00
|
|
|
if ptr.offset.bytes() > allocation_size {
|
2017-08-10 08:48:38 -07:00
|
|
|
return err!(PointerOutOfBounds {
|
|
|
|
|
ptr,
|
|
|
|
|
access,
|
2018-05-19 16:37:29 +02:00
|
|
|
allocation_size: Size::from_bytes(allocation_size),
|
2017-08-10 08:48:38 -07:00
|
|
|
});
|
2017-06-04 19:31:34 -07:00
|
|
|
}
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
2018-10-02 17:02:58 +02:00
|
|
|
|
|
|
|
|
/// Check if the memory range beginning at `ptr` and of size `Size` is "in-bounds".
|
|
|
|
|
#[inline(always)]
|
|
|
|
|
pub fn check_bounds(&self, ptr: Pointer, size: Size, access: bool) -> EvalResult<'tcx> {
|
|
|
|
|
// if ptr.offset is in bounds, then so is ptr (because offset checks for overflow)
|
|
|
|
|
self.check_bounds_ptr(ptr.offset(size, &*self)?, access)
|
|
|
|
|
}
|
2016-06-23 09:40:01 +02:00
|
|
|
}
|
2016-04-07 03:02:02 -06:00
|
|
|
|
2016-06-23 09:40:01 +02:00
|
|
|
/// Allocation accessors
|
2018-09-20 10:12:21 +02:00
|
|
|
impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
|
2018-08-27 13:34:12 +02:00
|
|
|
/// Helper function to obtain the global (tcx) allocation for a static
|
|
|
|
|
fn get_static_alloc(
|
|
|
|
|
tcx: TyCtxtAt<'a, 'tcx, 'tcx>,
|
|
|
|
|
id: AllocId,
|
|
|
|
|
) -> EvalResult<'tcx, &'tcx Allocation> {
|
|
|
|
|
let alloc = tcx.alloc_map.lock().get(id);
|
|
|
|
|
let def_id = match alloc {
|
|
|
|
|
Some(AllocType::Memory(mem)) => {
|
|
|
|
|
return Ok(mem)
|
|
|
|
|
}
|
|
|
|
|
Some(AllocType::Function(..)) => {
|
|
|
|
|
return err!(DerefFunctionPointer)
|
|
|
|
|
}
|
|
|
|
|
Some(AllocType::Static(did)) => {
|
|
|
|
|
did
|
|
|
|
|
}
|
|
|
|
|
None =>
|
|
|
|
|
return err!(DanglingPointerDeref),
|
|
|
|
|
};
|
|
|
|
|
// We got a "lazy" static that has not been computed yet, do some work
|
|
|
|
|
trace!("static_alloc: Need to compute {:?}", def_id);
|
|
|
|
|
if tcx.is_foreign_item(def_id) {
|
|
|
|
|
return M::find_foreign_static(tcx, def_id);
|
|
|
|
|
}
|
|
|
|
|
let instance = Instance::mono(tcx.tcx, def_id);
|
|
|
|
|
let gid = GlobalId {
|
|
|
|
|
instance,
|
|
|
|
|
promoted: None,
|
|
|
|
|
};
|
|
|
|
|
tcx.const_eval(ty::ParamEnv::reveal_all().and(gid)).map_err(|err| {
|
|
|
|
|
// no need to report anything, the const_eval call takes care of that for statics
|
|
|
|
|
assert!(tcx.is_static(def_id).is_some());
|
|
|
|
|
EvalErrorKind::ReferencedConstant(err).into()
|
2018-08-31 18:31:15 +08:00
|
|
|
}).map(|const_val| {
|
|
|
|
|
if let ConstValue::ByRef(_, allocation, _) = const_val.val {
|
|
|
|
|
allocation
|
|
|
|
|
} else {
|
2018-09-01 18:13:28 +08:00
|
|
|
bug!("Matching on non-ByRef static")
|
2018-08-31 18:31:15 +08:00
|
|
|
}
|
2018-08-27 13:34:12 +02:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-06 09:25:29 +01:00
|
|
|
pub fn get(&self, id: AllocId) -> EvalResult<'tcx, &Allocation> {
|
2018-01-05 05:12:38 +02:00
|
|
|
match self.alloc_map.get(&id) {
|
2018-08-26 12:59:59 +02:00
|
|
|
// Normal alloc?
|
2018-08-23 19:04:33 +02:00
|
|
|
Some(alloc) => Ok(&alloc.1),
|
2018-08-26 12:59:59 +02:00
|
|
|
// Static. No need to make any copies, just provide read access to the global static
|
2018-08-23 19:04:33 +02:00
|
|
|
// memory in tcx.
|
2018-08-27 13:34:12 +02:00
|
|
|
None => Self::get_static_alloc(self.tcx, id),
|
2016-06-13 11:39:15 +02:00
|
|
|
}
|
2016-03-05 00:48:23 -06:00
|
|
|
}
|
2017-08-10 08:48:38 -07:00
|
|
|
|
2018-09-15 16:34:30 +02:00
|
|
|
pub fn get_size_and_align(&self, id: AllocId) -> EvalResult<'tcx, (Size, Align)> {
|
|
|
|
|
Ok(match self.get(id) {
|
|
|
|
|
Ok(alloc) => (Size::from_bytes(alloc.bytes.len() as u64), alloc.align),
|
|
|
|
|
Err(err) => match err.kind {
|
|
|
|
|
EvalErrorKind::DanglingPointerDeref =>
|
|
|
|
|
// This should be in the dead allocation map
|
|
|
|
|
*self.dead_alloc_map.get(&id).expect(
|
|
|
|
|
"allocation missing in dead_alloc_map"
|
|
|
|
|
),
|
|
|
|
|
// E.g. a function ptr allocation
|
|
|
|
|
_ => return Err(err)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-23 19:04:33 +02:00
|
|
|
pub fn get_mut(
|
2017-08-10 08:48:38 -07:00
|
|
|
&mut self,
|
|
|
|
|
id: AllocId,
|
2017-12-06 09:25:29 +01:00
|
|
|
) -> EvalResult<'tcx, &mut Allocation> {
|
2018-08-23 19:04:33 +02:00
|
|
|
// Static?
|
2018-08-26 12:59:59 +02:00
|
|
|
if !self.alloc_map.contains_key(&id) {
|
|
|
|
|
// Ask the machine for what to do
|
|
|
|
|
if let Some(kind) = M::MUT_STATIC_KIND {
|
|
|
|
|
// The machine supports mutating statics. Make a copy, use that.
|
|
|
|
|
self.deep_copy_static(id, MemoryKind::Machine(kind))?;
|
|
|
|
|
} else {
|
|
|
|
|
return err!(ModifiedConstantMemory)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// If we come here, we know the allocation is in our map
|
|
|
|
|
let alloc = &mut self.alloc_map.get_mut(&id).unwrap().1;
|
|
|
|
|
// See if we are allowed to mutate this
|
2018-08-23 19:04:33 +02:00
|
|
|
if alloc.mutability == Mutability::Immutable {
|
|
|
|
|
err!(ModifiedConstantMemory)
|
|
|
|
|
} else {
|
|
|
|
|
Ok(alloc)
|
2017-07-13 20:33:06 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-21 00:37:44 +02:00
|
|
|
pub fn get_fn(&self, ptr: Pointer) -> EvalResult<'tcx, Instance<'tcx>> {
|
2018-05-19 16:37:29 +02:00
|
|
|
if ptr.offset.bytes() != 0 {
|
2017-08-02 16:59:01 +02:00
|
|
|
return err!(InvalidFunctionPointer);
|
2017-06-21 21:45:51 -07:00
|
|
|
}
|
2018-08-27 13:34:12 +02:00
|
|
|
trace!("reading fn ptr: {}", ptr.alloc_id);
|
2018-05-02 06:03:06 +02:00
|
|
|
match self.tcx.alloc_map.lock().get(ptr.alloc_id) {
|
|
|
|
|
Some(AllocType::Function(instance)) => Ok(instance),
|
|
|
|
|
_ => Err(EvalErrorKind::ExecuteMemory.into()),
|
|
|
|
|
}
|
2016-06-08 13:43:34 +02:00
|
|
|
}
|
|
|
|
|
|
2016-12-07 22:00:46 -08:00
|
|
|
/// For debugging, print an allocation and all allocations it points to, recursively.
|
|
|
|
|
pub fn dump_alloc(&self, id: AllocId) {
|
2018-04-26 15:35:24 +10:00
|
|
|
if !log_enabled!(::log::Level::Trace) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2016-12-07 22:00:46 -08:00
|
|
|
self.dump_allocs(vec![id]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// For debugging, print a list of allocations and all allocations they point to, recursively.
|
|
|
|
|
pub fn dump_allocs(&self, mut allocs: Vec<AllocId>) {
|
2018-04-26 15:35:24 +10:00
|
|
|
if !log_enabled!(::log::Level::Trace) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2016-09-22 13:01:08 +02:00
|
|
|
use std::fmt::Write;
|
2016-12-07 22:00:46 -08:00
|
|
|
allocs.sort();
|
|
|
|
|
allocs.dedup();
|
|
|
|
|
let mut allocs_to_print = VecDeque::from(allocs);
|
2018-03-23 10:32:27 +01:00
|
|
|
let mut allocs_seen = FxHashSet::default();
|
2016-04-06 03:45:06 -06:00
|
|
|
|
|
|
|
|
while let Some(id) = allocs_to_print.pop_front() {
|
2016-09-22 13:01:08 +02:00
|
|
|
let mut msg = format!("Alloc {:<5} ", format!("{}:", id));
|
|
|
|
|
let prefix_len = msg.len();
|
2016-04-06 03:45:06 -06:00
|
|
|
let mut relocations = vec![];
|
|
|
|
|
|
2017-12-06 09:25:29 +01:00
|
|
|
let (alloc, immutable) =
|
|
|
|
|
// normal alloc?
|
2018-01-05 05:12:38 +02:00
|
|
|
match self.alloc_map.get(&id) {
|
2018-08-23 19:04:33 +02:00
|
|
|
Some((kind, alloc)) => (alloc, match kind {
|
2017-12-06 09:25:29 +01:00
|
|
|
MemoryKind::Stack => " (stack)".to_owned(),
|
|
|
|
|
MemoryKind::Machine(m) => format!(" ({:?})", m),
|
|
|
|
|
}),
|
2018-06-08 03:47:26 +01:00
|
|
|
None => {
|
|
|
|
|
// static alloc?
|
|
|
|
|
match self.tcx.alloc_map.lock().get(id) {
|
2018-08-23 19:04:33 +02:00
|
|
|
Some(AllocType::Memory(a)) => (a, " (immutable)".to_owned()),
|
2018-06-08 03:47:26 +01:00
|
|
|
Some(AllocType::Function(func)) => {
|
|
|
|
|
trace!("{} {}", msg, func);
|
|
|
|
|
continue;
|
2018-01-16 09:31:48 +01:00
|
|
|
}
|
2018-06-08 03:47:26 +01:00
|
|
|
Some(AllocType::Static(did)) => {
|
|
|
|
|
trace!("{} {:?}", msg, did);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
None => {
|
|
|
|
|
trace!("{} (deallocated)", msg);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-12-06 09:25:29 +01:00
|
|
|
},
|
2018-01-16 09:31:48 +01:00
|
|
|
};
|
2016-04-07 03:07:57 -06:00
|
|
|
|
2016-11-18 12:55:14 +01:00
|
|
|
for i in 0..(alloc.bytes.len() as u64) {
|
2018-05-19 16:37:29 +02:00
|
|
|
let i = Size::from_bytes(i);
|
2016-04-06 03:45:06 -06:00
|
|
|
if let Some(&target_id) = alloc.relocations.get(&i) {
|
2017-02-06 09:26:01 -08:00
|
|
|
if allocs_seen.insert(target_id) {
|
2016-04-06 03:45:06 -06:00
|
|
|
allocs_to_print.push_back(target_id);
|
|
|
|
|
}
|
2016-04-09 19:31:53 -06:00
|
|
|
relocations.push((i, target_id));
|
2016-04-06 03:45:06 -06:00
|
|
|
}
|
2018-09-02 17:22:46 +03:00
|
|
|
if alloc.undef_mask.is_range_defined(i, i + Size::from_bytes(1)).is_ok() {
|
2016-11-18 12:55:14 +01:00
|
|
|
// this `as usize` is fine, since `i` came from a `usize`
|
2018-05-19 16:37:29 +02:00
|
|
|
write!(msg, "{:02x} ", alloc.bytes[i.bytes() as usize]).unwrap();
|
2016-04-06 03:45:06 -06:00
|
|
|
} else {
|
2016-09-22 13:01:08 +02:00
|
|
|
msg.push_str("__ ");
|
2016-04-06 03:45:06 -06:00
|
|
|
}
|
|
|
|
|
}
|
2016-09-19 04:10:51 -06:00
|
|
|
|
2017-08-10 08:48:38 -07:00
|
|
|
trace!(
|
|
|
|
|
"{}({} bytes, alignment {}){}",
|
|
|
|
|
msg,
|
|
|
|
|
alloc.bytes.len(),
|
2017-12-17 08:47:22 +02:00
|
|
|
alloc.align.abi(),
|
2017-08-10 08:48:38 -07:00
|
|
|
immutable
|
|
|
|
|
);
|
2016-04-06 03:45:06 -06:00
|
|
|
|
|
|
|
|
if !relocations.is_empty() {
|
2016-09-22 13:01:08 +02:00
|
|
|
msg.clear();
|
|
|
|
|
write!(msg, "{:1$}", "", prefix_len).unwrap(); // Print spaces.
|
2018-05-20 14:14:39 +02:00
|
|
|
let mut pos = Size::ZERO;
|
2018-05-19 16:37:29 +02:00
|
|
|
let relocation_width = (self.pointer_size().bytes() - 1) * 3;
|
2016-04-06 03:45:06 -06:00
|
|
|
for (i, target_id) in relocations {
|
2016-11-18 12:55:14 +01:00
|
|
|
// this `as usize` is fine, since we can't print more chars than `usize::MAX`
|
2018-05-19 16:37:29 +02:00
|
|
|
write!(msg, "{:1$}", "", ((i - pos) * 3).bytes() as usize).unwrap();
|
2017-06-23 12:55:49 +02:00
|
|
|
let target = format!("({})", target_id);
|
2016-11-18 12:55:14 +01:00
|
|
|
// this `as usize` is fine, since we can't print more chars than `usize::MAX`
|
|
|
|
|
write!(msg, "└{0:─^1$}┘ ", target, relocation_width as usize).unwrap();
|
2016-06-23 10:00:31 +02:00
|
|
|
pos = i + self.pointer_size();
|
2016-04-06 03:45:06 -06:00
|
|
|
}
|
2016-09-22 13:01:08 +02:00
|
|
|
trace!("{}", msg);
|
2016-04-06 03:45:06 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-02-14 15:35:13 +01:00
|
|
|
|
|
|
|
|
pub fn leak_report(&self) -> usize {
|
|
|
|
|
trace!("### LEAK REPORT ###");
|
2018-08-26 12:59:59 +02:00
|
|
|
let mut_static_kind = M::MUT_STATIC_KIND.map(|k| MemoryKind::Machine(k));
|
2017-02-14 15:35:13 +01:00
|
|
|
let leaks: Vec<_> = self.alloc_map
|
2018-08-23 19:04:33 +02:00
|
|
|
.iter()
|
2018-08-26 12:59:59 +02:00
|
|
|
.filter_map(|(&id, &(kind, _))|
|
|
|
|
|
// exclude mutable statics
|
|
|
|
|
if Some(kind) == mut_static_kind { None } else { Some(id) } )
|
2017-02-14 15:35:13 +01:00
|
|
|
.collect();
|
|
|
|
|
let n = leaks.len();
|
|
|
|
|
self.dump_allocs(leaks);
|
|
|
|
|
n
|
|
|
|
|
}
|
2016-06-23 09:40:01 +02:00
|
|
|
}
|
2016-04-06 03:45:06 -06:00
|
|
|
|
2016-06-23 09:40:01 +02:00
|
|
|
/// Byte accessors
|
2018-09-20 10:12:21 +02:00
|
|
|
impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
|
2018-08-28 17:49:24 +02:00
|
|
|
/// The last argument controls whether we error out when there are undefined
|
|
|
|
|
/// or pointer bytes. You should never call this, call `get_bytes` or
|
2018-08-29 10:07:27 +02:00
|
|
|
/// `get_bytes_with_undef_and_ptr` instead,
|
2018-08-28 17:49:24 +02:00
|
|
|
fn get_bytes_internal(
|
2017-08-10 08:48:38 -07:00
|
|
|
&self,
|
2018-05-21 00:37:44 +02:00
|
|
|
ptr: Pointer,
|
2018-05-19 16:37:29 +02:00
|
|
|
size: Size,
|
2017-12-17 08:47:22 +02:00
|
|
|
align: Align,
|
2018-08-28 17:49:24 +02:00
|
|
|
check_defined_and_ptr: bool,
|
2017-08-10 08:48:38 -07:00
|
|
|
) -> EvalResult<'tcx, &[u8]> {
|
2018-08-28 17:49:24 +02:00
|
|
|
assert_ne!(size.bytes(), 0, "0-sized accesses should never even get a `Pointer`");
|
2017-12-17 08:47:22 +02:00
|
|
|
self.check_align(ptr.into(), align)?;
|
2018-10-02 17:02:58 +02:00
|
|
|
self.check_bounds(ptr, size, true)?;
|
2018-08-28 17:49:24 +02:00
|
|
|
|
|
|
|
|
if check_defined_and_ptr {
|
|
|
|
|
self.check_defined(ptr, size)?;
|
2018-08-29 10:07:27 +02:00
|
|
|
self.check_relocations(ptr, size)?;
|
|
|
|
|
} else {
|
|
|
|
|
// We still don't want relocations on the *edges*
|
|
|
|
|
self.check_relocation_edges(ptr, size)?;
|
2018-08-28 17:49:24 +02:00
|
|
|
}
|
|
|
|
|
|
2016-05-09 18:52:44 -06:00
|
|
|
let alloc = self.get(ptr.alloc_id)?;
|
2018-05-19 16:37:29 +02:00
|
|
|
assert_eq!(ptr.offset.bytes() as usize as u64, ptr.offset.bytes());
|
|
|
|
|
assert_eq!(size.bytes() as usize as u64, size.bytes());
|
|
|
|
|
let offset = ptr.offset.bytes() as usize;
|
|
|
|
|
Ok(&alloc.bytes[offset..offset + size.bytes() as usize])
|
2016-03-05 00:48:23 -06:00
|
|
|
}
|
|
|
|
|
|
2018-08-28 17:49:24 +02:00
|
|
|
#[inline]
|
|
|
|
|
fn get_bytes(&self, ptr: Pointer, size: Size, align: Align) -> EvalResult<'tcx, &[u8]> {
|
|
|
|
|
self.get_bytes_internal(ptr, size, align, true)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// It is the caller's responsibility to handle undefined and pointer bytes.
|
2018-08-29 10:07:27 +02:00
|
|
|
/// However, this still checks that there are no relocations on the egdes.
|
2018-08-28 17:49:24 +02:00
|
|
|
#[inline]
|
|
|
|
|
fn get_bytes_with_undef_and_ptr(
|
|
|
|
|
&self,
|
|
|
|
|
ptr: Pointer,
|
|
|
|
|
size: Size,
|
|
|
|
|
align: Align
|
|
|
|
|
) -> EvalResult<'tcx, &[u8]> {
|
|
|
|
|
self.get_bytes_internal(ptr, size, align, false)
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-29 10:07:27 +02:00
|
|
|
/// Just calling this already marks everything as defined and removes relocations,
|
|
|
|
|
/// so be sure to actually put data there!
|
2018-08-28 17:49:24 +02:00
|
|
|
fn get_bytes_mut(
|
2017-08-10 08:48:38 -07:00
|
|
|
&mut self,
|
2018-05-21 00:37:44 +02:00
|
|
|
ptr: Pointer,
|
2018-05-19 16:37:29 +02:00
|
|
|
size: Size,
|
2017-12-17 08:47:22 +02:00
|
|
|
align: Align,
|
2017-08-10 08:48:38 -07:00
|
|
|
) -> EvalResult<'tcx, &mut [u8]> {
|
2018-08-28 17:49:24 +02:00
|
|
|
assert_ne!(size.bytes(), 0, "0-sized accesses should never even get a `Pointer`");
|
2017-12-17 08:47:22 +02:00
|
|
|
self.check_align(ptr.into(), align)?;
|
2018-10-02 17:02:58 +02:00
|
|
|
self.check_bounds(ptr, size, true)?;
|
2018-08-28 17:49:24 +02:00
|
|
|
|
|
|
|
|
self.mark_definedness(ptr, size, true)?;
|
|
|
|
|
self.clear_relocations(ptr, size)?;
|
|
|
|
|
|
2016-05-09 18:52:44 -06:00
|
|
|
let alloc = self.get_mut(ptr.alloc_id)?;
|
2018-05-19 16:37:29 +02:00
|
|
|
assert_eq!(ptr.offset.bytes() as usize as u64, ptr.offset.bytes());
|
|
|
|
|
assert_eq!(size.bytes() as usize as u64, size.bytes());
|
|
|
|
|
let offset = ptr.offset.bytes() as usize;
|
|
|
|
|
Ok(&mut alloc.bytes[offset..offset + size.bytes() as usize])
|
2016-03-05 00:48:23 -06:00
|
|
|
}
|
2016-06-23 09:40:01 +02:00
|
|
|
}
|
2016-03-21 05:27:34 -06:00
|
|
|
|
2016-06-23 09:40:01 +02:00
|
|
|
/// Reading and writing
|
2018-09-20 10:12:21 +02:00
|
|
|
impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
|
2017-02-08 16:27:28 +01:00
|
|
|
/// mark an allocation as static and initialized, either mutable or not
|
2018-08-24 17:36:18 +02:00
|
|
|
pub fn intern_static(
|
2017-08-10 08:48:38 -07:00
|
|
|
&mut self,
|
|
|
|
|
alloc_id: AllocId,
|
|
|
|
|
mutability: Mutability,
|
|
|
|
|
) -> EvalResult<'tcx> {
|
|
|
|
|
trace!(
|
2018-01-16 10:16:38 +01:00
|
|
|
"mark_static_initialized {:?}, mutability: {:?}",
|
2017-08-10 08:48:38 -07:00
|
|
|
alloc_id,
|
|
|
|
|
mutability
|
|
|
|
|
);
|
2018-08-23 19:04:33 +02:00
|
|
|
// remove allocation
|
|
|
|
|
let (kind, mut alloc) = self.alloc_map.remove(&alloc_id).unwrap();
|
|
|
|
|
match kind {
|
|
|
|
|
MemoryKind::Machine(_) => bug!("Static cannot refer to machine memory"),
|
|
|
|
|
MemoryKind::Stack => {},
|
|
|
|
|
}
|
|
|
|
|
// ensure llvm knows not to put this into immutable memory
|
|
|
|
|
alloc.mutability = mutability;
|
|
|
|
|
let alloc = self.tcx.intern_const_alloc(alloc);
|
|
|
|
|
self.tcx.alloc_map.lock().set_id_memory(alloc_id, alloc);
|
|
|
|
|
// recurse into inner allocations
|
|
|
|
|
for &alloc in alloc.relocations.values() {
|
|
|
|
|
// FIXME: Reusing the mutability here is likely incorrect. It is originally
|
|
|
|
|
// determined via `is_freeze`, and data is considered frozen if there is no
|
|
|
|
|
// `UnsafeCell` *immediately* in that data -- however, this search stops
|
|
|
|
|
// at references. So whenever we follow a reference, we should likely
|
|
|
|
|
// assume immutability -- and we should make sure that the compiler
|
|
|
|
|
// does not permit code that would break this!
|
2018-08-24 17:36:18 +02:00
|
|
|
if self.alloc_map.contains_key(&alloc) {
|
|
|
|
|
// Not yet interned, so proceed recursively
|
|
|
|
|
self.intern_static(alloc, mutability)?;
|
|
|
|
|
}
|
2017-12-06 09:25:29 +01:00
|
|
|
}
|
2018-08-23 19:04:33 +02:00
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// The alloc_id must refer to a (mutable) static; a deep copy of that
|
|
|
|
|
/// static is made into this memory.
|
2018-08-26 12:59:59 +02:00
|
|
|
fn deep_copy_static(
|
2018-08-23 19:04:33 +02:00
|
|
|
&mut self,
|
|
|
|
|
id: AllocId,
|
|
|
|
|
kind: MemoryKind<M::MemoryKinds>,
|
|
|
|
|
) -> EvalResult<'tcx> {
|
2018-08-27 13:34:12 +02:00
|
|
|
let alloc = Self::get_static_alloc(self.tcx, id)?;
|
2018-08-23 19:04:33 +02:00
|
|
|
if alloc.mutability == Mutability::Immutable {
|
|
|
|
|
return err!(ModifiedConstantMemory);
|
2016-09-19 04:10:18 -06:00
|
|
|
}
|
2018-08-23 19:04:33 +02:00
|
|
|
let old = self.alloc_map.insert(id, (kind, alloc.clone()));
|
2018-08-26 12:59:59 +02:00
|
|
|
assert!(old.is_none(), "deep_copy_static: must not overwrite existing memory");
|
2016-09-09 17:44:04 +02:00
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-10 08:48:38 -07:00
|
|
|
pub fn copy(
|
|
|
|
|
&mut self,
|
2018-05-21 00:30:00 +02:00
|
|
|
src: Scalar,
|
2017-12-17 08:47:22 +02:00
|
|
|
src_align: Align,
|
2018-05-21 00:30:00 +02:00
|
|
|
dest: Scalar,
|
2017-12-17 08:47:22 +02:00
|
|
|
dest_align: Align,
|
2018-05-19 16:37:29 +02:00
|
|
|
size: Size,
|
2017-08-10 08:48:38 -07:00
|
|
|
nonoverlapping: bool,
|
2018-06-26 22:59:10 -04:00
|
|
|
) -> EvalResult<'tcx> {
|
|
|
|
|
self.copy_repeatedly(src, src_align, dest, dest_align, size, 1, nonoverlapping)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn copy_repeatedly(
|
|
|
|
|
&mut self,
|
|
|
|
|
src: Scalar,
|
|
|
|
|
src_align: Align,
|
|
|
|
|
dest: Scalar,
|
|
|
|
|
dest_align: Align,
|
|
|
|
|
size: Size,
|
|
|
|
|
length: u64,
|
|
|
|
|
nonoverlapping: bool,
|
2017-08-10 08:48:38 -07:00
|
|
|
) -> EvalResult<'tcx> {
|
2018-05-19 16:37:29 +02:00
|
|
|
if size.bytes() == 0 {
|
2018-08-18 11:53:15 +02:00
|
|
|
// Nothing to do for ZST, other than checking alignment and non-NULLness.
|
|
|
|
|
self.check_align(src, src_align)?;
|
|
|
|
|
self.check_align(dest, dest_align)?;
|
2016-09-22 15:22:00 +02:00
|
|
|
return Ok(());
|
|
|
|
|
}
|
2017-06-20 14:26:50 +02:00
|
|
|
let src = src.to_ptr()?;
|
|
|
|
|
let dest = dest.to_ptr()?;
|
2016-03-21 05:27:34 -06:00
|
|
|
|
2017-08-28 14:08:55 +02:00
|
|
|
// first copy the relocations to a temporary buffer, because
|
|
|
|
|
// `get_bytes_mut` will clear the relocations, which is correct,
|
|
|
|
|
// since we don't want to keep any relocations at the target.
|
2018-08-29 10:07:27 +02:00
|
|
|
// (`get_bytes_with_undef_and_ptr` below checks that there are no
|
|
|
|
|
// relocations overlapping the edges; those would not be handled correctly).
|
2018-06-30 00:44:58 -04:00
|
|
|
let relocations = {
|
|
|
|
|
let relocations = self.relocations(src, size)?;
|
|
|
|
|
let mut new_relocations = Vec::with_capacity(relocations.len() * (length as usize));
|
|
|
|
|
for i in 0..length {
|
|
|
|
|
new_relocations.extend(
|
|
|
|
|
relocations
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|&(offset, alloc_id)| {
|
2018-08-22 16:59:14 -03:00
|
|
|
(offset + dest.offset - src.offset + (i * size * relocations.len() as u64),
|
|
|
|
|
alloc_id)
|
2018-06-30 00:44:58 -04:00
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
new_relocations
|
|
|
|
|
};
|
2017-08-28 14:08:55 +02:00
|
|
|
|
2018-08-29 10:07:27 +02:00
|
|
|
// This also checks alignment, and relocation edges on the src.
|
2018-08-28 17:49:24 +02:00
|
|
|
let src_bytes = self.get_bytes_with_undef_and_ptr(src, size, src_align)?.as_ptr();
|
2018-06-26 22:59:10 -04:00
|
|
|
let dest_bytes = self.get_bytes_mut(dest, size * length, dest_align)?.as_mut_ptr();
|
2016-03-05 00:48:23 -06:00
|
|
|
|
|
|
|
|
// SAFE: The above indexing would have panicked if there weren't at least `size` bytes
|
|
|
|
|
// behind `src` and `dest`. Also, we use the overlapping-safe `ptr::copy` if `src` and
|
|
|
|
|
// `dest` could possibly overlap.
|
|
|
|
|
unsafe {
|
2018-05-19 16:37:29 +02:00
|
|
|
assert_eq!(size.bytes() as usize as u64, size.bytes());
|
2016-03-05 00:48:23 -06:00
|
|
|
if src.alloc_id == dest.alloc_id {
|
2017-07-03 20:27:09 -07:00
|
|
|
if nonoverlapping {
|
|
|
|
|
if (src.offset <= dest.offset && src.offset + size > dest.offset) ||
|
2017-08-10 08:48:38 -07:00
|
|
|
(dest.offset <= src.offset && dest.offset + size > src.offset)
|
|
|
|
|
{
|
|
|
|
|
return err!(Intrinsic(
|
2018-07-28 14:40:32 +02:00
|
|
|
"copy_nonoverlapping called on overlapping ranges".to_string(),
|
2017-08-10 08:48:38 -07:00
|
|
|
));
|
2017-07-03 20:27:09 -07:00
|
|
|
}
|
|
|
|
|
}
|
2018-06-26 22:59:10 -04:00
|
|
|
|
|
|
|
|
for i in 0..length {
|
2018-08-22 16:59:14 -03:00
|
|
|
ptr::copy(src_bytes,
|
|
|
|
|
dest_bytes.offset((size.bytes() * i) as isize),
|
|
|
|
|
size.bytes() as usize);
|
2018-06-26 22:59:10 -04:00
|
|
|
}
|
2016-03-05 00:48:23 -06:00
|
|
|
} else {
|
2018-06-26 22:59:10 -04:00
|
|
|
for i in 0..length {
|
2018-08-22 16:59:14 -03:00
|
|
|
ptr::copy_nonoverlapping(src_bytes,
|
|
|
|
|
dest_bytes.offset((size.bytes() * i) as isize),
|
|
|
|
|
size.bytes() as usize);
|
2018-06-26 22:59:10 -04:00
|
|
|
}
|
2016-03-05 00:48:23 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-29 10:07:27 +02:00
|
|
|
// copy definedness to the destination
|
2018-06-30 14:23:41 -04:00
|
|
|
self.copy_undef_mask(src, dest, size, length)?;
|
2018-08-29 10:07:27 +02:00
|
|
|
// copy the relocations to the destination
|
2018-05-18 16:06:20 +02:00
|
|
|
self.get_mut(dest.alloc_id)?.relocations.insert_presorted(relocations);
|
2016-04-06 04:08:52 -06:00
|
|
|
|
|
|
|
|
Ok(())
|
2016-03-05 00:48:23 -06:00
|
|
|
}
|
|
|
|
|
|
2018-05-21 00:37:44 +02:00
|
|
|
pub fn read_c_str(&self, ptr: Pointer) -> EvalResult<'tcx, &[u8]> {
|
2016-12-16 17:10:16 -08:00
|
|
|
let alloc = self.get(ptr.alloc_id)?;
|
2018-05-19 16:37:29 +02:00
|
|
|
assert_eq!(ptr.offset.bytes() as usize as u64, ptr.offset.bytes());
|
|
|
|
|
let offset = ptr.offset.bytes() as usize;
|
2016-12-16 17:10:16 -08:00
|
|
|
match alloc.bytes[offset..].iter().position(|&c| c == 0) {
|
|
|
|
|
Some(size) => {
|
2018-05-19 16:37:29 +02:00
|
|
|
let p1 = Size::from_bytes((size + 1) as u64);
|
2018-08-29 10:07:27 +02:00
|
|
|
self.check_relocations(ptr, p1)?;
|
2018-05-19 16:37:29 +02:00
|
|
|
self.check_defined(ptr, p1)?;
|
2016-12-16 17:10:16 -08:00
|
|
|
Ok(&alloc.bytes[offset..offset + size])
|
2017-08-10 08:48:38 -07:00
|
|
|
}
|
2017-08-02 16:59:01 +02:00
|
|
|
None => err!(UnterminatedCString(ptr)),
|
2016-12-16 17:10:16 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-21 00:30:00 +02:00
|
|
|
pub fn read_bytes(&self, ptr: Scalar, size: Size) -> EvalResult<'tcx, &[u8]> {
|
2017-08-25 14:41:59 +02:00
|
|
|
// Empty accesses don't need to be valid pointers, but they should still be non-NULL
|
2017-12-17 08:47:22 +02:00
|
|
|
let align = Align::from_bytes(1, 1).unwrap();
|
2018-05-19 16:37:29 +02:00
|
|
|
if size.bytes() == 0 {
|
2018-08-18 11:53:15 +02:00
|
|
|
self.check_align(ptr, align)?;
|
2017-06-20 16:26:53 +02:00
|
|
|
return Ok(&[]);
|
|
|
|
|
}
|
2017-12-17 08:47:22 +02:00
|
|
|
self.get_bytes(ptr.to_ptr()?, size, align)
|
2016-04-15 03:16:35 -06:00
|
|
|
}
|
|
|
|
|
|
2018-05-21 00:30:00 +02:00
|
|
|
pub fn write_bytes(&mut self, ptr: Scalar, src: &[u8]) -> EvalResult<'tcx> {
|
2017-08-25 14:41:59 +02:00
|
|
|
// Empty accesses don't need to be valid pointers, but they should still be non-NULL
|
2017-12-17 08:47:22 +02:00
|
|
|
let align = Align::from_bytes(1, 1).unwrap();
|
2017-06-20 16:26:53 +02:00
|
|
|
if src.is_empty() {
|
2018-08-18 11:53:15 +02:00
|
|
|
self.check_align(ptr, align)?;
|
2017-06-20 16:26:53 +02:00
|
|
|
return Ok(());
|
|
|
|
|
}
|
2018-05-19 16:37:29 +02:00
|
|
|
let bytes = self.get_bytes_mut(ptr.to_ptr()?, Size::from_bytes(src.len() as u64), align)?;
|
2016-04-07 05:56:07 -06:00
|
|
|
bytes.clone_from_slice(src);
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-21 00:30:00 +02:00
|
|
|
pub fn write_repeat(&mut self, ptr: Scalar, val: u8, count: Size) -> EvalResult<'tcx> {
|
2017-08-25 14:41:59 +02:00
|
|
|
// Empty accesses don't need to be valid pointers, but they should still be non-NULL
|
2017-12-17 08:47:22 +02:00
|
|
|
let align = Align::from_bytes(1, 1).unwrap();
|
2018-05-19 16:37:29 +02:00
|
|
|
if count.bytes() == 0 {
|
2018-08-18 11:53:15 +02:00
|
|
|
self.check_align(ptr, align)?;
|
2017-06-20 16:26:53 +02:00
|
|
|
return Ok(());
|
|
|
|
|
}
|
2017-12-17 08:47:22 +02:00
|
|
|
let bytes = self.get_bytes_mut(ptr.to_ptr()?, count, align)?;
|
2017-08-10 08:48:38 -07:00
|
|
|
for b in bytes {
|
|
|
|
|
*b = val;
|
|
|
|
|
}
|
2016-04-07 05:56:07 -06:00
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-17 17:47:37 +02:00
|
|
|
/// Read a *non-ZST* scalar
|
2018-08-23 09:34:21 -07:00
|
|
|
pub fn read_scalar(
|
|
|
|
|
&self,
|
|
|
|
|
ptr: Pointer,
|
|
|
|
|
ptr_align: Align,
|
|
|
|
|
size: Size
|
|
|
|
|
) -> EvalResult<'tcx, ScalarMaybeUndef> {
|
2018-08-29 10:07:27 +02:00
|
|
|
// get_bytes_unchecked tests alignment and relocation edges
|
2018-08-28 17:49:24 +02:00
|
|
|
let bytes = self.get_bytes_with_undef_and_ptr(
|
|
|
|
|
ptr, size, ptr_align.min(self.int_align(size))
|
|
|
|
|
)?;
|
2017-07-19 20:24:09 -07:00
|
|
|
// Undef check happens *after* we established that the alignment is correct.
|
|
|
|
|
// We must not return Ok() for unaligned pointers!
|
2018-09-02 17:22:46 +03:00
|
|
|
if self.check_defined(ptr, size).is_err() {
|
2018-08-23 19:04:33 +02:00
|
|
|
// this inflates undefined bytes to the entire scalar, even if only a few
|
|
|
|
|
// bytes are undefined
|
2018-07-24 18:28:53 +02:00
|
|
|
return Ok(ScalarMaybeUndef::Undef);
|
2017-07-19 20:24:09 -07:00
|
|
|
}
|
2017-08-25 16:20:13 +02:00
|
|
|
// Now we do the actual reading
|
2018-08-26 20:42:52 +02:00
|
|
|
let bits = read_target_uint(self.tcx.data_layout.endian, bytes).unwrap();
|
2017-08-25 16:20:13 +02:00
|
|
|
// See if we got a pointer
|
|
|
|
|
if size != self.pointer_size() {
|
2018-08-29 10:07:27 +02:00
|
|
|
// *Now* better make sure that the inside also is free of relocations.
|
|
|
|
|
self.check_relocations(ptr, size)?;
|
2017-08-25 16:20:13 +02:00
|
|
|
} else {
|
|
|
|
|
let alloc = self.get(ptr.alloc_id)?;
|
|
|
|
|
match alloc.relocations.get(&ptr.offset) {
|
2018-08-22 16:59:14 -03:00
|
|
|
Some(&alloc_id) => {
|
|
|
|
|
let ptr = Pointer::new(alloc_id, Size::from_bytes(bits as u64));
|
|
|
|
|
return Ok(ScalarMaybeUndef::Scalar(ptr.into()))
|
|
|
|
|
}
|
2017-08-25 16:20:13 +02:00
|
|
|
None => {},
|
|
|
|
|
}
|
2016-03-17 07:24:10 -06:00
|
|
|
}
|
2018-05-22 10:28:46 +02:00
|
|
|
// We don't. Just return the bits.
|
2018-08-26 20:42:52 +02:00
|
|
|
Ok(ScalarMaybeUndef::Scalar(Scalar::from_uint(bits, size)))
|
2017-08-25 16:20:13 +02:00
|
|
|
}
|
|
|
|
|
|
2018-08-22 16:59:14 -03:00
|
|
|
pub fn read_ptr_sized(&self, ptr: Pointer, ptr_align: Align)
|
|
|
|
|
-> EvalResult<'tcx, ScalarMaybeUndef> {
|
2018-05-22 19:30:16 +02:00
|
|
|
self.read_scalar(ptr, ptr_align, self.pointer_size())
|
2016-03-13 14:36:25 -06:00
|
|
|
}
|
|
|
|
|
|
2018-08-17 17:47:37 +02:00
|
|
|
/// Write a *non-ZST* scalar
|
2018-08-01 12:55:05 +02:00
|
|
|
pub fn write_scalar(
|
|
|
|
|
&mut self,
|
2018-08-17 17:47:37 +02:00
|
|
|
ptr: Pointer,
|
2018-08-01 12:55:05 +02:00
|
|
|
ptr_align: Align,
|
|
|
|
|
val: ScalarMaybeUndef,
|
|
|
|
|
type_size: Size,
|
|
|
|
|
) -> EvalResult<'tcx> {
|
2018-07-24 18:28:53 +02:00
|
|
|
let val = match val {
|
|
|
|
|
ScalarMaybeUndef::Scalar(scalar) => scalar,
|
|
|
|
|
ScalarMaybeUndef::Undef => return self.mark_definedness(ptr, type_size, false),
|
|
|
|
|
};
|
|
|
|
|
|
2017-08-25 18:25:05 +02:00
|
|
|
let bytes = match val {
|
2018-05-20 23:43:16 +02:00
|
|
|
Scalar::Ptr(val) => {
|
2018-07-24 18:28:53 +02:00
|
|
|
assert_eq!(type_size, self.pointer_size());
|
2018-05-19 16:37:29 +02:00
|
|
|
val.offset.bytes() as u128
|
2016-12-17 01:36:02 -08:00
|
|
|
}
|
|
|
|
|
|
2018-07-24 18:28:53 +02:00
|
|
|
Scalar::Bits { bits, size } => {
|
|
|
|
|
assert_eq!(size as u64, type_size.bytes());
|
2018-08-26 20:42:52 +02:00
|
|
|
debug_assert_eq!(truncate(bits, Size::from_bytes(size.into())), bits,
|
2018-08-16 11:38:16 +02:00
|
|
|
"Unexpected value of size {} when writing to memory", size);
|
2018-07-24 18:28:53 +02:00
|
|
|
bits
|
|
|
|
|
},
|
2017-08-25 18:25:05 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
{
|
2018-08-17 17:47:37 +02:00
|
|
|
// get_bytes_mut checks alignment
|
2018-08-26 20:42:52 +02:00
|
|
|
let endian = self.tcx.data_layout.endian;
|
2018-08-17 17:47:37 +02:00
|
|
|
let dst = self.get_bytes_mut(ptr, type_size, ptr_align)?;
|
2018-08-26 20:42:52 +02:00
|
|
|
write_target_uint(endian, dst, bytes).unwrap();
|
2017-08-25 18:25:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// See if we have to also write a relocation
|
|
|
|
|
match val {
|
2018-05-20 23:43:16 +02:00
|
|
|
Scalar::Ptr(val) => {
|
2017-08-25 18:25:05 +02:00
|
|
|
self.get_mut(ptr.alloc_id)?.relocations.insert(
|
|
|
|
|
ptr.offset,
|
|
|
|
|
val.alloc_id,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
_ => {}
|
2016-12-17 01:36:02 -08:00
|
|
|
}
|
2017-08-25 18:25:05 +02:00
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-22 16:59:14 -03:00
|
|
|
pub fn write_ptr_sized(&mut self, ptr: Pointer, ptr_align: Align, val: ScalarMaybeUndef)
|
|
|
|
|
-> EvalResult<'tcx> {
|
2017-08-25 18:25:05 +02:00
|
|
|
let ptr_size = self.pointer_size();
|
2018-08-17 17:47:37 +02:00
|
|
|
self.write_scalar(ptr.into(), ptr_align, val, ptr_size)
|
2016-03-07 04:44:03 -06:00
|
|
|
}
|
|
|
|
|
|
2018-05-19 16:37:29 +02:00
|
|
|
fn int_align(&self, size: Size) -> Align {
|
2017-08-25 16:20:13 +02:00
|
|
|
// We assume pointer-sized integers have the same alignment as pointers.
|
2017-08-28 15:58:58 +02:00
|
|
|
// We also assume signed and unsigned integers of the same size have the same alignment.
|
2018-05-19 16:37:29 +02:00
|
|
|
let ity = match size.bytes() {
|
2017-12-17 08:47:22 +02:00
|
|
|
1 => layout::I8,
|
|
|
|
|
2 => layout::I16,
|
|
|
|
|
4 => layout::I32,
|
|
|
|
|
8 => layout::I64,
|
|
|
|
|
16 => layout::I128,
|
2018-05-19 16:37:29 +02:00
|
|
|
_ => bug!("bad integer size: {}", size.bytes()),
|
2017-12-17 08:47:22 +02:00
|
|
|
};
|
|
|
|
|
ity.align(self)
|
2016-07-06 11:12:44 +02:00
|
|
|
}
|
2016-06-23 09:40:01 +02:00
|
|
|
}
|
2016-03-05 00:48:23 -06:00
|
|
|
|
2016-06-23 09:40:01 +02:00
|
|
|
/// Relocations
|
2018-09-20 10:12:21 +02:00
|
|
|
impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
|
2018-08-29 10:07:27 +02:00
|
|
|
/// Return all relocations overlapping with the given ptr-offset pair.
|
2017-08-10 08:48:38 -07:00
|
|
|
fn relocations(
|
|
|
|
|
&self,
|
2018-05-21 00:37:44 +02:00
|
|
|
ptr: Pointer,
|
2018-05-19 16:37:29 +02:00
|
|
|
size: Size,
|
2018-05-18 16:06:20 +02:00
|
|
|
) -> EvalResult<'tcx, &[(Size, AllocId)]> {
|
2018-08-29 10:07:27 +02:00
|
|
|
// We have to go back `pointer_size - 1` bytes, as that one would still overlap with
|
|
|
|
|
// the beginning of this range.
|
2018-05-19 16:37:29 +02:00
|
|
|
let start = ptr.offset.bytes().saturating_sub(self.pointer_size().bytes() - 1);
|
2018-08-29 10:07:27 +02:00
|
|
|
let end = ptr.offset + size; // this does overflow checking
|
2018-05-19 16:37:29 +02:00
|
|
|
Ok(self.get(ptr.alloc_id)?.relocations.range(Size::from_bytes(start)..end))
|
2016-03-05 00:48:23 -06:00
|
|
|
}
|
|
|
|
|
|
2018-08-29 10:07:27 +02:00
|
|
|
/// Check that there ar eno relocations overlapping with the given range.
|
|
|
|
|
#[inline(always)]
|
|
|
|
|
fn check_relocations(&self, ptr: Pointer, size: Size) -> EvalResult<'tcx> {
|
|
|
|
|
if self.relocations(ptr, size)?.len() != 0 {
|
|
|
|
|
err!(ReadPointerAsBytes)
|
|
|
|
|
} else {
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Remove all relocations inside the given range.
|
|
|
|
|
/// If there are relocations overlapping with the edges, they
|
|
|
|
|
/// are removed as well *and* the bytes they cover are marked as
|
|
|
|
|
/// uninitialized. This is a somewhat odd "spooky action at a distance",
|
|
|
|
|
/// but it allows strictly more code to run than if we would just error
|
|
|
|
|
/// immediately in that case.
|
2018-05-21 00:37:44 +02:00
|
|
|
fn clear_relocations(&mut self, ptr: Pointer, size: Size) -> EvalResult<'tcx> {
|
2016-03-27 00:29:02 -06:00
|
|
|
// Find the start and end of the given range and its outermost relocations.
|
2018-05-18 16:06:20 +02:00
|
|
|
let (first, last) = {
|
|
|
|
|
// Find all relocations overlapping the given range.
|
|
|
|
|
let relocations = self.relocations(ptr, size)?;
|
|
|
|
|
if relocations.is_empty() {
|
|
|
|
|
return Ok(());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
(relocations.first().unwrap().0,
|
|
|
|
|
relocations.last().unwrap().0 + self.pointer_size())
|
|
|
|
|
};
|
2016-03-27 00:29:02 -06:00
|
|
|
let start = ptr.offset;
|
|
|
|
|
let end = start + size;
|
|
|
|
|
|
2016-05-09 18:52:44 -06:00
|
|
|
let alloc = self.get_mut(ptr.alloc_id)?;
|
2016-03-27 00:29:02 -06:00
|
|
|
|
|
|
|
|
// Mark parts of the outermost relocations as undefined if they partially fall outside the
|
|
|
|
|
// given range.
|
2017-08-10 08:48:38 -07:00
|
|
|
if first < start {
|
|
|
|
|
alloc.undef_mask.set_range(first, start, false);
|
|
|
|
|
}
|
|
|
|
|
if last > end {
|
|
|
|
|
alloc.undef_mask.set_range(end, last, false);
|
|
|
|
|
}
|
2016-03-27 00:29:02 -06:00
|
|
|
|
|
|
|
|
// Forget all the relocations.
|
2018-05-30 15:59:42 +02:00
|
|
|
alloc.relocations.remove_range(first..last);
|
2016-03-27 00:29:02 -06:00
|
|
|
|
2016-03-23 21:40:58 -06:00
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-29 10:07:27 +02:00
|
|
|
/// Error if there are relocations overlapping with the egdes of the
|
|
|
|
|
/// given memory range.
|
|
|
|
|
#[inline]
|
2018-05-21 00:37:44 +02:00
|
|
|
fn check_relocation_edges(&self, ptr: Pointer, size: Size) -> EvalResult<'tcx> {
|
2018-08-29 10:07:27 +02:00
|
|
|
self.check_relocations(ptr, Size::ZERO)?;
|
|
|
|
|
self.check_relocations(ptr.offset(size, self)?, Size::ZERO)?;
|
2016-03-23 21:40:58 -06:00
|
|
|
Ok(())
|
|
|
|
|
}
|
2016-06-23 09:40:01 +02:00
|
|
|
}
|
2016-03-26 22:25:08 -06:00
|
|
|
|
2016-06-23 09:40:01 +02:00
|
|
|
/// Undefined bytes
|
2018-09-20 10:12:21 +02:00
|
|
|
impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
|
2018-10-02 22:11:38 -04:00
|
|
|
// FIXME: Add a fast version for the common, nonoverlapping case
|
2017-08-10 08:48:38 -07:00
|
|
|
fn copy_undef_mask(
|
|
|
|
|
&mut self,
|
2018-05-21 00:37:44 +02:00
|
|
|
src: Pointer,
|
|
|
|
|
dest: Pointer,
|
2018-05-19 16:37:29 +02:00
|
|
|
size: Size,
|
2018-06-30 14:23:41 -04:00
|
|
|
repeat: u64,
|
2017-08-10 08:48:38 -07:00
|
|
|
) -> EvalResult<'tcx> {
|
2016-04-06 04:08:52 -06:00
|
|
|
// The bits have to be saved locally before writing to dest in case src and dest overlap.
|
2018-05-19 16:37:29 +02:00
|
|
|
assert_eq!(size.bytes() as usize as u64, size.bytes());
|
2018-06-29 20:22:35 -04:00
|
|
|
|
2018-06-29 22:26:15 -04:00
|
|
|
let undef_mask = self.get(src.alloc_id)?.undef_mask.clone();
|
|
|
|
|
let dest_allocation = self.get_mut(dest.alloc_id)?;
|
2018-06-29 20:22:35 -04:00
|
|
|
|
2018-05-19 16:37:29 +02:00
|
|
|
for i in 0..size.bytes() {
|
2018-06-29 22:26:15 -04:00
|
|
|
let defined = undef_mask.get(src.offset + Size::from_bytes(i));
|
2018-06-21 21:40:14 -07:00
|
|
|
|
2018-06-30 14:23:41 -04:00
|
|
|
for j in 0..repeat {
|
|
|
|
|
dest_allocation.undef_mask.set(
|
|
|
|
|
dest.offset + Size::from_bytes(i + (size.bytes() * j)),
|
|
|
|
|
defined
|
|
|
|
|
);
|
|
|
|
|
}
|
2016-04-06 04:08:52 -06:00
|
|
|
}
|
2018-06-29 20:22:35 -04:00
|
|
|
|
2016-04-06 04:08:52 -06:00
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-02 17:22:46 +03:00
|
|
|
/// Checks that a range of bytes is defined. If not, returns the `ReadUndefBytes`
|
|
|
|
|
/// error which will report the first byte which is undefined.
|
|
|
|
|
#[inline]
|
|
|
|
|
fn check_defined(&self, ptr: Pointer, size: Size) -> EvalResult<'tcx> {
|
2016-05-09 18:52:44 -06:00
|
|
|
let alloc = self.get(ptr.alloc_id)?;
|
2018-09-02 17:22:46 +03:00
|
|
|
alloc.undef_mask.is_range_defined(
|
2017-08-10 08:48:38 -07:00
|
|
|
ptr.offset,
|
|
|
|
|
ptr.offset + size,
|
2018-09-02 17:22:46 +03:00
|
|
|
).or_else(|idx| err!(ReadUndefBytes(idx)))
|
2016-03-26 23:56:49 -06:00
|
|
|
}
|
|
|
|
|
|
2017-02-04 13:09:10 -08:00
|
|
|
pub fn mark_definedness(
|
|
|
|
|
&mut self,
|
2018-08-17 17:47:37 +02:00
|
|
|
ptr: Pointer,
|
2018-05-19 16:37:29 +02:00
|
|
|
size: Size,
|
2017-08-10 08:48:38 -07:00
|
|
|
new_state: bool,
|
2017-02-04 13:09:10 -08:00
|
|
|
) -> EvalResult<'tcx> {
|
2018-05-19 16:37:29 +02:00
|
|
|
if size.bytes() == 0 {
|
2017-08-10 08:48:38 -07:00
|
|
|
return Ok(());
|
2016-09-22 15:22:00 +02:00
|
|
|
}
|
2017-08-12 09:45:44 -07:00
|
|
|
let alloc = self.get_mut(ptr.alloc_id)?;
|
2017-08-10 08:48:38 -07:00
|
|
|
alloc.undef_mask.set_range(
|
|
|
|
|
ptr.offset,
|
|
|
|
|
ptr.offset + size,
|
|
|
|
|
new_state,
|
|
|
|
|
);
|
2016-03-26 22:25:08 -06:00
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
}
|