WebAssembly / spec

WebAssembly specification, reference interpreter, and test suite.
https://webassembly.github.io/spec/
Other
3.13k stars 445 forks source link

Add text syntax for defining multiple locals of the same type #1588

Open alexcrichton opened 1 year ago

alexcrichton commented 1 year ago

The binary format for WebAssembly defines function locals as a counts of a type to define contiguous sets of typed indices. The s-expression text format for WebAssembly, however, does not have an anlogue of this form and requires that each local is defined individually with its type. For example where the binary format says "I have 5 i32 locals" the text format has to say (local i32 i32 i32 i32 i32).

I work on a binary-to-text printer called wasmprinter in Rust and this printer is additionally fuzzed. Fuzzing historically has brought up that "I have 4 billion i32 locals" is a valid wasm module to print but takes gigabytes more space to print than it does to hold the binary format in memory. For this reason I've matched our validator's limits where the printer will print up to 50k locals before returning an error.

This typically works fine but recent I was working with a wasm module that was failing to validate because it had too many locals in a function. I wanted to print the module to scan over and see if I could see what was going on, but I ended up being unable to print the module due to this restriction as well. While I ended up fixing my issue by debugging via other means, this is what leads me to this issue's feature request.

It would be nice to have textual syntax in the s-expression format for "there are N locals of type T" to mirror the capability of the binary format. This wouldn't have to be used by text printers and the current format could still be used, but if a function says it has 4 million locals it would be helpful to print one item instead of four million items.

rossberg commented 1 year ago

Sounds plausible. Something like (local 5 i32)?

alexcrichton commented 1 year ago

Indeed yeah that would work well and solve the issue at hand.