From 56d89364a5d097c73a1a148bd0c2760784ce0b9c Mon Sep 17 00:00:00 2001
From: JCTyblaidd <JCTyblaidd@users.noreply.github.com>
Date: Fri, 11 Dec 2020 18:42:36 +0000
Subject: [PATCH 1/3] Add post-initialization hook for static memory
 initialized using the interpereter.

---
 compiler/rustc_mir/src/interpret/machine.rs | 10 ++++++++++
 compiler/rustc_mir/src/interpret/traits.rs  |  5 ++++-
 2 files changed, 14 insertions(+), 1 deletion(-)

diff --git a/compiler/rustc_mir/src/interpret/machine.rs b/compiler/rustc_mir/src/interpret/machine.rs
index 74625569432c3..67166062d40da 100644
--- a/compiler/rustc_mir/src/interpret/machine.rs
+++ b/compiler/rustc_mir/src/interpret/machine.rs
@@ -9,6 +9,7 @@ use std::hash::Hash;
 use rustc_middle::mir;
 use rustc_middle::ty::{self, Ty};
 use rustc_span::def_id::DefId;
+use rustc_target::abi::Size;
 
 use super::{
     AllocId, Allocation, AllocationExtra, CheckInAllocMsg, Frame, ImmTy, InterpCx, InterpResult,
@@ -299,6 +300,15 @@ pub trait Machine<'mir, 'tcx>: Sized {
         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
+    ) -> InterpResult<'tcx> {
+        Ok(())
+    }
+
     /// Executes a retagging operation
     #[inline]
     fn retag(
diff --git a/compiler/rustc_mir/src/interpret/traits.rs b/compiler/rustc_mir/src/interpret/traits.rs
index fa7036f4e5b02..18dba4775980b 100644
--- a/compiler/rustc_mir/src/interpret/traits.rs
+++ b/compiler/rustc_mir/src/interpret/traits.rs
@@ -56,8 +56,9 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
         // If you touch this code, be sure to also make the corresponding changes to
         // `get_vtable` in `rust_codegen_llvm/meth.rs`.
         // /////////////////////////////////////////////////////////////////////////////////////////
+        let vtable_size = ptr_size * u64::try_from(methods.len()).unwrap().checked_add(3).unwrap();
         let vtable = self.memory.allocate(
-            ptr_size * u64::try_from(methods.len()).unwrap().checked_add(3).unwrap(),
+            vtable_size,
             ptr_align,
             MemoryKind::Vtable,
         );
@@ -93,6 +94,8 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
             }
         }
 
+        M::after_static_mem_initialized(self, vtable, vtable_size)?;
+
         self.memory.mark_immutable(vtable.alloc_id)?;
         assert!(self.vtables.insert((ty, poly_trait_ref), vtable).is_none());
 

From 6ce29906f1fce80284cb050605f46a2959924a83 Mon Sep 17 00:00:00 2001
From: JCTyblaidd <JCTyblaidd@users.noreply.github.com>
Date: Fri, 11 Dec 2020 19:11:39 +0000
Subject: [PATCH 2/3] Fix rustfmt failure

---
 compiler/rustc_mir/src/interpret/machine.rs | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/compiler/rustc_mir/src/interpret/machine.rs b/compiler/rustc_mir/src/interpret/machine.rs
index 67166062d40da..f50cc6c16ea16 100644
--- a/compiler/rustc_mir/src/interpret/machine.rs
+++ b/compiler/rustc_mir/src/interpret/machine.rs
@@ -304,7 +304,7 @@ pub trait Machine<'mir, 'tcx>: Sized {
     fn after_static_mem_initialized(
         _ecx: &mut InterpCx<'mir, 'tcx, Self>,
         _ptr: Pointer<Self::PointerTag>,
-        _size: Size
+        _size: Size,
     ) -> InterpResult<'tcx> {
         Ok(())
     }

From 175226a01c53a9b9779e05e2d1076d4a3ed37911 Mon Sep 17 00:00:00 2001
From: JCTyblaidd <JCTyblaidd@users.noreply.github.com>
Date: Fri, 11 Dec 2020 19:28:20 +0000
Subject: [PATCH 3/3] Rustfmt

---
 compiler/rustc_mir/src/interpret/traits.rs | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)

diff --git a/compiler/rustc_mir/src/interpret/traits.rs b/compiler/rustc_mir/src/interpret/traits.rs
index 18dba4775980b..09ce6bc0fb754 100644
--- a/compiler/rustc_mir/src/interpret/traits.rs
+++ b/compiler/rustc_mir/src/interpret/traits.rs
@@ -57,11 +57,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
         // `get_vtable` in `rust_codegen_llvm/meth.rs`.
         // /////////////////////////////////////////////////////////////////////////////////////////
         let vtable_size = ptr_size * u64::try_from(methods.len()).unwrap().checked_add(3).unwrap();
-        let vtable = self.memory.allocate(
-            vtable_size,
-            ptr_align,
-            MemoryKind::Vtable,
-        );
+        let vtable = self.memory.allocate(vtable_size, ptr_align, MemoryKind::Vtable);
 
         let drop = Instance::resolve_drop_in_place(tcx, ty);
         let drop = self.memory.create_fn_alloc(FnVal::Instance(drop));