rust/src/error.rs

164 lines
6.9 KiB
Rust
Raw Normal View History

2016-03-14 21:48:00 -06:00
use std::error::Error;
use std::fmt;
use rustc::mir;
use rustc::ty::{BareFnTy, Ty, FnSig, layout};
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
#[derive(Clone, Debug)]
pub enum EvalError<'tcx> {
FunctionPointerTyMismatch(Abi, &'tcx FnSig<'tcx>, &'tcx BareFnTy<'tcx>),
NoMirFor(String),
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,
InvalidDiscriminant,
2016-05-31 12:05:25 +02:00
PointerOutOfBounds {
2016-06-01 11:22:37 +02:00
ptr: Pointer,
size: u64,
allocation_size: u64,
2016-05-31 12:05:25 +02:00
},
ReadPointerAsBytes,
InvalidPointerMath,
ReadUndefBytes,
2016-05-30 15:27:52 +02:00
InvalidBoolOp(mir::BinOp),
Unimplemented(String),
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 {
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,
AlignmentCheckFailed {
required: u64,
has: u64,
},
CalledClosureAsFunction,
VtableForArgumentlessMethod,
ModifiedConstantMemory,
2016-09-13 13:08:57 +02:00
AssumptionNotHeld,
2016-09-28 11:48:43 -06:00
InlineAsm,
TypeNotPrimitive(Ty<'tcx>),
ReallocatedFrozenMemory,
DeallocatedFrozenMemory,
Layout(layout::LayoutError<'tcx>),
Unreachable,
2016-03-14 21:48:00 -06:00
}
pub type EvalResult<'tcx, T> = Result<T, EvalError<'tcx>>;
2016-03-14 21:48:00 -06:00
impl<'tcx> Error for EvalError<'tcx> {
2016-03-14 21:48:00 -06:00
fn description(&self) -> &str {
match *self {
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",
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",
EvalError::InvalidBool =>
"invalid boolean value read",
EvalError::InvalidDiscriminant =>
"invalid enum discriminant value read",
2016-05-31 12:05:25 +02:00
EvalError::PointerOutOfBounds { .. } =>
"pointer offset outside bounds of allocation",
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",
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,
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",
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",
EvalError::AlignmentCheckFailed{..} =>
"tried to execute a misaligned read or write",
EvalError::CalledClosureAsFunction =>
"tried to call a closure through a function pointer",
EvalError::VtableForArgumentlessMethod =>
"tried to call a vtable function without arguments",
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",
EvalError::TypeNotPrimitive(_) =>
"expected primitive type, got nonprimitive",
EvalError::ReallocatedFrozenMemory =>
"tried to reallocate frozen memory",
EvalError::DeallocatedFrozenMemory =>
"tried to deallocate frozen memory",
EvalError::Layout(_) =>
"rustc layout computation failed",
EvalError::UnterminatedCString(_) =>
"attempted to get length of a null terminated string, but no null found before end of allocation",
EvalError::Unreachable =>
"entered unreachable code",
2016-03-14 21:48:00 -06:00
}
}
fn cause(&self) -> Option<&Error> { None }
}
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)
},
EvalError::NoMirFor(ref func) => write!(f, "no mir for `{}`", func),
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),
EvalError::AlignmentCheckFailed { required, has } =>
write!(f, "tried to access memory with alignment {}, but alignment {} is required",
has, required),
EvalError::TypeNotPrimitive(ref ty) =>
write!(f, "expected primitive type, got {}", ty),
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
}
}