@@ -29,7 +29,7 @@ use crate::ty;
29
29
pub trait AllocBytes : Clone + fmt:: Debug + Deref < Target = [ u8 ] > + DerefMut < Target = [ u8 ] > {
30
30
/// Used for giving extra metadata on creation. Miri uses this to allow
31
31
/// machine memory to be allocated separately from other allocations.
32
- type ByteMetadata : Default + Clone + fmt:: Debug ;
32
+ type ByteMetadata : Clone + fmt:: Debug ;
33
33
34
34
/// Create an `AllocBytes` from a slice of `u8`.
35
35
fn from_bytes < ' a > (
@@ -55,6 +55,9 @@ pub trait AllocBytes: Clone + fmt::Debug + Deref<Target = [u8]> + DerefMut<Targe
55
55
/// - other pointers returned by this method, and
56
56
/// - references returned from `deref()`, as long as there was no write.
57
57
fn as_ptr ( & self ) -> * const u8 ;
58
+
59
+ /// Gets the allocation metadata.
60
+ fn get_mdata ( & self ) -> Self :: ByteMetadata ;
58
61
}
59
62
60
63
/// Default `bytes` for `Allocation` is a `Box<u8>`.
@@ -79,6 +82,8 @@ impl AllocBytes for Box<[u8]> {
79
82
fn as_ptr ( & self ) -> * const u8 {
80
83
Box :: as_ptr ( self ) . cast ( )
81
84
}
85
+
86
+ fn get_mdata ( & self ) -> ( ) { }
82
87
}
83
88
84
89
/// This type represents an Allocation in the Miri/CTFE core engine.
@@ -185,6 +190,7 @@ fn all_zero(buf: &[u8]) -> bool {
185
190
impl < Prov : Provenance , Extra , Bytes , E : Encoder > Encodable < E > for Allocation < Prov , Extra , Bytes >
186
191
where
187
192
Bytes : AllocBytes ,
193
+ Bytes :: ByteMetadata : Encodable < E > ,
188
194
ProvenanceMap < Prov > : Encodable < E > ,
189
195
Extra : Encodable < E > ,
190
196
{
@@ -196,6 +202,7 @@ where
196
202
if !all_zero {
197
203
encoder. emit_raw_bytes ( & self . bytes ) ;
198
204
}
205
+ self . bytes . get_mdata ( ) . encode ( encoder) ;
199
206
self . provenance . encode ( encoder) ;
200
207
self . init_mask . encode ( encoder) ;
201
208
self . extra . encode ( encoder) ;
@@ -205,6 +212,7 @@ where
205
212
impl < Prov : Provenance , Extra , Bytes , D : Decoder > Decodable < D > for Allocation < Prov , Extra , Bytes >
206
213
where
207
214
Bytes : AllocBytes ,
215
+ Bytes :: ByteMetadata : Decodable < D > ,
208
216
ProvenanceMap < Prov > : Decodable < D > ,
209
217
Extra : Decodable < D > ,
210
218
{
@@ -213,7 +221,9 @@ where
213
221
214
222
let len = decoder. read_usize ( ) ;
215
223
let bytes = if all_zero { vec ! [ 0u8 ; len] } else { decoder. read_raw_bytes ( len) . to_vec ( ) } ;
216
- let bytes = Bytes :: from_bytes ( bytes, align, Bytes :: ByteMetadata :: default ( ) ) ;
224
+
225
+ let mdata = Decodable :: decode ( decoder) ;
226
+ let bytes = Bytes :: from_bytes ( bytes, align, mdata) ;
217
227
218
228
let provenance = Decodable :: decode ( decoder) ;
219
229
let init_mask = Decodable :: decode ( decoder) ;
@@ -405,8 +415,9 @@ impl<Prov: Provenance, Bytes: AllocBytes> Allocation<Prov, (), Bytes> {
405
415
slice : impl Into < Cow < ' a , [ u8 ] > > ,
406
416
align : Align ,
407
417
mutability : Mutability ,
418
+ dsc : <Bytes as AllocBytes >:: ByteMetadata ,
408
419
) -> Self {
409
- let bytes = Bytes :: from_bytes ( slice, align, Bytes :: ByteMetadata :: default ( ) ) ;
420
+ let bytes = Bytes :: from_bytes ( slice, align, dsc ) ;
410
421
let size = Size :: from_bytes ( bytes. len ( ) ) ;
411
422
Self {
412
423
bytes,
@@ -418,14 +429,18 @@ impl<Prov: Provenance, Bytes: AllocBytes> Allocation<Prov, (), Bytes> {
418
429
}
419
430
}
420
431
421
- pub fn from_bytes_byte_aligned_immutable < ' a > ( slice : impl Into < Cow < ' a , [ u8 ] > > ) -> Self {
422
- Allocation :: from_bytes ( slice, Align :: ONE , Mutability :: Not )
432
+ pub fn from_bytes_byte_aligned_immutable < ' a > (
433
+ slice : impl Into < Cow < ' a , [ u8 ] > > ,
434
+ dsc : <Bytes as AllocBytes >:: ByteMetadata ,
435
+ ) -> Self {
436
+ Allocation :: from_bytes ( slice, Align :: ONE , Mutability :: Not , dsc)
423
437
}
424
438
425
439
fn new_inner < R > (
426
440
size : Size ,
427
441
align : Align ,
428
442
init : AllocInit ,
443
+ dsc : <Bytes as AllocBytes >:: ByteMetadata ,
429
444
fail : impl FnOnce ( ) -> R ,
430
445
) -> Result < Self , R > {
431
446
// We raise an error if we cannot create the allocation on the host.
@@ -434,7 +449,7 @@ impl<Prov: Provenance, Bytes: AllocBytes> Allocation<Prov, (), Bytes> {
434
449
// deterministic. However, we can be non-deterministic here because all uses of const
435
450
// evaluation (including ConstProp!) will make compilation fail (via hard error
436
451
// or ICE) upon encountering a `MemoryExhausted` error.
437
- let bytes = Bytes :: zeroed ( size, align, Bytes :: ByteMetadata :: default ( ) ) . ok_or_else ( fail) ?;
452
+ let bytes = Bytes :: zeroed ( size, align, dsc ) . ok_or_else ( fail) ?;
438
453
439
454
Ok ( Allocation {
440
455
bytes,
@@ -454,8 +469,13 @@ impl<Prov: Provenance, Bytes: AllocBytes> Allocation<Prov, (), Bytes> {
454
469
455
470
/// Try to create an Allocation of `size` bytes, failing if there is not enough memory
456
471
/// available to the compiler to do so.
457
- pub fn try_new < ' tcx > ( size : Size , align : Align , init : AllocInit ) -> InterpResult < ' tcx , Self > {
458
- Self :: new_inner ( size, align, init, || {
472
+ pub fn try_new < ' tcx > (
473
+ size : Size ,
474
+ align : Align ,
475
+ init : AllocInit ,
476
+ dsc : <Bytes as AllocBytes >:: ByteMetadata ,
477
+ ) -> InterpResult < ' tcx , Self > {
478
+ Self :: new_inner ( size, align, init, dsc, || {
459
479
ty:: tls:: with ( |tcx| tcx. dcx ( ) . delayed_bug ( "exhausted memory during interpretation" ) ) ;
460
480
InterpErrorKind :: ResourceExhaustion ( ResourceExhaustionInfo :: MemoryExhausted )
461
481
} )
@@ -467,8 +487,13 @@ impl<Prov: Provenance, Bytes: AllocBytes> Allocation<Prov, (), Bytes> {
467
487
///
468
488
/// Example use case: To obtain an Allocation filled with specific data,
469
489
/// first call this function and then call write_scalar to fill in the right data.
470
- pub fn new ( size : Size , align : Align , init : AllocInit ) -> Self {
471
- match Self :: new_inner ( size, align, init, || {
490
+ pub fn new (
491
+ size : Size ,
492
+ align : Align ,
493
+ init : AllocInit ,
494
+ dsc : <Bytes as AllocBytes >:: ByteMetadata ,
495
+ ) -> Self {
496
+ match Self :: new_inner ( size, align, init, dsc, || {
472
497
panic ! (
473
498
"interpreter ran out of memory: cannot create allocation of {} bytes" ,
474
499
size. bytes( )
0 commit comments