Skip to content

Commit 39c7aed

Browse files
Rollup merge of rust-lang#108022 - CraftSpider:align-bytes, r=oli-obk
Support allocations with non-Box<[u8]> bytes This is prep work for allowing miri to support passing pointers to C code, which will require `Allocation`s to be correctly aligned. Currently, it just makes `Allocation` generic and plumbs the necessary changes through the right places. The follow-up to this will be adding a type in the miri interpreter which correctly aligns the bytes, using that for the Miri engine, then allowing Miri to pass pointers into these allocations to C calls. Based off of rust-lang#100467, credit to `@emarteca` for the code
2 parents 18caf88 + f26b0a2 commit 39c7aed

File tree

7 files changed

+128
-50
lines changed

7 files changed

+128
-50
lines changed

compiler/rustc_const_eval/src/interpret/machine.rs

+11-4
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ use rustc_target::spec::abi::Abi as CallAbi;
1616
use crate::const_eval::CheckAlignment;
1717

1818
use super::{
19-
AllocId, AllocRange, Allocation, ConstAllocation, Frame, ImmTy, InterpCx, InterpResult,
20-
MemoryKind, OpTy, Operand, PlaceTy, Pointer, Provenance, Scalar, StackPopUnwind,
19+
AllocBytes, AllocId, AllocRange, Allocation, ConstAllocation, Frame, ImmTy, InterpCx,
20+
InterpResult, MemoryKind, OpTy, Operand, PlaceTy, Pointer, Provenance, Scalar, StackPopUnwind,
2121
};
2222

2323
/// Data returned by Machine::stack_pop,
@@ -105,10 +105,16 @@ pub trait Machine<'mir, 'tcx>: Sized {
105105
/// Extra data stored in every allocation.
106106
type AllocExtra: Debug + Clone + 'static;
107107

108+
/// Type for the bytes of the allocation.
109+
type Bytes: AllocBytes + 'static;
110+
108111
/// Memory's allocation map
109112
type MemoryMap: AllocMap<
110113
AllocId,
111-
(MemoryKind<Self::MemoryKind>, Allocation<Self::Provenance, Self::AllocExtra>),
114+
(
115+
MemoryKind<Self::MemoryKind>,
116+
Allocation<Self::Provenance, Self::AllocExtra, Self::Bytes>,
117+
),
112118
> + Default
113119
+ Clone;
114120

@@ -338,7 +344,7 @@ pub trait Machine<'mir, 'tcx>: Sized {
338344
id: AllocId,
339345
alloc: Cow<'b, Allocation>,
340346
kind: Option<MemoryKind<Self::MemoryKind>>,
341-
) -> InterpResult<'tcx, Cow<'b, Allocation<Self::Provenance, Self::AllocExtra>>>;
347+
) -> InterpResult<'tcx, Cow<'b, Allocation<Self::Provenance, Self::AllocExtra, Self::Bytes>>>;
342348

343349
fn eval_inline_asm(
344350
_ecx: &mut InterpCx<'mir, 'tcx, Self>,
@@ -459,6 +465,7 @@ pub macro compile_time_machine(<$mir: lifetime, $tcx: lifetime>) {
459465

460466
type AllocExtra = ();
461467
type FrameExtra = ();
468+
type Bytes = Box<[u8]>;
462469

463470
#[inline(always)]
464471
fn use_addr_for_alignment_check(_ecx: &InterpCx<$mir, $tcx, Self>) -> bool {

compiler/rustc_const_eval/src/interpret/memory.rs

+31-15
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ use rustc_target::abi::{Align, HasDataLayout, Size};
2121
use crate::const_eval::CheckAlignment;
2222

2323
use super::{
24-
alloc_range, AllocId, AllocMap, AllocRange, Allocation, CheckInAllocMsg, GlobalAlloc, InterpCx,
25-
InterpResult, Machine, MayLeak, Pointer, PointerArithmetic, Provenance, Scalar,
24+
alloc_range, AllocBytes, AllocId, AllocMap, AllocRange, Allocation, CheckInAllocMsg,
25+
GlobalAlloc, InterpCx, InterpResult, Machine, MayLeak, Pointer, PointerArithmetic, Provenance,
26+
Scalar,
2627
};
2728

2829
#[derive(Debug, PartialEq, Copy, Clone)]
@@ -114,16 +115,16 @@ pub struct Memory<'mir, 'tcx, M: Machine<'mir, 'tcx>> {
114115
/// A reference to some allocation that was already bounds-checked for the given region
115116
/// and had the on-access machine hooks run.
116117
#[derive(Copy, Clone)]
117-
pub struct AllocRef<'a, 'tcx, Prov: Provenance, Extra> {
118-
alloc: &'a Allocation<Prov, Extra>,
118+
pub struct AllocRef<'a, 'tcx, Prov: Provenance, Extra, Bytes: AllocBytes = Box<[u8]>> {
119+
alloc: &'a Allocation<Prov, Extra, Bytes>,
119120
range: AllocRange,
120121
tcx: TyCtxt<'tcx>,
121122
alloc_id: AllocId,
122123
}
123124
/// A reference to some allocation that was already bounds-checked for the given region
124125
/// and had the on-access machine hooks run.
125-
pub struct AllocRefMut<'a, 'tcx, Prov: Provenance, Extra> {
126-
alloc: &'a mut Allocation<Prov, Extra>,
126+
pub struct AllocRefMut<'a, 'tcx, Prov: Provenance, Extra, Bytes: AllocBytes = Box<[u8]>> {
127+
alloc: &'a mut Allocation<Prov, Extra, Bytes>,
127128
range: AllocRange,
128129
tcx: TyCtxt<'tcx>,
129130
alloc_id: AllocId,
@@ -483,7 +484,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
483484
&self,
484485
id: AllocId,
485486
is_write: bool,
486-
) -> InterpResult<'tcx, Cow<'tcx, Allocation<M::Provenance, M::AllocExtra>>> {
487+
) -> InterpResult<'tcx, Cow<'tcx, Allocation<M::Provenance, M::AllocExtra, M::Bytes>>> {
487488
let (alloc, def_id) = match self.tcx.try_get_global_alloc(id) {
488489
Some(GlobalAlloc::Memory(mem)) => {
489490
// Memory of a constant or promoted or anonymous memory referenced by a static.
@@ -526,14 +527,25 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
526527
)
527528
}
528529

530+
/// Get the base address for the bytes in an `Allocation` specified by the
531+
/// `AllocID` passed in; error if no such allocation exists.
532+
///
533+
/// It is up to the caller to take sufficient care when using this address:
534+
/// there could be provenance or uninit memory in there, and other memory
535+
/// accesses could invalidate the exposed pointer.
536+
pub fn alloc_base_addr(&self, id: AllocId) -> InterpResult<'tcx, *const u8> {
537+
let alloc = self.get_alloc_raw(id)?;
538+
Ok(alloc.base_addr())
539+
}
540+
529541
/// Gives raw access to the `Allocation`, without bounds or alignment checks.
530542
/// The caller is responsible for calling the access hooks!
531543
///
532544
/// You almost certainly want to use `get_ptr_alloc`/`get_ptr_alloc_mut` instead.
533545
fn get_alloc_raw(
534546
&self,
535547
id: AllocId,
536-
) -> InterpResult<'tcx, &Allocation<M::Provenance, M::AllocExtra>> {
548+
) -> InterpResult<'tcx, &Allocation<M::Provenance, M::AllocExtra, M::Bytes>> {
537549
// The error type of the inner closure here is somewhat funny. We have two
538550
// ways of "erroring": An actual error, or because we got a reference from
539551
// `get_global_alloc` that we can actually use directly without inserting anything anywhere.
@@ -569,7 +581,8 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
569581
ptr: Pointer<Option<M::Provenance>>,
570582
size: Size,
571583
align: Align,
572-
) -> InterpResult<'tcx, Option<AllocRef<'a, 'tcx, M::Provenance, M::AllocExtra>>> {
584+
) -> InterpResult<'tcx, Option<AllocRef<'a, 'tcx, M::Provenance, M::AllocExtra, M::Bytes>>>
585+
{
573586
let ptr_and_alloc = self.check_and_deref_ptr(
574587
ptr,
575588
size,
@@ -612,7 +625,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
612625
fn get_alloc_raw_mut(
613626
&mut self,
614627
id: AllocId,
615-
) -> InterpResult<'tcx, (&mut Allocation<M::Provenance, M::AllocExtra>, &mut M)> {
628+
) -> InterpResult<'tcx, (&mut Allocation<M::Provenance, M::AllocExtra, M::Bytes>, &mut M)> {
616629
// We have "NLL problem case #3" here, which cannot be worked around without loss of
617630
// efficiency even for the common case where the key is in the map.
618631
// <https://rust-lang.github.io/rfcs/2094-nll.html#problem-case-3-conditional-control-flow-across-functions>
@@ -641,7 +654,8 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
641654
ptr: Pointer<Option<M::Provenance>>,
642655
size: Size,
643656
align: Align,
644-
) -> InterpResult<'tcx, Option<AllocRefMut<'a, 'tcx, M::Provenance, M::AllocExtra>>> {
657+
) -> InterpResult<'tcx, Option<AllocRefMut<'a, 'tcx, M::Provenance, M::AllocExtra, M::Bytes>>>
658+
{
645659
let parts = self.get_ptr_access(ptr, size, align)?;
646660
if let Some((alloc_id, offset, prov)) = parts {
647661
let tcx = *self.tcx;
@@ -840,11 +854,11 @@ pub struct DumpAllocs<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> {
840854
impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> std::fmt::Debug for DumpAllocs<'a, 'mir, 'tcx, M> {
841855
fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
842856
// Cannot be a closure because it is generic in `Prov`, `Extra`.
843-
fn write_allocation_track_relocs<'tcx, Prov: Provenance, Extra>(
857+
fn write_allocation_track_relocs<'tcx, Prov: Provenance, Extra, Bytes: AllocBytes>(
844858
fmt: &mut std::fmt::Formatter<'_>,
845859
tcx: TyCtxt<'tcx>,
846860
allocs_to_print: &mut VecDeque<AllocId>,
847-
alloc: &Allocation<Prov, Extra>,
861+
alloc: &Allocation<Prov, Extra, Bytes>,
848862
) -> std::fmt::Result {
849863
for alloc_id in alloc.provenance().provenances().filter_map(|prov| prov.get_alloc_id())
850864
{
@@ -912,7 +926,9 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> std::fmt::Debug for DumpAllocs<'a,
912926
}
913927

914928
/// Reading and writing.
915-
impl<'tcx, 'a, Prov: Provenance, Extra> AllocRefMut<'a, 'tcx, Prov, Extra> {
929+
impl<'tcx, 'a, Prov: Provenance, Extra, Bytes: AllocBytes>
930+
AllocRefMut<'a, 'tcx, Prov, Extra, Bytes>
931+
{
916932
/// `range` is relative to this allocation reference, not the base of the allocation.
917933
pub fn write_scalar(&mut self, range: AllocRange, val: Scalar<Prov>) -> InterpResult<'tcx> {
918934
let range = self.range.subrange(range);
@@ -937,7 +953,7 @@ impl<'tcx, 'a, Prov: Provenance, Extra> AllocRefMut<'a, 'tcx, Prov, Extra> {
937953
}
938954
}
939955

940-
impl<'tcx, 'a, Prov: Provenance, Extra> AllocRef<'a, 'tcx, Prov, Extra> {
956+
impl<'tcx, 'a, Prov: Provenance, Extra, Bytes: AllocBytes> AllocRef<'a, 'tcx, Prov, Extra, Bytes> {
941957
/// `range` is relative to this allocation reference, not the base of the allocation.
942958
pub fn read_scalar(
943959
&self,

compiler/rustc_const_eval/src/interpret/place.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,8 @@ where
353353
pub(super) fn get_place_alloc(
354354
&self,
355355
place: &MPlaceTy<'tcx, M::Provenance>,
356-
) -> InterpResult<'tcx, Option<AllocRef<'_, 'tcx, M::Provenance, M::AllocExtra>>> {
356+
) -> InterpResult<'tcx, Option<AllocRef<'_, 'tcx, M::Provenance, M::AllocExtra, M::Bytes>>>
357+
{
357358
assert!(place.layout.is_sized());
358359
assert!(!place.meta.has_meta());
359360
let size = place.layout.size;
@@ -364,7 +365,8 @@ where
364365
pub(super) fn get_place_alloc_mut(
365366
&mut self,
366367
place: &MPlaceTy<'tcx, M::Provenance>,
367-
) -> InterpResult<'tcx, Option<AllocRefMut<'_, 'tcx, M::Provenance, M::AllocExtra>>> {
368+
) -> InterpResult<'tcx, Option<AllocRefMut<'_, 'tcx, M::Provenance, M::AllocExtra, M::Bytes>>>
369+
{
368370
assert!(place.layout.is_sized());
369371
assert!(!place.meta.has_meta());
370372
let size = place.layout.size;

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

+67-15
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ mod tests;
88
use std::borrow::Cow;
99
use std::fmt;
1010
use std::hash;
11-
use std::ops::Range;
11+
use std::hash::Hash;
12+
use std::ops::{Deref, DerefMut, Range};
1213
use std::ptr;
1314

1415
use either::{Left, Right};
@@ -29,6 +30,39 @@ use provenance_map::*;
2930

3031
pub use init_mask::{InitChunk, InitChunkIter};
3132

33+
/// Functionality required for the bytes of an `Allocation`.
34+
pub trait AllocBytes:
35+
Clone + fmt::Debug + Eq + PartialEq + Hash + Deref<Target = [u8]> + DerefMut<Target = [u8]>
36+
{
37+
/// Adjust the bytes to the specified alignment -- by default, this is a no-op.
38+
fn adjust_to_align(self, _align: Align) -> Self;
39+
40+
/// Create an `AllocBytes` from a slice of `u8`.
41+
fn from_bytes<'a>(slice: impl Into<Cow<'a, [u8]>>, _align: Align) -> Self;
42+
43+
/// Create a zeroed `AllocBytes` of the specified size and alignment;
44+
/// call the callback error handler if there is an error in allocating the memory.
45+
fn zeroed(size: Size, _align: Align) -> Option<Self>;
46+
}
47+
48+
// Default `bytes` for `Allocation` is a `Box<[u8]>`.
49+
impl AllocBytes for Box<[u8]> {
50+
fn adjust_to_align(self, _align: Align) -> Self {
51+
self
52+
}
53+
54+
fn from_bytes<'a>(slice: impl Into<Cow<'a, [u8]>>, _align: Align) -> Self {
55+
Box::<[u8]>::from(slice.into())
56+
}
57+
58+
fn zeroed(size: Size, _align: Align) -> Option<Self> {
59+
let bytes = Box::<[u8]>::try_new_zeroed_slice(size.bytes_usize()).ok()?;
60+
// SAFETY: the box was zero-allocated, which is a valid initial value for Box<[u8]>
61+
let bytes = unsafe { bytes.assume_init() };
62+
Some(bytes)
63+
}
64+
}
65+
3266
/// This type represents an Allocation in the Miri/CTFE core engine.
3367
///
3468
/// Its public API is rather low-level, working directly with allocation offsets and a custom error
@@ -38,10 +72,10 @@ pub use init_mask::{InitChunk, InitChunkIter};
3872
// hashed. (see the `Hash` impl below for more details), so the impl is not derived.
3973
#[derive(Clone, Eq, PartialEq, TyEncodable, TyDecodable)]
4074
#[derive(HashStable)]
41-
pub struct Allocation<Prov: Provenance = AllocId, Extra = ()> {
75+
pub struct Allocation<Prov: Provenance = AllocId, Extra = (), Bytes = Box<[u8]>> {
4276
/// The actual bytes of the allocation.
4377
/// Note that the bytes of a pointer represent the offset of the pointer.
44-
bytes: Box<[u8]>,
78+
bytes: Bytes,
4579
/// Maps from byte addresses to extra provenance data for each pointer.
4680
/// Only the first byte of a pointer is inserted into the map; i.e.,
4781
/// every entry in this map applies to `pointer_size` consecutive bytes starting
@@ -220,14 +254,27 @@ impl AllocRange {
220254
}
221255

222256
// The constructors are all without extra; the extra gets added by a machine hook later.
223-
impl<Prov: Provenance> Allocation<Prov> {
257+
impl<Prov: Provenance, Bytes: AllocBytes> Allocation<Prov, (), Bytes> {
258+
/// Creates an allocation from an existing `Bytes` value - this is needed for miri FFI support
259+
pub fn from_raw_bytes(bytes: Bytes, align: Align, mutability: Mutability) -> Self {
260+
let size = Size::from_bytes(bytes.len());
261+
Self {
262+
bytes,
263+
provenance: ProvenanceMap::new(),
264+
init_mask: InitMask::new(size, true),
265+
align,
266+
mutability,
267+
extra: (),
268+
}
269+
}
270+
224271
/// Creates an allocation initialized by the given bytes
225272
pub fn from_bytes<'a>(
226273
slice: impl Into<Cow<'a, [u8]>>,
227274
align: Align,
228275
mutability: Mutability,
229276
) -> Self {
230-
let bytes = Box::<[u8]>::from(slice.into());
277+
let bytes = Bytes::from_bytes(slice, align);
231278
let size = Size::from_bytes(bytes.len());
232279
Self {
233280
bytes,
@@ -248,7 +295,7 @@ impl<Prov: Provenance> Allocation<Prov> {
248295
///
249296
/// If `panic_on_fail` is true, this will never return `Err`.
250297
pub fn uninit<'tcx>(size: Size, align: Align, panic_on_fail: bool) -> InterpResult<'tcx, Self> {
251-
let bytes = Box::<[u8]>::try_new_zeroed_slice(size.bytes_usize()).map_err(|_| {
298+
let bytes = Bytes::zeroed(size, align).ok_or_else(|| {
252299
// This results in an error that can happen non-deterministically, since the memory
253300
// available to the compiler can change between runs. Normally queries are always
254301
// deterministic. However, we can be non-deterministic here because all uses of const
@@ -262,8 +309,7 @@ impl<Prov: Provenance> Allocation<Prov> {
262309
});
263310
InterpError::ResourceExhaustion(ResourceExhaustionInfo::MemoryExhausted)
264311
})?;
265-
// SAFETY: the box was zero-allocated, which is a valid initial value for Box<[u8]>
266-
let bytes = unsafe { bytes.assume_init() };
312+
267313
Ok(Allocation {
268314
bytes,
269315
provenance: ProvenanceMap::new(),
@@ -275,17 +321,19 @@ impl<Prov: Provenance> Allocation<Prov> {
275321
}
276322
}
277323

278-
impl Allocation {
324+
impl<Bytes: AllocBytes> Allocation<AllocId, (), Bytes> {
279325
/// Adjust allocation from the ones in tcx to a custom Machine instance
280326
/// with a different Provenance and Extra type.
281327
pub fn adjust_from_tcx<Prov: Provenance, Extra, Err>(
282328
self,
283329
cx: &impl HasDataLayout,
284330
extra: Extra,
285331
mut adjust_ptr: impl FnMut(Pointer<AllocId>) -> Result<Pointer<Prov>, Err>,
286-
) -> Result<Allocation<Prov, Extra>, Err> {
287-
// Compute new pointer provenance, which also adjusts the bytes.
288-
let mut bytes = self.bytes;
332+
) -> Result<Allocation<Prov, Extra, Bytes>, Err> {
333+
// Compute new pointer provenance, which also adjusts the bytes, and realign the pointer if
334+
// necessary.
335+
let mut bytes = self.bytes.adjust_to_align(self.align);
336+
289337
let mut new_provenance = Vec::with_capacity(self.provenance.ptrs().len());
290338
let ptr_size = cx.data_layout().pointer_size.bytes_usize();
291339
let endian = cx.data_layout().endian;
@@ -311,7 +359,7 @@ impl Allocation {
311359
}
312360

313361
/// Raw accessors. Provide access to otherwise private bytes.
314-
impl<Prov: Provenance, Extra> Allocation<Prov, Extra> {
362+
impl<Prov: Provenance, Extra, Bytes: AllocBytes> Allocation<Prov, Extra, Bytes> {
315363
pub fn len(&self) -> usize {
316364
self.bytes.len()
317365
}
@@ -340,7 +388,11 @@ impl<Prov: Provenance, Extra> Allocation<Prov, Extra> {
340388
}
341389

342390
/// Byte accessors.
343-
impl<Prov: Provenance, Extra> Allocation<Prov, Extra> {
391+
impl<Prov: Provenance, Extra, Bytes: AllocBytes> Allocation<Prov, Extra, Bytes> {
392+
pub fn base_addr(&self) -> *const u8 {
393+
self.bytes.as_ptr()
394+
}
395+
344396
/// This is the entirely abstraction-violating way to just grab the raw bytes without
345397
/// caring about provenance or initialization.
346398
///
@@ -412,7 +464,7 @@ impl<Prov: Provenance, Extra> Allocation<Prov, Extra> {
412464
}
413465

414466
/// Reading and writing.
415-
impl<Prov: Provenance, Extra> Allocation<Prov, Extra> {
467+
impl<Prov: Provenance, Extra, Bytes: AllocBytes> Allocation<Prov, Extra, Bytes> {
416468
/// Sets the init bit for the given range.
417469
fn mark_init(&mut self, range: AllocRange, is_init: bool) {
418470
if range.size.bytes() == 0 {

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,8 @@ pub use self::error::{
127127
pub use self::value::{get_slice_bytes, ConstAlloc, ConstValue, Scalar};
128128

129129
pub use self::allocation::{
130-
alloc_range, AllocError, AllocRange, AllocResult, Allocation, ConstAllocation, InitChunk,
131-
InitChunkIter,
130+
alloc_range, AllocBytes, AllocError, AllocRange, AllocResult, Allocation, ConstAllocation,
131+
InitChunk, InitChunkIter,
132132
};
133133

134134
pub use self::pointer::{Pointer, PointerArithmetic, Provenance};

0 commit comments

Comments
 (0)