authorgravatar for zackmusgrave20@gmail.comziggoon <zackmusgrave20@gmail.com> 2025-03-04 22:10:49-06:00
committergravatar for zackmusgrave20@gmail.comziggoon <zackmusgrave20@gmail.com> 2025-03-04 22:10:49-06:00
log5b03e248b7c88e7ac0266ba33fd25ca8b4cd7f15
tree4783399ea15fc4ba5b72f30c23c2a66b61dfd777
parent16875b35985e3343586e7aafee4eeba90c11b753

add FFI & wrappers for NtAllocateVirtualMemory & NtFreeVirtualMemory + add missing alloction constants MEM_RESERVE_PLACEHOLDER / MEM_PRESERVE_PLACEHOLDER


2 files changed, 51 insertions(+), 0 deletions(-)

lib/std/os/windows.zig+34
......@@ -1758,6 +1758,38 @@ pub fn TerminateProcess(hProcess: HANDLE, uExitCode: UINT) TerminateProcessError
17581758 }
17591759}
17601760
1761pub const NtAllocateVirtualMemoryError = error{
1762 AccessDenied,
1763 InvalidParameter,
1764 NoMemory,
1765 Unexpected,
1766};
1767
1768pub fn NtAllocateVirtualMemory(hProcess: HANDLE, addr: ?*PVOID, zero_bits: ULONG_PTR, size: ?*SIZE_T, alloc_type: ULONG, protect: ULONG) NtAllocateVirtualMemoryError!void {
1769 return switch (ntdll.NtAllocateVirtualMemory(hProcess, addr, zero_bits, size, alloc_type, protect)) {
1770 .SUCCESS => return,
1771 .ACCESS_DENIED => NtAllocateVirtualMemoryError.AccessDenied,
1772 .INVALID_PARAMETER => NtAllocateVirtualMemoryError.InvalidParameter,
1773 .NO_MEMORY => NtAllocateVirtualMemoryError.NoMemory,
1774 else => |st| unexpectedStatus(st),
1775 };
1776}
1777
1778pub const NtFreeVirtualMemoryError = error{
1779 AccessDenied,
1780 InvalidParameter,
1781 Unexpected,
1782};
1783
1784pub fn NtFreeVirtualMemory(hProcess: HANDLE, addr: ?*PVOID, size: *SIZE_T, free_type: ULONG) NtFreeVirtualMemoryError!void {
1785 return switch (ntdll.NtFreeVirtualMemory(hProcess, addr, size, free_type)) {
1786 .SUCCESS => return,
1787 .ACCESS_DENIED => NtFreeVirtualMemoryError.AccessDenied,
1788 .INVALID_PARAMETER => NtFreeVirtualMemoryError.InvalidParameter,
1789 else => NtFreeVirtualMemoryError.Unexpected,
1790 };
1791}
1792
17611793pub const VirtualAllocError = error{Unexpected};
17621794
17631795pub fn VirtualAlloc(addr: ?LPVOID, size: usize, alloc_type: DWORD, flProtect: DWORD) VirtualAllocError!LPVOID {
......@@ -3539,6 +3571,8 @@ pub const MEM_LARGE_PAGES = 0x20000000;
35393571pub const MEM_PHYSICAL = 0x400000;
35403572pub const MEM_TOP_DOWN = 0x100000;
35413573pub const MEM_WRITE_WATCH = 0x200000;
3574pub const MEM_RESERVE_PLACEHOLDER = 0x00040000;
3575pub const MEM_PRESERVE_PLACEHOLDER = 0x00000400;
35423576
35433577// Protect values
35443578pub const PAGE_EXECUTE = 0x10;
lib/std/os/windows/ntdll.zig+17
......@@ -5,6 +5,7 @@ const BOOL = windows.BOOL;
55const DWORD = windows.DWORD;
66const DWORD64 = windows.DWORD64;
77const ULONG = windows.ULONG;
8const ULONG_PTR = windows.ULONG_PTR;
89const NTSTATUS = windows.NTSTATUS;
910const WORD = windows.WORD;
1011const HANDLE = windows.HANDLE;
......@@ -358,3 +359,19 @@ pub extern "ntdll" fn NtCreateNamedPipeFile(
358359 OutboundQuota: ULONG,
359360 DefaultTimeout: *LARGE_INTEGER,
360361) callconv(.winapi) NTSTATUS;
362
363pub extern "ntdll" fn NtAllocateVirtualMemory(
364 ProcessHandle: HANDLE,
365 BaseAddress: ?*PVOID,
366 ZeroBits: ULONG_PTR,
367 RegionSize: ?*SIZE_T,
368 AllocationType: ULONG,
369 PageProtection: ULONG,
370) callconv(.winapi) NTSTATUS;
371
372pub extern "ntdll" fn NtFreeVirtualMemory(
373 ProcessHandle: HANDLE,
374 BaseAddress: ?*PVOID,
375 RegionSize: *SIZE_T,
376 FreeType: ULONG,
377) callconv(.winapi) NTSTATUS;