Release 1.6-rc2.
[wine/testsucceed.git] / dlls / kernel32 / tests / heap.c
blobe36336cdd1417f77371b7916cbf64cd081898627
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;
89 const SIZE_T max_size = 1024, init_size = 10;
91 /* Heap*() functions */
92 mem = HeapAlloc(GetProcessHeap(), 0, 0);
93 ok(mem != NULL, "memory not allocated for size 0\n");
94 HeapFree(GetProcessHeap(), 0, mem);
96 mem = HeapReAlloc(GetProcessHeap(), 0, NULL, 10);
97 ok(mem == NULL, "memory allocated by HeapReAlloc\n");
99 for (size = 0; size <= 256; size++)
101 SIZE_T heap_size;
102 mem = HeapAlloc(GetProcessHeap(), 0, size);
103 heap_size = HeapSize(GetProcessHeap(), 0, mem);
104 ok(heap_size == size || heap_size == resize_9x(size),
105 "HeapSize returned %lu instead of %lu or %lu\n", heap_size, size, resize_9x(size));
106 HeapFree(GetProcessHeap(), 0, mem);
109 /* test some border cases of HeapAlloc and HeapReAlloc */
110 mem = HeapAlloc(GetProcessHeap(), 0, 0);
111 ok(mem != NULL, "memory not allocated for size 0\n");
112 msecond = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, mem, ~(SIZE_T)0 - 7);
113 ok(msecond == NULL, "HeapReAlloc(~0 - 7) should have failed\n");
114 msecond = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, mem, ~(SIZE_T)0);
115 ok(msecond == NULL, "HeapReAlloc(~0) should have failed\n");
116 HeapFree(GetProcessHeap(), 0, mem);
117 mem = HeapAlloc(GetProcessHeap(), 0, ~(SIZE_T)0);
118 ok(mem == NULL, "memory allocated for size ~0\n");
120 /* large blocks must be 16-byte aligned */
121 mem = HeapAlloc(GetProcessHeap(), 0, 512 * 1024);
122 ok( mem != NULL, "failed for size 512K\n" );
123 ok( (ULONG_PTR)mem % 16 == 0 || broken((ULONG_PTR)mem % 16) /* win9x */,
124 "512K block not 16-byte aligned\n" );
125 HeapFree(GetProcessHeap(), 0, mem);
127 /* Global*() functions */
128 gbl = GlobalAlloc(GMEM_MOVEABLE, 0);
129 ok(gbl != NULL, "global memory not allocated for size 0\n");
131 gbl = GlobalReAlloc(gbl, 10, GMEM_MOVEABLE);
132 ok(gbl != NULL, "Can't realloc global memory\n");
133 size = GlobalSize(gbl);
134 ok(size >= 10 && size <= 16, "Memory not resized to size 10, instead size=%ld\n", size);
136 gbl = GlobalReAlloc(gbl, 0, GMEM_MOVEABLE);
137 ok(gbl != NULL, "GlobalReAlloc should not fail on size 0\n");
139 size = GlobalSize(gbl);
140 ok(size == 0, "Memory not resized to size 0, instead size=%ld\n", size);
141 ok(GlobalFree(gbl) == NULL, "Memory not freed\n");
142 size = GlobalSize(gbl);
143 ok(size == 0, "Memory should have been freed, size=%ld\n", size);
145 gbl = GlobalReAlloc(0, 10, GMEM_MOVEABLE);
146 ok(gbl == NULL, "global realloc allocated memory\n");
148 /* GlobalLock / GlobalUnlock with a valid handle */
149 gbl = GlobalAlloc(GMEM_MOVEABLE, 256);
151 SetLastError(MAGIC_DEAD);
152 mem = GlobalLock(gbl); /* #1 */
153 ok(mem != NULL, "returned %p with %d (expected '!= NULL')\n", mem, GetLastError());
154 SetLastError(MAGIC_DEAD);
155 flags = GlobalFlags(gbl);
156 ok( flags == 1, "returned 0x%04x with %d (expected '0x0001')\n",
157 flags, GetLastError());
159 SetLastError(MAGIC_DEAD);
160 msecond = GlobalLock(gbl); /* #2 */
161 ok( msecond == mem, "returned %p with %d (expected '%p')\n",
162 msecond, GetLastError(), mem);
163 SetLastError(MAGIC_DEAD);
164 flags = GlobalFlags(gbl);
165 ok( flags == 2, "returned 0x%04x with %d (expected '0x0002')\n",
166 flags, GetLastError());
167 SetLastError(MAGIC_DEAD);
169 SetLastError(MAGIC_DEAD);
170 res = GlobalUnlock(gbl); /* #1 */
171 ok(res, "returned %d with %d (expected '!= 0')\n", res, GetLastError());
172 SetLastError(MAGIC_DEAD);
173 flags = GlobalFlags(gbl);
174 ok( flags , "returned 0x%04x with %d (expected '!= 0')\n",
175 flags, GetLastError());
177 SetLastError(MAGIC_DEAD);
178 res = GlobalUnlock(gbl); /* #0 */
179 /* NT: ERROR_SUCCESS (documented on MSDN), 9x: untouched */
180 ok(!res && ((GetLastError() == ERROR_SUCCESS) || (GetLastError() == MAGIC_DEAD)),
181 "returned %d with %d (expected '0' with: ERROR_SUCCESS or "
182 "MAGIC_DEAD)\n", res, GetLastError());
183 SetLastError(MAGIC_DEAD);
184 flags = GlobalFlags(gbl);
185 ok( !flags , "returned 0x%04x with %d (expected '0')\n",
186 flags, GetLastError());
188 /* Unlock an already unlocked Handle */
189 SetLastError(MAGIC_DEAD);
190 res = GlobalUnlock(gbl);
191 /* NT: ERROR_NOT_LOCKED, 9x: untouched */
192 ok( !res &&
193 ((GetLastError() == ERROR_NOT_LOCKED) || (GetLastError() == MAGIC_DEAD)),
194 "returned %d with %d (expected '0' with: ERROR_NOT_LOCKED or "
195 "MAGIC_DEAD)\n", res, GetLastError());
197 GlobalFree(gbl);
198 /* invalid handles are caught in windows: */
199 SetLastError(MAGIC_DEAD);
200 hsecond = GlobalFree(gbl); /* invalid handle: free memory twice */
201 ok( (hsecond == gbl) && (GetLastError() == ERROR_INVALID_HANDLE),
202 "returned %p with 0x%08x (expected %p with ERROR_INVALID_HANDLE)\n",
203 hsecond, GetLastError(), gbl);
204 SetLastError(MAGIC_DEAD);
205 flags = GlobalFlags(gbl);
206 ok( (flags == GMEM_INVALID_HANDLE) && (GetLastError() == ERROR_INVALID_HANDLE),
207 "returned 0x%04x with 0x%08x (expected GMEM_INVALID_HANDLE with "
208 "ERROR_INVALID_HANDLE)\n", flags, GetLastError());
209 SetLastError(MAGIC_DEAD);
210 size = GlobalSize(gbl);
211 ok( (size == 0) && (GetLastError() == ERROR_INVALID_HANDLE),
212 "returned %ld with 0x%08x (expected '0' with ERROR_INVALID_HANDLE)\n",
213 size, GetLastError());
215 SetLastError(MAGIC_DEAD);
216 mem = GlobalLock(gbl);
217 ok( (mem == NULL) && (GetLastError() == ERROR_INVALID_HANDLE),
218 "returned %p with 0x%08x (expected NULL with ERROR_INVALID_HANDLE)\n",
219 mem, GetLastError());
221 /* documented on MSDN: GlobalUnlock() return FALSE on failure.
222 Win9x and wine return FALSE with ERROR_INVALID_HANDLE, but on
223 NT 3.51 and XPsp2, TRUE with ERROR_INVALID_HANDLE is returned.
224 The similar Test for LocalUnlock() works on all Systems */
225 SetLastError(MAGIC_DEAD);
226 res = GlobalUnlock(gbl);
227 ok(GetLastError() == ERROR_INVALID_HANDLE,
228 "returned %d with %d (expected ERROR_INVALID_HANDLE)\n",
229 res, GetLastError());
231 gbl = GlobalAlloc(GMEM_DDESHARE, 100);
233 /* first free */
234 mem = GlobalFree(gbl);
235 ok(mem == NULL, "Expected NULL, got %p\n", mem);
237 /* invalid free */
238 if (sizeof(void *) != 8) /* crashes on 64-bit Vista */
240 SetLastError(MAGIC_DEAD);
241 mem = GlobalFree(gbl);
242 ok(mem == gbl || broken(mem == NULL) /* nt4 */, "Expected gbl, got %p\n", mem);
243 if (mem == gbl)
244 ok(GetLastError() == ERROR_INVALID_HANDLE ||
245 GetLastError() == ERROR_INVALID_PARAMETER, /* win9x */
246 "Expected ERROR_INVALID_HANDLE or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
249 /* GMEM_FIXED block expands in place only without flags */
250 for (size = 1; size <= max_size; size <<= 1) {
251 gbl = GlobalAlloc(GMEM_FIXED, init_size);
252 SetLastError(MAGIC_DEAD);
253 hsecond = GlobalReAlloc(gbl, size + init_size, 0);
254 ok(hsecond == gbl || (hsecond == NULL && GetLastError() == ERROR_NOT_ENOUGH_MEMORY),
255 "got %p with %x (expected %p or NULL) @%ld\n", hsecond, GetLastError(), gbl, size);
256 GlobalFree(gbl);
259 /* GMEM_FIXED block can be relocated with GMEM_MOVEABLE */
260 for (size = 1; size <= max_size; size <<= 1) {
261 gbl = GlobalAlloc(GMEM_FIXED, init_size);
262 SetLastError(MAGIC_DEAD);
263 hsecond = GlobalReAlloc(gbl, size + init_size, GMEM_MOVEABLE);
264 ok(hsecond != NULL,
265 "got %p with %x (expected non-NULL) @%ld\n", hsecond, GetLastError(), size);
266 mem = GlobalLock(hsecond);
267 ok(mem == hsecond, "got %p (expected %p) @%ld\n", mem, hsecond, size);
268 GlobalFree(hsecond);
271 gbl = GlobalAlloc(GMEM_DDESHARE, 100);
273 res = GlobalUnlock(gbl);
274 ok(res == 1 ||
275 broken(res == 0), /* win9x */
276 "Expected 1 or 0, got %d\n", res);
278 res = GlobalUnlock(gbl);
279 ok(res == 1 ||
280 broken(res == 0), /* win9x */
281 "Expected 1 or 0, got %d\n", res);
283 GlobalFree(gbl);
285 gbl = GlobalAlloc(GMEM_FIXED, 100);
287 SetLastError(0xdeadbeef);
288 res = GlobalUnlock(gbl);
289 ok(res == 1 ||
290 broken(res == 0), /* win9x */
291 "Expected 1 or 0, got %d\n", res);
292 ok(GetLastError() == 0xdeadbeef, "got %d\n", GetLastError());
294 GlobalFree(gbl);
296 /* GlobalSize on an invalid handle */
297 if (sizeof(void *) != 8) /* crashes on 64-bit Vista */
299 SetLastError(MAGIC_DEAD);
300 size = GlobalSize((HGLOBAL)0xc042);
301 ok(size == 0, "Expected 0, got %ld\n", size);
302 ok(GetLastError() == ERROR_INVALID_HANDLE ||
303 GetLastError() == ERROR_INVALID_PARAMETER, /* win9x */
304 "Expected ERROR_INVALID_HANDLE or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
307 /* ####################################### */
308 /* Local*() functions */
309 gbl = LocalAlloc(LMEM_MOVEABLE, 0);
310 ok(gbl != NULL, "local memory not allocated for size 0\n");
312 gbl = LocalReAlloc(gbl, 10, LMEM_MOVEABLE);
313 ok(gbl != NULL, "Can't realloc local memory\n");
314 size = LocalSize(gbl);
315 ok(size >= 10 && size <= 16, "Memory not resized to size 10, instead size=%ld\n", size);
317 gbl = LocalReAlloc(gbl, 0, LMEM_MOVEABLE);
318 ok(gbl != NULL, "LocalReAlloc should not fail on size 0\n");
320 size = LocalSize(gbl);
321 ok(size == 0, "Memory not resized to size 0, instead size=%ld\n", size);
322 ok(LocalFree(gbl) == NULL, "Memory not freed\n");
323 size = LocalSize(gbl);
324 ok(size == 0, "Memory should have been freed, size=%ld\n", size);
326 gbl = LocalReAlloc(0, 10, LMEM_MOVEABLE);
327 ok(gbl == NULL, "local realloc allocated memory\n");
329 /* LocalLock / LocalUnlock with a valid handle */
330 gbl = LocalAlloc(LMEM_MOVEABLE, 256);
331 SetLastError(MAGIC_DEAD);
332 mem = LocalLock(gbl); /* #1 */
333 ok(mem != NULL, "returned %p with %d (expected '!= NULL')\n", mem, GetLastError());
334 SetLastError(MAGIC_DEAD);
335 flags = LocalFlags(gbl);
336 ok( flags == 1, "returned 0x%04x with %d (expected '0x0001')\n",
337 flags, GetLastError());
339 SetLastError(MAGIC_DEAD);
340 msecond = LocalLock(gbl); /* #2 */
341 ok( msecond == mem, "returned %p with %d (expected '%p')\n",
342 msecond, GetLastError(), mem);
343 SetLastError(MAGIC_DEAD);
344 flags = LocalFlags(gbl);
345 ok( flags == 2, "returned 0x%04x with %d (expected '0x0002')\n",
346 flags, GetLastError());
347 SetLastError(MAGIC_DEAD);
349 SetLastError(MAGIC_DEAD);
350 res = LocalUnlock(gbl); /* #1 */
351 ok(res, "returned %d with %d (expected '!= 0')\n", res, GetLastError());
352 SetLastError(MAGIC_DEAD);
353 flags = LocalFlags(gbl);
354 ok( flags , "returned 0x%04x with %d (expected '!= 0')\n",
355 flags, GetLastError());
357 SetLastError(MAGIC_DEAD);
358 res = LocalUnlock(gbl); /* #0 */
359 /* NT: ERROR_SUCCESS (documented on MSDN), 9x: untouched */
360 ok(!res && ((GetLastError() == ERROR_SUCCESS) || (GetLastError() == MAGIC_DEAD)),
361 "returned %d with %d (expected '0' with: ERROR_SUCCESS or "
362 "MAGIC_DEAD)\n", res, GetLastError());
363 SetLastError(MAGIC_DEAD);
364 flags = LocalFlags(gbl);
365 ok( !flags , "returned 0x%04x with %d (expected '0')\n",
366 flags, GetLastError());
368 /* Unlock an already unlocked Handle */
369 SetLastError(MAGIC_DEAD);
370 res = LocalUnlock(gbl);
371 /* NT: ERROR_NOT_LOCKED, 9x: untouched */
372 ok( !res &&
373 ((GetLastError() == ERROR_NOT_LOCKED) || (GetLastError() == MAGIC_DEAD)),
374 "returned %d with %d (expected '0' with: ERROR_NOT_LOCKED or "
375 "MAGIC_DEAD)\n", res, GetLastError());
377 LocalFree(gbl);
378 /* invalid handles are caught in windows: */
379 SetLastError(MAGIC_DEAD);
380 hsecond = LocalFree(gbl); /* invalid handle: free memory twice */
381 ok( (hsecond == gbl) && (GetLastError() == ERROR_INVALID_HANDLE),
382 "returned %p with 0x%08x (expected %p with ERROR_INVALID_HANDLE)\n",
383 hsecond, GetLastError(), gbl);
384 SetLastError(MAGIC_DEAD);
385 flags = LocalFlags(gbl);
386 ok( (flags == LMEM_INVALID_HANDLE) && (GetLastError() == ERROR_INVALID_HANDLE),
387 "returned 0x%04x with 0x%08x (expected LMEM_INVALID_HANDLE with "
388 "ERROR_INVALID_HANDLE)\n", flags, GetLastError());
389 SetLastError(MAGIC_DEAD);
390 size = LocalSize(gbl);
391 ok( (size == 0) && (GetLastError() == ERROR_INVALID_HANDLE),
392 "returned %ld with 0x%08x (expected '0' with ERROR_INVALID_HANDLE)\n",
393 size, GetLastError());
395 SetLastError(MAGIC_DEAD);
396 mem = LocalLock(gbl);
397 ok( (mem == NULL) && (GetLastError() == ERROR_INVALID_HANDLE),
398 "returned %p with 0x%08x (expected NULL with ERROR_INVALID_HANDLE)\n",
399 mem, GetLastError());
401 /* This Test works the same on all Systems (GlobalUnlock() is different) */
402 SetLastError(MAGIC_DEAD);
403 res = LocalUnlock(gbl);
404 ok(!res && (GetLastError() == ERROR_INVALID_HANDLE),
405 "returned %d with %d (expected '0' with ERROR_INVALID_HANDLE)\n",
406 res, GetLastError());
408 /* LMEM_FIXED block expands in place only without flags */
409 for (size = 1; size <= max_size; size <<= 1) {
410 gbl = LocalAlloc(LMEM_FIXED, init_size);
411 SetLastError(MAGIC_DEAD);
412 hsecond = LocalReAlloc(gbl, size + init_size, 0);
413 ok(hsecond == gbl || (hsecond == NULL && GetLastError() == ERROR_NOT_ENOUGH_MEMORY),
414 "got %p with %x (expected %p or NULL) @%ld\n", hsecond, GetLastError(), gbl, size);
415 LocalFree(gbl);
418 /* LMEM_FIXED memory can be relocated with LMEM_MOVEABLE */
419 for (size = 1; size <= max_size; size <<= 1) {
420 gbl = LocalAlloc(LMEM_FIXED, init_size);
421 SetLastError(MAGIC_DEAD);
422 hsecond = LocalReAlloc(gbl, size + init_size, LMEM_MOVEABLE);
423 ok(hsecond != NULL,
424 "got %p with %x (expected non-NULL) @%ld\n", hsecond, GetLastError(), size);
425 mem = LocalLock(hsecond);
426 ok(mem == hsecond, "got %p (expected %p) @%ld\n", mem, hsecond, size);
427 LocalFree(hsecond);
430 /* trying to unlock pointer from LocalAlloc */
431 gbl = LocalAlloc(LMEM_FIXED, 100);
432 SetLastError(0xdeadbeef);
433 res = LocalUnlock(gbl);
434 ok(res == 0, "Expected 0, got %d\n", res);
435 ok(GetLastError() == ERROR_NOT_LOCKED ||
436 broken(GetLastError() == 0xdeadbeef) /* win9x */, "got %d\n", GetLastError());
437 LocalFree(gbl);
439 /* trying to lock empty memory should give an error */
440 gbl = GlobalAlloc(GMEM_MOVEABLE|GMEM_ZEROINIT,0);
441 ok(gbl != NULL, "returned NULL\n");
442 SetLastError(MAGIC_DEAD);
443 mem = GlobalLock(gbl);
444 /* NT: ERROR_DISCARDED, 9x: untouched */
445 ok( (mem == NULL) &&
446 ((GetLastError() == ERROR_DISCARDED) || (GetLastError() == MAGIC_DEAD)),
447 "returned %p with 0x%x/%d (expected 'NULL' with: ERROR_DISCARDED or "
448 "MAGIC_DEAD)\n", mem, GetLastError(), GetLastError());
450 GlobalFree(gbl);
452 /* trying to get size from data pointer (GMEM_MOVEABLE) */
453 gbl = GlobalAlloc(GMEM_MOVEABLE, 0x123);
454 ok(gbl != NULL, "returned NULL\n");
455 mem = GlobalLock(gbl);
456 ok(mem != NULL, "returned NULL.\n");
457 ok(gbl != mem, "unexpectedly equal.\n");
459 size = GlobalSize(gbl);
460 size2 = GlobalSize(mem);
461 ok(size == 0x123, "got %lu\n", size);
462 ok(size2 == 0x123, "got %lu\n", size2);
464 GlobalFree(gbl);
466 /* trying to get size from data pointer (GMEM_FIXED) */
467 gbl = GlobalAlloc(GMEM_FIXED, 0x123);
468 ok(gbl != NULL, "returned NULL\n");
469 mem = GlobalLock(gbl);
470 ok(mem != NULL, "returned NULL.\n");
471 ok(gbl == mem, "got %p, %p.\n", gbl, mem);
473 size = GlobalSize(gbl);
474 ok(size == 0x123, "got %lu\n", size);
476 GlobalFree(gbl);
478 size = GlobalSize((void *)0xdeadbee0);
479 ok(size == 0, "got %lu\n", size);
483 static void test_obsolete_flags(void)
485 static struct {
486 UINT flags;
487 UINT globalflags;
488 } test_global_flags[] = {
489 {GMEM_FIXED | GMEM_NOTIFY, 0},
490 {GMEM_FIXED | GMEM_DISCARDABLE, 0},
491 {GMEM_MOVEABLE | GMEM_NOTIFY, 0},
492 {GMEM_MOVEABLE | GMEM_DDESHARE, GMEM_DDESHARE},
493 {GMEM_MOVEABLE | GMEM_NOT_BANKED, 0},
494 {GMEM_MOVEABLE | GMEM_NODISCARD, 0},
495 {GMEM_MOVEABLE | GMEM_DISCARDABLE, GMEM_DISCARDABLE},
496 {GMEM_MOVEABLE | GMEM_DDESHARE | GMEM_DISCARDABLE | GMEM_LOWER | GMEM_NOCOMPACT | GMEM_NODISCARD |
497 GMEM_NOT_BANKED | GMEM_NOTIFY, GMEM_DDESHARE | GMEM_DISCARDABLE},
500 unsigned int i;
501 HGLOBAL gbl;
502 UINT resultflags;
504 UINT (WINAPI *pGlobalFlags)(HGLOBAL);
506 pGlobalFlags = (void *) GetProcAddress(GetModuleHandleA("kernel32"), "GlobalFlags");
508 if (!pGlobalFlags)
510 win_skip("GlobalFlags is not available\n");
511 return;
514 for (i = 0; i < sizeof(test_global_flags)/sizeof(test_global_flags[0]); i++)
516 gbl = GlobalAlloc(test_global_flags[i].flags, 4);
517 ok(gbl != NULL, "GlobalAlloc failed\n");
519 SetLastError(MAGIC_DEAD);
520 resultflags = pGlobalFlags(gbl);
522 ok( resultflags == test_global_flags[i].globalflags ||
523 broken(resultflags == (test_global_flags[i].globalflags & ~GMEM_DDESHARE)), /* win9x */
524 "%u: expected 0x%08x, but returned 0x%08x with %d\n",
525 i, test_global_flags[i].globalflags, resultflags, GetLastError() );
527 GlobalFree(gbl);
531 static void test_HeapQueryInformation(void)
533 ULONG info;
534 SIZE_T size;
535 BOOL ret;
537 pHeapQueryInformation = (void *)GetProcAddress(GetModuleHandle("kernel32.dll"), "HeapQueryInformation");
538 if (!pHeapQueryInformation)
540 win_skip("HeapQueryInformation is not available\n");
541 return;
544 if (0) /* crashes under XP */
546 size = 0;
547 pHeapQueryInformation(0,
548 HeapCompatibilityInformation,
549 &info, sizeof(info), &size);
550 size = 0;
551 pHeapQueryInformation(GetProcessHeap(),
552 HeapCompatibilityInformation,
553 NULL, sizeof(info), &size);
556 size = 0;
557 SetLastError(0xdeadbeef);
558 ret = pHeapQueryInformation(GetProcessHeap(),
559 HeapCompatibilityInformation,
560 NULL, 0, &size);
561 ok(!ret, "HeapQueryInformation should fail\n");
562 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
563 "expected ERROR_INSUFFICIENT_BUFFER got %u\n", GetLastError());
564 ok(size == sizeof(ULONG), "expected 4, got %lu\n", size);
566 SetLastError(0xdeadbeef);
567 ret = pHeapQueryInformation(GetProcessHeap(),
568 HeapCompatibilityInformation,
569 NULL, 0, NULL);
570 ok(!ret, "HeapQueryInformation should fail\n");
571 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
572 "expected ERROR_INSUFFICIENT_BUFFER got %u\n", GetLastError());
574 info = 0xdeadbeaf;
575 SetLastError(0xdeadbeef);
576 ret = pHeapQueryInformation(GetProcessHeap(),
577 HeapCompatibilityInformation,
578 &info, sizeof(info) + 1, NULL);
579 ok(ret, "HeapQueryInformation error %u\n", GetLastError());
580 ok(info == 0 || info == 1 || info == 2, "expected 0, 1 or 2, got %u\n", info);
583 static void test_heap_checks( DWORD flags )
585 BYTE old, *p, *p2;
586 BOOL ret;
587 SIZE_T i, size, large_size = 3000 * 1024 + 37;
589 if (flags & HEAP_PAGE_ALLOCS) return; /* no tests for that case yet */
590 trace( "testing heap flags %08x\n", flags );
592 p = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, 17 );
593 ok( p != NULL, "HeapAlloc failed\n" );
595 ret = HeapValidate( GetProcessHeap(), 0, p );
596 ok( ret, "HeapValidate failed\n" );
598 size = HeapSize( GetProcessHeap(), 0, p );
599 ok( size == 17, "Wrong size %lu\n", size );
601 ok( p[14] == 0, "wrong data %x\n", p[14] );
602 ok( p[15] == 0, "wrong data %x\n", p[15] );
603 ok( p[16] == 0, "wrong data %x\n", p[16] );
605 if (flags & HEAP_TAIL_CHECKING_ENABLED)
607 ok( p[17] == 0xab, "wrong padding %x\n", p[17] );
608 ok( p[18] == 0xab, "wrong padding %x\n", p[18] );
609 ok( p[19] == 0xab, "wrong padding %x\n", p[19] );
612 p2 = HeapReAlloc( GetProcessHeap(), HEAP_REALLOC_IN_PLACE_ONLY, p, 14 );
613 if (p2 == p)
615 if (flags & HEAP_TAIL_CHECKING_ENABLED)
617 ok( p[14] == 0xab, "wrong padding %x\n", p[14] );
618 ok( p[15] == 0xab, "wrong padding %x\n", p[15] );
619 ok( p[16] == 0xab, "wrong padding %x\n", p[16] );
621 else
623 ok( p[14] == 0, "wrong padding %x\n", p[14] );
624 ok( p[15] == 0, "wrong padding %x\n", p[15] );
627 else skip( "realloc in place failed\n");
629 ret = HeapFree( GetProcessHeap(), 0, p );
630 ok( ret, "HeapFree failed\n" );
632 p = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, 17 );
633 ok( p != NULL, "HeapAlloc failed\n" );
634 old = p[17];
635 p[17] = 0xcc;
637 if (flags & HEAP_TAIL_CHECKING_ENABLED)
639 ret = HeapValidate( GetProcessHeap(), 0, p );
640 ok( !ret, "HeapValidate succeeded\n" );
642 /* other calls only check when HEAP_VALIDATE is set */
643 if (flags & HEAP_VALIDATE)
645 size = HeapSize( GetProcessHeap(), 0, p );
646 ok( size == ~(SIZE_T)0 || broken(size == ~0u), "Wrong size %lu\n", size );
648 p2 = HeapReAlloc( GetProcessHeap(), 0, p, 14 );
649 ok( p2 == NULL, "HeapReAlloc succeeded\n" );
651 ret = HeapFree( GetProcessHeap(), 0, p );
652 ok( !ret || broken(sizeof(void*) == 8), /* not caught on xp64 */
653 "HeapFree succeeded\n" );
656 p[17] = old;
657 size = HeapSize( GetProcessHeap(), 0, p );
658 ok( size == 17, "Wrong size %lu\n", size );
660 p2 = HeapReAlloc( GetProcessHeap(), 0, p, 14 );
661 ok( p2 != NULL, "HeapReAlloc failed\n" );
662 p = p2;
665 ret = HeapFree( GetProcessHeap(), 0, p );
666 ok( ret, "HeapFree failed\n" );
668 p = HeapAlloc( GetProcessHeap(), 0, 37 );
669 ok( p != NULL, "HeapAlloc failed\n" );
670 memset( p, 0xcc, 37 );
672 ret = HeapFree( GetProcessHeap(), 0, p );
673 ok( ret, "HeapFree failed\n" );
675 if (flags & HEAP_FREE_CHECKING_ENABLED)
677 ok( p[16] == 0xee, "wrong data %x\n", p[16] );
678 ok( p[17] == 0xfe, "wrong data %x\n", p[17] );
679 ok( p[18] == 0xee, "wrong data %x\n", p[18] );
680 ok( p[19] == 0xfe, "wrong data %x\n", p[19] );
682 ret = HeapValidate( GetProcessHeap(), 0, NULL );
683 ok( ret, "HeapValidate failed\n" );
685 old = p[16];
686 p[16] = 0xcc;
687 ret = HeapValidate( GetProcessHeap(), 0, NULL );
688 ok( !ret, "HeapValidate succeeded\n" );
690 p[16] = old;
691 ret = HeapValidate( GetProcessHeap(), 0, NULL );
692 ok( ret, "HeapValidate failed\n" );
695 /* now test large blocks */
697 p = HeapAlloc( GetProcessHeap(), 0, large_size );
698 ok( p != NULL, "HeapAlloc failed\n" );
700 ret = HeapValidate( GetProcessHeap(), 0, p );
701 ok( ret, "HeapValidate failed\n" );
703 size = HeapSize( GetProcessHeap(), 0, p );
704 ok( size == large_size, "Wrong size %lu\n", size );
706 ok( p[large_size - 2] == 0, "wrong data %x\n", p[large_size - 2] );
707 ok( p[large_size - 1] == 0, "wrong data %x\n", p[large_size - 1] );
709 if (flags & HEAP_TAIL_CHECKING_ENABLED)
711 /* Windows doesn't do tail checking on large blocks */
712 ok( p[large_size] == 0xab || broken(p[large_size] == 0), "wrong data %x\n", p[large_size] );
713 ok( p[large_size+1] == 0xab || broken(p[large_size+1] == 0), "wrong data %x\n", p[large_size+1] );
714 ok( p[large_size+2] == 0xab || broken(p[large_size+2] == 0), "wrong data %x\n", p[large_size+2] );
715 if (p[large_size] == 0xab)
717 p[large_size] = 0xcc;
718 ret = HeapValidate( GetProcessHeap(), 0, p );
719 ok( !ret, "HeapValidate succeeded\n" );
721 /* other calls only check when HEAP_VALIDATE is set */
722 if (flags & HEAP_VALIDATE)
724 size = HeapSize( GetProcessHeap(), 0, p );
725 ok( size == ~(SIZE_T)0, "Wrong size %lu\n", size );
727 p2 = HeapReAlloc( GetProcessHeap(), 0, p, large_size - 3 );
728 ok( p2 == NULL, "HeapReAlloc succeeded\n" );
730 ret = HeapFree( GetProcessHeap(), 0, p );
731 ok( !ret, "HeapFree succeeded\n" );
733 p[large_size] = 0xab;
737 ret = HeapFree( GetProcessHeap(), 0, p );
738 ok( ret, "HeapFree failed\n" );
740 /* test block sizes when tail checking */
741 if (flags & HEAP_TAIL_CHECKING_ENABLED)
743 for (size = 0; size < 64; size++)
745 p = HeapAlloc( GetProcessHeap(), 0, size );
746 for (i = 0; i < 32; i++) if (p[size + i] != 0xab) break;
747 ok( i >= 8, "only %lu tail bytes for size %lu\n", i, size );
748 HeapFree( GetProcessHeap(), 0, p );
753 static void test_debug_heap( const char *argv0, DWORD flags )
755 char keyname[MAX_PATH];
756 char buffer[MAX_PATH];
757 PROCESS_INFORMATION info;
758 STARTUPINFOA startup;
759 BOOL ret;
760 DWORD err;
761 HKEY hkey;
762 const char *basename;
764 if ((basename = strrchr( argv0, '\\' ))) basename++;
765 else basename = argv0;
767 sprintf( keyname, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Image File Execution Options\\%s",
768 basename );
769 if (!strcmp( keyname + strlen(keyname) - 3, ".so" )) keyname[strlen(keyname) - 3] = 0;
771 err = RegCreateKeyA( HKEY_LOCAL_MACHINE, keyname, &hkey );
772 if (err == ERROR_ACCESS_DENIED)
774 skip("Not authorized to change the image file execution options\n");
775 return;
777 ok( !err, "failed to create '%s' error %u\n", keyname, err );
778 if (err) return;
780 if (flags == 0xdeadbeef) /* magic value for unsetting it */
781 RegDeleteValueA( hkey, "GlobalFlag" );
782 else
783 RegSetValueExA( hkey, "GlobalFlag", 0, REG_DWORD, (BYTE *)&flags, sizeof(flags) );
785 memset( &startup, 0, sizeof(startup) );
786 startup.cb = sizeof(startup);
788 sprintf( buffer, "%s heap.c 0x%x", argv0, flags );
789 ret = CreateProcessA( NULL, buffer, NULL, NULL, FALSE, 0, NULL, NULL, &startup, &info );
790 ok( ret, "failed to create child process error %u\n", GetLastError() );
791 if (ret)
793 winetest_wait_child_process( info.hProcess );
794 CloseHandle( info.hThread );
795 CloseHandle( info.hProcess );
797 RegDeleteValueA( hkey, "GlobalFlag" );
798 RegCloseKey( hkey );
799 RegDeleteKeyA( HKEY_LOCAL_MACHINE, keyname );
802 static DWORD heap_flags_from_global_flag( DWORD flag )
804 DWORD ret = 0;
806 if (flag & FLG_HEAP_ENABLE_TAIL_CHECK)
807 ret |= HEAP_TAIL_CHECKING_ENABLED;
808 if (flag & FLG_HEAP_ENABLE_FREE_CHECK)
809 ret |= HEAP_FREE_CHECKING_ENABLED;
810 if (flag & FLG_HEAP_VALIDATE_PARAMETERS)
811 ret |= HEAP_VALIDATE_PARAMS | HEAP_VALIDATE | HEAP_TAIL_CHECKING_ENABLED | HEAP_FREE_CHECKING_ENABLED;
812 if (flag & FLG_HEAP_VALIDATE_ALL)
813 ret |= HEAP_VALIDATE_ALL | HEAP_VALIDATE | HEAP_TAIL_CHECKING_ENABLED | HEAP_FREE_CHECKING_ENABLED;
814 if (flag & FLG_HEAP_DISABLE_COALESCING)
815 ret |= HEAP_DISABLE_COALESCE_ON_FREE;
816 if (flag & FLG_HEAP_PAGE_ALLOCS)
817 ret |= HEAP_PAGE_ALLOCS | HEAP_GROWABLE;
818 return ret;
821 static void test_child_heap( const char *arg )
823 struct heap_layout *heap = GetProcessHeap();
824 DWORD expected = strtoul( arg, 0, 16 );
825 DWORD expect_heap;
827 if (expected == 0xdeadbeef) /* expected value comes from Session Manager global flags */
829 HKEY hkey;
830 expected = 0;
831 if (!RegOpenKeyA( HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Control\\Session Manager", &hkey ))
833 char buffer[32];
834 DWORD type, size = sizeof(buffer);
836 if (!RegQueryValueExA( hkey, "GlobalFlag", 0, &type, (BYTE *)buffer, &size ))
838 if (type == REG_DWORD) expected = *(DWORD *)buffer;
839 else if (type == REG_SZ) expected = strtoul( buffer, 0, 16 );
841 RegCloseKey( hkey );
844 if (expected && !pRtlGetNtGlobalFlags()) /* not working on NT4 */
846 win_skip( "global flags not set\n" );
847 return;
850 ok( pRtlGetNtGlobalFlags() == expected,
851 "%s: got global flags %08x expected %08x\n", arg, pRtlGetNtGlobalFlags(), expected );
853 expect_heap = heap_flags_from_global_flag( expected );
855 if (!(heap->flags & HEAP_GROWABLE) || heap->pattern == 0xffeeffee) /* vista layout */
857 ok( heap->flags == 0, "%s: got heap flags %08x expected 0\n", arg, heap->flags );
859 else if (heap->pattern == 0xeeeeeeee && heap->flags == 0xeeeeeeee)
861 ok( expected & FLG_HEAP_PAGE_ALLOCS, "%s: got heap flags 0xeeeeeeee without page alloc\n", arg );
863 else
865 ok( heap->flags == (expect_heap | HEAP_GROWABLE),
866 "%s: got heap flags %08x expected %08x\n", arg, heap->flags, expect_heap );
867 ok( heap->force_flags == (expect_heap & ~0x18000080),
868 "%s: got heap force flags %08x expected %08x\n", arg, heap->force_flags, expect_heap );
869 expect_heap = heap->flags;
872 test_heap_checks( expect_heap );
875 START_TEST(heap)
877 int argc;
878 char **argv;
880 pRtlGetNtGlobalFlags = (void *)GetProcAddress( GetModuleHandleA("ntdll.dll"), "RtlGetNtGlobalFlags" );
882 argc = winetest_get_mainargs( &argv );
883 if (argc >= 3)
885 test_child_heap( argv[2] );
886 return;
889 test_heap();
890 test_obsolete_flags();
892 /* Test both short and very long blocks */
893 test_sized_HeapAlloc(1);
894 test_sized_HeapAlloc(1 << 20);
895 test_sized_HeapReAlloc(1, 100);
896 test_sized_HeapReAlloc(1, (1 << 20));
897 test_sized_HeapReAlloc((1 << 20), (2 << 20));
898 test_sized_HeapReAlloc((1 << 20), 1);
899 test_HeapQueryInformation();
901 if (pRtlGetNtGlobalFlags)
903 test_debug_heap( argv[0], 0 );
904 test_debug_heap( argv[0], FLG_HEAP_ENABLE_TAIL_CHECK );
905 test_debug_heap( argv[0], FLG_HEAP_ENABLE_FREE_CHECK );
906 test_debug_heap( argv[0], FLG_HEAP_VALIDATE_PARAMETERS );
907 test_debug_heap( argv[0], FLG_HEAP_VALIDATE_ALL );
908 test_debug_heap( argv[0], FLG_POOL_ENABLE_TAGGING );
909 test_debug_heap( argv[0], FLG_HEAP_ENABLE_TAGGING );
910 test_debug_heap( argv[0], FLG_HEAP_ENABLE_TAG_BY_DLL );
911 test_debug_heap( argv[0], FLG_HEAP_DISABLE_COALESCING );
912 test_debug_heap( argv[0], FLG_HEAP_PAGE_ALLOCS );
913 test_debug_heap( argv[0], 0xdeadbeef );
915 else win_skip( "RtlGetNtGlobalFlags not found, skipping heap debug tests\n" );