Skip to content

Commit

Permalink
Auto merge of #50967 - oli-obk:miri_api_refactor, r=eddyb
Browse files Browse the repository at this point in the history
Miri api refactor

r? @eddyb

cc @Zoxc

based on #50916
  • Loading branch information
bors committed May 25, 2018
2 parents 9823cb9 + 5f599bb commit 990d8aa
Show file tree
Hide file tree
Showing 50 changed files with 1,068 additions and 988 deletions.
33 changes: 22 additions & 11 deletions src/librustc/ich/impls_ty.rs
Expand Up @@ -394,10 +394,10 @@ for ::mir::interpret::ConstValue<'gcx> {
mem::discriminant(self).hash_stable(hcx, hasher);

match *self {
ByVal(val) => {
Scalar(val) => {
val.hash_stable(hcx, hasher);
}
ByValPair(a, b) => {
ScalarPair(a, b) => {
a.hash_stable(hcx, hasher);
b.hash_stable(hcx, hasher);
}
Expand All @@ -410,12 +410,12 @@ for ::mir::interpret::ConstValue<'gcx> {
}

impl_stable_hash_for!(enum mir::interpret::Value {
ByVal(v),
ByValPair(a, b),
Scalar(v),
ScalarPair(a, b),
ByRef(ptr, align)
});

impl_stable_hash_for!(struct mir::interpret::MemoryPointer {
impl_stable_hash_for!(struct mir::interpret::Pointer {
alloc_id,
offset
});
Expand Down Expand Up @@ -473,13 +473,24 @@ impl_stable_hash_for!(enum ::syntax::ast::Mutability {
Mutable
});

impl_stable_hash_for!(struct mir::interpret::Pointer{primval});

impl_stable_hash_for!(enum mir::interpret::PrimVal {
Bytes(b),
Ptr(p),
Undef
});
impl<'a> HashStable<StableHashingContext<'a>>
for ::mir::interpret::Scalar {
fn hash_stable<W: StableHasherResult>(&self,
hcx: &mut StableHashingContext<'a>,
hasher: &mut StableHasher<W>) {
use mir::interpret::Scalar::*;

mem::discriminant(self).hash_stable(hcx, hasher);
match *self {
Bits { bits, defined } => {
bits.hash_stable(hcx, hasher);
defined.hash_stable(hcx, hasher);
},
Ptr(ptr) => ptr.hash_stable(hcx, hasher),
}
}
}

impl_stable_hash_for!(struct ty::Const<'tcx> {
ty,
Expand Down
1 change: 1 addition & 0 deletions src/librustc/lib.rs
Expand Up @@ -68,6 +68,7 @@
#![feature(trusted_len)]
#![feature(catch_expr)]
#![feature(test)]
#![feature(in_band_lifetimes)]

#![recursion_limit="512"]

Expand Down
14 changes: 7 additions & 7 deletions src/librustc/mir/interpret/error.rs
Expand Up @@ -5,7 +5,7 @@ use ty::{FnSig, Ty, layout};
use ty::layout::{Size, Align};

use super::{
MemoryPointer, Lock, AccessKind
Pointer, Lock, AccessKind
};

use backtrace::Backtrace;
Expand Down Expand Up @@ -38,15 +38,15 @@ pub enum EvalErrorKind<'tcx, O> {
MachineError(String),
FunctionPointerTyMismatch(FnSig<'tcx>, FnSig<'tcx>),
NoMirFor(String),
UnterminatedCString(MemoryPointer),
UnterminatedCString(Pointer),
DanglingPointerDeref,
DoubleFree,
InvalidMemoryAccess,
InvalidFunctionPointer,
InvalidBool,
InvalidDiscriminant,
PointerOutOfBounds {
ptr: MemoryPointer,
ptr: Pointer,
access: bool,
allocation_size: Size,
},
Expand Down Expand Up @@ -76,26 +76,26 @@ pub enum EvalErrorKind<'tcx, O> {
has: Align,
},
MemoryLockViolation {
ptr: MemoryPointer,
ptr: Pointer,
len: u64,
frame: usize,
access: AccessKind,
lock: Lock,
},
MemoryAcquireConflict {
ptr: MemoryPointer,
ptr: Pointer,
len: u64,
kind: AccessKind,
lock: Lock,
},
InvalidMemoryLockRelease {
ptr: MemoryPointer,
ptr: Pointer,
len: u64,
frame: usize,
lock: Lock,
},
DeallocatedLockedMemory {
ptr: MemoryPointer,
ptr: Pointer,
lock: Lock,
},
ValidationFailure(String),
Expand Down
29 changes: 18 additions & 11 deletions src/librustc/mir/interpret/mod.rs
Expand Up @@ -10,7 +10,7 @@ mod value;

pub use self::error::{EvalError, EvalResult, EvalErrorKind, AssertMessage};

pub use self::value::{PrimVal, PrimValKind, Value, Pointer, ConstValue};
pub use self::value::{Scalar, Value, ConstValue};

use std::fmt;
use mir;
Expand Down Expand Up @@ -110,42 +110,49 @@ impl<T: layout::HasDataLayout> PointerArithmetic for T {}


#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, RustcEncodable, RustcDecodable, Hash)]
pub struct MemoryPointer {
pub struct Pointer {
pub alloc_id: AllocId,
pub offset: Size,
}

impl<'tcx> MemoryPointer {
/// Produces a `Pointer` which points to the beginning of the Allocation
impl From<AllocId> for Pointer {
fn from(alloc_id: AllocId) -> Self {
Pointer::new(alloc_id, Size::ZERO)
}
}

impl<'tcx> Pointer {
pub fn new(alloc_id: AllocId, offset: Size) -> Self {
MemoryPointer { alloc_id, offset }
Pointer { alloc_id, offset }
}

pub(crate) fn wrapping_signed_offset<C: HasDataLayout>(self, i: i64, cx: C) -> Self {
MemoryPointer::new(
Pointer::new(
self.alloc_id,
Size::from_bytes(cx.data_layout().wrapping_signed_offset(self.offset.bytes(), i)),
)
}

pub fn overflowing_signed_offset<C: HasDataLayout>(self, i: i128, cx: C) -> (Self, bool) {
let (res, over) = cx.data_layout().overflowing_signed_offset(self.offset.bytes(), i);
(MemoryPointer::new(self.alloc_id, Size::from_bytes(res)), over)
(Pointer::new(self.alloc_id, Size::from_bytes(res)), over)
}

pub(crate) fn signed_offset<C: HasDataLayout>(self, i: i64, cx: C) -> EvalResult<'tcx, Self> {
Ok(MemoryPointer::new(
Ok(Pointer::new(
self.alloc_id,
Size::from_bytes(cx.data_layout().signed_offset(self.offset.bytes(), i)?),
))
}

pub fn overflowing_offset<C: HasDataLayout>(self, i: Size, cx: C) -> (Self, bool) {
let (res, over) = cx.data_layout().overflowing_offset(self.offset.bytes(), i.bytes());
(MemoryPointer::new(self.alloc_id, Size::from_bytes(res)), over)
(Pointer::new(self.alloc_id, Size::from_bytes(res)), over)
}

pub fn offset<C: HasDataLayout>(self, i: Size, cx: C) -> EvalResult<'tcx, Self> {
Ok(MemoryPointer::new(
Ok(Pointer::new(
self.alloc_id,
Size::from_bytes(cx.data_layout().offset(self.offset.bytes(), i.bytes())?),
))
Expand Down Expand Up @@ -355,7 +362,7 @@ pub struct Allocation {

impl Allocation {
pub fn from_bytes(slice: &[u8], align: Align) -> Self {
let mut undef_mask = UndefMask::new(Size::from_bytes(0));
let mut undef_mask = UndefMask::new(Size::ZERO);
undef_mask.grow(Size::from_bytes(slice.len() as u64), true);
Self {
bytes: slice.to_owned(),
Expand Down Expand Up @@ -467,7 +474,7 @@ impl UndefMask {
pub fn new(size: Size) -> Self {
let mut m = UndefMask {
blocks: vec![],
len: Size::from_bytes(0),
len: Size::ZERO,
};
m.grow(size, false);
m
Expand Down

0 comments on commit 990d8aa

Please sign in to comment.