mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-26 13:27:57 +00:00
Update wasmtime to 0.29.0 (#9552)
* Start
* Move to ctx
* Make it compile for now
* More work
* Get rid off state-holder
* Use less Refcells
* 🤦
* Don't use RefCell
* Use names for parameters
* Fixes after merge
* Fixes after merge
* Review feedback
* FMT
This commit is contained in:
@@ -19,7 +19,7 @@
|
||||
//! This module defines `HostState` and `HostContext` structs which provide logic and state
|
||||
//! required for execution of host.
|
||||
|
||||
use crate::instance_wrapper::InstanceWrapper;
|
||||
use crate::{instance_wrapper::InstanceWrapper, runtime::StoreData};
|
||||
use codec::{Decode, Encode};
|
||||
use log::trace;
|
||||
use sc_allocator::FreeingBumpHeapAllocator;
|
||||
@@ -31,7 +31,7 @@ use sc_executor_common::{
|
||||
use sp_core::sandbox as sandbox_primitives;
|
||||
use sp_wasm_interface::{FunctionContext, MemoryId, Pointer, Sandbox, WordSize};
|
||||
use std::{cell::RefCell, rc::Rc};
|
||||
use wasmtime::{Func, Val};
|
||||
use wasmtime::{Caller, Func, Val};
|
||||
|
||||
/// The state required to construct a HostContext context. The context only lasts for one host
|
||||
/// call, whereas the state is maintained for the duration of a Wasm runtime call, which may make
|
||||
@@ -64,45 +64,67 @@ impl HostState {
|
||||
}
|
||||
|
||||
/// Materialize `HostContext` that can be used to invoke a substrate host `dyn Function`.
|
||||
pub fn materialize<'a>(&'a self) -> HostContext<'a> {
|
||||
HostContext(self)
|
||||
pub(crate) fn materialize<'a, 'b, 'c>(
|
||||
&'a self,
|
||||
caller: &'b mut Caller<'c, StoreData>,
|
||||
) -> HostContext<'a, 'b, 'c> {
|
||||
HostContext { host_state: self, caller }
|
||||
}
|
||||
}
|
||||
|
||||
/// A `HostContext` implements `FunctionContext` for making host calls from a Wasmtime
|
||||
/// runtime. The `HostContext` exists only for the lifetime of the call and borrows state from
|
||||
/// a longer-living `HostState`.
|
||||
pub struct HostContext<'a>(&'a HostState);
|
||||
pub(crate) struct HostContext<'a, 'b, 'c> {
|
||||
host_state: &'a HostState,
|
||||
caller: &'b mut Caller<'c, StoreData>,
|
||||
}
|
||||
|
||||
impl<'a> std::ops::Deref for HostContext<'a> {
|
||||
impl<'a, 'b, 'c> std::ops::Deref for HostContext<'a, 'b, 'c> {
|
||||
type Target = HostState;
|
||||
fn deref(&self) -> &HostState {
|
||||
self.0
|
||||
self.host_state
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> sp_wasm_interface::FunctionContext for HostContext<'a> {
|
||||
impl<'a, 'b, 'c> sp_wasm_interface::FunctionContext for HostContext<'a, 'b, 'c> {
|
||||
fn read_memory_into(
|
||||
&self,
|
||||
address: Pointer<u8>,
|
||||
dest: &mut [u8],
|
||||
) -> sp_wasm_interface::Result<()> {
|
||||
self.instance.read_memory_into(address, dest).map_err(|e| e.to_string())
|
||||
let ctx = &self.caller;
|
||||
self.host_state
|
||||
.instance
|
||||
.read_memory_into(ctx, address, dest)
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
fn write_memory(&mut self, address: Pointer<u8>, data: &[u8]) -> sp_wasm_interface::Result<()> {
|
||||
self.instance.write_memory_from(address, data).map_err(|e| e.to_string())
|
||||
let ctx = &mut self.caller;
|
||||
self.host_state
|
||||
.instance
|
||||
.write_memory_from(ctx, address, data)
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
fn allocate_memory(&mut self, size: WordSize) -> sp_wasm_interface::Result<Pointer<u8>> {
|
||||
self.instance
|
||||
.allocate(&mut *self.allocator.borrow_mut(), size)
|
||||
let ctx = &mut self.caller;
|
||||
let allocator = &self.host_state.allocator;
|
||||
|
||||
self.host_state
|
||||
.instance
|
||||
.allocate(ctx, &mut *allocator.borrow_mut(), size)
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
fn deallocate_memory(&mut self, ptr: Pointer<u8>) -> sp_wasm_interface::Result<()> {
|
||||
self.instance
|
||||
.deallocate(&mut *self.allocator.borrow_mut(), ptr)
|
||||
let ctx = &mut self.caller;
|
||||
let allocator = &self.host_state.allocator;
|
||||
|
||||
self.host_state
|
||||
.instance
|
||||
.deallocate(ctx, &mut *allocator.borrow_mut(), ptr)
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
@@ -111,7 +133,7 @@ impl<'a> sp_wasm_interface::FunctionContext for HostContext<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Sandbox for HostContext<'a> {
|
||||
impl<'a, 'b, 'c> Sandbox for HostContext<'a, 'b, 'c> {
|
||||
fn memory_get(
|
||||
&mut self,
|
||||
memory_id: MemoryId,
|
||||
@@ -129,7 +151,8 @@ impl<'a> Sandbox for HostContext<'a> {
|
||||
Ok(buffer) => buffer,
|
||||
};
|
||||
|
||||
if let Err(_) = self.instance.write_memory_from(buf_ptr, &buffer) {
|
||||
let instance = self.instance.clone();
|
||||
if let Err(_) = instance.write_memory_from(&mut self.caller, buf_ptr, &buffer) {
|
||||
return Ok(sandbox_primitives::ERR_OUT_OF_BOUNDS)
|
||||
}
|
||||
|
||||
@@ -148,7 +171,7 @@ impl<'a> Sandbox for HostContext<'a> {
|
||||
|
||||
let len = val_len as usize;
|
||||
|
||||
let buffer = match self.instance.read_memory(val_ptr, len) {
|
||||
let buffer = match self.instance.read_memory(&self.caller, val_ptr, len) {
|
||||
Err(_) => return Ok(sandbox_primitives::ERR_OUT_OF_BOUNDS),
|
||||
Ok(buffer) => buffer,
|
||||
};
|
||||
@@ -241,12 +264,14 @@ impl<'a> Sandbox for HostContext<'a> {
|
||||
) -> sp_wasm_interface::Result<u32> {
|
||||
// Extract a dispatch thunk from the instance's table by the specified index.
|
||||
let dispatch_thunk = {
|
||||
let ctx = &mut self.caller;
|
||||
let table_item = self
|
||||
.host_state
|
||||
.instance
|
||||
.table()
|
||||
.as_ref()
|
||||
.ok_or_else(|| "Runtime doesn't have a table; sandbox is unavailable")?
|
||||
.get(dispatch_thunk_id);
|
||||
.get(ctx, dispatch_thunk_id);
|
||||
|
||||
table_item
|
||||
.ok_or_else(|| "dispatch_thunk_id is out of bounds")?
|
||||
@@ -295,12 +320,12 @@ impl<'a> Sandbox for HostContext<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
struct SandboxContext<'a, 'b> {
|
||||
host_context: &'a mut HostContext<'b>,
|
||||
struct SandboxContext<'a, 'b, 'c, 'd> {
|
||||
host_context: &'a mut HostContext<'b, 'c, 'd>,
|
||||
dispatch_thunk: Func,
|
||||
}
|
||||
|
||||
impl<'a, 'b> sandbox::SandboxContext for SandboxContext<'a, 'b> {
|
||||
impl<'a, 'b, 'c, 'd> sandbox::SandboxContext for SandboxContext<'a, 'b, 'c, 'd> {
|
||||
fn invoke(
|
||||
&mut self,
|
||||
invoke_args_ptr: Pointer<u8>,
|
||||
@@ -308,12 +333,16 @@ impl<'a, 'b> sandbox::SandboxContext for SandboxContext<'a, 'b> {
|
||||
state: u32,
|
||||
func_idx: SupervisorFuncIndex,
|
||||
) -> Result<i64> {
|
||||
let result = self.dispatch_thunk.call(&[
|
||||
Val::I32(u32::from(invoke_args_ptr) as i32),
|
||||
Val::I32(invoke_args_len as i32),
|
||||
Val::I32(state as i32),
|
||||
Val::I32(usize::from(func_idx) as i32),
|
||||
]);
|
||||
let result = self.dispatch_thunk.call(
|
||||
&mut self.host_context.caller,
|
||||
&[
|
||||
Val::I32(u32::from(invoke_args_ptr) as i32),
|
||||
Val::I32(invoke_args_len as i32),
|
||||
Val::I32(state as i32),
|
||||
Val::I32(usize::from(func_idx) as i32),
|
||||
],
|
||||
);
|
||||
|
||||
match result {
|
||||
Ok(ret_vals) => {
|
||||
let ret_val = if ret_vals.len() != 1 {
|
||||
|
||||
@@ -16,12 +16,15 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::{state_holder, util};
|
||||
use crate::{
|
||||
runtime::{Store, StoreData},
|
||||
util,
|
||||
};
|
||||
use sc_executor_common::error::WasmError;
|
||||
use sp_wasm_interface::{Function, ValueType};
|
||||
use std::any::Any;
|
||||
use wasmtime::{
|
||||
Extern, ExternType, Func, FuncType, ImportType, Limits, Memory, MemoryType, Module, Store,
|
||||
Caller, Extern, ExternType, Func, FuncType, ImportType, Limits, Memory, MemoryType, Module,
|
||||
Trap, Val,
|
||||
};
|
||||
|
||||
@@ -34,8 +37,8 @@ pub struct Imports {
|
||||
|
||||
/// Goes over all imports of a module and prepares a vector of `Extern`s that can be used for
|
||||
/// instantiation of the module. Returns an error if there are imports that cannot be satisfied.
|
||||
pub fn resolve_imports(
|
||||
store: &Store,
|
||||
pub(crate) fn resolve_imports(
|
||||
store: &mut Store,
|
||||
module: &Module,
|
||||
host_functions: &[&'static dyn Function],
|
||||
heap_pages: u32,
|
||||
@@ -78,7 +81,7 @@ fn import_name<'a, 'b: 'a>(import: &'a ImportType<'b>) -> Result<&'a str, WasmEr
|
||||
}
|
||||
|
||||
fn resolve_memory_import(
|
||||
store: &Store,
|
||||
store: &mut Store,
|
||||
import_ty: &ImportType,
|
||||
heap_pages: u32,
|
||||
) -> Result<Extern, WasmError> {
|
||||
@@ -117,7 +120,7 @@ fn resolve_memory_import(
|
||||
}
|
||||
|
||||
fn resolve_func_import(
|
||||
store: &Store,
|
||||
store: &mut Store,
|
||||
import_ty: &ImportType,
|
||||
host_functions: &[&'static dyn Function],
|
||||
allow_missing_func_imports: bool,
|
||||
@@ -162,19 +165,27 @@ struct HostFuncHandler {
|
||||
host_func: &'static dyn Function,
|
||||
}
|
||||
|
||||
fn call_static(
|
||||
fn call_static<'a>(
|
||||
static_func: &'static dyn Function,
|
||||
wasmtime_params: &[Val],
|
||||
wasmtime_results: &mut [Val],
|
||||
mut caller: Caller<'a, StoreData>,
|
||||
) -> Result<(), wasmtime::Trap> {
|
||||
let unwind_result = state_holder::with_context(|host_ctx| {
|
||||
let mut host_ctx = host_ctx.expect(
|
||||
"host functions can be called only from wasm instance;
|
||||
wasm instance is always called initializing context;
|
||||
therefore host_ctx cannot be None;
|
||||
qed
|
||||
",
|
||||
);
|
||||
let unwind_result = {
|
||||
let host_state = caller
|
||||
.data()
|
||||
.host_state()
|
||||
.expect(
|
||||
"host functions can be called only from wasm instance;
|
||||
wasm instance is always called initializing context;
|
||||
therefore host_ctx cannot be None;
|
||||
qed
|
||||
",
|
||||
)
|
||||
.clone();
|
||||
|
||||
let mut host_ctx = host_state.materialize(&mut caller);
|
||||
|
||||
// `from_wasmtime_val` panics if it encounters a value that doesn't fit into the values
|
||||
// available in substrate.
|
||||
//
|
||||
@@ -185,7 +196,7 @@ fn call_static(
|
||||
std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
|
||||
static_func.execute(&mut host_ctx, &mut params)
|
||||
}))
|
||||
});
|
||||
};
|
||||
|
||||
let execution_result = match unwind_result {
|
||||
Ok(execution_result) => execution_result,
|
||||
@@ -219,11 +230,11 @@ impl HostFuncHandler {
|
||||
Self { host_func }
|
||||
}
|
||||
|
||||
fn into_extern(self, store: &Store) -> Extern {
|
||||
fn into_extern(self, store: &mut Store) -> Extern {
|
||||
let host_func = self.host_func;
|
||||
let func_ty = wasmtime_func_sig(self.host_func);
|
||||
let func = Func::new(store, func_ty, move |_, params, result| {
|
||||
call_static(host_func, params, result)
|
||||
let func = Func::new(store, func_ty, move |caller, params, result| {
|
||||
call_static(host_func, params, result, caller)
|
||||
});
|
||||
Extern::Func(func)
|
||||
}
|
||||
@@ -243,7 +254,7 @@ impl MissingHostFuncHandler {
|
||||
})
|
||||
}
|
||||
|
||||
fn into_extern(self, store: &Store, func_ty: &FuncType) -> Extern {
|
||||
fn into_extern(self, store: &mut Store, func_ty: &FuncType) -> Extern {
|
||||
let Self { module, name } = self;
|
||||
let func = Func::new(store, func_ty.clone(), move |_, _, _| {
|
||||
Err(Trap::new(format!("call to a missing function {}:{}", module, name)))
|
||||
|
||||
@@ -19,20 +19,18 @@
|
||||
//! Defines data and logic needed for interaction with an WebAssembly instance of a substrate
|
||||
//! runtime module.
|
||||
|
||||
use crate::{
|
||||
imports::Imports,
|
||||
util::{from_wasmtime_val, into_wasmtime_val},
|
||||
};
|
||||
use crate::imports::Imports;
|
||||
|
||||
use sc_executor_common::{
|
||||
error::{Error, Result},
|
||||
runtime_blob,
|
||||
util::checked_range,
|
||||
wasm_runtime::InvokeMethod,
|
||||
};
|
||||
use sp_wasm_interface::{Pointer, Value, WordSize};
|
||||
use std::{marker, slice};
|
||||
use wasmtime::{Extern, Func, Global, Instance, Memory, Module, Store, Table, Val};
|
||||
use std::marker;
|
||||
use wasmtime::{
|
||||
AsContext, AsContextMut, Extern, Func, Global, Instance, Memory, Module, Table, Val,
|
||||
};
|
||||
|
||||
/// Invoked entrypoint format.
|
||||
pub enum EntryPointType {
|
||||
@@ -58,7 +56,12 @@ pub struct EntryPoint {
|
||||
|
||||
impl EntryPoint {
|
||||
/// Call this entry point.
|
||||
pub fn call(&self, data_ptr: Pointer<u8>, data_len: WordSize) -> Result<u64> {
|
||||
pub fn call(
|
||||
&self,
|
||||
ctx: impl AsContextMut,
|
||||
data_ptr: Pointer<u8>,
|
||||
data_len: WordSize,
|
||||
) -> Result<u64> {
|
||||
let data_ptr = u32::from(data_ptr);
|
||||
let data_len = u32::from(data_len);
|
||||
|
||||
@@ -68,15 +71,18 @@ impl EntryPoint {
|
||||
|
||||
match self.call_type {
|
||||
EntryPointType::Direct { ref entrypoint } =>
|
||||
entrypoint.call((data_ptr, data_len)).map_err(handle_trap),
|
||||
entrypoint.call(ctx, (data_ptr, data_len)).map_err(handle_trap),
|
||||
EntryPointType::Wrapped { func, ref dispatcher } =>
|
||||
dispatcher.call((func, data_ptr, data_len)).map_err(handle_trap),
|
||||
dispatcher.call(ctx, (func, data_ptr, data_len)).map_err(handle_trap),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn direct(func: wasmtime::Func) -> std::result::Result<Self, &'static str> {
|
||||
pub fn direct(
|
||||
func: wasmtime::Func,
|
||||
ctx: impl AsContext,
|
||||
) -> std::result::Result<Self, &'static str> {
|
||||
let entrypoint = func
|
||||
.typed::<(u32, u32), u64>()
|
||||
.typed::<(u32, u32), u64, _>(ctx)
|
||||
.map_err(|_| "Invalid signature for direct entry point")?
|
||||
.clone();
|
||||
Ok(Self { call_type: EntryPointType::Direct { entrypoint } })
|
||||
@@ -85,9 +91,10 @@ impl EntryPoint {
|
||||
pub fn wrapped(
|
||||
dispatcher: wasmtime::Func,
|
||||
func: u32,
|
||||
ctx: impl AsContext,
|
||||
) -> std::result::Result<Self, &'static str> {
|
||||
let dispatcher = dispatcher
|
||||
.typed::<(u32, u32, u32), u64>()
|
||||
.typed::<(u32, u32, u32), u64, _>(ctx)
|
||||
.map_err(|_| "Invalid signature for wrapped entry point")?
|
||||
.clone();
|
||||
Ok(Self { call_type: EntryPointType::Wrapped { func, dispatcher } })
|
||||
@@ -144,8 +151,13 @@ fn extern_func(extern_: &Extern) -> Option<&Func> {
|
||||
|
||||
impl InstanceWrapper {
|
||||
/// Create a new instance wrapper from the given wasm module.
|
||||
pub fn new(store: &Store, module: &Module, imports: &Imports, heap_pages: u32) -> Result<Self> {
|
||||
let instance = Instance::new(store, module, &imports.externs)
|
||||
pub fn new(
|
||||
module: &Module,
|
||||
imports: &Imports,
|
||||
heap_pages: u32,
|
||||
mut ctx: impl AsContextMut,
|
||||
) -> Result<Self> {
|
||||
let instance = Instance::new(&mut ctx, module, &imports.externs)
|
||||
.map_err(|e| Error::from(format!("cannot instantiate: {}", e)))?;
|
||||
|
||||
let memory = match imports.memory_import_index {
|
||||
@@ -153,51 +165,55 @@ impl InstanceWrapper {
|
||||
.expect("only memory can be at the `memory_idx`; qed")
|
||||
.clone(),
|
||||
None => {
|
||||
let memory = get_linear_memory(&instance)?;
|
||||
if !memory.grow(heap_pages).is_ok() {
|
||||
return Err("failed to increase the linear memory size".into())
|
||||
let memory = get_linear_memory(&instance, &mut ctx)?;
|
||||
if !memory.grow(&mut ctx, heap_pages).is_ok() {
|
||||
return Err("failed top increase the linear memory size".into())
|
||||
}
|
||||
memory
|
||||
},
|
||||
};
|
||||
|
||||
Ok(Self {
|
||||
table: get_table(&instance),
|
||||
instance,
|
||||
memory,
|
||||
_not_send_nor_sync: marker::PhantomData,
|
||||
})
|
||||
let table = get_table(&instance, ctx);
|
||||
|
||||
Ok(Self { table, instance, memory, _not_send_nor_sync: marker::PhantomData })
|
||||
}
|
||||
|
||||
/// Resolves a substrate entrypoint by the given name.
|
||||
///
|
||||
/// An entrypoint must have a signature `(i32, i32) -> i64`, otherwise this function will return
|
||||
/// an error.
|
||||
pub fn resolve_entrypoint(&self, method: InvokeMethod) -> Result<EntryPoint> {
|
||||
pub fn resolve_entrypoint(
|
||||
&self,
|
||||
method: InvokeMethod,
|
||||
mut ctx: impl AsContextMut,
|
||||
) -> Result<EntryPoint> {
|
||||
Ok(match method {
|
||||
InvokeMethod::Export(method) => {
|
||||
// Resolve the requested method and verify that it has a proper signature.
|
||||
let export = self.instance.get_export(method).ok_or_else(|| {
|
||||
let export = self.instance.get_export(&mut ctx, method).ok_or_else(|| {
|
||||
Error::from(format!("Exported method {} is not found", method))
|
||||
})?;
|
||||
let func = extern_func(&export)
|
||||
.ok_or_else(|| Error::from(format!("Export {} is not a function", method)))?
|
||||
.clone();
|
||||
EntryPoint::direct(func).map_err(|_| {
|
||||
EntryPoint::direct(func, ctx).map_err(|_| {
|
||||
Error::from(format!("Exported function '{}' has invalid signature.", method))
|
||||
})?
|
||||
},
|
||||
InvokeMethod::Table(func_ref) => {
|
||||
let table =
|
||||
self.instance.get_table("__indirect_function_table").ok_or(Error::NoTable)?;
|
||||
let val = table.get(func_ref).ok_or(Error::NoTableEntryWithIndex(func_ref))?;
|
||||
let table = self
|
||||
.instance
|
||||
.get_table(&mut ctx, "__indirect_function_table")
|
||||
.ok_or(Error::NoTable)?;
|
||||
let val =
|
||||
table.get(&mut ctx, func_ref).ok_or(Error::NoTableEntryWithIndex(func_ref))?;
|
||||
let func = val
|
||||
.funcref()
|
||||
.ok_or(Error::TableElementIsNotAFunction(func_ref))?
|
||||
.ok_or(Error::FunctionRefIsNull(func_ref))?
|
||||
.clone();
|
||||
|
||||
EntryPoint::direct(func).map_err(|_| {
|
||||
EntryPoint::direct(func, ctx).map_err(|_| {
|
||||
Error::from(format!(
|
||||
"Function @{} in exported table has invalid signature for direct call.",
|
||||
func_ref,
|
||||
@@ -205,10 +221,12 @@ impl InstanceWrapper {
|
||||
})?
|
||||
},
|
||||
InvokeMethod::TableWithWrapper { dispatcher_ref, func } => {
|
||||
let table =
|
||||
self.instance.get_table("__indirect_function_table").ok_or(Error::NoTable)?;
|
||||
let table = self
|
||||
.instance
|
||||
.get_table(&mut ctx, "__indirect_function_table")
|
||||
.ok_or(Error::NoTable)?;
|
||||
let val = table
|
||||
.get(dispatcher_ref)
|
||||
.get(&mut ctx, dispatcher_ref)
|
||||
.ok_or(Error::NoTableEntryWithIndex(dispatcher_ref))?;
|
||||
let dispatcher = val
|
||||
.funcref()
|
||||
@@ -216,7 +234,7 @@ impl InstanceWrapper {
|
||||
.ok_or(Error::FunctionRefIsNull(dispatcher_ref))?
|
||||
.clone();
|
||||
|
||||
EntryPoint::wrapped(dispatcher, func).map_err(|_| {
|
||||
EntryPoint::wrapped(dispatcher, func, ctx).map_err(|_| {
|
||||
Error::from(format!(
|
||||
"Function @{} in exported table has invalid signature for wrapped call.",
|
||||
dispatcher_ref,
|
||||
@@ -234,17 +252,17 @@ impl InstanceWrapper {
|
||||
/// Reads `__heap_base: i32` global variable and returns it.
|
||||
///
|
||||
/// If it doesn't exist, not a global or of not i32 type returns an error.
|
||||
pub fn extract_heap_base(&self) -> Result<u32> {
|
||||
pub fn extract_heap_base(&self, mut ctx: impl AsContextMut) -> Result<u32> {
|
||||
let heap_base_export = self
|
||||
.instance
|
||||
.get_export("__heap_base")
|
||||
.get_export(&mut ctx, "__heap_base")
|
||||
.ok_or_else(|| Error::from("__heap_base is not found"))?;
|
||||
|
||||
let heap_base_global = extern_global(&heap_base_export)
|
||||
.ok_or_else(|| Error::from("__heap_base is not a global"))?;
|
||||
|
||||
let heap_base = heap_base_global
|
||||
.get()
|
||||
.get(&mut ctx)
|
||||
.i32()
|
||||
.ok_or_else(|| Error::from("__heap_base is not a i32"))?;
|
||||
|
||||
@@ -252,15 +270,15 @@ impl InstanceWrapper {
|
||||
}
|
||||
|
||||
/// Get the value from a global with the given `name`.
|
||||
pub fn get_global_val(&self, name: &str) -> Result<Option<Value>> {
|
||||
let global = match self.instance.get_export(name) {
|
||||
pub fn get_global_val(&self, mut ctx: impl AsContextMut, name: &str) -> Result<Option<Value>> {
|
||||
let global = match self.instance.get_export(&mut ctx, name) {
|
||||
Some(global) => global,
|
||||
None => return Ok(None),
|
||||
};
|
||||
|
||||
let global = extern_global(&global).ok_or_else(|| format!("`{}` is not a global", name))?;
|
||||
|
||||
match global.get() {
|
||||
match global.get(ctx) {
|
||||
Val::I32(val) => Ok(Some(Value::I32(val))),
|
||||
Val::I64(val) => Ok(Some(Value::I64(val))),
|
||||
Val::F32(val) => Ok(Some(Value::F32(val))),
|
||||
@@ -268,12 +286,17 @@ impl InstanceWrapper {
|
||||
_ => Err("Unknown value type".into()),
|
||||
}
|
||||
}
|
||||
|
||||
/// Get a global with the given `name`.
|
||||
pub fn get_global(&self, ctx: impl AsContextMut, name: &str) -> Option<wasmtime::Global> {
|
||||
self.instance.get_global(ctx, name)
|
||||
}
|
||||
}
|
||||
|
||||
/// Extract linear memory instance from the given instance.
|
||||
fn get_linear_memory(instance: &Instance) -> Result<Memory> {
|
||||
fn get_linear_memory(instance: &Instance, ctx: impl AsContextMut) -> Result<Memory> {
|
||||
let memory_export = instance
|
||||
.get_export("memory")
|
||||
.get_export(ctx, "memory")
|
||||
.ok_or_else(|| Error::from("memory is not exported under `memory` name"))?;
|
||||
|
||||
let memory = extern_memory(&memory_export)
|
||||
@@ -284,9 +307,9 @@ fn get_linear_memory(instance: &Instance) -> Result<Memory> {
|
||||
}
|
||||
|
||||
/// Extract the table from the given instance if any.
|
||||
fn get_table(instance: &Instance) -> Option<Table> {
|
||||
fn get_table(instance: &Instance, ctx: impl AsContextMut) -> Option<Table> {
|
||||
instance
|
||||
.get_export("__indirect_function_table")
|
||||
.get_export(ctx, "__indirect_function_table")
|
||||
.as_ref()
|
||||
.and_then(extern_table)
|
||||
.cloned()
|
||||
@@ -297,12 +320,17 @@ impl InstanceWrapper {
|
||||
/// Read data from a slice of memory into a newly allocated buffer.
|
||||
///
|
||||
/// Returns an error if the read would go out of the memory bounds.
|
||||
pub fn read_memory(&self, source_addr: Pointer<u8>, size: usize) -> Result<Vec<u8>> {
|
||||
let range = checked_range(source_addr.into(), size, self.memory.data_size())
|
||||
pub fn read_memory(
|
||||
&self,
|
||||
ctx: impl AsContext,
|
||||
source_addr: Pointer<u8>,
|
||||
size: usize,
|
||||
) -> Result<Vec<u8>> {
|
||||
let range = checked_range(source_addr.into(), size, self.memory.data_size(&ctx))
|
||||
.ok_or_else(|| Error::Other("memory read is out of bounds".into()))?;
|
||||
|
||||
let mut buffer = vec![0; range.len()];
|
||||
self.read_memory_into(source_addr, &mut buffer)?;
|
||||
self.read_memory_into(ctx, source_addr, &mut buffer)?;
|
||||
|
||||
Ok(buffer)
|
||||
}
|
||||
@@ -310,33 +338,35 @@ impl InstanceWrapper {
|
||||
/// Read data from the instance memory into a slice.
|
||||
///
|
||||
/// Returns an error if the read would go out of the memory bounds.
|
||||
pub fn read_memory_into(&self, source_addr: Pointer<u8>, dest: &mut [u8]) -> Result<()> {
|
||||
unsafe {
|
||||
// This should be safe since we don't grow up memory while caching this reference and
|
||||
// we give up the reference before returning from this function.
|
||||
let memory = self.memory_as_slice();
|
||||
pub fn read_memory_into(
|
||||
&self,
|
||||
ctx: impl AsContext,
|
||||
address: Pointer<u8>,
|
||||
dest: &mut [u8],
|
||||
) -> Result<()> {
|
||||
let memory = self.memory.data(ctx.as_context());
|
||||
|
||||
let range = checked_range(source_addr.into(), dest.len(), memory.len())
|
||||
.ok_or_else(|| Error::Other("memory read is out of bounds".into()))?;
|
||||
dest.copy_from_slice(&memory[range]);
|
||||
Ok(())
|
||||
}
|
||||
let range = checked_range(address.into(), dest.len(), memory.len())
|
||||
.ok_or_else(|| Error::Other("memory read is out of bounds".into()))?;
|
||||
dest.copy_from_slice(&memory[range]);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Write data to the instance memory from a slice.
|
||||
///
|
||||
/// Returns an error if the write would go out of the memory bounds.
|
||||
pub fn write_memory_from(&self, dest_addr: Pointer<u8>, data: &[u8]) -> Result<()> {
|
||||
unsafe {
|
||||
// This should be safe since we don't grow up memory while caching this reference and
|
||||
// we give up the reference before returning from this function.
|
||||
let memory = self.memory_as_slice_mut();
|
||||
pub fn write_memory_from(
|
||||
&self,
|
||||
mut ctx: impl AsContextMut,
|
||||
address: Pointer<u8>,
|
||||
data: &[u8],
|
||||
) -> Result<()> {
|
||||
let memory = self.memory.data_mut(ctx.as_context_mut());
|
||||
|
||||
let range = checked_range(dest_addr.into(), data.len(), memory.len())
|
||||
.ok_or_else(|| Error::Other("memory write is out of bounds".into()))?;
|
||||
memory[range].copy_from_slice(data);
|
||||
Ok(())
|
||||
}
|
||||
let range = checked_range(address.into(), data.len(), memory.len())
|
||||
.ok_or_else(|| Error::Other("memory write is out of bounds".into()))?;
|
||||
memory[range].copy_from_slice(data);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Allocate some memory of the given size. Returns pointer to the allocated memory region.
|
||||
@@ -345,16 +375,13 @@ impl InstanceWrapper {
|
||||
/// to get more details.
|
||||
pub fn allocate(
|
||||
&self,
|
||||
mut ctx: impl AsContextMut,
|
||||
allocator: &mut sc_allocator::FreeingBumpHeapAllocator,
|
||||
size: WordSize,
|
||||
) -> Result<Pointer<u8>> {
|
||||
unsafe {
|
||||
// This should be safe since we don't grow up memory while caching this reference and
|
||||
// we give up the reference before returning from this function.
|
||||
let memory = self.memory_as_slice_mut();
|
||||
let memory = self.memory.data_mut(ctx.as_context_mut());
|
||||
|
||||
allocator.allocate(memory, size).map_err(Into::into)
|
||||
}
|
||||
allocator.allocate(memory, size).map_err(Into::into)
|
||||
}
|
||||
|
||||
/// Deallocate the memory pointed by the given pointer.
|
||||
@@ -362,64 +389,25 @@ impl InstanceWrapper {
|
||||
/// Returns `Err` in case the given memory region cannot be deallocated.
|
||||
pub fn deallocate(
|
||||
&self,
|
||||
mut ctx: impl AsContextMut,
|
||||
allocator: &mut sc_allocator::FreeingBumpHeapAllocator,
|
||||
ptr: Pointer<u8>,
|
||||
) -> Result<()> {
|
||||
unsafe {
|
||||
// This should be safe since we don't grow up memory while caching this reference and
|
||||
// we give up the reference before returning from this function.
|
||||
let memory = self.memory_as_slice_mut();
|
||||
let memory = self.memory.data_mut(ctx.as_context_mut());
|
||||
|
||||
allocator.deallocate(memory, ptr).map_err(Into::into)
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns linear memory of the wasm instance as a slice.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// Wasmtime doesn't provide comprehensive documentation about the exact behavior of the data
|
||||
/// pointer. If a dynamic style heap is used the base pointer of the heap can change. Since
|
||||
/// growing, we cannot guarantee the lifetime of the returned slice reference.
|
||||
unsafe fn memory_as_slice(&self) -> &[u8] {
|
||||
let ptr = self.memory.data_ptr() as *const _;
|
||||
let len = self.memory.data_size();
|
||||
|
||||
if len == 0 {
|
||||
&[]
|
||||
} else {
|
||||
slice::from_raw_parts(ptr, len)
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns linear memory of the wasm instance as a slice.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// See `[memory_as_slice]`. In addition to those requirements, since a mutable reference is
|
||||
/// returned it must be ensured that only one mutable and no shared references to memory exists
|
||||
/// at the same time.
|
||||
unsafe fn memory_as_slice_mut(&self) -> &mut [u8] {
|
||||
let ptr = self.memory.data_ptr();
|
||||
let len = self.memory.data_size();
|
||||
|
||||
if len == 0 {
|
||||
&mut []
|
||||
} else {
|
||||
slice::from_raw_parts_mut(ptr, len)
|
||||
}
|
||||
allocator.deallocate(memory, ptr).map_err(Into::into)
|
||||
}
|
||||
|
||||
/// Returns the pointer to the first byte of the linear memory for this instance.
|
||||
pub fn base_ptr(&self) -> *const u8 {
|
||||
self.memory.data_ptr()
|
||||
pub fn base_ptr(&self, ctx: impl AsContext) -> *const u8 {
|
||||
self.memory.data_ptr(ctx)
|
||||
}
|
||||
|
||||
/// Removes physical backing from the allocated linear memory. This leads to returning the
|
||||
/// memory back to the system. While the memory is zeroed this is considered as a side-effect
|
||||
/// and is not relied upon. Thus this function acts as a hint.
|
||||
pub fn decommit(&self) {
|
||||
if self.memory.data_size() == 0 {
|
||||
pub fn decommit(&self, ctx: impl AsContext) {
|
||||
if self.memory.data_size(&ctx) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -428,8 +416,8 @@ impl InstanceWrapper {
|
||||
use std::sync::Once;
|
||||
|
||||
unsafe {
|
||||
let ptr = self.memory.data_ptr();
|
||||
let len = self.memory.data_size();
|
||||
let ptr = self.memory.data_ptr(&ctx);
|
||||
let len = self.memory.data_size(ctx);
|
||||
|
||||
// Linux handles MADV_DONTNEED reliably. The result is that the given area
|
||||
// is unmapped and will be zeroed on the next pagefault.
|
||||
@@ -447,23 +435,3 @@ impl InstanceWrapper {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl runtime_blob::InstanceGlobals for InstanceWrapper {
|
||||
type Global = wasmtime::Global;
|
||||
|
||||
fn get_global(&self, export_name: &str) -> Self::Global {
|
||||
self.instance
|
||||
.get_global(export_name)
|
||||
.expect("get_global is guaranteed to be called with an export name of a global; qed")
|
||||
}
|
||||
|
||||
fn get_global_value(&self, global: &Self::Global) -> Value {
|
||||
from_wasmtime_val(global.get())
|
||||
}
|
||||
|
||||
fn set_global_value(&self, global: &Self::Global, value: Value) {
|
||||
global.set(into_wasmtime_val(value)).expect(
|
||||
"the value is guaranteed to be of the same value; the global is guaranteed to be mutable; qed",
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,7 +21,6 @@ mod host;
|
||||
mod imports;
|
||||
mod instance_wrapper;
|
||||
mod runtime;
|
||||
mod state_holder;
|
||||
mod util;
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
@@ -22,13 +22,15 @@ use crate::{
|
||||
host::HostState,
|
||||
imports::{resolve_imports, Imports},
|
||||
instance_wrapper::{EntryPoint, InstanceWrapper},
|
||||
state_holder,
|
||||
util,
|
||||
};
|
||||
|
||||
use sc_allocator::FreeingBumpHeapAllocator;
|
||||
use sc_executor_common::{
|
||||
error::{Result, WasmError},
|
||||
runtime_blob::{DataSegmentsSnapshot, ExposedMutableGlobalsSet, GlobalsSnapshot, RuntimeBlob},
|
||||
runtime_blob::{
|
||||
self, DataSegmentsSnapshot, ExposedMutableGlobalsSet, GlobalsSnapshot, RuntimeBlob,
|
||||
},
|
||||
wasm_runtime::{InvokeMethod, WasmInstance, WasmModule},
|
||||
};
|
||||
use sp_runtime_interface::unpack_ptr_and_len;
|
||||
@@ -38,7 +40,24 @@ use std::{
|
||||
rc::Rc,
|
||||
sync::Arc,
|
||||
};
|
||||
use wasmtime::{Engine, Store};
|
||||
use wasmtime::{AsContext, AsContextMut, Engine, StoreLimits};
|
||||
|
||||
pub(crate) struct StoreData {
|
||||
/// The limits we aply to the store. We need to store it here to return a reference to this
|
||||
/// object when we have the limits enabled.
|
||||
limits: StoreLimits,
|
||||
/// This will only be set when we call into the runtime.
|
||||
host_state: Option<Rc<HostState>>,
|
||||
}
|
||||
|
||||
impl StoreData {
|
||||
/// Returns a reference to the host state.
|
||||
pub fn host_state(&self) -> Option<&Rc<HostState>> {
|
||||
self.host_state.as_ref()
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) type Store = wasmtime::Store<StoreData>;
|
||||
|
||||
enum Strategy {
|
||||
FastInstanceReuse {
|
||||
@@ -46,6 +65,7 @@ enum Strategy {
|
||||
globals_snapshot: GlobalsSnapshot<wasmtime::Global>,
|
||||
data_segments_snapshot: Arc<DataSegmentsSnapshot>,
|
||||
heap_base: u32,
|
||||
store: Store,
|
||||
},
|
||||
RecreateInstance(InstanceCreator),
|
||||
}
|
||||
@@ -58,8 +78,33 @@ struct InstanceCreator {
|
||||
}
|
||||
|
||||
impl InstanceCreator {
|
||||
fn instantiate(&self) -> Result<InstanceWrapper> {
|
||||
InstanceWrapper::new(&self.store, &*self.module, &*self.imports, self.heap_pages)
|
||||
fn instantiate(&mut self) -> Result<InstanceWrapper> {
|
||||
InstanceWrapper::new(&*self.module, &*self.imports, self.heap_pages, &mut self.store)
|
||||
}
|
||||
}
|
||||
|
||||
struct InstanceGlobals<'a, C> {
|
||||
ctx: &'a mut C,
|
||||
instance: &'a InstanceWrapper,
|
||||
}
|
||||
|
||||
impl<'a, C: AsContextMut> runtime_blob::InstanceGlobals for InstanceGlobals<'a, C> {
|
||||
type Global = wasmtime::Global;
|
||||
|
||||
fn get_global(&mut self, export_name: &str) -> Self::Global {
|
||||
self.instance
|
||||
.get_global(&mut self.ctx, export_name)
|
||||
.expect("get_global is guaranteed to be called with an export name of a global; qed")
|
||||
}
|
||||
|
||||
fn get_global_value(&mut self, global: &Self::Global) -> Value {
|
||||
util::from_wasmtime_val(global.get(&mut self.ctx))
|
||||
}
|
||||
|
||||
fn set_global_value(&mut self, global: &Self::Global, value: Value) {
|
||||
global.set(&mut self.ctx, util::into_wasmtime_val(value)).expect(
|
||||
"the value is guaranteed to be of the same value; the global is guaranteed to be mutable; qed",
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -82,19 +127,25 @@ pub struct WasmtimeRuntime {
|
||||
impl WasmtimeRuntime {
|
||||
/// Creates the store respecting the set limits.
|
||||
fn new_store(&self) -> Store {
|
||||
match self.config.max_memory_pages {
|
||||
Some(max_memory_pages) => Store::new_with_limits(
|
||||
&self.engine,
|
||||
wasmtime::StoreLimitsBuilder::new().memory_pages(max_memory_pages).build(),
|
||||
),
|
||||
None => Store::new(&self.engine),
|
||||
let limits = if let Some(max_memory_pages) = self.config.max_memory_pages {
|
||||
wasmtime::StoreLimitsBuilder::new().memory_pages(max_memory_pages).build()
|
||||
} else {
|
||||
Default::default()
|
||||
};
|
||||
|
||||
let mut store = Store::new(&self.engine, StoreData { limits, host_state: None });
|
||||
|
||||
if self.config.max_memory_pages.is_some() {
|
||||
store.limiter(|s| &mut s.limits);
|
||||
}
|
||||
|
||||
store
|
||||
}
|
||||
}
|
||||
|
||||
impl WasmModule for WasmtimeRuntime {
|
||||
fn new_instance(&self) -> Result<Box<dyn WasmInstance>> {
|
||||
let store = self.new_store();
|
||||
let mut store = self.new_store();
|
||||
|
||||
// Scan all imports, find the matching host functions, and create stubs that adapt arguments
|
||||
// and results.
|
||||
@@ -103,7 +154,7 @@ impl WasmModule for WasmtimeRuntime {
|
||||
// However, I am not sure if that's a good idea since it would be pushing our luck
|
||||
// further by assuming that `Store` not only `Send` but also `Sync`.
|
||||
let imports = resolve_imports(
|
||||
&store,
|
||||
&mut store,
|
||||
&self.module,
|
||||
&self.host_functions,
|
||||
self.config.heap_pages,
|
||||
@@ -112,21 +163,24 @@ impl WasmModule for WasmtimeRuntime {
|
||||
|
||||
let strategy = if let Some(ref snapshot_data) = self.snapshot_data {
|
||||
let instance_wrapper =
|
||||
InstanceWrapper::new(&store, &self.module, &imports, self.config.heap_pages)?;
|
||||
let heap_base = instance_wrapper.extract_heap_base()?;
|
||||
InstanceWrapper::new(&self.module, &imports, self.config.heap_pages, &mut store)?;
|
||||
let heap_base = instance_wrapper.extract_heap_base(&mut store)?;
|
||||
|
||||
// This function panics if the instance was created from a runtime blob different from
|
||||
// which the mutable globals were collected. Here, it is easy to see that there is only
|
||||
// a single runtime blob and thus it's the same that was used for both creating the
|
||||
// instance and collecting the mutable globals.
|
||||
let globals_snapshot =
|
||||
GlobalsSnapshot::take(&snapshot_data.mutable_globals, &instance_wrapper);
|
||||
let globals_snapshot = GlobalsSnapshot::take(
|
||||
&snapshot_data.mutable_globals,
|
||||
&mut InstanceGlobals { ctx: &mut store, instance: &instance_wrapper },
|
||||
);
|
||||
|
||||
Strategy::FastInstanceReuse {
|
||||
instance_wrapper: Rc::new(instance_wrapper),
|
||||
globals_snapshot,
|
||||
data_segments_snapshot: snapshot_data.data_segments_snapshot.clone(),
|
||||
heap_base,
|
||||
store,
|
||||
}
|
||||
} else {
|
||||
Strategy::RecreateInstance(InstanceCreator {
|
||||
@@ -152,48 +206,63 @@ pub struct WasmtimeInstance {
|
||||
unsafe impl Send for WasmtimeInstance {}
|
||||
|
||||
impl WasmInstance for WasmtimeInstance {
|
||||
fn call(&self, method: InvokeMethod, data: &[u8]) -> Result<Vec<u8>> {
|
||||
match &self.strategy {
|
||||
fn call(&mut self, method: InvokeMethod, data: &[u8]) -> Result<Vec<u8>> {
|
||||
match &mut self.strategy {
|
||||
Strategy::FastInstanceReuse {
|
||||
instance_wrapper,
|
||||
globals_snapshot,
|
||||
data_segments_snapshot,
|
||||
heap_base,
|
||||
ref mut store,
|
||||
} => {
|
||||
let entrypoint = instance_wrapper.resolve_entrypoint(method)?;
|
||||
let entrypoint = instance_wrapper.resolve_entrypoint(method, &mut *store)?;
|
||||
|
||||
data_segments_snapshot.apply(|offset, contents| {
|
||||
instance_wrapper.write_memory_from(Pointer::new(offset), contents)
|
||||
instance_wrapper.write_memory_from(&mut *store, Pointer::new(offset), contents)
|
||||
})?;
|
||||
globals_snapshot.apply(&**instance_wrapper);
|
||||
globals_snapshot
|
||||
.apply(&mut InstanceGlobals { ctx: &mut *store, instance: &*instance_wrapper });
|
||||
let allocator = FreeingBumpHeapAllocator::new(*heap_base);
|
||||
|
||||
let result =
|
||||
perform_call(data, Rc::clone(&instance_wrapper), entrypoint, allocator);
|
||||
let result = perform_call(
|
||||
&mut *store,
|
||||
data,
|
||||
instance_wrapper.clone(),
|
||||
entrypoint,
|
||||
allocator,
|
||||
);
|
||||
|
||||
// Signal to the OS that we are done with the linear memory and that it can be
|
||||
// reclaimed.
|
||||
instance_wrapper.decommit();
|
||||
instance_wrapper.decommit(&store);
|
||||
|
||||
result
|
||||
},
|
||||
Strategy::RecreateInstance(instance_creator) => {
|
||||
Strategy::RecreateInstance(ref mut instance_creator) => {
|
||||
let instance_wrapper = instance_creator.instantiate()?;
|
||||
let heap_base = instance_wrapper.extract_heap_base()?;
|
||||
let entrypoint = instance_wrapper.resolve_entrypoint(method)?;
|
||||
let heap_base = instance_wrapper.extract_heap_base(&mut instance_creator.store)?;
|
||||
let entrypoint =
|
||||
instance_wrapper.resolve_entrypoint(method, &mut instance_creator.store)?;
|
||||
|
||||
let allocator = FreeingBumpHeapAllocator::new(heap_base);
|
||||
perform_call(data, Rc::new(instance_wrapper), entrypoint, allocator)
|
||||
perform_call(
|
||||
&mut instance_creator.store,
|
||||
data,
|
||||
Rc::new(instance_wrapper),
|
||||
entrypoint,
|
||||
allocator,
|
||||
)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn get_global_const(&self, name: &str) -> Result<Option<Value>> {
|
||||
match &self.strategy {
|
||||
Strategy::FastInstanceReuse { instance_wrapper, .. } =>
|
||||
instance_wrapper.get_global_val(name),
|
||||
Strategy::RecreateInstance(instance_creator) =>
|
||||
instance_creator.instantiate()?.get_global_val(name),
|
||||
fn get_global_const(&mut self, name: &str) -> Result<Option<Value>> {
|
||||
match &mut self.strategy {
|
||||
Strategy::FastInstanceReuse { instance_wrapper, ref mut store, .. } =>
|
||||
instance_wrapper.get_global_val(&mut *store, name),
|
||||
Strategy::RecreateInstance(ref mut instance_creator) => instance_creator
|
||||
.instantiate()?
|
||||
.get_global_val(&mut instance_creator.store, name),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -204,8 +273,8 @@ impl WasmInstance for WasmtimeInstance {
|
||||
// associated with it.
|
||||
None
|
||||
},
|
||||
Strategy::FastInstanceReuse { instance_wrapper, .. } =>
|
||||
Some(instance_wrapper.base_ptr()),
|
||||
Strategy::FastInstanceReuse { instance_wrapper, store, .. } =>
|
||||
Some(instance_wrapper.base_ptr(&store)),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -536,40 +605,50 @@ pub fn prepare_runtime_artifact(
|
||||
}
|
||||
|
||||
fn perform_call(
|
||||
mut ctx: impl AsContextMut<Data = StoreData>,
|
||||
data: &[u8],
|
||||
instance_wrapper: Rc<InstanceWrapper>,
|
||||
entrypoint: EntryPoint,
|
||||
mut allocator: FreeingBumpHeapAllocator,
|
||||
) -> Result<Vec<u8>> {
|
||||
let (data_ptr, data_len) = inject_input_data(&instance_wrapper, &mut allocator, data)?;
|
||||
let (data_ptr, data_len) =
|
||||
inject_input_data(&mut ctx, &instance_wrapper, &mut allocator, data)?;
|
||||
|
||||
let host_state = HostState::new(allocator, instance_wrapper.clone());
|
||||
let ret = state_holder::with_initialized_state(&host_state, || -> Result<_> {
|
||||
Ok(unpack_ptr_and_len(entrypoint.call(data_ptr, data_len)?))
|
||||
});
|
||||
|
||||
// Set the host state before calling into wasm.
|
||||
ctx.as_context_mut().data_mut().host_state = Some(Rc::new(host_state));
|
||||
|
||||
let ret = entrypoint.call(&mut ctx, data_ptr, data_len).map(unpack_ptr_and_len);
|
||||
|
||||
// Reset the host state
|
||||
ctx.as_context_mut().data_mut().host_state = None;
|
||||
|
||||
let (output_ptr, output_len) = ret?;
|
||||
let output = extract_output_data(&instance_wrapper, output_ptr, output_len)?;
|
||||
let output = extract_output_data(ctx, &instance_wrapper, output_ptr, output_len)?;
|
||||
|
||||
Ok(output)
|
||||
}
|
||||
|
||||
fn inject_input_data(
|
||||
mut ctx: impl AsContextMut,
|
||||
instance: &InstanceWrapper,
|
||||
allocator: &mut FreeingBumpHeapAllocator,
|
||||
data: &[u8],
|
||||
) -> Result<(Pointer<u8>, WordSize)> {
|
||||
let data_len = data.len() as WordSize;
|
||||
let data_ptr = instance.allocate(allocator, data_len)?;
|
||||
instance.write_memory_from(data_ptr, data)?;
|
||||
let data_ptr = instance.allocate(&mut ctx, allocator, data_len)?;
|
||||
instance.write_memory_from(ctx, data_ptr, data)?;
|
||||
Ok((data_ptr, data_len))
|
||||
}
|
||||
|
||||
fn extract_output_data(
|
||||
ctx: impl AsContext,
|
||||
instance: &InstanceWrapper,
|
||||
output_ptr: u32,
|
||||
output_len: u32,
|
||||
) -> Result<Vec<u8>> {
|
||||
let mut output = vec![0; output_len as usize];
|
||||
instance.read_memory_into(Pointer::new(output_ptr), &mut output)?;
|
||||
instance.read_memory_into(ctx, Pointer::new(output_ptr), &mut output)?;
|
||||
Ok(output)
|
||||
}
|
||||
|
||||
@@ -1,45 +0,0 @@
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Copyright (C) 2020-2021 Parity Technologies (UK) Ltd.
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
|
||||
|
||||
// This program 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.
|
||||
|
||||
// This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::host::{HostContext, HostState};
|
||||
|
||||
scoped_tls::scoped_thread_local!(static HOST_STATE: HostState);
|
||||
|
||||
/// Provide `HostState` for the runtime method call and execute the given function `f`.
|
||||
///
|
||||
/// During the execution of the provided function `with_context` will be callable.
|
||||
pub fn with_initialized_state<R, F>(s: &HostState, f: F) -> R
|
||||
where
|
||||
F: FnOnce() -> R,
|
||||
{
|
||||
HOST_STATE.set(s, f)
|
||||
}
|
||||
|
||||
/// Create a `HostContext` from the contained `HostState` and execute the given function `f`.
|
||||
///
|
||||
/// This function is only callable within closure passed to `init_state`. Otherwise, the passed
|
||||
/// context will be `None`.
|
||||
pub fn with_context<R, F>(f: F) -> R
|
||||
where
|
||||
F: FnOnce(Option<HostContext>) -> R,
|
||||
{
|
||||
if !HOST_STATE.is_set() {
|
||||
return f(None)
|
||||
}
|
||||
HOST_STATE.with(|state| f(Some(state.materialize())))
|
||||
}
|
||||
@@ -116,7 +116,7 @@ fn test_nan_canonicalization() {
|
||||
builder.build()
|
||||
};
|
||||
|
||||
let instance = runtime.new_instance().expect("failed to instantiate a runtime");
|
||||
let mut instance = runtime.new_instance().expect("failed to instantiate a runtime");
|
||||
|
||||
/// A NaN with canonical payload bits.
|
||||
const CANONICAL_NAN_BITS: u32 = 0x7fc00000;
|
||||
@@ -159,7 +159,7 @@ fn test_stack_depth_reaching() {
|
||||
builder.deterministic_stack(true);
|
||||
builder.build()
|
||||
};
|
||||
let instance = runtime.new_instance().expect("failed to instantiate a runtime");
|
||||
let mut instance = runtime.new_instance().expect("failed to instantiate a runtime");
|
||||
|
||||
let err = instance.call_export("test-many-locals", &[]).unwrap_err();
|
||||
|
||||
@@ -180,7 +180,7 @@ fn test_max_memory_pages() {
|
||||
builder.max_memory_pages(max_memory_pages);
|
||||
builder.build()
|
||||
};
|
||||
let instance = runtime.new_instance()?;
|
||||
let mut instance = runtime.new_instance()?;
|
||||
let _ = instance.call_export("main", &[])?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user