rust/src/librustc_mir/interpret/error.rs

249 lines
10 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::{FnSig, Ty, layout};
use memory::{MemoryPointer, LockInfo, AccessKind, Kind};
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(FnSig<'tcx>, FnSig<'tcx>),
NoMirFor(String),
2017-07-04 13:16:29 +02:00
UnterminatedCString(MemoryPointer),
2016-03-14 21:48:00 -06:00
DanglingPointerDeref,
DoubleFree,
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 {
2017-07-04 13:16:29 +02:00
ptr: MemoryPointer,
access: bool,
allocation_size: u64,
2016-05-31 12:05:25 +02:00
},
InvalidNullPointerUsage,
ReadPointerAsBytes,
2017-06-19 10:58:59 +02:00
ReadBytesAsPointer,
InvalidPointerMath,
ReadUndefBytes,
DeadLocal,
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-06-22 00:08:19 -07:00
Intrinsic(String),
OverflowingMath,
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,
2017-05-25 16:40:13 -07:00
OutOfTls,
TlsOutOfBounds,
2017-05-25 22:38:07 -07:00
AbiViolation(String),
AlignmentCheckFailed {
required: u64,
has: u64,
},
MemoryLockViolation {
ptr: MemoryPointer,
len: u64,
access: AccessKind,
lock: LockInfo,
},
CalledClosureAsFunction,
VtableForArgumentlessMethod,
ModifiedConstantMemory,
2016-09-13 13:08:57 +02:00
AssumptionNotHeld,
2016-09-28 11:48:43 -06:00
InlineAsm,
TypeNotPrimitive(Ty<'tcx>),
ReallocatedWrongMemoryKind(Kind, Kind),
DeallocatedWrongMemoryKind(Kind, Kind),
DeallocatedLockedMemory,
ReallocateNonBasePtr,
DeallocateNonBasePtr,
IncorrectAllocationInformation,
Layout(layout::LayoutError<'tcx>),
2017-06-23 12:55:49 +02:00
HeapAllocZeroBytes,
HeapAllocNonPowerOfTwoAlignment(u64),
Unreachable,
Panic,
NeedsRfc(String),
NotConst(String),
ReadFromReturnPointer,
PathNotFound(Vec<String>),
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 {
use EvalError::*;
2016-03-14 21:48:00 -06:00
match *self {
FunctionPointerTyMismatch(..) =>
"tried to call a function through a function pointer of a different type",
InvalidMemoryAccess =>
2016-09-22 15:47:16 +02:00
"tried to access memory through an invalid pointer",
DanglingPointerDeref =>
"dangling pointer was dereferenced",
DoubleFree =>
"tried to deallocate dangling pointer",
InvalidFunctionPointer =>
2016-11-15 14:11:00 +01:00
"tried to use an integer pointer or a dangling pointer as a function pointer",
InvalidBool =>
"invalid boolean value read",
InvalidDiscriminant =>
"invalid enum discriminant value read",
PointerOutOfBounds { .. } =>
"pointer offset outside bounds of allocation",
InvalidNullPointerUsage =>
"invalid use of NULL pointer",
MemoryLockViolation { .. } =>
"memory access conflicts with lock",
DeallocatedLockedMemory =>
"deallocated memory while a lock was held",
ReadPointerAsBytes =>
"a raw memory access tried to access part of a pointer value as raw bytes",
ReadBytesAsPointer =>
2017-06-19 10:58:59 +02:00
"a memory access tried to interpret some bytes as a pointer",
InvalidPointerMath =>
2017-06-23 13:30:31 +02:00
"attempted to do invalid arithmetic on pointers that would leak base addresses, e.g. comparing pointers into different allocations",
ReadUndefBytes =>
"attempted to read undefined bytes",
DeadLocal =>
"tried to access a dead local variable",
InvalidBoolOp(_) =>
2016-05-30 15:27:52 +02:00
"invalid boolean operation",
Unimplemented(ref msg) => msg,
DerefFunctionPointer =>
"tried to dereference a function pointer",
ExecuteMemory =>
"tried to treat a memory pointer as a function pointer",
ArrayIndexOutOfBounds(..) =>
2016-06-17 13:09:20 +02:00
"array index out of bounds",
Math(..) =>
2016-06-17 13:09:20 +02:00
"mathematical operation failed",
Intrinsic(..) =>
2017-06-22 00:08:19 -07:00
"intrinsic failed",
OverflowingMath =>
"attempted to do overflowing math",
NoMirFor(..) =>
"mir not found",
InvalidChar(..) =>
2016-06-20 12:29:45 +02:00
"tried to interpret an invalid 32-bit value as a char",
OutOfMemory{..} =>
2016-07-05 13:17:40 +02:00
"could not allocate more memory",
ExecutionTimeLimitReached =>
2016-07-05 13:17:40 +02:00
"reached the configured maximum execution time",
StackFrameLimitReached =>
2016-07-05 13:23:58 +02:00
"reached the configured maximum number of stack frames",
OutOfTls =>
2017-05-25 16:40:13 -07:00
"reached the maximum number of representable TLS keys",
TlsOutOfBounds =>
2017-05-25 16:40:13 -07:00
"accessed an invalid (unallocated) TLS key",
AbiViolation(ref msg) => msg,
AlignmentCheckFailed{..} =>
"tried to execute a misaligned read or write",
CalledClosureAsFunction =>
"tried to call a closure through a function pointer",
VtableForArgumentlessMethod =>
"tried to call a vtable function without arguments",
ModifiedConstantMemory =>
"tried to modify constant memory",
AssumptionNotHeld =>
2016-09-28 18:22:25 +02:00
"`assume` argument was false",
InlineAsm =>
2017-02-10 05:27:02 -08:00
"miri does not support inline assembly",
TypeNotPrimitive(_) =>
"expected primitive type, got nonprimitive",
ReallocatedWrongMemoryKind(_, _) =>
"tried to reallocate memory from one kind to another",
DeallocatedWrongMemoryKind(_, _) =>
"tried to deallocate memory of the wrong kind",
ReallocateNonBasePtr =>
"tried to reallocate with a pointer not to the beginning of an existing object",
DeallocateNonBasePtr =>
"tried to deallocate with a pointer not to the beginning of an existing object",
IncorrectAllocationInformation =>
"tried to deallocate or reallocate using incorrect alignment or size",
Layout(_) =>
"rustc layout computation failed",
UnterminatedCString(_) =>
"attempted to get length of a null terminated string, but no null found before end of allocation",
HeapAllocZeroBytes =>
2017-06-23 12:55:49 +02:00
"tried to re-, de- or allocate zero bytes on the heap",
HeapAllocNonPowerOfTwoAlignment(_) =>
2017-06-23 12:55:49 +02:00
"tried to re-, de-, or allocate heap memory with alignment that is not a power of two",
Unreachable =>
"entered unreachable code",
Panic =>
"the evaluated program panicked",
NeedsRfc(_) =>
"this feature needs an rfc before being allowed inside constants",
NotConst(_) =>
"this feature is not compatible with constant evaluation",
ReadFromReturnPointer =>
"tried to read from the return pointer",
EvalError::PathNotFound(_) =>
"a path could not be resolved, maybe the crate is not loaded",
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 {
use EvalError::*;
2016-05-31 12:05:25 +02:00
match *self {
PointerOutOfBounds { ptr, access, allocation_size } => {
write!(f, "{} at offset {}, outside bounds of allocation {} which has size {}",
if access { "memory access" } else { "pointer computed" },
ptr.offset, ptr.alloc_id, allocation_size)
2016-06-01 11:22:37 +02:00
},
MemoryLockViolation { ptr, len, access, lock } => {
write!(f, "{:?} access at {:?}, size {}, is in conflict with lock {:?}",
access, ptr, len, lock)
}
NoMirFor(ref func) => write!(f, "no mir for `{}`", func),
FunctionPointerTyMismatch(sig, got) =>
write!(f, "tried to call a function with sig {} through a function pointer of type {}", sig, got),
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),
ReallocatedWrongMemoryKind(old, new) =>
write!(f, "tried to reallocate memory from {:?} to {:?}", old, new),
DeallocatedWrongMemoryKind(old, new) =>
write!(f, "tried to deallocate {:?} memory but gave {:?} as the kind", old, new),
Math(span, ref err) =>
2016-06-21 09:43:27 +02:00
write!(f, "{:?} at {:?}", err, span),
Intrinsic(ref err) =>
2017-06-22 00:08:19 -07:00
write!(f, "{}", err),
InvalidChar(c) =>
2016-06-21 09:43:27 +02:00
write!(f, "tried to interpret an invalid 32-bit value as a char: {}", c),
OutOfMemory { allocation_size, memory_size, memory_usage } =>
2016-07-05 10:47:10 +02:00
write!(f, "tried to allocate {} more bytes, but only {} bytes are free of the {} byte memory",
allocation_size, memory_size - memory_usage, memory_size),
AlignmentCheckFailed { required, has } =>
write!(f, "tried to access memory with alignment {}, but alignment {} is required",
has, required),
TypeNotPrimitive(ty) =>
write!(f, "expected primitive type, got {}", ty),
Layout(ref err) =>
write!(f, "rustc layout computation failed: {:?}", err),
NeedsRfc(ref msg) =>
write!(f, "\"{}\" needs an rfc before being allowed inside constants", msg),
NotConst(ref msg) =>
write!(f, "Cannot evaluate within constants: \"{}\"", msg),
EvalError::PathNotFound(ref path) =>
write!(f, "Cannot find path {:?}", path),
2016-05-31 12:05:25 +02:00
_ => write!(f, "{}", self.description()),
}
2016-03-14 21:48:00 -06:00
}
}