Skip to content

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

@gustavohenke

Description

@gustavohenke

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions