Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Changes from 1 commit
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
4f5c5b6
Introduce first groundwork for Wasm executor.
gavofyork Dec 5, 2017
6237848
Remove old Rust-runtime code.
gavofyork Dec 6, 2017
293cf5d
Avoid commiting compled files.
gavofyork Dec 6, 2017
66b636e
Add runtime precompile.
gavofyork Dec 6, 2017
c7e7456
Rename so module makes more sense.
gavofyork Dec 6, 2017
0cfd67e
Further renaming.
gavofyork Dec 6, 2017
3011edf
Ensure tests work.
gavofyork Dec 6, 2017
a74b4fa
Allow bringing in of externalities.
gavofyork Dec 7, 2017
6dac275
Nice macros for imports.
gavofyork Dec 9, 2017
08f7b26
Allow passing in of data through allocators.
gavofyork Dec 11, 2017
1ace6b2
Can now pass in bytes to WasmExecutor.
gavofyork Dec 11, 2017
27e5fed
Additional cleanup.
gavofyork Dec 11, 2017
13dcd89
Switch usages of `OutData` to `u64`
gavofyork Dec 11, 2017
84b9f84
convert to safe but extremely verbose type conversion.
gavofyork Dec 11, 2017
7f31899
Remove StaticExternalities distinction.
gavofyork Dec 11, 2017
edf061e
Remove another unused use.
gavofyork Dec 11, 2017
edb6bea
Refactor wasm utils out
gavofyork Dec 11, 2017
9f59f48
Remove extraneous copies that weren't really testing anything.
gavofyork Dec 11, 2017
287b29d
Try to use wasm 0.15
gavofyork Dec 31, 2017
1bd55fe
Make it work!
gavofyork Dec 31, 2017
36e254a
Call-time externalities working.
gavofyork Jan 1, 2018
cd651a3
Add basic externalities.
gavofyork Jan 1, 2018
a8f9cca
Merge branch 'with-wasm-0.15' into with-wasm
gavofyork Jan 1, 2018
4404846
Fix grumbles and note unwraps to be sorted.
gavofyork Jan 1, 2018
b1d963a
Test storage externality.
gavofyork Jan 3, 2018
319d9c0
Fix nits.
gavofyork Jan 3, 2018
7ec9221
Merge branch 'master' into with-wasm
gavofyork Jan 3, 2018
2934d94
Compile collation logic.
gavofyork Jan 3, 2018
5998aa1
Move back to refs. Yey.
gavofyork Jan 3, 2018
3f4085a
Remove "object" id for storage access.
gavofyork Jan 4, 2018
4be0537
Fix test.
gavofyork Jan 4, 2018
01d7019
Fix up rest of tests.
gavofyork Jan 4, 2018
db1adee
remove unwrap.
gavofyork Jan 4, 2018
87c54f7
Expose set/get code in externalities
gavofyork Jan 5, 2018
471ea1e
Add validator set.
gavofyork Jan 5, 2018
fa35993
Introduce validator set into externalities and test.
gavofyork Jan 5, 2018
a0f64df
Add another external function.
gavofyork Jan 6, 2018
e736d46
Remove code and validators; use storage for everything.
gavofyork Jan 6, 2018
234297c
Introduce validators function.
gavofyork Jan 6, 2018
3f8a96d
Tests (and a fix) for the validators getter.
gavofyork Jan 6, 2018
6636520
Allow calls into runtime to return data.
gavofyork Jan 7, 2018
964659e
Remove unneeded trace.
gavofyork Jan 7, 2018
8ca1b7b
Make runtime printing a bit nicer.
gavofyork Jan 7, 2018
74156a2
Create separate runtimes for testing and polkadot.
gavofyork Jan 8, 2018
611a7ac
Remove commented code.
gavofyork Jan 8, 2018
c3afecc
Use new path.
gavofyork Jan 8, 2018
ea4d6c5
Refactor into shared support module.
gavofyork Jan 8, 2018
709693d
Fix warning.
gavofyork Jan 8, 2018
ec1e6b6
Remove unwraps.
gavofyork Jan 8, 2018
5c0ec3d
Make macro a little less unhygenic.
gavofyork Jan 8, 2018
79ab46f
Add wasm files.
gavofyork Jan 8, 2018
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
Prev Previous commit
Next Next commit
Can now pass in bytes to WasmExecutor.
  • Loading branch information
gavofyork committed Dec 11, 2017
commit 1ace6b2204b457818524c3a5b2ad8e059d89ceb8
134 changes: 99 additions & 35 deletions executor/src/wasm_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,21 @@ use state_machine::{Externalities, CodeExecutor};

use error::{Error, ErrorKind, Result};

use std::sync::{Weak};

fn program_with_externals<E: parity_wasm::interpreter::UserFunctionExecutor + 'static>(externals: parity_wasm::interpreter::UserDefinedElements<E>, module_name: &str) -> result::Result<parity_wasm::ProgramInstance, parity_wasm::interpreter::Error> {
let program = parity_wasm::ProgramInstance::new();
let instance = {
let module = parity_wasm::builder::module().build();
let mut instance = parity_wasm::ModuleInstance::new(Weak::default(), module_name.into(), module)?;
instance.instantiate(None)?;
instance
};
let other_instance = parity_wasm::interpreter::native_module(Arc::new(instance), externals)?;
program.insert_loaded_module(module_name, other_instance)?;
Ok(program)
}

pub trait ConvertibleToWasm { const VALUE_TYPE: parity_wasm::elements::ValueType; type NativeType; fn to_runtime_value(self) -> parity_wasm::interpreter::RuntimeValue; }
impl ConvertibleToWasm for i32 { type NativeType = i32; const VALUE_TYPE: parity_wasm::elements::ValueType = parity_wasm::elements::ValueType::I32; fn to_runtime_value(self) -> parity_wasm::interpreter::RuntimeValue { parity_wasm::interpreter::RuntimeValue::I32(self) } }
impl ConvertibleToWasm for u32 { type NativeType = u32; const VALUE_TYPE: parity_wasm::elements::ValueType = parity_wasm::elements::ValueType::I32; fn to_runtime_value(self) -> parity_wasm::interpreter::RuntimeValue { parity_wasm::interpreter::RuntimeValue::I32(self as i32) } }
Expand All @@ -44,8 +59,8 @@ macro_rules! convert_args {

#[macro_export]
macro_rules! convert_fn {
( $name:ident ( $( $params:ty ),* ) ) => ( UserFunctionDescriptor::Static(stringify!($name), &convert_args!($($params),*), None) );
( $name:ident ( $( $params:ty ),* ) -> $returns:ty ) => ( UserFunctionDescriptor::Static(stringify!($name), &convert_args!($($params),*), Some(<$returns>::VALUE_TYPE) ) );
( $name:ident ( $( $params:ty ),* ) ) => ( parity_wasm::interpreter::UserFunctionDescriptor::Static(stringify!($name), &convert_args!($($params),*), None) );
( $name:ident ( $( $params:ty ),* ) -> $returns:ty ) => ( parity_wasm::interpreter::UserFunctionDescriptor::Static(stringify!($name), &convert_args!($($params),*), Some(<$returns>::VALUE_TYPE) ) );
}

#[macro_export]
Expand Down Expand Up @@ -83,7 +98,7 @@ macro_rules! marshall {
#[macro_export]
macro_rules! dispatch {
( $objectname:ident, $( $name:ident ( $( $names:ident : $params:ty ),* ) $( -> $returns:ty )* => $body:tt ),* ) => (
fn execute(&mut self, name: &str, context: CallerContext)
fn execute(&mut self, name: &str, context: parity_wasm::interpreter::CallerContext)
-> result::Result<Option<parity_wasm::interpreter::RuntimeValue>, parity_wasm::interpreter::Error> {
let $objectname = self;
match name {
Expand All @@ -99,7 +114,7 @@ macro_rules! dispatch {
#[macro_export]
macro_rules! signatures {
( $( $name:ident ( $( $params:ty ),* ) $( -> $returns:ty )* ),* ) => (
const SIGNATURES: &'static [UserFunctionDescriptor] = &[
const SIGNATURES: &'static [parity_wasm::interpreter::UserFunctionDescriptor] = &[
$(
convert_fn!( $name ( $( $params ),* ) $( -> $returns )* ),
)*
Expand All @@ -110,7 +125,7 @@ macro_rules! signatures {
#[macro_export]
macro_rules! function_executor {
( $objectname:ident : $structname:ident, $( $name:ident ( $( $names:ident : $params:ty ),* ) $( -> $returns:ty )* => $body:tt ),* ) => (
impl UserFunctionExecutor for $structname {
impl parity_wasm::interpreter::UserFunctionExecutor for $structname {
dispatch!($objectname, $( $name( $( $names : $params ),* ) $( -> $returns )* => $body ),*);
}
impl $structname {
Expand All @@ -119,6 +134,57 @@ macro_rules! function_executor {
);
}

use std::result;
use std::sync::{Arc, Mutex};

// user function executor
#[derive(Default)]
struct FunctionExecutor {
context: Arc<Mutex<Option<FEContext>>>,
}

struct FEContext {
heap_end: u32,
memory: Arc<parity_wasm::interpreter::MemoryInstance>,
}

impl FEContext {
fn new(m: &Arc<parity_wasm::interpreter::ModuleInstance>) -> Self {
use parity_wasm::ModuleInstanceInterface;
FEContext { heap_end: 1024, memory: Arc::clone(&m.memory(parity_wasm::interpreter::ItemIndex::Internal(0)).unwrap()) }
}
fn allocate(&mut self, size: u32) -> u32 {
let r = self.heap_end;
self.heap_end += size;
r
}
fn deallocate(&mut self, _offset: u32) {
}
}

function_executor!(this: FunctionExecutor,
imported(n: u64) -> u64 => { println!("imported {:?}", n); n + 1 },
ext_memcpy(dest: *mut u8, src: *const u8, count: usize) -> *mut u8 => {
let mut context = this.context.lock().unwrap();
context.as_mut().unwrap().memory.copy_nonoverlapping(src as usize, dest as usize, count as usize).unwrap();
println!("memcpy {} from {}, {} bytes", dest, src, count);
dest
},
ext_memmove(dest: *mut u8, src: *const u8, count: usize) -> *mut u8 => { println!("memmove {} from {}, {} bytes", dest, src, count); dest },
ext_memset(dest: *mut u8, val: i32, count: usize) -> *mut u8 => { println!("memset {} with {}, {} bytes", dest, val, count); dest },
Copy link
Contributor

Choose a reason for hiding this comment

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

this function and others are a little weird because although it returns pointers, those pointers are not actually safe to deref since they are just offsets in the heap memory instance. How is code meant to actually read from the written memory?

Copy link
Member Author

Choose a reason for hiding this comment

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

so all function params are mapped to wasm-native types (i8/u8/i32/i64/u32/u64) for the code. most types are left alone, but pointers become i32s. the declared type is what is implemented as far as the rust compiler that generates the wasm code is concerned. it basically means you can copy/paste your rust-side extern declarations here and implement them.

ext_malloc(size: usize) -> *mut u8 => {
let mut context = this.context.lock().unwrap();
let r = context.as_mut().unwrap().allocate(size);
println!("malloc {} bytes at {}", size, r);
r
},
ext_free(addr: *mut u8) => {
let mut context = this.context.lock().unwrap();
context.as_mut().unwrap().deallocate(addr);
println!("free {}", addr)
}
);


/// Dummy rust executor for contracts.
///
Expand All @@ -137,7 +203,6 @@ impl CodeExecutor for WasmExecutor {
method: &str,
data: &CallData,
) -> Result<OutData> {

// TODO: avoid copying code by requiring code to remain immutable through execution,
// splitting it off from potentially mutable externalities.
let code = match ext.code() {
Expand All @@ -146,12 +211,31 @@ impl CodeExecutor for WasmExecutor {
};

use parity_wasm::ModuleInstanceInterface;
use parity_wasm::RuntimeValue::{I64};
let program = parity_wasm::ProgramInstance::new();
use parity_wasm::interpreter::UserDefinedElements;
use parity_wasm::RuntimeValue::I32;
use std::collections::HashMap;

let fe_context = Arc::new(Mutex::new(None));
Copy link
Contributor

Choose a reason for hiding this comment

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

lots of lock().unwrap(); parking_lot mutexes don't poison

let externals = UserDefinedElements {
executor: Some(FunctionExecutor { context: Arc::clone(&fe_context) }),
globals: HashMap::new(),
functions: ::std::borrow::Cow::from(FunctionExecutor::SIGNATURES),
};

let program = program_with_externals(externals, "env").unwrap();
let module = parity_wasm::deserialize_buffer(code).expect("Failed to load module");
let module = program.add_module("main", module, None).expect("Failed to initialize module");
module.execute_export(method, vec![I64(data.0.len() as i64)].into())
.map(|o| OutData(vec![1; if let Some(I64(l)) = o { l as usize } else { 0 }]))
let module = program.add_module("test", module, None).expect("Failed to initialize module");
Copy link
Contributor

@rphmeier rphmeier Dec 12, 2017

Choose a reason for hiding this comment

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

these errors should be handled

*fe_context.lock().unwrap() = Some(FEContext::new(&module));

let size = data.0.len() as u32;
let offset = fe_context.lock().unwrap().as_mut().unwrap().allocate(size);
module.memory(parity_wasm::interpreter::ItemIndex::Internal(0)).unwrap().set(offset, &data.0).unwrap();

module.execute_export(method, vec![I32(offset as i32), I32(size as i32)].into())
.map(|o| {
// TODO: populate vec properly
OutData(vec![1; if let Some(I32(l)) = o { l as usize } else { 0 }])
})
.map_err(|_| ErrorKind::Runtime.into())
}
}
Expand Down Expand Up @@ -188,9 +272,11 @@ mod tests {
}

use std::result;
use std::sync::{Arc, Weak, Mutex};
use std::sync::{Arc, Mutex};
use std::mem::transmute;
use parity_wasm::interpreter::{CallerContext, MemoryInstance, UserDefinedElements, UserFunctionExecutor, UserFunctionDescriptor};
use parity_wasm::interpreter::{MemoryInstance, UserDefinedElements};
use parity_wasm::ModuleInstanceInterface;
use parity_wasm::RuntimeValue::{I32, I64};

// user function executor
#[derive(Default)]
Expand Down Expand Up @@ -236,24 +322,8 @@ mod tests {
}
);

fn program_with_externals<E: UserFunctionExecutor + 'static>(externals: UserDefinedElements<E>, module_name: &str) -> result::Result<parity_wasm::ProgramInstance, parity_wasm::interpreter::Error> {
let program = parity_wasm::ProgramInstance::new();
let instance = {
let module = parity_wasm::builder::module().build();
let mut instance = parity_wasm::ModuleInstance::new(Weak::default(), module_name.into(), module)?;
instance.instantiate(None)?;
instance
};
let other_instance = parity_wasm::interpreter::native_module(Arc::new(instance), externals)?;
program.insert_loaded_module(module_name, other_instance)?;
Ok(program)
}

#[test]
fn should_pass_freeable_data() {
use parity_wasm::ModuleInstanceInterface;
use parity_wasm::RuntimeValue::{I32};

let fe_context = Arc::new(Mutex::new(None));
let externals = UserDefinedElements {
executor: Some(FunctionExecutor { context: Arc::clone(&fe_context) }),
Expand Down Expand Up @@ -281,9 +351,6 @@ mod tests {

#[test]
fn should_provide_externalities() {
use parity_wasm::ModuleInstanceInterface;
use parity_wasm::RuntimeValue::{I64};

let fe_context = Arc::new(Mutex::new(None));
let externals = UserDefinedElements {
executor: Some(FunctionExecutor { context: Arc::clone(&fe_context) }),
Expand All @@ -310,9 +377,6 @@ mod tests {

#[test]
fn should_run_wasm() {
use parity_wasm::ModuleInstanceInterface;
use parity_wasm::RuntimeValue::{I64};

let program = parity_wasm::ProgramInstance::new();
let test_module = include_bytes!("../../runtime/target/wasm32-unknown-unknown/release/runtime.wasm");
let module = parity_wasm::deserialize_buffer(test_module.to_vec()).expect("Failed to load module");
Expand Down