From 922d8378e724063063e709223e90e3a577e4566a Mon Sep 17 00:00:00 2001 From: Cody Tapscott Date: Tue, 1 Mar 2022 10:41:38 -0700 Subject: stage2: Add limited WASI support for selfExePath and globalCacheDir 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 ``` --- src/introspect.zig | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/introspect.zig b/src/introspect.zig index 562d6b04f4..c0de4dc7f5 100644 --- a/src/introspect.zig +++ b/src/introspect.zig @@ -1,6 +1,7 @@ const std = @import("std"); const builtin = @import("builtin"); const mem = std.mem; +const os = std.os; const fs = std.fs; const Compilation = @import("Compilation.zig"); @@ -80,5 +81,15 @@ pub fn resolveGlobalCacheDir(allocator: mem.Allocator) ![]u8 { } } - return fs.getAppDataDir(allocator, appname); + if (builtin.os.tag == .wasi) { + // On WASI, we have no way to get an App data dir, so we try to use a fixed + // Preopen path "/cache" as a last resort + const path = "/cache"; + + const file = os.fstatat(os.wasi.AT.FDCWD, path, 0) catch return error.CacheDirUnavailable; + if (file.filetype != .DIRECTORY) return error.CacheDirUnavailable; + return allocator.dupe(u8, path); + } else { + return fs.getAppDataDir(allocator, appname); + } } -- cgit v1.2.3 From 089651716c3d326f8540f4e415fc88443e42f5f2 Mon Sep 17 00:00:00 2001 From: Cody Tapscott Date: Tue, 1 Mar 2022 10:42:07 -0700 Subject: stage2: Bypass file locks in src/Cache.zig for WASI targets --- src/Cache.zig | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/Cache.zig b/src/Cache.zig index 993114905e..37cd7a7529 100644 --- a/src/Cache.zig +++ b/src/Cache.zig @@ -762,18 +762,22 @@ pub const Manifest = struct { fn downgradeToSharedLock(self: *Manifest) !void { if (!self.have_exclusive_lock) return; - const manifest_file = self.manifest_file.?; - try manifest_file.downgradeLock(); + if (std.process.can_spawn or !builtin.single_threaded) { // Some targets (WASI) do not support flock + const manifest_file = self.manifest_file.?; + try manifest_file.downgradeLock(); + } self.have_exclusive_lock = false; } fn upgradeToExclusiveLock(self: *Manifest) !void { if (self.have_exclusive_lock) return; - const manifest_file = self.manifest_file.?; - // Here we intentionally have a period where the lock is released, in case there are - // other processes holding a shared lock. - manifest_file.unlock(); - try manifest_file.lock(.Exclusive); + if (std.process.can_spawn or !builtin.single_threaded) { // Some targets (WASI) do not support flock + const manifest_file = self.manifest_file.?; + // Here we intentionally have a period where the lock is released, in case there are + // other processes holding a shared lock. + manifest_file.unlock(); + try manifest_file.lock(.Exclusive); + } self.have_exclusive_lock = true; } -- cgit v1.2.3 From 692ccd01b4ebaa78f6702b15459437228e6a790d Mon Sep 17 00:00:00 2001 From: Cody Tapscott Date: Tue, 1 Mar 2022 10:42:34 -0700 Subject: stage2: Initialize WASI preopens on startup --- src/main.zig | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'src') diff --git a/src/main.zig b/src/main.zig index a647216b05..8696661017 100644 --- a/src/main.zig +++ b/src/main.zig @@ -162,6 +162,16 @@ pub fn main() anyerror!void { return mainArgs(gpa_tracy.allocator(), arena, args); } + // WASI: `--dir` instructs the WASM runtime to "preopen" a directory, making + // it available to the us, the guest program. This is the only way for us to + // access files/dirs on the host filesystem + if (builtin.os.tag == .wasi) { + // This sets our CWD to "/preopens/cwd" + // Dot-prefixed preopens like `--dir=.` are "mounted" at "/preopens/cwd" + // Other preopens like `--dir=lib` are "mounted" at "/" + try std.os.initPreopensWasi(std.heap.page_allocator, "/preopens/cwd"); + } + return mainArgs(gpa, arena, args); } -- cgit v1.2.3