Commit Graph

63 Commits

Author SHA1 Message Date
Edoardo Vacchi
76dce033d3 Upgrade floor Go version to 1.19 everywhere. (#1622) 2023-08-10 06:09:16 +09:00
Crypt Keeper
8d3874d3c8 fsapi: migrates PollRead to Poll with Pflag (#1599)
Signed-off-by: Adrian Cole <adrian@tetrate.io>
Signed-off-by: Edoardo Vacchi <evacchi@users.noreply.github.com>
Co-authored-by: Edoardo Vacchi <evacchi@users.noreply.github.com>
2023-07-30 10:56:17 +08:00
Edoardo Vacchi
023a38349a sysfs: implements PollRead with poll instead of select (#1596)
Signed-off-by: Edoardo Vacchi <evacchi@users.noreply.github.com>
2023-07-28 10:49:17 +02:00
Edoardo Vacchi
1e0c73d569 wasi: nonblocking I/O for sockets and pipes on Windows (#1579)
Signed-off-by: Edoardo Vacchi <evacchi@users.noreply.github.com>
2023-07-19 07:30:06 +08:00
Crypt Keeper
326c267726 Exposes Module.IsClosed to prevent calling functions when closed (#1573)
Signed-off-by: Adrian Cole <adrian@tetrate.io>
Co-authored-by: Takeshi Yoneda <takeshi@tetrate.io>
2023-07-10 15:32:51 +08:00
Crypt Keeper
6efcf25505 Exposes sys.Stat_t as a portable alternative to syscall.Stat_t (#1567)
Signed-off-by: Adrian Cole <adrian@tetrate.io>
2023-07-10 11:46:20 +08:00
Crypt Keeper
0d0db0e12f Adds more links to rationale about start functions (#1569)
Signed-off-by: Adrian Cole <adrian@tetrate.io>
2023-07-07 16:27:39 +08:00
Crypt Keeper
6a9088b46b Centralizes docs on Ino, specifically zero (#1560)
Signed-off-by: Adrian Cole <adrian@tetrate.io>
2023-07-07 08:33:50 +08:00
Crypt Keeper
7498ad335f gojs: drops HTTP support to be compatible with Go 1.21 (#1557)
Signed-off-by: Adrian Cole <adrian@tetrate.io>
2023-07-05 16:56:18 +08:00
Crypt Keeper
c2ee2242f6 fd_readdir: reverts to special cased dirent cache (#1549)
Signed-off-by: Adrian Cole <adrian@tetrate.io>
2023-07-05 08:17:07 +08:00
Crypt Keeper
221ed0373a fs: stops pre-fetching the inode of dot-dot ("..") (#1544)
Signed-off-by: Adrian Cole <adrian@tetrate.io>
2023-06-29 15:00:09 +02:00
Crypt Keeper
615732f1e6 Fixes drift in RATIONALE about readdir and dot entries (#1539)
Signed-off-by: Adrian Cole <adrian@tetrate.io>
2023-06-28 14:15:27 +08:00
Edoardo Vacchi
ea336061c2 wasi: introduce platform.Select and use it for poll_oneoff (#1346)
Some checks failed
Release CLI / Pre-release build (push) Has been cancelled
Release CLI / Pre-release test (macos-12) (push) Has been cancelled
Release CLI / Pre-release test (ubuntu-22.04) (push) Has been cancelled
Release CLI / Pre-release test (windows-2022) (push) Has been cancelled
Release CLI / Release (push) Has been cancelled
The PR introduces the `platform.Select()` API, wrapping `select(2)` on POSIX and emulated in some cases on Windows. RATIONALE.md contains a full explanation of the approach followed in `poll_oneoff` to handle Stdin and the other types of file descriptors, and the clock subscriptions.

It also introduces an abstraction (`StdioFilePoller`) to allow the simulation of different scenarios (waiting for input, input ready, timeout expired, etc.) when unit-testing interactive input.

This closes #1317.

Signed-off-by: Edoardo Vacchi <evacchi@users.noreply.github.com>
2023-04-18 16:31:34 +02:00
Crypt Keeper
89f918105a platform: adds notes about darwin and CGO (#1212)
Signed-off-by: Adrian Cole <adrian@tetrate.io>
2023-03-08 09:47:10 +08:00
Crypt Keeper
e77f24fe31 sysfs: drops os.File special casing for fs.FS to pass wasi-testsuite (#1174)
This adds a wazero adapter which passes wasi-testsuite 100pct on darwin,
linux and windows. While the main change was adding inodes to the wasi
`fd_readdir` dirents, there was a lot of incidental work needed.

Most of the work was troubleshooting in nature, around windows
specifically, but also wrapping of files. This backfills a lot of tests
and reworked how wrapping works, particularly around windows.

To accommodate this, we drop `os.File` special casing except for
`sysfs.DirFS`

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2023-03-01 13:28:57 +08:00
Crypt Keeper
3d72f2cb90 wasi: implements sched_yield with sys.Osyield (#1131)
This implements WASI `sched_yield` with `sys.Osyield` that defaults to
return immediately. This is intentionally left without a built-in
alternative as common platforms such as darwin implement
`runtime.osyield` by sleeping for a microsecond. If we implemented that,
user code would be slowed down without a clear reason why.

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2023-02-15 15:57:24 -10:00
Crypt Keeper
f18bb221c4 gojs: backfills errno tests and updates links (#1110)
Signed-off-by: Adrian Cole <adrian@tetrate.io>
2023-02-09 07:38:22 -10:00
Crypt Keeper
1421e10a43 wasi: skips fd_readdir until fixed upstream (#1098)
The current fd_readdir test is invalid as it requires dot and dot-dot
entries, which both aren't required by POSIX nor returned by go. Once
this is addressed upstream, we can remove the skip.

See https://github.com/WebAssembly/wasi-testsuite/issues/52
See #1036

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2023-02-05 10:17:40 +02:00
Crypt Keeper
8338a0cfc6 Adds rationale about WASI vs os.DirFS (#1094)
Signed-off-by: Adrian Cole <adrian@tetrate.io>
2023-02-02 16:07:35 +08:00
Crypt Keeper
574b2a70ab wasi: prepares for native support of preopens (#1067)
This makes all the code changes necessary to enable multiple pre-opens
once #1077 is fixed.

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2023-01-29 11:31:40 +02:00
Crypt Keeper
713e187796 fs: extracts syscallfs.ReaderAtOffset for WASI and gojs (#1037)
This extracts a utility `syscallfs.ReaderAtOffset()` to allow WASI and
gojs to re-use the same logic to implement `syscall.Pread`.

What's different than before is that if WASI passes multiple iovecs an
emulated `ReaderAt` will seek to the read position on each call to
`Read` vs once per loop. This was a design decision to keep the call
sites compatible between files that implement ReaderAt and those that
emulate them with Seeker (e.g. avoid the need for a read-scoped closer/
defer function). The main use case for emulation is `embed.file`, whose
seek function is cheap, so there's little performance impact to this.

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2023-01-15 17:30:45 -08:00
Achille
1db5433154 add internal/sys.FileTable (#975)
Signed-off-by: Achille Roussel <achille.roussel@gmail.com>
2022-12-31 06:34:25 +08:00
Nuno Cruces
3ab68ed8f8 Fix fdPread: shouldn't move file offset (#967)
Signed-off-by: Nuno Cruces <ncruces@users.noreply.github.com>
2022-12-29 11:56:48 +08:00
Takeshi Yoneda
6b9d119a7b doc: document why it's safe against async preemption (#957)
Signed-off-by: Takeshi Yoneda <takeshi@tetrate.io>
2022-12-23 13:38:55 +09:00
Crypt Keeper
126bd9050d Removes context parameter from instruction-scoped operations (#923)
We originally had a `context.Context` for anything that might be
traced, but it turned out to be only useful for lifecycle and host functions.

For instruction-scoped aspects like memory updates, a context parameter is too
fine-grained and also invisible in practice. For example, most users will use
the compiler engine, and its memory, global or table access will never use go's
context.

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-12-14 16:08:07 +09:00
Crypt Keeper
2e13f57f56 wasi: enforce fd_preXXX functions only work on pre-opened files (#919)
At the moment, the only pre-open support we have is the file system
itself (root a.k.a. / or file-descriptor 3). We may in the future add
the ability to pre-open sockets, but in any case, this is where we are
today.

This change hardens logic around fd_preXXX functions, ensuring they only
work on actual pre-opens. This also fixes the path returned in filestat
as we sometimes returned a full path, when typically the basename is the
only part that can be returned.

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-12-13 16:42:53 +09: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
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
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
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
b01effc8a9 Top-levels CoreFeatures and defaults to 2.0 (#800)
While compilers should be conservative when targeting WebAssembly Core
features, runtimes should be lenient as otherwise people need to
constantly turn on all features. Currently, most examples have to turn
on 2.0 features because compilers such as AssemblyScript and TinyGo use
them by default. This matches the policy with the reality, and should
make first time use easier.

This top-levels an internal type as `api.CoreFeatures` and defaults to
2.0 as opposed to 1.0, our previous default. This is less cluttered than
the excess of `WithXXX` methods we had prior to implementing all
planned WebAssembly Core Specification 1.0 features.

Finally, this backfills rationale as flat config types were a distinct
decision even if feature set selection muddied the topic.

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-09-06 15:14:36 +08:00
Nuno Cruces
ab0cb6f273 Fix fdRead: handle io.Reader corner cases (#740)
Signed-off-by: Nuno Cruces <ncruces@users.noreply.github.com>
Signed-off-by: Adrian Cole <adrian@tetrate.io>
Co-authored-by: Adrian Cole <adrian@tetrate.io>
2022-08-12 16:04:44 +08:00
Crypt Keeper
1689fc1bbf Allows wasm-defined host functions to use memory in interpreter (#713)
Before, we allowed stubbed host functions to be defined in wasm instead
of Go. This improves performance and reduces a chance of side-effects vs
Go. In fact, any pure function was supported in wasm, provided it only
called pure functions.

This changes internals so that a wasm-defined host function can use
memory. Notably, host functions use the caller's memory, so this is
simpler to initially support in the interpreter.

This is needed to simplify and reduce performance hit of GOARCH=wasm,
GOOS=js code, which perform a lot of memory reads and do not have
idiomatic signatures.

Note: wasm-defined host functions remain internal until we gain
experience, at least conclusion of the wasm_exec host module.


Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-07-25 09:12:44 +08:00
Crypt Keeper
8c2f0928bc Panics caller on exit error (#673)
This changes the AssemblyScript abort handler and WASI proc_exit
implementation to panic the caller which eventually invoked close.

This ensures no code executes afterwards, For example, LLVM inserts
unreachable instructions after calls to exit.

See https://github.com/emscripten-core/emscripten/issues/12322
See #601

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-07-06 16:30:31 +08:00
Crypt Keeper
798ff20f81 Removes WithWorkDirFS and "." resolution (#660)
This removes WithWorkDirFS and any other attempts to resolve the current directory (".") in host functions. This is a reaction to reality of compilers who track this inside wasm (not via host functions). One nice side effect is substantially simpler internal implementation of file-systems.

This also allows experimental.WithFS to block file access via passing nil.

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-06-27 13:29:35 +08:00
Crypt Keeper
30be6a8e2a wasi: Implements wasi_snapshot_preview1.poll_oneoff for relative clock events (#629)
This implements wasi_snapshot_preview1.poll_oneoff for relative clock events,
and in doing so stubs `Nanosleep` which defaults to noop, but can be configured
to `time.Sleep`.

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-06-17 16:48:35 +08:00
Crypt Keeper
d6330d9cfa Makes fake clocks increment and fixes mutability bug (#630)
This ensures fake clocks increment so that compilers that implement
sleep with them don't spin.

This also fixes a mutability bug in config where we weren't really doing
clone properly because map references are shared.

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-06-17 11:31:48 +08:00
Crypt Keeper
507ce79080 Adds sys.Walltime and sys.Nanotime for security and determinism (#616)
This adds two clock interfaces: sys.Walltime and sys.Nanotime to
allow implementations to override readings for purposes of security or
determinism.

The default values of both are a fake timestamp, to avoid the sandbox
break we formerly had by returning the real time. This is similar to how
we don't inherit OS Env values.
2022-06-04 15:14:31 +08:00
Crypt Keeper
e0e9e27326 Extracts platform-specific runtime code into its own package (#608)
This moves the platform-specific runtime code (currently only used by
the compiler) into its own package. Specifically, this moves the mmap
logic, and in doing so makes it easier to test, for example new
operating systems.

This also backfills missing RATIONALE about x/sys and hints at a future
possibility to allow a plugin. However, the next step is to get FreeBSD
working natively on the compiler without any additional dependencies.

See #607

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-06-01 09:24:02 +08:00
Anuraag Agrawal
d54a1348c4 Implements clock ID support for getting time/res in wasi (#605)
* Implements clock ID support for getting time/res in wasi

Signed-off-by: Anuraag Agrawal <anuraaga@gmail.com>
2022-05-31 12:11:52 +09:00
Crypt Keeper
c815060196 Renames JIT to Compiler and notes it is AOT (#564)
This notably changes NewRuntimeJIT to NewRuntimeCompiler as well renames
packages from jit to compiler.

This clarifies the implementation is AOT, not JIT, at least when
clarified to where it occurs (Runtime.CompileModule). In doing so, we
reduce any concern that compilation will happen during function
execution. We also free ourselves to create a JIT option without
confusion in the future via CompileConfig or otherwise.

Fixes #560

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-05-17 08:50:56 +09:00
Takeshi Yoneda
bfac34e927 Adds limit on the number of tables per module (#549)
Signed-off-by: Takeshi Yoneda <takeshi@tetrate.io>
2022-05-12 20:04:35 +09:00
Crypt Keeper
eec3cdd79e Documents why we don't use Option pattern (#521)
Closes #480

Co-authored-by: Takeshi Yoneda <takeshi@tetrate.io>
Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-05-03 08:40:29 +08: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
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
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
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
cde7ce6e4a Refactors concurrent close tests (#411)
Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-03-26 15:01:47 +08:00