From 5d23518a12548fdbbca74fcc3171fdc2f3888334 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Thu, 28 Nov 2019 09:03:00 +0100 Subject: [PATCH] const_prop: detect and avoid catching Miri errors that require allocation --- src/librustc/mir/interpret/error.rs | 20 +++++++++------ src/librustc_mir/transform/const_prop.rs | 32 ++++++++++++++++++------ 2 files changed, 38 insertions(+), 14 deletions(-) diff --git a/src/librustc/mir/interpret/error.rs b/src/librustc/mir/interpret/error.rs index 4fe82f03b03..fadd90c2257 100644 --- a/src/librustc/mir/interpret/error.rs +++ b/src/librustc/mir/interpret/error.rs @@ -44,14 +44,14 @@ pub fn assert_reported(self) { pub type ConstEvalRawResult<'tcx> = Result, ErrorHandled>; pub type ConstEvalResult<'tcx> = Result<&'tcx ty::Const<'tcx>, ErrorHandled>; -#[derive(Clone, Debug, RustcEncodable, RustcDecodable)] +#[derive(Clone, Debug)] pub struct ConstEvalErr<'tcx> { pub span: Span, pub error: crate::mir::interpret::InterpError<'tcx>, pub stacktrace: Vec>, } -#[derive(Clone, Debug, RustcEncodable, RustcDecodable, HashStable)] +#[derive(Clone, Debug)] pub struct FrameInfo<'tcx> { /// This span is in the caller. pub call_site: Span, @@ -327,7 +327,7 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { /// Error information for when the program we executed turned out not to actually be a valid /// program. This cannot happen in stand-alone Miri, but it can happen during CTFE/ConstProp /// where we work on generic code or execution does not have all information available. -#[derive(Clone, RustcEncodable, RustcDecodable, HashStable)] +#[derive(Clone, HashStable)] pub enum InvalidProgramInfo<'tcx> { /// Resolution can fail if we are in a too generic context. TooGeneric, @@ -357,7 +357,7 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { } /// Error information for when the program caused Undefined Behavior. -#[derive(Clone, RustcEncodable, RustcDecodable, HashStable)] +#[derive(Clone, HashStable)] pub enum UndefinedBehaviorInfo { /// Free-form case. Only for errors that are never caught! Ub(String), @@ -390,11 +390,15 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { /// /// Currently, we also use this as fall-back error kind for errors that have not been /// categorized yet. -#[derive(Clone, RustcEncodable, RustcDecodable, HashStable)] +#[derive(Clone, HashStable)] pub enum UnsupportedOpInfo<'tcx> { /// Free-form case. Only for errors that are never caught! Unsupported(String), + /// When const-prop encounters a situation it does not support, it raises this error. + /// This must not allocate for performance reasons. + ConstPropUnsupported(&'tcx str), + // -- Everything below is not categorized yet -- FunctionAbiMismatch(Abi, Abi), FunctionArgMismatch(Ty<'tcx>, Ty<'tcx>), @@ -555,13 +559,15 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { not a power of two"), Unsupported(ref msg) => write!(f, "{}", msg), + ConstPropUnsupported(ref msg) => + write!(f, "Constant propagation encountered an unsupported situation: {}", msg), } } } /// Error information for when the program exhausted the resources granted to it /// by the interpreter. -#[derive(Clone, RustcEncodable, RustcDecodable, HashStable)] +#[derive(Clone, HashStable)] pub enum ResourceExhaustionInfo { /// The stack grew too big. StackFrameLimitReached, @@ -582,7 +588,7 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { } } -#[derive(Clone, RustcEncodable, RustcDecodable, HashStable)] +#[derive(Clone, HashStable)] pub enum InterpError<'tcx> { /// The program panicked. Panic(PanicInfo), diff --git a/src/librustc_mir/transform/const_prop.rs b/src/librustc_mir/transform/const_prop.rs index 6f0b960cab1..c02d07b80fa 100644 --- a/src/librustc_mir/transform/const_prop.rs +++ b/src/librustc_mir/transform/const_prop.rs @@ -168,14 +168,14 @@ fn call_intrinsic( _ret: Option<(PlaceTy<'tcx>, BasicBlock)>, _unwind: Option ) -> InterpResult<'tcx> { - throw_unsup_format!("calling intrinsics isn't supported in ConstProp"); + throw_unsup!(ConstPropUnsupported("calling intrinsics isn't supported in ConstProp")); } fn ptr_to_int( _mem: &Memory<'mir, 'tcx, Self>, _ptr: Pointer, ) -> InterpResult<'tcx, u64> { - throw_unsup_format!("ptr-to-int casts aren't supported in ConstProp"); + throw_unsup!(ConstPropUnsupported("ptr-to-int casts aren't supported in ConstProp")); } fn binary_ptr_op( @@ -185,7 +185,8 @@ fn binary_ptr_op( _right: ImmTy<'tcx>, ) -> InterpResult<'tcx, (Scalar, bool, Ty<'tcx>)> { // We can't do this because aliasing of memory can differ between const eval and llvm - throw_unsup_format!("pointer arithmetic or comparisons aren't supported in ConstProp"); + throw_unsup!(ConstPropUnsupported("pointer arithmetic or comparisons aren't supported \ + in ConstProp")); } fn find_foreign_static( @@ -218,7 +219,7 @@ fn box_alloc( _ecx: &mut InterpCx<'mir, 'tcx, Self>, _dest: PlaceTy<'tcx>, ) -> InterpResult<'tcx> { - throw_unsup_format!("can't const prop `box` keyword"); + throw_unsup!(ConstPropUnsupported("can't const prop `box` keyword")); } fn access_local( @@ -229,7 +230,7 @@ fn access_local( let l = &frame.locals[local]; if l.value == LocalValue::Uninitialized { - throw_unsup_format!("tried to access an uninitialized local"); + throw_unsup!(ConstPropUnsupported("tried to access an uninitialized local")); } l.access() @@ -241,7 +242,7 @@ fn before_access_static( // if the static allocation is mutable or if it has relocations (it may be legal to mutate // the memory behind that in the future), then we can't const prop it if allocation.mutability == Mutability::Mutable || allocation.relocations().len() > 0 { - throw_unsup_format!("can't eval mutable statics in ConstProp"); + throw_unsup!(ConstPropUnsupported("can't eval mutable statics in ConstProp")); } Ok(()) @@ -389,9 +390,26 @@ fn use_ecx( let r = match f(self) { Ok(val) => Some(val), Err(error) => { - use rustc::mir::interpret::InterpError::*; + use rustc::mir::interpret::{ + UnsupportedOpInfo, + UndefinedBehaviorInfo, + InterpError::* + }; match error.kind { Exit(_) => bug!("the CTFE program cannot exit"), + + // Some error shouldn't come up because creating them causes + // an allocation, which we should avoid. When that happens, + // dedicated error variants should be introduced instead. + // Only test this in debug builds though to avoid disruptions. + Unsupported(UnsupportedOpInfo::Unsupported(_)) + | Unsupported(UnsupportedOpInfo::ValidationFailure(_)) + | UndefinedBehavior(UndefinedBehaviorInfo::Ub(_)) + | UndefinedBehavior(UndefinedBehaviorInfo::UbExperimental(_)) + if cfg!(debug_assertions) => { + bug!("const-prop encountered allocating error: {:?}", error.kind); + } + Unsupported(_) | UndefinedBehavior(_) | InvalidProgram(_) -- 2.44.0