diff --git a/substrate/Cargo.lock b/substrate/Cargo.lock index 335932c595..5b09589be9 100644 --- a/substrate/Cargo.lock +++ b/substrate/Cargo.lock @@ -4868,7 +4868,6 @@ name = "substrate-executor" version = "2.0.0" dependencies = [ "assert_matches 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", - "byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)", "derive_more 0.14.1 (registry+https://github.com/rust-lang/crates.io-index)", "hex-literal 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -4887,6 +4886,7 @@ dependencies = [ "substrate-serializer 2.0.0", "substrate-state-machine 2.0.0", "substrate-trie 2.0.0", + "substrate-wasm-interface 2.0.0", "tiny-keccak 1.5.0 (registry+https://github.com/rust-lang/crates.io-index)", "wabt 0.7.4 (registry+https://github.com/rust-lang/crates.io-index)", "wasmi 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -5464,6 +5464,13 @@ source = "registry+https://github.com/rust-lang/crates.io-index" name = "substrate-wasm-builder-runner" version = "1.0.3" +[[package]] +name = "substrate-wasm-interface" +version = "2.0.0" +dependencies = [ + "wasmi 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "subtle" version = "1.0.0" diff --git a/substrate/Cargo.toml b/substrate/Cargo.toml index 29f5307610..1920587440 100644 --- a/substrate/Cargo.toml +++ b/substrate/Cargo.toml @@ -19,6 +19,7 @@ vergen = "3" [workspace] members = [ + "core/authority-discovery", "core/application-crypto", "core/cli", "core/client", @@ -64,7 +65,7 @@ members = [ "core/utils/fork-tree", "core/utils/wasm-builder", "core/utils/wasm-builder-runner", - "core/authority-discovery", + "core/wasm-interface", "srml/support", "srml/support/procedural", "srml/support/procedural/tools", diff --git a/substrate/core/executor/Cargo.toml b/substrate/core/executor/Cargo.toml index a4707fb89e..a62a969d21 100644 --- a/substrate/core/executor/Cargo.toml +++ b/substrate/core/executor/Cargo.toml @@ -14,9 +14,9 @@ serializer = { package = "substrate-serializer", path = "../serializer" } state_machine = { package = "substrate-state-machine", path = "../state-machine" } runtime_version = { package = "sr-version", path = "../sr-version" } panic-handler = { package = "substrate-panic-handler", path = "../panic-handler" } +wasm-interface = { package = "substrate-wasm-interface", path = "../wasm-interface" } wasmi = "0.5.0" parity-wasm = "0.31" -byteorder = "1.3" lazy_static = "1.3" parking_lot = "0.9.0" log = "0.4" diff --git a/substrate/core/executor/src/allocator.rs b/substrate/core/executor/src/allocator.rs index 4cc8174f70..024af84768 100644 --- a/substrate/core/executor/src/allocator.rs +++ b/substrate/core/executor/src/allocator.rs @@ -19,8 +19,8 @@ use crate::error::{Error, Result}; use log::trace; -use wasmi::MemoryRef; -use wasmi::memory_units::Bytes; +use wasmi::{MemoryRef, memory_units::Bytes}; +use wasm_interface::{Pointer, WordSize}; // The pointers need to be aligned to 8 bytes. const ALIGNMENT: u32 = 8; @@ -79,7 +79,7 @@ impl FreeingBumpHeapAllocator { /// Gets requested number of bytes to allocate and returns a pointer. /// The maximum size which can be allocated at once is 16 MiB. - pub fn allocate(&mut self, size: u32) -> Result { + pub fn allocate(&mut self, size: WordSize) -> Result> { if size > MAX_POSSIBLE_ALLOCATION { return Err(Error::RequestedAllocationTooLarge); } @@ -95,7 +95,7 @@ impl FreeingBumpHeapAllocator { // Something from the free list let item = self.heads[list_index]; let four_bytes = self.get_heap_4bytes(item)?; - self.heads[list_index] = FreeingBumpHeapAllocator::le_bytes_to_u32(four_bytes); + self.heads[list_index] = Self::le_bytes_to_u32(four_bytes); item + 8 } else { // Nothing to be freed. Bump. @@ -109,12 +109,12 @@ impl FreeingBumpHeapAllocator { self.total_size = self.total_size + item_size + 8; trace!(target: "wasm-heap", "Heap size is {} bytes after allocation", self.total_size); - Ok(self.ptr_offset + ptr) + Ok(Pointer::new(self.ptr_offset + ptr)) } /// Deallocates the space which was allocated for a pointer. - pub fn deallocate(&mut self, ptr: u32) -> Result<()> { - let ptr = ptr - self.ptr_offset; + pub fn deallocate(&mut self, ptr: Pointer) -> Result<()> { + let ptr = u32::from(ptr) - self.ptr_offset; if ptr < 8 { return Err(error("Invalid pointer for deallocation")); } @@ -125,10 +125,10 @@ impl FreeingBumpHeapAllocator { self.heads[list_index] = ptr - 8; let mut slice = self.get_heap_4bytes(ptr - 8)?; - FreeingBumpHeapAllocator::write_u32_into_le_bytes(tail, &mut slice); + Self::write_u32_into_le_bytes(tail, &mut slice); self.set_heap_4bytes(ptr - 8, slice)?; - let item_size = FreeingBumpHeapAllocator::get_item_size_from_index(list_index); + let item_size = Self::get_item_size_from_index(list_index); self.total_size = self.total_size.checked_sub(item_size as u32 + 8) .ok_or_else(|| error("Unable to subtract from total heap size without overflow"))?; trace!(target: "wasm-heap", "Heap size is {} bytes after deallocation", self.total_size); @@ -147,8 +147,7 @@ impl FreeingBumpHeapAllocator { } fn write_u32_into_le_bytes(bytes: u32, slice: &mut [u8]) { - let bytes: [u8; 4] = unsafe { std::mem::transmute::(bytes.to_le()) }; - for i in 0..4 { slice[i] = bytes[i]; } + slice[..4].copy_from_slice(&bytes.to_le_bytes()); } fn get_item_size_from_index(index: usize) -> usize { @@ -175,7 +174,6 @@ impl FreeingBumpHeapAllocator { fn set_heap_4bytes(&mut self, ptr: u32, value: [u8; 4]) -> Result<()> { self.heap.set(self.ptr_offset + ptr, &value).map_err(Into::into) } - } #[cfg(test)] @@ -186,6 +184,11 @@ mod tests { const PAGE_SIZE: u32 = 65536; + /// Makes a pointer out of the given address. + fn to_pointer(address: u32) -> Pointer { + Pointer::new(address) + } + #[test] fn should_allocate_properly() { // given @@ -196,7 +199,7 @@ mod tests { let ptr = heap.allocate(1).unwrap(); // then - assert_eq!(ptr, 8); + assert_eq!(ptr, to_pointer(8)); } #[test] @@ -211,7 +214,7 @@ mod tests { // then // the pointer must start at the next multiple of 8 from 13 // + the prefix of 8 bytes. - assert_eq!(ptr, 24); + assert_eq!(ptr, to_pointer(24)); } #[test] @@ -227,14 +230,14 @@ mod tests { // then // a prefix of 8 bytes is prepended to each pointer - assert_eq!(ptr1, 8); + assert_eq!(ptr1, to_pointer(8)); // the prefix of 8 bytes + the content of ptr1 padded to the lowest possible // item size of 8 bytes + the prefix of ptr1 - assert_eq!(ptr2, 24); + assert_eq!(ptr2, to_pointer(24)); // ptr2 + its content of 16 bytes + the prefix of 8 bytes - assert_eq!(ptr3, 24 + 16 + 8); + assert_eq!(ptr3, to_pointer(24 + 16 + 8)); } #[test] @@ -244,11 +247,11 @@ mod tests { let mut heap = FreeingBumpHeapAllocator::new(mem, 0); let ptr1 = heap.allocate(1).unwrap(); // the prefix of 8 bytes is prepended to the pointer - assert_eq!(ptr1, 8); + assert_eq!(ptr1, to_pointer(8)); let ptr2 = heap.allocate(1).unwrap(); // the prefix of 8 bytes + the content of ptr 1 is prepended to the pointer - assert_eq!(ptr2, 24); + assert_eq!(ptr2, to_pointer(24)); // when heap.deallocate(ptr2).unwrap(); @@ -256,7 +259,7 @@ mod tests { // then // then the heads table should contain a pointer to the // prefix of ptr2 in the leftmost entry - assert_eq!(heap.heads[0], ptr2 - 8); + assert_eq!(heap.heads[0], u32::from(ptr2) - 8); } #[test] @@ -268,13 +271,13 @@ mod tests { let ptr1 = heap.allocate(1).unwrap(); // the prefix of 8 bytes is prepended to the pointer - assert_eq!(ptr1, padded_offset + 8); + assert_eq!(ptr1, to_pointer(padded_offset + 8)); let ptr2 = heap.allocate(9).unwrap(); // the padded_offset + the previously allocated ptr (8 bytes prefix + // 8 bytes content) + the prefix of 8 bytes which is prepended to the // current pointer - assert_eq!(ptr2, padded_offset + 16 + 8); + assert_eq!(ptr2, to_pointer(padded_offset + 16 + 8)); // when heap.deallocate(ptr2).unwrap(); @@ -282,7 +285,7 @@ mod tests { // then // should have re-allocated - assert_eq!(ptr3, padded_offset + 16 + 8); + assert_eq!(ptr3, to_pointer(padded_offset + 16 + 8)); assert_eq!(heap.heads, [0; N]); } @@ -302,15 +305,12 @@ mod tests { heap.deallocate(ptr3).unwrap(); // then - let mut expected = [0; N]; - expected[0] = ptr3 - 8; - assert_eq!(heap.heads, expected); + assert_eq!(heap.heads[0], u32::from(ptr3) - 8); let ptr4 = heap.allocate(8).unwrap(); assert_eq!(ptr4, ptr3); - expected[0] = ptr2 - 8; - assert_eq!(heap.heads, expected); + assert_eq!(heap.heads[0], u32::from(ptr2) - 8); } #[test] @@ -338,7 +338,7 @@ mod tests { let mem = MemoryInstance::alloc(Pages(1), Some(Pages(1))).unwrap(); let mut heap = FreeingBumpHeapAllocator::new(mem, 0); let ptr1 = heap.allocate((PAGE_SIZE / 2) - 8).unwrap(); - assert_eq!(ptr1, 8); + assert_eq!(ptr1, to_pointer(8)); // when let ptr2 = heap.allocate(PAGE_SIZE / 2); @@ -365,7 +365,7 @@ mod tests { let ptr = heap.allocate(MAX_POSSIBLE_ALLOCATION).unwrap(); // then - assert_eq!(ptr, 8); + assert_eq!(ptr, to_pointer(8)); } #[test] @@ -409,7 +409,7 @@ mod tests { // when let ptr = heap.allocate(42).unwrap(); - assert_eq!(ptr, 16 + 8); + assert_eq!(ptr, to_pointer(16 + 8)); heap.deallocate(ptr).unwrap(); // then diff --git a/substrate/core/executor/src/error.rs b/substrate/core/executor/src/error.rs index 9bfa05ff5f..967d074ec9 100644 --- a/substrate/core/executor/src/error.rs +++ b/substrate/core/executor/src/error.rs @@ -66,7 +66,7 @@ pub enum Error { #[display(fmt="The runtime has the `start` function")] RuntimeHasStartFn, /// Some other error occurred - Other(&'static str), + Other(String), /// Some error occurred in the allocator #[display(fmt="Error in allocator: {}", _0)] Allocator(&'static str), @@ -76,6 +76,9 @@ pub enum Error { /// Someone tried to allocate more memory than the allowed maximum per allocation. #[display(fmt="Requested allocation size is too large")] RequestedAllocationTooLarge, + /// Executing the given function failed with the given error. + #[display(fmt="Function execution failed with: {}", _0)] + FunctionExecution(String), } impl std::error::Error for Error { @@ -93,8 +96,8 @@ impl state_machine::Error for Error {} impl wasmi::HostError for Error {} -impl From<&'static str> for Error { - fn from(err: &'static str) -> Error { +impl From for Error { + fn from(err: String) -> Error { Error::Other(err) } } diff --git a/substrate/core/executor/src/lib.rs b/substrate/core/executor/src/lib.rs index 684200519e..d4bed6964e 100644 --- a/substrate/core/executor/src/lib.rs +++ b/substrate/core/executor/src/lib.rs @@ -48,6 +48,8 @@ pub use runtime_version::{RuntimeVersion, NativeVersion}; pub use codec::Codec; #[doc(hidden)] pub use primitives::Blake2Hasher; +#[doc(hidden)] +pub use wasm_interface; /// Provides runtime information. pub trait RuntimeInfo { diff --git a/substrate/core/executor/src/sandbox.rs b/substrate/core/executor/src/sandbox.rs index 6687738abc..2c38499ec8 100644 --- a/substrate/core/executor/src/sandbox.rs +++ b/substrate/core/executor/src/sandbox.rs @@ -26,6 +26,7 @@ use wasmi::{ Externals, FuncRef, ImportResolver, MemoryInstance, MemoryRef, Module, ModuleInstance, ModuleRef, RuntimeArgs, RuntimeValue, Trap, TrapKind, memory_units::Pages, }; +use wasm_interface::{Pointer, WordSize}; /// Index of a function inside the supervisor. /// @@ -150,7 +151,7 @@ pub trait SandboxCapabilities { /// Returns `Err` if allocation not possible or errors during heap management. /// /// Returns pointer to the allocated block. - fn allocate(&mut self, len: u32) -> Result; + fn allocate(&mut self, len: WordSize) -> Result>; /// Deallocate space specified by the pointer that was previously returned by [`allocate`]. /// @@ -159,21 +160,21 @@ pub trait SandboxCapabilities { /// Returns `Err` if deallocation not possible or because of errors in heap management. /// /// [`allocate`]: #tymethod.allocate - fn deallocate(&mut self, ptr: u32) -> Result<()>; + fn deallocate(&mut self, ptr: Pointer) -> Result<()>; /// Write `data` into the supervisor memory at offset specified by `ptr`. /// /// # Errors /// /// Returns `Err` if `ptr + data.len()` is out of bounds. - fn write_memory(&mut self, ptr: u32, data: &[u8]) -> Result<()>; + fn write_memory(&mut self, ptr: Pointer, data: &[u8]) -> Result<()>; /// Read `len` bytes from the supervisor memory. /// /// # Errors /// /// Returns `Err` if `ptr + len` is out of bounds. - fn read_memory(&self, ptr: u32, len: u32) -> Result>; + fn read_memory(&self, ptr: Pointer, len: WordSize) -> Result>; } /// Implementation of [`Externals`] that allows execution of guest module with @@ -187,7 +188,7 @@ pub struct GuestExternals<'a, FE: SandboxCapabilities + Externals + 'a> { } fn trap(msg: &'static str) -> Trap { - TrapKind::Host(Box::new(Error::Other(msg))).into() + TrapKind::Host(Box::new(Error::Other(msg.into()))).into() } fn deserialize_result(serialized_result: &[u8]) -> std::result::Result, Trap> { @@ -243,7 +244,7 @@ impl<'a, FE: SandboxCapabilities + Externals + 'a> Externals for GuestExternals< let result = ::wasmi::FuncInstance::invoke( &dispatch_thunk, &[ - RuntimeValue::I32(invoke_args_ptr as i32), + RuntimeValue::I32(u32::from(invoke_args_ptr) as i32), RuntimeValue::I32(invoke_args_data.len() as i32), RuntimeValue::I32(state as i32), RuntimeValue::I32(func_idx.0 as i32), @@ -260,7 +261,7 @@ impl<'a, FE: SandboxCapabilities + Externals + 'a> Externals for GuestExternals< let v = v as u64; let ptr = (v as u64 >> 32) as u32; let len = (v & 0xFFFFFFFF) as u32; - (ptr, len) + (Pointer::new(ptr), len) } Ok(_) => return Err(trap("Supervisor function returned unexpected result!")), Err(_) => return Err(trap("Supervisor function trapped!")), @@ -643,7 +644,10 @@ mod tests { if let Err(err) = res { assert_eq!( format!("{}", err), - format!("{}", wasmi::Error::Trap(Error::AllocatorOutOfSpace.into())) + format!( + "{}", + wasmi::Error::Trap(Error::FunctionExecution("AllocatorOutOfSpace".into()).into()), + ), ); } } diff --git a/substrate/core/executor/src/wasm_executor.rs b/substrate/core/executor/src/wasm_executor.rs index d8fd41ab7f..25f1f969b8 100644 --- a/substrate/core/executor/src/wasm_executor.rs +++ b/substrate/core/executor/src/wasm_executor.rs @@ -19,7 +19,7 @@ //! This module defines and implements the wasm part of Substrate Host Interface and provides //! an interface for calling into the wasm runtime. -use std::{collections::HashMap, convert::TryFrom, str}; +use std::{convert::TryFrom, str}; use tiny_keccak; use secp256k1; @@ -27,92 +27,238 @@ use wasmi::{ Module, ModuleInstance, MemoryInstance, MemoryRef, TableRef, ImportsBuilder, ModuleRef, memory_units::Pages, RuntimeValue::{I32, I64, self}, }; -use state_machine::{Externalities, ChildStorageKey}; +use state_machine::Externalities; use crate::error::{Error, Result}; -use codec::Encode; +use codec::{Encode, Decode}; use primitives::{ blake2_128, blake2_256, twox_64, twox_128, twox_256, ed25519, sr25519, Pair, crypto::KeyTypeId, - offchain, hexdisplay::HexDisplay, sandbox as sandbox_primitives, H256, Blake2Hasher, + offchain, hexdisplay::HexDisplay, sandbox as sandbox_primitives, Blake2Hasher, }; use trie::{TrieConfiguration, trie_types::Layout}; use crate::sandbox; use crate::allocator; use log::trace; +use wasm_interface::{ + FunctionContext, HostFunctions, Pointer, WordSize, Sandbox, MemoryId, PointerType, + Result as WResult, +}; #[cfg(feature="wasm-extern-trace")] macro_rules! debug_trace { ( $( $x:tt )* ) => ( trace!( $( $x )* ) ) } + #[cfg(not(feature="wasm-extern-trace"))] macro_rules! debug_trace { ( $( $x:tt )* ) => () } -struct FunctionExecutor<'e, E: Externalities + 'e> { +struct FunctionExecutor { sandbox_store: sandbox::Store, heap: allocator::FreeingBumpHeapAllocator, memory: MemoryRef, table: Option, - ext: &'e mut E, - hash_lookup: HashMap, Vec>, } -impl<'e, E: Externalities> FunctionExecutor<'e, E> { - fn new(m: MemoryRef, heap_base: u32, t: Option, e: &'e mut E) -> Result { +impl FunctionExecutor { + fn new(m: MemoryRef, heap_base: u32, t: Option) -> Result { Ok(FunctionExecutor { sandbox_store: sandbox::Store::new(), heap: allocator::FreeingBumpHeapAllocator::new(m.clone(), heap_base), memory: m, table: t, - ext: e, - hash_lookup: HashMap::new(), }) } } -impl<'e, E: Externalities> sandbox::SandboxCapabilities for FunctionExecutor<'e, E> { +impl sandbox::SandboxCapabilities for FunctionExecutor { fn store(&self) -> &sandbox::Store { &self.sandbox_store } fn store_mut(&mut self) -> &mut sandbox::Store { &mut self.sandbox_store } - fn allocate(&mut self, len: u32) -> Result { + fn allocate(&mut self, len: WordSize) -> Result> { self.heap.allocate(len) } - fn deallocate(&mut self, ptr: u32) -> Result<()> { + fn deallocate(&mut self, ptr: Pointer) -> Result<()> { self.heap.deallocate(ptr) } - fn write_memory(&mut self, ptr: u32, data: &[u8]) -> Result<()> { - self.memory.set(ptr, data).map_err(Into::into) + fn write_memory(&mut self, ptr: Pointer, data: &[u8]) -> Result<()> { + self.memory.set(ptr.into(), data).map_err(Into::into) } - fn read_memory(&self, ptr: u32, len: u32) -> Result> { - self.memory.get(ptr, len as usize).map_err(Into::into) + fn read_memory(&self, ptr: Pointer, len: WordSize) -> Result> { + self.memory.get(ptr.into(), len as usize).map_err(Into::into) } } -trait WritePrimitive { - fn write_primitive(&self, offset: u32, t: T) -> Result<()>; +impl FunctionContext for FunctionExecutor { + fn read_memory_into(&self, address: Pointer, dest: &mut [u8]) -> WResult<()> { + self.memory.get_into(address.into(), dest).map_err(|e| format!("{:?}", e)) + } + + fn write_memory(&mut self, address: Pointer, data: &[u8]) -> WResult<()> { + self.memory.set(address.into(), data).map_err(|e| format!("{:?}", e)) + } + + fn allocate_memory(&mut self, size: WordSize) -> WResult> { + self.heap.allocate(size).map_err(|e| format!("{:?}", e)) + } + + fn deallocate_memory(&mut self, ptr: Pointer) -> WResult<()> { + self.heap.deallocate(ptr).map_err(|e| format!("{:?}", e)) + } + + fn sandbox(&mut self) -> &mut dyn Sandbox { + self + } } -impl WritePrimitive for MemoryInstance { - fn write_primitive(&self, offset: u32, t: u32) -> Result<()> { - use byteorder::{LittleEndian, ByteOrder}; +impl Sandbox for FunctionExecutor { + fn memory_get( + &self, + memory_id: MemoryId, + offset: WordSize, + buf_ptr: Pointer, + buf_len: WordSize, + ) -> WResult { + let sandboxed_memory = self.sandbox_store.memory(memory_id).map_err(|e| format!("{:?}", e))?; + + match MemoryInstance::transfer( + &sandboxed_memory, + offset as usize, + &self.memory, + buf_ptr.into(), + buf_len as usize, + ) { + Ok(()) => Ok(sandbox_primitives::ERR_OK), + Err(_) => Ok(sandbox_primitives::ERR_OUT_OF_BOUNDS), + } + } + + fn memory_set( + &mut self, + memory_id: MemoryId, + offset: WordSize, + val_ptr: Pointer, + val_len: WordSize, + ) -> WResult { + let sandboxed_memory = self.sandbox_store.memory(memory_id).map_err(|e| format!("{:?}", e))?; + + match MemoryInstance::transfer( + &self.memory, + val_ptr.into(), + &sandboxed_memory, + offset as usize, + val_len as usize, + ) { + Ok(()) => Ok(sandbox_primitives::ERR_OK), + Err(_) => Ok(sandbox_primitives::ERR_OUT_OF_BOUNDS), + } + } + + fn memory_teardown(&mut self, memory_id: MemoryId) -> WResult<()> { + self.sandbox_store.memory_teardown(memory_id).map_err(|e| format!("{:?}", e)) + } + + fn memory_new( + &mut self, + initial: u32, + maximum: u32, + ) -> WResult { + self.sandbox_store.new_memory(initial, maximum).map_err(|e| format!("{:?}", e)) + } + + fn invoke( + &mut self, + instance_id: u32, + export_name: &str, + args: &[u8], + return_val: Pointer, + return_val_len: WordSize, + state: u32, + ) -> WResult { + trace!(target: "sr-sandbox", "invoke, instance_idx={}", instance_id); + + // Deserialize arguments and convert them into wasmi types. + let args = Vec::::decode(&mut &args[..]) + .map_err(|_| "Can't decode serialized arguments for the invocation")? + .into_iter() + .map(Into::into) + .collect::>(); + + let instance = self.sandbox_store.instance(instance_id).map_err(|e| format!("{:?}", e))?; + let result = instance.invoke(export_name, &args, self, state); + + match result { + Ok(None) => Ok(sandbox_primitives::ERR_OK), + Ok(Some(val)) => { + // Serialize return value and write it back into the memory. + sandbox_primitives::ReturnValue::Value(val.into()).using_encoded(|val| { + if val.len() > return_val_len as usize { + Err("Return value buffer is too small")?; + } + self.write_memory(return_val, val).map_err(|_| "Return value buffer is OOB")?; + Ok(sandbox_primitives::ERR_OK) + }) + } + Err(_) => Ok(sandbox_primitives::ERR_EXECUTION), + } + } + + fn instance_teardown(&mut self, instance_id: u32) -> WResult<()> { + self.sandbox_store.instance_teardown(instance_id).map_err(|e| format!("{:?}", e)) + } + + fn instance_new( + &mut self, + dispatch_thunk_id: u32, + wasm: &[u8], + raw_env_def: &[u8], + state: u32, + ) -> WResult { + // Extract a dispatch thunk from instance's table by the specified index. + let dispatch_thunk = { + let table = self.table.as_ref() + .ok_or_else(|| "Runtime doesn't have a table; sandbox is unavailable")?; + table.get(dispatch_thunk_id) + .map_err(|_| "dispatch_thunk_idx is out of the table bounds")? + .ok_or_else(|| "dispatch_thunk_idx points on an empty table entry")? + .clone() + }; + + let instance_idx_or_err_code = + match sandbox::instantiate(self, dispatch_thunk, wasm, raw_env_def, state) { + Ok(instance_idx) => instance_idx, + Err(sandbox::InstantiationError::StartTrapped) => + sandbox_primitives::ERR_EXECUTION, + Err(_) => sandbox_primitives::ERR_MODULE, + }; + + Ok(instance_idx_or_err_code as u32) + } +} + +trait WritePrimitive { + fn write_primitive(&mut self, ptr: Pointer, t: T) -> WResult<()>; +} + +impl WritePrimitive for &mut dyn FunctionContext { + fn write_primitive(&mut self, ptr: Pointer, t: u32) -> WResult<()> { + let r = t.to_le_bytes(); + self.write_memory(ptr.cast(), &r) + } +} + +trait ReadPrimitive { + fn read_primitive(&self, offset: Pointer) -> WResult; +} + +impl ReadPrimitive for &mut dyn FunctionContext { + fn read_primitive(&self, ptr: Pointer) -> WResult { let mut r = [0u8; 4]; - LittleEndian::write_u32(&mut r, t); - self.set(offset, &r).map_err(Into::into) - } -} - -trait ReadPrimitive { - fn read_primitive(&self, offset: u32) -> Result; -} - -impl ReadPrimitive for MemoryInstance { - fn read_primitive(&self, offset: u32) -> Result { - use byteorder::{LittleEndian, ByteOrder}; - let result = self.get(offset, 4)?; - Ok(LittleEndian::read_u32(&result)) + self.read_memory_into(ptr.cast(), &mut r)?; + Ok(u32::from_le_bytes(r)) } } @@ -124,1201 +270,1066 @@ fn deadline_to_timestamp(deadline: u64) -> Option { } } -impl_function_executor!(this: FunctionExecutor<'e, E>, - ext_print_utf8(utf8_data: *const u8, utf8_len: u32) => { - if let Ok(utf8) = this.memory.get(utf8_data, utf8_len as usize) { - if let Ok(message) = String::from_utf8(utf8) { - println!("{}", message); +impl FunctionExecutor { + fn resolver() -> &'static dyn wasmi::ModuleImportResolver { + struct Resolver; + impl wasmi::ModuleImportResolver for Resolver { + fn resolve_func(&self, name: &str, signature: &wasmi::Signature) + -> std::result::Result + { + let signature = wasm_interface::Signature::from(signature); + + if let Some((index, func)) = SubstrateExternals::functions().iter() + .enumerate() + .find(|f| name == f.1.name()) + { + if signature == func.signature() { + Ok(wasmi::FuncInstance::alloc_host(signature.into(), index)) + } else { + Err(wasmi::Error::Instantiation( + format!( + "Invalid signature for function `{}` expected `{:?}`, got `{:?}`", + func.name(), + signature, + func.signature(), + ) + )) + } + } else { + Err(wasmi::Error::Instantiation( + format!("Export {} not found", name), + )) + } } } - Ok(()) - }, - ext_print_hex(data: *const u8, len: u32) => { - if let Ok(hex) = this.memory.get(data, len as usize) { - println!("{}", HexDisplay::from(&hex)); - } - Ok(()) - }, - ext_print_num(number: u64) => { - println!("{}", number); - Ok(()) - }, - ext_malloc(size: usize) -> *mut u8 => { - let r = this.heap.allocate(size)?; - debug_trace!(target: "sr-io", "malloc {} bytes at {}", size, r); - Ok(r) - }, - ext_free(addr: *mut u8) => { - this.heap.deallocate(addr)?; - debug_trace!(target: "sr-io", "free {}", addr); - Ok(()) - }, - ext_set_storage(key_data: *const u8, key_len: u32, value_data: *const u8, value_len: u32) => { - let key = this.memory.get(key_data, key_len as usize) - .map_err(|_| "Invalid attempt to determine key in ext_set_storage")?; - let value = this.memory.get(value_data, value_len as usize) - .map_err(|_| "Invalid attempt to determine value in ext_set_storage")?; - if let Some(_preimage) = this.hash_lookup.get(&key) { - debug_trace!( - target: "wasm-trace", - "*** Setting storage: %{} -> {} [k={}]", - primitives::hexdisplay::ascii_format(&_preimage), - HexDisplay::from(&value), - HexDisplay::from(&key), - ); - } else { - debug_trace!( - target: "wasm-trace", - "*** Setting storage: {} -> {} [k={}]", - primitives::hexdisplay::ascii_format(&key), - HexDisplay::from(&value), - HexDisplay::from(&key), - ); - } - this.ext.set_storage(key, value); - Ok(()) - }, - ext_set_child_storage( - storage_key_data: *const u8, - storage_key_len: u32, - key_data: *const u8, - key_len: u32, - value_data: *const u8, - value_len: u32 - ) => { - let storage_key = this.memory.get(storage_key_data, storage_key_len as usize) - .map_err(|_| "Invalid attempt to determine storage_key in ext_set_child_storage")?; - let key = this.memory.get(key_data, key_len as usize) - .map_err(|_| "Invalid attempt to determine key in ext_set_child_storage")?; - let value = this.memory.get(value_data, value_len as usize) - .map_err(|_| "Invalid attempt to determine value in ext_set_child_storage")?; - if let Some(_preimage) = this.hash_lookup.get(&key) { - debug_trace!( - target: "wasm-trace", "*** Setting child storage: {} -> %{} -> {} [k={}]", - primitives::hexdisplay::ascii_format(&storage_key), - primitives::hexdisplay::ascii_format(&_preimage), - HexDisplay::from(&value), - HexDisplay::from(&key) - ); - } else { - debug_trace!( - target: "wasm-trace", "*** Setting child storage: {} -> {} -> {} [k={}]", - primitives::hexdisplay::ascii_format(&storage_key), - primitives::hexdisplay::ascii_format(&key), - HexDisplay::from(&value), - HexDisplay::from(&key) - ); - } - let storage_key = ChildStorageKey::from_vec(storage_key) - .ok_or_else(|| "ext_set_child_storage: child storage key is invalid")?; - this.ext.set_child_storage(storage_key, key, value); - Ok(()) - }, - ext_clear_child_storage( - storage_key_data: *const u8, - storage_key_len: u32, - key_data: *const u8, - key_len: u32 - ) => { - let storage_key = this.memory.get( - storage_key_data, - storage_key_len as usize - ).map_err(|_| "Invalid attempt to determine storage_key in ext_clear_child_storage")?; - let key = this.memory.get(key_data, key_len as usize) - .map_err(|_| "Invalid attempt to determine key in ext_clear_child_storage")?; - debug_trace!( - target: "wasm-trace", "*** Clearing child storage: {} -> {} [k={}]", - primitives::hexdisplay::ascii_format(&storage_key), - if let Some(_preimage) = this.hash_lookup.get(&key) { - format!("%{}", primitives::hexdisplay::ascii_format(&_preimage)) - } else { - format!(" {}", primitives::hexdisplay::ascii_format(&key)) - }, - HexDisplay::from(&key) - ); - let storage_key = ChildStorageKey::from_vec(storage_key) - .ok_or_else(|| "ext_clear_child_storage: child storage key is not valid")?; + &Resolver + } +} - this.ext.clear_child_storage(storage_key, &key); - Ok(()) - }, - ext_clear_storage(key_data: *const u8, key_len: u32) => { - let key = this.memory.get(key_data, key_len as usize) - .map_err(|_| "Invalid attempt to determine key in ext_clear_storage")?; - debug_trace!( - target: "wasm-trace", "*** Clearing storage: {} [k={}]", - if let Some(_preimage) = this.hash_lookup.get(&key) { - format!("%{}", ::primitives::hexdisplay::ascii_format(&_preimage)) - } else { - format!(" {}", ::primitives::hexdisplay::ascii_format(&key)) - }, - HexDisplay::from(&key) - ); - this.ext.clear_storage(&key); - Ok(()) - }, - ext_exists_storage(key_data: *const u8, key_len: u32) -> u32 => { - let key = this.memory.get(key_data, key_len as usize) - .map_err(|_| "Invalid attempt to determine key in ext_exists_storage")?; - Ok(if this.ext.exists_storage(&key) { 1 } else { 0 }) - }, - ext_exists_child_storage( - storage_key_data: *const u8, - storage_key_len: u32, - key_data: *const u8, - key_len: u32 - ) -> u32 => { - let storage_key = this.memory.get( - storage_key_data, - storage_key_len as usize - ).map_err(|_| "Invalid attempt to determine storage_key in ext_exists_child_storage")?; - let key = this.memory.get(key_data, key_len as usize) - .map_err(|_| "Invalid attempt to determine key in ext_exists_child_storage")?; - let storage_key = ChildStorageKey::from_vec(storage_key) - .ok_or_else(|| "ext_exists_child_storage: child storage key is not valid")?; - Ok(if this.ext.exists_child_storage(storage_key, &key) { 1 } else { 0 }) - }, - ext_clear_prefix(prefix_data: *const u8, prefix_len: u32) => { - let prefix = this.memory.get(prefix_data, prefix_len as usize) - .map_err(|_| "Invalid attempt to determine prefix in ext_clear_prefix")?; - this.ext.clear_prefix(&prefix); - Ok(()) - }, - ext_clear_child_prefix( - storage_key_data: *const u8, - storage_key_len: u32, - prefix_data: *const u8, - prefix_len: u32 - ) => { - let storage_key = this.memory.get( - storage_key_data, - storage_key_len as usize - ).map_err(|_| "Invalid attempt to determine storage_key in ext_clear_child_prefix")?; - let storage_key = ChildStorageKey::from_vec(storage_key) - .ok_or_else(|| "ext_clear_child_prefix: child storage key is not valid")?; - let prefix = this.memory.get(prefix_data, prefix_len as usize) - .map_err(|_| "Invalid attempt to determine prefix in ext_clear_child_prefix")?; - this.ext.clear_child_prefix(storage_key, &prefix); - Ok(()) - }, - ext_kill_child_storage(storage_key_data: *const u8, storage_key_len: u32) => { - let storage_key = this.memory.get( - storage_key_data, - storage_key_len as usize - ).map_err(|_| "Invalid attempt to determine storage_key in ext_kill_child_storage")?; - let storage_key = ChildStorageKey::from_vec(storage_key) - .ok_or_else(|| "ext_exists_child_storage: child storage key is not valid")?; - this.ext.kill_child_storage(storage_key); - Ok(()) - }, - // return 0 and place u32::max_value() into written_out if no value exists for the key. - ext_get_allocated_storage(key_data: *const u8, key_len: u32, written_out: *mut u32) -> *mut u8 => { - let key = this.memory.get( - key_data, - key_len as usize - ).map_err(|_| "Invalid attempt to determine key in ext_get_allocated_storage")?; - let maybe_value = this.ext.storage(&key); +impl wasmi::Externals for FunctionExecutor { + fn invoke_index(&mut self, index: usize, args: wasmi::RuntimeArgs) + -> std::result::Result, wasmi::Trap> + { + let mut args = args.as_ref().iter().copied().map(Into::into); + let function = SubstrateExternals::functions().get(index).ok_or_else(|| + Error::from( + format!("Could not find host function with index: {}", index), + ) + )?; - debug_trace!( - target: "wasm-trace", "*** Getting storage: {} == {} [k={}]", - if let Some(_preimage) = this.hash_lookup.get(&key) { - format!("%{}", ::primitives::hexdisplay::ascii_format(&_preimage)) - } else { - format!(" {}", ::primitives::hexdisplay::ascii_format(&key)) - }, - if let Some(ref b) = maybe_value { - &format!("{}", HexDisplay::from(b)) - } else { - "" - }, - HexDisplay::from(&key), - ); + function.execute(self, &mut args) + .map_err(Error::FunctionExecution) + .map_err(wasmi::Trap::from) + .map(|v| v.map(Into::into)) + } +} +struct SubstrateExternals; - if let Some(value) = maybe_value { - let offset = this.heap.allocate(value.len() as u32)? as u32; - this.memory.set(offset, &value) - .map_err(|_| "Invalid attempt to set memory in ext_get_allocated_storage")?; - this.memory.write_primitive(written_out, value.len() as u32) - .map_err(|_| "Invalid attempt to write written_out in ext_get_allocated_storage")?; +impl_wasm_host_interface! { + impl SubstrateExternals where context { + ext_malloc(size: WordSize) -> Pointer { + let r = context.allocate_memory(size)?; + debug_trace!(target: "sr-io", "malloc {} bytes at {}", size, r); + Ok(r) + } + + ext_free(addr: Pointer) { + context.deallocate_memory(addr)?; + debug_trace!(target: "sr-io", "free {}", addr); + Ok(()) + } + + ext_sandbox_instantiate( + dispatch_thunk_idx: u32, + wasm_ptr: Pointer, + wasm_len: WordSize, + imports_ptr: Pointer, + imports_len: WordSize, + state: u32, + ) -> u32 { + let wasm = context.read_memory(wasm_ptr, wasm_len) + .map_err(|_| "OOB while ext_sandbox_instantiate: wasm")?; + let raw_env_def = context.read_memory(imports_ptr, imports_len) + .map_err(|_| "OOB while ext_sandbox_instantiate: imports")?; + + context.sandbox().instance_new(dispatch_thunk_idx, &wasm, &raw_env_def, state) + } + + ext_sandbox_instance_teardown(instance_idx: u32) { + context.sandbox().instance_teardown(instance_idx) + } + + ext_sandbox_invoke( + instance_idx: u32, + export_ptr: Pointer, + export_len: WordSize, + args_ptr: Pointer, + args_len: WordSize, + return_val_ptr: Pointer, + return_val_len: WordSize, + state: u32, + ) -> u32 { + let export = context.read_memory(export_ptr, export_len) + .map_err(|_| "OOB while ext_sandbox_invoke: export") + .and_then(|b| + String::from_utf8(b) + .map_err(|_| "Export name should be a valid utf-8 sequence") + )?; + + // Deserialize arguments and convert them into wasmi types. + let serialized_args = context.read_memory(args_ptr, args_len) + .map_err(|_| "OOB while ext_sandbox_invoke: args")?; + + context.sandbox().invoke( + instance_idx, + &export, + &serialized_args, + return_val_ptr, + return_val_len, + state, + ) + } + + ext_sandbox_memory_new(initial: WordSize, maximum: WordSize) -> u32 { + context.sandbox().memory_new(initial, maximum) + } + + ext_sandbox_memory_get( + memory_idx: u32, + offset: WordSize, + buf_ptr: Pointer, + buf_len: WordSize, + ) -> u32 { + context.sandbox().memory_get(memory_idx, offset, buf_ptr, buf_len) + } + + ext_sandbox_memory_set( + memory_idx: u32, + offset: WordSize, + val_ptr: Pointer, + val_len: WordSize, + ) -> u32 { + context.sandbox().memory_set(memory_idx, offset, val_ptr, val_len) + } + + ext_sandbox_memory_teardown(memory_idx: u32) { + context.sandbox().memory_teardown(memory_idx) + } + + ext_print_utf8(utf8_data: Pointer, utf8_len: WordSize) { + if let Ok(utf8) = context.read_memory(utf8_data, utf8_len) { + if let Ok(message) = String::from_utf8(utf8) { + println!("{}", message); + } + } + Ok(()) + } + + ext_print_hex(data: Pointer, len: WordSize) { + if let Ok(hex) = context.read_memory(data, len) { + println!("{}", HexDisplay::from(&hex)); + } + Ok(()) + } + + ext_print_num(number: u64) { + println!("{}", number); + Ok(()) + } + + ext_set_storage( + key_data: Pointer, + key_len: WordSize, + value_data: Pointer, + value_len: WordSize, + ) { + let key = context.read_memory(key_data, key_len) + .map_err(|_| "Invalid attempt to determine key in ext_set_storage")?; + let value = context.read_memory(value_data, value_len) + .map_err(|_| "Invalid attempt to determine value in ext_set_storage")?; + runtime_io::set_storage(&key, &value); + Ok(()) + } + + ext_set_child_storage( + storage_key_data: Pointer, + storage_key_len: WordSize, + key_data: Pointer, + key_len: WordSize, + value_data: Pointer, + value_len: WordSize, + ) { + let storage_key = context.read_memory(storage_key_data, storage_key_len) + .map_err(|_| "Invalid attempt to determine storage_key in ext_set_child_storage")?; + let key = context.read_memory(key_data, key_len) + .map_err(|_| "Invalid attempt to determine key in ext_set_child_storage")?; + let value = context.read_memory(value_data, value_len) + .map_err(|_| "Invalid attempt to determine value in ext_set_child_storage")?; + + runtime_io::set_child_storage(&storage_key, &key, &value); + Ok(()) + } + + ext_clear_child_storage( + storage_key_data: Pointer, + storage_key_len: WordSize, + key_data: Pointer, + key_len: WordSize, + ) { + let storage_key = context.read_memory(storage_key_data, storage_key_len) + .map_err(|_| "Invalid attempt to determine storage_key in ext_clear_child_storage")?; + let key = context.read_memory(key_data, key_len) + .map_err(|_| "Invalid attempt to determine key in ext_clear_child_storage")?; + + runtime_io::clear_child_storage(&storage_key, &key); + Ok(()) + } + + ext_clear_storage(key_data: Pointer, key_len: WordSize) { + let key = context.read_memory(key_data, key_len) + .map_err(|_| "Invalid attempt to determine key in ext_clear_storage")?; + runtime_io::clear_storage(&key); + Ok(()) + } + + ext_exists_storage(key_data: Pointer, key_len: WordSize) -> u32 { + let key = context.read_memory(key_data, key_len) + .map_err(|_| "Invalid attempt to determine key in ext_exists_storage")?; + Ok(if runtime_io::exists_storage(&key) { 1 } else { 0 }) + } + + ext_exists_child_storage( + storage_key_data: Pointer, + storage_key_len: WordSize, + key_data: Pointer, + key_len: WordSize, + ) -> u32 { + let storage_key = context.read_memory(storage_key_data, storage_key_len) + .map_err(|_| "Invalid attempt to determine storage_key in ext_exists_child_storage")?; + let key = context.read_memory(key_data, key_len) + .map_err(|_| "Invalid attempt to determine key in ext_exists_child_storage")?; + + Ok(if runtime_io::exists_child_storage(&storage_key, &key) { 1 } else { 0 }) + } + + ext_clear_prefix(prefix_data: Pointer, prefix_len: WordSize) { + let prefix = context.read_memory(prefix_data, prefix_len) + .map_err(|_| "Invalid attempt to determine prefix in ext_clear_prefix")?; + runtime_io::clear_prefix(&prefix); + Ok(()) + } + + ext_clear_child_prefix( + storage_key_data: Pointer, + storage_key_len: WordSize, + prefix_data: Pointer, + prefix_len: WordSize, + ) { + let storage_key = context.read_memory(storage_key_data, storage_key_len) + .map_err(|_| "Invalid attempt to determine storage_key in ext_clear_child_prefix")?; + let prefix = context.read_memory(prefix_data, prefix_len) + .map_err(|_| "Invalid attempt to determine prefix in ext_clear_child_prefix")?; + runtime_io::clear_child_prefix(&storage_key, &prefix); + + Ok(()) + } + + ext_kill_child_storage(storage_key_data: Pointer, storage_key_len: WordSize) { + let storage_key = context.read_memory(storage_key_data, storage_key_len) + .map_err(|_| "Invalid attempt to determine storage_key in ext_kill_child_storage")?; + runtime_io::kill_child_storage(&storage_key); + + Ok(()) + } + + ext_get_allocated_storage( + key_data: Pointer, + key_len: WordSize, + written_out: Pointer, + ) -> Pointer { + let key = context.read_memory(key_data, key_len) + .map_err(|_| "Invalid attempt to determine key in ext_get_allocated_storage")?; + let maybe_value = runtime_io::storage(&key); + + if let Some(value) = maybe_value { + let offset = context.allocate_memory(value.len() as u32)?; + context.write_memory(offset, &value) + .map_err(|_| "Invalid attempt to set memory in ext_get_allocated_storage")?; + context.write_primitive(written_out, value.len() as u32) + .map_err(|_| "Invalid attempt to write written_out in ext_get_allocated_storage")?; + Ok(offset) + } else { + context.write_primitive(written_out, u32::max_value()) + .map_err(|_| "Invalid attempt to write failed written_out in ext_get_allocated_storage")?; + Ok(Pointer::null()) + } + } + + ext_get_allocated_child_storage( + storage_key_data: Pointer, + storage_key_len: WordSize, + key_data: Pointer, + key_len: WordSize, + written_out: Pointer, + ) -> Pointer { + let storage_key = context.read_memory(storage_key_data, storage_key_len) + .map_err(|_| "Invalid attempt to determine storage_key in ext_get_allocated_child_storage")?; + let key = context.read_memory(key_data, key_len) + .map_err(|_| "Invalid attempt to determine key in ext_get_allocated_child_storage")?; + + let maybe_value = runtime_io::child_storage(&storage_key, &key); + + if let Some(value) = maybe_value { + let offset = context.allocate_memory(value.len() as u32)?; + context.write_memory(offset, &value) + .map_err(|_| "Invalid attempt to set memory in ext_get_allocated_child_storage")?; + context.write_primitive(written_out, value.len() as u32) + .map_err(|_| "Invalid attempt to write written_out in ext_get_allocated_child_storage")?; + Ok(offset) + } else { + context.write_primitive(written_out, u32::max_value()) + .map_err(|_| "Invalid attempt to write failed written_out in ext_get_allocated_child_storage")?; + Ok(Pointer::null()) + } + } + + ext_get_storage_into( + key_data: Pointer, + key_len: WordSize, + value_data: Pointer, + value_len: WordSize, + value_offset: WordSize, + ) -> WordSize { + let key = context.read_memory(key_data, key_len) + .map_err(|_| "Invalid attempt to get key in ext_get_storage_into")?; + let maybe_value = runtime_io::storage(&key); + + if let Some(value) = maybe_value { + let value = &value[value_offset as usize..]; + let written = std::cmp::min(value_len as usize, value.len()); + context.write_memory(value_data, &value[..written]) + .map_err(|_| "Invalid attempt to set value in ext_get_storage_into")?; + Ok(value.len() as u32) + } else { + Ok(u32::max_value()) + } + } + + ext_get_child_storage_into( + storage_key_data: Pointer, + storage_key_len: WordSize, + key_data: Pointer, + key_len: WordSize, + value_data: Pointer, + value_len: WordSize, + value_offset: WordSize, + ) -> WordSize { + let storage_key = context.read_memory(storage_key_data, storage_key_len) + .map_err(|_| "Invalid attempt to determine storage_key in ext_get_child_storage_into")?; + let key = context.read_memory(key_data, key_len) + .map_err(|_| "Invalid attempt to get key in ext_get_child_storage_into")?; + + let maybe_value = runtime_io::child_storage(&storage_key, &key); + + if let Some(value) = maybe_value { + let value = &value[value_offset as usize..]; + let written = std::cmp::min(value_len as usize, value.len()); + context.write_memory(value_data, &value[..written]) + .map_err(|_| "Invalid attempt to set value in ext_get_child_storage_into")?; + Ok(value.len() as u32) + } else { + Ok(u32::max_value()) + } + } + + ext_storage_root(result: Pointer) { + let r = runtime_io::storage_root(); + context.write_memory(result, r.as_ref()) + .map_err(|_| "Invalid attempt to set memory in ext_storage_root")?; + Ok(()) + } + + ext_child_storage_root( + storage_key_data: Pointer, + storage_key_len: WordSize, + written_out: Pointer, + ) -> Pointer { + let storage_key = context.read_memory(storage_key_data, storage_key_len) + .map_err(|_| "Invalid attempt to determine storage_key in ext_child_storage_root")?; + let value = runtime_io::child_storage_root(&storage_key); + + let offset = context.allocate_memory(value.len() as u32)?; + context.write_memory(offset, &value) + .map_err(|_| "Invalid attempt to set memory in ext_child_storage_root")?; + context.write_primitive(written_out, value.len() as u32) + .map_err(|_| "Invalid attempt to write written_out in ext_child_storage_root")?; Ok(offset) - } else { - this.memory.write_primitive(written_out, u32::max_value()) - .map_err(|_| "Invalid attempt to write failed written_out in ext_get_allocated_storage")?; - Ok(0) } - }, - // return 0 and place u32::max_value() into written_out if no value exists for the key. - ext_get_allocated_child_storage( - storage_key_data: *const u8, - storage_key_len: u32, - key_data: *const u8, - key_len: u32, - written_out: *mut u32 - ) -> *mut u8 => { - let storage_key = this.memory.get( - storage_key_data, - storage_key_len as usize - ).map_err(|_| "Invalid attempt to determine storage_key in ext_get_allocated_child_storage")?; - let key = this.memory.get( - key_data, - key_len as usize - ).map_err(|_| "Invalid attempt to determine key in ext_get_allocated_child_storage")?; - let maybe_value = { - let storage_key = ChildStorageKey::from_slice(&storage_key) - .ok_or_else(|| "ext_get_allocated_child_storage: child storage key is not valid")?; - this.ext.child_storage(storage_key, &key) - }; + ext_storage_changes_root( + parent_hash_data: Pointer, + _len: WordSize, + result: Pointer, + ) -> u32 { + let mut parent_hash = [0u8; 32]; + context.read_memory_into(parent_hash_data, &mut parent_hash[..]) + .map_err(|_| "Invalid attempt to get parent_hash in ext_storage_changes_root")?; + let r = runtime_io::storage_changes_root(parent_hash); - debug_trace!( - target: "wasm-trace", "*** Getting child storage: {} -> {} == {} [k={}]", - primitives::hexdisplay::ascii_format(&storage_key), - if let Some(_preimage) = this.hash_lookup.get(&key) { - format!("%{}", ::primitives::hexdisplay::ascii_format(&_preimage)) + if let Some(r) = r { + context.write_memory(result, &r[..]) + .map_err(|_| "Invalid attempt to set memory in ext_storage_changes_root")?; + Ok(1) } else { - format!(" {}", ::primitives::hexdisplay::ascii_format(&key)) - }, - if let Some(ref b) = maybe_value { - &format!("{}", HexDisplay::from(b)) - } else { - "" - }, - HexDisplay::from(&key), - ); - - if let Some(value) = maybe_value { - let offset = this.heap.allocate(value.len() as u32)? as u32; - this.memory.set(offset, &value) - .map_err(|_| "Invalid attempt to set memory in ext_get_allocated_child_storage")?; - this.memory.write_primitive(written_out, value.len() as u32) - .map_err(|_| "Invalid attempt to write written_out in ext_get_allocated_child_storage")?; - Ok(offset) - } else { - this.memory.write_primitive(written_out, u32::max_value()) - .map_err(|_| "Invalid attempt to write failed written_out in ext_get_allocated_child_storage")?; - Ok(0) + Ok(0) + } } - }, - // return u32::max_value() if no value exists for the key. - ext_get_storage_into( - key_data: *const u8, - key_len: u32, - value_data: *mut u8, - value_len: u32, - value_offset: u32 - ) -> u32 => { - let key = this.memory.get(key_data, key_len as usize) - .map_err(|_| "Invalid attempt to get key in ext_get_storage_into")?; - let maybe_value = this.ext.storage(&key); - debug_trace!( - target: "wasm-trace", "*** Getting storage: {} == {} [k={}]", - if let Some(_preimage) = this.hash_lookup.get(&key) { - format!("%{}", primitives::hexdisplay::ascii_format(&_preimage)) - } else { - format!(" {}", primitives::hexdisplay::ascii_format(&key)) - }, - if let Some(ref b) = maybe_value { - &format!("{}", HexDisplay::from(b)) - } else { - "" - }, - HexDisplay::from(&key), - ); - if let Some(value) = maybe_value { - let value = &value[value_offset as usize..]; - let written = std::cmp::min(value_len as usize, value.len()); - this.memory.set(value_data, &value[..written]) - .map_err(|_| "Invalid attempt to set value in ext_get_storage_into")?; - Ok(value.len() as u32) - } else { - Ok(u32::max_value()) - } - }, - // return u32::max_value() if no value exists for the key. - ext_get_child_storage_into( - storage_key_data: *const u8, - storage_key_len: u32, - key_data: *const u8, - key_len: u32, - value_data: *mut u8, - value_len: u32, - value_offset: u32 - ) -> u32 => { - let storage_key = this.memory.get( - storage_key_data, - storage_key_len as usize - ).map_err(|_| "Invalid attempt to determine storage_key in ext_get_child_storage_into")?; - let key = this.memory.get( - key_data, - key_len as usize - ).map_err(|_| "Invalid attempt to get key in ext_get_child_storage_into")?; - - let maybe_value = { - let storage_key = ChildStorageKey::from_slice(&*storage_key) - .ok_or_else(|| "ext_get_child_storage_into: child storage key is not valid")?; - this.ext.child_storage(storage_key, &key) - }; - debug_trace!( - target: "wasm-trace", "*** Getting storage: {} -> {} == {} [k={}]", - primitives::hexdisplay::ascii_format(&storage_key), - if let Some(_preimage) = this.hash_lookup.get(&key) { - format!("%{}", ::primitives::hexdisplay::ascii_format(&_preimage)) - } else { - format!(" {}", ::primitives::hexdisplay::ascii_format(&key)) - }, - if let Some(ref b) = maybe_value { - &format!("{}", HexDisplay::from(b)) - } else { - "" - }, - HexDisplay::from(&key), - ); - - if let Some(value) = maybe_value { - let value = &value[value_offset as usize..]; - let written = std::cmp::min(value_len as usize, value.len()); - this.memory.set(value_data, &value[..written]) - .map_err(|_| "Invalid attempt to set value in ext_get_child_storage_into")?; - Ok(value.len() as u32) - } else { - Ok(u32::max_value()) - } - }, - ext_storage_root(result: *mut u8) => { - let r = this.ext.storage_root(); - this.memory.set(result, r.as_ref()) - .map_err(|_| "Invalid attempt to set memory in ext_storage_root")?; - Ok(()) - }, - ext_child_storage_root( - storage_key_data: *const u8, - storage_key_len: u32, - written_out: *mut u32 - ) -> *mut u8 => { - let storage_key = this.memory.get(storage_key_data, storage_key_len as usize) - .map_err(|_| "Invalid attempt to determine storage_key in ext_child_storage_root")?; - let storage_key = ChildStorageKey::from_slice(&*storage_key) - .ok_or_else(|| "ext_child_storage_root: child storage key is not valid")?; - let value = this.ext.child_storage_root(storage_key); - - let offset = this.heap.allocate(value.len() as u32)? as u32; - this.memory.set(offset, &value) - .map_err(|_| "Invalid attempt to set memory in ext_child_storage_root")?; - this.memory.write_primitive(written_out, value.len() as u32) - .map_err(|_| "Invalid attempt to write written_out in ext_child_storage_root")?; - Ok(offset) - }, - ext_storage_changes_root(parent_hash_data: *const u8, parent_hash_len: u32, result: *mut u8) -> u32 => { - let mut parent_hash = H256::default(); - if parent_hash_len != parent_hash.as_ref().len() as u32 { - return Err("Invalid parent_hash_len in ext_storage_changes_root".into()); - } - let raw_parent_hash = this.memory.get(parent_hash_data, parent_hash_len as usize) - .map_err(|_| "Invalid attempt to get parent_hash in ext_storage_changes_root")?; - parent_hash.as_mut().copy_from_slice(&raw_parent_hash[..]); - let r = this.ext.storage_changes_root(parent_hash) - .map_err(|_| "Invaid parent_hash passed to ext_storage_changes_root")?; - if let Some(r) = r { - this.memory.set(result, &r[..]) - .map_err(|_| "Invalid attempt to set memory in ext_storage_changes_root")?; - Ok(1) - } else { - Ok(0) - } - }, - ext_blake2_256_enumerated_trie_root( - values_data: *const u8, - lens_data: *const u32, - lens_len: u32, - result: *mut u8 - ) => { - let values = (0..lens_len) - .map(|i| this.memory.read_primitive(lens_data + i * 4)) - .collect::>>()? - .into_iter() - .scan(0u32, |acc, v| { let o = *acc; *acc += v; Some((o, v)) }) - .map(|(offset, len)| - this.memory.get(values_data + offset, len as usize) - .map_err(|_| - Error::from( + ext_blake2_256_enumerated_trie_root( + values_data: Pointer, + lens_data: Pointer, + lens_len: WordSize, + result: Pointer, + ) { + let values = (0..lens_len) + .map(|i| context.read_primitive(lens_data.offset(i).ok_or("Pointer overflow")?)) + .collect::, _>>()? + .into_iter() + .scan(0u32, |acc, v| { let o = *acc; *acc += v; Some((o, v)) }) + .map(|(offset, len)| + context.read_memory(values_data.offset(offset).ok_or("Pointer overflow")?, len) + .map_err(|_| "Invalid attempt to get memory in ext_blake2_256_enumerated_trie_root" ) - ) - ) - .collect::>>()?; - let r = Layout::::ordered_trie_root(values.into_iter()); - this.memory.set(result, &r[..]) - .map_err(|_| "Invalid attempt to set memory in ext_blake2_256_enumerated_trie_root")?; - Ok(()) - }, - ext_chain_id() -> u64 => { - Ok(this.ext.chain_id()) - }, - ext_twox_64(data: *const u8, len: u32, out: *mut u8) => { - let result: [u8; 8] = if len == 0 { - let hashed = twox_64(&[0u8; 0]); - debug_trace!(target: "xxhash", "XXhash: '' -> {}", HexDisplay::from(&hashed)); - this.hash_lookup.insert(hashed.to_vec(), vec![]); - hashed - } else { - let key = this.memory.get(data, len as usize) - .map_err(|_| "Invalid attempt to get key in ext_twox_64")?; - let hashed_key = twox_64(&key); - - debug_trace!( - target: "xxhash", "XXhash: {} -> {}", - if let Ok(_skey) = str::from_utf8(&key) { - _skey - } else { - &format!("{}", HexDisplay::from(&key)) - }, - HexDisplay::from(&hashed_key), - ); - - this.hash_lookup.insert(hashed_key.to_vec(), key); - hashed_key - }; - - this.memory.set(out, &result).map_err(|_| "Invalid attempt to set result in ext_twox_64")?; - Ok(()) - }, - ext_twox_128(data: *const u8, len: u32, out: *mut u8) => { - let result: [u8; 16] = if len == 0 { - let hashed = twox_128(&[0u8; 0]); - debug_trace!(target: "xxhash", "XXhash: '' -> {}", HexDisplay::from(&hashed)); - this.hash_lookup.insert(hashed.to_vec(), vec![]); - hashed - } else { - let key = this.memory.get(data, len as usize) - .map_err(|_| "Invalid attempt to get key in ext_twox_128")?; - let hashed_key = twox_128(&key); - debug_trace!( - target: "xxhash", "XXhash: {} -> {}", - &if let Ok(_skey) = str::from_utf8(&key) { - *_skey - } else { - format!("{}", HexDisplay::from(&key)) - }, - HexDisplay::from(&hashed_key), - ); - this.hash_lookup.insert(hashed_key.to_vec(), key); - hashed_key - }; - - this.memory.set(out, &result) - .map_err(|_| "Invalid attempt to set result in ext_twox_128")?; - Ok(()) - }, - ext_twox_256(data: *const u8, len: u32, out: *mut u8) => { - let result: [u8; 32] = if len == 0 { - twox_256(&[0u8; 0]) - } else { - let mem = this.memory.get(data, len as usize) - .map_err(|_| "Invalid attempt to get data in ext_twox_256")?; - twox_256(&mem) - }; - this.memory.set(out, &result).map_err(|_| "Invalid attempt to set result in ext_twox_256")?; - Ok(()) - }, - ext_blake2_128(data: *const u8, len: u32, out: *mut u8) => { - let result: [u8; 16] = if len == 0 { - let hashed = blake2_128(&[0u8; 0]); - this.hash_lookup.insert(hashed.to_vec(), vec![]); - hashed - } else { - let key = this.memory.get(data, len as usize) - .map_err(|_| "Invalid attempt to get key in ext_blake2_128")?; - let hashed_key = blake2_128(&key); - this.hash_lookup.insert(hashed_key.to_vec(), key); - hashed_key - }; - - this.memory.set(out, &result) - .map_err(|_| "Invalid attempt to set result in ext_blake2_128")?; - Ok(()) - }, - ext_blake2_256(data: *const u8, len: u32, out: *mut u8) => { - let result: [u8; 32] = if len == 0 { - blake2_256(&[0u8; 0]) - } else { - let mem = this.memory.get(data, len as usize) - .map_err(|_| "Invalid attempt to get data in ext_blake2_256")?; - blake2_256(&mem) - }; - this.memory.set(out, &result).map_err(|_| "Invalid attempt to set result in ext_blake2_256")?; - Ok(()) - }, - ext_keccak_256(data: *const u8, len: u32, out: *mut u8) => { - let result: [u8; 32] = if len == 0 { - tiny_keccak::keccak256(&[0u8; 0]) - } else { - let mem = this.memory.get(data, len as usize) - .map_err(|_| "Invalid attempt to get data in ext_keccak_256")?; - tiny_keccak::keccak256(&mem) - }; - this.memory.set(out, &result).map_err(|_| "Invalid attempt to set result in ext_keccak_256")?; - Ok(()) - }, - ext_ed25519_public_keys(id_data: *const u8, result_len: *mut u32) -> *mut u8 => { - let mut id = [0u8; 4]; - this.memory.get_into(id_data, &mut id[..]) - .map_err(|_| "Invalid attempt to get id in ext_ed25519_public_keys")?; - let key_type = KeyTypeId(id); - - let keys = this.ext - .keystore() - .ok_or("No `keystore` associated for the current context!")? - .read() - .ed25519_public_keys(key_type) - .encode(); - - let len = keys.len() as u32; - let offset = this.heap.allocate(len)? as u32; - - this.memory.set(offset, keys.as_ref()) - .map_err(|_| "Invalid attempt to set memory in ext_ed25519_public_keys")?; - this.memory.write_primitive(result_len, len) - .map_err(|_| "Invalid attempt to write result_len in ext_ed25519_public_keys")?; - - Ok(offset) - }, - ext_ed25519_verify( - msg_data: *const u8, - msg_len: u32, - sig_data: *const u8, - pubkey_data: *const u8, - ) -> u32 => { - let mut sig = [0u8; 64]; - this.memory.get_into(sig_data, &mut sig[..]) - .map_err(|_| "Invalid attempt to get signature in ext_ed25519_verify")?; - let mut pubkey = [0u8; 32]; - this.memory.get_into(pubkey_data, &mut pubkey[..]) - .map_err(|_| "Invalid attempt to get pubkey in ext_ed25519_verify")?; - let msg = this.memory.get(msg_data, msg_len as usize) - .map_err(|_| "Invalid attempt to get message in ext_ed25519_verify")?; - - Ok(if ed25519::Pair::verify_weak(&sig, &msg, &pubkey) { - 0 - } else { - 5 - }) - }, - ext_ed25519_generate(id_data: *const u8, seed: *const u8, seed_len: u32, out: *mut u8) => { - let mut id = [0u8; 4]; - this.memory.get_into(id_data, &mut id[..]) - .map_err(|_| "Invalid attempt to get id in ext_ed25519_generate")?; - let key_type = KeyTypeId(id); - - let seed = if seed_len == 0 { - None - } else { - Some( - this.memory.get(seed, seed_len as usize) - .map_err(|_| "Invalid attempt to get seed in ext_ed25519_generate")? - ) - }; - - let seed = seed.as_ref() - .map(|seed| - std::str::from_utf8(&seed) - .map_err(|_| "Seed not a valid utf8 string in ext_sr25119_generate") - ).transpose()?; - - let pubkey = this.ext - .keystore() - .ok_or("No `keystore` associated for the current context!")? - .write() - .ed25519_generate_new(key_type, seed) - .map_err(|_| "`ed25519` key generation failed")?; - - this.memory.set(out, pubkey.as_ref()) - .map_err(|_| "Invalid attempt to set out in ext_ed25519_generate".into()) - }, - ext_ed25519_sign( - id_data: *const u8, - pubkey_data: *const u8, - msg_data: *const u8, - msg_len: u32, - out: *mut u8, - ) -> u32 => { - let mut id = [0u8; 4]; - this.memory.get_into(id_data, &mut id[..]) - .map_err(|_| "Invalid attempt to get id in ext_ed25519_sign")?; - let key_type = KeyTypeId(id); - - let mut pubkey = [0u8; 32]; - this.memory.get_into(pubkey_data, &mut pubkey[..]) - .map_err(|_| "Invalid attempt to get pubkey in ext_ed25519_sign")?; - - let msg = this.memory.get(msg_data, msg_len as usize) - .map_err(|_| "Invalid attempt to get message in ext_ed25519_sign")?; - - let pub_key = ed25519::Public::try_from(pubkey.as_ref()) - .map_err(|_| "Invalid `ed25519` public key")?; - - let signature = this.ext - .keystore() - .ok_or("No `keystore` associated for the current context!")? - .read() - .ed25519_key_pair(key_type, &pub_key) - .map(|k| k.sign(msg.as_ref())); - - match signature { - Some(signature) => { - this.memory - .set(out, signature.as_ref()) - .map_err(|_| "Invalid attempt to set out in ext_ed25519_sign")?; - Ok(0) - }, - None => Ok(1), + ) + .collect::, _>>()?; + let r = Layout::::ordered_trie_root(values.into_iter()); + context.write_memory(result, &r[..]) + .map_err(|_| "Invalid attempt to set memory in ext_blake2_256_enumerated_trie_root")?; + Ok(()) } - }, - ext_sr25519_public_keys(id_data: *const u8, result_len: *mut u32) -> *mut u8 => { - let mut id = [0u8; 4]; - this.memory.get_into(id_data, &mut id[..]) - .map_err(|_| "Invalid attempt to get id in ext_sr25519_public_keys")?; - let key_type = KeyTypeId(id); - let keys = this.ext - .keystore() - .ok_or("No `keystore` associated for the current context!")? - .read() - .sr25519_public_keys(key_type) - .encode(); - - let len = keys.len() as u32; - let offset = this.heap.allocate(len)? as u32; - - this.memory.set(offset, keys.as_ref()) - .map_err(|_| "Invalid attempt to set memory in ext_sr25519_public_keys")?; - this.memory.write_primitive(result_len, len) - .map_err(|_| "Invalid attempt to write result_len in ext_sr25519_public_keys")?; - - Ok(offset) - }, - ext_sr25519_verify( - msg_data: *const u8, - msg_len: u32, - sig_data: *const u8, - pubkey_data: *const u8, - ) -> u32 => { - let mut sig = [0u8; 64]; - this.memory.get_into(sig_data, &mut sig[..]) - .map_err(|_| "Invalid attempt to get signature in ext_sr25519_verify")?; - let mut pubkey = [0u8; 32]; - this.memory.get_into(pubkey_data, &mut pubkey[..]) - .map_err(|_| "Invalid attempt to get pubkey in ext_sr25519_verify")?; - let msg = this.memory.get(msg_data, msg_len as usize) - .map_err(|_| "Invalid attempt to get message in ext_sr25519_verify")?; - - Ok(if sr25519::Pair::verify_weak(&sig, &msg, &pubkey) { - 0 - } else { - 5 - }) - }, - ext_sr25519_generate(id_data: *const u8, seed: *const u8, seed_len: u32, out: *mut u8) => { - let mut id = [0u8; 4]; - this.memory.get_into(id_data, &mut id[..]) - .map_err(|_| "Invalid attempt to get id in ext_sr25519_generate")?; - let key_type = KeyTypeId(id); - let seed = if seed_len == 0 { - None - } else { - Some( - this.memory.get(seed, seed_len as usize) - .map_err(|_| "Invalid attempt to get seed in ext_sr25519_generate")? - ) - }; - - let seed = seed.as_ref() - .map(|seed| - std::str::from_utf8(&seed) - .map_err(|_| "Seed not a valid utf8 string in ext_sr25119_generate") - ) - .transpose()?; - - let pubkey = this.ext - .keystore() - .ok_or("No `keystore` associated for the current context!")? - .write() - .sr25519_generate_new(key_type, seed) - .map_err(|_| "`sr25519` key generation failed")?; - - this.memory.set(out, pubkey.as_ref()) - .map_err(|_| "Invalid attempt to set out in ext_sr25519_generate".into()) - }, - ext_sr25519_sign( - id_data: *const u8, - pubkey_data: *const u8, - msg_data: *const u8, - msg_len: u32, - out: *mut u8, - ) -> u32 => { - let mut id = [0u8; 4]; - this.memory.get_into(id_data, &mut id[..]) - .map_err(|_| "Invalid attempt to get id in ext_sr25519_sign")?; - let key_type = KeyTypeId(id); - - let mut pubkey = [0u8; 32]; - this.memory.get_into(pubkey_data, &mut pubkey[..]) - .map_err(|_| "Invalid attempt to get pubkey in ext_sr25519_sign")?; - - let msg = this.memory.get(msg_data, msg_len as usize) - .map_err(|_| "Invalid attempt to get message in ext_sr25519_sign")?; - - let pub_key = sr25519::Public::try_from(pubkey.as_ref()) - .map_err(|_| "Invalid `sr25519` public key")?; - - let signature = this.ext - .keystore() - .ok_or("No `keystore` associated for the current context!")? - .read() - .sr25519_key_pair(key_type, &pub_key) - .map(|k| k.sign(msg.as_ref())); - - match signature { - Some(signature) => { - this.memory.set(out, signature.as_ref()) - .map_err(|_| "Invalid attempt to set out in ext_sr25519_sign")?; - Ok(0) - }, - None => Ok(1), + ext_chain_id() -> u64 { + Ok(runtime_io::chain_id()) } - }, - ext_secp256k1_ecdsa_recover(msg_data: *const u8, sig_data: *const u8, pubkey_data: *mut u8) -> u32 => { - let mut sig = [0u8; 65]; - this.memory.get_into(sig_data, &mut sig[..]) - .map_err(|_| "Invalid attempt to get signature in ext_secp256k1_ecdsa_recover")?; - let rs = match secp256k1::Signature::parse_slice(&sig[0..64]) { - Ok(rs) => rs, - _ => return Ok(1), - }; - let v = match secp256k1::RecoveryId::parse(if sig[64] > 26 { sig[64] - 27 } else { sig[64] } as u8) { - Ok(v) => v, - _ => return Ok(2), - }; - - let mut msg = [0u8; 32]; - this.memory.get_into(msg_data, &mut msg[..]) - .map_err(|_| "Invalid attempt to get message in ext_secp256k1_ecdsa_recover")?; - - let pubkey = match secp256k1::recover(&secp256k1::Message::parse(&msg), &rs, &v) { - Ok(pk) => pk, - _ => return Ok(3), - }; - - this.memory.set(pubkey_data, &pubkey.serialize()[1..65]) - .map_err(|_| "Invalid attempt to set pubkey in ext_secp256k1_ecdsa_recover")?; - - Ok(0) - }, - ext_is_validator() -> u32 => { - this.ext.offchain() - .map(|o| if o.is_validator() { 1 } else { 0 }) - .ok_or("Calling unavailable API ext_is_validator: wasm".into()) - }, - ext_submit_transaction(msg_data: *const u8, len: u32) -> u32 => { - let extrinsic = this.memory.get(msg_data, len as usize) - .map_err(|_| "OOB while ext_submit_transaction: wasm")?; - - let res = this.ext.offchain() - .map(|api| api.submit_transaction(extrinsic)) - .ok_or_else(|| "Calling unavailable API ext_submit_transaction: wasm")?; - - Ok(if res.is_ok() { 0 } else { 1 }) - }, - ext_network_state(written_out: *mut u32) -> *mut u8 => { - let res = this.ext.offchain() - .map(|api| api.network_state()) - .ok_or_else(|| "Calling unavailable API ext_network_state: wasm")?; - - let encoded = res.encode(); - let len = encoded.len() as u32; - let offset = this.heap.allocate(len)? as u32; - this.memory.set(offset, &encoded) - .map_err(|_| "Invalid attempt to set memory in ext_network_state")?; - - this.memory.write_primitive(written_out, len) - .map_err(|_| "Invalid attempt to write written_out in ext_network_state")?; - - Ok(offset) - }, - ext_timestamp() -> u64 => { - let timestamp = this.ext.offchain() - .map(|api| api.timestamp()) - .ok_or_else(|| "Calling unavailable API ext_timestamp: wasm")?; - Ok(timestamp.unix_millis()) - }, - ext_sleep_until(deadline: u64) => { - this.ext.offchain() - .map(|api| api.sleep_until(offchain::Timestamp::from_unix_millis(deadline))) - .ok_or_else(|| "Calling unavailable API ext_sleep_until: wasm")?; - Ok(()) - }, - ext_random_seed(seed_data: *mut u8) => { - // NOTE the runtime as assumptions about seed size. - let seed: [u8; 32] = this.ext.offchain() - .map(|api| api.random_seed()) - .ok_or_else(|| "Calling unavailable API ext_random_seed: wasm")?; - - this.memory.set(seed_data, &seed) - .map_err(|_| "Invalid attempt to set value in ext_random_seed")?; - Ok(()) - }, - ext_local_storage_set(kind: u32, key: *const u8, key_len: u32, value: *const u8, value_len: u32) => { - let kind = offchain::StorageKind::try_from(kind) - .map_err(|_| "storage kind OOB while ext_local_storage_set: wasm")?; - let key = this.memory.get(key, key_len as usize) - .map_err(|_| "OOB while ext_local_storage_set: wasm")?; - let value = this.memory.get(value, value_len as usize) - .map_err(|_| "OOB while ext_local_storage_set: wasm")?; - - this.ext.offchain() - .map(|api| api.local_storage_set(kind, &key, &value)) - .ok_or_else(|| "Calling unavailable API ext_local_storage_set: wasm")?; - - Ok(()) - }, - ext_local_storage_get(kind: u32, key: *const u8, key_len: u32, value_len: *mut u32) -> *mut u8 => { - let kind = offchain::StorageKind::try_from(kind) - .map_err(|_| "storage kind OOB while ext_local_storage_get: wasm")?; - let key = this.memory.get(key, key_len as usize) - .map_err(|_| "OOB while ext_local_storage_get: wasm")?; - - let maybe_value = this.ext.offchain() - .map(|api| api.local_storage_get(kind, &key)) - .ok_or_else(|| "Calling unavailable API ext_local_storage_get: wasm")?; - - let (offset, len) = if let Some(value) = maybe_value { - let offset = this.heap.allocate(value.len() as u32)? as u32; - this.memory.set(offset, &value) - .map_err(|_| "Invalid attempt to set memory in ext_local_storage_get")?; - (offset, value.len() as u32) - } else { - (0, u32::max_value()) - }; - - this.memory.write_primitive(value_len, len) - .map_err(|_| "Invalid attempt to write value_len in ext_local_storage_get")?; - - Ok(offset) - }, - ext_local_storage_compare_and_set( - kind: u32, - key: *const u8, - key_len: u32, - old_value: *const u8, - old_value_len: u32, - new_value: *const u8, - new_value_len: u32 - ) -> u32 => { - let kind = offchain::StorageKind::try_from(kind) - .map_err(|_| "storage kind OOB while ext_local_storage_compare_and_set: wasm")?; - let key = this.memory.get(key, key_len as usize) - .map_err(|_| "OOB while ext_local_storage_compare_and_set: wasm")?; - let new_value = this.memory.get(new_value, new_value_len as usize) - .map_err(|_| "OOB while ext_local_storage_compare_and_set: wasm")?; - - let res = { - if old_value_len == u32::max_value() { - this.ext.offchain() - .map(|api| api.local_storage_compare_and_set(kind, &key, None, &new_value)) - .ok_or_else(|| "Calling unavailable API ext_local_storage_compare_and_set: wasm")? + ext_twox_64(data: Pointer, len: WordSize, out: Pointer) { + let result: [u8; 8] = if len == 0 { + let hashed = twox_64(&[0u8; 0]); + hashed } else { - let v = this.memory.get(old_value, old_value_len as usize) - .map_err(|_| "OOB while ext_local_storage_compare_and_set: wasm")?; - this.ext.offchain() - .map(|api| api.local_storage_compare_and_set(kind, &key, Some(v.as_slice()), &new_value)) - .ok_or_else(|| "Calling unavailable API ext_local_storage_compare_and_set: wasm")? - } - }; + let key = context.read_memory(data, len) + .map_err(|_| "Invalid attempt to get key in ext_twox_64")?; + let hashed_key = twox_64(&key); + hashed_key + }; - Ok(if res { 0 } else { 1 }) - }, - ext_http_request_start( - method: *const u8, - method_len: u32, - url: *const u8, - url_len: u32, - meta: *const u8, - meta_len: u32 - ) -> u32 => { - let method = this.memory.get(method, method_len as usize) - .map_err(|_| "OOB while ext_http_request_start: wasm")?; - let url = this.memory.get(url, url_len as usize) - .map_err(|_| "OOB while ext_http_request_start: wasm")?; - let meta = this.memory.get(meta, meta_len as usize) - .map_err(|_| "OOB while ext_http_request_start: wasm")?; - - let method_str = str::from_utf8(&method) - .map_err(|_| "invalid str while ext_http_request_start: wasm")?; - let url_str = str::from_utf8(&url) - .map_err(|_| "invalid str while ext_http_request_start: wasm")?; - - let id = this.ext.offchain() - .map(|api| api.http_request_start(method_str, url_str, &*meta)) - .ok_or_else(|| "Calling unavailable API ext_http_request_start: wasm")?; - - if let Ok(id) = id { - Ok(id.into()) - } else { - Ok(u32::max_value()) + context.write_memory(out, &result) + .map_err(|_| "Invalid attempt to set result in ext_twox_64")?; + Ok(()) } - }, - ext_http_request_add_header( - request_id: u32, - name: *const u8, - name_len: u32, - value: *const u8, - value_len: u32 - ) -> u32 => { - let name = this.memory.get(name, name_len as usize) - .map_err(|_| "OOB while ext_http_request_add_header: wasm")?; - let value = this.memory.get(value, value_len as usize) - .map_err(|_| "OOB while ext_http_request_add_header: wasm")?; - let name_str = str::from_utf8(&name) - .map_err(|_| "Invalid str while ext_http_request_add_header: wasm")?; - let value_str = str::from_utf8(&value) - .map_err(|_| "Invalid str while ext_http_request_add_header: wasm")?; + ext_twox_128(data: Pointer, len: WordSize, out: Pointer) { + let result: [u8; 16] = if len == 0 { + let hashed = twox_128(&[0u8; 0]); + hashed + } else { + let key = context.read_memory(data, len) + .map_err(|_| "Invalid attempt to get key in ext_twox_128")?; + let hashed_key = twox_128(&key); + hashed_key + }; - let res = this.ext.offchain() - .map(|api| api.http_request_add_header( + context.write_memory(out, &result) + .map_err(|_| "Invalid attempt to set result in ext_twox_128")?; + Ok(()) + } + + ext_twox_256(data: Pointer, len: WordSize, out: Pointer) { + let result: [u8; 32] = if len == 0 { + twox_256(&[0u8; 0]) + } else { + let mem = context.read_memory(data, len) + .map_err(|_| "Invalid attempt to get data in ext_twox_256")?; + twox_256(&mem) + }; + context.write_memory(out, &result) + .map_err(|_| "Invalid attempt to set result in ext_twox_256")?; + Ok(()) + } + + ext_blake2_128(data: Pointer, len: WordSize, out: Pointer) { + let result: [u8; 16] = if len == 0 { + let hashed = blake2_128(&[0u8; 0]); + hashed + } else { + let key = context.read_memory(data, len) + .map_err(|_| "Invalid attempt to get key in ext_blake2_128")?; + let hashed_key = blake2_128(&key); + hashed_key + }; + + context.write_memory(out, &result) + .map_err(|_| "Invalid attempt to set result in ext_blake2_128")?; + Ok(()) + } + + ext_blake2_256(data: Pointer, len: WordSize, out: Pointer) { + let result: [u8; 32] = if len == 0 { + blake2_256(&[0u8; 0]) + } else { + let mem = context.read_memory(data, len) + .map_err(|_| "Invalid attempt to get data in ext_blake2_256")?; + blake2_256(&mem) + }; + context.write_memory(out, &result) + .map_err(|_| "Invalid attempt to set result in ext_blake2_256")?; + Ok(()) + } + + ext_keccak_256(data: Pointer, len: WordSize, out: Pointer) { + let result: [u8; 32] = if len == 0 { + tiny_keccak::keccak256(&[0u8; 0]) + } else { + let mem = context.read_memory(data, len) + .map_err(|_| "Invalid attempt to get data in ext_keccak_256")?; + tiny_keccak::keccak256(&mem) + }; + context.write_memory(out, &result) + .map_err(|_| "Invalid attempt to set result in ext_keccak_256")?; + Ok(()) + } + + ext_ed25519_public_keys(id_data: Pointer, result_len: Pointer) -> Pointer { + let mut id = [0u8; 4]; + context.read_memory_into(id_data, &mut id[..]) + .map_err(|_| "Invalid attempt to get id in ext_ed25519_public_keys")?; + let key_type = KeyTypeId(id); + + let keys = runtime_io::ed25519_public_keys(key_type).encode(); + + let len = keys.len() as u32; + let offset = context.allocate_memory(len)?; + + context.write_memory(offset, keys.as_ref()) + .map_err(|_| "Invalid attempt to set memory in ext_ed25519_public_keys")?; + context.write_primitive(result_len, len) + .map_err(|_| "Invalid attempt to write result_len in ext_ed25519_public_keys")?; + + Ok(offset) + } + + ext_ed25519_verify( + msg_data: Pointer, + msg_len: WordSize, + sig_data: Pointer, + pubkey_data: Pointer, + ) -> u32 { + let mut sig = [0u8; 64]; + context.read_memory_into(sig_data, &mut sig[..]) + .map_err(|_| "Invalid attempt to get signature in ext_ed25519_verify")?; + let mut pubkey = [0u8; 32]; + context.read_memory_into(pubkey_data, &mut pubkey[..]) + .map_err(|_| "Invalid attempt to get pubkey in ext_ed25519_verify")?; + let msg = context.read_memory(msg_data, msg_len) + .map_err(|_| "Invalid attempt to get message in ext_ed25519_verify")?; + + Ok(if ed25519::Pair::verify_weak(&sig, &msg, &pubkey) { + 0 + } else { + 1 + }) + } + + ext_ed25519_generate( + id_data: Pointer, + seed: Pointer, + seed_len: WordSize, + out: Pointer, + ) { + let mut id = [0u8; 4]; + context.read_memory_into(id_data, &mut id[..]) + .map_err(|_| "Invalid attempt to get id in ext_ed25519_generate")?; + let key_type = KeyTypeId(id); + + let seed = if seed_len == 0 { + None + } else { + Some( + context.read_memory(seed, seed_len) + .map_err(|_| "Invalid attempt to get seed in ext_ed25519_generate")? + ) + }; + + let seed = seed.as_ref() + .map(|seed| + std::str::from_utf8(&seed) + .map_err(|_| "Seed not a valid utf8 string in ext_sr25119_generate") + ).transpose()?; + + let pubkey = runtime_io::ed25519_generate(key_type, seed); + + context.write_memory(out, pubkey.as_ref()) + .map_err(|_| "Invalid attempt to set out in ext_ed25519_generate".into()) + } + + ext_ed25519_sign( + id_data: Pointer, + pubkey_data: Pointer, + msg_data: Pointer, + msg_len: WordSize, + out: Pointer, + ) -> u32 { + let mut id = [0u8; 4]; + context.read_memory_into(id_data, &mut id[..]) + .map_err(|_| "Invalid attempt to get id in ext_ed25519_sign")?; + let key_type = KeyTypeId(id); + + let mut pubkey = [0u8; 32]; + context.read_memory_into(pubkey_data, &mut pubkey[..]) + .map_err(|_| "Invalid attempt to get pubkey in ext_ed25519_sign")?; + + let msg = context.read_memory(msg_data, msg_len) + .map_err(|_| "Invalid attempt to get message in ext_ed25519_sign")?; + + let pub_key = ed25519::Public::try_from(pubkey.as_ref()) + .map_err(|_| "Invalid `ed25519` public key")?; + + let signature = runtime_io::ed25519_sign(key_type, &pub_key, &msg); + + match signature { + Some(signature) => { + context.write_memory(out, signature.as_ref()) + .map_err(|_| "Invalid attempt to set out in ext_ed25519_sign")?; + Ok(0) + }, + None => Ok(1), + } + } + + ext_sr25519_public_keys(id_data: Pointer, result_len: Pointer) -> Pointer { + let mut id = [0u8; 4]; + context.read_memory_into(id_data, &mut id[..]) + .map_err(|_| "Invalid attempt to get id in ext_sr25519_public_keys")?; + let key_type = KeyTypeId(id); + + let keys = runtime_io::sr25519_public_keys(key_type).encode(); + + let len = keys.len() as u32; + let offset = context.allocate_memory(len)?; + + context.write_memory(offset, keys.as_ref()) + .map_err(|_| "Invalid attempt to set memory in ext_sr25519_public_keys")?; + context.write_primitive(result_len, len) + .map_err(|_| "Invalid attempt to write result_len in ext_sr25519_public_keys")?; + + Ok(offset) + } + + ext_sr25519_verify( + msg_data: Pointer, + msg_len: WordSize, + sig_data: Pointer, + pubkey_data: Pointer, + ) -> u32 { + let mut sig = [0u8; 64]; + context.read_memory_into(sig_data, &mut sig[..]) + .map_err(|_| "Invalid attempt to get signature in ext_sr25519_verify")?; + let mut pubkey = [0u8; 32]; + context.read_memory_into(pubkey_data, &mut pubkey[..]) + .map_err(|_| "Invalid attempt to get pubkey in ext_sr25519_verify")?; + let msg = context.read_memory(msg_data, msg_len) + .map_err(|_| "Invalid attempt to get message in ext_sr25519_verify")?; + + Ok(if sr25519::Pair::verify_weak(&sig, &msg, &pubkey) { + 0 + } else { + 1 + }) + } + + ext_sr25519_generate( + id_data: Pointer, + seed: Pointer, + seed_len: WordSize, + out: Pointer, + ) { + let mut id = [0u8; 4]; + context.read_memory_into(id_data, &mut id[..]) + .map_err(|_| "Invalid attempt to get id in ext_sr25519_generate")?; + let key_type = KeyTypeId(id); + let seed = if seed_len == 0 { + None + } else { + Some( + context.read_memory(seed, seed_len) + .map_err(|_| "Invalid attempt to get seed in ext_sr25519_generate")? + ) + }; + + let seed = seed.as_ref() + .map(|seed| + std::str::from_utf8(&seed) + .map_err(|_| "Seed not a valid utf8 string in ext_sr25119_generate") + ) + .transpose()?; + + let pubkey = runtime_io::sr25519_generate(key_type, seed); + + context.write_memory(out, pubkey.as_ref()) + .map_err(|_| "Invalid attempt to set out in ext_sr25519_generate".into()) + } + + ext_sr25519_sign( + id_data: Pointer, + pubkey_data: Pointer, + msg_data: Pointer, + msg_len: WordSize, + out: Pointer, + ) -> u32 { + let mut id = [0u8; 4]; + context.read_memory_into(id_data, &mut id[..]) + .map_err(|_| "Invalid attempt to get id in ext_sr25519_sign")?; + let key_type = KeyTypeId(id); + + let mut pubkey = [0u8; 32]; + context.read_memory_into(pubkey_data, &mut pubkey[..]) + .map_err(|_| "Invalid attempt to get pubkey in ext_sr25519_sign")?; + + let msg = context.read_memory(msg_data, msg_len) + .map_err(|_| "Invalid attempt to get message in ext_sr25519_sign")?; + + let pub_key = sr25519::Public::try_from(pubkey.as_ref()) + .map_err(|_| "Invalid `sr25519` public key")?; + + let signature = runtime_io::sr25519_sign(key_type, &pub_key, &msg); + + match signature { + Some(signature) => { + context.write_memory(out, signature.as_ref()) + .map_err(|_| "Invalid attempt to set out in ext_sr25519_sign")?; + Ok(0) + }, + None => Ok(1), + } + } + + ext_secp256k1_ecdsa_recover( + msg_data: Pointer, + sig_data: Pointer, + pubkey_data: Pointer, + ) -> u32 { + let mut sig = [0u8; 65]; + context.read_memory_into(sig_data, &mut sig[..]) + .map_err(|_| "Invalid attempt to get signature in ext_secp256k1_ecdsa_recover")?; + let rs = match secp256k1::Signature::parse_slice(&sig[0..64]) { + Ok(rs) => rs, + _ => return Ok(1), + }; + + let recovery_id = if sig[64] > 26 { sig[64] - 27 } else { sig[64] } as u8; + let v = match secp256k1::RecoveryId::parse(recovery_id) { + Ok(v) => v, + _ => return Ok(2), + }; + + let mut msg = [0u8; 32]; + context.read_memory_into(msg_data, &mut msg[..]) + .map_err(|_| "Invalid attempt to get message in ext_secp256k1_ecdsa_recover")?; + + let pubkey = match secp256k1::recover(&secp256k1::Message::parse(&msg), &rs, &v) { + Ok(pk) => pk, + _ => return Ok(3), + }; + + context.write_memory(pubkey_data, &pubkey.serialize()[1..65]) + .map_err(|_| "Invalid attempt to set pubkey in ext_secp256k1_ecdsa_recover")?; + + Ok(0) + } + + ext_is_validator() -> u32 { + if runtime_io::is_validator() { Ok(1) } else { Ok(0) } + } + + ext_submit_transaction(msg_data: Pointer, len: WordSize) -> u32 { + let extrinsic = context.read_memory(msg_data, len) + .map_err(|_| "OOB while ext_submit_transaction: wasm")?; + + let res = runtime_io::submit_transaction(extrinsic); + + Ok(if res.is_ok() { 0 } else { 1 }) + } + + ext_network_state(written_out: Pointer) -> Pointer { + let res = runtime_io::network_state(); + + let encoded = res.encode(); + let len = encoded.len() as u32; + let offset = context.allocate_memory(len)?; + context.write_memory(offset, &encoded) + .map_err(|_| "Invalid attempt to set memory in ext_network_state")?; + + context.write_primitive(written_out, len) + .map_err(|_| "Invalid attempt to write written_out in ext_network_state")?; + + Ok(offset) + } + + ext_timestamp() -> u64 { + Ok(runtime_io::timestamp().unix_millis()) + } + + ext_sleep_until(deadline: u64) { + runtime_io::sleep_until(offchain::Timestamp::from_unix_millis(deadline)); + Ok(()) + } + + ext_random_seed(seed_data: Pointer) { + // NOTE the runtime as assumptions about seed size. + let seed = runtime_io::random_seed(); + + context.write_memory(seed_data, &seed) + .map_err(|_| "Invalid attempt to set value in ext_random_seed")?; + Ok(()) + } + + ext_local_storage_set( + kind: u32, + key: Pointer, + key_len: WordSize, + value: Pointer, + value_len: WordSize, + ) { + let kind = offchain::StorageKind::try_from(kind) + .map_err(|_| "storage kind OOB while ext_local_storage_set: wasm")?; + let key = context.read_memory(key, key_len) + .map_err(|_| "OOB while ext_local_storage_set: wasm")?; + let value = context.read_memory(value, value_len) + .map_err(|_| "OOB while ext_local_storage_set: wasm")?; + + runtime_io::local_storage_set(kind, &key, &value); + + Ok(()) + } + + ext_local_storage_get( + kind: u32, + key: Pointer, + key_len: WordSize, + value_len: Pointer, + ) -> Pointer { + let kind = offchain::StorageKind::try_from(kind) + .map_err(|_| "storage kind OOB while ext_local_storage_get: wasm")?; + let key = context.read_memory(key, key_len) + .map_err(|_| "OOB while ext_local_storage_get: wasm")?; + + let maybe_value = runtime_io::local_storage_get(kind, &key); + + let (offset, len) = if let Some(value) = maybe_value { + let offset = context.allocate_memory(value.len() as u32)?; + context.write_memory(offset, &value) + .map_err(|_| "Invalid attempt to set memory in ext_local_storage_get")?; + (offset, value.len() as u32) + } else { + (Pointer::null(), u32::max_value()) + }; + + context.write_primitive(value_len, len) + .map_err(|_| "Invalid attempt to write value_len in ext_local_storage_get")?; + + Ok(offset) + } + + ext_local_storage_compare_and_set( + kind: u32, + key: Pointer, + key_len: WordSize, + old_value: Pointer, + old_value_len: WordSize, + new_value: Pointer, + new_value_len: WordSize, + ) -> u32 { + let kind = offchain::StorageKind::try_from(kind) + .map_err(|_| "storage kind OOB while ext_local_storage_compare_and_set: wasm")?; + let key = context.read_memory(key, key_len) + .map_err(|_| "OOB while ext_local_storage_compare_and_set: wasm")?; + let new_value = context.read_memory(new_value, new_value_len) + .map_err(|_| "OOB while ext_local_storage_compare_and_set: wasm")?; + + let old_value = if old_value_len == u32::max_value() { + None + } else { + Some( + context.read_memory(old_value, old_value_len) + .map_err(|_| "OOB while ext_local_storage_compare_and_set: wasm")? + ) + }; + + let res = runtime_io::local_storage_compare_and_set( + kind, + &key, + old_value.as_ref().map(|v| v.as_ref()), + &new_value, + ); + + Ok(if res { 0 } else { 1 }) + } + + ext_http_request_start( + method: Pointer, + method_len: WordSize, + url: Pointer, + url_len: WordSize, + meta: Pointer, + meta_len: WordSize, + ) -> u32 { + let method = context.read_memory(method, method_len) + .map_err(|_| "OOB while ext_http_request_start: wasm")?; + let url = context.read_memory(url, url_len) + .map_err(|_| "OOB while ext_http_request_start: wasm")?; + let meta = context.read_memory(meta, meta_len) + .map_err(|_| "OOB while ext_http_request_start: wasm")?; + + let method_str = str::from_utf8(&method) + .map_err(|_| "invalid str while ext_http_request_start: wasm")?; + let url_str = str::from_utf8(&url) + .map_err(|_| "invalid str while ext_http_request_start: wasm")?; + + let id = runtime_io::http_request_start(method_str, url_str, &meta); + + if let Ok(id) = id { + Ok(id.into()) + } else { + Ok(u32::max_value()) + } + } + + ext_http_request_add_header( + request_id: u32, + name: Pointer, + name_len: WordSize, + value: Pointer, + value_len: WordSize, + ) -> u32 { + let name = context.read_memory(name, name_len) + .map_err(|_| "OOB while ext_http_request_add_header: wasm")?; + let value = context.read_memory(value, value_len) + .map_err(|_| "OOB while ext_http_request_add_header: wasm")?; + + let name_str = str::from_utf8(&name) + .map_err(|_| "Invalid str while ext_http_request_add_header: wasm")?; + let value_str = str::from_utf8(&value) + .map_err(|_| "Invalid str while ext_http_request_add_header: wasm")?; + + let res = runtime_io::http_request_add_header( offchain::HttpRequestId(request_id as u16), - &name_str, - &value_str, - )) - .ok_or_else(|| "Calling unavailable API ext_http_request_add_header: wasm")?; + name_str, + value_str, + ); - Ok(if res.is_ok() { 0 } else { 1 }) - }, - ext_http_request_write_body( - request_id: u32, - chunk: *const u8, - chunk_len: u32, - deadline: u64 - ) -> u32 => { - let chunk = this.memory.get(chunk, chunk_len as usize) - .map_err(|_| "OOB while ext_http_request_write_body: wasm")?; + Ok(if res.is_ok() { 0 } else { 1 }) + } - let res = this.ext.offchain() - .map(|api| api.http_request_write_body( + ext_http_request_write_body( + request_id: u32, + chunk: Pointer, + chunk_len: WordSize, + deadline: u64, + ) -> u32 { + let chunk = context.read_memory(chunk, chunk_len) + .map_err(|_| "OOB while ext_http_request_write_body: wasm")?; + + let res = runtime_io::http_request_write_body( offchain::HttpRequestId(request_id as u16), &chunk, - deadline_to_timestamp(deadline) - )) - .ok_or_else(|| "Calling unavailable API ext_http_request_write_body: wasm")?; + deadline_to_timestamp(deadline), + ); - Ok(match res { - Ok(()) => 0, - Err(e) => e.into(), - }) - }, - ext_http_response_wait( - ids: *const u32, - ids_len: u32, - statuses: *mut u32, - deadline: u64 - ) => { - let ids = (0..ids_len) - .map(|i| - this.memory.read_primitive(ids + i * 4) - .map(|id: u32| offchain::HttpRequestId(id as u16)) - .map_err(|_| "OOB while ext_http_response_wait: wasm") - ) - .collect::<::std::result::Result, _>>()?; - - let res = this.ext.offchain() - .map(|api| api.http_response_wait(&ids, deadline_to_timestamp(deadline))) - .ok_or_else(|| "Calling unavailable API ext_http_response_wait: wasm")? - .into_iter() - .map(|status| status.into()) - .enumerate() - // make sure to take up to `ids_len` to avoid exceeding the mem. - .take(ids_len as usize); - - for (i, status) in res { - this.memory.write_primitive(statuses + i as u32 * 4, status) - .map_err(|_| "Invalid attempt to set memory in ext_http_response_wait")?; + Ok(match res { + Ok(()) => 0, + Err(e) => e.into(), + }) } - Ok(()) - }, - ext_http_response_headers( - request_id: u32, - written_out: *mut u32 - ) -> *mut u8 => { - use codec::Encode; + ext_http_response_wait( + ids: Pointer, + ids_len: WordSize, + statuses: Pointer, + deadline: u64, + ) { + let ids = (0..ids_len) + .map(|i| + context.read_primitive(ids.offset(i).ok_or("Point overflow")?) + .map(|id: u32| offchain::HttpRequestId(id as u16)) + .map_err(|_| "OOB while ext_http_response_wait: wasm") + ) + .collect::, _>>()?; - let headers = this.ext.offchain() - .map(|api| api.http_response_headers(offchain::HttpRequestId(request_id as u16))) - .ok_or_else(|| "Calling unavailable API ext_http_response_headers: wasm")?; + let res = runtime_io::http_response_wait(&ids, deadline_to_timestamp(deadline)) + .into_iter() + .map(|status| u32::from(status)) + .enumerate() + // make sure to take up to `ids_len` to avoid exceeding the mem. + .take(ids_len as usize); - let encoded = headers.encode(); - let len = encoded.len() as u32; - let offset = this.heap.allocate(len)? as u32; - this.memory.set(offset, &encoded) - .map_err(|_| "Invalid attempt to set memory in ext_http_response_headers")?; - this.memory.write_primitive(written_out, len) - .map_err(|_| "Invalid attempt to write written_out in ext_http_response_headers")?; + for (i, status) in res { + context.write_primitive(statuses.offset(i as u32).ok_or("Point overflow")?, status) + .map_err(|_| "Invalid attempt to set memory in ext_http_response_wait")?; + } - Ok(offset) - }, - ext_http_response_read_body( - request_id: u32, - buffer: *mut u8, - buffer_len: u32, - deadline: u64 - ) -> u32 => { - let mut internal_buffer = Vec::with_capacity(buffer_len as usize); - internal_buffer.resize(buffer_len as usize, 0); + Ok(()) + } - let res = this.ext.offchain() - .map(|api| api.http_response_read_body( + ext_http_response_headers( + request_id: u32, + written_out: Pointer, + ) -> Pointer { + use codec::Encode; + + let headers = runtime_io::http_response_headers(offchain::HttpRequestId(request_id as u16)); + + let encoded = headers.encode(); + let len = encoded.len() as u32; + let offset = context.allocate_memory(len)?; + + context.write_memory(offset, &encoded) + .map_err(|_| "Invalid attempt to set memory in ext_http_response_headers")?; + context.write_primitive(written_out, len) + .map_err(|_| "Invalid attempt to write written_out in ext_http_response_headers")?; + + Ok(offset) + } + + ext_http_response_read_body( + request_id: u32, + buffer: Pointer, + buffer_len: WordSize, + deadline: u64, + ) -> WordSize { + let mut internal_buffer = Vec::with_capacity(buffer_len as usize); + internal_buffer.resize(buffer_len as usize, 0); + + let res = runtime_io::http_response_read_body( offchain::HttpRequestId(request_id as u16), &mut internal_buffer, deadline_to_timestamp(deadline), - )) - .ok_or_else(|| "Calling unavailable API ext_http_response_read_body: wasm")?; + ); - Ok(match res { - Ok(read) => { - this.memory.set(buffer, &internal_buffer[..read]) - .map_err(|_| "Invalid attempt to set memory in ext_http_response_read_body")?; + Ok(match res { + Ok(read) => { + context.write_memory(buffer, &internal_buffer[..read]) + .map_err(|_| "Invalid attempt to set memory in ext_http_response_read_body")?; - read as u32 - }, - Err(err) => { - u32::max_value() - u32::from(err) + 1 - } - }) - }, - ext_sandbox_instantiate( - dispatch_thunk_idx: usize, - wasm_ptr: *const u8, - wasm_len: usize, - imports_ptr: *const u8, - imports_len: usize, - state: usize - ) -> u32 => { - let wasm = this.memory.get(wasm_ptr, wasm_len as usize) - .map_err(|_| "OOB while ext_sandbox_instantiate: wasm")?; - let raw_env_def = this.memory.get(imports_ptr, imports_len as usize) - .map_err(|_| "OOB while ext_sandbox_instantiate: imports")?; - - // Extract a dispatch thunk from instance's table by the specified index. - let dispatch_thunk = { - let table = this.table.as_ref() - .ok_or_else(|| "Runtime doesn't have a table; sandbox is unavailable")?; - table.get(dispatch_thunk_idx) - .map_err(|_| "dispatch_thunk_idx is out of the table bounds")? - .ok_or_else(|| "dispatch_thunk_idx points on an empty table entry")? - .clone() - }; - - let instance_idx_or_err_code = - match sandbox::instantiate(this, dispatch_thunk, &wasm, &raw_env_def, state) { - Ok(instance_idx) => instance_idx, - Err(sandbox::InstantiationError::StartTrapped) => sandbox_primitives::ERR_EXECUTION, - Err(_) => sandbox_primitives::ERR_MODULE, - }; - - Ok(instance_idx_or_err_code as u32) - }, - ext_sandbox_instance_teardown(instance_idx: u32) => { - this.sandbox_store.instance_teardown(instance_idx)?; - Ok(()) - }, - ext_sandbox_invoke( - instance_idx: u32, - export_ptr: *const u8, - export_len: usize, - args_ptr: *const u8, - args_len: usize, - return_val_ptr: *const u8, - return_val_len: usize, - state: usize - ) -> u32 => { - use codec::{Decode, Encode}; - - trace!(target: "sr-sandbox", "invoke, instance_idx={}", instance_idx); - let export = this.memory.get(export_ptr, export_len as usize) - .map_err(|_| "OOB while ext_sandbox_invoke: export") - .and_then(|b| - String::from_utf8(b) - .map_err(|_| "Export name should be a valid utf-8 sequence") - )?; - - // Deserialize arguments and convert them into wasmi types. - let serialized_args = this.memory.get(args_ptr, args_len as usize) - .map_err(|_| "OOB while ext_sandbox_invoke: args")?; - let args = Vec::::decode(&mut &serialized_args[..]) - .map_err(|_| "Can't decode serialized arguments for the invocation")? - .into_iter() - .map(Into::into) - .collect::>(); - - let instance = this.sandbox_store.instance(instance_idx)?; - let result = instance.invoke(&export, &args, this, state); - - match result { - Ok(None) => Ok(sandbox_primitives::ERR_OK), - Ok(Some(val)) => { - // Serialize return value and write it back into the memory. - sandbox_primitives::ReturnValue::Value(val.into()).using_encoded(|val| { - if val.len() > return_val_len as usize { - Err("Return value buffer is too small")?; - } - this.memory - .set(return_val_ptr, val) - .map_err(|_| "Return value buffer is OOB")?; - Ok(sandbox_primitives::ERR_OK) - }) - } - Err(_) => Ok(sandbox_primitives::ERR_EXECUTION), + read as u32 + }, + Err(err) => { + u32::max_value() - u32::from(err) + 1 + } + }) } - }, - ext_sandbox_memory_new(initial: u32, maximum: u32) -> u32 => { - let mem_idx = this.sandbox_store.new_memory(initial, maximum)?; - Ok(mem_idx) - }, - ext_sandbox_memory_get(memory_idx: u32, offset: u32, buf_ptr: *mut u8, buf_len: u32) -> u32 => { - let sandboxed_memory = this.sandbox_store.memory(memory_idx)?; - - match MemoryInstance::transfer( - &sandboxed_memory, - offset as usize, - &this.memory, - buf_ptr as usize, - buf_len as usize, - ) { - Ok(()) => Ok(sandbox_primitives::ERR_OK), - Err(_) => Ok(sandbox_primitives::ERR_OUT_OF_BOUNDS), - } - }, - ext_sandbox_memory_set(memory_idx: u32, offset: u32, val_ptr: *const u8, val_len: u32) -> u32 => { - let sandboxed_memory = this.sandbox_store.memory(memory_idx)?; - - match MemoryInstance::transfer( - &this.memory, - val_ptr as usize, - &sandboxed_memory, - offset as usize, - val_len as usize, - ) { - Ok(()) => Ok(sandbox_primitives::ERR_OK), - Err(_) => Ok(sandbox_primitives::ERR_OUT_OF_BOUNDS), - } - }, - ext_sandbox_memory_teardown(memory_idx: u32) => { - this.sandbox_store.memory_teardown(memory_idx)?; - Ok(()) - }, - => <'e, E: Externalities + 'e> -); + } +} /// Wasm rust executor for contracts. /// @@ -1327,7 +1338,6 @@ impl_function_executor!(this: FunctionExecutor<'e, E>, pub struct WasmExecutor; impl WasmExecutor { - /// Create a new instance. pub fn new() -> Self { WasmExecutor @@ -1346,8 +1356,9 @@ impl WasmExecutor { method: &str, data: &[u8], ) -> Result> { - let module = ::wasmi::Module::from_buffer(code)?; - let module = Self::instantiate_module::(heap_pages, &module)?; + let module = wasmi::Module::from_buffer(code)?; + let module = Self::instantiate_module(heap_pages, &module)?; + self.call_in_wasm_module(ext, &module, method, data) } @@ -1369,7 +1380,8 @@ impl WasmExecutor { filter_result: FR, ) -> Result { let module = wasmi::Module::from_buffer(code)?; - let module = Self::instantiate_module::(heap_pages, &module)?; + let module = Self::instantiate_module(heap_pages, &module)?; + self.call_in_wasm_module_with_custom_signature( ext, &module, @@ -1398,10 +1410,10 @@ impl WasmExecutor { .ok_or_else(|| Error::HeapBaseNotFoundOrInvalid)? .get(); - Ok(match heap_base_val { - wasmi::RuntimeValue::I32(v) => v as u32, - _ => return Err(Error::HeapBaseNotFoundOrInvalid), - }) + match heap_base_val { + wasmi::RuntimeValue::I32(v) => Ok(v as u32), + _ => Err(Error::HeapBaseNotFoundOrInvalid), + } } /// Call a given method in the given wasm-module runtime. @@ -1454,34 +1466,40 @@ impl WasmExecutor { .and_then(|e| e.as_table().cloned()); let heap_base = Self::get_heap_base(module_instance)?; - let mut fec = FunctionExecutor::new(memory.clone(), heap_base, table, ext)?; + let mut fec = FunctionExecutor::new( + memory.clone(), + heap_base, + table, + )?; + let parameters = create_parameters(&mut |data: &[u8]| { - let offset = fec.heap.allocate(data.len() as u32)?; - memory.set(offset, &data)?; - Ok(offset) + let offset = fec.allocate_memory(data.len() as u32)?; + fec.write_memory(offset, data).map(|_| offset.into()).map_err(Into::into) })?; - let result = module_instance.invoke_export( - method, - ¶meters, - &mut fec + let result = runtime_io::with_externalities( + ext, + || module_instance.invoke_export(method, ¶meters, &mut fec), ); - let result = match result { + + match result { Ok(val) => match filter_result(val, &memory)? { Some(val) => Ok(val), None => Err(Error::InvalidReturn), }, Err(e) => { - trace!(target: "wasm-executor", "Failed to execute code with {} pages", memory.current_size().0); + trace!( + target: "wasm-executor", + "Failed to execute code with {} pages", + memory.current_size().0 + ); Err(e.into()) }, - }; - - result + } } /// Prepare module instance - pub fn instantiate_module>( + pub fn instantiate_module( heap_pages: usize, module: &Module, ) -> Result { @@ -1489,7 +1507,7 @@ impl WasmExecutor { let intermediate_instance = ModuleInstance::new( module, &ImportsBuilder::new() - .with_resolver("env", FunctionExecutor::::resolver()) + .with_resolver("env", FunctionExecutor::resolver()) )?; // Verify that the module has the heap base global variable. @@ -1622,11 +1640,11 @@ mod tests { let test_code = WASM_BINARY; assert_eq!( WasmExecutor::new().call(&mut ext, 8, &test_code[..], "test_twox_256", &[]).unwrap(), - hex!("99e9d85137db46ef4bbea33613baafd56f963c64b1f3685a4eb4abd67ff6203a") + hex!("99e9d85137db46ef4bbea33613baafd56f963c64b1f3685a4eb4abd67ff6203a"), ); assert_eq!( WasmExecutor::new().call(&mut ext, 8, &test_code[..], "test_twox_256", b"Hello world!").unwrap(), - hex!("b27dfd7f223f177f2a13647b533599af0c07f68bda23d96d059da2b451a35a74") + hex!("b27dfd7f223f177f2a13647b533599af0c07f68bda23d96d059da2b451a35a74"), ); } diff --git a/substrate/core/executor/src/wasm_runtimes_cache.rs b/substrate/core/executor/src/wasm_runtimes_cache.rs index 57845f8126..fd0ab91503 100644 --- a/substrate/core/executor/src/wasm_runtimes_cache.rs +++ b/substrate/core/executor/src/wasm_runtimes_cache.rs @@ -282,7 +282,11 @@ impl RuntimesCache { }, Entry::Vacant(v) => { trace!(target: "runtimes_cache", "no instance found in cache, creating now."); - let result = Self::create_wasm_instance(wasm_executor, ext, heap_pages); + let result = Self::create_wasm_instance( + wasm_executor, + ext, + heap_pages, + ); if let Err(ref err) = result { warn!(target: "runtimes_cache", "cannot create a runtime: {:?}", err); } @@ -305,10 +309,10 @@ impl RuntimesCache { // // A return of this error actually indicates that there is a problem in logic, since // we just loaded and validated the `module` above. - let data_segments = extract_data_segments(&code).ok_or(CacheError::CantDeserializeWasm)?; + let data_segments = extract_data_segments(&code)?; // Instantiate this module. - let instance = WasmExecutor::instantiate_module::(heap_pages as usize, &module) + let instance = WasmExecutor::instantiate_module(heap_pages as usize, &module) .map_err(CacheError::Instantiation)?; // Take state snapshot before executing anything. @@ -335,12 +339,14 @@ impl RuntimesCache { /// Extract the data segments from the given wasm code. /// /// Returns `Err` if the given wasm code cannot be deserialized. -fn extract_data_segments(wasm_code: &[u8]) -> Option> { - let raw_module: RawModule = deserialize_buffer(wasm_code).ok()?; +fn extract_data_segments(wasm_code: &[u8]) -> Result, CacheError> { + let raw_module: RawModule = deserialize_buffer(wasm_code) + .map_err(|_| CacheError::CantDeserializeWasm)?; + let segments = raw_module .data_section() .map(|ds| ds.entries()) .unwrap_or(&[]) .to_vec(); - Some(segments) + Ok(segments) } diff --git a/substrate/core/executor/src/wasm_utils.rs b/substrate/core/executor/src/wasm_utils.rs index 8f38499321..b217350ac6 100644 --- a/substrate/core/executor/src/wasm_utils.rs +++ b/substrate/core/executor/src/wasm_utils.rs @@ -16,129 +16,94 @@ //! Utilities for defining the wasm host environment. -use wasmi::{ValueType, RuntimeValue}; -use wasmi::nan_preserving_float::{F32, F64}; - -pub trait ConvertibleToWasm { - const VALUE_TYPE: ValueType; - type NativeType; fn to_runtime_value(self) -> RuntimeValue; -} - -impl ConvertibleToWasm for i32 { - type NativeType = i32; - const VALUE_TYPE: ValueType = ValueType::I32; - fn to_runtime_value(self) -> RuntimeValue { RuntimeValue::I32(self) } -} - -impl ConvertibleToWasm for u32 { - type NativeType = u32; - const VALUE_TYPE: ValueType = ValueType::I32; - fn to_runtime_value(self) -> RuntimeValue { RuntimeValue::I32(self as i32) } -} - -impl ConvertibleToWasm for i64 { - type NativeType = i64; - const VALUE_TYPE: ValueType = ValueType::I64; - fn to_runtime_value(self) -> RuntimeValue { RuntimeValue::I64(self) } -} - -impl ConvertibleToWasm for u64 { - type NativeType = u64; - const VALUE_TYPE: ValueType = ValueType::I64; - fn to_runtime_value(self) -> RuntimeValue { RuntimeValue::I64(self as i64) } -} - -impl ConvertibleToWasm for F32 { - type NativeType = F32; - const VALUE_TYPE: ValueType = ValueType::F32; - fn to_runtime_value(self) -> RuntimeValue { RuntimeValue::F32(self) } -} - -impl ConvertibleToWasm for F64 { - type NativeType = F64; - const VALUE_TYPE: ValueType = ValueType::F64; - fn to_runtime_value(self) -> RuntimeValue { RuntimeValue::F64(self) } -} - -impl ConvertibleToWasm for isize { - type NativeType = i32; - const VALUE_TYPE: ValueType = ValueType::I32; - fn to_runtime_value(self) -> RuntimeValue { RuntimeValue::I32(self as i32) } -} - -impl ConvertibleToWasm for usize { - type NativeType = u32; - const VALUE_TYPE: ValueType = ValueType::I32; - fn to_runtime_value(self) -> RuntimeValue { RuntimeValue::I32(self as u32 as i32) } -} - -impl ConvertibleToWasm for *const T { - type NativeType = u32; - const VALUE_TYPE: ValueType = ValueType::I32; - fn to_runtime_value(self) -> RuntimeValue { RuntimeValue::I32(self as isize as i32) } -} - -impl ConvertibleToWasm for *mut T { - type NativeType = u32; - const VALUE_TYPE: ValueType = ValueType::I32; - fn to_runtime_value(self) -> RuntimeValue { RuntimeValue::I32(self as isize as i32) } -} - /// Converts arguments into respective WASM types. #[macro_export] macro_rules! convert_args { () => ([]); - ( $( $t:ty ),* ) => ( [ $( { use $crate::wasm_utils::ConvertibleToWasm; <$t>::VALUE_TYPE }, )* ] ); + ( $( $t:ty ),* ) => ( [ $( <$t as $crate::wasm_interface::IntoValue>::VALUE_TYPE, )* ] ); } /// Generates a WASM signature for given list of parameters. #[macro_export] macro_rules! gen_signature { ( ( $( $params: ty ),* ) ) => ( - { - $crate::wasmi::Signature::new(&convert_args!($($params),*)[..], None) + $crate::wasm_interface::Signature { + args: std::borrow::Cow::Borrowed(&convert_args!( $( $params ),* )[..]), + return_value: None, } ); - - ( ( $( $params: ty ),* ) -> $returns: ty ) => ( - { - $crate::wasmi::Signature::new(&convert_args!($($params),*)[..], Some({ - use $crate::wasm_utils::ConvertibleToWasm; <$returns>::VALUE_TYPE - })) + ( ( $( $params: ty ),* ) -> $returns:ty ) => ( + $crate::wasm_interface::Signature { + args: std::borrow::Cow::Borrowed(&convert_args!( $( $params ),* )[..]), + return_value: Some(<$returns as $crate::wasm_interface::IntoValue>::VALUE_TYPE), } ); } -macro_rules! resolve_fn { - (@iter $index:expr, $sig_var:ident, $name_var:ident) => (); - (@iter $index:expr, $sig_var:ident, $name_var:ident $name:ident ( $( $params:ty ),* ) $( -> $returns:ty )* => $($tail:tt)* ) => ( - if $name_var == stringify!($name) { - let signature = gen_signature!( ( $( $params ),* ) $( -> $returns )* ); - if $sig_var != &signature { - return Err($crate::wasmi::Error::Instantiation( - format!("Export {} has different signature {:?}", $name_var, $sig_var), - )); +macro_rules! gen_functions { + (@INTERNAL + { $( $generated:tt )* } + $context:ident, + ) => ( + &[ $( $generated )* ] + ); + (@INTERNAL + { $( $generated:tt )* } + $context:ident, + $name:ident ( $( $names:ident: $params:ty ),* ) $( -> $returns:ty )? { $( $body:tt )* } + $( $tail:tt )* + ) => ( + gen_functions! { + @INTERNAL + { + $( $generated )* + { + struct $name; + + #[allow(unused)] + impl $crate::wasm_interface::Function for $name { + fn name(&self) -> &str { + stringify!($name) + } + fn signature(&self) -> $crate::wasm_interface::Signature { + gen_signature!( ( $( $params ),* ) $( -> $returns )? ) + } + fn execute( + &self, + context: &mut dyn $crate::wasm_interface::FunctionContext, + args: &mut dyn Iterator, + ) -> ::std::result::Result, String> { + let mut $context = context; + marshall! { + args, + ( $( $names : $params ),* ) $( -> $returns )? => { $( $body )* } + } + } + } + + &$name as &dyn $crate::wasm_interface::Function + }, } - return Ok($crate::wasmi::FuncInstance::alloc_host(signature, $index)); + $context, + $( $tail )* } - resolve_fn!(@iter $index + 1, $sig_var, $name_var $($tail)*) ); - ($sig_var:ident, $name_var:ident, $($tail:tt)* ) => ( - resolve_fn!(@iter 0, $sig_var, $name_var $($tail)*); + ( $context:ident, $( $tail:tt )* ) => ( + gen_functions!(@INTERNAL {} $context, $($tail)*); ); } /// Converts the list of arguments coming from WASM into their native types. #[macro_export] macro_rules! unmarshall_args { - ( $body:tt, $objectname:ident, $args_iter:ident, $( $names:ident : $params:ty ),*) => ({ + ( $body:tt, $args_iter:ident, $( $names:ident : $params:ty ),*) => ({ $( - let $names : <$params as $crate::wasm_utils::ConvertibleToWasm>::NativeType = + let $names : $params = $args_iter.next() - .and_then(|rt_val| rt_val.try_into()) + .and_then(|val| <$params as $crate::wasm_interface::TryFromValue>::try_from_value(val)) .expect( - "`$args_iter` comes from an argument of Externals::invoke_index; + "`$args_iter` comes from an argument of Externals::execute_function; args to an external call always matches the signature of the external; external signatures are built with count and types and in order defined by `$params`; here, we iterating on `$params`; @@ -160,7 +125,7 @@ macro_rules! unmarshall_args { #[inline(always)] pub fn constrain_closure(f: F) -> F where - F: FnOnce() -> Result + F: FnOnce() -> Result { f } @@ -168,103 +133,40 @@ where /// Pass the list of parameters by converting them to respective WASM types. #[macro_export] macro_rules! marshall { - ( $args_iter:ident, $objectname:ident, ( $( $names:ident : $params:ty ),* ) -> $returns:ty => $body:tt ) => ({ - let body = $crate::wasm_utils::constrain_closure::< - <$returns as $crate::wasm_utils::ConvertibleToWasm>::NativeType, _ - >(|| { - unmarshall_args!($body, $objectname, $args_iter, $( $names : $params ),*) + ( $args_iter:ident, ( $( $names:ident : $params:ty ),* ) -> $returns:ty => $body:tt ) => ({ + let body = $crate::wasm_utils::constrain_closure::<$returns, _>(|| { + unmarshall_args!($body, $args_iter, $( $names : $params ),*) }); - let r = body().map_err(wasmi::Trap::from)?; - return Ok(Some({ use $crate::wasm_utils::ConvertibleToWasm; r.to_runtime_value() })) + let r = body()?; + return Ok(Some($crate::wasm_interface::IntoValue::into_value(r))) }); - ( $args_iter:ident, $objectname:ident, ( $( $names:ident : $params:ty ),* ) => $body:tt ) => ({ + ( $args_iter:ident, ( $( $names:ident : $params:ty ),* ) => $body:tt ) => ({ let body = $crate::wasm_utils::constrain_closure::<(), _>(|| { - unmarshall_args!($body, $objectname, $args_iter, $( $names : $params ),*) + unmarshall_args!($body, $args_iter, $( $names : $params ),*) }); - body().map_err(wasmi::Trap::from)?; + body()?; return Ok(None) }) } -macro_rules! dispatch_fn { - ( @iter $index:expr, $index_ident:ident, $objectname:ident, $args_iter:ident) => { - // `$index` comes from an argument of Externals::invoke_index; - // externals are always invoked with index given by resolve_fn! at resolve time; - // For each next function resolve_fn! gives new index, starting from 0; - // Both dispatch_fn! and resolve_fn! are called with the same list of functions; - // qed; - panic!("fn with index {} is undefined", $index); - }; - - (@iter - $index:expr, - $index_ident:ident, - $objectname:ident, - $args_iter:ident, - $name:ident ( $( $names:ident : $params:ty ),* ) $( -> $returns:ty )* => $body:tt $($tail:tt)* - ) => ( - if $index_ident == $index { - { marshall!($args_iter, $objectname, ( $( $names : $params ),* ) $( -> $returns )* => $body) } - } - dispatch_fn!( @iter $index + 1, $index_ident, $objectname, $args_iter $($tail)*) - ); - - ( $index_ident:ident, $objectname:ident, $args_iter:ident, $($tail:tt)* ) => ( - dispatch_fn!( @iter 0, $index_ident, $objectname, $args_iter, $($tail)*); - ); -} - -/// Implements `wasmi::Externals` trait and `Resolver` for given struct. +/// Implements the wasm host interface for the given type. #[macro_export] -macro_rules! impl_function_executor { +macro_rules! impl_wasm_host_interface { ( - $objectname:ident : $structname:ty, - $( - $name:ident - ( $( $names:ident : $params:ty ),* $(,)? ) - $( -> $returns:ty )? => { $( $body:tt )* }, - )* - => $( $pre:tt )+ - ) => ( - impl $( $pre ) + $structname { - #[allow(unused)] - fn resolver() -> &'static dyn $crate::wasmi::ModuleImportResolver { - struct Resolver; - impl $crate::wasmi::ModuleImportResolver for Resolver { - fn resolve_func( - &self, - name: &str, - signature: &$crate::wasmi::Signature - ) -> std::result::Result<$crate::wasmi::FuncRef, $crate::wasmi::Error> { - resolve_fn!( - signature, - name, - $( $name( $( $params ),* ) $( -> $returns )? => )* - ); - - Err($crate::wasmi::Error::Instantiation( - format!("Export {} not found", name), - )) - } - } - &Resolver - } + impl $interface_name:ident where $context:ident { + $( + $name:ident($( $names:ident : $params:ty ),* $(,)? ) $( -> $returns:ty )? + { $( $body:tt )* } + )* } - - impl $( $pre ) + $crate::wasmi::Externals for $structname { - fn invoke_index( - &mut self, - index: usize, - args: $crate::wasmi::RuntimeArgs, - ) -> std::result::Result, $crate::wasmi::Trap> { - let $objectname = self; - let mut args = args.as_ref().iter(); - dispatch_fn! { - index, - $objectname, - args, - $( $name( $( $names : $params ),* ) $( -> $returns )? => { $( $body )* } ),* - }; + ) => ( + impl $crate::wasm_interface::HostFunctions for $interface_name { + #[allow(non_camel_case_types)] + fn functions() -> &'static [&'static dyn $crate::wasm_interface::Function] { + gen_functions!( + $context, + $( $name( $( $names: $params ),* ) $( -> $returns )? { $( $body )* } )* + ) } } ); diff --git a/substrate/core/sr-io/src/lib.rs b/substrate/core/sr-io/src/lib.rs index de5c48f5fe..642b3e4881 100644 --- a/substrate/core/sr-io/src/lib.rs +++ b/substrate/core/sr-io/src/lib.rs @@ -279,7 +279,7 @@ export_api! { /// Submit transaction to the pool. /// /// The transaction will end up in the pool. - fn submit_transaction(data: &T) -> Result<(), ()>; + fn submit_transaction(data: Vec) -> Result<(), ()>; /// Returns information about the local node's network state. fn network_state() -> Result; diff --git a/substrate/core/sr-io/with_std.rs b/substrate/core/sr-io/with_std.rs index acbeb8ce0e..cee17a5bdd 100644 --- a/substrate/core/sr-io/with_std.rs +++ b/substrate/core/sr-io/with_std.rs @@ -334,9 +334,9 @@ impl OffchainApi for () { }, "is_validator can be called only in the offchain worker context") } - fn submit_transaction(data: &T) -> Result<(), ()> { + fn submit_transaction(data: Vec) -> Result<(), ()> { with_offchain(|ext| { - ext.submit_transaction(codec::Encode::encode(data)) + ext.submit_transaction(data) }, "submit_transaction can be called only in the offchain worker context") } diff --git a/substrate/core/sr-io/without_std.rs b/substrate/core/sr-io/without_std.rs index 58aff2444e..d6ad3e1434 100644 --- a/substrate/core/sr-io/without_std.rs +++ b/substrate/core/sr-io/without_std.rs @@ -47,7 +47,7 @@ pub fn panic(info: &PanicInfo) -> ! { #[cfg(not(feature = "no_oom"))] #[alloc_error_handler] -pub extern fn oom(_: ::core::alloc::Layout) -> ! { +pub extern fn oom(_: core::alloc::Layout) -> ! { static OOM_MSG: &str = "Runtime memory exhausted. Aborting"; unsafe { @@ -980,10 +980,9 @@ impl OffchainApi for () { unsafe { ext_is_validator.get()() == 1 } } - fn submit_transaction(data: &T) -> Result<(), ()> { - let encoded_data = codec::Encode::encode(data); + fn submit_transaction(data: Vec) -> Result<(), ()> { let ret = unsafe { - ext_submit_transaction.get()(encoded_data.as_ptr(), encoded_data.len() as u32) + ext_submit_transaction.get()(data.as_ptr(), data.len() as u32) }; if ret == 0 { diff --git a/substrate/core/sr-std/without_std.rs b/substrate/core/sr-std/without_std.rs index 327e271049..845ea79aa4 100755 --- a/substrate/core/sr-std/without_std.rs +++ b/substrate/core/sr-std/without_std.rs @@ -19,7 +19,7 @@ pub extern crate alloc; extern "C" { - fn ext_malloc(size: usize) -> *mut u8; + fn ext_malloc(size: u32) -> *mut u8; fn ext_free(ptr: *mut u8); } @@ -37,7 +37,7 @@ mod __impl { unsafe impl GlobalAlloc for WasmAllocator { unsafe fn alloc(&self, layout: Layout) -> *mut u8 { - super::ext_malloc(layout.size()) as *mut u8 + super::ext_malloc(layout.size() as u32) as *mut u8 } unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) { diff --git a/substrate/core/test-runtime/src/lib.rs b/substrate/core/test-runtime/src/lib.rs index ec7ed9670c..aa0b57ec92 100644 --- a/substrate/core/test-runtime/src/lib.rs +++ b/substrate/core/test-runtime/src/lib.rs @@ -629,7 +629,7 @@ cfg_if! { impl offchain_primitives::OffchainWorkerApi for Runtime { fn offchain_worker(block: u64) { let ex = Extrinsic::IncludeData(block.encode()); - runtime_io::submit_transaction(&ex).unwrap(); + runtime_io::submit_transaction(ex.encode()).unwrap(); } } @@ -844,7 +844,7 @@ cfg_if! { impl offchain_primitives::OffchainWorkerApi for Runtime { fn offchain_worker(block: u64) { let ex = Extrinsic::IncludeData(block.encode()); - runtime_io::submit_transaction(&ex).unwrap() + runtime_io::submit_transaction(ex.encode()).unwrap() } } diff --git a/substrate/core/wasm-interface/Cargo.toml b/substrate/core/wasm-interface/Cargo.toml new file mode 100644 index 0000000000..c388b32930 --- /dev/null +++ b/substrate/core/wasm-interface/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "substrate-wasm-interface" +version = "2.0.0" +authors = ["Parity Technologies "] +edition = "2018" + +[dependencies] +wasmi = "0.5.0" diff --git a/substrate/core/wasm-interface/src/lib.rs b/substrate/core/wasm-interface/src/lib.rs new file mode 100644 index 0000000000..b3cbde556e --- /dev/null +++ b/substrate/core/wasm-interface/src/lib.rs @@ -0,0 +1,324 @@ +// Copyright 2019 Parity Technologies (UK) Ltd. +// This file is part of Substrate. + +// Substrate is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Substrate is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Substrate. If not, see . + +//! Types and traits for interfacing between the host and the wasm runtime. + +use std::{borrow::Cow, marker::PhantomData, mem, iter::Iterator, result}; + +mod wasmi_impl; + +/// Result type used by traits in this crate. +pub type Result = result::Result; + +/// Value types supported by Substrate on the boundary between host/Wasm. +#[derive(Copy, Clone, PartialEq, Debug, Eq)] +pub enum ValueType { + /// An `i32` value type. + I32, + /// An `i64` value type. + I64, + /// An `f32` value type. + F32, + /// An `f64` value type. + F64, +} + +/// Values supported by Substrate on the boundary between host/Wasm. +#[derive(PartialEq, Debug, Clone, Copy)] +pub enum Value { + /// An `i32` value. + I32(i32), + /// An `i64` value. + I64(i64), + /// An nan-preserving `f32` value. + F32(u32), + /// An nan-preserving `f64` value. + F64(u64), +} + +/// Provides `Sealed` trait to prevent implementing trait `PointerType` outside of this crate. +mod private { + pub trait Sealed {} + + impl Sealed for u8 {} + impl Sealed for u16 {} + impl Sealed for u32 {} + impl Sealed for u64 {} +} + +/// Something that can be wrapped in a wasm `Pointer`. +/// +/// This trait is sealed. +pub trait PointerType: Sized { + /// The size of the type in wasm. + const SIZE: u32 = mem::size_of::() as u32; +} + +impl PointerType for u8 {} +impl PointerType for u16 {} +impl PointerType for u32 {} +impl PointerType for u64 {} + +/// Type to represent a pointer in wasm at the host. +#[derive(Debug, PartialEq, Eq, Clone, Copy)] +pub struct Pointer { + ptr: u32, + _marker: PhantomData, +} + +impl Pointer { + /// Create a new instance of `Self`. + pub fn new(ptr: u32) -> Self { + Self { + ptr, + _marker: Default::default(), + } + } + + /// Calculate the offset from this pointer. + /// + /// `offset` is in units of `T`. So, `3` means `3 * mem::size_of::()` as offset to the pointer. + /// + /// Returns an `Option` to respect that the pointer could probably overflow. + pub fn offset(self, offset: u32) -> Option { + offset.checked_mul(T::SIZE).and_then(|o| self.ptr.checked_add(o)).map(|ptr| { + Self { + ptr, + _marker: Default::default(), + } + }) + } + + /// Create a null pointer. + pub fn null() -> Self { + Self::new(0) + } + + /// Cast this pointer of type `T` to a pointer of type `R`. + pub fn cast(self) -> Pointer { + Pointer::new(self.ptr) + } +} + +impl From> for u32 { + fn from(ptr: Pointer) -> Self { + ptr.ptr + } +} + +impl From> for usize { + fn from(ptr: Pointer) -> Self { + ptr.ptr as _ + } +} + +impl IntoValue for Pointer { + const VALUE_TYPE: ValueType = ValueType::I32; + fn into_value(self) -> Value { Value::I32(self.ptr as _) } +} + +impl TryFromValue for Pointer { + fn try_from_value(val: Value) -> Option { + match val { + Value::I32(val) => Some(Self::new(val as _)), + _ => None, + } + } +} + +/// The word size used in wasm. Normally known as `usize` in Rust. +pub type WordSize = u32; + +/// The Signature of a function +#[derive(Eq, PartialEq, Debug, Clone)] +pub struct Signature { + /// The arguments of a function. + pub args: Cow<'static, [ValueType]>, + /// The optional return value of a function. + pub return_value: Option, +} + +impl Signature { + /// Create a new instance of `Signature`. + pub fn new>>(args: T, return_value: Option) -> Self { + Self { + args: args.into(), + return_value, + } + } + + /// Create a new instance of `Signature` with the given `args` and without any return value. + pub fn new_with_args>>(args: T) -> Self { + Self { + args: args.into(), + return_value: None, + } + } + +} + +/// Something that provides a function implementation on the host for a wasm function. +pub trait Function { + /// Returns the name of this function. + fn name(&self) -> &str; + /// Returns the signature of this function. + fn signature(&self) -> Signature; + /// Execute this function with the given arguments. + fn execute( + &self, + context: &mut dyn FunctionContext, + args: &mut dyn Iterator, + ) -> Result>; +} + +/// Context used by `Function` to interact with the allocator and the memory of the wasm instance. +pub trait FunctionContext { + /// Read memory from `address` into a vector. + fn read_memory(&self, address: Pointer, size: WordSize) -> Result> { + let mut vec = Vec::with_capacity(size as usize); + vec.resize(size as usize, 0); + self.read_memory_into(address, &mut vec)?; + Ok(vec) + } + /// Read memory into the given `dest` buffer from `address`. + fn read_memory_into(&self, address: Pointer, dest: &mut [u8]) -> Result<()>; + /// Write the given data at `address` into the memory. + fn write_memory(&mut self, address: Pointer, data: &[u8]) -> Result<()>; + /// Allocate a memory instance of `size` bytes. + fn allocate_memory(&mut self, size: WordSize) -> Result>; + /// Deallocate a given memory instance. + fn deallocate_memory(&mut self, ptr: Pointer) -> Result<()>; + /// Provides access to the sandbox. + fn sandbox(&mut self) -> &mut dyn Sandbox; +} + +/// Sandbox memory identifier. +pub type MemoryId = u32; + +/// Something that provides access to the sandbox. +pub trait Sandbox { + /// Get sandbox memory from the `memory_id` instance at `offset` into the given buffer. + fn memory_get( + &self, + memory_id: MemoryId, + offset: WordSize, + buf_ptr: Pointer, + buf_len: WordSize, + ) -> Result; + /// Set sandbox memory from the given value. + fn memory_set( + &mut self, + memory_id: MemoryId, + offset: WordSize, + val_ptr: Pointer, + val_len: WordSize, + ) -> Result; + /// Delete a memory instance. + fn memory_teardown(&mut self, memory_id: MemoryId) -> Result<()>; + /// Create a new memory instance with the given `initial` size and the `maximum` size. + /// The size is given in wasm pages. + fn memory_new(&mut self, initial: u32, maximum: u32) -> Result; + /// Invoke an exported function by a name. + fn invoke( + &mut self, + instance_id: u32, + export_name: &str, + args: &[u8], + return_val: Pointer, + return_val_len: WordSize, + state: u32, + ) -> Result; + /// Delete a sandbox instance. + fn instance_teardown(&mut self, instance_id: u32) -> Result<()>; + /// Create a new sandbox instance. + fn instance_new( + &mut self, + dispatch_thunk_id: u32, + wasm: &[u8], + raw_env_def: &[u8], + state: u32, + ) -> Result; +} + +/// Something that provides implementations for host functions. +pub trait HostFunctions { + /// Returns all host functions. + fn functions() -> &'static [&'static dyn Function]; +} + +/// Something that can be converted into a wasm compatible `Value`. +pub trait IntoValue { + /// The type of the value in wasm. + const VALUE_TYPE: ValueType; + + /// Convert `self` into a wasm `Value`. + fn into_value(self) -> Value; +} + +/// Something that can may be created from a wasm `Value`. +pub trait TryFromValue: Sized { + /// Try to convert the given `Value` into `Self`. + fn try_from_value(val: Value) -> Option; +} + +macro_rules! impl_into_and_from_value { + ( + $( + $type:ty, $( < $gen:ident >, )? $value_variant:ident, + )* + ) => { + $( + impl $( <$gen> )? IntoValue for $type { + const VALUE_TYPE: ValueType = ValueType::$value_variant; + fn into_value(self) -> Value { Value::$value_variant(self as _) } + } + + impl $( <$gen> )? TryFromValue for $type { + fn try_from_value(val: Value) -> Option { + match val { + Value::$value_variant(val) => Some(val as _), + _ => None, + } + } + } + )* + } +} + +impl_into_and_from_value! { + u32, I32, + i32, I32, + u64, I64, + i64, I64, +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn pointer_offset_works() { + let ptr = Pointer::::null(); + + assert_eq!(ptr.offset(10).unwrap(), Pointer::new(40)); + assert_eq!(ptr.offset(32).unwrap(), Pointer::new(128)); + + let ptr = Pointer::::null(); + + assert_eq!(ptr.offset(10).unwrap(), Pointer::new(80)); + assert_eq!(ptr.offset(32).unwrap(), Pointer::new(256)); + } +} diff --git a/substrate/core/wasm-interface/src/wasmi_impl.rs b/substrate/core/wasm-interface/src/wasmi_impl.rs new file mode 100644 index 0000000000..be9b724d29 --- /dev/null +++ b/substrate/core/wasm-interface/src/wasmi_impl.rs @@ -0,0 +1,79 @@ +// Copyright 2019 Parity Technologies (UK) Ltd. +// This file is part of Substrate. + +// Substrate is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Substrate is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Substrate. If not, see . + +//! Implementation of conversions between Substrate and wasmi types. + +use crate::{Value, ValueType, Signature}; + +impl From for wasmi::RuntimeValue { + fn from(value: Value) -> Self { + match value { + Value::I32(val) => Self::I32(val), + Value::I64(val) => Self::I64(val), + Value::F32(val) => Self::F32(val.into()), + Value::F64(val) => Self::F64(val.into()), + } + } +} + +impl From for Value { + fn from(value: wasmi::RuntimeValue) -> Self { + match value { + wasmi::RuntimeValue::I32(val) => Self::I32(val), + wasmi::RuntimeValue::I64(val) => Self::I64(val), + wasmi::RuntimeValue::F32(val) => Self::F32(val.into()), + wasmi::RuntimeValue::F64(val) => Self::F64(val.into()), + } + } +} + +impl From for wasmi::ValueType { + fn from(value: ValueType) -> Self { + match value { + ValueType::I32 => Self::I32, + ValueType::I64 => Self::I64, + ValueType::F32 => Self::F32, + ValueType::F64 => Self::F64, + } + } +} + +impl From for ValueType { + fn from(value: wasmi::ValueType) -> Self { + match value { + wasmi::ValueType::I32 => Self::I32, + wasmi::ValueType::I64 => Self::I64, + wasmi::ValueType::F32 => Self::F32, + wasmi::ValueType::F64 => Self::F64, + } + } +} + +impl From for wasmi::Signature { + fn from(sig: Signature) -> Self { + let args = sig.args.iter().map(|a| (*a).into()).collect::>(); + wasmi::Signature::new(args, sig.return_value.map(Into::into)) + } +} + +impl From<&wasmi::Signature> for Signature { + fn from(sig: &wasmi::Signature) -> Self { + Signature::new( + sig.params().into_iter().copied().map(Into::into).collect::>(), + sig.return_type().map(Into::into), + ) + } +} diff --git a/substrate/node-template/src/service.rs b/substrate/node-template/src/service.rs index e3c3c670a0..310b8f44a7 100644 --- a/substrate/node-template/src/service.rs +++ b/substrate/node-template/src/service.rs @@ -18,7 +18,7 @@ pub use substrate_executor::NativeExecutor; native_executor_instance!( pub Executor, node_template_runtime::api::dispatch, - node_template_runtime::native_version + node_template_runtime::native_version, ); construct_simple_protocol! { diff --git a/substrate/srml/system/src/offchain.rs b/substrate/srml/system/src/offchain.rs index d67598f64f..e234c74c08 100644 --- a/substrate/srml/system/src/offchain.rs +++ b/substrate/srml/system/src/offchain.rs @@ -82,7 +82,7 @@ pub trait SubmitSignedTransaction { ::create_transaction::(call, id, expected) .ok_or(())?; let xt = Self::Extrinsic::new(call, Some(signature_data)).ok_or(())?; - runtime_io::submit_transaction(&xt) + runtime_io::submit_transaction(xt.encode()) } } @@ -97,7 +97,7 @@ pub trait SubmitUnsignedTransaction { /// and `Err` if transaction was rejected from the pool. fn submit_unsigned(call: impl Into) -> Result<(), ()> { let xt = Self::Extrinsic::new(call.into(), None).ok_or(())?; - runtime_io::submit_transaction(&xt) + runtime_io::submit_transaction(xt.encode()) } }