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

[wasm/rs]: "wasm-interface-types" Custom Section Present Post-Compilation #1420

Closed
dOrgJelli opened this issue Nov 15, 2022 · 1 comment
Closed
Labels
type: bug Something isn't working

Comments

@dOrgJelli
Copy link
Contributor

Describe the bug
When rust wrappers include dependencies that use wasm-bindgen, even if these wasm-bindgen types do not cross the wasm/host boundary, they still make their way into the resulting wasm binary. This has been discussed and a proposed solution has been described here:
polywrap/wrap-integrations#71

The "fix" that was described was implemented in this PR here:
#988

This "fix" tells the wasm-bindgen CLI to remove itself from the module, and use wasm-interface-types in its place. These wasm-interface-types never make their way into the resulting wasm binary though, because again they do not cross the boundary. One final artifact of this though is that the wasm module has a "custom section" added to it that effectively says "wasm-interface-types has been used in this module". This is causing the following error to occur when running these wasm modules with Wasmtime: polywrap/python-client#39

Please provide the steps to reproduce and if possible a minimal demo of the problem.
Build a wrapper that uses wasm-bindgen dependencies, here is an example (sha3-wasm-rs wrapper): https://wrappers.io/v/ipfs/QmbYw6XfEmNdR3Uoa7u2U1WRqJEXbseiSoBNBt3yPFnKvi

Notice that Wasmtime looks for the "wasm-interface-types" custom section, and throws an error if it's present:
https://github.com/bytecodealliance/wasmtime/blob/0548952319d112ecffe519d7eb0fdf9bd10feec7/crates/environ/src/module_environ.rs#L631-L647

Now, inspect the wasm module downloaded above (sha3-wasm-rs), and see what custom sections are held within. Here is a simple program to do so:

extern crate anyhow;
extern crate wasmparser;

use anyhow::Result;
use wasmparser::{Parser, Payload};
use std::env;
use std::str;

fn main() -> Result<()> {
    let args = env::args().collect::<Vec<_>>();
    if args.len() != 2 {
        println!("Usage: {} in.wasm", args[0]);
        return Ok(());
    }

    let buf: Vec<u8> = std::fs::read(&args[1])?;
    for payload in Parser::new(0).parse_all(&buf) {
        match payload? {
            Payload::CustomSection(s) => {
                println!("  CustomSection {:?} {:?}", s.name(), str::from_utf8(s.data()));
            }
            _other => {
                // println!("found payload {:?}", _other);
            }
        }
    }

    Ok(())
}

This will output the following:

CustomSection "producers" Ok("\u{2}\u{8}language\u{1}\u{4}Rust\0\u{c}processed-by\u{4}\u{5}rustc\u{1d}1.60.0 (7737e0b5c 2022-04-04)\u{6}walrus\u{6}0.12.0\u{c}wasm-bindgen\u{6}0.2.82\twasm-snip\u{5}0.4.0")

CustomSection "wasm-interface-types" Ok("\u{5}0.1.0")

Expected behavior
The module does not need the "wasm-interface-types" custom section, as it does not need interface types. This is simply an artifact from wasm-bindgen.

@dOrgJelli dOrgJelli added the type: bug Something isn't working label Nov 15, 2022
@dOrgJelli
Copy link
Contributor Author

This custom sections can be removed with wasm-tools: https://crates.io/crates/wasm-tools

You can simply run wasm-tools strip ./wrap.wasm -d wasm-interface-types -o ./wrap.wasm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant