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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions lib/std/fs.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2547,6 +2547,15 @@ pub const SelfExePathError = os.ReadLinkError || os.SysCtlError || os.RealPathEr
/// `selfExePath` except allocates the result on the heap.
/// Caller owns returned memory.
pub fn selfExePathAlloc(allocator: Allocator) ![]u8 {
if (builtin.os.tag == .wasi) {
var args = try std.process.argsWithAllocator(allocator);
defer args.deinit();
// On WASI, argv[0] is always just the basename of the current executable
const exe_name = args.next() orelse return error.FileNotFound;

var buf: [MAX_PATH_BYTES]u8 = undefined;
return allocator.dupe(u8, try selfExePathWasi(&buf, exe_name));
}
// Use of MAX_PATH_BYTES here is justified as, at least on one tested Linux
// system, readlink will completely fail to return a result larger than
// PATH_MAX even if given a sufficiently large buffer. This makes it
Expand Down Expand Up @@ -2643,10 +2652,48 @@ pub fn selfExePath(out_buffer: []u8) SelfExePathError![]u8 {
const end_index = std.unicode.utf16leToUtf8(out_buffer, utf16le_slice) catch unreachable;
return out_buffer[0..end_index];
},
.wasi => @compileError("std.fs.selfExePath not supported for WASI. Use std.fs.selfExePathAlloc instead."),
else => @compileError("std.fs.selfExePath not supported for this target"),
}
}

/// WASI-specific implementation of selfExePath
///
/// On WASI argv0 is always just the executable basename, so this function relies
/// using a fixed executable directory path: "/zig"
///
/// This path can be configured in wasmtime using `--mapdir=/zig::/path/to/zig/dir/`
fn selfExePathWasi(out_buffer: []u8, argv0: []const u8) SelfExePathError![]const u8 {
topolarity marked this conversation as resolved.
Show resolved Hide resolved
var allocator = std.heap.FixedBufferAllocator.init(out_buffer);
var alloc = allocator.allocator();

// Check these paths:
// 1. "/zig/{exe_name}"
// 2. "/zig/bin/{exe_name}"
const base_paths_to_check = &[_][]const u8{ "/zig", "/zig/bin" };
const exe_names_to_check = &[_][]const u8{ path.basename(argv0), "zig.wasm" };

for (base_paths_to_check) |base_path| {
for (exe_names_to_check) |exe_name| {
const test_path = path.join(alloc, &.{ base_path, exe_name }) catch continue;

// Make sure it's a file we're pointing to
const file = os.fstatat(os.wasi.AT.FDCWD, test_path, 0) catch continue;
if (file.filetype != .REGULAR_FILE) continue;

// Path seems to be valid, let's try to turn it into an absolute path
var real_path_buf: [MAX_PATH_BYTES]u8 = undefined;
if (os.realpath(test_path, &real_path_buf)) |real_path| {
if (real_path.len > out_buffer.len)
return error.NameTooLong;
mem.copy(u8, out_buffer, real_path);
return out_buffer[0..real_path.len];
} else |_| continue;
}
}
return error.FileNotFound;
}

/// The result is UTF16LE-encoded.
pub fn selfExePathW() [:0]const u16 {
const image_path_name = &os.windows.peb().ProcessParameters.ImagePathName;
Expand Down
18 changes: 11 additions & 7 deletions src/Cache.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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
topolarity marked this conversation as resolved.
Show resolved Hide resolved
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;
}

Expand Down
13 changes: 12 additions & 1 deletion src/introspect.zig
Original file line number Diff line number Diff line change
@@ -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");

Expand Down Expand Up @@ -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);
}
}
10 changes: 10 additions & 0 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down