Commit Graph

753 Commits

Author SHA1 Message Date
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
Anuraag Agrawal
1a9b174631 Use repo-scoped docker images in workflows (#822)
Signed-off-by: Anuraag Agrawal <anuraaga@gmail.com>
2022-10-04 14:29:24 +09:00
Anuraag Agrawal
25d9901568 Use repo-scoped image for internal build image (#821)
* Use repo-scoped image for internal build image

Signed-off-by: Anuraag Agrawal <anuraaga@gmail.com>
2022-10-04 14:05:50 +09:00
Anuraag Agrawal
0b4dbfd8d6 Add Memory.WriteString (#820)
Signed-off-by: Anuraag Agrawal <anuraaga@gmail.com>
2022-10-03 10:26:36 +02:00
Takeshi Yoneda
2fc5f6d0c5 Update spectest v2 to latest (#818)
Signed-off-by: Takeshi Yoneda <takeshi@tetrate.io>
2022-10-03 14:25:56 +09:00
Anuraag Agrawal
6cf113bb3a cli: add mount option to expose paths to wasm app (#816)
Signed-off-by: Anuraag Agrawal <anuraaga@gmail.com>
v1.0.0-pre.2
2022-09-29 16:22:46 +08:00
Crypt Keeper
9a623c4f88 Adds MemoryDefinition to CompiledModule and Memory (#817)
It is more often the case that projects are enabling a freestanding
target, and that may or may not have an exporting memory depending on
how that's interpreted. This adds the ability to inspect memories
similar to how you can already inspect compiled code prior to
instantiation. For example, you can enforce an ABI constraint that
"memory" must be exported even if WASI is not in use.

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-09-29 13:37:52 +08:00
Crypt Keeper
761347db1e Replaces MemorySizer and CompileConfig with RuntimeConfig (#815)
We formerly introduced `MemorySizer` as a way to control capacity independently of size. This was the first and only feature in `CompileConfig`. While possibly used privately, `MemorySizer` has never been used in public GitHub code.

These APIs interfere with how we do caching of compiled modules. Notably, they can change the min or max defined in wasm, which invalidates some constants. This has also had a bad experience, forcing everyone to boilerplate`wazero.NewCompileConfig()` despite that API never being used in open source.

This addresses the use cases in a different way, by moving configuration to `RuntimeConfig` instead. This allows us to remove `MemorySizer` and `CompileConfig`, and the problems with them, yet still retaining functionality in case someone uses it.

* `RuntimeConfig.WithMemoryLimitPages(uint32)`: Prevents memory from growing to 4GB (spec limit) per instance.
  * This works regardless of whether the wasm encodes max or not. If there is no max, it becomes effectively this value.
* `RuntimeConfig.WithMemoryCapacityFromMax(bool)`: Prevents reallocations (when growing).
  * Wasm that never sets max will grow from min to the limit above.

Note: Those who want to change their wasm (ex insert a max where there was none), have to do that externally, ex via compiler settings or post-build transformations such as [wabin](https://github.com/tetratelabs/wabin)

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-09-29 08:03:03 +08:00
Anuraag Agrawal
84488768e4 Initial wazero CLI to run a standalone Wasm binary (#813)
* Initial wazero CLI to run a standalone Wasm binary

Signed-off-by: Anuraag Agrawal <anuraaga@gmail.com>
2022-09-28 20:59:38 +09:00
Crypt Keeper
1561c4ca7b Adds MustInstantiate to host imports (#814)
Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-09-28 16:21:30 +08:00
Crypt Keeper
429334cf98 Renames ModuleBuilder to HostModuleBuilder and drops memory and globals (#812)
We at one point considered making `ModuleBuilder` create complete
WebAssembly binaries. However, we recently spun out
[wabin](https://github.com/tetratelabs/wabin), which allows this.

Meanwhile, the features in `ModuleBuilder` were confusing and misused.
For example, the only two cases memory was exported on GitHub were done
by accident. This is because host functions act on the guest's memory,
not their own.

Hence, this removes memory and globals from host side definitions, and
renames the type to HostModuleBuilder to clarify this is not ever going
to be used to construct normal Wasm binaries.

Most importantly, this simplifies the API and reduces a lot of code. It
is important to make changes like this, particularly deleting any
experimental things that didn't end up useful.

Signed-off-by: Adrian Cole <adrian@tetrate.io>
Co-authored-by: Anuraag Agrawal <anuraaga@gmail.com>
2022-09-28 14:42:14 +08:00
Crypt Keeper
06e2cd3757 ci: use cache: true where possible (#811)
Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-09-26 13:30:51 +08:00
Adrian Cole
9454f4357d more polish
Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-09-26 11:04:16 +08:00