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

WASM Runtime refactoring #6596

Merged
merged 4 commits into from
Oct 4, 2017
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why doesn't context.value_stack.pop_as::<u32>()? work?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's not native webassembly type
there are only four i32, i64, f32, f64

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