Commit Graph

865 Commits

Author SHA1 Message Date
Crypt Keeper
6f30a42828 wasi: optimizes args/environ parsing (#885)
While most compilers will only read args/environ once, tools like WAGI
make heavy use of environment, possibly dozens of long variables. This
optimizes both args and environ for this reason and also to setup for
optimizing other functions.

Here are the notable changes:
* eagerly coerce to byte slices instead of strings
* re-use null terminated length for writing values
* avoid loops that call mem.WriteXXX internally

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-12-05 11:07:51 +08:00
Takeshi Yoneda
dca3bfa683 Delete obsolete integration test from wat parser (#884)
Signed-off-by: Takeshi Yoneda <takeshi@tetrate.io>
2022-12-05 12:04:20 +09:00
Takeshi Yoneda
0e9af73038 validation: not panic with redundant Else instrs (#883)
Signed-off-by: Takeshi Yoneda <takeshi@tetrate.io>
2022-12-05 09:04:42 +09:00
Crypt Keeper
e717d09bb7 Adds recently added WASI functions to the website (#880)
Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-12-02 11:08:26 +08:00
Takeshi Yoneda
e9de52c5d6 fuzz: adds validation target for maybe invalid module compilation (#879)
Signed-off-by: Takeshi Yoneda <takeshi@tetrate.io>
2022-12-02 10:56:13 +09:00
Clifton Kaznocha
dbb13117c2 Fix typos in examples (#878) 2022-12-02 08:44:32 +09:00
Takeshi Yoneda
40e698e068 binary: adds custom section decoder (#877)
Signed-off-by: Takeshi Yoneda <takeshi@tetrate.io>
2022-12-01 20:08:40 +09:00
Takeshi Yoneda
4adfa48eb0 ci: fixes cache key on Minmal Fuzzing (#876)
Signed-off-by: Takeshi Yoneda <takeshi@tetrate.io>
2022-12-01 14:51:08 +09:00
Crypt Keeper
b4f3371a16 wasi: adds fd_readdir (#865)
This adds an implementation of `fd_readdir` for WASI, which ensures a
very large directory is not kept in host memory until its directory is
closed.

Original implementation and test data are with thanks from @jerbob92.

Signed-off-by: Adrian Cole <adrian@tetrate.io>
Co-authored-by: Takeshi Yoneda <takeshi@tetrate.io>
Co-authored-by: jerbob92 <jerbob92@users.noreply.github.com>
v1.0.0-pre.4
2022-12-01 10:19:21 +08:00
Takeshi Yoneda
651f71b8ab Merge wazero-fuzz into internal/integration_test (#872)
Signed-off-by: Takeshi Yoneda <takeshi@tetrate.io>
2022-12-01 10:22:58 +09:00
Takeshi Yoneda
610af79719 Fixes applyTableInits for many extern null inits (#874)
Signed-off-by: Takeshi Yoneda <takeshi@tetrate.io>
2022-11-30 15:32:06 +09:00
Takeshi Yoneda
0a52a732f0 api: adds Encode and Decode functions for int32 and uint32 (#871)
Signed-off-by: Takeshi Yoneda <takeshi@tetrate.io>
2022-11-30 14:29:00 +09:00
Takeshi Yoneda
721327decc Consolidates table init logics (#873)
Signed-off-by: Takeshi Yoneda <takeshi@tetrate.io>
2022-11-30 14:28:37 +09:00
Takeshi Yoneda
8339045657 Ensures listeners only bound to compile time objects (#870)
Signed-off-by: Takeshi Yoneda <takeshi@tetrate.io>
2022-11-29 17:25:59 +09:00
Takeshi Yoneda
84d733eb0a compiler: adds support for FunctionListeners (#869)
Signed-off-by: Takeshi Yoneda <takeshi@tetrate.io>
2022-11-29 15:15:49 +09:00
Crypt Keeper
b49622b881 Adds logging.NewHostLoggingListenerFactory (#867)
This formalizes something I hand edit constantly, which is a listener
that only shows logging when it is a host function or called by the
host. This is important as some compilers create a large amount of
overhead functions, which can make thousands of lines around a host call
you are looking for.

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-11-29 09:42:35 +08:00
Takeshi Yoneda
4a6cbfc55b compiler: small optimization on CompileModule (#868)
Signed-off-by: Takeshi Yoneda <takeshi@tetrate.io>
2022-11-29 09:42:14 +08:00
Takeshi Yoneda
61a7001897 Update spectest v2 to latest (#863)
Signed-off-by: Takeshi Yoneda <takeshi@tetrate.io>
2022-11-28 16:12:10 +09:00
Takeshi Yoneda
1b1da58e93 ci: fix DNS issue with FreeBSD VM (#864)
Signed-off-by: Takeshi Yoneda <takeshi@tetrate.io>
2022-11-28 13:17:45 +09:00
Takeshi Yoneda
19aab2d998 Upgrade wabt to 1.0.31 (#861)
Signed-off-by: Takeshi Yoneda <takeshi@tetrate.io>
2022-11-28 11:03:29 +09:00
Crypt Keeper
0e851b71a8 Optimizes GoModuleFunction signature and ensures function result slices are unique (#860)
Signed-off-by: Adrian Cole <adrian@tetrate.io>
Co-authored-by: Takeshi Yoneda <takeshi@tetrate.io>
2022-11-28 10:00:07 +08:00
Crypt Keeper
8990932a12 wasi: adds path_filestat_get (#858)
This adds path_filestat_get which is needed for PDFium and zig's
fstatatWasi.

aea617c60b/lib/std/os.zig (L4171)


Signed-off-by: Adrian Cole <adrian@tetrate.io>
Co-authored-by: jerbob92 <jerbob92@users.noreply.github.com>
2022-11-25 14:49:14 +07:00
Crypt Keeper
5c02958a52 implements fd_pread in wasi and adds zig wasi example (#857)
This implements fd_pread which is required when you don't buffer reads
in zig (particularly when your buffer isn't >= file length).

Advice on zig code welcome as it is a new language to me.

Note: The approach here is same as we use internall for GOOS=js as that
has an fdPread syscall. Just, we never implemented it for wasi, yet.

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-11-24 20:09:42 +07:00
Adrian Cole
eae86afc8c Revert "implements fd_pread in wasi and adds zig wasi example"
This reverts commit ce5927fd02.
2022-11-24 12:55:58 +07:00
Adrian Cole
ce5927fd02 implements fd_pread in wasi and adds zig wasi example
This implements fd_pread which is required when you don't buffer reads
in zig (particularly when your buffer isn't >= file length).

Advice on zig code welcome as it is a new language to me.

Note: The approach here is same as we use internall for GOOS=js as that
has an fdPread syscall. Just, we never implemented it for wasi, yet.

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-11-24 12:53:29 +07:00
jerbob92
b8d985462e Implement WASI FS fd_filestat_get (#838)
Signed-off-by: Adrian Cole <adrian@tetrate.io>
Co-authored-by: Adrian Cole <adrian@tetrate.io>
2022-11-22 18:45:15 +07:00
Crypt Keeper
3cd9cbcfb5 Adds emscripten invoke_xxx functions needed for PDFium (#856)
This adds host functions that work on dynamic function tables. These are
only used by emscripten, but require some infrastructure to support it.
I added the least possible to due the task. This also only handles i32
and void returns with up to four parameters as that covers the needs of
PDFium. Future integrations may need more parameters or a mix of floats.
Such use cases should be addressed as they come as otherwise it is a lot
of work for the cartesian product of all combinations.

See 1b0d724fd5/test/passes/post-emscripten.wast
See https://github.com/jerbob92/go-pdfium-wasm

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-11-22 16:23:59 +07:00
Crypt Keeper
b41421e606 Adds rationale for api.ValueType encoding (#855)
We made some decisions around encoding last year, but we didn't detail
why.

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-11-21 09:49:10 +07:00
Takeshi Yoneda
704f6c9546 example: use the stable Zig 0.10.0 (#854)
Signed-off-by: Takeshi Yoneda <takeshi@tetrate.io>
2022-11-16 10:21:20 -08:00
Takeshi Yoneda
60312083d5 Fixes builds on compiler-unsupported platoforms (#853)
Signed-off-by: Takeshi Yoneda <takeshi@tetrate.io>
2022-11-16 09:54:51 -08:00
Clifton Kaznocha
d507d8666f Optimize setting the ModuleInstance.DataInstances (#852)
Signed-off-by: Clifton Kaznocha <ckaznocha@users.noreply.github.com>
2022-11-12 14:52:11 -08:00
Takeshi Yoneda
76ed3047cf arm64: adds support for large in-block jumps with ADR (#851)
Signed-off-by: Takeshi Yoneda <takeshi@tetrate.io>
2022-11-10 05:46:46 +01:00
Crypt Keeper
7e3cda8680 Adds more context for ADR offset overrun (#850)
See #847

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-11-09 08:27:10 +01:00
Crypt Keeper
af3281c39c switches to gosimports for grouping (#849)
Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-11-09 07:04:24 +01:00
Crypt Keeper
329ccca6b1 Switches from gofmt to gofumpt (#848)
This switches to gofumpt and applies changes, as I've noticed working
in dapr (who uses this) that it finds some things that are annoying,
such as inconsistent block formatting in test tables.

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-11-09 05:48:24 +01:00
Crypt Keeper
b8dce95377 Adds wasi_snapshot_preview1.NewFunctionExporter (#846)
This adds `wasi_snapshot_preview1.NewFunctionExporter` for the following
two use cases:

* Overriding a builtin function with an alternate implementation.
  * Allows developers like @jerbob92 to test new WASI functions without
    forking wazero.
* Exporting functions to the module "wasi_unstable" for legacy code.
  * Needed by mosn to support existing wasm which may never go away.

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-11-09 05:21:30 +01:00
Robbert van Ginkel
ac5ec317a7 gojs: gc fix refcount issue (#845)
Signed-off-by: Robbert van Ginkel <570934+robbertvanginkel@users.noreply.github.com>
2022-11-08 16:22:29 +09:00
Clifton Kaznocha
483dfe17c3 Replace module name check linear scan with map lookup (#844)
Signed-off-by: Clifton Kaznocha <ckaznocha@users.noreply.github.com>
2022-11-07 22:38:29 +01:00
Clifton Kaznocha
0f19bb21ff Optimizes slice initializations (#842)
Signed-off-by: Clifton Kaznocha <ckaznocha@users.noreply.github.com>
Co-authored-by: Crypt Keeper <64215+codefromthecrypt@users.noreply.github.com>
2022-11-07 08:25:37 +09:00
Clifton Kaznocha
45fc45c499 Reduces lock contention in store on type IDs (#843)
Signed-off-by: Clifton Kaznocha <ckaznocha@users.noreply.github.com>
2022-11-06 10:26:49 +09:00
Clifton Kaznocha
f391a1d312 add ieee754 and leb128 byte slice funcs (#837)
Signed-off-by: Clifton Kaznocha <ckaznocha@users.noreply.github.com>
2022-11-03 10:23:35 +08:00
Crypt Keeper
62f1a32aac Updates test dependencies (#834)
Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-11-01 15:02:30 +08:00
Crypt Keeper
958b25e3a6 Extends build compatability to one version behind Go's support policy (#833)
This enforces that wazero will build and operate one version behind Go's
support policy, making wazero's Go policy effectively three versions.

This is to allow libraries with more conservative Go policies to be able
to use wazero, specifically mosn is the first to need this.

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-11-01 13:19:57 +08:00
Crypt Keeper
d108ce4c43 Restores ability to define host functions w/o context via reflection (#832)
This restores the ability to leave out the initial context parameter
when defining functions with reflection. This is important because some
projects are porting from a different library to wazero, and all the
alternatives are not contextualized.

For example, this project is porting envoy host functions, and the
original definitions (in mosn) don't have a context parameter. By being
lenient, they can migrate easier.

See 6b813482b6/pkg/proxywasm/wazero/imports_v1.go

Signed-off-by: Adrian Cole <adrian@tetrate.io>
v1.0.0-pre.3
2022-10-28 12:44:12 -07:00
Adrian Cole
1ac6c06acb Removes spurious any
Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-10-28 08:30:43 -07:00
Crypt Keeper
be33572289 Adds HostFunctionBuilder to enable high performance host functions (#828)
This PR follows @hafeidejiangyou advice to not only enable end users to
avoid reflection when calling host functions, but also use that approach
ourselves internally. The performance results are staggering and will be
noticable in high performance applications.

Before
```
BenchmarkHostCall/Call
BenchmarkHostCall/Call-16            	 1000000	      1050 ns/op
Benchmark_EnvironGet/environGet
Benchmark_EnvironGet/environGet-16         	  525492	      2224 ns/op
```

Now
```
BenchmarkHostCall/Call
BenchmarkHostCall/Call-16            	14807203	        83.22 ns/op
Benchmark_EnvironGet/environGet
Benchmark_EnvironGet/environGet-16         	  951690	      1054 ns/op
```

To accomplish this, this PR consolidates code around host function
definition and enables a fast path for functions where the user takes
responsibility for defining its WebAssembly mappings. Existing users
will need to change their code a bit, as signatures have changed.

For example, we are now more strict that all host functions require a
context parameter zero. Also, we've replaced
`HostModuleBuilder.ExportFunction` and `ExportFunctions` with a new type
`HostFunctionBuilder` that consolidates the responsibility and the
documentation.

```diff
 ctx := context.Background()
-hello := func() {
+hello := func(context.Context) {
         fmt.Fprintln(stdout, "hello!")
 }
-_, err := r.NewHostModuleBuilder("env").ExportFunction("hello", hello).Instantiate(ctx, r)
+_, err := r.NewHostModuleBuilder("env").
+        NewFunctionBuilder().WithFunc(hello).Export("hello").
+        Instantiate(ctx, r)
```

Power users can now use `HostFunctionBuilder` to define functions that
won't use reflection. There are two choices of interfaces to use
depending on if that function needs access to the calling module or not:
`api.GoFunction` and `api.GoModuleFunction`. Here's an example defining
one.

```go
builder.WithGoFunction(api.GoFunc(func(ctx context.Context, params []uint64) []uint64 {
	x, y := uint32(params[0]), uint32(params[1])
	sum := x + y
	return []uint64{sum}
}, []api.ValueType{api.ValueTypeI32, api.ValueTypeI32}, []api.ValueType{api.ValueTypeI32})
```
As you'll notice and as documented, this approach is more verbose and
not for everyone. If you aren't making a low-level library, you are
likely able to afford the 1us penalty for the convenience of reflection.
However, we are happy to enable this option for foundational libraries
and those with high performance requirements (like ourselves)!

Fixes #825

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-10-28 07:51:08 -07:00
Crypt Keeper
2173f30758 Fixes wasi benchmark (#830)
This benchmark hadn't been run recently and it broke. This sorts it out.

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-10-27 13:51:36 -07:00
Crypt Keeper
90095f2ddd Special cases io.Discard on fd_write (#829)
This avoids memory copying in default configuration where stdout/stderr
are discarded. This helps avoid performance impact when console logging
happens, but intentionally discarded.

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-10-27 12:43:10 -07:00
June
3fc5efaf11 Adds test to ensure custom contexts are usable (#826)
Signed-off-by: Qi He <heqi@hyperchain.cn>
2022-10-26 07:02:20 +08:00
Crypt Keeper
1cbb496c26 Stops using "ex." to abbreviate "for example" (#827)
Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-10-24 11:51:48 +09:00