Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add support for component modules #17

Merged
merged 3 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,4 @@ edition = "2021"
crate-type = ["staticlib", "cdylib"]

[dependencies]
wasmparser = "0.113"

wasmparser = "0.115"
26 changes: 21 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::io::Read;
use wasmparser::{Chunk, Parser, Validator};
use wasmparser::{Chunk, Parser, Payload::*, Validator};

fn err<T: ToString>(x: T) -> String {
x.to_string()
Expand All @@ -10,12 +10,20 @@ fn validate(mut reader: impl Read) -> Result<(), String> {
let mut parser = Parser::new(0);
let mut eof = false;
let mut stack = Vec::new();
let mut validator = Validator::new();
let mut validator = Validator::new_with_features(wasmparser::WasmFeatures {
component_model: true,
..Default::default()
});

loop {
let (payload, consumed) = match parser.parse(&buf, eof).map_err(err)? {
let (payload, consumed) = match parser
.parse(&buf, eof)
.map_err(|x| x.message().to_string())?
{
Chunk::NeedMoreData(hint) => {
assert!(!eof); // otherwise an error would be returned
if eof {
return Err("unexpected end-of-file".to_string());
}

// Use the hint to preallocate more space, then read
// some more data into our buffer.
Expand All @@ -33,6 +41,14 @@ fn validate(mut reader: impl Read) -> Result<(), String> {
Chunk::Parsed { consumed, payload } => (payload, consumed),
};

match &payload {
ModuleSection { parser: p, .. } | ComponentSection { parser: p, .. } => {
stack.push(parser.clone());
parser = p.clone();
}
_ => (),
}

match validator.payload(&payload).map_err(err)? {
wasmparser::ValidPayload::End(_) => {
if let Some(parent_parser) = stack.pop() {
Expand Down Expand Up @@ -78,7 +94,7 @@ pub unsafe fn wasm_verify_file(filename: *const u8, len: usize) -> *mut u8 {

match validate(file) {
Ok(()) => std::ptr::null_mut(),
Err(e) => return_string(format!("{}", e)),
Err(e) => return_string(e),
}
} else {
std::ptr::null_mut()
Expand Down
4 changes: 2 additions & 2 deletions test/wasmstore.t
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Parsing fail
$ head -c 12 a.wasm | wasmstore add - a.wasm
ERROR invalid module: unexpected end-of-file (at offset 0xb)
ERROR invalid module: unexpected end-of-file

Add wasm module `a`
$ cat a.wasm | wasmstore add - a.wasm
Expand Down Expand Up @@ -82,7 +82,7 @@ Run garbage collector

Invalid WASM module
$ head -c 5 a.wasm | wasmstore add - invalid.wasm
ERROR invalid module: unexpected end-of-file (at offset 0x4)
ERROR invalid module: unexpected end-of-file

Versions
$ wasmstore add a.wasm b.wasm
Expand Down
Loading