ReactOS 0.4.15-dev-7961-gdcf9eb0
NtAllocateVirtualMemory.c File Reference
#include "precomp.h"
#include <pseh/pseh2.h>
Include dependency graph for NtAllocateVirtualMemory.c:

Go to the source code of this file.

Macros

#define RUNS   32
 

Functions

static VOID ValidateAllocations (VOID)
 
static PVOID Allocate (SIZE_T DataSize)
 
static VOID Free (PVOID UserBuffer)
 
static PVOID ReAllocate (PVOID OldUserBuffer, SIZE_T NewDataSize)
 
static VOID AccessMemory1 (PVOID UserBuffer, SIZE_T DataSize)
 
static BOOLEAN CheckMemory1 (PVOID UserBuffer, SIZE_T DataSize)
 
static VOID AccessMemory2 (PVOID UserBuffer, SIZE_T DataSize)
 
static BOOLEAN CheckMemory2 (PVOID UserBuffer, SIZE_T DataSize)
 
VOID CheckSize (ULONG_PTR Base, SIZE_T InSize, SIZE_T ExpectedSize)
 
VOID CheckAlignment ()
 
static VOID CheckAdjacentVADs ()
 
static VOID CheckSomeDefaultAddresses (VOID)
 
 START_TEST (NtAllocateVirtualMemory)
 

Variables

static PVOID Allocations [4096] = { NULL }
 
static ULONG CurrentAllocation = 0
 

Macro Definition Documentation

◆ RUNS

#define RUNS   32

Definition at line 747 of file NtAllocateVirtualMemory.c.

Function Documentation

◆ AccessMemory1()

static VOID AccessMemory1 ( PVOID  UserBuffer,
SIZE_T  DataSize 
)
static

Definition at line 143 of file NtAllocateVirtualMemory.c.

146{
147 PBYTE Buffer = UserBuffer;
148 SIZE_T i;
149
150 for (i = 0; i < DataSize; ++i)
151 Buffer[i] = LOBYTE(i);
152}
Definition: bufpool.h:45
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 LOBYTE(W)
Definition: jmemdos.c:487
_In_ NDIS_STATUS _In_ ULONG _In_ USHORT _In_opt_ PVOID _In_ ULONG DataSize
Definition: ndis.h:4755
BYTE * PBYTE
Definition: pedump.c:66
ULONG_PTR SIZE_T
Definition: typedefs.h:80

Referenced by START_TEST().

◆ AccessMemory2()

static VOID AccessMemory2 ( PVOID  UserBuffer,
SIZE_T  DataSize 
)
static

Definition at line 175 of file NtAllocateVirtualMemory.c.

178{
179 PBYTE Buffer = UserBuffer;
180 SIZE_T i;
181
182 for (i = 0; i < DataSize; ++i)
183 Buffer[i] = UCHAR_MAX - LOBYTE(i);
184}
#define UCHAR_MAX
Definition: limits.h:25

Referenced by START_TEST().

◆ Allocate()

static PVOID Allocate ( SIZE_T  DataSize)
static

Definition at line 45 of file NtAllocateVirtualMemory.c.

47{
49 PVOID AllocationStart = NULL;
51 PVOID FirstPageStart;
52 SIZE_T NumberOfPages = AllocationSize / PAGE_SIZE;
54 PUCHAR UserBuffer;
55
57
58 if (!NT_SUCCESS(Status))
59 return NULL;
60
61 FirstPageStart = (PUCHAR)AllocationStart + AllocationSize - PAGE_SIZE * NumberOfPages;
62 Size = (NumberOfPages - 1) * PAGE_SIZE;
64 if (!NT_SUCCESS(Status))
65 {
69 return NULL;
70 }
71 ASSERT(Size % sizeof(ULONG) == 0);
72 ASSERT(RtlCompareMemoryUlong(FirstPageStart, Size, 0) == Size);
73
74 UserBuffer = AllocationStart;
75 UserBuffer += AllocationSize - PAGE_SIZE - DataSize;
76 RtlFillMemory(FirstPageStart, UserBuffer - (PUCHAR)FirstPageStart, 0xae);
77 RtlZeroMemory(UserBuffer, DataSize);
78 ((PSIZE_T)UserBuffer)[-2] = AllocationSize;
79 ((PSIZE_T)UserBuffer)[-1] = DataSize;
80
81 Allocations[CurrentAllocation++] = UserBuffer;
83 return UserBuffer;
84}
static VOID ValidateAllocations(VOID)
static ULONG CurrentAllocation
static PVOID Allocations[4096]
LONG NTSTATUS
Definition: precomp.h:26
#define NULL
Definition: types.h:112
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define PAGE_SIZE
Definition: env_spec_w32.h:49
IN PFCB IN PFILE_OBJECT FileObject IN ULONG AllocationSize
Definition: fatprocs.h:322
Status
Definition: gdiplustypes.h:25
#define RtlFillMemory(Dest, Length, Fill)
Definition: winternl.h:599
#define ASSERT(a)
Definition: mode.c:44
#define PAGE_ROUND_UP(x)
Definition: mmtypes.h:38
#define PAGE_READWRITE
Definition: nt_native.h:1304
#define NtCurrentProcess()
Definition: nt_native.h:1657
#define MEM_RESERVE
Definition: nt_native.h:1314
#define MEM_RELEASE
Definition: nt_native.h:1316
#define MEM_COMMIT
Definition: nt_native.h:1313
#define PAGE_NOACCESS
Definition: nt_native.h:1302
NTSTATUS NTAPI NtFreeVirtualMemory(IN HANDLE ProcessHandle, IN PVOID *UBaseAddress, IN PSIZE_T URegionSize, IN ULONG FreeType)
Definition: virtual.c:5230
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
#define STATUS_SUCCESS
Definition: shellext.h:65
ULONG_PTR * PSIZE_T
Definition: typedefs.h:80
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:262
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:4533
_Must_inspect_result_ NTSYSAPI SIZE_T NTAPI RtlCompareMemoryUlong(_In_reads_bytes_(Length) PVOID Source, _In_ SIZE_T Length, _In_ ULONG Pattern)

◆ CheckAdjacentVADs()

static VOID CheckAdjacentVADs ( )
static

Definition at line 366 of file NtAllocateVirtualMemory.c.

367{
370 SIZE_T Size;
371 MEMORY_BASIC_INFORMATION MemoryBasicInfo;
372
373 /* Reserve a full 64k region */
374 BaseAddress = UlongToPtr(0x50000000);
375 Size = 0x10000;
378 0,
379 &Size,
383 if (!NT_SUCCESS(Status))
384 return;
385
386 /* Reserve another 64k region, but with 64k between */
387 BaseAddress = UlongToPtr(0x50020000);
388 Size = 0x10000;
391 0,
392 &Size,
396 if (!NT_SUCCESS(Status))
397 return;
398
399 /* Try to free the whole at once */
400 BaseAddress = UlongToPtr(0x50000000);
401 Size = 0x30000;
404
405 /* Reserve the part in the middle */
406 BaseAddress = UlongToPtr(0x50010000);
407 Size = 0x10000;
410 0,
411 &Size,
415
416 /* Try to commit memory covering 2 allocations */
417 BaseAddress = UlongToPtr(0x50004000);
418 Size = 0x10000;
421 0,
422 &Size,
426
427 /* Commit a page */
428 BaseAddress = UlongToPtr(0x50000000);
429 Size = 0x1000;
432 0,
433 &Size,
437
438 /* Commit another page */
439 BaseAddress = UlongToPtr(0x50002000);
440 Size = 0x1000;
443 0,
444 &Size,
448
450 {
451 *(int*)UlongToPtr(0x50000000) = 1;
452 }
454 {
455 ok(0, "Got exception\n");
456 }
457 _SEH2_END;
458
460 {
461 (void)*(volatile int*)UlongToPtr(0x50002000);
462 }
464 {
466 }
467 _SEH2_END;
469
470 /* Allocate 3 pages, on top of the previous 2 */
471 BaseAddress = UlongToPtr(0x50000000);
472 Size = 0x3000;
475 0,
476 &Size,
480
482 {
483 *(int*)UlongToPtr(0x50000000) = 1;
484 }
486 {
488 }
489 _SEH2_END;
491
492 /* Commit a page at the end of the first region */
493 BaseAddress = UlongToPtr(0x5000F000);
494 Size = 0x1000;
497 0,
498 &Size,
502 ok_ptr(BaseAddress, UlongToPtr(0x5000F000));
503
504 /* See where is the base of this newly committed area
505 * (choose a base address in the middle of it) */
507 UlongToPtr(0x5000F700),
509 &MemoryBasicInfo,
510 sizeof(MemoryBasicInfo),
511 NULL);
513 /* The base address is the beginning of the committed area */
514 ok_ptr(MemoryBasicInfo.BaseAddress, UlongToPtr(0x5000F000));
515 /* The allocation base address is the beginning of the whole region */
516 ok_ptr(MemoryBasicInfo.AllocationBase, UlongToPtr(0x50000000));
517 /* This is the protection of the memory when it was reserved. */
518 ok_long(MemoryBasicInfo.AllocationProtect, PAGE_NOACCESS);
519 /* This is the size of the committed region. (ie, smallest chunk size) */
520 ok_long(MemoryBasicInfo.RegionSize, 0x1000);
521 /* This is the state of the queried address */
522 ok_long(MemoryBasicInfo.State, MEM_COMMIT);
523 /* This is the protection of the queried address */
524 ok_long(MemoryBasicInfo.Protect, PAGE_READWRITE);
525 /* NtAllocateVirtualMemory makes it MEM_PRIVATE */
526 ok_long(MemoryBasicInfo.Type, MEM_PRIVATE);
527
528 /* Try to free the whole region at once */
529 BaseAddress = UlongToPtr(0x50000000);
530 Size = 0x30000;
533
534 BaseAddress = UlongToPtr(0x50000000);
535 Size = 0x10000;
538
539 BaseAddress = UlongToPtr(0x50010000);
540 Size = 0x10000;
543
544 BaseAddress = UlongToPtr(0x50020000);
545 Size = 0x10000;
548
549 /* Reserve 3 full 64k region */
550 BaseAddress = UlongToPtr(0x50000000);
551 Size = 0x30000;
554 0,
555 &Size,
559 if (!NT_SUCCESS(Status))
560 return;
561
562 /* Release the 64k region in the middle */
563 BaseAddress = UlongToPtr(0x50010000);
564 Size = 0x10000;
567
568}
#define ok_ntstatus(status, expected)
Definition: atltest.h:135
#define ok_long(expression, result)
Definition: atltest.h:133
#define ok(value,...)
Definition: atltest.h:57
#define ok_ptr(expression, result)
Definition: atltest.h:108
#define PAGE_READONLY
Definition: compat.h:138
#define UlongToPtr(u)
Definition: config.h:106
#define _SEH2_END
Definition: filesup.c:22
#define _SEH2_TRY
Definition: filesup.c:19
#define EXCEPTION_EXECUTE_HANDLER
Definition: excpt.h:85
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID * BaseAddress
Definition: mmfuncs.h:404
@ MemoryBasicInformation
Definition: mmtypes.h:183
#define MEM_PRIVATE
Definition: nt_native.h:1318
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
#define STATUS_UNABLE_TO_FREE_VM
Definition: ntstatus.h:263
#define STATUS_ACCESS_VIOLATION
Definition: ntstatus.h:242
#define STATUS_CONFLICTING_ADDRESSES
Definition: ntstatus.h:261
#define _SEH2_GetExceptionCode()
Definition: pseh2_64.h:159
#define _SEH2_EXCEPT(...)
Definition: pseh2_64.h:34

Referenced by START_TEST().

◆ CheckAlignment()

VOID CheckAlignment ( )

Definition at line 229 of file NtAllocateVirtualMemory.c.

230{
233 SIZE_T Size;
234
235 CheckSize(0x50000000, 0x0001, 0x1000);
236 CheckSize(0x50008000, 0x0001, 0x9000);
237 CheckSize(0x50000010, 0x1000, 0x2000);
238 CheckSize(0x50010000, 0x2000, 0x2000);
239 CheckSize(0x5000FFFF, 0x3000, 0x13000);
240 CheckSize(0x50001010, 0x7000, 0x9000);
241 CheckSize(0x50001010, 0xC000, 0xe000);
242
243 /* Reserve memory not aligned to allocation granularity */
244 BaseAddress = UlongToPtr(0x50001010);
245 Size = 0x1000;
248 0,
249 &Size,
253 ok(BaseAddress == UlongToPtr(0x50000000), "Got back wrong base address: %p", BaseAddress);
254 ok(Size == 0x3000, "Got back wrong size: 0x%Ix", Size);
255
256 /* Try to reserve again in the same 64k region */
257 BaseAddress = UlongToPtr(0x50008000);
258 Size = 0x1000;
261 0,
262 &Size,
266
267 /* Commit memory */
268 BaseAddress = UlongToPtr(0x50002000);
269 Size = 0x1000;
272 0,
273 &Size,
277 ok(BaseAddress == UlongToPtr(0x50002000), "Got back wrong base address: %p", BaseAddress);
278 ok(Size == 0x1000, "Got back wrong size: 0x%Ix", Size);
279
280 /* Commit the same address again with a different protection */
281 BaseAddress = UlongToPtr(0x50002000);
282 Size = 0x1000;
285 0,
286 &Size,
290 ok(BaseAddress == UlongToPtr(0x50002000), "Got back wrong base address: %p", BaseAddress);
291 ok(Size == 0x1000, "Got back wrong size: 0x%Ix", Size);
292
293 /* Commit memory at a too high address */
294 BaseAddress = UlongToPtr(0x50003000);
295 Size = 0x1000;
298 0,
299 &Size,
303
304 /* Decommit the memory, even those pages that were not committed */
305 BaseAddress = UlongToPtr(0x50000000);
306 Size = 0x3000;
309
310 /* Try to release memory in a different 64k region */
311 BaseAddress = UlongToPtr(0x50010000);
312 Size = 0x1000;
315
316 /* Release the memory in the same 64k region at a different address */
317 BaseAddress = UlongToPtr(0x50008000);
318 Size = 0x1000;
321
322 /* Release the memory at the correct address but with wrong size */
323 BaseAddress = UlongToPtr(0x50000000);
324 Size = 0x4000;
327
328 /* Release the memory */
329 BaseAddress = UlongToPtr(0x50000000);
330 Size = 0x3000;
333
334 /* Reserve and commit at once */
335 BaseAddress = UlongToPtr(0x50004080);
336 Size = 0x1000;
339 0,
340 &Size,
344 ok(BaseAddress == UlongToPtr(0x50000000), "Got back wrong base address: %p", BaseAddress);
345 ok(Size == 0x6000, "Got back wrong size: 0x%Ix", Size);
346
348 {
349 *(int*)BaseAddress = 1;
350 *(int*)UlongToPtr(0x50004080) = 1;
351 }
353 {
354 ok(0, "Got exception\n");
355 }
356 _SEH2_END;
357
358 /* Release the memory */
361
362}
VOID CheckSize(ULONG_PTR Base, SIZE_T InSize, SIZE_T ExpectedSize)
#define MEM_DECOMMIT
Definition: nt_native.h:1315
#define STATUS_MEMORY_NOT_ALLOCATED
Definition: ntstatus.h:396

Referenced by PPBridge_SaveCurrentSettings(), and START_TEST().

◆ CheckMemory1()

static BOOLEAN CheckMemory1 ( PVOID  UserBuffer,
SIZE_T  DataSize 
)
static

Definition at line 156 of file NtAllocateVirtualMemory.c.

159{
160 PBYTE Buffer = UserBuffer;
161 SIZE_T i;
162
163 for (i = 0; i < DataSize; ++i)
164 if (Buffer[i] != LOBYTE(i))
165 {
166 trace("Mismatch in region %p at index %lu. Value=%02x\n", UserBuffer, (ULONG)i, Buffer[i]);
167 ASSERT(FALSE);
168 return FALSE;
169 }
170 return TRUE;
171}
#define trace
Definition: atltest.h:70
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117

Referenced by START_TEST().

◆ CheckMemory2()

static BOOLEAN CheckMemory2 ( PVOID  UserBuffer,
SIZE_T  DataSize 
)
static

Definition at line 188 of file NtAllocateVirtualMemory.c.

191{
192 PBYTE Buffer = UserBuffer;
193 SIZE_T i;
194
195 for (i = 0; i < DataSize; ++i)
196 if (Buffer[i] != UCHAR_MAX - LOBYTE(i))
197 {
198 trace("Mismatch in region %p at index %lu. Value=%02x\n", UserBuffer, (ULONG)i, Buffer[i]);
199 ASSERT(FALSE);
200 return FALSE;
201 }
202 return TRUE;
203}

Referenced by START_TEST().

◆ CheckSize()

VOID CheckSize ( ULONG_PTR  Base,
SIZE_T  InSize,
SIZE_T  ExpectedSize 
)

Definition at line 206 of file NtAllocateVirtualMemory.c.

207{
210 SIZE_T Size;
211
212 /* Reserve memory */
214 Size = InSize;
217 0,
218 &Size,
221 ok(NT_SUCCESS(Status), "NtAllocateVirtualMemory failed!\n");
222 ok(BaseAddress == (PVOID)(Base & ~((ULONG_PTR)0xFFFF)), "Got back wrong base address: %p\n", BaseAddress);
223 ok(Size == ExpectedSize, "Alloc of 0x%Ix: got back wrong size: 0x%Ix, expected 0x%Ix\n", InSize, Size, ExpectedSize);
225 ok(NT_SUCCESS(Status), "NtFreeVirtualMemory failed!\n");
226}
_In_opt_ ULONG Base
Definition: rtlfuncs.h:2439
void * PVOID
Definition: typedefs.h:50
uint32_t ULONG_PTR
Definition: typedefs.h:65

Referenced by CheckAlignment().

◆ CheckSomeDefaultAddresses()

static VOID CheckSomeDefaultAddresses ( VOID  )
static

Definition at line 572 of file NtAllocateVirtualMemory.c.

573{
576 SIZE_T Size;
577
578 // NULL.
579
580 /* Reserve memory dynamically, not at 0x00000000 */
582 Size = 0x1000;
585 0,
586 &Size,
590 ok(BaseAddress != 0x00000000, "Unexpected BaseAddress = 0x00000000\n");
593
594 // 0x00000000, 64k: Free.
595
596 /* Reserve and commit memory at 0x00000000, after round down */
597 BaseAddress = UlongToPtr(0x00000000 + 0x0FFF);
598 Size = 0x1000;
601 0,
602 &Size,
606 ok_ptr(BaseAddress, 0x00000000);
607
608 // Double-check that it is not forbidden "in order to catch null pointer accesses".
609 StartSeh()
610 *(int*)UlongToPtr(0x00000000) = 1;
612
615
616 /* Reserve memory above 0x00000000 */
617 BaseAddress = UlongToPtr(0x00000000 + 0x1000);
618 Size = 0x1000;
621 0,
622 &Size,
628
629 /* The following checks assume very default addresses,
630 * no address space layout randomization (ASLR). */
631#ifdef _WIN64
632 ok(FALSE, "ToDo, 64-bit: Check/Adapt 32-bit results\n");
633#endif
634
635 // 0x00010000, 4k: Private Data.
636 // 0x00011000, 60k: Unusable.
637
638 /* Reserve memory below 0x00010000 */
639 BaseAddress = UlongToPtr(0x00010000 - 0x1000);
640 Size = 0x1000;
643 0,
644 &Size,
650
651 /* Reserve memory at 0x00010000:
652 * Windows NT legacy default executable image base */
653 BaseAddress = UlongToPtr(0x00010000);
654 Size = 0x1000;
657 0,
658 &Size,
662 if (NT_SUCCESS(Status))
663 { // Unexpected, cleanup.
666 }
667
668 // 0x00400000: Image base.
669
670 /* Reserve memory below 0x00400000 */
671 BaseAddress = UlongToPtr(0x00400000 - 0x1000);
672 Size = 0x1000;
675 0,
676 &Size,
679 if (NT_SUCCESS(Status))
680 {
681 trace("Below 0x00400000 is available, as on ReactOS and Windows S03\n");
682 // 0x003F0000, 64k: Free.
686 }
687 else
688 {
689 trace("Below 0x00400000 is not available, as on Windows XP\n");
690 // 0x003F0000, 4k: Shareable.
691 // 0x003F1000, 60k: Unusable.
693 }
694
695 /* Reserve memory at 0x00400000:
696 * Windows NT legacy default DLL image base,
697 * (ReactOS and) Windows 95 new default executable image base */
698 BaseAddress = UlongToPtr(0x00400000);
699 Size = 0x1000;
702 0,
703 &Size,
707 if (NT_SUCCESS(Status))
708 { // Unexpected, cleanup.
711 }
712
713 // 0x10000000: Free.
714
715 /* Reserve memory below 0x10000000 */
716 BaseAddress = UlongToPtr(0x10000000 - 0x1000);
717 Size = 0x1000;
720 0,
721 &Size,
727
728 /* Reserve memory at 0x10000000:
729 * Windows new default non-OS DLL image base */
730 BaseAddress = UlongToPtr(0x10000000);
731 Size = 0x1000;
734 0,
735 &Size,
741
742#ifdef _WIN64
743 skip("ToDo, 64-bit: Add 0x140000000/Exe and 0x180000000/DLL checks\n");
744#endif
745}
#define StartSeh()
Definition: _sntprintf.h:16
#define EndSeh(ExpectedStatus)
Definition: _sntprintf.h:17
#define skip(...)
Definition: atltest.h:64

Referenced by START_TEST().

◆ Free()

static VOID Free ( PVOID  UserBuffer)
static

Definition at line 88 of file NtAllocateVirtualMemory.c.

90{
92 PVOID AllocationStart;
93 SIZE_T Zero = 0;
96 ULONG i;
97
98 AllocationSize = ((PSIZE_T)UserBuffer)[-2];
99 DataSize = ((PSIZE_T)UserBuffer)[-1];
100 ASSERT(DataSize != 0);
101
102 AllocationStart = (PUCHAR)UserBuffer + DataSize + PAGE_SIZE - AllocationSize;
103 ASSERT((SIZE_T)AllocationStart % PAGE_SIZE == 0);
104
105 RtlFillMemory(UserBuffer, DataSize, 0xbe);
106 ((PSIZE_T)UserBuffer)[-1] = 0;
107 ((PSIZE_T)UserBuffer)[-2] = 0xFAFBFCFD;
108
109 for (i = 0; i < CurrentAllocation; ++i)
110 if (Allocations[i] == UserBuffer)
111 {
112 Allocations[i] = NULL;
113 break;
114 }
116
119}
IN PVCB IN VBO IN ULONG OUT PBCB OUT PVOID IN BOOLEAN IN BOOLEAN Zero
Definition: fatprocs.h:418

◆ ReAllocate()

static PVOID ReAllocate ( PVOID  OldUserBuffer,
SIZE_T  NewDataSize 
)
static

Definition at line 123 of file NtAllocateVirtualMemory.c.

126{
127 PVOID NewUserBuffer;
128 SIZE_T OldDataSize;
129
130 OldDataSize = ((PSIZE_T)OldUserBuffer)[-1];
131 ASSERT(OldDataSize != 0);
132
133 NewUserBuffer = Allocate(NewDataSize);
134 ASSERT(((PSIZE_T)OldUserBuffer)[-1] == OldDataSize);
135 RtlCopyMemory(NewUserBuffer, OldUserBuffer, min(OldDataSize, NewDataSize));
136 ASSERT(((PSIZE_T)OldUserBuffer)[-1] == OldDataSize);
137 Free(OldUserBuffer);
138 return NewUserBuffer;
139}
#define min(a, b)
Definition: monoChain.cc:55
#define RtlCopyMemory(Destination, Source, Length)
Definition: typedefs.h:263
_In_opt_ PALLOCATE_FUNCTION Allocate
Definition: exfuncs.h:814
_In_opt_ PALLOCATE_FUNCTION _In_opt_ PFREE_FUNCTION Free
Definition: exfuncs.h:815

Referenced by START_TEST().

◆ START_TEST()

START_TEST ( NtAllocateVirtualMemory  )

Definition at line 749 of file NtAllocateVirtualMemory.c.

750{
751 PVOID Mem1, Mem2;
752 SIZE_T Size1, Size2;
753 ULONG i;
754
758
759 Size1 = 32;
760 Mem1 = Allocate(Size1);
761 AccessMemory1(Mem1, Size1);
762 Size2 = 128;
763 Mem2 = Allocate(Size2);
764 AccessMemory2(Mem2, Size2);
765 for (i = 0; i < RUNS; ++i)
766 {
767 PVOID New;
768 ok(CheckMemory1(Mem1, Size1) == TRUE, "CheckMemory1 failure\n");
769 New = ReAllocate(Mem1, Size1 * 3 / 2);
770 if (New == NULL)
771 {
772 skip("Realloc failure\n");
773 break;
774 }
775 Mem1 = New;
776 ok(CheckMemory1(Mem1, Size1) == TRUE, "CheckMemory1 failure\n");
777 Size1 = Size1 * 3 / 2;
778 AccessMemory1(Mem1, Size1);
779
780 ok(CheckMemory2(Mem2, Size2) == TRUE, "CheckMemory2 failure\n");
781 New = ReAllocate(Mem2, Size2 + 128);
782 if (New == NULL)
783 {
784 skip("Realloc failure\n");
785 break;
786 }
787 Mem2 = New;
788 ok(CheckMemory2(Mem2, Size2) == TRUE, "CheckMemory2 failure\n");
789 Size2 += 128;
790 AccessMemory2(Mem2, Size2);
791 }
792 ok(CheckMemory2(Mem2, Size2) == TRUE, "CheckMemory2 failure\n");
793 Free(Mem2);
794 ok(CheckMemory1(Mem1, Size1) == TRUE, "CheckMemory1 failure\n");
795 Free(Mem1);
796}
static VOID AccessMemory1(PVOID UserBuffer, SIZE_T DataSize)
static BOOLEAN CheckMemory2(PVOID UserBuffer, SIZE_T DataSize)
static VOID CheckAdjacentVADs()
#define RUNS
static VOID AccessMemory2(PVOID UserBuffer, SIZE_T DataSize)
static BOOLEAN CheckMemory1(PVOID UserBuffer, SIZE_T DataSize)
static PVOID ReAllocate(PVOID OldUserBuffer, SIZE_T NewDataSize)
VOID CheckAlignment()
static VOID CheckSomeDefaultAddresses(VOID)
#define New(t)
Definition: rtf.h:1086

◆ ValidateAllocations()

static VOID ValidateAllocations ( VOID  )
static

Definition at line 20 of file NtAllocateVirtualMemory.c.

21{
22 ULONG i;
23
24 ASSERT(CurrentAllocation < sizeof(Allocations) / sizeof(Allocations[0]));
25 for (i = 0; i < CurrentAllocation; ++i)
26 {
27 PUCHAR UserBuffer = Allocations[i];
30
31 if (UserBuffer == NULL)
32 continue;
33
34 AllocationSize = ((PSIZE_T)UserBuffer)[-2];
35 DataSize = ((PSIZE_T)UserBuffer)[-1];
38 ASSERT(DataSize != 0);
39 ASSERT(((SIZE_T)UserBuffer + DataSize) % PAGE_SIZE == 0);
40 }
41}

Referenced by Allocate(), and Free().

Variable Documentation

◆ Allocations

PVOID Allocations[4096] = { NULL }
static

Definition at line 15 of file NtAllocateVirtualMemory.c.

Referenced by Allocate(), Free(), and ValidateAllocations().

◆ CurrentAllocation

ULONG CurrentAllocation = 0
static

Definition at line 16 of file NtAllocateVirtualMemory.c.

Referenced by Allocate(), Free(), and ValidateAllocations().