@@ -27,6 +27,13 @@ use std::ptr;
27
27
use ty:: layout:: { self , Size , Align } ;
28
28
use syntax:: ast:: Mutability ;
29
29
30
+ /// Classifying memory accesses
31
+ #[ derive( Clone , Copy , Debug , PartialEq , Eq ) ]
32
+ pub enum MemoryAccess {
33
+ Read ,
34
+ Write ,
35
+ }
36
+
30
37
#[ derive( Clone , Debug , Eq , PartialEq , PartialOrd , Ord , Hash , RustcEncodable , RustcDecodable ) ]
31
38
pub struct Allocation < Tag =( ) , Extra =( ) > {
32
39
/// The actual bytes of the allocation.
@@ -49,6 +56,22 @@ pub struct Allocation<Tag=(),Extra=()> {
49
56
pub extra : Extra ,
50
57
}
51
58
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
+
52
75
impl < Tag , Extra : Default > Allocation < Tag , Extra > {
53
76
/// Creates a read-only allocation initialized by the given bytes
54
77
pub fn from_bytes ( slice : & [ u8 ] , align : Align ) -> Self {
@@ -84,7 +107,7 @@ impl<Tag, Extra: Default> Allocation<Tag, Extra> {
84
107
impl < ' tcx > :: serialize:: UseSpecializedDecodable for & ' tcx Allocation { }
85
108
86
109
/// Byte accessors
87
- impl < ' tcx , Tag , Extra > Allocation < Tag , Extra > {
110
+ impl < ' tcx , Tag , Extra : AllocationExtra < Tag > > Allocation < Tag , Extra > {
88
111
/// The last argument controls whether we error out when there are undefined
89
112
/// or pointer bytes. You should never call this, call `get_bytes` or
90
113
/// `get_bytes_with_undef_and_ptr` instead,
@@ -112,7 +135,7 @@ impl<'tcx, Tag, Extra> Allocation<Tag, Extra> {
112
135
}
113
136
114
137
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 ) ?;
116
139
117
140
assert_eq ! ( ptr. offset. bytes( ) as usize as u64 , ptr. offset. bytes( ) ) ;
118
141
assert_eq ! ( size. bytes( ) as usize as u64 , size. bytes( ) ) ;
@@ -158,7 +181,7 @@ impl<'tcx, Tag, Extra> Allocation<Tag, Extra> {
158
181
self . clear_relocations ( ptr, size) ?;
159
182
160
183
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 ) ?;
162
185
163
186
assert_eq ! ( ptr. offset. bytes( ) as usize as u64 , ptr. offset. bytes( ) ) ;
164
187
assert_eq ! ( size. bytes( ) as usize as u64 , size. bytes( ) ) ;
0 commit comments