1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
// Copyright 2015-2018 Parity Technologies (UK) Ltd.
// This file is part of Parity.
// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
// Copyright 2019 Conflux Foundation. All rights reserved.
// Conflux is free software and distributed under GNU General Public License.
// See http://www.gnu.org/licenses/
//! VM errors module
use super::{action_params::ActionParams, ResumeCall, ResumeCreate};
use cfx_db_errors::statedb::{Error as DbError, Result as DbResult};
use cfx_types::{Address, U256};
use solidity_abi::ABIDecodeError;
use std::fmt;
#[derive(Debug, Clone)]
pub enum TrapKind {
Call(ActionParams),
Create(ActionParams),
}
pub enum TrapError<Call, Create> {
Call(ActionParams, Call),
Create(ActionParams, Create),
}
/// VM errors.
#[derive(Debug, PartialEq)]
pub enum Error {
/// `OutOfGas` is returned when transaction execution runs out of gas.
/// The state should be reverted to the state from before the
/// transaction execution. But it does not mean that transaction
/// was invalid. Balance still should be transfered and nonce
/// should be increased.
OutOfGas,
/// `BadJumpDestination` is returned when execution tried to move
/// to position that wasn't marked with JUMPDEST instruction
BadJumpDestination {
/// Position the code tried to jump to.
destination: usize,
},
/// `BadInstructions` is returned when given instruction is not supported
BadInstruction {
/// Unrecognized opcode
instruction: u8,
},
/// `StackUnderflow` when there is not enough stack elements to execute
/// instruction
StackUnderflow {
/// Invoked instruction
instruction: &'static str,
/// How many stack elements was requested by instruction
wanted: usize,
/// How many elements were on stack
on_stack: usize,
},
/// When execution would exceed defined Stack Limit
OutOfStack {
/// Invoked instruction
instruction: &'static str,
/// How many stack elements instruction wanted to push
wanted: usize,
/// What was the stack limit
limit: usize,
},
/// `SubStackUnderflow` when there is not enough stack elements to execute
/// a subroutine return
SubStackUnderflow {
/// How many stack elements was requested by instruction
wanted: usize,
/// How many elements were on stack
on_stack: usize,
},
/// When execution would exceed defined subroutine Stack Limit
OutOfSubStack {
/// How many stack elements instruction wanted to pop
wanted: usize,
/// What was the stack limit
limit: usize,
},
InvalidSubEntry,
/// When balance is not enough for `collateral_for_storage`.
/// The state should be reverted to the state from before the
/// transaction execution.
NotEnoughBalanceForStorage {
required: U256,
got: U256,
},
/// `ExceedStorageLimit` is returned when the `collateral_for_storage`
/// exceed the `storage_limit`.
ExceedStorageLimit,
/// Built-in contract failed on given input
BuiltIn(&'static str),
/// Internal contract failed
InternalContract(String),
/// When execution tries to modify the state in static context
MutableCallInStaticContext,
/// Error from storage.
StateDbError(PartialEqWrapper<DbError>),
/// Wasm runtime error
Wasm(String),
/// Out of bounds access in RETURNDATACOPY.
OutOfBounds,
/// Execution has been reverted with REVERT.
Reverted,
/// Invalid address
InvalidAddress(Address),
/// Create a contract on an address with existing contract
ConflictAddress(Address),
}
#[derive(Debug)]
pub struct PartialEqWrapper<T: std::fmt::Debug>(pub T);
impl<T: std::fmt::Debug> PartialEq for PartialEqWrapper<T> {
fn eq(&self, other: &Self) -> bool {
format!("{:?}", self.0) == format!("{:?}", other.0)
}
}
impl From<DbError> for Error {
fn from(err: DbError) -> Self { Error::StateDbError(PartialEqWrapper(err)) }
}
impl From<ABIDecodeError> for Error {
fn from(err: ABIDecodeError) -> Self {
Error::InternalContract(format!("ABI decode error: {}", err.0))
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use self::Error::*;
match *self {
OutOfGas => write!(f, "Out of gas"),
BadJumpDestination { destination } => {
write!(f, "Bad jump destination {:x}", destination)
}
BadInstruction { instruction } => {
write!(f, "Bad instruction {:x}", instruction)
}
StackUnderflow {
instruction,
wanted,
on_stack,
} => write!(
f,
"Stack underflow {} {}/{}",
instruction, wanted, on_stack
),
OutOfStack {
instruction,
wanted,
limit,
} => write!(f, "Out of stack {} {}/{}", instruction, wanted, limit),
SubStackUnderflow { wanted, on_stack } => {
write!(f, "Subroutine stack underflow {}/{}", wanted, on_stack)
}
InvalidSubEntry => {
write!(f, "Invalid Subroutine Entry via BEGINSUB")
}
OutOfSubStack { wanted, limit } => {
write!(f, "Out of subroutine stack {}/{}", wanted, limit)
}
NotEnoughBalanceForStorage { required, got } => {
write!(f, "Not enough balance for storage {}/{}", required, got,)
}
ExceedStorageLimit => write!(f, "Exceed storage limit"),
BuiltIn(name) => write!(f, "Built-in failed: {}", name),
InternalContract(ref name) => {
write!(f, "InternalContract failed: {}", name)
}
StateDbError(ref msg) => {
write!(f, "Irrecoverable state db error: {}", msg.0)
}
MutableCallInStaticContext => {
write!(f, "Mutable call in static context")
}
Wasm(ref msg) => write!(f, "Internal error: {}", msg),
OutOfBounds => write!(f, "Out of bounds"),
Reverted => write!(f, "Reverted by bytecode"),
InvalidAddress(ref addr) => write!(f, "InvalidAddress: {}", addr),
ConflictAddress(ref addr) => {
write!(f, "Contract creation on an existing address: {}", addr)
}
}
}
}
pub type Result<T> = ::std::result::Result<T, Error>;
/// Separate out database-related errors from other EVM errors.
/// The EVM itself does not distinguish between errors that originate from
/// within (e.g., index out-of-bounds) and those that come from external
/// sources like database operations.
pub fn separate_out_db_error<T>(result: Result<T>) -> DbResult<Result<T>> {
match result {
Err(Error::StateDbError(err)) => Err(err.0),
x => Ok(x),
}
}
pub enum TrapResult<T, Call, Create> {
Return(Result<T>),
SubCallCreate(TrapError<Call, Create>),
}
impl<T, Call, Create> TrapResult<T, Call, Create> {
#[cfg(any(test, feature = "testonly_code"))]
pub fn ok(self) -> Option<Result<T>> {
if let TrapResult::Return(result) = self {
Some(result)
} else {
None
}
}
}
pub type ExecTrapResult<T> =
TrapResult<T, Box<dyn ResumeCall>, Box<dyn ResumeCreate>>;
pub type ExecTrapError = TrapError<Box<dyn ResumeCall>, Box<dyn ResumeCreate>>;