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

Commit

Permalink
WASM Runtime refactoring (#6596)
Browse files Browse the repository at this point in the history
* refactoring to new pwasm-std

* pass reference

* remove ref

* missing underscores
  • Loading branch information
NikVolf authored and arkpar committed Oct 4, 2017
1 parent ec1a892 commit 4260910
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 113 deletions.
2 changes: 1 addition & 1 deletion ethcore/res/wasm-tests
63 changes: 0 additions & 63 deletions ethcore/wasm/src/call_args.rs

This file was deleted.

20 changes: 20 additions & 0 deletions ethcore/wasm/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,26 @@ pub const SIGNATURES: &'static [UserFunctionDescriptor] = &[
&[I32],
None,
),
Static(
"_sender",
&[I32],
None,
),
Static(
"_origin",
&[I32],
None,
),
Static(
"_address",
&[I32],
None,
),
Static(
"_value",
&[I32],
None,
),
Static(
"_timestamp",
&[],
Expand Down
19 changes: 8 additions & 11 deletions ethcore/wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ extern crate wasm_utils;

mod runtime;
mod ptr;
mod call_args;
mod result;
#[cfg(test)]
mod tests;
Expand Down Expand Up @@ -107,7 +106,12 @@ impl vm::Vm for WasmInterpreter {
env_memory,
DEFAULT_STACK_SPACE,
params.gas.low_u64(),
RuntimeContext::new(params.address, params.sender),
RuntimeContext {
address: params.address,
sender: params.sender,
origin: params.origin,
value: params.value.value(),
},
&self.program,
);

Expand All @@ -121,15 +125,8 @@ impl vm::Vm for WasmInterpreter {
})?
);

let d_ptr = runtime.write_descriptor(
call_args::CallArgs::new(
params.address,
params.sender,
params.origin,
params.value.value(),
params.data.unwrap_or(Vec::with_capacity(0)),
)
).map_err(|e| Error(e))?;
let d_ptr = runtime.write_descriptor(&params.data.unwrap_or_default())
.map_err(Error)?;

{
let execution_params = runtime.execution_params()
Expand Down
71 changes: 52 additions & 19 deletions ethcore/wasm/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ use util::Address;

use vm::CallType;
use super::ptr::{WasmPtr, Error as PtrError};
use super::call_args::CallArgs;

/// User trap in native code
#[derive(Debug, Clone, PartialEq)]
Expand Down Expand Up @@ -97,17 +96,10 @@ impl From<PtrError> for UserTrap {
}

pub struct RuntimeContext {
address: Address,
sender: Address,
}

impl RuntimeContext {
pub fn new(address: Address, sender: Address) -> Self {
RuntimeContext {
address: address,
sender: sender,
}
}
pub address: Address,
pub sender: Address,
pub origin: Address,
pub value: U256,
}

/// Runtime enviroment data for wasm contract execution
Expand Down Expand Up @@ -442,10 +434,10 @@ impl<'a, 'b> Runtime<'a, 'b> {
}

/// Write call descriptor to wasm memory
pub fn write_descriptor(&mut self, call_args: CallArgs) -> Result<WasmPtr, InterpreterError> {
pub fn write_descriptor(&mut self, input: &[u8]) -> Result<WasmPtr, InterpreterError> {
let d_ptr = self.alloc(16)?;

let args_len = call_args.len();
let args_len = input.len() as u32;
let args_ptr = self.alloc(args_len)?;

// write call descriptor
Expand All @@ -457,11 +449,7 @@ impl<'a, 'b> Runtime<'a, 'b> {
self.memory.set(d_ptr, &d_buf)?;

// write call args to memory
self.memory.set(args_ptr, &call_args.address)?;
self.memory.set(args_ptr+20, &call_args.sender)?;
self.memory.set(args_ptr+40, &call_args.origin)?;
self.memory.set(args_ptr+60, &call_args.value)?;
self.memory.set(args_ptr+92, &call_args.data)?;
self.memory.set(args_ptr, input)?;

Ok(d_ptr.into())
}
Expand Down Expand Up @@ -559,6 +547,39 @@ impl<'a, 'b> Runtime<'a, 'b> {
Ok(None)
}

fn sender(&mut self, context: InterpreterCallerContext)
-> Result<Option<interpreter::RuntimeValue>, InterpreterError>
{
let return_ptr = context.value_stack.pop_as::<i32>()? as u32;
self.memory.set(return_ptr, &*self.context.sender)?;
Ok(None)
}

fn address(&mut self, context: InterpreterCallerContext)
-> Result<Option<interpreter::RuntimeValue>, InterpreterError>
{
let return_ptr = context.value_stack.pop_as::<i32>()? as u32;
self.memory.set(return_ptr, &*self.context.address)?;
Ok(None)
}

fn origin(&mut self, context: InterpreterCallerContext)
-> Result<Option<interpreter::RuntimeValue>, InterpreterError>
{
let return_ptr = context.value_stack.pop_as::<i32>()? as u32;
self.memory.set(return_ptr, &*self.context.origin)?;
Ok(None)
}

fn value(&mut self, context: InterpreterCallerContext)
-> Result<Option<interpreter::RuntimeValue>, InterpreterError>
{
let return_ptr = context.value_stack.pop_as::<i32>()? as u32;
let value: H256 = self.context.value.clone().into();
self.memory.set(return_ptr, &*value)?;
Ok(None)
}

fn timestamp(&mut self, _context: InterpreterCallerContext)
-> Result<Option<interpreter::RuntimeValue>, InterpreterError>
{
Expand Down Expand Up @@ -691,6 +712,18 @@ impl<'a, 'b> interpreter::UserFunctionExecutor<UserTrap> for Runtime<'a, 'b> {
"_gaslimit" => {
self.ext_gas_limit(context)
},
"_sender" => {
self.sender(context)
},
"_address" => {
self.address(context)
},
"_origin" => {
self.origin(context)
},
"_value" => {
self.value(context)
},
_ => {
trace!(target: "wasm", "Trapped due to unhandled function: '{}'", name);
Ok(self.unknown_trap(context)?)
Expand Down
Loading

0 comments on commit 4260910

Please sign in to comment.