rust/src/error.rs

61 lines
2 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-03-14 21:48:00 -06:00
#[derive(Clone, Debug)]
pub enum EvalError {
DanglingPointerDeref,
InvalidBool,
InvalidDiscriminant,
2016-05-31 12:05:25 +02:00
PointerOutOfBounds {
offset: usize,
size: usize,
len: usize,
},
ReadPointerAsBytes,
ReadBytesAsPointer,
InvalidPointerMath,
ReadUndefBytes,
2016-05-30 15:27:52 +02:00
InvalidBoolOp(mir::BinOp),
Unimplemented(String),
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",
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,
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 {
EvalError::PointerOutOfBounds { offset, size, len } => write!(f, "pointer offset ({} + {}) outside bounds ({}) of allocation", offset, size, len),
_ => write!(f, "{}", self.description()),
}
2016-03-14 21:48:00 -06:00
}
}