ReactOS 0.4.16-dev-2216-ga08d639
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 797 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:11

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:33
#define PAGE_SIZE
Definition: env_spec_w32.h:49
IN PFCB IN PFILE_OBJECT FileObject IN ULONG AllocationSize
Definition: fatprocs.h:323
Status
Definition: gdiplustypes.h:25
#define RtlFillMemory(Dest, Length, Fill)
Definition: winternl.h:603
#define ASSERT(a)
Definition: mode.c:44
#define PAGE_ROUND_UP(x)
Definition: mmtypes.h:38
#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_RELEASE
Definition: nt_native.h:1319
#define MEM_COMMIT
Definition: nt_native.h:1316
#define PAGE_NOACCESS
Definition: nt_native.h:1305
NTSTATUS NTAPI NtFreeVirtualMemory(IN HANDLE ProcessHandle, IN PVOID *UBaseAddress, IN PSIZE_T URegionSize, IN ULONG FreeType)
Definition: virtual.c:5192
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:4457
#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:4539
_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 STATUS_ACCESS_VIOLATION
#define EXCEPTION_EXECUTE_HANDLER
Definition: excpt.h:90
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID * BaseAddress
Definition: mmfuncs.h:404
@ MemoryBasicInformation
Definition: mmtypes.h:183
#define MEM_PRIVATE
Definition: nt_native.h:1321
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:4374
#define STATUS_UNABLE_TO_FREE_VM
Definition: ntstatus.h:356
#define STATUS_CONFLICTING_ADDRESSES
Definition: ntstatus.h:354
#define _SEH2_GetExceptionCode()
Definition: pseh2_64.h:181
#define _SEH2_EXCEPT(...)
Definition: pseh2_64.h:82
#define _SEH2_END
Definition: pseh2_64.h:171
#define _SEH2_TRY
Definition: pseh2_64.h:71

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:1318
#define STATUS_MEMORY_NOT_ALLOCATED
Definition: ntstatus.h:490

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:2486
void * PVOID
Definition: typedefs.h:50
uint32_t ULONG_PTR
Definition: typedefs.h:65

Referenced by CheckAlignment(), and CRegTreeOptions::GetCheckState().

◆ 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 /* Try to reserve memory at the shared user page */
596 Size = 0x1000;
599 0,
600 &Size,
604
605 /* Try to unmap the shared user page */
607 Size = 0x1000;
610
611 // 0x00000000, 64k: Free on XP/2K3, reserved later.
612
613 /* Reserve and commit memory at 0x00000000, after round down */
614 BaseAddress = UlongToPtr(0x00000000 + 0x0FFF);
615 Size = 0x1000;
618 0,
619 &Size,
623 {
625 }
626 else if (GetNTVersion() >= _WIN32_WINNT_VISTA)
627 {
629 }
630 else
631 {
633 ok_ptr(BaseAddress, 0x00000000);
634
635 // Double-check that it is not forbidden "in order to catch null pointer accesses".
636 StartSeh()
637 *(int*)UlongToPtr(0x00000000) = 1;
639
642 }
643
644 /* Reserve memory above 0x00000000 */
645 BaseAddress = UlongToPtr(0x00000000 + 0x1000);
646 Size = 0x1000;
649 0,
650 &Size,
654 {
656 }
657 else if (GetNTVersion() >= _WIN32_WINNT_VISTA)
658 {
660 }
661 else
662 {
666 }
667
668 // 0x00010000, 4k: Private Data.
669 // 0x00011000, 60k: Unusable.
670
671 /* Reserve memory below 0x00010000 */
672 BaseAddress = UlongToPtr(0x00010000 - 0x1000);
673 Size = 0x1000;
676 0,
677 &Size,
681 {
683 }
684 else if (GetNTVersion() >= _WIN32_WINNT_VISTA)
685 {
687 }
688 else
689 {
693 }
694
695 /* The following checks assume very default addresses,
696 * no address space layout randomization (ASLR). */
698 {
699 skip("Skipping default address checks on Vista+\n");
700 }
701 else
702 {
703
704 /* Reserve memory at 0x00010000:
705 * Windows NT legacy default executable image base */
706 BaseAddress = UlongToPtr(0x00010000);
707 Size = 0x1000;
710 0,
711 &Size,
715 if (NT_SUCCESS(Status))
716 { // Unexpected, cleanup.
719 }
720
721 // 0x00400000: Image base.
722
723 /* Reserve memory below 0x00400000 */
724 BaseAddress = UlongToPtr(0x00400000 - 0x1000);
725 Size = 0x1000;
728 0,
729 &Size,
732 if (NT_SUCCESS(Status))
733 {
734 trace("Below 0x00400000 is available, as on ReactOS and Windows S03\n");
735 // 0x003F0000, 64k: Free.
739 }
740 else
741 {
742 trace("Below 0x00400000 is not available, as on Windows XP\n");
743 // 0x003F0000, 4k: Shareable.
744 // 0x003F1000, 60k: Unusable.
746 }
747
748 /* Reserve memory at 0x00400000:
749 * Windows NT legacy default DLL image base,
750 * (ReactOS and) Windows 95 new default executable image base */
751 BaseAddress = UlongToPtr(0x00400000);
752 Size = 0x1000;
755 0,
756 &Size,
760 if (NT_SUCCESS(Status))
761 { // Unexpected, cleanup.
764 }
765
766 // 0x10000000: Free.
767
768 /* Reserve memory below 0x10000000 */
769 BaseAddress = UlongToPtr(0x10000000 - 0x1000);
770 Size = 0x1000;
773 0,
774 &Size,
780
781 /* Reserve memory at 0x10000000:
782 * Windows new default non-OS DLL image base */
783 BaseAddress = UlongToPtr(0x10000000);
784 Size = 0x1000;
787 0,
788 &Size,
794 }
795}
#define GetNTVersion()
Definition: apitest.h:17
#define StartSeh()
Definition: apitest.h:93
#define EndSeh(ExpectedStatus)
Definition: apitest.h:99
#define skip(...)
Definition: atltest.h:64
#define USER_SHARED_DATA
Definition: pstypes.h:51
#define STATUS_INVALID_PARAMETER_2
Definition: ntstatus.h:570
#define STATUS_INVALID_PAGE_PROTECTION
Definition: ntstatus.h:399
#define _WIN32_WINNT_WIN10
Definition: sdkddkver.h:32
#define _WIN32_WINNT_VISTA
Definition: sdkddkver.h:25

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:419

◆ 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 799 of file NtAllocateVirtualMemory.c.

800{
801 PVOID Mem1, Mem2;
802 SIZE_T Size1, Size2;
803 ULONG i;
804
808
809 Size1 = 32;
810 Mem1 = Allocate(Size1);
811 AccessMemory1(Mem1, Size1);
812 Size2 = 128;
813 Mem2 = Allocate(Size2);
814 AccessMemory2(Mem2, Size2);
815 for (i = 0; i < RUNS; ++i)
816 {
817 PVOID New;
818 ok(CheckMemory1(Mem1, Size1) == TRUE, "CheckMemory1 failure\n");
819 New = ReAllocate(Mem1, Size1 * 3 / 2);
820 if (New == NULL)
821 {
822 skip("Realloc failure\n");
823 break;
824 }
825 Mem1 = New;
826 ok(CheckMemory1(Mem1, Size1) == TRUE, "CheckMemory1 failure\n");
827 Size1 = Size1 * 3 / 2;
828 AccessMemory1(Mem1, Size1);
829
830 ok(CheckMemory2(Mem2, Size2) == TRUE, "CheckMemory2 failure\n");
831 New = ReAllocate(Mem2, Size2 + 128);
832 if (New == NULL)
833 {
834 skip("Realloc failure\n");
835 break;
836 }
837 Mem2 = New;
838 ok(CheckMemory2(Mem2, Size2) == TRUE, "CheckMemory2 failure\n");
839 Size2 += 128;
840 AccessMemory2(Mem2, Size2);
841 }
842 ok(CheckMemory2(Mem2, Size2) == TRUE, "CheckMemory2 failure\n");
843 Free(Mem2);
844 ok(CheckMemory1(Mem1, Size1) == TRUE, "CheckMemory1 failure\n");
845 Free(Mem1);
846}
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)

◆ 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().