rust/src/error.rs

74 lines
2.5 KiB
Rust
Raw Normal View History

2016-03-14 21:48:00 -06:00
use std::error::Error;
use std::fmt;
2016-05-30 15:27:52 +02:00
use rustc::mir::repr as mir;
2016-06-01 11:22:37 +02:00
use memory::Pointer;
2016-03-14 21:48:00 -06:00
#[derive(Clone, Debug)]
pub enum EvalError {
DanglingPointerDeref,
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,
2016-05-31 12:05:25 +02:00
size: usize,
2016-06-01 11:22:37 +02:00
allocation_size: usize,
2016-05-31 12:05:25 +02:00
},
ReadPointerAsBytes,
ReadBytesAsPointer,
InvalidPointerMath,
ReadUndefBytes,
2016-05-30 15:27:52 +02:00
InvalidBoolOp(mir::BinOp),
Unimplemented(String),
DerefFunctionPointer,
ExecuteMemory,
2016-03-14 21:48:00 -06:00
}
pub type EvalResult<T> = Result<T, EvalError>;
impl Error for EvalError {
fn description(&self) -> &str {
match *self {
EvalError::DanglingPointerDeref =>
"dangling pointer was dereferenced",
2016-06-08 13:43:34 +02:00
EvalError::InvalidFunctionPointer =>
"tried to use a 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::ReadBytesAsPointer =>
"attempted to interpret some raw bytes as a pointer address",
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-03-14 21:48:00 -06:00
}
}
fn cause(&self) -> Option<&Error> { None }
}
impl fmt::Display for EvalError {
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-05-31 12:05:25 +02:00
_ => write!(f, "{}", self.description()),
}
2016-03-14 21:48:00 -06:00
}
}