Commit Graph

34 Commits

Author SHA1 Message Date
Takeshi Yoneda
2fc1fa9d79 Stop using pointer of function pointers in sys.Context (#1301)
Signed-off-by: Takeshi Yoneda <takeshi@tetrate.io>
2023-03-29 11:23:38 +09:00
Edoardo Vacchi
d63813a830 fs: stat and cache mode for stdio devices (#1295)
Ensure that stdio device modes are consistent with the given
file descriptors by stat'ing, instead of returning mocks.

* Use `Stat()` on `poll_oneoff()` too, instead of `IsTerminal()`,
thus avoiding a useless syscall.

* Delete leftover type decl `fileModeStat`.

* Remove IsPlatform()

* Propagate error when Stat() fails

Signed-off-by: Edoardo Vacchi <evacchi@users.noreply.github.com>
2023-03-28 12:48:24 +02:00
Crypt Keeper
36bf277534 sysfs: requires all methods to return syscall.Errno (#1264)
This forces all syscall functions, notably filesystem, to return numeric
codes as opposed to mapping in two different areas. The result of this
change is better consolidation in call sites of `sysfs.FS`, while
further refactoring is needed to address consolidation of file errors.

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2023-03-22 07:47:57 +01:00
Crypt Keeper
046af4356e wasi: removes constraint around closing a pre-open, and temporarily skips interesting_paths (#1265)
wasi-testsuite changed its mind on pre-open WebAssembly/wasi-testsuite#66

they also now explicitly forbid paths being passed in with a leading slash. Even when the config bug on WebAssembly/wasi-testsuite#67 is finished, this requires discussion if we want to EPERM on that.

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2023-03-21 23:43:59 +08:00
Adrian Cole
a43a0f11e4 Revert "sysfs: requires all methods to return syscall.Errno"
This reverts commit 4ea9d1a7b5.
2023-03-21 22:22:36 +08:00
Adrian Cole
4ea9d1a7b5 sysfs: requires all methods to return syscall.Errno
This forces all syscall functions, notably filesystem, to return numeric
codes as opposed to mapping in two different areas. The result of this
change is better consolidation in call sites of `sysfs.FS`, while
further refactoring is needed to address consolidation of file errors.

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2023-03-21 22:20:28 +08:00
Crypt Keeper
8a92cd3841 returns EBADF when attempting to write to STDIN (#1237)
Found this problem while troubleshooting #1232

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2023-03-14 12:30:09 +08:00
Achille
9ebbb41adb sys.FileTable => descriptor.Table (#1155)
Signed-off-by: Achille Roussel <achille.roussel@gmail.com>
2023-02-24 08:37:24 +08:00
Crypt Keeper
bb002c862f wasi: adds platform.Dirent in preparation of inode fetching (#1154)
wasi_snapshot_preview1 recently requires fd_readdir to return actual
inode values. On zero, wasi-libc will call fdstat to retrieve them.

This introduces our own `platform.Dirent` type and `Readdir` function
which a later change will allow fetching of inodes.

See https://github.com/WebAssembly/wasi-libc/pull/345

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2023-02-23 18:56:27 +08:00
Takeshi Yoneda
4464d0f4e3 wasi: implements filestatset (#1100)
Signed-off-by: Takeshi Yoneda <takeshi@tetrate.io>
2023-02-06 14:22:22 +09:00
Takeshi Yoneda
f719b49d1e wasi: implements fd_renumber (#1095)
Signed-off-by: Takeshi Yoneda <takeshi@tetrate.io>
2023-02-03 13:57:52 +09:00
Crypt Keeper
043da0d8b0 wasi: prohibits closing pre-opens (#1085)
See https://github.com/WebAssembly/wasi-testsuite/issues/50

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2023-01-31 11:55:12 +02:00
Crypt Keeper
67125fcaa4 wasi: supports rewinding a directory and fixes seek error (#1084)
This allows rewinding a directory by specifying the cookie 0, after
already scrolling a directory. This allows zig and clang functionality
to work which expect this.

This also fixes the case where a directory was allowed to be seeked.
This is prohibited by the current version of wasi-testsuite.

Signed-off-by: Adrian Cole <adrian@tetrate.io>
Co-authored-by: Takeshi Yoneda <takeshi@tetrate.io>
2023-01-31 10:22:48 +02:00
Crypt Keeper
d04199d1c3 cli,wasi: passes mounts directly as preopens to run rust wasi-testsuite (#1078)
This allows wasi to see mounts as individual preopens instead of
virtualized under a root. (GOOS=js doesn't use pre-opens so it still
sees a virtual root). This allows us to progress wasi-testsuite rust
tests, but acknowledges a current glitch in zig support, which is
tracked separately as #1077

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2023-01-30 08:11:16 +02: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
cb97d7a488 fs: decouples sysfs from fs.FS and consolidates guest path logic (#1058)
This decouples sysfs.FS from fs.FS by introducing a temporary type
FSHolder, which will be removed when we top-level FSConfig (shortly).

This further reduces complexity by consolidating guest path
configuration into the only type that uses it: CompositeFS.

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2023-01-23 17:49:51 -10:00
Crypt Keeper
2a584a8937 fs: renames internal syscallfs package to sysfs and notes RATIONALE (#1056)
It will help for us to rename earlier vs later, and syscallfs will be
laborious, especially after we introduce an FSConfig type and need to
declare a method name that differentiates from normal fs.FS. e.g. WithFS
vs WithSysFS reads nicer than WithSyscallFS, and meanwhile sys is
already a public package.

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2023-01-23 11:11:35 +08:00
Crypt Keeper
319d6cca62 fs: adds base UnimplementedFS type and unwraps PathError (#1046)
This reduces some boilerplate by extracting UnimplementedFS from the
existing FS implementations, such that it returns ENOSYS. This also
removes inconsistency where some methods on FS returned syscall.Errno
and others PathError.

Note: this doesn't get rid of all PathError, yet. We still need to
create a syscallfs.File type which would be able to do that. This is
just one preliminary cleanup before refactoring out the `fs.FS`
embedding from `syscallfs.DS`.

P.S. naming convention is arbitrary, so I took UnimplementedXXX from
grpc. This pattern is used a lot of places, also proxy-wasm-go-sdk, e.g.
`DefaultVMContext`.

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2023-01-18 09:37:12 -06:00
Crypt Keeper
3cf29f9f76 fs: adds string for better error experience (#1042)
This prepares for pseudo-root when the CLI doesn't provide one by
improving the error messages in general, as well being consistent about
parameter order.

Signed-off-by: Adrian Cole <adrian@tetrate.io>

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2023-01-17 10:01:51 -06:00
Crypt Keeper
105cdcdef7 cli: rewrites compositeFS to syscallfs and adds read-only (:ro) mounts (#1030)
This rewrites compositeFS to syscallfs.FS following wasi-sdk preopen
rules. Notably, this allows use of read-only mounts now.

For example,
```bash
$ GOOS=js GOARCH=wasm bin/go test -c -o template.wasm text/template
$ wazero run -mount=src/text/template:/ -mount=/tmp:/tmp template.wasm -test.v
=== RUN   TestExecute
--- PASS: TestExecute (0.07s)
--snip--
```

This is the first step to native WASI handling of multiple pre-opens.
After this change, it is still the case that there's only one pre-open
FD visible to wasm. A later change will make it possible for WASI to see
multiple pre-opens while `GOOS=js` which doesn't use preopens, remains
on a rootFS.

A future PR may need to add a CLI flag to disable escaping directories,
(e.g. make ../.. EINVAL), similar to `fs.FS` in Go. The simplest way to
allow this is to use a host-side RootFS even in WASI, and wrap that with
a `syscallfs` filename filter.

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2023-01-13 15:50:11 +08:00
Crypt Keeper
bedde6dc7a Clarifies at semantics and preopen semantics in WASI (#1009)
This adds FS.Path which holds the pre-open path currently only used in
WASI. It also fixes a TODO where we didn't know for sure if the FD
parameter for `path_` functions must always be a pre-open. The TL;DR; is
that usually it is, but it may not be (e.g. in our zig-cc example we can
see any directory FD, not just pre-opens).

Finally, this fixes a bug in our path resolution where we mistook paths
like "foo/foo" for "foo" because we only considered basenames instead of
the full path from the pre-open root.

This also makes pre-open directory lookup lazy because I noticed in
Trivy specifically, this is unnecessary for us to do eagerly, as they
change the FS at runtime per-call. In other words, any value from init
time is invalid later.

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2023-01-05 18:59:55 +08:00
Crypt Keeper
83e4b66659 Consolidates internal code to syscallfs (#1003)
This consolidates internal code to syscallfs, which removes the fs.FS
specific path rules, except when adapting one to syscallfs. For example,
this allows the underlying filesystem to decide if relative paths are
supported or not, as well any EINVAL related concerns.

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2023-01-04 13:53:53 +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
Crypt Keeper
15dc3d7d37 Adds experimental write support and implements on gojs (#970)
This adds writefs.FS, allowing functions to create and delete files.
This begins by implementing them on `GOARCH=js GOOS=wasm`. The current
status is a lot farther than before, even if completing write on WASI is
left for a later PR (possibly by another volunteer).

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-12-28 19:49:46 +08:00
Crypt Keeper
b02b516807 wasi: allows closing stdio file descriptors (#954)
Allows wasm to close stdio file descriptors

This allows wasm to close stdio file descriptors such as STDOUT(1).
This will not close the underlying host resource as that would break a
lot of folks doing logging to the console.

Fixes #953

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-12-22 08:01:13 +08:00
Crypt Keeper
407f3ea3c0 wasi: detect if stdio are char devices instead of assuming (#935)
This uses ioctl syscalls or appropriate alternative, to detect if
stdin/out/err are character devices or not. This caches the result, to
ensure performance is ok at runtime as executing stat can approach
microsecond overhead.

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-12-16 16:06:54 +09:00
Crypt Keeper
0ed4002549 Removes experimental.WithFS (#922)
This removes the ability to override the current file system with Go
context, allowing us to simplify common paths and improve performance.

The context override was only used once in GitHub, in Trivy, and we
found another way to do that without it.

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-12-14 15:00:17 +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
3b70504f3f wasi: ensure someone can tell if root is a real file (#918)
`os.DirFS` ironically doesn't implement `fs.ReadDirFS`, so we cannot
open a directory this way. This blindly attempts to open "." regardless
of if the `fs.FS` implementation is a `fs.ReadDirFS` or not, and if
successful, enforces that the file returned is a directory. If not, a
fake directory is returned.

Doing so allows real stat to be returned for root, and also a chance to
know if a filesystem configured is real or not. Later, we'll need this
to implement open flags.

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-12-13 13:50:00 +09:00
Crypt Keeper
ce62d77ef0 wasi: allows stat on root file descriptor (3) (#917)
Before, we didn't allow a real stat on the root file descriptor. Now,
those that pass fs.ReadDirFS will return the stat of the root file
(which is implemented by a open against ".").

This also simplifies logic as we always have a file representing root,
even if faked.

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-12-13 11:00:05 +09: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
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
adc7e5b170 Adds Runtime.NewNamespace to allow intentional name collisions (#604)
Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-06-01 10:03:19 +08:00
Crypt Keeper
7fec94b2e1 experimental: consolidates context patterns (#579)
This consolidates the pattern used for context overrides, notably
replacing clock overrides via experimental.WithTimeNowUnixNano
and making all context keys internal.

This also makes sure experimental example tests are handled the same
way, notably backfilling one for WithFS
2022-05-26 18:22:29 -07:00