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
);
69 UserBuffer
= AllocationStart
;
70 UserBuffer
+= AllocationSize
- PAGE_SIZE
- DataSize
;
71 RtlFillMemory(FirstPageStart
, UserBuffer
- (PUCHAR
)FirstPageStart
, 0xae);
72 RtlZeroMemory(UserBuffer
, DataSize
);
73 ((PSIZE_T
)UserBuffer
)[-2] = AllocationSize
;
74 ((PSIZE_T
)UserBuffer
)[-1] = DataSize
;
76 Allocations
[CurrentAllocation
++] = UserBuffer
;
77 ValidateAllocations();
87 PVOID AllocationStart
;
89 SIZE_T AllocationSize
;
93 AllocationSize
= ((PSIZE_T
)UserBuffer
)[-2];
94 DataSize
= ((PSIZE_T
)UserBuffer
)[-1];
95 ASSERT(DataSize
!= 0);
97 AllocationStart
= (PUCHAR
)UserBuffer
+ DataSize
+ PAGE_SIZE
- AllocationSize
;
98 ASSERT((SIZE_T
)AllocationStart
% PAGE_SIZE
== 0);
100 RtlFillMemory(UserBuffer
, DataSize
, 0xbe);
101 ((PSIZE_T
)UserBuffer
)[-1] = 0;
102 ((PSIZE_T
)UserBuffer
)[-2] = 0xFAFBFCFD;
104 for (i
= 0; i
< CurrentAllocation
; ++i
)
105 if (Allocations
[i
] == UserBuffer
)
107 Allocations
[i
] = NULL
;
110 ValidateAllocations();
112 Status
= NtFreeVirtualMemory(NtCurrentProcess(), &AllocationStart
, &Zero
, MEM_RELEASE
);
113 ASSERT(Status
== STATUS_SUCCESS
);
125 OldDataSize
= ((PSIZE_T
)OldUserBuffer
)[-1];
126 ASSERT(OldDataSize
!= 0);
128 NewUserBuffer
= Allocate(NewDataSize
);
129 ASSERT(((PSIZE_T
)OldUserBuffer
)[-1] == OldDataSize
);
130 RtlCopyMemory(NewUserBuffer
, OldUserBuffer
, min(OldDataSize
, NewDataSize
));
131 ASSERT(((PSIZE_T
)OldUserBuffer
)[-1] == OldDataSize
);
133 return NewUserBuffer
;
142 PBYTE Buffer
= UserBuffer
;
145 for (i
= 0; i
< DataSize
; ++i
)
146 Buffer
[i
] = LOBYTE(i
);
155 PBYTE Buffer
= UserBuffer
;
158 for (i
= 0; i
< DataSize
; ++i
)
159 if (Buffer
[i
] != LOBYTE(i
))
161 trace("Mismatch in region %p at index %lu. Value=%02x\n", UserBuffer
, (ULONG
)i
, Buffer
[i
]);
174 PBYTE Buffer
= UserBuffer
;
177 for (i
= 0; i
< DataSize
; ++i
)
178 Buffer
[i
] = UCHAR_MAX
- LOBYTE(i
);
187 PBYTE Buffer
= UserBuffer
;
190 for (i
= 0; i
< DataSize
; ++i
)
191 if (Buffer
[i
] != UCHAR_MAX
- LOBYTE(i
))
193 trace("Mismatch in region %p at index %lu. Value=%02x\n", UserBuffer
, (ULONG
)i
, Buffer
[i
]);
202 START_TEST(NtAllocateVirtualMemory
)
209 Mem1
= Allocate(Size1
);
210 AccessMemory1(Mem1
, Size1
);
212 Mem2
= Allocate(Size2
);
213 AccessMemory2(Mem2
, Size2
);
214 for (i
= 0; i
< RUNS
; ++i
)
217 ok(CheckMemory1(Mem1
, Size1
) == TRUE
, "CheckMemory1 failure\n");
218 New
= ReAllocate(Mem1
, Size1
* 3 / 2);
221 skip("Realloc failure\n");
225 ok(CheckMemory1(Mem1
, Size1
) == TRUE
, "CheckMemory1 failure\n");
226 Size1
= Size1
* 3 / 2;
227 AccessMemory1(Mem1
, Size1
);
229 ok(CheckMemory2(Mem2
, Size2
) == TRUE
, "CheckMemory2 failure\n");
230 New
= ReAllocate(Mem2
, Size2
+ 128);
233 skip("Realloc failure\n");
237 ok(CheckMemory2(Mem2
, Size2
) == TRUE
, "CheckMemory2 failure\n");
239 AccessMemory2(Mem2
, Size2
);
241 ok(CheckMemory2(Mem2
, Size2
) == TRUE
, "CheckMemory2 failure\n");
243 ok(CheckMemory1(Mem1
, Size1
) == TRUE
, "CheckMemory1 failure\n");