Skip to content

Commit b56a147

Browse files
committed
interpret: pass MemoryKind to adjust_alloc_base_pointer
1 parent 468f115 commit b56a147

File tree

4 files changed

+36
-18
lines changed

4 files changed

+36
-18
lines changed

compiler/rustc_const_eval/src/interpret/machine.rs

+14-9
Original file line numberDiff line numberDiff line change
@@ -301,15 +301,6 @@ pub trait Machine<'mir, 'tcx: 'mir>: Sized {
301301
def_id: DefId,
302302
) -> InterpResult<'tcx, Pointer<Self::Provenance>>;
303303

304-
/// Return a "base" pointer for the given allocation: the one that is used for direct
305-
/// accesses to this static/const/fn allocation, or the one returned from the heap allocator.
306-
///
307-
/// Not called on `extern` or thread-local statics (those use the methods above).
308-
fn adjust_alloc_base_pointer(
309-
ecx: &InterpCx<'mir, 'tcx, Self>,
310-
ptr: Pointer,
311-
) -> InterpResult<'tcx, Pointer<Self::Provenance>>;
312-
313304
/// "Int-to-pointer cast"
314305
fn ptr_from_addr_cast(
315306
ecx: &InterpCx<'mir, 'tcx, Self>,
@@ -354,6 +345,19 @@ pub trait Machine<'mir, 'tcx: 'mir>: Sized {
354345
kind: Option<MemoryKind<Self::MemoryKind>>,
355346
) -> InterpResult<'tcx, Cow<'b, Allocation<Self::Provenance, Self::AllocExtra, Self::Bytes>>>;
356347

348+
/// Return a "root" pointer for the given allocation: the one that is used for direct
349+
/// accesses to this static/const/fn allocation, or the one returned from the heap allocator.
350+
///
351+
/// Not called on `extern` or thread-local statics (those use the methods above).
352+
///
353+
/// `kind` is the kind of the allocation the pointer points to; it can be `None` when
354+
/// it's a global and `GLOBAL_KIND` is `None`.
355+
fn adjust_alloc_base_pointer(
356+
ecx: &InterpCx<'mir, 'tcx, Self>,
357+
ptr: Pointer,
358+
kind: Option<MemoryKind<Self::MemoryKind>>,
359+
) -> InterpResult<'tcx, Pointer<Self::Provenance>>;
360+
357361
/// Evaluate the inline assembly.
358362
///
359363
/// This should take care of jumping to the next block (one of `targets`) when asm goto
@@ -604,6 +608,7 @@ pub macro compile_time_machine(<$mir: lifetime, $tcx: lifetime>) {
604608
fn adjust_alloc_base_pointer(
605609
_ecx: &InterpCx<$mir, $tcx, Self>,
606610
ptr: Pointer<CtfeProvenance>,
611+
_kind: Option<MemoryKind<Self::MemoryKind>>,
607612
) -> InterpResult<$tcx, Pointer<CtfeProvenance>> {
608613
Ok(ptr)
609614
}

compiler/rustc_const_eval/src/interpret/memory.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -180,10 +180,16 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
180180
Some(GlobalAlloc::Static(def_id)) if self.tcx.is_foreign_item(def_id) => {
181181
return M::extern_static_base_pointer(self, def_id);
182182
}
183+
None => {
184+
assert!(
185+
self.memory.extra_fn_ptr_map.contains_key(&alloc_id),
186+
"{alloc_id:?} is neither global nor a function pointer"
187+
);
188+
}
183189
_ => {}
184190
}
185191
// And we need to get the provenance.
186-
M::adjust_alloc_base_pointer(self, ptr)
192+
M::adjust_alloc_base_pointer(self, ptr, M::GLOBAL_KIND.map(MemoryKind::Machine))
187193
}
188194

189195
pub fn fn_ptr(&mut self, fn_val: FnVal<'tcx, M::ExtraFnVal>) -> Pointer<M::Provenance> {
@@ -240,7 +246,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
240246
);
241247
let alloc = M::adjust_allocation(self, id, Cow::Owned(alloc), Some(kind))?;
242248
self.memory.alloc_map.insert(id, (kind, alloc.into_owned()));
243-
M::adjust_alloc_base_pointer(self, Pointer::from(id))
249+
M::adjust_alloc_base_pointer(self, Pointer::from(id), Some(kind))
244250
}
245251

246252
pub fn reallocate_ptr(

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

+10-5
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,11 @@ trait EvalContextExtPriv<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
141141
}
142142
}
143143

144-
fn addr_from_alloc_id(&self, alloc_id: AllocId) -> InterpResult<'tcx, u64> {
144+
fn addr_from_alloc_id(
145+
&self,
146+
alloc_id: AllocId,
147+
_kind: MemoryKind,
148+
) -> InterpResult<'tcx, u64> {
145149
let ecx = self.eval_context_ref();
146150
let mut global_state = ecx.machine.alloc_addresses.borrow_mut();
147151
let global_state = &mut *global_state;
@@ -283,16 +287,17 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
283287
}
284288

285289
/// Convert a relative (tcx) pointer to a Miri pointer.
286-
fn ptr_from_rel_ptr(
290+
fn adjust_alloc_base_pointer(
287291
&self,
288292
ptr: Pointer<CtfeProvenance>,
289293
tag: BorTag,
294+
kind: MemoryKind,
290295
) -> InterpResult<'tcx, Pointer<Provenance>> {
291296
let ecx = self.eval_context_ref();
292297

293298
let (prov, offset) = ptr.into_parts(); // offset is relative (AllocId provenance)
294299
let alloc_id = prov.alloc_id();
295-
let base_addr = ecx.addr_from_alloc_id(alloc_id)?;
300+
let base_addr = ecx.addr_from_alloc_id(alloc_id, kind)?;
296301

297302
// Add offset with the right kind of pointer-overflowing arithmetic.
298303
let dl = ecx.data_layout();
@@ -314,9 +319,9 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
314319
ecx.alloc_id_from_addr(addr.bytes())?
315320
};
316321

317-
// This cannot fail: since we already have a pointer with that provenance, rel_ptr_to_addr
322+
// This cannot fail: since we already have a pointer with that provenance, adjust_alloc_base_pointer
318323
// must have been called in the past, so we can just look up the address in the map.
319-
let base_addr = ecx.addr_from_alloc_id(alloc_id).unwrap();
324+
let base_addr = *ecx.machine.alloc_addresses.borrow().base_addr.get(&alloc_id).unwrap();
320325

321326
// Wrapping "addr - base_addr"
322327
#[allow(clippy::cast_possible_wrap)] // we want to wrap here

src/tools/miri/src/machine.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1090,7 +1090,7 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for MiriMachine<'mir, 'tcx> {
10901090
alloc: Cow<'b, Allocation>,
10911091
kind: Option<MemoryKind>,
10921092
) -> InterpResult<'tcx, Cow<'b, Allocation<Self::Provenance, Self::AllocExtra>>> {
1093-
let kind = kind.expect("we set our STATIC_KIND so this cannot be None");
1093+
let kind = kind.expect("we set our GLOBAL_KIND so this cannot be None");
10941094
if ecx.machine.tracked_alloc_ids.contains(&id) {
10951095
ecx.emit_diagnostic(NonHaltingDiagnostic::CreatedAlloc(
10961096
id,
@@ -1151,7 +1151,9 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for MiriMachine<'mir, 'tcx> {
11511151
fn adjust_alloc_base_pointer(
11521152
ecx: &MiriInterpCx<'mir, 'tcx>,
11531153
ptr: Pointer<CtfeProvenance>,
1154+
kind: Option<MemoryKind>,
11541155
) -> InterpResult<'tcx, Pointer<Provenance>> {
1156+
let kind = kind.expect("we set our GLOBAL_KIND so this cannot be None");
11551157
let alloc_id = ptr.provenance.alloc_id();
11561158
if cfg!(debug_assertions) {
11571159
// The machine promises to never call us on thread-local or extern statics.
@@ -1172,7 +1174,7 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for MiriMachine<'mir, 'tcx> {
11721174
// Value does not matter, SB is disabled
11731175
BorTag::default()
11741176
};
1175-
ecx.ptr_from_rel_ptr(ptr, tag)
1177+
ecx.adjust_alloc_base_pointer(ptr, tag, kind)
11761178
}
11771179

11781180
/// Called on `usize as ptr` casts.

0 commit comments

Comments
 (0)