twiggy icon indicating copy to clipboard operation
twiggy copied to clipboard

Incompatibility with WASI Preview 2

Open k0pernicus opened this issue 10 months ago • 4 comments

🐛 Bug Description

I tried to use twiggy on a WASM binary using WASI Preview 2, but go the following parsing error:

error: WASM error: Bad version number (at offset 4)
  caused by: Bad version number (at offset 4)

The WASM binary, compiled with the Rust wasm32-wasip1 target, is correctly parsed by twiggy.
So, I do think the problem is the parser is incompatible with the Rust wasm32-wasip2 target.

twiggy version: twiggy-opt 0.7.0

🌍 Test Case

As Github does not allow to upload a WASM binary, please to follow the case here:

cargo new --bin test_wasm_twiggy # Create a random project
cd test_wasm_twiggy && cargo build --release --target=wasm32-wasip2 # Compile the basic Rust project
twiggy top target/wasm32-wasip2/release/test_wasm_twiggy.wasm # Try to test the WASM binary

👟 Steps to Reproduce

Run twiggy top target/wasm32-wasip2/release/test_wasm_twiggy.wasm.

😲 Actual Behavior

error: WASM error: Bad version number (at offset 4)
  caused by: Bad version number (at offset 4)

🤔 Expected Behavior

Twiggy should parse the WASM binary correctly.

k0pernicus avatar Mar 06 '25 09:03 k0pernicus

After debugging it, it seems the issue is coming from wasmparser-0.80.2, as the version of a compiled wasm32-wasip2 is 0x1000D, but it expected 0x1 or 0xd.

The version of wasmparser is 0.80.0, and should be updated to the last version imoo (which is 0.227.0).

k0pernicus avatar Mar 06 '25 10:03 k0pernicus

Thanks for the detailed report. I brieflly looked into updating it, but it looks like quite a bit of work and don't have any time now. Will prioritise this once / when i get some time :D

AlexEne avatar Mar 07 '25 10:03 AlexEne

Hi @AlexEne,

No problem, I think I can help on this problem. I can try to submit a WiP PR in the next few days, and welcome contributors to bump versions and move to the new interface of wasmparser iteratively.

k0pernicus avatar Mar 07 '25 13:03 k0pernicus

wasm32-wasip2 uses wasm components which bundle one or more core wasm modules. Twiggy likely only supports core wasm modules. Bumping wasmparser is not enough. Twiggy also needs to be adapted to handle wasm components.

bjorn3 avatar Mar 26 '25 09:03 bjorn3