kernel32/tests: Fix test for win8 heap layout.
[wine/wine-gecko.git] / dlls / kernel32 / tests / heap.c
blobd8768da945f35a14632c7a61863f0a3cae70f006
1 /*
2 * Unit test suite for heap functions
4 * Copyright 2002 Geoffrey Hausheer
5 * Copyright 2003 Dimitrie O. Paun
6 * Copyright 2006 Detlef Riekenberg
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include <stdarg.h>
24 #include <stdlib.h>
25 #include <stdio.h>
27 #include "windef.h"
28 #include "winbase.h"
29 #include "winreg.h"
30 #include "winternl.h"
31 #include "wine/test.h"
33 #define MAGIC_DEAD 0xdeadbeef
35 /* some undocumented flags (names are made up) */
36 #define HEAP_PAGE_ALLOCS 0x01000000
37 #define HEAP_VALIDATE 0x10000000
38 #define HEAP_VALIDATE_ALL 0x20000000
39 #define HEAP_VALIDATE_PARAMS 0x40000000
41 static BOOL (WINAPI *pHeapQueryInformation)(HANDLE, HEAP_INFORMATION_CLASS, PVOID, SIZE_T, PSIZE_T);
42 static ULONG (WINAPI *pRtlGetNtGlobalFlags)(void);
44 struct heap_layout
46 DWORD_PTR unknown[2];
47 DWORD pattern;
48 DWORD flags;
49 DWORD force_flags;
52 static SIZE_T resize_9x(SIZE_T size)
54 DWORD dwSizeAligned = (size + 3) & ~3;
55 return max(dwSizeAligned, 12); /* at least 12 bytes */
58 static void test_sized_HeapAlloc(int nbytes)
60 BOOL success;
61 char *buf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, nbytes);
62 ok(buf != NULL, "allocate failed\n");
63 ok(buf[0] == 0, "buffer not zeroed\n");
64 success = HeapFree(GetProcessHeap(), 0, buf);
65 ok(success, "free failed\n");
68 static void test_sized_HeapReAlloc(int nbytes1, int nbytes2)
70 BOOL success;
71 char *buf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, nbytes1);
72 ok(buf != NULL, "allocate failed\n");
73 ok(buf[0] == 0, "buffer not zeroed\n");
74 buf = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, buf, nbytes2);
75 ok(buf != NULL, "reallocate failed\n");
76 ok(buf[nbytes2-1] == 0, "buffer not zeroed\n");
77 success = HeapFree(GetProcessHeap(), 0, buf);
78 ok(success, "free failed\n");
81 static void test_heap(void)
83 LPVOID mem;
84 LPVOID msecond;
85 DWORD res;
86 UINT flags;
87 HGLOBAL gbl;
88 HGLOBAL hsecond;
89 SIZE_T size, size2;
90 const SIZE_T max_size = 1024, init_size = 10;
92 /* Heap*() functions */
93 mem = HeapAlloc(GetProcessHeap(), 0, 0);
94 ok(mem != NULL, "memory not allocated for size 0\n");
95 HeapFree(GetProcessHeap(), 0, mem);
97 mem = HeapReAlloc(GetProcessHeap(), 0, NULL, 10);
98 ok(mem == NULL, "memory allocated by HeapReAlloc\n");
100 for (size = 0; size <= 256; size++)
102 SIZE_T heap_size;
103 mem = HeapAlloc(GetProcessHeap(), 0, size);
104 heap_size = HeapSize(GetProcessHeap(), 0, mem);
105 ok(heap_size == size || heap_size == resize_9x(size),
106 "HeapSize returned %lu instead of %lu or %lu\n", heap_size, size, resize_9x(size));
107 HeapFree(GetProcessHeap(), 0, mem);
110 /* test some border cases of HeapAlloc and HeapReAlloc */
111 mem = HeapAlloc(GetProcessHeap(), 0, 0);
112 ok(mem != NULL, "memory not allocated for size 0\n");
113 msecond = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, mem, ~(SIZE_T)0 - 7);
114 ok(msecond == NULL, "HeapReAlloc(~0 - 7) should have failed\n");
115 msecond = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, mem, ~(SIZE_T)0);
116 ok(msecond == NULL, "HeapReAlloc(~0) should have failed\n");
117 HeapFree(GetProcessHeap(), 0, mem);
118 mem = HeapAlloc(GetProcessHeap(), 0, ~(SIZE_T)0);
119 ok(mem == NULL, "memory allocated for size ~0\n");
121 /* large blocks must be 16-byte aligned */
122 mem = HeapAlloc(GetProcessHeap(), 0, 512 * 1024);
123 ok( mem != NULL, "failed for size 512K\n" );
124 ok( (ULONG_PTR)mem % 16 == 0 || broken((ULONG_PTR)mem % 16) /* win9x */,
125 "512K block not 16-byte aligned\n" );
126 HeapFree(GetProcessHeap(), 0, mem);
128 /* Global*() functions */
129 gbl = GlobalAlloc(GMEM_MOVEABLE, 0);
130 ok(gbl != NULL, "global memory not allocated for size 0\n");
132 gbl = GlobalReAlloc(gbl, 10, GMEM_MOVEABLE);
133 ok(gbl != NULL, "Can't realloc global memory\n");
134 size = GlobalSize(gbl);
135 ok(size >= 10 && size <= 16, "Memory not resized to size 10, instead size=%ld\n", size);
137 gbl = GlobalReAlloc(gbl, 0, GMEM_MOVEABLE);
138 ok(gbl != NULL, "GlobalReAlloc should not fail on size 0\n");
140 size = GlobalSize(gbl);
141 ok(size == 0, "Memory not resized to size 0, instead size=%ld\n", size);
142 ok(GlobalFree(gbl) == NULL, "Memory not freed\n");
143 size = GlobalSize(gbl);
144 ok(size == 0, "Memory should have been freed, size=%ld\n", size);
146 gbl = GlobalReAlloc(0, 10, GMEM_MOVEABLE);
147 ok(gbl == NULL, "global realloc allocated memory\n");
149 /* GlobalLock / GlobalUnlock with a valid handle */
150 gbl = GlobalAlloc(GMEM_MOVEABLE, 256);
152 SetLastError(MAGIC_DEAD);
153 mem = GlobalLock(gbl); /* #1 */
154 ok(mem != NULL, "returned %p with %d (expected '!= NULL')\n", mem, GetLastError());
155 SetLastError(MAGIC_DEAD);
156 flags = GlobalFlags(gbl);
157 ok( flags == 1, "returned 0x%04x with %d (expected '0x0001')\n",
158 flags, GetLastError());
160 SetLastError(MAGIC_DEAD);
161 msecond = GlobalLock(gbl); /* #2 */
162 ok( msecond == mem, "returned %p with %d (expected '%p')\n",
163 msecond, GetLastError(), mem);
164 SetLastError(MAGIC_DEAD);
165 flags = GlobalFlags(gbl);
166 ok( flags == 2, "returned 0x%04x with %d (expected '0x0002')\n",
167 flags, GetLastError());
168 SetLastError(MAGIC_DEAD);
170 SetLastError(MAGIC_DEAD);
171 res = GlobalUnlock(gbl); /* #1 */
172 ok(res, "returned %d with %d (expected '!= 0')\n", res, GetLastError());
173 SetLastError(MAGIC_DEAD);
174 flags = GlobalFlags(gbl);
175 ok( flags , "returned 0x%04x with %d (expected '!= 0')\n",
176 flags, GetLastError());
178 SetLastError(MAGIC_DEAD);
179 res = GlobalUnlock(gbl); /* #0 */
180 /* NT: ERROR_SUCCESS (documented on MSDN), 9x: untouched */
181 ok(!res && ((GetLastError() == ERROR_SUCCESS) || (GetLastError() == MAGIC_DEAD)),
182 "returned %d with %d (expected '0' with: ERROR_SUCCESS or "
183 "MAGIC_DEAD)\n", res, GetLastError());
184 SetLastError(MAGIC_DEAD);
185 flags = GlobalFlags(gbl);
186 ok( !flags , "returned 0x%04x with %d (expected '0')\n",
187 flags, GetLastError());
189 /* Unlock an already unlocked Handle */
190 SetLastError(MAGIC_DEAD);
191 res = GlobalUnlock(gbl);
192 /* NT: ERROR_NOT_LOCKED, 9x: untouched */
193 ok( !res &&
194 ((GetLastError() == ERROR_NOT_LOCKED) || (GetLastError() == MAGIC_DEAD)),
195 "returned %d with %d (expected '0' with: ERROR_NOT_LOCKED or "
196 "MAGIC_DEAD)\n", res, GetLastError());
198 GlobalFree(gbl);
199 /* invalid handles are caught in windows: */
200 SetLastError(MAGIC_DEAD);
201 hsecond = GlobalFree(gbl); /* invalid handle: free memory twice */
202 ok( (hsecond == gbl) && (GetLastError() == ERROR_INVALID_HANDLE),
203 "returned %p with 0x%08x (expected %p with ERROR_INVALID_HANDLE)\n",
204 hsecond, GetLastError(), gbl);
205 SetLastError(MAGIC_DEAD);
206 flags = GlobalFlags(gbl);
207 ok( (flags == GMEM_INVALID_HANDLE) && (GetLastError() == ERROR_INVALID_HANDLE),
208 "returned 0x%04x with 0x%08x (expected GMEM_INVALID_HANDLE with "
209 "ERROR_INVALID_HANDLE)\n", flags, GetLastError());
210 SetLastError(MAGIC_DEAD);
211 size = GlobalSize(gbl);
212 ok( (size == 0) && (GetLastError() == ERROR_INVALID_HANDLE),
213 "returned %ld with 0x%08x (expected '0' with ERROR_INVALID_HANDLE)\n",
214 size, GetLastError());
216 SetLastError(MAGIC_DEAD);
217 mem = GlobalLock(gbl);
218 ok( (mem == NULL) && (GetLastError() == ERROR_INVALID_HANDLE),
219 "returned %p with 0x%08x (expected NULL with ERROR_INVALID_HANDLE)\n",
220 mem, GetLastError());
222 /* documented on MSDN: GlobalUnlock() return FALSE on failure.
223 Win9x and wine return FALSE with ERROR_INVALID_HANDLE, but on
224 NT 3.51 and XPsp2, TRUE with ERROR_INVALID_HANDLE is returned.
225 The similar Test for LocalUnlock() works on all Systems */
226 SetLastError(MAGIC_DEAD);
227 res = GlobalUnlock(gbl);
228 ok(GetLastError() == ERROR_INVALID_HANDLE,
229 "returned %d with %d (expected ERROR_INVALID_HANDLE)\n",
230 res, GetLastError());
232 gbl = GlobalAlloc(GMEM_DDESHARE, 100);
234 /* first free */
235 mem = GlobalFree(gbl);
236 ok(mem == NULL, "Expected NULL, got %p\n", mem);
238 /* invalid free */
239 if (sizeof(void *) != 8) /* crashes on 64-bit Vista */
241 SetLastError(MAGIC_DEAD);
242 mem = GlobalFree(gbl);
243 ok(mem == gbl || broken(mem == NULL) /* nt4 */, "Expected gbl, got %p\n", mem);
244 if (mem == gbl)
245 ok(GetLastError() == ERROR_INVALID_HANDLE ||
246 GetLastError() == ERROR_INVALID_PARAMETER, /* win9x */
247 "Expected ERROR_INVALID_HANDLE or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
250 /* GMEM_FIXED block expands in place only without flags */
251 for (size = 1; size <= max_size; size <<= 1) {
252 gbl = GlobalAlloc(GMEM_FIXED, init_size);
253 SetLastError(MAGIC_DEAD);
254 hsecond = GlobalReAlloc(gbl, size + init_size, 0);
255 ok(hsecond == gbl || (hsecond == NULL && GetLastError() == ERROR_NOT_ENOUGH_MEMORY),
256 "got %p with %x (expected %p or NULL) @%ld\n", hsecond, GetLastError(), gbl, size);
257 GlobalFree(gbl);
260 /* GMEM_FIXED block can be relocated with GMEM_MOVEABLE */
261 for (size = 1; size <= max_size; size <<= 1) {
262 gbl = GlobalAlloc(GMEM_FIXED, init_size);
263 SetLastError(MAGIC_DEAD);
264 hsecond = GlobalReAlloc(gbl, size + init_size, GMEM_MOVEABLE);
265 ok(hsecond != NULL,
266 "got %p with %x (expected non-NULL) @%ld\n", hsecond, GetLastError(), size);
267 mem = GlobalLock(hsecond);
268 ok(mem == hsecond, "got %p (expected %p) @%ld\n", mem, hsecond, size);
269 GlobalFree(hsecond);
272 gbl = GlobalAlloc(GMEM_DDESHARE, 100);
274 res = GlobalUnlock(gbl);
275 ok(res == 1 ||
276 broken(res == 0), /* win9x */
277 "Expected 1 or 0, got %d\n", res);
279 res = GlobalUnlock(gbl);
280 ok(res == 1 ||
281 broken(res == 0), /* win9x */
282 "Expected 1 or 0, got %d\n", res);
284 GlobalFree(gbl);
286 gbl = GlobalAlloc(GMEM_FIXED, 100);
288 SetLastError(0xdeadbeef);
289 res = GlobalUnlock(gbl);
290 ok(res == 1 ||
291 broken(res == 0), /* win9x */
292 "Expected 1 or 0, got %d\n", res);
293 ok(GetLastError() == 0xdeadbeef, "got %d\n", GetLastError());
295 GlobalFree(gbl);
297 /* GlobalSize on an invalid handle */
298 if (sizeof(void *) != 8) /* crashes on 64-bit Vista */
300 SetLastError(MAGIC_DEAD);
301 size = GlobalSize((HGLOBAL)0xc042);
302 ok(size == 0, "Expected 0, got %ld\n", size);
303 ok(GetLastError() == ERROR_INVALID_HANDLE ||
304 GetLastError() == ERROR_INVALID_PARAMETER, /* win9x */
305 "Expected ERROR_INVALID_HANDLE or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
308 /* ####################################### */
309 /* Local*() functions */
310 gbl = LocalAlloc(LMEM_MOVEABLE, 0);
311 ok(gbl != NULL, "local memory not allocated for size 0\n");
313 gbl = LocalReAlloc(gbl, 10, LMEM_MOVEABLE);
314 ok(gbl != NULL, "Can't realloc local memory\n");
315 size = LocalSize(gbl);
316 ok(size >= 10 && size <= 16, "Memory not resized to size 10, instead size=%ld\n", size);
318 gbl = LocalReAlloc(gbl, 0, LMEM_MOVEABLE);
319 ok(gbl != NULL, "LocalReAlloc should not fail on size 0\n");
321 size = LocalSize(gbl);
322 ok(size == 0, "Memory not resized to size 0, instead size=%ld\n", size);
323 ok(LocalFree(gbl) == NULL, "Memory not freed\n");
324 size = LocalSize(gbl);
325 ok(size == 0, "Memory should have been freed, size=%ld\n", size);
327 gbl = LocalReAlloc(0, 10, LMEM_MOVEABLE);
328 ok(gbl == NULL, "local realloc allocated memory\n");
330 /* LocalLock / LocalUnlock with a valid handle */
331 gbl = LocalAlloc(LMEM_MOVEABLE, 256);
332 SetLastError(MAGIC_DEAD);
333 mem = LocalLock(gbl); /* #1 */
334 ok(mem != NULL, "returned %p with %d (expected '!= NULL')\n", mem, GetLastError());
335 SetLastError(MAGIC_DEAD);
336 flags = LocalFlags(gbl);
337 ok( flags == 1, "returned 0x%04x with %d (expected '0x0001')\n",
338 flags, GetLastError());
340 SetLastError(MAGIC_DEAD);
341 msecond = LocalLock(gbl); /* #2 */
342 ok( msecond == mem, "returned %p with %d (expected '%p')\n",
343 msecond, GetLastError(), mem);
344 SetLastError(MAGIC_DEAD);
345 flags = LocalFlags(gbl);
346 ok( flags == 2, "returned 0x%04x with %d (expected '0x0002')\n",
347 flags, GetLastError());
348 SetLastError(MAGIC_DEAD);
350 SetLastError(MAGIC_DEAD);
351 res = LocalUnlock(gbl); /* #1 */
352 ok(res, "returned %d with %d (expected '!= 0')\n", res, GetLastError());
353 SetLastError(MAGIC_DEAD);
354 flags = LocalFlags(gbl);
355 ok( flags , "returned 0x%04x with %d (expected '!= 0')\n",
356 flags, GetLastError());
358 SetLastError(MAGIC_DEAD);
359 res = LocalUnlock(gbl); /* #0 */
360 /* NT: ERROR_SUCCESS (documented on MSDN), 9x: untouched */
361 ok(!res && ((GetLastError() == ERROR_SUCCESS) || (GetLastError() == MAGIC_DEAD)),
362 "returned %d with %d (expected '0' with: ERROR_SUCCESS or "
363 "MAGIC_DEAD)\n", res, GetLastError());
364 SetLastError(MAGIC_DEAD);
365 flags = LocalFlags(gbl);
366 ok( !flags , "returned 0x%04x with %d (expected '0')\n",
367 flags, GetLastError());
369 /* Unlock an already unlocked Handle */
370 SetLastError(MAGIC_DEAD);
371 res = LocalUnlock(gbl);
372 /* NT: ERROR_NOT_LOCKED, 9x: untouched */
373 ok( !res &&
374 ((GetLastError() == ERROR_NOT_LOCKED) || (GetLastError() == MAGIC_DEAD)),
375 "returned %d with %d (expected '0' with: ERROR_NOT_LOCKED or "
376 "MAGIC_DEAD)\n", res, GetLastError());
378 LocalFree(gbl);
379 /* invalid handles are caught in windows: */
380 SetLastError(MAGIC_DEAD);
381 hsecond = LocalFree(gbl); /* invalid handle: free memory twice */
382 ok( (hsecond == gbl) && (GetLastError() == ERROR_INVALID_HANDLE),
383 "returned %p with 0x%08x (expected %p with ERROR_INVALID_HANDLE)\n",
384 hsecond, GetLastError(), gbl);
385 SetLastError(MAGIC_DEAD);
386 flags = LocalFlags(gbl);
387 ok( (flags == LMEM_INVALID_HANDLE) && (GetLastError() == ERROR_INVALID_HANDLE),
388 "returned 0x%04x with 0x%08x (expected LMEM_INVALID_HANDLE with "
389 "ERROR_INVALID_HANDLE)\n", flags, GetLastError());
390 SetLastError(MAGIC_DEAD);
391 size = LocalSize(gbl);
392 ok( (size == 0) && (GetLastError() == ERROR_INVALID_HANDLE),
393 "returned %ld with 0x%08x (expected '0' with ERROR_INVALID_HANDLE)\n",
394 size, GetLastError());
396 SetLastError(MAGIC_DEAD);
397 mem = LocalLock(gbl);
398 ok( (mem == NULL) && (GetLastError() == ERROR_INVALID_HANDLE),
399 "returned %p with 0x%08x (expected NULL with ERROR_INVALID_HANDLE)\n",
400 mem, GetLastError());
402 /* This Test works the same on all Systems (GlobalUnlock() is different) */
403 SetLastError(MAGIC_DEAD);
404 res = LocalUnlock(gbl);
405 ok(!res && (GetLastError() == ERROR_INVALID_HANDLE),
406 "returned %d with %d (expected '0' with ERROR_INVALID_HANDLE)\n",
407 res, GetLastError());
409 /* LMEM_FIXED block expands in place only without flags */
410 for (size = 1; size <= max_size; size <<= 1) {
411 gbl = LocalAlloc(LMEM_FIXED, init_size);
412 SetLastError(MAGIC_DEAD);
413 hsecond = LocalReAlloc(gbl, size + init_size, 0);
414 ok(hsecond == gbl || (hsecond == NULL && GetLastError() == ERROR_NOT_ENOUGH_MEMORY),
415 "got %p with %x (expected %p or NULL) @%ld\n", hsecond, GetLastError(), gbl, size);
416 LocalFree(gbl);
419 /* LMEM_FIXED memory can be relocated with LMEM_MOVEABLE */
420 for (size = 1; size <= max_size; size <<= 1) {
421 gbl = LocalAlloc(LMEM_FIXED, init_size);
422 SetLastError(MAGIC_DEAD);
423 hsecond = LocalReAlloc(gbl, size + init_size, LMEM_MOVEABLE);
424 ok(hsecond != NULL,
425 "got %p with %x (expected non-NULL) @%ld\n", hsecond, GetLastError(), size);
426 mem = LocalLock(hsecond);
427 ok(mem == hsecond, "got %p (expected %p) @%ld\n", mem, hsecond, size);
428 LocalFree(hsecond);
431 /* trying to unlock pointer from LocalAlloc */
432 gbl = LocalAlloc(LMEM_FIXED, 100);
433 SetLastError(0xdeadbeef);
434 res = LocalUnlock(gbl);
435 ok(res == 0, "Expected 0, got %d\n", res);
436 ok(GetLastError() == ERROR_NOT_LOCKED ||
437 broken(GetLastError() == 0xdeadbeef) /* win9x */, "got %d\n", GetLastError());
438 LocalFree(gbl);
440 /* trying to lock empty memory should give an error */
441 gbl = GlobalAlloc(GMEM_MOVEABLE|GMEM_ZEROINIT,0);
442 ok(gbl != NULL, "returned NULL\n");
443 SetLastError(MAGIC_DEAD);
444 mem = GlobalLock(gbl);
445 /* NT: ERROR_DISCARDED, 9x: untouched */
446 ok( (mem == NULL) &&
447 ((GetLastError() == ERROR_DISCARDED) || (GetLastError() == MAGIC_DEAD)),
448 "returned %p with 0x%x/%d (expected 'NULL' with: ERROR_DISCARDED or "
449 "MAGIC_DEAD)\n", mem, GetLastError(), GetLastError());
451 GlobalFree(gbl);
453 /* trying to get size from data pointer (GMEM_MOVEABLE) */
454 gbl = GlobalAlloc(GMEM_MOVEABLE, 0x123);
455 ok(gbl != NULL, "returned NULL\n");
456 mem = GlobalLock(gbl);
457 ok(mem != NULL, "returned NULL.\n");
458 ok(gbl != mem, "unexpectedly equal.\n");
460 size = GlobalSize(gbl);
461 size2 = GlobalSize(mem);
462 ok(size == 0x123, "got %lu\n", size);
463 ok(size2 == 0x123, "got %lu\n", size2);
465 GlobalFree(gbl);
467 /* trying to get size from data pointer (GMEM_FIXED) */
468 gbl = GlobalAlloc(GMEM_FIXED, 0x123);
469 ok(gbl != NULL, "returned NULL\n");
470 mem = GlobalLock(gbl);
471 ok(mem != NULL, "returned NULL.\n");
472 ok(gbl == mem, "got %p, %p.\n", gbl, mem);
474 size = GlobalSize(gbl);
475 ok(size == 0x123, "got %lu\n", size);
477 GlobalFree(gbl);
479 size = GlobalSize((void *)0xdeadbee0);
480 ok(size == 0, "got %lu\n", size);
485 static void test_HeapCreate(void)
487 SYSTEM_INFO sysInfo;
488 ULONG memchunk;
489 HANDLE heap;
490 LPVOID mem1,mem1a,mem3;
491 UCHAR *mem2,*mem2a;
492 UINT i;
493 BOOL error;
494 DWORD dwSize;
496 /* Retrieve the page size for this system */
497 GetSystemInfo(&sysInfo);
498 ok(sysInfo.dwPageSize>0,"GetSystemInfo should return a valid page size\n");
500 /* Create a Heap with a minimum and maximum size */
501 /* Note that Windows and Wine seem to behave a bit differently with respect
502 to memory allocation. In Windows, you can't access all the memory
503 specified in the heap (due to overhead), so choosing a reasonable maximum
504 size for the heap was done mostly by trial-and-error on Win2k. It may need
505 more tweaking for otherWindows variants.
507 memchunk=10*sysInfo.dwPageSize;
508 heap=HeapCreate(0,2*memchunk,5*memchunk);
509 ok( !((ULONG_PTR)heap & 0xffff), "heap %p not 64K aligned\n", heap );
511 /* Check that HeapCreate allocated the right amount of ram */
512 mem1=HeapAlloc(heap,0,5*memchunk+1);
513 ok(mem1==NULL,"HeapCreate allocated more Ram than it should have\n");
514 HeapFree(heap,0,mem1);
516 /* Check that a normal alloc works */
517 mem1=HeapAlloc(heap,0,memchunk);
518 ok(mem1!=NULL,"HeapAlloc failed\n");
519 if(mem1) {
520 ok(HeapSize(heap,0,mem1)>=memchunk, "HeapAlloc should return a big enough memory block\n");
523 /* Check that a 'zeroing' alloc works */
524 mem2=HeapAlloc(heap,HEAP_ZERO_MEMORY,memchunk);
525 ok(mem2!=NULL,"HeapAlloc failed\n");
526 if(mem2) {
527 ok(HeapSize(heap,0,mem2)>=memchunk,"HeapAlloc should return a big enough memory block\n");
528 error=FALSE;
529 for(i=0;i<memchunk;i++) {
530 if(mem2[i]!=0) {
531 error=TRUE;
534 ok(!error,"HeapAlloc should have zeroed out its allocated memory\n");
537 /* Check that HeapAlloc returns NULL when requested way too much memory */
538 mem3=HeapAlloc(heap,0,5*memchunk);
539 ok(mem3==NULL,"HeapAlloc should return NULL\n");
540 if(mem3) {
541 ok(HeapFree(heap,0,mem3),"HeapFree didn't pass successfully\n");
544 /* Check that HeapRealloc works */
545 mem2a=HeapReAlloc(heap,HEAP_ZERO_MEMORY,mem2,memchunk+5*sysInfo.dwPageSize);
546 ok(mem2a!=NULL,"HeapReAlloc failed\n");
547 if(mem2a) {
548 ok(HeapSize(heap,0,mem2a)>=memchunk+5*sysInfo.dwPageSize,"HeapReAlloc failed\n");
549 error=FALSE;
550 for(i=0;i<5*sysInfo.dwPageSize;i++) {
551 if(mem2a[memchunk+i]!=0) {
552 error=TRUE;
555 ok(!error,"HeapReAlloc should have zeroed out its allocated memory\n");
558 /* Check that HeapRealloc honours HEAP_REALLOC_IN_PLACE_ONLY */
559 error=FALSE;
560 mem1a=HeapReAlloc(heap,HEAP_REALLOC_IN_PLACE_ONLY,mem1,memchunk+sysInfo.dwPageSize);
561 if(mem1a!=NULL) {
562 if(mem1a!=mem1) {
563 error=TRUE;
566 ok(mem1a==NULL || !error,"HeapReAlloc didn't honour HEAP_REALLOC_IN_PLACE_ONLY\n");
568 /* Check that HeapFree works correctly */
569 if(mem1a) {
570 ok(HeapFree(heap,0,mem1a),"HeapFree failed\n");
571 } else {
572 ok(HeapFree(heap,0,mem1),"HeapFree failed\n");
574 if(mem2a) {
575 ok(HeapFree(heap,0,mem2a),"HeapFree failed\n");
576 } else {
577 ok(HeapFree(heap,0,mem2),"HeapFree failed\n");
580 /* 0-length buffer */
581 mem1 = HeapAlloc(heap, 0, 0);
582 ok(mem1 != NULL, "Reserved memory\n");
584 dwSize = HeapSize(heap, 0, mem1);
585 /* should work with 0-length buffer */
586 ok(dwSize < 0xFFFFFFFF, "The size of the 0-length buffer\n");
587 ok(HeapFree(heap, 0, mem1), "Freed the 0-length buffer\n");
589 /* Check that HeapDestroy works */
590 ok(HeapDestroy(heap),"HeapDestroy failed\n");
594 static void test_GlobalAlloc(void)
596 ULONG memchunk;
597 HGLOBAL mem1,mem2,mem2a,mem2b;
598 UCHAR *mem2ptr;
599 UINT i;
600 BOOL error;
601 memchunk=100000;
603 SetLastError(NO_ERROR);
604 /* Check that a normal alloc works */
605 mem1=GlobalAlloc(0,memchunk);
606 ok(mem1!=NULL,"GlobalAlloc failed\n");
607 if(mem1) {
608 ok(GlobalSize(mem1)>=memchunk, "GlobalAlloc should return a big enough memory block\n");
611 /* Check that a 'zeroing' alloc works */
612 mem2=GlobalAlloc(GMEM_ZEROINIT,memchunk);
613 ok(mem2!=NULL,"GlobalAlloc failed: error=%d\n",GetLastError());
614 if(mem2) {
615 ok(GlobalSize(mem2)>=memchunk,"GlobalAlloc should return a big enough memory block\n");
616 mem2ptr=GlobalLock(mem2);
617 ok(mem2ptr==mem2,"GlobalLock should have returned the same memory as was allocated\n");
618 if(mem2ptr) {
619 error=FALSE;
620 for(i=0;i<memchunk;i++) {
621 if(mem2ptr[i]!=0) {
622 error=TRUE;
625 ok(!error,"GlobalAlloc should have zeroed out its allocated memory\n");
628 /* Check that GlobalReAlloc works */
629 /* Check that we can change GMEM_FIXED to GMEM_MOVEABLE */
630 mem2a=GlobalReAlloc(mem2,0,GMEM_MODIFY | GMEM_MOVEABLE);
631 if(mem2a!=NULL) {
632 mem2=mem2a;
633 mem2ptr=GlobalLock(mem2a);
634 ok(mem2ptr!=NULL && !GlobalUnlock(mem2a)&&GetLastError()==NO_ERROR,
635 "Converting from FIXED to MOVEABLE didn't REALLY work\n");
638 /* Check that ReAllocing memory works as expected */
639 mem2a=GlobalReAlloc(mem2,2*memchunk,GMEM_MOVEABLE | GMEM_ZEROINIT);
640 ok(mem2a!=NULL,"GlobalReAlloc failed\n");
641 if(mem2a) {
642 ok(GlobalSize(mem2a)>=2*memchunk,"GlobalReAlloc failed\n");
643 mem2ptr=GlobalLock(mem2a);
644 ok(mem2ptr!=NULL,"GlobalLock Failed\n");
645 if(mem2ptr) {
646 error=FALSE;
647 for(i=0;i<memchunk;i++) {
648 if(mem2ptr[memchunk+i]!=0) {
649 error=TRUE;
652 ok(!error,"GlobalReAlloc should have zeroed out its allocated memory\n");
654 /* Check that GlobalHandle works */
655 mem2b=GlobalHandle(mem2ptr);
656 ok(mem2b==mem2a,"GlobalHandle didn't return the correct memory handle %p/%p for %p\n",
657 mem2a, mem2b, mem2ptr);
658 /* Check that we can't discard locked memory */
659 mem2b=GlobalDiscard(mem2a);
660 if(mem2b==NULL) {
661 ok(!GlobalUnlock(mem2a) && GetLastError()==NO_ERROR,"GlobalUnlock Failed\n");
665 if(mem1) {
666 ok(GlobalFree(mem1)==NULL,"GlobalFree failed\n");
668 if(mem2a) {
669 ok(GlobalFree(mem2a)==NULL,"GlobalFree failed\n");
670 } else {
671 ok(GlobalFree(mem2)==NULL,"GlobalFree failed\n");
676 static void test_LocalAlloc(void)
678 ULONG memchunk;
679 HLOCAL mem1,mem2,mem2a,mem2b;
680 UCHAR *mem2ptr;
681 UINT i;
682 BOOL error;
683 memchunk=100000;
685 /* Check that a normal alloc works */
686 mem1=LocalAlloc(0,memchunk);
687 ok(mem1!=NULL,"LocalAlloc failed: error=%d\n",GetLastError());
688 if(mem1) {
689 ok(LocalSize(mem1)>=memchunk, "LocalAlloc should return a big enough memory block\n");
692 /* Check that a 'zeroing' and lock alloc works */
693 mem2=LocalAlloc(LMEM_ZEROINIT|LMEM_MOVEABLE,memchunk);
694 ok(mem2!=NULL,"LocalAlloc failed: error=%d\n",GetLastError());
695 if(mem2) {
696 ok(LocalSize(mem2)>=memchunk,"LocalAlloc should return a big enough memory block\n");
697 mem2ptr=LocalLock(mem2);
698 ok(mem2ptr!=NULL,"LocalLock: error=%d\n",GetLastError());
699 if(mem2ptr) {
700 error=FALSE;
701 for(i=0;i<memchunk;i++) {
702 if(mem2ptr[i]!=0) {
703 error=TRUE;
706 ok(!error,"LocalAlloc should have zeroed out its allocated memory\n");
707 SetLastError(0);
708 error=LocalUnlock(mem2);
709 ok(!error && GetLastError()==NO_ERROR,
710 "LocalUnlock Failed: rc=%d err=%d\n",error,GetLastError());
713 mem2a=LocalFree(mem2);
714 ok(mem2a==NULL, "LocalFree failed: %p\n",mem2a);
716 /* Reallocate mem2 as moveable memory */
717 mem2=LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT,memchunk);
718 ok(mem2!=NULL, "LocalAlloc failed to create moveable memory, error=%d\n",GetLastError());
720 /* Check that ReAllocing memory works as expected */
721 mem2a=LocalReAlloc(mem2,2*memchunk,LMEM_MOVEABLE | LMEM_ZEROINIT);
722 ok(mem2a!=NULL,"LocalReAlloc failed, error=%d\n",GetLastError());
723 if(mem2a) {
724 ok(LocalSize(mem2a)>=2*memchunk,"LocalReAlloc failed\n");
725 mem2ptr=LocalLock(mem2a);
726 ok(mem2ptr!=NULL,"LocalLock Failed\n");
727 if(mem2ptr) {
728 error=FALSE;
729 for(i=0;i<memchunk;i++) {
730 if(mem2ptr[memchunk+i]!=0) {
731 error=TRUE;
734 ok(!error,"LocalReAlloc should have zeroed out its allocated memory\n");
735 /* Check that LocalHandle works */
736 mem2b=LocalHandle(mem2ptr);
737 ok(mem2b==mem2a,"LocalHandle didn't return the correct memory handle %p/%p for %p\n",
738 mem2a, mem2b, mem2ptr);
739 /* Check that we can't discard locked memory */
740 mem2b=LocalDiscard(mem2a);
741 ok(mem2b==NULL,"Discarded memory we shouldn't have\n");
742 SetLastError(NO_ERROR);
743 ok(!LocalUnlock(mem2a) && GetLastError()==NO_ERROR, "LocalUnlock Failed\n");
746 if(mem1) {
747 ok(LocalFree(mem1)==NULL,"LocalFree failed\n");
749 if(mem2a) {
750 ok(LocalFree(mem2a)==NULL,"LocalFree failed\n");
751 } else {
752 ok(LocalFree(mem2)==NULL,"LocalFree failed\n");
756 static void test_obsolete_flags(void)
758 static struct {
759 UINT flags;
760 UINT globalflags;
761 } test_global_flags[] = {
762 {GMEM_FIXED | GMEM_NOTIFY, 0},
763 {GMEM_FIXED | GMEM_DISCARDABLE, 0},
764 {GMEM_MOVEABLE | GMEM_NOTIFY, 0},
765 {GMEM_MOVEABLE | GMEM_DDESHARE, GMEM_DDESHARE},
766 {GMEM_MOVEABLE | GMEM_NOT_BANKED, 0},
767 {GMEM_MOVEABLE | GMEM_NODISCARD, 0},
768 {GMEM_MOVEABLE | GMEM_DISCARDABLE, GMEM_DISCARDABLE},
769 {GMEM_MOVEABLE | GMEM_DDESHARE | GMEM_DISCARDABLE | GMEM_LOWER | GMEM_NOCOMPACT | GMEM_NODISCARD |
770 GMEM_NOT_BANKED | GMEM_NOTIFY, GMEM_DDESHARE | GMEM_DISCARDABLE},
773 unsigned int i;
774 HGLOBAL gbl;
775 UINT resultflags;
777 UINT (WINAPI *pGlobalFlags)(HGLOBAL);
779 pGlobalFlags = (void *) GetProcAddress(GetModuleHandleA("kernel32"), "GlobalFlags");
781 if (!pGlobalFlags)
783 win_skip("GlobalFlags is not available\n");
784 return;
787 for (i = 0; i < sizeof(test_global_flags)/sizeof(test_global_flags[0]); i++)
789 gbl = GlobalAlloc(test_global_flags[i].flags, 4);
790 ok(gbl != NULL, "GlobalAlloc failed\n");
792 SetLastError(MAGIC_DEAD);
793 resultflags = pGlobalFlags(gbl);
795 ok( resultflags == test_global_flags[i].globalflags ||
796 broken(resultflags == (test_global_flags[i].globalflags & ~GMEM_DDESHARE)), /* win9x */
797 "%u: expected 0x%08x, but returned 0x%08x with %d\n",
798 i, test_global_flags[i].globalflags, resultflags, GetLastError() );
800 GlobalFree(gbl);
804 static void test_HeapQueryInformation(void)
806 ULONG info;
807 SIZE_T size;
808 BOOL ret;
810 pHeapQueryInformation = (void *)GetProcAddress(GetModuleHandleA("kernel32.dll"), "HeapQueryInformation");
811 if (!pHeapQueryInformation)
813 win_skip("HeapQueryInformation is not available\n");
814 return;
817 if (0) /* crashes under XP */
819 size = 0;
820 pHeapQueryInformation(0,
821 HeapCompatibilityInformation,
822 &info, sizeof(info), &size);
823 size = 0;
824 pHeapQueryInformation(GetProcessHeap(),
825 HeapCompatibilityInformation,
826 NULL, sizeof(info), &size);
829 size = 0;
830 SetLastError(0xdeadbeef);
831 ret = pHeapQueryInformation(GetProcessHeap(),
832 HeapCompatibilityInformation,
833 NULL, 0, &size);
834 ok(!ret, "HeapQueryInformation should fail\n");
835 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
836 "expected ERROR_INSUFFICIENT_BUFFER got %u\n", GetLastError());
837 ok(size == sizeof(ULONG), "expected 4, got %lu\n", size);
839 SetLastError(0xdeadbeef);
840 ret = pHeapQueryInformation(GetProcessHeap(),
841 HeapCompatibilityInformation,
842 NULL, 0, NULL);
843 ok(!ret, "HeapQueryInformation should fail\n");
844 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
845 "expected ERROR_INSUFFICIENT_BUFFER got %u\n", GetLastError());
847 info = 0xdeadbeaf;
848 SetLastError(0xdeadbeef);
849 ret = pHeapQueryInformation(GetProcessHeap(),
850 HeapCompatibilityInformation,
851 &info, sizeof(info) + 1, NULL);
852 ok(ret, "HeapQueryInformation error %u\n", GetLastError());
853 ok(info == 0 || info == 1 || info == 2, "expected 0, 1 or 2, got %u\n", info);
856 static void test_heap_checks( DWORD flags )
858 BYTE old, *p, *p2;
859 BOOL ret;
860 SIZE_T i, size, large_size = 3000 * 1024 + 37;
862 if (flags & HEAP_PAGE_ALLOCS) return; /* no tests for that case yet */
863 trace( "testing heap flags %08x\n", flags );
865 p = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, 17 );
866 ok( p != NULL, "HeapAlloc failed\n" );
868 ret = HeapValidate( GetProcessHeap(), 0, p );
869 ok( ret, "HeapValidate failed\n" );
871 size = HeapSize( GetProcessHeap(), 0, p );
872 ok( size == 17, "Wrong size %lu\n", size );
874 ok( p[14] == 0, "wrong data %x\n", p[14] );
875 ok( p[15] == 0, "wrong data %x\n", p[15] );
876 ok( p[16] == 0, "wrong data %x\n", p[16] );
878 if (flags & HEAP_TAIL_CHECKING_ENABLED)
880 ok( p[17] == 0xab, "wrong padding %x\n", p[17] );
881 ok( p[18] == 0xab, "wrong padding %x\n", p[18] );
882 ok( p[19] == 0xab, "wrong padding %x\n", p[19] );
885 p2 = HeapReAlloc( GetProcessHeap(), HEAP_REALLOC_IN_PLACE_ONLY, p, 14 );
886 if (p2 == p)
888 if (flags & HEAP_TAIL_CHECKING_ENABLED)
890 ok( p[14] == 0xab, "wrong padding %x\n", p[14] );
891 ok( p[15] == 0xab, "wrong padding %x\n", p[15] );
892 ok( p[16] == 0xab, "wrong padding %x\n", p[16] );
894 else
896 ok( p[14] == 0, "wrong padding %x\n", p[14] );
897 ok( p[15] == 0, "wrong padding %x\n", p[15] );
900 else skip( "realloc in place failed\n");
902 ret = HeapFree( GetProcessHeap(), 0, p );
903 ok( ret, "HeapFree failed\n" );
905 p = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, 17 );
906 ok( p != NULL, "HeapAlloc failed\n" );
907 old = p[17];
908 p[17] = 0xcc;
910 if (flags & HEAP_TAIL_CHECKING_ENABLED)
912 ret = HeapValidate( GetProcessHeap(), 0, p );
913 ok( !ret, "HeapValidate succeeded\n" );
915 /* other calls only check when HEAP_VALIDATE is set */
916 if (flags & HEAP_VALIDATE)
918 size = HeapSize( GetProcessHeap(), 0, p );
919 ok( size == ~(SIZE_T)0 || broken(size == ~0u), "Wrong size %lu\n", size );
921 p2 = HeapReAlloc( GetProcessHeap(), 0, p, 14 );
922 ok( p2 == NULL, "HeapReAlloc succeeded\n" );
924 ret = HeapFree( GetProcessHeap(), 0, p );
925 ok( !ret || broken(sizeof(void*) == 8), /* not caught on xp64 */
926 "HeapFree succeeded\n" );
929 p[17] = old;
930 size = HeapSize( GetProcessHeap(), 0, p );
931 ok( size == 17, "Wrong size %lu\n", size );
933 p2 = HeapReAlloc( GetProcessHeap(), 0, p, 14 );
934 ok( p2 != NULL, "HeapReAlloc failed\n" );
935 p = p2;
938 ret = HeapFree( GetProcessHeap(), 0, p );
939 ok( ret, "HeapFree failed\n" );
941 p = HeapAlloc( GetProcessHeap(), 0, 37 );
942 ok( p != NULL, "HeapAlloc failed\n" );
943 memset( p, 0xcc, 37 );
945 ret = HeapFree( GetProcessHeap(), 0, p );
946 ok( ret, "HeapFree failed\n" );
948 if (flags & HEAP_FREE_CHECKING_ENABLED)
950 ok( p[16] == 0xee, "wrong data %x\n", p[16] );
951 ok( p[17] == 0xfe, "wrong data %x\n", p[17] );
952 ok( p[18] == 0xee, "wrong data %x\n", p[18] );
953 ok( p[19] == 0xfe, "wrong data %x\n", p[19] );
955 ret = HeapValidate( GetProcessHeap(), 0, NULL );
956 ok( ret, "HeapValidate failed\n" );
958 old = p[16];
959 p[16] = 0xcc;
960 ret = HeapValidate( GetProcessHeap(), 0, NULL );
961 ok( !ret, "HeapValidate succeeded\n" );
963 p[16] = old;
964 ret = HeapValidate( GetProcessHeap(), 0, NULL );
965 ok( ret, "HeapValidate failed\n" );
968 /* now test large blocks */
970 p = HeapAlloc( GetProcessHeap(), 0, large_size );
971 ok( p != NULL, "HeapAlloc failed\n" );
973 ret = HeapValidate( GetProcessHeap(), 0, p );
974 ok( ret, "HeapValidate failed\n" );
976 size = HeapSize( GetProcessHeap(), 0, p );
977 ok( size == large_size, "Wrong size %lu\n", size );
979 ok( p[large_size - 2] == 0, "wrong data %x\n", p[large_size - 2] );
980 ok( p[large_size - 1] == 0, "wrong data %x\n", p[large_size - 1] );
982 if (flags & HEAP_TAIL_CHECKING_ENABLED)
984 /* Windows doesn't do tail checking on large blocks */
985 ok( p[large_size] == 0xab || broken(p[large_size] == 0), "wrong data %x\n", p[large_size] );
986 ok( p[large_size+1] == 0xab || broken(p[large_size+1] == 0), "wrong data %x\n", p[large_size+1] );
987 ok( p[large_size+2] == 0xab || broken(p[large_size+2] == 0), "wrong data %x\n", p[large_size+2] );
988 if (p[large_size] == 0xab)
990 p[large_size] = 0xcc;
991 ret = HeapValidate( GetProcessHeap(), 0, p );
992 ok( !ret, "HeapValidate succeeded\n" );
994 /* other calls only check when HEAP_VALIDATE is set */
995 if (flags & HEAP_VALIDATE)
997 size = HeapSize( GetProcessHeap(), 0, p );
998 ok( size == ~(SIZE_T)0, "Wrong size %lu\n", size );
1000 p2 = HeapReAlloc( GetProcessHeap(), 0, p, large_size - 3 );
1001 ok( p2 == NULL, "HeapReAlloc succeeded\n" );
1003 ret = HeapFree( GetProcessHeap(), 0, p );
1004 ok( !ret, "HeapFree succeeded\n" );
1006 p[large_size] = 0xab;
1010 ret = HeapFree( GetProcessHeap(), 0, p );
1011 ok( ret, "HeapFree failed\n" );
1013 /* test block sizes when tail checking */
1014 if (flags & HEAP_TAIL_CHECKING_ENABLED)
1016 for (size = 0; size < 64; size++)
1018 p = HeapAlloc( GetProcessHeap(), 0, size );
1019 for (i = 0; i < 32; i++) if (p[size + i] != 0xab) break;
1020 ok( i >= 8, "only %lu tail bytes for size %lu\n", i, size );
1021 HeapFree( GetProcessHeap(), 0, p );
1026 static void test_debug_heap( const char *argv0, DWORD flags )
1028 char keyname[MAX_PATH];
1029 char buffer[MAX_PATH];
1030 PROCESS_INFORMATION info;
1031 STARTUPINFOA startup;
1032 BOOL ret;
1033 DWORD err;
1034 HKEY hkey;
1035 const char *basename;
1037 if ((basename = strrchr( argv0, '\\' ))) basename++;
1038 else basename = argv0;
1040 sprintf( keyname, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Image File Execution Options\\%s",
1041 basename );
1042 if (!strcmp( keyname + strlen(keyname) - 3, ".so" )) keyname[strlen(keyname) - 3] = 0;
1044 err = RegCreateKeyA( HKEY_LOCAL_MACHINE, keyname, &hkey );
1045 if (err == ERROR_ACCESS_DENIED)
1047 skip("Not authorized to change the image file execution options\n");
1048 return;
1050 ok( !err, "failed to create '%s' error %u\n", keyname, err );
1051 if (err) return;
1053 if (flags == 0xdeadbeef) /* magic value for unsetting it */
1054 RegDeleteValueA( hkey, "GlobalFlag" );
1055 else
1056 RegSetValueExA( hkey, "GlobalFlag", 0, REG_DWORD, (BYTE *)&flags, sizeof(flags) );
1058 memset( &startup, 0, sizeof(startup) );
1059 startup.cb = sizeof(startup);
1061 sprintf( buffer, "%s heap.c 0x%x", argv0, flags );
1062 ret = CreateProcessA( NULL, buffer, NULL, NULL, FALSE, 0, NULL, NULL, &startup, &info );
1063 ok( ret, "failed to create child process error %u\n", GetLastError() );
1064 if (ret)
1066 winetest_wait_child_process( info.hProcess );
1067 CloseHandle( info.hThread );
1068 CloseHandle( info.hProcess );
1070 RegDeleteValueA( hkey, "GlobalFlag" );
1071 RegCloseKey( hkey );
1072 RegDeleteKeyA( HKEY_LOCAL_MACHINE, keyname );
1075 static DWORD heap_flags_from_global_flag( DWORD flag )
1077 DWORD ret = 0;
1079 if (flag & FLG_HEAP_ENABLE_TAIL_CHECK)
1080 ret |= HEAP_TAIL_CHECKING_ENABLED;
1081 if (flag & FLG_HEAP_ENABLE_FREE_CHECK)
1082 ret |= HEAP_FREE_CHECKING_ENABLED;
1083 if (flag & FLG_HEAP_VALIDATE_PARAMETERS)
1084 ret |= HEAP_VALIDATE_PARAMS | HEAP_VALIDATE | HEAP_TAIL_CHECKING_ENABLED | HEAP_FREE_CHECKING_ENABLED;
1085 if (flag & FLG_HEAP_VALIDATE_ALL)
1086 ret |= HEAP_VALIDATE_ALL | HEAP_VALIDATE | HEAP_TAIL_CHECKING_ENABLED | HEAP_FREE_CHECKING_ENABLED;
1087 if (flag & FLG_HEAP_DISABLE_COALESCING)
1088 ret |= HEAP_DISABLE_COALESCE_ON_FREE;
1089 if (flag & FLG_HEAP_PAGE_ALLOCS)
1090 ret |= HEAP_PAGE_ALLOCS | HEAP_GROWABLE;
1091 return ret;
1094 static void test_child_heap( const char *arg )
1096 struct heap_layout *heap = GetProcessHeap();
1097 DWORD expected = strtoul( arg, 0, 16 );
1098 DWORD expect_heap;
1100 if (expected == 0xdeadbeef) /* expected value comes from Session Manager global flags */
1102 HKEY hkey;
1103 expected = 0;
1104 if (!RegOpenKeyA( HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Control\\Session Manager", &hkey ))
1106 char buffer[32];
1107 DWORD type, size = sizeof(buffer);
1109 if (!RegQueryValueExA( hkey, "GlobalFlag", 0, &type, (BYTE *)buffer, &size ))
1111 if (type == REG_DWORD) expected = *(DWORD *)buffer;
1112 else if (type == REG_SZ) expected = strtoul( buffer, 0, 16 );
1114 RegCloseKey( hkey );
1117 if (expected && !pRtlGetNtGlobalFlags()) /* not working on NT4 */
1119 win_skip( "global flags not set\n" );
1120 return;
1123 ok( pRtlGetNtGlobalFlags() == expected,
1124 "%s: got global flags %08x expected %08x\n", arg, pRtlGetNtGlobalFlags(), expected );
1126 expect_heap = heap_flags_from_global_flag( expected );
1128 if (!(heap->flags & HEAP_GROWABLE) || heap->pattern == 0xffeeffee) /* vista layout */
1130 ok( (heap->flags & ~HEAP_GROWABLE) == 0, "%s: got heap flags %08x\n", arg, heap->flags );
1132 else if (heap->pattern == 0xeeeeeeee && heap->flags == 0xeeeeeeee)
1134 ok( expected & FLG_HEAP_PAGE_ALLOCS, "%s: got heap flags 0xeeeeeeee without page alloc\n", arg );
1136 else
1138 ok( heap->flags == (expect_heap | HEAP_GROWABLE),
1139 "%s: got heap flags %08x expected %08x\n", arg, heap->flags, expect_heap );
1140 ok( heap->force_flags == (expect_heap & ~0x18000080),
1141 "%s: got heap force flags %08x expected %08x\n", arg, heap->force_flags, expect_heap );
1142 expect_heap = heap->flags;
1145 test_heap_checks( expect_heap );
1148 START_TEST(heap)
1150 int argc;
1151 char **argv;
1153 pRtlGetNtGlobalFlags = (void *)GetProcAddress( GetModuleHandleA("ntdll.dll"), "RtlGetNtGlobalFlags" );
1155 argc = winetest_get_mainargs( &argv );
1156 if (argc >= 3)
1158 test_child_heap( argv[2] );
1159 return;
1162 test_heap();
1163 test_obsolete_flags();
1164 test_HeapCreate();
1165 test_GlobalAlloc();
1166 test_LocalAlloc();
1168 /* Test both short and very long blocks */
1169 test_sized_HeapAlloc(1);
1170 test_sized_HeapAlloc(1 << 20);
1171 test_sized_HeapReAlloc(1, 100);
1172 test_sized_HeapReAlloc(1, (1 << 20));
1173 test_sized_HeapReAlloc((1 << 20), (2 << 20));
1174 test_sized_HeapReAlloc((1 << 20), 1);
1175 test_HeapQueryInformation();
1177 if (pRtlGetNtGlobalFlags)
1179 test_debug_heap( argv[0], 0 );
1180 test_debug_heap( argv[0], FLG_HEAP_ENABLE_TAIL_CHECK );
1181 test_debug_heap( argv[0], FLG_HEAP_ENABLE_FREE_CHECK );
1182 test_debug_heap( argv[0], FLG_HEAP_VALIDATE_PARAMETERS );
1183 test_debug_heap( argv[0], FLG_HEAP_VALIDATE_ALL );
1184 test_debug_heap( argv[0], FLG_POOL_ENABLE_TAGGING );
1185 test_debug_heap( argv[0], FLG_HEAP_ENABLE_TAGGING );
1186 test_debug_heap( argv[0], FLG_HEAP_ENABLE_TAG_BY_DLL );
1187 test_debug_heap( argv[0], FLG_HEAP_DISABLE_COALESCING );
1188 test_debug_heap( argv[0], FLG_HEAP_PAGE_ALLOCS );
1189 test_debug_heap( argv[0], 0xdeadbeef );
1191 else win_skip( "RtlGetNtGlobalFlags not found, skipping heap debug tests\n" );