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

Support gzipped WASM inputs in utils::parse_wasm and utils::parse_wasm_file #55

Merged
merged 6 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
33 changes: 33 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ walrus = "0.20.1"
candid = "0.10"
rustc-demangle = "0.1"
thiserror = "1.0.35"
libflate = "1.3.0"

wasm-opt = { version = "0.113.0", optional = true }
tempfile = { version = "3.5.0", optional = true }
Expand Down
52 changes: 49 additions & 3 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,74 @@
use crate::Error;
use libflate::gzip;
use std::collections::HashMap;
use std::io::{self, Read};
use walrus::*;

pub const WASM_MAGIC_BYTES: &[u8] = &[0, 97, 115, 109];

pub const GZIPPED_WASM_MAGIC_BYTES: &[u8] = &[31, 139, 8, 0];

fn wasm_parser_config(keep_name_section: bool) -> ModuleConfig {
let mut config = walrus::ModuleConfig::new();
config.generate_name_section(keep_name_section);
config.generate_producers_section(false);
config
}

pub fn parse_wasm(wasm: &[u8], keep_name_section: bool) -> Result<Module, Error> {
pub fn parse_wasm(bytes: &[u8], keep_name_section: bool) -> Result<Module, Error> {
let wasm = if bytes.starts_with(WASM_MAGIC_BYTES) {
Ok(bytes)
} else if bytes.starts_with(GZIPPED_WASM_MAGIC_BYTES) {
Err(io::Error::new(
io::ErrorKind::InvalidInput,
"Please use parse_wasm_robust with gzipped inputs.",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't seem to be a breaking change, why not just decompress here, instead of a new function?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original public function parse_wasm's signature has bytes: &[u8], changing it to bytes: Vec<u8> would be a breaking change, right?

If we keep it &[u8], then the decompress function's result type (Result<Vec<u8>) is incompatible with it in one of the branches. So either we would need to inline decompress or have another function that takes Vec<u8> to begin with.

What's your preference? Am I missing a better possible implementation?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we keep it &[u8], then the decompress function's result type (Result<Vec) is incompatible with it in one of the branches.

We can make both branches to return Cow::Borrowed and Cow::Owned, so that they end up with the same type.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me give it a try

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems to have worked right away. Thanks for the tip!

))
} else {
Err(io::Error::new(
io::ErrorKind::InvalidInput,
"Input must be uncompressed WASM.",
))
}
.map_err(Error::IO)?;
let config = wasm_parser_config(keep_name_section);
config
.parse(wasm)
.map_err(|e| Error::WasmParse(e.to_string()))
}

pub fn parse_wasm_file(file: std::path::PathBuf, keep_name_section: bool) -> Result<Module, Error> {
fn decompress(bytes: &[u8]) -> Result<Vec<u8>, std::io::Error> {
let mut decoder = gzip::Decoder::new(bytes)?;
let mut decoded_data = Vec::new();
decoder.read_to_end(&mut decoded_data)?;
Ok(decoded_data)
}

/// Similar to parse_wasm, but if the input is gzipped, first uncompresses it and then parses.
pub fn parse_wasm_robust(bytes: Vec<u8>, keep_name_section: bool) -> Result<Module, Error> {
let wasm = if bytes.starts_with(WASM_MAGIC_BYTES) {
Ok(bytes)
} else if bytes.starts_with(GZIPPED_WASM_MAGIC_BYTES) {
decompress(&bytes[..])
} else {
Err(io::Error::new(
io::ErrorKind::InvalidInput,
"Input must be either gzipped or uncompressed WASM.",
))
}
.map_err(Error::IO)?;

let config = wasm_parser_config(keep_name_section);
config
.parse_file(file)
.parse(&wasm[..])
.map_err(|e| Error::WasmParse(e.to_string()))
}

pub fn parse_wasm_file(file: std::path::PathBuf, keep_name_section: bool) -> Result<Module, Error> {
let bytes = std::fs::read(file).map_err(|err| Error::IO(err))?;

parse_wasm_robust(bytes, keep_name_section)
}

#[derive(Clone, Copy, PartialEq, Eq)]
pub(crate) enum InjectionKind {
Static,
Expand Down
Loading