2016-03-14 21:48:00 -06:00
|
|
|
use std::error::Error;
|
|
|
|
use std::fmt;
|
2016-11-03 10:38:08 +01:00
|
|
|
use rustc::mir;
|
2016-11-17 17:23:40 +01:00
|
|
|
use rustc::ty::{BareFnTy, Ty, FnSig, layout};
|
2016-11-15 17:19:37 +01:00
|
|
|
use syntax::abi::Abi;
|
2016-06-01 11:22:37 +02:00
|
|
|
use memory::Pointer;
|
2016-06-17 13:09:20 +02:00
|
|
|
use rustc_const_math::ConstMathErr;
|
|
|
|
use syntax::codemap::Span;
|
2016-03-14 21:48:00 -06:00
|
|
|
|
2016-11-17 17:23:40 +01:00
|
|
|
#[derive(Clone, Debug)]
|
2016-06-14 10:34:54 +02:00
|
|
|
pub enum EvalError<'tcx> {
|
2016-11-15 17:19:37 +01:00
|
|
|
FunctionPointerTyMismatch(Abi, &'tcx FnSig<'tcx>, &'tcx BareFnTy<'tcx>),
|
2016-09-27 17:01:06 +02:00
|
|
|
NoMirFor(String),
|
2016-12-16 17:10:16 -08:00
|
|
|
UnterminatedCString(Pointer),
|
2016-03-14 21:48:00 -06:00
|
|
|
DanglingPointerDeref,
|
2016-09-22 15:47:16 +02:00
|
|
|
InvalidMemoryAccess,
|
2016-06-08 13:43:34 +02:00
|
|
|
InvalidFunctionPointer,
|
2016-03-14 21:48:00 -06:00
|
|
|
InvalidBool,
|
2016-03-28 21:08:08 -06:00
|
|
|
InvalidDiscriminant,
|
2016-05-31 12:05:25 +02:00
|
|
|
PointerOutOfBounds {
|
2016-06-01 11:22:37 +02:00
|
|
|
ptr: Pointer,
|
2016-11-18 12:55:14 +01:00
|
|
|
size: u64,
|
|
|
|
allocation_size: u64,
|
2016-05-31 12:05:25 +02:00
|
|
|
},
|
2016-03-18 23:03:46 -06:00
|
|
|
ReadPointerAsBytes,
|
|
|
|
InvalidPointerMath,
|
2016-03-26 23:56:49 -06:00
|
|
|
ReadUndefBytes,
|
2016-05-30 15:27:52 +02:00
|
|
|
InvalidBoolOp(mir::BinOp),
|
|
|
|
Unimplemented(String),
|
2016-06-13 11:39:15 +02:00
|
|
|
DerefFunctionPointer,
|
|
|
|
ExecuteMemory,
|
2016-06-17 13:09:20 +02:00
|
|
|
ArrayIndexOutOfBounds(Span, u64, u64),
|
|
|
|
Math(Span, ConstMathErr),
|
2017-01-12 08:28:42 +01:00
|
|
|
InvalidChar(u128),
|
2016-07-05 10:47:10 +02:00
|
|
|
OutOfMemory {
|
2016-11-18 12:55:14 +01:00
|
|
|
allocation_size: u64,
|
|
|
|
memory_size: u64,
|
|
|
|
memory_usage: u64,
|
2016-07-05 13:17:40 +02:00
|
|
|
},
|
|
|
|
ExecutionTimeLimitReached,
|
2016-07-05 13:23:58 +02:00
|
|
|
StackFrameLimitReached,
|
2016-07-05 14:27:27 +02:00
|
|
|
AlignmentCheckFailed {
|
2016-11-18 12:55:14 +01:00
|
|
|
required: u64,
|
|
|
|
has: u64,
|
2016-07-05 14:27:27 +02:00
|
|
|
},
|
2016-09-09 12:51:14 +02:00
|
|
|
CalledClosureAsFunction,
|
|
|
|
VtableForArgumentlessMethod,
|
2016-09-09 17:44:04 +02:00
|
|
|
ModifiedConstantMemory,
|
2016-09-13 13:08:57 +02:00
|
|
|
AssumptionNotHeld,
|
2016-09-28 11:48:43 -06:00
|
|
|
InlineAsm,
|
2016-11-03 12:30:41 +01:00
|
|
|
TypeNotPrimitive(Ty<'tcx>),
|
2016-11-17 14:48:34 +01:00
|
|
|
ReallocatedFrozenMemory,
|
|
|
|
DeallocatedFrozenMemory,
|
2016-11-17 17:23:40 +01:00
|
|
|
Layout(layout::LayoutError<'tcx>),
|
2017-01-12 09:41:36 +01:00
|
|
|
Unreachable,
|
2016-03-14 21:48:00 -06:00
|
|
|
}
|
|
|
|
|
2016-06-14 10:34:54 +02:00
|
|
|
pub type EvalResult<'tcx, T> = Result<T, EvalError<'tcx>>;
|
2016-03-14 21:48:00 -06:00
|
|
|
|
2016-06-14 10:34:54 +02:00
|
|
|
impl<'tcx> Error for EvalError<'tcx> {
|
2016-03-14 21:48:00 -06:00
|
|
|
fn description(&self) -> &str {
|
|
|
|
match *self {
|
2016-06-14 10:34:54 +02:00
|
|
|
EvalError::FunctionPointerTyMismatch(..) =>
|
|
|
|
"tried to call a function through a function pointer of a different type",
|
2016-09-22 15:47:16 +02:00
|
|
|
EvalError::InvalidMemoryAccess =>
|
|
|
|
"tried to access memory through an invalid pointer",
|
2016-03-26 23:56:49 -06:00
|
|
|
EvalError::DanglingPointerDeref =>
|
|
|
|
"dangling pointer was dereferenced",
|
2016-06-08 13:43:34 +02:00
|
|
|
EvalError::InvalidFunctionPointer =>
|
2016-11-15 14:11:00 +01:00
|
|
|
"tried to use an integer pointer or a dangling pointer as a function pointer",
|
2016-03-26 23:56:49 -06:00
|
|
|
EvalError::InvalidBool =>
|
|
|
|
"invalid boolean value read",
|
2016-03-28 21:08:08 -06:00
|
|
|
EvalError::InvalidDiscriminant =>
|
|
|
|
"invalid enum discriminant value read",
|
2016-05-31 12:05:25 +02:00
|
|
|
EvalError::PointerOutOfBounds { .. } =>
|
2016-03-26 23:56:49 -06:00
|
|
|
"pointer offset outside bounds of allocation",
|
2016-03-18 23:03:46 -06:00
|
|
|
EvalError::ReadPointerAsBytes =>
|
|
|
|
"a raw memory access tried to access part of a pointer value as raw bytes",
|
|
|
|
EvalError::InvalidPointerMath =>
|
|
|
|
"attempted to do math or a comparison on pointers into different allocations",
|
2016-03-26 23:56:49 -06:00
|
|
|
EvalError::ReadUndefBytes =>
|
|
|
|
"attempted to read undefined bytes",
|
2016-05-30 15:27:52 +02:00
|
|
|
EvalError::InvalidBoolOp(_) =>
|
|
|
|
"invalid boolean operation",
|
|
|
|
EvalError::Unimplemented(ref msg) => msg,
|
2016-06-13 11:39:15 +02:00
|
|
|
EvalError::DerefFunctionPointer =>
|
|
|
|
"tried to dereference a function pointer",
|
|
|
|
EvalError::ExecuteMemory =>
|
|
|
|
"tried to treat a memory pointer as a function pointer",
|
2016-06-17 13:09:20 +02:00
|
|
|
EvalError::ArrayIndexOutOfBounds(..) =>
|
|
|
|
"array index out of bounds",
|
|
|
|
EvalError::Math(..) =>
|
|
|
|
"mathematical operation failed",
|
2016-09-27 17:01:06 +02:00
|
|
|
EvalError::NoMirFor(..) =>
|
|
|
|
"mir not found",
|
2016-06-20 12:29:45 +02:00
|
|
|
EvalError::InvalidChar(..) =>
|
|
|
|
"tried to interpret an invalid 32-bit value as a char",
|
2016-07-05 10:47:10 +02:00
|
|
|
EvalError::OutOfMemory{..} =>
|
2016-07-05 13:17:40 +02:00
|
|
|
"could not allocate more memory",
|
|
|
|
EvalError::ExecutionTimeLimitReached =>
|
|
|
|
"reached the configured maximum execution time",
|
2016-07-05 13:23:58 +02:00
|
|
|
EvalError::StackFrameLimitReached =>
|
|
|
|
"reached the configured maximum number of stack frames",
|
2016-07-05 14:27:27 +02:00
|
|
|
EvalError::AlignmentCheckFailed{..} =>
|
|
|
|
"tried to execute a misaligned read or write",
|
2016-09-09 12:51:14 +02:00
|
|
|
EvalError::CalledClosureAsFunction =>
|
|
|
|
"tried to call a closure through a function pointer",
|
|
|
|
EvalError::VtableForArgumentlessMethod =>
|
|
|
|
"tried to call a vtable function without arguments",
|
2016-09-09 17:44:04 +02:00
|
|
|
EvalError::ModifiedConstantMemory =>
|
|
|
|
"tried to modify constant memory",
|
2016-09-13 13:08:57 +02:00
|
|
|
EvalError::AssumptionNotHeld =>
|
2016-09-28 18:22:25 +02:00
|
|
|
"`assume` argument was false",
|
2016-09-28 11:48:43 -06:00
|
|
|
EvalError::InlineAsm =>
|
|
|
|
"cannot evaluate inline assembly",
|
2016-11-03 12:30:41 +01:00
|
|
|
EvalError::TypeNotPrimitive(_) =>
|
|
|
|
"expected primitive type, got nonprimitive",
|
2016-11-17 14:48:34 +01:00
|
|
|
EvalError::ReallocatedFrozenMemory =>
|
|
|
|
"tried to reallocate frozen memory",
|
|
|
|
EvalError::DeallocatedFrozenMemory =>
|
|
|
|
"tried to deallocate frozen memory",
|
2016-11-17 17:23:40 +01:00
|
|
|
EvalError::Layout(_) =>
|
|
|
|
"rustc layout computation failed",
|
2016-12-16 17:10:16 -08:00
|
|
|
EvalError::UnterminatedCString(_) =>
|
|
|
|
"attempted to get length of a null terminated string, but no null found before end of allocation",
|
2017-01-12 09:41:36 +01:00
|
|
|
EvalError::Unreachable =>
|
|
|
|
"entered unreachable code",
|
2016-03-14 21:48:00 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn cause(&self) -> Option<&Error> { None }
|
|
|
|
}
|
|
|
|
|
2016-06-14 10:34:54 +02:00
|
|
|
impl<'tcx> fmt::Display for EvalError<'tcx> {
|
2016-03-14 21:48:00 -06:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2016-05-31 12:05:25 +02:00
|
|
|
match *self {
|
2016-06-01 11:22:37 +02:00
|
|
|
EvalError::PointerOutOfBounds { ptr, size, allocation_size } => {
|
|
|
|
write!(f, "memory access of {}..{} outside bounds of allocation {} which has size {}",
|
|
|
|
ptr.offset, ptr.offset + size, ptr.alloc_id, allocation_size)
|
|
|
|
},
|
2016-09-27 17:01:06 +02:00
|
|
|
EvalError::NoMirFor(ref func) => write!(f, "no mir for `{}`", func),
|
2016-11-15 17:19:37 +01:00
|
|
|
EvalError::FunctionPointerTyMismatch(abi, sig, got) =>
|
|
|
|
write!(f, "tried to call a function with abi {:?} and sig {:?} through a function pointer of type {:?}", abi, sig, got),
|
2016-06-17 13:09:20 +02:00
|
|
|
EvalError::ArrayIndexOutOfBounds(span, len, index) =>
|
2016-06-21 09:43:27 +02:00
|
|
|
write!(f, "index out of bounds: the len is {} but the index is {} at {:?}", len, index, span),
|
2016-06-17 13:09:20 +02:00
|
|
|
EvalError::Math(span, ref err) =>
|
2016-06-21 09:43:27 +02:00
|
|
|
write!(f, "{:?} at {:?}", err, span),
|
2016-06-20 12:29:45 +02:00
|
|
|
EvalError::InvalidChar(c) =>
|
2016-06-21 09:43:27 +02:00
|
|
|
write!(f, "tried to interpret an invalid 32-bit value as a char: {}", c),
|
2016-07-05 10:47:10 +02:00
|
|
|
EvalError::OutOfMemory { allocation_size, memory_size, memory_usage } =>
|
|
|
|
write!(f, "tried to allocate {} more bytes, but only {} bytes are free of the {} byte memory",
|
|
|
|
allocation_size, memory_size - memory_usage, memory_size),
|
2016-07-05 14:27:27 +02:00
|
|
|
EvalError::AlignmentCheckFailed { required, has } =>
|
|
|
|
write!(f, "tried to access memory with alignment {}, but alignment {} is required",
|
|
|
|
has, required),
|
2016-11-03 12:30:41 +01:00
|
|
|
EvalError::TypeNotPrimitive(ref ty) =>
|
|
|
|
write!(f, "expected primitive type, got {}", ty),
|
2016-11-17 17:23:40 +01:00
|
|
|
EvalError::Layout(ref err) =>
|
|
|
|
write!(f, "rustc layout computation failed: {:?}", err),
|
2016-05-31 12:05:25 +02:00
|
|
|
_ => write!(f, "{}", self.description()),
|
|
|
|
}
|
2016-03-14 21:48:00 -06:00
|
|
|
}
|
|
|
|
}
|