2 * Unit test suite for heap functions
4 * Copyright 2002 Geoffrey Hausheer
5 * Copyright 2003 Dimitrie O. Paun
6 * Copyright 2006 Detlef Riekenberg
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
31 #include "wine/test.h"
33 #define MAGIC_DEAD 0xdeadbeef
35 /* some undocumented flags (names are made up) */
36 #define HEAP_PAGE_ALLOCS 0x01000000
37 #define HEAP_VALIDATE 0x10000000
38 #define HEAP_VALIDATE_ALL 0x20000000
39 #define HEAP_VALIDATE_PARAMS 0x40000000
41 static BOOL (WINAPI
*pHeapQueryInformation
)(HANDLE
, HEAP_INFORMATION_CLASS
, PVOID
, SIZE_T
, PSIZE_T
);
42 static BOOL (WINAPI
*pGetPhysicallyInstalledSystemMemory
)(ULONGLONG
*);
43 static ULONG (WINAPI
*pRtlGetNtGlobalFlags
)(void);
53 static SIZE_T
resize_9x(SIZE_T size
)
55 DWORD dwSizeAligned
= (size
+ 3) & ~3;
56 return max(dwSizeAligned
, 12); /* at least 12 bytes */
59 static void test_sized_HeapAlloc(int nbytes
)
62 char *buf
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, nbytes
);
63 ok(buf
!= NULL
, "allocate failed\n");
64 ok(buf
[0] == 0, "buffer not zeroed\n");
65 success
= HeapFree(GetProcessHeap(), 0, buf
);
66 ok(success
, "free failed\n");
69 static void test_sized_HeapReAlloc(int nbytes1
, int nbytes2
)
72 char *buf
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, nbytes1
);
73 ok(buf
!= NULL
, "allocate failed\n");
74 ok(buf
[0] == 0, "buffer not zeroed\n");
75 buf
= HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, buf
, nbytes2
);
76 ok(buf
!= NULL
, "reallocate failed\n");
77 ok(buf
[nbytes2
-1] == 0, "buffer not zeroed\n");
78 success
= HeapFree(GetProcessHeap(), 0, buf
);
79 ok(success
, "free failed\n");
82 static void test_heap(void)
91 const SIZE_T max_size
= 1024, init_size
= 10;
93 /* Heap*() functions */
94 mem
= HeapAlloc(GetProcessHeap(), 0, 0);
95 ok(mem
!= NULL
, "memory not allocated for size 0\n");
96 HeapFree(GetProcessHeap(), 0, mem
);
98 mem
= HeapReAlloc(GetProcessHeap(), 0, NULL
, 10);
99 ok(mem
== NULL
, "memory allocated by HeapReAlloc\n");
101 for (size
= 0; size
<= 256; size
++)
104 mem
= HeapAlloc(GetProcessHeap(), 0, size
);
105 heap_size
= HeapSize(GetProcessHeap(), 0, mem
);
106 ok(heap_size
== size
|| heap_size
== resize_9x(size
),
107 "HeapSize returned %lu instead of %lu or %lu\n", heap_size
, size
, resize_9x(size
));
108 HeapFree(GetProcessHeap(), 0, mem
);
111 /* test some border cases of HeapAlloc and HeapReAlloc */
112 mem
= HeapAlloc(GetProcessHeap(), 0, 0);
113 ok(mem
!= NULL
, "memory not allocated for size 0\n");
114 msecond
= HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, mem
, ~(SIZE_T
)0 - 7);
115 ok(msecond
== NULL
, "HeapReAlloc(~0 - 7) should have failed\n");
116 msecond
= HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, mem
, ~(SIZE_T
)0);
117 ok(msecond
== NULL
, "HeapReAlloc(~0) should have failed\n");
118 HeapFree(GetProcessHeap(), 0, mem
);
119 mem
= HeapAlloc(GetProcessHeap(), 0, ~(SIZE_T
)0);
120 ok(mem
== NULL
, "memory allocated for size ~0\n");
122 /* large blocks must be 16-byte aligned */
123 mem
= HeapAlloc(GetProcessHeap(), 0, 512 * 1024);
124 ok( mem
!= NULL
, "failed for size 512K\n" );
125 ok( (ULONG_PTR
)mem
% 16 == 0 || broken((ULONG_PTR
)mem
% 16) /* win9x */,
126 "512K block not 16-byte aligned\n" );
127 HeapFree(GetProcessHeap(), 0, mem
);
129 /* Global*() functions */
130 gbl
= GlobalAlloc(GMEM_MOVEABLE
, 0);
131 ok(gbl
!= NULL
, "global memory not allocated for size 0\n");
133 gbl
= GlobalReAlloc(gbl
, 10, GMEM_MOVEABLE
);
134 ok(gbl
!= NULL
, "Can't realloc global memory\n");
135 size
= GlobalSize(gbl
);
136 ok(size
>= 10 && size
<= 16, "Memory not resized to size 10, instead size=%ld\n", size
);
138 gbl
= GlobalReAlloc(gbl
, 0, GMEM_MOVEABLE
);
139 ok(gbl
!= NULL
, "GlobalReAlloc should not fail on size 0\n");
141 size
= GlobalSize(gbl
);
142 ok(size
== 0, "Memory not resized to size 0, instead size=%ld\n", size
);
143 ok(GlobalFree(gbl
) == NULL
, "Memory not freed\n");
144 size
= GlobalSize(gbl
);
145 ok(size
== 0, "Memory should have been freed, size=%ld\n", size
);
147 gbl
= GlobalReAlloc(0, 10, GMEM_MOVEABLE
);
148 ok(gbl
== NULL
, "global realloc allocated memory\n");
150 /* GlobalLock / GlobalUnlock with a valid handle */
151 gbl
= GlobalAlloc(GMEM_MOVEABLE
, 256);
153 SetLastError(MAGIC_DEAD
);
154 mem
= GlobalLock(gbl
); /* #1 */
155 ok(mem
!= NULL
, "returned %p with %d (expected '!= NULL')\n", mem
, GetLastError());
156 SetLastError(MAGIC_DEAD
);
157 flags
= GlobalFlags(gbl
);
158 ok( flags
== 1, "returned 0x%04x with %d (expected '0x0001')\n",
159 flags
, GetLastError());
161 SetLastError(MAGIC_DEAD
);
162 msecond
= GlobalLock(gbl
); /* #2 */
163 ok( msecond
== mem
, "returned %p with %d (expected '%p')\n",
164 msecond
, GetLastError(), mem
);
165 SetLastError(MAGIC_DEAD
);
166 flags
= GlobalFlags(gbl
);
167 ok( flags
== 2, "returned 0x%04x with %d (expected '0x0002')\n",
168 flags
, GetLastError());
169 SetLastError(MAGIC_DEAD
);
171 SetLastError(MAGIC_DEAD
);
172 res
= GlobalUnlock(gbl
); /* #1 */
173 ok(res
, "returned %d with %d (expected '!= 0')\n", res
, GetLastError());
174 SetLastError(MAGIC_DEAD
);
175 flags
= GlobalFlags(gbl
);
176 ok( flags
, "returned 0x%04x with %d (expected '!= 0')\n",
177 flags
, GetLastError());
179 SetLastError(MAGIC_DEAD
);
180 res
= GlobalUnlock(gbl
); /* #0 */
181 /* NT: ERROR_SUCCESS (documented on MSDN), 9x: untouched */
182 ok(!res
&& ((GetLastError() == ERROR_SUCCESS
) || (GetLastError() == MAGIC_DEAD
)),
183 "returned %d with %d (expected '0' with: ERROR_SUCCESS or "
184 "MAGIC_DEAD)\n", res
, GetLastError());
185 SetLastError(MAGIC_DEAD
);
186 flags
= GlobalFlags(gbl
);
187 ok( !flags
, "returned 0x%04x with %d (expected '0')\n",
188 flags
, GetLastError());
190 /* Unlock an already unlocked Handle */
191 SetLastError(MAGIC_DEAD
);
192 res
= GlobalUnlock(gbl
);
193 /* NT: ERROR_NOT_LOCKED, 9x: untouched */
195 ((GetLastError() == ERROR_NOT_LOCKED
) || (GetLastError() == MAGIC_DEAD
)),
196 "returned %d with %d (expected '0' with: ERROR_NOT_LOCKED or "
197 "MAGIC_DEAD)\n", res
, GetLastError());
200 /* invalid handles are caught in windows: */
201 SetLastError(MAGIC_DEAD
);
202 hsecond
= GlobalFree(gbl
); /* invalid handle: free memory twice */
203 ok( (hsecond
== gbl
) && (GetLastError() == ERROR_INVALID_HANDLE
),
204 "returned %p with 0x%08x (expected %p with ERROR_INVALID_HANDLE)\n",
205 hsecond
, GetLastError(), gbl
);
206 SetLastError(MAGIC_DEAD
);
207 flags
= GlobalFlags(gbl
);
208 ok( (flags
== GMEM_INVALID_HANDLE
) && (GetLastError() == ERROR_INVALID_HANDLE
),
209 "returned 0x%04x with 0x%08x (expected GMEM_INVALID_HANDLE with "
210 "ERROR_INVALID_HANDLE)\n", flags
, GetLastError());
211 SetLastError(MAGIC_DEAD
);
212 size
= GlobalSize(gbl
);
213 ok( (size
== 0) && (GetLastError() == ERROR_INVALID_HANDLE
),
214 "returned %ld with 0x%08x (expected '0' with ERROR_INVALID_HANDLE)\n",
215 size
, GetLastError());
217 SetLastError(MAGIC_DEAD
);
218 mem
= GlobalLock(gbl
);
219 ok( (mem
== NULL
) && (GetLastError() == ERROR_INVALID_HANDLE
),
220 "returned %p with 0x%08x (expected NULL with ERROR_INVALID_HANDLE)\n",
221 mem
, GetLastError());
223 /* documented on MSDN: GlobalUnlock() return FALSE on failure.
224 Win9x and wine return FALSE with ERROR_INVALID_HANDLE, but on
225 NT 3.51 and XPsp2, TRUE with ERROR_INVALID_HANDLE is returned.
226 The similar Test for LocalUnlock() works on all Systems */
227 SetLastError(MAGIC_DEAD
);
228 res
= GlobalUnlock(gbl
);
229 ok(GetLastError() == ERROR_INVALID_HANDLE
,
230 "returned %d with %d (expected ERROR_INVALID_HANDLE)\n",
231 res
, GetLastError());
233 gbl
= GlobalAlloc(GMEM_DDESHARE
, 100);
236 mem
= GlobalFree(gbl
);
237 ok(mem
== NULL
, "Expected NULL, got %p\n", mem
);
240 if (sizeof(void *) != 8) /* crashes on 64-bit Vista */
242 SetLastError(MAGIC_DEAD
);
243 mem
= GlobalFree(gbl
);
244 ok(mem
== gbl
|| broken(mem
== NULL
) /* nt4 */, "Expected gbl, got %p\n", mem
);
246 ok(GetLastError() == ERROR_INVALID_HANDLE
||
247 GetLastError() == ERROR_INVALID_PARAMETER
, /* win9x */
248 "Expected ERROR_INVALID_HANDLE or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
251 /* GMEM_FIXED block expands in place only without flags */
252 for (size
= 1; size
<= max_size
; size
<<= 1) {
253 gbl
= GlobalAlloc(GMEM_FIXED
, init_size
);
254 SetLastError(MAGIC_DEAD
);
255 hsecond
= GlobalReAlloc(gbl
, size
+ init_size
, 0);
256 ok(hsecond
== gbl
|| (hsecond
== NULL
&& GetLastError() == ERROR_NOT_ENOUGH_MEMORY
),
257 "got %p with %x (expected %p or NULL) @%ld\n", hsecond
, GetLastError(), gbl
, size
);
261 /* GMEM_FIXED block can be relocated with GMEM_MOVEABLE */
262 for (size
= 1; size
<= max_size
; size
<<= 1) {
263 gbl
= GlobalAlloc(GMEM_FIXED
, init_size
);
264 SetLastError(MAGIC_DEAD
);
265 hsecond
= GlobalReAlloc(gbl
, size
+ init_size
, GMEM_MOVEABLE
);
267 "got %p with %x (expected non-NULL) @%ld\n", hsecond
, GetLastError(), size
);
268 mem
= GlobalLock(hsecond
);
269 ok(mem
== hsecond
, "got %p (expected %p) @%ld\n", mem
, hsecond
, size
);
273 gbl
= GlobalAlloc(GMEM_DDESHARE
, 100);
275 res
= GlobalUnlock(gbl
);
277 broken(res
== 0), /* win9x */
278 "Expected 1 or 0, got %d\n", res
);
280 res
= GlobalUnlock(gbl
);
282 broken(res
== 0), /* win9x */
283 "Expected 1 or 0, got %d\n", res
);
287 gbl
= GlobalAlloc(GMEM_FIXED
, 100);
289 SetLastError(0xdeadbeef);
290 res
= GlobalUnlock(gbl
);
292 broken(res
== 0), /* win9x */
293 "Expected 1 or 0, got %d\n", res
);
294 ok(GetLastError() == 0xdeadbeef, "got %d\n", GetLastError());
298 /* GlobalSize on an invalid handle */
299 if (sizeof(void *) != 8) /* crashes on 64-bit Vista */
301 SetLastError(MAGIC_DEAD
);
302 size
= GlobalSize((HGLOBAL
)0xc042);
303 ok(size
== 0, "Expected 0, got %ld\n", size
);
304 ok(GetLastError() == ERROR_INVALID_HANDLE
||
305 GetLastError() == ERROR_INVALID_PARAMETER
, /* win9x */
306 "Expected ERROR_INVALID_HANDLE or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
309 /* ####################################### */
310 /* Local*() functions */
311 gbl
= LocalAlloc(LMEM_MOVEABLE
, 0);
312 ok(gbl
!= NULL
, "local memory not allocated for size 0\n");
314 gbl
= LocalReAlloc(gbl
, 10, LMEM_MOVEABLE
);
315 ok(gbl
!= NULL
, "Can't realloc local memory\n");
316 size
= LocalSize(gbl
);
317 ok(size
>= 10 && size
<= 16, "Memory not resized to size 10, instead size=%ld\n", size
);
319 gbl
= LocalReAlloc(gbl
, 0, LMEM_MOVEABLE
);
320 ok(gbl
!= NULL
, "LocalReAlloc should not fail on size 0\n");
322 size
= LocalSize(gbl
);
323 ok(size
== 0, "Memory not resized to size 0, instead size=%ld\n", size
);
324 ok(LocalFree(gbl
) == NULL
, "Memory not freed\n");
325 size
= LocalSize(gbl
);
326 ok(size
== 0, "Memory should have been freed, size=%ld\n", size
);
328 gbl
= LocalReAlloc(0, 10, LMEM_MOVEABLE
);
329 ok(gbl
== NULL
, "local realloc allocated memory\n");
331 /* LocalLock / LocalUnlock with a valid handle */
332 gbl
= LocalAlloc(LMEM_MOVEABLE
, 256);
333 SetLastError(MAGIC_DEAD
);
334 mem
= LocalLock(gbl
); /* #1 */
335 ok(mem
!= NULL
, "returned %p with %d (expected '!= NULL')\n", mem
, GetLastError());
336 SetLastError(MAGIC_DEAD
);
337 flags
= LocalFlags(gbl
);
338 ok( flags
== 1, "returned 0x%04x with %d (expected '0x0001')\n",
339 flags
, GetLastError());
341 SetLastError(MAGIC_DEAD
);
342 msecond
= LocalLock(gbl
); /* #2 */
343 ok( msecond
== mem
, "returned %p with %d (expected '%p')\n",
344 msecond
, GetLastError(), mem
);
345 SetLastError(MAGIC_DEAD
);
346 flags
= LocalFlags(gbl
);
347 ok( flags
== 2, "returned 0x%04x with %d (expected '0x0002')\n",
348 flags
, GetLastError());
349 SetLastError(MAGIC_DEAD
);
351 SetLastError(MAGIC_DEAD
);
352 res
= LocalUnlock(gbl
); /* #1 */
353 ok(res
, "returned %d with %d (expected '!= 0')\n", res
, GetLastError());
354 SetLastError(MAGIC_DEAD
);
355 flags
= LocalFlags(gbl
);
356 ok( flags
, "returned 0x%04x with %d (expected '!= 0')\n",
357 flags
, GetLastError());
359 SetLastError(MAGIC_DEAD
);
360 res
= LocalUnlock(gbl
); /* #0 */
361 /* NT: ERROR_SUCCESS (documented on MSDN), 9x: untouched */
362 ok(!res
&& ((GetLastError() == ERROR_SUCCESS
) || (GetLastError() == MAGIC_DEAD
)),
363 "returned %d with %d (expected '0' with: ERROR_SUCCESS or "
364 "MAGIC_DEAD)\n", res
, GetLastError());
365 SetLastError(MAGIC_DEAD
);
366 flags
= LocalFlags(gbl
);
367 ok( !flags
, "returned 0x%04x with %d (expected '0')\n",
368 flags
, GetLastError());
370 /* Unlock an already unlocked Handle */
371 SetLastError(MAGIC_DEAD
);
372 res
= LocalUnlock(gbl
);
373 /* NT: ERROR_NOT_LOCKED, 9x: untouched */
375 ((GetLastError() == ERROR_NOT_LOCKED
) || (GetLastError() == MAGIC_DEAD
)),
376 "returned %d with %d (expected '0' with: ERROR_NOT_LOCKED or "
377 "MAGIC_DEAD)\n", res
, GetLastError());
380 /* invalid handles are caught in windows: */
381 SetLastError(MAGIC_DEAD
);
382 hsecond
= LocalFree(gbl
); /* invalid handle: free memory twice */
383 ok( (hsecond
== gbl
) && (GetLastError() == ERROR_INVALID_HANDLE
),
384 "returned %p with 0x%08x (expected %p with ERROR_INVALID_HANDLE)\n",
385 hsecond
, GetLastError(), gbl
);
386 SetLastError(MAGIC_DEAD
);
387 flags
= LocalFlags(gbl
);
388 ok( (flags
== LMEM_INVALID_HANDLE
) && (GetLastError() == ERROR_INVALID_HANDLE
),
389 "returned 0x%04x with 0x%08x (expected LMEM_INVALID_HANDLE with "
390 "ERROR_INVALID_HANDLE)\n", flags
, GetLastError());
391 SetLastError(MAGIC_DEAD
);
392 size
= LocalSize(gbl
);
393 ok( (size
== 0) && (GetLastError() == ERROR_INVALID_HANDLE
),
394 "returned %ld with 0x%08x (expected '0' with ERROR_INVALID_HANDLE)\n",
395 size
, GetLastError());
397 SetLastError(MAGIC_DEAD
);
398 mem
= LocalLock(gbl
);
399 ok( (mem
== NULL
) && (GetLastError() == ERROR_INVALID_HANDLE
),
400 "returned %p with 0x%08x (expected NULL with ERROR_INVALID_HANDLE)\n",
401 mem
, GetLastError());
403 /* This Test works the same on all Systems (GlobalUnlock() is different) */
404 SetLastError(MAGIC_DEAD
);
405 res
= LocalUnlock(gbl
);
406 ok(!res
&& (GetLastError() == ERROR_INVALID_HANDLE
),
407 "returned %d with %d (expected '0' with ERROR_INVALID_HANDLE)\n",
408 res
, GetLastError());
410 /* LMEM_FIXED block expands in place only without flags */
411 for (size
= 1; size
<= max_size
; size
<<= 1) {
412 gbl
= LocalAlloc(LMEM_FIXED
, init_size
);
413 SetLastError(MAGIC_DEAD
);
414 hsecond
= LocalReAlloc(gbl
, size
+ init_size
, 0);
415 ok(hsecond
== gbl
|| (hsecond
== NULL
&& GetLastError() == ERROR_NOT_ENOUGH_MEMORY
),
416 "got %p with %x (expected %p or NULL) @%ld\n", hsecond
, GetLastError(), gbl
, size
);
420 /* LMEM_FIXED memory can be relocated with LMEM_MOVEABLE */
421 for (size
= 1; size
<= max_size
; size
<<= 1) {
422 gbl
= LocalAlloc(LMEM_FIXED
, init_size
);
423 SetLastError(MAGIC_DEAD
);
424 hsecond
= LocalReAlloc(gbl
, size
+ init_size
, LMEM_MOVEABLE
);
426 "got %p with %x (expected non-NULL) @%ld\n", hsecond
, GetLastError(), size
);
427 mem
= LocalLock(hsecond
);
428 ok(mem
== hsecond
, "got %p (expected %p) @%ld\n", mem
, hsecond
, size
);
432 /* trying to unlock pointer from LocalAlloc */
433 gbl
= LocalAlloc(LMEM_FIXED
, 100);
434 SetLastError(0xdeadbeef);
435 res
= LocalUnlock(gbl
);
436 ok(res
== 0, "Expected 0, got %d\n", res
);
437 ok(GetLastError() == ERROR_NOT_LOCKED
||
438 broken(GetLastError() == 0xdeadbeef) /* win9x */, "got %d\n", GetLastError());
441 /* trying to lock empty memory should give an error */
442 gbl
= GlobalAlloc(GMEM_MOVEABLE
|GMEM_ZEROINIT
,0);
443 ok(gbl
!= NULL
, "returned NULL\n");
444 SetLastError(MAGIC_DEAD
);
445 mem
= GlobalLock(gbl
);
446 /* NT: ERROR_DISCARDED, 9x: untouched */
448 ((GetLastError() == ERROR_DISCARDED
) || (GetLastError() == MAGIC_DEAD
)),
449 "returned %p with 0x%x/%d (expected 'NULL' with: ERROR_DISCARDED or "
450 "MAGIC_DEAD)\n", mem
, GetLastError(), GetLastError());
454 /* trying to get size from data pointer (GMEM_MOVEABLE) */
455 gbl
= GlobalAlloc(GMEM_MOVEABLE
, 0x123);
456 ok(gbl
!= NULL
, "returned NULL\n");
457 mem
= GlobalLock(gbl
);
458 ok(mem
!= NULL
, "returned NULL.\n");
459 ok(gbl
!= mem
, "unexpectedly equal.\n");
461 size
= GlobalSize(gbl
);
462 size2
= GlobalSize(mem
);
463 ok(size
== 0x123, "got %lu\n", size
);
464 ok(size2
== 0x123, "got %lu\n", size2
);
468 /* trying to get size from data pointer (GMEM_FIXED) */
469 gbl
= GlobalAlloc(GMEM_FIXED
, 0x123);
470 ok(gbl
!= NULL
, "returned NULL\n");
471 mem
= GlobalLock(gbl
);
472 ok(mem
!= NULL
, "returned NULL.\n");
473 ok(gbl
== mem
, "got %p, %p.\n", gbl
, mem
);
475 size
= GlobalSize(gbl
);
476 ok(size
== 0x123, "got %lu\n", size
);
480 size
= GlobalSize((void *)0xdeadbee0);
481 ok(size
== 0, "got %lu\n", size
);
486 static void test_HeapCreate(void)
491 LPVOID mem1
,mem1a
,mem3
;
497 /* Retrieve the page size for this system */
498 GetSystemInfo(&sysInfo
);
499 ok(sysInfo
.dwPageSize
>0,"GetSystemInfo should return a valid page size\n");
501 /* Create a Heap with a minimum and maximum size */
502 /* Note that Windows and Wine seem to behave a bit differently with respect
503 to memory allocation. In Windows, you can't access all the memory
504 specified in the heap (due to overhead), so choosing a reasonable maximum
505 size for the heap was done mostly by trial-and-error on Win2k. It may need
506 more tweaking for otherWindows variants.
508 memchunk
=10*sysInfo
.dwPageSize
;
509 heap
=HeapCreate(0,2*memchunk
,5*memchunk
);
510 ok( !((ULONG_PTR
)heap
& 0xffff), "heap %p not 64K aligned\n", heap
);
512 /* Check that HeapCreate allocated the right amount of ram */
513 mem1
=HeapAlloc(heap
,0,5*memchunk
+1);
514 ok(mem1
==NULL
,"HeapCreate allocated more Ram than it should have\n");
515 HeapFree(heap
,0,mem1
);
517 /* Check that a normal alloc works */
518 mem1
=HeapAlloc(heap
,0,memchunk
);
519 ok(mem1
!=NULL
,"HeapAlloc failed\n");
521 ok(HeapSize(heap
,0,mem1
)>=memchunk
, "HeapAlloc should return a big enough memory block\n");
524 /* Check that a 'zeroing' alloc works */
525 mem2
=HeapAlloc(heap
,HEAP_ZERO_MEMORY
,memchunk
);
526 ok(mem2
!=NULL
,"HeapAlloc failed\n");
528 ok(HeapSize(heap
,0,mem2
)>=memchunk
,"HeapAlloc should return a big enough memory block\n");
530 for(i
=0;i
<memchunk
;i
++) {
535 ok(!error
,"HeapAlloc should have zeroed out its allocated memory\n");
538 /* Check that HeapAlloc returns NULL when requested way too much memory */
539 mem3
=HeapAlloc(heap
,0,5*memchunk
);
540 ok(mem3
==NULL
,"HeapAlloc should return NULL\n");
542 ok(HeapFree(heap
,0,mem3
),"HeapFree didn't pass successfully\n");
545 /* Check that HeapRealloc works */
546 mem2a
=HeapReAlloc(heap
,HEAP_ZERO_MEMORY
,mem2
,memchunk
+5*sysInfo
.dwPageSize
);
547 ok(mem2a
!=NULL
,"HeapReAlloc failed\n");
549 ok(HeapSize(heap
,0,mem2a
)>=memchunk
+5*sysInfo
.dwPageSize
,"HeapReAlloc failed\n");
551 for(i
=0;i
<5*sysInfo
.dwPageSize
;i
++) {
552 if(mem2a
[memchunk
+i
]!=0) {
556 ok(!error
,"HeapReAlloc should have zeroed out its allocated memory\n");
559 /* Check that HeapRealloc honours HEAP_REALLOC_IN_PLACE_ONLY */
561 mem1a
=HeapReAlloc(heap
,HEAP_REALLOC_IN_PLACE_ONLY
,mem1
,memchunk
+sysInfo
.dwPageSize
);
567 ok(mem1a
==NULL
|| !error
,"HeapReAlloc didn't honour HEAP_REALLOC_IN_PLACE_ONLY\n");
569 /* Check that HeapFree works correctly */
571 ok(HeapFree(heap
,0,mem1a
),"HeapFree failed\n");
573 ok(HeapFree(heap
,0,mem1
),"HeapFree failed\n");
576 ok(HeapFree(heap
,0,mem2a
),"HeapFree failed\n");
578 ok(HeapFree(heap
,0,mem2
),"HeapFree failed\n");
581 /* 0-length buffer */
582 mem1
= HeapAlloc(heap
, 0, 0);
583 ok(mem1
!= NULL
, "Reserved memory\n");
585 dwSize
= HeapSize(heap
, 0, mem1
);
586 /* should work with 0-length buffer */
587 ok(dwSize
< 0xFFFFFFFF, "The size of the 0-length buffer\n");
588 ok(HeapFree(heap
, 0, mem1
), "Freed the 0-length buffer\n");
590 /* Check that HeapDestroy works */
591 ok(HeapDestroy(heap
),"HeapDestroy failed\n");
595 static void test_GlobalAlloc(void)
598 HGLOBAL mem1
,mem2
,mem2a
,mem2b
;
604 SetLastError(NO_ERROR
);
605 /* Check that a normal alloc works */
606 mem1
=GlobalAlloc(0,memchunk
);
607 ok(mem1
!=NULL
,"GlobalAlloc failed\n");
609 ok(GlobalSize(mem1
)>=memchunk
, "GlobalAlloc should return a big enough memory block\n");
612 /* Check that a 'zeroing' alloc works */
613 mem2
=GlobalAlloc(GMEM_ZEROINIT
,memchunk
);
614 ok(mem2
!=NULL
,"GlobalAlloc failed: error=%d\n",GetLastError());
616 ok(GlobalSize(mem2
)>=memchunk
,"GlobalAlloc should return a big enough memory block\n");
617 mem2ptr
=GlobalLock(mem2
);
618 ok(mem2ptr
==mem2
,"GlobalLock should have returned the same memory as was allocated\n");
621 for(i
=0;i
<memchunk
;i
++) {
626 ok(!error
,"GlobalAlloc should have zeroed out its allocated memory\n");
629 /* Check that GlobalReAlloc works */
630 /* Check that we can change GMEM_FIXED to GMEM_MOVEABLE */
631 mem2a
=GlobalReAlloc(mem2
,0,GMEM_MODIFY
| GMEM_MOVEABLE
);
634 mem2ptr
=GlobalLock(mem2a
);
635 ok(mem2ptr
!=NULL
&& !GlobalUnlock(mem2a
)&&GetLastError()==NO_ERROR
,
636 "Converting from FIXED to MOVEABLE didn't REALLY work\n");
639 /* Check that ReAllocing memory works as expected */
640 mem2a
=GlobalReAlloc(mem2
,2*memchunk
,GMEM_MOVEABLE
| GMEM_ZEROINIT
);
641 ok(mem2a
!=NULL
,"GlobalReAlloc failed\n");
643 ok(GlobalSize(mem2a
)>=2*memchunk
,"GlobalReAlloc failed\n");
644 mem2ptr
=GlobalLock(mem2a
);
645 ok(mem2ptr
!=NULL
,"GlobalLock Failed\n");
648 for(i
=0;i
<memchunk
;i
++) {
649 if(mem2ptr
[memchunk
+i
]!=0) {
653 ok(!error
,"GlobalReAlloc should have zeroed out its allocated memory\n");
655 /* Check that GlobalHandle works */
656 mem2b
=GlobalHandle(mem2ptr
);
657 ok(mem2b
==mem2a
,"GlobalHandle didn't return the correct memory handle %p/%p for %p\n",
658 mem2a
, mem2b
, mem2ptr
);
659 /* Check that we can't discard locked memory */
660 mem2b
=GlobalDiscard(mem2a
);
662 ok(!GlobalUnlock(mem2a
) && GetLastError()==NO_ERROR
,"GlobalUnlock Failed\n");
667 ok(GlobalFree(mem1
)==NULL
,"GlobalFree failed\n");
670 ok(GlobalFree(mem2a
)==NULL
,"GlobalFree failed\n");
672 ok(GlobalFree(mem2
)==NULL
,"GlobalFree failed\n");
677 static void test_LocalAlloc(void)
680 HLOCAL mem1
,mem2
,mem2a
,mem2b
;
686 /* Check that a normal alloc works */
687 mem1
=LocalAlloc(0,memchunk
);
688 ok(mem1
!=NULL
,"LocalAlloc failed: error=%d\n",GetLastError());
690 ok(LocalSize(mem1
)>=memchunk
, "LocalAlloc should return a big enough memory block\n");
693 /* Check that a 'zeroing' and lock alloc works */
694 mem2
=LocalAlloc(LMEM_ZEROINIT
|LMEM_MOVEABLE
,memchunk
);
695 ok(mem2
!=NULL
,"LocalAlloc failed: error=%d\n",GetLastError());
697 ok(LocalSize(mem2
)>=memchunk
,"LocalAlloc should return a big enough memory block\n");
698 mem2ptr
=LocalLock(mem2
);
699 ok(mem2ptr
!=NULL
,"LocalLock: error=%d\n",GetLastError());
702 for(i
=0;i
<memchunk
;i
++) {
707 ok(!error
,"LocalAlloc should have zeroed out its allocated memory\n");
709 error
=LocalUnlock(mem2
);
710 ok(!error
&& GetLastError()==NO_ERROR
,
711 "LocalUnlock Failed: rc=%d err=%d\n",error
,GetLastError());
714 mem2a
=LocalFree(mem2
);
715 ok(mem2a
==NULL
, "LocalFree failed: %p\n",mem2a
);
717 /* Reallocate mem2 as moveable memory */
718 mem2
=LocalAlloc(LMEM_MOVEABLE
| LMEM_ZEROINIT
,memchunk
);
719 ok(mem2
!=NULL
, "LocalAlloc failed to create moveable memory, error=%d\n",GetLastError());
721 /* Check that ReAllocing memory works as expected */
722 mem2a
=LocalReAlloc(mem2
,2*memchunk
,LMEM_MOVEABLE
| LMEM_ZEROINIT
);
723 ok(mem2a
!=NULL
,"LocalReAlloc failed, error=%d\n",GetLastError());
725 ok(LocalSize(mem2a
)>=2*memchunk
,"LocalReAlloc failed\n");
726 mem2ptr
=LocalLock(mem2a
);
727 ok(mem2ptr
!=NULL
,"LocalLock Failed\n");
730 for(i
=0;i
<memchunk
;i
++) {
731 if(mem2ptr
[memchunk
+i
]!=0) {
735 ok(!error
,"LocalReAlloc should have zeroed out its allocated memory\n");
736 /* Check that LocalHandle works */
737 mem2b
=LocalHandle(mem2ptr
);
738 ok(mem2b
==mem2a
,"LocalHandle didn't return the correct memory handle %p/%p for %p\n",
739 mem2a
, mem2b
, mem2ptr
);
740 /* Check that we can't discard locked memory */
741 mem2b
=LocalDiscard(mem2a
);
742 ok(mem2b
==NULL
,"Discarded memory we shouldn't have\n");
743 SetLastError(NO_ERROR
);
744 ok(!LocalUnlock(mem2a
) && GetLastError()==NO_ERROR
, "LocalUnlock Failed\n");
748 ok(LocalFree(mem1
)==NULL
,"LocalFree failed\n");
751 ok(LocalFree(mem2a
)==NULL
,"LocalFree failed\n");
753 ok(LocalFree(mem2
)==NULL
,"LocalFree failed\n");
757 static void test_obsolete_flags(void)
762 } test_global_flags
[] = {
763 {GMEM_FIXED
| GMEM_NOTIFY
, 0},
764 {GMEM_FIXED
| GMEM_DISCARDABLE
, 0},
765 {GMEM_MOVEABLE
| GMEM_NOTIFY
, 0},
766 {GMEM_MOVEABLE
| GMEM_DDESHARE
, GMEM_DDESHARE
},
767 {GMEM_MOVEABLE
| GMEM_NOT_BANKED
, 0},
768 {GMEM_MOVEABLE
| GMEM_NODISCARD
, 0},
769 {GMEM_MOVEABLE
| GMEM_DISCARDABLE
, GMEM_DISCARDABLE
},
770 {GMEM_MOVEABLE
| GMEM_DDESHARE
| GMEM_DISCARDABLE
| GMEM_LOWER
| GMEM_NOCOMPACT
| GMEM_NODISCARD
|
771 GMEM_NOT_BANKED
| GMEM_NOTIFY
, GMEM_DDESHARE
| GMEM_DISCARDABLE
},
778 UINT (WINAPI
*pGlobalFlags
)(HGLOBAL
);
780 pGlobalFlags
= (void *) GetProcAddress(GetModuleHandleA("kernel32"), "GlobalFlags");
784 win_skip("GlobalFlags is not available\n");
788 for (i
= 0; i
< sizeof(test_global_flags
)/sizeof(test_global_flags
[0]); i
++)
790 gbl
= GlobalAlloc(test_global_flags
[i
].flags
, 4);
791 ok(gbl
!= NULL
, "GlobalAlloc failed\n");
793 SetLastError(MAGIC_DEAD
);
794 resultflags
= pGlobalFlags(gbl
);
796 ok( resultflags
== test_global_flags
[i
].globalflags
||
797 broken(resultflags
== (test_global_flags
[i
].globalflags
& ~GMEM_DDESHARE
)), /* win9x */
798 "%u: expected 0x%08x, but returned 0x%08x with %d\n",
799 i
, test_global_flags
[i
].globalflags
, resultflags
, GetLastError() );
805 static void test_HeapQueryInformation(void)
811 pHeapQueryInformation
= (void *)GetProcAddress(GetModuleHandleA("kernel32.dll"), "HeapQueryInformation");
812 if (!pHeapQueryInformation
)
814 win_skip("HeapQueryInformation is not available\n");
818 if (0) /* crashes under XP */
821 pHeapQueryInformation(0,
822 HeapCompatibilityInformation
,
823 &info
, sizeof(info
), &size
);
825 pHeapQueryInformation(GetProcessHeap(),
826 HeapCompatibilityInformation
,
827 NULL
, sizeof(info
), &size
);
831 SetLastError(0xdeadbeef);
832 ret
= pHeapQueryInformation(GetProcessHeap(),
833 HeapCompatibilityInformation
,
835 ok(!ret
, "HeapQueryInformation should fail\n");
836 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
837 "expected ERROR_INSUFFICIENT_BUFFER got %u\n", GetLastError());
838 ok(size
== sizeof(ULONG
), "expected 4, got %lu\n", size
);
840 SetLastError(0xdeadbeef);
841 ret
= pHeapQueryInformation(GetProcessHeap(),
842 HeapCompatibilityInformation
,
844 ok(!ret
, "HeapQueryInformation should fail\n");
845 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
846 "expected ERROR_INSUFFICIENT_BUFFER got %u\n", GetLastError());
849 SetLastError(0xdeadbeef);
850 ret
= pHeapQueryInformation(GetProcessHeap(),
851 HeapCompatibilityInformation
,
852 &info
, sizeof(info
) + 1, NULL
);
853 ok(ret
, "HeapQueryInformation error %u\n", GetLastError());
854 ok(info
== 0 || info
== 1 || info
== 2, "expected 0, 1 or 2, got %u\n", info
);
857 static void test_heap_checks( DWORD flags
)
861 SIZE_T i
, size
, large_size
= 3000 * 1024 + 37;
863 if (flags
& HEAP_PAGE_ALLOCS
) return; /* no tests for that case yet */
864 trace( "testing heap flags %08x\n", flags
);
866 p
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, 17 );
867 ok( p
!= NULL
, "HeapAlloc failed\n" );
869 ret
= HeapValidate( GetProcessHeap(), 0, p
);
870 ok( ret
, "HeapValidate failed\n" );
872 size
= HeapSize( GetProcessHeap(), 0, p
);
873 ok( size
== 17, "Wrong size %lu\n", size
);
875 ok( p
[14] == 0, "wrong data %x\n", p
[14] );
876 ok( p
[15] == 0, "wrong data %x\n", p
[15] );
877 ok( p
[16] == 0, "wrong data %x\n", p
[16] );
879 if (flags
& HEAP_TAIL_CHECKING_ENABLED
)
881 ok( p
[17] == 0xab, "wrong padding %x\n", p
[17] );
882 ok( p
[18] == 0xab, "wrong padding %x\n", p
[18] );
883 ok( p
[19] == 0xab, "wrong padding %x\n", p
[19] );
886 p2
= HeapReAlloc( GetProcessHeap(), HEAP_REALLOC_IN_PLACE_ONLY
, p
, 14 );
889 if (flags
& HEAP_TAIL_CHECKING_ENABLED
)
891 ok( p
[14] == 0xab, "wrong padding %x\n", p
[14] );
892 ok( p
[15] == 0xab, "wrong padding %x\n", p
[15] );
893 ok( p
[16] == 0xab, "wrong padding %x\n", p
[16] );
897 ok( p
[14] == 0, "wrong padding %x\n", p
[14] );
898 ok( p
[15] == 0, "wrong padding %x\n", p
[15] );
901 else skip( "realloc in place failed\n");
903 ret
= HeapFree( GetProcessHeap(), 0, p
);
904 ok( ret
, "HeapFree failed\n" );
906 p
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, 17 );
907 ok( p
!= NULL
, "HeapAlloc failed\n" );
911 if (flags
& HEAP_TAIL_CHECKING_ENABLED
)
913 ret
= HeapValidate( GetProcessHeap(), 0, p
);
914 ok( !ret
, "HeapValidate succeeded\n" );
916 /* other calls only check when HEAP_VALIDATE is set */
917 if (flags
& HEAP_VALIDATE
)
919 size
= HeapSize( GetProcessHeap(), 0, p
);
920 ok( size
== ~(SIZE_T
)0 || broken(size
== ~0u), "Wrong size %lu\n", size
);
922 p2
= HeapReAlloc( GetProcessHeap(), 0, p
, 14 );
923 ok( p2
== NULL
, "HeapReAlloc succeeded\n" );
925 ret
= HeapFree( GetProcessHeap(), 0, p
);
926 ok( !ret
|| broken(sizeof(void*) == 8), /* not caught on xp64 */
927 "HeapFree succeeded\n" );
931 size
= HeapSize( GetProcessHeap(), 0, p
);
932 ok( size
== 17, "Wrong size %lu\n", size
);
934 p2
= HeapReAlloc( GetProcessHeap(), 0, p
, 14 );
935 ok( p2
!= NULL
, "HeapReAlloc failed\n" );
939 ret
= HeapFree( GetProcessHeap(), 0, p
);
940 ok( ret
, "HeapFree failed\n" );
942 p
= HeapAlloc( GetProcessHeap(), 0, 37 );
943 ok( p
!= NULL
, "HeapAlloc failed\n" );
944 memset( p
, 0xcc, 37 );
946 ret
= HeapFree( GetProcessHeap(), 0, p
);
947 ok( ret
, "HeapFree failed\n" );
949 if (flags
& HEAP_FREE_CHECKING_ENABLED
)
951 ok( p
[16] == 0xee, "wrong data %x\n", p
[16] );
952 ok( p
[17] == 0xfe, "wrong data %x\n", p
[17] );
953 ok( p
[18] == 0xee, "wrong data %x\n", p
[18] );
954 ok( p
[19] == 0xfe, "wrong data %x\n", p
[19] );
956 ret
= HeapValidate( GetProcessHeap(), 0, NULL
);
957 ok( ret
, "HeapValidate failed\n" );
961 ret
= HeapValidate( GetProcessHeap(), 0, NULL
);
962 ok( !ret
, "HeapValidate succeeded\n" );
965 ret
= HeapValidate( GetProcessHeap(), 0, NULL
);
966 ok( ret
, "HeapValidate failed\n" );
969 /* now test large blocks */
971 p
= HeapAlloc( GetProcessHeap(), 0, large_size
);
972 ok( p
!= NULL
, "HeapAlloc failed\n" );
974 ret
= HeapValidate( GetProcessHeap(), 0, p
);
975 ok( ret
, "HeapValidate failed\n" );
977 size
= HeapSize( GetProcessHeap(), 0, p
);
978 ok( size
== large_size
, "Wrong size %lu\n", size
);
980 ok( p
[large_size
- 2] == 0, "wrong data %x\n", p
[large_size
- 2] );
981 ok( p
[large_size
- 1] == 0, "wrong data %x\n", p
[large_size
- 1] );
983 if (flags
& HEAP_TAIL_CHECKING_ENABLED
)
985 /* Windows doesn't do tail checking on large blocks */
986 ok( p
[large_size
] == 0xab || broken(p
[large_size
] == 0), "wrong data %x\n", p
[large_size
] );
987 ok( p
[large_size
+1] == 0xab || broken(p
[large_size
+1] == 0), "wrong data %x\n", p
[large_size
+1] );
988 ok( p
[large_size
+2] == 0xab || broken(p
[large_size
+2] == 0), "wrong data %x\n", p
[large_size
+2] );
989 if (p
[large_size
] == 0xab)
991 p
[large_size
] = 0xcc;
992 ret
= HeapValidate( GetProcessHeap(), 0, p
);
993 ok( !ret
, "HeapValidate succeeded\n" );
995 /* other calls only check when HEAP_VALIDATE is set */
996 if (flags
& HEAP_VALIDATE
)
998 size
= HeapSize( GetProcessHeap(), 0, p
);
999 ok( size
== ~(SIZE_T
)0, "Wrong size %lu\n", size
);
1001 p2
= HeapReAlloc( GetProcessHeap(), 0, p
, large_size
- 3 );
1002 ok( p2
== NULL
, "HeapReAlloc succeeded\n" );
1004 ret
= HeapFree( GetProcessHeap(), 0, p
);
1005 ok( !ret
, "HeapFree succeeded\n" );
1007 p
[large_size
] = 0xab;
1011 ret
= HeapFree( GetProcessHeap(), 0, p
);
1012 ok( ret
, "HeapFree failed\n" );
1014 /* test block sizes when tail checking */
1015 if (flags
& HEAP_TAIL_CHECKING_ENABLED
)
1017 for (size
= 0; size
< 64; size
++)
1019 p
= HeapAlloc( GetProcessHeap(), 0, size
);
1020 for (i
= 0; i
< 32; i
++) if (p
[size
+ i
] != 0xab) break;
1021 ok( i
>= 8, "only %lu tail bytes for size %lu\n", i
, size
);
1022 HeapFree( GetProcessHeap(), 0, p
);
1027 static void test_debug_heap( const char *argv0
, DWORD flags
)
1029 char keyname
[MAX_PATH
];
1030 char buffer
[MAX_PATH
];
1031 PROCESS_INFORMATION info
;
1032 STARTUPINFOA startup
;
1036 const char *basename
;
1038 if ((basename
= strrchr( argv0
, '\\' ))) basename
++;
1039 else basename
= argv0
;
1041 sprintf( keyname
, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Image File Execution Options\\%s",
1043 if (!strcmp( keyname
+ strlen(keyname
) - 3, ".so" )) keyname
[strlen(keyname
) - 3] = 0;
1045 err
= RegCreateKeyA( HKEY_LOCAL_MACHINE
, keyname
, &hkey
);
1046 if (err
== ERROR_ACCESS_DENIED
)
1048 skip("Not authorized to change the image file execution options\n");
1051 ok( !err
, "failed to create '%s' error %u\n", keyname
, err
);
1054 if (flags
== 0xdeadbeef) /* magic value for unsetting it */
1055 RegDeleteValueA( hkey
, "GlobalFlag" );
1057 RegSetValueExA( hkey
, "GlobalFlag", 0, REG_DWORD
, (BYTE
*)&flags
, sizeof(flags
) );
1059 memset( &startup
, 0, sizeof(startup
) );
1060 startup
.cb
= sizeof(startup
);
1062 sprintf( buffer
, "%s heap.c 0x%x", argv0
, flags
);
1063 ret
= CreateProcessA( NULL
, buffer
, NULL
, NULL
, FALSE
, 0, NULL
, NULL
, &startup
, &info
);
1064 ok( ret
, "failed to create child process error %u\n", GetLastError() );
1067 winetest_wait_child_process( info
.hProcess
);
1068 CloseHandle( info
.hThread
);
1069 CloseHandle( info
.hProcess
);
1071 RegDeleteValueA( hkey
, "GlobalFlag" );
1072 RegCloseKey( hkey
);
1073 RegDeleteKeyA( HKEY_LOCAL_MACHINE
, keyname
);
1076 static DWORD
heap_flags_from_global_flag( DWORD flag
)
1080 if (flag
& FLG_HEAP_ENABLE_TAIL_CHECK
)
1081 ret
|= HEAP_TAIL_CHECKING_ENABLED
;
1082 if (flag
& FLG_HEAP_ENABLE_FREE_CHECK
)
1083 ret
|= HEAP_FREE_CHECKING_ENABLED
;
1084 if (flag
& FLG_HEAP_VALIDATE_PARAMETERS
)
1085 ret
|= HEAP_VALIDATE_PARAMS
| HEAP_VALIDATE
| HEAP_TAIL_CHECKING_ENABLED
| HEAP_FREE_CHECKING_ENABLED
;
1086 if (flag
& FLG_HEAP_VALIDATE_ALL
)
1087 ret
|= HEAP_VALIDATE_ALL
| HEAP_VALIDATE
| HEAP_TAIL_CHECKING_ENABLED
| HEAP_FREE_CHECKING_ENABLED
;
1088 if (flag
& FLG_HEAP_DISABLE_COALESCING
)
1089 ret
|= HEAP_DISABLE_COALESCE_ON_FREE
;
1090 if (flag
& FLG_HEAP_PAGE_ALLOCS
)
1091 ret
|= HEAP_PAGE_ALLOCS
| HEAP_GROWABLE
;
1095 static void test_child_heap( const char *arg
)
1097 struct heap_layout
*heap
= GetProcessHeap();
1098 DWORD expected
= strtoul( arg
, 0, 16 );
1101 if (expected
== 0xdeadbeef) /* expected value comes from Session Manager global flags */
1105 if (!RegOpenKeyA( HKEY_LOCAL_MACHINE
, "SYSTEM\\CurrentControlSet\\Control\\Session Manager", &hkey
))
1108 DWORD type
, size
= sizeof(buffer
);
1110 if (!RegQueryValueExA( hkey
, "GlobalFlag", 0, &type
, (BYTE
*)buffer
, &size
))
1112 if (type
== REG_DWORD
) expected
= *(DWORD
*)buffer
;
1113 else if (type
== REG_SZ
) expected
= strtoul( buffer
, 0, 16 );
1115 RegCloseKey( hkey
);
1118 if (expected
&& !pRtlGetNtGlobalFlags()) /* not working on NT4 */
1120 win_skip( "global flags not set\n" );
1124 ok( pRtlGetNtGlobalFlags() == expected
,
1125 "%s: got global flags %08x expected %08x\n", arg
, pRtlGetNtGlobalFlags(), expected
);
1127 expect_heap
= heap_flags_from_global_flag( expected
);
1129 if (!(heap
->flags
& HEAP_GROWABLE
) || heap
->pattern
== 0xffeeffee) /* vista layout */
1131 ok( (heap
->flags
& ~HEAP_GROWABLE
) == 0, "%s: got heap flags %08x\n", arg
, heap
->flags
);
1133 else if (heap
->pattern
== 0xeeeeeeee && heap
->flags
== 0xeeeeeeee)
1135 ok( expected
& FLG_HEAP_PAGE_ALLOCS
, "%s: got heap flags 0xeeeeeeee without page alloc\n", arg
);
1139 ok( heap
->flags
== (expect_heap
| HEAP_GROWABLE
),
1140 "%s: got heap flags %08x expected %08x\n", arg
, heap
->flags
, expect_heap
);
1141 ok( heap
->force_flags
== (expect_heap
& ~0x18000080),
1142 "%s: got heap force flags %08x expected %08x\n", arg
, heap
->force_flags
, expect_heap
);
1143 expect_heap
= heap
->flags
;
1146 test_heap_checks( expect_heap
);
1149 static void test_GetPhysicallyInstalledSystemMemory(void)
1151 HMODULE kernel32
= GetModuleHandleA("kernel32.dll");
1152 MEMORYSTATUSEX memstatus
;
1153 ULONGLONG total_memory
;
1156 pGetPhysicallyInstalledSystemMemory
= (void *)GetProcAddress(kernel32
, "GetPhysicallyInstalledSystemMemory");
1157 if (!pGetPhysicallyInstalledSystemMemory
)
1159 win_skip("GetPhysicallyInstalledSystemMemory is not available\n");
1163 SetLastError(0xdeadbeef);
1164 ret
= pGetPhysicallyInstalledSystemMemory(NULL
);
1165 ok(!ret
, "GetPhysicallyInstalledSystemMemory should fail\n");
1166 ok(GetLastError() == ERROR_INVALID_PARAMETER
,
1167 "expected ERROR_INVALID_PARAMETER, got %u\n", GetLastError());
1170 ret
= pGetPhysicallyInstalledSystemMemory(&total_memory
);
1171 ok(ret
, "GetPhysicallyInstalledSystemMemory unexpectedly failed\n");
1172 ok(total_memory
!= 0, "expected total_memory != 0\n");
1174 memstatus
.dwLength
= sizeof(memstatus
);
1175 ret
= GlobalMemoryStatusEx(&memstatus
);
1176 ok(ret
, "GlobalMemoryStatusEx unexpectedly failed\n");
1177 ok(total_memory
>= memstatus
.ullTotalPhys
/ 1024,
1178 "expected total_memory >= memstatus.ullTotalPhys / 1024\n");
1186 pRtlGetNtGlobalFlags
= (void *)GetProcAddress( GetModuleHandleA("ntdll.dll"), "RtlGetNtGlobalFlags" );
1188 argc
= winetest_get_mainargs( &argv
);
1191 test_child_heap( argv
[2] );
1196 test_obsolete_flags();
1201 /* Test both short and very long blocks */
1202 test_sized_HeapAlloc(1);
1203 test_sized_HeapAlloc(1 << 20);
1204 test_sized_HeapReAlloc(1, 100);
1205 test_sized_HeapReAlloc(1, (1 << 20));
1206 test_sized_HeapReAlloc((1 << 20), (2 << 20));
1207 test_sized_HeapReAlloc((1 << 20), 1);
1209 test_HeapQueryInformation();
1210 test_GetPhysicallyInstalledSystemMemory();
1212 if (pRtlGetNtGlobalFlags
)
1214 test_debug_heap( argv
[0], 0 );
1215 test_debug_heap( argv
[0], FLG_HEAP_ENABLE_TAIL_CHECK
);
1216 test_debug_heap( argv
[0], FLG_HEAP_ENABLE_FREE_CHECK
);
1217 test_debug_heap( argv
[0], FLG_HEAP_VALIDATE_PARAMETERS
);
1218 test_debug_heap( argv
[0], FLG_HEAP_VALIDATE_ALL
);
1219 test_debug_heap( argv
[0], FLG_POOL_ENABLE_TAGGING
);
1220 test_debug_heap( argv
[0], FLG_HEAP_ENABLE_TAGGING
);
1221 test_debug_heap( argv
[0], FLG_HEAP_ENABLE_TAG_BY_DLL
);
1222 test_debug_heap( argv
[0], FLG_HEAP_DISABLE_COALESCING
);
1223 test_debug_heap( argv
[0], FLG_HEAP_PAGE_ALLOCS
);
1224 test_debug_heap( argv
[0], 0xdeadbeef );
1226 else win_skip( "RtlGetNtGlobalFlags not found, skipping heap debug tests\n" );