This repository was archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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 6237848
Remove old Rust-runtime code.
gavofyork 293cf5d
Avoid commiting compled files.
gavofyork 66b636e
Add runtime precompile.
gavofyork c7e7456
Rename so module makes more sense.
gavofyork 0cfd67e
Further renaming.
gavofyork 3011edf
Ensure tests work.
gavofyork a74b4fa
Allow bringing in of externalities.
gavofyork 6dac275
Nice macros for imports.
gavofyork 08f7b26
Allow passing in of data through allocators.
gavofyork 1ace6b2
Can now pass in bytes to WasmExecutor.
gavofyork 27e5fed
Additional cleanup.
gavofyork 13dcd89
Switch usages of `OutData` to `u64`
gavofyork 84b9f84
convert to safe but extremely verbose type conversion.
gavofyork 7f31899
Remove StaticExternalities distinction.
gavofyork edf061e
Remove another unused use.
gavofyork edb6bea
Refactor wasm utils out
gavofyork 9f59f48
Remove extraneous copies that weren't really testing anything.
gavofyork 287b29d
Try to use wasm 0.15
gavofyork 1bd55fe
Make it work!
gavofyork 36e254a
Call-time externalities working.
gavofyork cd651a3
Add basic externalities.
gavofyork a8f9cca
Merge branch 'with-wasm-0.15' into with-wasm
gavofyork 4404846
Fix grumbles and note unwraps to be sorted.
gavofyork b1d963a
Test storage externality.
gavofyork 319d9c0
Fix nits.
gavofyork 7ec9221
Merge branch 'master' into with-wasm
gavofyork 2934d94
Compile collation logic.
gavofyork 5998aa1
Move back to refs. Yey.
gavofyork 3f4085a
Remove "object" id for storage access.
gavofyork 4be0537
Fix test.
gavofyork 01d7019
Fix up rest of tests.
gavofyork db1adee
remove unwrap.
gavofyork 87c54f7
Expose set/get code in externalities
gavofyork 471ea1e
Add validator set.
gavofyork fa35993
Introduce validator set into externalities and test.
gavofyork a0f64df
Add another external function.
gavofyork e736d46
Remove code and validators; use storage for everything.
gavofyork 234297c
Introduce validators function.
gavofyork 3f8a96d
Tests (and a fix) for the validators getter.
gavofyork 6636520
Allow calls into runtime to return data.
gavofyork 964659e
Remove unneeded trace.
gavofyork 8ca1b7b
Make runtime printing a bit nicer.
gavofyork 74156a2
Create separate runtimes for testing and polkadot.
gavofyork 611a7ac
Remove commented code.
gavofyork c3afecc
Use new path.
gavofyork ea4d6c5
Refactor into shared support module.
gavofyork 709693d
Fix warning.
gavofyork ec1e6b6
Remove unwraps.
gavofyork 5c0ec3d
Make macro a little less unhygenic.
gavofyork 79ab46f
Add wasm files.
gavofyork File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Can now pass in bytes to WasmExecutor.
- Loading branch information
commit 1ace6b2204b457818524c3a5b2ad8e059d89ceb8
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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) } } | ||
|
|
@@ -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] | ||
|
|
@@ -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 { | ||
|
|
@@ -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 )* ), | ||
| )* | ||
|
|
@@ -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 { | ||
|
|
@@ -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 }, | ||
| 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. | ||
| /// | ||
|
|
@@ -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() { | ||
|
|
@@ -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)); | ||
|
||
| 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"); | ||
|
||
| *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()) | ||
| } | ||
| } | ||
|
|
@@ -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)] | ||
|
|
@@ -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) }), | ||
|
|
@@ -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) }), | ||
|
|
@@ -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"); | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 becomei32s. 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.