wasmerio / wasmer

🚀 The leading Wasm Runtime supporting WASIX and WASI
https://wasmer.io
MIT License
19.04k stars 814 forks source link

Error running a large wasm file: "Validation error: segments count is out of bounds" #2001

Closed weijiekoh closed 3 years ago

weijiekoh commented 3 years ago

Summary

I have a WASM file that's 9.7M large, and wasmer run emits this error no matter which combination of compiler and engine I use:

│   1: module instantiation failed (engine: jit, compiler: cranelift)
╰─> 2: Validation error: segments count is out of bounds (at offset 217025)

The WASM file is here: https://www.dropbox.com/s/fl0k41nsj3zjtad/processMessages_test.wasm?dl=0

Any help would be greatly appreciated! Thank you!

Hywan commented 3 years ago

Hello,

This error is raised by wasmparser:

https://github.com/bytecodealliance/wasm-tools/blob/737f1609d14e5484c2328224e2594d72fb440a6c/crates/wasmparser/src/validator.rs#L1477-L1497

We use wasmparser to validate the Wasm module before compiling it. It is raised by the Validator::data_section. It seems that your Wasm module contains too much data segments. The maximum data segments is defined by the MAX_WASM_DATA_SEGMENTS constant:

https://github.com/bytecodealliance/wasm-tools/blob/d3cbb38f990280861438d12c8a4546423fbc9ae6/crates/wasmparser/src/limits.rs#L23

Its value is 100_000 at the time of writing.

That's quite a lot of data segments :-p. How did you generate the Wasm module?

I tried to run wasm-opt to see if I can ameliorate things, but I get this warning:

$ wasm-opt -O3 a.wasm -o a.opt.wasm
Some VMs may not accept this binary because it has a large number of data segments. Run the limit-segments pass to merge segments.

And I get the same validation error with a.opt.wasm.

weijiekoh commented 3 years ago

Thank you so much for diving into this issue!

I see that it's a hardcoded constant. Would the team be interested if I submitted a PR that would make the maximum data segments a CLI config option?

I generated the WASM file using the circom tool: https://github.com/iden3/circom. The context is that I'm writing a pretty large zero knowledge circuit using the circom language. To generate proofs for this circuit, we need to generate a witness file. The WASM file does exactly this.

Anyway, I've worked around this problem by generating the witness a different way (namely by using C++ code that circom produces). It would, however, be nice if wasmer could do this too.

Hywan commented 3 years ago

Yeah sure, try to submit a patch to the bytecodealliance/wasm-tools, and link this issue please :-).

Hywan commented 3 years ago

I'm closing this issue since it's not related to Wasmer itself :-).

weijiekoh commented 3 years ago

Thanks! I'll ping again if/when the patch is done :D