Skip to content
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

Merged

Conversation

alexcrichton
Copy link
Member

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.

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.
Copy link
Member

@fitzgen fitzgen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice.

@alexcrichton alexcrichton merged commit 5dd59d7 into bytecodealliance:main Oct 12, 2022
@alexcrichton alexcrichton deleted the refactor-operator-printing branch October 12, 2022 18:11
alexcrichton added a commit to alexcrichton/wasm-tools that referenced this pull request Oct 13, 2022
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.
alexcrichton added a commit that referenced this pull request Oct 14, 2022
* 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
@Robbepop
Copy link
Contributor

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 read_memarg_of_align: https://github.com/bytecodealliance/wasm-tools/pull/795/files#diff-154ee93e092342f2f38473cbc114e5d864ebf78c4a302e5988805f4f659e45f3

This reminded me of this wasmi validation error (note that wasmi uses wasmparser for validation): wasmi-labs/wasmi#570

However, wasmi uses the older version in which this check was still existing. 🤔
I would love to get some clarification. 😅

@alexcrichton
Copy link
Member Author

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.

@Robbepop
Copy link
Contributor

@alexcrichton Thanks a lot for the clarification!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants