comctl32: Remove redundant parameter from a helper.
[wine/multimedia.git] / dlls / kernel32 / tests / heap.c
blob634e14d9cd21d1cae4f7c45f0ee7d44b28af9c57
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, size2;
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);
407 /* trying to get size from data pointer (GMEM_MOVEABLE) */
408 gbl = GlobalAlloc(GMEM_MOVEABLE, 0x123);
409 ok(gbl != NULL, "returned NULL\n");
410 mem = GlobalLock(gbl);
411 ok(mem != NULL, "returned NULL.\n");
412 ok(gbl != mem, "unexpectedly equal.\n");
414 size = GlobalSize(gbl);
415 size2 = GlobalSize(mem);
416 ok(size == 0x123, "got %lu\n", size);
417 ok(size2 == 0x123, "got %lu\n", size2);
419 GlobalFree(gbl);
421 /* trying to get size from data pointer (GMEM_FIXED) */
422 gbl = GlobalAlloc(GMEM_FIXED, 0x123);
423 ok(gbl != NULL, "returned NULL\n");
424 mem = GlobalLock(gbl);
425 ok(mem != NULL, "returned NULL.\n");
426 ok(gbl == mem, "got %p, %p.\n", gbl, mem);
428 size = GlobalSize(gbl);
429 ok(size == 0x123, "got %lu\n", size);
431 GlobalFree(gbl);
433 size = GlobalSize((void *)0xdeadbee0);
434 ok(size == 0, "got %lu\n", size);
438 static void test_obsolete_flags(void)
440 static struct {
441 UINT flags;
442 UINT globalflags;
443 } test_global_flags[] = {
444 {GMEM_FIXED | GMEM_NOTIFY, 0},
445 {GMEM_FIXED | GMEM_DISCARDABLE, 0},
446 {GMEM_MOVEABLE | GMEM_NOTIFY, 0},
447 {GMEM_MOVEABLE | GMEM_DDESHARE, GMEM_DDESHARE},
448 {GMEM_MOVEABLE | GMEM_NOT_BANKED, 0},
449 {GMEM_MOVEABLE | GMEM_NODISCARD, 0},
450 {GMEM_MOVEABLE | GMEM_DISCARDABLE, GMEM_DISCARDABLE},
451 {GMEM_MOVEABLE | GMEM_DDESHARE | GMEM_DISCARDABLE | GMEM_LOWER | GMEM_NOCOMPACT | GMEM_NODISCARD |
452 GMEM_NOT_BANKED | GMEM_NOTIFY, GMEM_DDESHARE | GMEM_DISCARDABLE},
455 unsigned int i;
456 HGLOBAL gbl;
457 UINT resultflags;
459 UINT (WINAPI *pGlobalFlags)(HGLOBAL);
461 pGlobalFlags = (void *) GetProcAddress(GetModuleHandleA("kernel32"), "GlobalFlags");
463 if (!pGlobalFlags)
465 win_skip("GlobalFlags is not available\n");
466 return;
469 for (i = 0; i < sizeof(test_global_flags)/sizeof(test_global_flags[0]); i++)
471 gbl = GlobalAlloc(test_global_flags[i].flags, 4);
472 ok(gbl != NULL, "GlobalAlloc failed\n");
474 SetLastError(MAGIC_DEAD);
475 resultflags = pGlobalFlags(gbl);
477 ok( resultflags == test_global_flags[i].globalflags ||
478 broken(resultflags == (test_global_flags[i].globalflags & ~GMEM_DDESHARE)), /* win9x */
479 "%u: expected 0x%08x, but returned 0x%08x with %d\n",
480 i, test_global_flags[i].globalflags, resultflags, GetLastError() );
482 GlobalFree(gbl);
486 static void test_HeapQueryInformation(void)
488 ULONG info;
489 SIZE_T size;
490 BOOL ret;
492 pHeapQueryInformation = (void *)GetProcAddress(GetModuleHandle("kernel32.dll"), "HeapQueryInformation");
493 if (!pHeapQueryInformation)
495 win_skip("HeapQueryInformation is not available\n");
496 return;
499 if (0) /* crashes under XP */
501 size = 0;
502 pHeapQueryInformation(0,
503 HeapCompatibilityInformation,
504 &info, sizeof(info), &size);
505 size = 0;
506 pHeapQueryInformation(GetProcessHeap(),
507 HeapCompatibilityInformation,
508 NULL, sizeof(info), &size);
511 size = 0;
512 SetLastError(0xdeadbeef);
513 ret = pHeapQueryInformation(GetProcessHeap(),
514 HeapCompatibilityInformation,
515 NULL, 0, &size);
516 ok(!ret, "HeapQueryInformation should fail\n");
517 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
518 "expected ERROR_INSUFFICIENT_BUFFER got %u\n", GetLastError());
519 ok(size == sizeof(ULONG), "expected 4, got %lu\n", size);
521 SetLastError(0xdeadbeef);
522 ret = pHeapQueryInformation(GetProcessHeap(),
523 HeapCompatibilityInformation,
524 NULL, 0, NULL);
525 ok(!ret, "HeapQueryInformation should fail\n");
526 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
527 "expected ERROR_INSUFFICIENT_BUFFER got %u\n", GetLastError());
529 info = 0xdeadbeaf;
530 SetLastError(0xdeadbeef);
531 ret = pHeapQueryInformation(GetProcessHeap(),
532 HeapCompatibilityInformation,
533 &info, sizeof(info) + 1, NULL);
534 ok(ret, "HeapQueryInformation error %u\n", GetLastError());
535 ok(info == 0 || info == 1 || info == 2, "expected 0, 1 or 2, got %u\n", info);
538 static void test_heap_checks( DWORD flags )
540 BYTE old, *p, *p2;
541 BOOL ret;
542 SIZE_T i, size, large_size = 3000 * 1024 + 37;
544 if (flags & HEAP_PAGE_ALLOCS) return; /* no tests for that case yet */
545 trace( "testing heap flags %08x\n", flags );
547 p = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, 17 );
548 ok( p != NULL, "HeapAlloc failed\n" );
550 ret = HeapValidate( GetProcessHeap(), 0, p );
551 ok( ret, "HeapValidate failed\n" );
553 size = HeapSize( GetProcessHeap(), 0, p );
554 ok( size == 17, "Wrong size %lu\n", size );
556 ok( p[14] == 0, "wrong data %x\n", p[14] );
557 ok( p[15] == 0, "wrong data %x\n", p[15] );
558 ok( p[16] == 0, "wrong data %x\n", p[16] );
560 if (flags & HEAP_TAIL_CHECKING_ENABLED)
562 ok( p[17] == 0xab, "wrong padding %x\n", p[17] );
563 ok( p[18] == 0xab, "wrong padding %x\n", p[18] );
564 ok( p[19] == 0xab, "wrong padding %x\n", p[19] );
567 p2 = HeapReAlloc( GetProcessHeap(), HEAP_REALLOC_IN_PLACE_ONLY, p, 14 );
568 if (p2 == p)
570 if (flags & HEAP_TAIL_CHECKING_ENABLED)
572 ok( p[14] == 0xab, "wrong padding %x\n", p[14] );
573 ok( p[15] == 0xab, "wrong padding %x\n", p[15] );
574 ok( p[16] == 0xab, "wrong padding %x\n", p[16] );
576 else
578 ok( p[14] == 0, "wrong padding %x\n", p[14] );
579 ok( p[15] == 0, "wrong padding %x\n", p[15] );
582 else skip( "realloc in place failed\n");
584 ret = HeapFree( GetProcessHeap(), 0, p );
585 ok( ret, "HeapFree failed\n" );
587 p = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, 17 );
588 ok( p != NULL, "HeapAlloc failed\n" );
589 old = p[17];
590 p[17] = 0xcc;
592 if (flags & HEAP_TAIL_CHECKING_ENABLED)
594 ret = HeapValidate( GetProcessHeap(), 0, p );
595 ok( !ret, "HeapValidate succeeded\n" );
597 /* other calls only check when HEAP_VALIDATE is set */
598 if (flags & HEAP_VALIDATE)
600 size = HeapSize( GetProcessHeap(), 0, p );
601 ok( size == ~(SIZE_T)0 || broken(size == ~0u), "Wrong size %lu\n", size );
603 p2 = HeapReAlloc( GetProcessHeap(), 0, p, 14 );
604 ok( p2 == NULL, "HeapReAlloc succeeded\n" );
606 ret = HeapFree( GetProcessHeap(), 0, p );
607 ok( !ret || broken(sizeof(void*) == 8), /* not caught on xp64 */
608 "HeapFree succeeded\n" );
611 p[17] = old;
612 size = HeapSize( GetProcessHeap(), 0, p );
613 ok( size == 17, "Wrong size %lu\n", size );
615 p2 = HeapReAlloc( GetProcessHeap(), 0, p, 14 );
616 ok( p2 != NULL, "HeapReAlloc failed\n" );
617 p = p2;
620 ret = HeapFree( GetProcessHeap(), 0, p );
621 ok( ret, "HeapFree failed\n" );
623 p = HeapAlloc( GetProcessHeap(), 0, 37 );
624 ok( p != NULL, "HeapAlloc failed\n" );
625 memset( p, 0xcc, 37 );
627 ret = HeapFree( GetProcessHeap(), 0, p );
628 ok( ret, "HeapFree failed\n" );
630 if (flags & HEAP_FREE_CHECKING_ENABLED)
632 ok( p[16] == 0xee, "wrong data %x\n", p[16] );
633 ok( p[17] == 0xfe, "wrong data %x\n", p[17] );
634 ok( p[18] == 0xee, "wrong data %x\n", p[18] );
635 ok( p[19] == 0xfe, "wrong data %x\n", p[19] );
637 ret = HeapValidate( GetProcessHeap(), 0, NULL );
638 ok( ret, "HeapValidate failed\n" );
640 old = p[16];
641 p[16] = 0xcc;
642 ret = HeapValidate( GetProcessHeap(), 0, NULL );
643 ok( !ret, "HeapValidate succeeded\n" );
645 p[16] = old;
646 ret = HeapValidate( GetProcessHeap(), 0, NULL );
647 ok( ret, "HeapValidate failed\n" );
650 /* now test large blocks */
652 p = HeapAlloc( GetProcessHeap(), 0, large_size );
653 ok( p != NULL, "HeapAlloc failed\n" );
655 ret = HeapValidate( GetProcessHeap(), 0, p );
656 ok( ret, "HeapValidate failed\n" );
658 size = HeapSize( GetProcessHeap(), 0, p );
659 ok( size == large_size, "Wrong size %lu\n", size );
661 ok( p[large_size - 2] == 0, "wrong data %x\n", p[large_size - 2] );
662 ok( p[large_size - 1] == 0, "wrong data %x\n", p[large_size - 1] );
664 if (flags & HEAP_TAIL_CHECKING_ENABLED)
666 /* Windows doesn't do tail checking on large blocks */
667 ok( p[large_size] == 0xab || broken(p[large_size] == 0), "wrong data %x\n", p[large_size] );
668 ok( p[large_size+1] == 0xab || broken(p[large_size+1] == 0), "wrong data %x\n", p[large_size+1] );
669 ok( p[large_size+2] == 0xab || broken(p[large_size+2] == 0), "wrong data %x\n", p[large_size+2] );
670 if (p[large_size] == 0xab)
672 p[large_size] = 0xcc;
673 ret = HeapValidate( GetProcessHeap(), 0, p );
674 ok( !ret, "HeapValidate succeeded\n" );
676 /* other calls only check when HEAP_VALIDATE is set */
677 if (flags & HEAP_VALIDATE)
679 size = HeapSize( GetProcessHeap(), 0, p );
680 ok( size == ~(SIZE_T)0, "Wrong size %lu\n", size );
682 p2 = HeapReAlloc( GetProcessHeap(), 0, p, large_size - 3 );
683 ok( p2 == NULL, "HeapReAlloc succeeded\n" );
685 ret = HeapFree( GetProcessHeap(), 0, p );
686 ok( !ret, "HeapFree succeeded\n" );
688 p[large_size] = 0xab;
692 ret = HeapFree( GetProcessHeap(), 0, p );
693 ok( ret, "HeapFree failed\n" );
695 /* test block sizes when tail checking */
696 if (flags & HEAP_TAIL_CHECKING_ENABLED)
698 for (size = 0; size < 64; size++)
700 p = HeapAlloc( GetProcessHeap(), 0, size );
701 for (i = 0; i < 32; i++) if (p[size + i] != 0xab) break;
702 ok( i >= 8, "only %lu tail bytes for size %lu\n", i, size );
703 HeapFree( GetProcessHeap(), 0, p );
708 static void test_debug_heap( const char *argv0, DWORD flags )
710 char keyname[MAX_PATH];
711 char buffer[MAX_PATH];
712 PROCESS_INFORMATION info;
713 STARTUPINFOA startup;
714 BOOL ret;
715 DWORD err;
716 HKEY hkey;
717 const char *basename;
719 if ((basename = strrchr( argv0, '\\' ))) basename++;
720 else basename = argv0;
722 sprintf( keyname, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Image File Execution Options\\%s",
723 basename );
724 if (!strcmp( keyname + strlen(keyname) - 3, ".so" )) keyname[strlen(keyname) - 3] = 0;
726 err = RegCreateKeyA( HKEY_LOCAL_MACHINE, keyname, &hkey );
727 if (err == ERROR_ACCESS_DENIED)
729 skip("Not authorized to change the image file execution options\n");
730 return;
732 ok( !err, "failed to create '%s' error %u\n", keyname, err );
733 if (err) return;
735 if (flags == 0xdeadbeef) /* magic value for unsetting it */
736 RegDeleteValueA( hkey, "GlobalFlag" );
737 else
738 RegSetValueExA( hkey, "GlobalFlag", 0, REG_DWORD, (BYTE *)&flags, sizeof(flags) );
740 memset( &startup, 0, sizeof(startup) );
741 startup.cb = sizeof(startup);
743 sprintf( buffer, "%s heap.c 0x%x", argv0, flags );
744 ret = CreateProcessA( NULL, buffer, NULL, NULL, FALSE, 0, NULL, NULL, &startup, &info );
745 ok( ret, "failed to create child process error %u\n", GetLastError() );
746 if (ret)
748 winetest_wait_child_process( info.hProcess );
749 CloseHandle( info.hThread );
750 CloseHandle( info.hProcess );
752 RegDeleteValueA( hkey, "GlobalFlag" );
753 RegCloseKey( hkey );
754 RegDeleteKeyA( HKEY_LOCAL_MACHINE, keyname );
757 static DWORD heap_flags_from_global_flag( DWORD flag )
759 DWORD ret = 0;
761 if (flag & FLG_HEAP_ENABLE_TAIL_CHECK)
762 ret |= HEAP_TAIL_CHECKING_ENABLED;
763 if (flag & FLG_HEAP_ENABLE_FREE_CHECK)
764 ret |= HEAP_FREE_CHECKING_ENABLED;
765 if (flag & FLG_HEAP_VALIDATE_PARAMETERS)
766 ret |= HEAP_VALIDATE_PARAMS | HEAP_VALIDATE | HEAP_TAIL_CHECKING_ENABLED | HEAP_FREE_CHECKING_ENABLED;
767 if (flag & FLG_HEAP_VALIDATE_ALL)
768 ret |= HEAP_VALIDATE_ALL | HEAP_VALIDATE | HEAP_TAIL_CHECKING_ENABLED | HEAP_FREE_CHECKING_ENABLED;
769 if (flag & FLG_HEAP_DISABLE_COALESCING)
770 ret |= HEAP_DISABLE_COALESCE_ON_FREE;
771 if (flag & FLG_HEAP_PAGE_ALLOCS)
772 ret |= HEAP_PAGE_ALLOCS | HEAP_GROWABLE;
773 return ret;
776 static void test_child_heap( const char *arg )
778 struct heap_layout *heap = GetProcessHeap();
779 DWORD expected = strtoul( arg, 0, 16 );
780 DWORD expect_heap;
782 if (expected == 0xdeadbeef) /* expected value comes from Session Manager global flags */
784 HKEY hkey;
785 expected = 0;
786 if (!RegOpenKeyA( HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Control\\Session Manager", &hkey ))
788 char buffer[32];
789 DWORD type, size = sizeof(buffer);
791 if (!RegQueryValueExA( hkey, "GlobalFlag", 0, &type, (BYTE *)buffer, &size ))
793 if (type == REG_DWORD) expected = *(DWORD *)buffer;
794 else if (type == REG_SZ) expected = strtoul( buffer, 0, 16 );
796 RegCloseKey( hkey );
799 if (expected && !pRtlGetNtGlobalFlags()) /* not working on NT4 */
801 win_skip( "global flags not set\n" );
802 return;
805 ok( pRtlGetNtGlobalFlags() == expected,
806 "%s: got global flags %08x expected %08x\n", arg, pRtlGetNtGlobalFlags(), expected );
808 expect_heap = heap_flags_from_global_flag( expected );
810 if (!(heap->flags & HEAP_GROWABLE) || heap->pattern == 0xffeeffee) /* vista layout */
812 ok( heap->flags == 0, "%s: got heap flags %08x expected 0\n", arg, heap->flags );
814 else if (heap->pattern == 0xeeeeeeee && heap->flags == 0xeeeeeeee)
816 ok( expected & FLG_HEAP_PAGE_ALLOCS, "%s: got heap flags 0xeeeeeeee without page alloc\n", arg );
818 else
820 ok( heap->flags == (expect_heap | HEAP_GROWABLE),
821 "%s: got heap flags %08x expected %08x\n", arg, heap->flags, expect_heap );
822 ok( heap->force_flags == (expect_heap & ~0x18000080),
823 "%s: got heap force flags %08x expected %08x\n", arg, heap->force_flags, expect_heap );
824 expect_heap = heap->flags;
827 test_heap_checks( expect_heap );
830 START_TEST(heap)
832 int argc;
833 char **argv;
835 pRtlGetNtGlobalFlags = (void *)GetProcAddress( GetModuleHandleA("ntdll.dll"), "RtlGetNtGlobalFlags" );
837 argc = winetest_get_mainargs( &argv );
838 if (argc >= 3)
840 test_child_heap( argv[2] );
841 return;
844 test_heap();
845 test_obsolete_flags();
847 /* Test both short and very long blocks */
848 test_sized_HeapAlloc(1);
849 test_sized_HeapAlloc(1 << 20);
850 test_sized_HeapReAlloc(1, 100);
851 test_sized_HeapReAlloc(1, (1 << 20));
852 test_sized_HeapReAlloc((1 << 20), (2 << 20));
853 test_sized_HeapReAlloc((1 << 20), 1);
854 test_HeapQueryInformation();
856 if (pRtlGetNtGlobalFlags)
858 test_debug_heap( argv[0], 0 );
859 test_debug_heap( argv[0], FLG_HEAP_ENABLE_TAIL_CHECK );
860 test_debug_heap( argv[0], FLG_HEAP_ENABLE_FREE_CHECK );
861 test_debug_heap( argv[0], FLG_HEAP_VALIDATE_PARAMETERS );
862 test_debug_heap( argv[0], FLG_HEAP_VALIDATE_ALL );
863 test_debug_heap( argv[0], FLG_POOL_ENABLE_TAGGING );
864 test_debug_heap( argv[0], FLG_HEAP_ENABLE_TAGGING );
865 test_debug_heap( argv[0], FLG_HEAP_ENABLE_TAG_BY_DLL );
866 test_debug_heap( argv[0], FLG_HEAP_DISABLE_COALESCING );
867 test_debug_heap( argv[0], FLG_HEAP_PAGE_ALLOCS );
868 test_debug_heap( argv[0], 0xdeadbeef );
870 else win_skip( "RtlGetNtGlobalFlags not found, skipping heap debug tests\n" );