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;
92 /* use function pointers to avoid warnings for invalid parameter tests */
93 LPVOID (WINAPI
*pHeapAlloc
)(HANDLE
,DWORD
,SIZE_T
);
94 LPVOID (WINAPI
*pHeapReAlloc
)(HANDLE
,DWORD
,LPVOID
,SIZE_T
);
96 pHeapAlloc
= (void *)GetProcAddress( GetModuleHandleA("kernel32"), "HeapAlloc" );
97 pHeapReAlloc
= (void *)GetProcAddress( GetModuleHandleA("kernel32"), "HeapReAlloc" );
99 /* Heap*() functions */
100 mem
= HeapAlloc(GetProcessHeap(), 0, 0);
101 ok(mem
!= NULL
, "memory not allocated for size 0\n");
102 HeapFree(GetProcessHeap(), 0, mem
);
104 mem
= HeapReAlloc(GetProcessHeap(), 0, NULL
, 10);
105 ok(mem
== NULL
, "memory allocated by HeapReAlloc\n");
107 for (size
= 0; size
<= 256; size
++)
110 mem
= HeapAlloc(GetProcessHeap(), 0, size
);
111 heap_size
= HeapSize(GetProcessHeap(), 0, mem
);
112 ok(heap_size
== size
|| heap_size
== resize_9x(size
),
113 "HeapSize returned %lu instead of %lu or %lu\n", heap_size
, size
, resize_9x(size
));
114 HeapFree(GetProcessHeap(), 0, mem
);
117 /* test some border cases of HeapAlloc and HeapReAlloc */
118 mem
= HeapAlloc(GetProcessHeap(), 0, 0);
119 ok(mem
!= NULL
, "memory not allocated for size 0\n");
120 msecond
= pHeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, mem
, ~(SIZE_T
)0 - 7);
121 ok(msecond
== NULL
, "HeapReAlloc(~0 - 7) should have failed\n");
122 msecond
= pHeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, mem
, ~(SIZE_T
)0);
123 ok(msecond
== NULL
, "HeapReAlloc(~0) should have failed\n");
124 HeapFree(GetProcessHeap(), 0, mem
);
125 mem
= pHeapAlloc(GetProcessHeap(), 0, ~(SIZE_T
)0);
126 ok(mem
== NULL
, "memory allocated for size ~0\n");
127 mem
= HeapAlloc(GetProcessHeap(), 0, 17);
128 msecond
= HeapReAlloc(GetProcessHeap(), 0, mem
, 0);
129 ok(msecond
!= NULL
, "HeapReAlloc(0) should have succeeded\n");
130 size
= HeapSize(GetProcessHeap(), 0, msecond
);
131 ok(size
== 0 || broken(size
== 1) /* some vista and win7 */,
132 "HeapSize should have returned 0 instead of %lu\n", size
);
133 HeapFree(GetProcessHeap(), 0, msecond
);
135 /* large blocks must be 16-byte aligned */
136 mem
= HeapAlloc(GetProcessHeap(), 0, 512 * 1024);
137 ok( mem
!= NULL
, "failed for size 512K\n" );
138 ok( (ULONG_PTR
)mem
% 16 == 0 || broken((ULONG_PTR
)mem
% 16) /* win9x */,
139 "512K block not 16-byte aligned\n" );
140 HeapFree(GetProcessHeap(), 0, mem
);
142 /* Global*() functions */
143 gbl
= GlobalAlloc(GMEM_MOVEABLE
, 0);
144 ok(gbl
!= NULL
, "global memory not allocated for size 0\n");
146 gbl
= GlobalReAlloc(gbl
, 10, GMEM_MOVEABLE
);
147 ok(gbl
!= NULL
, "Can't realloc global memory\n");
148 size
= GlobalSize(gbl
);
149 ok(size
>= 10 && size
<= 16, "Memory not resized to size 10, instead size=%ld\n", size
);
151 gbl
= GlobalReAlloc(gbl
, 0, GMEM_MOVEABLE
);
152 ok(gbl
!= NULL
, "GlobalReAlloc should not fail on size 0\n");
154 size
= GlobalSize(gbl
);
155 ok(size
== 0, "Memory not resized to size 0, instead size=%ld\n", size
);
156 ok(GlobalFree(gbl
) == NULL
, "Memory not freed\n");
157 size
= GlobalSize(gbl
);
158 ok(size
== 0, "Memory should have been freed, size=%ld\n", size
);
160 gbl
= GlobalReAlloc(0, 10, GMEM_MOVEABLE
);
161 ok(gbl
== NULL
, "global realloc allocated memory\n");
163 /* GlobalLock / GlobalUnlock with a valid handle */
164 gbl
= GlobalAlloc(GMEM_MOVEABLE
, 256);
166 SetLastError(MAGIC_DEAD
);
167 mem
= GlobalLock(gbl
); /* #1 */
168 ok(mem
!= NULL
, "returned %p with %d (expected '!= NULL')\n", mem
, GetLastError());
169 SetLastError(MAGIC_DEAD
);
170 flags
= GlobalFlags(gbl
);
171 ok( flags
== 1, "returned 0x%04x with %d (expected '0x0001')\n",
172 flags
, GetLastError());
174 SetLastError(MAGIC_DEAD
);
175 msecond
= GlobalLock(gbl
); /* #2 */
176 ok( msecond
== mem
, "returned %p with %d (expected '%p')\n",
177 msecond
, GetLastError(), mem
);
178 SetLastError(MAGIC_DEAD
);
179 flags
= GlobalFlags(gbl
);
180 ok( flags
== 2, "returned 0x%04x with %d (expected '0x0002')\n",
181 flags
, GetLastError());
182 SetLastError(MAGIC_DEAD
);
184 SetLastError(MAGIC_DEAD
);
185 res
= GlobalUnlock(gbl
); /* #1 */
186 ok(res
, "returned %d with %d (expected '!= 0')\n", res
, GetLastError());
187 SetLastError(MAGIC_DEAD
);
188 flags
= GlobalFlags(gbl
);
189 ok( flags
, "returned 0x%04x with %d (expected '!= 0')\n",
190 flags
, GetLastError());
192 SetLastError(MAGIC_DEAD
);
193 res
= GlobalUnlock(gbl
); /* #0 */
194 /* NT: ERROR_SUCCESS (documented on MSDN), 9x: untouched */
195 ok(!res
&& ((GetLastError() == ERROR_SUCCESS
) || (GetLastError() == MAGIC_DEAD
)),
196 "returned %d with %d (expected '0' with: ERROR_SUCCESS or "
197 "MAGIC_DEAD)\n", res
, GetLastError());
198 SetLastError(MAGIC_DEAD
);
199 flags
= GlobalFlags(gbl
);
200 ok( !flags
, "returned 0x%04x with %d (expected '0')\n",
201 flags
, GetLastError());
203 /* Unlock an already unlocked Handle */
204 SetLastError(MAGIC_DEAD
);
205 res
= GlobalUnlock(gbl
);
206 /* NT: ERROR_NOT_LOCKED, 9x: untouched */
208 ((GetLastError() == ERROR_NOT_LOCKED
) || (GetLastError() == MAGIC_DEAD
)),
209 "returned %d with %d (expected '0' with: ERROR_NOT_LOCKED or "
210 "MAGIC_DEAD)\n", res
, GetLastError());
213 /* invalid handles are caught in windows: */
214 SetLastError(MAGIC_DEAD
);
215 hsecond
= GlobalFree(gbl
); /* invalid handle: free memory twice */
216 ok( (hsecond
== gbl
) && (GetLastError() == ERROR_INVALID_HANDLE
),
217 "returned %p with 0x%08x (expected %p with ERROR_INVALID_HANDLE)\n",
218 hsecond
, GetLastError(), gbl
);
219 SetLastError(MAGIC_DEAD
);
220 flags
= GlobalFlags(gbl
);
221 ok( (flags
== GMEM_INVALID_HANDLE
) && (GetLastError() == ERROR_INVALID_HANDLE
),
222 "returned 0x%04x with 0x%08x (expected GMEM_INVALID_HANDLE with "
223 "ERROR_INVALID_HANDLE)\n", flags
, GetLastError());
224 SetLastError(MAGIC_DEAD
);
225 size
= GlobalSize(gbl
);
226 ok( (size
== 0) && (GetLastError() == ERROR_INVALID_HANDLE
),
227 "returned %ld with 0x%08x (expected '0' with ERROR_INVALID_HANDLE)\n",
228 size
, GetLastError());
230 SetLastError(MAGIC_DEAD
);
231 mem
= GlobalLock(gbl
);
232 ok( (mem
== NULL
) && (GetLastError() == ERROR_INVALID_HANDLE
),
233 "returned %p with 0x%08x (expected NULL with ERROR_INVALID_HANDLE)\n",
234 mem
, GetLastError());
236 /* documented on MSDN: GlobalUnlock() return FALSE on failure.
237 Win9x and wine return FALSE with ERROR_INVALID_HANDLE, but on
238 NT 3.51 and XPsp2, TRUE with ERROR_INVALID_HANDLE is returned.
239 The similar Test for LocalUnlock() works on all Systems */
240 SetLastError(MAGIC_DEAD
);
241 res
= GlobalUnlock(gbl
);
242 ok(GetLastError() == ERROR_INVALID_HANDLE
,
243 "returned %d with %d (expected ERROR_INVALID_HANDLE)\n",
244 res
, GetLastError());
246 gbl
= GlobalAlloc(GMEM_DDESHARE
, 100);
249 mem
= GlobalFree(gbl
);
250 ok(mem
== NULL
, "Expected NULL, got %p\n", mem
);
253 if (sizeof(void *) != 8) /* crashes on 64-bit Vista */
255 SetLastError(MAGIC_DEAD
);
256 mem
= GlobalFree(gbl
);
257 ok(mem
== gbl
|| broken(mem
== NULL
) /* nt4 */, "Expected gbl, got %p\n", mem
);
259 ok(GetLastError() == ERROR_INVALID_HANDLE
||
260 GetLastError() == ERROR_INVALID_PARAMETER
, /* win9x */
261 "Expected ERROR_INVALID_HANDLE or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
264 /* GMEM_FIXED block expands in place only without flags */
265 for (size
= 1; size
<= max_size
; size
<<= 1) {
266 gbl
= GlobalAlloc(GMEM_FIXED
, init_size
);
267 SetLastError(MAGIC_DEAD
);
268 hsecond
= GlobalReAlloc(gbl
, size
+ init_size
, 0);
269 ok(hsecond
== gbl
|| (hsecond
== NULL
&& GetLastError() == ERROR_NOT_ENOUGH_MEMORY
),
270 "got %p with %x (expected %p or NULL) @%ld\n", hsecond
, GetLastError(), gbl
, size
);
274 /* GMEM_FIXED block can be relocated with GMEM_MOVEABLE */
275 for (size
= 1; size
<= max_size
; size
<<= 1) {
276 gbl
= GlobalAlloc(GMEM_FIXED
, init_size
);
277 SetLastError(MAGIC_DEAD
);
278 hsecond
= GlobalReAlloc(gbl
, size
+ init_size
, GMEM_MOVEABLE
);
280 "got %p with %x (expected non-NULL) @%ld\n", hsecond
, GetLastError(), size
);
281 mem
= GlobalLock(hsecond
);
282 ok(mem
== hsecond
, "got %p (expected %p) @%ld\n", mem
, hsecond
, size
);
286 gbl
= GlobalAlloc(GMEM_DDESHARE
, 100);
288 res
= GlobalUnlock(gbl
);
290 broken(res
== 0), /* win9x */
291 "Expected 1 or 0, got %d\n", res
);
293 res
= GlobalUnlock(gbl
);
295 broken(res
== 0), /* win9x */
296 "Expected 1 or 0, got %d\n", res
);
300 gbl
= GlobalAlloc(GMEM_FIXED
, 100);
302 SetLastError(0xdeadbeef);
303 res
= GlobalUnlock(gbl
);
305 broken(res
== 0), /* win9x */
306 "Expected 1 or 0, got %d\n", res
);
307 ok(GetLastError() == 0xdeadbeef, "got %d\n", GetLastError());
311 /* GlobalSize on an invalid handle */
312 if (sizeof(void *) != 8) /* crashes on 64-bit Vista */
314 SetLastError(MAGIC_DEAD
);
315 size
= GlobalSize((HGLOBAL
)0xc042);
316 ok(size
== 0, "Expected 0, got %ld\n", size
);
317 ok(GetLastError() == ERROR_INVALID_HANDLE
||
318 GetLastError() == ERROR_INVALID_PARAMETER
, /* win9x */
319 "Expected ERROR_INVALID_HANDLE or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
322 gbl
= GlobalAlloc( GMEM_FIXED
, 0 );
323 SetLastError(0xdeadbeef);
324 size
= GlobalSize( gbl
);
325 ok( size
== 1, "wrong size %lu\n", size
);
328 /* ####################################### */
329 /* Local*() functions */
330 gbl
= LocalAlloc(LMEM_MOVEABLE
, 0);
331 ok(gbl
!= NULL
, "local memory not allocated for size 0\n");
333 gbl
= LocalReAlloc(gbl
, 10, LMEM_MOVEABLE
);
334 ok(gbl
!= NULL
, "Can't realloc local memory\n");
335 size
= LocalSize(gbl
);
336 ok(size
>= 10 && size
<= 16, "Memory not resized to size 10, instead size=%ld\n", size
);
338 gbl
= LocalReAlloc(gbl
, 0, LMEM_MOVEABLE
);
339 ok(gbl
!= NULL
, "LocalReAlloc should not fail on size 0\n");
341 size
= LocalSize(gbl
);
342 ok(size
== 0, "Memory not resized to size 0, instead size=%ld\n", size
);
343 ok(LocalFree(gbl
) == NULL
, "Memory not freed\n");
344 size
= LocalSize(gbl
);
345 ok(size
== 0, "Memory should have been freed, size=%ld\n", size
);
347 gbl
= LocalReAlloc(0, 10, LMEM_MOVEABLE
);
348 ok(gbl
== NULL
, "local realloc allocated memory\n");
350 /* LocalLock / LocalUnlock with a valid handle */
351 gbl
= LocalAlloc(LMEM_MOVEABLE
, 256);
352 SetLastError(MAGIC_DEAD
);
353 mem
= LocalLock(gbl
); /* #1 */
354 ok(mem
!= NULL
, "returned %p with %d (expected '!= NULL')\n", mem
, GetLastError());
355 SetLastError(MAGIC_DEAD
);
356 flags
= LocalFlags(gbl
);
357 ok( flags
== 1, "returned 0x%04x with %d (expected '0x0001')\n",
358 flags
, GetLastError());
360 SetLastError(MAGIC_DEAD
);
361 msecond
= LocalLock(gbl
); /* #2 */
362 ok( msecond
== mem
, "returned %p with %d (expected '%p')\n",
363 msecond
, GetLastError(), mem
);
364 SetLastError(MAGIC_DEAD
);
365 flags
= LocalFlags(gbl
);
366 ok( flags
== 2, "returned 0x%04x with %d (expected '0x0002')\n",
367 flags
, GetLastError());
368 SetLastError(MAGIC_DEAD
);
370 SetLastError(MAGIC_DEAD
);
371 res
= LocalUnlock(gbl
); /* #1 */
372 ok(res
, "returned %d with %d (expected '!= 0')\n", res
, GetLastError());
373 SetLastError(MAGIC_DEAD
);
374 flags
= LocalFlags(gbl
);
375 ok( flags
, "returned 0x%04x with %d (expected '!= 0')\n",
376 flags
, GetLastError());
378 SetLastError(MAGIC_DEAD
);
379 res
= LocalUnlock(gbl
); /* #0 */
380 /* NT: ERROR_SUCCESS (documented on MSDN), 9x: untouched */
381 ok(!res
&& ((GetLastError() == ERROR_SUCCESS
) || (GetLastError() == MAGIC_DEAD
)),
382 "returned %d with %d (expected '0' with: ERROR_SUCCESS or "
383 "MAGIC_DEAD)\n", res
, GetLastError());
384 SetLastError(MAGIC_DEAD
);
385 flags
= LocalFlags(gbl
);
386 ok( !flags
, "returned 0x%04x with %d (expected '0')\n",
387 flags
, GetLastError());
389 /* Unlock an already unlocked Handle */
390 SetLastError(MAGIC_DEAD
);
391 res
= LocalUnlock(gbl
);
392 /* NT: ERROR_NOT_LOCKED, 9x: untouched */
394 ((GetLastError() == ERROR_NOT_LOCKED
) || (GetLastError() == MAGIC_DEAD
)),
395 "returned %d with %d (expected '0' with: ERROR_NOT_LOCKED or "
396 "MAGIC_DEAD)\n", res
, GetLastError());
399 /* invalid handles are caught in windows: */
400 SetLastError(MAGIC_DEAD
);
401 hsecond
= LocalFree(gbl
); /* invalid handle: free memory twice */
402 ok( (hsecond
== gbl
) && (GetLastError() == ERROR_INVALID_HANDLE
),
403 "returned %p with 0x%08x (expected %p with ERROR_INVALID_HANDLE)\n",
404 hsecond
, GetLastError(), gbl
);
405 SetLastError(MAGIC_DEAD
);
406 flags
= LocalFlags(gbl
);
407 ok( (flags
== LMEM_INVALID_HANDLE
) && (GetLastError() == ERROR_INVALID_HANDLE
),
408 "returned 0x%04x with 0x%08x (expected LMEM_INVALID_HANDLE with "
409 "ERROR_INVALID_HANDLE)\n", flags
, GetLastError());
410 SetLastError(MAGIC_DEAD
);
411 size
= LocalSize(gbl
);
412 ok( (size
== 0) && (GetLastError() == ERROR_INVALID_HANDLE
),
413 "returned %ld with 0x%08x (expected '0' with ERROR_INVALID_HANDLE)\n",
414 size
, GetLastError());
416 SetLastError(MAGIC_DEAD
);
417 mem
= LocalLock(gbl
);
418 ok( (mem
== NULL
) && (GetLastError() == ERROR_INVALID_HANDLE
),
419 "returned %p with 0x%08x (expected NULL with ERROR_INVALID_HANDLE)\n",
420 mem
, GetLastError());
422 /* This Test works the same on all Systems (GlobalUnlock() is different) */
423 SetLastError(MAGIC_DEAD
);
424 res
= LocalUnlock(gbl
);
425 ok(!res
&& (GetLastError() == ERROR_INVALID_HANDLE
),
426 "returned %d with %d (expected '0' with ERROR_INVALID_HANDLE)\n",
427 res
, GetLastError());
429 /* LMEM_FIXED block expands in place only without flags */
430 for (size
= 1; size
<= max_size
; size
<<= 1) {
431 gbl
= LocalAlloc(LMEM_FIXED
, init_size
);
432 SetLastError(MAGIC_DEAD
);
433 hsecond
= LocalReAlloc(gbl
, size
+ init_size
, 0);
434 ok(hsecond
== gbl
|| (hsecond
== NULL
&& GetLastError() == ERROR_NOT_ENOUGH_MEMORY
),
435 "got %p with %x (expected %p or NULL) @%ld\n", hsecond
, GetLastError(), gbl
, size
);
439 /* LMEM_FIXED memory can be relocated with LMEM_MOVEABLE */
440 for (size
= 1; size
<= max_size
; size
<<= 1) {
441 gbl
= LocalAlloc(LMEM_FIXED
, init_size
);
442 SetLastError(MAGIC_DEAD
);
443 hsecond
= LocalReAlloc(gbl
, size
+ init_size
, LMEM_MOVEABLE
);
445 "got %p with %x (expected non-NULL) @%ld\n", hsecond
, GetLastError(), size
);
446 mem
= LocalLock(hsecond
);
447 ok(mem
== hsecond
, "got %p (expected %p) @%ld\n", mem
, hsecond
, size
);
451 /* trying to unlock pointer from LocalAlloc */
452 gbl
= LocalAlloc(LMEM_FIXED
, 100);
453 SetLastError(0xdeadbeef);
454 res
= LocalUnlock(gbl
);
455 ok(res
== 0, "Expected 0, got %d\n", res
);
456 ok(GetLastError() == ERROR_NOT_LOCKED
||
457 broken(GetLastError() == 0xdeadbeef) /* win9x */, "got %d\n", GetLastError());
460 gbl
= LocalAlloc( LMEM_FIXED
, 0 );
461 SetLastError(0xdeadbeef);
462 size
= LocalSize( gbl
);
463 ok( !size
|| broken(size
== 1), /* vistau64 */
464 "wrong size %lu\n", size
);
467 /* trying to lock empty memory should give an error */
468 gbl
= GlobalAlloc(GMEM_MOVEABLE
|GMEM_ZEROINIT
,0);
469 ok(gbl
!= NULL
, "returned NULL\n");
470 SetLastError(MAGIC_DEAD
);
471 mem
= GlobalLock(gbl
);
472 /* NT: ERROR_DISCARDED, 9x: untouched */
474 ((GetLastError() == ERROR_DISCARDED
) || (GetLastError() == MAGIC_DEAD
)),
475 "returned %p with 0x%x/%d (expected 'NULL' with: ERROR_DISCARDED or "
476 "MAGIC_DEAD)\n", mem
, GetLastError(), GetLastError());
480 /* trying to get size from data pointer (GMEM_MOVEABLE) */
481 gbl
= GlobalAlloc(GMEM_MOVEABLE
, 0x123);
482 ok(gbl
!= NULL
, "returned NULL\n");
483 mem
= GlobalLock(gbl
);
484 ok(mem
!= NULL
, "returned NULL.\n");
485 ok(gbl
!= mem
, "unexpectedly equal.\n");
487 size
= GlobalSize(gbl
);
488 size2
= GlobalSize(mem
);
489 ok(size
== 0x123, "got %lu\n", size
);
490 ok(size2
== 0x123, "got %lu\n", size2
);
494 /* trying to get size from data pointer (GMEM_FIXED) */
495 gbl
= GlobalAlloc(GMEM_FIXED
, 0x123);
496 ok(gbl
!= NULL
, "returned NULL\n");
497 mem
= GlobalLock(gbl
);
498 ok(mem
!= NULL
, "returned NULL.\n");
499 ok(gbl
== mem
, "got %p, %p.\n", gbl
, mem
);
501 size
= GlobalSize(gbl
);
502 ok(size
== 0x123, "got %lu\n", size
);
506 size
= GlobalSize((void *)0xdeadbee0);
507 ok(size
== 0, "got %lu\n", size
);
512 static void test_HeapCreate(void)
517 LPVOID mem1
,mem1a
,mem3
;
523 /* Retrieve the page size for this system */
524 GetSystemInfo(&sysInfo
);
525 ok(sysInfo
.dwPageSize
>0,"GetSystemInfo should return a valid page size\n");
527 /* Create a Heap with a minimum and maximum size */
528 /* Note that Windows and Wine seem to behave a bit differently with respect
529 to memory allocation. In Windows, you can't access all the memory
530 specified in the heap (due to overhead), so choosing a reasonable maximum
531 size for the heap was done mostly by trial-and-error on Win2k. It may need
532 more tweaking for otherWindows variants.
534 memchunk
=10*sysInfo
.dwPageSize
;
535 heap
=HeapCreate(0,2*memchunk
,5*memchunk
);
536 ok( !((ULONG_PTR
)heap
& 0xffff), "heap %p not 64K aligned\n", heap
);
538 /* Check that HeapCreate allocated the right amount of ram */
539 mem1
=HeapAlloc(heap
,0,5*memchunk
+1);
540 ok(mem1
==NULL
,"HeapCreate allocated more Ram than it should have\n");
541 HeapFree(heap
,0,mem1
);
543 /* Check that a normal alloc works */
544 mem1
=HeapAlloc(heap
,0,memchunk
);
545 ok(mem1
!=NULL
,"HeapAlloc failed\n");
547 ok(HeapSize(heap
,0,mem1
)>=memchunk
, "HeapAlloc should return a big enough memory block\n");
550 /* Check that a 'zeroing' alloc works */
551 mem2
=HeapAlloc(heap
,HEAP_ZERO_MEMORY
,memchunk
);
552 ok(mem2
!=NULL
,"HeapAlloc failed\n");
554 ok(HeapSize(heap
,0,mem2
)>=memchunk
,"HeapAlloc should return a big enough memory block\n");
556 for(i
=0;i
<memchunk
;i
++) {
561 ok(!error
,"HeapAlloc should have zeroed out its allocated memory\n");
564 /* Check that HeapAlloc returns NULL when requested way too much memory */
565 mem3
=HeapAlloc(heap
,0,5*memchunk
);
566 ok(mem3
==NULL
,"HeapAlloc should return NULL\n");
568 ok(HeapFree(heap
,0,mem3
),"HeapFree didn't pass successfully\n");
571 /* Check that HeapRealloc works */
572 mem2a
=HeapReAlloc(heap
,HEAP_ZERO_MEMORY
,mem2
,memchunk
+5*sysInfo
.dwPageSize
);
573 ok(mem2a
!=NULL
,"HeapReAlloc failed\n");
575 ok(HeapSize(heap
,0,mem2a
)>=memchunk
+5*sysInfo
.dwPageSize
,"HeapReAlloc failed\n");
577 for(i
=0;i
<5*sysInfo
.dwPageSize
;i
++) {
578 if(mem2a
[memchunk
+i
]!=0) {
582 ok(!error
,"HeapReAlloc should have zeroed out its allocated memory\n");
585 /* Check that HeapRealloc honours HEAP_REALLOC_IN_PLACE_ONLY */
587 mem1a
=HeapReAlloc(heap
,HEAP_REALLOC_IN_PLACE_ONLY
,mem1
,memchunk
+sysInfo
.dwPageSize
);
593 ok(mem1a
==NULL
|| !error
,"HeapReAlloc didn't honour HEAP_REALLOC_IN_PLACE_ONLY\n");
595 /* Check that HeapFree works correctly */
597 ok(HeapFree(heap
,0,mem1a
),"HeapFree failed\n");
599 ok(HeapFree(heap
,0,mem1
),"HeapFree failed\n");
602 ok(HeapFree(heap
,0,mem2a
),"HeapFree failed\n");
604 ok(HeapFree(heap
,0,mem2
),"HeapFree failed\n");
607 /* 0-length buffer */
608 mem1
= HeapAlloc(heap
, 0, 0);
609 ok(mem1
!= NULL
, "Reserved memory\n");
611 dwSize
= HeapSize(heap
, 0, mem1
);
612 /* should work with 0-length buffer */
613 ok(dwSize
< 0xFFFFFFFF, "The size of the 0-length buffer\n");
614 ok(HeapFree(heap
, 0, mem1
), "Freed the 0-length buffer\n");
616 /* Check that HeapDestroy works */
617 ok(HeapDestroy(heap
),"HeapDestroy failed\n");
621 static void test_GlobalAlloc(void)
624 HGLOBAL mem1
,mem2
,mem2a
,mem2b
;
630 SetLastError(NO_ERROR
);
631 /* Check that a normal alloc works */
632 mem1
=GlobalAlloc(0,memchunk
);
633 ok(mem1
!=NULL
,"GlobalAlloc failed\n");
635 ok(GlobalSize(mem1
)>=memchunk
, "GlobalAlloc should return a big enough memory block\n");
638 /* Check that a 'zeroing' alloc works */
639 mem2
=GlobalAlloc(GMEM_ZEROINIT
,memchunk
);
640 ok(mem2
!=NULL
,"GlobalAlloc failed: error=%d\n",GetLastError());
642 ok(GlobalSize(mem2
)>=memchunk
,"GlobalAlloc should return a big enough memory block\n");
643 mem2ptr
=GlobalLock(mem2
);
644 ok(mem2ptr
==mem2
,"GlobalLock should have returned the same memory as was allocated\n");
647 for(i
=0;i
<memchunk
;i
++) {
652 ok(!error
,"GlobalAlloc should have zeroed out its allocated memory\n");
655 /* Check that GlobalReAlloc works */
656 /* Check that we can change GMEM_FIXED to GMEM_MOVEABLE */
657 mem2a
=GlobalReAlloc(mem2
,0,GMEM_MODIFY
| GMEM_MOVEABLE
);
660 mem2ptr
=GlobalLock(mem2a
);
661 ok(mem2ptr
!=NULL
&& !GlobalUnlock(mem2a
)&&GetLastError()==NO_ERROR
,
662 "Converting from FIXED to MOVEABLE didn't REALLY work\n");
665 /* Check that ReAllocing memory works as expected */
666 mem2a
=GlobalReAlloc(mem2
,2*memchunk
,GMEM_MOVEABLE
| GMEM_ZEROINIT
);
667 ok(mem2a
!=NULL
,"GlobalReAlloc failed\n");
669 ok(GlobalSize(mem2a
)>=2*memchunk
,"GlobalReAlloc failed\n");
670 mem2ptr
=GlobalLock(mem2a
);
671 ok(mem2ptr
!=NULL
,"GlobalLock Failed\n");
674 for(i
=0;i
<memchunk
;i
++) {
675 if(mem2ptr
[memchunk
+i
]!=0) {
679 ok(!error
,"GlobalReAlloc should have zeroed out its allocated memory\n");
681 /* Check that GlobalHandle works */
682 mem2b
=GlobalHandle(mem2ptr
);
683 ok(mem2b
==mem2a
,"GlobalHandle didn't return the correct memory handle %p/%p for %p\n",
684 mem2a
, mem2b
, mem2ptr
);
685 /* Check that we can't discard locked memory */
686 mem2b
=GlobalDiscard(mem2a
);
688 ok(!GlobalUnlock(mem2a
) && GetLastError()==NO_ERROR
,"GlobalUnlock Failed\n");
693 ok(GlobalFree(mem1
)==NULL
,"GlobalFree failed\n");
696 ok(GlobalFree(mem2a
)==NULL
,"GlobalFree failed\n");
698 ok(GlobalFree(mem2
)==NULL
,"GlobalFree failed\n");
703 static void test_LocalAlloc(void)
706 HLOCAL mem1
,mem2
,mem2a
,mem2b
;
712 /* Check that a normal alloc works */
713 mem1
=LocalAlloc(0,memchunk
);
714 ok(mem1
!=NULL
,"LocalAlloc failed: error=%d\n",GetLastError());
716 ok(LocalSize(mem1
)>=memchunk
, "LocalAlloc should return a big enough memory block\n");
719 /* Check that a 'zeroing' and lock alloc works */
720 mem2
=LocalAlloc(LMEM_ZEROINIT
|LMEM_MOVEABLE
,memchunk
);
721 ok(mem2
!=NULL
,"LocalAlloc failed: error=%d\n",GetLastError());
723 ok(LocalSize(mem2
)>=memchunk
,"LocalAlloc should return a big enough memory block\n");
724 mem2ptr
=LocalLock(mem2
);
725 ok(mem2ptr
!=NULL
,"LocalLock: error=%d\n",GetLastError());
728 for(i
=0;i
<memchunk
;i
++) {
733 ok(!error
,"LocalAlloc should have zeroed out its allocated memory\n");
735 error
=LocalUnlock(mem2
);
736 ok(!error
&& GetLastError()==NO_ERROR
,
737 "LocalUnlock Failed: rc=%d err=%d\n",error
,GetLastError());
740 mem2a
=LocalFree(mem2
);
741 ok(mem2a
==NULL
, "LocalFree failed: %p\n",mem2a
);
743 /* Reallocate mem2 as moveable memory */
744 mem2
=LocalAlloc(LMEM_MOVEABLE
| LMEM_ZEROINIT
,memchunk
);
745 ok(mem2
!=NULL
, "LocalAlloc failed to create moveable memory, error=%d\n",GetLastError());
747 /* Check that ReAllocing memory works as expected */
748 mem2a
=LocalReAlloc(mem2
,2*memchunk
,LMEM_MOVEABLE
| LMEM_ZEROINIT
);
749 ok(mem2a
!=NULL
,"LocalReAlloc failed, error=%d\n",GetLastError());
751 ok(LocalSize(mem2a
)>=2*memchunk
,"LocalReAlloc failed\n");
752 mem2ptr
=LocalLock(mem2a
);
753 ok(mem2ptr
!=NULL
,"LocalLock Failed\n");
756 for(i
=0;i
<memchunk
;i
++) {
757 if(mem2ptr
[memchunk
+i
]!=0) {
761 ok(!error
,"LocalReAlloc should have zeroed out its allocated memory\n");
762 /* Check that LocalHandle works */
763 mem2b
=LocalHandle(mem2ptr
);
764 ok(mem2b
==mem2a
,"LocalHandle didn't return the correct memory handle %p/%p for %p\n",
765 mem2a
, mem2b
, mem2ptr
);
766 /* Check that we can't discard locked memory */
767 mem2b
=LocalDiscard(mem2a
);
768 ok(mem2b
==NULL
,"Discarded memory we shouldn't have\n");
769 SetLastError(NO_ERROR
);
770 ok(!LocalUnlock(mem2a
) && GetLastError()==NO_ERROR
, "LocalUnlock Failed\n");
774 ok(LocalFree(mem1
)==NULL
,"LocalFree failed\n");
777 ok(LocalFree(mem2a
)==NULL
,"LocalFree failed\n");
779 ok(LocalFree(mem2
)==NULL
,"LocalFree failed\n");
783 static void test_obsolete_flags(void)
788 } test_global_flags
[] = {
789 {GMEM_FIXED
| GMEM_NOTIFY
, 0},
790 {GMEM_FIXED
| GMEM_DISCARDABLE
, 0},
791 {GMEM_MOVEABLE
| GMEM_NOTIFY
, 0},
792 {GMEM_MOVEABLE
| GMEM_DDESHARE
, GMEM_DDESHARE
},
793 {GMEM_MOVEABLE
| GMEM_NOT_BANKED
, 0},
794 {GMEM_MOVEABLE
| GMEM_NODISCARD
, 0},
795 {GMEM_MOVEABLE
| GMEM_DISCARDABLE
, GMEM_DISCARDABLE
},
796 {GMEM_MOVEABLE
| GMEM_DDESHARE
| GMEM_DISCARDABLE
| GMEM_LOWER
| GMEM_NOCOMPACT
| GMEM_NODISCARD
|
797 GMEM_NOT_BANKED
| GMEM_NOTIFY
, GMEM_DDESHARE
| GMEM_DISCARDABLE
},
804 UINT (WINAPI
*pGlobalFlags
)(HGLOBAL
);
806 pGlobalFlags
= (void *) GetProcAddress(GetModuleHandleA("kernel32"), "GlobalFlags");
810 win_skip("GlobalFlags is not available\n");
814 for (i
= 0; i
< sizeof(test_global_flags
)/sizeof(test_global_flags
[0]); i
++)
816 gbl
= GlobalAlloc(test_global_flags
[i
].flags
, 4);
817 ok(gbl
!= NULL
, "GlobalAlloc failed\n");
819 SetLastError(MAGIC_DEAD
);
820 resultflags
= pGlobalFlags(gbl
);
822 ok( resultflags
== test_global_flags
[i
].globalflags
||
823 broken(resultflags
== (test_global_flags
[i
].globalflags
& ~GMEM_DDESHARE
)), /* win9x */
824 "%u: expected 0x%08x, but returned 0x%08x with %d\n",
825 i
, test_global_flags
[i
].globalflags
, resultflags
, GetLastError() );
831 static void test_HeapQueryInformation(void)
837 pHeapQueryInformation
= (void *)GetProcAddress(GetModuleHandleA("kernel32.dll"), "HeapQueryInformation");
838 if (!pHeapQueryInformation
)
840 win_skip("HeapQueryInformation is not available\n");
844 if (0) /* crashes under XP */
847 pHeapQueryInformation(0,
848 HeapCompatibilityInformation
,
849 &info
, sizeof(info
), &size
);
851 pHeapQueryInformation(GetProcessHeap(),
852 HeapCompatibilityInformation
,
853 NULL
, sizeof(info
), &size
);
857 SetLastError(0xdeadbeef);
858 ret
= pHeapQueryInformation(GetProcessHeap(),
859 HeapCompatibilityInformation
,
861 ok(!ret
, "HeapQueryInformation should fail\n");
862 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
863 "expected ERROR_INSUFFICIENT_BUFFER got %u\n", GetLastError());
864 ok(size
== sizeof(ULONG
), "expected 4, got %lu\n", size
);
866 SetLastError(0xdeadbeef);
867 ret
= pHeapQueryInformation(GetProcessHeap(),
868 HeapCompatibilityInformation
,
870 ok(!ret
, "HeapQueryInformation should fail\n");
871 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
872 "expected ERROR_INSUFFICIENT_BUFFER got %u\n", GetLastError());
875 SetLastError(0xdeadbeef);
876 ret
= pHeapQueryInformation(GetProcessHeap(),
877 HeapCompatibilityInformation
,
878 &info
, sizeof(info
) + 1, NULL
);
879 ok(ret
, "HeapQueryInformation error %u\n", GetLastError());
880 ok(info
== 0 || info
== 1 || info
== 2, "expected 0, 1 or 2, got %u\n", info
);
883 static void test_heap_checks( DWORD flags
)
887 SIZE_T i
, size
, large_size
= 3000 * 1024 + 37;
889 if (flags
& HEAP_PAGE_ALLOCS
) return; /* no tests for that case yet */
890 trace( "testing heap flags %08x\n", flags
);
892 p
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, 17 );
893 ok( p
!= NULL
, "HeapAlloc failed\n" );
895 ret
= HeapValidate( GetProcessHeap(), 0, p
);
896 ok( ret
, "HeapValidate failed\n" );
898 size
= HeapSize( GetProcessHeap(), 0, p
);
899 ok( size
== 17, "Wrong size %lu\n", size
);
901 ok( p
[14] == 0, "wrong data %x\n", p
[14] );
902 ok( p
[15] == 0, "wrong data %x\n", p
[15] );
903 ok( p
[16] == 0, "wrong data %x\n", p
[16] );
905 if (flags
& HEAP_TAIL_CHECKING_ENABLED
)
907 ok( p
[17] == 0xab, "wrong padding %x\n", p
[17] );
908 ok( p
[18] == 0xab, "wrong padding %x\n", p
[18] );
909 ok( p
[19] == 0xab, "wrong padding %x\n", p
[19] );
912 p2
= HeapReAlloc( GetProcessHeap(), HEAP_REALLOC_IN_PLACE_ONLY
, p
, 14 );
915 if (flags
& HEAP_TAIL_CHECKING_ENABLED
)
917 ok( p
[14] == 0xab, "wrong padding %x\n", p
[14] );
918 ok( p
[15] == 0xab, "wrong padding %x\n", p
[15] );
919 ok( p
[16] == 0xab, "wrong padding %x\n", p
[16] );
923 ok( p
[14] == 0, "wrong padding %x\n", p
[14] );
924 ok( p
[15] == 0, "wrong padding %x\n", p
[15] );
927 else skip( "realloc in place failed\n");
929 ret
= HeapFree( GetProcessHeap(), 0, p
);
930 ok( ret
, "HeapFree failed\n" );
932 p
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, 17 );
933 ok( p
!= NULL
, "HeapAlloc failed\n" );
937 if (flags
& HEAP_TAIL_CHECKING_ENABLED
)
939 ret
= HeapValidate( GetProcessHeap(), 0, p
);
940 ok( !ret
, "HeapValidate succeeded\n" );
942 /* other calls only check when HEAP_VALIDATE is set */
943 if (flags
& HEAP_VALIDATE
)
945 size
= HeapSize( GetProcessHeap(), 0, p
);
946 ok( size
== ~(SIZE_T
)0 || broken(size
== ~0u), "Wrong size %lu\n", size
);
948 p2
= HeapReAlloc( GetProcessHeap(), 0, p
, 14 );
949 ok( p2
== NULL
, "HeapReAlloc succeeded\n" );
951 ret
= HeapFree( GetProcessHeap(), 0, p
);
952 ok( !ret
|| broken(sizeof(void*) == 8), /* not caught on xp64 */
953 "HeapFree succeeded\n" );
957 size
= HeapSize( GetProcessHeap(), 0, p
);
958 ok( size
== 17, "Wrong size %lu\n", size
);
960 p2
= HeapReAlloc( GetProcessHeap(), 0, p
, 14 );
961 ok( p2
!= NULL
, "HeapReAlloc failed\n" );
965 ret
= HeapFree( GetProcessHeap(), 0, p
);
966 ok( ret
, "HeapFree failed\n" );
968 p
= HeapAlloc( GetProcessHeap(), 0, 37 );
969 ok( p
!= NULL
, "HeapAlloc failed\n" );
970 memset( p
, 0xcc, 37 );
972 ret
= HeapFree( GetProcessHeap(), 0, p
);
973 ok( ret
, "HeapFree failed\n" );
975 if (flags
& HEAP_FREE_CHECKING_ENABLED
)
977 ok( p
[16] == 0xee, "wrong data %x\n", p
[16] );
978 ok( p
[17] == 0xfe, "wrong data %x\n", p
[17] );
979 ok( p
[18] == 0xee, "wrong data %x\n", p
[18] );
980 ok( p
[19] == 0xfe, "wrong data %x\n", p
[19] );
982 ret
= HeapValidate( GetProcessHeap(), 0, NULL
);
983 ok( ret
, "HeapValidate failed\n" );
987 ret
= HeapValidate( GetProcessHeap(), 0, NULL
);
988 ok( !ret
, "HeapValidate succeeded\n" );
991 ret
= HeapValidate( GetProcessHeap(), 0, NULL
);
992 ok( ret
, "HeapValidate failed\n" );
995 /* now test large blocks */
997 p
= HeapAlloc( GetProcessHeap(), 0, large_size
);
998 ok( p
!= NULL
, "HeapAlloc failed\n" );
1000 ret
= HeapValidate( GetProcessHeap(), 0, p
);
1001 ok( ret
, "HeapValidate failed\n" );
1003 size
= HeapSize( GetProcessHeap(), 0, p
);
1004 ok( size
== large_size
, "Wrong size %lu\n", size
);
1006 ok( p
[large_size
- 2] == 0, "wrong data %x\n", p
[large_size
- 2] );
1007 ok( p
[large_size
- 1] == 0, "wrong data %x\n", p
[large_size
- 1] );
1009 if (flags
& HEAP_TAIL_CHECKING_ENABLED
)
1011 /* Windows doesn't do tail checking on large blocks */
1012 ok( p
[large_size
] == 0xab || broken(p
[large_size
] == 0), "wrong data %x\n", p
[large_size
] );
1013 ok( p
[large_size
+1] == 0xab || broken(p
[large_size
+1] == 0), "wrong data %x\n", p
[large_size
+1] );
1014 ok( p
[large_size
+2] == 0xab || broken(p
[large_size
+2] == 0), "wrong data %x\n", p
[large_size
+2] );
1015 if (p
[large_size
] == 0xab)
1017 p
[large_size
] = 0xcc;
1018 ret
= HeapValidate( GetProcessHeap(), 0, p
);
1019 ok( !ret
, "HeapValidate succeeded\n" );
1021 /* other calls only check when HEAP_VALIDATE is set */
1022 if (flags
& HEAP_VALIDATE
)
1024 size
= HeapSize( GetProcessHeap(), 0, p
);
1025 ok( size
== ~(SIZE_T
)0, "Wrong size %lu\n", size
);
1027 p2
= HeapReAlloc( GetProcessHeap(), 0, p
, large_size
- 3 );
1028 ok( p2
== NULL
, "HeapReAlloc succeeded\n" );
1030 ret
= HeapFree( GetProcessHeap(), 0, p
);
1031 ok( !ret
, "HeapFree succeeded\n" );
1033 p
[large_size
] = 0xab;
1037 ret
= HeapFree( GetProcessHeap(), 0, p
);
1038 ok( ret
, "HeapFree failed\n" );
1040 /* test block sizes when tail checking */
1041 if (flags
& HEAP_TAIL_CHECKING_ENABLED
)
1043 for (size
= 0; size
< 64; size
++)
1045 p
= HeapAlloc( GetProcessHeap(), 0, size
);
1046 for (i
= 0; i
< 32; i
++) if (p
[size
+ i
] != 0xab) break;
1047 ok( i
>= 8, "only %lu tail bytes for size %lu\n", i
, size
);
1048 HeapFree( GetProcessHeap(), 0, p
);
1053 static void test_debug_heap( const char *argv0
, DWORD flags
)
1055 char keyname
[MAX_PATH
];
1056 char buffer
[MAX_PATH
];
1057 PROCESS_INFORMATION info
;
1058 STARTUPINFOA startup
;
1062 const char *basename
;
1064 if ((basename
= strrchr( argv0
, '\\' ))) basename
++;
1065 else basename
= argv0
;
1067 sprintf( keyname
, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Image File Execution Options\\%s",
1069 if (!strcmp( keyname
+ strlen(keyname
) - 3, ".so" )) keyname
[strlen(keyname
) - 3] = 0;
1071 err
= RegCreateKeyA( HKEY_LOCAL_MACHINE
, keyname
, &hkey
);
1072 if (err
== ERROR_ACCESS_DENIED
)
1074 skip("Not authorized to change the image file execution options\n");
1077 ok( !err
, "failed to create '%s' error %u\n", keyname
, err
);
1080 if (flags
== 0xdeadbeef) /* magic value for unsetting it */
1081 RegDeleteValueA( hkey
, "GlobalFlag" );
1083 RegSetValueExA( hkey
, "GlobalFlag", 0, REG_DWORD
, (BYTE
*)&flags
, sizeof(flags
) );
1085 memset( &startup
, 0, sizeof(startup
) );
1086 startup
.cb
= sizeof(startup
);
1088 sprintf( buffer
, "%s heap.c 0x%x", argv0
, flags
);
1089 ret
= CreateProcessA( NULL
, buffer
, NULL
, NULL
, FALSE
, 0, NULL
, NULL
, &startup
, &info
);
1090 ok( ret
, "failed to create child process error %u\n", GetLastError() );
1093 winetest_wait_child_process( info
.hProcess
);
1094 CloseHandle( info
.hThread
);
1095 CloseHandle( info
.hProcess
);
1097 RegDeleteValueA( hkey
, "GlobalFlag" );
1098 RegCloseKey( hkey
);
1099 RegDeleteKeyA( HKEY_LOCAL_MACHINE
, keyname
);
1102 static DWORD
heap_flags_from_global_flag( DWORD flag
)
1106 if (flag
& FLG_HEAP_ENABLE_TAIL_CHECK
)
1107 ret
|= HEAP_TAIL_CHECKING_ENABLED
;
1108 if (flag
& FLG_HEAP_ENABLE_FREE_CHECK
)
1109 ret
|= HEAP_FREE_CHECKING_ENABLED
;
1110 if (flag
& FLG_HEAP_VALIDATE_PARAMETERS
)
1111 ret
|= HEAP_VALIDATE_PARAMS
| HEAP_VALIDATE
| HEAP_TAIL_CHECKING_ENABLED
| HEAP_FREE_CHECKING_ENABLED
;
1112 if (flag
& FLG_HEAP_VALIDATE_ALL
)
1113 ret
|= HEAP_VALIDATE_ALL
| HEAP_VALIDATE
| HEAP_TAIL_CHECKING_ENABLED
| HEAP_FREE_CHECKING_ENABLED
;
1114 if (flag
& FLG_HEAP_DISABLE_COALESCING
)
1115 ret
|= HEAP_DISABLE_COALESCE_ON_FREE
;
1116 if (flag
& FLG_HEAP_PAGE_ALLOCS
)
1117 ret
|= HEAP_PAGE_ALLOCS
| HEAP_GROWABLE
;
1121 static void test_child_heap( const char *arg
)
1123 struct heap_layout
*heap
= GetProcessHeap();
1124 DWORD expected
= strtoul( arg
, 0, 16 );
1127 if (expected
== 0xdeadbeef) /* expected value comes from Session Manager global flags */
1131 if (!RegOpenKeyA( HKEY_LOCAL_MACHINE
, "SYSTEM\\CurrentControlSet\\Control\\Session Manager", &hkey
))
1134 DWORD type
, size
= sizeof(buffer
);
1136 if (!RegQueryValueExA( hkey
, "GlobalFlag", 0, &type
, (BYTE
*)buffer
, &size
))
1138 if (type
== REG_DWORD
) expected
= *(DWORD
*)buffer
;
1139 else if (type
== REG_SZ
) expected
= strtoul( buffer
, 0, 16 );
1141 RegCloseKey( hkey
);
1144 if (expected
&& !pRtlGetNtGlobalFlags()) /* not working on NT4 */
1146 win_skip( "global flags not set\n" );
1150 ok( pRtlGetNtGlobalFlags() == expected
,
1151 "%s: got global flags %08x expected %08x\n", arg
, pRtlGetNtGlobalFlags(), expected
);
1153 expect_heap
= heap_flags_from_global_flag( expected
);
1155 if (!(heap
->flags
& HEAP_GROWABLE
) || heap
->pattern
== 0xffeeffee) /* vista layout */
1157 ok( (heap
->flags
& ~HEAP_GROWABLE
) == 0, "%s: got heap flags %08x\n", arg
, heap
->flags
);
1159 else if (heap
->pattern
== 0xeeeeeeee && heap
->flags
== 0xeeeeeeee)
1161 ok( expected
& FLG_HEAP_PAGE_ALLOCS
, "%s: got heap flags 0xeeeeeeee without page alloc\n", arg
);
1165 ok( heap
->flags
== (expect_heap
| HEAP_GROWABLE
),
1166 "%s: got heap flags %08x expected %08x\n", arg
, heap
->flags
, expect_heap
);
1167 ok( heap
->force_flags
== (expect_heap
& ~0x18000080),
1168 "%s: got heap force flags %08x expected %08x\n", arg
, heap
->force_flags
, expect_heap
);
1169 expect_heap
= heap
->flags
;
1172 test_heap_checks( expect_heap
);
1175 static void test_GetPhysicallyInstalledSystemMemory(void)
1177 HMODULE kernel32
= GetModuleHandleA("kernel32.dll");
1178 MEMORYSTATUSEX memstatus
;
1179 ULONGLONG total_memory
;
1182 pGetPhysicallyInstalledSystemMemory
= (void *)GetProcAddress(kernel32
, "GetPhysicallyInstalledSystemMemory");
1183 if (!pGetPhysicallyInstalledSystemMemory
)
1185 win_skip("GetPhysicallyInstalledSystemMemory is not available\n");
1189 SetLastError(0xdeadbeef);
1190 ret
= pGetPhysicallyInstalledSystemMemory(NULL
);
1191 ok(!ret
, "GetPhysicallyInstalledSystemMemory should fail\n");
1192 ok(GetLastError() == ERROR_INVALID_PARAMETER
,
1193 "expected ERROR_INVALID_PARAMETER, got %u\n", GetLastError());
1196 ret
= pGetPhysicallyInstalledSystemMemory(&total_memory
);
1197 ok(ret
, "GetPhysicallyInstalledSystemMemory unexpectedly failed\n");
1198 ok(total_memory
!= 0, "expected total_memory != 0\n");
1200 memstatus
.dwLength
= sizeof(memstatus
);
1201 ret
= GlobalMemoryStatusEx(&memstatus
);
1202 ok(ret
, "GlobalMemoryStatusEx unexpectedly failed\n");
1203 ok(total_memory
>= memstatus
.ullTotalPhys
/ 1024,
1204 "expected total_memory >= memstatus.ullTotalPhys / 1024\n");
1212 pRtlGetNtGlobalFlags
= (void *)GetProcAddress( GetModuleHandleA("ntdll.dll"), "RtlGetNtGlobalFlags" );
1214 argc
= winetest_get_mainargs( &argv
);
1217 test_child_heap( argv
[2] );
1222 test_obsolete_flags();
1227 /* Test both short and very long blocks */
1228 test_sized_HeapAlloc(1);
1229 test_sized_HeapAlloc(1 << 20);
1230 test_sized_HeapReAlloc(1, 100);
1231 test_sized_HeapReAlloc(1, (1 << 20));
1232 test_sized_HeapReAlloc((1 << 20), (2 << 20));
1233 test_sized_HeapReAlloc((1 << 20), 1);
1235 test_HeapQueryInformation();
1236 test_GetPhysicallyInstalledSystemMemory();
1238 if (pRtlGetNtGlobalFlags
)
1240 test_debug_heap( argv
[0], 0 );
1241 test_debug_heap( argv
[0], FLG_HEAP_ENABLE_TAIL_CHECK
);
1242 test_debug_heap( argv
[0], FLG_HEAP_ENABLE_FREE_CHECK
);
1243 test_debug_heap( argv
[0], FLG_HEAP_VALIDATE_PARAMETERS
);
1244 test_debug_heap( argv
[0], FLG_HEAP_VALIDATE_ALL
);
1245 test_debug_heap( argv
[0], FLG_POOL_ENABLE_TAGGING
);
1246 test_debug_heap( argv
[0], FLG_HEAP_ENABLE_TAGGING
);
1247 test_debug_heap( argv
[0], FLG_HEAP_ENABLE_TAG_BY_DLL
);
1248 test_debug_heap( argv
[0], FLG_HEAP_DISABLE_COALESCING
);
1249 test_debug_heap( argv
[0], FLG_HEAP_PAGE_ALLOCS
);
1250 test_debug_heap( argv
[0], 0xdeadbeef );
1252 else win_skip( "RtlGetNtGlobalFlags not found, skipping heap debug tests\n" );