diff --git a/lib/clif-backend/Cargo.toml b/lib/clif-backend/Cargo.toml index 3ac5105cf..5e68f96af 100644 --- a/lib/clif-backend/Cargo.toml +++ b/lib/clif-backend/Cargo.toml @@ -8,7 +8,7 @@ repository = "https://github.com/wasmerio/wasmer" edition = "2018" [dependencies] -wasmer-runtime-core = { path = "../runtime-core", version = "0.2.0" } +wasmer-runtime-core = { path = "../runtime-core", version = "0.2.1" } cranelift-native = "0.26.0" cranelift-codegen = "0.26.0" cranelift-entity = "0.26.0" @@ -37,3 +37,4 @@ wasmer-win-exception-handler = { path = "../win-exception-handler", version = "0 [features] debug = ["wasmer-runtime-core/debug"] +vfs = ["wasmer-runtime-core/vfs"] diff --git a/lib/clif-backend/src/module.rs b/lib/clif-backend/src/module.rs index 9e934ee0a..a0929e03b 100644 --- a/lib/clif-backend/src/module.rs +++ b/lib/clif-backend/src/module.rs @@ -50,6 +50,9 @@ impl Module { namespace_table: StringTable::new(), name_table: StringTable::new(), + + #[cfg(feature = "vfs")] + custom_sections: HashMap::new(), }, } } diff --git a/lib/runtime-core/Cargo.toml b/lib/runtime-core/Cargo.toml index c6ac22f40..6ba8c0e56 100644 --- a/lib/runtime-core/Cargo.toml +++ b/lib/runtime-core/Cargo.toml @@ -45,4 +45,4 @@ field-offset = "0.1.1" [features] debug = [] - +vfs = [] diff --git a/lib/runtime-core/src/error.rs b/lib/runtime-core/src/error.rs index 9e99c2833..63297554f 100644 --- a/lib/runtime-core/src/error.rs +++ b/lib/runtime-core/src/error.rs @@ -8,6 +8,8 @@ pub type LinkResult = std::result::Result>; pub type RuntimeResult = std::result::Result; pub type CallResult = std::result::Result; pub type ResolveResult = std::result::Result; +#[cfg(feature = "vfs")] +pub type ParseResult = std::result::Result; /// This is returned when the chosen compiler is unable to /// successfully compile the provided webassembly module into @@ -445,3 +447,16 @@ impl Into for MemoryProtectionError { GrowError::CouldNotProtectMemory(self) } } + +#[cfg(feature = "vfs")] +#[derive(Debug)] +pub enum ParseError { + BinaryReadError, +} + +#[cfg(feature = "vfs")] +impl From for ParseError { + fn from(_: wasmparser::BinaryReaderError) -> Self { + ParseError::BinaryReadError + } +} diff --git a/lib/runtime-core/src/lib.rs b/lib/runtime-core/src/lib.rs index 663e17534..83b9e110b 100644 --- a/lib/runtime-core/src/lib.rs +++ b/lib/runtime-core/src/lib.rs @@ -67,9 +67,16 @@ pub fn compile_with( compiler: &dyn backend::Compiler, ) -> CompileResult { let token = backend::Token::generate(); - compiler - .compile(wasm, token) - .map(|inner| module::Module::new(Arc::new(inner))) + compiler.compile(wasm, token).map(|inner| { + #[cfg(feature = "vfs")] + let inner = { + let mut inner = inner; + let inner_info: &mut crate::module::ModuleInfo = &mut inner.info; + inner_info.import_custom_sections(wasm).unwrap(); + inner + }; + module::Module::new(Arc::new(inner)) + }) } /// Perform validation as defined by the diff --git a/lib/runtime-core/src/module.rs b/lib/runtime-core/src/module.rs index 212c197c3..5bf2df172 100644 --- a/lib/runtime-core/src/module.rs +++ b/lib/runtime-core/src/module.rs @@ -56,6 +56,28 @@ pub struct ModuleInfo { pub namespace_table: StringTable, pub name_table: StringTable, + + #[cfg(feature = "vfs")] + pub custom_sections: HashMap>, +} + +impl ModuleInfo { + #[cfg(feature = "vfs")] + pub fn import_custom_sections(&mut self, wasm: &[u8]) -> crate::error::ParseResult<()> { + let mut parser = wasmparser::ModuleReader::new(wasm)?; + while !parser.eof() { + let section = parser.read()?; + if let wasmparser::SectionCode::Custom { name, kind: _ } = section.code { + let mut reader = section.get_binary_reader(); + let len = reader.bytes_remaining(); + let bytes = reader.read_bytes(len)?; + let data = bytes.to_vec(); + let name = String::from_utf8_lossy(name).to_string(); + self.custom_sections.insert(name, data); + } + } + Ok(()) + } } /// A compiled WebAssembly module.