summary refs log tree commit diff
path: root/compiler/rustc_mir/src/interpret/machine.rs
diff options
context:
space:
mode:
authorRalf Jung <post@ralfj.de>2021-07-14 22:10:17 +0200
committerRalf Jung <post@ralfj.de>2021-07-15 17:14:11 +0200
commitf4b61ba509e71710df5c14ac282fbdd512344072 (patch)
tree6508c060e035d2d3354447eb50a0a84371a6a5a4 /compiler/rustc_mir/src/interpret/machine.rs
parent8932aebfdfc8e4be18ed5213ba24f72954c7ba47 (diff)
downloadrust-f4b61ba509e71710df5c14ac282fbdd512344072.tar.gz
rust-f4b61ba509e71710df5c14ac282fbdd512344072.zip
adjustions and cleanup to make Miri build again
Diffstat (limited to 'compiler/rustc_mir/src/interpret/machine.rs')
-rw-r--r--compiler/rustc_mir/src/interpret/machine.rs108
1 files changed, 49 insertions, 59 deletions
diff --git a/compiler/rustc_mir/src/interpret/machine.rs b/compiler/rustc_mir/src/interpret/machine.rs
index c135e4f9963..7b8f2aecd0d 100644
--- a/compiler/rustc_mir/src/interpret/machine.rs
+++ b/compiler/rustc_mir/src/interpret/machine.rs
@@ -7,14 +7,14 @@ use std::fmt::Debug;
 use std::hash::Hash;
 
 use rustc_middle::mir;
-use rustc_middle::ty::{self, Ty};
+use rustc_middle::ty::{self, Ty, TyCtxt};
 use rustc_span::def_id::DefId;
 use rustc_target::abi::Size;
 use rustc_target::spec::abi::Abi;
 
 use super::{
-    AllocId, Allocation, Frame, ImmTy, InterpCx, InterpResult, LocalValue, MemPlace, Memory,
-    MemoryKind, OpTy, Operand, PlaceTy, Pointer, Provenance, Scalar, StackPopUnwind,
+    AllocId, AllocRange, Allocation, Frame, ImmTy, InterpCx, InterpResult, LocalValue, MemPlace,
+    Memory, MemoryKind, OpTy, Operand, PlaceTy, Pointer, Provenance, Scalar, StackPopUnwind,
 };
 
 /// Data returned by Machine::stack_pop,
@@ -262,34 +262,40 @@ pub trait Machine<'mir, 'tcx>: Sized {
     }
 
     /// Return the `AllocId` for the given thread-local static in the current thread.
-    fn thread_local_static_alloc_id(
+    fn thread_local_static_base_pointer(
         _ecx: &mut InterpCx<'mir, 'tcx, Self>,
         def_id: DefId,
-    ) -> InterpResult<'tcx, AllocId> {
+    ) -> InterpResult<'tcx, Pointer<Self::PointerTag>> {
         throw_unsup!(ThreadLocalStatic(def_id))
     }
 
-    /// Return the `AllocId` backing the given `extern static`.
-    fn extern_static_alloc_id(
+    /// Return the root pointer for the given `extern static`.
+    fn extern_static_base_pointer(
         mem: &Memory<'mir, 'tcx, Self>,
         def_id: DefId,
-    ) -> InterpResult<'tcx, AllocId> {
-        // Use the `AllocId` associated with the `DefId`. Any actual *access* will fail.
-        Ok(mem.tcx.create_static_alloc(def_id))
-    }
+    ) -> InterpResult<'tcx, Pointer<Self::PointerTag>>;
 
-    /// Return the "base" tag for the given *global* allocation: the one that is used for direct
-    /// accesses to this static/const/fn allocation. If `id` is not a global allocation,
-    /// this will return an unusable tag (i.e., accesses will be UB)!
-    ///
-    /// Called on the id returned by `thread_local_static_alloc_id` and `extern_static_alloc_id`, if needed.
+    /// Return a "base" pointer for the given allocation: the one that is used for direct
+    /// accesses to this static/const/fn allocation, or the one returned from the heap allocator.
     ///
-    /// `offset` is relative inside the allocation.
-    fn tag_global_base_pointer(
-        memory_extra: &Self::MemoryExtra,
+    /// Not called on `extern` or thread-local statics (those use the methods above).
+    fn tag_alloc_base_pointer(
+        mem: &Memory<'mir, 'tcx, Self>,
         ptr: Pointer,
     ) -> Pointer<Self::PointerTag>;
 
+    /// "Int-to-pointer cast"
+    fn ptr_from_addr(
+        mem: &Memory<'mir, 'tcx, Self>,
+        addr: u64,
+    ) -> Pointer<Option<Self::PointerTag>>;
+
+    /// Convert a pointer with provenance into an allocation-offset pair.
+    fn ptr_get_alloc(
+        mem: &Memory<'mir, 'tcx, Self>,
+        ptr: Pointer<Self::PointerTag>,
+    ) -> (AllocId, Size);
+
     /// Called to initialize the "extra" state of an allocation and make the pointers
     /// it contains (in relocations) tagged.  The way we construct allocations is
     /// to always first construct it without extra and then add the extra.
@@ -303,16 +309,13 @@ 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_global_base_pointer`.
     fn init_allocation_extra<'b>(
         memory_extra: &Self::MemoryExtra,
+        tcx: TyCtxt<'tcx>,
         id: AllocId,
         alloc: Cow<'b, Allocation>,
         kind: Option<MemoryKind<Self::MemoryKind>>,
-    ) -> (Cow<'b, Allocation<Self::PointerTag, Self::AllocExtra>>, Self::PointerTag);
+    ) -> Cow<'b, Allocation<Self::PointerTag, Self::AllocExtra>>;
 
     /// Hook for performing extra checks on a memory read access.
     ///
@@ -323,8 +326,8 @@ pub trait Machine<'mir, 'tcx>: Sized {
     fn memory_read(
         _memory_extra: &Self::MemoryExtra,
         _alloc_extra: &Self::AllocExtra,
-        _ptr: Pointer<Self::PointerTag>,
-        _size: Size,
+        _tag: Self::PointerTag,
+        _range: AllocRange,
     ) -> InterpResult<'tcx> {
         Ok(())
     }
@@ -334,8 +337,8 @@ pub trait Machine<'mir, 'tcx>: Sized {
     fn memory_written(
         _memory_extra: &mut Self::MemoryExtra,
         _alloc_extra: &mut Self::AllocExtra,
-        _ptr: Pointer<Self::PointerTag>,
-        _size: Size,
+        _tag: Self::PointerTag,
+        _range: AllocRange,
     ) -> InterpResult<'tcx> {
         Ok(())
     }
@@ -345,17 +348,8 @@ pub trait Machine<'mir, 'tcx>: Sized {
     fn memory_deallocated(
         _memory_extra: &mut Self::MemoryExtra,
         _alloc_extra: &mut Self::AllocExtra,
-        _ptr: Pointer<Self::PointerTag>,
-        _size: Size,
-    ) -> InterpResult<'tcx> {
-        Ok(())
-    }
-
-    /// Called after initializing static memory using the interpreter.
-    fn after_static_mem_initialized(
-        _ecx: &mut InterpCx<'mir, 'tcx, Self>,
-        _ptr: Pointer<Self::PointerTag>,
-        _size: Size,
+        _tag: Self::PointerTag,
+        _range: AllocRange,
     ) -> InterpResult<'tcx> {
         Ok(())
     }
@@ -400,19 +394,6 @@ pub trait Machine<'mir, 'tcx>: Sized {
         // By default, we do not support unwinding from panics
         Ok(StackPopJump::Normal)
     }
-
-    /// "Int-to-pointer cast"
-    fn ptr_from_addr(
-        mem: &Memory<'mir, 'tcx, Self>,
-        addr: u64,
-    ) -> Pointer<Option<Self::PointerTag>>;
-
-    /// Convert a pointer with provenance into an allocation-offset pair,
-    /// or a `None` with an absolute address if that conversion is not possible.
-    fn ptr_get_alloc(
-        mem: &Memory<'mir, 'tcx, Self>,
-        ptr: Pointer<Self::PointerTag>,
-    ) -> (Option<AllocId>, Size);
 }
 
 // A lot of the flexibility above is just needed for `Miri`, but all "compile-time" machines
@@ -461,17 +442,26 @@ pub macro compile_time_machine(<$mir: lifetime, $tcx: lifetime>) {
     #[inline(always)]
     fn init_allocation_extra<'b>(
         _memory_extra: &Self::MemoryExtra,
-        id: AllocId,
+        _tcx: TyCtxt<$tcx>,
+        _id: AllocId,
         alloc: Cow<'b, Allocation>,
         _kind: Option<MemoryKind<Self::MemoryKind>>,
-    ) -> (Cow<'b, Allocation<Self::PointerTag>>, Self::PointerTag) {
+    ) -> Cow<'b, Allocation<Self::PointerTag>> {
         // We do not use a tag so we can just cheaply forward the allocation
-        (alloc, id)
+        alloc
+    }
+
+    fn extern_static_base_pointer(
+        mem: &Memory<$mir, $tcx, Self>,
+        def_id: DefId,
+    ) -> InterpResult<$tcx, Pointer> {
+        // Use the `AllocId` associated with the `DefId`. Any actual *access* will fail.
+        Ok(Pointer::new(mem.tcx.create_static_alloc(def_id), Size::ZERO))
     }
 
     #[inline(always)]
-    fn tag_global_base_pointer(
-        _memory_extra: &Self::MemoryExtra,
+    fn tag_alloc_base_pointer(
+        _mem: &Memory<$mir, $tcx, Self>,
         ptr: Pointer<AllocId>,
     ) -> Pointer<AllocId> {
         ptr
@@ -486,9 +476,9 @@ pub macro compile_time_machine(<$mir: lifetime, $tcx: lifetime>) {
     fn ptr_get_alloc(
         _mem: &Memory<$mir, $tcx, Self>,
         ptr: Pointer<AllocId>,
-    ) -> (Option<AllocId>, Size) {
+    ) -> (AllocId, Size) {
         // We know `offset` is relative to the allocation, so we can use `into_parts`.
         let (alloc_id, offset) = ptr.into_parts();
-        (Some(alloc_id), offset)
+        (alloc_id, offset)
     }
 }