Skip to content

Commit fb489dc

Browse files
committed
Move the memory_accessed hook onto the Extra value
1 parent 6099356 commit fb489dc

File tree

3 files changed

+28
-27
lines changed

3 files changed

+28
-27
lines changed

src/librustc/mir/interpret/allocation.rs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ use std::ptr;
2727
use ty::layout::{self, Size, Align};
2828
use syntax::ast::Mutability;
2929

30+
/// Classifying memory accesses
31+
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
32+
pub enum MemoryAccess {
33+
Read,
34+
Write,
35+
}
36+
3037
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Hash, RustcEncodable, RustcDecodable)]
3138
pub struct Allocation<Tag=(),Extra=()> {
3239
/// The actual bytes of the allocation.
@@ -49,6 +56,22 @@ pub struct Allocation<Tag=(),Extra=()> {
4956
pub extra: Extra,
5057
}
5158

59+
trait AllocationExtra<Tag> {
60+
/// Hook for performing extra checks on a memory access.
61+
///
62+
/// Takes read-only access to the allocation so we can keep all the memory read
63+
/// operations take `&self`. Use a `RefCell` in `AllocExtra` if you
64+
/// need to mutate.
65+
fn memory_accessed(
66+
&self,
67+
ptr: Pointer<Tag>,
68+
size: Size,
69+
access: MemoryAccess,
70+
) -> EvalResult<'tcx> {
71+
Ok(())
72+
}
73+
}
74+
5275
impl<Tag, Extra: Default> Allocation<Tag, Extra> {
5376
/// Creates a read-only allocation initialized by the given bytes
5477
pub fn from_bytes(slice: &[u8], align: Align) -> Self {
@@ -84,7 +107,7 @@ impl<Tag, Extra: Default> Allocation<Tag, Extra> {
84107
impl<'tcx> ::serialize::UseSpecializedDecodable for &'tcx Allocation {}
85108

86109
/// Byte accessors
87-
impl<'tcx, Tag, Extra> Allocation<Tag, Extra> {
110+
impl<'tcx, Tag, Extra: AllocationExtra<Tag>> Allocation<Tag, Extra> {
88111
/// The last argument controls whether we error out when there are undefined
89112
/// or pointer bytes. You should never call this, call `get_bytes` or
90113
/// `get_bytes_with_undef_and_ptr` instead,
@@ -112,7 +135,7 @@ impl<'tcx, Tag, Extra> Allocation<Tag, Extra> {
112135
}
113136

114137
let alloc = self.get(ptr.alloc_id)?;
115-
M::memory_accessed(alloc, ptr, size, MemoryAccess::Read)?;
138+
Extra::memory_accessed(&self.extra, ptr, size, MemoryAccess::Read)?;
116139

117140
assert_eq!(ptr.offset.bytes() as usize as u64, ptr.offset.bytes());
118141
assert_eq!(size.bytes() as usize as u64, size.bytes());
@@ -158,7 +181,7 @@ impl<'tcx, Tag, Extra> Allocation<Tag, Extra> {
158181
self.clear_relocations(ptr, size)?;
159182

160183
let alloc = self.get_mut(ptr.alloc_id)?;
161-
M::memory_accessed(alloc, ptr, size, MemoryAccess::Write)?;
184+
Extra::memory_accessed(alloc, ptr, size, MemoryAccess::Write)?;
162185

163186
assert_eq!(ptr.offset.bytes() as usize as u64, ptr.offset.bytes());
164187
assert_eq!(size.bytes() as usize as u64, size.bytes());

src/librustc/mir/interpret/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub use self::error::{
2626

2727
pub use self::value::{Scalar, ConstValue};
2828

29-
pub use self::allocation::Allocation;
29+
pub use self::allocation::{Allocation, MemoryAccess};
3030

3131
use std::fmt;
3232
use mir;

src/librustc_mir/interpret/machine.rs

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,9 @@ use rustc::ty::{self, Ty, layout::{Size, TyLayout}, query::TyCtxtAt};
2121

2222
use super::{
2323
Allocation, AllocId, EvalResult, Scalar,
24-
EvalContext, PlaceTy, OpTy, Pointer, MemoryKind,
24+
EvalContext, PlaceTy, OpTy, Pointer, MemoryKind, MemoryAccess,
2525
};
2626

27-
/// Classifying memory accesses
28-
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
29-
pub enum MemoryAccess {
30-
Read,
31-
Write,
32-
}
33-
3427
/// Whether this kind of memory is allowed to leak
3528
pub trait MayLeak: Copy {
3629
fn may_leak(self) -> bool;
@@ -180,21 +173,6 @@ pub trait Machine<'a, 'mir, 'tcx>: Sized {
180173
dest: PlaceTy<'tcx, Self::PointerTag>,
181174
) -> EvalResult<'tcx>;
182175

183-
/// Hook for performing extra checks on a memory access.
184-
///
185-
/// Takes read-only access to the allocation so we can keep all the memory read
186-
/// operations take `&self`. Use a `RefCell` in `AllocExtra` if you
187-
/// need to mutate.
188-
#[inline]
189-
fn memory_accessed(
190-
_alloc: &Allocation<Self::PointerTag, Self::AllocExtra>,
191-
_ptr: Pointer<Self::PointerTag>,
192-
_size: Size,
193-
_access: MemoryAccess,
194-
) -> EvalResult<'tcx> {
195-
Ok(())
196-
}
197-
198176
/// Hook for performing extra checks when memory gets deallocated.
199177
#[inline]
200178
fn memory_deallocated(

0 commit comments

Comments
 (0)