@@ -27,12 +27,20 @@ use crate::ty;
2727
2828/// Functionality required for the bytes of an `Allocation`.
2929pub trait AllocBytes : Clone + fmt:: Debug + Deref < Target = [ u8 ] > + DerefMut < Target = [ u8 ] > {
30+ /// Used for giving extra metadata on creation. Miri uses this to allow
31+ /// machine memory to be allocated separately from other allocations.
32+ type AllocParams ;
33+
3034 /// Create an `AllocBytes` from a slice of `u8`.
31- fn from_bytes < ' a > ( slice : impl Into < Cow < ' a , [ u8 ] > > , _align : Align ) -> Self ;
35+ fn from_bytes < ' a > (
36+ slice : impl Into < Cow < ' a , [ u8 ] > > ,
37+ _align : Align ,
38+ _params : Self :: AllocParams ,
39+ ) -> Self ;
3240
3341 /// Create a zeroed `AllocBytes` of the specified size and alignment.
3442 /// Returns `None` if we ran out of memory on the host.
35- fn zeroed ( size : Size , _align : Align ) -> Option < Self > ;
43+ fn zeroed ( size : Size , _align : Align , _params : Self :: AllocParams ) -> Option < Self > ;
3644
3745 /// Gives direct access to the raw underlying storage.
3846 ///
@@ -51,11 +59,13 @@ pub trait AllocBytes: Clone + fmt::Debug + Deref<Target = [u8]> + DerefMut<Targe
5159
5260/// Default `bytes` for `Allocation` is a `Box<u8>`.
5361impl AllocBytes for Box < [ u8 ] > {
54- fn from_bytes < ' a > ( slice : impl Into < Cow < ' a , [ u8 ] > > , _align : Align ) -> Self {
62+ type AllocParams = ( ) ;
63+
64+ fn from_bytes < ' a > ( slice : impl Into < Cow < ' a , [ u8 ] > > , _align : Align , _params : ( ) ) -> Self {
5565 Box :: < [ u8 ] > :: from ( slice. into ( ) )
5666 }
5767
58- fn zeroed ( size : Size , _align : Align ) -> Option < Self > {
68+ fn zeroed ( size : Size , _align : Align , _params : ( ) ) -> Option < Self > {
5969 let bytes = Box :: < [ u8 ] > :: try_new_zeroed_slice ( size. bytes ( ) . try_into ( ) . ok ( ) ?) . ok ( ) ?;
6070 // SAFETY: the box was zero-allocated, which is a valid initial value for Box<[u8]>
6171 let bytes = unsafe { bytes. assume_init ( ) } ;
@@ -172,9 +182,8 @@ fn all_zero(buf: &[u8]) -> bool {
172182}
173183
174184/// Custom encoder for [`Allocation`] to more efficiently represent the case where all bytes are 0.
175- impl < Prov : Provenance , Extra , Bytes , E : Encoder > Encodable < E > for Allocation < Prov , Extra , Bytes >
185+ impl < Prov : Provenance , Extra , E : Encoder > Encodable < E > for Allocation < Prov , Extra , Box < [ u8 ] > >
176186where
177- Bytes : AllocBytes ,
178187 ProvenanceMap < Prov > : Encodable < E > ,
179188 Extra : Encodable < E > ,
180189{
@@ -192,9 +201,8 @@ where
192201 }
193202}
194203
195- impl < Prov : Provenance , Extra , Bytes , D : Decoder > Decodable < D > for Allocation < Prov , Extra , Bytes >
204+ impl < Prov : Provenance , Extra , D : Decoder > Decodable < D > for Allocation < Prov , Extra , Box < [ u8 ] > >
196205where
197- Bytes : AllocBytes ,
198206 ProvenanceMap < Prov > : Decodable < D > ,
199207 Extra : Decodable < D > ,
200208{
@@ -203,7 +211,7 @@ where
203211
204212 let len = decoder. read_usize ( ) ;
205213 let bytes = if all_zero { vec ! [ 0u8 ; len] } else { decoder. read_raw_bytes ( len) . to_vec ( ) } ;
206- let bytes = Bytes :: from_bytes ( bytes, align) ;
214+ let bytes = < Box < [ u8 ] > as AllocBytes > :: from_bytes ( bytes, align, ( ) ) ;
207215
208216 let provenance = Decodable :: decode ( decoder) ;
209217 let init_mask = Decodable :: decode ( decoder) ;
@@ -395,8 +403,9 @@ impl<Prov: Provenance, Bytes: AllocBytes> Allocation<Prov, (), Bytes> {
395403 slice : impl Into < Cow < ' a , [ u8 ] > > ,
396404 align : Align ,
397405 mutability : Mutability ,
406+ params : <Bytes as AllocBytes >:: AllocParams ,
398407 ) -> Self {
399- let bytes = Bytes :: from_bytes ( slice, align) ;
408+ let bytes = Bytes :: from_bytes ( slice, align, params ) ;
400409 let size = Size :: from_bytes ( bytes. len ( ) ) ;
401410 Self {
402411 bytes,
@@ -408,14 +417,18 @@ impl<Prov: Provenance, Bytes: AllocBytes> Allocation<Prov, (), Bytes> {
408417 }
409418 }
410419
411- pub fn from_bytes_byte_aligned_immutable < ' a > ( slice : impl Into < Cow < ' a , [ u8 ] > > ) -> Self {
412- Allocation :: from_bytes ( slice, Align :: ONE , Mutability :: Not )
420+ pub fn from_bytes_byte_aligned_immutable < ' a > (
421+ slice : impl Into < Cow < ' a , [ u8 ] > > ,
422+ params : <Bytes as AllocBytes >:: AllocParams ,
423+ ) -> Self {
424+ Allocation :: from_bytes ( slice, Align :: ONE , Mutability :: Not , params)
413425 }
414426
415427 fn new_inner < R > (
416428 size : Size ,
417429 align : Align ,
418430 init : AllocInit ,
431+ params : <Bytes as AllocBytes >:: AllocParams ,
419432 fail : impl FnOnce ( ) -> R ,
420433 ) -> Result < Self , R > {
421434 // We raise an error if we cannot create the allocation on the host.
@@ -424,7 +437,7 @@ impl<Prov: Provenance, Bytes: AllocBytes> Allocation<Prov, (), Bytes> {
424437 // deterministic. However, we can be non-deterministic here because all uses of const
425438 // evaluation (including ConstProp!) will make compilation fail (via hard error
426439 // or ICE) upon encountering a `MemoryExhausted` error.
427- let bytes = Bytes :: zeroed ( size, align) . ok_or_else ( fail) ?;
440+ let bytes = Bytes :: zeroed ( size, align, params ) . ok_or_else ( fail) ?;
428441
429442 Ok ( Allocation {
430443 bytes,
@@ -444,8 +457,13 @@ impl<Prov: Provenance, Bytes: AllocBytes> Allocation<Prov, (), Bytes> {
444457
445458 /// Try to create an Allocation of `size` bytes, failing if there is not enough memory
446459 /// available to the compiler to do so.
447- pub fn try_new < ' tcx > ( size : Size , align : Align , init : AllocInit ) -> InterpResult < ' tcx , Self > {
448- Self :: new_inner ( size, align, init, || {
460+ pub fn try_new < ' tcx > (
461+ size : Size ,
462+ align : Align ,
463+ init : AllocInit ,
464+ params : <Bytes as AllocBytes >:: AllocParams ,
465+ ) -> InterpResult < ' tcx , Self > {
466+ Self :: new_inner ( size, align, init, params, || {
449467 ty:: tls:: with ( |tcx| tcx. dcx ( ) . delayed_bug ( "exhausted memory during interpretation" ) ) ;
450468 InterpErrorKind :: ResourceExhaustion ( ResourceExhaustionInfo :: MemoryExhausted )
451469 } )
@@ -457,8 +475,13 @@ impl<Prov: Provenance, Bytes: AllocBytes> Allocation<Prov, (), Bytes> {
457475 ///
458476 /// Example use case: To obtain an Allocation filled with specific data,
459477 /// first call this function and then call write_scalar to fill in the right data.
460- pub fn new ( size : Size , align : Align , init : AllocInit ) -> Self {
461- match Self :: new_inner ( size, align, init, || {
478+ pub fn new (
479+ size : Size ,
480+ align : Align ,
481+ init : AllocInit ,
482+ params : <Bytes as AllocBytes >:: AllocParams ,
483+ ) -> Self {
484+ match Self :: new_inner ( size, align, init, params, || {
462485 panic ! (
463486 "interpreter ran out of memory: cannot create allocation of {} bytes" ,
464487 size. bytes( )
0 commit comments