Skip to content

start.zig: Replace kernel32 usage with ntdll/PEB #17839

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

Merged
merged 1 commit into from
Nov 3, 2023
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
4 changes: 4 additions & 0 deletions lib/std/os/windows/ntdll.zig
Original file line number Diff line number Diff line change
Expand Up @@ -330,3 +330,7 @@ pub extern "ntdll" fn NtProtectVirtualMemory(
NewAccessProtection: ULONG,
OldAccessProtection: *ULONG,
) callconv(WINAPI) NTSTATUS;

pub extern "ntdll" fn RtlExitUserProcess(
ExitStatus: u32,
) callconv(WINAPI) noreturn;
12 changes: 5 additions & 7 deletions lib/std/start.zig
Original file line number Diff line number Diff line change
Expand Up @@ -168,15 +168,13 @@ fn exit2(code: usize) noreturn {
// exits(0)
.plan9 => std.os.plan9.exits(null),
.windows => {
ExitProcess(@as(u32, @truncate(code)));
std.os.windows.ntdll.RtlExitUserProcess(@as(u32, @truncate(code)));
},
else => @compileError("TODO"),
}
unreachable;
}

extern "kernel32" fn ExitProcess(exit_code: u32) callconv(.C) noreturn;

////////////////////////////////////////////////////////////////////////////////

fn _DllMainCRTStartup(
Expand Down Expand Up @@ -345,7 +343,7 @@ fn WinStartup() callconv(std.os.windows.WINAPI) noreturn {

std.debug.maybeEnableSegfaultHandler();

std.os.windows.kernel32.ExitProcess(initEventLoopAndCallMain());
std.os.windows.ntdll.RtlExitUserProcess(initEventLoopAndCallMain());
}

fn wWinMainCRTStartup() callconv(std.os.windows.WINAPI) noreturn {
Expand All @@ -357,7 +355,7 @@ fn wWinMainCRTStartup() callconv(std.os.windows.WINAPI) noreturn {
std.debug.maybeEnableSegfaultHandler();

const result: std.os.windows.INT = initEventLoopAndCallWinMain();
std.os.windows.kernel32.ExitProcess(@as(std.os.windows.UINT, @bitCast(result)));
std.os.windows.ntdll.RtlExitUserProcess(@as(std.os.windows.UINT, @bitCast(result)));
}

fn posixCallMainAndExit() callconv(.C) noreturn {
Expand Down Expand Up @@ -605,8 +603,8 @@ pub fn callMain() u8 {
pub fn call_wWinMain() std.os.windows.INT {
const peb = std.os.windows.peb();
const MAIN_HINSTANCE = @typeInfo(@TypeOf(root.wWinMain)).Fn.params[0].type.?;
const hInstance = @as(MAIN_HINSTANCE, @ptrCast(std.os.windows.kernel32.GetModuleHandleW(null).?));
const lpCmdLine = std.os.windows.kernel32.GetCommandLineW();
const hInstance = @as(MAIN_HINSTANCE, @ptrCast(peb.ImageBaseAddress));
const lpCmdLine: [*:0]u16 = @ptrCast(peb.ProcessParameters.CommandLine.Buffer);

// There are various types used for the 'show window' variable through the Win32 APIs:
// - u16 in STARTUPINFOA.wShowWindow / STARTUPINFOW.wShowWindow
Expand Down