Skip to content

Commit 7f2754f

Browse files
committed
add FFI & wrappers for NtAllocateVirtualMemory & NtFreeVirtualMemory + add missing alloction constants MEM_RESERVE_PLACEHOLDER / MEM_PRESERVE_PLACEHOLDER
1 parent 05937b3 commit 7f2754f

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 {
@@ -3554,6 +3586,8 @@ pub const PAGE_TARGETS_NO_UPDATE = 0x40000000; // Same as PAGE_TARGETS_INVALID
35543586
pub const PAGE_GUARD = 0x100;
35553587
pub const PAGE_NOCACHE = 0x200;
35563588
pub const PAGE_WRITECOMBINE = 0x400;
3589+
pub const MEM_RESERVE_PLACEHOLDER = 0x00040000;
3590+
pub const MEM_PRESERVE_PLACEHOLDER = 0x00000400;
35573591

35583592
// FreeType values
35593593
pub const MEM_COALESCE_PLACEHOLDERS = 0x1;

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)