New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor and simplify wasm opcode printing #795
Refactor and simplify wasm opcode printing #795
Conversation
While looking through `wasmprinter` for other issues I was struck with inspiration to simplify the printing of operators with the `for_each_operator!` macro to avoid the verbosity of the full trait implementation. Along the way I made a few other adjustments as well: * `*RoundingAverage*` instructions now use `Avgr` in the name to match the instruction name. * `MemArg` now specifies its `max_align` in a field to uniformly handle this without needing per-instruction treatment. * Alignment of atomic operators now properly validates that the alignment must be specified at the maximum. * The text representation of a relaxed simd instruction was updated to have `*_sat_*` in the name to match the in-Rust name.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very nice.
This commit fixes a minor issue found during fuzzing that is a regression from bytecodealliance#795. Specifically some encodings of block types would lead to printing the binary format with two spaces after the `block` instruction but printing again would only print one space. This was due to a number of issues fixed in this commit: * Primarily `wasmprinter` has been reorganized to no longer assume some methods print the leading whitespace. This basically "randomly" moves around space printing in `wasmprinter`. To help make this less "random" I've also added a test in `roundtrip.rs` that all whitespace tokens in the `wasmprinter`-produced output are not "bad whitespace" which means there's no trailing whitespace or double-spaces printed. This caught a few issues with prior iterations of this commit * Additionally when looking more at block type parsing and printing I was reminded that much of the processing here was an artifact of historical compatibility with `wabt` where `wabt` canonicalized non-MVP encodings into MVP encodings where possible. I removed this canonicalization to remain faithful to the original binary format since `wabt` is no longer used as a reference test. This was orthogonal to the spacing changes above but was something that seemed like it should be fixed regardless. A `*.dump` test was added to assert that the encodings are as expected and not canonicalized, even when possible.
* Update blockty printing and space handling This commit fixes a minor issue found during fuzzing that is a regression from #795. Specifically some encodings of block types would lead to printing the binary format with two spaces after the `block` instruction but printing again would only print one space. This was due to a number of issues fixed in this commit: * Primarily `wasmprinter` has been reorganized to no longer assume some methods print the leading whitespace. This basically "randomly" moves around space printing in `wasmprinter`. To help make this less "random" I've also added a test in `roundtrip.rs` that all whitespace tokens in the `wasmprinter`-produced output are not "bad whitespace" which means there's no trailing whitespace or double-spaces printed. This caught a few issues with prior iterations of this commit * Additionally when looking more at block type parsing and printing I was reminded that much of the processing here was an artifact of historical compatibility with `wabt` where `wabt` canonicalized non-MVP encodings into MVP encodings where possible. I removed this canonicalization to remain faithful to the original binary format since `wabt` is no longer used as a reference test. This was orthogonal to the spacing changes above but was something that seemed like it should be fixed regardless. A `*.dump` test was added to assert that the encodings are as expected and not canonicalized, even when possible. * Adjust spacing with `type_index` and fix test expectations
Sorry for being very late. While reading the changes introduced in this PR I noticed that this removed a check for maximum alignment that existed in the removed This reminded me of this However, |
I removed that because it was duplicating logic already in the validator, and generally where possible we try to have the parser be relatively flexible about what it reads and the validator is what makes sure it's all lined up. With the validator in use there shouldn't have been any change in modules accepted or rejected with this change. |
@alexcrichton Thanks a lot for the clarification! |
While looking through
wasmprinter
for other issues I was struck with inspiration to simplify the printing of operators with thefor_each_operator!
macro to avoid the verbosity of the full trait implementation. Along the way I made a few other adjustments as well:*RoundingAverage*
instructions now useAvgr
in the name to match the instruction name.MemArg
now specifies itsmax_align
in a field to uniformly handle this without needing per-instruction treatment.*_sat_*
in the name to match the in-Rust name.