2 * PROJECT: ReactOS API Tests
3 * LICENSE: GPLv2+ - See COPYING in the top level directory
4 * PURPOSE: Stress Test for virtual memory allocation
5 * PROGRAMMER: Thomas Faber <thfabba@gmx.de>
8 #define WIN32_NO_STATUS
10 #include <wine/test.h>
11 #include <ndk/rtlfuncs.h>
12 #include <ndk/mmfuncs.h>
14 static PVOID Allocations
[4096] = { NULL
};
15 static ULONG CurrentAllocation
= 0;
19 ValidateAllocations(VOID
)
23 ASSERT(CurrentAllocation
< sizeof(Allocations
) / sizeof(Allocations
[0]));
24 for (i
= 0; i
< CurrentAllocation
; ++i
)
26 PUCHAR UserBuffer
= Allocations
[i
];
27 SIZE_T AllocationSize
;
30 if (UserBuffer
== NULL
)
33 AllocationSize
= ((PSIZE_T
)UserBuffer
)[-2];
34 DataSize
= ((PSIZE_T
)UserBuffer
)[-1];
35 ASSERT(DataSize
!= 0);
36 ASSERT(((SIZE_T
)UserBuffer
+ DataSize
) % PAGE_SIZE
== 0);
46 PVOID AllocationStart
= NULL
;
47 SIZE_T AllocationSize
= PAGE_ROUND_UP(DataSize
+ PAGE_SIZE
+ 2 * sizeof(SIZE_T
));
49 SIZE_T NumberOfPages
= AllocationSize
/ PAGE_SIZE
;
53 Status
= NtAllocateVirtualMemory(NtCurrentProcess(), &AllocationStart
, 0, &AllocationSize
, MEM_RESERVE
, PAGE_NOACCESS
);
55 if (!NT_SUCCESS(Status
))
58 FirstPageStart
= (PUCHAR
)AllocationStart
+ AllocationSize
- PAGE_SIZE
* NumberOfPages
;
59 Size
= (NumberOfPages
- 1) * PAGE_SIZE
;
60 Status
= NtAllocateVirtualMemory(NtCurrentProcess(), &FirstPageStart
, 0, &Size
, MEM_COMMIT
, PAGE_READWRITE
);
61 if (!NT_SUCCESS(Status
))
64 Status
= NtFreeVirtualMemory(NtCurrentProcess(), &AllocationStart
, &AllocationSize
, MEM_RELEASE
);
65 ASSERT(Status
== STATUS_SUCCESS
);
68 ASSERT(Size
% sizeof(ULONG
) == 0);
69 ASSERT(RtlCompareMemoryUlong(FirstPageStart
, Size
, 0) == Size
);
71 UserBuffer
= AllocationStart
;
72 UserBuffer
+= AllocationSize
- PAGE_SIZE
- DataSize
;
73 RtlFillMemory(FirstPageStart
, UserBuffer
- (PUCHAR
)FirstPageStart
, 0xae);
74 RtlZeroMemory(UserBuffer
, DataSize
);
75 ((PSIZE_T
)UserBuffer
)[-2] = AllocationSize
;
76 ((PSIZE_T
)UserBuffer
)[-1] = DataSize
;
78 Allocations
[CurrentAllocation
++] = UserBuffer
;
79 ValidateAllocations();
89 PVOID AllocationStart
;
91 SIZE_T AllocationSize
;
95 AllocationSize
= ((PSIZE_T
)UserBuffer
)[-2];
96 DataSize
= ((PSIZE_T
)UserBuffer
)[-1];
97 ASSERT(DataSize
!= 0);
99 AllocationStart
= (PUCHAR
)UserBuffer
+ DataSize
+ PAGE_SIZE
- AllocationSize
;
100 ASSERT((SIZE_T
)AllocationStart
% PAGE_SIZE
== 0);
102 RtlFillMemory(UserBuffer
, DataSize
, 0xbe);
103 ((PSIZE_T
)UserBuffer
)[-1] = 0;
104 ((PSIZE_T
)UserBuffer
)[-2] = 0xFAFBFCFD;
106 for (i
= 0; i
< CurrentAllocation
; ++i
)
107 if (Allocations
[i
] == UserBuffer
)
109 Allocations
[i
] = NULL
;
112 ValidateAllocations();
114 Status
= NtFreeVirtualMemory(NtCurrentProcess(), &AllocationStart
, &Zero
, MEM_RELEASE
);
115 ASSERT(Status
== STATUS_SUCCESS
);
127 OldDataSize
= ((PSIZE_T
)OldUserBuffer
)[-1];
128 ASSERT(OldDataSize
!= 0);
130 NewUserBuffer
= Allocate(NewDataSize
);
131 ASSERT(((PSIZE_T
)OldUserBuffer
)[-1] == OldDataSize
);
132 RtlCopyMemory(NewUserBuffer
, OldUserBuffer
, min(OldDataSize
, NewDataSize
));
133 ASSERT(((PSIZE_T
)OldUserBuffer
)[-1] == OldDataSize
);
135 return NewUserBuffer
;
144 PBYTE Buffer
= UserBuffer
;
147 for (i
= 0; i
< DataSize
; ++i
)
148 Buffer
[i
] = LOBYTE(i
);
157 PBYTE Buffer
= UserBuffer
;
160 for (i
= 0; i
< DataSize
; ++i
)
161 if (Buffer
[i
] != LOBYTE(i
))
163 trace("Mismatch in region %p at index %lu. Value=%02x\n", UserBuffer
, (ULONG
)i
, Buffer
[i
]);
176 PBYTE Buffer
= UserBuffer
;
179 for (i
= 0; i
< DataSize
; ++i
)
180 Buffer
[i
] = UCHAR_MAX
- LOBYTE(i
);
189 PBYTE Buffer
= UserBuffer
;
192 for (i
= 0; i
< DataSize
; ++i
)
193 if (Buffer
[i
] != UCHAR_MAX
- LOBYTE(i
))
195 trace("Mismatch in region %p at index %lu. Value=%02x\n", UserBuffer
, (ULONG
)i
, Buffer
[i
]);
204 START_TEST(NtAllocateVirtualMemory
)
211 Mem1
= Allocate(Size1
);
212 AccessMemory1(Mem1
, Size1
);
214 Mem2
= Allocate(Size2
);
215 AccessMemory2(Mem2
, Size2
);
216 for (i
= 0; i
< RUNS
; ++i
)
219 ok(CheckMemory1(Mem1
, Size1
) == TRUE
, "CheckMemory1 failure\n");
220 New
= ReAllocate(Mem1
, Size1
* 3 / 2);
223 skip("Realloc failure\n");
227 ok(CheckMemory1(Mem1
, Size1
) == TRUE
, "CheckMemory1 failure\n");
228 Size1
= Size1
* 3 / 2;
229 AccessMemory1(Mem1
, Size1
);
231 ok(CheckMemory2(Mem2
, Size2
) == TRUE
, "CheckMemory2 failure\n");
232 New
= ReAllocate(Mem2
, Size2
+ 128);
235 skip("Realloc failure\n");
239 ok(CheckMemory2(Mem2
, Size2
) == TRUE
, "CheckMemory2 failure\n");
241 AccessMemory2(Mem2
, Size2
);
243 ok(CheckMemory2(Mem2
, Size2
) == TRUE
, "CheckMemory2 failure\n");
245 ok(CheckMemory1(Mem1
, Size1
) == TRUE
, "CheckMemory1 failure\n");