diff --git a/lib/std/Build/Watch/FsEvents.zig b/lib/std/Build/Watch/FsEvents.zig index 59238c8725f8f1de161db4f9bd6bc976ba0f972b..2a48534b3accd009b42efa9abcb64d2da725e66f 100644 --- a/lib/std/Build/Watch/FsEvents.zig +++ b/lib/std/Build/Watch/FsEvents.zig @@ -78,10 +78,10 @@ const ResolvedSymbols = struct { kCFAllocatorUseContext: *const CFAllocatorRef, }; -pub fn init(io: Io) error{ OpenFrameworkFailed, MissingCoreServicesSymbol }!FsEvents { +pub fn init() error{ OpenFrameworkFailed, MissingCoreServicesSymbol }!FsEvents { var core_services = std.DynLib.open("/System/Library/Frameworks/CoreServices.framework/CoreServices") catch return error.OpenFrameworkFailed; - errdefer core_services.close(io); + errdefer core_services.close(); var resolved_symbols: ResolvedSymbols = undefined; inline for (@typeInfo(ResolvedSymbols).@"struct".fields) |f| { diff --git a/lib/std/Io/Dir.zig b/lib/std/Io/Dir.zig index 94c159d0bfb4b0c5bf6c82283a1e4830454f3185..142ea8c2cfa97bf43ddd37698cd60a47652d5c14 100644 --- a/lib/std/Io/Dir.zig +++ b/lib/std/Io/Dir.zig @@ -377,6 +377,8 @@ pub fn walk(dir: Dir, allocator: Allocator) Allocator.Error!Walker { pub const Handle = std.posix.fd_t; pub const PathNameError = error{ + /// Returned when an insufficient buffer is provided that cannot fit the + /// path name. NameTooLong, /// File system cannot encode the requested file name bytes. /// Could be due to invalid WTF-8 on Windows, invalid UTF-8 on WASI, diff --git a/lib/std/Io/Threaded.zig b/lib/std/Io/Threaded.zig index d9ef36cfda903c45be9eda8ced0980a10e5c0e46..f291af645ebb397ac80092cc61d114a987b5384c 100644 --- a/lib/std/Io/Threaded.zig +++ b/lib/std/Io/Threaded.zig @@ -2033,7 +2033,7 @@ fn fileLength(userdata: ?*anyopaque, file: File) File.LengthError!u64 { // TODO call NtQueryInformationFile and ask for only the size instead of "all" } - const stat = try fileStat(ioBasic(t), file); + const stat = try fileStat(t, file); return stat.size; } @@ -3429,7 +3429,7 @@ fn dirReadDarwin(userdata: ?*anyopaque, dr: *Dir.Reader, buffer: []Dir.Entry) Di const dents_buffer = dr.buffer[header_end..]; try current_thread.beginSyscall(); const n: usize = while (true) { - const rc = posix.system.getdirentries(dr.dir.fd, dents_buffer.ptr, dents_buffer.len, &header.seek); + const rc = posix.system.getdirentries(dr.dir.handle, dents_buffer.ptr, dents_buffer.len, &header.seek); switch (posix.errno(rc)) { .SUCCESS => { current_thread.endSyscall(); @@ -3735,9 +3735,11 @@ fn dirRealPathPosix(userdata: ?*anyopaque, dir: Dir, sub_path: []const u8, out_b if (@hasField(posix.O, "CLOEXEC")) flags.CLOEXEC = true; if (@hasField(posix.O, "PATH")) flags.PATH = true; + const mode: posix.mode_t = 0; + try current_thread.beginSyscall(); const fd: posix.fd_t = while (true) { - const rc = openat_sym(dir.handle, sub_path_posix, flags, 0); + const rc = openat_sym(dir.handle, sub_path_posix, flags, mode); switch (posix.errno(rc)) { .SUCCESS => { current_thread.endSyscall(); @@ -3784,10 +3786,11 @@ fn dirRealPathPosix(userdata: ?*anyopaque, dir: Dir, sub_path: []const u8, out_b .driverkit, .ios, .maccatalyst, .macos, .tvos, .visionos, .watchos => { // On macOS, we can use F.GETPATH fcntl command to query the OS for // the path to the file descriptor. - @memset(out_buffer, 0); + var sufficient_buffer: [posix.PATH_MAX]u8 = undefined; + @memset(&sufficient_buffer, 0); try current_thread.beginSyscall(); while (true) { - switch (posix.errno(posix.system.fcntl(fd, posix.F.GETPATH, out_buffer))) { + switch (posix.errno(posix.system.fcntl(fd, posix.F.GETPATH, &sufficient_buffer))) { .SUCCESS => { current_thread.endSyscall(); break; @@ -3803,14 +3806,15 @@ fn dirRealPathPosix(userdata: ?*anyopaque, dir: Dir, sub_path: []const u8, out_b .BADF => return error.FileNotFound, .NOSPC => return error.NameTooLong, .NOENT => return error.FileNotFound, - // TODO man pages for fcntl on macOS don't really tell you what - // errno values to expect when command is F.GETPATH... else => |err| return posix.unexpectedErrno(err), } }, } } - return std.mem.indexOfScalar(u8, &out_buffer, 0) orelse out_buffer.len; + const n = std.mem.indexOfScalar(u8, &sufficient_buffer, 0) orelse sufficient_buffer.len; + if (n > out_buffer.len) return error.NameTooLong; + @memcpy(out_buffer[0..n], sufficient_buffer[0..n]); + return n; }, .linux, .serenity, .illumos => { var procfs_buf: ["/proc/self/path/-2147483648\x00".len]u8 = undefined; @@ -6578,7 +6582,6 @@ fn processExecutableOpen(userdata: ?*anyopaque, flags: File.OpenFlags) std.proce fn processExecutablePath(userdata: ?*anyopaque, out_buffer: []u8) std.process.ExecutablePathError!usize { const t: *Threaded = @ptrCast(@alignCast(userdata)); - const max_path_bytes = std.fs.max_path_bytes; switch (native_os) { .driverkit, @@ -6591,20 +6594,19 @@ fn processExecutablePath(userdata: ?*anyopaque, out_buffer: []u8) std.process.Ex => { // Note that _NSGetExecutablePath() will return "a path" to // the executable not a "real path" to the executable. - var symlink_path_buf: [max_path_bytes:0]u8 = undefined; - var u32_len: u32 = max_path_bytes + 1; // include the sentinel + var symlink_path_buf: [posix.PATH_MAX:0]u8 = undefined; + var u32_len: u32 = posix.PATH_MAX + 1; // include the sentinel const rc = std.c._NSGetExecutablePath(&symlink_path_buf, &u32_len); if (rc != 0) return error.NameTooLong; - var real_path_buf: [max_path_bytes]u8 = undefined; - const real_path = Io.Dir.realPathAbsolute(ioBasic(t), &symlink_path_buf, &real_path_buf) catch |err| switch (err) { + var real_path_buf: [posix.PATH_MAX]u8 = undefined; + const n = Io.Dir.realPathAbsolute(ioBasic(t), &symlink_path_buf, &real_path_buf) catch |err| switch (err) { error.NetworkNotFound => unreachable, // Windows-only else => |e| return e, }; - if (real_path.len > out_buffer.len) return error.NameTooLong; - const result = out_buffer[0..real_path.len]; - @memcpy(result, real_path); - return result.len; + if (n > out_buffer.len) return error.NameTooLong; + @memcpy(out_buffer[0..n], real_path_buf[0..n]); + return n; }, .linux, .serenity => return Io.Dir.readLinkAbsolute(ioBasic(t), "/proc/self/exe", out_buffer) catch |err| switch (err) { error.UnsupportedReparsePointType => unreachable, // Windows-only @@ -6640,7 +6642,7 @@ fn processExecutablePath(userdata: ?*anyopaque, out_buffer: []u8) std.process.Ex const argv0 = std.mem.span(std.os.argv[0]); if (std.mem.indexOf(u8, argv0, "/") != null) { // argv[0] is a path (relative or absolute): use realpath(3) directly - var real_path_buf: [max_path_bytes]u8 = undefined; + var real_path_buf: [posix.PATH_MAX]u8 = undefined; const real_path = Io.Dir.realPathAbsolute(ioBasic(t), std.os.argv[0], &real_path_buf) catch |err| switch (err) { error.NetworkNotFound => unreachable, // Windows-only else => |e| return e, @@ -6655,12 +6657,12 @@ fn processExecutablePath(userdata: ?*anyopaque, out_buffer: []u8) std.process.Ex const PATH = posix.getenvZ("PATH") orelse return error.FileNotFound; var path_it = std.mem.tokenizeScalar(u8, PATH, std.fs.path.delimiter); while (path_it.next()) |a_path| { - var resolved_path_buf: [max_path_bytes - 1:0]u8 = undefined; + var resolved_path_buf: [posix.PATH_MAX - 1:0]u8 = undefined; const resolved_path = std.fmt.bufPrintSentinel(&resolved_path_buf, "{s}/{s}", .{ a_path, std.os.argv[0], }, 0) catch continue; - var real_path_buf: [max_path_bytes]u8 = undefined; + var real_path_buf: [posix.PATH_MAX]u8 = undefined; if (Io.Dir.realPathAbsolute(ioBasic(t), resolved_path, &real_path_buf)) |real_path| { // found a file, and hope it is the right file if (real_path.len > out_buffer.len) @@ -7566,7 +7568,7 @@ fn fileWriteFilePositional( current_thread.endSyscall(); assert(error.Unexpected == switch (e) { .NOMEM => return error.SystemResources, - .INVAL => |err| posix.errnoBug(err), + .INVAL => |err| errnoBug(err), else => |err| posix.unexpectedErrno(err), }); return 0; diff --git a/lib/std/debug/SelfInfo/MachO.zig b/lib/std/debug/SelfInfo/MachO.zig index 714ff539f1c42073a7f9e5582a0d39311e1c0b49..d09493adb0feb2aaa1647b8aa9ae213da40c85a2 100644 --- a/lib/std/debug/SelfInfo/MachO.zig +++ b/lib/std/debug/SelfInfo/MachO.zig @@ -21,11 +21,10 @@ pub fn deinit(si: *SelfInfo, gpa: Allocator) void { } pub fn getSymbol(si: *SelfInfo, gpa: Allocator, io: Io, address: usize) Error!std.debug.Symbol { - _ = io; const module = try si.findModule(gpa, address); defer si.mutex.unlock(); - const file = try module.getFile(gpa); + const file = try module.getFile(gpa, io); // This is not necessarily the same as the vmaddr_slide that dyld would report. This is // because the segments in the file on disk might differ from the ones in memory. Normally @@ -39,7 +38,7 @@ pub fn getSymbol(si: *SelfInfo, gpa: Allocator, io: Io, address: usize) Error!st const vaddr = address - vaddr_offset; - const ofile_dwarf, const ofile_vaddr = file.getDwarfForAddress(gpa, vaddr) catch { + const ofile_dwarf, const ofile_vaddr = file.getDwarfForAddress(gpa, io, vaddr) catch { // Return at least the symbol name if available. return .{ .name = try file.lookupSymbolName(vaddr), @@ -107,7 +106,8 @@ pub const UnwindContext = std.debug.Dwarf.SelfUnwinder; /// Unwind a frame using MachO compact unwind info (from `__unwind_info`). /// If the compact encoding can't encode a way to unwind a frame, it will /// defer unwinding to DWARF, in which case `__eh_frame` will be used if available. -pub fn unwindFrame(si: *SelfInfo, gpa: Allocator, context: *UnwindContext) Error!usize { +pub fn unwindFrame(si: *SelfInfo, gpa: Allocator, io: Io, context: *UnwindContext) Error!usize { + _ = io; return unwindFrameInner(si, gpa, context) catch |err| switch (err) { error.InvalidDebugInfo, error.MissingDebugInfo, @@ -546,12 +546,12 @@ const Module = struct { }; } - fn getFile(module: *Module, gpa: Allocator) Error!*MachOFile { + fn getFile(module: *Module, gpa: Allocator, io: Io) Error!*MachOFile { if (module.file == null) { const path = std.mem.span( std.c.dyld_image_path_containing_address(@ptrFromInt(module.text_base)).?, ); - module.file = MachOFile.load(gpa, path, builtin.cpu.arch) catch |err| switch (err) { + module.file = MachOFile.load(gpa, io, path, builtin.cpu.arch) catch |err| switch (err) { error.InvalidMachO, error.InvalidDwarf => error.InvalidDebugInfo, error.MissingDebugInfo, error.OutOfMemory, error.UnsupportedDebugInfo, error.ReadFailed => |e| e, }; diff --git a/lib/std/dynamic_library.zig b/lib/std/dynamic_library.zig index f02caa1f5ba025372dde22682bb6eb28c2ae3879..a52d64ac3ffbabb1b7d421b2330dbe22afbc1ca0 100644 --- a/lib/std/dynamic_library.zig +++ b/lib/std/dynamic_library.zig @@ -40,8 +40,8 @@ pub const DynLib = struct { } /// Trusts the file. - pub fn close(self: *DynLib, io: Io) void { - return self.inner.close(io); + pub fn close(self: *DynLib) void { + return self.inner.close(); } pub fn lookup(self: *DynLib, comptime T: type, name: [:0]const u8) ?T { diff --git a/lib/std/zig/system.zig b/lib/std/zig/system.zig index feed21efec24601bc8577a36134f4f91329e2506..92c23a2c18b7878395853f4576bf47d24f748d38 100644 --- a/lib/std/zig/system.zig +++ b/lib/std/zig/system.zig @@ -248,7 +248,7 @@ pub fn resolveTargetQuery(io: Io, query: Target.Query) DetectError!Target { os.version_range.windows.min = detected_version; os.version_range.windows.max = detected_version; }, - .macos => try darwin.macos.detect(&os), + .macos => try darwin.macos.detect(io, &os), .freebsd, .netbsd, .dragonfly => { const key = switch (builtin.target.os.tag) { .freebsd => "kern.osreldate", diff --git a/lib/std/zig/system/darwin/macos.zig b/lib/std/zig/system/darwin/macos.zig index 4ff6846a099e752ffeef831e24a7c42bc1a44ff8..7d80c2b588e6a6587ae7437d454666a9d8d2fed4 100644 --- a/lib/std/zig/system/darwin/macos.zig +++ b/lib/std/zig/system/darwin/macos.zig @@ -9,7 +9,7 @@ const Target = std.Target; /// Detect macOS version. /// `target_os` is not modified in case of error. -pub fn detect(target_os: *Target.Os) !void { +pub fn detect(io: Io, target_os: *Target.Os) !void { // Drop use of osproductversion sysctl because: // 1. only available 10.13.4 High Sierra and later // 2. when used from a binary built against < SDK 11.0 it returns 10.16 and masks Big Sur 11.x version @@ -55,7 +55,7 @@ pub fn detect(target_os: *Target.Os) !void { // approx. 4 times historical file size var buf: [2048]u8 = undefined; - if (Io.Dir.cwd().readFile(path, &buf)) |bytes| { + if (Io.Dir.cwd().readFile(io, path, &buf)) |bytes| { if (parseSystemVersion(bytes)) |ver| { // never return non-canonical `10.(16+)` if (!(ver.major == 10 and ver.minor >= 16)) {