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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

馃 Non-trapping mode for wasm-smith #659

Merged
merged 11 commits into from
Jul 25, 2022

Conversation

itsrainy
Copy link
Contributor

@itsrainy itsrainy commented Jun 29, 2022

Partially addresses #266. This inserts instructions around or in place of instructions that have the potential to trap.

Completed:

  • Replace unreachables with dummy-value returns
  • Return dummy results for call_indirect calls
  • Return dummy value for out of range load operations
  • Drop the value from the stack for out of range store operations
  • Change the divisor from 0 to 1 for div and rem operations
  • Change the divisor to 1 for signed div and rem operations that would result in 2n-1
  • Handle float -> int conversion
    • Will trap for NaN, Infinity, and out of range values (ref)

Not completed, but punting to a later PR:

  • Handle table operations
  • Handle memory.{init,copy,fill} (ref)
  • Vector/SIMD Instructions (i'm not sure what to do with these)

@itsrainy itsrainy marked this pull request as ready for review June 30, 2022 15:07
@itsrainy
Copy link
Contributor Author

putting this up for some initial review as I work on the todos

Copy link
Member

@alexcrichton alexcrichton left a comment

Choose a reason for hiding this comment

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

This is looking awesome, thanks for this!

One thing I might recommend adding, could a fuzzer (or a runner like crates/fuzz-stats) be added to test out that there are no traps? I presume that at this moment in time it'd quickly find the todo! panics but it might be useful to have something in-tree which tests the no-trap mode and asserts that traps in fact do not come out.

crates/wasm-smith/src/core.rs Outdated Show resolved Hide resolved
// there is an `unreachable` on the stack. This would allow
// us to keep executing the rest of the code following the
// `unreachable`, but also is a ton more work, and it isn't
// clear that it would pay for itself.
Copy link
Member

Choose a reason for hiding this comment

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

Personally I agree with this comment, and the more prudent thing would probably be to simply disable generation of the unreachable instruction in the first place if we really want to lean into it.

Copy link
Member

Choose a reason for hiding this comment

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

I think this is a question we can kick down the road to a different PR. Personally I don't think we should completely 100% disable unreachable, so I think this pass would have to handle it somehow anyways.

Comment on lines 64 to 68
// When we can, avoid emitting `drop`s to consume the
// arguments when possible, since dead code isn't
// usually an interesting thing to give to a Wasm
// compiler. Instead, prefer writing them to the first
// page of the first memory if possible.
Copy link
Member

Choose a reason for hiding this comment

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

This definitely makes sense to me, but this may not be the best place to put this perhaps? For example during general code generation we already generate a lot of drop instructions to fixup block/function types. Given the discussion on #655, I wonder if it might be a good idea to (eventually) replace this with drop and then have a later "pass", similar to this one, where it replaces all drop instructions with stores to memory or fold-the-value-into-a-global. That way this could be simpler by "just drop" and then all drop instructions could be uniformly handled no matter where they're generated.

@itsrainy itsrainy changed the title WIP - non-trapping mode for wasm-smith 馃 Non-trapping mode for wasm-smith Jul 21, 2022
@itsrainy
Copy link
Contributor Author

One thing I might recommend adding, could a fuzzer (or a runner like crates/fuzz-stats) be added to test out that there are no traps? I presume that at this moment in time it'd quickly find the todo! panics but it might be useful to have something in-tree which tests the no-trap mode and asserts that traps in fact do not come out.

I think this is also something I will push to a later PR if that's cool (likely once the simd and table instructions are handled, since that's the point when such a fuzzer will be informative).

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.

LGTM with the little comment nitpicks below, thanks!

crates/wasm-smith/src/core/notrap.rs Outdated Show resolved Hide resolved
crates/wasm-smith/src/core/notrap.rs Outdated Show resolved Hide resolved
crates/wasm-smith/src/core/notrap.rs Outdated Show resolved Hide resolved
crates/wasm-smith/src/core/notrap.rs Outdated Show resolved Hide resolved
Co-authored-by: Nick Fitzgerald <fitzgen@gmail.com>
@alexcrichton alexcrichton merged commit f229c4f into bytecodealliance:main Jul 25, 2022
@itsrainy itsrainy deleted the wasm-smith-no-traps-mode branch July 26, 2022 14:33
code-terror pushed a commit to code-terror/wasm-tools that referenced this pull request Aug 24, 2022
* Introduce non-trapping mode for wasm-smith

* Ensure integer division and remainder operations don't trap

* Ensure `store` operations don't trap

* Avoid trapping on float -> int conversion for NaN, and Infinities

* Move notrap.rs under wasm-smith core

* Fixed some bugs with the wasm we were emitting

* Fixed the last few bugs with call_indirect rewrites

Co-authored-by: Nick Fitzgerald <fitzgen@gmail.com>

* Clean up unused variables for now

* cargo fmt

* revert validator changes and remove unused import

* fixup comments

Co-authored-by: Nick Fitzgerald <fitzgen@gmail.com>

Co-authored-by: Nick Fitzgerald <fitzgen@gmail.com>
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