From 8cbfbf77f86ad242395bc283d784514b3398de25 Mon Sep 17 00:00:00 2001 From: Oliver Schneider Date: Fri, 15 Sep 2017 13:02:33 +0200 Subject: [PATCH] Remove `#[linkage(foo)]` statics from core miri --- miri/lib.rs | 28 ++++++++++++++++++++++++ src/librustc_mir/interpret/const_eval.rs | 10 +++++++++ src/librustc_mir/interpret/machine.rs | 8 +++++++ src/librustc_mir/interpret/step.rs | 18 +-------------- 4 files changed, 47 insertions(+), 17 deletions(-) diff --git a/miri/lib.rs b/miri/lib.rs index a824f47a509..428724f7de5 100644 --- a/miri/lib.rs +++ b/miri/lib.rs @@ -239,4 +239,32 @@ fn box_alloc<'a>( .map(PrimVal::Ptr) } } + + fn global_item_with_linkage<'a>( + ecx: &mut EvalContext<'a, 'tcx, Self>, + instance: ty::Instance<'tcx>, + mutability: Mutability, + ) -> EvalResult<'tcx> { + // FIXME: check that it's `#[linkage = "extern_weak"]` + trace!("Initializing an extern global with NULL"); + let ptr_size = ecx.memory.pointer_size(); + let ptr = ecx.memory.allocate( + ptr_size, + ptr_size, + MemoryKind::UninitializedStatic, + )?; + ecx.memory.write_ptr_sized_unsigned(ptr, PrimVal::Bytes(0))?; + ecx.memory.mark_static_initalized(ptr.alloc_id, mutability)?; + ecx.globals.insert( + GlobalId { + instance, + promoted: None, + }, + PtrAndAlign { + ptr: ptr.into(), + aligned: true, + }, + ); + Ok(()) + } } diff --git a/src/librustc_mir/interpret/const_eval.rs b/src/librustc_mir/interpret/const_eval.rs index c6483ff1783..7fa28dccbab 100644 --- a/src/librustc_mir/interpret/const_eval.rs +++ b/src/librustc_mir/interpret/const_eval.rs @@ -245,4 +245,14 @@ fn box_alloc<'a>( ConstEvalError::NeedsRfc("Heap allocations via `box` keyword".to_string()).into(), ) } + + fn global_item_with_linkage<'a>( + _ecx: &mut EvalContext<'a, 'tcx, Self>, + _instance: ty::Instance<'tcx>, + _mutability: Mutability, + ) -> EvalResult<'tcx> { + Err( + ConstEvalError::NotConst("statics with `linkage` attribute".to_string()).into(), + ) + } } diff --git a/src/librustc_mir/interpret/machine.rs b/src/librustc_mir/interpret/machine.rs index 2d607005fe7..debb17fc0a7 100644 --- a/src/librustc_mir/interpret/machine.rs +++ b/src/librustc_mir/interpret/machine.rs @@ -6,6 +6,7 @@ use rustc::{mir, ty}; use syntax::codemap::Span; +use syntax::ast::Mutability; /// Methods of this trait signifies a point where CTFE evaluation would fail /// and some use case dependent behaviour can instead be applied @@ -70,4 +71,11 @@ fn box_alloc<'a>( ecx: &mut EvalContext<'a, 'tcx, Self>, ty: ty::Ty<'tcx>, ) -> EvalResult<'tcx, PrimVal>; + + /// Called when trying to access a global declared with a `linkage` attribute + fn global_item_with_linkage<'a>( + ecx: &mut EvalContext<'a, 'tcx, Self>, + instance: ty::Instance<'tcx>, + mutability: Mutability, + ) -> EvalResult<'tcx>; } diff --git a/src/librustc_mir/interpret/step.rs b/src/librustc_mir/interpret/step.rs index 05f1bd10e87..1f538707527 100644 --- a/src/librustc_mir/interpret/step.rs +++ b/src/librustc_mir/interpret/step.rs @@ -205,23 +205,7 @@ fn global_item( return Ok(false); } if self.tcx.has_attr(def_id, "linkage") { - // FIXME: check that it's `#[linkage = "extern_weak"]` - trace!("Initializing an extern global with NULL"); - let ptr_size = self.memory.pointer_size(); - let ptr = self.memory.allocate( - ptr_size, - ptr_size, - MemoryKind::UninitializedStatic, - )?; - self.memory.write_ptr_sized_unsigned(ptr, PrimVal::Bytes(0))?; - self.memory.mark_static_initalized(ptr.alloc_id, mutability)?; - self.globals.insert( - cid, - PtrAndAlign { - ptr: ptr.into(), - aligned: true, - }, - ); + M::global_item_with_linkage(self, cid.instance, mutability)?; return Ok(false); } let mir = self.load_mir(instance.def)?;