Commit Graph

210 Commits

Author SHA1 Message Date
Diego Frias
ed11132675 LibWasm: Clean up module sections API
Remove `for_each_section_of_type` in favor of making the module's
sections defined as distinct fields. This means it is no longer possible
to have two of the same section (which is invalid in WebAssembly, for
anything other than custom sections).

(cherry picked from commit 23cfee22058880f8251d9627e7f6640d466c37a9)
2024-10-31 20:13:13 -04:00
Nico Weber
32720f3a2d Userland: Use read_until_eof() more
No intended behavior change.
2024-09-09 23:25:08 +02:00
Diego Frias
e5b14aadb0 LibWasm: Remove Module::functions
`Module::functions` created clones of all of the functions in the
module. It provided a _slightly_ better API, but ended up costing around
40ms when instantiating spidermonkey.

(cherry picked from commit dc52998341bb86ad8fb790fb72f943e43b16e8e5)
2024-07-29 14:34:01 +02:00
Diego Frias
c0b69a3466 LibWasm: Remove unused vector methods of the interpreter
(cherry picked from commit 4e8376d07e6b928572f7b4a9bb3b6a4468a8b6e7)
2024-07-29 14:34:01 +02:00
Diego Frias
519d3674fa LibWasm: Fix SIMD shuffle and swizzle
`swizzle` had the wrong operands, and the vector masking boolean logic
was incorrect in the internal `shuffle_or_0` implementation. `shuffle`
was previously implemented as a dynamic swizzle, when it uses an
immediate operand for lane indices in the spec.

(cherry picked from commit 9cc3e7d32d150dd30d683c1a8cf0bd59676f14ab)
2024-07-29 14:34:01 +02:00
Diego Frias
f92916b4ce LibWasm: Fix SIMD bit shift right
Set the sign in the vector's element type (even though it's a bit
redundant).

(cherry picked from commit d841742c3582e086613bda41a9679dfa6f7f2393)
2024-07-29 14:34:01 +02:00
Diego Frias
73ed02a3d4 LibWasm: Implement the rest of the SIMD conversions
(cherry picked from commit 4b9649282ebfef46e645e404e09f3b50b578d56f)
2024-07-29 14:34:01 +02:00
Diego Frias
91fe4b8a30 LibWasm: Fix v128.any_true instruction
(cherry picked from commit 21c5084d233a0e04bc1235c0f53852d0cd2d3a08)
2024-07-29 14:34:01 +02:00
Diego Frias
b47c9dfe45 LibWasm: Implement integer conversion and narrowing SIMD instructions
(cherry picked from commit 616048c67e47c88d8426b46f83ecf13e6f9826e8)
2024-07-29 14:34:01 +02:00
Diego Frias
c510c5375f LibWasm: Implement bitmask and float conversion instructions
(cherry picked from commit 146646b59741c1300461056e384dae50815d1621)
2024-07-29 14:34:01 +02:00
Diego Frias
d00e37fa20 LibWasm: Remove some unnecessary memory checks
Also make `store_to_memory` take a `MemoryArgument` so that we no longer
have to make "synthetic instructions" in some scenarios.

(cherry picked from commit ea67bc989f58e27a28f473819e4265a0ad0af97f)
2024-07-29 14:34:01 +02:00
Diego Frias
2f317eef75 LibWasm: Remove some dead code
(cherry picked from commit 56ae6b3968b9e688a3ea5500228c1544c8afcbc3)
2024-07-29 14:34:01 +02:00
Ali Mohammad Pur
4b32aaeb5d LibWasm: Replace a hashtable with an RBTree to make instantiation faster
...by about 40%.

(cherry picked from commit 8cf0f36f7d917ce9f0f6759f27ba0553db00e82a)
2024-07-29 14:34:01 +02:00
Ali Mohammad Pur
22f9386ab6 LibWasm: Make import errors a bit more descriptive and helpful
Instead of "yeah something went wrong", show what went wrong.

(cherry picked from commit e22408b8b47c5ae25d4b043a7b6da4b5477871c9)
2024-07-29 14:34:01 +02:00
Ali Mohammad Pur
68aa4cbb91 LibWasm: Make Absolute/Negate<SignedIntegral> explicitly work mod 2^N
Previously we relied on signed overflow, this commit makes the same
behaviour explicit (avoiding UB in the process).

(cherry picked from commit 8c8310f0bddc874a9f7f07c4158f0abc799357d4)
2024-07-16 17:35:43 +02:00
Diego Frias
bc5c549e7f LibWasm: Correctly validate v128_load*_lane instructions
(cherry picked from commit 8a0ef17d9a9621ab4bd52dc402c0fbd57944d42c)
2024-07-16 17:35:43 +02:00
Diego Frias
a2bb6e1cfc LibWasm: Implement rest of SIMD load/store instructions
Also implement `v128.any_true`.

(cherry picked from commit f5326f1747b9559993cb6f89841de2fc54c10387)
2024-07-16 17:35:43 +02:00
Enver Balalic
34b3015c16 LibWasm: Implement most of iNxM SIMD operations
With this we pass an additional ~2100 tests.
We are left with 7106 WASM fails :).

There's still some test cases in the iNxM tests that fail with
this PR, but they are somewhat weird.

(cherry picked from commit b4acd4fb0b7f4105c7ef673ccc00904114c3c468)

Co-authored-by: Diego Frias <styx5242@gmail.com>
2024-07-16 17:35:43 +02:00
Diego
b896f27a45 LibWasm: Fix sign issues in SIMD cmp ops
(cherry picked from commit 1e1dcd89438c5b0b8ad34682de4f1c7c62cbacb9)
2024-07-16 17:35:43 +02:00
Diego Frias
9ae48f6a49 LibWasm: Make SIMD float min/max operations binary ops
They previously acted like comparison operators, which was not correct.

(cherry picked from commit d6acda2047dec0a0ba6eda50039feff816c3e82b)
2024-07-16 17:35:43 +02:00
Diego
c3af74f0a2 LibWasm: Validate stack correctly in v128_store*_lane instructions
Previously the validator put a `v128` on the stack, which is not what
the spec defines.

(cherry picked from commit 0d38572d8bd2a276be1b6066b62efd376ddbd4d6)
2024-07-16 17:35:43 +02:00
Diego
59628b5eeb LibWasm: Make memory.grow grow the memory's type
After a `memory.grow`, the type of the memory instance should be
updated so potential memory imports on the boundary are unlinkable.

(cherry picked from commit cdb6e834a1c0eaa6e62a9018026a599916332ab3)
2024-07-16 17:35:43 +02:00
Diego
e5a842c78e LibWasm: Fix loop arity for single-type blocktypes
Single-type blocktypes previously gave loop labels an arity of 1, even
though they're shorthand for `[] -> [T]`.

(cherry picked from commit ad6a80144c23f9ccdeeccb123a9de85396524040)
2024-07-16 17:35:43 +02:00
Diego
7df774c473 LibWasm: Implement SIMD bitwise operations
(cherry picked from commit 2ab676860e56216cf0560dac1aafd4e5656ec586)
2024-07-16 17:35:43 +02:00
Diego
5e8dba07a6 LibWasm: Give names to functions exported to JS via ref.func
https://webassembly.github.io/spec/js-api/index.html#name-of-the-webassembly-function
(cherry picked from commit e8fd8982f82e91f97b24523f3ee60eef774990dd)
2024-07-10 01:10:12 +02:00
Diego
3bec014c53 LibWasm: Remove Wasm::ValueType::Kind::Null* variants
As far as I know, they're not in the spec and don't serve any purposes
in the internals of LibWasm.

(cherry picked from commit 5382fbb6171555264e29872029330e1373b39671)
2024-07-10 01:10:12 +02:00
Diego
29ae76925f LibWasm: Fix comparisons between 0.0 and -0.0
According to the spec, -0.0 < 0.0.

(cherry picked from commit 31c7e98a4a46c2d0ef93c5fca47d64d05b96449f)
2024-07-10 01:10:12 +02:00
Diego
3ed4cac2a5 LibWasm: Fix some floating-point conversion issues
NaN bit patterns are now (hopefully) preserved. `static_cast` does not
preserve the bit pattern of a given NaN, so ideally we'd use some other
sort of cast and avoid `static_cast` altogether, but that's a large
change for this commit. For now, this fixes the issues found in spec
tests.

(cherry picked from commit c882498d4450c4c2e46d77a8ab36afc4eb412c00)
2024-07-10 01:10:12 +02:00
Diego
d7d36a28de LibWasm: Validate potentially empty else branch in if instruction
(cherry picked from commit fce8ed15630a4969be7c9761b9b7d3cef0530cc6)
2024-07-10 01:10:12 +02:00
Hendiadyoin1
cd454a1e3d LibWasm: Use shuffle_or_0 in for vector swizzles and shuffles
Otherwise we'd hit a VERIFY in AK::SIMD::shuffle() when that operand
contains an out-of-range value, the spec tests indicate that a swizzle
with an out-of-range index should return 0.
2024-07-05 00:52:30 +02:00
Diego
0520de42f1 LibWasm: Check source and destination offsets in memory.init
Overflows are no longer possible.

(cherry picked from commit 3b40667413ce0885d10491589207b9556d5161d0)
2024-06-26 22:13:13 +02:00
Diego
da3aaac7ea LibWasm: Check exports for valid ref.func targets
(cherry picked from commit 0e705f431eab80635dd24857aaa4606b7907c325)
2024-06-26 22:13:13 +02:00
Diego
145fb50fe0 LibWasm: Ensure that global.get only accesses imports in const exprs
(cherry picked from commit bd97091cbb4fd12cd323cedfa11f4c6f33250958)
2024-06-26 22:13:13 +02:00
Diego
ef9f3fd091 LibWasm: Check data segment offset at correct time during instantiation
The data segment offset should be checked _before_ checking if the
contents of the segment are non-existent.

(cherry picked from commit 78c56d80f90f913e4cbc14c865af308c6af9aeae)
2024-06-26 22:13:13 +02:00
Diego
d7413560f8 LibWasm: Report start function traps during instantiation
(cherry picked from commit c2a0c4f58126e9db833e482b7611c3cea18622f6)
2024-06-26 22:13:13 +02:00
Diego
a91f00fed7 LibWasm: Improve element validation and instantiation
(cherry picked from commit 3225e6fad2b077a160d682ec3953a9d8fb49ffec)
2024-06-26 22:13:13 +02:00
Diego
9605b0f28d LibWasm: Implement rest of table instructions
(cherry picked from commit 4c3071c7c209c2e53c73862be72c9b493f263e78)
2024-06-26 22:13:13 +02:00
Diego
cafc66f272 LibWasm: Tighten validation algorithm
The big improvement included in this commit is stack height mismatch
validation. There are other minor improvements included (related to the
validation algorithm). The method of supporting stack polymorphism has
changed to be more like the spec, which was necessary for confidently
handling stack height mismatches.

See:
https://webassembly.github.io/spec/core/appendix/algorithm.html
(cherry picked from commit 9b58271f8b6de2dbfff416780a54e0322f9c6799)
2024-06-13 23:14:39 +02:00
Diego
cdf15887db LibWasm: Refactor validator to use one stack for frame information
Previously, the validator had a lot of extraneous information related to
frames. Now, there's just one stack with all the necessary information
derived from it.

(cherry picked from commit ad54b69de9df6ccd44178cbe49779e313f95f273)
2024-06-13 23:14:39 +02:00
Diego
f6d7702411 LibWasm: Make loops work
This commit should hopefully allow for `loop.wast` to be run in CI.

(cherry picked from commit 6ca6fd248a14ab394f11131c3df029b486952d54)
2024-06-13 23:14:39 +02:00
Diego
1c86b8146a LibWasm: Make memory.fill fill with single bytes
Previously, `memory.fill` filled memory with 4-byte values, even though
`memory.fill` should fill with just one byte. Also fixes some other
issues with some of the bulk memory instructions, like `memory.init`.

(cherry picked from commit d8ee2e343df25d12637e08d54908b4fd86a22dc3)
2024-06-13 23:14:39 +02:00
Diego
510bfbbbc3 LibWasm: Validate imports
(cherry picked from commit a1ed3e5d8f4f7b9f44ec4d3e15432d60cba9f2ee)
2024-06-13 23:14:39 +02:00
Diego
985c4c590d LibWasm: Validate that data section exists for some instructions
(cherry picked from commit 5e5df136849c1d450a6e0dd4af3805f7e56f6e76)
2024-06-13 23:14:39 +02:00
Diego
be3bc9bd7c LibWasm: Disallow multiple start sections
(cherry picked from commit 6b9977a265b8175cf4da68d601ded427cb49e44e)
2024-06-13 23:14:39 +02:00
Diego
96fb79bb98 LibWasm: Make sure no duplicate export names are possible
(cherry picked from commit c51c018fb13f4fb182da5b12e9e3fda69f6b72ef)
2024-06-13 23:14:39 +02:00
Diego
5502c1a1f8 LibWasm: Validate number of data sections
(cherry picked from commit f7d3ab8e160a61abddfe4db9c8d79260e890bb41)
2024-06-13 23:14:39 +02:00
Enver Balalic
b43d2307cd LibWasm: Implement f32x4 and f64x2 arithmetic SIMD ops
Adds all the arithmetic ops for f32x4 and f64x2 SIMD instructions.

With this, we pass 8375 additional tests :)

Quite a few of the spec tests for this are still failing.
I confirmed with the wasmer runtime manually for a number of them,
and we seem to match their and results. I'm not really sure
what's happening here, a spec bug or wasmer is broken in
the same way.

18476 failed before.
10101 failed after.
2024-06-09 16:30:09 +02:00
Diego
ebee41494e LibWasm: Check data section offset for overflow during instantiation 2024-06-09 16:30:09 +02:00
Diego
ad9457b725 LibWasm: Properly check memory.copy addresses
Prevents overflow when checking that `memory.copy` addresses are valid.
This prevents a potential crash in the VM.
2024-06-02 19:45:54 +02:00
Diego
308592969c LibWasm: Properly check table bounds in element instantiation
Offset is now checked using saturating addition to avoid overflow. This
prevents a crash in the VM during instantiation.
2024-06-02 19:31:25 +02:00