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

Default output from wasm-bindgen throws "Invalid UTF-8 encoding" #1142

Open
gustavohenke opened this issue Jun 22, 2023 · 0 comments
Open

Default output from wasm-bindgen throws "Invalid UTF-8 encoding" #1142

gustavohenke opened this issue Jun 22, 2023 · 0 comments

Comments

@gustavohenke
Copy link

gustavohenke commented Jun 22, 2023

Hello,
I noticed a couple of issues that might be related to mine - I'm not sure if mine's a duplicate of any though, so I'm creating it anyways, hopefully with some useful information and workarounds for others.

I've been trying to use wasm-edit to update .wasm files, but I noticed that it doesn't work and throws a "Invalid UTF-8 encoding" even with the simplest of the outputs from Rust + wasm-bindgen.

const { edit } = require('@webassemblyjs/wasm-edit');
const fs = require('fs');

edit(fs.readFileSync('./some_bindgen.wasm'), {});
Full stacktrace, just in case
Error: invalid UTF-8 encoding
    at _decode (./node_modules/@webassemblyjs/utf8/lib/decoder.js:43:13)
    at Object.decode (./node_modules/@webassemblyjs/utf8/lib/decoder.js:25:10)
    at readUTF8String (./node_modules/@webassemblyjs/wasm-parser/lib/decoder.js:229:22)
    at parseSection (./node_modules/@webassemblyjs/wasm-parser/lib/decoder.js:1671:29)
    at Object.decode (./node_modules/@webassemblyjs/wasm-parser/lib/decoder.js:1740:25)
    at decode (./node_modules/@webassemblyjs/wasm-parser/lib/index.js:253:21)
    at edit (./node_modules/@webassemblyjs/wasm-edit/lib/index.js:82:36)

I also noticed that this doesn't happen if I comment out the call to shrinkPaddedLEB128 below, or if I use editWithAST directly

const optBin = shrinkPaddedLEB128(new Uint8Array(ab));

With that said, there are a couple of flag combinations that I can pass to wasm-bindgen that will remove whatever problematic sections there are, and edit will work just fine.
The smallest set of additional flags I found to do that is --remove-name-section --remove-producers-section
I'm not sure yet what these are useful for, but I imagine that they would be at least during development? Anyways, i can survive with my workaround for now 🙂

Here's a reproduction repo using @webassemblyjs/[email protected]:
https://github.com/gustavohenke/wasm-bindgen-wasm-edit

Thanks in advance!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant