From c885d8b41e5cc22ae08447c7eadfc65a15a7af9a Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Wed, 17 Oct 2018 17:36:26 +0200 Subject: [PATCH] don't do any work towards ptr provenance in const mode --- src/librustc_mir/const_eval.rs | 1 + src/librustc_mir/interpret/cast.rs | 4 +++- src/librustc_mir/interpret/machine.rs | 5 +++++ src/librustc_mir/interpret/place.rs | 8 ++++---- 4 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/librustc_mir/const_eval.rs b/src/librustc_mir/const_eval.rs index ebe877484c6..bc917140bbd 100644 --- a/src/librustc_mir/const_eval.rs +++ b/src/librustc_mir/const_eval.rs @@ -348,6 +348,7 @@ impl<'a, 'mir, 'tcx> interpret::Machine<'a, 'mir, 'tcx> type MemoryMap = FxHashMap, Allocation)>; const STATIC_KIND: Option = None; // no copying of statics allowed + const ENABLE_PTR_TRACKING_HOOKS: bool = false; // we don't have no provenance #[inline(always)] fn enforce_validity(_ecx: &EvalContext<'a, 'mir, 'tcx, Self>) -> bool { diff --git a/src/librustc_mir/interpret/cast.rs b/src/librustc_mir/interpret/cast.rs index 9b11a1637c6..62c42c4a493 100644 --- a/src/librustc_mir/interpret/cast.rs +++ b/src/librustc_mir/interpret/cast.rs @@ -47,7 +47,9 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M> Misc => { let src = self.read_value(src)?; - if src.layout.ty.is_region_ptr() && dest.layout.ty.is_unsafe_ptr() { + if M::ENABLE_PTR_TRACKING_HOOKS && + src.layout.ty.is_region_ptr() && dest.layout.ty.is_unsafe_ptr() + { // For the purpose of the "ptr tag hooks", treat this as creating // a new, raw reference. let place = self.ref_to_mplace(src)?; diff --git a/src/librustc_mir/interpret/machine.rs b/src/librustc_mir/interpret/machine.rs index 8f7abea47d8..1318bbe1c2b 100644 --- a/src/librustc_mir/interpret/machine.rs +++ b/src/librustc_mir/interpret/machine.rs @@ -101,6 +101,11 @@ pub trait Machine<'a, 'mir, 'tcx>: Sized { /// that is added to the memory so that the work is not done twice. const STATIC_KIND: Option; + /// As an optimization, you can prevent the pointer tracking hooks from ever being + /// called. You should only do this if you do not care about provenance tracking. + /// This controls the `tag_reference` and `tag_dereference` hooks. + const ENABLE_PTR_TRACKING_HOOKS: bool; + /// Whether to enforce the validity invariant fn enforce_validity(ecx: &EvalContext<'a, 'mir, 'tcx, Self>) -> bool; diff --git a/src/librustc_mir/interpret/place.rs b/src/librustc_mir/interpret/place.rs index 6ee70bac583..af3d6948628 100644 --- a/src/librustc_mir/interpret/place.rs +++ b/src/librustc_mir/interpret/place.rs @@ -265,12 +265,12 @@ where val: ValTy<'tcx, M::PointerTag>, ) -> EvalResult<'tcx, MPlaceTy<'tcx, M::PointerTag>> { let ptr = match val.to_scalar_ptr()? { - Scalar::Ptr(ptr) => { + Scalar::Ptr(ptr) if M::ENABLE_PTR_TRACKING_HOOKS => { // Machine might want to track the `*` operator let tag = M::tag_dereference(self, ptr, val.layout.ty)?; Scalar::Ptr(Pointer::new_with_tag(ptr.alloc_id, ptr.offset, tag)) } - scalar @ Scalar::Bits { .. } => scalar, + other => other, }; let pointee_type = val.layout.ty.builtin_deref(true).unwrap().ty; @@ -294,14 +294,14 @@ where borrow_kind: Option, ) -> EvalResult<'tcx, Value> { let ptr = match place.ptr { - Scalar::Ptr(ptr) => { + Scalar::Ptr(ptr) if M::ENABLE_PTR_TRACKING_HOOKS => { // Machine might want to track the `&` operator let (size, _) = self.size_and_align_of_mplace(place)? .expect("create_ref cannot determine size"); let tag = M::tag_reference(self, ptr, place.layout.ty, size, borrow_kind)?; Scalar::Ptr(Pointer::new_with_tag(ptr.alloc_id, ptr.offset, tag)) }, - scalar @ Scalar::Bits { .. } => scalar, + other => other, }; Ok(match place.meta { None => Value::Scalar(ptr.into()),