Skip to content

Commit fb3e79d

Browse files
committed
interpret/allocation: rename mutating functions to be more scary; add a new raw bytes getter
1 parent ee03c28 commit fb3e79d

File tree

2 files changed

+47
-13
lines changed

2 files changed

+47
-13
lines changed

compiler/rustc_const_eval/src/interpret/memory.rs

+14-3
Original file line numberDiff line numberDiff line change
@@ -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

+33-10
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ 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. Must be aligned according to the `align`
42+
/// passed during construction.
43+
///
44+
/// Crucially this pointer will *not* be invalidated by `deref`/`deref_mut` calls!
45+
/// In Tree Borrows terms, this pointer is a parent of the references returned there.
46+
fn get_bytes_unchecked_raw_mut(&mut self) -> *mut u8;
4047
}
4148

4249
// Default `bytes` for `Allocation` is a `Box<[u8]>`.
@@ -51,6 +58,13 @@ impl AllocBytes for Box<[u8]> {
5158
let bytes = unsafe { bytes.assume_init() };
5259
Some(bytes)
5360
}
61+
62+
fn get_bytes_unchecked_raw_mut(&mut self) -> *mut u8 {
63+
// We didn't actually put the memory at the right alignment, so we can't support this.
64+
// (That's okay, this function is only needed in Miri which implements this trait for its
65+
// own type.)
66+
panic!("raw bytes access not supported");
67+
}
5468
}
5569

5670
/// This type represents an Allocation in the Miri/CTFE core engine.
@@ -399,10 +413,6 @@ impl<Prov: Provenance, Extra, Bytes: AllocBytes> Allocation<Prov, Extra, Bytes>
399413

400414
/// Byte accessors.
401415
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-
406416
/// This is the entirely abstraction-violating way to just grab the raw bytes without
407417
/// caring about provenance or initialization.
408418
///
@@ -452,13 +462,14 @@ impl<Prov: Provenance, Extra, Bytes: AllocBytes> Allocation<Prov, Extra, Bytes>
452462
Ok(self.get_bytes_unchecked(range))
453463
}
454464

455-
/// Just calling this already marks everything as defined and removes provenance,
456-
/// so be sure to actually put data there!
465+
/// This is the entirely abstraction-violating way to just get mutable access to the raw bytes.
466+
/// Just calling this already marks everything as defined and removes provenance, so be sure to
467+
/// actually overwrite all the data there!
457468
///
458469
/// It is the caller's responsibility to check bounds and alignment beforehand.
459470
/// Most likely, you want to use the `PlaceTy` and `OperandTy`-based methods
460471
/// on `InterpCx` instead.
461-
pub fn get_bytes_mut(
472+
pub fn get_bytes_unchecked_for_overwrite(
462473
&mut self,
463474
cx: &impl HasDataLayout,
464475
range: AllocRange,
@@ -469,8 +480,9 @@ impl<Prov: Provenance, Extra, Bytes: AllocBytes> Allocation<Prov, Extra, Bytes>
469480
Ok(&mut self.bytes[range.start.bytes_usize()..range.end().bytes_usize()])
470481
}
471482

472-
/// A raw pointer variant of `get_bytes_mut` that avoids invalidating existing aliases into this memory.
473-
pub fn get_bytes_mut_ptr(
483+
/// A raw pointer variant of `get_bytes_unchecked_for_overwrite` that avoids invalidating existing immutable aliases
484+
/// into this memory.
485+
pub fn get_bytes_unchecked_for_overwrite_ptr(
474486
&mut self,
475487
cx: &impl HasDataLayout,
476488
range: AllocRange,
@@ -479,10 +491,20 @@ impl<Prov: Provenance, Extra, Bytes: AllocBytes> Allocation<Prov, Extra, Bytes>
479491
self.provenance.clear(range, cx)?;
480492

481493
assert!(range.end().bytes_usize() <= self.bytes.len()); // need to do our own bounds-check
494+
// FIXME: actually now that `self.bytes` is a `Box`, this *does* invalidate existing
495+
// immutable aliases at least under Stacked Borrows...
482496
let begin_ptr = self.bytes.as_mut_ptr().wrapping_add(range.start.bytes_usize());
483497
let len = range.end().bytes_usize() - range.start.bytes_usize();
484498
Ok(ptr::slice_from_raw_parts_mut(begin_ptr, len))
485499
}
500+
501+
/// This gives direct mutable access to the entire buffer, just exposing their internal state
502+
/// without reseting anything. Directly exposes the `AllocBytes` method of the same name. Only
503+
/// works if `OFFSET_IS_ADDR` is true.
504+
pub fn get_bytes_unchecked_raw_mut(&mut self) -> *mut u8 {
505+
assert!(Prov::OFFSET_IS_ADDR);
506+
self.bytes.get_bytes_unchecked_raw_mut()
507+
}
486508
}
487509

488510
/// Reading and writing.
@@ -589,7 +611,8 @@ impl<Prov: Provenance, Extra, Bytes: AllocBytes> Allocation<Prov, Extra, Bytes>
589611
};
590612

591613
let endian = cx.data_layout().endian;
592-
let dst = self.get_bytes_mut(cx, range)?;
614+
// Yes we do overwrite all the bytes in `dst`.
615+
let dst = self.get_bytes_unchecked_for_overwrite(cx, range)?;
593616
write_target_uint(endian, dst, bytes).unwrap();
594617

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

0 commit comments

Comments
 (0)