-
Let's say I have a crate with the following Cargo.toml and src/lib.rs: [package]
name = "foo"
version = "0.1.0"
edition = "2021"
[lib]
name = "foo"
crate-type = ["cdylib"]
[dependencies]
wasm-bindgen = { version = "0.2.89", default-features = false }
[profile.release]
panic = "abort" #[no_mangle]
pub extern "C" fn add(a: u32, b: u32) -> u32 {
a.wrapping_add(b)
} Compiling it results in a small and compact WASM file as expected. But trying to use use wasm_bindgen::prelude::wasm_bindgen;
#[no_mangle]
pub extern "C" fn add(a: u32, b: u32) -> u32 {
a.wrapping_add(b)
}
#[wasm_bindgen]
extern "C" {} results in a much bigger file, which includes allocator and potential panic paths. This not only bloats size of generated WASM files, but also makes it harder to write robust code which attempts to "prove" that it does not contain any potential panics. Is this behavior intended and expected? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
This is the default on
I'm going to assume that you have done the post-processing with the CLI here. The only difference I can see is a data segment, that is unreferenced.
I'm not aware of any way to remove these parts of the data segment reliably and correctly. What could be done is to avoid any panicking code in these "synthetic" functions (e.g. here), but unfortunately this requires unchecked indexing into arrays and slices, which Rust doesn't support. That said, it should be quite easy to verify that the code generated by your example can't panic: (module
(type $type0 (func (param i32 i32) (result i32)))
(memory $memory0 17)
(export "memory" (memory $memory0))
(export "add" (func $func0))
(func $func0 (param $var0 i32) (param $var1 i32) (result i32)
local.get $var0
local.get $var1
i32.add
)
(data (i32.const 1048576) "/rust/deps/dlmalloc-0.2.6/src/dlmalloc.rsassertion failed: psize >= size + min_overhead\00\00\00\10\00)\00\00\00\a8\04\00\00\09\00\00\00assertion failed: psize <= size + max_overhead\00\00\00\00\10\00)\00\00\00\ae\04\00\00\0d")
) As you can see, no |
Beta Was this translation helpful? Give feedback.
-
Assuming enabled optimizations, the
The original code is much less trivial than this minimal reproduction example. I could write a static analyzer of WAT for checking whether function's call tree contains |
Beta Was this translation helpful? Give feedback.
This is the default on
wasm32-unknown-unknown
btw.I'm going to assume that you have done the post-processing with the CLI here.
The only difference I can see is a data segment, that is unreferenced.
The data segment doesn't do anything, it just holds a bunch of unused strings that as you say bloat the resulting Wasm module.
wasm-bindgen
works by inserting functions into the module, which are executed b…