Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Commit

Permalink
EIP-211 updates
Browse files Browse the repository at this point in the history
  • Loading branch information
arkpar committed Jun 16, 2017
1 parent 4a1f205 commit ae68dc3
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 0 deletions.
3 changes: 3 additions & 0 deletions ethcore/src/evm/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ pub enum Error {
BuiltIn(&'static str),
/// When execution tries to modify the state in static context
MutableCallInStaticContext,
/// Out of bounds access in RETURNDATACOPY.
OutOfBounds,
/// Likely to cause consensus issues.
Internal(String),
}
Expand All @@ -85,6 +87,7 @@ impl fmt::Display for Error {
use self::Error::*;
match *self {
OutOfGas => write!(f, "Out of gas"),
OutOfBounds => write!(f, "Out of bounds"),
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),
Expand Down
14 changes: 14 additions & 0 deletions ethcore/src/evm/interpreter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,9 @@ impl<Cost: CostType> Interpreter<Cost> {
let contract_code = self.mem.read_slice(init_off, init_size);
let can_create = ext.balance(&params.address)? >= endowment && ext.depth() < ext.schedule().max_depth;

// clear return data buffer before crearing new call frame.
self.return_data = ReturnData::empty();

if !can_create {
stack.push(U256::zero());
return Ok(InstructionResult::UnusedGas(create_gas));
Expand Down Expand Up @@ -353,6 +356,9 @@ impl<Cost: CostType> Interpreter<Cost> {
_ => panic!(format!("Unexpected instruction {} in CALL branch.", instruction))
};

// clear return data buffer before crearing new call frame.
self.return_data = ReturnData::empty();

let can_call = has_balance && ext.depth() < ext.schedule().max_depth;
if !can_call {
stack.push(U256::zero());
Expand Down Expand Up @@ -514,6 +520,14 @@ impl<Cost: CostType> Interpreter<Cost> {
Self::copy_data_to_memory(&mut self.mem, stack, params.data.as_ref().map_or_else(|| &[] as &[u8], |d| &*d as &[u8]));
},
instructions::RETURNDATACOPY => {
{
let source_offset = stack.peek(1);
let size = stack.peek(2);
let return_data_len = U256::from(self.return_data.len());
if source_offset <= &return_data_len && source_offset.overflow_add(*size).0 <= return_data_len {
return Err(evm::Error::OutOfBounds);
}
}
Self::copy_data_to_memory(&mut self.mem, stack, &*self.return_data);
},
instructions::CODECOPY => {
Expand Down
1 change: 1 addition & 0 deletions ethcore/src/executive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,7 @@ impl<'a, B: 'a + StateBackend, E: Engine + ?Sized> Executive<'a, B, E> {
| Err(evm::Error::BuiltIn {..})
| Err(evm::Error::OutOfStack {..})
| Err(evm::Error::MutableCallInStaticContext)
| Err(evm::Error::OutOfBounds)
| Ok(FinalizationResult { apply_state: false, .. }) => {
self.state.revert_to_checkpoint();
},
Expand Down
6 changes: 6 additions & 0 deletions ethcore/src/types/trace_types/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ pub enum Error {
Internal,
/// When execution tries to modify the state in static context
MutableCallInStaticContext,
/// Contract tried to access past the return data buffer.
OutOfBounds,
}

impl<'a> From<&'a EvmError> for Error {
Expand All @@ -55,6 +57,7 @@ impl<'a> From<&'a EvmError> for Error {
EvmError::BuiltIn { .. } => Error::BuiltIn,
EvmError::Internal(_) => Error::Internal,
EvmError::MutableCallInStaticContext => Error::MutableCallInStaticContext,
EvmError::OutOfBounds => Error::OutOfBounds,
}
}
}
Expand All @@ -77,6 +80,7 @@ impl fmt::Display for Error {
BuiltIn => "Built-in failed",
Internal => "Internal error",
MutableCallInStaticContext => "Mutable Call In Static Context",
OutOfBounds => "Out of bounds",
};
message.fmt(f)
}
Expand All @@ -94,6 +98,7 @@ impl Encodable for Error {
Internal => 5,
BuiltIn => 6,
MutableCallInStaticContext => 7,
OutOfBounds => 8,
};

s.append_internal(&value);
Expand All @@ -113,6 +118,7 @@ impl Decodable for Error {
5 => Ok(Internal),
6 => Ok(BuiltIn),
7 => Ok(MutableCallInStaticContext),
8 => Ok(OutOfBounds),
_ => Err(DecoderError::Custom("Invalid error type")),
}
}
Expand Down

0 comments on commit ae68dc3

Please sign in to comment.