Commit Graph

364 Commits

Author SHA1 Message Date
Crypt Keeper
958ce19c0b Makes examples runnable and pares down list (#458)
This deduplicates examples, leaving only the most maintained or targeted
ones. Notably, this makes each runnable, avoiding test dependencies.

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-04-13 16:03:50 +08:00
Takeshi Yoneda
4cd4d8a590 Fix broken base64 bench (#456)
Signed-off-by: Takeshi Yoneda <takeshi@tetrate.io>
2022-04-13 11:02:26 +08:00
Takeshi Yoneda
8fc5d36efa jit: decouple compiled code from moduleInstance's pointer (#454)
Signed-off-by: Takeshi Yoneda <takeshi@tetrate.io>
2022-04-13 11:29:47 +09:00
Crypt Keeper
c3ff16d596 Supports functions with multiple results (multi-value) (#446)
Signed-off-by: Adrian Cole <adrian@tetrate.io>
Signed-off-by: Takeshi Yoneda <takeshi@tetrate.io>
2022-04-13 09:22:39 +08:00
Takeshi Yoneda
927914ffb0 ci: run benches under tests/ dir (#455)
Signed-off-by: Takeshi Yoneda <takeshi@tetrate.io>
2022-04-12 18:14:52 +09:00
Takeshi Yoneda
4a28a1fa1b Update jit/RATIONALE.md and README.md (#452)
Signed-off-by: Takeshi Yoneda <takeshi@tetrate.io>
Co-authored-by: Crypt Keeper <64215+codefromthecrypt@users.noreply.github.com>
2022-04-12 09:57:48 +09:00
Takeshi Yoneda
ecb35e2b94 asm: complete arm64 instruction encodings for homemade assembler (#431)
This commit implements all the arm64 instruction encodings necessary
for our JIT compiler and replaces the golang-asm assembler with our
handmade assembler on arm64 platform. Notably, this allows us to do
concurrent compilations.

This closes #233 combined with #406.

Signed-off-by: Takeshi Yoneda <takeshi@tetrate.io>
Co-authored-by: Adrian Cole <adrian@tetrate.io>
2022-04-12 09:13:27 +09:00
Taction
9040fff0e1 fix typo in host func example (#449)
Signed-off-by: zhangchao <zchao9100@gmail.com>
2022-04-09 19:47:52 +09:00
Takeshi Yoneda
2577ec9863 leb127: use fixed length slice for encoding cache (#448)
Signed-off-by: Takeshi Yoneda <takeshi@tetrate.io>
2022-04-08 20:50:41 +08:00
Takeshi Yoneda
5f5c25cbe7 Set timeout for 'make test' (#447)
Signed-off-by: Takeshi Yoneda <takeshi@tetrate.io>
2022-04-07 18:44:17 +08:00
Crypt Keeper
1527e019e9 Adds ModuleConfig.WithImport and WithImportModule (#444)
Before, complicated wasm could be hard to implement, particularly as it
might have cyclic imports. This change allows users to re-map imports to
untangle any cycles or to break up monolithic modules like "env".

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-04-06 18:28:37 +08:00
Taction
a05b291836 Close module correctly in host func example (#445)
Signed-off-by: zhangchao <zchao9100@gmail.com>
2022-04-06 19:15:26 +09:00
Crypt Keeper
fb0b311844 Consistently uses LEB128 signed encoding for global constants (#443)
Global constants can be defined in wasm or in ModuleBuilder. In either
case, they end up being decoded and interpreted during instantiation.
This chooses signed encoding to avoid surprises. A more comprehensive
explanation was added to RATIONALE.md, but the motivation was a global
100 coming out negative.

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-04-06 09:50:47 +08:00
Crypt Keeper
f5598c9a8e Fixes global numeric types to have max of signed encoding (#442)
This adjusts towards the exiting code which used int32/64 instead of
uint32/64. The reason is that the spec indicates intepretation as signed
numbers, which affects the maximum value.

See https://www.w3.org/TR/wasm-core-1/#value-types%E2%91%A2

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-04-06 06:35:31 +08:00
Crypt Keeper
b1cffcc58e Adds ModuleBuilder.ExportGlobalXXX to configure constants (#441)
See https://github.com/summerwind/the-art-of-webassembly-go/issues/1

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-04-05 15:41:19 +08:00
Crypt Keeper
9345a89bea Adds ModuleBuilder.ExportMemory and ExportMemoryWithMax (#440)
This adds functions to configure memory with ModuleBuilder. This uses
two functions, ExportMemory and ExportMemoryWithMax, as working with
uint32 pointers is awkward.

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-04-05 13:12:43 +08:00
Crypt Keeper
02d6365d5d wat: syncs spectest with latest text decoder (#439)
Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-04-05 10:04:53 +08:00
Crypt Keeper
3a6cabfb8a Extracts stack trace formatting logic and adds more context (#434)
Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-04-04 19:47:51 +08:00
Crypt Keeper
abb3559310 Moves close responsibility to the module context (#438)
This moves the responsibility to close a module from the engine to the
context. The reason for this is that the engine is what defines the
function. When a module is closed, it is often from an imported host
function. If it is on the module engine, it is easy to accidentally
close an imported module.

This refactors the WASI tests also, to ensure they aren't cheating too
much. This allows us to know for example "proc_exit" works without too
much trouble.

Signed-off-by: Adrian Cole <adrian@tetrate.io>
Signed-off-by: Takeshi Yoneda <takeshi@tetrate.io>
2022-04-04 18:41:42 +08:00
Crypt Keeper
673a657e08 Adds base call error tests (#436)
This adds tests that pass without changing deferred error handling.
There are some tests that don't pass even without deferred error
handling. I'll add those in a separate PR.

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-04-04 11:13:29 +08:00
Crypt Keeper
2664b1eb62 Simplifies API per feedback (#427)
During #425, @neilalexander gave constructive feedback that the API is
both moving fast, and not good enough yet. This attempts to reduce the
incidental complexity at the cost of a little conflation.

### odd presence of `wasm` and `wasi` packages -> `api` package

We had public API packages in wasm and wasi, which helped us avoid
leaking too many internals as public. That these had names that look
like there should be implementations in them cause unnecessary
confusion. This squashes both into one package "api" which has no
package collission with anything.

We've long struggled with the poorly specified and non-uniformly
implemented WASI specification. Trying to bring visibility to its
constraints knowing they are routinely invalid taints our API for no
good reason. This removes all `WASI` commands for a default to invoke
the function `_start` if it exists. In doing so, there's only one path
to start a module.

Moreover, this puts all wasi code in a top-level package "wasi" as it
isn't re-imported by any internal types.

### Reuse of Module for pre and post instantiation to `Binary` -> `Module`

Module is defined by WebAssembly in many phases, from decoded to
instantiated. However, using the same noun in multiple packages is very
confusing. We at one point tried a name "DecodedModule" or
"InstantiatedModule", but this is a fools errand. By deviating slightly
from the spec we can make it unambiguous what a module is.

This make a result of compilation a `Binary`, retaining `Module` for an
instantiated one. In doing so, there's no longer any name conflicts
whatsoever.

### Confusion about config -> `ModuleConfig`

Also caused by splitting wasm into wasm+wasi is configuration. This
conflates both into the same type `ModuleConfig` as it is simpler than
trying to explain a "will never be finished" api of wasi snapshot-01 in
routine use of WebAssembly. In other words, this further moves WASI out
of the foreground as it has been nothing but burden.

```diff
--- a/README.md
+++ b/README.md
@@ -49,8 +49,8 @@ For example, here's how you can allow WebAssembly modules to read
-wm, err := r.InstantiateModule(wazero.WASISnapshotPreview1())
-defer wm.Close()
+wm, err := wasi.InstantiateSnapshotPreview1(r)
+defer wm.Close()

-sysConfig := wazero.NewSysConfig().WithFS(os.DirFS("/work/home"))
-module, err := wazero.StartWASICommandWithConfig(r, compiled, sysConfig)
+config := wazero.ModuleConfig().WithFS(os.DirFS("/work/home"))
+module, err := r.InstantiateModule(binary, config)
 defer module.Close()
 ...
```
2022-04-02 06:42:36 +08:00
Crypt Keeper
3c3df6b836 Updates to latest wabt and pares back to 1.0 (#430)
Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-04-01 15:09:37 +08:00
Crypt Keeper
fbc91614c7 pin wabt to eliminate spectest fail (#429)
Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-04-01 14:30:15 +08:00
Takeshi Yoneda
a351daa858 asm(amd64): ROL and ROR on Register To Memory type. (#423)
Signed-off-by: Takeshi Yoneda <takeshi@tetrate.io>
2022-03-31 15:36:38 +09:00
Takeshi Yoneda
be3aed46be asm: complete amd64 instruction encodings (#406)
Signed-off-by: Takeshi Yoneda <takeshi@tetrate.io>
Co-authored-by: Adrian Cole <adrian@tetrate.io>
2022-03-31 13:30:38 +08:00
Crypt Keeper
8f461f6f12 Makes memory limit configurable and a compile error (#419)
This allows users to reduce the memory limit per module below 4 Gi. This
is often needed because Wasm routinely leaves off the max, which implies
spec max (4 Gi). This uses Ki Gi etc in error messages because the spec
chooses to, though we can change to make it less awkward.

This also fixes an issue where we instantiated an engine inside config.

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-03-31 08:57:28 +08:00
Crypt Keeper
81c2414fff wasi: exposes CloseWithExitCode to correct implementation of proc_exit (#410)
Exposes CloseWithExitCode to correct implementation of proc_exit

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-03-30 17:46:01 +08:00
Crypt Keeper
40c09b832f Adds Module.Name and ports all hammer tests to the same impl (#418)
This exposes `wasm.Module.Name()` so that it is more coherent to
redefine a module with the same name, or know the name to import.

This uses that to port existing tests to use the new hammer
infrastracture (without module name collision).

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-03-30 10:28:07 +08:00
Crypt Keeper
1e20fe3bfe Extracts hammer.Hammer and adds atomic release of goroutines (#416)
This extracts `hammer.Hammer` as a utility for re-use, notably adding a
feature that ensures all tests run concurrently. Before, tests start in
a loop that could be delayed due to goroutine sheduling.

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-03-30 10:18:16 +08:00
Crypt Keeper
146615d94b Refactors concurrency tests as "hammer" tests and adjusts style (#415)
This refactors tests that hammer shared state in ways that use locks or
atomics, so that they are consistent and also follow practice internally
used by Go itself. Notably, this supports the `-test.short` flag also.

Signed-off-by: Adrian Cole <adrian@tetrate.io>
Co-authored-by: Takeshi Yoneda <takeshi@tetrate.io>
2022-03-29 16:23:39 +08:00
Crypt Keeper
52fdc073dc Refactors JIT tests to not be concurrent. (#414)
This changes the way the JIT engine is tested by focusing on two areas:
* Did NewModuleEngine set a finalizer for each compiled function
  correctly?
* Did ModuleEngine.Close remove its compiled function correctly?

A later change will do parallel tests for both engines and won't have to
at the same time check how finalizers work.

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-03-28 16:18:57 +08:00
Crypt Keeper
50713cfefb Deduplicates internalwasm.Engine tests (#412)
This deduplicates Engine tests so that there's less chance of copy/paste
errors and less requirement to use ad-hoc tests which are outside the
relevant source tree.

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-03-28 08:17:02 +08:00
Crypt Keeper
cde7ce6e4a Refactors concurrent close tests (#411)
Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-03-26 15:01:47 +08:00
Crypt Keeper
a1dc1f56a0 wasi: stops enforcing _start function export (#409)
Currently, we have custom code in wapc-go because our library forces a
failure when a module that uses WASI doesn't define a "_start" function.
Using the same pragmatism that resulted in us not enforcing the WASI
table, this makes the "_start" function optional. This doesn't add a
flag as the spec is not a proper version anyway (snapshot-01), so
there's no need to further complicate configuration.

If a "_start" function exists, we enforce it is of the proper signature
and succeeds. Otherwise, we allow it to be absent.

Signed-off-by: Adrian Cole <adrian@tetrate.io>
Co-authored-by: Takeshi Yoneda <takeshi@tetrate.io>
2022-03-25 07:34:30 +08:00
Crypt Keeper
000cbdeb54 wasi: replaces existing filesystem apis with fs.FS (#394)
Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-03-24 15:35:17 +08:00
Crypt Keeper
6f3867f13e Adds tests to cover ModuleContext.Close (#407)
This backfills tests that ModuleContext.Close ends up calling
`Store.CloseModule` and `SysContext.Close`.

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-03-24 08:08:38 +08:00
Crypt Keeper
59617a24c8 wasi: renames WASIConfig to SysConfig and makes stdio defaults safer (#396)
This introduces `SysConfig` to replace `WASIConfig` and formalize documentation around system calls.

The only incompatible change planned after this is to switch from wasi.FS to fs.FS

Implementation Notes:

Defaulting to os.Stdin os.Stdout and os.Stderr doesn't make sense for
the same reasons as why we don't propagate ENV or ARGV: it violates
sand-boxing. Moreover, these are worse as they prevent concurrency and
can also lead to console overload if accidentally not overridden.

This also changes default stdin to read EOF as that is safer than reading
from os.DevNull, which can run the host out of file descriptors.

Finally, this removes "WithPreopens" for "WithFS" and "WithWorkDirFS",
to focus on the intended result. Similar Docker, if the WorkDir isn't set, it
defaults to the same as root.

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-03-23 12:58:55 +08:00
Takeshi Yoneda
2cb56beb02 asm: add skeleton amd64 assembler (#403)
This adds the skeleton implementation of amd64 assembler.
The assembler is not able to produce binary at all, and the actual
encoding implementations follow in subsequent commits.

Signed-off-by: Takeshi Yoneda <takeshi@tetrate.io>
2022-03-23 10:22:46 +09:00
Takeshi Yoneda
5eaf0860eb ci: fix test binary name duplicates (#404)
Signed-off-by: Takeshi Yoneda <takeshi@tetrate.io>
2022-03-22 18:35:03 +09:00
Crypt Keeper
9a0f7f6bc6 wasi: moves system resource management to the importing module (#401)
The prior design had a problem, where multiple imports of WASI would end
up having different file descriptors for the same file. Moreover, there
was no means to close any of these when a module importing WASI was
closed.

This moves all existing functionality to a new type SystemContext, which
is owned by the importing module, similar to how it owns its memory.
While this PR doesn't fix some problems such as unclosed files, the code
is now organized in a way it can be, and these issues will be resolved
by #394.

In order to fix scope, `WASISnapshotPreview1WithConfig` had to be
removed.

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-03-22 07:58:31 +08:00
Crypt Keeper
bd245de1b4 readme: elaborates SCRATCH and fixes broken links (#402)
Thanks @jcchavezs for noticing the bad links and also the cryptic way
operating systems were defined.

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-03-22 07:51:41 +08:00
Crypt Keeper
61b082ba97 Adds VS go-wasm3 (#400)
go-wasm3 is the first implementation to beat wazero at a benchmark. Its
initialization time is less than half our time in our bench case.

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-03-21 08:14:09 +08:00
Crypt Keeper
8d6ed5651b wasi: moves test-only overrides out of config, simplifies error tests (#399)
This moves test-only overrides out of config in preparation of
normalized configuration. This also simplifies error tests by using the
API directly for all cases except exit.

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-03-21 07:08:27 +08:00
Crypt Keeper
9fc3725bc0 typo 2022-03-20 16:35:28 +08:00
Crypt Keeper
5b35bd9b66 Makes filesystem test simpler and removes use of panic in examples (#395)
Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-03-19 20:01:55 +08:00
Takeshi Yoneda
159077a7db Hide WASI system call lists by default in README (#397)
Signed-off-by: Takeshi Yoneda <takeshi@tetrate.io>
2022-03-19 17:42:42 +09:00
Takeshi Yoneda
9746b519fc ci: supress WARNING on emulation (#398)
Signed-off-by: Takeshi Yoneda <takeshi@tetrate.io>
2022-03-19 17:42:28 +09:00
Takeshi Yoneda
b160cb65e7 Reflect scratch test change on README (#393)
Signed-off-by: Takeshi Yoneda <takeshi@tetrate.io>
2022-03-18 16:01:49 +08:00
Takeshi Yoneda
065d2077ee jit: make compiler implementations build-tag free (#391)
This commit makes compiler implementations build tag free.
Notably, now the arm64 and amd64 compilers are compilable
on any architecture. Anything arch-specific is isolated in files
named with arch_* prefixes.

This reduces the pain to make some broad change across
multiple architectures and allow developers to notice errors
on whichever architecture they are working on.

Signed-off-by: Takeshi Yoneda <takeshi@tetrate.io>
2022-03-18 13:28:10 +09:00
Takeshi Yoneda
c634838a64 CI: change non-amd64 job into scratch platform tests (#392)
Signed-off-by: Takeshi Yoneda <takeshi@tetrate.io>
2022-03-18 13:14:53 +09:00