@@ -29,7 +29,7 @@ use crate::ty;
2929pub trait AllocBytes : Clone + fmt:: Debug + Deref < Target = [ u8 ] > + DerefMut < Target = [ u8 ] > {
3030 /// Used for giving extra metadata on creation. Miri uses this to allow
3131 /// machine memory to be allocated separately from other allocations.
32- type ByteMetadata : Default + Clone + fmt:: Debug ;
32+ type ByteMetadata : Clone + fmt:: Debug ;
3333
3434 /// Create an `AllocBytes` from a slice of `u8`.
3535 fn from_bytes < ' a > (
@@ -55,6 +55,9 @@ pub trait AllocBytes: Clone + fmt::Debug + Deref<Target = [u8]> + DerefMut<Targe
5555 /// - other pointers returned by this method, and
5656 /// - references returned from `deref()`, as long as there was no write.
5757 fn as_ptr ( & self ) -> * const u8 ;
58+
59+ /// Gets the allocation metadata.
60+ fn get_mdata ( & self ) -> Self :: ByteMetadata ;
5861}
5962
6063/// Default `bytes` for `Allocation` is a `Box<u8>`.
@@ -79,6 +82,8 @@ impl AllocBytes for Box<[u8]> {
7982 fn as_ptr ( & self ) -> * const u8 {
8083 Box :: as_ptr ( self ) . cast ( )
8184 }
85+
86+ fn get_mdata ( & self ) -> ( ) { }
8287}
8388
8489/// This type represents an Allocation in the Miri/CTFE core engine.
@@ -185,6 +190,7 @@ fn all_zero(buf: &[u8]) -> bool {
185190impl < Prov : Provenance , Extra , Bytes , E : Encoder > Encodable < E > for Allocation < Prov , Extra , Bytes >
186191where
187192 Bytes : AllocBytes ,
193+ Bytes :: ByteMetadata : Encodable < E > ,
188194 ProvenanceMap < Prov > : Encodable < E > ,
189195 Extra : Encodable < E > ,
190196{
@@ -196,6 +202,7 @@ where
196202 if !all_zero {
197203 encoder. emit_raw_bytes ( & self . bytes ) ;
198204 }
205+ self . bytes . get_mdata ( ) . encode ( encoder) ;
199206 self . provenance . encode ( encoder) ;
200207 self . init_mask . encode ( encoder) ;
201208 self . extra . encode ( encoder) ;
@@ -205,6 +212,7 @@ where
205212impl < Prov : Provenance , Extra , Bytes , D : Decoder > Decodable < D > for Allocation < Prov , Extra , Bytes >
206213where
207214 Bytes : AllocBytes ,
215+ Bytes :: ByteMetadata : Decodable < D > ,
208216 ProvenanceMap < Prov > : Decodable < D > ,
209217 Extra : Decodable < D > ,
210218{
@@ -213,7 +221,9 @@ where
213221
214222 let len = decoder. read_usize ( ) ;
215223 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) ;
217227
218228 let provenance = Decodable :: decode ( decoder) ;
219229 let init_mask = Decodable :: decode ( decoder) ;
@@ -405,8 +415,9 @@ impl<Prov: Provenance, Bytes: AllocBytes> Allocation<Prov, (), Bytes> {
405415 slice : impl Into < Cow < ' a , [ u8 ] > > ,
406416 align : Align ,
407417 mutability : Mutability ,
418+ dsc : <Bytes as AllocBytes >:: ByteMetadata ,
408419 ) -> Self {
409- let bytes = Bytes :: from_bytes ( slice, align, Bytes :: ByteMetadata :: default ( ) ) ;
420+ let bytes = Bytes :: from_bytes ( slice, align, dsc ) ;
410421 let size = Size :: from_bytes ( bytes. len ( ) ) ;
411422 Self {
412423 bytes,
@@ -418,14 +429,18 @@ impl<Prov: Provenance, Bytes: AllocBytes> Allocation<Prov, (), Bytes> {
418429 }
419430 }
420431
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)
423437 }
424438
425439 fn new_inner < R > (
426440 size : Size ,
427441 align : Align ,
428442 init : AllocInit ,
443+ dsc : <Bytes as AllocBytes >:: ByteMetadata ,
429444 fail : impl FnOnce ( ) -> R ,
430445 ) -> Result < Self , R > {
431446 // 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> {
434449 // deterministic. However, we can be non-deterministic here because all uses of const
435450 // evaluation (including ConstProp!) will make compilation fail (via hard error
436451 // 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) ?;
438453
439454 Ok ( Allocation {
440455 bytes,
@@ -454,8 +469,13 @@ impl<Prov: Provenance, Bytes: AllocBytes> Allocation<Prov, (), Bytes> {
454469
455470 /// Try to create an Allocation of `size` bytes, failing if there is not enough memory
456471 /// 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, || {
459479 ty:: tls:: with ( |tcx| tcx. dcx ( ) . delayed_bug ( "exhausted memory during interpretation" ) ) ;
460480 InterpErrorKind :: ResourceExhaustion ( ResourceExhaustionInfo :: MemoryExhausted )
461481 } )
@@ -467,8 +487,13 @@ impl<Prov: Provenance, Bytes: AllocBytes> Allocation<Prov, (), Bytes> {
467487 ///
468488 /// Example use case: To obtain an Allocation filled with specific data,
469489 /// 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, || {
472497 panic ! (
473498 "interpreter ran out of memory: cannot create allocation of {} bytes" ,
474499 size. bytes( )
0 commit comments