Commit Graph

26 Commits

Author SHA1 Message Date
Julian Seward
07652ca0d4 wasm->CLIF: fn translate_operator: Select/TypedSelect: add missing bitcasts
The translation of Operator::Select and Operator::TypedSelect for vector-typed
operands, lacks the relevant bitcasting of the operands to I8X16.  This commit
adds it.
2021-01-11 11:59:05 +01:00
Julian Seward
ab65d8f10c wasm->CLIF translation: consistently bitcast V128 values that are block formal parameters.
In the current translation of wasm (128-bit) SIMD into CLIF, we work around differences in the
type system models of wasm vs CLIF by inserting `bitcast` (a no-op cast) CLIF instructions before
more or less every use of a SIMD value.  Unfortunately this was not being done consistently and
even small examples with a single if-then-else diamond that produces a SIMD value, could cause a
verification failure downstream.  In this case, the jump out of the "else" block needed a
bitcast, but didn't have one.

This patch wraps creation of CLIF jumps and conditional branches up into three functions,
`canonicalise_then_jump` and `canonicalise_then_br{z,nz}`, and uses them consistently.  They
first cast the relevant block formal parameters, then generate the relevant kind of branch/jump.
Hence, provided they are also used consistently in future to generate branches/jumps in this
file, we are protected against such failures.

The patch also adds a large(ish) comment at the top explaining this in more detail.
2020-10-21 17:43:49 +02:00
Alex Crichton
2c6841041d Validate modules while translating (#2059)
* Validate modules while translating

This commit is a change to cranelift-wasm to validate each function body
as it is translated. Additionally top-level module translation functions
will perform module validation. This commit builds on changes in
wasmparser to perform module validation interwtwined with parsing and
translation. This will be necessary for future wasm features such as
module linking where the type behind a function index, for example, can
be far away in another module. Additionally this also brings a nice
benefit where parsing the binary only happens once (instead of having an
up-front serial validation step) and validation can happen in parallel
for each function.

Most of the changes in this commit are plumbing to make sure everything
lines up right. The major functional change here is that module
compilation should be faster by validating in parallel (or skipping
function validation entirely in the case of a cache hit). Otherwise from
a user-facing perspective nothing should be that different.

This commit does mean that cranelift's translation now inherently
validates the input wasm module. This means that the Spidermonkey
integration of cranelift-wasm will also be validating the function as
it's being translated with cranelift. The associated PR for wasmparser
(bytecodealliance/wasmparser#62) provides the necessary tools to create
a `FuncValidator` for Gecko, but this is something I'll want careful
review for before landing!

* Read function operators until EOF

This way we can let the validator take care of any issues with
mismatched `end` instructions and/or trailing operators/bytes.
2020-10-05 11:02:01 -05:00
Chris Fallin
a0646c8d3f Account for duplicated if-block params on end op in unreachable case.
This is a close analogue to bnjbvr@'s fix in commit 518b7a7e. Similar to
that fix, this PR fixes a bug in which the Wasm translator could
misalign its value stack and either mistranslate or cause a panic with a
type-checking error.

Found via fuzzing by :decoder in SpiderMonkey (bug 1664453).
2020-09-14 16:41:53 -07:00
Benjamin Bouvier
518b7a7e23 wasm: Remove duplicated parameters when popping an If
Parameters are duplicated when pushing an If block, so they're available
to the Else block without an extra heap allocation. However, when
truncating the stack after popping the If control frame, the stack size
at entry doesn't account for the duplicated parameters. That is
intentional: the Else block uses this value to know what's the stack
size when it is entered, so there's nothing to change there.

This patch makes the wasm translation truncates the value stack to the
right size after an If block, by taking those duplicated parameters into
account.
2020-09-11 13:12:48 +02:00
Chris Fallin
dd5a5ebdbc Wasm translation bugfix: properly clean up value stack for else-branch when if-branch ends in unreachable.
The Wasm translation handles unreachable code sections
specially, skipping ops until the end of a block and a control-flow
merger at which code becomes reachable again. Unfortunately, while the
ordinary else-op handler properly sets up the value stack for the
else-branch with the parameters to the if/else, the unreachable-case
else-op handler did not. This resulted in a bad translation and CLIF
type error despite valid Wasm.

Found via fuzzing by :decoder in
https://bugzilla.mozilla.org/show_bug.cgi?id=1657895.
2020-08-07 18:33:04 -07:00
Nick Fitzgerald
1a4f3fb2df Update deps and tests for anyref --> externref
* Update to using `wasmparser` 0.55.0
* Update wasmprinter to 0.2.5
* Update `wat` to 1.0.18, and `wast` to 17.0.0
2020-05-14 12:47:37 -07:00
Andrew Brown
27532410d2 Fix local.set and local.tee types for SIMD
Because Wasm SIMD vectors store their type as `v128`, there is a mismatch between the more specific types Cranelift uses and Wasm SIMD. Because of this mismatch, Wasm SIMD translates to the default Cranelift type `I8X16`, causing issues when more specific type information is available (e.g. `I32x4`). To fix this, all incoming values to SIMD instructions are checked during translation (not runtime) and if necessary cast from `I8X16` to the appropriate type by functions like `optionally_bitcast_vector`, `pop1_with_bitcast` and `pop2_with_bitcast`. However, there are times when we must also cast to `I8X16` for outgoing values, as with `local.set` and `local.tee`.

There are other ways of resolving this (e.g., see adding a new vector type, https://github.com/bytecodealliance/cranelift/pull/1251) but we discussed staying with this casting approach in https://github.com/bytecodealliance/wasmtime/issues/1147.
2020-03-17 11:44:25 -07:00
Nick Fitzgerald
9b3ac10ebc wasm: Add support for passive data and element segments (#1389)
This is part of the bulk memory and reference types proposals.
2020-02-15 14:53:32 -08:00
Nick Fitzgerald
ce1ee2d2f5 Enable ref.func global initializers (#1380)
* Fix comment referencing an outdated instruction name

* cranelift-wasm: Enable `ref.func` global initializers
2020-02-07 11:44:07 -08:00
Ryan Hunt
710182ad26 Wasm: Add support for nullref type
Reference types now supports proper a nullref type. This commit changes
the Wasm translator to support this.
2020-01-23 16:31:34 -06:00
Ryan Hunt
32a95a89eb Wasm: Add support for typed select instruction
Reference types introduces a typed select operation. It has identical execution
semantics so no codegen change is needed.
2020-01-23 16:31:34 -06:00
Andrew Brown
46e58fbaaa Bitcasting at control flow exits (#1272)
* Bitcast vectors immediately before a return

* Bitcast vectors immediately before a block end

* Use helper function for bitcasting arguments

* Add FuncTranslationState::peekn_mut; allows mutating of peeked values

* Bitcast values in place, avoiding an allocation

Also, retrieves the correct EBB header types for bitcasting on Operator::End.

* Bitcast values of a function with no explicit Wasm return instruction

* Add Signature::return_types method

This eliminates some duplicate code and avoids extra `use`s of `Vec`.

* Add Signature::param_types method; only collect normal parameters in both this and Signature::return_types

* Move normal_args to Signature::num_normal_params method

This matches the organization of the other Signature::num_*_params methods.

* Bitcast values of Operator::Call and Operator::CallIndirect

* Add DataFlowGraph::ebb_param_types

* Bitcast values of Operator::Br and Operator::BrIf

* Bitcast values of Operator::BrTable
2020-01-06 15:33:22 -08:00
Nick Fitzgerald
c1c55607e1 cranelift-wasm: Check for u32::MAX function indices (#1307)
As an implementation-specific limit, we do not allow the full index space of
`0..=2^32 - 1` because we reserve index `2^32 - 1` for ourselves in
`cranelift-entity`.

Fixes #1306
2019-12-21 13:37:42 -08:00
Benjamin Bouvier
2b6ea31621 [wasm] Include more large tests; 2019-10-23 10:15:49 +02:00
Nick Fitzgerald
bae0257fc3 cranelift-wasm: Fix reachability tracking for if .. else .. end
We weren't previously keeping track of quite the right information for whether
an `if .. else .. end`'s following block was reachable or not. It should be
reachable if the head is reachable and either the consequent or alternative end
reachable (and therefore fall through to the following block) or do an early
`br_if` to it.

This commit rejiggers `ControlStackFrame::If` to keep track of reachability at
the end of the consequent (we don't need to keep track of it at the end of the
alternative, since that is simply `state.reachable`) and adds Wasm tests for
every reachability situation we can encounter with `if .. else .. end`.

Fixes #1132
2019-10-15 10:37:59 -07:00
Nick Fitzgerald
913d26841a cranelift-wasm: Jump to the destination block at end of consequent
This commit fixes a bug where at the end of an `if..else..end`'s consequent
block, we would sometimes erroneously jump to the `else` block instead of to the
following destination block. Not good!
2019-10-02 17:33:56 -07:00
Nick Fitzgerald
10be3e4ba8 cranelift-wasm: support multi-value Wasm (#1049)
This commit introduces initial support for multi-value Wasm. Wasm blocks and
calls can now take and return an arbitrary number of values.

The encoding for multi-value blocks means that we need to keep the contents of
the "Types" section around when translating function bodies. To do this, we
introduce a `WasmTypesMap` type that maps the type indices to their parameters
and returns, construct it when parsing the "Types" section, and shepherd it
through a bunch of functions and methods when translating function bodies.
2019-10-02 12:40:35 -07:00
Andrew Brown
ca6449626f Verify that cranelift-wasm can translate SIMD instructions
To do so we must use a new version of wabt-rs that allows us to enable features (e.g. SIMD) when translating the wasmtests
2019-08-26 16:12:06 -07:00
Dan Gohman
3ff8867e57 Split the default edge of a br_table.
When splitting critical edges for a br_table to handle arguments being
passed, split the default edge along with the normal table edges.
2018-11-06 15:53:05 +01:00
Dan Gohman
bf041e3ae2 Move return_at_end out of Settings and into the wasm FuncEnvironment. (#547)
* Move `return_at_end` out of Settings and into the wasm FuncEnvironment.

The `return_at_end` flag supports users that want to append a custom
epilogue to Cranelift-produced functions. It arranges for functions to
always return via a single return statement at the end, and users are
expected to remove this return to append their code.

This patch makes two changes:
 - First, introduce a `fallthrough_return` instruction and use that
   instead of adding a `return` at the end. That's simpler than having
   users remove the `return` themselves.

 - Second, move this setting out of the Settings and into the wasm
   FuncEnvironment. This flag isn't something the code generator uses,
   it's something that the wasm translator uses. The code generator
   needs to preserve the property, however we can give the
   `fallthrough_return` instruction properties to ensure this as needed,
   such as marking it non-cloneable.
2018-10-05 06:43:22 -07:00
Dan Gohman
0e22c74085 Track wasm reachability explicitly.
Maintain an explicit "reachable" flag when decoding wasm. Push placeholder
frames on the control-flow stack instead of just maintaining a count of
the stack depth in unreachable code, so that we can whether If blocks
have Elses, and whether block exits are branched to, in all contexts.

Fixes #217.
2018-02-26 15:24:33 -08:00
Pat Hickey
cced2c8b0c Fix wat syntax so wasm tests pass (#199)
* wasm testsuite: ignore hidden files in test dir

and report a rejected file. it was picking up vim .swp files

* wasmtests: correct wat syntax in icall.wat
2017-11-27 12:46:53 -08:00
Dan Gohman
1ab207b93c Add support for emitting code with a single return at the end. (#153)
This also enables testing of the wasmtests tests.

This also updates for wabt updating to the official "wat" filename
extension, as opposed to "wast".
2017-09-12 13:27:36 -07:00
Jakob Stoklund Olesen
27e9e16077 Add a FuncEnvironment::make_indirect_sig() callback.
The function environment is now expected to keep track of the function
signatures in the module, and it is asked to generate Cretonne
signatures to be used for indirect calls.

The combination of make_indirect_sig() and translate_call_indirect()
callbacks allow the runtime to insert additional function arguments for
indirect calls such as vmctx pointers and CFI-style signature identifiers.
2017-09-06 10:28:11 -07:00
Denis Merigoux
ee9989c4b9 Dumped code from the wasm2cretonne repo.
Integrated wasm test suite translation as cretonne test

Fixes #146.
Fixes #143.
2017-08-28 15:57:43 -07:00