Skip to content

Commit c9e568f

Browse files
committed
avoid pairing up AllocId and PointerTag, which is redundant
1 parent 54ab357 commit c9e568f

File tree

3 files changed

+43
-23
lines changed

3 files changed

+43
-23
lines changed

compiler/rustc_const_eval/src/interpret/machine.rs

+19-7
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ pub trait Machine<'mir, 'tcx>: Sized {
8888
/// Pointers are "tagged" with provenance information; typically the `AllocId` they belong to.
8989
type PointerTag: Provenance + Eq + Hash + 'static;
9090

91+
/// When getting the AllocId of a pointer, some extra data is also obtained from the tag
92+
/// that is passed to memory access hooks so they can do things with it.
93+
type TagExtra: Copy + 'static;
94+
9195
/// Machines can define extra (non-instance) things that represent values of function pointers.
9296
/// For example, Miri uses this to return a function pointer from `dlsym`
9397
/// that can later be called to execute the right thing.
@@ -285,11 +289,14 @@ pub trait Machine<'mir, 'tcx>: Sized {
285289
addr: u64,
286290
) -> Pointer<Option<Self::PointerTag>>;
287291

288-
/// Convert a pointer with provenance into an allocation-offset pair.
292+
/// Convert a pointer with provenance into an allocation-offset pair
293+
/// and extra provenance info.
294+
///
295+
/// The returned `AllocId` must be the same as `ptr.provenance.get_alloc_id()`.
289296
fn ptr_get_alloc(
290297
ecx: &InterpCx<'mir, 'tcx, Self>,
291298
ptr: Pointer<Self::PointerTag>,
292-
) -> (AllocId, Size);
299+
) -> (AllocId, Size, Self::TagExtra);
293300

294301
/// Called to initialize the "extra" state of an allocation and make the pointers
295302
/// it contains (in relocations) tagged. The way we construct allocations is
@@ -321,7 +328,7 @@ pub trait Machine<'mir, 'tcx>: Sized {
321328
_tcx: TyCtxt<'tcx>,
322329
_machine: &Self,
323330
_alloc_extra: &Self::AllocExtra,
324-
_tag: Self::PointerTag,
331+
_tag: (AllocId, Self::TagExtra),
325332
_range: AllocRange,
326333
) -> InterpResult<'tcx> {
327334
Ok(())
@@ -333,7 +340,7 @@ pub trait Machine<'mir, 'tcx>: Sized {
333340
_tcx: TyCtxt<'tcx>,
334341
_machine: &mut Self,
335342
_alloc_extra: &mut Self::AllocExtra,
336-
_tag: Self::PointerTag,
343+
_tag: (AllocId, Self::TagExtra),
337344
_range: AllocRange,
338345
) -> InterpResult<'tcx> {
339346
Ok(())
@@ -345,7 +352,7 @@ pub trait Machine<'mir, 'tcx>: Sized {
345352
_tcx: TyCtxt<'tcx>,
346353
_machine: &mut Self,
347354
_alloc_extra: &mut Self::AllocExtra,
348-
_tag: Self::PointerTag,
355+
_tag: (AllocId, Self::TagExtra),
349356
_range: AllocRange,
350357
) -> InterpResult<'tcx> {
351358
Ok(())
@@ -397,6 +404,8 @@ pub trait Machine<'mir, 'tcx>: Sized {
397404
// (CTFE and ConstProp) use the same instance. Here, we share that code.
398405
pub macro compile_time_machine(<$mir: lifetime, $tcx: lifetime>) {
399406
type PointerTag = AllocId;
407+
type TagExtra = ();
408+
400409
type ExtraFnVal = !;
401410

402411
type MemoryMap =
@@ -474,9 +483,12 @@ pub macro compile_time_machine(<$mir: lifetime, $tcx: lifetime>) {
474483
}
475484

476485
#[inline(always)]
477-
fn ptr_get_alloc(_ecx: &InterpCx<$mir, $tcx, Self>, ptr: Pointer<AllocId>) -> (AllocId, Size) {
486+
fn ptr_get_alloc(
487+
_ecx: &InterpCx<$mir, $tcx, Self>,
488+
ptr: Pointer<AllocId>,
489+
) -> (AllocId, Size, Self::TagExtra) {
478490
// We know `offset` is relative to the allocation, so we can use `into_parts`.
479491
let (alloc_id, offset) = ptr.into_parts();
480-
(alloc_id, offset)
492+
(alloc_id, offset, ())
481493
}
482494
}

compiler/rustc_const_eval/src/interpret/memory.rs

+21-16
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
330330
*self.tcx,
331331
&mut self.machine,
332332
&mut alloc.extra,
333-
tag,
333+
(alloc_id, tag),
334334
alloc_range(Size::ZERO, size),
335335
)?;
336336

@@ -350,7 +350,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
350350
ptr: Pointer<Option<M::PointerTag>>,
351351
size: Size,
352352
align: Align,
353-
) -> InterpResult<'tcx, Option<(AllocId, Size, M::PointerTag)>> {
353+
) -> InterpResult<'tcx, Option<(AllocId, Size, M::TagExtra)>> {
354354
let align = M::enforce_alignment(&self).then_some(align);
355355
self.check_and_deref_ptr(
356356
ptr,
@@ -401,11 +401,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
401401
size: Size,
402402
align: Option<Align>,
403403
msg: CheckInAllocMsg,
404-
alloc_size: impl FnOnce(
405-
AllocId,
406-
Size,
407-
M::PointerTag,
408-
) -> InterpResult<'tcx, (Size, Align, T)>,
404+
alloc_size: impl FnOnce(AllocId, Size, M::TagExtra) -> InterpResult<'tcx, (Size, Align, T)>,
409405
) -> InterpResult<'tcx, Option<T>> {
410406
fn check_offset_align(offset: u64, align: Align) -> InterpResult<'static> {
411407
if offset % align.bytes() == 0 {
@@ -450,7 +446,10 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
450446
// we want the error to be about the bounds.
451447
if let Some(align) = align {
452448
if M::force_int_for_alignment_check(self) {
453-
assert!(M::PointerTag::OFFSET_IS_ADDR, "ptr-to-int cast for align check should never fail");
449+
assert!(
450+
M::PointerTag::OFFSET_IS_ADDR,
451+
"ptr-to-int cast for align check should never fail"
452+
);
454453
let (_, addr) = ptr.into_parts(); // we checked that offset is absolute
455454
check_offset_align(addr.bytes(), align)?;
456455
} else {
@@ -575,7 +574,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
575574
)?;
576575
if let Some((alloc_id, offset, tag, alloc)) = ptr_and_alloc {
577576
let range = alloc_range(offset, size);
578-
M::memory_read(*self.tcx, &self.machine, &alloc.extra, tag, range)?;
577+
M::memory_read(*self.tcx, &self.machine, &alloc.extra, (alloc_id, tag), range)?;
579578
Ok(Some(AllocRef { alloc, range, tcx: *self.tcx, alloc_id }))
580579
} else {
581580
// Even in this branch we have to be sure that we actually access the allocation, in
@@ -636,7 +635,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
636635
// We cannot call `get_raw_mut` inside `check_and_deref_ptr` as that would duplicate `&mut self`.
637636
let (alloc, machine) = self.get_alloc_raw_mut(alloc_id)?;
638637
let range = alloc_range(offset, size);
639-
M::memory_written(tcx, machine, &mut alloc.extra, tag, range)?;
638+
M::memory_written(tcx, machine, &mut alloc.extra, (alloc_id, tag), range)?;
640639
Ok(Some(AllocRefMut { alloc, range, tcx, alloc_id }))
641640
} else {
642641
Ok(None)
@@ -1014,7 +1013,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
10141013
};
10151014
let src_alloc = self.get_alloc_raw(src_alloc_id)?;
10161015
let src_range = alloc_range(src_offset, size);
1017-
M::memory_read(*tcx, &self.machine, &src_alloc.extra, src_tag, src_range)?;
1016+
M::memory_read(*tcx, &self.machine, &src_alloc.extra, (src_alloc_id, src_tag), src_range)?;
10181017
// We need the `dest` ptr for the next operation, so we get it now.
10191018
// We already did the source checks and called the hooks so we are good to return early.
10201019
let Some((dest_alloc_id, dest_offset, dest_tag)) = dest_parts else {
@@ -1039,7 +1038,13 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
10391038
// Destination alloc preparations and access hooks.
10401039
let (dest_alloc, extra) = self.get_alloc_raw_mut(dest_alloc_id)?;
10411040
let dest_range = alloc_range(dest_offset, size * num_copies);
1042-
M::memory_written(*tcx, extra, &mut dest_alloc.extra, dest_tag, dest_range)?;
1041+
M::memory_written(
1042+
*tcx,
1043+
extra,
1044+
&mut dest_alloc.extra,
1045+
(dest_alloc_id, dest_tag),
1046+
dest_range,
1047+
)?;
10431048
let dest_bytes = dest_alloc
10441049
.get_bytes_mut_ptr(&tcx, dest_range)
10451050
.map_err(|e| e.to_interp_error(dest_alloc_id))?
@@ -1158,11 +1163,11 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
11581163
pub fn ptr_try_get_alloc_id(
11591164
&self,
11601165
ptr: Pointer<Option<M::PointerTag>>,
1161-
) -> Result<(AllocId, Size, M::PointerTag), u64> {
1166+
) -> Result<(AllocId, Size, M::TagExtra), u64> {
11621167
match ptr.into_pointer_or_addr() {
11631168
Ok(ptr) => {
1164-
let (alloc_id, offset) = M::ptr_get_alloc(self, ptr);
1165-
Ok((alloc_id, offset, ptr.provenance))
1169+
let (alloc_id, offset, extra) = M::ptr_get_alloc(self, ptr);
1170+
Ok((alloc_id, offset, extra))
11661171
}
11671172
Err(addr) => Err(addr.bytes()),
11681173
}
@@ -1173,7 +1178,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
11731178
pub fn ptr_get_alloc_id(
11741179
&self,
11751180
ptr: Pointer<Option<M::PointerTag>>,
1176-
) -> InterpResult<'tcx, (AllocId, Size, M::PointerTag)> {
1181+
) -> InterpResult<'tcx, (AllocId, Size, M::TagExtra)> {
11771182
self.ptr_try_get_alloc_id(ptr).map_err(|offset| {
11781183
err_ub!(DanglingIntPointer(offset, CheckInAllocMsg::InboundsTest)).into()
11791184
})

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

+3
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,9 @@ pub struct Pointer<Tag = AllocId> {
163163
}
164164

165165
static_assert_size!(Pointer, 16);
166+
// `Option<Tag>` pointers are also passed around quite a bit
167+
// (but not stored in permanent machine state).
168+
static_assert_size!(Pointer<Option<AllocId>>, 16);
166169

167170
// We want the `Debug` output to be readable as it is used by `derive(Debug)` for
168171
// all the Miri types.

0 commit comments

Comments
 (0)