Skip to content

Commit fc99b4c

Browse files
committed
avoid unwrap in Miri's adjust_global_allocation
1 parent 95cb78f commit fc99b4c

File tree

4 files changed

+6
-8
lines changed

4 files changed

+6
-8
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -358,11 +358,11 @@ impl Allocation {
358358
pub fn adjust_from_tcx<Prov: Provenance, Bytes: AllocBytes, Err>(
359359
&self,
360360
cx: &impl HasDataLayout,
361-
mut alloc_bytes: impl FnMut(&[u8], Align) -> Bytes,
361+
mut alloc_bytes: impl FnMut(&[u8], Align) -> Result<Bytes, Err>,
362362
mut adjust_ptr: impl FnMut(Pointer<CtfeProvenance>) -> Result<Pointer<Prov>, Err>,
363363
) -> Result<Allocation<Prov, (), Bytes>, Err> {
364364
// Copy the data.
365-
let mut bytes = alloc_bytes(&*self.bytes, self.align);
365+
let mut bytes = alloc_bytes(&*self.bytes, self.align)?;
366366
// Adjust provenance of pointers stored in this allocation.
367367
let mut new_provenance = Vec::with_capacity(self.provenance.ptrs().len());
368368
let ptr_size = cx.data_layout().pointer_size.bytes_usize();

src/tools/miri/src/alloc_addresses/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,12 +357,11 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
357357
let ecx = self.eval_context_ref();
358358
// This additional call ensures that some `MiriAllocBytes` are prepared.
359359
ecx.addr_from_alloc_id(id, kind)?;
360-
// TODO: Better debug message?
361360
let mut global_state = ecx.machine.alloc_addresses.borrow_mut();
362361
let prepared_alloc_bytes = global_state
363362
.prepared_alloc_bytes
364363
.remove(&id)
365-
.expect("alloc bytes for {id:?} has not been prepared");
364+
.unwrap_or_else(|| panic!("alloc bytes for {id:?} has not been prepared"));
366365
Ok(prepared_alloc_bytes)
367366
}
368367

src/tools/miri/src/concurrency/thread.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -887,7 +887,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
887887
let alloc = this.ctfe_query(|tcx| tcx.eval_static_initializer(def_id))?;
888888
// We make a full copy of this allocation.
889889
let mut alloc =
890-
alloc.inner().adjust_from_tcx(&this.tcx, |bytes, align| MiriAllocBytes::from_bytes(std::borrow::Cow::Borrowed(bytes), align), |ptr| this.global_root_pointer(ptr))?;
890+
alloc.inner().adjust_from_tcx(&this.tcx, |bytes, align| Ok(MiriAllocBytes::from_bytes(std::borrow::Cow::Borrowed(bytes), align)), |ptr| this.global_root_pointer(ptr))?;
891891
// This allocation will be deallocated when the thread dies, so it is not in read-only memory.
892892
alloc.mutability = Mutability::Mut;
893893
// Create a fresh allocation with this content.

src/tools/miri/src/machine.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1249,12 +1249,11 @@ impl<'tcx> Machine<'tcx> for MiriMachine<'tcx> {
12491249
let alloc = alloc.adjust_from_tcx(&ecx.tcx, |bytes, align| {
12501250
assert_eq!(alloc.align, align);
12511251
let kind = Self::GLOBAL_KIND.unwrap().into();
1252-
// TODO: is this unwrap warranted?
1253-
let mut alloc_bytes = ecx.take_prepared_alloc_bytes(id, kind).unwrap();
1252+
let mut alloc_bytes = ecx.take_prepared_alloc_bytes(id, kind)?;
12541253
assert_eq!(alloc_bytes.len(), bytes.len());
12551254
// SAFETY: `alloc_bytes` and `bytes` span the same number of bytes.
12561255
unsafe { alloc_bytes.as_mut_ptr().copy_from(bytes.as_ptr(), bytes.len()) };
1257-
alloc_bytes
1256+
Ok(alloc_bytes)
12581257
},
12591258
|ptr| ecx.global_root_pointer(ptr),
12601259
)?;

0 commit comments

Comments
 (0)