Skip to content

Commit 9498d5c

Browse files
Rollup merge of #126787 - Strophox:get-bytes, r=RalfJung
Add direct accessors for memory addresses in `Machine` (for Miri) The purpose of this PR is to enable direct (immutable) access to memory addresses in `Machine`, which will be needed for further extension of Miri. This is done by adding (/completing missings pairs of) accessor functions, with the relevant signatures as follows: ```rust /* rust/compiler/rustc_middle/src/mir/interpret/allocation.rs */ pub trait AllocBytes { // .. fn as_ptr(&self) -> *const u8; /*fn as_mut_ptr(&mut self) -> *mut u8; -- Already in the compiler*/ } impl<Prov: Provenance, Extra, Bytes: AllocBytes> Allocation<Prov, Extra, Bytes> { // .. pub fn get_bytes_unchecked_raw(&self) -> *const u8; /*pub fn get_bytes_unchecked_raw_mut(&mut self) -> *mut u8; -- Already in the compiler*/ } ``` ```rust /* rust/compiler/rustc_const_eval/src/interpret/memory.rs */ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> { // .. pub fn get_alloc_bytes_unchecked_raw(&self, id: AllocId) -> InterpResult<'tcx, *const u8>; pub fn get_alloc_bytes_unchecked_raw_mut(&mut self, id: AllocId) -> InterpResult<'tcx, *mut u8>; } ``` r? ``@RalfJung``
2 parents 1f9793f + b512bf6 commit 9498d5c

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed

compiler/rustc_const_eval/src/interpret/memory.rs

Lines changed: 17 additions & 0 deletions
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

Lines changed: 23 additions & 3 deletions
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

Lines changed: 4 additions & 0 deletions
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy