wasm-pack icon indicating copy to clipboard operation
wasm-pack copied to clipboard

wasm-opt fail with [parse exception: invalid code after misc prefix: 17 (at 0:61491)]

Open Wayonb opened this issue 1 year ago • 13 comments

🐛 Bug description

Our build was working up to rust 1.81.0. With Rustc 1.82.0, I am getting an error with wasm-opt.

🤔 Expected Behavior

wasm-opt should work with rustc 1.82.0

👟 Steps to reproduce

 wasm-pack build --release --no-typescript --target nodejs --out-dir ../_build/wasm/node
[INFO]: Checking for the Wasm target...
info: downloading component 'rust-std' for 'wasm32-unknown-unknown'
info: installing component 'rust-std' for 'wasm32-unknown-unknown'
 18.7 MiB /  18.7 MiB (100 %)  13.8 MiB/s in  1s ETA:  0s
[INFO]: Compiling to Wasm...
    Finished `release` profile [optimized] target(s) in 0.08s
[INFO]: Installing wasm-bindgen...
[INFO]: found wasm-opt at "/usr/bin/wasm-opt"
[INFO]: Optimizing wasm binaries with `wasm-opt`...
[parse exception: invalid code after misc prefix: 17 (at 0:61491)]
Fatal: error parsing wasm
Error: failed to execute `wasm-opt`: exited with exit status: 1
  full command: "/usr/bin/wasm-opt" "/home/ubuntu/symbol/sdk/javascript/_build/wasm/node/symbol_crypto_wasm_bg.wasm" "-o" "/home/ubuntu/symbol/sdk/javascript/_build/wasm/node/symbol_crypto_wasm_bg.wasm-opt.wasm" "-O"
To disable `wasm-opt`, add `wasm-opt = false` to your package metadata in your `Cargo.toml`.
Caused by: failed to execute `wasm-opt`: exited with exit status: 1
  full command: "/usr/bin/wasm-opt" "/home/ubuntu/symbol/sdk/javascript/_build/wasm/node/symbol_crypto_wasm_bg.wasm" "-o" "/home/ubuntu/symbol/sdk/javascript/_build/wasm/node/symbol_crypto_wasm_bg.wasm-opt.wasm" "-O"
To disable `wasm-opt`, add `wasm-opt = false` to your package metadata in your `Cargo.toml`.

🌍 Your environment

Include the relevant details of your environment. Ubuntu 24.04(it works on MacOs) wasm-pack version: wasm-pack -V wasm-pack 0.13.1

rustc version: rustc -V rustc 1.82.0 (f6e511eec 2024-10-15)

Wayonb avatar Oct 30 '24 02:10 Wayonb

The code is here - https://github.com/symbol/symbol/tree/dev/sdk/javascript/wasm

Wayonb avatar Oct 30 '24 11:10 Wayonb

have the same problem

jellybobbin avatar Nov 13 '24 08:11 jellybobbin

When wasm-bindgen 0.2.94-95 sees the wasm target-features metadata emitted by rustc 1.82.0, it enables additional webassembly features on its output (bulk memory instructions) that can cause issues with downstream tools like wasm-opt. This will be fixed in the next release of wasm-bindgen. Until then, workarounds include adding --enable-bulk-memory to the wasm-opt commandline (if your runtime is compatible with bulk-memory instructions), not using wasm-opt at all, or downgrading to rust 1.81 or to wasm-bindgen 0.2.93.

Related discussions: https://github.com/rustwasm/wasm-bindgen/issues/4250 https://github.com/rustwasm/wasm-bindgen/issues/4211 https://github.com/rust-lang/rust/issues/132620 https://github.com/trunk-rs/trunk/issues/904

eric-seppanen avatar Nov 13 '24 17:11 eric-seppanen

I fixed this by upgrading wasm-pack to the latest version.

It's a bit annoying because it doesn't auto update. It's not a js library or a rust crate or whatever. I had to install it from the site: https://rustwasm.github.io/wasm-pack/installer/

paulcdejean avatar Feb 26 '25 01:02 paulcdejean

I upgraded to 0.13.1 but seems to not have any effect.

Yatekii avatar Feb 26 '25 21:02 Yatekii

Also experiencing compilation failures when following the MDN docs at https://developer.mozilla.org/en-US/docs/WebAssembly/Guides/Rust_to_Wasm

jlanssie avatar May 13 '25 14:05 jlanssie

It seems a similar case was reported with Rust 1.87. Is there anything that can be done on the wasm-pack side to address this?

https://github.com/rust-lang/rust/issues/141048

dalance avatar May 16 '25 10:05 dalance

In our case, we worked around this by putting

[package.metadata.wasm-pack.profile.release]
wasm-opt = ["--enable-bulk-memory", "--enable-nontrapping-float-to-int"]

in our Cargo.toml.


Note from the future: the default for this is ["-O"], so you probably want ["-O", "--enable-bulk-memory", "--enable-nontrapping-float-to-int"].

TomFryersMidsummer avatar May 16 '25 11:05 TomFryersMidsummer

I am seeing this problem again with Rustc 1.87.0

  1.87.0-aarch64-unknown-linux-gnu installed - rustc 1.87.0 (17067e9ac 2025-05-09)

info: default toolchain set to '1.87.0-aarch64-unknown-linux-gnu'
info: checking for self-update
+ wasm-pack build --release --no-typescript --target nodejs --out-dir ../_build/wasm/node
[INFO]: Checking for the Wasm target...
info: downloading component 'rust-std' for 'wasm32-unknown-unknown'
info: installing component 'rust-std' for 'wasm32-unknown-unknown'
[INFO]: Compiling to Wasm...
   Compiling unicode-ident v1.0.18
   Compiling proc-macro2 v1.0.95
   Compiling version_check v0.9.5
   Compiling wasm-bindgen-shared v0.2.100
   Compiling typenum v1.18.0
   Compiling bumpalo v3.17.0
   Compiling semver v1.0.26
   Compiling log v0.4.27
   Compiling cfg-if v1.0.0
   Compiling rustversion v1.0.21
   Compiling wasm-bindgen v0.2.100
   Compiling once_cell v1.21.3
   Compiling serde v1.0.219
   Compiling keccak v0.1.5
   Compiling zeroize v1.8.1
   Compiling subtle v2.6.1
   Compiling generic-array v0.14.7
   Compiling rustc_version v0.4.1
   Compiling curve25519-dalek v4.1.3
   Compiling quote v1.0.40
   Compiling syn v2.0.101
   Compiling block-buffer v0.10.4
   Compiling crypto-common v0.1.6
   Compiling digest v0.10.7
   Compiling sha2 v0.10.9
   Compiling sha3 v0.10.8
   Compiling hex v0.4.3
   Compiling wasm-bindgen-backend v0.2.100
   Compiling wasm-bindgen-macro-support v0.2.100
   Compiling wasm-bindgen-macro v0.2.100
   Compiling console_error_panic_hook v0.1.7
   Compiling js-sys v0.3.77
   Compiling symbol-crypto-wasm v0.1.0 (/symbol/sdk/javascript/wasm)
    Finished `release` profile [optimized] target(s) in 4.00s
[INFO]: Installing wasm-bindgen...
[INFO]: found wasm-opt at "/usr/bin/wasm-opt"
[INFO]: Optimizing wasm binaries with `wasm-opt`...
[parse exception: invalid code after misc prefix: 17 (at 0:62712)]
Fatal: error parsing wasm
Error: failed to execute `wasm-opt`: exited with exit status: 1
  full command: "/usr/bin/wasm-opt" "/symbol/sdk/javascript/_build/wasm/node/symbol_crypto_wasm_bg.wasm" "-o" "/symbol/sdk/javascript/_build/wasm/node/symbol_crypto_wasm_bg.wasm-opt.wasm" "-O"
To disable `wasm-opt`, add `wasm-opt = false` to your package metadata in your `Cargo.toml`.
Caused by: failed to execute `wasm-opt`: exited with exit status: 1
  full command: "/usr/bin/wasm-opt" "/symbol/sdk/javascript/_build/wasm/node/symbol_crypto_wasm_bg.wasm" "-o" "/symbol/sdk/javascript/_build/wasm/node/symbol_crypto_wasm_bg.wasm-opt.wasm" "-O"
To disable `wasm-opt`, add `wasm-opt = false` to your package metadata in your `Cargo.toml`.

Wayonb avatar Jun 03 '25 03:06 Wayonb

Rust 1.87.0 moved to LLVM 20, which enabled more webassembly features by default.

wasm-opt needs additional commandline flags to enable the same webassembly features, or it will fail with this error. See earlier comments for some flags to try.

eric-seppanen avatar Jun 03 '25 17:06 eric-seppanen

Rust 1.87.0 moved to LLVM 20, which enabled more webassembly features by default.

wasm-opt needs additional commandline flags to enable the same webassembly features, or it will fail with this error. See earlier comments for some flags to try.

Yes, Thanks. I have tried the flags above, but it does not help. I forgot to mention that the build only fails on Ubuntu ARM. So, Ubuntu AMD64/Windows AMD64 builds are correct.

Wayonb avatar Jun 03 '25 17:06 Wayonb

@Wayonb consider checking what version of wasm-opt you are running at /usr/bin/wasm-opt. I saw a similar issue on Rustc 1.87.0 with version 116 and it was fixed by upgrading to version 117.

mthiesmeyer avatar Jul 01 '25 15:07 mthiesmeyer

I'm getting the same error on rust 1.88

gignsky avatar Aug 21 '25 13:08 gignsky