Skip to content

fix backtraces produced by standard library under Wine #22484

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

Closed
wants to merge 2 commits into from

Conversation

leroycep
Copy link
Contributor

Quick Zig program to test changes (at ~/code/zig/build/example-dump-stacktrace/main.zig):

pub const std = @import("std");

pub fn main() !void {
    var arena_allocator = std.heap.ArenaAllocator.init(std.heap.page_allocator);
    defer arena_allocator.deinit();
    const allocator = arena_allocator.allocator();

    // Raw value from wine indicating location of the executable
    const image_path_name_unicode_string = std.os.windows.peb().ProcessParameters.ImagePathName;
    const image_path_name = image_path_name_unicode_string.Buffer.?[0 .. image_path_name_unicode_string.Length / @sizeOf(u16) :0];
    std.log.debug("{s}:{} PEB.ProcessParameters.ImagePathName = {}", .{ @src().file, @src().line, std.unicode.fmtUtf16Le(image_path_name) });

    const pathname_w = try std.os.windows.wToPrefixedFileW(null, image_path_name);
    std.log.info("{s}:{} wToPrefixedFileW() = {s}", .{ @src().file, @src().line, std.unicode.fmtUtf16Le(pathname_w.span()) });

    std.debug.dumpCurrentStackTrace(null);

    const self_path = try std.fs.selfExePathAlloc(allocator);
    std.log.info("{s}:{} self path = {s}", .{ @src().file, @src().line, self_path });
}

Before these changes:

~/code/zig/build/example-dump-stacktrace> ../stage3/bin/zig build-exe -target x86_64-windows-msvc ./main.zig; wine ./main.exe
00c8:err:ntoskrnl:ZwLoadDriver failed to create driver L"\\Registry\\Machine\\System\\CurrentControlSet\\Services\\winebth": c0000135
003c:fixme:service:scmdatabase_autostart_services Auto-start service L"winebth" failed to start: 126
debug: main.zig:11 PEB.ProcessParameters.ImagePathName = Z:\home\geemili\code\zig\build\example-dump-stacktrace\main.exe
info: main.zig:14 wToPrefixedFileW() = \??\Z:\home\geemili\code\zig\build\example-dump-stacktrace\main.exe
Unable to dump stack trace: Unexpected
error: Unexpected
Unable to dump stack trace: Unexpected

After commit 64ba10d:

~/code/zig/build/example-dump-stacktrace> ../stage3/bin/zig build-exe -target x86_64-windows-msvc ./main.zig; wine ./main.exe
00c8:err:ntoskrnl:ZwLoadDriver failed to create driver L"\\Registry\\Machine\\System\\CurrentControlSet\\Services\\winebth": c0000135
003c:fixme:service:scmdatabase_autostart_services Auto-start service L"winebth" failed to start: 126
debug: main.zig:11 PEB.ProcessParameters.ImagePathName = Z:\home\geemili\code\zig\build\example-dump-stacktrace\main.exe
info: main.zig:14 wToPrefixedFileW() = \??\Z:\home\geemili\code\zig\build\example-dump-stacktrace\main.exe
/home/geemili/code/zig/lib/std/debug.zig:198:31: 0x140003b07 in dumpCurrentStackTrace (main.exe.obj)
        writeCurrentStackTrace(stderr, debug_info, io.tty.detectConfig(io.getStdErr()), start_addr) catch |err| {
                              ^
/main.zig:16:36: 0x1400019f5 in main (main.exe.obj)
/home/geemili/code/zig/lib/std/start.zig:475:53: 0x140003f47 in WinStartup (main.exe.obj)
    std.os.windows.ntdll.RtlExitUserProcess(callMain());
                                                    ^
Unable to dump stack trace: InvalidDebugDirectory
info: main.zig:19 self path = \??\Z:\home\geemili\code\zig\build\example-dump-stacktrace\main.exe

After commit 21a3b43:

~/code/zig/build/example-dump-stacktrace> ../stage3/bin/zig build-exe -target x86_64-windows-msvc ./main.zig; wine ./main.exe
00c8:err:ntoskrnl:ZwLoadDriver failed to create driver L"\\Registry\\Machine\\System\\CurrentControlSet\\Services\\winebth": c0000135
003c:fixme:service:scmdatabase_autostart_services Auto-start service L"winebth" failed to start: 126
debug: main.zig:11 PEB.ProcessParameters.ImagePathName = Z:\home\geemili\code\zig\build\example-dump-stacktrace\main.exe
info: main.zig:14 wToPrefixedFileW() = \??\Z:\home\geemili\code\zig\build\example-dump-stacktrace\main.exe
/home/geemili/code/zig/lib/std/debug.zig:198:31: 0x140003b07 in dumpCurrentStackTrace (main.exe.obj)
        writeCurrentStackTrace(stderr, debug_info, io.tty.detectConfig(io.getStdErr()), start_addr) catch |err| {
                              ^
/main.zig:16:36: 0x1400019f5 in main (main.exe.obj)
/home/geemili/code/zig/lib/std/start.zig:475:53: 0x140003f47 in WinStartup (main.exe.obj)
    std.os.windows.ntdll.RtlExitUserProcess(callMain());
                                                    ^
???:?:?: 0x6fffffa547f8 in ??? (kernel32.dll)
???:?:?: 0x6fffffc2f93a in ??? (ntdll.dll)
info: main.zig:19 self path = \??\Z:\home\geemili\code\zig\build\example-dump-stacktrace\main.exe

These changes have not been tested on an actual windows machine yet, only under wine.

@leroycep
Copy link
Contributor Author

Well, that didn't work at all. I may come back to this once I have a local install of windows to try changes on.

@leroycep leroycep closed this Jan 13, 2025
@leroycep
Copy link
Contributor Author

leroycep commented Jan 14, 2025

EDIT: Reopening the pull request failed, going to make a new one.


Alright, I've decided to give another go. I think I understood the problem better this time, otherwise the CI will inform me of my hubris.


main.zig:

pub const std = @import("std");

pub fn main() !void {
    std.debug.dumpCurrentStackTrace(null);
}

Compile using zig build-exe -target x86_64-windows-msvc ./main.zig and run using wine ./main.exe

Output before these changes:

Unable to dump stack trace: Unexpected

Output after these changes:

/home/geemili/code/zig/lib/std/debug.zig:198:31: 0x140001117 in dumpCurrentStackTrace (main.exe.obj)
        writeCurrentStackTrace(stderr, debug_info, io.tty.detectConfig(io.getStdErr()), start_addr) catch |err| {
                              ^
/main.zig:4:36: 0x140001015 in main (main.exe.obj)
/home/geemili/code/zig/lib/std/start.zig:475:53: 0x1400014aa in WinStartup (main.exe.obj)
    std.os.windows.ntdll.RtlExitUserProcess(callMain());
                                                    ^
???:?:?: 0x6fffffa547f8 in ??? (kernel32.dll)
???:?:?: 0x6fffffc2f93a in ??? (ntdll.dll)

It seems like it's failing to find the location of main.zig, but that's a mystery for another time.

  • Wine version: wine-9.22

#17535 is a related issue. Compiling Zig with these changes to the standard library fix the original error message, but replace it with another error that comes from an unimplemented feature in wine (as well as a now-working backtrace 😁):

~/Downloads/zig-wine-backtraces/invoking-zig-cc-17535> wine ../zig-windows-x86_64-0.14.0-dev.2643+fb43e91b2/zig.exe build-exe main.zig
error: unable to find zig self exe path: Unexpected
~/Downloads/zig-wine-backtraces/invoking-zig-cc-17535> wine ~/code/zig/build/stage4-windows/bin/zig.exe build-exe main.zig
0184:fixme:file:NtLockFile Unimplemented yet parameter
error.Unexpected NTSTATUS=0xc0000002
/home/geemili/code/zig/lib/std/os/windows.zig:2093:40: 0x14023a46f in LockFile (zig.exe.obj)
        else => return unexpectedStatus(rc),
                                       ^
/home/geemili/code/zig/lib/std/fs/Dir.zig:1099:19: 0x1401ad675 in createFileW (zig.exe.obj)
    try w.LockFile(
                  ^
/home/geemili/code/zig/lib/std/fs/Dir.zig:968:32: 0x140055eac in createFile (zig.exe.obj)
        return self.createFileW(path_w.span(), flags);
                               ^
/home/geemili/code/zig/src/Zcu/PerThread.zig:134:33: 0x140835003 in astGenFile (zig.exe.obj)
        break zir_dir.createFile(&hex_digest, .{
                                ^
/home/geemili/code/zig/src/Compilation.zig:4253:18: 0x14052157a in workerAstGenFile (zig.exe.obj)
    pt.astGenFile(file, path_digest) catch |err| switch (err) {
                 ^
/home/geemili/code/zig/lib/std/Thread/Pool.zig:178:50: 0x140521cce in runFn (zig.exe.obj)
            @call(.auto, func, .{id.?} ++ closure.arguments);
                                                 ^
/home/geemili/code/zig/lib/std/Thread/Pool.zig:291:32: 0x1404dad5e in worker (zig.exe.obj)
            run_node.data.runFn(&run_node.data, id);
                               ^
/home/geemili/code/zig/lib/std/Thread.zig:486:13: 0x140300e6c in callFn__anon_179210 (zig.exe.obj)
            @call(.auto, f, args);
            ^
/home/geemili/code/zig/lib/std/Thread.zig:599:30: 0x1401abf1e in entryFn (zig.exe.obj)
                return callFn(f, self.fn_args);
                             ^
???:?:?: 0x6fffffa547f8 in ??? (kernel32.dll)
???:?:?: 0x6fffffc2f93a in ??? (ntdll.dll)
Z:\home\geemili\code\zig\lib\std\std.zig:1:1: error: unable to load 'Z:\home\geemili\code\zig\lib\std/std.zig': Unexpected

Running the test suite under wine also had a couple tests fail, presumably from similar issues. Hard to say without running it on Microsoft Windows.

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.

1 participant