From 5b03e248b7c88e7ac0266ba33fd25ca8b4cd7f15 Mon Sep 17 00:00:00 2001 From: ziggoon Date: Tue, 4 Mar 2025 22:10:49 -0600 Subject: [PATCH] add FFI & wrappers for NtAllocateVirtualMemory & NtFreeVirtualMemory + add missing alloction constants MEM_RESERVE_PLACEHOLDER / MEM_PRESERVE_PLACEHOLDER --- lib/std/os/windows.zig | 34 ++++++++++++++++++++++++++++++++++ lib/std/os/windows/ntdll.zig | 17 +++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/lib/std/os/windows.zig b/lib/std/os/windows.zig index 563b24cf83c390c6a62355c8b258423e685529fb..c29eb0f05a1442817ea294b4ff4bdd1a174e4040 100644 --- a/lib/std/os/windows.zig +++ b/lib/std/os/windows.zig @@ -1758,6 +1758,38 @@ pub fn TerminateProcess(hProcess: HANDLE, uExitCode: UINT) TerminateProcessError } } +pub const NtAllocateVirtualMemoryError = error{ + AccessDenied, + InvalidParameter, + NoMemory, + Unexpected, +}; + +pub fn NtAllocateVirtualMemory(hProcess: HANDLE, addr: ?*PVOID, zero_bits: ULONG_PTR, size: ?*SIZE_T, alloc_type: ULONG, protect: ULONG) NtAllocateVirtualMemoryError!void { + return switch (ntdll.NtAllocateVirtualMemory(hProcess, addr, zero_bits, size, alloc_type, protect)) { + .SUCCESS => return, + .ACCESS_DENIED => NtAllocateVirtualMemoryError.AccessDenied, + .INVALID_PARAMETER => NtAllocateVirtualMemoryError.InvalidParameter, + .NO_MEMORY => NtAllocateVirtualMemoryError.NoMemory, + else => |st| unexpectedStatus(st), + }; +} + +pub const NtFreeVirtualMemoryError = error{ + AccessDenied, + InvalidParameter, + Unexpected, +}; + +pub fn NtFreeVirtualMemory(hProcess: HANDLE, addr: ?*PVOID, size: *SIZE_T, free_type: ULONG) NtFreeVirtualMemoryError!void { + return switch (ntdll.NtFreeVirtualMemory(hProcess, addr, size, free_type)) { + .SUCCESS => return, + .ACCESS_DENIED => NtFreeVirtualMemoryError.AccessDenied, + .INVALID_PARAMETER => NtFreeVirtualMemoryError.InvalidParameter, + else => NtFreeVirtualMemoryError.Unexpected, + }; +} + pub const VirtualAllocError = error{Unexpected}; pub fn VirtualAlloc(addr: ?LPVOID, size: usize, alloc_type: DWORD, flProtect: DWORD) VirtualAllocError!LPVOID { @@ -3539,6 +3571,8 @@ pub const MEM_LARGE_PAGES = 0x20000000; pub const MEM_PHYSICAL = 0x400000; pub const MEM_TOP_DOWN = 0x100000; pub const MEM_WRITE_WATCH = 0x200000; +pub const MEM_RESERVE_PLACEHOLDER = 0x00040000; +pub const MEM_PRESERVE_PLACEHOLDER = 0x00000400; // Protect values pub const PAGE_EXECUTE = 0x10; diff --git a/lib/std/os/windows/ntdll.zig b/lib/std/os/windows/ntdll.zig index 490deaeacc5fe0df5a528ab595ece254c44a3d22..474aea3330c7a23c2a236238ce3fc3e8b1244655 100644 --- a/lib/std/os/windows/ntdll.zig +++ b/lib/std/os/windows/ntdll.zig @@ -5,6 +5,7 @@ const BOOL = windows.BOOL; const DWORD = windows.DWORD; const DWORD64 = windows.DWORD64; const ULONG = windows.ULONG; +const ULONG_PTR = windows.ULONG_PTR; const NTSTATUS = windows.NTSTATUS; const WORD = windows.WORD; const HANDLE = windows.HANDLE; @@ -358,3 +359,19 @@ pub extern "ntdll" fn NtCreateNamedPipeFile( OutboundQuota: ULONG, DefaultTimeout: *LARGE_INTEGER, ) callconv(.winapi) NTSTATUS; + +pub extern "ntdll" fn NtAllocateVirtualMemory( + ProcessHandle: HANDLE, + BaseAddress: ?*PVOID, + ZeroBits: ULONG_PTR, + RegionSize: ?*SIZE_T, + AllocationType: ULONG, + PageProtection: ULONG, +) callconv(.winapi) NTSTATUS; + +pub extern "ntdll" fn NtFreeVirtualMemory( + ProcessHandle: HANDLE, + BaseAddress: ?*PVOID, + RegionSize: *SIZE_T, + FreeType: ULONG, +) callconv(.winapi) NTSTATUS; -- 2.54.0