From 085bbbff68a94d864c500dbdee0b499b9e4fdde7 Mon Sep 17 00:00:00 2001 From: Adam Bratschi-Kaye Date: Thu, 5 Sep 2024 09:37:31 +0200 Subject: [PATCH] feat(EXC): Name Section for Metadata Command (#69) Add support for keeping the name section in the Wasm module when running the `metadata` command. Currently that command will always drop the name section when mutating the custom sections. --- src/bin/main.rs | 6 ++++++ tests/tests.rs | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/src/bin/main.rs b/src/bin/main.rs index 72572e7..37d4fac 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -33,6 +33,8 @@ enum SubCommand { /// Visibility of metadata #[clap(short, long, value_parser = ["public", "private"], default_value = "private")] visibility: String, + #[clap(short, long)] + keep_name_section: bool, }, /// Limit resource usage Resource { @@ -94,6 +96,9 @@ fn main() -> anyhow::Result<()> { SubCommand::Optimize { keep_name_section, .. } => keep_name_section, + SubCommand::Metadata { + keep_name_section, .. + } => keep_name_section, _ => false, }; let mut m = ic_wasm::utils::parse_wasm_file(opts.input, keep_name_section)?; @@ -155,6 +160,7 @@ fn main() -> anyhow::Result<()> { data, file, visibility, + keep_name_section: _, } => { use ic_wasm::metadata::*; if let Some(name) = name { diff --git a/tests/tests.rs b/tests/tests.rs index 4cb8ac2..5a2d303 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -1,4 +1,5 @@ use assert_cmd::Command; + use std::fs; use std::path::Path; @@ -34,6 +35,21 @@ fn assert_wasm(expected: &str) { } } +fn assert_functions_are_named() { + let path = Path::new(env!("CARGO_MANIFEST_DIR")).join("tests"); + let out = path.join("out.wasm"); + + let module = walrus::Module::from_file(out).unwrap(); + let name_count = module.funcs.iter().filter(|f| f.name.is_some()).count(); + let total = module.funcs.iter().count(); + // Walrus doesn't give direct access to the name section, but as a proxy + // just check that moste functions have names. + assert!( + name_count > total / 2, + "Module has {total} functions but only {name_count} have names." + ) +} + #[test] fn instrumentation() { wasm_input("motoko.wasm", true) @@ -352,3 +368,23 @@ icp:public whatever ) .success(); } + +#[test] +fn metadata_keep_name_section() { + for file in [ + "motoko.wasm", + "classes.wasm", + "motoko-region.wasm", + "rust.wasm", + ] { + wasm_input(file, true) + .arg("metadata") + .arg("foo") + .arg("-d") + .arg("hello") + .arg("--keep-name-section") + .assert() + .success(); + assert_functions_are_named(); + } +}