Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stage2: Add limited WASI support for selfExePath and globalCacheDir #11024

Merged
merged 4 commits into from
Apr 18, 2022

Conversation

topolarity
Copy link
Contributor

@topolarity topolarity commented Mar 1, 2022

Dependent on #11053. Resolves #10716.

This PR adds support for locating the Zig executable and the library and global cache directories, based on looking in the fixed "/zig" and "/cache" directories. It also bypasses the file-locking in "src/Cache.zig" and initializes the WASI preopens on startup.

The expected directories can be provided on the command-line using --mapdir, as follows:

wasmtime --dir=. --mapdir=/cache::"$HOME/.cache/zig" --mapdir=/zig::./stage2-wasm/ ./stage2-wasm/bin/zig.wasm

The reason for the fixed directories is that argv[0] on WASI is just the basename (any absolute/relative path information is deleted by the runtime) and there is no WASI-equivalent to procfs.

This is enough to get stage2 running without LLVM on WASI:

$ ./build/zig build -Dtarget=wasm32-wasi --zig-lib-dir lib --prefix $(pwd)/stage2-wasm -Dsingle-threaded -Drelease
$ alias zigwasm="wasmtime --dir=. --mapdir=/cache::\"$HOME/.cache/zig\" --mapdir=/zig::$(pwd)/stage2-wasm/ $(pwd)/stage2-wasm/bin/zig.wasm"
$ zigwasm build-exe ./simple_hello.zig -target x86_64-linux
$ chmod +x ./simple_hello
$ ./simple_hello
Hello, world!

This should be considered highly experimental - I haven't even checked the behavior tests or anything.

@Jarred-Sumner
Copy link
Contributor

Jarred-Sumner commented Mar 2, 2022

The other nice thing about WASI is it's not too hard to emulate a subset of it in browsers

@topolarity topolarity force-pushed the wasi-stage2 branch 2 times, most recently from fec1f3b to b60f2bb Compare March 5, 2022 20:38
@topolarity
Copy link
Contributor Author

topolarity commented Mar 6, 2022

I've hit what seems to be an instability in the CI tests a couple times now, related to the test "POSIX file locking with fcntl".

Maybe the 1ms wait in that test is too low? Is this a known problem?

Copy link
Contributor

@matu3ba matu3ba left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit to reorder sentence and question on tests with recommended preopen

src/main.zig Outdated Show resolved Hide resolved
src/main.zig Outdated Show resolved Hide resolved
@topolarity
Copy link
Contributor Author

Chose to make this dependent on #11053 - Let's try to get that one merged first

src/Cache.zig Outdated Show resolved Hide resolved
This change adds support for locating the Zig executable and the library
and global cache directories, based on looking in the fixed "/zig" and
"/cache" directories.

Since our argv[0] on WASI is just the basename (any absolute/relative
path information is deleted by the runtime), there's very limited
introspection we can do on WASI, so we rely on these fixed directories.
These can be provided on the command-line using `--mapdir`, as follows:

```
wasmtime --mapdir=/cwd::. --mapdir=/cache::"$HOME/.cache/zig" --mapdir=/zig::./zig-out/ ./zig-out/bin/zig.wasm
```
@kubkon kubkon merged commit b03345f into ziglang:master Apr 18, 2022
@Pyrolistical
Copy link
Contributor

Pyrolistical commented Sep 8, 2022

I tried making a build, but it seems to be broken now.

$ zig version
0.10.0-dev.3900+ab4b26d8a

$ cat .git/refs/heads/master 
ab4b26d8a6a36ccf75af8e25f0a1f7b88063b76f

$ zig build -Dtarget=wasm32-wasi --zig-lib-dir lib --prefix $(pwd)/stage2-wasm -Dsingle-threaded -Drelease
.../lib/std/os.zig:110:28: error: struct 'os.wasi.os.wasi' has no member named 'PATH_MAX'
pub const PATH_MAX = system.PATH_MAX;
                     ~~~~~~^~~~~~~~~
.../lib/std/os/wasi.zig:4:1: note: struct declared here
const std = @import("std");
^~~~~

@andrewrk
Copy link
Member

I tried making a build, but it seems to be broken now.

#13229

Fixed in a4eb221

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Compile Zig (compiler) to Wasm/WASI
6 participants