ReactOS 0.4.17-dev-1218-gfae4549
ZwAllocateVirtualMemory.c File Reference
#include <kmt_test.h>
Include dependency graph for ZwAllocateVirtualMemory.c:

Go to the source code of this file.

Classes

struct  _TEST_CONTEXT
 

Macros

#define ROUND_DOWN(n, align)   (((ULONG_PTR)n) & ~((align) - 1l))
 
#define DEFAULT_ALLOC_SIZE   200
 
#define IGNORE   -1
 
#define PAGE_NOPROT   0x0
 
#define ALLOC_MEMORY_WITH_FREE(ProcessHandle, BaseAddress, ZeroBits, RegionSize, AllocationType, Protect, RetStatus, FreeStatus)
 

Typedefs

typedef struct _TEST_CONTEXT TEST_CONTEXT
 
typedef struct _TEST_CONTEXT * PTEST_CONTEXT
 

Functions

VOID Test_ZwAllocateVirtualMemory (VOID)
 
static BOOLEAN CheckBuffer (PVOID Buffer, SIZE_T Size, UCHAR Value)
 
static SIZE_T CheckBufferRead (CONST VOID *Source, CONST VOID *Destination, SIZE_T Length, NTSTATUS ExpectedStatus)
 
static VOID CheckBufferReadWrite (PVOID Destination, CONST VOID *Source, SIZE_T Length, NTSTATUS ExpectedStatus)
 
static VOID SimpleErrorChecks (VOID)
 
static NTSTATUS SimpleAllocation (VOID)
 
static VOID CustomBaseAllocation (VOID)
 
static NTSTATUS StressTesting (ULONG AllocationType)
 
static VOID ZeroBitsTest (VOID)
 
static VOID NTAPI SystemProcessTestWorker (PVOID StartContext)
 
static VOID KmtInitTestContext (PTEST_CONTEXT Ctx, SHORT ThreadId, ULONG RegionSize, ULONG AllocationType, ULONG Protect)
 
static VOID SystemProcessTest (VOID)
 
 START_TEST (ZwAllocateVirtualMemory)
 

Variables

const char TestString [] = "TheLongBrownFoxJumpedTheWhiteRabbitTheLongBrownFoxJumpedTheWhiteRabbitTheLongBrownFoxJumpedTheWhiteRabbitTheLongBrownFoxJumpedTheWhiteRabbitTheLongBrownFoxJumpedTheWhiteRabbitTheLongBrownFoxJumpedThe"
 
const ULONG TestStringSize = sizeof(TestString)
 

Macro Definition Documentation

◆ ALLOC_MEMORY_WITH_FREE

#define ALLOC_MEMORY_WITH_FREE (   ProcessHandle,
  BaseAddress,
  ZeroBits,
  RegionSize,
  AllocationType,
  Protect,
  RetStatus,
  FreeStatus 
)
Value:
do { \
PVOID __BaseSave = BaseAddress; \
ok_eq_hex(Status, RetStatus); \
if (__BaseSave != NULL) \
ok_eq_pointer(BaseAddress, __BaseSave); \
else if (!NT_SUCCESS(Status)) \
ok_eq_pointer(BaseAddress, NULL); \
RegionSize = 0; \
Status = ZwFreeVirtualMemory(ProcessHandle, &BaseAddress, &RegionSize, MEM_RELEASE); \
if (FreeStatus != IGNORE) ok_eq_hex(Status, FreeStatus); \
} while (0) \
#define IGNORE
#define DEFAULT_ALLOC_SIZE
#define ok_eq_hex(value, expected)
Definition: apitest.h:136
#define NULL
Definition: types.h:112
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:33
Status
Definition: gdiplustypes.h:24
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize _Pre_valid_ PVOID _In_ ULONG_PTR ZeroBits
Definition: mmfuncs.h:409
_In_ HANDLE ProcessHandle
Definition: mmfuncs.h:407
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize _Pre_valid_ PVOID _In_ ULONG_PTR _In_ SIZE_T _Inout_opt_ PLARGE_INTEGER _Inout_ PSIZE_T _In_ ULONG AllocationType
Definition: mmfuncs.h:414
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize _Pre_valid_ PVOID * BaseAddress
Definition: mmfuncs.h:408
__kernel_entry _Inout_ _Inout_ PSIZE_T RegionSize
Definition: mmfuncs.h:176
#define MEM_RELEASE
Definition: nt_native.h:1319
static int Protect(const char **args)
Definition: vfdcmd.c:2132

Definition at line 32 of file ZwAllocateVirtualMemory.c.

◆ DEFAULT_ALLOC_SIZE

#define DEFAULT_ALLOC_SIZE   200

Definition at line 11 of file ZwAllocateVirtualMemory.c.

◆ IGNORE

#define IGNORE   -1

Definition at line 12 of file ZwAllocateVirtualMemory.c.

◆ PAGE_NOPROT

#define PAGE_NOPROT   0x0

Definition at line 13 of file ZwAllocateVirtualMemory.c.

◆ ROUND_DOWN

#define ROUND_DOWN (   n,
  align 
)    (((ULONG_PTR)n) & ~((align) - 1l))

Definition at line 10 of file ZwAllocateVirtualMemory.c.

Typedef Documentation

◆ PTEST_CONTEXT

◆ TEST_CONTEXT

Function Documentation

◆ CheckBuffer()

static BOOLEAN CheckBuffer ( PVOID  Buffer,
SIZE_T  Size,
UCHAR  Value 
)
static

Definition at line 52 of file ZwAllocateVirtualMemory.c.

53{
54 PUCHAR Array = Buffer;
55 SIZE_T i;
56
57 for (i = 0; i < Size; i++)
58 {
59 if (Array[i] != Value)
60 {
61 trace("Expected %x, found %x at offset %lu\n", Value, Array[i], (ULONG)i);
62 return FALSE;
63 }
64 }
65 return TRUE;
66}
#define trace
Definition: atltest.h:70
Definition: bufpool.h:45
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
ULONG_PTR SIZE_T
Definition: typedefs.h:80
unsigned char * PUCHAR
Definition: typedefs.h:53
uint32_t ULONG
Definition: typedefs.h:59
_Must_inspect_result_ _In_ WDFDEVICE _In_ PWDF_DEVICE_PROPERTY_DATA _In_ DEVPROPTYPE _In_ ULONG Size
Definition: wdfdevice.h:4539
_Must_inspect_result_ _In_ WDFKEY _In_ PCUNICODE_STRING _Out_opt_ PUSHORT _Inout_opt_ PUNICODE_STRING Value
Definition: wdfregistry.h:413

Referenced by SimpleAllocation().

◆ CheckBufferRead()

static SIZE_T CheckBufferRead ( CONST VOID *  Source,
CONST VOID *  Destination,
SIZE_T  Length,
NTSTATUS  ExpectedStatus 
)
static

Definition at line 70 of file ZwAllocateVirtualMemory.c.

71{
72 SIZE_T Match = 0;
73
76 KmtEndSeh(ExpectedStatus);
77
78 return Match;
79}
return
Definition: dirsup.c:529
#define RtlCompareMemory(s1, s2, l)
Definition: env_spec_w32.h:465
#define KmtStartSeh()
Definition: kmt_test.h:289
#define KmtEndSeh(ExpectedStatus)
Definition: kmt_test.h:295
_In_ UINT _In_ UINT _In_ PNDIS_PACKET Source
Definition: ndis.h:3169
_In_ PUNICODE_STRING _Inout_ PUNICODE_STRING Destination
Definition: rtlfuncs.h:3051
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:102

Referenced by CheckBufferReadWrite(), and SimpleAllocation().

◆ CheckBufferReadWrite()

static VOID CheckBufferReadWrite ( PVOID  Destination,
CONST VOID *  Source,
SIZE_T  Length,
NTSTATUS  ExpectedStatus 
)
static

Definition at line 83 of file ZwAllocateVirtualMemory.c.

84{
85 //do a little bit of writing/reading to memory
86 SIZE_T Match = 0;
87
90 KmtEndSeh(ExpectedStatus);
91
92 Match = CheckBufferRead(Source, Destination, Length, ExpectedStatus);
93 if (ExpectedStatus == STATUS_SUCCESS) ok_eq_int(Match, Length);
94}
static SIZE_T CheckBufferRead(CONST VOID *Source, CONST VOID *Destination, SIZE_T Length, NTSTATUS ExpectedStatus)
#define ok_eq_int(value, expected)
Definition: apitest.h:119
if(dx< 0)
Definition: linetemp.h:194
#define STATUS_SUCCESS
Definition: shellext.h:65
#define RtlCopyMemory(Destination, Source, Length)
Definition: typedefs.h:263

Referenced by SimpleAllocation(), StressTesting(), and SystemProcessTestWorker().

◆ CustomBaseAllocation()

static VOID CustomBaseAllocation ( VOID  )
static

Definition at line 266 of file ZwAllocateVirtualMemory.c.

267{
269 SIZE_T RegionSize = 200;
270 PVOID Base = (PVOID) 0x60025000;
271 PVOID ActualStartingAddress = (PVOID)ROUND_DOWN(Base, MM_ALLOCATION_GRANULARITY); //it is rounded down to the nearest allocation granularity (64k) address
272 PVOID EndingAddress = (PVOID)(((ULONG_PTR)Base + RegionSize - 1) | (PAGE_SIZE - 1));
273 SIZE_T ActualSize = BYTES_TO_PAGES((ULONG_PTR)EndingAddress - (ULONG_PTR)ActualStartingAddress) * PAGE_SIZE; //calculates the actual size based on the required pages
275
276 // allocate the memory
277 Status = ZwAllocateVirtualMemory(NtCurrentProcess(), (PVOID *)&Base, 0, &RegionSize, (MEM_COMMIT | MEM_RESERVE), PAGE_READWRITE);
279 ok_eq_size(RegionSize, ActualSize);
280 ok_eq_ulong(Base, ActualStartingAddress);
281 Test_NtQueryVirtualMemory(ActualStartingAddress, ActualSize, MEM_COMMIT, PAGE_READWRITE);
282
283 // try freeing
284 RegionSize = 0;
285 Status = ZwFreeVirtualMemory(NtCurrentProcess(), (PVOID *)&Base, &RegionSize, MEM_RELEASE);
287 ok_eq_ulong(RegionSize, ActualSize);
288
289 // try to use process memory as base address
291 if (NT_SUCCESS(Status))
292 {
293 PPEB Peb = pbi.PebBaseAddress;
294 Base = NULL;
296
298 Base = Peb->ImageBaseAddress;
300
301 if (Base)
302 {
304 }
305 }
306 else
307 {
309 }
310}
#define ALLOC_MEMORY_WITH_FREE(ProcessHandle, BaseAddress, ZeroBits, RegionSize, AllocationType, Protect, RetStatus, FreeStatus)
#define ROUND_DOWN(n, align)
#define ok_eq_ulong(value, expected)
Definition: apitest.h:122
#define ok_eq_size(value, expected)
Definition: apitest.h:128
LONG NTSTATUS
Definition: precomp.h:26
@ ProcessBasicInformation
Definition: cicbase.cpp:63
PPEB Peb
Definition: dllmain.c:27
#define PAGE_SIZE
Definition: env_spec_w32.h:49
#define Test_NtQueryVirtualMemory(BaseAddress, Size, AllocationType, ProtectionType)
Definition: kmt_test.h:78
#define MM_ALLOCATION_GRANULARITY
Definition: mmtypes.h:36
NTSYSAPI NTSTATUS NTAPI ZwQueryInformationProcess(_In_ HANDLE ProcessHandle, _In_ PROCESSINFOCLASS ProcessInformationClass, _Out_ PVOID ProcessInformation, _In_ ULONG ProcessInformationLength, _Out_opt_ PULONG ReturnLength)
_In_opt_ ULONG Base
Definition: rtlfuncs.h:2486
#define PAGE_READWRITE
Definition: nt_native.h:1307
#define NtCurrentProcess()
Definition: nt_native.h:1660
#define MEM_RESERVE
Definition: nt_native.h:1317
#define MEM_COMMIT
Definition: nt_native.h:1316
#define STATUS_UNABLE_TO_DELETE_SECTION
Definition: ntstatus.h:357
#define STATUS_CONFLICTING_ADDRESSES
Definition: ntstatus.h:354
void * PVOID
Definition: typedefs.h:50
uint32_t ULONG_PTR
Definition: typedefs.h:65
_Inout_ PERBANDINFO * pbi
Definition: winddi.h:3917
#define BYTES_TO_PAGES(Size)

Referenced by START_TEST().

◆ KmtInitTestContext()

static VOID KmtInitTestContext ( PTEST_CONTEXT  Ctx,
SHORT  ThreadId,
ULONG  RegionSize,
ULONG  AllocationType,
ULONG  Protect 
)
static

Definition at line 479 of file ZwAllocateVirtualMemory.c.

480{
481 PAGED_CODE();
482
484 Ctx->Protect = Protect;
485 Ctx->RegionSize = RegionSize;
486 Ctx->ThreadId = ThreadId;
487}
#define PAGED_CODE()

Referenced by SystemProcessTest().

◆ SimpleAllocation()

static NTSTATUS SimpleAllocation ( VOID  )
static

Definition at line 168 of file ZwAllocateVirtualMemory.c.

169{
171 PVOID Base = NULL;
173
175 //Normal operation
177 Status = ZwAllocateVirtualMemory(NtCurrentProcess(), &Base, 0, &RegionSize, MEM_COMMIT, PAGE_READWRITE);
178 ok_eq_size(RegionSize, 4096);
179
180 //check for the zero-filled pages
181 ok_bool_true(CheckBuffer(Base, RegionSize, 0), "The buffer is not zero-filled");
182
184
185 // try freeing
186 RegionSize = 0;
187 Status = ZwFreeVirtualMemory(NtCurrentProcess(), &Base, &RegionSize, MEM_RELEASE);
190
192 // COMMIT AND RESERVE SCENARIO AND STATE CHANGE
194 //reserve and then commit
195 Base = NULL;
197 Status = ZwAllocateVirtualMemory(NtCurrentProcess(), &Base, 0, &RegionSize, MEM_RESERVE, PAGE_READWRITE);
200
201
202 Status = ZwAllocateVirtualMemory(NtCurrentProcess(), &Base, 0, &RegionSize, MEM_COMMIT, PAGE_READWRITE);
205
206 RegionSize = 0;
207 ZwFreeVirtualMemory(NtCurrentProcess(), &Base, &RegionSize, MEM_RELEASE);
208
210 // TRY READING/WRITING TO INVALID PROTECTION PAGES
213 Base = NULL;
214 ZwAllocateVirtualMemory(NtCurrentProcess(), &Base, 0, &RegionSize, (MEM_COMMIT | MEM_RESERVE), PAGE_NOACCESS);
215
219
222
223 RegionSize = 0;
224 ZwFreeVirtualMemory(NtCurrentProcess(), &Base, &RegionSize, MEM_RELEASE);
225
226 ZwAllocateVirtualMemory(NtCurrentProcess(), &Base, 0, &RegionSize, (MEM_COMMIT | MEM_RESERVE), PAGE_READONLY);
230
232
234
235 RegionSize = 0;
236 ZwFreeVirtualMemory(NtCurrentProcess(), &Base, &RegionSize, MEM_RELEASE);
237
239 // GUARD PAGES
241
242 RegionSize = 1000;
243 Base = NULL;
244 ZwAllocateVirtualMemory(NtCurrentProcess(), &Base, 0, &RegionSize, (MEM_COMMIT | MEM_RESERVE), (PAGE_GUARD | PAGE_READWRITE));
245
250
252
256
257 RegionSize = 0;
258 ZwFreeVirtualMemory(NtCurrentProcess(), &Base, &RegionSize, MEM_RELEASE);
259
261}
#define PAGE_NOPROT
static VOID CheckBufferReadWrite(PVOID Destination, CONST VOID *Source, SIZE_T Length, NTSTATUS ExpectedStatus)
const ULONG TestStringSize
static BOOLEAN CheckBuffer(PVOID Buffer, SIZE_T Size, UCHAR Value)
#define read
Definition: acwin.h:97
#define ok_bool_true(value, desc)
Definition: apitest.h:137
#define PAGE_READONLY
Definition: compat.h:138
#define STATUS_ACCESS_VIOLATION
GLdouble GLdouble t
Definition: gl.h:2047
GLuint buffer
Definition: glext.h:5915
GLboolean GLboolean GLboolean GLboolean a
Definition: glext.h:6204
#define PAGE_NOACCESS
Definition: nt_native.h:1305
#define PAGE_GUARD
Definition: nt_native.h:1313
#define STATUS_GUARD_PAGE_VIOLATION
Definition: ntstatus.h:262
EH_STD::basic_string< char, EH_STD::char_traits< char >, eh_allocator(char) > TestString
Definition: test_string.cpp:30

Referenced by START_TEST().

◆ SimpleErrorChecks()

static VOID SimpleErrorChecks ( VOID  )
static

Definition at line 99 of file ZwAllocateVirtualMemory.c.

100{
102 PVOID Base = NULL;
104
105 //HANDLE TESTS
109
110 //BASE ADDRESS TESTS
111 Base = (PVOID) 0x60000000;
113
114 Base = (PVOID)((char *)MmSystemRangeStart + 200);
116
117 /* http://jira.reactos.org/browse/CORE-6814 */
118 RegionSize = 0x1000;
121
122 //ZERO BITS TESTS
126 // -1 is valid on x64, see ZeroBitsTest below
127#ifndef _WIN64
129#endif
130
131 //REGION SIZE TESTS
133 RegionSize = -1;
135 RegionSize = 0;
137
138 //Allocation type tests
153
154 //Memory protection tests
158 {
161 }
163}
#define STATUS_INVALID_HANDLE
Definition: d3dkmdt.h:40
#define STATUS_NO_MEMORY
Definition: d3dkmdt.h:51
BOOLEAN KmtIsCheckedBuild
#define MEM_PHYSICAL
Definition: mmtypes.h:87
#define MEM_TOP_DOWN
Definition: nt_native.h:1324
#define PAGE_WRITECOPY
Definition: nt_native.h:1308
#define MEM_RESET
Definition: nt_native.h:1323
#define PAGE_EXECUTE_WRITECOPY
Definition: nt_native.h:1312
#define MmSystemRangeStart
Definition: mm.h:34
#define STATUS_MEMORY_NOT_ALLOCATED
Definition: ntstatus.h:490
#define STATUS_INVALID_PARAMETER_4
Definition: ntstatus.h:572
#define STATUS_INVALID_PARAMETER_2
Definition: ntstatus.h:570
#define STATUS_INVALID_PAGE_PROTECTION
Definition: ntstatus.h:399
#define STATUS_INVALID_PARAMETER_3
Definition: ntstatus.h:571
#define STATUS_INVALID_PARAMETER_5
Definition: ntstatus.h:573
KMT_TESTFUNC Test_ZwAllocateVirtualMemory
Definition: testlist.c:91
#define PAGE_WRITECOMBINE
Definition: mmtypes.h:78

Referenced by START_TEST().

◆ START_TEST()

START_TEST ( ZwAllocateVirtualMemory  )

Definition at line 572 of file ZwAllocateVirtualMemory.c.

573{
575
577
579
581
583#ifdef _WIN64
585 "Expected STATUS_NO_MEMORY or STATUS_COMMITMENT_LIMIT, got 0x%lx\n", Status);
586#else
588#endif
589
591#ifdef _WIN64
593#else
595#endif
596
597 ZeroBitsTest();
598
600}
static VOID SystemProcessTest(VOID)
static VOID CustomBaseAllocation(VOID)
static VOID ZeroBitsTest(VOID)
static NTSTATUS StressTesting(ULONG AllocationType)
static VOID SimpleErrorChecks(VOID)
static NTSTATUS SimpleAllocation(VOID)
#define ok(value,...)
Definition: atltest.h:57
#define STATUS_COMMITMENT_LIMIT
Definition: ntstatus.h:631

◆ StressTesting()

static NTSTATUS StressTesting ( ULONG  AllocationType)
static

Definition at line 314 of file ZwAllocateVirtualMemory.c.

315{
317 NTSTATUS ReturnStatus = STATUS_SUCCESS;
318 ULONG Index = 0;
319 PVOID Base = NULL;
320 PVOID *bases = NULL;
321 ULONG NumberOfElements;
323
324#ifdef _WIN64
325 // 256 TB total, large chunks keep the VAD count low
326 NumberOfElements = 4096;
327 RegionSize = 64ULL * 1024 * 1024 * 1024;
328#else
329 // 5 GB total
330 NumberOfElements = 1024;
331 RegionSize = 5 * 1024 * 1024;
332#endif
333
334 bases = ExAllocatePoolZero(PagedPool, NumberOfElements * sizeof(*bases), 'stts');
335 if (!bases)
336 {
337 trace("Failed to allocate memory for bases array\n");
339 }
340
341 for (Index = 0; Index < NumberOfElements && NT_SUCCESS(Status); Index++)
342 {
343 Status = ZwAllocateVirtualMemory(NtCurrentProcess(), &Base, 0, &RegionSize, AllocationType, PAGE_READWRITE);
344
345 bases[Index] = Base;
346 if ((Index % 10) == 0)
347 {
349 {
351 }
352 else
353 {
355 }
356 }
357
358 Base = NULL;
359 }
360
361 trace("Finished reserving. Error code %x. Chunks allocated: %d\n", Status, Index );
362
363 ReturnStatus = Status;
364
365 //free the allocated memory so that we can continue with the tests
367 Index = 0;
368 while (NT_SUCCESS(Status) && Index < NumberOfElements)
369 {
370 RegionSize = 0;
371 Status = ZwFreeVirtualMemory(NtCurrentProcess(), &bases[Index], &RegionSize, MEM_RELEASE);
372 bases[Index++] = NULL;
373 }
374
375 ExFreePoolWithTag(bases, 'stts');
376 return ReturnStatus;
377}
#define PagedPool
Definition: env_spec_w32.h:308
#define ExFreePoolWithTag(_P, _T)
Definition: module.h:1109
static PVOID ExAllocatePoolZero(ULONG PoolType, SIZE_T NumberOfBytes, ULONG Tag)
Definition: precomp.h:45
#define STATUS_INSUFFICIENT_RESOURCES
Definition: udferr_usr.h:158
_In_ WDFCOLLECTION _In_ ULONG Index

Referenced by START_TEST().

◆ SystemProcessTest()

static VOID SystemProcessTest ( VOID  )
static

Definition at line 492 of file ZwAllocateVirtualMemory.c.

493{
497 PVOID ThreadObjects[2] = { NULL };
499 PTEST_CONTEXT StartContext1;
500 PTEST_CONTEXT StartContext2;
501
502 PAGED_CODE();
503
504 StartContext1 = ExAllocatePoolWithTag(PagedPool, sizeof(TEST_CONTEXT), 'tXTC');
505 StartContext2 = ExAllocatePoolWithTag(PagedPool, sizeof(TEST_CONTEXT), 'tXTC');
506 if (StartContext1 == NULL || StartContext2 == NULL)
507 {
508 trace("Error allocating space for context structs\n");
509 goto cleanup;
510 }
511
512 KmtInitTestContext(StartContext1, 1, 1 * 1024 * 1024, MEM_COMMIT, PAGE_READWRITE);
513 KmtInitTestContext(StartContext2, 2, 3 * 1024 * 1024, MEM_COMMIT, PAGE_READWRITE);
515
517 if (!NT_SUCCESS(Status))
518 {
519 trace("Error creating thread1\n");
520 goto cleanup;
521 }
522
524 if (!NT_SUCCESS(Status))
525 {
526 trace("error referencing thread1\n");
527 goto cleanup;
528 }
529
531 if (!NT_SUCCESS(Status))
532 {
533 trace("Error creating thread2\n");
534 goto cleanup;
535 }
536
538 if (!NT_SUCCESS(Status))
539 {
540 trace("error referencing thread2\n");
541 goto cleanup;
542 }
543
544cleanup:
545
546 if (ThreadObjects[0])
548
549 if (StartContext1 != NULL)
550 ExFreePoolWithTag(StartContext1, 'tXTC');
551
552 if (ThreadObjects[1])
554
555 if (StartContext2 != NULL)
556 ExFreePoolWithTag(StartContext2, 'tXTC');
557
558 if (ThreadObjects[0] != NULL)
559 ObDereferenceObject(ThreadObjects[0]);
560
561 if (ThreadObjects[1] != NULL)
562 ObDereferenceObject(ThreadObjects[1]);
563
566
569}
static DWORD WINAPI Thread2(_Inout_opt_ PVOID Parameter)
static DWORD WINAPI Thread1(_Inout_opt_ PVOID Parameter)
static VOID KmtInitTestContext(PTEST_CONTEXT Ctx, SHORT ThreadId, ULONG RegionSize, ULONG AllocationType, ULONG Protect)
static VOID NTAPI SystemProcessTestWorker(PVOID StartContext)
IN PUNICODE_STRING IN POBJECT_ATTRIBUTES ObjectAttributes
Definition: conport.c:36
#define INVALID_HANDLE_VALUE
Definition: compat.h:731
static void cleanup(void)
Definition: main.c:1335
#define ExAllocatePoolWithTag(hernya, size, tag)
Definition: env_spec_w32.h:350
#define KeWaitForSingleObject(pEvt, foo, a, b, c)
Definition: env_spec_w32.h:478
#define InitializeObjectAttributes(p, n, a, r, s)
Definition: reg.c:115
#define KernelMode
Definition: asm.h:38
NTSYSAPI NTSTATUS NTAPI ZwClose(_In_ HANDLE Handle)
#define THREAD_ALL_ACCESS
Definition: nt_native.h:1342
POBJECT_TYPE PsThreadType
Definition: thread.c:20
NTSTATUS NTAPI PsCreateSystemThread(OUT PHANDLE ThreadHandle, IN ACCESS_MASK DesiredAccess, IN POBJECT_ATTRIBUTES ObjectAttributes, IN HANDLE ProcessHandle, IN PCLIENT_ID ClientId, IN PKSTART_ROUTINE StartRoutine, IN PVOID StartContext)
Definition: thread.c:626
NTSTATUS NTAPI ObReferenceObjectByHandle(IN HANDLE Handle, IN ACCESS_MASK DesiredAccess, IN POBJECT_TYPE ObjectType, IN KPROCESSOR_MODE AccessMode, OUT PVOID *Object, OUT POBJECT_HANDLE_INFORMATION HandleInformation OPTIONAL)
Definition: obref.c:493
#define OBJ_KERNEL_HANDLE
Definition: winternl.h:231
@ Executive
Definition: ketypes.h:467
#define ObDereferenceObject
Definition: obfuncs.h:203

Referenced by START_TEST().

◆ SystemProcessTestWorker()

static VOID NTAPI SystemProcessTestWorker ( PVOID  StartContext)
static

Definition at line 424 of file ZwAllocateVirtualMemory.c.

425{
427 PTEST_CONTEXT Context = (PTEST_CONTEXT)StartContext;
428 ULONG Index = 0;
429 PVOID Base = NULL;
430
431 PAGED_CODE();
432
433 RtlZeroMemory(Context->Bases, sizeof(Context->Bases));
434
435 Status = ZwAllocateVirtualMemory(NtCurrentProcess(), &Base, 0, &Context->RegionSize, Context->AllocationType, Context->Protect);
436 ZwFreeVirtualMemory(NtCurrentProcess(), &Base, &Context->RegionSize, MEM_RELEASE);
437 Base = NULL;
438
439 //if the previous allocation has failed there is no need to do the loop
440 while (NT_SUCCESS(Status) && Index < RTL_NUMBER_OF(Context->Bases))
441 {
442 Status = ZwAllocateVirtualMemory(NtCurrentProcess(), &Base, 0, &Context->RegionSize, Context->AllocationType, Context->Protect);
443
444 Context->Bases[Index] = Base;
445 if ((Index % 10) == 0)
446 {
447 if (Context->AllocationType == MEM_COMMIT)
448 {
450 }
451 else
452 {
454 }
455 }
456
457 Base = NULL;
458 Index++;
459 }
460
461 trace("[SYSTEM THREAD %d]. Error code %x. Chunks allocated: %d\n", Context->ThreadId, Status, Index);
462
463 //free the allocated memory so that we can continue with the tests
465 Index = 0;
466 while (NT_SUCCESS(Status) && Index < RTL_NUMBER_OF(Context->Bases))
467 {
468 Context->RegionSize = 0;
469 Status = ZwFreeVirtualMemory(NtCurrentProcess(), &Context->Bases[Index], &Context->RegionSize, MEM_RELEASE);
470 Context->Bases[Index++] = NULL;
471 }
472
474}
#define RTL_NUMBER_OF(x)
Definition: RtlRegistry.c:12
struct _TEST_CONTEXT * PTEST_CONTEXT
_In_ PIRP _In_ PIO_COMPLETION_ROUTINE _In_ PVOID Context
Definition: kse.h:81
NTSTATUS NTAPI PsTerminateSystemThread(IN NTSTATUS ExitStatus)
Definition: kill.c:1181
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:262

Referenced by SystemProcessTest().

◆ Test_ZwAllocateVirtualMemory()

VOID Test_ZwAllocateVirtualMemory ( VOID  )

◆ ZeroBitsTest()

static VOID ZeroBitsTest ( VOID  )
static

Definition at line 381 of file ZwAllocateVirtualMemory.c.

382{
384 PVOID Base = NULL;
385 SIZE_T RegionSize = 1;
386 ULONG_PTR MaxPtr = -1;
387
388 /* https://stackoverflow.com/a/50553691 */
389#ifdef _WIN64
391 {
392 for (ULONG_PTR ZeroBits = MaxPtr; Status == STATUS_SUCCESS && ZeroBits > 32; ZeroBits >>= 1)
393 {
395 trace("%p:%p - Status: %x\n", ZeroBits, Base, Status);
396 if ((ULONG_PTR)Base > ZeroBits)
397 {
398 ok_eq_pointer((ULONG_PTR)Base, (ZeroBits >> 16) << 16);
399 }
400 ZwFreeVirtualMemory(NtCurrentProcess(), &Base, &RegionSize, MEM_RELEASE);
401 Base = NULL;
402 RegionSize = 1;
403 }
404 ok(Status == STATUS_SUCCESS || Status == STATUS_NO_MEMORY, "ZeroBits mask test failed\n");
405 }
406#endif
407
409 for (ULONG_PTR ZeroBits = 0; Status == STATUS_SUCCESS && ZeroBits <= 32; ZeroBits += 1)
410 {
412 ok_bool_true((ULONG_PTR)Base < (MaxPtr >> ZeroBits), "Base must be < (MaxPtr >> ZeroBits)\n");
413 ZwFreeVirtualMemory(NtCurrentProcess(), &Base, &RegionSize, MEM_RELEASE);
414 Base = NULL;
415 RegionSize = 1;
416 }
417 ok(Status == STATUS_SUCCESS || Status == STATUS_NO_MEMORY, "ZeroBits test failed\n");
418}
#define ok_eq_pointer(value, expected)
Definition: apitest.h:118
#define GetNTVersion()
Definition: apitest.h:17
#define _WIN32_WINNT_WINBLUE
Definition: sdkddkver.h:30

Referenced by START_TEST().

Variable Documentation

◆ TestString

const char TestString[] = "TheLongBrownFoxJumpedTheWhiteRabbitTheLongBrownFoxJumpedTheWhiteRabbitTheLongBrownFoxJumpedTheWhiteRabbitTheLongBrownFoxJumpedTheWhiteRabbitTheLongBrownFoxJumpedTheWhiteRabbitTheLongBrownFoxJumpedThe"

Definition at line 16 of file ZwAllocateVirtualMemory.c.

◆ TestStringSize