configure: Changes from running autconf after previous patch.
[wine/hacks.git] / dlls / kernel32 / tests / heap.c
blob98c99d75aedb7179173329d1bb6f3d2b2432e82e
1 /*
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
22 #include <stdarg.h>
23 #include <stdlib.h>
24 #include <stdio.h>
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winreg.h"
29 #include "winternl.h"
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);
43 struct heap_layout
45 DWORD_PTR unknown[2];
46 DWORD pattern;
47 DWORD flags;
48 DWORD force_flags;
51 static SIZE_T resize_9x(SIZE_T size)
53 DWORD dwSizeAligned = (size + 3) & ~3;
54 return max(dwSizeAligned, 12); /* at least 12 bytes */
57 static void test_sized_HeapAlloc(int nbytes)
59 int success;
60 char *buf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, nbytes);
61 ok(buf != NULL, "allocate failed\n");
62 ok(buf[0] == 0, "buffer not zeroed\n");
63 success = HeapFree(GetProcessHeap(), 0, buf);
64 ok(success, "free failed\n");
67 static void test_sized_HeapReAlloc(int nbytes1, int nbytes2)
69 int success;
70 char *buf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, nbytes1);
71 ok(buf != NULL, "allocate failed\n");
72 ok(buf[0] == 0, "buffer not zeroed\n");
73 buf = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, buf, nbytes2);
74 ok(buf != NULL, "reallocate failed\n");
75 ok(buf[nbytes2-1] == 0, "buffer not zeroed\n");
76 success = HeapFree(GetProcessHeap(), 0, buf);
77 ok(success, "free failed\n");
80 static void test_heap(void)
82 LPVOID mem;
83 LPVOID msecond;
84 DWORD res;
85 UINT flags;
86 HGLOBAL gbl;
87 HGLOBAL hsecond;
88 SIZE_T size;
90 /* Heap*() functions */
91 mem = HeapAlloc(GetProcessHeap(), 0, 0);
92 ok(mem != NULL, "memory not allocated for size 0\n");
93 HeapFree(GetProcessHeap(), 0, mem);
95 mem = HeapReAlloc(GetProcessHeap(), 0, NULL, 10);
96 ok(mem == NULL, "memory allocated by HeapReAlloc\n");
98 for (size = 0; size <= 256; size++)
100 SIZE_T heap_size;
101 mem = HeapAlloc(GetProcessHeap(), 0, size);
102 heap_size = HeapSize(GetProcessHeap(), 0, mem);
103 ok(heap_size == size || heap_size == resize_9x(size),
104 "HeapSize returned %lu instead of %lu or %lu\n", heap_size, size, resize_9x(size));
105 HeapFree(GetProcessHeap(), 0, mem);
108 /* test some border cases of HeapAlloc and HeapReAlloc */
109 mem = HeapAlloc(GetProcessHeap(), 0, 0);
110 ok(mem != NULL, "memory not allocated for size 0\n");
111 msecond = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, mem, ~(SIZE_T)0 - 7);
112 ok(msecond == NULL, "HeapReAlloc(~0 - 7) should have failed\n");
113 msecond = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, mem, ~(SIZE_T)0);
114 ok(msecond == NULL, "HeapReAlloc(~0) should have failed\n");
115 HeapFree(GetProcessHeap(), 0, mem);
116 mem = HeapAlloc(GetProcessHeap(), 0, ~(SIZE_T)0);
117 ok(mem == NULL, "memory allocated for size ~0\n");
119 /* large blocks must be 16-byte aligned */
120 mem = HeapAlloc(GetProcessHeap(), 0, 512 * 1024);
121 ok( mem != NULL, "failed for size 512K\n" );
122 ok( (ULONG_PTR)mem % 16 == 0 || broken((ULONG_PTR)mem % 16) /* win9x */,
123 "512K block not 16-byte aligned\n" );
124 HeapFree(GetProcessHeap(), 0, mem);
126 /* Global*() functions */
127 gbl = GlobalAlloc(GMEM_MOVEABLE, 0);
128 ok(gbl != NULL, "global memory not allocated for size 0\n");
130 gbl = GlobalReAlloc(gbl, 10, GMEM_MOVEABLE);
131 ok(gbl != NULL, "Can't realloc global memory\n");
132 size = GlobalSize(gbl);
133 ok(size >= 10 && size <= 16, "Memory not resized to size 10, instead size=%ld\n", size);
135 gbl = GlobalReAlloc(gbl, 0, GMEM_MOVEABLE);
136 ok(gbl != NULL, "GlobalReAlloc should not fail on size 0\n");
138 size = GlobalSize(gbl);
139 ok(size == 0, "Memory not resized to size 0, instead size=%ld\n", size);
140 ok(GlobalFree(gbl) == NULL, "Memory not freed\n");
141 size = GlobalSize(gbl);
142 ok(size == 0, "Memory should have been freed, size=%ld\n", size);
144 gbl = GlobalReAlloc(0, 10, GMEM_MOVEABLE);
145 ok(gbl == NULL, "global realloc allocated memory\n");
147 /* GlobalLock / GlobalUnlock with a valid handle */
148 gbl = GlobalAlloc(GMEM_MOVEABLE, 256);
150 SetLastError(MAGIC_DEAD);
151 mem = GlobalLock(gbl); /* #1 */
152 ok(mem != NULL, "returned %p with %d (expected '!= NULL')\n", mem, GetLastError());
153 SetLastError(MAGIC_DEAD);
154 flags = GlobalFlags(gbl);
155 ok( flags == 1, "returned 0x%04x with %d (expected '0x0001')\n",
156 flags, GetLastError());
158 SetLastError(MAGIC_DEAD);
159 msecond = GlobalLock(gbl); /* #2 */
160 ok( msecond == mem, "returned %p with %d (expected '%p')\n",
161 msecond, GetLastError(), mem);
162 SetLastError(MAGIC_DEAD);
163 flags = GlobalFlags(gbl);
164 ok( flags == 2, "returned 0x%04x with %d (expected '0x0002')\n",
165 flags, GetLastError());
166 SetLastError(MAGIC_DEAD);
168 SetLastError(MAGIC_DEAD);
169 res = GlobalUnlock(gbl); /* #1 */
170 ok(res, "returned %d with %d (expected '!= 0')\n", res, GetLastError());
171 SetLastError(MAGIC_DEAD);
172 flags = GlobalFlags(gbl);
173 ok( flags , "returned 0x%04x with %d (expected '!= 0')\n",
174 flags, GetLastError());
176 SetLastError(MAGIC_DEAD);
177 res = GlobalUnlock(gbl); /* #0 */
178 /* NT: ERROR_SUCCESS (documented on MSDN), 9x: untouched */
179 ok(!res && ((GetLastError() == ERROR_SUCCESS) || (GetLastError() == MAGIC_DEAD)),
180 "returned %d with %d (expected '0' with: ERROR_SUCCESS or "
181 "MAGIC_DEAD)\n", res, GetLastError());
182 SetLastError(MAGIC_DEAD);
183 flags = GlobalFlags(gbl);
184 ok( !flags , "returned 0x%04x with %d (expected '0')\n",
185 flags, GetLastError());
187 /* Unlock an already unlocked Handle */
188 SetLastError(MAGIC_DEAD);
189 res = GlobalUnlock(gbl);
190 /* NT: ERROR_NOT_LOCKED, 9x: untouched */
191 ok( !res &&
192 ((GetLastError() == ERROR_NOT_LOCKED) || (GetLastError() == MAGIC_DEAD)),
193 "returned %d with %d (expected '0' with: ERROR_NOT_LOCKED or "
194 "MAGIC_DEAD)\n", res, GetLastError());
196 GlobalFree(gbl);
197 /* invalid handles are caught in windows: */
198 SetLastError(MAGIC_DEAD);
199 hsecond = GlobalFree(gbl); /* invalid handle: free memory twice */
200 ok( (hsecond == gbl) && (GetLastError() == ERROR_INVALID_HANDLE),
201 "returned %p with 0x%08x (expected %p with ERROR_INVALID_HANDLE)\n",
202 hsecond, GetLastError(), gbl);
203 SetLastError(MAGIC_DEAD);
204 flags = GlobalFlags(gbl);
205 ok( (flags == GMEM_INVALID_HANDLE) && (GetLastError() == ERROR_INVALID_HANDLE),
206 "returned 0x%04x with 0x%08x (expected GMEM_INVALID_HANDLE with "
207 "ERROR_INVALID_HANDLE)\n", flags, GetLastError());
208 SetLastError(MAGIC_DEAD);
209 size = GlobalSize(gbl);
210 ok( (size == 0) && (GetLastError() == ERROR_INVALID_HANDLE),
211 "returned %ld with 0x%08x (expected '0' with ERROR_INVALID_HANDLE)\n",
212 size, GetLastError());
214 SetLastError(MAGIC_DEAD);
215 mem = GlobalLock(gbl);
216 ok( (mem == NULL) && (GetLastError() == ERROR_INVALID_HANDLE),
217 "returned %p with 0x%08x (expected NULL with ERROR_INVALID_HANDLE)\n",
218 mem, GetLastError());
220 /* documented on MSDN: GlobalUnlock() return FALSE on failure.
221 Win9x and wine return FALSE with ERROR_INVALID_HANDLE, but on
222 NT 3.51 and XPsp2, TRUE with ERROR_INVALID_HANDLE is returned.
223 The similar Test for LocalUnlock() works on all Systems */
224 SetLastError(MAGIC_DEAD);
225 res = GlobalUnlock(gbl);
226 ok(GetLastError() == ERROR_INVALID_HANDLE,
227 "returned %d with %d (expected ERROR_INVALID_HANDLE)\n",
228 res, GetLastError());
230 gbl = GlobalAlloc(GMEM_DDESHARE, 100);
232 /* first free */
233 mem = GlobalFree(gbl);
234 ok(mem == NULL, "Expected NULL, got %p\n", mem);
236 /* invalid free */
237 if (sizeof(void *) != 8) /* crashes on 64-bit Vista */
239 SetLastError(MAGIC_DEAD);
240 mem = GlobalFree(gbl);
241 ok(mem == gbl || broken(mem == NULL) /* nt4 */, "Expected gbl, got %p\n", mem);
242 if (mem == gbl)
243 ok(GetLastError() == ERROR_INVALID_HANDLE ||
244 GetLastError() == ERROR_INVALID_PARAMETER, /* win9x */
245 "Expected ERROR_INVALID_HANDLE or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
248 gbl = GlobalAlloc(GMEM_DDESHARE, 100);
250 res = GlobalUnlock(gbl);
251 ok(res == 1 ||
252 broken(res == 0), /* win9x */
253 "Expected 1 or 0, got %d\n", res);
255 res = GlobalUnlock(gbl);
256 ok(res == 1 ||
257 broken(res == 0), /* win9x */
258 "Expected 1 or 0, got %d\n", res);
260 GlobalFree(gbl);
262 gbl = GlobalAlloc(GMEM_FIXED, 100);
264 SetLastError(0xdeadbeef);
265 res = GlobalUnlock(gbl);
266 ok(res == 1 ||
267 broken(res == 0), /* win9x */
268 "Expected 1 or 0, got %d\n", res);
269 ok(GetLastError() == 0xdeadbeef, "got %d\n", GetLastError());
271 GlobalFree(gbl);
273 /* GlobalSize on an invalid handle */
274 if (sizeof(void *) != 8) /* crashes on 64-bit Vista */
276 SetLastError(MAGIC_DEAD);
277 size = GlobalSize((HGLOBAL)0xc042);
278 ok(size == 0, "Expected 0, got %ld\n", size);
279 ok(GetLastError() == ERROR_INVALID_HANDLE ||
280 GetLastError() == ERROR_INVALID_PARAMETER, /* win9x */
281 "Expected ERROR_INVALID_HANDLE or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
284 /* ####################################### */
285 /* Local*() functions */
286 gbl = LocalAlloc(LMEM_MOVEABLE, 0);
287 ok(gbl != NULL, "local memory not allocated for size 0\n");
289 gbl = LocalReAlloc(gbl, 10, LMEM_MOVEABLE);
290 ok(gbl != NULL, "Can't realloc local memory\n");
291 size = LocalSize(gbl);
292 ok(size >= 10 && size <= 16, "Memory not resized to size 10, instead size=%ld\n", size);
294 gbl = LocalReAlloc(gbl, 0, LMEM_MOVEABLE);
295 ok(gbl != NULL, "LocalReAlloc should not fail on size 0\n");
297 size = LocalSize(gbl);
298 ok(size == 0, "Memory not resized to size 0, instead size=%ld\n", size);
299 ok(LocalFree(gbl) == NULL, "Memory not freed\n");
300 size = LocalSize(gbl);
301 ok(size == 0, "Memory should have been freed, size=%ld\n", size);
303 gbl = LocalReAlloc(0, 10, LMEM_MOVEABLE);
304 ok(gbl == NULL, "local realloc allocated memory\n");
306 /* LocalLock / LocalUnlock with a valid handle */
307 gbl = LocalAlloc(LMEM_MOVEABLE, 256);
308 SetLastError(MAGIC_DEAD);
309 mem = LocalLock(gbl); /* #1 */
310 ok(mem != NULL, "returned %p with %d (expected '!= NULL')\n", mem, GetLastError());
311 SetLastError(MAGIC_DEAD);
312 flags = LocalFlags(gbl);
313 ok( flags == 1, "returned 0x%04x with %d (expected '0x0001')\n",
314 flags, GetLastError());
316 SetLastError(MAGIC_DEAD);
317 msecond = LocalLock(gbl); /* #2 */
318 ok( msecond == mem, "returned %p with %d (expected '%p')\n",
319 msecond, GetLastError(), mem);
320 SetLastError(MAGIC_DEAD);
321 flags = LocalFlags(gbl);
322 ok( flags == 2, "returned 0x%04x with %d (expected '0x0002')\n",
323 flags, GetLastError());
324 SetLastError(MAGIC_DEAD);
326 SetLastError(MAGIC_DEAD);
327 res = LocalUnlock(gbl); /* #1 */
328 ok(res, "returned %d with %d (expected '!= 0')\n", res, GetLastError());
329 SetLastError(MAGIC_DEAD);
330 flags = LocalFlags(gbl);
331 ok( flags , "returned 0x%04x with %d (expected '!= 0')\n",
332 flags, GetLastError());
334 SetLastError(MAGIC_DEAD);
335 res = LocalUnlock(gbl); /* #0 */
336 /* NT: ERROR_SUCCESS (documented on MSDN), 9x: untouched */
337 ok(!res && ((GetLastError() == ERROR_SUCCESS) || (GetLastError() == MAGIC_DEAD)),
338 "returned %d with %d (expected '0' with: ERROR_SUCCESS or "
339 "MAGIC_DEAD)\n", res, GetLastError());
340 SetLastError(MAGIC_DEAD);
341 flags = LocalFlags(gbl);
342 ok( !flags , "returned 0x%04x with %d (expected '0')\n",
343 flags, GetLastError());
345 /* Unlock an already unlocked Handle */
346 SetLastError(MAGIC_DEAD);
347 res = LocalUnlock(gbl);
348 /* NT: ERROR_NOT_LOCKED, 9x: untouched */
349 ok( !res &&
350 ((GetLastError() == ERROR_NOT_LOCKED) || (GetLastError() == MAGIC_DEAD)),
351 "returned %d with %d (expected '0' with: ERROR_NOT_LOCKED or "
352 "MAGIC_DEAD)\n", res, GetLastError());
354 LocalFree(gbl);
355 /* invalid handles are caught in windows: */
356 SetLastError(MAGIC_DEAD);
357 hsecond = LocalFree(gbl); /* invalid handle: free memory twice */
358 ok( (hsecond == gbl) && (GetLastError() == ERROR_INVALID_HANDLE),
359 "returned %p with 0x%08x (expected %p with ERROR_INVALID_HANDLE)\n",
360 hsecond, GetLastError(), gbl);
361 SetLastError(MAGIC_DEAD);
362 flags = LocalFlags(gbl);
363 ok( (flags == LMEM_INVALID_HANDLE) && (GetLastError() == ERROR_INVALID_HANDLE),
364 "returned 0x%04x with 0x%08x (expected LMEM_INVALID_HANDLE with "
365 "ERROR_INVALID_HANDLE)\n", flags, GetLastError());
366 SetLastError(MAGIC_DEAD);
367 size = LocalSize(gbl);
368 ok( (size == 0) && (GetLastError() == ERROR_INVALID_HANDLE),
369 "returned %ld with 0x%08x (expected '0' with ERROR_INVALID_HANDLE)\n",
370 size, GetLastError());
372 SetLastError(MAGIC_DEAD);
373 mem = LocalLock(gbl);
374 ok( (mem == NULL) && (GetLastError() == ERROR_INVALID_HANDLE),
375 "returned %p with 0x%08x (expected NULL with ERROR_INVALID_HANDLE)\n",
376 mem, GetLastError());
378 /* This Test works the same on all Systems (GlobalUnlock() is different) */
379 SetLastError(MAGIC_DEAD);
380 res = LocalUnlock(gbl);
381 ok(!res && (GetLastError() == ERROR_INVALID_HANDLE),
382 "returned %d with %d (expected '0' with ERROR_INVALID_HANDLE)\n",
383 res, GetLastError());
385 /* trying to unlock pointer from LocalAlloc */
386 gbl = LocalAlloc(LMEM_FIXED, 100);
387 SetLastError(0xdeadbeef);
388 res = LocalUnlock(gbl);
389 ok(res == 0, "Expected 0, got %d\n", res);
390 ok(GetLastError() == ERROR_NOT_LOCKED ||
391 broken(GetLastError() == 0xdeadbeef) /* win9x */, "got %d\n", GetLastError());
392 LocalFree(gbl);
394 /* trying to lock empty memory should give an error */
395 gbl = GlobalAlloc(GMEM_MOVEABLE|GMEM_ZEROINIT,0);
396 ok(gbl != NULL, "returned NULL\n");
397 SetLastError(MAGIC_DEAD);
398 mem = GlobalLock(gbl);
399 /* NT: ERROR_DISCARDED, 9x: untouched */
400 ok( (mem == NULL) &&
401 ((GetLastError() == ERROR_DISCARDED) || (GetLastError() == MAGIC_DEAD)),
402 "returned %p with 0x%x/%d (expected 'NULL' with: ERROR_DISCARDED or "
403 "MAGIC_DEAD)\n", mem, GetLastError(), GetLastError());
405 GlobalFree(gbl);
408 static void test_obsolete_flags(void)
410 static struct {
411 UINT flags;
412 UINT globalflags;
413 } test_global_flags[] = {
414 {GMEM_FIXED | GMEM_NOTIFY, 0},
415 {GMEM_FIXED | GMEM_DISCARDABLE, 0},
416 {GMEM_MOVEABLE | GMEM_NOTIFY, 0},
417 {GMEM_MOVEABLE | GMEM_DDESHARE, GMEM_DDESHARE},
418 {GMEM_MOVEABLE | GMEM_NOT_BANKED, 0},
419 {GMEM_MOVEABLE | GMEM_NODISCARD, 0},
420 {GMEM_MOVEABLE | GMEM_DISCARDABLE, GMEM_DISCARDABLE},
421 {GMEM_MOVEABLE | GMEM_DDESHARE | GMEM_DISCARDABLE | GMEM_LOWER | GMEM_NOCOMPACT | GMEM_NODISCARD |
422 GMEM_NOT_BANKED | GMEM_NOTIFY, GMEM_DDESHARE | GMEM_DISCARDABLE},
425 unsigned int i;
426 HGLOBAL gbl;
427 UINT resultflags;
429 UINT (WINAPI *pGlobalFlags)(HGLOBAL);
431 pGlobalFlags = (void *) GetProcAddress(GetModuleHandleA("kernel32"), "GlobalFlags");
433 if (!pGlobalFlags)
435 win_skip("GlobalFlags is not available\n");
436 return;
439 for (i = 0; i < sizeof(test_global_flags)/sizeof(test_global_flags[0]); i++)
441 gbl = GlobalAlloc(test_global_flags[i].flags, 4);
442 ok(gbl != NULL, "GlobalAlloc failed\n");
444 SetLastError(MAGIC_DEAD);
445 resultflags = pGlobalFlags(gbl);
447 ok( resultflags == test_global_flags[i].globalflags ||
448 broken(resultflags == (test_global_flags[i].globalflags & ~GMEM_DDESHARE)), /* win9x */
449 "%u: expected 0x%08x, but returned 0x%08x with %d\n",
450 i, test_global_flags[i].globalflags, resultflags, GetLastError() );
452 GlobalFree(gbl);
456 static void test_HeapQueryInformation(void)
458 ULONG info;
459 SIZE_T size;
460 BOOL ret;
462 pHeapQueryInformation = (void *)GetProcAddress(GetModuleHandle("kernel32.dll"), "HeapQueryInformation");
463 if (!pHeapQueryInformation)
465 win_skip("HeapQueryInformation is not available\n");
466 return;
469 if (0) /* crashes under XP */
471 size = 0;
472 ret = pHeapQueryInformation(0,
473 HeapCompatibilityInformation,
474 &info, sizeof(info), &size);
475 size = 0;
476 ret = pHeapQueryInformation(GetProcessHeap(),
477 HeapCompatibilityInformation,
478 NULL, sizeof(info), &size);
481 size = 0;
482 SetLastError(0xdeadbeef);
483 ret = pHeapQueryInformation(GetProcessHeap(),
484 HeapCompatibilityInformation,
485 NULL, 0, &size);
486 ok(!ret, "HeapQueryInformation should fail\n");
487 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
488 "expected ERROR_INSUFFICIENT_BUFFER got %u\n", GetLastError());
489 ok(size == sizeof(ULONG), "expected 4, got %lu\n", size);
491 SetLastError(0xdeadbeef);
492 ret = pHeapQueryInformation(GetProcessHeap(),
493 HeapCompatibilityInformation,
494 NULL, 0, NULL);
495 ok(!ret, "HeapQueryInformation should fail\n");
496 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
497 "expected ERROR_INSUFFICIENT_BUFFER got %u\n", GetLastError());
499 info = 0xdeadbeaf;
500 SetLastError(0xdeadbeef);
501 ret = pHeapQueryInformation(GetProcessHeap(),
502 HeapCompatibilityInformation,
503 &info, sizeof(info) + 1, NULL);
504 ok(ret, "HeapQueryInformation error %u\n", GetLastError());
505 ok(info == 0 || info == 1 || info == 2, "expected 0, 1 or 2, got %u\n", info);
508 static void test_heap_checks( DWORD flags )
510 BYTE old, *p, *p2;
511 BOOL ret;
512 SIZE_T i, size, large_size = 3000 * 1024 + 37;
514 if (flags & HEAP_PAGE_ALLOCS) return; /* no tests for that case yet */
515 trace( "testing heap flags %08x\n", flags );
517 p = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, 17 );
518 ok( p != NULL, "HeapAlloc failed\n" );
520 ret = HeapValidate( GetProcessHeap(), 0, p );
521 ok( ret, "HeapValidate failed\n" );
523 size = HeapSize( GetProcessHeap(), 0, p );
524 ok( size == 17, "Wrong size %lu\n", size );
526 ok( p[14] == 0, "wrong data %x\n", p[14] );
527 ok( p[15] == 0, "wrong data %x\n", p[15] );
528 ok( p[16] == 0, "wrong data %x\n", p[16] );
530 if (flags & HEAP_TAIL_CHECKING_ENABLED)
532 ok( p[17] == 0xab, "wrong padding %x\n", p[17] );
533 ok( p[18] == 0xab, "wrong padding %x\n", p[18] );
534 ok( p[19] == 0xab, "wrong padding %x\n", p[19] );
537 p2 = HeapReAlloc( GetProcessHeap(), HEAP_REALLOC_IN_PLACE_ONLY, p, 14 );
538 if (p2 == p)
540 if (flags & HEAP_TAIL_CHECKING_ENABLED)
542 ok( p[14] == 0xab, "wrong padding %x\n", p[14] );
543 ok( p[15] == 0xab, "wrong padding %x\n", p[15] );
544 ok( p[16] == 0xab, "wrong padding %x\n", p[16] );
546 else
548 ok( p[14] == 0, "wrong padding %x\n", p[14] );
549 ok( p[15] == 0, "wrong padding %x\n", p[15] );
552 else skip( "realloc in place failed\n");
554 ret = HeapFree( GetProcessHeap(), 0, p );
555 ok( ret, "HeapFree failed\n" );
557 p = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, 17 );
558 ok( p != NULL, "HeapAlloc failed\n" );
559 old = p[17];
560 p[17] = 0xcc;
562 if (flags & HEAP_TAIL_CHECKING_ENABLED)
564 ret = HeapValidate( GetProcessHeap(), 0, p );
565 ok( !ret, "HeapValidate succeeded\n" );
567 /* other calls only check when HEAP_VALIDATE is set */
568 if (flags & HEAP_VALIDATE)
570 size = HeapSize( GetProcessHeap(), 0, p );
571 ok( size == ~(SIZE_T)0 || broken(size == ~0u), "Wrong size %lu\n", size );
573 p2 = HeapReAlloc( GetProcessHeap(), 0, p, 14 );
574 ok( p2 == NULL, "HeapReAlloc succeeded\n" );
576 ret = HeapFree( GetProcessHeap(), 0, p );
577 ok( !ret || broken(sizeof(void*) == 8), /* not caught on xp64 */
578 "HeapFree succeeded\n" );
581 p[17] = old;
582 size = HeapSize( GetProcessHeap(), 0, p );
583 ok( size == 17, "Wrong size %lu\n", size );
585 p2 = HeapReAlloc( GetProcessHeap(), 0, p, 14 );
586 ok( p2 != NULL, "HeapReAlloc failed\n" );
587 p = p2;
590 ret = HeapFree( GetProcessHeap(), 0, p );
591 ok( ret, "HeapFree failed\n" );
593 p = HeapAlloc( GetProcessHeap(), 0, 37 );
594 ok( p != NULL, "HeapAlloc failed\n" );
595 memset( p, 0xcc, 37 );
597 ret = HeapFree( GetProcessHeap(), 0, p );
598 ok( ret, "HeapFree failed\n" );
600 if (flags & HEAP_FREE_CHECKING_ENABLED)
602 ok( p[16] == 0xee, "wrong data %x\n", p[16] );
603 ok( p[17] == 0xfe, "wrong data %x\n", p[17] );
604 ok( p[18] == 0xee, "wrong data %x\n", p[18] );
605 ok( p[19] == 0xfe, "wrong data %x\n", p[19] );
607 ret = HeapValidate( GetProcessHeap(), 0, NULL );
608 ok( ret, "HeapValidate failed\n" );
610 old = p[16];
611 p[16] = 0xcc;
612 ret = HeapValidate( GetProcessHeap(), 0, NULL );
613 ok( !ret, "HeapValidate succeeded\n" );
615 p[16] = old;
616 ret = HeapValidate( GetProcessHeap(), 0, NULL );
617 ok( ret, "HeapValidate failed\n" );
620 /* now test large blocks */
622 p = HeapAlloc( GetProcessHeap(), 0, large_size );
623 ok( p != NULL, "HeapAlloc failed\n" );
625 ret = HeapValidate( GetProcessHeap(), 0, p );
626 ok( ret, "HeapValidate failed\n" );
628 size = HeapSize( GetProcessHeap(), 0, p );
629 ok( size == large_size, "Wrong size %lu\n", size );
631 ok( p[large_size - 2] == 0, "wrong data %x\n", p[large_size - 2] );
632 ok( p[large_size - 1] == 0, "wrong data %x\n", p[large_size - 1] );
634 if (flags & HEAP_TAIL_CHECKING_ENABLED)
636 /* Windows doesn't do tail checking on large blocks */
637 ok( p[large_size] == 0xab || broken(p[large_size] == 0), "wrong data %x\n", p[large_size] );
638 ok( p[large_size+1] == 0xab || broken(p[large_size+1] == 0), "wrong data %x\n", p[large_size+1] );
639 ok( p[large_size+2] == 0xab || broken(p[large_size+2] == 0), "wrong data %x\n", p[large_size+2] );
640 if (p[large_size] == 0xab)
642 p[large_size] = 0xcc;
643 ret = HeapValidate( GetProcessHeap(), 0, p );
644 ok( !ret, "HeapValidate succeeded\n" );
646 /* other calls only check when HEAP_VALIDATE is set */
647 if (flags & HEAP_VALIDATE)
649 size = HeapSize( GetProcessHeap(), 0, p );
650 ok( size == ~(SIZE_T)0, "Wrong size %lu\n", size );
652 p2 = HeapReAlloc( GetProcessHeap(), 0, p, large_size - 3 );
653 ok( p2 == NULL, "HeapReAlloc succeeded\n" );
655 ret = HeapFree( GetProcessHeap(), 0, p );
656 ok( !ret, "HeapFree succeeded\n" );
658 p[large_size] = 0xab;
662 ret = HeapFree( GetProcessHeap(), 0, p );
663 ok( ret, "HeapFree failed\n" );
665 /* test block sizes when tail checking */
666 if (flags & HEAP_TAIL_CHECKING_ENABLED)
668 for (size = 0; size < 64; size++)
670 p = HeapAlloc( GetProcessHeap(), 0, size );
671 for (i = 0; i < 32; i++) if (p[size + i] != 0xab) break;
672 ok( i >= 8, "only %lu tail bytes for size %lu\n", i, size );
673 HeapFree( GetProcessHeap(), 0, p );
678 static void test_debug_heap( const char *argv0, DWORD flags )
680 char keyname[MAX_PATH];
681 char buffer[MAX_PATH];
682 PROCESS_INFORMATION info;
683 STARTUPINFOA startup;
684 BOOL ret;
685 DWORD err;
686 HKEY hkey;
687 const char *basename;
689 if ((basename = strrchr( argv0, '\\' ))) basename++;
690 else basename = argv0;
692 sprintf( keyname, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Image File Execution Options\\%s",
693 basename );
694 if (!strcmp( keyname + strlen(keyname) - 3, ".so" )) keyname[strlen(keyname) - 3] = 0;
696 err = RegCreateKeyA( HKEY_LOCAL_MACHINE, keyname, &hkey );
697 ok( !err, "failed to create '%s' error %u\n", keyname, err );
698 if (err) return;
700 if (flags == 0xdeadbeef) /* magic value for unsetting it */
701 RegDeleteValueA( hkey, "GlobalFlag" );
702 else
703 RegSetValueExA( hkey, "GlobalFlag", 0, REG_DWORD, (BYTE *)&flags, sizeof(flags) );
705 memset( &startup, 0, sizeof(startup) );
706 startup.cb = sizeof(startup);
708 sprintf( buffer, "%s heap.c 0x%x", argv0, flags );
709 ret = CreateProcessA( NULL, buffer, NULL, NULL, FALSE, 0, NULL, NULL, &startup, &info );
710 ok( ret, "failed to create child process error %u\n", GetLastError() );
711 if (ret)
713 winetest_wait_child_process( info.hProcess );
714 CloseHandle( info.hThread );
715 CloseHandle( info.hProcess );
717 RegDeleteValueA( hkey, "GlobalFlag" );
718 RegCloseKey( hkey );
719 RegDeleteKeyA( HKEY_LOCAL_MACHINE, keyname );
722 static DWORD heap_flags_from_global_flag( DWORD flag )
724 DWORD ret = 0;
726 if (flag & FLG_HEAP_ENABLE_TAIL_CHECK)
727 ret |= HEAP_TAIL_CHECKING_ENABLED;
728 if (flag & FLG_HEAP_ENABLE_FREE_CHECK)
729 ret |= HEAP_FREE_CHECKING_ENABLED;
730 if (flag & FLG_HEAP_VALIDATE_PARAMETERS)
731 ret |= HEAP_VALIDATE_PARAMS | HEAP_VALIDATE | HEAP_TAIL_CHECKING_ENABLED | HEAP_FREE_CHECKING_ENABLED;
732 if (flag & FLG_HEAP_VALIDATE_ALL)
733 ret |= HEAP_VALIDATE_ALL | HEAP_VALIDATE | HEAP_TAIL_CHECKING_ENABLED | HEAP_FREE_CHECKING_ENABLED;
734 if (flag & FLG_HEAP_DISABLE_COALESCING)
735 ret |= HEAP_DISABLE_COALESCE_ON_FREE;
736 if (flag & FLG_HEAP_PAGE_ALLOCS)
737 ret |= HEAP_PAGE_ALLOCS | HEAP_GROWABLE;
738 return ret;
741 static void test_child_heap( const char *arg )
743 struct heap_layout *heap = GetProcessHeap();
744 DWORD expected = strtoul( arg, 0, 16 );
745 DWORD expect_heap;
747 if (expected == 0xdeadbeef) /* expected value comes from Session Manager global flags */
749 HKEY hkey;
750 expected = 0;
751 if (!RegOpenKeyA( HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Control\\Session Manager", &hkey ))
753 char buffer[32];
754 DWORD type, size = sizeof(buffer);
756 if (!RegQueryValueExA( hkey, "GlobalFlag", 0, &type, (BYTE *)buffer, &size ))
758 if (type == REG_DWORD) expected = *(DWORD *)buffer;
759 else if (type == REG_SZ) expected = strtoul( buffer, 0, 16 );
761 RegCloseKey( hkey );
764 if (expected && !pRtlGetNtGlobalFlags()) /* not working on NT4 */
766 win_skip( "global flags not set\n" );
767 return;
770 ok( pRtlGetNtGlobalFlags() == expected,
771 "%s: got global flags %08x expected %08x\n", arg, pRtlGetNtGlobalFlags(), expected );
773 expect_heap = heap_flags_from_global_flag( expected );
775 if (!(heap->flags & HEAP_GROWABLE) || heap->pattern == 0xffeeffee) /* vista layout */
777 ok( heap->flags == 0, "%s: got heap flags %08x expected 0\n", arg, heap->flags );
779 else if (heap->pattern == 0xeeeeeeee && heap->flags == 0xeeeeeeee)
781 ok( expected & FLG_HEAP_PAGE_ALLOCS, "%s: got heap flags 0xeeeeeeee without page alloc\n", arg );
783 else
785 ok( heap->flags == (expect_heap | HEAP_GROWABLE),
786 "%s: got heap flags %08x expected %08x\n", arg, heap->flags, expect_heap );
787 ok( heap->force_flags == (expect_heap & ~0x18000080),
788 "%s: got heap force flags %08x expected %08x\n", arg, heap->force_flags, expect_heap );
789 expect_heap = heap->flags;
792 test_heap_checks( expect_heap );
795 START_TEST(heap)
797 int argc;
798 char **argv;
800 pRtlGetNtGlobalFlags = (void *)GetProcAddress( GetModuleHandleA("ntdll.dll"), "RtlGetNtGlobalFlags" );
802 argc = winetest_get_mainargs( &argv );
803 if (argc >= 3)
805 test_child_heap( argv[2] );
806 return;
809 test_heap();
810 test_obsolete_flags();
812 /* Test both short and very long blocks */
813 test_sized_HeapAlloc(1);
814 test_sized_HeapAlloc(1 << 20);
815 test_sized_HeapReAlloc(1, 100);
816 test_sized_HeapReAlloc(1, (1 << 20));
817 test_sized_HeapReAlloc((1 << 20), (2 << 20));
818 test_sized_HeapReAlloc((1 << 20), 1);
819 test_HeapQueryInformation();
821 if (pRtlGetNtGlobalFlags)
823 test_debug_heap( argv[0], 0 );
824 test_debug_heap( argv[0], FLG_HEAP_ENABLE_TAIL_CHECK );
825 test_debug_heap( argv[0], FLG_HEAP_ENABLE_FREE_CHECK );
826 test_debug_heap( argv[0], FLG_HEAP_VALIDATE_PARAMETERS );
827 test_debug_heap( argv[0], FLG_HEAP_VALIDATE_ALL );
828 test_debug_heap( argv[0], FLG_POOL_ENABLE_TAGGING );
829 test_debug_heap( argv[0], FLG_HEAP_ENABLE_TAGGING );
830 test_debug_heap( argv[0], FLG_HEAP_ENABLE_TAG_BY_DLL );
831 test_debug_heap( argv[0], FLG_HEAP_DISABLE_COALESCING );
832 test_debug_heap( argv[0], FLG_HEAP_PAGE_ALLOCS );
833 test_debug_heap( argv[0], 0xdeadbeef );
835 else win_skip( "RtlGetNtGlobalFlags not found, skipping heap debug tests\n" );