ReactOS 0.4.16-dev-109-gf4cb10f
virtmem.c File Reference
#include <k32.h>
#include <debug.h>
Include dependency graph for virtmem.c:

Go to the source code of this file.

Macros

#define NDEBUG
 

Functions

LPVOID NTAPI VirtualAllocEx (IN HANDLE hProcess, IN LPVOID lpAddress, IN SIZE_T dwSize, IN DWORD flAllocationType, IN DWORD flProtect)
 
LPVOID NTAPI VirtualAlloc (IN LPVOID lpAddress, IN SIZE_T dwSize, IN DWORD flAllocationType, IN DWORD flProtect)
 
BOOL NTAPI VirtualFreeEx (IN HANDLE hProcess, IN LPVOID lpAddress, IN SIZE_T dwSize, IN DWORD dwFreeType)
 
BOOL NTAPI VirtualFree (IN LPVOID lpAddress, IN SIZE_T dwSize, IN DWORD dwFreeType)
 
BOOL NTAPI VirtualProtect (IN LPVOID lpAddress, IN SIZE_T dwSize, IN DWORD flNewProtect, OUT PDWORD lpflOldProtect)
 
BOOL NTAPI VirtualProtectEx (IN HANDLE hProcess, IN LPVOID lpAddress, IN SIZE_T dwSize, IN DWORD flNewProtect, OUT PDWORD lpflOldProtect)
 
BOOL NTAPI VirtualLock (IN LPVOID lpAddress, IN SIZE_T dwSize)
 
SIZE_T NTAPI VirtualQuery (IN LPCVOID lpAddress, OUT PMEMORY_BASIC_INFORMATION lpBuffer, IN SIZE_T dwLength)
 
SIZE_T NTAPI VirtualQueryEx (IN HANDLE hProcess, IN LPCVOID lpAddress, OUT PMEMORY_BASIC_INFORMATION lpBuffer, IN SIZE_T dwLength)
 
BOOL NTAPI VirtualUnlock (IN LPVOID lpAddress, IN SIZE_T dwSize)
 
UINT WINAPI GetWriteWatch (IN DWORD dwFlags, IN PVOID lpBaseAddress, IN SIZE_T dwRegionSize, IN PVOID *lpAddresses, OUT PULONG_PTR lpdwCount, OUT PULONG lpdwGranularity)
 
UINT WINAPI ResetWriteWatch (IN LPVOID lpBaseAddress, IN SIZE_T dwRegionSize)
 
BOOL WINAPI AllocateUserPhysicalPages (IN HANDLE hProcess, IN PULONG_PTR NumberOfPages, OUT PULONG_PTR UserPfnArray)
 
BOOL WINAPI FreeUserPhysicalPages (IN HANDLE hProcess, IN PULONG_PTR NumberOfPages, IN PULONG_PTR PageArray)
 
BOOL WINAPI MapUserPhysicalPages (IN PVOID VirtualAddress, IN ULONG_PTR NumberOfPages, OUT PULONG_PTR PageArray OPTIONAL)
 
BOOL WINAPI MapUserPhysicalPagesScatter (IN PVOID *VirtualAddresses, IN ULONG_PTR NumberOfPages, OUT PULONG_PTR PageArray OPTIONAL)
 

Macro Definition Documentation

◆ NDEBUG

#define NDEBUG

Definition at line 13 of file virtmem.c.

Function Documentation

◆ AllocateUserPhysicalPages()

BOOL WINAPI AllocateUserPhysicalPages ( IN HANDLE  hProcess,
IN PULONG_PTR  NumberOfPages,
OUT PULONG_PTR  UserPfnArray 
)

Definition at line 338 of file virtmem.c.

341{
343
344 Status = NtAllocateUserPhysicalPages(hProcess, NumberOfPages, UserPfnArray);
345 if (!NT_SUCCESS(Status))
346 {
348 return FALSE;
349 }
350
351 return TRUE;
352}
LONG NTSTATUS
Definition: precomp.h:26
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:33
Status
Definition: gdiplustypes.h:25
_In_ BOOL _In_ HANDLE hProcess
Definition: mapping.h:71
NTSTATUS NTAPI NtAllocateUserPhysicalPages(IN HANDLE ProcessHandle, IN OUT PULONG_PTR NumberOfPages, IN OUT PULONG_PTR UserPfnArray)
Definition: procsup.c:1445
DWORD BaseSetLastNTError(IN NTSTATUS Status)
Definition: reactos.cpp:167

◆ FreeUserPhysicalPages()

BOOL WINAPI FreeUserPhysicalPages ( IN HANDLE  hProcess,
IN PULONG_PTR  NumberOfPages,
IN PULONG_PTR  PageArray 
)

Definition at line 359 of file virtmem.c.

362{
364
365 Status = NtFreeUserPhysicalPages(hProcess, NumberOfPages, PageArray);
366 if (!NT_SUCCESS(Status))
367 {
369 return FALSE;
370 }
371
372 return TRUE;
373}
NTSTATUS NTAPI NtFreeUserPhysicalPages(IN HANDLE ProcessHandle, IN OUT PULONG_PTR NumberOfPages, IN OUT PULONG_PTR UserPfnArray)
Definition: procsup.c:1475

◆ GetWriteWatch()

UINT WINAPI GetWriteWatch ( IN DWORD  dwFlags,
IN PVOID  lpBaseAddress,
IN SIZE_T  dwRegionSize,
IN PVOID lpAddresses,
OUT PULONG_PTR  lpdwCount,
OUT PULONG  lpdwGranularity 
)

Definition at line 286 of file virtmem.c.

292{
294
296 dwFlags,
297 lpBaseAddress,
298 dwRegionSize,
299 lpAddresses,
300 lpdwCount,
301 lpdwGranularity);
302 if (!NT_SUCCESS(Status))
303 {
305 return -1;
306 }
307
308 return 0;
309}
#define GetCurrentProcess()
Definition: compat.h:759
NTSTATUS NTAPI NtGetWriteWatch(IN HANDLE ProcessHandle, IN ULONG Flags, IN PVOID BaseAddress, IN SIZE_T RegionSize, IN PVOID *UserAddressArray, OUT PULONG_PTR EntriesInUserAddressArray, OUT PULONG Granularity)
Definition: virtual.c:4157
_In_ PCCERT_CONTEXT _In_ DWORD dwFlags
Definition: wincrypt.h:1176

◆ MapUserPhysicalPages()

BOOL WINAPI MapUserPhysicalPages ( IN PVOID  VirtualAddress,
IN ULONG_PTR  NumberOfPages,
OUT PULONG_PTR PageArray  OPTIONAL 
)

Definition at line 380 of file virtmem.c.

383{
385
386 Status = NtMapUserPhysicalPages(VirtualAddress, NumberOfPages, PageArray);
387 if (!NT_SUCCESS(Status))
388 {
390 return FALSE;
391 }
392
393 return TRUE;
394}
NTSTATUS NTAPI NtMapUserPhysicalPages(IN PVOID VirtualAddresses, IN ULONG_PTR NumberOfPages, IN OUT PULONG_PTR UserPfnArray)
Definition: procsup.c:1455
_Must_inspect_result_ _In_ WDFDMATRANSACTION _In_ PFN_WDF_PROGRAM_DMA _In_ WDF_DMA_DIRECTION _In_ PMDL _In_ PVOID VirtualAddress

◆ MapUserPhysicalPagesScatter()

BOOL WINAPI MapUserPhysicalPagesScatter ( IN PVOID VirtualAddresses,
IN ULONG_PTR  NumberOfPages,
OUT PULONG_PTR PageArray  OPTIONAL 
)

Definition at line 401 of file virtmem.c.

404{
406
407 Status = NtMapUserPhysicalPagesScatter(VirtualAddresses,
408 NumberOfPages,
409 PageArray);
410 if (!NT_SUCCESS(Status))
411 {
413 return FALSE;
414 }
415
416 return TRUE;
417}
NTSTATUS NTAPI NtMapUserPhysicalPagesScatter(IN PVOID *VirtualAddresses, IN ULONG_PTR NumberOfPages, IN OUT PULONG_PTR UserPfnArray)
Definition: procsup.c:1465

◆ ResetWriteWatch()

UINT WINAPI ResetWriteWatch ( IN LPVOID  lpBaseAddress,
IN SIZE_T  dwRegionSize 
)

Definition at line 316 of file virtmem.c.

318{
320
322 lpBaseAddress,
323 dwRegionSize);
324 if (!NT_SUCCESS(Status))
325 {
327 return -1;
328 }
329
330 return 0;
331}
#define NtCurrentProcess()
Definition: nt_native.h:1657
NTSTATUS NTAPI NtResetWriteWatch(IN HANDLE ProcessHandle, IN PVOID BaseAddress, IN SIZE_T RegionSize)
Definition: virtual.c:4328

◆ VirtualAlloc()

LPVOID NTAPI VirtualAlloc ( IN LPVOID  lpAddress,
IN SIZE_T  dwSize,
IN DWORD  flAllocationType,
IN DWORD  flProtect 
)

◆ VirtualAllocEx()

LPVOID NTAPI VirtualAllocEx ( IN HANDLE  hProcess,
IN LPVOID  lpAddress,
IN SIZE_T  dwSize,
IN DWORD  flAllocationType,
IN DWORD  flProtect 
)

Definition at line 23 of file virtmem.c.

28{
30
31 /* Make sure the address is within the granularity of the system (64K) */
32 if ((lpAddress != NULL) &&
34 {
35 /* Fail the call */
37 return NULL;
38 }
39
40 /* Allocate the memory */
42 &lpAddress,
43 0,
44 &dwSize,
45 flAllocationType,
46 flProtect);
47
48 /* Check for status */
49 if (!NT_SUCCESS(Status))
50 {
51 /* We failed */
53 return NULL;
54 }
55
56 /* Return the allocated address */
57 return lpAddress;
58}
#define NULL
Definition: types.h:112
#define ERROR_INVALID_PARAMETER
Definition: compat.h:101
#define SetLastError(x)
Definition: compat.h:752
PBASE_STATIC_SERVER_DATA BaseStaticServerData
Definition: dllmain.c:19
#define UlongToPtr(u)
Definition: config.h:106
NTSTATUS NTAPI NtAllocateVirtualMemory(IN HANDLE ProcessHandle, IN OUT PVOID *UBaseAddress, IN ULONG_PTR ZeroBits, IN OUT PSIZE_T URegionSize, IN ULONG AllocationType, IN ULONG Protect)
Definition: virtual.c:4492
SYSTEM_BASIC_INFORMATION SysInfo
Definition: base.h:130

Referenced by _DoDLLInjection(), test_ExitProcess(), and VirtualAlloc().

◆ VirtualFree()

◆ VirtualFreeEx()

BOOL NTAPI VirtualFreeEx ( IN HANDLE  hProcess,
IN LPVOID  lpAddress,
IN SIZE_T  dwSize,
IN DWORD  dwFreeType 
)

Definition at line 83 of file virtmem.c.

87{
89
90 /* Validate size and flags */
91 if (!(dwSize) || !(dwFreeType & MEM_RELEASE))
92 {
93 /* Free the memory */
95 &lpAddress,
96 &dwSize,
97 dwFreeType);
98 if (!NT_SUCCESS(Status))
99 {
100 /* We failed */
102 return FALSE;
103 }
104
105 /* Return success */
106 return TRUE;
107 }
108
109 /* Invalid combo */
111 return FALSE;
112}
#define MEM_RELEASE
Definition: nt_native.h:1316
NTSTATUS NTAPI NtFreeVirtualMemory(IN HANDLE ProcessHandle, IN PVOID *UBaseAddress, IN PSIZE_T URegionSize, IN ULONG FreeType)
Definition: virtual.c:5230
#define STATUS_INVALID_PARAMETER
Definition: udferr_usr.h:135

Referenced by test_ExitProcess(), and VirtualFree().

◆ VirtualLock()

BOOL NTAPI VirtualLock ( IN LPVOID  lpAddress,
IN SIZE_T  dwSize 
)

Definition at line 183 of file virtmem.c.

185{
188 PVOID BaseAddress = lpAddress;
189
190 /* Lock the memory */
193 &RegionSize,
195 if (!NT_SUCCESS(Status))
196 {
197 /* We failed */
199 return FALSE;
200 }
201
202 /* Return success */
203 return TRUE;
204}
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID * BaseAddress
Definition: mmfuncs.h:404
__kernel_entry _Inout_ _Inout_ PSIZE_T RegionSize
Definition: mmfuncs.h:172
#define MAP_PROCESS
Definition: mmtypes.h:67
NTSTATUS NTAPI NtLockVirtualMemory(IN HANDLE ProcessHandle, IN OUT PVOID *BaseAddress, IN OUT PSIZE_T NumberOfBytesToLock, IN ULONG MapType)
Definition: virtual.c:3529
ULONG_PTR SIZE_T
Definition: typedefs.h:80

Referenced by test_ws_functions().

◆ VirtualProtect()

BOOL NTAPI VirtualProtect ( IN LPVOID  lpAddress,
IN SIZE_T  dwSize,
IN DWORD  flNewProtect,
OUT PDWORD  lpflOldProtect 
)

Definition at line 135 of file virtmem.c.

139{
140 /* Call the extended API */
142 lpAddress,
143 dwSize,
144 flNewProtect,
145 lpflOldProtect);
146}
BOOL NTAPI VirtualProtectEx(IN HANDLE hProcess, IN LPVOID lpAddress, IN SIZE_T dwSize, IN DWORD flNewProtect, OUT PDWORD lpflOldProtect)
Definition: virtmem.c:153

Referenced by __write_memory(), _resetstkoflw(), allocate_block(), hex2mem(), hook_WaitForInputIdle(), main(), RedirectIat(), test__strupr(), test_CompareStringW(), test_CreateFileMapping_protection(), test_mapping(), test_read_write(), test_VirtualAlloc(), test_VirtualAllocEx(), test_VirtualProtect(), test_write_watch(), UnProtect::UnProtect(), wave_in_test_device(), wave_out_test_device(), and UnProtect::~UnProtect().

◆ VirtualProtectEx()

BOOL NTAPI VirtualProtectEx ( IN HANDLE  hProcess,
IN LPVOID  lpAddress,
IN SIZE_T  dwSize,
IN DWORD  flNewProtect,
OUT PDWORD  lpflOldProtect 
)

Definition at line 153 of file virtmem.c.

158{
160
161 /* Change the protection */
163 &lpAddress,
164 &dwSize,
165 flNewProtect,
166 (PULONG)lpflOldProtect);
167 if (!NT_SUCCESS(Status))
168 {
169 /* We failed */
171 return FALSE;
172 }
173
174 /* Return success */
175 return TRUE;
176}
NTSTATUS NTAPI NtProtectVirtualMemory(IN HANDLE ProcessHandle, IN OUT PVOID *UnsafeBaseAddress, IN OUT SIZE_T *UnsafeNumberOfBytesToProtect, IN ULONG NewAccessProtection, OUT PULONG UnsafeOldAccessProtection)
Definition: virtual.c:3111
uint32_t * PULONG
Definition: typedefs.h:59

Referenced by test_ExitProcess(), test_VirtualAllocEx(), and VirtualProtect().

◆ VirtualQuery()

SIZE_T NTAPI VirtualQuery ( IN LPCVOID  lpAddress,
OUT PMEMORY_BASIC_INFORMATION  lpBuffer,
IN SIZE_T  dwLength 
)

Definition at line 211 of file virtmem.c.

214{
215 /* Call the extended API */
217 lpAddress,
218 lpBuffer,
219 dwLength);
220}
static TAGREF LPCWSTR LPDWORD LPVOID lpBuffer
Definition: db.cpp:175
static DWORD DWORD * dwLength
Definition: fusion.c:86
SIZE_T NTAPI VirtualQueryEx(IN HANDLE hProcess, IN LPCVOID lpAddress, OUT PMEMORY_BASIC_INFORMATION lpBuffer, IN SIZE_T dwLength)
Definition: virtmem.c:227

Referenced by __write_memory(), _module_name_from_addr(), create_stream_from_map(), KmtUserCallbackThread(), test_CreateFileMapping_protection(), test_dib_bits_access(), test_dibsections(), test_image_mapping(), test_IME(), test_Loader(), test_mapping(), test_MapViewOfFile(), test_section_access(), test_VirtualAlloc(), test_VirtualAlloc_protection(), test_VirtualProtect(), and test_write_watch().

◆ VirtualQueryEx()

SIZE_T NTAPI VirtualQueryEx ( IN HANDLE  hProcess,
IN LPCVOID  lpAddress,
OUT PMEMORY_BASIC_INFORMATION  lpBuffer,
IN SIZE_T  dwLength 
)

Definition at line 227 of file virtmem.c.

231{
234
235 /* Query basic information */
237 (LPVOID)lpAddress,
239 lpBuffer,
240 dwLength,
241 &ResultLength);
242 if (!NT_SUCCESS(Status))
243 {
244 /* We failed */
246 return 0;
247 }
248
249 /* Return the length returned */
250 return ResultLength;
251}
@ MemoryBasicInformation
Definition: mmtypes.h:183
NTSTATUS NTAPI NtQueryVirtualMemory(IN HANDLE ProcessHandle, IN PVOID BaseAddress, IN MEMORY_INFORMATION_CLASS MemoryInformationClass, OUT PVOID MemoryInformation, IN SIZE_T MemoryInformationLength, OUT PSIZE_T ReturnLength)
Definition: virtual.c:4409
_Must_inspect_result_ _In_ WDFDEVICE _In_ DEVICE_REGISTRY_PROPERTY _In_ ULONG _Out_ PULONG ResultLength
Definition: wdfdevice.h:3776

Referenced by fetch_memory64_info(), get_shiminfo(), test_ExitProcess(), test_OpenProcess(), test_section_access(), test_VirtualAllocEx(), and VirtualQuery().

◆ VirtualUnlock()

BOOL NTAPI VirtualUnlock ( IN LPVOID  lpAddress,
IN SIZE_T  dwSize 
)

Definition at line 258 of file virtmem.c.

260{
263 PVOID BaseAddress = lpAddress;
264
265 /* Lock the memory */
268 &RegionSize,
270 if (!NT_SUCCESS(Status))
271 {
272 /* We failed */
274 return FALSE;
275 }
276
277 /* Return success */
278 return TRUE;
279}
NTSTATUS NTAPI NtUnlockVirtualMemory(IN HANDLE ProcessHandle, IN OUT PVOID *BaseAddress, IN OUT PSIZE_T NumberOfBytesToUnlock, IN ULONG MapType)
Definition: virtual.c:3865