Skip to content

Commit fb38aea

Browse files
committed
Auto merge of #2782 - RalfJung:rustup, r=RalfJung
Rustup
2 parents 04a5e18 + 2d8addb commit fb38aea

File tree

6 files changed

+45
-23
lines changed

6 files changed

+45
-23
lines changed

rust-version

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
dffea43fc1102bdfe16d88ed412c23d4f0f08d9d
1+
e7acd078f443156b95cee11759a735db1cfc796e

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

+15-9
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,8 @@ pub struct MiriMachine<'mir, 'tcx> {
477477

478478
impl<'mir, 'tcx> MiriMachine<'mir, 'tcx> {
479479
pub(crate) fn new(config: &MiriConfig, layout_cx: LayoutCx<'tcx, TyCtxt<'tcx>>) -> Self {
480-
let local_crates = helpers::get_local_crates(layout_cx.tcx);
480+
let tcx = layout_cx.tcx;
481+
let local_crates = helpers::get_local_crates(tcx);
481482
let layouts =
482483
PrimitiveLayouts::new(layout_cx).expect("Couldn't get layouts of primitive types");
483484
let profiler = config.measureme_out.as_ref().map(|out| {
@@ -486,10 +487,13 @@ impl<'mir, 'tcx> MiriMachine<'mir, 'tcx> {
486487
let rng = StdRng::seed_from_u64(config.seed.unwrap_or(0));
487488
let borrow_tracker = config.borrow_tracker.map(|bt| bt.instanciate_global_state(config));
488489
let data_race = config.data_race_detector.then(|| data_race::GlobalState::new(config));
490+
// Determinine page size, stack address, and stack size.
491+
// These values are mostly meaningless, but the stack address is also where we start
492+
// allocating physical integer addresses for all allocations.
489493
let page_size = if let Some(page_size) = config.page_size {
490494
page_size
491495
} else {
492-
let target = &layout_cx.tcx.sess.target;
496+
let target = &tcx.sess.target;
493497
match target.arch.as_ref() {
494498
"wasm32" | "wasm64" => 64 * 1024, // https://webassembly.github.io/spec/core/exec/runtime.html#memory-instances
495499
"aarch64" =>
@@ -504,10 +508,12 @@ impl<'mir, 'tcx> MiriMachine<'mir, 'tcx> {
504508
_ => 4 * 1024,
505509
}
506510
};
507-
let stack_addr = page_size * 32;
508-
let stack_size = page_size * 16;
511+
// On 16bit targets, 32 pages is more than the entire address space!
512+
let stack_addr = if tcx.pointer_size().bits() < 32 { page_size } else { page_size * 32 };
513+
let stack_size =
514+
if tcx.pointer_size().bits() < 32 { page_size * 4 } else { page_size * 16 };
509515
MiriMachine {
510-
tcx: layout_cx.tcx,
516+
tcx,
511517
borrow_tracker,
512518
data_race,
513519
intptrcast: RefCell::new(intptrcast::GlobalStateInner::new(config, stack_addr)),
@@ -971,7 +977,7 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for MiriMachine<'mir, 'tcx> {
971977
fn adjust_alloc_base_pointer(
972978
ecx: &MiriInterpCx<'mir, 'tcx>,
973979
ptr: Pointer<AllocId>,
974-
) -> Pointer<Provenance> {
980+
) -> InterpResult<'tcx, Pointer<Provenance>> {
975981
if cfg!(debug_assertions) {
976982
// The machine promises to never call us on thread-local or extern statics.
977983
let alloc_id = ptr.provenance;
@@ -985,17 +991,17 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for MiriMachine<'mir, 'tcx> {
985991
_ => {}
986992
}
987993
}
988-
let absolute_addr = intptrcast::GlobalStateInner::rel_ptr_to_addr(ecx, ptr);
994+
let absolute_addr = intptrcast::GlobalStateInner::rel_ptr_to_addr(ecx, ptr)?;
989995
let tag = if let Some(borrow_tracker) = &ecx.machine.borrow_tracker {
990996
borrow_tracker.borrow_mut().base_ptr_tag(ptr.provenance, &ecx.machine)
991997
} else {
992998
// Value does not matter, SB is disabled
993999
BorTag::default()
9941000
};
995-
Pointer::new(
1001+
Ok(Pointer::new(
9961002
Provenance::Concrete { alloc_id: ptr.provenance, tag },
9971003
Size::from_bytes(absolute_addr),
998-
)
1004+
))
9991005
}
10001006

10011007
#[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)