binaryen icon indicating copy to clipboard operation
binaryen copied to clipboard

Issue with features section: mutable-globals, nontrapping-float-to-int, bulk-memory, sign-ext, bulk-memory-opt in wasm2js and wasm-dis

Open CreatorEdition opened this issue 11 months ago • 6 comments

When converting a WebAssembly module using wasm2js, I encountered an error related to the features section of the module. The module includes advanced WebAssembly features such as mutable-globals, bulk-memory, and sign-ext. Despite attempts to optimize or strip these features using wasm-opt, the error persists.

The error message is as follows:

;; features section: mutable-globals, nontrapping-float-to-int, bulk-memory, sign-ext, bulk-memory-opt Fatal: error in validating wasm2js output

;; features section: mutable-globals, nontrapping-float-to-int, bulk-memory, sign-ext, bulk-memory-opt in after use wasm-dis in file

Steps to Reproduce

  1. Use the following WebAssembly file (or a similar file with advanced features):

  2. Run the following commands:```bash wasm-opt input.wasm --strip-debug --strip-producers --disable-mutable-globals --disable-bulk-memory -o optimized.wasm -O2 wasm2js optimized.wasm -o output.js

3.Observe the error related to the features section.

Actual Behavior

The conversion process fails with a validation error, even after attempting to strip or disable advanced WebAssembly features using wasm-opt.

Environment

Tool version: Binaryen 121 Command line used: wasm2js, wasm-opt OS: win11

Additional Context

I have attempted the following solutions:

Stripping debug and producer sections using wasm-opt. Disabling specific features like mutable-globals and bulk-memory during optimization. Re-generating the WebAssembly file with minimal features. The issue persists even when using the latest version of Binaryen.

Suggested Improvement

Enhance wasm2js to either skip unsupported features or convert as much as possible while issuing warnings. Provide better documentation or guidance for handling advanced WebAssembly features in wasm2js.

CreatorEdition avatar Jan 18 '25 17:01 CreatorEdition

I have attempted the following solutions: Re-generating the WebAssembly file with minimal features.

That should work. If it does not then something else is going on rather than features. Make sure that you are emitting a wasm file without those features.

In general, wasm2js is meant to support very old browsers and runtimes, places without wasm. Those places will not benefit from newer wasm features like sign-ext, so wasm2js does not support them.

Note that there are some lowering passes you can run before wasm2js, that can help in some cases: --llvm-memory-copy-fill-lowering, --llvm-nontrapping-fptoint-lowering, --signext-lowering, but they have limitations. It is best to just not emit the features, if you will be running wasm2js.

kripken avatar Jan 18 '25 19:01 kripken

What executable has that --llvm-nontrapping-fptoint-lowering?

../binaryen/bin/wasm-opt module.wasm --llvm-nontrapping-fptoint-lowering -o optimized-module.wasm
Unknown option '--llvm-nontrapping-fptoint-lowering'

I might be running in to that with AssemblyScript to wasm2js that hangs.

guest271314 avatar Jan 28 '25 15:01 guest271314

--llvm-nontrapping-fptoint-lowering is in wasm-opt from the very latest version of binaryen. Make sure you are not using an earlier version.

kripken avatar Jan 28 '25 15:01 kripken

I get Fatal: error validating input WASM compiled with AssemblyScript.

guest271314 avatar Jan 29 '25 02:01 guest271314

This worked for Rust source code, not for AssemblyScript source code

../binaryen/bin/wasm-opt module.wasm --llvm-memory-copy-fill-lowering  --llvm-nontrapping-fptoint-lowering --signext-lowering --enable-mutable-globals --enable-bulk-memory  --signext-lowering  -o module-wasm-opt.wasm
[wasm-validator error in function 130] unexpected false: all used features should be allowed, on 
(i32.trunc_sat_f64_s
 (call $117
  (local.get $5)
  (i32.const 0)
 )
)
[wasm-validator error in function 130] unexpected false: all used features should be allowed, on 
(i32.trunc_sat_f64_s
 (call $117
  (local.get $5)
  (i32.const 0)
 )
)
[wasm-validator error in function 130] unexpected false: all used features should be allowed, on 
(i32.trunc_sat_f64_s
 (call $117
  (local.get $5)
  (i32.const 0)
 )
)
[wasm-validator error in function 130] unexpected false: all used features should be allowed, on 
(i32.trunc_sat_f64_s
 (call $117
  (local.get $5)
  (i32.const 0)
 )
)
Fatal: error validating input

guest271314 avatar Feb 01 '25 20:02 guest271314

I'm running into a very similar issue with Rust code and wasm-opt version 123, maybe the output helps further with debugging, with (Rustflags, not wasm-opt) -Oz:

[wasm-validator error in function 475] unexpected false: all used features should be allowed, on 
(i32.trunc_sat_f64_u
 (f64.mul
  (call $fimport$8)
  (f64.const 1e3)
 )
)
Fatal: error validating input

It doesn't seem to matter for this issue whether I add these CLI flags: --llvm-memory-copy-fill-lowering --llvm-nontrapping-fptoint-lowering --signext-lowering

Philipp-M avatar Jun 09 '25 13:06 Philipp-M

We were also hitting this when we switched from rust 1.86 to rust 1.88 (in https://github.com/n0-computer/iroh-examples/pull/118).

(And also some other errors)

[wasm-validator error in function 10708] unexpected false: Bulk memory operations require bulk memory [--enable-bulk-memory], on 
(memory.fill
 (local.get $0)
 (i32.const 0)
 (i32.const 256)
)
[wasm-validator error in function 12234] unexpected false: all used features should be allowed, on 
(i64.trunc_sat_f64_u
 (call $13204)
)
Fatal: error validating input

We were able to fix these by passing --all-features to wasm-opt. (Tested with wasm-opt version 116 and 118)

matheus23 avatar Jul 04 '25 14:07 matheus23

Note that --all-features may end up enabling more features than you intend! It's best to look at what features your intended deployment targets support and explicitly enable just those features in production scenarios.

tlively avatar Jul 18 '25 03:07 tlively

Thanks @tlively. How would I find out which feature that would be? The error message doesn't help me find that out:

[wasm-validator error in function 12234] unexpected false: all used features should be allowed, on [...]

matheus23 avatar Jul 18 '25 09:07 matheus23

i64.trunc_sat_f64_u requires --nontrapping-float-to-int.

The original question was about using wasm2jd, though, and that has been answered so I will close this.

tlively avatar Jul 18 '25 15:07 tlively