Skip to content

Update to latest Walrus #134

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ path = "src/bin/wasm-snip.rs"
required-features = ["exe"]

[dependencies]
failure = "0.1.5"
walrus = { version = "0.12.0", features = ["parallel"] }
walrus = { version = "0.23.2", features = ["parallel"] }
regex = "1.3.1"
rayon = "1.2.0"
anyhow = "1.0"

[dependencies.clap]
optional = true
Expand Down
11 changes: 6 additions & 5 deletions src/bin/wasm-snip.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
use failure::ResultExt;
use std::fs;
use std::io::{self, Write};
use std::process;

use anyhow::Context;

fn main() {
if let Err(e) = try_main() {
eprintln!("error: {}", e);
for c in e.iter_chain().skip(1) {
for c in e.chain().skip(1) {
eprintln!(" caused by {}", c);
}
eprintln!("{}", e.backtrace());
process::exit(1)
}
}

fn try_main() -> Result<(), failure::Error> {
fn try_main() -> Result<(), anyhow::Error> {
let matches = parse_args();

let mut opts = wasm_snip::Options::default();
Expand Down Expand Up @@ -43,15 +44,15 @@ fn try_main() -> Result<(), failure::Error> {

let config = walrus_config_from_options(&opts);
let path = matches.value_of("input").unwrap();
let buf = fs::read(&path).with_context(|_| format!("failed to read file {}", path))?;
let buf = fs::read(&path).with_context(|| format!("failed to read file {}", path))?;
let mut module = config.parse(&buf)?;

wasm_snip::snip(&mut module, opts).context("failed to snip functions from wasm module")?;

if let Some(output) = matches.value_of("output") {
module
.emit_wasm_file(output)
.with_context(|_| format!("failed to emit snipped wasm to {}", output))?;
.with_context(|| format!("failed to emit snipped wasm to {}", output))?;
} else {
let wasm = module.emit_wasm();
let stdout = io::stdout();
Expand Down
69 changes: 43 additions & 26 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ dual licensed as above, without any additional terms or conditions.
#![deny(missing_docs)]
#![deny(missing_debug_implementations)]

use failure::ResultExt;
use anyhow::Context;
use rayon::prelude::*;
use std::collections::HashMap;
use std::collections::HashSet;
Expand Down Expand Up @@ -152,7 +152,7 @@ pub struct Options {
}

/// Snip the functions from the input file described by the options.
pub fn snip(module: &mut walrus::Module, options: Options) -> Result<(), failure::Error> {
pub fn snip(module: &mut walrus::Module, options: Options) -> Result<(), anyhow::Error> {
if !options.skip_producers_section {
module
.producers
Expand All @@ -173,7 +173,7 @@ pub fn snip(module: &mut walrus::Module, options: Options) -> Result<(), failure
Ok(())
}

fn build_regex_set(mut options: Options) -> Result<regex::RegexSet, failure::Error> {
fn build_regex_set(mut options: Options) -> Result<regex::RegexSet, anyhow::Error> {
// Snip the Rust `fmt` code, if requested.
if options.snip_rust_fmt_code {
// Mangled symbols.
Expand Down Expand Up @@ -253,7 +253,7 @@ fn replace_calls_with_unreachable(
}

impl VisitorMut for Replacer<'_> {
fn visit_instr_mut(&mut self, instr: &mut walrus::ir::Instr) {
fn visit_instr_mut(&mut self, instr: &mut walrus::ir::Instr, _: &mut walrus::InstrLocId) {
if self.should_snip_call(instr) {
*instr = walrus::ir::Unreachable {}.into();
}
Expand Down Expand Up @@ -319,31 +319,48 @@ fn snip_table_elements(module: &mut walrus::Module, to_snip: &HashSet<walrus::Fu
};

for t in module.tables.iter_mut() {
if let walrus::TableKind::Function(ref mut ft) = t.kind {
if matches!(t.element_ty, walrus::RefType::Funcref) {
let types = &mut module.types;
let locals = &mut module.locals;
let funcs = &mut module.funcs;

ft.elements
.iter_mut()
.flat_map(|el| el)
.filter(|f| to_snip.contains(f))
.for_each(|el| {
let ty = funcs.get(*el).ty();
*el = *unreachable_funcs
.entry(ty)
.or_insert_with(|| make_unreachable_func(ty, types, locals, funcs));
});

ft.relative_elements
.iter_mut()
.flat_map(|(_, elems)| elems.iter_mut().filter(|f| to_snip.contains(f)))
.for_each(|el| {
let ty = funcs.get(*el).ty();
*el = *unreachable_funcs
.entry(ty)
.or_insert_with(|| make_unreachable_func(ty, types, locals, funcs));
});
let elements = &mut module.elements;

for segment in &t.elem_segments {
let elements = elements.get_mut(*segment);
match &mut elements.items {
walrus::ElementItems::Functions(fs) => {
fs.iter_mut()
.filter(|f| to_snip.contains(f))
.for_each(|el| {
let ty = funcs.get(*el).ty();
*el = *unreachable_funcs.entry(ty).or_insert_with(|| {
make_unreachable_func(ty, types, locals, funcs)
});
});
}
walrus::ElementItems::Expressions(walrus::RefType::Funcref, const_exprs) => {
const_exprs
.iter_mut()
.filter_map(|f| {
if let walrus::ConstExpr::RefFunc(f) = f {
Some(f)
} else {
None
}
})
.filter(|f| to_snip.contains(f))
.for_each(|el| {
let ty = funcs.get(*el).ty();
*el = *unreachable_funcs.entry(ty).or_insert_with(|| {
make_unreachable_func(ty, types, locals, funcs)
});
});
}
walrus::ElementItems::Expressions(..) => {
panic!("funcref table should only have elements of type functions")
}
}
}
}
}
}
Binary file modified tests/no_alloc.wasm
Binary file not shown.
Binary file modified tests/no_fmt.wasm
Binary file not shown.
Binary file modified tests/no_panicking.wasm
Binary file not shown.
Binary file modified tests/snip_me.wasm
Binary file not shown.