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

@ -45,4 +45,4 @@ field-offset = "0.1.1"
[features]
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 CallResult<T> = std::result::Result<T, CallError>;
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
/// successfully compile the provided webassembly module into
@ -445,3 +447,16 @@ impl Into<GrowError> for MemoryProtectionError {
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,
) -> CompileResult<module::Module> {
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

View File

@ -56,6 +56,28 @@ pub struct ModuleInfo {
pub namespace_table: StringTable<NamespaceIndex>,
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.