2016-02-05 13:40:41 +01:00
|
|
|
// Copyright 2015, 2016 Ethcore (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/>.
|
|
|
|
|
2016-01-13 00:13:09 +01:00
|
|
|
///! Rust VM implementation
|
|
|
|
|
|
|
|
use common::*;
|
2016-06-02 12:40:31 +02:00
|
|
|
use trace::VMTracer;
|
2016-01-13 00:13:09 +01:00
|
|
|
use super::instructions as instructions;
|
2016-06-02 12:40:31 +02:00
|
|
|
use super::instructions::{Instruction, get_info};
|
2016-01-16 16:11:30 +01:00
|
|
|
use std::marker::Copy;
|
2016-02-02 18:02:58 +01:00
|
|
|
use evm::{self, MessageCallResult, ContractCreateResult};
|
2016-01-16 20:11:12 +01:00
|
|
|
|
2016-01-21 16:08:09 +01:00
|
|
|
#[cfg(not(feature = "evm-debug"))]
|
2016-01-16 20:11:12 +01:00
|
|
|
macro_rules! evm_debug {
|
|
|
|
($x: expr) => {}
|
|
|
|
}
|
2016-01-13 00:13:09 +01:00
|
|
|
|
2016-01-21 16:08:09 +01:00
|
|
|
#[cfg(feature = "evm-debug")]
|
2016-01-16 13:59:48 +01:00
|
|
|
macro_rules! evm_debug {
|
|
|
|
($x: expr) => {
|
2016-01-16 20:11:12 +01:00
|
|
|
$x
|
2016-01-16 13:59:48 +01:00
|
|
|
}
|
|
|
|
}
|
2016-01-16 17:08:57 +01:00
|
|
|
|
2016-01-21 16:08:09 +01:00
|
|
|
#[cfg(feature = "evm-debug")]
|
2016-01-16 13:59:48 +01:00
|
|
|
fn color(instruction: Instruction, name: &'static str) -> String {
|
|
|
|
let c = instruction as usize % 6;
|
|
|
|
let colors = [31, 34, 33, 32, 35, 36];
|
|
|
|
format!("\x1B[1;{}m{}\x1B[0m", colors[c], name)
|
|
|
|
}
|
|
|
|
|
2016-01-17 11:54:29 +01:00
|
|
|
macro_rules! overflowing {
|
|
|
|
($x: expr) => {{
|
|
|
|
let (v, overflow) = $x;
|
|
|
|
if overflow { return Err(evm::Error::OutOfGas); }
|
|
|
|
v
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
2016-01-13 00:13:09 +01:00
|
|
|
type CodePosition = usize;
|
|
|
|
type Gas = U256;
|
|
|
|
type ProgramCounter = usize;
|
|
|
|
|
|
|
|
/// Stack trait with VM-friendly API
|
|
|
|
trait Stack<T> {
|
|
|
|
/// Returns `Stack[len(Stack) - no_from_top]`
|
|
|
|
fn peek(&self, no_from_top: usize) -> &T;
|
|
|
|
/// Swaps Stack[len(Stack)] and Stack[len(Stack) - no_from_top]
|
|
|
|
fn swap_with_top(&mut self, no_from_top: usize);
|
|
|
|
/// Returns true if Stack has at least `no_of_elems` elements
|
|
|
|
fn has(&self, no_of_elems: usize) -> bool;
|
|
|
|
/// Get element from top and remove it from Stack. Panics if stack is empty.
|
|
|
|
fn pop_back(&mut self) -> T;
|
2016-01-16 17:24:42 +01:00
|
|
|
/// Get (up to `instructions::MAX_NO_OF_TOPICS`) elements from top and remove them from Stack. Panics if stack is empty.
|
2016-01-16 16:11:30 +01:00
|
|
|
fn pop_n(&mut self, no_of_elems: usize) -> &[T];
|
2016-01-13 00:13:09 +01:00
|
|
|
/// Add element on top of the Stack
|
|
|
|
fn push(&mut self, elem: T);
|
2016-01-13 15:21:13 +01:00
|
|
|
/// Get number of elements on Stack
|
|
|
|
fn size(&self) -> usize;
|
2016-06-02 12:40:31 +02:00
|
|
|
/// Returns all data on stack.
|
|
|
|
fn peek_top(&mut self, no_of_elems: usize) -> &[T];
|
2016-01-13 00:13:09 +01:00
|
|
|
}
|
2016-01-16 16:11:30 +01:00
|
|
|
|
|
|
|
struct VecStack<S> {
|
|
|
|
stack: Vec<S>,
|
2016-01-16 17:24:42 +01:00
|
|
|
logs: [S; instructions::MAX_NO_OF_TOPICS]
|
2016-01-16 16:11:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<S : Copy> VecStack<S> {
|
|
|
|
fn with_capacity(capacity: usize, zero: S) -> Self {
|
|
|
|
VecStack {
|
|
|
|
stack: Vec::with_capacity(capacity),
|
2016-01-16 17:24:42 +01:00
|
|
|
logs: [zero; instructions::MAX_NO_OF_TOPICS]
|
2016-01-16 16:11:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-16 17:27:54 +01:00
|
|
|
impl<S : fmt::Display> Stack<S> for VecStack<S> {
|
2016-01-13 00:13:09 +01:00
|
|
|
fn peek(&self, no_from_top: usize) -> &S {
|
2016-01-17 15:56:09 +01:00
|
|
|
&self.stack[self.stack.len() - no_from_top - 1]
|
2016-01-13 00:13:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn swap_with_top(&mut self, no_from_top: usize) {
|
2016-01-16 16:11:30 +01:00
|
|
|
let len = self.stack.len();
|
|
|
|
self.stack.swap(len - no_from_top - 1, len - 1);
|
2016-01-13 00:13:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn has(&self, no_of_elems: usize) -> bool {
|
2016-01-16 16:11:30 +01:00
|
|
|
self.stack.len() >= no_of_elems
|
2016-01-13 00:13:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn pop_back(&mut self) -> S {
|
2016-01-16 16:11:30 +01:00
|
|
|
let val = self.stack.pop();
|
2016-01-13 00:13:09 +01:00
|
|
|
match val {
|
2016-01-15 22:46:29 +01:00
|
|
|
Some(x) => {
|
2016-01-16 13:59:48 +01:00
|
|
|
evm_debug!({
|
2016-01-16 20:11:12 +01:00
|
|
|
println!(" POP: {}", x)
|
2016-01-16 13:59:48 +01:00
|
|
|
});
|
2016-01-15 22:46:29 +01:00
|
|
|
x
|
|
|
|
},
|
2016-01-13 00:13:09 +01:00
|
|
|
None => panic!("Tried to pop from empty stack.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-16 16:11:30 +01:00
|
|
|
fn pop_n(&mut self, no_of_elems: usize) -> &[S] {
|
2016-01-16 17:24:42 +01:00
|
|
|
assert!(no_of_elems <= instructions::MAX_NO_OF_TOPICS);
|
2016-01-14 11:00:29 +01:00
|
|
|
|
2016-01-16 16:11:30 +01:00
|
|
|
for i in 0..no_of_elems {
|
|
|
|
self.logs[i] = self.pop_back();
|
2016-01-13 00:13:09 +01:00
|
|
|
}
|
2016-01-16 16:11:30 +01:00
|
|
|
&self.logs[0..no_of_elems]
|
2016-01-13 00:13:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn push(&mut self, elem: S) {
|
2016-01-16 13:59:48 +01:00
|
|
|
evm_debug!({
|
2016-01-16 20:11:12 +01:00
|
|
|
println!(" PUSH: {}", elem)
|
2016-01-16 13:59:48 +01:00
|
|
|
});
|
2016-01-16 16:11:30 +01:00
|
|
|
self.stack.push(elem);
|
2016-01-13 00:13:09 +01:00
|
|
|
}
|
2016-01-13 15:21:13 +01:00
|
|
|
|
|
|
|
fn size(&self) -> usize {
|
2016-01-16 16:11:30 +01:00
|
|
|
self.stack.len()
|
2016-01-13 15:21:13 +01:00
|
|
|
}
|
2016-06-02 12:40:31 +02:00
|
|
|
|
|
|
|
fn peek_top(&mut self, no_from_top: usize) -> &[S] {
|
|
|
|
assert!(self.stack.len() >= no_from_top, "peek_top asked for more items than exist.");
|
|
|
|
&self.stack[self.stack.len() - no_from_top .. self.stack.len()]
|
|
|
|
}
|
2016-01-13 00:13:09 +01:00
|
|
|
}
|
2016-01-13 22:30:41 +01:00
|
|
|
|
|
|
|
trait Memory {
|
|
|
|
/// Retrieve current size of the memory
|
|
|
|
fn size(&self) -> usize;
|
|
|
|
/// Resize (shrink or expand) the memory to specified size (fills 0)
|
|
|
|
fn resize(&mut self, new_size: usize);
|
2016-01-13 23:51:10 +01:00
|
|
|
/// Resize the memory only if its smaller
|
2016-01-14 14:49:41 +01:00
|
|
|
fn expand(&mut self, new_size: usize);
|
2016-01-13 22:30:41 +01:00
|
|
|
/// Write single byte to memory
|
|
|
|
fn write_byte(&mut self, offset: U256, value: U256);
|
2016-01-13 23:51:10 +01:00
|
|
|
/// Write a word to memory. Does not resize memory!
|
2016-01-13 22:30:41 +01:00
|
|
|
fn write(&mut self, offset: U256, value: U256);
|
|
|
|
/// Read a word from memory
|
2016-01-13 23:51:10 +01:00
|
|
|
fn read(&self, offset: U256) -> U256;
|
|
|
|
/// Write slice of bytes to memory. Does not resize memory!
|
2016-01-13 22:30:41 +01:00
|
|
|
fn write_slice(&mut self, offset: U256, &[u8]);
|
|
|
|
/// Retrieve part of the memory between offset and offset + size
|
|
|
|
fn read_slice(&self, offset: U256, size: U256) -> &[u8];
|
2016-01-14 00:39:59 +01:00
|
|
|
/// Retrieve writeable part of memory
|
|
|
|
fn writeable_slice(&mut self, offset: U256, size: U256) -> &mut[u8];
|
2016-01-15 22:46:29 +01:00
|
|
|
fn dump(&self);
|
2016-01-13 22:30:41 +01:00
|
|
|
}
|
2016-01-16 01:24:37 +01:00
|
|
|
|
2016-01-16 17:08:57 +01:00
|
|
|
/// Checks whether offset and size is valid memory range
|
2016-01-16 01:24:37 +01:00
|
|
|
fn is_valid_range(off: usize, size: usize) -> bool {
|
|
|
|
// When size is zero we haven't actually expanded the memory
|
2016-01-17 13:24:57 +01:00
|
|
|
let overflow = off.overflowing_add(size).1;
|
2016-01-16 01:24:37 +01:00
|
|
|
size > 0 && !overflow
|
|
|
|
}
|
|
|
|
|
2016-01-13 22:30:41 +01:00
|
|
|
impl Memory for Vec<u8> {
|
2016-01-15 22:46:29 +01:00
|
|
|
fn dump(&self) {
|
|
|
|
println!("MemoryDump:");
|
|
|
|
for i in self.iter() {
|
2016-01-16 14:14:35 +01:00
|
|
|
println!("{:02x} ", i);
|
2016-01-15 22:46:29 +01:00
|
|
|
}
|
|
|
|
println!("");
|
|
|
|
}
|
|
|
|
|
2016-01-13 22:30:41 +01:00
|
|
|
fn size(&self) -> usize {
|
2016-01-17 15:56:09 +01:00
|
|
|
self.len()
|
2016-01-13 22:30:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn read_slice(&self, init_off_u: U256, init_size_u: U256) -> &[u8] {
|
2016-01-16 01:24:37 +01:00
|
|
|
let off = init_off_u.low_u64() as usize;
|
|
|
|
let size = init_size_u.low_u64() as usize;
|
|
|
|
if !is_valid_range(off, size) {
|
2016-01-16 00:31:04 +01:00
|
|
|
&self[0..0]
|
|
|
|
} else {
|
2016-01-16 01:24:37 +01:00
|
|
|
&self[off..off+size]
|
2016-01-16 00:31:04 +01:00
|
|
|
}
|
2016-01-13 22:30:41 +01:00
|
|
|
}
|
|
|
|
|
2016-01-13 23:51:10 +01:00
|
|
|
fn read(&self, offset: U256) -> U256 {
|
2016-01-13 22:30:41 +01:00
|
|
|
let off = offset.low_u64() as usize;
|
2016-01-14 00:46:24 +01:00
|
|
|
U256::from(&self[off..off+32])
|
2016-01-13 22:30:41 +01:00
|
|
|
}
|
|
|
|
|
2016-01-14 00:39:59 +01:00
|
|
|
fn writeable_slice(&mut self, offset: U256, size: U256) -> &mut [u8] {
|
|
|
|
let off = offset.low_u64() as usize;
|
|
|
|
let s = size.low_u64() as usize;
|
2016-01-16 01:24:37 +01:00
|
|
|
if !is_valid_range(off, s) {
|
2016-01-16 00:31:04 +01:00
|
|
|
&mut self[0..0]
|
|
|
|
} else {
|
|
|
|
&mut self[off..off+s]
|
|
|
|
}
|
2016-01-14 00:39:59 +01:00
|
|
|
}
|
|
|
|
|
2016-01-13 22:30:41 +01:00
|
|
|
fn write_slice(&mut self, offset: U256, slice: &[u8]) {
|
|
|
|
let off = offset.low_u64() as usize;
|
2016-01-13 23:51:10 +01:00
|
|
|
|
2016-01-13 22:30:41 +01:00
|
|
|
// TODO [todr] Optimize?
|
|
|
|
for pos in off..off+slice.len() {
|
|
|
|
self[pos] = slice[pos - off];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn write(&mut self, offset: U256, value: U256) {
|
|
|
|
let off = offset.low_u64() as usize;
|
2016-01-13 23:51:10 +01:00
|
|
|
let mut val = value;
|
2016-02-15 00:51:50 +01:00
|
|
|
|
2016-01-13 23:51:10 +01:00
|
|
|
let end = off + 32;
|
2016-01-15 22:46:29 +01:00
|
|
|
for pos in 0..32 {
|
2016-01-14 00:46:24 +01:00
|
|
|
self[end - pos - 1] = val.low_u64() as u8;
|
2016-01-13 23:51:10 +01:00
|
|
|
val = val >> 8;
|
|
|
|
}
|
2016-01-13 22:30:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn write_byte(&mut self, offset: U256, value: U256) {
|
|
|
|
let off = offset.low_u64() as usize;
|
|
|
|
let val = value.low_u64() as u64;
|
2016-01-14 00:46:24 +01:00
|
|
|
self[off] = val as u8;
|
2016-01-13 22:30:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn resize(&mut self, new_size: usize) {
|
|
|
|
self.resize(new_size, 0);
|
|
|
|
}
|
2016-02-15 00:51:50 +01:00
|
|
|
|
2016-01-14 14:49:41 +01:00
|
|
|
fn expand(&mut self, size: usize) {
|
2016-01-13 23:51:10 +01:00
|
|
|
if size > self.len() {
|
|
|
|
Memory::resize(self, size)
|
|
|
|
}
|
|
|
|
}
|
2016-01-13 22:30:41 +01:00
|
|
|
}
|
|
|
|
|
2016-01-13 00:13:09 +01:00
|
|
|
/// Abstraction over raw vector of Bytes. Easier state management of PC.
|
|
|
|
struct CodeReader<'a> {
|
|
|
|
position: ProgramCounter,
|
|
|
|
code: &'a Bytes
|
|
|
|
}
|
2016-01-16 17:08:57 +01:00
|
|
|
|
2016-03-11 11:16:49 +01:00
|
|
|
#[cfg_attr(feature="dev", allow(len_without_is_empty))]
|
2016-01-13 00:13:09 +01:00
|
|
|
impl<'a> CodeReader<'a> {
|
|
|
|
/// Get `no_of_bytes` from code and convert to U256. Move PC
|
|
|
|
fn read(&mut self, no_of_bytes: usize) -> U256 {
|
2016-01-14 01:05:01 +01:00
|
|
|
let pos = self.position;
|
2016-01-13 00:13:09 +01:00
|
|
|
self.position += no_of_bytes;
|
2016-01-15 22:46:29 +01:00
|
|
|
let max = cmp::min(pos + no_of_bytes, self.code.len());
|
|
|
|
U256::from(&self.code[pos..max])
|
2016-01-13 00:13:09 +01:00
|
|
|
}
|
|
|
|
|
2016-01-13 01:53:33 +01:00
|
|
|
fn len (&self) -> usize {
|
|
|
|
self.code.len()
|
|
|
|
}
|
2016-01-13 15:21:13 +01:00
|
|
|
}
|
2016-01-13 00:13:09 +01:00
|
|
|
|
2016-03-11 11:16:49 +01:00
|
|
|
#[cfg_attr(feature="dev", allow(enum_variant_names))]
|
2016-01-13 17:11:00 +01:00
|
|
|
enum InstructionCost {
|
|
|
|
Gas(U256),
|
2016-01-17 14:17:05 +01:00
|
|
|
GasMem(U256, U256),
|
|
|
|
GasMemCopy(U256, U256, U256)
|
2016-01-13 17:11:00 +01:00
|
|
|
}
|
2016-01-15 21:46:08 +01:00
|
|
|
|
2016-01-13 15:21:13 +01:00
|
|
|
enum InstructionResult {
|
2016-01-16 20:11:12 +01:00
|
|
|
Ok,
|
|
|
|
UseAllGas,
|
2016-01-16 21:24:19 +01:00
|
|
|
GasLeft(U256),
|
2016-01-16 20:11:12 +01:00
|
|
|
UnusedGas(U256),
|
2016-01-13 15:21:13 +01:00
|
|
|
JumpToPosition(U256),
|
2016-01-16 21:24:19 +01:00
|
|
|
StopExecutionWithGasLeft(U256),
|
2016-01-13 15:21:13 +01:00
|
|
|
StopExecution
|
2016-01-13 00:13:09 +01:00
|
|
|
}
|
|
|
|
|
2016-01-16 17:08:57 +01:00
|
|
|
/// Intepreter EVM implementation
|
2016-01-13 00:13:09 +01:00
|
|
|
pub struct Interpreter;
|
|
|
|
|
|
|
|
impl evm::Evm for Interpreter {
|
2016-01-16 20:11:12 +01:00
|
|
|
fn exec(&self, params: ActionParams, ext: &mut evm::Ext) -> evm::Result {
|
2016-02-05 02:15:26 +01:00
|
|
|
let code = ¶ms.code.as_ref().unwrap();
|
2016-01-14 11:00:29 +01:00
|
|
|
let valid_jump_destinations = self.find_jump_destinations(&code);
|
2016-01-13 00:13:09 +01:00
|
|
|
|
2016-02-15 00:51:50 +01:00
|
|
|
let mut current_gas = params.gas;
|
2016-01-16 16:11:30 +01:00
|
|
|
let mut stack = VecStack::with_capacity(ext.schedule().stack_limit, U256::zero());
|
2016-01-14 11:00:29 +01:00
|
|
|
let mut mem = vec![];
|
2016-01-13 00:13:09 +01:00
|
|
|
let mut reader = CodeReader {
|
|
|
|
position: 0,
|
|
|
|
code: &code
|
|
|
|
};
|
|
|
|
|
2016-01-14 11:00:29 +01:00
|
|
|
while reader.position < code.len() {
|
|
|
|
let instruction = code[reader.position];
|
|
|
|
|
|
|
|
// Calculate gas cost
|
2016-01-15 23:32:16 +01:00
|
|
|
let (gas_cost, mem_size) = try!(self.get_gas_cost_mem(ext, instruction, &mut mem, &stack));
|
2016-06-02 12:40:31 +02:00
|
|
|
|
|
|
|
// TODO: make compile-time removable if too much of a performance hit.
|
|
|
|
let trace_executed = ext.trace_prepare_execute(reader.position, instruction, &gas_cost);
|
|
|
|
|
|
|
|
reader.position += 1;
|
|
|
|
|
2016-01-14 11:00:29 +01:00
|
|
|
try!(self.verify_gas(¤t_gas, &gas_cost));
|
2016-01-15 23:32:16 +01:00
|
|
|
mem.expand(mem_size);
|
2016-02-19 00:23:05 +01:00
|
|
|
current_gas = current_gas - gas_cost; //TODO: use operator -=
|
2016-01-15 23:32:16 +01:00
|
|
|
|
2016-01-16 13:59:48 +01:00
|
|
|
evm_debug!({
|
2016-01-16 20:11:12 +01:00
|
|
|
println!("[0x{:x}][{}(0x{:x}) Gas: {:x}\n Gas Before: {:x}",
|
2016-01-16 13:59:48 +01:00
|
|
|
reader.position,
|
|
|
|
color(instruction, instructions::get_info(instruction).name),
|
|
|
|
instruction,
|
|
|
|
gas_cost,
|
|
|
|
current_gas + gas_cost
|
2016-01-16 20:11:12 +01:00
|
|
|
);
|
2016-01-16 13:59:48 +01:00
|
|
|
});
|
2016-01-15 23:32:16 +01:00
|
|
|
|
2016-06-02 12:40:31 +02:00
|
|
|
let (mem_written, store_written) = match trace_executed {
|
|
|
|
true => (Self::mem_written(instruction, &stack), Self::store_written(instruction, &stack)),
|
|
|
|
false => (None, None),
|
|
|
|
};
|
|
|
|
|
2016-01-14 11:00:29 +01:00
|
|
|
// Execute instruction
|
|
|
|
let result = try!(self.exec_instruction(
|
2016-01-16 20:11:12 +01:00
|
|
|
current_gas, ¶ms, ext, instruction, &mut reader, &mut mem, &mut stack
|
2016-06-02 12:40:31 +02:00
|
|
|
));
|
|
|
|
|
|
|
|
if trace_executed {
|
|
|
|
ext.trace_executed(current_gas, stack.peek_top(get_info(instruction).ret), mem_written.map(|(o, s)| (o, &(mem[o..(o + s)]))), store_written);
|
|
|
|
}
|
2016-01-14 11:00:29 +01:00
|
|
|
|
|
|
|
// Advance
|
|
|
|
match result {
|
2016-01-16 20:11:12 +01:00
|
|
|
InstructionResult::Ok => {},
|
|
|
|
InstructionResult::UnusedGas(gas) => {
|
2016-02-19 00:23:05 +01:00
|
|
|
current_gas = current_gas + gas; //TODO: use operator +=
|
2016-01-16 20:11:12 +01:00
|
|
|
},
|
|
|
|
InstructionResult::UseAllGas => {
|
|
|
|
current_gas = U256::zero();
|
|
|
|
},
|
2016-01-16 21:24:19 +01:00
|
|
|
InstructionResult::GasLeft(gas_left) => {
|
|
|
|
current_gas = gas_left;
|
|
|
|
},
|
2016-01-14 11:00:29 +01:00
|
|
|
InstructionResult::JumpToPosition(position) => {
|
|
|
|
let pos = try!(self.verify_jump(position, &valid_jump_destinations));
|
|
|
|
reader.position = pos;
|
|
|
|
},
|
2016-02-15 00:51:50 +01:00
|
|
|
InstructionResult::StopExecutionWithGasLeft(gas_left) => {
|
2016-01-16 21:24:19 +01:00
|
|
|
current_gas = gas_left;
|
2016-01-14 11:00:29 +01:00
|
|
|
reader.position = code.len();
|
|
|
|
},
|
|
|
|
InstructionResult::StopExecution => {
|
|
|
|
reader.position = code.len();
|
|
|
|
}
|
2016-01-13 00:13:09 +01:00
|
|
|
}
|
|
|
|
}
|
2016-01-13 15:21:13 +01:00
|
|
|
|
2016-01-14 11:00:29 +01:00
|
|
|
Ok(current_gas)
|
|
|
|
}
|
2016-01-13 00:13:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Interpreter {
|
2016-03-11 11:16:49 +01:00
|
|
|
#[cfg_attr(feature="dev", allow(cyclomatic_complexity))]
|
2016-03-20 19:20:37 +01:00
|
|
|
fn get_gas_cost_mem(
|
|
|
|
&self,
|
|
|
|
ext: &evm::Ext,
|
|
|
|
instruction: Instruction,
|
|
|
|
mem: &mut Memory,
|
|
|
|
stack: &Stack<U256>
|
|
|
|
) -> Result<(U256, usize), evm::Error> {
|
2016-01-14 01:56:37 +01:00
|
|
|
let schedule = ext.schedule();
|
|
|
|
let info = instructions::get_info(instruction);
|
2016-01-13 15:21:13 +01:00
|
|
|
|
2016-01-14 01:56:37 +01:00
|
|
|
if !schedule.have_delegate_call && instruction == instructions::DELEGATECALL {
|
2016-01-14 02:36:48 +01:00
|
|
|
return Err(evm::Error::BadInstruction {
|
2016-01-14 02:45:16 +01:00
|
|
|
instruction: instruction
|
2016-01-14 02:36:48 +01:00
|
|
|
});
|
2016-01-14 01:56:37 +01:00
|
|
|
}
|
2016-01-16 17:08:57 +01:00
|
|
|
if info.tier == instructions::GasPriceTier::Invalid {
|
2016-01-14 02:36:48 +01:00
|
|
|
return Err(evm::Error::BadInstruction {
|
2016-01-14 02:45:16 +01:00
|
|
|
instruction: instruction
|
2016-01-14 02:36:48 +01:00
|
|
|
});
|
2016-01-14 01:56:37 +01:00
|
|
|
}
|
2016-01-13 15:21:13 +01:00
|
|
|
|
2016-01-14 01:56:37 +01:00
|
|
|
try!(self.verify_instructions_requirements(&info, schedule.stack_limit, stack));
|
2016-01-13 15:21:13 +01:00
|
|
|
|
2016-01-14 01:56:37 +01:00
|
|
|
let tier = instructions::get_tier_idx(info.tier);
|
|
|
|
let default_gas = U256::from(schedule.tier_step_gas[tier]);
|
2016-01-13 15:21:13 +01:00
|
|
|
|
2016-01-14 01:56:37 +01:00
|
|
|
let cost = match instruction {
|
|
|
|
instructions::SSTORE => {
|
|
|
|
let address = H256::from(stack.peek(0));
|
2016-01-14 12:33:49 +01:00
|
|
|
let newval = stack.peek(1);
|
2016-01-16 11:33:41 +01:00
|
|
|
let val = U256::from(ext.storage_at(&address).as_slice());
|
2016-01-14 12:33:49 +01:00
|
|
|
|
|
|
|
let gas = if self.is_zero(&val) && !self.is_zero(newval) {
|
2016-01-14 01:56:37 +01:00
|
|
|
schedule.sstore_set_gas
|
|
|
|
} else {
|
2016-02-15 00:51:50 +01:00
|
|
|
// Refund for below case is added when actually executing sstore
|
|
|
|
// !self.is_zero(&val) && self.is_zero(newval)
|
2016-01-14 01:56:37 +01:00
|
|
|
schedule.sstore_reset_gas
|
|
|
|
};
|
|
|
|
InstructionCost::Gas(U256::from(gas))
|
|
|
|
},
|
|
|
|
instructions::SLOAD => {
|
|
|
|
InstructionCost::Gas(U256::from(schedule.sload_gas))
|
|
|
|
},
|
2016-02-14 12:54:27 +01:00
|
|
|
instructions::MSTORE | instructions::MLOAD => {
|
2016-01-17 14:17:05 +01:00
|
|
|
InstructionCost::GasMem(default_gas, try!(self.mem_needed_const(stack.peek(0), 32)))
|
2016-01-14 01:56:37 +01:00
|
|
|
},
|
|
|
|
instructions::MSTORE8 => {
|
2016-01-17 14:17:05 +01:00
|
|
|
InstructionCost::GasMem(default_gas, try!(self.mem_needed_const(stack.peek(0), 1)))
|
2016-01-14 01:56:37 +01:00
|
|
|
},
|
|
|
|
instructions::RETURN => {
|
2016-01-17 14:17:05 +01:00
|
|
|
InstructionCost::GasMem(default_gas, try!(self.mem_needed(stack.peek(0), stack.peek(1))))
|
2016-01-14 01:56:37 +01:00
|
|
|
},
|
|
|
|
instructions::SHA3 => {
|
2016-01-17 11:54:29 +01:00
|
|
|
let w = overflowing!(add_u256_usize(stack.peek(1), 31));
|
|
|
|
let words = w >> 5;
|
|
|
|
let gas = U256::from(schedule.sha3_gas) + (U256::from(schedule.sha3_word_gas) * words);
|
2016-01-17 14:17:05 +01:00
|
|
|
InstructionCost::GasMem(gas, try!(self.mem_needed(stack.peek(0), stack.peek(1))))
|
2016-01-14 01:56:37 +01:00
|
|
|
},
|
2016-02-15 00:51:50 +01:00
|
|
|
instructions::CALLDATACOPY | instructions::CODECOPY => {
|
2016-01-17 14:17:05 +01:00
|
|
|
InstructionCost::GasMemCopy(default_gas, try!(self.mem_needed(stack.peek(0), stack.peek(2))), stack.peek(2).clone())
|
2016-01-14 01:56:37 +01:00
|
|
|
},
|
|
|
|
instructions::EXTCODECOPY => {
|
2016-01-17 14:17:05 +01:00
|
|
|
InstructionCost::GasMemCopy(default_gas, try!(self.mem_needed(stack.peek(1), stack.peek(3))), stack.peek(3).clone())
|
2016-01-14 01:56:37 +01:00
|
|
|
},
|
|
|
|
instructions::JUMPDEST => {
|
|
|
|
InstructionCost::Gas(U256::one())
|
|
|
|
},
|
|
|
|
instructions::LOG0...instructions::LOG4 => {
|
|
|
|
let no_of_topics = instructions::get_log_topics(instruction);
|
|
|
|
let log_gas = schedule.log_gas + schedule.log_topic_gas * no_of_topics;
|
2016-01-17 11:54:29 +01:00
|
|
|
|
|
|
|
let data_gas = overflowing!(stack.peek(1).overflowing_mul(U256::from(schedule.log_data_gas)));
|
|
|
|
let gas = overflowing!(data_gas.overflowing_add(U256::from(log_gas)));
|
2016-01-17 14:17:05 +01:00
|
|
|
InstructionCost::GasMem(gas, try!(self.mem_needed(stack.peek(0), stack.peek(1))))
|
2016-01-14 01:56:37 +01:00
|
|
|
},
|
|
|
|
instructions::CALL | instructions::CALLCODE => {
|
2016-01-17 11:54:29 +01:00
|
|
|
let mut gas = overflowing!(add_u256_usize(stack.peek(0), schedule.call_gas));
|
2016-01-17 14:17:05 +01:00
|
|
|
let mem = cmp::max(
|
|
|
|
try!(self.mem_needed(stack.peek(5), stack.peek(6))),
|
|
|
|
try!(self.mem_needed(stack.peek(3), stack.peek(4)))
|
2016-01-17 11:54:29 +01:00
|
|
|
);
|
2016-02-15 00:51:50 +01:00
|
|
|
|
2016-01-17 11:54:29 +01:00
|
|
|
let address = u256_to_address(stack.peek(1));
|
|
|
|
|
|
|
|
if instruction == instructions::CALL && !ext.exists(&address) {
|
|
|
|
gas = overflowing!(gas.overflowing_add(U256::from(schedule.call_new_account_gas)));
|
|
|
|
};
|
|
|
|
|
|
|
|
if stack.peek(2).clone() > U256::zero() {
|
|
|
|
gas = overflowing!(gas.overflowing_add(U256::from(schedule.call_value_transfer_gas)));
|
|
|
|
};
|
|
|
|
|
|
|
|
InstructionCost::GasMem(gas,mem)
|
2016-01-14 01:56:37 +01:00
|
|
|
},
|
|
|
|
instructions::DELEGATECALL => {
|
2016-01-17 11:54:29 +01:00
|
|
|
let gas = overflowing!(add_u256_usize(stack.peek(0), schedule.call_gas));
|
2016-01-17 14:17:05 +01:00
|
|
|
let mem = cmp::max(
|
|
|
|
try!(self.mem_needed(stack.peek(4), stack.peek(5))),
|
|
|
|
try!(self.mem_needed(stack.peek(2), stack.peek(3)))
|
2016-01-17 11:54:29 +01:00
|
|
|
);
|
|
|
|
InstructionCost::GasMem(gas, mem)
|
2016-01-14 01:56:37 +01:00
|
|
|
},
|
|
|
|
instructions::CREATE => {
|
|
|
|
let gas = U256::from(schedule.create_gas);
|
2016-01-17 14:17:05 +01:00
|
|
|
let mem = try!(self.mem_needed(stack.peek(1), stack.peek(2)));
|
2016-01-14 01:56:37 +01:00
|
|
|
InstructionCost::GasMem(gas, mem)
|
|
|
|
},
|
|
|
|
instructions::EXP => {
|
|
|
|
let expon = stack.peek(1);
|
2016-01-15 19:15:37 +01:00
|
|
|
let bytes = ((expon.bits() + 7) / 8) as usize;
|
|
|
|
let gas = U256::from(schedule.exp_gas + schedule.exp_byte_gas * bytes);
|
2016-01-14 01:56:37 +01:00
|
|
|
InstructionCost::Gas(gas)
|
|
|
|
},
|
|
|
|
_ => InstructionCost::Gas(default_gas)
|
|
|
|
};
|
|
|
|
|
|
|
|
match cost {
|
|
|
|
InstructionCost::Gas(gas) => {
|
2016-01-15 23:32:16 +01:00
|
|
|
Ok((gas, 0))
|
2016-01-14 01:56:37 +01:00
|
|
|
},
|
2016-01-17 14:17:05 +01:00
|
|
|
InstructionCost::GasMem(gas, mem_size) => {
|
|
|
|
let (mem_gas, new_mem_size) = try!(self.mem_gas_cost(schedule, mem.size(), &mem_size));
|
|
|
|
let gas = overflowing!(gas.overflowing_add(mem_gas));
|
|
|
|
Ok((gas, new_mem_size))
|
2016-01-14 01:56:37 +01:00
|
|
|
},
|
2016-01-17 14:17:05 +01:00
|
|
|
InstructionCost::GasMemCopy(gas, mem_size, copy) => {
|
|
|
|
let (mem_gas, new_mem_size) = try!(self.mem_gas_cost(schedule, mem.size(), &mem_size));
|
|
|
|
let copy = overflowing!(add_u256_usize(©, 31));
|
|
|
|
let copy_gas = U256::from(schedule.copy_gas) * (copy / U256::from(32));
|
|
|
|
let gas = overflowing!(gas.overflowing_add(copy_gas));
|
|
|
|
let gas = overflowing!(gas.overflowing_add(mem_gas));
|
|
|
|
Ok((gas, new_mem_size))
|
2016-01-13 17:11:00 +01:00
|
|
|
}
|
2016-01-14 01:56:37 +01:00
|
|
|
}
|
2016-01-13 15:21:13 +01:00
|
|
|
}
|
2016-01-15 21:46:08 +01:00
|
|
|
|
2016-06-02 12:40:31 +02:00
|
|
|
fn mem_written(
|
|
|
|
instruction: Instruction,
|
|
|
|
stack: &Stack<U256>
|
|
|
|
) -> Option<(usize, usize)> {
|
|
|
|
match instruction {
|
|
|
|
instructions::MSTORE | instructions::MLOAD => Some((stack.peek(0).low_u64() as usize, 32)),
|
|
|
|
instructions::MSTORE8 => Some((stack.peek(0).low_u64() as usize, 1)),
|
|
|
|
instructions::CALLDATACOPY | instructions::CODECOPY => Some((stack.peek(0).low_u64() as usize, stack.peek(2).low_u64() as usize)),
|
|
|
|
instructions::EXTCODECOPY => Some((stack.peek(1).low_u64() as usize, stack.peek(3).low_u64() as usize)),
|
|
|
|
instructions::CALL | instructions::CALLCODE => Some((stack.peek(5).low_u64() as usize, stack.peek(6).low_u64() as usize)),
|
|
|
|
instructions::DELEGATECALL => Some((stack.peek(4).low_u64() as usize, stack.peek(5).low_u64() as usize)),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn store_written(
|
|
|
|
instruction: Instruction,
|
|
|
|
stack: &Stack<U256>
|
|
|
|
) -> Option<(U256, U256)> {
|
|
|
|
match instruction {
|
|
|
|
instructions::SSTORE => Some((stack.peek(0).clone(), stack.peek(1).clone())),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-17 13:47:16 +01:00
|
|
|
fn mem_gas_cost(&self, schedule: &evm::Schedule, current_mem_size: usize, mem_size: &U256) -> Result<(U256, usize), evm::Error> {
|
2016-01-16 00:31:04 +01:00
|
|
|
let gas_for_mem = |mem_size: U256| {
|
|
|
|
let s = mem_size >> 5;
|
2016-01-17 13:47:16 +01:00
|
|
|
// s * memory_gas + s * s / quad_coeff_div
|
|
|
|
let a = overflowing!(s.overflowing_mul(U256::from(schedule.memory_gas)));
|
2016-01-17 13:59:30 +01:00
|
|
|
// We need to go to U512 to calculate s*s/quad_coeff_div
|
2016-01-17 14:08:31 +01:00
|
|
|
let b = U512::from(s) * U512::from(s) / U512::from(schedule.quad_coeff_div);
|
|
|
|
if b > U512::from(!U256::zero()) {
|
|
|
|
Err(evm::Error::OutOfGas)
|
|
|
|
} else {
|
|
|
|
Ok(overflowing!(a.overflowing_add(U256::from(b))))
|
|
|
|
}
|
2016-01-13 22:30:41 +01:00
|
|
|
};
|
2016-01-16 01:24:37 +01:00
|
|
|
let current_mem_size = U256::from(current_mem_size);
|
2016-01-17 13:47:16 +01:00
|
|
|
let req_mem_size_rounded = (overflowing!(mem_size.overflowing_add(U256::from(31))) >> 5) << 5;
|
|
|
|
let new_mem_gas = try!(gas_for_mem(U256::from(req_mem_size_rounded)));
|
|
|
|
let current_mem_gas = try!(gas_for_mem(current_mem_size));
|
2016-01-13 22:30:41 +01:00
|
|
|
|
2016-01-17 13:47:16 +01:00
|
|
|
Ok((if req_mem_size_rounded > current_mem_size {
|
2016-01-16 00:31:04 +01:00
|
|
|
new_mem_gas - current_mem_gas
|
2016-01-13 22:30:41 +01:00
|
|
|
} else {
|
2016-01-14 14:49:41 +01:00
|
|
|
U256::zero()
|
2016-01-17 13:47:16 +01:00
|
|
|
}, req_mem_size_rounded.low_u64() as usize))
|
2016-01-13 22:30:41 +01:00
|
|
|
}
|
|
|
|
|
2016-01-17 14:17:05 +01:00
|
|
|
fn mem_needed_const(&self, mem: &U256, add: usize) -> Result<U256, evm::Error> {
|
|
|
|
Ok(overflowing!(mem.overflowing_add(U256::from(add))))
|
2016-01-15 21:46:08 +01:00
|
|
|
}
|
|
|
|
|
2016-01-17 14:17:05 +01:00
|
|
|
fn mem_needed(&self, offset: &U256, size: &U256) -> Result<U256, ::evm::Error> {
|
2016-01-13 17:11:00 +01:00
|
|
|
if self.is_zero(size) {
|
2016-01-17 14:17:05 +01:00
|
|
|
return Ok(U256::zero());
|
2016-01-16 17:08:57 +01:00
|
|
|
}
|
|
|
|
|
2016-01-17 14:17:05 +01:00
|
|
|
Ok(overflowing!(offset.overflowing_add(size.clone())))
|
2016-01-13 00:13:09 +01:00
|
|
|
}
|
|
|
|
|
2016-03-18 10:14:19 +01:00
|
|
|
#[cfg_attr(feature="dev", allow(too_many_arguments))]
|
2016-03-20 19:20:37 +01:00
|
|
|
fn exec_instruction(
|
|
|
|
&self,
|
|
|
|
gas: Gas,
|
|
|
|
params: &ActionParams,
|
|
|
|
ext: &mut evm::Ext,
|
|
|
|
instruction: Instruction,
|
|
|
|
code: &mut CodeReader,
|
|
|
|
mem: &mut Memory,
|
|
|
|
stack: &mut Stack<U256>
|
|
|
|
) -> Result<InstructionResult, evm::Error> {
|
2016-01-13 00:13:09 +01:00
|
|
|
match instruction {
|
2016-01-13 15:21:13 +01:00
|
|
|
instructions::JUMP => {
|
|
|
|
let jump = stack.pop_back();
|
|
|
|
return Ok(InstructionResult::JumpToPosition(
|
|
|
|
jump
|
|
|
|
));
|
|
|
|
},
|
|
|
|
instructions::JUMPI => {
|
|
|
|
let jump = stack.pop_back();
|
2016-01-15 19:15:37 +01:00
|
|
|
let condition = stack.pop_back();
|
2016-01-13 17:11:00 +01:00
|
|
|
if !self.is_zero(&condition) {
|
2016-01-13 15:21:13 +01:00
|
|
|
return Ok(InstructionResult::JumpToPosition(
|
|
|
|
jump
|
|
|
|
));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
instructions::JUMPDEST => {
|
|
|
|
// ignore
|
|
|
|
},
|
2016-01-13 00:13:09 +01:00
|
|
|
instructions::CREATE => {
|
|
|
|
let endowment = stack.pop_back();
|
|
|
|
let init_off = stack.pop_back();
|
|
|
|
let init_size = stack.pop_back();
|
|
|
|
|
2016-01-13 22:30:41 +01:00
|
|
|
let contract_code = mem.read_slice(init_off, init_size);
|
2016-01-16 20:11:12 +01:00
|
|
|
let can_create = ext.balance(¶ms.address) >= endowment && ext.depth() < ext.schedule().max_depth;
|
2016-02-15 00:51:50 +01:00
|
|
|
|
2016-01-16 20:11:12 +01:00
|
|
|
if !can_create {
|
|
|
|
stack.push(U256::zero());
|
|
|
|
return Ok(InstructionResult::Ok);
|
2016-01-13 00:13:09 +01:00
|
|
|
}
|
2016-01-16 20:11:12 +01:00
|
|
|
|
|
|
|
let create_result = ext.create(&gas, &endowment, &contract_code);
|
|
|
|
return match create_result {
|
|
|
|
ContractCreateResult::Created(address, gas_left) => {
|
|
|
|
stack.push(address_to_u256(address));
|
2016-01-16 21:24:19 +01:00
|
|
|
Ok(InstructionResult::GasLeft(gas_left))
|
2016-01-16 20:11:12 +01:00
|
|
|
},
|
|
|
|
ContractCreateResult::Failed => {
|
|
|
|
stack.push(U256::zero());
|
|
|
|
// TODO [todr] Should we just StopExecution here?
|
|
|
|
Ok(InstructionResult::UseAllGas)
|
|
|
|
}
|
|
|
|
};
|
2016-01-13 00:13:09 +01:00
|
|
|
},
|
2016-01-25 23:59:50 +01:00
|
|
|
instructions::CALL | instructions::CALLCODE | instructions::DELEGATECALL => {
|
2016-01-20 16:52:22 +01:00
|
|
|
assert!(ext.schedule().call_value_transfer_gas > ext.schedule().call_stipend, "overflow possible");
|
|
|
|
let call_gas = stack.pop_back();
|
|
|
|
let code_address = stack.pop_back();
|
|
|
|
let code_address = u256_to_address(&code_address);
|
|
|
|
|
2016-02-23 11:40:23 +01:00
|
|
|
let value = if instruction == instructions::DELEGATECALL {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(stack.pop_back())
|
2016-01-25 23:59:50 +01:00
|
|
|
};
|
2016-01-14 00:39:59 +01:00
|
|
|
|
|
|
|
let in_off = stack.pop_back();
|
|
|
|
let in_size = stack.pop_back();
|
|
|
|
let out_off = stack.pop_back();
|
|
|
|
let out_size = stack.pop_back();
|
|
|
|
|
2016-01-25 23:59:50 +01:00
|
|
|
// Add stipend (only CALL|CALLCODE when value > 0)
|
|
|
|
let call_gas = call_gas + value.map_or_else(U256::zero, |val| match val > U256::zero() {
|
2016-01-16 20:11:12 +01:00
|
|
|
true => U256::from(ext.schedule().call_stipend),
|
|
|
|
false => U256::zero()
|
2016-01-25 23:59:50 +01:00
|
|
|
});
|
2016-01-16 20:11:12 +01:00
|
|
|
|
2016-01-25 23:59:50 +01:00
|
|
|
// Get sender & receive addresses, check if we have balance
|
|
|
|
let (sender_address, receive_address, has_balance) = match instruction {
|
|
|
|
instructions::CALL => {
|
|
|
|
let has_balance = ext.balance(¶ms.address) >= value.unwrap();
|
|
|
|
(¶ms.address, &code_address, has_balance)
|
|
|
|
},
|
|
|
|
instructions::CALLCODE => {
|
|
|
|
let has_balance = ext.balance(¶ms.address) >= value.unwrap();
|
|
|
|
(¶ms.address, ¶ms.address, has_balance)
|
|
|
|
},
|
|
|
|
instructions::DELEGATECALL => (¶ms.sender, ¶ms.address, true),
|
|
|
|
_ => panic!(format!("Unexpected instruction {} in CALL branch.", instruction))
|
2016-01-23 10:41:13 +01:00
|
|
|
};
|
|
|
|
|
2016-01-25 23:59:50 +01:00
|
|
|
let can_call = has_balance && ext.depth() < ext.schedule().max_depth;
|
2016-01-16 20:11:12 +01:00
|
|
|
if !can_call {
|
|
|
|
stack.push(U256::zero());
|
|
|
|
return Ok(InstructionResult::UnusedGas(call_gas));
|
|
|
|
}
|
|
|
|
|
|
|
|
let call_result = {
|
2016-01-14 01:56:37 +01:00
|
|
|
// we need to write and read from memory in the same time
|
|
|
|
// and we don't want to copy
|
|
|
|
let input = unsafe { ::std::mem::transmute(mem.read_slice(in_off, in_size)) };
|
|
|
|
let output = mem.writeable_slice(out_off, out_size);
|
2016-01-25 23:59:50 +01:00
|
|
|
ext.call(&call_gas, sender_address, receive_address, value, input, &code_address, output)
|
2016-01-16 20:11:12 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
return match call_result {
|
|
|
|
MessageCallResult::Success(gas_left) => {
|
|
|
|
stack.push(U256::one());
|
|
|
|
Ok(InstructionResult::UnusedGas(gas_left))
|
|
|
|
},
|
|
|
|
MessageCallResult::Failed => {
|
|
|
|
stack.push(U256::zero());
|
|
|
|
Ok(InstructionResult::Ok)
|
|
|
|
}
|
2016-01-14 00:39:59 +01:00
|
|
|
};
|
2016-02-15 00:51:50 +01:00
|
|
|
},
|
2016-01-13 00:13:09 +01:00
|
|
|
instructions::RETURN => {
|
|
|
|
let init_off = stack.pop_back();
|
|
|
|
let init_size = stack.pop_back();
|
2016-01-13 22:30:41 +01:00
|
|
|
let return_code = mem.read_slice(init_off, init_size);
|
2016-01-14 00:39:59 +01:00
|
|
|
let gas_left = try!(ext.ret(&gas, &return_code));
|
2016-01-16 21:24:19 +01:00
|
|
|
return Ok(InstructionResult::StopExecutionWithGasLeft(
|
|
|
|
gas_left
|
2016-01-13 15:21:13 +01:00
|
|
|
));
|
2016-01-13 00:13:09 +01:00
|
|
|
},
|
|
|
|
instructions::STOP => {
|
2016-01-13 15:21:13 +01:00
|
|
|
return Ok(InstructionResult::StopExecution);
|
2016-01-13 00:13:09 +01:00
|
|
|
},
|
|
|
|
instructions::SUICIDE => {
|
|
|
|
let address = stack.pop_back();
|
2016-01-14 00:39:59 +01:00
|
|
|
ext.suicide(&u256_to_address(&address));
|
2016-01-13 15:21:13 +01:00
|
|
|
return Ok(InstructionResult::StopExecution);
|
2016-01-13 00:13:09 +01:00
|
|
|
},
|
|
|
|
instructions::LOG0...instructions::LOG4 => {
|
|
|
|
let no_of_topics = instructions::get_log_topics(instruction);
|
|
|
|
|
2016-01-14 12:33:49 +01:00
|
|
|
let offset = stack.pop_back();
|
|
|
|
let size = stack.pop_back();
|
|
|
|
let topics = stack.pop_n(no_of_topics)
|
2016-01-13 00:13:09 +01:00
|
|
|
.iter()
|
|
|
|
.map(H256::from)
|
|
|
|
.collect();
|
2016-01-16 17:17:43 +01:00
|
|
|
ext.log(topics, mem.read_slice(offset, size));
|
2016-01-13 00:13:09 +01:00
|
|
|
},
|
|
|
|
instructions::PUSH1...instructions::PUSH32 => {
|
|
|
|
let bytes = instructions::get_push_bytes(instruction);
|
|
|
|
let val = code.read(bytes);
|
|
|
|
stack.push(val);
|
|
|
|
},
|
|
|
|
instructions::MLOAD => {
|
2016-01-13 22:30:41 +01:00
|
|
|
let word = mem.read(stack.pop_back());
|
|
|
|
stack.push(U256::from(word));
|
2016-01-13 00:13:09 +01:00
|
|
|
},
|
|
|
|
instructions::MSTORE => {
|
2016-01-13 22:30:41 +01:00
|
|
|
let offset = stack.pop_back();
|
|
|
|
let word = stack.pop_back();
|
|
|
|
mem.write(offset, word);
|
2016-01-13 00:13:09 +01:00
|
|
|
},
|
|
|
|
instructions::MSTORE8 => {
|
2016-01-13 22:30:41 +01:00
|
|
|
let offset = stack.pop_back();
|
|
|
|
let byte = stack.pop_back();
|
|
|
|
mem.write_byte(offset, byte);
|
2016-01-13 00:13:09 +01:00
|
|
|
},
|
|
|
|
instructions::MSIZE => {
|
2016-01-13 22:30:41 +01:00
|
|
|
stack.push(U256::from(mem.size()));
|
2016-01-13 00:13:09 +01:00
|
|
|
},
|
2016-01-13 01:53:33 +01:00
|
|
|
instructions::SHA3 => {
|
|
|
|
let offset = stack.pop_back();
|
|
|
|
let size = stack.pop_back();
|
2016-01-13 22:30:41 +01:00
|
|
|
let sha3 = mem.read_slice(offset, size).sha3();
|
2016-01-13 01:53:33 +01:00
|
|
|
stack.push(U256::from(sha3.as_slice()));
|
|
|
|
},
|
2016-01-13 00:13:09 +01:00
|
|
|
instructions::SLOAD => {
|
|
|
|
let key = H256::from(&stack.pop_back());
|
2016-01-16 11:33:41 +01:00
|
|
|
let word = U256::from(ext.storage_at(&key).as_slice());
|
2016-01-13 00:13:09 +01:00
|
|
|
stack.push(word);
|
|
|
|
},
|
|
|
|
instructions::SSTORE => {
|
2016-01-16 20:11:12 +01:00
|
|
|
let address = H256::from(&stack.pop_back());
|
|
|
|
let val = stack.pop_back();
|
|
|
|
|
|
|
|
let current_val = U256::from(ext.storage_at(&address).as_slice());
|
|
|
|
// Increase refund for clear
|
|
|
|
if !self.is_zero(¤t_val) && self.is_zero(&val) {
|
|
|
|
ext.inc_sstore_clears();
|
|
|
|
}
|
|
|
|
ext.set_storage(address, H256::from(&val));
|
2016-01-13 00:13:09 +01:00
|
|
|
},
|
|
|
|
instructions::PC => {
|
2016-01-14 16:32:28 +01:00
|
|
|
stack.push(U256::from(code.position - 1));
|
2016-01-13 00:13:09 +01:00
|
|
|
},
|
|
|
|
instructions::GAS => {
|
2016-01-13 15:21:13 +01:00
|
|
|
stack.push(gas.clone());
|
2016-01-13 00:13:09 +01:00
|
|
|
},
|
2016-01-13 01:53:33 +01:00
|
|
|
instructions::ADDRESS => {
|
|
|
|
stack.push(address_to_u256(params.address.clone()));
|
|
|
|
},
|
|
|
|
instructions::ORIGIN => {
|
|
|
|
stack.push(address_to_u256(params.origin.clone()));
|
|
|
|
},
|
|
|
|
instructions::BALANCE => {
|
|
|
|
let address = u256_to_address(&stack.pop_back());
|
|
|
|
let balance = ext.balance(&address);
|
|
|
|
stack.push(balance);
|
|
|
|
},
|
|
|
|
instructions::CALLER => {
|
|
|
|
stack.push(address_to_u256(params.sender.clone()));
|
|
|
|
},
|
|
|
|
instructions::CALLVALUE => {
|
2016-01-20 17:27:33 +01:00
|
|
|
stack.push(match params.value {
|
2016-02-14 12:54:27 +01:00
|
|
|
ActionValue::Transfer(val) | ActionValue::Apparent(val) => val
|
2016-01-20 17:27:33 +01:00
|
|
|
});
|
2016-01-13 01:53:33 +01:00
|
|
|
},
|
2016-01-13 23:51:10 +01:00
|
|
|
instructions::CALLDATALOAD => {
|
2016-01-15 20:19:46 +01:00
|
|
|
let big_id = stack.pop_back();
|
|
|
|
let id = big_id.low_u64() as usize;
|
2016-01-15 20:14:16 +01:00
|
|
|
let max = id.wrapping_add(32);
|
2016-02-05 02:15:26 +01:00
|
|
|
if let Some(data) = params.data.as_ref() {
|
|
|
|
let bound = cmp::min(data.len(), max);
|
|
|
|
if id < bound && big_id < U256::from(data.len()) {
|
|
|
|
let mut v = [0u8; 32];
|
|
|
|
v[0..bound-id].clone_from_slice(&data[id..bound]);
|
|
|
|
stack.push(U256::from(&v[..]))
|
|
|
|
} else {
|
|
|
|
stack.push(U256::zero())
|
|
|
|
}
|
2016-01-15 20:14:16 +01:00
|
|
|
} else {
|
|
|
|
stack.push(U256::zero())
|
|
|
|
}
|
2016-01-13 23:51:10 +01:00
|
|
|
},
|
2016-01-13 01:53:33 +01:00
|
|
|
instructions::CALLDATASIZE => {
|
2016-01-19 13:47:30 +01:00
|
|
|
stack.push(U256::from(params.data.clone().map_or(0, |l| l.len())));
|
2016-01-13 01:53:33 +01:00
|
|
|
},
|
|
|
|
instructions::CODESIZE => {
|
|
|
|
stack.push(U256::from(code.len()));
|
|
|
|
},
|
|
|
|
instructions::EXTCODESIZE => {
|
|
|
|
let address = u256_to_address(&stack.pop_back());
|
|
|
|
let len = ext.extcode(&address).len();
|
|
|
|
stack.push(U256::from(len));
|
|
|
|
},
|
2016-01-13 22:30:41 +01:00
|
|
|
instructions::CALLDATACOPY => {
|
2016-01-19 13:47:30 +01:00
|
|
|
self.copy_data_to_memory(mem, stack, ¶ms.data.clone().unwrap_or_else(|| vec![]));
|
2016-01-13 22:30:41 +01:00
|
|
|
},
|
|
|
|
instructions::CODECOPY => {
|
2016-01-19 13:47:30 +01:00
|
|
|
self.copy_data_to_memory(mem, stack, ¶ms.code.clone().unwrap_or_else(|| vec![]));
|
2016-01-13 22:30:41 +01:00
|
|
|
},
|
|
|
|
instructions::EXTCODECOPY => {
|
|
|
|
let address = u256_to_address(&stack.pop_back());
|
|
|
|
let code = ext.extcode(&address);
|
|
|
|
self.copy_data_to_memory(mem, stack, &code);
|
|
|
|
},
|
2016-01-13 01:53:33 +01:00
|
|
|
instructions::GASPRICE => {
|
|
|
|
stack.push(params.gas_price.clone());
|
|
|
|
},
|
|
|
|
instructions::BLOCKHASH => {
|
|
|
|
let block_number = stack.pop_back();
|
|
|
|
let block_hash = ext.blockhash(&block_number);
|
|
|
|
stack.push(U256::from(block_hash.as_slice()));
|
|
|
|
},
|
|
|
|
instructions::COINBASE => {
|
|
|
|
stack.push(address_to_u256(ext.env_info().author.clone()));
|
|
|
|
},
|
|
|
|
instructions::TIMESTAMP => {
|
|
|
|
stack.push(U256::from(ext.env_info().timestamp));
|
|
|
|
},
|
|
|
|
instructions::NUMBER => {
|
|
|
|
stack.push(U256::from(ext.env_info().number));
|
|
|
|
},
|
|
|
|
instructions::DIFFICULTY => {
|
|
|
|
stack.push(ext.env_info().difficulty.clone());
|
|
|
|
},
|
|
|
|
instructions::GASLIMIT => {
|
|
|
|
stack.push(ext.env_info().gas_limit.clone());
|
|
|
|
},
|
2016-01-13 00:13:09 +01:00
|
|
|
_ => {
|
2016-01-14 02:45:16 +01:00
|
|
|
try!(self.exec_stack_instruction(instruction, stack));
|
2016-01-13 00:13:09 +01:00
|
|
|
}
|
2016-01-13 15:21:13 +01:00
|
|
|
};
|
2016-01-16 20:11:12 +01:00
|
|
|
Ok(InstructionResult::Ok)
|
2016-01-13 15:21:13 +01:00
|
|
|
}
|
|
|
|
|
2016-01-31 22:47:24 +01:00
|
|
|
fn copy_data_to_memory(&self, mem: &mut Memory, stack: &mut Stack<U256>, data: &[u8]) {
|
|
|
|
let dest_offset = stack.pop_back();
|
|
|
|
let source_offset = stack.pop_back();
|
2016-01-15 20:14:16 +01:00
|
|
|
let size = stack.pop_back();
|
2016-01-31 22:47:24 +01:00
|
|
|
let source_size = U256::from(data.len());
|
2016-01-13 22:30:41 +01:00
|
|
|
|
2016-01-31 22:47:24 +01:00
|
|
|
let output_end = match source_offset > source_size || size > source_size || source_offset + size > source_size {
|
|
|
|
true => {
|
|
|
|
let zero_slice = if source_offset > source_size {
|
|
|
|
mem.writeable_slice(dest_offset, size)
|
|
|
|
} else {
|
|
|
|
mem.writeable_slice(dest_offset + source_size - source_offset, source_offset + size - source_size)
|
|
|
|
};
|
|
|
|
for i in zero_slice.iter_mut() {
|
|
|
|
*i = 0;
|
|
|
|
}
|
|
|
|
data.len()
|
|
|
|
},
|
|
|
|
false => (size.low_u64() + source_offset.low_u64()) as usize
|
|
|
|
};
|
2016-01-15 20:14:16 +01:00
|
|
|
|
2016-01-31 22:47:24 +01:00
|
|
|
if source_offset < source_size {
|
|
|
|
let output_begin = source_offset.low_u64() as usize;
|
|
|
|
mem.write_slice(dest_offset, &data[output_begin..output_end]);
|
2016-01-15 20:14:16 +01:00
|
|
|
}
|
2016-01-13 22:30:41 +01:00
|
|
|
}
|
|
|
|
|
2016-06-02 12:40:31 +02:00
|
|
|
fn verify_instructions_requirements(
|
|
|
|
&self,
|
|
|
|
info: &instructions::InstructionInfo,
|
|
|
|
stack_limit: usize,
|
|
|
|
stack: &Stack<U256>
|
|
|
|
) -> Result<(), evm::Error> {
|
2016-01-13 17:11:00 +01:00
|
|
|
if !stack.has(info.args) {
|
2016-01-14 01:31:45 +01:00
|
|
|
Err(evm::Error::StackUnderflow {
|
|
|
|
instruction: info.name,
|
2016-02-15 00:51:50 +01:00
|
|
|
wanted: info.args,
|
2016-01-14 01:31:45 +01:00
|
|
|
on_stack: stack.size()
|
|
|
|
})
|
2016-01-13 17:11:00 +01:00
|
|
|
} else if stack.size() - info.args + info.ret > stack_limit {
|
2016-01-14 01:31:45 +01:00
|
|
|
Err(evm::Error::OutOfStack {
|
|
|
|
instruction: info.name,
|
2016-02-15 00:51:50 +01:00
|
|
|
wanted: info.ret - info.args,
|
2016-01-14 01:31:45 +01:00
|
|
|
limit: stack_limit
|
|
|
|
})
|
2016-01-13 17:11:00 +01:00
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-13 15:21:13 +01:00
|
|
|
fn verify_gas(&self, current_gas: &U256, gas_cost: &U256) -> Result<(), evm::Error> {
|
2016-01-16 20:11:12 +01:00
|
|
|
match current_gas < gas_cost {
|
|
|
|
true => Err(evm::Error::OutOfGas),
|
|
|
|
false => Ok(())
|
2016-01-13 00:13:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn verify_jump(&self, jump_u: U256, valid_jump_destinations: &HashSet<usize>) -> Result<usize, evm::Error> {
|
|
|
|
let jump = jump_u.low_u64() as usize;
|
|
|
|
|
2016-01-15 22:46:29 +01:00
|
|
|
if valid_jump_destinations.contains(&jump) && jump_u < U256::from(!0 as usize) {
|
2016-01-13 00:13:09 +01:00
|
|
|
Ok(jump)
|
|
|
|
} else {
|
2016-01-14 02:36:48 +01:00
|
|
|
Err(evm::Error::BadJumpDestination {
|
|
|
|
destination: jump
|
|
|
|
})
|
2016-01-13 00:13:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-13 17:11:00 +01:00
|
|
|
fn is_zero(&self, val: &U256) -> bool {
|
|
|
|
&U256::zero() == val
|
2016-01-13 00:13:09 +01:00
|
|
|
}
|
|
|
|
|
2016-01-13 01:53:33 +01:00
|
|
|
fn bool_to_u256(&self, val: bool) -> U256 {
|
|
|
|
if val {
|
|
|
|
U256::one()
|
|
|
|
} else {
|
|
|
|
U256::zero()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-16 17:08:57 +01:00
|
|
|
fn exec_stack_instruction(&self, instruction: Instruction, stack: &mut Stack<U256>) -> Result<(), evm::Error> {
|
2016-01-13 00:13:09 +01:00
|
|
|
match instruction {
|
|
|
|
instructions::DUP1...instructions::DUP16 => {
|
|
|
|
let position = instructions::get_dup_position(instruction);
|
|
|
|
let val = stack.peek(position).clone();
|
|
|
|
stack.push(val);
|
|
|
|
},
|
|
|
|
instructions::SWAP1...instructions::SWAP16 => {
|
|
|
|
let position = instructions::get_swap_position(instruction);
|
|
|
|
stack.swap_with_top(position)
|
|
|
|
},
|
2016-01-13 01:53:33 +01:00
|
|
|
instructions::POP => {
|
|
|
|
stack.pop_back();
|
|
|
|
},
|
2016-01-13 00:13:09 +01:00
|
|
|
instructions::ADD => {
|
|
|
|
let a = stack.pop_back();
|
|
|
|
let b = stack.pop_back();
|
2016-01-17 13:24:57 +01:00
|
|
|
stack.push(a.overflowing_add(b).0);
|
2016-01-13 00:13:09 +01:00
|
|
|
},
|
2016-01-13 01:53:33 +01:00
|
|
|
instructions::MUL => {
|
|
|
|
let a = stack.pop_back();
|
|
|
|
let b = stack.pop_back();
|
2016-01-17 13:24:57 +01:00
|
|
|
stack.push(a.overflowing_mul(b).0);
|
2016-01-13 01:53:33 +01:00
|
|
|
},
|
|
|
|
instructions::SUB => {
|
|
|
|
let a = stack.pop_back();
|
|
|
|
let b = stack.pop_back();
|
2016-01-17 13:24:57 +01:00
|
|
|
stack.push(a.overflowing_sub(b).0);
|
2016-01-13 01:53:33 +01:00
|
|
|
},
|
|
|
|
instructions::DIV => {
|
|
|
|
let a = stack.pop_back();
|
|
|
|
let b = stack.pop_back();
|
2016-01-17 11:54:29 +01:00
|
|
|
stack.push(if !self.is_zero(&b) {
|
2016-01-17 13:24:57 +01:00
|
|
|
a.overflowing_div(b).0
|
2016-01-17 11:54:29 +01:00
|
|
|
} else {
|
2016-02-15 00:51:50 +01:00
|
|
|
U256::zero()
|
2016-01-13 01:53:33 +01:00
|
|
|
});
|
|
|
|
},
|
|
|
|
instructions::MOD => {
|
|
|
|
let a = stack.pop_back();
|
|
|
|
let b = stack.pop_back();
|
2016-01-17 11:54:29 +01:00
|
|
|
stack.push(if !self.is_zero(&b) {
|
2016-01-17 13:24:57 +01:00
|
|
|
a.overflowing_rem(b).0
|
2016-01-17 11:54:29 +01:00
|
|
|
} else {
|
|
|
|
U256::zero()
|
2016-01-13 01:53:33 +01:00
|
|
|
});
|
|
|
|
},
|
2016-01-15 03:12:39 +01:00
|
|
|
instructions::SDIV => {
|
2016-01-15 19:15:37 +01:00
|
|
|
let (a, sign_a) = get_and_reset_sign(stack.pop_back());
|
|
|
|
let (b, sign_b) = get_and_reset_sign(stack.pop_back());
|
2016-01-15 16:04:18 +01:00
|
|
|
|
2016-01-15 19:15:37 +01:00
|
|
|
// -2^255
|
2016-01-16 21:24:19 +01:00
|
|
|
let min = (U256::one() << 255) - U256::one();
|
2016-01-15 19:15:37 +01:00
|
|
|
stack.push(if self.is_zero(&b) {
|
2016-01-15 16:04:18 +01:00
|
|
|
U256::zero()
|
2016-01-15 19:15:37 +01:00
|
|
|
} else if a == min && b == !U256::zero() {
|
|
|
|
min
|
2016-01-15 16:04:18 +01:00
|
|
|
} else {
|
2016-01-17 13:24:57 +01:00
|
|
|
let c = a.overflowing_div(b).0;
|
2016-01-15 03:12:39 +01:00
|
|
|
set_sign(c, sign_a ^ sign_b)
|
|
|
|
});
|
|
|
|
},
|
|
|
|
instructions::SMOD => {
|
2016-01-15 19:15:37 +01:00
|
|
|
let ua = stack.pop_back();
|
2016-01-15 16:04:18 +01:00
|
|
|
let ub = stack.pop_back();
|
2016-01-15 19:15:37 +01:00
|
|
|
let (a, sign_a) = get_and_reset_sign(ua);
|
2016-01-17 13:24:57 +01:00
|
|
|
let b = get_and_reset_sign(ub).0;
|
2016-01-15 03:12:39 +01:00
|
|
|
|
2016-01-15 19:15:37 +01:00
|
|
|
stack.push(if !self.is_zero(&b) {
|
2016-01-17 13:24:57 +01:00
|
|
|
let c = a.overflowing_rem(b).0;
|
2016-01-15 19:15:37 +01:00
|
|
|
set_sign(c, sign_a)
|
2016-01-15 03:12:39 +01:00
|
|
|
} else {
|
|
|
|
U256::zero()
|
|
|
|
});
|
|
|
|
},
|
|
|
|
instructions::EXP => {
|
|
|
|
let base = stack.pop_back();
|
|
|
|
let expon = stack.pop_back();
|
2016-01-17 13:24:57 +01:00
|
|
|
let res = base.overflowing_pow(expon).0;
|
2016-01-15 14:48:07 +01:00
|
|
|
stack.push(res);
|
2016-01-15 03:12:39 +01:00
|
|
|
},
|
2016-01-13 01:53:33 +01:00
|
|
|
instructions::NOT => {
|
|
|
|
let a = stack.pop_back();
|
|
|
|
stack.push(!a);
|
|
|
|
},
|
|
|
|
instructions::LT => {
|
|
|
|
let a = stack.pop_back();
|
|
|
|
let b = stack.pop_back();
|
|
|
|
stack.push(self.bool_to_u256(a < b));
|
|
|
|
},
|
2016-01-15 03:12:39 +01:00
|
|
|
instructions::SLT => {
|
|
|
|
let (a, neg_a) = get_and_reset_sign(stack.pop_back());
|
|
|
|
let (b, neg_b) = get_and_reset_sign(stack.pop_back());
|
|
|
|
|
2016-02-15 00:51:50 +01:00
|
|
|
let is_positive_lt = a < b && !(neg_a | neg_b);
|
|
|
|
let is_negative_lt = a > b && (neg_a & neg_b);
|
|
|
|
let has_different_signs = neg_a && !neg_b;
|
2016-01-15 03:12:39 +01:00
|
|
|
|
|
|
|
stack.push(self.bool_to_u256(is_positive_lt | is_negative_lt | has_different_signs));
|
|
|
|
},
|
2016-01-13 01:53:33 +01:00
|
|
|
instructions::GT => {
|
|
|
|
let a = stack.pop_back();
|
|
|
|
let b = stack.pop_back();
|
|
|
|
stack.push(self.bool_to_u256(a > b));
|
|
|
|
},
|
2016-01-15 03:12:39 +01:00
|
|
|
instructions::SGT => {
|
|
|
|
let (a, neg_a) = get_and_reset_sign(stack.pop_back());
|
|
|
|
let (b, neg_b) = get_and_reset_sign(stack.pop_back());
|
|
|
|
|
2016-02-15 00:51:50 +01:00
|
|
|
let is_positive_gt = a > b && !(neg_a | neg_b);
|
|
|
|
let is_negative_gt = a < b && (neg_a & neg_b);
|
|
|
|
let has_different_signs = !neg_a && neg_b;
|
2016-01-15 03:12:39 +01:00
|
|
|
|
|
|
|
stack.push(self.bool_to_u256(is_positive_gt | is_negative_gt | has_different_signs));
|
|
|
|
},
|
2016-01-13 01:53:33 +01:00
|
|
|
instructions::EQ => {
|
|
|
|
let a = stack.pop_back();
|
|
|
|
let b = stack.pop_back();
|
|
|
|
stack.push(self.bool_to_u256(a == b));
|
|
|
|
},
|
|
|
|
instructions::ISZERO => {
|
|
|
|
let a = stack.pop_back();
|
2016-01-13 17:11:00 +01:00
|
|
|
stack.push(self.bool_to_u256(self.is_zero(&a)));
|
2016-01-13 01:53:33 +01:00
|
|
|
},
|
|
|
|
instructions::AND => {
|
|
|
|
let a = stack.pop_back();
|
|
|
|
let b = stack.pop_back();
|
|
|
|
stack.push(a & b);
|
|
|
|
},
|
|
|
|
instructions::OR => {
|
|
|
|
let a = stack.pop_back();
|
|
|
|
let b = stack.pop_back();
|
|
|
|
stack.push(a | b);
|
|
|
|
},
|
|
|
|
instructions::XOR => {
|
|
|
|
let a = stack.pop_back();
|
|
|
|
let b = stack.pop_back();
|
|
|
|
stack.push(a ^ b);
|
|
|
|
},
|
2016-01-15 03:12:39 +01:00
|
|
|
instructions::BYTE => {
|
|
|
|
let word = stack.pop_back();
|
2016-01-15 19:48:51 +01:00
|
|
|
let val = stack.pop_back();
|
2016-01-16 20:11:12 +01:00
|
|
|
let byte = match word < U256::from(32) {
|
|
|
|
true => (val >> (8 * (31 - word.low_u64() as usize))) & U256::from(0xff),
|
|
|
|
false => U256::zero()
|
2016-01-15 03:12:39 +01:00
|
|
|
};
|
|
|
|
stack.push(byte);
|
|
|
|
},
|
|
|
|
instructions::ADDMOD => {
|
|
|
|
let a = stack.pop_back();
|
|
|
|
let b = stack.pop_back();
|
|
|
|
let c = stack.pop_back();
|
|
|
|
|
|
|
|
stack.push(if !self.is_zero(&c) {
|
2016-01-15 16:04:18 +01:00
|
|
|
// upcast to 512
|
|
|
|
let a5 = U512::from(a);
|
2016-01-17 13:24:57 +01:00
|
|
|
let res = a5.overflowing_add(U512::from(b)).0;
|
|
|
|
let x = res.overflowing_rem(U512::from(c)).0;
|
2016-01-15 16:04:18 +01:00
|
|
|
U256::from(x)
|
2016-01-15 03:12:39 +01:00
|
|
|
} else {
|
|
|
|
U256::zero()
|
|
|
|
});
|
|
|
|
},
|
|
|
|
instructions::MULMOD => {
|
|
|
|
let a = stack.pop_back();
|
|
|
|
let b = stack.pop_back();
|
|
|
|
let c = stack.pop_back();
|
|
|
|
|
|
|
|
stack.push(if !self.is_zero(&c) {
|
2016-01-15 16:04:18 +01:00
|
|
|
let a5 = U512::from(a);
|
2016-01-17 13:24:57 +01:00
|
|
|
let res = a5.overflowing_mul(U512::from(b)).0;
|
|
|
|
let x = res.overflowing_rem(U512::from(c)).0;
|
2016-01-15 16:04:18 +01:00
|
|
|
U256::from(x)
|
2016-01-15 03:12:39 +01:00
|
|
|
} else {
|
|
|
|
U256::zero()
|
|
|
|
});
|
|
|
|
},
|
2016-01-15 19:15:37 +01:00
|
|
|
instructions::SIGNEXTEND => {
|
|
|
|
let bit = stack.pop_back();
|
|
|
|
if bit < U256::from(32) {
|
|
|
|
let number = stack.pop_back();
|
|
|
|
let bit_position = (bit.low_u64() * 8 + 7) as usize;
|
|
|
|
|
|
|
|
let bit = number.bit(bit_position);
|
|
|
|
let mask = (U256::one() << bit_position) - U256::one();
|
|
|
|
stack.push(if bit {
|
|
|
|
number | !mask
|
|
|
|
} else {
|
|
|
|
number & mask
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
2016-01-14 02:45:16 +01:00
|
|
|
_ => {
|
|
|
|
return Err(evm::Error::BadInstruction {
|
|
|
|
instruction: instruction
|
|
|
|
});
|
|
|
|
}
|
2016-01-13 00:13:09 +01:00
|
|
|
}
|
2016-01-14 02:45:16 +01:00
|
|
|
Ok(())
|
2016-01-14 01:56:37 +01:00
|
|
|
}
|
2016-01-13 00:13:09 +01:00
|
|
|
|
2016-01-17 15:56:09 +01:00
|
|
|
fn find_jump_destinations(&self, code: &[u8]) -> HashSet<CodePosition> {
|
2016-01-14 01:56:37 +01:00
|
|
|
let mut jump_dests = HashSet::new();
|
2016-01-13 00:13:09 +01:00
|
|
|
let mut position = 0;
|
|
|
|
|
|
|
|
while position < code.len() {
|
2016-01-14 01:56:37 +01:00
|
|
|
let instruction = code[position];
|
2016-01-13 00:13:09 +01:00
|
|
|
|
|
|
|
if instruction == instructions::JUMPDEST {
|
|
|
|
jump_dests.insert(position);
|
|
|
|
} else if instructions::is_push(instruction) {
|
|
|
|
position += instructions::get_push_bytes(instruction);
|
|
|
|
}
|
|
|
|
position += 1;
|
2016-01-14 01:56:37 +01:00
|
|
|
}
|
2016-01-13 00:13:09 +01:00
|
|
|
|
2016-01-17 15:56:09 +01:00
|
|
|
jump_dests
|
2016-01-14 01:56:37 +01:00
|
|
|
}
|
2016-01-13 01:53:33 +01:00
|
|
|
}
|
|
|
|
|
2016-01-15 03:12:39 +01:00
|
|
|
fn get_and_reset_sign(value: U256) -> (U256, bool) {
|
2016-01-15 19:15:37 +01:00
|
|
|
let sign = (value >> 255).low_u64() == 1;
|
|
|
|
(set_sign(value, sign), sign)
|
2016-01-15 03:12:39 +01:00
|
|
|
}
|
|
|
|
|
2016-01-16 17:08:57 +01:00
|
|
|
fn set_sign(value: U256, sign: bool) -> U256 {
|
2016-01-15 19:15:37 +01:00
|
|
|
if sign {
|
2016-01-17 13:47:16 +01:00
|
|
|
(!U256::zero() ^ value).overflowing_add(U256::one()).0
|
2016-01-15 19:15:37 +01:00
|
|
|
} else {
|
|
|
|
value
|
|
|
|
}
|
2016-01-15 03:12:39 +01:00
|
|
|
}
|
|
|
|
|
2016-01-16 17:08:57 +01:00
|
|
|
#[inline]
|
2016-01-16 01:24:37 +01:00
|
|
|
fn add_u256_usize(value: &U256, num: usize) -> (U256, bool) {
|
|
|
|
value.clone().overflowing_add(U256::from(num))
|
2016-01-13 17:11:00 +01:00
|
|
|
}
|
|
|
|
|
2016-01-16 17:08:57 +01:00
|
|
|
#[inline]
|
2016-01-13 01:53:33 +01:00
|
|
|
fn u256_to_address(value: &U256) -> Address {
|
|
|
|
Address::from(H256::from(value))
|
|
|
|
}
|
|
|
|
|
2016-01-16 17:08:57 +01:00
|
|
|
#[inline]
|
2016-01-13 01:53:33 +01:00
|
|
|
fn address_to_u256(value: Address) -> U256 {
|
|
|
|
U256::from(H256::from(value).as_slice())
|
2016-01-13 00:13:09 +01:00
|
|
|
}
|
|
|
|
|
2016-01-17 13:47:16 +01:00
|
|
|
#[test]
|
|
|
|
fn test_mem_gas_cost() {
|
|
|
|
// given
|
|
|
|
let interpreter = Interpreter;
|
|
|
|
let schedule = evm::Schedule::default();
|
|
|
|
let current_mem_size = 5;
|
|
|
|
let mem_size = !U256::zero();
|
|
|
|
|
|
|
|
// when
|
|
|
|
let result = interpreter.mem_gas_cost(&schedule, current_mem_size, &mem_size);
|
|
|
|
|
|
|
|
// then
|
|
|
|
if let Ok(_) = result {
|
|
|
|
assert!(false, "Should fail with OutOfGas");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-13 00:13:09 +01:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use common::*;
|
|
|
|
use super::*;
|
2016-01-14 14:49:41 +01:00
|
|
|
use evm;
|
2016-01-13 00:13:09 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_find_jump_destinations() {
|
|
|
|
// given
|
|
|
|
let interpreter = Interpreter;
|
|
|
|
let code = "7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff5b01600055".from_hex().unwrap();
|
|
|
|
|
|
|
|
// when
|
|
|
|
let valid_jump_destinations = interpreter.find_jump_destinations(&code);
|
|
|
|
|
|
|
|
// then
|
|
|
|
assert!(valid_jump_destinations.contains(&66));
|
|
|
|
}
|
2016-01-13 22:30:41 +01:00
|
|
|
|
2016-01-14 14:49:41 +01:00
|
|
|
#[test]
|
|
|
|
fn test_calculate_mem_cost() {
|
|
|
|
// given
|
|
|
|
let interpreter = Interpreter;
|
|
|
|
let schedule = evm::Schedule::default();
|
|
|
|
let current_mem_size = 0;
|
|
|
|
let mem_size = U256::from(5);
|
2016-02-15 00:51:50 +01:00
|
|
|
|
2016-01-14 14:49:41 +01:00
|
|
|
// when
|
2016-01-17 13:47:16 +01:00
|
|
|
let (mem_cost, mem_size) = interpreter.mem_gas_cost(&schedule, current_mem_size, &mem_size).unwrap();
|
2016-01-14 14:49:41 +01:00
|
|
|
|
|
|
|
// then
|
|
|
|
assert_eq!(mem_cost, U256::from(3));
|
|
|
|
assert_eq!(mem_size, 32);
|
|
|
|
}
|
|
|
|
|
2016-01-13 22:30:41 +01:00
|
|
|
#[test]
|
|
|
|
fn test_memory_read_and_write() {
|
|
|
|
// given
|
2016-01-16 17:08:57 +01:00
|
|
|
let mem: &mut super::Memory = &mut vec![];
|
2016-01-15 22:46:29 +01:00
|
|
|
mem.resize(0x80 + 32);
|
2016-01-13 22:30:41 +01:00
|
|
|
|
|
|
|
// when
|
2016-01-15 22:46:29 +01:00
|
|
|
mem.write(U256::from(0x80), U256::from(0xabcdef));
|
2016-01-13 22:30:41 +01:00
|
|
|
|
|
|
|
// then
|
2016-01-15 22:46:29 +01:00
|
|
|
assert_eq!(mem.read(U256::from(0x80)), U256::from(0xabcdef));
|
2016-01-13 22:30:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_memory_read_and_write_byte() {
|
|
|
|
// given
|
2016-01-16 17:08:57 +01:00
|
|
|
let mem: &mut super::Memory = &mut vec![];
|
2016-01-13 23:51:10 +01:00
|
|
|
mem.resize(32);
|
2016-01-13 22:30:41 +01:00
|
|
|
|
|
|
|
// when
|
2016-01-13 23:51:10 +01:00
|
|
|
mem.write_byte(U256::from(0x1d), U256::from(0xab));
|
|
|
|
mem.write_byte(U256::from(0x1e), U256::from(0xcd));
|
|
|
|
mem.write_byte(U256::from(0x1f), U256::from(0xef));
|
2016-01-13 22:30:41 +01:00
|
|
|
|
|
|
|
// then
|
2016-01-13 23:51:10 +01:00
|
|
|
assert_eq!(mem.read(U256::from(0x00)), U256::from(0xabcdef));
|
2016-01-13 22:30:41 +01:00
|
|
|
}
|
2016-01-13 00:13:09 +01:00
|
|
|
}
|