2 * Unit test suite for heap functions
4 * Copyright 2003 Dimitrie O. Paun
5 * Copyright 2006 Detlef Riekenberg
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
30 #include "wine/test.h"
32 #define MAGIC_DEAD 0xdeadbeef
34 /* some undocumented flags (names are made up) */
35 #define HEAP_PAGE_ALLOCS 0x01000000
36 #define HEAP_VALIDATE 0x10000000
37 #define HEAP_VALIDATE_ALL 0x20000000
38 #define HEAP_VALIDATE_PARAMS 0x40000000
40 static BOOL (WINAPI *pHeapQueryInformation)(HANDLE, HEAP_INFORMATION_CLASS, PVOID, SIZE_T, PSIZE_T);
41 static ULONG (WINAPI *pRtlGetNtGlobalFlags)(void);
50 static SIZE_T resize_9x(SIZE_T size)
52 DWORD dwSizeAligned = (size + 3) & ~3;
53 return max(dwSizeAligned, 12); /* at least 12 bytes */
56 static void test_sized_HeapAlloc(int nbytes)
59 char *buf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, nbytes);
60 ok(buf != NULL, "allocate failed\n");
61 ok(buf[0] == 0, "buffer not zeroed\n");
62 success = HeapFree(GetProcessHeap(), 0, buf);
63 ok(success, "free failed\n");
66 static void test_sized_HeapReAlloc(int nbytes1, int nbytes2)
69 char *buf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, nbytes1);
70 ok(buf != NULL, "allocate failed\n");
71 ok(buf[0] == 0, "buffer not zeroed\n");
72 buf = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, buf, nbytes2);
73 ok(buf != NULL, "reallocate failed\n");
74 ok(buf[nbytes2-1] == 0, "buffer not zeroed\n");
75 success = HeapFree(GetProcessHeap(), 0, buf);
76 ok(success, "free failed\n");
79 static void test_heap(void)
89 /* Heap*() functions */
90 mem = HeapAlloc(GetProcessHeap(), 0, 0);
91 ok(mem != NULL, "memory not allocated for size 0\n");
92 HeapFree(GetProcessHeap(), 0, mem);
94 mem = HeapReAlloc(GetProcessHeap(), 0, NULL, 10);
95 ok(mem == NULL, "memory allocated by HeapReAlloc\n");
97 for (size = 0; size <= 256; size++)
100 mem = HeapAlloc(GetProcessHeap(), 0, size);
101 heap_size = HeapSize(GetProcessHeap(), 0, mem);
102 ok(heap_size == size || heap_size == resize_9x(size),
103 "HeapSize returned %lu instead of %lu or %lu\n", heap_size, size, resize_9x(size));
104 HeapFree(GetProcessHeap(), 0, mem);
107 /* test some border cases of HeapAlloc and HeapReAlloc */
108 mem = HeapAlloc(GetProcessHeap(), 0, 0);
109 ok(mem != NULL, "memory not allocated for size 0\n");
110 msecond = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, mem, ~(SIZE_T)0 - 7);
111 ok(msecond == NULL, "HeapReAlloc(~0 - 7) should have failed\n");
112 msecond = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, mem, ~(SIZE_T)0);
113 ok(msecond == NULL, "HeapReAlloc(~0) should have failed\n");
114 HeapFree(GetProcessHeap(), 0, mem);
115 mem = HeapAlloc(GetProcessHeap(), 0, ~(SIZE_T)0);
116 ok(mem == NULL, "memory allocated for size ~0\n");
118 /* large blocks must be 16-byte aligned */
119 mem = HeapAlloc(GetProcessHeap(), 0, 512 * 1024);
120 ok( mem != NULL, "failed for size 512K\n" );
121 ok( (ULONG_PTR)mem % 16 == 0 || broken((ULONG_PTR)mem % 16) /* win9x */,
122 "512K block not 16-byte aligned\n" );
123 HeapFree(GetProcessHeap(), 0, mem);
125 /* Global*() functions */
126 gbl = GlobalAlloc(GMEM_MOVEABLE, 0);
127 ok(gbl != NULL, "global memory not allocated for size 0\n");
129 gbl = GlobalReAlloc(gbl, 10, GMEM_MOVEABLE);
130 ok(gbl != NULL, "Can't realloc global memory\n");
131 size = GlobalSize(gbl);
132 ok(size >= 10 && size <= 16, "Memory not resized to size 10, instead size=%ld\n", size);
134 gbl = GlobalReAlloc(gbl, 0, GMEM_MOVEABLE);
135 ok(gbl != NULL, "GlobalReAlloc should not fail on size 0\n");
137 size = GlobalSize(gbl);
138 ok(size == 0, "Memory not resized to size 0, instead size=%ld\n", size);
139 ok(GlobalFree(gbl) == NULL, "Memory not freed\n");
140 size = GlobalSize(gbl);
141 ok(size == 0, "Memory should have been freed, size=%ld\n", size);
143 gbl = GlobalReAlloc(0, 10, GMEM_MOVEABLE);
144 ok(gbl == NULL, "global realloc allocated memory\n");
146 /* GlobalLock / GlobalUnlock with a valid handle */
147 gbl = GlobalAlloc(GMEM_MOVEABLE, 256);
149 SetLastError(MAGIC_DEAD);
150 mem = GlobalLock(gbl); /* #1 */
151 ok(mem != NULL, "returned %p with %d (expected '!= NULL')\n", mem, GetLastError());
152 SetLastError(MAGIC_DEAD);
153 flags = GlobalFlags(gbl);
154 ok( flags == 1, "returned 0x%04x with %d (expected '0x0001')\n",
155 flags, GetLastError());
157 SetLastError(MAGIC_DEAD);
158 msecond = GlobalLock(gbl); /* #2 */
159 ok( msecond == mem, "returned %p with %d (expected '%p')\n",
160 msecond, GetLastError(), mem);
161 SetLastError(MAGIC_DEAD);
162 flags = GlobalFlags(gbl);
163 ok( flags == 2, "returned 0x%04x with %d (expected '0x0002')\n",
164 flags, GetLastError());
165 SetLastError(MAGIC_DEAD);
167 SetLastError(MAGIC_DEAD);
168 res = GlobalUnlock(gbl); /* #1 */
169 ok(res, "returned %d with %d (expected '!= 0')\n", res, GetLastError());
170 SetLastError(MAGIC_DEAD);
171 flags = GlobalFlags(gbl);
172 ok( flags , "returned 0x%04x with %d (expected '!= 0')\n",
173 flags, GetLastError());
175 SetLastError(MAGIC_DEAD);
176 res = GlobalUnlock(gbl); /* #0 */
177 /* NT: ERROR_SUCCESS (documented on MSDN), 9x: untouched */
178 ok(!res && ((GetLastError() == ERROR_SUCCESS) || (GetLastError() == MAGIC_DEAD)),
179 "returned %d with %d (expected '0' with: ERROR_SUCCESS or "
180 "MAGIC_DEAD)\n", res, GetLastError());
181 SetLastError(MAGIC_DEAD);
182 flags = GlobalFlags(gbl);
183 ok( !flags , "returned 0x%04x with %d (expected '0')\n",
184 flags, GetLastError());
186 /* Unlock an already unlocked Handle */
187 SetLastError(MAGIC_DEAD);
188 res = GlobalUnlock(gbl);
189 /* NT: ERROR_NOT_LOCKED, 9x: untouched */
191 ((GetLastError() == ERROR_NOT_LOCKED) || (GetLastError() == MAGIC_DEAD)),
192 "returned %d with %d (expected '0' with: ERROR_NOT_LOCKED or "
193 "MAGIC_DEAD)\n", res, GetLastError());
196 /* invalid handles are caught in windows: */
197 SetLastError(MAGIC_DEAD);
198 hsecond = GlobalFree(gbl); /* invalid handle: free memory twice */
199 ok( (hsecond == gbl) && (GetLastError() == ERROR_INVALID_HANDLE),
200 "returned %p with 0x%08x (expected %p with ERROR_INVALID_HANDLE)\n",
201 hsecond, GetLastError(), gbl);
202 SetLastError(MAGIC_DEAD);
203 flags = GlobalFlags(gbl);
204 ok( (flags == GMEM_INVALID_HANDLE) && (GetLastError() == ERROR_INVALID_HANDLE),
205 "returned 0x%04x with 0x%08x (expected GMEM_INVALID_HANDLE with "
206 "ERROR_INVALID_HANDLE)\n", flags, GetLastError());
207 SetLastError(MAGIC_DEAD);
208 size = GlobalSize(gbl);
209 ok( (size == 0) && (GetLastError() == ERROR_INVALID_HANDLE),
210 "returned %ld with 0x%08x (expected '0' with ERROR_INVALID_HANDLE)\n",
211 size, GetLastError());
213 SetLastError(MAGIC_DEAD);
214 mem = GlobalLock(gbl);
215 ok( (mem == NULL) && (GetLastError() == ERROR_INVALID_HANDLE),
216 "returned %p with 0x%08x (expected NULL with ERROR_INVALID_HANDLE)\n",
217 mem, GetLastError());
219 /* documented on MSDN: GlobalUnlock() return FALSE on failure.
220 Win9x and wine return FALSE with ERROR_INVALID_HANDLE, but on
221 NT 3.51 and XPsp2, TRUE with ERROR_INVALID_HANDLE is returned.
222 The similar Test for LocalUnlock() works on all Systems */
223 SetLastError(MAGIC_DEAD);
224 res = GlobalUnlock(gbl);
225 ok(GetLastError() == ERROR_INVALID_HANDLE,
226 "returned %d with %d (expected ERROR_INVALID_HANDLE)\n",
227 res, GetLastError());
229 gbl = GlobalAlloc(GMEM_DDESHARE, 100);
232 mem = GlobalFree(gbl);
233 ok(mem == NULL, "Expected NULL, got %p\n", mem);
236 if (sizeof(void *) != 8) /* crashes on 64-bit Vista */
238 SetLastError(MAGIC_DEAD);
239 mem = GlobalFree(gbl);
240 ok(mem == gbl || broken(mem == NULL) /* nt4 */, "Expected gbl, got %p\n", mem);
242 ok(GetLastError() == ERROR_INVALID_HANDLE ||
243 GetLastError() == ERROR_INVALID_PARAMETER, /* win9x */
244 "Expected ERROR_INVALID_HANDLE or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
247 gbl = GlobalAlloc(GMEM_DDESHARE, 100);
249 res = GlobalUnlock(gbl);
251 res == 0, /* win9x */
252 "Expected 1 or 0, got %d\n", res);
254 res = GlobalUnlock(gbl);
256 res == 0, /* win9x */
257 "Expected 1 or 0, got %d\n", res);
259 /* GlobalSize on an invalid handle */
260 if (sizeof(void *) != 8) /* crashes on 64-bit Vista */
262 SetLastError(MAGIC_DEAD);
263 size = GlobalSize((HGLOBAL)0xc042);
264 ok(size == 0, "Expected 0, got %ld\n", size);
265 ok(GetLastError() == ERROR_INVALID_HANDLE ||
266 GetLastError() == ERROR_INVALID_PARAMETER, /* win9x */
267 "Expected ERROR_INVALID_HANDLE or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
272 /* ####################################### */
273 /* Local*() functions */
274 gbl = LocalAlloc(LMEM_MOVEABLE, 0);
275 ok(gbl != NULL, "local memory not allocated for size 0\n");
277 gbl = LocalReAlloc(gbl, 10, LMEM_MOVEABLE);
278 ok(gbl != NULL, "Can't realloc local memory\n");
279 size = LocalSize(gbl);
280 ok(size >= 10 && size <= 16, "Memory not resized to size 10, instead size=%ld\n", size);
282 gbl = LocalReAlloc(gbl, 0, LMEM_MOVEABLE);
283 ok(gbl != NULL, "LocalReAlloc should not fail on size 0\n");
285 size = LocalSize(gbl);
286 ok(size == 0, "Memory not resized to size 0, instead size=%ld\n", size);
287 ok(LocalFree(gbl) == NULL, "Memory not freed\n");
288 size = LocalSize(gbl);
289 ok(size == 0, "Memory should have been freed, size=%ld\n", size);
291 gbl = LocalReAlloc(0, 10, LMEM_MOVEABLE);
292 ok(gbl == NULL, "local realloc allocated memory\n");
294 /* LocalLock / LocalUnlock with a valid handle */
295 gbl = LocalAlloc(LMEM_MOVEABLE, 256);
296 SetLastError(MAGIC_DEAD);
297 mem = LocalLock(gbl); /* #1 */
298 ok(mem != NULL, "returned %p with %d (expected '!= NULL')\n", mem, GetLastError());
299 SetLastError(MAGIC_DEAD);
300 flags = LocalFlags(gbl);
301 ok( flags == 1, "returned 0x%04x with %d (expected '0x0001')\n",
302 flags, GetLastError());
304 SetLastError(MAGIC_DEAD);
305 msecond = LocalLock(gbl); /* #2 */
306 ok( msecond == mem, "returned %p with %d (expected '%p')\n",
307 msecond, GetLastError(), mem);
308 SetLastError(MAGIC_DEAD);
309 flags = LocalFlags(gbl);
310 ok( flags == 2, "returned 0x%04x with %d (expected '0x0002')\n",
311 flags, GetLastError());
312 SetLastError(MAGIC_DEAD);
314 SetLastError(MAGIC_DEAD);
315 res = LocalUnlock(gbl); /* #1 */
316 ok(res, "returned %d with %d (expected '!= 0')\n", res, GetLastError());
317 SetLastError(MAGIC_DEAD);
318 flags = LocalFlags(gbl);
319 ok( flags , "returned 0x%04x with %d (expected '!= 0')\n",
320 flags, GetLastError());
322 SetLastError(MAGIC_DEAD);
323 res = LocalUnlock(gbl); /* #0 */
324 /* NT: ERROR_SUCCESS (documented on MSDN), 9x: untouched */
325 ok(!res && ((GetLastError() == ERROR_SUCCESS) || (GetLastError() == MAGIC_DEAD)),
326 "returned %d with %d (expected '0' with: ERROR_SUCCESS or "
327 "MAGIC_DEAD)\n", res, GetLastError());
328 SetLastError(MAGIC_DEAD);
329 flags = LocalFlags(gbl);
330 ok( !flags , "returned 0x%04x with %d (expected '0')\n",
331 flags, GetLastError());
333 /* Unlock an already unlocked Handle */
334 SetLastError(MAGIC_DEAD);
335 res = LocalUnlock(gbl);
336 /* NT: ERROR_NOT_LOCKED, 9x: untouched */
338 ((GetLastError() == ERROR_NOT_LOCKED) || (GetLastError() == MAGIC_DEAD)),
339 "returned %d with %d (expected '0' with: ERROR_NOT_LOCKED or "
340 "MAGIC_DEAD)\n", res, GetLastError());
343 /* invalid handles are caught in windows: */
344 SetLastError(MAGIC_DEAD);
345 hsecond = LocalFree(gbl); /* invalid handle: free memory twice */
346 ok( (hsecond == gbl) && (GetLastError() == ERROR_INVALID_HANDLE),
347 "returned %p with 0x%08x (expected %p with ERROR_INVALID_HANDLE)\n",
348 hsecond, GetLastError(), gbl);
349 SetLastError(MAGIC_DEAD);
350 flags = LocalFlags(gbl);
351 ok( (flags == LMEM_INVALID_HANDLE) && (GetLastError() == ERROR_INVALID_HANDLE),
352 "returned 0x%04x with 0x%08x (expected LMEM_INVALID_HANDLE with "
353 "ERROR_INVALID_HANDLE)\n", flags, GetLastError());
354 SetLastError(MAGIC_DEAD);
355 size = LocalSize(gbl);
356 ok( (size == 0) && (GetLastError() == ERROR_INVALID_HANDLE),
357 "returned %ld with 0x%08x (expected '0' with ERROR_INVALID_HANDLE)\n",
358 size, GetLastError());
360 SetLastError(MAGIC_DEAD);
361 mem = LocalLock(gbl);
362 ok( (mem == NULL) && (GetLastError() == ERROR_INVALID_HANDLE),
363 "returned %p with 0x%08x (expected NULL with ERROR_INVALID_HANDLE)\n",
364 mem, GetLastError());
366 /* This Test works the same on all Systems (GlobalUnlock() is different) */
367 SetLastError(MAGIC_DEAD);
368 res = LocalUnlock(gbl);
369 ok(!res && (GetLastError() == ERROR_INVALID_HANDLE),
370 "returned %d with %d (expected '0' with ERROR_INVALID_HANDLE)\n",
371 res, GetLastError());
373 /* trying to lock empty memory should give an error */
374 gbl = GlobalAlloc(GMEM_MOVEABLE|GMEM_ZEROINIT,0);
375 ok(gbl != NULL, "returned NULL\n");
376 SetLastError(MAGIC_DEAD);
377 mem = GlobalLock(gbl);
378 /* NT: ERROR_DISCARDED, 9x: untouched */
380 ((GetLastError() == ERROR_DISCARDED) || (GetLastError() == MAGIC_DEAD)),
381 "returned %p with 0x%x/%d (expected 'NULL' with: ERROR_DISCARDED or "
382 "MAGIC_DEAD)\n", mem, GetLastError(), GetLastError());
387 static void test_obsolete_flags(void)
392 } test_global_flags[] = {
393 {GMEM_FIXED | GMEM_NOTIFY, 0},
394 {GMEM_FIXED | GMEM_DISCARDABLE, 0},
395 {GMEM_MOVEABLE | GMEM_NOTIFY, 0},
396 {GMEM_MOVEABLE | GMEM_DDESHARE, GMEM_DDESHARE},
397 {GMEM_MOVEABLE | GMEM_NOT_BANKED, 0},
398 {GMEM_MOVEABLE | GMEM_NODISCARD, 0},
399 {GMEM_MOVEABLE | GMEM_DISCARDABLE, GMEM_DISCARDABLE},
400 {GMEM_MOVEABLE | GMEM_DDESHARE | GMEM_DISCARDABLE | GMEM_LOWER | GMEM_NOCOMPACT | GMEM_NODISCARD |
401 GMEM_NOT_BANKED | GMEM_NOTIFY, GMEM_DDESHARE | GMEM_DISCARDABLE},
408 UINT (WINAPI *pGlobalFlags)(HGLOBAL);
410 pGlobalFlags = (void *) GetProcAddress(GetModuleHandleA("kernel32"), "GlobalFlags");
414 win_skip("GlobalFlags is not available\n");
418 for (i = 0; i < sizeof(test_global_flags)/sizeof(test_global_flags[0]); i++)
420 gbl = GlobalAlloc(test_global_flags[i].flags, 4);
421 ok(gbl != NULL, "GlobalAlloc failed\n");
423 SetLastError(MAGIC_DEAD);
424 resultflags = pGlobalFlags(gbl);
426 ok( resultflags == test_global_flags[i].globalflags ||
427 broken(resultflags == (test_global_flags[i].globalflags & ~GMEM_DDESHARE)), /* win9x */
428 "%u: expected 0x%08x, but returned 0x%08x with %d\n",
429 i, test_global_flags[i].globalflags, resultflags, GetLastError() );
435 static void test_HeapQueryInformation(void)
441 pHeapQueryInformation = (void *)GetProcAddress(GetModuleHandle("kernel32.dll"), "HeapQueryInformation");
442 if (!pHeapQueryInformation)
444 win_skip("HeapQueryInformation is not available\n");
448 if (0) /* crashes under XP */
451 ret = pHeapQueryInformation(0,
452 HeapCompatibilityInformation,
453 &info, sizeof(info), &size);
455 ret = pHeapQueryInformation(GetProcessHeap(),
456 HeapCompatibilityInformation,
457 NULL, sizeof(info), &size);
461 SetLastError(0xdeadbeef);
462 ret = pHeapQueryInformation(GetProcessHeap(),
463 HeapCompatibilityInformation,
465 ok(!ret, "HeapQueryInformation should fail\n");
466 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
467 "expected ERROR_INSUFFICIENT_BUFFER got %u\n", GetLastError());
468 ok(size == sizeof(ULONG), "expected 4, got %lu\n", size);
470 SetLastError(0xdeadbeef);
471 ret = pHeapQueryInformation(GetProcessHeap(),
472 HeapCompatibilityInformation,
474 ok(!ret, "HeapQueryInformation should fail\n");
475 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
476 "expected ERROR_INSUFFICIENT_BUFFER got %u\n", GetLastError());
479 SetLastError(0xdeadbeef);
480 ret = pHeapQueryInformation(GetProcessHeap(),
481 HeapCompatibilityInformation,
482 &info, sizeof(info) + 1, NULL);
483 ok(ret, "HeapQueryInformation error %u\n", GetLastError());
484 ok(info == 0 || info == 1 || info == 2, "expected 0, 1 or 2, got %u\n", info);
487 static void test_heap_checks( DWORD flags )
491 SIZE_T i, size, large_size = 800 * 1024 + 37;
493 if (flags & HEAP_PAGE_ALLOCS) return; /* no tests for that case yet */
494 trace( "testing heap flags %08x\n", flags );
496 p = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, 17 );
497 ok( p != NULL, "HeapAlloc failed\n" );
499 ret = HeapValidate( GetProcessHeap(), 0, p );
500 ok( ret, "HeapValidate failed\n" );
502 size = HeapSize( GetProcessHeap(), 0, p );
503 ok( size == 17, "Wrong size %lu\n", size );
505 ok( p[14] == 0, "wrong data %x\n", p[14] );
506 ok( p[15] == 0, "wrong data %x\n", p[15] );
507 ok( p[16] == 0, "wrong data %x\n", p[16] );
509 if (flags & HEAP_TAIL_CHECKING_ENABLED)
511 ok( p[17] == 0xab, "wrong padding %x\n", p[17] );
512 ok( p[18] == 0xab, "wrong padding %x\n", p[18] );
513 ok( p[19] == 0xab, "wrong padding %x\n", p[19] );
516 p2 = HeapReAlloc( GetProcessHeap(), HEAP_REALLOC_IN_PLACE_ONLY, p, 14 );
519 if (flags & HEAP_TAIL_CHECKING_ENABLED)
521 ok( p[14] == 0xab, "wrong padding %x\n", p[14] );
522 ok( p[15] == 0xab, "wrong padding %x\n", p[15] );
523 ok( p[16] == 0xab, "wrong padding %x\n", p[16] );
527 ok( p[14] == 0, "wrong padding %x\n", p[14] );
528 ok( p[15] == 0, "wrong padding %x\n", p[15] );
529 ok( p[16] == 0, "wrong padding %x\n", p[16] );
532 else skip( "realloc in place failed\n ");
534 ret = HeapFree( GetProcessHeap(), 0, p );
535 ok( ret, "HeapFree failed\n" );
537 p = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, 17 );
538 ok( p != NULL, "HeapAlloc failed\n" );
542 if (flags & HEAP_TAIL_CHECKING_ENABLED)
544 ret = HeapValidate( GetProcessHeap(), 0, p );
545 ok( !ret, "HeapValidate succeeded\n" );
547 /* other calls only check when HEAP_VALIDATE is set */
548 if (flags & HEAP_VALIDATE)
550 size = HeapSize( GetProcessHeap(), 0, p );
551 ok( size == ~(SIZE_T)0, "Wrong size %lu\n", size );
553 p2 = HeapReAlloc( GetProcessHeap(), 0, p, 14 );
554 ok( p2 == NULL, "HeapReAlloc succeeded\n" );
556 ret = HeapFree( GetProcessHeap(), 0, p );
557 ok( !ret, "HeapFree succeeded\n" );
561 size = HeapSize( GetProcessHeap(), 0, p );
562 ok( size == 17, "Wrong size %lu\n", size );
564 p2 = HeapReAlloc( GetProcessHeap(), 0, p, 14 );
565 ok( p2 != NULL, "HeapReAlloc failed\n" );
569 ret = HeapFree( GetProcessHeap(), 0, p );
570 ok( ret, "HeapFree failed\n" );
572 p = HeapAlloc( GetProcessHeap(), 0, 37 );
573 ok( p != NULL, "HeapAlloc failed\n" );
574 memset( p, 0xcc, 37 );
576 ret = HeapFree( GetProcessHeap(), 0, p );
577 ok( ret, "HeapFree failed\n" );
579 if (flags & HEAP_FREE_CHECKING_ENABLED)
581 ok( p[16] == 0xee, "wrong data %x\n", p[16] );
582 ok( p[17] == 0xfe, "wrong data %x\n", p[17] );
583 ok( p[18] == 0xee, "wrong data %x\n", p[18] );
584 ok( p[19] == 0xfe, "wrong data %x\n", p[19] );
586 ret = HeapValidate( GetProcessHeap(), 0, NULL );
587 ok( ret, "HeapValidate failed\n" );
591 ret = HeapValidate( GetProcessHeap(), 0, NULL );
592 ok( !ret, "HeapValidate succeeded\n" );
595 ret = HeapValidate( GetProcessHeap(), 0, NULL );
596 ok( ret, "HeapValidate failed\n" );
599 /* now test large blocks */
601 p = HeapAlloc( GetProcessHeap(), 0, large_size );
602 ok( p != NULL, "HeapAlloc failed\n" );
604 ret = HeapValidate( GetProcessHeap(), 0, p );
605 ok( ret, "HeapValidate failed\n" );
607 size = HeapSize( GetProcessHeap(), 0, p );
608 ok( size == large_size, "Wrong size %lu\n", size );
610 ok( p[large_size - 2] == 0, "wrong data %x\n", p[large_size - 2] );
611 ok( p[large_size - 1] == 0, "wrong data %x\n", p[large_size - 1] );
613 if (flags & HEAP_TAIL_CHECKING_ENABLED)
615 /* Windows doesn't do tail checking on large blocks */
616 ok( p[large_size] == 0xab || broken(p[large_size] == 0), "wrong data %x\n", p[large_size] );
617 ok( p[large_size+1] == 0xab || broken(p[large_size+1] == 0), "wrong data %x\n", p[large_size+1] );
618 ok( p[large_size+2] == 0xab || broken(p[large_size+2] == 0), "wrong data %x\n", p[large_size+2] );
619 if (p[large_size] == 0xab)
621 p[large_size] = 0xcc;
622 ret = HeapValidate( GetProcessHeap(), 0, p );
623 ok( !ret, "HeapValidate succeeded\n" );
625 /* other calls only check when HEAP_VALIDATE is set */
626 if (flags & HEAP_VALIDATE)
628 size = HeapSize( GetProcessHeap(), 0, p );
629 ok( size == ~(SIZE_T)0, "Wrong size %lu\n", size );
631 p2 = HeapReAlloc( GetProcessHeap(), 0, p, large_size - 3 );
632 ok( p2 == NULL, "HeapReAlloc succeeded\n" );
634 ret = HeapFree( GetProcessHeap(), 0, p );
635 ok( !ret, "HeapFree succeeded\n" );
637 p[large_size] = 0xab;
641 ret = HeapFree( GetProcessHeap(), 0, p );
642 ok( ret, "HeapFree failed\n" );
644 /* test block sizes when tail checking */
645 if (flags & HEAP_TAIL_CHECKING_ENABLED)
647 for (size = 0; size < 64; size++)
649 p = HeapAlloc( GetProcessHeap(), 0, size );
650 for (i = 0; i < 32; i++) if (p[size + i] != 0xab) break;
651 ok( i >= 8, "only %lu tail bytes for size %lu\n", i, size );
652 HeapFree( GetProcessHeap(), 0, p );
657 static void test_debug_heap( const char *argv0, DWORD flags )
659 char keyname[MAX_PATH];
660 char buffer[MAX_PATH];
661 PROCESS_INFORMATION info;
662 STARTUPINFOA startup;
666 const char *basename;
668 if ((basename = strrchr( argv0, '\\' ))) basename++;
669 else basename = argv0;
671 sprintf( keyname, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Image File Execution Options\\%s",
673 if (!strcmp( keyname + strlen(keyname) - 3, ".so" )) keyname[strlen(keyname) - 3] = 0;
675 err = RegCreateKeyA( HKEY_LOCAL_MACHINE, keyname, &hkey );
676 ok( !err, "failed to create '%s' error %u\n", keyname, err );
679 if (flags == 0xdeadbeef) /* magic value for unsetting it */
680 RegDeleteValueA( hkey, "GlobalFlag" );
682 RegSetValueExA( hkey, "GlobalFlag", 0, REG_DWORD, (BYTE *)&flags, sizeof(flags) );
684 memset( &startup, 0, sizeof(startup) );
685 startup.cb = sizeof(startup);
687 sprintf( buffer, "%s heap.c 0x%x", argv0, flags );
688 ret = CreateProcessA( NULL, buffer, NULL, NULL, FALSE, 0, NULL, NULL, &startup, &info );
689 ok( ret, "failed to create child process error %u\n", GetLastError() );
692 winetest_wait_child_process( info.hProcess );
693 CloseHandle( info.hThread );
694 CloseHandle( info.hProcess );
696 RegDeleteValueA( hkey, "GlobalFlag" );
698 RegDeleteKeyA( HKEY_LOCAL_MACHINE, keyname );
701 static DWORD heap_flags_from_global_flag( DWORD flag )
705 if (flag & FLG_HEAP_ENABLE_TAIL_CHECK)
706 ret |= HEAP_TAIL_CHECKING_ENABLED;
707 if (flag & FLG_HEAP_ENABLE_FREE_CHECK)
708 ret |= HEAP_FREE_CHECKING_ENABLED;
709 if (flag & FLG_HEAP_VALIDATE_PARAMETERS)
710 ret |= HEAP_VALIDATE_PARAMS | HEAP_VALIDATE | HEAP_TAIL_CHECKING_ENABLED | HEAP_FREE_CHECKING_ENABLED;
711 if (flag & FLG_HEAP_VALIDATE_ALL)
712 ret |= HEAP_VALIDATE_ALL | HEAP_VALIDATE | HEAP_TAIL_CHECKING_ENABLED | HEAP_FREE_CHECKING_ENABLED;
713 if (flag & FLG_HEAP_DISABLE_COALESCING)
714 ret |= HEAP_DISABLE_COALESCE_ON_FREE;
715 if (flag & FLG_HEAP_PAGE_ALLOCS)
716 ret |= HEAP_PAGE_ALLOCS | HEAP_GROWABLE;
720 static void test_child_heap( const char *arg )
722 struct heap_layout *heap = GetProcessHeap();
723 DWORD expected = strtoul( arg, 0, 16 );
726 if (expected == 0xdeadbeef) /* expected value comes from Session Manager global flags */
730 if (!RegOpenKeyA( HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Control\\Session Manager", &hkey ))
733 DWORD type, size = sizeof(buffer);
735 if (!RegQueryValueExA( hkey, "GlobalFlag", 0, &type, (BYTE *)buffer, &size ))
737 if (type == REG_DWORD) expected = *(DWORD *)buffer;
738 else if (type == REG_SZ) expected = strtoul( buffer, 0, 16 );
743 if (expected && !pRtlGetNtGlobalFlags()) /* not working on NT4 */
745 win_skip( "global flags not set\n" );
749 ok( pRtlGetNtGlobalFlags() == expected,
750 "%s: got global flags %08x expected %08x\n", arg, pRtlGetNtGlobalFlags(), expected );
752 expect_heap = heap_flags_from_global_flag( expected );
754 if (!(heap->flags & HEAP_GROWABLE) || heap->flags == 0xeeeeeeee) /* vista layout */
756 if (expected & FLG_HEAP_PAGE_ALLOCS)
757 ok( heap->flags == 0xeeeeeeee, "%s: got heap flags %08x expected 0xeeeeeeee\n",
760 ok( heap->flags == 0, "%s: got heap flags %08x expected 0\n", arg, heap->flags );
764 ok( heap->flags == (expect_heap | HEAP_GROWABLE),
765 "%s: got heap flags %08x expected %08x\n", arg, heap->flags, expect_heap );
766 ok( heap->force_flags == (expect_heap & ~0x18000080),
767 "%s: got heap force flags %08x expected %08x\n", arg, heap->force_flags, expect_heap );
770 test_heap_checks( expect_heap );
778 pRtlGetNtGlobalFlags = (void *)GetProcAddress( GetModuleHandleA("ntdll.dll"), "RtlGetNtGlobalFlags" );
780 argc = winetest_get_mainargs( &argv );
783 test_child_heap( argv[2] );
788 test_obsolete_flags();
790 /* Test both short and very long blocks */
791 test_sized_HeapAlloc(1);
792 test_sized_HeapAlloc(1 << 20);
793 test_sized_HeapReAlloc(1, 100);
794 test_sized_HeapReAlloc(1, (1 << 20));
795 test_sized_HeapReAlloc((1 << 20), (2 << 20));
796 test_sized_HeapReAlloc((1 << 20), 1);
797 test_HeapQueryInformation();
799 if (pRtlGetNtGlobalFlags)
801 test_debug_heap( argv[0], 0 );
802 test_debug_heap( argv[0], FLG_HEAP_ENABLE_TAIL_CHECK );
803 test_debug_heap( argv[0], FLG_HEAP_ENABLE_FREE_CHECK );
804 test_debug_heap( argv[0], FLG_HEAP_VALIDATE_PARAMETERS );
805 test_debug_heap( argv[0], FLG_HEAP_VALIDATE_ALL );
806 test_debug_heap( argv[0], FLG_POOL_ENABLE_TAGGING );
807 test_debug_heap( argv[0], FLG_HEAP_ENABLE_TAGGING );
808 test_debug_heap( argv[0], FLG_HEAP_ENABLE_TAG_BY_DLL );
809 test_debug_heap( argv[0], FLG_HEAP_DISABLE_COALESCING );
810 test_debug_heap( argv[0], FLG_HEAP_PAGE_ALLOCS );
811 test_debug_heap( argv[0], 0xdeadbeef );
813 else win_skip( "RtlGetNtGlobalFlags not found, skipping heap debug tests\n" );