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

Conversation

squeek502
Copy link
Collaborator

@squeek502 squeek502 commented Nov 3, 2023

Credit to @e4m2 for identifying which ntdll functions/PEB fields could be used to replace the kernel32 functions (#17763 (comment)). Progress towards #1840

Functionality is equivalent before and after:

Test code:

winmain-zig.zig:

const std = @import("std");

pub fn wWinMain(
    inst: std.os.windows.HINSTANCE,
    prev: ?std.os.windows.HINSTANCE,
    cmd_line: std.os.windows.LPWSTR,
    cmd_show: i32,
) std.os.windows.INT {
    std.debug.print("inst: {}\n", .{inst});
    std.debug.print("prev: {?}\n", .{prev});
    const cmd_line_slice = std.mem.span(cmd_line);
    std.debug.print("cmd_line: {}\n", .{std.unicode.fmtUtf16le(cmd_line_slice)});
    std.debug.print("cmd_show: {}\n", .{cmd_show});
    return 0;
}

Before:

> winmain-zig-kernel32.exe a b c "кириллица"
inst: os.windows.HINSTANCE__opaque_2599@7ff62c1f0000
prev: null
cmd_line: winmain-zig-kernel32.exe  a b c "кириллица"
cmd_show: 10

NtTrace of how the process is terminated:

NtTerminateProcess( ProcessHandle=0, ExitStatus=0 ) => 0
NtClose( Handle=0x78 ) => 0
NtClose( Handle=0x50 ) => 0
NtClose( Handle=0x70 ) => 0
NtClose( Handle=0x74 ) => 0
Process 15996 exit code: 0

After:

> winmain-zig-ntdll.exe a b c "кириллица"
inst: os.windows.HINSTANCE__opaque_2614@7ff6dc490000
prev: null
cmd_line: winmain-zig-ntdll.exe  a b c "кириллица"
cmd_show: 10

NtTrace:

NtTerminateProcess( ProcessHandle=0, ExitStatus=0 ) => 0
NtClose( Handle=0x78 ) => 0
NtClose( Handle=0x60 ) => 0
NtClose( Handle=0x70 ) => 0
NtClose( Handle=0x74 ) => 0
Process 23044 exit code: 0

@squeek502 squeek502 changed the title start.zig: Replace kernel32 usage with ntdll start.zig: Replace kernel32 usage with ntdll/PEB Nov 3, 2023
@Vexu Vexu merged commit ae6df9e into ziglang:master Nov 3, 2023
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.

2 participants