From 5e48ed4a8d700c213a8cc71e7e8f0c57be52c70d Mon Sep 17 00:00:00 2001 From: Anthony Arian Date: Mon, 15 Jun 2020 14:24:25 +0100 Subject: [PATCH 1/2] Implement WinMain Callers that Pass Valid Params --- lib/std/os/windows/bits.zig | 4 +- lib/std/os/windows/kernel32.zig | 1 + lib/std/start.zig | 68 +++++++++++++++++++++++++++++---- src/link.cpp | 6 +-- 4 files changed, 65 insertions(+), 14 deletions(-) diff --git a/lib/std/os/windows/bits.zig b/lib/std/os/windows/bits.zig index 191e8deded..27f949227a 100644 --- a/lib/std/os/windows/bits.zig +++ b/lib/std/os/windows/bits.zig @@ -30,9 +30,9 @@ pub const HCRYPTPROV = ULONG_PTR; pub const HBRUSH = *@Type(.Opaque); pub const HCURSOR = *@Type(.Opaque); pub const HICON = *@Type(.Opaque); -pub const HINSTANCE = *@Type(.Opaque); +pub const HINSTANCE = HANDLE; pub const HMENU = *@Type(.Opaque); -pub const HMODULE = *@Type(.Opaque); +pub const HMODULE = HANDLE; pub const HWND = *@Type(.Opaque); pub const HDC = *@Type(.Opaque); pub const HGLRC = *@Type(.Opaque); diff --git a/lib/std/os/windows/kernel32.zig b/lib/std/os/windows/kernel32.zig index b50143b075..88a50ef5e2 100644 --- a/lib/std/os/windows/kernel32.zig +++ b/lib/std/os/windows/kernel32.zig @@ -79,6 +79,7 @@ pub extern "kernel32" fn FormatMessageW(dwFlags: DWORD, lpSource: ?LPVOID, dwMes pub extern "kernel32" fn FreeEnvironmentStringsW(penv: [*:0]u16) callconv(.Stdcall) BOOL; pub extern "kernel32" fn GetCommandLineA() callconv(.Stdcall) LPSTR; +pub extern "kernel32" fn GetCommandLineW() callconv(.Stdcall) LPWSTR; pub extern "kernel32" fn GetConsoleMode(in_hConsoleHandle: HANDLE, out_lpMode: *DWORD) callconv(.Stdcall) BOOL; diff --git a/lib/std/start.zig b/lib/std/start.zig index 604c22101c..9b1a330391 100644 --- a/lib/std/start.zig +++ b/lib/std/start.zig @@ -23,8 +23,16 @@ comptime { } else if (builtin.os.tag == .windows) { if (!@hasDecl(root, "WinMain") and !@hasDecl(root, "WinMainCRTStartup") and !@hasDecl(root, "wWinMain") and !@hasDecl(root, "wWinMainCRTStartup")) + { + @export(WinStartup, .{ .name = "WinMainCRTStartup" }); + } else if (@hasDecl(root, "WinMain") and !@hasDecl(root, "WinMainCRTStartup") and + !@hasDecl(root, "wWinMain") and !@hasDecl(root, "wWinMainCRTStartup")) { @export(WinMainCRTStartup, .{ .name = "WinMainCRTStartup" }); + } else if (@hasDecl(root, "wWinMain") and !@hasDecl(root, "wWinMainCRTStartup") and + !@hasDecl(root, "WinMain") and !@hasDecl(root, "WinMainCRTStartup")) + { + @export(wWinMainCRTStartup, .{ .name = "wWinMainCRTStartup" }); } } else if (builtin.os.tag == .uefi) { if (!@hasDecl(root, "EfiMain")) @export(EfiMain, .{ .name = "EfiMain" }); @@ -123,6 +131,17 @@ fn _start() callconv(.Naked) noreturn { @call(.{ .modifier = .never_inline }, posixCallMainAndExit, .{}); } +fn WinStartup() callconv(.Stdcall) noreturn { + @setAlignStack(16); + if (!builtin.single_threaded) { + _ = @import("start_windows_tls.zig"); + } + + std.debug.maybeEnableSegfaultHandler(); + + std.os.windows.kernel32.ExitProcess(initEventLoopAndCallMain(u8, callMain)); +} + fn WinMainCRTStartup() callconv(.Stdcall) noreturn { @setAlignStack(16); if (!builtin.single_threaded) { @@ -131,7 +150,20 @@ fn WinMainCRTStartup() callconv(.Stdcall) noreturn { std.debug.maybeEnableSegfaultHandler(); - std.os.windows.kernel32.ExitProcess(initEventLoopAndCallMain()); + const result = initEventLoopAndCallMain(std.os.windows.INT, callWinMain); + std.os.windows.kernel32.ExitProcess(@bitCast(std.os.windows.UINT, result)); +} + +fn wWinMainCRTStartup() callconv(.Stdcall) noreturn { + @setAlignStack(16); + if (!builtin.single_threaded) { + _ = @import("start_windows_tls.zig"); + } + + std.debug.maybeEnableSegfaultHandler(); + + const result = initEventLoopAndCallMain(std.os.windows.INT, callWWinMain); + std.os.windows.kernel32.ExitProcess(@bitCast(std.os.windows.UINT, result)); } // TODO https://github.com/ziglang/zig/issues/265 @@ -185,7 +217,7 @@ fn callMainWithArgs(argc: usize, argv: [*][*:0]u8, envp: [][*:0]u8) u8 { std.debug.maybeEnableSegfaultHandler(); - return initEventLoopAndCallMain(); + return initEventLoopAndCallMain(u8, callMain); } fn main(c_argc: i32, c_argv: [*][*:0]u8, c_envp: [*:null]?[*:0]u8) callconv(.C) i32 { @@ -200,7 +232,7 @@ const bad_main_ret = "expected return type of main to be 'void', '!void', 'noret // This is marked inline because for some reason LLVM in release mode fails to inline it, // and we want fewer call frames in stack traces. -inline fn initEventLoopAndCallMain() u8 { +inline fn initEventLoopAndCallMain(comptime Out: type, comptime mainFunc: fn () Out) Out { if (std.event.Loop.instance) |loop| { if (!@hasDecl(root, "event_loop")) { loop.init() catch |err| { @@ -214,7 +246,7 @@ inline fn initEventLoopAndCallMain() u8 { var result: u8 = undefined; var frame: @Frame(callMainAsync) = undefined; - _ = @asyncCall(&frame, &result, callMainAsync, loop); + _ = @asyncCall(&frame, &result, callMainAsync, u8, mainFunc, loop); loop.run(); return result; } @@ -222,13 +254,13 @@ inline fn initEventLoopAndCallMain() u8 { // This is marked inline because for some reason LLVM in release mode fails to inline it, // and we want fewer call frames in stack traces. - return @call(.{ .modifier = .always_inline }, callMain, .{}); + return @call(.{ .modifier = .always_inline }, mainFunc, .{}); } -fn callMainAsync(loop: *std.event.Loop) callconv(.Async) u8 { +fn callMainAsync(comptime Out: type, comptime mainProc: fn () Out, loop: *std.event.Loop) callconv(.Async) Out { // This prevents the event loop from terminating at least until main() has returned. loop.beginOneEvent(); defer loop.finishOneEvent(); - return callMain(); + return mainProc(); } // This is not marked inline because it is called with @asyncCall when @@ -270,3 +302,25 @@ pub fn callMain() u8 { else => @compileError(bad_main_ret), } } + +pub fn callWinMain() std.os.windows.INT { + const hInstance = std.os.windows.kernel32.GetModuleHandleA(null); + const lpCmdLine = std.os.windows.kernel32.GetCommandLineA(); + + // There's no (documented) way to get the nCmdShow parameter, so we're + // using this fairly standard default. + const nCmdShow = std.os.windows.user32.SW_SHOW; + + return root.WinMain(hInstance, null, lpCmdLine, nCmdShow); +} + +pub fn callWWinMain() std.os.windows.INT { + const hInstance = std.os.windows.kernel32.GetModuleHandleA(null); + const lpCmdLine = std.os.windows.kernel32.GetCommandLineW(); + + // There's no (documented) way to get the nCmdShow parameter, so we're + // using this fairly standard default. + const nCmdShow = std.os.windows.user32.SW_SHOW; + + return root.wWinMain(hInstance, null, lpCmdLine, nCmdShow); +} diff --git a/src/link.cpp b/src/link.cpp index 0f27cee9ab..5156c2043c 100644 --- a/src/link.cpp +++ b/src/link.cpp @@ -2459,11 +2459,7 @@ static void add_win_link_args(LinkJob *lj, bool is_library, bool *have_windows_d } else { lj->args.append("-NODEFAULTLIB"); if (!is_library) { - if (lj->codegen->have_winmain) { - lj->args.append("-ENTRY:WinMain"); - } else if (lj->codegen->have_wwinmain) { - lj->args.append("-ENTRY:wWinMain"); - } else if (lj->codegen->have_wwinmain_crt_startup) { + if (lj->codegen->have_wwinmain_crt_startup) { lj->args.append("-ENTRY:wWinMainCRTStartup"); } else { lj->args.append("-ENTRY:WinMainCRTStartup"); From 68fe3e116d9c4bde67df990b8e0cbb3e70fc98b2 Mon Sep 17 00:00:00 2001 From: DixiE Date: Mon, 15 Jun 2020 23:33:58 +0100 Subject: [PATCH 2/2] Update Stack Trace For start.zig Changes --- test/stack_traces.zig | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/stack_traces.zig b/test/stack_traces.zig index 616136b9ee..78ab702408 100644 --- a/test/stack_traces.zig +++ b/test/stack_traces.zig @@ -282,10 +282,10 @@ pub fn addCases(cases: *tests.StackTracesContext) void { \\source.zig:10:8: [address] in main (test) \\ foo(); \\ ^ - \\start.zig:249:29: [address] in std.start.posixCallMainAndExit (test) + \\start.zig:281:29: [address] in std.start.posixCallMainAndExit (test) \\ return root.main(); \\ ^ - \\start.zig:123:5: [address] in std.start._start (test) + \\start.zig:131:5: [address] in std.start._start (test) \\ @call(.{ .modifier = .never_inline }, posixCallMainAndExit, .{}); \\ ^ \\ @@ -294,7 +294,7 @@ pub fn addCases(cases: *tests.StackTracesContext) void { switch (std.Target.current.cpu.arch) { .aarch64 => "", // TODO disabled; results in segfault else => - \\start.zig:123:5: [address] in std.start._start (test) + \\start.zig:131:5: [address] in std.start._start (test) \\ @call(.{ .modifier = .never_inline }, posixCallMainAndExit, .{}); \\ ^ \\