@@ -37,9 +37,16 @@ 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] >`.
49
+ /// Default `bytes` for `Allocation` is a `Box<u8 >`.
43
50
impl AllocBytes for Box < [ u8 ] > {
44
51
fn from_bytes < ' a > ( slice : impl Into < Cow < ' a , [ u8 ] > > , _align : Align ) -> Self {
45
52
Box :: < [ u8 ] > :: from ( slice. into ( ) )
@@ -51,6 +58,11 @@ impl AllocBytes for Box<[u8]> {
51
58
let bytes = unsafe { bytes. assume_init ( ) } ;
52
59
Some ( bytes)
53
60
}
61
+
62
+ fn as_mut_ptr ( & mut self ) -> * mut u8 {
63
+ // Carefully avoiding any intermediate references.
64
+ ptr:: addr_of_mut!( * * self ) . cast ( )
65
+ }
54
66
}
55
67
56
68
/// This type represents an Allocation in the Miri/CTFE core engine.
@@ -399,10 +411,6 @@ impl<Prov: Provenance, Extra, Bytes: AllocBytes> Allocation<Prov, Extra, Bytes>
399
411
400
412
/// Byte accessors.
401
413
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
414
/// This is the entirely abstraction-violating way to just grab the raw bytes without
407
415
/// caring about provenance or initialization.
408
416
///
@@ -452,13 +460,14 @@ impl<Prov: Provenance, Extra, Bytes: AllocBytes> Allocation<Prov, Extra, Bytes>
452
460
Ok ( self . get_bytes_unchecked ( range) )
453
461
}
454
462
455
- /// Just calling this already marks everything as defined and removes provenance,
456
- /// so be sure to actually put data there!
463
+ /// This is the entirely abstraction-violating way to just get mutable access to the raw bytes.
464
+ /// Just calling this already marks everything as defined and removes provenance, so be sure to
465
+ /// actually overwrite all the data there!
457
466
///
458
467
/// It is the caller's responsibility to check bounds and alignment beforehand.
459
468
/// Most likely, you want to use the `PlaceTy` and `OperandTy`-based methods
460
469
/// on `InterpCx` instead.
461
- pub fn get_bytes_mut (
470
+ pub fn get_bytes_unchecked_for_overwrite (
462
471
& mut self ,
463
472
cx : & impl HasDataLayout ,
464
473
range : AllocRange ,
@@ -469,8 +478,9 @@ impl<Prov: Provenance, Extra, Bytes: AllocBytes> Allocation<Prov, Extra, Bytes>
469
478
Ok ( & mut self . bytes [ range. start . bytes_usize ( ) ..range. end ( ) . bytes_usize ( ) ] )
470
479
}
471
480
472
- /// A raw pointer variant of `get_bytes_mut` that avoids invalidating existing aliases into this memory.
473
- pub fn get_bytes_mut_ptr (
481
+ /// A raw pointer variant of `get_bytes_unchecked_for_overwrite` that avoids invalidating existing immutable aliases
482
+ /// into this memory.
483
+ pub fn get_bytes_unchecked_for_overwrite_ptr (
474
484
& mut self ,
475
485
cx : & impl HasDataLayout ,
476
486
range : AllocRange ,
@@ -479,10 +489,19 @@ impl<Prov: Provenance, Extra, Bytes: AllocBytes> Allocation<Prov, Extra, Bytes>
479
489
self . provenance . clear ( range, cx) ?;
480
490
481
491
assert ! ( range. end( ) . bytes_usize( ) <= self . bytes. len( ) ) ; // need to do our own bounds-check
492
+ // Cruciall, we go via `AllocBytes::as_mut_ptr`, not `AllocBytes::deref_mut`.
482
493
let begin_ptr = self . bytes . as_mut_ptr ( ) . wrapping_add ( range. start . bytes_usize ( ) ) ;
483
494
let len = range. end ( ) . bytes_usize ( ) - range. start . bytes_usize ( ) ;
484
495
Ok ( ptr:: slice_from_raw_parts_mut ( begin_ptr, len) )
485
496
}
497
+
498
+ /// This gives direct mutable access to the entire buffer, just exposing their internal state
499
+ /// without reseting anything. Directly exposes `AllocBytes::as_mut_ptr`. Only works if
500
+ /// `OFFSET_IS_ADDR` is true.
501
+ pub fn get_bytes_unchecked_raw_mut ( & mut self ) -> * mut u8 {
502
+ assert ! ( Prov :: OFFSET_IS_ADDR ) ;
503
+ self . bytes . as_mut_ptr ( )
504
+ }
486
505
}
487
506
488
507
/// Reading and writing.
@@ -589,7 +608,8 @@ impl<Prov: Provenance, Extra, Bytes: AllocBytes> Allocation<Prov, Extra, Bytes>
589
608
} ;
590
609
591
610
let endian = cx. data_layout ( ) . endian ;
592
- let dst = self . get_bytes_mut ( cx, range) ?;
611
+ // Yes we do overwrite all the bytes in `dst`.
612
+ let dst = self . get_bytes_unchecked_for_overwrite ( cx, range) ?;
593
613
write_target_uint ( endian, dst, bytes) . unwrap ( ) ;
594
614
595
615
// See if we have to also store some provenance.
0 commit comments