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:
Bastian Köcher
2021-09-29 14:30:46 +02:00
committed by GitHub
parent e64693933f
commit 2deed49706
19 changed files with 451 additions and 430 deletions
@@ -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)))