Skip to content

Commit 5e51a15

Browse files
committed
fix init_allocation_extra
1 parent 9a52543 commit 5e51a15

File tree

5 files changed

+30
-20
lines changed

5 files changed

+30
-20
lines changed

src/librustc_mir/const_eval.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -462,9 +462,9 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
462462
_id: AllocId,
463463
alloc: Cow<'b, Allocation>,
464464
_kind: Option<MemoryKind<!>>,
465-
) -> Cow<'b, Allocation<Self::PointerTag>> {
465+
) -> (Cow<'b, Allocation<Self::PointerTag>>, Self::PointerTag) {
466466
// We do not use a tag so we can just cheaply forward the allocation
467-
alloc
467+
(alloc, ())
468468
}
469469

470470
#[inline(always)]

src/librustc_mir/interpret/eval_context.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -251,9 +251,11 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
251251
}
252252

253253
/// Call this to turn untagged "global" pointers (obtained via `tcx`) into
254-
/// the *canonical* machine pointer to the allocation. This represents a *direct*
255-
/// access to that memory, as opposed to access through a pointer that was created
256-
/// by the program. Must never be used for derived (program-created) pointers!
254+
/// the *canonical* machine pointer to the allocation. Must never be used
255+
/// for any other pointers!
256+
///
257+
/// This represents a *direct* access to that memory, as opposed to access
258+
/// through a pointer that was created by the program.
257259
#[inline(always)]
258260
pub fn tag_static_base_pointer(&self, ptr: Pointer) -> Pointer<M::PointerTag> {
259261
self.memory.tag_static_base_pointer(ptr)

src/librustc_mir/interpret/machine.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -240,18 +240,20 @@ pub trait Machine<'mir, 'tcx>: Sized {
240240
/// allocation (because a copy had to be done to add tags or metadata), machine memory will
241241
/// cache the result. (This relies on `AllocMap::get_or` being able to add the
242242
/// owned allocation to the map even when the map is shared.)
243+
///
244+
/// Also return the "base" tag to use for this allocation: the one that is used for direct
245+
/// accesses to this allocation. If `kind == STATIC_KIND`, this tag must be consistent
246+
/// with `tag_static_base_pointer`.
243247
fn init_allocation_extra<'b>(
244248
memory_extra: &Self::MemoryExtra,
245249
id: AllocId,
246250
alloc: Cow<'b, Allocation>,
247251
kind: Option<MemoryKind<Self::MemoryKinds>>,
248-
) -> Cow<'b, Allocation<Self::PointerTag, Self::AllocExtra>>;
252+
) -> (Cow<'b, Allocation<Self::PointerTag, Self::AllocExtra>>, Self::PointerTag);
249253

250-
/// Return the "base" tag for the given static allocation: the one that is used for direct
251-
/// accesses to this static/const/fn allocation.
252-
///
253-
/// Be aware that requesting the `Allocation` for that `id` will lead to cycles
254-
/// for cyclic statics!
254+
/// Return the "base" tag for the given *static* allocation: the one that is used for direct
255+
/// accesses to this static/const/fn allocation. If `id` is not a static allocation,
256+
/// this will return an unusable tag (i.e., accesses will be UB)!
255257
fn tag_static_base_pointer(
256258
memory_extra: &Self::MemoryExtra,
257259
id: AllocId,

src/librustc_mir/interpret/memory.rs

+13-7
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,11 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
144144
}
145145

146146
/// Call this to turn untagged "global" pointers (obtained via `tcx`) into
147-
/// the *canonical* machine pointer to the allocation. This represents a *direct*
148-
/// access to that memory, as opposed to access through a pointer that was created
149-
/// by the program. Must never be used for derived (program-created) pointers!
147+
/// the *canonical* machine pointer to the allocation. Must never be used
148+
/// for any other pointers!
149+
///
150+
/// This represents a *direct* access to that memory, as opposed to access
151+
/// through a pointer that was created by the program.
150152
#[inline]
151153
pub fn tag_static_base_pointer(&self, ptr: Pointer) -> Pointer<M::PointerTag> {
152154
ptr.with_tag(M::tag_static_base_pointer(&self.extra, ptr.alloc_id))
@@ -195,9 +197,11 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
195197
kind: MemoryKind<M::MemoryKinds>,
196198
) -> Pointer<M::PointerTag> {
197199
let id = self.tcx.alloc_map.lock().reserve();
198-
let alloc = M::init_allocation_extra(&self.extra, id, Cow::Owned(alloc), Some(kind));
200+
debug_assert_ne!(Some(kind), M::STATIC_KIND.map(MemoryKind::Machine),
201+
"dynamically allocating static memory");
202+
let (alloc, tag) = M::init_allocation_extra(&self.extra, id, Cow::Owned(alloc), Some(kind));
199203
self.alloc_map.insert(id, (kind, alloc.into_owned()));
200-
self.tag_static_base_pointer(Pointer::from(id))
204+
Pointer::from(id).with_tag(tag)
201205
}
202206

203207
pub fn reallocate(
@@ -478,12 +482,14 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
478482
}
479483
};
480484
// We got tcx memory. Let the machine initialize its "extra" stuff.
481-
Ok(M::init_allocation_extra(
485+
let (alloc, tag) = M::init_allocation_extra(
482486
memory_extra,
483487
id, // always use the ID we got as input, not the "hidden" one.
484488
alloc,
485489
M::STATIC_KIND.map(MemoryKind::Machine),
486-
))
490+
);
491+
debug_assert_eq!(tag, M::tag_static_base_pointer(memory_extra, id));
492+
Ok(alloc)
487493
}
488494

489495
/// Gives raw access to the `Allocation`, without bounds or alignment checks.

src/librustc_mir/transform/const_prop.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,9 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for ConstPropMachine {
196196
_id: AllocId,
197197
alloc: Cow<'b, Allocation>,
198198
_kind: Option<MemoryKind<!>>,
199-
) -> Cow<'b, Allocation<Self::PointerTag>> {
199+
) -> (Cow<'b, Allocation<Self::PointerTag>>, Self::PointerTag) {
200200
// We do not use a tag so we can just cheaply forward the allocation
201-
alloc
201+
(alloc, ())
202202
}
203203

204204
#[inline(always)]

0 commit comments

Comments
 (0)