@@ -37,19 +37,33 @@ pub trait AllocBytes:
37
37
/// Create a zeroed `AllocBytes` of the specified size and alignment.
38
38
/// Returns `None` if we ran out of memory on the host.
39
39
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 ;
40
47
}
41
48
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
+ ///
51
+ /// We use `Vec`, not `Box`, since we need `Vec::as_mut_ptr` and how it interacts with other
52
+ /// pointers to the backing buffer. `Box` has no corresponding method.
53
+ impl AllocBytes for Vec < u8 > {
44
54
fn from_bytes < ' a > ( slice : impl Into < Cow < ' a , [ u8 ] > > , _align : Align ) -> Self {
45
- Box :: < [ u8 ] > :: from ( slice. into ( ) )
55
+ slice. into ( ) . into_owned ( )
46
56
}
47
57
48
58
fn zeroed ( size : Size , _align : Align ) -> Option < Self > {
49
59
let bytes = Box :: < [ u8 ] > :: try_new_zeroed_slice ( size. bytes_usize ( ) ) . ok ( ) ?;
50
60
// SAFETY: the box was zero-allocated, which is a valid initial value for Box<[u8]>
51
61
let bytes = unsafe { bytes. assume_init ( ) } ;
52
- Some ( bytes)
62
+ Some ( bytes. into ( ) )
63
+ }
64
+
65
+ fn as_mut_ptr ( & mut self ) -> * mut u8 {
66
+ Vec :: as_mut_ptr ( self )
53
67
}
54
68
}
55
69
@@ -62,7 +76,7 @@ impl AllocBytes for Box<[u8]> {
62
76
// hashed. (see the `Hash` impl below for more details), so the impl is not derived.
63
77
#[ derive( Clone , Eq , PartialEq , TyEncodable , TyDecodable ) ]
64
78
#[ derive( HashStable ) ]
65
- pub struct Allocation < Prov : Provenance = CtfeProvenance , Extra = ( ) , Bytes = Box < [ u8 ] > > {
79
+ pub struct Allocation < Prov : Provenance = CtfeProvenance , Extra = ( ) , Bytes = Vec < u8 > > {
66
80
/// The actual bytes of the allocation.
67
81
/// Note that the bytes of a pointer represent the offset of the pointer.
68
82
bytes : Bytes ,
@@ -399,10 +413,6 @@ impl<Prov: Provenance, Extra, Bytes: AllocBytes> Allocation<Prov, Extra, Bytes>
399
413
400
414
/// Byte accessors.
401
415
impl < Prov : Provenance , Extra , Bytes : AllocBytes > Allocation < Prov , Extra , Bytes > {
402
- pub fn base_addr ( & self ) -> * const u8 {
403
- self . bytes . as_ptr ( )
404
- }
405
-
406
416
/// This is the entirely abstraction-violating way to just grab the raw bytes without
407
417
/// caring about provenance or initialization.
408
418
///
@@ -452,13 +462,14 @@ impl<Prov: Provenance, Extra, Bytes: AllocBytes> Allocation<Prov, Extra, Bytes>
452
462
Ok ( self . get_bytes_unchecked ( range) )
453
463
}
454
464
455
- /// Just calling this already marks everything as defined and removes provenance,
456
- /// so be sure to actually put data there!
465
+ /// This is the entirely abstraction-violating way to just get mutable access to the raw bytes.
466
+ /// Just calling this already marks everything as defined and removes provenance, so be sure to
467
+ /// actually overwrite all the data there!
457
468
///
458
469
/// It is the caller's responsibility to check bounds and alignment beforehand.
459
470
/// Most likely, you want to use the `PlaceTy` and `OperandTy`-based methods
460
471
/// on `InterpCx` instead.
461
- pub fn get_bytes_mut (
472
+ pub fn get_bytes_unchecked_for_overwrite (
462
473
& mut self ,
463
474
cx : & impl HasDataLayout ,
464
475
range : AllocRange ,
@@ -469,8 +480,9 @@ impl<Prov: Provenance, Extra, Bytes: AllocBytes> Allocation<Prov, Extra, Bytes>
469
480
Ok ( & mut self . bytes [ range. start . bytes_usize ( ) ..range. end ( ) . bytes_usize ( ) ] )
470
481
}
471
482
472
- /// A raw pointer variant of `get_bytes_mut` that avoids invalidating existing aliases into this memory.
473
- pub fn get_bytes_mut_ptr (
483
+ /// A raw pointer variant of `get_bytes_unchecked_for_overwrite` that avoids invalidating existing immutable aliases
484
+ /// into this memory.
485
+ pub fn get_bytes_unchecked_for_overwrite_ptr (
474
486
& mut self ,
475
487
cx : & impl HasDataLayout ,
476
488
range : AllocRange ,
@@ -479,10 +491,19 @@ impl<Prov: Provenance, Extra, Bytes: AllocBytes> Allocation<Prov, Extra, Bytes>
479
491
self . provenance . clear ( range, cx) ?;
480
492
481
493
assert ! ( range. end( ) . bytes_usize( ) <= self . bytes. len( ) ) ; // need to do our own bounds-check
494
+ // Cruciall, we go via `AllocBytes::as_mut_ptr`, not `AllocBytes::deref_mut`.
482
495
let begin_ptr = self . bytes . as_mut_ptr ( ) . wrapping_add ( range. start . bytes_usize ( ) ) ;
483
496
let len = range. end ( ) . bytes_usize ( ) - range. start . bytes_usize ( ) ;
484
497
Ok ( ptr:: slice_from_raw_parts_mut ( begin_ptr, len) )
485
498
}
499
+
500
+ /// This gives direct mutable access to the entire buffer, just exposing their internal state
501
+ /// without reseting anything. Directly exposes `AllocBytes::as_mut_ptr`. Only works if
502
+ /// `OFFSET_IS_ADDR` is true.
503
+ pub fn get_bytes_unchecked_raw_mut ( & mut self ) -> * mut u8 {
504
+ assert ! ( Prov :: OFFSET_IS_ADDR ) ;
505
+ self . bytes . as_mut_ptr ( )
506
+ }
486
507
}
487
508
488
509
/// Reading and writing.
@@ -589,7 +610,8 @@ impl<Prov: Provenance, Extra, Bytes: AllocBytes> Allocation<Prov, Extra, Bytes>
589
610
} ;
590
611
591
612
let endian = cx. data_layout ( ) . endian ;
592
- let dst = self . get_bytes_mut ( cx, range) ?;
613
+ // Yes we do overwrite all the bytes in `dst`.
614
+ let dst = self . get_bytes_unchecked_for_overwrite ( cx, range) ?;
593
615
write_target_uint ( endian, dst, bytes) . unwrap ( ) ;
594
616
595
617
// See if we have to also store some provenance.
0 commit comments