@@ -16,15 +16,17 @@ use std::borrow::Cow;
16
16
#[ derive( Clone , Debug , Eq , PartialEq , PartialOrd , Ord , Hash , RustcEncodable , RustcDecodable ) ]
17
17
pub struct Allocation < Tag =( ) , Extra =( ) > {
18
18
/// The actual bytes of the allocation.
19
- /// Note that the bytes of a pointer represent the offset of the pointer
20
- pub bytes : Vec < u8 > ,
19
+ /// Note that the bytes of a pointer represent the offset of the pointer.
20
+ bytes : Vec < u8 > ,
21
21
/// Maps from byte addresses to extra data for each pointer.
22
22
/// Only the first byte of a pointer is inserted into the map; i.e.,
23
23
/// every entry in this map applies to `pointer_size` consecutive bytes starting
24
24
/// at the given offset.
25
25
pub relocations : Relocations < Tag > ,
26
- /// Denotes undefined memory. Reading from undefined memory is forbidden in miri
27
- pub undef_mask : UndefMask ,
26
+ /// Denotes which part of this allocation is initialized.
27
+ undef_mask : UndefMask ,
28
+ /// The size of the allocation. Currently, must always equal `bytes.len()`.
29
+ pub size : Size ,
28
30
/// The alignment of the allocation to detect unaligned reads.
29
31
pub align : Align ,
30
32
/// Whether the allocation is mutable.
@@ -85,11 +87,12 @@ impl<Tag> Allocation<Tag> {
85
87
/// Creates a read-only allocation initialized by the given bytes
86
88
pub fn from_bytes < ' a > ( slice : impl Into < Cow < ' a , [ u8 ] > > , align : Align ) -> Self {
87
89
let bytes = slice. into ( ) . into_owned ( ) ;
88
- let undef_mask = UndefMask :: new ( Size :: from_bytes ( bytes. len ( ) as u64 ) , true ) ;
90
+ let size = Size :: from_bytes ( bytes. len ( ) as u64 ) ;
89
91
Self {
90
92
bytes,
91
93
relocations : Relocations :: new ( ) ,
92
- undef_mask,
94
+ undef_mask : UndefMask :: new ( size, true ) ,
95
+ size,
93
96
align,
94
97
mutability : Mutability :: Immutable ,
95
98
extra : ( ) ,
@@ -106,13 +109,29 @@ impl<Tag> Allocation<Tag> {
106
109
bytes : vec ! [ 0 ; size. bytes( ) as usize ] ,
107
110
relocations : Relocations :: new ( ) ,
108
111
undef_mask : UndefMask :: new ( size, false ) ,
112
+ size,
109
113
align,
110
114
mutability : Mutability :: Mutable ,
111
115
extra : ( ) ,
112
116
}
113
117
}
114
118
}
115
119
120
+ /// Raw accessors. Provide access to otherwise private bytes.
121
+ impl < Tag , Extra > Allocation < Tag , Extra > {
122
+ pub fn len ( & self ) -> usize {
123
+ self . size . bytes ( ) as usize
124
+ }
125
+
126
+ /// Look at a slice which may describe undefined bytes or describe a relocation. This differs
127
+ /// from `get_bytes_with_undef_and_ptr` in that it does no relocation checks (even on the
128
+ /// edges) at all. It further ignores `AllocationExtra` callbacks.
129
+ /// This must not be used for reads affecting the interpreter execution.
130
+ pub fn inspect_with_undef_and_ptr_outside_interpreter ( & self , range : Range < usize > ) -> & [ u8 ] {
131
+ & self . bytes [ range]
132
+ }
133
+ }
134
+
116
135
impl < ' tcx > rustc_serialize:: UseSpecializedDecodable for & ' tcx Allocation { }
117
136
118
137
/// Byte accessors
@@ -132,9 +151,9 @@ impl<'tcx, Tag: Copy, Extra: AllocationExtra<Tag>> Allocation<Tag, Extra> {
132
151
) ;
133
152
let end = end. bytes ( ) as usize ;
134
153
assert ! (
135
- end <= self . bytes . len( ) ,
154
+ end <= self . len( ) ,
136
155
"Out-of-bounds access at offset {}, size {} in allocation of size {}" ,
137
- offset. bytes( ) , size. bytes( ) , self . bytes . len( )
156
+ offset. bytes( ) , size. bytes( ) , self . len( )
138
157
) ;
139
158
( offset. bytes ( ) as usize ) ..end
140
159
}
0 commit comments