Skip to content

Commit b512bf6

Browse files
committed
add as_ptr to trait AllocBytes, fix 2 impls; add pub fn get_bytes_unchecked_raw in allocation.rs; add pub fn get_alloc_bytes_unchecked_raw[_mut] in memory.rs
1 parent e32ea48 commit b512bf6

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed

compiler/rustc_const_eval/src/interpret/memory.rs

+17
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,13 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
630630
}
631631
}
632632

633+
/// Gives raw, immutable access to the `Allocation` address, without bounds or alignment checks.
634+
/// The caller is responsible for calling the access hooks!
635+
pub fn get_alloc_bytes_unchecked_raw(&self, id: AllocId) -> InterpResult<'tcx, *const u8> {
636+
let alloc = self.get_alloc_raw(id)?;
637+
Ok(alloc.get_bytes_unchecked_raw())
638+
}
639+
633640
/// Bounds-checked *but not align-checked* allocation access.
634641
pub fn get_ptr_alloc<'a>(
635642
&'a self,
@@ -713,6 +720,16 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
713720
Ok((alloc, &mut self.machine))
714721
}
715722

723+
/// Gives raw, mutable access to the `Allocation` address, without bounds or alignment checks.
724+
/// The caller is responsible for calling the access hooks!
725+
pub fn get_alloc_bytes_unchecked_raw_mut(
726+
&mut self,
727+
id: AllocId,
728+
) -> InterpResult<'tcx, *mut u8> {
729+
let alloc = self.get_alloc_raw_mut(id)?.0;
730+
Ok(alloc.get_bytes_unchecked_raw_mut())
731+
}
732+
716733
/// Bounds-checked *but not align-checked* allocation access.
717734
pub fn get_ptr_alloc_mut<'a>(
718735
&'a mut self,

compiler/rustc_middle/src/mir/interpret/allocation.rs

+23-3
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,16 @@ pub trait AllocBytes: Clone + fmt::Debug + Deref<Target = [u8]> + DerefMut<Targe
4040
/// Gives direct access to the raw underlying storage.
4141
///
4242
/// Crucially this pointer is compatible with:
43-
/// - other pointers retunred by this method, and
43+
/// - other pointers returned by this method, and
4444
/// - references returned from `deref()`, as long as there was no write.
4545
fn as_mut_ptr(&mut self) -> *mut u8;
46+
47+
/// Gives direct access to the raw underlying storage.
48+
///
49+
/// Crucially this pointer is compatible with:
50+
/// - other pointers returned by this method, and
51+
/// - references returned from `deref()`, as long as there was no write.
52+
fn as_ptr(&self) -> *const u8;
4653
}
4754

4855
/// Default `bytes` for `Allocation` is a `Box<u8>`.
@@ -62,6 +69,11 @@ impl AllocBytes for Box<[u8]> {
6269
// Carefully avoiding any intermediate references.
6370
ptr::addr_of_mut!(**self).cast()
6471
}
72+
73+
fn as_ptr(&self) -> *const u8 {
74+
// Carefully avoiding any intermediate references.
75+
ptr::addr_of!(**self).cast()
76+
}
6577
}
6678

6779
/// This type represents an Allocation in the Miri/CTFE core engine.
@@ -490,19 +502,27 @@ impl<Prov: Provenance, Extra, Bytes: AllocBytes> Allocation<Prov, Extra, Bytes>
490502
self.provenance.clear(range, cx)?;
491503

492504
assert!(range.end().bytes_usize() <= self.bytes.len()); // need to do our own bounds-check
493-
// Cruciall, we go via `AllocBytes::as_mut_ptr`, not `AllocBytes::deref_mut`.
505+
// Crucially, we go via `AllocBytes::as_mut_ptr`, not `AllocBytes::deref_mut`.
494506
let begin_ptr = self.bytes.as_mut_ptr().wrapping_add(range.start.bytes_usize());
495507
let len = range.end().bytes_usize() - range.start.bytes_usize();
496508
Ok(ptr::slice_from_raw_parts_mut(begin_ptr, len))
497509
}
498510

499511
/// This gives direct mutable access to the entire buffer, just exposing their internal state
500-
/// without reseting anything. Directly exposes `AllocBytes::as_mut_ptr`. Only works if
512+
/// without resetting anything. Directly exposes `AllocBytes::as_mut_ptr`. Only works if
501513
/// `OFFSET_IS_ADDR` is true.
502514
pub fn get_bytes_unchecked_raw_mut(&mut self) -> *mut u8 {
503515
assert!(Prov::OFFSET_IS_ADDR);
504516
self.bytes.as_mut_ptr()
505517
}
518+
519+
/// This gives direct immutable access to the entire buffer, just exposing their internal state
520+
/// without resetting anything. Directly exposes `AllocBytes::as_ptr`. Only works if
521+
/// `OFFSET_IS_ADDR` is true.
522+
pub fn get_bytes_unchecked_raw(&self) -> *const u8 {
523+
assert!(Prov::OFFSET_IS_ADDR);
524+
self.bytes.as_ptr()
525+
}
506526
}
507527

508528
/// Reading and writing.

src/tools/miri/src/alloc_bytes.rs

+4
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,8 @@ impl AllocBytes for MiriAllocBytes {
108108
fn as_mut_ptr(&mut self) -> *mut u8 {
109109
self.ptr
110110
}
111+
112+
fn as_ptr(&self) -> *const u8 {
113+
self.ptr
114+
}
111115
}

0 commit comments

Comments
 (0)