More descriptive interpreter errors

This commit is contained in:
Tomusdrw 2016-01-14 01:31:45 +01:00
parent dedf340381
commit 7e5de5f5c7
5 changed files with 25 additions and 9 deletions

View File

@ -19,9 +19,17 @@ pub enum Error {
BadInstruction, BadInstruction,
/// `StackUnderflow` when there is not enough stack elements to execute instruction /// `StackUnderflow` when there is not enough stack elements to execute instruction
/// First parameter says how many elements were needed and the second how many were actually on Stack /// First parameter says how many elements were needed and the second how many were actually on Stack
StackUnderflow(/*wanted*/usize, /*on_stack*/usize), StackUnderflow {
instruction: &'static str,
wanted: usize,
on_stack: usize
},
/// When execution would exceed defined Stack Limit /// When execution would exceed defined Stack Limit
OutOfStack(/*wanted*/usize, /*stack_limit*/usize), OutOfStack {
instruction: &'static str,
wanted: usize,
limit: usize
},
/// Returned on evm internal error. Should never be ignored during development. /// Returned on evm internal error. Should never be ignored during development.
/// Likely to cause consensus issues. /// Likely to cause consensus issues.
Internal, Internal,

View File

@ -15,7 +15,7 @@ impl Factory {
/// Returns native rust evm /// Returns native rust evm
#[cfg(not(feature = "jit"))] #[cfg(not(feature = "jit"))]
pub fn create() -> Box<Evm> { pub fn create() -> Box<Evm> {
Box::new(super::interpreter::Interpreter::new()) Box::new(super::interpreter::Interpreter)
} }
} }

View File

@ -626,9 +626,17 @@ impl Interpreter {
stack_limit: usize, stack_limit: usize,
stack: &Stack<U256>) -> Result<(), evm::Error> { stack: &Stack<U256>) -> Result<(), evm::Error> {
if !stack.has(info.args) { if !stack.has(info.args) {
Err(evm::Error::StackUnderflow(info.args, stack.size())) Err(evm::Error::StackUnderflow {
instruction: info.name,
wanted: info.args,
on_stack: stack.size()
})
} else if stack.size() - info.args + info.ret > stack_limit { } else if stack.size() - info.args + info.ret > stack_limit {
Err(evm::Error::OutOfStack(info.ret - info.args, stack_limit)) Err(evm::Error::OutOfStack {
instruction: info.name,
wanted: info.ret - info.args,
limit: stack_limit
})
} else { } else {
Ok(()) Ok(())
} }

View File

@ -112,9 +112,9 @@ fn test_stack_underflow() {
}; };
match err { match err {
evm::Error::StackUnderflow(wanted, stack) => { evm::Error::StackUnderflow {instruction: _, wanted, on_stack} => {
assert_eq!(wanted, 2); assert_eq!(wanted, 2);
assert_eq!(stack, 0); assert_eq!(on_stack, 0);
} }
_ => { _ => {
assert!(false, "Expected StackUndeflow") assert!(false, "Expected StackUndeflow")

View File

@ -221,8 +221,8 @@ impl<'a> Executive<'a> {
Err(evm::Error::OutOfGas) Err(evm::Error::OutOfGas)
| Err(evm::Error::BadJumpDestination) | Err(evm::Error::BadJumpDestination)
| Err(evm::Error::BadInstruction) | Err(evm::Error::BadInstruction)
| Err(evm::Error::StackUnderflow(_, _)) | Err(evm::Error::StackUnderflow {instruction: _, wanted: _, on_stack: _})
| Err(evm::Error::OutOfStack(_, _)) => { | Err(evm::Error::OutOfStack {instruction: _, wanted: _, limit: _}) => {
*self.state = backup; *self.state = backup;
Ok(Executed { Ok(Executed {
gas: t.gas, gas: t.gas,