From 242465a7f927233b9fffaf04b59be4a59e82ec1b Mon Sep 17 00:00:00 2001 From: joeoc2001 Date: Fri, 6 Dec 2024 15:07:26 +0000 Subject: [PATCH 1/2] Update to latest Walrus --- Cargo.toml | 4 ++-- src/bin/wasm-snip.rs | 11 ++++----- src/lib.rs | 48 ++++++++++++++++++---------------------- tests/no_alloc.wasm | Bin 68712 -> 68673 bytes tests/no_fmt.wasm | Bin 55061 -> 55022 bytes tests/no_panicking.wasm | Bin 75388 -> 75349 bytes tests/snip_me.wasm | Bin 78354 -> 78315 bytes 7 files changed, 30 insertions(+), 33 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index e6f53ff..f3ce055 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 diff --git a/src/bin/wasm-snip.rs b/src/bin/wasm-snip.rs index 12d38d7..03e2e41 100644 --- a/src/bin/wasm-snip.rs +++ b/src/bin/wasm-snip.rs @@ -1,12 +1,13 @@ -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()); @@ -14,7 +15,7 @@ fn main() { } } -fn try_main() -> Result<(), failure::Error> { +fn try_main() -> Result<(), anyhow::Error> { let matches = parse_args(); let mut opts = wasm_snip::Options::default(); @@ -43,7 +44,7 @@ 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")?; @@ -51,7 +52,7 @@ fn try_main() -> Result<(), failure::Error> { 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(); diff --git a/src/lib.rs b/src/lib.rs index c475caa..8e6579b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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; @@ -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 @@ -173,7 +173,7 @@ pub fn snip(module: &mut walrus::Module, options: Options) -> Result<(), failure Ok(()) } -fn build_regex_set(mut options: Options) -> Result { +fn build_regex_set(mut options: Options) -> Result { // Snip the Rust `fmt` code, if requested. if options.snip_rust_fmt_code { // Mangled symbols. @@ -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(); } @@ -319,31 +319,27 @@ fn snip_table_elements(module: &mut walrus::Module, to_snip: &HashSetJ0YH)3LIDAjAOXw{1p#mYQc?v20RRyP z`3AEE0(t`m0&gXP5x7Cwf=;#@`s1_s7sjSUS96S_6LpVw*oORx$H7fy6d4a(06?6tMO&Hvy&ov%cQNau}rO^??P70^2VGnJjsU Qxv7k6K+MfkAFa^@02@LwDF6Tf diff --git a/tests/no_fmt.wasm b/tests/no_fmt.wasm index a439fe841abaf762cfe7ab558e9f2cd5d13387d3..7d1b0c310815c78e5b76cc7a7cfd9bbad3259c5f 100644 GIT binary patch delta 110 zcmV-!0FnQdt^@9^0}B9Qb8P_t0005I0g(>80YH(@LIDAj6#<_P1p#mYQc?v20RRyP z=LWOQ0cZmg0*3(!o&f;>K>!OJ0YH)3LIDAjAOX@21p#mYQc?v20RRyP z{06fN0(}Dp0=fYSi~*CC1&;v(vnK}R&H+QS57nR{3dLUpZeeX@0liInHv^?O*`AzZISf+t?#cp1ffM(EOqRUF Q+*HQBAm-*7-}dSQ08*bacK`qY diff --git a/tests/snip_me.wasm b/tests/snip_me.wasm index 622f57e58098e919b28c6a220a8579ff9feafbc3..92a4232b38a7203def51c484a154c0cac749b382 100644 GIT binary patch delta 93 zcmV-j0HXhrJ0YH)3LIDAjAOYD91p#mYQc?v20RRyP z{06fU0(}Dp0>J?Zj{%dM1(E>+voQwu*a1VcEZ?9Z3cg|lZeeX@0k&eb+yOQ*^9med delta 132 zcmaF;m}Sxv7Cwf=;#@`s1_s7sjSUS96S Date: Mon, 9 Jun 2025 13:13:56 +0100 Subject: [PATCH 2/2] Fix expr-ref calculations --- src/lib.rs | 45 +++++++++++++++++++++++++++++++++------------ 1 file changed, 33 insertions(+), 12 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 8e6579b..63a71d7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -327,18 +327,39 @@ fn snip_table_elements(module: &mut walrus::Module, to_snip: &HashSet { + 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") + } + } } } }