From 5e51a153f97b141dfeef795d22ec9e47967764f2 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sun, 1 Dec 2019 10:02:41 +0100 Subject: [PATCH] fix init_allocation_extra --- src/librustc_mir/const_eval.rs | 4 ++-- src/librustc_mir/interpret/eval_context.rs | 8 +++++--- src/librustc_mir/interpret/machine.rs | 14 ++++++++------ src/librustc_mir/interpret/memory.rs | 20 +++++++++++++------- src/librustc_mir/transform/const_prop.rs | 4 ++-- 5 files changed, 30 insertions(+), 20 deletions(-) diff --git a/src/librustc_mir/const_eval.rs b/src/librustc_mir/const_eval.rs index ef6e34c3dd0af..ff0cf6f4fdd21 100644 --- a/src/librustc_mir/const_eval.rs +++ b/src/librustc_mir/const_eval.rs @@ -462,9 +462,9 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir, _id: AllocId, alloc: Cow<'b, Allocation>, _kind: Option>, - ) -> Cow<'b, Allocation> { + ) -> (Cow<'b, Allocation>, Self::PointerTag) { // We do not use a tag so we can just cheaply forward the allocation - alloc + (alloc, ()) } #[inline(always)] diff --git a/src/librustc_mir/interpret/eval_context.rs b/src/librustc_mir/interpret/eval_context.rs index 16ebf2a34a0bf..8250cadb01d44 100644 --- a/src/librustc_mir/interpret/eval_context.rs +++ b/src/librustc_mir/interpret/eval_context.rs @@ -251,9 +251,11 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { } /// Call this to turn untagged "global" pointers (obtained via `tcx`) into - /// the *canonical* machine pointer to the allocation. This represents a *direct* - /// access to that memory, as opposed to access through a pointer that was created - /// by the program. Must never be used for derived (program-created) pointers! + /// the *canonical* machine pointer to the allocation. Must never be used + /// for any other pointers! + /// + /// This represents a *direct* access to that memory, as opposed to access + /// through a pointer that was created by the program. #[inline(always)] pub fn tag_static_base_pointer(&self, ptr: Pointer) -> Pointer { self.memory.tag_static_base_pointer(ptr) diff --git a/src/librustc_mir/interpret/machine.rs b/src/librustc_mir/interpret/machine.rs index 0bdb217203420..2ecc8d88ad398 100644 --- a/src/librustc_mir/interpret/machine.rs +++ b/src/librustc_mir/interpret/machine.rs @@ -240,18 +240,20 @@ pub trait Machine<'mir, 'tcx>: Sized { /// allocation (because a copy had to be done to add tags or metadata), machine memory will /// cache the result. (This relies on `AllocMap::get_or` being able to add the /// owned allocation to the map even when the map is shared.) + /// + /// Also return the "base" tag to use for this allocation: the one that is used for direct + /// accesses to this allocation. If `kind == STATIC_KIND`, this tag must be consistent + /// with `tag_static_base_pointer`. fn init_allocation_extra<'b>( memory_extra: &Self::MemoryExtra, id: AllocId, alloc: Cow<'b, Allocation>, kind: Option>, - ) -> Cow<'b, Allocation>; + ) -> (Cow<'b, Allocation>, Self::PointerTag); - /// Return the "base" tag for the given static allocation: the one that is used for direct - /// accesses to this static/const/fn allocation. - /// - /// Be aware that requesting the `Allocation` for that `id` will lead to cycles - /// for cyclic statics! + /// Return the "base" tag for the given *static* allocation: the one that is used for direct + /// accesses to this static/const/fn allocation. If `id` is not a static allocation, + /// this will return an unusable tag (i.e., accesses will be UB)! fn tag_static_base_pointer( memory_extra: &Self::MemoryExtra, id: AllocId, diff --git a/src/librustc_mir/interpret/memory.rs b/src/librustc_mir/interpret/memory.rs index 8aaaa2c22fe77..ee7fb18fd05a5 100644 --- a/src/librustc_mir/interpret/memory.rs +++ b/src/librustc_mir/interpret/memory.rs @@ -144,9 +144,11 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> { } /// Call this to turn untagged "global" pointers (obtained via `tcx`) into - /// the *canonical* machine pointer to the allocation. This represents a *direct* - /// access to that memory, as opposed to access through a pointer that was created - /// by the program. Must never be used for derived (program-created) pointers! + /// the *canonical* machine pointer to the allocation. Must never be used + /// for any other pointers! + /// + /// This represents a *direct* access to that memory, as opposed to access + /// through a pointer that was created by the program. #[inline] pub fn tag_static_base_pointer(&self, ptr: Pointer) -> Pointer { 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> { kind: MemoryKind, ) -> Pointer { let id = self.tcx.alloc_map.lock().reserve(); - let alloc = M::init_allocation_extra(&self.extra, id, Cow::Owned(alloc), Some(kind)); + debug_assert_ne!(Some(kind), M::STATIC_KIND.map(MemoryKind::Machine), + "dynamically allocating static memory"); + let (alloc, tag) = M::init_allocation_extra(&self.extra, id, Cow::Owned(alloc), Some(kind)); self.alloc_map.insert(id, (kind, alloc.into_owned())); - self.tag_static_base_pointer(Pointer::from(id)) + Pointer::from(id).with_tag(tag) } pub fn reallocate( @@ -478,12 +482,14 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> { } }; // We got tcx memory. Let the machine initialize its "extra" stuff. - Ok(M::init_allocation_extra( + let (alloc, tag) = M::init_allocation_extra( memory_extra, id, // always use the ID we got as input, not the "hidden" one. alloc, M::STATIC_KIND.map(MemoryKind::Machine), - )) + ); + debug_assert_eq!(tag, M::tag_static_base_pointer(memory_extra, id)); + Ok(alloc) } /// Gives raw access to the `Allocation`, without bounds or alignment checks. diff --git a/src/librustc_mir/transform/const_prop.rs b/src/librustc_mir/transform/const_prop.rs index 03c352bbd7d1d..bbbaac145f559 100644 --- a/src/librustc_mir/transform/const_prop.rs +++ b/src/librustc_mir/transform/const_prop.rs @@ -196,9 +196,9 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for ConstPropMachine { _id: AllocId, alloc: Cow<'b, Allocation>, _kind: Option>, - ) -> Cow<'b, Allocation> { + ) -> (Cow<'b, Allocation>, Self::PointerTag) { // We do not use a tag so we can just cheaply forward the allocation - alloc + (alloc, ()) } #[inline(always)]