mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-05-01 06:37:56 +00:00
b691cfe093
* Adds first version of traits for generating the host functions * First steps of the procedural macro * Implements generation of the host extern functions * Prefix ext host function with snake case trait name * Implement host functions implementation on the host * Change `HostFunctions` interface * Implement `HostFunctions` for tuples * Make `WasmExecutor` generic over the host functions * Begin to add a test and make it compile * Make the test succeed * Add test to ensure that host functions are not found * It's alive! Make the `set_storage` test work * Add test for mutable references * Code cleanup and documentation etc * Add marker trait for types that should be passed as SCALE encoded * Inherit the visibility from the trait and more improvements * More impls and move them into their own file * Code simplification by dropping one trait * Give it a better name * Implement traits for arrays * Refactor code to support pass by codec/inner * Docs * Implement pass by inner for some crypto types and add a test * Implement exchangeable function support * Rewrite sr-io with as runtime interface * Start reworking after master merge * Adds `PassByCodec` derive * Adds `PassByInner` derive * Fix compilation errors * More implementations * Implement runtime interface traits for `str` * Make `sr-io` compile again * Fix more compilation errors * More progress on getting stuff back to compile * More compilation fixes * Fix warnings * Remove le conversions * Add support for `wasm_only` interfaces * Implement `Allocator` interface * Improve error message * Move `WasmAllocator` to `sr-io` and more clean ups * Use correct function signature for wasm functions * Store the host functions with the Wasm runtime * Docs update * Fix compilation after master merge * Remove `sr-io/without_std` * Make `srml-support` tests run again * More compilation error fixes * Use correct doc syntax * Fix test-runtime * Fix compilation * Catch native panics when executing the wasm runtime As with the native runtime, we now catch all native panics when we execute the wasm runtime. The panics inside the wasm runtime were already catched before by the wasm executor automatically, but any panic in the host functions could bring down the node. The recent switch to execute the native counterpart of the host function in `sr-io`, makes this change required. The native `sr-io` functions just `panic` when something is not provided or any other error occured. * Fix compilation * Don't panic in a panic * Move `sr-sandbox` to new runtime interface * Fixes tests after sandbox changes * Make sure we detect invalid utf8 * Fixes after master merge * Adds pass by enum strategy * Fix wasmtime integration * Some macro structure clean up * Rework and test exchangebale host functions * PassBy derive macros documentation * Docs for `runtime_interface` macro * Support wild card argument names * Adds ui tests * Make sure that we are backwards compatible to the old runtime interfaces * Documentation * Fixes after latest master merge * Make `wasmtime` happy * Make `full_crypto` work * Make the new interface versionable * Rename `Sanboxing` to `Sandbox` * Don't finalize in test while importing * Fix Performance regression * Fix test
279 lines
7.8 KiB
Rust
Executable File
279 lines
7.8 KiB
Rust
Executable File
// Copyright 2018-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 <http://www.gnu.org/licenses/>.
|
|
|
|
use rstd::{prelude::*, slice, marker, mem, vec, rc::Rc};
|
|
use codec::{Decode, Encode};
|
|
use primitives::sandbox as sandbox_primitives;
|
|
use super::{Error, TypedValue, ReturnValue, HostFuncType};
|
|
use runtime_io::sandbox;
|
|
|
|
mod ffi {
|
|
use rstd::mem;
|
|
use super::HostFuncType;
|
|
|
|
/// Index into the default table that points to a `HostFuncType`.
|
|
pub type HostFuncIndex = usize;
|
|
|
|
/// Coerce `HostFuncIndex` to a callable host function pointer.
|
|
///
|
|
/// # Safety
|
|
///
|
|
/// This function should be only called with a `HostFuncIndex` that was previously registered
|
|
/// in the environment definition. Typically this should only
|
|
/// be called with an argument received in `dispatch_thunk`.
|
|
pub unsafe fn coerce_host_index_to_func<T>(idx: HostFuncIndex) -> HostFuncType<T> {
|
|
// We need to ensure that sizes of a callable function pointer and host function index is
|
|
// indeed equal.
|
|
// We can't use `static_assertions` create because it makes compiler panic, fallback to runtime assert.
|
|
// const_assert!(mem::size_of::<HostFuncIndex>() == mem::size_of::<HostFuncType<T>>(),);
|
|
assert!(mem::size_of::<HostFuncIndex>() == mem::size_of::<HostFuncType<T>>());
|
|
mem::transmute::<HostFuncIndex, HostFuncType<T>>(idx)
|
|
}
|
|
}
|
|
|
|
struct MemoryHandle {
|
|
memory_idx: u32,
|
|
}
|
|
|
|
impl Drop for MemoryHandle {
|
|
fn drop(&mut self) {
|
|
sandbox::memory_teardown(self.memory_idx);
|
|
}
|
|
}
|
|
|
|
#[derive(Clone)]
|
|
pub struct Memory {
|
|
// Handle to memory instance is wrapped to add reference-counting semantics
|
|
// to `Memory`.
|
|
handle: Rc<MemoryHandle>,
|
|
}
|
|
|
|
impl Memory {
|
|
pub fn new(initial: u32, maximum: Option<u32>) -> Result<Memory, Error> {
|
|
let maximum = if let Some(maximum) = maximum {
|
|
maximum
|
|
} else {
|
|
sandbox_primitives::MEM_UNLIMITED
|
|
};
|
|
|
|
match sandbox::memory_new(initial, maximum) {
|
|
sandbox_primitives::ERR_MODULE => Err(Error::Module),
|
|
memory_idx => Ok(Memory {
|
|
handle: Rc::new(MemoryHandle { memory_idx, }),
|
|
}),
|
|
}
|
|
}
|
|
|
|
pub fn get(&self, offset: u32, buf: &mut [u8]) -> Result<(), Error> {
|
|
let result = sandbox::memory_get(
|
|
self.handle.memory_idx,
|
|
offset,
|
|
buf.as_mut_ptr(),
|
|
buf.len() as u32,
|
|
);
|
|
match result {
|
|
sandbox_primitives::ERR_OK => Ok(()),
|
|
sandbox_primitives::ERR_OUT_OF_BOUNDS => Err(Error::OutOfBounds),
|
|
_ => unreachable!(),
|
|
}
|
|
}
|
|
|
|
pub fn set(&self, offset: u32, val: &[u8]) -> Result<(), Error> {
|
|
let result = sandbox::memory_set(
|
|
self.handle.memory_idx,
|
|
offset,
|
|
val.as_ptr() as _ ,
|
|
val.len() as u32,
|
|
);
|
|
match result {
|
|
sandbox_primitives::ERR_OK => Ok(()),
|
|
sandbox_primitives::ERR_OUT_OF_BOUNDS => Err(Error::OutOfBounds),
|
|
_ => unreachable!(),
|
|
}
|
|
}
|
|
}
|
|
|
|
pub struct EnvironmentDefinitionBuilder<T> {
|
|
env_def: sandbox_primitives::EnvironmentDefinition,
|
|
retained_memories: Vec<Memory>,
|
|
_marker: marker::PhantomData<T>,
|
|
}
|
|
|
|
impl<T> EnvironmentDefinitionBuilder<T> {
|
|
pub fn new() -> EnvironmentDefinitionBuilder<T> {
|
|
EnvironmentDefinitionBuilder {
|
|
env_def: sandbox_primitives::EnvironmentDefinition {
|
|
entries: Vec::new(),
|
|
},
|
|
retained_memories: Vec::new(),
|
|
_marker: marker::PhantomData::<T>,
|
|
}
|
|
}
|
|
|
|
fn add_entry<N1, N2>(
|
|
&mut self,
|
|
module: N1,
|
|
field: N2,
|
|
extern_entity: sandbox_primitives::ExternEntity,
|
|
) where
|
|
N1: Into<Vec<u8>>,
|
|
N2: Into<Vec<u8>>,
|
|
{
|
|
let entry = sandbox_primitives::Entry {
|
|
module_name: module.into(),
|
|
field_name: field.into(),
|
|
entity: extern_entity,
|
|
};
|
|
self.env_def.entries.push(entry);
|
|
}
|
|
|
|
pub fn add_host_func<N1, N2>(&mut self, module: N1, field: N2, f: HostFuncType<T>)
|
|
where
|
|
N1: Into<Vec<u8>>,
|
|
N2: Into<Vec<u8>>,
|
|
{
|
|
let f = sandbox_primitives::ExternEntity::Function(f as u32);
|
|
self.add_entry(module, field, f);
|
|
}
|
|
|
|
pub fn add_memory<N1, N2>(&mut self, module: N1, field: N2, mem: Memory)
|
|
where
|
|
N1: Into<Vec<u8>>,
|
|
N2: Into<Vec<u8>>,
|
|
{
|
|
// We need to retain memory to keep it alive while the EnvironmentDefinitionBuilder alive.
|
|
self.retained_memories.push(mem.clone());
|
|
|
|
let mem = sandbox_primitives::ExternEntity::Memory(mem.handle.memory_idx as u32);
|
|
self.add_entry(module, field, mem);
|
|
}
|
|
}
|
|
|
|
pub struct Instance<T> {
|
|
instance_idx: u32,
|
|
_retained_memories: Vec<Memory>,
|
|
_marker: marker::PhantomData<T>,
|
|
}
|
|
|
|
/// The primary responsibility of this thunk is to deserialize arguments and
|
|
/// call the original function, specified by the index.
|
|
extern "C" fn dispatch_thunk<T>(
|
|
serialized_args_ptr: *const u8,
|
|
serialized_args_len: usize,
|
|
state: usize,
|
|
f: ffi::HostFuncIndex,
|
|
) -> u64 {
|
|
let serialized_args = unsafe {
|
|
if serialized_args_len == 0 {
|
|
&[]
|
|
} else {
|
|
slice::from_raw_parts(serialized_args_ptr, serialized_args_len)
|
|
}
|
|
};
|
|
let args = Vec::<TypedValue>::decode(&mut &serialized_args[..]).expect(
|
|
"serialized args should be provided by the runtime;
|
|
correctly serialized data should be deserializable;
|
|
qed",
|
|
);
|
|
|
|
unsafe {
|
|
// This should be safe since `coerce_host_index_to_func` is called with an argument
|
|
// received in an `dispatch_thunk` implementation, so `f` should point
|
|
// on a valid host function.
|
|
let f = ffi::coerce_host_index_to_func(f);
|
|
|
|
// This should be safe since mutable reference to T is passed upon the invocation.
|
|
let state = &mut *(state as *mut T);
|
|
|
|
// Pass control flow to the designated function.
|
|
let result = f(state, &args).encode();
|
|
|
|
// Leak the result vector and return the pointer to return data.
|
|
let result_ptr = result.as_ptr() as u64;
|
|
let result_len = result.len() as u64;
|
|
mem::forget(result);
|
|
|
|
(result_ptr << 32) | result_len
|
|
}
|
|
}
|
|
|
|
impl<T> Instance<T> {
|
|
pub fn new(
|
|
code: &[u8],
|
|
env_def_builder: &EnvironmentDefinitionBuilder<T>,
|
|
state: &mut T,
|
|
) -> Result<Instance<T>, Error> {
|
|
let serialized_env_def: Vec<u8> = env_def_builder.env_def.encode();
|
|
// It's very important to instantiate thunk with the right type.
|
|
let dispatch_thunk = dispatch_thunk::<T>;
|
|
let result = sandbox::instantiate(
|
|
dispatch_thunk as u32,
|
|
code,
|
|
&serialized_env_def,
|
|
state as *const T as _,
|
|
);
|
|
|
|
let instance_idx = match result {
|
|
sandbox_primitives::ERR_MODULE => return Err(Error::Module),
|
|
sandbox_primitives::ERR_EXECUTION => return Err(Error::Execution),
|
|
instance_idx => instance_idx,
|
|
};
|
|
|
|
// We need to retain memories to keep them alive while the Instance is alive.
|
|
let retained_memories = env_def_builder.retained_memories.clone();
|
|
Ok(Instance {
|
|
instance_idx,
|
|
_retained_memories: retained_memories,
|
|
_marker: marker::PhantomData::<T>,
|
|
})
|
|
}
|
|
|
|
pub fn invoke(
|
|
&mut self,
|
|
name: &str,
|
|
args: &[TypedValue],
|
|
state: &mut T,
|
|
) -> Result<ReturnValue, Error> {
|
|
let serialized_args = args.to_vec().encode();
|
|
let mut return_val = vec![0u8; sandbox_primitives::ReturnValue::ENCODED_MAX_SIZE];
|
|
|
|
let result = sandbox::invoke(
|
|
self.instance_idx,
|
|
name,
|
|
&serialized_args,
|
|
return_val.as_mut_ptr() as _,
|
|
return_val.len() as u32,
|
|
state as *const T as _,
|
|
);
|
|
|
|
match result {
|
|
sandbox_primitives::ERR_OK => {
|
|
let return_val = sandbox_primitives::ReturnValue::decode(&mut &return_val[..])
|
|
.map_err(|_| Error::Execution)?;
|
|
Ok(return_val)
|
|
}
|
|
sandbox_primitives::ERR_EXECUTION => Err(Error::Execution),
|
|
_ => unreachable!(),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<T> Drop for Instance<T> {
|
|
fn drop(&mut self) {
|
|
sandbox::instance_teardown(self.instance_idx);
|
|
}
|
|
}
|