Commit Graph

220 Commits

Author SHA1 Message Date
Andrew Kaster
4a1bcb0380 LibWasm: Reject indirect calls to external function references
This fixes a test in the WebAssembly spec test suite that was
added in 924c1f816d

(cherry picked from commit a3157c8c6960541ba0a621680aadea7c9092bb03)
2024-11-13 10:17:34 -05:00
Ali Mohammad Pur
7c96d9a96d LibWasm+LibWeb+test-wasm: Refcount Wasm::Module for function references
Prior to funcref, a partial chunk of an invalid module was never needed,
but funcref allows a partially instantiated module to modify imported
tables with references to its own functions, which means we need to keep
the second module alive while that function reference is present within
the imported table.
This was tested by the spectests, but very rarely caught as our GC does
not behave particularly predictably, making it so the offending module
remains in memory just long enough to let the tests pass.

This commit makes it so all function references keep their respective
modules alive.

(cherry picked from commit a60ecea16abe62aae988ba877fdb98466d2919d3)
2024-11-13 10:17:34 -05:00
Andreas Kling
b0d8d9ef4a LibWasm: Make BytecodeInterpreter overrides final
This gives us free devirtualization of some hot calls inside the
bytecode interpreter. Most notably the did_trap() checks.

Modest performance improvement on the https://figma.com/ landing page.

(cherry picked from commit 2f3128333dcd5a781f35272800631d6cf03803ad)
2024-11-12 22:26:21 -05:00
Ali Mohammad Pur
28af9da66f LibWasm: Allow all Value::to<Integral>() calls
This brings back the old behaviour of Value::to<short>() (and other
similar calls), which WASI depends on.
To make sure all similar issues are caught in the future, this commit
also introduces an static assertion in Value::to().

(cherry picked from commit 0d05ab2ad0cc56cc6fe9ee647dfcfda44a92b4f2)
2024-11-12 22:26:21 -05:00
Ali Mohammad Pur
7d1b4c9ee0 LibWasm: Use braces to initialize Reference::Foo classes
These have no declared constructor, so apple clang and clang clang seem
to disagree on some versions.

(cherry picked from commit d2ff92077b2a4ead0a1ef65e7131702e1152cbeb)
2024-11-12 22:26:21 -05:00
Diego Frias
a97996351f LibWasm: Remove type information from Value
Gets fib(30) from 380ms to 340ms.

(cherry picked from commit a58704296cd36d3080dbf8f1be49300de2c25ea9;
amended to comment out two dbgln_if()s that no longer compile after
this change if WASM_TRACE_DEBUG is defined to 1 -- see comments on
LadybirdBrowser/ladybird#960. Likewise, one dbgln_if() in Wasi.cpp
got commented out as well)
2024-11-12 19:58:14 -05:00
Diego Frias
dea772ab0f LibWasm: Directly remove from the stack when clearing a label
Theoretically, the previous "pop, then push" method should be faster,
but it's actually faster to just remove from the stack directly.

(cherry picked from commit a2448308fd90ba0aafd42587a091c0da3c037ba9)
2024-11-12 19:58:14 -05:00
Diego Frias
764529cdc1 LibWasm: Inline bytecode interpreter instruction interpretation
Gets fib(30) from 480ms to 380ms.

(cherry picked from commit b73b17aab457bdeb33ada917ae7217aab81a664b)
2024-11-12 19:58:14 -05:00
Diego Frias
6a1d2f990c LibWasm: Split main interpreter stack into three
Instead of one stack to hold frames, labels, and values, there is now
three separate stacks. This speeds up fib(30) from 580ms to 480ms.

(cherry picked from commit a3b077c6415e0f4038b8c8da1e1e9c9cc2df5f0a)
2024-11-12 19:58:14 -05:00
Diego Frias
f7a322b1fb LibWasm: Simplify downcasting in bytecode interpreter
(cherry picked from commit 15510fb42e03b71e39b42d08dfbc7f7a822a0a1e)
2024-11-12 19:58:14 -05:00
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