From 3e3b557d3f65ee3897791bc2321e1e2ed63d3cb0 Mon Sep 17 00:00:00 2001 From: Matt Gabrenya Date: Sat, 7 Sep 2024 11:42:03 -0700 Subject: [PATCH 01/19] chore: remove unused function --- crates/host/src/module/mod.rs | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/crates/host/src/module/mod.rs b/crates/host/src/module/mod.rs index ee83528c..c7a9aa76 100644 --- a/crates/host/src/module/mod.rs +++ b/crates/host/src/module/mod.rs @@ -385,16 +385,6 @@ impl ModuleCache { } } -/// Configuration of a Target for wasmer for iOS -pub fn wasmer_ios_target() -> Target { - // use what I see in - // platform ios headless example - // https://github.com/wasmerio/wasmer/blob/447c2e3a152438db67be9ef649327fabcad6f5b8/examples/platform_ios_headless.rs#L38-L53 - let triple = Triple::from_str("aarch64-apple-ios").unwrap(); - let cpu_feature = CpuFeature::set(); - Target::new(triple, cpu_feature) -} - #[cfg(test)] pub mod tests { use crate::module::{CacheKey, ModuleCache, PlruCache}; From baedff69b5f553de030b2b7a0a6b8f344c7f248a Mon Sep 17 00:00:00 2001 From: Matt Gabrenya Date: Sat, 7 Sep 2024 11:45:20 -0700 Subject: [PATCH 02/19] refactor: different api for interfacing with precompiled & seriai;lzed modules and more clarifying comments --- crates/host/src/module/wasmer_sys.rs | 31 +++++++++++++++------------ crates/host/src/module/wasmer_wamr.rs | 22 ++++++++++++------- 2 files changed, 31 insertions(+), 22 deletions(-) diff --git a/crates/host/src/module/wasmer_sys.rs b/crates/host/src/module/wasmer_sys.rs index 2778e918..d0416db6 100644 --- a/crates/host/src/module/wasmer_sys.rs +++ b/crates/host/src/module/wasmer_sys.rs @@ -49,18 +49,21 @@ pub fn make_runtime_engine() -> Engine { Engine::headless() } -/// Take WASM binary and prepare a wasmer Module suitable for iOS -pub fn build_ios_module(wasm: &[u8]) -> Result { - info!( - "Found wasm and was instructed to serialize it for ios in wasmer format, doing so now..." - ); - let compiler_engine = make_engine(); - let store = Store::new(compiler_engine); - Module::from_binary(&store, wasm) -} +pub struct PreCompiledSerializedModule {} -/// Deserialize a previously compiled module for iOS from a file. -pub fn get_ios_module_from_file(path: &Path) -> Result { - let engine = Engine::headless(); - unsafe { Module::deserialize_from_file(&engine, path) } -} +impl PreCompiledSerializedModule { + /// Compile a wasm binary, serialize it with wasmer's serializtion format, and write to a file. + /// This file can later be used for contexts where JIT compilation is not possible (iOS for example). + pub fn write(wasm: &[u8], path: PathBuf) -> Result<(), CompileError> { + let compiler_engine = make_engine(); + let store = Store::new(compiler_engine); + Module::from_binary(&store, wasm)?.serialize_to_file(path.clone())?; + Ok(()) + } + + /// Deserialize a previously precompiled and serialized module + pub fn read(path: &Path) -> Result { + let engine = Engine::headless(); + unsafe { Module::deserialize_from_file(&engine, path) } + } +} \ No newline at end of file diff --git a/crates/host/src/module/wasmer_wamr.rs b/crates/host/src/module/wasmer_wamr.rs index 4852b240..049bb3a8 100644 --- a/crates/host/src/module/wasmer_wamr.rs +++ b/crates/host/src/module/wasmer_wamr.rs @@ -14,13 +14,19 @@ pub fn make_engine() -> Engine { pub fn make_runtime_engine() -> Engine { Engine::default() } +pub struct PreCompiledSerializedModule {} -/// Take WASM binary and prepare a wasmer Module suitable for iOS -pub fn build_ios_module(_wasm: &[u8]) -> Result { - unimplemented!("The feature flag 'wasmer_sys' must be enabled to support compiling wasm"); -} +impl PreCompiledSerializedModule { + /// Compile a wasm binary, serialize it with wasmer's serializtion format, and write to a file. + /// This file can later be used for contexts where JIT compilation is not possible (iOS for example). + pub fn write(wasm: &[u8], path: PathBuf) -> Result<(), CompileError> { + unimplemented!("The feature flag 'wasmer_sys' must be enabled to support compiling wasm"); + } -/// Deserialize a previously compiled module for iOS from a file. -pub fn get_ios_module_from_file(_path: &Path) -> Result { - unimplemented!("The feature flag 'wasmer_sys' must be enabled to support compiling wasm"); -} + /// Deserialize a previously precompiled and serialized module. While technically the `wasmer_wamr` feature flag + /// can support deserializing a pre-compiled and serialized module, it doesn't make sense to use it, since it would be + /// executed by an engine which will use an interpreter to anyway. + pub fn read(path: &Path) -> Result { + unimplemented!("The feature flag 'wasmer_sys' must be enabled to support compiling wasm"); + } +} \ No newline at end of file From a443063757c8ae1dcdb5cdf0a50917dbe8886131 Mon Sep 17 00:00:00 2001 From: Matt Gabrenya Date: Sat, 7 Sep 2024 11:52:53 -0700 Subject: [PATCH 03/19] chore: better comment --- crates/host/src/module/wasmer_wamr.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/host/src/module/wasmer_wamr.rs b/crates/host/src/module/wasmer_wamr.rs index 049bb3a8..fd2b8230 100644 --- a/crates/host/src/module/wasmer_wamr.rs +++ b/crates/host/src/module/wasmer_wamr.rs @@ -23,9 +23,9 @@ impl PreCompiledSerializedModule { unimplemented!("The feature flag 'wasmer_sys' must be enabled to support compiling wasm"); } - /// Deserialize a previously precompiled and serialized module. While technically the `wasmer_wamr` feature flag - /// can support deserializing a pre-compiled and serialized module, it doesn't make sense to use it, since it would be - /// executed by an engine which will use an interpreter to anyway. + /// Deserialize a previously precompiled and serialized module. + /// Even though the `wasmer_wamr` feature flag supports deserializing a pre-compiled and serialized module, + /// it doesn't make sense to use a pre-compiled module as it would be executed by the interpreter engine anyway. pub fn read(path: &Path) -> Result { unimplemented!("The feature flag 'wasmer_sys' must be enabled to support compiling wasm"); } From 7b6d3e78982311f1925b710016e305cb3feb5549 Mon Sep 17 00:00:00 2001 From: Matt Gabrenya Date: Sat, 7 Sep 2024 11:58:24 -0700 Subject: [PATCH 04/19] chore: api change --- crates/host/src/module/wasmer_sys.rs | 29 ++++++++++++--------------- crates/host/src/module/wasmer_wamr.rs | 25 ++++++++++------------- 2 files changed, 24 insertions(+), 30 deletions(-) diff --git a/crates/host/src/module/wasmer_sys.rs b/crates/host/src/module/wasmer_sys.rs index d0416db6..f2008e6c 100644 --- a/crates/host/src/module/wasmer_sys.rs +++ b/crates/host/src/module/wasmer_sys.rs @@ -49,21 +49,18 @@ pub fn make_runtime_engine() -> Engine { Engine::headless() } -pub struct PreCompiledSerializedModule {} -impl PreCompiledSerializedModule { - /// Compile a wasm binary, serialize it with wasmer's serializtion format, and write to a file. - /// This file can later be used for contexts where JIT compilation is not possible (iOS for example). - pub fn write(wasm: &[u8], path: PathBuf) -> Result<(), CompileError> { - let compiler_engine = make_engine(); - let store = Store::new(compiler_engine); - Module::from_binary(&store, wasm)?.serialize_to_file(path.clone())?; - Ok(()) - } +/// Compile a wasm binary, serialize it with wasmer's serializtion format, and write to a file. +/// This file can later be used for contexts where JIT compilation is not possible (iOS for example). +pub fn write_precompiled_serialized_module_to_file() -> Result<(), CompileError> { + let compiler_engine = make_engine(); + let store = Store::new(compiler_engine); + Module::from_binary(&store, wasm)?.serialize_to_file(path.clone())?; + Ok(()) +} - /// Deserialize a previously precompiled and serialized module - pub fn read(path: &Path) -> Result { - let engine = Engine::headless(); - unsafe { Module::deserialize_from_file(&engine, path) } - } -} \ No newline at end of file +/// Deserialize a previously precompiled and serialized module +pub fn read_precompiled_serialized_module_from_file() -> Result { + let engine = Engine::headless(); + unsafe { Module::deserialize_from_file(&engine, path) } +} diff --git a/crates/host/src/module/wasmer_wamr.rs b/crates/host/src/module/wasmer_wamr.rs index fd2b8230..b5ffbcc1 100644 --- a/crates/host/src/module/wasmer_wamr.rs +++ b/crates/host/src/module/wasmer_wamr.rs @@ -14,19 +14,16 @@ pub fn make_engine() -> Engine { pub fn make_runtime_engine() -> Engine { Engine::default() } -pub struct PreCompiledSerializedModule {} -impl PreCompiledSerializedModule { - /// Compile a wasm binary, serialize it with wasmer's serializtion format, and write to a file. - /// This file can later be used for contexts where JIT compilation is not possible (iOS for example). - pub fn write(wasm: &[u8], path: PathBuf) -> Result<(), CompileError> { - unimplemented!("The feature flag 'wasmer_sys' must be enabled to support compiling wasm"); - } +/// Compile a wasm binary, serialize it with wasmer's serializtion format, and write to a file. +/// This file can later be used for contexts where JIT compilation is not possible (iOS for example). +pub fn write_precompiled_serialized_module_to_file() -> Result<(), CompileError> { + unimplemented!("The feature flag 'wasmer_sys' must be enabled to support compiling wasm"); +} - /// Deserialize a previously precompiled and serialized module. - /// Even though the `wasmer_wamr` feature flag supports deserializing a pre-compiled and serialized module, - /// it doesn't make sense to use a pre-compiled module as it would be executed by the interpreter engine anyway. - pub fn read(path: &Path) -> Result { - unimplemented!("The feature flag 'wasmer_sys' must be enabled to support compiling wasm"); - } -} \ No newline at end of file +/// Deserialize a previously precompiled and serialized module. +/// Even though the `wasmer_wamr` feature flag supports deserializing a pre-compiled and serialized module, +/// it doesn't make sense to use a pre-compiled module as it would be executed by the interpreter engine anyway. +pub fn read_precompiled_serialized_module_from_file() -> Result { + unimplemented!("The feature flag 'wasmer_sys' must be enabled to support compiling wasm"); +} From 8b7545107de96d656a555c466130ca70c3473ec9 Mon Sep 17 00:00:00 2001 From: Matt Gabrenya Date: Sat, 7 Sep 2024 12:08:23 -0700 Subject: [PATCH 05/19] fix: fn args --- crates/host/src/module/wasmer_sys.rs | 5 ++--- crates/host/src/module/wasmer_wamr.rs | 4 ++-- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/crates/host/src/module/wasmer_sys.rs b/crates/host/src/module/wasmer_sys.rs index f2008e6c..32dc88e7 100644 --- a/crates/host/src/module/wasmer_sys.rs +++ b/crates/host/src/module/wasmer_sys.rs @@ -49,10 +49,9 @@ pub fn make_runtime_engine() -> Engine { Engine::headless() } - /// Compile a wasm binary, serialize it with wasmer's serializtion format, and write to a file. /// This file can later be used for contexts where JIT compilation is not possible (iOS for example). -pub fn write_precompiled_serialized_module_to_file() -> Result<(), CompileError> { +pub fn write_precompiled_serialized_module_to_file(wasm: &[u8], path: PathBuf) -> Result<(), CompileError> { let compiler_engine = make_engine(); let store = Store::new(compiler_engine); Module::from_binary(&store, wasm)?.serialize_to_file(path.clone())?; @@ -60,7 +59,7 @@ pub fn write_precompiled_serialized_module_to_file() -> Result<(), CompileError> } /// Deserialize a previously precompiled and serialized module -pub fn read_precompiled_serialized_module_from_file() -> Result { +pub fn read_precompiled_serialized_module_from_file(path: &Path) -> Result { let engine = Engine::headless(); unsafe { Module::deserialize_from_file(&engine, path) } } diff --git a/crates/host/src/module/wasmer_wamr.rs b/crates/host/src/module/wasmer_wamr.rs index b5ffbcc1..644d3c64 100644 --- a/crates/host/src/module/wasmer_wamr.rs +++ b/crates/host/src/module/wasmer_wamr.rs @@ -17,13 +17,13 @@ pub fn make_runtime_engine() -> Engine { /// Compile a wasm binary, serialize it with wasmer's serializtion format, and write to a file. /// This file can later be used for contexts where JIT compilation is not possible (iOS for example). -pub fn write_precompiled_serialized_module_to_file() -> Result<(), CompileError> { +pub fn write_precompiled_serialized_module_to_file(wasm: &[u8], path: PathBuf) -> Result<(), CompileError> { unimplemented!("The feature flag 'wasmer_sys' must be enabled to support compiling wasm"); } /// Deserialize a previously precompiled and serialized module. /// Even though the `wasmer_wamr` feature flag supports deserializing a pre-compiled and serialized module, /// it doesn't make sense to use a pre-compiled module as it would be executed by the interpreter engine anyway. -pub fn read_precompiled_serialized_module_from_file() -> Result { +pub fn read_precompiled_serialized_module_from_file(path: &Path) -> Result { unimplemented!("The feature flag 'wasmer_sys' must be enabled to support compiling wasm"); } From 34cdcc8d4de87d1a242271b91f1a24a42fed7146 Mon Sep 17 00:00:00 2001 From: Matt Gabrenya Date: Sat, 7 Sep 2024 12:19:12 -0700 Subject: [PATCH 06/19] fix: error & clippy --- crates/host/src/error.rs | 11 +++++++++++ crates/host/src/module/mod.rs | 4 ---- crates/host/src/module/wasmer_sys.rs | 6 +++--- crates/host/src/module/wasmer_wamr.rs | 4 ++-- 4 files changed, 16 insertions(+), 9 deletions(-) diff --git a/crates/host/src/error.rs b/crates/host/src/error.rs index dfe1dcc4..83e4ab42 100644 --- a/crates/host/src/error.rs +++ b/crates/host/src/error.rs @@ -1,6 +1,7 @@ use holochain_wasmer_common::WasmError; use serde::{Deserialize, Serialize}; use thiserror::Error; +use wasmer::{CompileError, SerializeError}; /// Wraps a WasmErrorInner with a file and line number. /// The easiest way to generate this is with the `wasm_error!` macro that will @@ -41,3 +42,13 @@ macro_rules! wasm_host_error { $crate::wasm_host_error!(std::format!($($arg)*)) }}; } + + +#[derive(Debug, Error)] +pub enum PreCompiledSerilializedModuleError { + #[error(transparent)] + CompileError(#[from] CompileError), + + #[error(transparent)] + SerializeError(#[from] SerializeError) +} diff --git a/crates/host/src/module/mod.rs b/crates/host/src/module/mod.rs index c7a9aa76..7f47c4c6 100644 --- a/crates/host/src/module/mod.rs +++ b/crates/host/src/module/mod.rs @@ -11,15 +11,11 @@ use std::fs::File; use std::fs::OpenOptions; use std::io::Write; use std::path::PathBuf; -use std::str::FromStr; use std::sync::Arc; -use wasmer::CpuFeature; use wasmer::Engine; use wasmer::Instance; use wasmer::Module; use wasmer::Store; -use wasmer::Target; -use wasmer::Triple; #[cfg(feature = "wasmer_sys")] mod wasmer_sys; diff --git a/crates/host/src/module/wasmer_sys.rs b/crates/host/src/module/wasmer_sys.rs index 32dc88e7..ac23083c 100644 --- a/crates/host/src/module/wasmer_sys.rs +++ b/crates/host/src/module/wasmer_sys.rs @@ -1,10 +1,8 @@ use std::path::Path; use std::sync::Arc; -use tracing::info; use wasmer::sys::BaseTunables; use wasmer::sys::CompilerConfig; use wasmer::wasmparser; -use wasmer::CompileError; use wasmer::Cranelift; use wasmer::DeserializeError; use wasmer::Engine; @@ -12,6 +10,8 @@ use wasmer::Module; use wasmer::NativeEngineExt; use wasmer::Store; use wasmer_middlewares::Metering; +use std::path::PathBuf; +use crate::error::PreCompiledSerilializedModuleError; #[cfg(not(test))] /// one hundred giga ops @@ -51,7 +51,7 @@ pub fn make_runtime_engine() -> Engine { /// Compile a wasm binary, serialize it with wasmer's serializtion format, and write to a file. /// This file can later be used for contexts where JIT compilation is not possible (iOS for example). -pub fn write_precompiled_serialized_module_to_file(wasm: &[u8], path: PathBuf) -> Result<(), CompileError> { +pub fn write_precompiled_serialized_module_to_file(wasm: &[u8], path: PathBuf) -> Result<(), PreCompiledSerilializedModuleError> { let compiler_engine = make_engine(); let store = Store::new(compiler_engine); Module::from_binary(&store, wasm)?.serialize_to_file(path.clone())?; diff --git a/crates/host/src/module/wasmer_wamr.rs b/crates/host/src/module/wasmer_wamr.rs index 644d3c64..3e7de9c6 100644 --- a/crates/host/src/module/wasmer_wamr.rs +++ b/crates/host/src/module/wasmer_wamr.rs @@ -1,8 +1,8 @@ -use std::path::Path; use wasmer::CompileError; use wasmer::DeserializeError; use wasmer::Engine; use wasmer::Module; +use std::path::PathBuf; /// Generate an engine with a wasm interpreter /// The interpreter used (wasm micro runtime) does not support metering @@ -17,7 +17,7 @@ pub fn make_runtime_engine() -> Engine { /// Compile a wasm binary, serialize it with wasmer's serializtion format, and write to a file. /// This file can later be used for contexts where JIT compilation is not possible (iOS for example). -pub fn write_precompiled_serialized_module_to_file(wasm: &[u8], path: PathBuf) -> Result<(), CompileError> { +pub fn write_precompiled_serialized_module_to_file(wasm: &[u8], path: PathBuf) -> Result<(), PreCompiledSerilializedModuleError> { unimplemented!("The feature flag 'wasmer_sys' must be enabled to support compiling wasm"); } From 2b127765038cb8c68b3d0ce5f588a82e6517c3fb Mon Sep 17 00:00:00 2001 From: Matt Gabrenya Date: Sat, 7 Sep 2024 12:19:25 -0700 Subject: [PATCH 07/19] chore: fmt --- crates/host/src/error.rs | 3 +-- crates/host/src/module/wasmer_sys.rs | 13 +++++++++---- crates/host/src/module/wasmer_wamr.rs | 13 +++++++++---- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/crates/host/src/error.rs b/crates/host/src/error.rs index 83e4ab42..a4981d29 100644 --- a/crates/host/src/error.rs +++ b/crates/host/src/error.rs @@ -43,12 +43,11 @@ macro_rules! wasm_host_error { }}; } - #[derive(Debug, Error)] pub enum PreCompiledSerilializedModuleError { #[error(transparent)] CompileError(#[from] CompileError), #[error(transparent)] - SerializeError(#[from] SerializeError) + SerializeError(#[from] SerializeError), } diff --git a/crates/host/src/module/wasmer_sys.rs b/crates/host/src/module/wasmer_sys.rs index ac23083c..173a1cb2 100644 --- a/crates/host/src/module/wasmer_sys.rs +++ b/crates/host/src/module/wasmer_sys.rs @@ -1,4 +1,6 @@ +use crate::error::PreCompiledSerilializedModuleError; use std::path::Path; +use std::path::PathBuf; use std::sync::Arc; use wasmer::sys::BaseTunables; use wasmer::sys::CompilerConfig; @@ -10,8 +12,6 @@ use wasmer::Module; use wasmer::NativeEngineExt; use wasmer::Store; use wasmer_middlewares::Metering; -use std::path::PathBuf; -use crate::error::PreCompiledSerilializedModuleError; #[cfg(not(test))] /// one hundred giga ops @@ -51,7 +51,10 @@ pub fn make_runtime_engine() -> Engine { /// Compile a wasm binary, serialize it with wasmer's serializtion format, and write to a file. /// This file can later be used for contexts where JIT compilation is not possible (iOS for example). -pub fn write_precompiled_serialized_module_to_file(wasm: &[u8], path: PathBuf) -> Result<(), PreCompiledSerilializedModuleError> { +pub fn write_precompiled_serialized_module_to_file( + wasm: &[u8], + path: PathBuf, +) -> Result<(), PreCompiledSerilializedModuleError> { let compiler_engine = make_engine(); let store = Store::new(compiler_engine); Module::from_binary(&store, wasm)?.serialize_to_file(path.clone())?; @@ -59,7 +62,9 @@ pub fn write_precompiled_serialized_module_to_file(wasm: &[u8], path: PathBuf) - } /// Deserialize a previously precompiled and serialized module -pub fn read_precompiled_serialized_module_from_file(path: &Path) -> Result { +pub fn read_precompiled_serialized_module_from_file( + path: &Path, +) -> Result { let engine = Engine::headless(); unsafe { Module::deserialize_from_file(&engine, path) } } diff --git a/crates/host/src/module/wasmer_wamr.rs b/crates/host/src/module/wasmer_wamr.rs index 3e7de9c6..08175435 100644 --- a/crates/host/src/module/wasmer_wamr.rs +++ b/crates/host/src/module/wasmer_wamr.rs @@ -1,8 +1,8 @@ +use std::path::PathBuf; use wasmer::CompileError; use wasmer::DeserializeError; use wasmer::Engine; use wasmer::Module; -use std::path::PathBuf; /// Generate an engine with a wasm interpreter /// The interpreter used (wasm micro runtime) does not support metering @@ -17,13 +17,18 @@ pub fn make_runtime_engine() -> Engine { /// Compile a wasm binary, serialize it with wasmer's serializtion format, and write to a file. /// This file can later be used for contexts where JIT compilation is not possible (iOS for example). -pub fn write_precompiled_serialized_module_to_file(wasm: &[u8], path: PathBuf) -> Result<(), PreCompiledSerilializedModuleError> { +pub fn write_precompiled_serialized_module_to_file( + wasm: &[u8], + path: PathBuf, +) -> Result<(), PreCompiledSerilializedModuleError> { unimplemented!("The feature flag 'wasmer_sys' must be enabled to support compiling wasm"); } -/// Deserialize a previously precompiled and serialized module. +/// Deserialize a previously precompiled and serialized module. /// Even though the `wasmer_wamr` feature flag supports deserializing a pre-compiled and serialized module, /// it doesn't make sense to use a pre-compiled module as it would be executed by the interpreter engine anyway. -pub fn read_precompiled_serialized_module_from_file(path: &Path) -> Result { +pub fn read_precompiled_serialized_module_from_file( + path: &Path, +) -> Result { unimplemented!("The feature flag 'wasmer_sys' must be enabled to support compiling wasm"); } From fc36003d1caa2e2441f545ea374925f8db18ee78 Mon Sep 17 00:00:00 2001 From: Matt Gabrenya Date: Sat, 7 Sep 2024 13:03:06 -0700 Subject: [PATCH 08/19] chore: clippy & fmt --- crates/host/src/module/wasmer_wamr.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/crates/host/src/module/wasmer_wamr.rs b/crates/host/src/module/wasmer_wamr.rs index 08175435..9b6256e5 100644 --- a/crates/host/src/module/wasmer_wamr.rs +++ b/crates/host/src/module/wasmer_wamr.rs @@ -1,5 +1,6 @@ +use crate::error::PreCompiledSerilializedModuleError; +use std::path::Path; use std::path::PathBuf; -use wasmer::CompileError; use wasmer::DeserializeError; use wasmer::Engine; use wasmer::Module; @@ -18,8 +19,8 @@ pub fn make_runtime_engine() -> Engine { /// Compile a wasm binary, serialize it with wasmer's serializtion format, and write to a file. /// This file can later be used for contexts where JIT compilation is not possible (iOS for example). pub fn write_precompiled_serialized_module_to_file( - wasm: &[u8], - path: PathBuf, + _wasm: &[u8], + _path: PathBuf, ) -> Result<(), PreCompiledSerilializedModuleError> { unimplemented!("The feature flag 'wasmer_sys' must be enabled to support compiling wasm"); } @@ -28,7 +29,7 @@ pub fn write_precompiled_serialized_module_to_file( /// Even though the `wasmer_wamr` feature flag supports deserializing a pre-compiled and serialized module, /// it doesn't make sense to use a pre-compiled module as it would be executed by the interpreter engine anyway. pub fn read_precompiled_serialized_module_from_file( - path: &Path, + _path: &Path, ) -> Result { unimplemented!("The feature flag 'wasmer_sys' must be enabled to support compiling wasm"); } From 66c12bcbeafc14dfc0645254231fdb24b051baff Mon Sep 17 00:00:00 2001 From: Matt Gabrenya Date: Sat, 7 Sep 2024 13:09:52 -0700 Subject: [PATCH 09/19] chore: changelog --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index fc8dca17..b5fb0115 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,9 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm ## [Unreleased] +- **BREAKING CHANGE:** Remove unused function `wasmer_ios_target` +- **BREAKING CHANGE:** Rename functions `build_ios_module` to `write_precompiled_serialized_module_to_file` and `get_ios_module_from_file` to `read_precompiled_serialized_module_from_file` to clarify that they are not an ios-specific. + ## [0.0.93] - 2024-04-24 ### Changed From 324457d64ace2b768ff3a7e59b467eb2a15733ff Mon Sep 17 00:00:00 2001 From: Matt Gabrenya Date: Sat, 7 Sep 2024 14:17:14 -0700 Subject: [PATCH 10/19] test: round trip precompile & serialize module --- Cargo.lock | 1 + crates/host/Cargo.toml | 3 +++ crates/host/src/module/mod.rs | 23 ++++++++++++++++++++++- 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 8c31c5cd..9c94c789 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -997,6 +997,7 @@ dependencies = [ "parking_lot", "rand", "serde", + "tempfile", "thiserror", "tracing", "wasmer", diff --git a/crates/host/Cargo.toml b/crates/host/Cargo.toml index dbd29d6a..f66f97ac 100644 --- a/crates/host/Cargo.toml +++ b/crates/host/Cargo.toml @@ -24,6 +24,9 @@ bytes = "1" hex = "0.4" thiserror = "1" +[dev-dependencies] +tempfile = "3.12.0" + [lib] name = "holochain_wasmer_host" crate-type = ["cdylib", "rlib"] diff --git a/crates/host/src/module/mod.rs b/crates/host/src/module/mod.rs index 7f47c4c6..4f74074f 100644 --- a/crates/host/src/module/mod.rs +++ b/crates/host/src/module/mod.rs @@ -383,7 +383,7 @@ impl ModuleCache { #[cfg(test)] pub mod tests { - use crate::module::{CacheKey, ModuleCache, PlruCache}; + use crate::module::{CacheKey, ModuleCache, PlruCache, write_precompiled_serialized_module_to_file, read_precompiled_serialized_module_from_file}; #[test] fn cache_test() { @@ -424,4 +424,25 @@ pub mod tests { assert_eq!(*deserialized_cached_module, *module); } } + + #[test] + #[cfg(feature = "wasmer_sys")] + fn precompiled_serialized_module_roundtrip_test() { + // simple example wasm taken from wasmer docs + // https://docs.rs/wasmer/latest/wasmer/struct.Module.html#example + let wasm: Vec = vec![ + 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x06, 0x01, 0x60, 0x01, 0x7f, + 0x01, 0x7f, 0x03, 0x02, 0x01, 0x00, 0x07, 0x0b, 0x01, 0x07, 0x61, 0x64, 0x64, 0x5f, + 0x6f, 0x6e, 0x65, 0x00, 0x00, 0x0a, 0x09, 0x01, 0x07, 0x00, 0x20, 0x00, 0x41, 0x01, + 0x6a, 0x0b, 0x00, 0x1a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x01, 0x0a, 0x01, 0x00, 0x07, + 0x61, 0x64, 0x64, 0x5f, 0x6f, 0x6e, 0x65, 0x02, 0x07, 0x01, 0x00, 0x01, 0x00, 0x02, + 0x70, 0x30, + ]; + + + let tmp = tempfile::tempdir().unwrap(); + let path = tmp.path().join("module.dylib"); + write_precompiled_serialized_module_to_file(wasm.as_slice(), path.clone()).unwrap(); + let _module = read_precompiled_serialized_module_from_file(path.as_path()).unwrap(); + } } From 74242db6a08af8d5e4bd3df5051e4f366f2ff62a Mon Sep 17 00:00:00 2001 From: Matt Gabrenya Date: Tue, 10 Sep 2024 10:04:07 -0700 Subject: [PATCH 11/19] fix: typo Co-authored-by: Jost Schulte --- crates/host/src/module/wasmer_sys.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/host/src/module/wasmer_sys.rs b/crates/host/src/module/wasmer_sys.rs index 173a1cb2..ce2fdf19 100644 --- a/crates/host/src/module/wasmer_sys.rs +++ b/crates/host/src/module/wasmer_sys.rs @@ -49,7 +49,7 @@ pub fn make_runtime_engine() -> Engine { Engine::headless() } -/// Compile a wasm binary, serialize it with wasmer's serializtion format, and write to a file. +/// Compile a wasm binary, serialize it with wasmer's serialization format, and write it to a file. /// This file can later be used for contexts where JIT compilation is not possible (iOS for example). pub fn write_precompiled_serialized_module_to_file( wasm: &[u8], From 3a238114cc8079cd963d881542f20ebb07db85e7 Mon Sep 17 00:00:00 2001 From: Matt Gabrenya Date: Tue, 10 Sep 2024 10:09:44 -0700 Subject: [PATCH 12/19] fix: typo Co-authored-by: Jost Schulte --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b5fb0115..20d504f3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm ## [Unreleased] - **BREAKING CHANGE:** Remove unused function `wasmer_ios_target` -- **BREAKING CHANGE:** Rename functions `build_ios_module` to `write_precompiled_serialized_module_to_file` and `get_ios_module_from_file` to `read_precompiled_serialized_module_from_file` to clarify that they are not an ios-specific. +- **BREAKING CHANGE:** Rename functions `build_ios_module` to `write_precompiled_serialized_module_to_file` and `get_ios_module_from_file` to `read_precompiled_serialized_module_from_file` to clarify that they are not ios-specific. ## [0.0.93] - 2024-04-24 From e3b91cf3643d116428a50931184fd1d4e96eac15 Mon Sep 17 00:00:00 2001 From: Matt Gabrenya Date: Tue, 10 Sep 2024 10:33:03 -0700 Subject: [PATCH 13/19] fix: typo Co-authored-by: Jost Schulte --- crates/host/src/module/wasmer_wamr.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/host/src/module/wasmer_wamr.rs b/crates/host/src/module/wasmer_wamr.rs index 9b6256e5..542c9035 100644 --- a/crates/host/src/module/wasmer_wamr.rs +++ b/crates/host/src/module/wasmer_wamr.rs @@ -16,7 +16,7 @@ pub fn make_runtime_engine() -> Engine { Engine::default() } -/// Compile a wasm binary, serialize it with wasmer's serializtion format, and write to a file. +/// Compile a wasm binary, serialize it with wasmer's serialization format, and write it to a file. /// This file can later be used for contexts where JIT compilation is not possible (iOS for example). pub fn write_precompiled_serialized_module_to_file( _wasm: &[u8], From 4beaeef39654147ddd3e607327a18b23a5eacd52 Mon Sep 17 00:00:00 2001 From: Matt Gabrenya Date: Tue, 10 Sep 2024 10:33:53 -0700 Subject: [PATCH 14/19] test: assert that module read from file is same as precompiled & serialized wasm bytes --- crates/host/src/module/mod.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/crates/host/src/module/mod.rs b/crates/host/src/module/mod.rs index 4f74074f..cce15b1c 100644 --- a/crates/host/src/module/mod.rs +++ b/crates/host/src/module/mod.rs @@ -439,10 +439,13 @@ pub mod tests { 0x70, 0x30, ]; - let tmp = tempfile::tempdir().unwrap(); let path = tmp.path().join("module.dylib"); + write_precompiled_serialized_module_to_file(wasm.as_slice(), path.clone()).unwrap(); - let _module = read_precompiled_serialized_module_from_file(path.as_path()).unwrap(); + let module = read_precompiled_serialized_module_from_file(path.as_path()).unwrap(); + + let module_fn = module.exports().into_iter().functions().next().unwrap(); + assert_eq!(module_fn.name(), "add_one") } } From 0cd33f82f42aef4bde057c0a84656a9798012946 Mon Sep 17 00:00:00 2001 From: Matt Gabrenya Date: Tue, 10 Sep 2024 10:35:40 -0700 Subject: [PATCH 15/19] chore: fmt --- crates/host/src/module/mod.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/crates/host/src/module/mod.rs b/crates/host/src/module/mod.rs index cce15b1c..93301dd9 100644 --- a/crates/host/src/module/mod.rs +++ b/crates/host/src/module/mod.rs @@ -383,7 +383,10 @@ impl ModuleCache { #[cfg(test)] pub mod tests { - use crate::module::{CacheKey, ModuleCache, PlruCache, write_precompiled_serialized_module_to_file, read_precompiled_serialized_module_from_file}; + use crate::module::{ + read_precompiled_serialized_module_from_file, write_precompiled_serialized_module_to_file, + CacheKey, ModuleCache, PlruCache, + }; #[test] fn cache_test() { @@ -441,10 +444,10 @@ pub mod tests { let tmp = tempfile::tempdir().unwrap(); let path = tmp.path().join("module.dylib"); - + write_precompiled_serialized_module_to_file(wasm.as_slice(), path.clone()).unwrap(); let module = read_precompiled_serialized_module_from_file(path.as_path()).unwrap(); - + let module_fn = module.exports().into_iter().functions().next().unwrap(); assert_eq!(module_fn.name(), "add_one") } From 7241f427c98e7a72e5e39a4dac9b728aa46bbcbd Mon Sep 17 00:00:00 2001 From: Matt Gabrenya Date: Wed, 11 Sep 2024 14:20:48 -0700 Subject: [PATCH 16/19] test: better assertion of module precompile & seriailize Co-authored-by: Jost Schulte --- crates/host/src/module/mod.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/crates/host/src/module/mod.rs b/crates/host/src/module/mod.rs index 93301dd9..53ccdab4 100644 --- a/crates/host/src/module/mod.rs +++ b/crates/host/src/module/mod.rs @@ -433,6 +433,8 @@ pub mod tests { fn precompiled_serialized_module_roundtrip_test() { // simple example wasm taken from wasmer docs // https://docs.rs/wasmer/latest/wasmer/struct.Module.html#example + use crate::module::make_engine; + let wasm: Vec = vec![ 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x06, 0x01, 0x60, 0x01, 0x7f, 0x01, 0x7f, 0x03, 0x02, 0x01, 0x00, 0x07, 0x0b, 0x01, 0x07, 0x61, 0x64, 0x64, 0x5f, @@ -445,10 +447,16 @@ pub mod tests { let tmp = tempfile::tempdir().unwrap(); let path = tmp.path().join("module.dylib"); + let compiler_engine = make_engine(); + let store = wasmer::Store::new(compiler_engine); + let original_module = wasmer::Module::new(&store, wasm.clone()).unwrap(); + write_precompiled_serialized_module_to_file(wasm.as_slice(), path.clone()).unwrap(); let module = read_precompiled_serialized_module_from_file(path.as_path()).unwrap(); - let module_fn = module.exports().into_iter().functions().next().unwrap(); - assert_eq!(module_fn.name(), "add_one") + assert_eq!( + module.serialize().unwrap(), + original_module.serialize().unwrap() + ); } } From f0ad4ab2e307542a3d9222ab693b539ef0cb045f Mon Sep 17 00:00:00 2001 From: Matt Gabrenya Date: Wed, 11 Sep 2024 14:22:42 -0700 Subject: [PATCH 17/19] refactor: remove unimplemented functions --- crates/host/src/module/wasmer_wamr.rs | 23 ----------------------- 1 file changed, 23 deletions(-) diff --git a/crates/host/src/module/wasmer_wamr.rs b/crates/host/src/module/wasmer_wamr.rs index 542c9035..821d6685 100644 --- a/crates/host/src/module/wasmer_wamr.rs +++ b/crates/host/src/module/wasmer_wamr.rs @@ -1,9 +1,4 @@ -use crate::error::PreCompiledSerilializedModuleError; -use std::path::Path; -use std::path::PathBuf; -use wasmer::DeserializeError; use wasmer::Engine; -use wasmer::Module; /// Generate an engine with a wasm interpreter /// The interpreter used (wasm micro runtime) does not support metering @@ -15,21 +10,3 @@ pub fn make_engine() -> Engine { pub fn make_runtime_engine() -> Engine { Engine::default() } - -/// Compile a wasm binary, serialize it with wasmer's serialization format, and write it to a file. -/// This file can later be used for contexts where JIT compilation is not possible (iOS for example). -pub fn write_precompiled_serialized_module_to_file( - _wasm: &[u8], - _path: PathBuf, -) -> Result<(), PreCompiledSerilializedModuleError> { - unimplemented!("The feature flag 'wasmer_sys' must be enabled to support compiling wasm"); -} - -/// Deserialize a previously precompiled and serialized module. -/// Even though the `wasmer_wamr` feature flag supports deserializing a pre-compiled and serialized module, -/// it doesn't make sense to use a pre-compiled module as it would be executed by the interpreter engine anyway. -pub fn read_precompiled_serialized_module_from_file( - _path: &Path, -) -> Result { - unimplemented!("The feature flag 'wasmer_sys' must be enabled to support compiling wasm"); -} From 75df8689327459f062b9145318d07781bc28e938 Mon Sep 17 00:00:00 2001 From: Matt Gabrenya Date: Wed, 11 Sep 2024 14:28:40 -0700 Subject: [PATCH 18/19] chore: rename module/mod.rs -> module.rs --- crates/host/src/{module/mod.rs => module.rs} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename crates/host/src/{module/mod.rs => module.rs} (100%) diff --git a/crates/host/src/module/mod.rs b/crates/host/src/module.rs similarity index 100% rename from crates/host/src/module/mod.rs rename to crates/host/src/module.rs From 0487795ee746771c1e157f5945114c399c7b695a Mon Sep 17 00:00:00 2001 From: Matt Gabrenya Date: Wed, 11 Sep 2024 16:16:29 -0700 Subject: [PATCH 19/19] fix: imports gated by feature flag --- crates/host/src/module.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/crates/host/src/module.rs b/crates/host/src/module.rs index 53ccdab4..9985dba3 100644 --- a/crates/host/src/module.rs +++ b/crates/host/src/module.rs @@ -383,10 +383,7 @@ impl ModuleCache { #[cfg(test)] pub mod tests { - use crate::module::{ - read_precompiled_serialized_module_from_file, write_precompiled_serialized_module_to_file, - CacheKey, ModuleCache, PlruCache, - }; + use crate::module::{CacheKey, ModuleCache, PlruCache}; #[test] fn cache_test() { @@ -433,7 +430,10 @@ pub mod tests { fn precompiled_serialized_module_roundtrip_test() { // simple example wasm taken from wasmer docs // https://docs.rs/wasmer/latest/wasmer/struct.Module.html#example - use crate::module::make_engine; + use crate::module::{ + make_engine, read_precompiled_serialized_module_from_file, + write_precompiled_serialized_module_to_file, + }; let wasm: Vec = vec![ 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x06, 0x01, 0x60, 0x01, 0x7f,