ReactOS  0.4.14-dev-317-g96040ec
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_CONTEXTPTEST_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 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; \
Status = ZwAllocateVirtualMemory(ProcessHandle, &BaseAddress, ZeroBits, &RegionSize, AllocationType, Protect); \
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); \
BaseAddress = NULL; \
RegionSize = DEFAULT_ALLOC_SIZE; \
} while (0) \
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID _In_ ULONG_PTR ZeroBits
Definition: mmfuncs.h:404
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID _In_ ULONG_PTR _In_ SIZE_T _Inout_opt_ PLARGE_INTEGER _Inout_ PSIZE_T _In_ SECTION_INHERIT _In_ ULONG _In_ ULONG Protect
Definition: zwfuncs.h:214
smooth NULL
Definition: ftsmooth.c:416
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID * BaseAddress
Definition: mmfuncs.h:404
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define DEFAULT_ALLOC_SIZE
#define IGNORE
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID _In_ ULONG_PTR _In_ SIZE_T _Inout_opt_ PLARGE_INTEGER _Inout_ PSIZE_T _In_ SECTION_INHERIT _In_ ULONG AllocationType
Definition: mmfuncs.h:404
Status
Definition: gdiplustypes.h:24
__kernel_entry _Inout_ _Inout_ PSIZE_T RegionSize
Definition: mmfuncs.h:172
#define MEM_RELEASE
Definition: nt_native.h:1316
#define ok_eq_hex(value, expected)
_In_ HANDLE ProcessHandle
Definition: mmfuncs.h:403

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 }
_In_opt_ ULONG _Out_ PULONG Value
Definition: rtlfuncs.h:2343
#define TRUE
Definition: types.h:120
unsigned char * PUCHAR
Definition: retypes.h:3
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
#define trace
Definition: atltest.h:70
IN PVOID IN PVOID IN USHORT IN USHORT Size
Definition: pci.h:359
ULONG_PTR SIZE_T
Definition: typedefs.h:78
unsigned int ULONG
Definition: retypes.h:1
IN BOOLEAN OUT PSTR Buffer
Definition: progress.h:34

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 
74  KmtStartSeh()
76  KmtEndSeh(ExpectedStatus);
77 
78  return Match;
79 }
return
Definition: dirsup.c:529
#define KmtEndSeh(ExpectedStatus)
Definition: kmt_test.h:283
_In_ PUNICODE_STRING _Inout_ PUNICODE_STRING Destination
Definition: rtlfuncs.h:2891
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:101
#define KmtStartSeh()
Definition: kmt_test.h:277
ULONG_PTR SIZE_T
Definition: typedefs.h:78
_In_ UINT _In_ UINT _In_ PNDIS_PACKET Source
Definition: ndis.h:3167
#define RtlCompareMemory(s1, s2, l)
Definition: env_spec_w32.h:465

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 
88  KmtStartSeh()
90  KmtEndSeh(ExpectedStatus);
91 
92  Match = CheckBufferRead(Source, Destination, Length, ExpectedStatus);
93  if (ExpectedStatus == STATUS_SUCCESS) ok_eq_int(Match, Length);
94 }
NTSYSAPI VOID NTAPI RtlCopyMemory(VOID UNALIGNED *Destination, CONST VOID UNALIGNED *Source, ULONG Length)
#define ok_eq_int(value, expected)
Definition: kmt_test.h:238
#define KmtEndSeh(ExpectedStatus)
Definition: kmt_test.h:283
if(!(yy_init))
Definition: macro.lex.yy.c:714
_In_ PUNICODE_STRING _Inout_ PUNICODE_STRING Destination
Definition: rtlfuncs.h:2891
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:101
static SIZE_T CheckBufferRead(CONST VOID *Source, CONST VOID *Destination, SIZE_T Length, NTSTATUS ExpectedStatus)
#define KmtStartSeh()
Definition: kmt_test.h:277
ULONG_PTR SIZE_T
Definition: typedefs.h:78
_In_ UINT _In_ UINT _In_ PNDIS_PACKET Source
Definition: ndis.h:3167
return STATUS_SUCCESS
Definition: btrfs.c:2938

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

◆ CustomBaseAllocation()

static VOID CustomBaseAllocation ( VOID  )
static

Definition at line 268 of file ZwAllocateVirtualMemory.c.

269 {
271  SIZE_T RegionSize = 200;
272  PVOID Base = (PVOID) 0x60025000;
273  PVOID ActualStartingAddress = (PVOID)ROUND_DOWN(Base, MM_ALLOCATION_GRANULARITY); //it is rounded down to the nearest allocation granularity (64k) address
274  PVOID EndingAddress = (PVOID)(((ULONG_PTR)Base + RegionSize - 1) | (PAGE_SIZE - 1));
275  SIZE_T ActualSize = BYTES_TO_PAGES((ULONG_PTR)EndingAddress - (ULONG_PTR)ActualStartingAddress) * PAGE_SIZE; //calculates the actual size based on the required pages
276 
277  // allocate the memory
278  Status = ZwAllocateVirtualMemory(NtCurrentProcess(), (PVOID *)&Base, 0, &RegionSize, (MEM_COMMIT | MEM_RESERVE), PAGE_READWRITE);
280  ok_eq_size(RegionSize, ActualSize);
281  ok_eq_ulong(Base, ActualStartingAddress);
282  Test_NtQueryVirtualMemory(ActualStartingAddress, ActualSize, MEM_COMMIT, PAGE_READWRITE);
283 
284  // try freeing
285  RegionSize = 0;
286  Status = ZwFreeVirtualMemory(NtCurrentProcess(), (PVOID *)&Base, &RegionSize, MEM_RELEASE);
288  ok_eq_ulong(RegionSize, ActualSize);
289 }
#define ok_eq_ulong(value, expected)
#define ok_eq_size(value, expected)
Definition: kmt_test.h:247
LONG NTSTATUS
Definition: precomp.h:26
_In_opt_ ULONG Base
Definition: rtlfuncs.h:2343
#define MEM_COMMIT
Definition: nt_native.h:1313
uint32_t ULONG_PTR
Definition: typedefs.h:63
#define MEM_RESERVE
Definition: nt_native.h:1314
#define Test_NtQueryVirtualMemory(BaseAddress, Size, AllocationType, ProtectionType)
Definition: kmt_test.h:72
void * PVOID
Definition: retypes.h:9
#define NtCurrentProcess()
Definition: nt_native.h:1657
#define BYTES_TO_PAGES(Size)
#define PAGE_SIZE
Definition: env_spec_w32.h:49
Status
Definition: gdiplustypes.h:24
ULONG_PTR SIZE_T
Definition: typedefs.h:78
__kernel_entry _Inout_ _Inout_ PSIZE_T RegionSize
Definition: mmfuncs.h:172
#define MEM_RELEASE
Definition: nt_native.h:1316
#define ok_eq_hex(value, expected)
return STATUS_SUCCESS
Definition: btrfs.c:2938
#define ROUND_DOWN(n, align)
#define MM_ALLOCATION_GRANULARITY
Definition: mmtypes.h:36
#define PAGE_READWRITE
Definition: nt_native.h:1304

Referenced by START_TEST().

◆ KmtInitTestContext()

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

Definition at line 401 of file ZwAllocateVirtualMemory.c.

402 {
403  PAGED_CODE();
404 
406  Ctx->Protect = Protect;
407  Ctx->RegionSize = RegionSize;
408  Ctx->ThreadId = ThreadId;
409 }
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID _In_ ULONG_PTR _In_ SIZE_T _Inout_opt_ PLARGE_INTEGER _Inout_ PSIZE_T _In_ SECTION_INHERIT _In_ ULONG _In_ ULONG Protect
Definition: zwfuncs.h:214
#define PAGED_CODE()
Definition: video.h:57
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID _In_ ULONG_PTR _In_ SIZE_T _Inout_opt_ PLARGE_INTEGER _Inout_ PSIZE_T _In_ SECTION_INHERIT _In_ ULONG AllocationType
Definition: mmfuncs.h:404
__kernel_entry _Inout_ _Inout_ PSIZE_T RegionSize
Definition: mmfuncs.h:172

Referenced by SystemProcessTest().

◆ SimpleAllocation()

static NTSTATUS SimpleAllocation ( VOID  )
static

Definition at line 170 of file ZwAllocateVirtualMemory.c.

171 {
173  PVOID Base = NULL;
175 
177  //Normal operation
179  Status = ZwAllocateVirtualMemory(NtCurrentProcess(), &Base, 0, &RegionSize, MEM_COMMIT, PAGE_READWRITE);
180  ok_eq_size(RegionSize, 4096);
181 
182  //check for the zero-filled pages
183  ok_bool_true(CheckBuffer(Base, RegionSize, 0), "The buffer is not zero-filled");
184 
186 
187  // try freeing
188  RegionSize = 0;
189  Status = ZwFreeVirtualMemory(NtCurrentProcess(), &Base, &RegionSize, MEM_RELEASE);
192 
194  // COMMIT AND RESERVE SCENARIO AND STATE CHANGE
196  //reserve and then commit
197  Base = NULL;
199  Status = ZwAllocateVirtualMemory(NtCurrentProcess(), &Base, 0, &RegionSize, MEM_RESERVE, PAGE_READWRITE);
202 
203 
204  Status = ZwAllocateVirtualMemory(NtCurrentProcess(), &Base, 0, &RegionSize, MEM_COMMIT, PAGE_READWRITE);
207 
208  RegionSize = 0;
209  ZwFreeVirtualMemory(NtCurrentProcess(), &Base, &RegionSize, MEM_RELEASE);
210 
212  // TRY READING/WRITING TO INVALID PROTECTION PAGES
215  Base = NULL;
216  ZwAllocateVirtualMemory(NtCurrentProcess(), &Base, 0, &RegionSize, (MEM_COMMIT | MEM_RESERVE), PAGE_NOACCESS);
217 
218  KmtStartSeh()
221 
224 
225  RegionSize = 0;
226  ZwFreeVirtualMemory(NtCurrentProcess(), &Base, &RegionSize, MEM_RELEASE);
227 
228  ZwAllocateVirtualMemory(NtCurrentProcess(), &Base, 0, &RegionSize, (MEM_COMMIT | MEM_RESERVE), PAGE_READONLY);
229  KmtStartSeh()
232 
234 
236 
237  RegionSize = 0;
238  ZwFreeVirtualMemory(NtCurrentProcess(), &Base, &RegionSize, MEM_RELEASE);
239 
241  // GUARD PAGES
243 
244  RegionSize = 1000;
245  Base = NULL;
246  ZwAllocateVirtualMemory(NtCurrentProcess(), &Base, 0, &RegionSize, (MEM_COMMIT | MEM_RESERVE), (PAGE_GUARD | PAGE_READWRITE));
247 
249  KmtStartSeh()
252 
254 
255  KmtStartSeh()
258 
259  RegionSize = 0;
260  ZwFreeVirtualMemory(NtCurrentProcess(), &Base, &RegionSize, MEM_RELEASE);
261 
262  return Status;
263 }
return
Definition: dirsup.c:529
NTSYSAPI VOID NTAPI RtlCopyMemory(VOID UNALIGNED *Destination, CONST VOID UNALIGNED *Source, ULONG Length)
EH_STD::basic_string< char, EH_STD::char_traits< char >, eh_allocator(char) > TestString
Definition: test_string.cpp:30
#define ok_eq_size(value, expected)
Definition: kmt_test.h:247
#define PAGE_GUARD
Definition: nt_native.h:1310
static VOID CheckBufferReadWrite(PVOID Destination, CONST VOID *Source, SIZE_T Length, NTSTATUS ExpectedStatus)
LONG NTSTATUS
Definition: precomp.h:26
_In_opt_ ULONG Base
Definition: rtlfuncs.h:2343
#define KmtEndSeh(ExpectedStatus)
Definition: kmt_test.h:283
#define STATUS_GUARD_PAGE_VIOLATION
Definition: ntstatus.h:170
GLdouble GLdouble t
Definition: gl.h:2047
GLuint buffer
Definition: glext.h:5915
#define MEM_COMMIT
Definition: nt_native.h:1313
#define ok_bool_true(value, desc)
Definition: kmt_test.h:256
#define MEM_RESERVE
Definition: nt_native.h:1314
#define PAGE_NOACCESS
Definition: nt_native.h:1302
#define Test_NtQueryVirtualMemory(BaseAddress, Size, AllocationType, ProtectionType)
Definition: kmt_test.h:72
smooth NULL
Definition: ftsmooth.c:416
#define NtCurrentProcess()
Definition: nt_native.h:1657
#define PAGE_NOPROT
static SIZE_T CheckBufferRead(CONST VOID *Source, CONST VOID *Destination, SIZE_T Length, NTSTATUS ExpectedStatus)
#define DEFAULT_ALLOC_SIZE
#define PAGE_SIZE
Definition: env_spec_w32.h:49
#define KmtStartSeh()
Definition: kmt_test.h:277
Status
Definition: gdiplustypes.h:24
const ULONG TestStringSize
ULONG_PTR SIZE_T
Definition: typedefs.h:78
#define STATUS_ACCESS_VIOLATION
Definition: ntstatus.h:228
__kernel_entry _Inout_ _Inout_ PSIZE_T RegionSize
Definition: mmfuncs.h:172
#define PAGE_READONLY
Definition: compat.h:127
#define MEM_RELEASE
Definition: nt_native.h:1316
static BOOLEAN CheckBuffer(PVOID Buffer, SIZE_T Size, UCHAR Value)
#define ok_eq_hex(value, expected)
GLboolean GLboolean GLboolean GLboolean a
Definition: glext.h:6204
return STATUS_SUCCESS
Definition: btrfs.c:2938
_CRTIMP int __cdecl read(_In_ int _FileHandle, _Out_writes_bytes_(_MaxCharCount) void *_DstBuf, _In_ unsigned int _MaxCharCount)
#define PAGE_READWRITE
Definition: nt_native.h:1304

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)0x00567A20;
113 
114  Base = (PVOID) 0x60000000;
116 
117  Base = (PVOID)((char *)MmSystemRangeStart + 200);
119 
120  /* http://jira.reactos.org/browse/CORE-6814 */
121  RegionSize = 0x1000;
124 
125  //ZERO BITS TESTS
130 
131  //REGION SIZE TESTS
133  RegionSize = -1;
135  RegionSize = 0;
137  RegionSize = 0xFFFFFFFF; // 4 gb is invalid
139 
140  //Allocation type tests
155 
156  //Memory protection tests
159  if (!KmtIsCheckedBuild)
160  {
163  }
165 }
VOID Test_ZwAllocateVirtualMemory(VOID)
#define PAGE_GUARD
Definition: nt_native.h:1310
LONG NTSTATUS
Definition: precomp.h:26
_In_opt_ ULONG Base
Definition: rtlfuncs.h:2343
#define ALLOC_MEMORY_WITH_FREE(ProcessHandle, BaseAddress, ZeroBits, RegionSize, AllocationType, Protect, RetStatus, FreeStatus)
#define MEM_COMMIT
Definition: nt_native.h:1313
#define STATUS_INVALID_HANDLE
Definition: ntstatus.h:231
uint32_t ULONG_PTR
Definition: typedefs.h:63
BOOLEAN KmtIsCheckedBuild
#define PAGE_EXECUTE_WRITECOPY
Definition: nt_native.h:1309
#define STATUS_INVALID_PARAMETER_3
Definition: ntstatus.h:463
#define MEM_RESERVE
Definition: nt_native.h:1314
#define PAGE_NOACCESS
Definition: nt_native.h:1302
smooth NULL
Definition: ftsmooth.c:416
#define STATUS_INVALID_PARAMETER_2
Definition: ntstatus.h:462
void * PVOID
Definition: retypes.h:9
#define NtCurrentProcess()
Definition: nt_native.h:1657
#define STATUS_INVALID_PARAMETER_5
Definition: ntstatus.h:465
#define MEM_RESET
Definition: nt_native.h:1320
#define MEM_PHYSICAL
Definition: mmtypes.h:86
#define DEFAULT_ALLOC_SIZE
#define STATUS_INVALID_PAGE_PROTECTION
Definition: ntstatus.h:291
Status
Definition: gdiplustypes.h:24
ULONG_PTR SIZE_T
Definition: typedefs.h:78
#define PAGE_WRITECOMBINE
Definition: mmtypes.h:78
#define STATUS_MEMORY_NOT_ALLOCATED
Definition: ntstatus.h:382
__kernel_entry _Inout_ _Inout_ PSIZE_T RegionSize
Definition: mmfuncs.h:172
#define STATUS_NO_MEMORY
Definition: ntstatus.h:246
#define PAGE_READONLY
Definition: compat.h:127
#define PAGE_WRITECOPY
Definition: nt_native.h:1305
#define STATUS_INVALID_PARAMETER_4
Definition: ntstatus.h:464
#define MEM_TOP_DOWN
Definition: nt_native.h:1321
#define STATUS_CONFLICTING_ADDRESSES
Definition: ntstatus.h:247
return STATUS_SUCCESS
Definition: btrfs.c:2938
#define MmSystemRangeStart
Definition: mm.h:32
#define STATUS_UNABLE_TO_DELETE_SECTION
Definition: ntstatus.h:250
#define PAGE_READWRITE
Definition: nt_native.h:1304

Referenced by START_TEST().

◆ START_TEST()

START_TEST ( ZwAllocateVirtualMemory  )

Definition at line 494 of file ZwAllocateVirtualMemory.c.

495 {
497 
499 
501 
503 
506 
510 
512 }
static NTSTATUS StressTesting(ULONG AllocationType)
static VOID SimpleErrorChecks(VOID)
LONG NTSTATUS
Definition: precomp.h:26
#define MEM_COMMIT
Definition: nt_native.h:1313
#define MEM_RESERVE
Definition: nt_native.h:1314
static VOID SystemProcessTest(VOID)
static NTSTATUS SimpleAllocation(VOID)
Status
Definition: gdiplustypes.h:24
static VOID CustomBaseAllocation(VOID)
#define STATUS_NO_MEMORY
Definition: ntstatus.h:246
#define ok_eq_hex(value, expected)
return STATUS_SUCCESS
Definition: btrfs.c:2938

◆ StressTesting()

static NTSTATUS StressTesting ( ULONG  AllocationType)
static

Definition at line 294 of file ZwAllocateVirtualMemory.c.

295 {
297  NTSTATUS ReturnStatus = STATUS_SUCCESS;
298  static PVOID bases[1024]; //assume we are going to allocate only 5 gigs. static here means the arrays is not allocated on the stack but in the BSS segment of the driver
299  ULONG Index = 0;
300  PVOID Base = NULL;
301  SIZE_T RegionSize = 5 * 1024 * 1024; // 5 megabytes;
302 
303  RtlZeroMemory(bases, sizeof(bases));
304 
305  for (Index = 0; Index < RTL_NUMBER_OF(bases) && NT_SUCCESS(Status); Index++)
306  {
307  Status = ZwAllocateVirtualMemory(NtCurrentProcess(), &Base, 0, &RegionSize, AllocationType, PAGE_READWRITE);
308 
309  bases[Index] = Base;
310  if ((Index % 10) == 0)
311  {
313  {
315  }
316  else
317  {
319  }
320  }
321 
322  Base = NULL;
323  }
324 
325  trace("Finished reserving. Error code %x. Chunks allocated: %d\n", Status, Index );
326 
327  ReturnStatus = Status;
328 
329  //free the allocated memory so that we can continue with the tests
331  Index = 0;
332  while (NT_SUCCESS(Status) && Index < RTL_NUMBER_OF(bases))
333  {
334  RegionSize = 0;
335  Status = ZwFreeVirtualMemory(NtCurrentProcess(), &bases[Index], &RegionSize, MEM_RELEASE);
336  bases[Index++] = NULL;
337  }
338 
339  return ReturnStatus;
340 }
EH_STD::basic_string< char, EH_STD::char_traits< char >, eh_allocator(char) > TestString
Definition: test_string.cpp:30
static VOID CheckBufferReadWrite(PVOID Destination, CONST VOID *Source, SIZE_T Length, NTSTATUS ExpectedStatus)
LONG NTSTATUS
Definition: precomp.h:26
_In_opt_ ULONG Base
Definition: rtlfuncs.h:2343
#define MEM_COMMIT
Definition: nt_native.h:1313
smooth NULL
Definition: ftsmooth.c:416
#define NtCurrentProcess()
Definition: nt_native.h:1657
#define trace
Definition: atltest.h:70
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
static const UCHAR Index[8]
Definition: usbohci.c:18
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID _In_ ULONG_PTR _In_ SIZE_T _Inout_opt_ PLARGE_INTEGER _Inout_ PSIZE_T _In_ SECTION_INHERIT _In_ ULONG AllocationType
Definition: mmfuncs.h:404
Status
Definition: gdiplustypes.h:24
const ULONG TestStringSize
ULONG_PTR SIZE_T
Definition: typedefs.h:78
#define STATUS_ACCESS_VIOLATION
Definition: ntstatus.h:228
__kernel_entry _Inout_ _Inout_ PSIZE_T RegionSize
Definition: mmfuncs.h:172
#define RTL_NUMBER_OF(x)
Definition: RtlRegistry.c:12
#define MEM_RELEASE
Definition: nt_native.h:1316
unsigned int ULONG
Definition: retypes.h:1
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:261
return STATUS_SUCCESS
Definition: btrfs.c:2938
#define PAGE_READWRITE
Definition: nt_native.h:1304

Referenced by START_TEST().

◆ SystemProcessTest()

static VOID SystemProcessTest ( VOID  )
static

Definition at line 414 of file ZwAllocateVirtualMemory.c.

415 {
419  PVOID ThreadObjects[2] = { NULL };
421  PTEST_CONTEXT StartContext1;
422  PTEST_CONTEXT StartContext2;
423 
424  PAGED_CODE();
425 
426  StartContext1 = ExAllocatePoolWithTag(PagedPool, sizeof(TEST_CONTEXT), 'tXTC');
427  StartContext2 = ExAllocatePoolWithTag(PagedPool, sizeof(TEST_CONTEXT), 'tXTC');
428  if (StartContext1 == NULL || StartContext2 == NULL)
429  {
430  trace("Error allocating space for context structs\n");
431  goto cleanup;
432  }
433 
434  KmtInitTestContext(StartContext1, 1, 1 * 1024 * 1024, MEM_COMMIT, PAGE_READWRITE);
435  KmtInitTestContext(StartContext2, 2, 3 * 1024 * 1024, MEM_COMMIT, PAGE_READWRITE);
437 
439  if (!NT_SUCCESS(Status))
440  {
441  trace("Error creating thread1\n");
442  goto cleanup;
443  }
444 
446  if (!NT_SUCCESS(Status))
447  {
448  trace("error referencing thread1\n");
449  goto cleanup;
450  }
451 
453  if (!NT_SUCCESS(Status))
454  {
455  trace("Error creating thread2\n");
456  goto cleanup;
457  }
458 
460  if (!NT_SUCCESS(Status))
461  {
462  trace("error referencing thread2\n");
463  goto cleanup;
464  }
465 
466 cleanup:
467 
468  if (ThreadObjects[0])
469  Status = KeWaitForSingleObject(ThreadObjects[0], Executive, KernelMode, FALSE, NULL);
470 
471  if (StartContext1 != NULL)
472  ExFreePoolWithTag(StartContext1, 'tXTC');
473 
474  if (ThreadObjects[1])
475  Status = KeWaitForSingleObject(ThreadObjects[1], Executive, KernelMode, FALSE, NULL);
476 
477  if (StartContext2 != NULL)
478  ExFreePoolWithTag(StartContext2, 'tXTC');
479 
480  if (ThreadObjects[0] != NULL)
481  ObDereferenceObject(ThreadObjects[0]);
482 
483  if (ThreadObjects[1] != NULL)
484  ObDereferenceObject(ThreadObjects[1]);
485 
487  ZwClose(Thread1);
488 
490  ZwClose(Thread2);
491 }
IN PUNICODE_STRING IN POBJECT_ATTRIBUTES ObjectAttributes
Definition: conport.c:35
#define THREAD_ALL_ACCESS
Definition: nt_native.h:1339
NTSYSAPI NTSTATUS NTAPI ZwClose(_In_ HANDLE Handle)
LONG NTSTATUS
Definition: precomp.h:26
static VOID KmtInitTestContext(PTEST_CONTEXT Ctx, SHORT ThreadId, ULONG RegionSize, ULONG AllocationType, ULONG Protect)
#define INVALID_HANDLE_VALUE
Definition: compat.h:391
VOID NTAPI ObDereferenceObject(IN PVOID Object)
Definition: obref.c:375
NTSTATUS NTAPI KeWaitForSingleObject(IN PVOID Object, IN KWAIT_REASON WaitReason, IN KPROCESSOR_MODE WaitMode, IN BOOLEAN Alertable, IN PLARGE_INTEGER Timeout OPTIONAL)
Definition: wait.c:416
#define MEM_COMMIT
Definition: nt_native.h:1313
static DWORD WINAPI Thread1(_Inout_opt_ PVOID Parameter)
#define PAGED_CODE()
Definition: video.h:57
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:496
smooth NULL
Definition: ftsmooth.c:416
#define trace
Definition: atltest.h:70
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
static VOID NTAPI SystemProcessTestWorker(PVOID StartContext)
#define ExAllocatePoolWithTag(hernya, size, tag)
Definition: env_spec_w32.h:350
POBJECT_TYPE PsThreadType
Definition: thread.c:20
Status
Definition: gdiplustypes.h:24
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:602
static DWORD WINAPI Thread2(_Inout_opt_ PVOID Parameter)
#define InitializeObjectAttributes(p, n, a, r, s)
Definition: reg.c:106
char * cleanup(char *str)
Definition: wpickclick.c:99
#define ExFreePoolWithTag(_P, _T)
Definition: module.h:1099
#define OBJ_KERNEL_HANDLE
Definition: winternl.h:231
#define PAGE_READWRITE
Definition: nt_native.h:1304

Referenced by START_TEST().

◆ SystemProcessTestWorker()

static VOID NTAPI SystemProcessTestWorker ( PVOID  StartContext)
static

Definition at line 346 of file ZwAllocateVirtualMemory.c.

347 {
349  PTEST_CONTEXT Context = (PTEST_CONTEXT)StartContext;
350  ULONG Index = 0;
351  PVOID Base = NULL;
352 
353  PAGED_CODE();
354 
355  RtlZeroMemory(Context->Bases, sizeof(Context->Bases));
356 
357  Status = ZwAllocateVirtualMemory(NtCurrentProcess(), &Base, 0, &Context->RegionSize, Context->AllocationType, Context->Protect);
358  ZwFreeVirtualMemory(NtCurrentProcess(), &Base, &Context->RegionSize, MEM_RELEASE);
359  Base = NULL;
360 
361  //if the previous allocation has failed there is no need to do the loop
362  while (NT_SUCCESS(Status) && Index < RTL_NUMBER_OF(Context->Bases))
363  {
364  Status = ZwAllocateVirtualMemory(NtCurrentProcess(), &Base, 0, &Context->RegionSize, Context->AllocationType, Context->Protect);
365 
366  Context->Bases[Index] = Base;
367  if ((Index % 10) == 0)
368  {
369  if (Context->AllocationType == MEM_COMMIT)
370  {
372  }
373  else
374  {
376  }
377  }
378 
379  Base = NULL;
380  Index++;
381  }
382 
383  trace("[SYSTEM THREAD %d]. Error code %x. Chunks allocated: %d\n", Context->ThreadId, Status, Index);
384 
385  //free the allocated memory so that we can continue with the tests
387  Index = 0;
388  while (NT_SUCCESS(Status) && Index < RTL_NUMBER_OF(Context->Bases))
389  {
390  Context->RegionSize = 0;
391  Status = ZwFreeVirtualMemory(NtCurrentProcess(), &Context->Bases[Index], &Context->RegionSize, MEM_RELEASE);
392  Context->Bases[Index++] = NULL;
393  }
394 
396 }
EH_STD::basic_string< char, EH_STD::char_traits< char >, eh_allocator(char) > TestString
Definition: test_string.cpp:30
static VOID CheckBufferReadWrite(PVOID Destination, CONST VOID *Source, SIZE_T Length, NTSTATUS ExpectedStatus)
LONG NTSTATUS
Definition: precomp.h:26
_In_opt_ ULONG Base
Definition: rtlfuncs.h:2343
#define MEM_COMMIT
Definition: nt_native.h:1313
#define PAGED_CODE()
Definition: video.h:57
smooth NULL
Definition: ftsmooth.c:416
#define NtCurrentProcess()
Definition: nt_native.h:1657
#define trace
Definition: atltest.h:70
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
static const UCHAR Index[8]
Definition: usbohci.c:18
struct _TEST_CONTEXT * PTEST_CONTEXT
Status
Definition: gdiplustypes.h:24
const ULONG TestStringSize
#define STATUS_ACCESS_VIOLATION
Definition: ntstatus.h:228
#define RTL_NUMBER_OF(x)
Definition: RtlRegistry.c:12
NTSTATUS NTAPI PsTerminateSystemThread(IN NTSTATUS ExitStatus)
Definition: kill.c:1144
#define MEM_RELEASE
Definition: nt_native.h:1316
unsigned int ULONG
Definition: retypes.h:1
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:261
return STATUS_SUCCESS
Definition: btrfs.c:2938

Referenced by SystemProcessTest().

◆ Test_ZwAllocateVirtualMemory()

VOID Test_ZwAllocateVirtualMemory ( VOID  )

Referenced by SimpleErrorChecks().

Variable Documentation

◆ TestString

const char TestString[] = "TheLongBrownFoxJumpedTheWhiteRabbitTheLongBrownFoxJumpedTheWhiteRabbitTheLongBrownFoxJumpedTheWhiteRabbitTheLongBrownFoxJumpedTheWhiteRabbitTheLongBrownFoxJumpedTheWhiteRabbitTheLongBrownFoxJumpedThe"

Definition at line 16 of file ZwAllocateVirtualMemory.c.

◆ TestStringSize