Skip to content

Commit 5b03e24

Browse files
committed
add FFI & wrappers for NtAllocateVirtualMemory & NtFreeVirtualMemory + add missing alloction constants MEM_RESERVE_PLACEHOLDER / MEM_PRESERVE_PLACEHOLDER
1 parent 16875b3 commit 5b03e24

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

lib/std/os/windows.zig

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1758,6 +1758,38 @@ pub fn TerminateProcess(hProcess: HANDLE, uExitCode: UINT) TerminateProcessError
17581758
}
17591759
}
17601760

1761+
pub const NtAllocateVirtualMemoryError = error{
1762+
AccessDenied,
1763+
InvalidParameter,
1764+
NoMemory,
1765+
Unexpected,
1766+
};
1767+
1768+
pub 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+
1778+
pub const NtFreeVirtualMemoryError = error{
1779+
AccessDenied,
1780+
InvalidParameter,
1781+
Unexpected,
1782+
};
1783+
1784+
pub 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+
17611793
pub const VirtualAllocError = error{Unexpected};
17621794

17631795
pub fn VirtualAlloc(addr: ?LPVOID, size: usize, alloc_type: DWORD, flProtect: DWORD) VirtualAllocError!LPVOID {
@@ -3539,6 +3571,8 @@ pub const MEM_LARGE_PAGES = 0x20000000;
35393571
pub const MEM_PHYSICAL = 0x400000;
35403572
pub const MEM_TOP_DOWN = 0x100000;
35413573
pub const MEM_WRITE_WATCH = 0x200000;
3574+
pub const MEM_RESERVE_PLACEHOLDER = 0x00040000;
3575+
pub const MEM_PRESERVE_PLACEHOLDER = 0x00000400;
35423576

35433577
// Protect values
35443578
pub const PAGE_EXECUTE = 0x10;

lib/std/os/windows/ntdll.zig

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const BOOL = windows.BOOL;
55
const DWORD = windows.DWORD;
66
const DWORD64 = windows.DWORD64;
77
const ULONG = windows.ULONG;
8+
const ULONG_PTR = windows.ULONG_PTR;
89
const NTSTATUS = windows.NTSTATUS;
910
const WORD = windows.WORD;
1011
const HANDLE = windows.HANDLE;
@@ -358,3 +359,19 @@ pub extern "ntdll" fn NtCreateNamedPipeFile(
358359
OutboundQuota: ULONG,
359360
DefaultTimeout: *LARGE_INTEGER,
360361
) callconv(.winapi) NTSTATUS;
362+
363+
pub 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+
372+
pub extern "ntdll" fn NtFreeVirtualMemory(
373+
ProcessHandle: HANDLE,
374+
BaseAddress: ?*PVOID,
375+
RegionSize: *SIZE_T,
376+
FreeType: ULONG,
377+
) callconv(.winapi) NTSTATUS;

0 commit comments

Comments
 (0)