Commit 6f64c8b693

Andrew Kelley <andrew@ziglang.org>
2025-10-28 16:47:39
std.debug.SelfInfo.Windows: less invasive change
restores code closer to master branch in hopes of avoiding a regression that was introduced when this was based on openSelfExe rather than GetModuleFileNameExW.
1 parent 1553c8e
Changed files (2)
lib
std
debug
SelfInfo
Io
lib/std/debug/SelfInfo/Windows.zig
@@ -297,7 +297,19 @@ const Module = struct {
         // a binary is produced with -gdwarf, since the section names are longer than 8 bytes.
         const mapped_file: ?DebugInfo.MappedFile = mapped: {
             if (!coff_obj.strtabRequired()) break :mapped null;
-            const coff_file = Io.File.openSelfExe(io, .{}) catch |err| switch (err) {
+            var name_buffer: [windows.PATH_MAX_WIDE + 4:0]u16 = undefined;
+            name_buffer[0..4].* = .{ '\\', '?', '?', '\\' }; // openFileAbsoluteW requires the prefix to be present
+            const process_handle = windows.GetCurrentProcess();
+            const len = windows.kernel32.GetModuleFileNameExW(
+                process_handle,
+                module.handle,
+                name_buffer[4..],
+                windows.PATH_MAX_WIDE,
+            );
+            if (len == 0) return error.MissingDebugInfo;
+            const name_w = name_buffer[0 .. len + 4 :0];
+            var threaded: Io.Threaded = .init_single_threaded;
+            const coff_file = threaded.dirOpenFileWtf16(null, name_w, .{}) catch |err| switch (err) {
                 error.Canceled => |e| return e,
                 error.Unexpected => |e| return e,
                 error.FileNotFound => return error.MissingDebugInfo,
@@ -327,12 +339,6 @@ const Module = struct {
                 error.SystemFdQuotaExceeded,
                 error.FileLocksNotSupported,
                 error.FileBusy,
-                error.InputOutput,
-                error.NotSupported,
-                error.FileSystem,
-                error.NotLink,
-                error.UnrecognizedVolume,
-                error.UnknownName,
                 => return error.ReadFailed,
             };
             errdefer coff_file.close(io);
@@ -352,7 +358,6 @@ const Module = struct {
             errdefer windows.CloseHandle(section_handle);
             var coff_len: usize = 0;
             var section_view_ptr: ?[*]const u8 = null;
-            const process_handle = windows.GetCurrentProcess();
             const map_section_rc = windows.ntdll.NtMapViewOfSection(
                 section_handle,
                 process_handle,
lib/std/Io/Threaded.zig
@@ -2052,10 +2052,10 @@ fn dirOpenFileWindows(
     const sub_path_w_array = try windows.sliceToPrefixedFileW(dir.handle, sub_path);
     const sub_path_w = sub_path_w_array.span();
     const dir_handle = if (std.fs.path.isAbsoluteWindowsWtf16(sub_path_w)) null else dir.handle;
-    return dirOpenFileWindowsInner(t, dir_handle, sub_path_w, flags);
+    return dirOpenFileWtf16(t, dir_handle, sub_path_w, flags);
 }
 
-fn dirOpenFileWindowsInner(
+pub fn dirOpenFileWtf16(
     t: *Threaded,
     dir_handle: ?windows.HANDLE,
     sub_path_w: [:0]const u16,
@@ -2800,7 +2800,7 @@ fn openSelfExe(userdata: ?*anyopaque, flags: Io.File.OpenFlags) Io.File.OpenSelf
             const image_path_unicode_string = &windows.peb().ProcessParameters.ImagePathName;
             const image_path_name = image_path_unicode_string.Buffer.?[0 .. image_path_unicode_string.Length / 2 :0];
             const prefixed_path_w = try windows.wToPrefixedFileW(null, image_path_name);
-            return dirOpenFileWindowsInner(t, null, prefixed_path_w.span(), flags);
+            return dirOpenFileWtf16(t, null, prefixed_path_w.span(), flags);
         },
         else => @panic("TODO implement openSelfExe"),
     }