openethereum/src/evm/interpreter.rs

1064 lines
29 KiB
Rust
Raw Normal View History

///! Rust VM implementation
use common::*;
use evm;
use super::instructions as instructions;
use super::instructions::Instruction;
2016-01-15 02:16:04 +01:00
use std::num::wrapping::OverflowingOps;
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;
/// Get elements from top and remove them from Stack. Panics if stack is empty.
fn pop_n(&mut self, no_of_elems: usize) -> Vec<T>;
/// Add element on top of the Stack
fn push(&mut self, elem: T);
/// Get number of elements on Stack
fn size(&self) -> usize;
}
2016-01-13 12:02:49 +01:00
impl<S : fmt::Display> Stack<S> for Vec<S> {
fn peek(&self, no_from_top: usize) -> &S {
return &self[self.len() - no_from_top - 1];
}
fn swap_with_top(&mut self, no_from_top: usize) {
let len = self.len();
self.swap(len - no_from_top - 1, len - 1);
}
fn has(&self, no_of_elems: usize) -> bool {
self.len() >= no_of_elems
}
fn pop_back(&mut self) -> S {
let val = self.pop();
match val {
Some(x) => x,
None => panic!("Tried to pop from empty stack.")
}
}
fn pop_n(&mut self, no_of_elems: usize) -> Vec<S> {
let mut vec = Vec::new();
2016-01-14 11:00:29 +01:00
for _i in 1..no_of_elems+1 {
vec.push(self.pop_back());
}
vec
}
fn push(&mut self, elem: S) {
// println!("Pushing to stack: {}", elem);
self.push(elem);
}
fn size(&self) -> usize {
self.len()
}
}
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);
/// 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);
/// 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
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-13 22:30:41 +01:00
}
impl Memory for Vec<u8> {
fn size(&self) -> usize {
return self.len()
}
fn read_slice(&self, init_off_u: U256, init_size_u: U256) -> &[u8] {
let init_off = init_off_u.low_u64() as usize;
let init_size = init_size_u.low_u64() as usize;
&self[init_off..init_off + init_size]
}
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;
&mut self[off..off+s]
}
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 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;
let mut val = value;
2016-01-13 22:30:41 +01:00
let end = off + 32;
for pos in off..end {
2016-01-14 00:46:24 +01:00
self[end - pos - 1] = val.low_u64() as u8;
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-01-14 14:49:41 +01:00
fn expand(&mut self, size: usize) {
if size > self.len() {
Memory::resize(self, size)
}
}
2016-01-13 22:30:41 +01:00
}
/// Abstraction over raw vector of Bytes. Easier state management of PC.
struct CodeReader<'a> {
position: ProgramCounter,
code: &'a Bytes
}
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;
self.position += no_of_bytes;
2016-01-14 01:05:01 +01:00
U256::from(&self.code[pos..pos+no_of_bytes])
}
2016-01-13 01:53:33 +01:00
fn len (&self) -> usize {
self.code.len()
}
}
enum RequiredMem {
Mem(U256),
OutOfMemory
}
2016-01-13 17:11:00 +01:00
enum InstructionCost {
Gas(U256),
GasMem(U256, RequiredMem),
GasMemCopy(U256, RequiredMem, U256)
2016-01-13 17:11:00 +01:00
}
enum InstructionResult {
AdditionalGasCost(U256),
JumpToPosition(U256),
StopExecutionWithGasCost(U256),
StopExecution
}
pub struct Interpreter;
impl evm::Evm for Interpreter {
fn exec(&self, params: &ActionParams, ext: &mut evm::Ext) -> evm::Result {
2016-01-14 11:00:29 +01:00
let code = &params.code;
let valid_jump_destinations = self.find_jump_destinations(&code);
let mut current_gas = params.gas.clone();
2016-01-14 11:00:29 +01:00
let mut stack = Vec::with_capacity(ext.schedule().stack_limit);
let mut mem = vec![];
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];
reader.position += 1;
// Calculate gas cost
let gas_cost = try!(self.get_gas_cost_and_expand_mem(ext, instruction, &mut mem, &stack));
try!(self.verify_gas(&current_gas, &gas_cost));
current_gas = current_gas - gas_cost;
// println!("Executing: {} (0x{:x}) [Gas Cost: {} (Left: {})]",
// instructions::get_info(instruction).name, instruction,
// gas_cost,
// current_gas
// );
2016-01-14 11:00:29 +01:00
// Execute instruction
let result = try!(self.exec_instruction(
current_gas, params, ext, instruction, &mut reader, &mut mem, &mut stack
));
// Advance
match result {
InstructionResult::JumpToPosition(position) => {
let pos = try!(self.verify_jump(position, &valid_jump_destinations));
reader.position = pos;
},
InstructionResult::AdditionalGasCost(gas_cost) => {
current_gas = current_gas - gas_cost;
},
InstructionResult::StopExecutionWithGasCost(gas_cost) => {
current_gas = current_gas - gas_cost;
reader.position = code.len();
},
InstructionResult::StopExecution => {
reader.position = code.len();
}
}
}
2016-01-14 11:00:29 +01:00
Ok(current_gas)
}
}
impl Interpreter {
fn get_gas_cost_and_expand_mem(&self,
2016-01-14 12:33:49 +01:00
ext: &evm::Ext,
instruction: Instruction,
mem: &mut Memory,
stack: &Stack<U256>
) -> evm::Result {
2016-01-14 01:56:37 +01:00
let schedule = ext.schedule();
let info = instructions::get_info(instruction);
2016-01-14 01:56:37 +01:00
if !schedule.have_delegate_call && instruction == instructions::DELEGATECALL {
return Err(evm::Error::BadInstruction {
2016-01-14 02:45:16 +01:00
instruction: instruction
});
2016-01-14 01:56:37 +01:00
}
if info.tier == instructions::GasPriceTier::InvalidTier {
return Err(evm::Error::BadInstruction {
2016-01-14 02:45:16 +01:00
instruction: instruction
});
2016-01-14 01:56:37 +01:00
}
2016-01-14 01:56:37 +01:00
try!(self.verify_instructions_requirements(&info, schedule.stack_limit, stack));
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-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-14 01:56:37 +01:00
let val = U256::from(ext.sload(&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
2016-01-15 16:04:18 +01:00
} else if !self.is_zero(&val) && self.is_zero(newval) {
schedule.sstore_set_gas
2016-01-14 01:56:37 +01:00
} else {
schedule.sstore_reset_gas
};
InstructionCost::Gas(U256::from(gas))
},
instructions::SLOAD => {
InstructionCost::Gas(U256::from(schedule.sload_gas))
},
instructions::MSTORE => {
InstructionCost::GasMem(default_gas, self.mem_needed_const(stack.peek(0), 32))
2016-01-14 01:56:37 +01:00
},
instructions::MLOAD => {
InstructionCost::GasMem(default_gas, self.mem_needed_const(stack.peek(0), 32))
2016-01-14 01:56:37 +01:00
},
instructions::MSTORE8 => {
InstructionCost::GasMem(default_gas, self.mem_needed_const(stack.peek(0), 1))
2016-01-14 01:56:37 +01:00
},
instructions::RETURN => {
InstructionCost::GasMem(default_gas, self.mem_needed(stack.peek(0), stack.peek(1)))
},
instructions::SHA3 => {
let words = add_u256_usize(stack.peek(1), 31) / U256::from(32);
let gas = U256::from(schedule.sha3_gas) + (U256::from(schedule.sha3_word_gas) * words);
InstructionCost::GasMem(gas, self.mem_needed(stack.peek(0), stack.peek(1)))
},
instructions::CALLDATACOPY => {
InstructionCost::GasMemCopy(default_gas, self.mem_needed(stack.peek(0), stack.peek(2)), stack.peek(2).clone())
},
instructions::CODECOPY => {
InstructionCost::GasMemCopy(default_gas, self.mem_needed(stack.peek(0), stack.peek(2)), stack.peek(2).clone())
},
instructions::EXTCODECOPY => {
InstructionCost::GasMemCopy(default_gas, self.mem_needed(stack.peek(1), stack.peek(3)), stack.peek(3).clone())
},
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;
let data_gas = stack.peek(1).clone() * U256::from(schedule.log_data_gas);
let gas = try!(self.gas_add(data_gas, U256::from(log_gas)));
2016-01-14 01:56:37 +01:00
InstructionCost::GasMem(gas, self.mem_needed(stack.peek(0), stack.peek(1)))
},
instructions::CALL | instructions::CALLCODE => {
2016-01-14 16:32:28 +01:00
// [todr] we actuall call gas_cost is calculated in ext
let gas = U256::from(schedule.call_gas);
let mem = self.mem_max(
2016-01-14 01:56:37 +01:00
self.mem_needed(stack.peek(5), stack.peek(6)),
self.mem_needed(stack.peek(3), stack.peek(4))
);
InstructionCost::GasMem(gas, mem)
},
instructions::DELEGATECALL => {
let gas = add_u256_usize(stack.peek(0), schedule.call_gas);
let mem = self.mem_max(
2016-01-14 01:56:37 +01:00
self.mem_needed(stack.peek(4), stack.peek(5)),
self.mem_needed(stack.peek(2), stack.peek(3))
);
InstructionCost::GasMem(gas, mem)
},
instructions::CREATE => {
let gas = U256::from(schedule.create_gas);
let mem = self.mem_needed(stack.peek(1), stack.peek(2));
InstructionCost::GasMem(gas, mem)
},
instructions::EXP => {
let expon = stack.peek(1);
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) => {
Ok(gas)
},
InstructionCost::GasMem(gas, mem_size) => match mem_size {
RequiredMem::Mem(mem_size) => {
let (mem_gas, new_mem_size) = self.mem_gas_cost(schedule, mem.size(), &mem_size);
// Expand after calculating the cost
mem.expand(new_mem_size);
self.gas_add(gas, mem_gas)
},
RequiredMem::OutOfMemory => Err(evm::Error::OutOfGas)
2016-01-14 01:56:37 +01:00
},
InstructionCost::GasMemCopy(gas, mem_size, copy) => match mem_size {
RequiredMem::Mem(mem_size) => {
let (mem_gas, new_mem_size) = self.mem_gas_cost(schedule, mem.size(), &mem_size);
let copy_gas = U256::from(schedule.copy_gas) * (add_u256_usize(&copy, 31) / U256::from(32));
// Expand after calculating the cost
mem.expand(new_mem_size);
self.gas_add(try!(self.gas_add(gas, copy_gas)), mem_gas)
},
RequiredMem::OutOfMemory => Err(evm::Error::OutOfGas)
2016-01-13 17:11:00 +01:00
}
2016-01-14 01:56:37 +01:00
}
}
fn gas_add(&self, a: U256, b: U256) -> Result<U256, evm::Error> {
match a.overflowing_add(b) {
(_val, true) => Err(evm::Error::OutOfGas),
(val, false) => Ok(val)
}
}
2016-01-13 22:30:41 +01:00
2016-01-14 14:49:41 +01:00
fn mem_gas_cost(&self, schedule: &evm::Schedule, current_mem_size: usize, mem_size: &U256) -> (U256, usize) {
2016-01-13 22:30:41 +01:00
let gas_for_mem = |mem_size: usize| {
let s = mem_size / 32;
schedule.memory_gas * s + s * s / schedule.quad_coeff_div
};
2016-01-14 14:49:41 +01:00
let req_mem_size = mem_size.low_u64() as usize;
let req_mem_size_rounded = (req_mem_size + 31) / 32 * 32;
let new_mem_gas = gas_for_mem(req_mem_size_rounded);
2016-01-13 22:30:41 +01:00
let current_mem_gas = gas_for_mem(current_mem_size);
2016-01-14 14:49:41 +01:00
(if req_mem_size_rounded > current_mem_size {
U256::from(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()
}, req_mem_size_rounded)
2016-01-13 22:30:41 +01:00
}
fn mem_max(&self, m_a: RequiredMem, m_b: RequiredMem) -> RequiredMem {
match (m_a, m_b) {
(RequiredMem::Mem(a), RequiredMem::Mem(b)) => {
RequiredMem::Mem(cmp::max(a, b))
},
(RequiredMem::OutOfMemory, _) | (_, RequiredMem::OutOfMemory) => {
RequiredMem::OutOfMemory
}
}
}
fn mem_needed_const(&self, mem: &U256, add: usize) -> RequiredMem {
match mem.overflowing_add(U256::from(add)) {
(_, true) => RequiredMem::OutOfMemory,
(mem, false) => RequiredMem::Mem(mem)
}
}
fn mem_needed(&self, offset: &U256, size: &U256) -> RequiredMem {
2016-01-13 17:11:00 +01:00
if self.is_zero(size) {
RequiredMem::Mem(U256::zero())
} else {
let (result, overflow) = offset.clone().overflowing_add(size.clone());
if overflow {
RequiredMem::OutOfMemory
} else {
RequiredMem::Mem(result)
}
}
}
fn exec_instruction(&self,
2016-01-14 01:56:37 +01:00
gas: Gas,
params: &ActionParams,
ext: &mut evm::Ext,
instruction: Instruction,
code: &mut CodeReader,
mem: &mut Memory,
stack: &mut Stack<U256>
) -> Result<InstructionResult, evm::Error> {
match instruction {
instructions::JUMP => {
let jump = stack.pop_back();
return Ok(InstructionResult::JumpToPosition(
jump
));
},
instructions::JUMPI => {
let jump = stack.pop_back();
let condition = stack.pop_back();
2016-01-13 17:11:00 +01:00
if !self.is_zero(&condition) {
return Ok(InstructionResult::JumpToPosition(
jump
));
}
},
instructions::JUMPDEST => {
// ignore
},
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-15 02:16:04 +01:00
let (gas_left, maybe_address) = ext.create(&gas, &endowment, &contract_code);
match maybe_address {
2016-01-13 01:53:33 +01:00
Some(address) => stack.push(address_to_u256(address)),
None => stack.push(U256::zero())
}
return Ok(InstructionResult::AdditionalGasCost(
2016-01-14 00:39:59 +01:00
gas - gas_left
));
},
2016-01-14 00:39:59 +01:00
instructions::CALL | instructions::CALLCODE | instructions::DELEGATECALL => {
let call_gas = stack.pop_back();
let code_address = u256_to_address(&stack.pop_back());
let value = if instruction == instructions::DELEGATECALL {
params.value
} else {
stack.pop_back()
};
let address = if instruction == instructions::CALL {
&code_address
} else {
&params.address
};
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-15 02:16:04 +01:00
let (gas_left, call_successful) = {
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);
try!(
ext.call(&gas, &call_gas, address, &value, input, &code_address, output)
)
2016-01-14 00:39:59 +01:00
};
2016-01-15 02:16:04 +01:00
if call_successful {
stack.push(U256::one());
} else {
stack.push(U256::zero());
}
2016-01-14 00:39:59 +01:00
return Ok(InstructionResult::AdditionalGasCost(
gas - gas_left
));
},
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));
return Ok(InstructionResult::StopExecutionWithGasCost(
2016-01-14 00:39:59 +01:00
gas - gas_left
));
},
instructions::STOP => {
return Ok(InstructionResult::StopExecution);
},
instructions::SUICIDE => {
let address = stack.pop_back();
2016-01-14 00:39:59 +01:00
ext.suicide(&u256_to_address(&address));
return Ok(InstructionResult::StopExecution);
},
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)
.iter()
.map(H256::from)
.collect();
2016-01-15 16:16:16 +01:00
ext.log(topics, mem.read_slice(offset, size).to_vec());
},
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));
},
instructions::MSTORE => {
2016-01-13 22:30:41 +01:00
let offset = stack.pop_back();
let word = stack.pop_back();
mem.write(offset, word);
},
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);
},
instructions::MSIZE => {
2016-01-13 22:30:41 +01:00
stack.push(U256::from(mem.size()));
},
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()));
},
instructions::SLOAD => {
let key = H256::from(&stack.pop_back());
let word = U256::from(ext.sload(&key).as_slice());
stack.push(word);
},
instructions::SSTORE => {
let key = H256::from(&stack.pop_back());
let word = H256::from(&stack.pop_back());
ext.sstore(key, word);
},
instructions::PC => {
2016-01-14 16:32:28 +01:00
stack.push(U256::from(code.position - 1));
},
instructions::GAS => {
stack.push(gas.clone());
},
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 => {
stack.push(params.value.clone());
},
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);
let bound = cmp::min(params.data.len(), max);
2016-01-15 20:19:46 +01:00
if id < bound && big_id < U256::from(params.data.len()) {
2016-01-15 20:14:16 +01:00
let mut v = params.data[id..bound].to_vec();
v.resize(32, 0);
stack.push(U256::from(&v[..]))
} else {
stack.push(U256::zero())
}
},
2016-01-13 01:53:33 +01:00
instructions::CALLDATASIZE => {
stack.push(U256::from(params.data.len()));
},
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 => {
self.copy_data_to_memory(mem, stack, &params.data);
},
instructions::CODECOPY => {
self.copy_data_to_memory(mem, stack, &params.code);
},
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-14 02:45:16 +01:00
try!(self.exec_stack_instruction(instruction, stack));
}
};
Ok(InstructionResult::AdditionalGasCost(U256::zero()))
}
2016-01-13 22:30:41 +01:00
fn copy_data_to_memory(&self,
2016-01-14 01:56:37 +01:00
mem: &mut Memory,
stack: &mut Stack<U256>,
data: &Bytes) {
2016-01-13 22:30:41 +01:00
let offset = stack.pop_back();
2016-01-15 20:14:16 +01:00
let index = stack.pop_back();
let size = stack.pop_back();
let data_size = data.len();
2016-01-13 22:30:41 +01:00
2016-01-15 20:14:16 +01:00
if index < U256::from(data_size) {
let u_index = index.low_u64() as usize;
let bound_size = if size + index > U256::from(data_size) {
data_size
} else {
size.low_u64() as usize + u_index
};
mem.write_slice(offset, &data[u_index..bound_size]);
}
2016-01-13 22:30:41 +01:00
}
2016-01-13 17:11:00 +01:00
fn verify_instructions_requirements(&self,
2016-01-14 01:56:37 +01:00
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,
wanted: info.args,
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,
wanted: info.ret - info.args,
limit: stack_limit
})
2016-01-13 17:11:00 +01:00
} else {
Ok(())
}
}
fn verify_gas(&self, current_gas: &U256, gas_cost: &U256) -> Result<(), evm::Error> {
if current_gas < gas_cost {
Err(evm::Error::OutOfGas)
} else {
Ok(())
}
}
fn verify_jump(&self, jump_u: U256, valid_jump_destinations: &HashSet<usize>) -> Result<usize, evm::Error> {
let jump = jump_u.low_u64() as usize;
if valid_jump_destinations.contains(&jump) {
Ok(jump)
} else {
Err(evm::Error::BadJumpDestination {
destination: jump
})
}
}
2016-01-13 17:11:00 +01:00
fn is_zero(&self, val: &U256) -> bool {
&U256::zero() == val
}
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-14 02:45:16 +01:00
fn exec_stack_instruction(&self, instruction: Instruction, stack : &mut Stack<U256>) -> Result<(), evm::Error> {
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();
},
instructions::ADD => {
let a = stack.pop_back();
let b = stack.pop_back();
2016-01-15 02:16:04 +01:00
let (c, _overflow) = a.overflowing_add(b);
2016-01-15 01:48:53 +01:00
stack.push(c);
},
2016-01-13 01:53:33 +01:00
instructions::MUL => {
let a = stack.pop_back();
let b = stack.pop_back();
2016-01-15 02:16:04 +01:00
let (c, _overflow) = a.overflowing_mul(b);
2016-01-15 01:48:53 +01:00
stack.push(c);
2016-01-13 01:53:33 +01:00
},
instructions::SUB => {
let a = stack.pop_back();
let b = stack.pop_back();
2016-01-15 02:16:04 +01:00
let (c, _overflow) = a.overflowing_sub(b);
2016-01-15 01:48:53 +01:00
stack.push(c);
2016-01-13 01:53:33 +01:00
},
instructions::DIV => {
let a = stack.pop_back();
let b = stack.pop_back();
2016-01-15 01:41:49 +01:00
stack.push(if !self.is_zero(&b) {
2016-01-15 02:16:04 +01:00
let (c, _overflow) = a.overflowing_div(b);
2016-01-15 01:48:53 +01:00
c
2016-01-13 01:53:33 +01:00
} else {
U256::zero()
});
},
instructions::MOD => {
let a = stack.pop_back();
let b = stack.pop_back();
2016-01-15 01:41:49 +01:00
stack.push(if !self.is_zero(&b) {
2016-01-15 02:16:04 +01:00
let (c, _overflow) = a.overflowing_rem(b);
2016-01-15 01:48:53 +01:00
c
2016-01-13 01:53:33 +01:00
} else {
U256::zero()
});
},
instructions::SDIV => {
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
// -2^255
let min = U256::from_str("7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff").unwrap();
stack.push(if self.is_zero(&b) {
2016-01-15 16:04:18 +01:00
U256::zero()
} else if a == min && b == !U256::zero() {
min
2016-01-15 16:04:18 +01:00
} else {
let (c, _overflow) = a.overflowing_div(b);
set_sign(c, sign_a ^ sign_b)
});
},
instructions::SMOD => {
let ua = stack.pop_back();
2016-01-15 16:04:18 +01:00
let ub = stack.pop_back();
let (a, sign_a) = get_and_reset_sign(ua);
let (b, _sign_b) = get_and_reset_sign(ub);
stack.push(if !self.is_zero(&b) {
let (c, _overflow) = a.overflowing_rem(b);
set_sign(c, sign_a)
} else {
U256::zero()
});
},
instructions::EXP => {
let base = stack.pop_back();
let expon = stack.pop_back();
2016-01-15 14:48:07 +01:00
let (res, _overflow) = base.overflowing_pow(expon);
stack.push(res);
},
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));
},
instructions::SLT => {
let (a, neg_a) = get_and_reset_sign(stack.pop_back());
let (b, neg_b) = get_and_reset_sign(stack.pop_back());
let is_positive_lt = a < b && (neg_a | neg_b) == false;
let is_negative_lt = a > b && (neg_a & neg_b) == true;
let has_different_signs = neg_a == true && neg_b == false;
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));
},
instructions::SGT => {
let (a, neg_a) = get_and_reset_sign(stack.pop_back());
let (b, neg_b) = get_and_reset_sign(stack.pop_back());
let is_positive_gt = a > b && (neg_a | neg_b) == false;
let is_negative_gt = a < b && (neg_a & neg_b) == true;
let has_different_signs = neg_a == false && neg_b == true;
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);
},
instructions::BYTE => {
let word = stack.pop_back();
2016-01-15 19:48:51 +01:00
let val = stack.pop_back();
let byte = if word < U256::from(32) {
2016-01-15 19:48:51 +01:00
(val >> (8 * (31 - word.low_u64() as usize))) & U256::from(0xff)
} else {
U256::zero()
};
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);
let (res, _overflow) = a5.overflowing_add(U512::from(b));
let (x, _overflow) = res.overflowing_rem(U512::from(c));
U256::from(x)
} 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);
let (res, _overflow) = a5.overflowing_mul(U512::from(b));
let (x, _overflow) = res.overflowing_rem(U512::from(c));
U256::from(x)
} else {
U256::zero()
});
},
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-14 02:45:16 +01:00
Ok(())
2016-01-14 01:56:37 +01:00
}
2016-01-14 01:56:37 +01:00
fn find_jump_destinations(&self, code : &Bytes) -> HashSet<CodePosition> {
let mut jump_dests = HashSet::new();
let mut position = 0;
while position < code.len() {
2016-01-14 01:56:37 +01:00
let instruction = code[position];
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-14 01:56:37 +01:00
return jump_dests;
}
2016-01-13 01:53:33 +01:00
}
fn get_and_reset_sign(value: U256) -> (U256, bool) {
let sign = (value >> 255).low_u64() == 1;
(set_sign(value, sign), sign)
}
pub fn set_sign(value: U256, sign: bool) -> U256 {
if sign {
let (val, _overflow) = (!U256::zero() ^ value).overflowing_add(U256::one());
val
} else {
value
}
}
2016-01-13 17:11:00 +01:00
fn add_u256_usize(value: &U256, num: usize) -> U256 {
value.clone() + U256::from(num)
}
2016-01-13 01:53:33 +01:00
fn u256_to_address(value: &U256) -> Address {
Address::from(H256::from(value))
}
fn address_to_u256(value: Address) -> U256 {
U256::from(H256::from(value).as_slice())
}
#[cfg(test)]
mod tests {
use common::*;
use super::*;
2016-01-14 14:49:41 +01:00
use evm;
#[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);
// when
let (mem_cost, mem_size) = interpreter.mem_gas_cost(&schedule, current_mem_size, &mem_size);
// 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
let mem : &mut super::Memory = &mut vec![];
mem.resize(32);
2016-01-13 22:30:41 +01:00
// when
mem.write(U256::from(0x00), U256::from(0xabcdef));
// then
assert_eq!(mem.read(U256::from(0x00)), U256::from(0xabcdef));
2016-01-13 22:30:41 +01:00
}
#[test]
fn test_memory_read_and_write_byte() {
// given
let mem : &mut super::Memory = &mut vec![];
mem.resize(32);
2016-01-13 22:30:41 +01:00
// when
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
assert_eq!(mem.read(U256::from(0x00)), U256::from(0xabcdef));
2016-01-13 22:30:41 +01:00
}
}