Skip to content

Commit b65ca27

Browse files
committed
interpret/allocation: fix aliasing issue in interpreter and refactor getters a bit
- rename mutating functions to be more scary - add a new raw bytes getter
1 parent ee03c28 commit b65ca27

File tree

4 files changed

+52
-22
lines changed

4 files changed

+52
-22
lines changed

compiler/rustc_const_eval/src/interpret/machine.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,7 @@ pub macro compile_time_machine(<$mir: lifetime, $tcx: lifetime>) {
555555

556556
type AllocExtra = ();
557557
type FrameExtra = ();
558-
type Bytes = Box<[u8]>;
558+
type Bytes = Vec<u8>;
559559

560560
#[inline(always)]
561561
fn ignore_optional_overflow_checks(_ecx: &InterpCx<$mir, $tcx, Self>) -> bool {

compiler/rustc_const_eval/src/interpret/memory.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,15 +123,15 @@ pub struct Memory<'mir, 'tcx, M: Machine<'mir, 'tcx>> {
123123
/// A reference to some allocation that was already bounds-checked for the given region
124124
/// and had the on-access machine hooks run.
125125
#[derive(Copy, Clone)]
126-
pub struct AllocRef<'a, 'tcx, Prov: Provenance, Extra, Bytes: AllocBytes = Box<[u8]>> {
126+
pub struct AllocRef<'a, 'tcx, Prov: Provenance, Extra, Bytes: AllocBytes = Vec<u8>> {
127127
alloc: &'a Allocation<Prov, Extra, Bytes>,
128128
range: AllocRange,
129129
tcx: TyCtxt<'tcx>,
130130
alloc_id: AllocId,
131131
}
132132
/// A reference to some allocation that was already bounds-checked for the given region
133133
/// and had the on-access machine hooks run.
134-
pub struct AllocRefMut<'a, 'tcx, Prov: Provenance, Extra, Bytes: AllocBytes = Box<[u8]>> {
134+
pub struct AllocRefMut<'a, 'tcx, Prov: Provenance, Extra, Bytes: AllocBytes = Vec<u8>> {
135135
alloc: &'a mut Allocation<Prov, Extra, Bytes>,
136136
range: AllocRange,
137137
tcx: TyCtxt<'tcx>,
@@ -1157,11 +1157,11 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
11571157
};
11581158

11591159
// Side-step AllocRef and directly access the underlying bytes more efficiently.
1160-
// (We are staying inside the bounds here so all is good.)
1160+
// (We are staying inside the bounds here and all bytes do get overwritten so all is good.)
11611161
let alloc_id = alloc_ref.alloc_id;
11621162
let bytes = alloc_ref
11631163
.alloc
1164-
.get_bytes_mut(&alloc_ref.tcx, alloc_ref.range)
1164+
.get_bytes_unchecked_for_overwrite(&alloc_ref.tcx, alloc_ref.range)
11651165
.map_err(move |e| e.to_interp_error(alloc_id))?;
11661166
// `zip` would stop when the first iterator ends; we want to definitely
11671167
// cover all of `bytes`.
@@ -1182,6 +1182,11 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
11821182
self.mem_copy_repeatedly(src, dest, size, 1, nonoverlapping)
11831183
}
11841184

1185+
/// Performs `num_copies` many copies of `size` many bytes from `src` to `dest + i*size` (where
1186+
/// `i` is the index of the copy).
1187+
///
1188+
/// Either `nonoverlapping` must be true or `num_copies` must be 1; doing repeated copies that
1189+
/// may overlap is not supported.
11851190
pub fn mem_copy_repeatedly(
11861191
&mut self,
11871192
src: Pointer<Option<M::Provenance>>,
@@ -1243,8 +1248,9 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
12431248
(dest_alloc_id, dest_prov),
12441249
dest_range,
12451250
)?;
1251+
// Yes we do overwrite all bytes in `dest_bytes`.
12461252
let dest_bytes = dest_alloc
1247-
.get_bytes_mut_ptr(&tcx, dest_range)
1253+
.get_bytes_unchecked_for_overwrite_ptr(&tcx, dest_range)
12481254
.map_err(|e| e.to_interp_error(dest_alloc_id))?
12491255
.as_mut_ptr();
12501256

@@ -1278,6 +1284,9 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
12781284
}
12791285
}
12801286
}
1287+
if num_copies > 1 {
1288+
assert!(nonoverlapping, "multi-copy only supported in non-overlapping mode");
1289+
}
12811290

12821291
let size_in_bytes = size.bytes_usize();
12831292
// For particularly large arrays (where this is perf-sensitive) it's common that
@@ -1290,6 +1299,8 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
12901299
} else if src_alloc_id == dest_alloc_id {
12911300
let mut dest_ptr = dest_bytes;
12921301
for _ in 0..num_copies {
1302+
// Here we rely on `src` and `dest` being non-overlapping if there is more than
1303+
// one copy.
12931304
ptr::copy(src_bytes, dest_ptr, size_in_bytes);
12941305
dest_ptr = dest_ptr.add(size_in_bytes);
12951306
}

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

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,30 @@ pub trait AllocBytes:
3737
/// Create a zeroed `AllocBytes` of the specified size and alignment.
3838
/// Returns `None` if we ran out of memory on the host.
3939
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;
4047
}
4148

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+
impl AllocBytes for Vec<u8> {
4451
fn from_bytes<'a>(slice: impl Into<Cow<'a, [u8]>>, _align: Align) -> Self {
45-
Box::<[u8]>::from(slice.into())
52+
slice.into().into_owned()
4653
}
4754

4855
fn zeroed(size: Size, _align: Align) -> Option<Self> {
4956
let bytes = Box::<[u8]>::try_new_zeroed_slice(size.bytes_usize()).ok()?;
5057
// SAFETY: the box was zero-allocated, which is a valid initial value for Box<[u8]>
5158
let bytes = unsafe { bytes.assume_init() };
52-
Some(bytes)
59+
Some(bytes.into())
60+
}
61+
62+
fn as_mut_ptr(&mut self) -> *mut u8 {
63+
Vec::as_mut_ptr(self)
5364
}
5465
}
5566

@@ -62,7 +73,7 @@ impl AllocBytes for Box<[u8]> {
6273
// hashed. (see the `Hash` impl below for more details), so the impl is not derived.
6374
#[derive(Clone, Eq, PartialEq, TyEncodable, TyDecodable)]
6475
#[derive(HashStable)]
65-
pub struct Allocation<Prov: Provenance = CtfeProvenance, Extra = (), Bytes = Box<[u8]>> {
76+
pub struct Allocation<Prov: Provenance = CtfeProvenance, Extra = (), Bytes = Vec<u8>> {
6677
/// The actual bytes of the allocation.
6778
/// Note that the bytes of a pointer represent the offset of the pointer.
6879
bytes: Bytes,
@@ -399,10 +410,6 @@ impl<Prov: Provenance, Extra, Bytes: AllocBytes> Allocation<Prov, Extra, Bytes>
399410

400411
/// Byte accessors.
401412
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-
406413
/// This is the entirely abstraction-violating way to just grab the raw bytes without
407414
/// caring about provenance or initialization.
408415
///
@@ -452,13 +459,14 @@ impl<Prov: Provenance, Extra, Bytes: AllocBytes> Allocation<Prov, Extra, Bytes>
452459
Ok(self.get_bytes_unchecked(range))
453460
}
454461

455-
/// Just calling this already marks everything as defined and removes provenance,
456-
/// so be sure to actually put data there!
462+
/// This is the entirely abstraction-violating way to just get mutable access to the raw bytes.
463+
/// Just calling this already marks everything as defined and removes provenance, so be sure to
464+
/// actually overwrite all the data there!
457465
///
458466
/// It is the caller's responsibility to check bounds and alignment beforehand.
459467
/// Most likely, you want to use the `PlaceTy` and `OperandTy`-based methods
460468
/// on `InterpCx` instead.
461-
pub fn get_bytes_mut(
469+
pub fn get_bytes_unchecked_for_overwrite(
462470
&mut self,
463471
cx: &impl HasDataLayout,
464472
range: AllocRange,
@@ -469,8 +477,9 @@ impl<Prov: Provenance, Extra, Bytes: AllocBytes> Allocation<Prov, Extra, Bytes>
469477
Ok(&mut self.bytes[range.start.bytes_usize()..range.end().bytes_usize()])
470478
}
471479

472-
/// A raw pointer variant of `get_bytes_mut` that avoids invalidating existing aliases into this memory.
473-
pub fn get_bytes_mut_ptr(
480+
/// A raw pointer variant of `get_bytes_unchecked_for_overwrite` that avoids invalidating existing immutable aliases
481+
/// into this memory.
482+
pub fn get_bytes_unchecked_for_overwrite_ptr(
474483
&mut self,
475484
cx: &impl HasDataLayout,
476485
range: AllocRange,
@@ -479,10 +488,19 @@ impl<Prov: Provenance, Extra, Bytes: AllocBytes> Allocation<Prov, Extra, Bytes>
479488
self.provenance.clear(range, cx)?;
480489

481490
assert!(range.end().bytes_usize() <= self.bytes.len()); // need to do our own bounds-check
491+
// Cruciall, we go via `AllocBytes::as_mut_ptr`, not `AllocBytes::deref_mut`.
482492
let begin_ptr = self.bytes.as_mut_ptr().wrapping_add(range.start.bytes_usize());
483493
let len = range.end().bytes_usize() - range.start.bytes_usize();
484494
Ok(ptr::slice_from_raw_parts_mut(begin_ptr, len))
485495
}
496+
497+
/// This gives direct mutable access to the entire buffer, just exposing their internal state
498+
/// without reseting anything. Directly exposes `AllocBytes::as_mut_ptr`. Only works if
499+
/// `OFFSET_IS_ADDR` is true.
500+
pub fn get_bytes_unchecked_raw_mut(&mut self) -> *mut u8 {
501+
assert!(Prov::OFFSET_IS_ADDR);
502+
self.bytes.as_mut_ptr()
503+
}
486504
}
487505

488506
/// Reading and writing.
@@ -589,7 +607,8 @@ impl<Prov: Provenance, Extra, Bytes: AllocBytes> Allocation<Prov, Extra, Bytes>
589607
};
590608

591609
let endian = cx.data_layout().endian;
592-
let dst = self.get_bytes_mut(cx, range)?;
610+
// Yes we do overwrite all the bytes in `dst`.
611+
let dst = self.get_bytes_unchecked_for_overwrite(cx, range)?;
593612
write_target_uint(endian, dst, bytes).unwrap();
594613

595614
// See if we have to also store some provenance.

src/tools/miri/src/machine.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -861,7 +861,7 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for MiriMachine<'mir, 'tcx> {
861861

862862
type Provenance = Provenance;
863863
type ProvenanceExtra = ProvenanceExtra;
864-
type Bytes = Box<[u8]>;
864+
type Bytes = Vec<u8>;
865865

866866
type MemoryMap = MonoHashMap<
867867
AllocId,

0 commit comments

Comments
 (0)