webassemblyjs icon indicating copy to clipboard operation
webassemblyjs copied to clipboard

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

Open gustavohenke opened this issue 2 years ago • 0 comments

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 https://github.com/xtuc/webassemblyjs/blob/daf8b9e819103f715285fd22af5832314957eb0d/packages/wasm-edit/src/index.js#L17

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!

gustavohenke avatar Jun 22 '23 08:06 gustavohenke