diff --git a/src/vm/module/abi.rs b/src/vm/module/abi.rs index 43c72830..cc3a0901 100644 --- a/src/vm/module/abi.rs +++ b/src/vm/module/abi.rs @@ -28,8 +28,11 @@ use wasmer_runtime::Func; /// 4. read a result from the res by reading 4 bytes as little-endian result_size /// and the read result_size bytes as the final result. /// 5. deallocate(res, strlen(sql)) to clean memory. + +pub(crate) trait ModuleABI<'a> {} + #[derive(Clone)] -pub(crate) struct ModuleABI<'a> { +pub(crate) struct ABI<'a> { // It is safe to use unwrap() while calling these functions because Option is used here // just to allow partially initialization. And all Option fields will contain Some if // invoking FCE::new has been succeed. diff --git a/src/vm/module/fce_module.rs b/src/vm/module/fce_module.rs index a10fb348..d05cc9f9 100644 --- a/src/vm/module/fce_module.rs +++ b/src/vm/module/fce_module.rs @@ -17,18 +17,18 @@ use crate::vm::config::Config; use crate::vm::errors::FCEError; use crate::vm::module::fce_result::FCEResult; -use crate::vm::module::{ModuleABI, ModuleAPI}; +use crate::vm::module::{ModuleAPI, ABI}; use sha2::digest::generic_array::GenericArray; use sha2::digest::FixedOutput; -use wasmer_runtime::{compile, func, imports, Ctx, Instance}; +use wasmer_runtime::{compile, func, imports, Ctx, Func, Instance}; use wasmer_runtime_core::import::ImportObject; use wasmer_runtime_core::memory::ptr::{Array, WasmPtr}; use wasmer_wasi::generate_import_object_for_version; pub(crate) struct FCEModule { - instance: &'static Instance, - abi: ModuleABI<'static>, + instance: Instance, + abi: ABI<'static>, } impl FCEModule { @@ -52,19 +52,46 @@ impl FCEModule { import_object.allow_missing_functions = false; let instance = compile(&wasm_bytes)?.instantiate(&import_object)?; - let instance: &'static mut Instance = Box::leak(Box::new(instance)); - let abi = ModuleABI { - allocate: Some(instance.exports.get(&config.allocate_fn_name)?), - deallocate: Some(instance.exports.get(&config.deallocate_fn_name)?), - invoke: Some(instance.exports.get(&config.invoke_fn_name)?), - store: Some(instance.exports.get(&config.store_fn_name)?), - load: Some(instance.exports.get(&config.load_fn_name)?), - }; - Ok(Self { instance, abi }) + unsafe { + #[rustfmt::skip] + let allocate = std::mem::transmute::, Func<'static, i32, i32>>( + instance.exports.get(&config.allocate_fn_name)? + ); + + #[rustfmt::skip] + let deallocate = std::mem::transmute::, Func<'static, (i32, i32)>>( + instance.exports.get(&config.deallocate_fn_name)? + ); + + #[rustfmt::skip] + let invoke = std::mem::transmute::, Func<'static, (i32, i32), i32>, >( + instance.exports.get(&config.invoke_fn_name)? + ); + + #[rustfmt::skip] + let store = std::mem::transmute::, Func<'static, _, _>>( + instance.exports.get(&config.store_fn_name)?, + ); + + #[rustfmt::skip] + let load = std::mem::transmute::, Func<'static, _, _>>( + instance.exports.get(&config.load_fn_name)?, + ); + + let abi = ABI { + allocate: Some(allocate), + deallocate: Some(deallocate), + invoke: Some(invoke), + store: Some(store), + load: Some(load), + }; + + Ok(Self { instance, abi }) + } } - pub fn get_abi(&self) -> &ModuleABI<'static> { + pub fn get_abi(&self) -> &ABI<'static> { &self.abi } diff --git a/src/vm/module/mod.rs b/src/vm/module/mod.rs index 92b744de..b8b3f5ec 100644 --- a/src/vm/module/mod.rs +++ b/src/vm/module/mod.rs @@ -19,6 +19,6 @@ mod api; mod fce_module; pub mod fce_result; -pub(crate) use abi::ModuleABI; +pub(crate) use abi::ABI; pub(crate) use api::ModuleAPI; pub(crate) use fce_module::FCEModule;