parse and store custom sections from wasm, guarded by vfs feature flag

This commit is contained in:
Mackenzie Clark
2019-03-12 10:45:44 -07:00
parent f014a05304
commit b9c3a49f3c
6 changed files with 53 additions and 5 deletions

View File

@ -8,7 +8,7 @@ repository = "https://github.com/wasmerio/wasmer"
edition = "2018" edition = "2018"
[dependencies] [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-native = "0.26.0"
cranelift-codegen = "0.26.0" cranelift-codegen = "0.26.0"
cranelift-entity = "0.26.0" cranelift-entity = "0.26.0"
@ -37,3 +37,4 @@ wasmer-win-exception-handler = { path = "../win-exception-handler", version = "0
[features] [features]
debug = ["wasmer-runtime-core/debug"] debug = ["wasmer-runtime-core/debug"]
vfs = ["wasmer-runtime-core/vfs"]

View File

@ -50,6 +50,9 @@ impl Module {
namespace_table: StringTable::new(), namespace_table: StringTable::new(),
name_table: StringTable::new(), name_table: StringTable::new(),
#[cfg(feature = "vfs")]
custom_sections: HashMap::new(),
}, },
} }
} }

View File

@ -45,4 +45,4 @@ field-offset = "0.1.1"
[features] [features]
debug = [] debug = []
vfs = []

View File

@ -8,6 +8,8 @@ pub type LinkResult<T> = std::result::Result<T, Vec<LinkError>>;
pub type RuntimeResult<T> = std::result::Result<T, RuntimeError>; pub type RuntimeResult<T> = std::result::Result<T, RuntimeError>;
pub type CallResult<T> = std::result::Result<T, CallError>; pub type CallResult<T> = std::result::Result<T, CallError>;
pub type ResolveResult<T> = std::result::Result<T, ResolveError>; pub type ResolveResult<T> = std::result::Result<T, ResolveError>;
#[cfg(feature = "vfs")]
pub type ParseResult<T> = std::result::Result<T, ParseError>;
/// This is returned when the chosen compiler is unable to /// This is returned when the chosen compiler is unable to
/// successfully compile the provided webassembly module into /// successfully compile the provided webassembly module into
@ -445,3 +447,16 @@ impl Into<GrowError> for MemoryProtectionError {
GrowError::CouldNotProtectMemory(self) GrowError::CouldNotProtectMemory(self)
} }
} }
#[cfg(feature = "vfs")]
#[derive(Debug)]
pub enum ParseError {
BinaryReadError,
}
#[cfg(feature = "vfs")]
impl From<wasmparser::BinaryReaderError> for ParseError {
fn from(_: wasmparser::BinaryReaderError) -> Self {
ParseError::BinaryReadError
}
}

View File

@ -67,9 +67,16 @@ pub fn compile_with(
compiler: &dyn backend::Compiler, compiler: &dyn backend::Compiler,
) -> CompileResult<module::Module> { ) -> CompileResult<module::Module> {
let token = backend::Token::generate(); let token = backend::Token::generate();
compiler compiler.compile(wasm, token).map(|inner| {
.compile(wasm, token) #[cfg(feature = "vfs")]
.map(|inner| module::Module::new(Arc::new(inner))) 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 /// Perform validation as defined by the

View File

@ -56,6 +56,28 @@ pub struct ModuleInfo {
pub namespace_table: StringTable<NamespaceIndex>, pub namespace_table: StringTable<NamespaceIndex>,
pub name_table: StringTable<NameIndex>, pub name_table: StringTable<NameIndex>,
#[cfg(feature = "vfs")]
pub custom_sections: HashMap<String, Vec<u8>>,
}
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. /// A compiled WebAssembly module.