Commit Graph

62 Commits

Author SHA1 Message Date
Andreas Kling
dc65f54c06 AK: Rename Vector::append(Vector) => Vector::extend(Vector)
Let's make it a bit more clear when we're appending the elements from
one vector to the end of another vector.
2021-06-12 13:24:45 +02:00
Ali Mohammad Pur
aa2916c21b LibWasm: ALWAYS_INLINE some very hot functions
These function couldn't be inlined before because the compiler would've
started flagging invalid paths in Variant as maybe-uninitialized.
2021-06-09 23:05:32 +04:30
Ali Mohammad Pur
45710d0724 LibWasm: Implement saturating float truncation instructions
With these, the only remaining unimplemented instructions are the
following:
- memory.init
- data.drop
- memory.copy
- memory.fill
- table.init
- elem.drop
- table.copy
- table.grow
- table.size
- table.fill
2021-06-09 23:05:32 +04:30
Ali Mohammad Pur
3a44011cd4 LibWasm: Implement sign extension instructions 2021-06-09 23:05:32 +04:30
Ali Mohammad Pur
a4c4dd928b LibWasm: Implement spec-compliant float min/max ops 2021-06-09 23:05:32 +04:30
Sahan Fernando
d02e7b3811 LibWasm: Move Wasm::BytecodeInterpreter into its own header 2021-06-05 14:31:54 +04:30
Ali Mohammad Pur
1b083392fa LibWasm+wasm: Switch to east-const to comply with project style
Against my better judgement, this change is mandated by the project code
style rules, even if it's not actually enforced.
2021-06-04 16:07:42 +04:30
Ali Mohammad Pur
be62e4d1d7 LibWasm: Load and instantiate tables
This commit is a fairly large refactor, mainly because it unified the
two different ways that existed to represent references.
Now Reference values are also a kind of value.
It also implements a printer for values/references instead of copying
the implementation everywhere.
2021-06-04 16:07:42 +04:30
Ali Mohammad Pur
c392a0cf7f LibWasm: Implement the br.table instruction
Unlike its name, this instruction has nothing to do with tables, it's
just a very simple switch-case instruction.
2021-06-04 16:07:42 +04:30
Ali Mohammad Pur
9db418e1fb LibWasm: Read from and write to memory as little-endian
The spec says so, we must do so.
2021-06-04 16:07:42 +04:30
BrandonKi
0d1481be7d LibWasm: Use builtins for clz, ctz, and popcnt 2021-06-03 03:45:06 +04:30
BrandonKi
e53df2ca9d LibWasm: Implement rotr and rotl 2021-06-03 03:45:06 +04:30
Ali Mohammad Pur
ea7ba34a31 AK+LibWasm+LibJS: Disallow Variant.has() on types that aren't contained
Checking for this (and get()'ing it) is always invalid, so let's just
disallow it.
This also finds two bugs where the code is checking for types that can
never actually be in the variant (which was actually a refactor
artifact).
2021-06-02 18:02:47 +02:00
Ali Mohammad Pur
b15a5d6ada LibWasm: Ensure that value signs are preserved when casting
Also makes normal arithmetic operations more spec-compliant by actually
ignoring overflow on them.
2021-06-02 16:09:16 +04:30
Ali Mohammad Pur
02b3238c41 LibWasm: Parse the "extend" set of instructions 2021-06-02 16:09:16 +04:30
Ali Mohammad Pur
9a1853c388 LibWasm: Don't execute the last instruction in the frame after return 2021-06-02 16:09:16 +04:30
Ali Mohammad Pur
b250a6ae7e wasm: Add a way to create dummy function exports
This should allow running modules with their imports stubbed out
in wasm, to debug them.
2021-06-02 16:09:16 +04:30
Ali Mohammad Pur
56bf80251c LibWasm: Implement reference instructions (ref.{null,func,is_null}) 2021-06-02 16:09:16 +04:30
Gunnar Beutner
5f18cf75c5 AK: Replace ByteBuffer::grow with resize()/ensure_capacity()
Previously ByteBuffer::grow() behaved like Vector<T>::resize().
However the function name was somewhat ambiguous - and so this patch
updates ByteBuffer to behave more like Vector<T> by replacing grow()
with resize() and adding an ensure_capacity() method.

This also lets the user change the buffer's capacity without affecting
the size which was not previously possible.

Additionally this patch makes the capacity() method public (again).
2021-05-31 14:49:00 +04:30
Ali Mohammad Pur
e0b17988bd LibWasm: Make f32-add/sub actually perform addition/subtraction
Instead of applying a unary operator to the ToS.
This alone fixes 4% of the spec test issues.
2021-05-30 01:34:28 +04:30
Ali Mohammad Pur
08b567e137 LibWasm: Avoid OOB accesses caused by user input
Just trap instead of crashing.
2021-05-30 01:34:28 +04:30
Ali Mohammad Pur
8ce015742d LibWasm: Fix logic error in Limits::parse()
The check was negated, and it errored out when the read actually
succeeded.
2021-05-29 23:03:18 +04:30
Ali Mohammad Pur
578bf6c45e LibWasm: Avoid excessive pop()-then-push() on the stack
Also make the stack a lot bigger, since we now have only one of these
instead of one per function call.
2021-05-27 17:28:41 +04:30
Ali Mohammad Pur
477ab6dc4c LibWasm: Let the interpreter itself manage the call frame 2021-05-27 17:28:41 +04:30
Ali Mohammad Pur
85794f8244 LibWasm: Add a copy assignment operator to Value 2021-05-27 17:28:41 +04:30
Ali Mohammad Pur
c5df55a8a2 LibWasm: Make Interpreter a virtual interface
This allows multiply different kinds of interpreters to be used by the
runtime; currently a BytecodeInterpreter and a
DebuggerBytecodeInterpreter is provided.
2021-05-27 17:28:41 +04:30
Ali Mohammad Pur
f91fa79fc5 LibWasm: Use the current configuration to run call ops
This should make it easier to implement multiple types of interpreters
on top of a configuration, and also give a small speed boost in not
initialising as many Stack objects.
2021-05-27 17:28:41 +04:30
Ali Mohammad Pur
bc936a5fac LibWasm: Make Frame a value type as well
This means stack operations will no longer do extra allocations.
2021-05-27 17:28:41 +04:30
Ali Mohammad Pur
73eb0785e0 LibWasm: Don't put values and labels in OwnPtrs
Doing that was causing a lot of malloc/free traffic, but since there's
no need to have a stable pointer to them, we can just store them by
value.
This makes execution significantly faster :^)
2021-05-27 17:28:41 +04:30
Ali Mohammad Pur
ba5da79617 LibWasm: Add execution hooks and a debugger mode to the wasm tool
This is useful for debugging *our* implementation of wasm :P
2021-05-26 15:34:13 +04:30
Ali Mohammad Pur
800bcb9965 LibWasm: Drop previous frame when a structured end instruction is run 2021-05-26 15:34:13 +04:30
Ali Mohammad Pur
7966168fea LibWasm: Turn memory read failures into traps 2021-05-26 15:34:13 +04:30
Ali Mohammad Pur
c31a4e9013 LibWasm: Once more fix structured instruction label indices
This finally works correctly. ™️
2021-05-26 15:34:13 +04:30
Ali Mohammad Pur
d05e5dbdcf LibWasm: Drop the correct number of frames
Prior to this commit, we would be dropping an extra frame.
2021-05-26 15:34:13 +04:30
Ali Mohammad Pur
a21ebae652 LibWasm: Implement checked truncation instructions
This implements the 8 i<n>.truncate.f<n>_<s> instructions.
2021-05-26 15:34:13 +04:30
Ali Mohammad Pur
cf8b75c2e5 LibWasm+LibWeb: Partially resolve memory exports
This allows the JS side to access the wasm memory, assuming it's
exported by the module.
This can be used to draw stuff on the wasm side and display them from
the js side, for example :^)
2021-05-26 15:34:13 +04:30
Ali Mohammad Pur
4a459d2430 LibWasm: Correct memory init size when instantiating
These limits are in units of page size, not bytes.
Also fixes incorrect debug logs.
2021-05-26 15:34:13 +04:30
Ali Mohammad Pur
3926eab3b7 LibWasm+LibWeb: Implement (a very basic version of) the JS link/import
This allows Wasm code to call javascript functions.
2021-05-26 15:34:13 +04:30
Ali Mohammad Pur
8cbdcffd05 LibWasm: Print instruction arguments too 2021-05-21 00:15:23 +01:00
Ali Mohammad Pur
207379165f LibWasm: Fix nested structured instruction parsing
Previously, the ip would not be propagated correctly, and we would
produce invalid jumps when more than one level of nesting was involved.
This makes loops work :P
2021-05-21 00:15:23 +01:00
Ali Mohammad Pur
29b193d25d LibWasm: Resolve labels starting from the top of the stack
Otherwise "label index 0" would be the first ever created label, not the
last one (as the spec wants) :^(
2021-05-21 00:15:23 +01:00
Ali Mohammad Pur
35b3ae26ed LibWasm: Implement a very basic linker
This will simply "link" any given module instances and produce a list of
external values that can be used to instantiate a module.
Note that this is extremely basic and cannot resolve circular
dependencies, and depends on the instance order.
2021-05-21 00:15:23 +01:00
Ali Mohammad Pur
3283c8a495 LibWasm: Make the instantiation process produce an OwnPtr
Managing the instantiated modules becomes a pain if they're on the
stack, since an instantiated module will eventually reference itself.
To make using this simpler, just avoid copying the instance.
2021-05-21 00:15:23 +01:00
Ali Mohammad Pur
efb106069b LibWasm: Decouple ModuleInstance from the AbstractMachine
This fixes a FIXME and will allow linking only select modules together,
instead of linking every instantiated module into a big mess of exported
entities :P
2021-05-21 00:15:23 +01:00
Ali Mohammad Pur
541091500c LibWasm: Trap instead of VERIFY()'ing
...unless something really is an assertion.
2021-05-21 00:15:23 +01:00
Ali Mohammad Pur
b3c13c3e8a LibWasm+Meta: Add test-wasm and optionally test the conformance tests
This only tests "can it be parsed", but the goal of this commit is to
provide a test framework that can be built upon :)
The conformance tests are downloaded, compiled* and installed only if
the INCLUDE_WASM_SPEC_TESTS cmake option is enabled.
(*) Since we do not yet have a wast parser, the compilation is delegated
to an external tool from binaryen, `wasm-as`, which is required for the
test suite download/install to succeed.
This *does* run the tests in CI, but it currently does not include the
spec conformance tests.
2021-05-21 00:15:23 +01:00
Ali Mohammad Pur
7fec66dd1c LibWasm: Make clang happy by removing an 'extra' set of parenthesis
These aren't actually an extra set, without them the fold operation
would be syntactically invalid.
Also remove possible cast of float->double/double->float in Value::to()
2021-05-17 23:25:30 +02:00
Ali Mohammad Pur
95b9821f26 LibWasm: Implement memory.grow, memory.size and drop
These allow a very basic memory-using program to work.
2021-05-17 23:25:30 +02:00
Ali Mohammad Pur
3402381d7a LibWasm: Execute the start function, if one exists 2021-05-17 23:25:30 +02:00
Ali Mohammad Pur
84e3957dc3 LibWasm: Implement most of the remaining instructions
This commit is a bit of a mixed bag, but most of the changes are
repetitive enough to just include in a single commit.
The following instructions remain unimplemented:
- br.table
- table.init
- table.get
- table.set
- table.copy
- table.size
- table.grow
- table.fill
- ref.null
- ref.func
- ref.is_null
- drop
- i32/i64.clz
- i32/i64.ctz
- i32/i64.popcnt
- i32/i64.rotl
- i32/i64.rotr
- X.trunc.Y
- X.trunc_sat.Y
- memory.size
- memory.grow
- memory.init
- memory.copy
- memory.fill
- elem.drop
- data.drop
2021-05-17 23:25:30 +02:00