Skip to content

Commit 67f6daf

Browse files
committed
Merge from rustc
2 parents c14ce96 + b1bd24a commit 67f6daf

File tree

5 files changed

+33
-17
lines changed

5 files changed

+33
-17
lines changed

src/intptrcast.rs

+26-9
Original file line numberDiff line numberDiff line change
@@ -162,11 +162,14 @@ impl<'mir, 'tcx> GlobalStateInner {
162162
Ok(Pointer::new(Some(Provenance::Wildcard), Size::from_bytes(addr)))
163163
}
164164

165-
fn alloc_base_addr(ecx: &MiriInterpCx<'mir, 'tcx>, alloc_id: AllocId) -> u64 {
165+
fn alloc_base_addr(
166+
ecx: &MiriInterpCx<'mir, 'tcx>,
167+
alloc_id: AllocId,
168+
) -> InterpResult<'tcx, u64> {
166169
let mut global_state = ecx.machine.intptrcast.borrow_mut();
167170
let global_state = &mut *global_state;
168171

169-
match global_state.base_addr.entry(alloc_id) {
172+
Ok(match global_state.base_addr.entry(alloc_id) {
170173
Entry::Occupied(entry) => *entry.get(),
171174
Entry::Vacant(entry) => {
172175
// There is nothing wrong with a raw pointer being cast to an integer only after
@@ -181,7 +184,10 @@ impl<'mir, 'tcx> GlobalStateInner {
181184
rng.gen_range(0..16)
182185
};
183186
// From next_base_addr + slack, round up to adjust for alignment.
184-
let base_addr = global_state.next_base_addr.checked_add(slack).unwrap();
187+
let base_addr = global_state
188+
.next_base_addr
189+
.checked_add(slack)
190+
.ok_or_else(|| err_exhaust!(AddressSpaceFull))?;
185191
let base_addr = Self::align_addr(base_addr, align.bytes());
186192
entry.insert(base_addr);
187193
trace!(
@@ -197,24 +203,33 @@ impl<'mir, 'tcx> GlobalStateInner {
197203
// of at least 1 to avoid two allocations having the same base address.
198204
// (The logic in `alloc_id_from_addr` assumes unique addresses, and different
199205
// function/vtable pointers need to be distinguishable!)
200-
global_state.next_base_addr = base_addr.checked_add(max(size.bytes(), 1)).unwrap();
206+
global_state.next_base_addr = base_addr
207+
.checked_add(max(size.bytes(), 1))
208+
.ok_or_else(|| err_exhaust!(AddressSpaceFull))?;
209+
// Even if `Size` didn't overflow, we might still have filled up the address space.
210+
if global_state.next_base_addr > ecx.machine_usize_max() {
211+
throw_exhaust!(AddressSpaceFull);
212+
}
201213
// Given that `next_base_addr` increases in each allocation, pushing the
202214
// corresponding tuple keeps `int_to_ptr_map` sorted
203215
global_state.int_to_ptr_map.push((base_addr, alloc_id));
204216

205217
base_addr
206218
}
207-
}
219+
})
208220
}
209221

210222
/// Convert a relative (tcx) pointer to an absolute address.
211-
pub fn rel_ptr_to_addr(ecx: &MiriInterpCx<'mir, 'tcx>, ptr: Pointer<AllocId>) -> u64 {
223+
pub fn rel_ptr_to_addr(
224+
ecx: &MiriInterpCx<'mir, 'tcx>,
225+
ptr: Pointer<AllocId>,
226+
) -> InterpResult<'tcx, u64> {
212227
let (alloc_id, offset) = ptr.into_parts(); // offset is relative (AllocId provenance)
213-
let base_addr = GlobalStateInner::alloc_base_addr(ecx, alloc_id);
228+
let base_addr = GlobalStateInner::alloc_base_addr(ecx, alloc_id)?;
214229

215230
// Add offset with the right kind of pointer-overflowing arithmetic.
216231
let dl = ecx.data_layout();
217-
dl.overflowing_offset(base_addr, offset.bytes()).0
232+
Ok(dl.overflowing_offset(base_addr, offset.bytes()).0)
218233
}
219234

220235
/// When a pointer is used for a memory access, this computes where in which allocation the
@@ -232,7 +247,9 @@ impl<'mir, 'tcx> GlobalStateInner {
232247
GlobalStateInner::alloc_id_from_addr(ecx, addr.bytes())?
233248
};
234249

235-
let base_addr = GlobalStateInner::alloc_base_addr(ecx, alloc_id);
250+
// This cannot fail: since we already have a pointer with that provenance, rel_ptr_to_addr
251+
// must have been called in the past.
252+
let base_addr = GlobalStateInner::alloc_base_addr(ecx, alloc_id).unwrap();
236253

237254
// Wrapping "addr - base_addr"
238255
let dl = ecx.data_layout();

src/machine.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -971,7 +971,7 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for MiriMachine<'mir, 'tcx> {
971971
fn adjust_alloc_base_pointer(
972972
ecx: &MiriInterpCx<'mir, 'tcx>,
973973
ptr: Pointer<AllocId>,
974-
) -> Pointer<Provenance> {
974+
) -> InterpResult<'tcx, Pointer<Provenance>> {
975975
if cfg!(debug_assertions) {
976976
// The machine promises to never call us on thread-local or extern statics.
977977
let alloc_id = ptr.provenance;
@@ -985,17 +985,17 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for MiriMachine<'mir, 'tcx> {
985985
_ => {}
986986
}
987987
}
988-
let absolute_addr = intptrcast::GlobalStateInner::rel_ptr_to_addr(ecx, ptr);
988+
let absolute_addr = intptrcast::GlobalStateInner::rel_ptr_to_addr(ecx, ptr)?;
989989
let tag = if let Some(borrow_tracker) = &ecx.machine.borrow_tracker {
990990
borrow_tracker.borrow_mut().base_ptr_tag(ptr.provenance, &ecx.machine)
991991
} else {
992992
// Value does not matter, SB is disabled
993993
BorTag::default()
994994
};
995-
Pointer::new(
995+
Ok(Pointer::new(
996996
Provenance::Concrete { alloc_id: ptr.provenance, tag },
997997
Size::from_bytes(absolute_addr),
998-
)
998+
))
999999
}
10001000

10011001
#[inline(always)]

src/shims/backtrace.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,9 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
190190
0 => {
191191
// These are "mutable" allocations as we consider them to be owned by the callee.
192192
let name_alloc =
193-
this.allocate_str(&name, MiriMemoryKind::Rust.into(), Mutability::Mut);
193+
this.allocate_str(&name, MiriMemoryKind::Rust.into(), Mutability::Mut)?;
194194
let filename_alloc =
195-
this.allocate_str(&filename, MiriMemoryKind::Rust.into(), Mutability::Mut);
195+
this.allocate_str(&filename, MiriMemoryKind::Rust.into(), Mutability::Mut)?;
196196

197197
this.write_immediate(
198198
name_alloc.to_ref(this),

src/shims/panic.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
172172
let this = self.eval_context_mut();
173173

174174
// First arg: message.
175-
let msg = this.allocate_str(msg, MiriMemoryKind::Machine.into(), Mutability::Not);
175+
let msg = this.allocate_str(msg, MiriMemoryKind::Machine.into(), Mutability::Not)?;
176176

177177
// Call the lang item.
178178
let panic = this.tcx.lang_items().panic_fn().unwrap();

tests/pass-dep/shims/pthreads.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
//@ignore-target-windows: No libc on Windows
2-
#![feature(cstr_from_bytes_until_nul)]
32
use std::ffi::{CStr, CString};
43
use std::thread;
54

0 commit comments

Comments
 (0)