@@ -37,19 +37,30 @@ pub trait AllocBytes:
3737 /// Create a zeroed `AllocBytes` of the specified size and alignment.
3838 /// Returns `None` if we ran out of memory on the host.
3939 fn zeroed ( size : Size , _align : Align ) -> Option < Self > ;
40+
41+ /// Gives direct access to the raw underlying storage.
42+ ///
43+ /// Crucially this pointer is compatible with:
44+ /// - other pointers retunred by this method, and
45+ /// - references returned from `deref()`, as long as there was no write.
46+ fn as_mut_ptr ( & mut self ) -> * mut u8 ;
4047}
4148
42- // Default `bytes` for `Allocation` is a `Box<[u8] >`.
43- impl AllocBytes for Box < [ u8 ] > {
49+ // Default `bytes` for `Allocation` is a `Vec<u8 >`.
50+ impl AllocBytes for Vec < u8 > {
4451 fn from_bytes < ' a > ( slice : impl Into < Cow < ' a , [ u8 ] > > , _align : Align ) -> Self {
45- Box :: < [ u8 ] > :: from ( slice. into ( ) )
52+ slice. into ( ) . into_owned ( )
4653 }
4754
4855 fn zeroed ( size : Size , _align : Align ) -> Option < Self > {
4956 let bytes = Box :: < [ u8 ] > :: try_new_zeroed_slice ( size. bytes_usize ( ) ) . ok ( ) ?;
5057 // SAFETY: the box was zero-allocated, which is a valid initial value for Box<[u8]>
5158 let bytes = unsafe { bytes. assume_init ( ) } ;
52- Some ( bytes)
59+ Some ( bytes. into ( ) )
60+ }
61+
62+ fn as_mut_ptr ( & mut self ) -> * mut u8 {
63+ Vec :: as_mut_ptr ( self )
5364 }
5465}
5566
@@ -62,7 +73,7 @@ impl AllocBytes for Box<[u8]> {
6273// hashed. (see the `Hash` impl below for more details), so the impl is not derived.
6374#[ derive( Clone , Eq , PartialEq , TyEncodable , TyDecodable ) ]
6475#[ derive( HashStable ) ]
65- pub struct Allocation < Prov : Provenance = CtfeProvenance , Extra = ( ) , Bytes = Box < [ u8 ] > > {
76+ pub struct Allocation < Prov : Provenance = CtfeProvenance , Extra = ( ) , Bytes = Vec < u8 > > {
6677 /// The actual bytes of the allocation.
6778 /// Note that the bytes of a pointer represent the offset of the pointer.
6879 bytes : Bytes ,
@@ -399,10 +410,6 @@ impl<Prov: Provenance, Extra, Bytes: AllocBytes> Allocation<Prov, Extra, Bytes>
399410
400411/// Byte accessors.
401412impl < Prov : Provenance , Extra , Bytes : AllocBytes > Allocation < Prov , Extra , Bytes > {
402- pub fn base_addr ( & self ) -> * const u8 {
403- self . bytes . as_ptr ( )
404- }
405-
406413 /// This is the entirely abstraction-violating way to just grab the raw bytes without
407414 /// caring about provenance or initialization.
408415 ///
@@ -452,13 +459,14 @@ impl<Prov: Provenance, Extra, Bytes: AllocBytes> Allocation<Prov, Extra, Bytes>
452459 Ok ( self . get_bytes_unchecked ( range) )
453460 }
454461
455- /// Just calling this already marks everything as defined and removes provenance,
456- /// so be sure to actually put data there!
462+ /// This is the entirely abstraction-violating way to just get mutable access to the raw bytes.
463+ /// Just calling this already marks everything as defined and removes provenance, so be sure to
464+ /// actually overwrite all the data there!
457465 ///
458466 /// It is the caller's responsibility to check bounds and alignment beforehand.
459467 /// Most likely, you want to use the `PlaceTy` and `OperandTy`-based methods
460468 /// on `InterpCx` instead.
461- pub fn get_bytes_mut (
469+ pub fn get_bytes_unchecked_for_overwrite (
462470 & mut self ,
463471 cx : & impl HasDataLayout ,
464472 range : AllocRange ,
@@ -469,8 +477,9 @@ impl<Prov: Provenance, Extra, Bytes: AllocBytes> Allocation<Prov, Extra, Bytes>
469477 Ok ( & mut self . bytes [ range. start . bytes_usize ( ) ..range. end ( ) . bytes_usize ( ) ] )
470478 }
471479
472- /// A raw pointer variant of `get_bytes_mut` that avoids invalidating existing aliases into this memory.
473- pub fn get_bytes_mut_ptr (
480+ /// A raw pointer variant of `get_bytes_unchecked_for_overwrite` that avoids invalidating existing immutable aliases
481+ /// into this memory.
482+ pub fn get_bytes_unchecked_for_overwrite_ptr (
474483 & mut self ,
475484 cx : & impl HasDataLayout ,
476485 range : AllocRange ,
@@ -479,10 +488,19 @@ impl<Prov: Provenance, Extra, Bytes: AllocBytes> Allocation<Prov, Extra, Bytes>
479488 self . provenance . clear ( range, cx) ?;
480489
481490 assert ! ( range. end( ) . bytes_usize( ) <= self . bytes. len( ) ) ; // need to do our own bounds-check
491+ // Cruciall, we go via `AllocBytes::as_mut_ptr`, not `AllocBytes::deref_mut`.
482492 let begin_ptr = self . bytes . as_mut_ptr ( ) . wrapping_add ( range. start . bytes_usize ( ) ) ;
483493 let len = range. end ( ) . bytes_usize ( ) - range. start . bytes_usize ( ) ;
484494 Ok ( ptr:: slice_from_raw_parts_mut ( begin_ptr, len) )
485495 }
496+
497+ /// This gives direct mutable access to the entire buffer, just exposing their internal state
498+ /// without reseting anything. Directly exposes `AllocBytes::as_mut_ptr`. Only works if
499+ /// `OFFSET_IS_ADDR` is true.
500+ pub fn get_bytes_unchecked_raw_mut ( & mut self ) -> * mut u8 {
501+ assert ! ( Prov :: OFFSET_IS_ADDR ) ;
502+ self . bytes . as_mut_ptr ( )
503+ }
486504}
487505
488506/// Reading and writing.
@@ -589,7 +607,8 @@ impl<Prov: Provenance, Extra, Bytes: AllocBytes> Allocation<Prov, Extra, Bytes>
589607 } ;
590608
591609 let endian = cx. data_layout ( ) . endian ;
592- let dst = self . get_bytes_mut ( cx, range) ?;
610+ // Yes we do overwrite all the bytes in `dst`.
611+ let dst = self . get_bytes_unchecked_for_overwrite ( cx, range) ?;
593612 write_target_uint ( endian, dst, bytes) . unwrap ( ) ;
594613
595614 // See if we have to also store some provenance.
0 commit comments