os.windows: Delete unused functions and kernel32 bindings

This commit is contained in:
Ryan Liptak 2025-11-23 19:44:28 -08:00
parent 9082b004b6
commit 17ecc77fc4
5 changed files with 3 additions and 154 deletions

View file

@ -46,7 +46,7 @@ pub var next_mmap_addr_hint: ?[*]align(page_size_min) u8 = null;
/// comptime-known minimum page size of the target. /// comptime-known minimum page size of the target.
/// ///
/// All pointers from `mmap` or `VirtualAlloc` are aligned to at least /// All pointers from `mmap` or `NtAllocateVirtualMemory` are aligned to at least
/// `page_size_min`, but their actual alignment may be bigger. /// `page_size_min`, but their actual alignment may be bigger.
/// ///
/// This value can be overridden via `std.options.page_size_min`. /// This value can be overridden via `std.options.page_size_min`.

View file

@ -56,21 +56,6 @@ pub var argv: [][*:0]u8 = if (builtin.link_libc) undefined else switch (native_o
else => undefined, else => undefined,
}; };
/// Call from Windows-specific code if you already have a WTF-16LE encoded, null terminated string.
/// Otherwise use `access`.
pub fn accessW(path: [*:0]const u16) windows.GetFileAttributesError!void {
const ret = try windows.GetFileAttributesW(path);
if (ret != windows.INVALID_FILE_ATTRIBUTES) {
return;
}
switch (windows.GetLastError()) {
.FILE_NOT_FOUND => return error.FileNotFound,
.PATH_NOT_FOUND => return error.FileNotFound,
.ACCESS_DENIED => return error.AccessDenied,
else => |err| return windows.unexpectedError(err),
}
}
pub fn isGetFdPathSupportedOnTarget(os: std.Target.Os) bool { pub fn isGetFdPathSupportedOnTarget(os: std.Target.Os) bool {
return switch (os.tag) { return switch (os.tag) {
.windows, .windows,

View file

@ -306,22 +306,6 @@ pub fn CreatePipe(rd: *HANDLE, wr: *HANDLE, sattr: *const SECURITY_ATTRIBUTES) C
wr.* = write; wr.* = write;
} }
pub fn CreateEventEx(attributes: ?*SECURITY_ATTRIBUTES, name: []const u8, flags: DWORD, desired_access: DWORD) !HANDLE {
const nameW = try sliceToPrefixedFileW(null, name);
return CreateEventExW(attributes, nameW.span().ptr, flags, desired_access);
}
pub fn CreateEventExW(attributes: ?*SECURITY_ATTRIBUTES, nameW: ?LPCWSTR, flags: DWORD, desired_access: DWORD) !HANDLE {
const handle = kernel32.CreateEventExW(attributes, nameW, flags, desired_access);
if (handle) |h| {
return h;
} else {
switch (GetLastError()) {
else => |err| return unexpectedError(err),
}
}
}
pub const DeviceIoControlError = error{ pub const DeviceIoControlError = error{
AccessDenied, AccessDenied,
/// The volume does not contain a recognized file system. File system /// The volume does not contain a recognized file system. File system
@ -598,10 +582,6 @@ pub fn CloseHandle(hObject: HANDLE) void {
assert(ntdll.NtClose(hObject) == .SUCCESS); assert(ntdll.NtClose(hObject) == .SUCCESS);
} }
pub fn FindClose(hFindFile: HANDLE) void {
assert(kernel32.FindClose(hFindFile) != 0);
}
pub const ReadFileError = error{ pub const ReadFileError = error{
BrokenPipe, BrokenPipe,
/// The specified network name is no longer available. /// The specified network name is no longer available.
@ -1515,30 +1495,6 @@ pub fn GetFileSizeEx(hFile: HANDLE) GetFileSizeError!u64 {
return @as(u64, @bitCast(file_size)); return @as(u64, @bitCast(file_size));
} }
pub const GetFileAttributesError = error{
FileNotFound,
AccessDenied,
Unexpected,
};
pub fn GetFileAttributes(filename: []const u8) (GetFileAttributesError || Wtf8ToPrefixedFileWError)!DWORD {
const filename_w = try sliceToPrefixedFileW(null, filename);
return GetFileAttributesW(filename_w.span().ptr);
}
pub fn GetFileAttributesW(lpFileName: [*:0]const u16) GetFileAttributesError!DWORD {
const rc = kernel32.GetFileAttributesW(lpFileName);
if (rc == INVALID_FILE_ATTRIBUTES) {
switch (GetLastError()) {
.FILE_NOT_FOUND => return error.FileNotFound,
.PATH_NOT_FOUND => return error.FileNotFound,
.ACCESS_DENIED => return error.AccessDenied,
else => |err| return unexpectedError(err),
}
}
return rc;
}
pub fn getpeername(s: ws2_32.SOCKET, name: *ws2_32.sockaddr, namelen: *ws2_32.socklen_t) i32 { pub fn getpeername(s: ws2_32.SOCKET, name: *ws2_32.sockaddr, namelen: *ws2_32.socklen_t) i32 {
return ws2_32.getpeername(s, name, @as(*i32, @ptrCast(namelen))); return ws2_32.getpeername(s, name, @as(*i32, @ptrCast(namelen)));
} }
@ -1657,6 +1613,7 @@ pub const NtFreeVirtualMemoryError = error{
}; };
pub fn NtFreeVirtualMemory(hProcess: HANDLE, addr: ?*PVOID, size: *SIZE_T, free_type: ULONG) NtFreeVirtualMemoryError!void { pub fn NtFreeVirtualMemory(hProcess: HANDLE, addr: ?*PVOID, size: *SIZE_T, free_type: ULONG) NtFreeVirtualMemoryError!void {
// TODO: If the return value is .INVALID_PAGE_PROTECTION, call RtlFlushSecureMemoryCache and try again.
return switch (ntdll.NtFreeVirtualMemory(hProcess, addr, size, free_type)) { return switch (ntdll.NtFreeVirtualMemory(hProcess, addr, size, free_type)) {
.SUCCESS => return, .SUCCESS => return,
.ACCESS_DENIED => NtFreeVirtualMemoryError.AccessDenied, .ACCESS_DENIED => NtFreeVirtualMemoryError.AccessDenied,
@ -1665,20 +1622,6 @@ pub fn NtFreeVirtualMemory(hProcess: HANDLE, addr: ?*PVOID, size: *SIZE_T, free_
}; };
} }
pub const VirtualAllocError = error{Unexpected};
pub fn VirtualAlloc(addr: ?LPVOID, size: usize, alloc_type: DWORD, flProtect: DWORD) VirtualAllocError!LPVOID {
return kernel32.VirtualAlloc(addr, size, alloc_type, flProtect) orelse {
switch (GetLastError()) {
else => |err| return unexpectedError(err),
}
};
}
pub fn VirtualFree(lpAddress: ?LPVOID, dwSize: usize, dwFreeType: DWORD) void {
assert(kernel32.VirtualFree(lpAddress, dwSize, dwFreeType) != 0);
}
pub const VirtualProtectError = error{ pub const VirtualProtectError = error{
InvalidAddress, InvalidAddress,
Unexpected, Unexpected,
@ -1713,19 +1656,6 @@ pub fn VirtualProtectEx(handle: HANDLE, addr: ?LPVOID, size: SIZE_T, new_prot: D
} }
} }
pub const VirtualQueryError = error{Unexpected};
pub fn VirtualQuery(lpAddress: ?LPVOID, lpBuffer: PMEMORY_BASIC_INFORMATION, dwLength: SIZE_T) VirtualQueryError!SIZE_T {
const rc = kernel32.VirtualQuery(lpAddress, lpBuffer, dwLength);
if (rc == 0) {
switch (GetLastError()) {
else => |err| return unexpectedError(err),
}
}
return rc;
}
pub const SetConsoleTextAttributeError = error{Unexpected}; pub const SetConsoleTextAttributeError = error{Unexpected};
pub fn SetConsoleTextAttribute(hConsoleOutput: HANDLE, wAttributes: WORD) SetConsoleTextAttributeError!void { pub fn SetConsoleTextAttribute(hConsoleOutput: HANDLE, wAttributes: WORD) SetConsoleTextAttributeError!void {
@ -2628,16 +2558,6 @@ test ntToWin32Namespace {
try std.testing.expectError(error.NameTooLong, ntToWin32Namespace(L("\\??\\C:\\test"), &too_small_buf)); try std.testing.expectError(error.NameTooLong, ntToWin32Namespace(L("\\??\\C:\\test"), &too_small_buf));
} }
fn getFullPathNameW(path: [*:0]const u16, out: []u16) !usize {
const result = kernel32.GetFullPathNameW(path, @as(u32, @intCast(out.len)), out.ptr, null);
if (result == 0) {
switch (GetLastError()) {
else => |err| return unexpectedError(err),
}
}
return result;
}
inline fn MAKELANGID(p: c_ushort, s: c_ushort) LANGID { inline fn MAKELANGID(p: c_ushort, s: c_ushort) LANGID {
return (s << 10) | p; return (s << 10) | p;
} }

View file

@ -86,23 +86,6 @@ pub extern "kernel32" fn CreateNamedPipeW(
lpSecurityAttributes: ?*const SECURITY_ATTRIBUTES, lpSecurityAttributes: ?*const SECURITY_ATTRIBUTES,
) callconv(.winapi) HANDLE; ) callconv(.winapi) HANDLE;
pub extern "kernel32" fn FindFirstFileW(
lpFileName: LPCWSTR,
lpFindFileData: *WIN32_FIND_DATAW,
) callconv(.winapi) HANDLE;
pub extern "kernel32" fn FindClose(
hFindFile: HANDLE,
) callconv(.winapi) BOOL;
// TODO: Wrapper around RtlGetFullPathName_UEx
pub extern "kernel32" fn GetFullPathNameW(
lpFileName: LPCWSTR,
nBufferLength: DWORD,
lpBuffer: LPWSTR,
lpFilePart: ?*?LPWSTR,
) callconv(.winapi) DWORD;
// TODO: Matches `STD_*_HANDLE` to peb().ProcessParameters.Standard* // TODO: Matches `STD_*_HANDLE` to peb().ProcessParameters.Standard*
pub extern "kernel32" fn GetStdHandle( pub extern "kernel32" fn GetStdHandle(
nStdHandle: DWORD, nStdHandle: DWORD,
@ -162,11 +145,6 @@ pub extern "kernel32" fn GetCurrentDirectoryW(
lpBuffer: ?[*]WCHAR, lpBuffer: ?[*]WCHAR,
) callconv(.winapi) DWORD; ) callconv(.winapi) DWORD;
// TODO: RtlDosPathNameToNtPathNameU_WithStatus + NtQueryAttributesFile.
pub extern "kernel32" fn GetFileAttributesW(
lpFileName: LPCWSTR,
) callconv(.winapi) DWORD;
pub extern "kernel32" fn ReadFile( pub extern "kernel32" fn ReadFile(
hFile: HANDLE, hFile: HANDLE,
lpBuffer: LPVOID, lpBuffer: LPVOID,
@ -182,14 +160,6 @@ pub extern "kernel32" fn GetSystemDirectoryW(
// I/O - Kernel Objects // I/O - Kernel Objects
// TODO: Wrapper around NtCreateEvent.
pub extern "kernel32" fn CreateEventExW(
lpEventAttributes: ?*SECURITY_ATTRIBUTES,
lpName: ?LPCWSTR,
dwFlags: DWORD,
dwDesiredAccess: DWORD,
) callconv(.winapi) ?HANDLE;
// TODO: Wrapper around GetStdHandle + NtDuplicateObject. // TODO: Wrapper around GetStdHandle + NtDuplicateObject.
pub extern "kernel32" fn DuplicateHandle( pub extern "kernel32" fn DuplicateHandle(
hSourceProcessHandle: HANDLE, hSourceProcessHandle: HANDLE,
@ -318,9 +288,6 @@ pub extern "kernel32" fn GetExitCodeProcess(
lpExitCode: *DWORD, lpExitCode: *DWORD,
) callconv(.winapi) BOOL; ) callconv(.winapi) BOOL;
// TODO: Already a wrapper for this, see `windows.GetCurrentProcess`.
pub extern "kernel32" fn GetCurrentProcess() callconv(.winapi) HANDLE;
// TODO: Wrapper around RtlSetEnvironmentVar. // TODO: Wrapper around RtlSetEnvironmentVar.
pub extern "kernel32" fn SetEnvironmentVariableW( pub extern "kernel32" fn SetEnvironmentVariableW(
lpName: LPCWSTR, lpName: LPCWSTR,
@ -465,29 +432,6 @@ pub extern "kernel32" fn HeapValidate(
lpMem: ?*const anyopaque, lpMem: ?*const anyopaque,
) callconv(.winapi) BOOL; ) callconv(.winapi) BOOL;
// TODO: Wrapper around NtAllocateVirtualMemory.
pub extern "kernel32" fn VirtualAlloc(
lpAddress: ?LPVOID,
dwSize: SIZE_T,
flAllocationType: DWORD,
flProtect: DWORD,
) callconv(.winapi) ?LPVOID;
// TODO: Wrapper around NtFreeVirtualMemory.
// If the return value is .INVALID_PAGE_PROTECTION, calls RtlFlushSecureMemoryCache and try again.
pub extern "kernel32" fn VirtualFree(
lpAddress: ?LPVOID,
dwSize: SIZE_T,
dwFreeType: DWORD,
) callconv(.winapi) BOOL;
// TODO: Wrapper around NtQueryVirtualMemory.
pub extern "kernel32" fn VirtualQuery(
lpAddress: ?LPVOID,
lpBuffer: PMEMORY_BASIC_INFORMATION,
dwLength: SIZE_T,
) callconv(.winapi) SIZE_T;
// TODO: Getter for peb.ProcessHeap // TODO: Getter for peb.ProcessHeap
pub extern "kernel32" fn GetProcessHeap() callconv(.winapi) ?HANDLE; pub extern "kernel32" fn GetProcessHeap() callconv(.winapi) ?HANDLE;

View file

@ -4409,7 +4409,7 @@ pub fn mmap(
/// Note that while POSIX allows unmapping a region in the middle of an existing mapping, /// Note that while POSIX allows unmapping a region in the middle of an existing mapping,
/// Zig's munmap function does not, for two reasons: /// Zig's munmap function does not, for two reasons:
/// * It violates the Zig principle that resource deallocation must succeed. /// * It violates the Zig principle that resource deallocation must succeed.
/// * The Windows function, VirtualFree, has this restriction. /// * The Windows function, NtFreeVirtualMemory, has this restriction.
pub fn munmap(memory: []align(page_size_min) const u8) void { pub fn munmap(memory: []align(page_size_min) const u8) void {
switch (errno(system.munmap(memory.ptr, memory.len))) { switch (errno(system.munmap(memory.ptr, memory.len))) {
.SUCCESS => return, .SUCCESS => return,