Skip to content

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

Merged
kubkon merged 4 commits into
ziglang:masterfrom
topolarity:wasi-stage2
Apr 18, 2022
Merged

stage2: Add limited WASI support for selfExePath and globalCacheDir#11024
kubkon merged 4 commits into
ziglang:masterfrom
topolarity:wasi-stage2

Conversation

@topolarity

@topolarity topolarity commented Mar 1, 2022

Copy link
Copy Markdown
Contributor

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

Jarred-Sumner commented Mar 2, 2022

Copy link
Copy Markdown
Contributor

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

topolarity commented Mar 6, 2022

Copy link
Copy Markdown
Contributor Author

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?

@matu3ba matu3ba left a comment

Copy link
Copy Markdown
Contributor

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

Comment thread src/main.zig Outdated
Comment thread src/main.zig Outdated
@topolarity

Copy link
Copy Markdown
Contributor Author

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

Comment thread src/Cache.zig Outdated
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
Comment thread src/Cache.zig
Comment thread lib/std/fs.zig
@Pyrolistical

Pyrolistical commented Sep 8, 2022

Copy link
Copy Markdown
Contributor

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
Copy Markdown
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