winhttp: Fix up headers with wrong termination.
[wine] / dlls / kernel32 / tests / heap.c
1 /*
2  * Unit test suite for heap functions
3  *
4  * Copyright 2003 Dimitrie O. Paun
5  * Copyright 2006 Detlef Riekenberg
6  *
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.
11  *
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.
16  *
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
20  */
21
22 #include <stdarg.h>
23 #include <stdlib.h>
24 #include <stdio.h>
25
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winreg.h"
29 #include "winternl.h"
30 #include "wine/test.h"
31
32 #define MAGIC_DEAD 0xdeadbeef
33
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
39
40 static BOOL (WINAPI *pHeapQueryInformation)(HANDLE, HEAP_INFORMATION_CLASS, PVOID, SIZE_T, PSIZE_T);
41 static ULONG (WINAPI *pRtlGetNtGlobalFlags)(void);
42
43 struct heap_layout
44 {
45     DWORD_PTR unknown[2];
46     DWORD pattern;
47     DWORD flags;
48     DWORD force_flags;
49 };
50
51 static SIZE_T resize_9x(SIZE_T size)
52 {
53     DWORD dwSizeAligned = (size + 3) & ~3;
54     return max(dwSizeAligned, 12); /* at least 12 bytes */
55 }
56
57 static void test_sized_HeapAlloc(int nbytes)
58 {
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");
65 }
66
67 static void test_sized_HeapReAlloc(int nbytes1, int nbytes2)
68 {
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");
78 }
79
80 static void test_heap(void)
81 {
82     LPVOID  mem;
83     LPVOID  msecond;
84     DWORD   res;
85     UINT    flags;
86     HGLOBAL gbl;
87     HGLOBAL hsecond;
88     SIZE_T  size, size2;
89
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);
94
95     mem = HeapReAlloc(GetProcessHeap(), 0, NULL, 10);
96     ok(mem == NULL, "memory allocated by HeapReAlloc\n");
97
98     for (size = 0; size <= 256; size++)
99     {
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);
106     }
107
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");
118
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);
125
126     /* Global*() functions */
127     gbl = GlobalAlloc(GMEM_MOVEABLE, 0);
128     ok(gbl != NULL, "global memory not allocated for size 0\n");
129
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);
134
135     gbl = GlobalReAlloc(gbl, 0, GMEM_MOVEABLE);
136     ok(gbl != NULL, "GlobalReAlloc should not fail on size 0\n");
137
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);
143
144     gbl = GlobalReAlloc(0, 10, GMEM_MOVEABLE);
145     ok(gbl == NULL, "global realloc allocated memory\n");
146
147     /* GlobalLock / GlobalUnlock with a valid handle */
148     gbl = GlobalAlloc(GMEM_MOVEABLE, 256);
149
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());
157
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);
167
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());
175
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());
186
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());
195  
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());
213
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());
219
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());
229
230     gbl = GlobalAlloc(GMEM_DDESHARE, 100);
231
232     /* first free */
233     mem = GlobalFree(gbl);
234     ok(mem == NULL, "Expected NULL, got %p\n", mem);
235
236     /* invalid free */
237     if (sizeof(void *) != 8)  /* crashes on 64-bit Vista */
238     {
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());
246     }
247
248     gbl = GlobalAlloc(GMEM_DDESHARE, 100);
249
250     res = GlobalUnlock(gbl);
251     ok(res == 1 ||
252        broken(res == 0), /* win9x */
253        "Expected 1 or 0, got %d\n", res);
254
255     res = GlobalUnlock(gbl);
256     ok(res == 1 ||
257        broken(res == 0), /* win9x */
258        "Expected 1 or 0, got %d\n", res);
259
260     GlobalFree(gbl);
261
262     gbl = GlobalAlloc(GMEM_FIXED, 100);
263
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());
270
271     GlobalFree(gbl);
272
273     /* GlobalSize on an invalid handle */
274     if (sizeof(void *) != 8)  /* crashes on 64-bit Vista */
275     {
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());
282     }
283
284     /* ####################################### */
285     /* Local*() functions */
286     gbl = LocalAlloc(LMEM_MOVEABLE, 0);
287     ok(gbl != NULL, "local memory not allocated for size 0\n");
288
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);
293
294     gbl = LocalReAlloc(gbl, 0, LMEM_MOVEABLE);
295     ok(gbl != NULL, "LocalReAlloc should not fail on size 0\n");
296
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);
302
303     gbl = LocalReAlloc(0, 10, LMEM_MOVEABLE);
304     ok(gbl == NULL, "local realloc allocated memory\n");
305
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());
315
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);
325
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());
333
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());
344
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());
353
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());
371
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());
377
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());
384
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);
393
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());
404
405     GlobalFree(gbl);
406
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");
413
414     size = GlobalSize(gbl);
415     size2 = GlobalSize(mem);
416     ok(size == 0x123, "got %lu\n", size);
417     ok(size2 == 0x123, "got %lu\n", size2);
418
419     GlobalFree(gbl);
420
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);
427
428     size = GlobalSize(gbl);
429     ok(size == 0x123, "got %lu\n", size);
430
431     GlobalFree(gbl);
432
433     size = GlobalSize((void *)0xdeadbee0);
434     ok(size == 0, "got %lu\n", size);
435
436 }
437
438 static void test_obsolete_flags(void)
439 {
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},
453     };
454
455     unsigned int i;
456     HGLOBAL gbl;
457     UINT resultflags;
458
459     UINT (WINAPI *pGlobalFlags)(HGLOBAL);
460
461     pGlobalFlags = (void *) GetProcAddress(GetModuleHandleA("kernel32"), "GlobalFlags");
462
463     if (!pGlobalFlags)
464     {
465         win_skip("GlobalFlags is not available\n");
466         return;
467     }
468
469     for (i = 0; i < sizeof(test_global_flags)/sizeof(test_global_flags[0]); i++)
470     {
471         gbl = GlobalAlloc(test_global_flags[i].flags, 4);
472         ok(gbl != NULL, "GlobalAlloc failed\n");
473
474         SetLastError(MAGIC_DEAD);
475         resultflags = pGlobalFlags(gbl);
476
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() );
481
482         GlobalFree(gbl);
483     }
484 }
485
486 static void test_HeapQueryInformation(void)
487 {
488     ULONG info;
489     SIZE_T size;
490     BOOL ret;
491
492     pHeapQueryInformation = (void *)GetProcAddress(GetModuleHandle("kernel32.dll"), "HeapQueryInformation");
493     if (!pHeapQueryInformation)
494     {
495         win_skip("HeapQueryInformation is not available\n");
496         return;
497     }
498
499     if (0) /* crashes under XP */
500     {
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);
509     }
510
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);
520
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());
528
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);
536 }
537
538 static void test_heap_checks( DWORD flags )
539 {
540     BYTE old, *p, *p2;
541     BOOL ret;
542     SIZE_T i, size, large_size = 3000 * 1024 + 37;
543
544     if (flags & HEAP_PAGE_ALLOCS) return;  /* no tests for that case yet */
545     trace( "testing heap flags %08x\n", flags );
546
547     p = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, 17 );
548     ok( p != NULL, "HeapAlloc failed\n" );
549
550     ret = HeapValidate( GetProcessHeap(), 0, p );
551     ok( ret, "HeapValidate failed\n" );
552
553     size = HeapSize( GetProcessHeap(), 0, p );
554     ok( size == 17, "Wrong size %lu\n", size );
555
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] );
559
560     if (flags & HEAP_TAIL_CHECKING_ENABLED)
561     {
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] );
565     }
566
567     p2 = HeapReAlloc( GetProcessHeap(), HEAP_REALLOC_IN_PLACE_ONLY, p, 14 );
568     if (p2 == p)
569     {
570         if (flags & HEAP_TAIL_CHECKING_ENABLED)
571         {
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] );
575         }
576         else
577         {
578             ok( p[14] == 0, "wrong padding %x\n", p[14] );
579             ok( p[15] == 0, "wrong padding %x\n", p[15] );
580         }
581     }
582     else skip( "realloc in place failed\n");
583
584     ret = HeapFree( GetProcessHeap(), 0, p );
585     ok( ret, "HeapFree failed\n" );
586
587     p = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, 17 );
588     ok( p != NULL, "HeapAlloc failed\n" );
589     old = p[17];
590     p[17] = 0xcc;
591
592     if (flags & HEAP_TAIL_CHECKING_ENABLED)
593     {
594         ret = HeapValidate( GetProcessHeap(), 0, p );
595         ok( !ret, "HeapValidate succeeded\n" );
596
597         /* other calls only check when HEAP_VALIDATE is set */
598         if (flags & HEAP_VALIDATE)
599         {
600             size = HeapSize( GetProcessHeap(), 0, p );
601             ok( size == ~(SIZE_T)0 || broken(size == ~0u), "Wrong size %lu\n", size );
602
603             p2 = HeapReAlloc( GetProcessHeap(), 0, p, 14 );
604             ok( p2 == NULL, "HeapReAlloc succeeded\n" );
605
606             ret = HeapFree( GetProcessHeap(), 0, p );
607             ok( !ret || broken(sizeof(void*) == 8), /* not caught on xp64 */
608                 "HeapFree succeeded\n" );
609         }
610
611         p[17] = old;
612         size = HeapSize( GetProcessHeap(), 0, p );
613         ok( size == 17, "Wrong size %lu\n", size );
614
615         p2 = HeapReAlloc( GetProcessHeap(), 0, p, 14 );
616         ok( p2 != NULL, "HeapReAlloc failed\n" );
617         p = p2;
618     }
619
620     ret = HeapFree( GetProcessHeap(), 0, p );
621     ok( ret, "HeapFree failed\n" );
622
623     p = HeapAlloc( GetProcessHeap(), 0, 37 );
624     ok( p != NULL, "HeapAlloc failed\n" );
625     memset( p, 0xcc, 37 );
626
627     ret = HeapFree( GetProcessHeap(), 0, p );
628     ok( ret, "HeapFree failed\n" );
629
630     if (flags & HEAP_FREE_CHECKING_ENABLED)
631     {
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] );
636
637         ret = HeapValidate( GetProcessHeap(), 0, NULL );
638         ok( ret, "HeapValidate failed\n" );
639
640         old = p[16];
641         p[16] = 0xcc;
642         ret = HeapValidate( GetProcessHeap(), 0, NULL );
643         ok( !ret, "HeapValidate succeeded\n" );
644
645         p[16] = old;
646         ret = HeapValidate( GetProcessHeap(), 0, NULL );
647         ok( ret, "HeapValidate failed\n" );
648     }
649
650     /* now test large blocks */
651
652     p = HeapAlloc( GetProcessHeap(), 0, large_size );
653     ok( p != NULL, "HeapAlloc failed\n" );
654
655     ret = HeapValidate( GetProcessHeap(), 0, p );
656     ok( ret, "HeapValidate failed\n" );
657
658     size = HeapSize( GetProcessHeap(), 0, p );
659     ok( size == large_size, "Wrong size %lu\n", size );
660
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] );
663
664     if (flags & HEAP_TAIL_CHECKING_ENABLED)
665     {
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)
671         {
672             p[large_size] = 0xcc;
673             ret = HeapValidate( GetProcessHeap(), 0, p );
674             ok( !ret, "HeapValidate succeeded\n" );
675
676             /* other calls only check when HEAP_VALIDATE is set */
677             if (flags & HEAP_VALIDATE)
678             {
679                 size = HeapSize( GetProcessHeap(), 0, p );
680                 ok( size == ~(SIZE_T)0, "Wrong size %lu\n", size );
681
682                 p2 = HeapReAlloc( GetProcessHeap(), 0, p, large_size - 3 );
683                 ok( p2 == NULL, "HeapReAlloc succeeded\n" );
684
685                 ret = HeapFree( GetProcessHeap(), 0, p );
686                 ok( !ret, "HeapFree succeeded\n" );
687             }
688             p[large_size] = 0xab;
689         }
690     }
691
692     ret = HeapFree( GetProcessHeap(), 0, p );
693     ok( ret, "HeapFree failed\n" );
694
695     /* test block sizes when tail checking */
696     if (flags & HEAP_TAIL_CHECKING_ENABLED)
697     {
698         for (size = 0; size < 64; size++)
699         {
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 );
704         }
705     }
706 }
707
708 static void test_debug_heap( const char *argv0, DWORD flags )
709 {
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;
718
719     if ((basename = strrchr( argv0, '\\' ))) basename++;
720     else basename = argv0;
721
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;
725
726     err = RegCreateKeyA( HKEY_LOCAL_MACHINE, keyname, &hkey );
727     if (err == ERROR_ACCESS_DENIED)
728     {
729         skip("Not authorized to change the image file execution options\n");
730         return;
731     }
732     ok( !err, "failed to create '%s' error %u\n", keyname, err );
733     if (err) return;
734
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) );
739
740     memset( &startup, 0, sizeof(startup) );
741     startup.cb = sizeof(startup);
742
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)
747     {
748         winetest_wait_child_process( info.hProcess );
749         CloseHandle( info.hThread );
750         CloseHandle( info.hProcess );
751     }
752     RegDeleteValueA( hkey, "GlobalFlag" );
753     RegCloseKey( hkey );
754     RegDeleteKeyA( HKEY_LOCAL_MACHINE, keyname );
755 }
756
757 static DWORD heap_flags_from_global_flag( DWORD flag )
758 {
759     DWORD ret = 0;
760
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;
774 }
775
776 static void test_child_heap( const char *arg )
777 {
778     struct heap_layout *heap = GetProcessHeap();
779     DWORD expected = strtoul( arg, 0, 16 );
780     DWORD expect_heap;
781
782     if (expected == 0xdeadbeef)  /* expected value comes from Session Manager global flags */
783     {
784         HKEY hkey;
785         expected = 0;
786         if (!RegOpenKeyA( HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Control\\Session Manager", &hkey ))
787         {
788             char buffer[32];
789             DWORD type, size = sizeof(buffer);
790
791             if (!RegQueryValueExA( hkey, "GlobalFlag", 0, &type, (BYTE *)buffer, &size ))
792             {
793                 if (type == REG_DWORD) expected = *(DWORD *)buffer;
794                 else if (type == REG_SZ) expected = strtoul( buffer, 0, 16 );
795             }
796             RegCloseKey( hkey );
797         }
798     }
799     if (expected && !pRtlGetNtGlobalFlags())  /* not working on NT4 */
800     {
801         win_skip( "global flags not set\n" );
802         return;
803     }
804
805     ok( pRtlGetNtGlobalFlags() == expected,
806         "%s: got global flags %08x expected %08x\n", arg, pRtlGetNtGlobalFlags(), expected );
807
808     expect_heap = heap_flags_from_global_flag( expected );
809
810     if (!(heap->flags & HEAP_GROWABLE) || heap->pattern == 0xffeeffee)  /* vista layout */
811     {
812         ok( heap->flags == 0, "%s: got heap flags %08x expected 0\n", arg, heap->flags );
813     }
814     else if (heap->pattern == 0xeeeeeeee && heap->flags == 0xeeeeeeee)
815     {
816         ok( expected & FLG_HEAP_PAGE_ALLOCS, "%s: got heap flags 0xeeeeeeee without page alloc\n", arg );
817     }
818     else
819     {
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;
825     }
826
827     test_heap_checks( expect_heap );
828 }
829
830 START_TEST(heap)
831 {
832     int argc;
833     char **argv;
834
835     pRtlGetNtGlobalFlags = (void *)GetProcAddress( GetModuleHandleA("ntdll.dll"), "RtlGetNtGlobalFlags" );
836
837     argc = winetest_get_mainargs( &argv );
838     if (argc >= 3)
839     {
840         test_child_heap( argv[2] );
841         return;
842     }
843
844     test_heap();
845     test_obsolete_flags();
846
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();
855
856     if (pRtlGetNtGlobalFlags)
857     {
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 );
869     }
870     else win_skip( "RtlGetNtGlobalFlags not found, skipping heap debug tests\n" );
871 }