wintrust: Close file handle on error loading a message from it.
[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 unknown[3];
46     DWORD flags;
47     DWORD force_flags;
48 };
49
50 static SIZE_T resize_9x(SIZE_T size)
51 {
52     DWORD dwSizeAligned = (size + 3) & ~3;
53     return max(dwSizeAligned, 12); /* at least 12 bytes */
54 }
55
56 static void test_sized_HeapAlloc(int nbytes)
57 {
58     int success;
59     char *buf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, nbytes);
60     ok(buf != NULL, "allocate failed\n");
61     ok(buf[0] == 0, "buffer not zeroed\n");
62     success = HeapFree(GetProcessHeap(), 0, buf);
63     ok(success, "free failed\n");
64 }
65
66 static void test_sized_HeapReAlloc(int nbytes1, int nbytes2)
67 {
68     int success;
69     char *buf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, nbytes1);
70     ok(buf != NULL, "allocate failed\n");
71     ok(buf[0] == 0, "buffer not zeroed\n");
72     buf = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, buf, nbytes2);
73     ok(buf != NULL, "reallocate failed\n");
74     ok(buf[nbytes2-1] == 0, "buffer not zeroed\n");
75     success = HeapFree(GetProcessHeap(), 0, buf);
76     ok(success, "free failed\n");
77 }
78
79 static void test_heap(void)
80 {
81     LPVOID  mem;
82     LPVOID  msecond;
83     DWORD   res;
84     UINT    flags;
85     HGLOBAL gbl;
86     HGLOBAL hsecond;
87     SIZE_T  size;
88
89     /* Heap*() functions */
90     mem = HeapAlloc(GetProcessHeap(), 0, 0);
91     ok(mem != NULL, "memory not allocated for size 0\n");
92     HeapFree(GetProcessHeap(), 0, mem);
93
94     mem = HeapReAlloc(GetProcessHeap(), 0, NULL, 10);
95     ok(mem == NULL, "memory allocated by HeapReAlloc\n");
96
97     for (size = 0; size <= 256; size++)
98     {
99         SIZE_T heap_size;
100         mem = HeapAlloc(GetProcessHeap(), 0, size);
101         heap_size = HeapSize(GetProcessHeap(), 0, mem);
102         ok(heap_size == size || heap_size == resize_9x(size), 
103             "HeapSize returned %lu instead of %lu or %lu\n", heap_size, size, resize_9x(size));
104         HeapFree(GetProcessHeap(), 0, mem);
105     }
106
107     /* test some border cases of HeapAlloc and HeapReAlloc */
108     mem = HeapAlloc(GetProcessHeap(), 0, 0);
109     ok(mem != NULL, "memory not allocated for size 0\n");
110     msecond = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, mem, ~(SIZE_T)0 - 7);
111     ok(msecond == NULL, "HeapReAlloc(~0 - 7) should have failed\n");
112     msecond = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, mem, ~(SIZE_T)0);
113     ok(msecond == NULL, "HeapReAlloc(~0) should have failed\n");
114     HeapFree(GetProcessHeap(), 0, mem);
115     mem = HeapAlloc(GetProcessHeap(), 0, ~(SIZE_T)0);
116     ok(mem == NULL, "memory allocated for size ~0\n");
117
118     /* large blocks must be 16-byte aligned */
119     mem = HeapAlloc(GetProcessHeap(), 0, 512 * 1024);
120     ok( mem != NULL, "failed for size 512K\n" );
121     ok( (ULONG_PTR)mem % 16 == 0 || broken((ULONG_PTR)mem % 16) /* win9x */,
122         "512K block not 16-byte aligned\n" );
123     HeapFree(GetProcessHeap(), 0, mem);
124
125     /* Global*() functions */
126     gbl = GlobalAlloc(GMEM_MOVEABLE, 0);
127     ok(gbl != NULL, "global memory not allocated for size 0\n");
128
129     gbl = GlobalReAlloc(gbl, 10, GMEM_MOVEABLE);
130     ok(gbl != NULL, "Can't realloc global memory\n");
131     size = GlobalSize(gbl);
132     ok(size >= 10 && size <= 16, "Memory not resized to size 10, instead size=%ld\n", size);
133
134     gbl = GlobalReAlloc(gbl, 0, GMEM_MOVEABLE);
135     ok(gbl != NULL, "GlobalReAlloc should not fail on size 0\n");
136
137     size = GlobalSize(gbl);
138     ok(size == 0, "Memory not resized to size 0, instead size=%ld\n", size);
139     ok(GlobalFree(gbl) == NULL, "Memory not freed\n");
140     size = GlobalSize(gbl);
141     ok(size == 0, "Memory should have been freed, size=%ld\n", size);
142
143     gbl = GlobalReAlloc(0, 10, GMEM_MOVEABLE);
144     ok(gbl == NULL, "global realloc allocated memory\n");
145
146     /* GlobalLock / GlobalUnlock with a valid handle */
147     gbl = GlobalAlloc(GMEM_MOVEABLE, 256);
148
149     SetLastError(MAGIC_DEAD);
150     mem = GlobalLock(gbl);      /* #1 */
151     ok(mem != NULL, "returned %p with %d (expected '!= NULL')\n", mem, GetLastError());
152     SetLastError(MAGIC_DEAD);
153     flags = GlobalFlags(gbl);
154     ok( flags == 1, "returned 0x%04x with %d (expected '0x0001')\n",
155         flags, GetLastError());
156
157     SetLastError(MAGIC_DEAD);
158     msecond = GlobalLock(gbl);   /* #2 */
159     ok( msecond == mem, "returned %p with %d (expected '%p')\n",
160         msecond, GetLastError(), mem);
161     SetLastError(MAGIC_DEAD);
162     flags = GlobalFlags(gbl);
163     ok( flags == 2, "returned 0x%04x with %d (expected '0x0002')\n",
164         flags, GetLastError());
165     SetLastError(MAGIC_DEAD);
166
167     SetLastError(MAGIC_DEAD);
168     res = GlobalUnlock(gbl);    /* #1 */
169     ok(res, "returned %d with %d (expected '!= 0')\n", res, GetLastError());
170     SetLastError(MAGIC_DEAD);
171     flags = GlobalFlags(gbl);
172     ok( flags , "returned 0x%04x with %d (expected '!= 0')\n",
173         flags, GetLastError());
174
175     SetLastError(MAGIC_DEAD);
176     res = GlobalUnlock(gbl);    /* #0 */
177     /* NT: ERROR_SUCCESS (documented on MSDN), 9x: untouched */
178     ok(!res && ((GetLastError() == ERROR_SUCCESS) || (GetLastError() == MAGIC_DEAD)),
179         "returned %d with %d (expected '0' with: ERROR_SUCCESS or "
180         "MAGIC_DEAD)\n", res, GetLastError());
181     SetLastError(MAGIC_DEAD);
182     flags = GlobalFlags(gbl);
183     ok( !flags , "returned 0x%04x with %d (expected '0')\n",
184         flags, GetLastError());
185
186     /* Unlock an already unlocked Handle */
187     SetLastError(MAGIC_DEAD);
188     res = GlobalUnlock(gbl);
189     /* NT: ERROR_NOT_LOCKED,  9x: untouched */
190     ok( !res &&
191         ((GetLastError() == ERROR_NOT_LOCKED) || (GetLastError() == MAGIC_DEAD)),
192         "returned %d with %d (expected '0' with: ERROR_NOT_LOCKED or "
193         "MAGIC_DEAD)\n", res, GetLastError());
194  
195     GlobalFree(gbl);
196     /* invalid handles are caught in windows: */
197     SetLastError(MAGIC_DEAD);
198     hsecond = GlobalFree(gbl);      /* invalid handle: free memory twice */
199     ok( (hsecond == gbl) && (GetLastError() == ERROR_INVALID_HANDLE),
200         "returned %p with 0x%08x (expected %p with ERROR_INVALID_HANDLE)\n",
201         hsecond, GetLastError(), gbl);
202     SetLastError(MAGIC_DEAD);
203     flags = GlobalFlags(gbl);
204     ok( (flags == GMEM_INVALID_HANDLE) && (GetLastError() == ERROR_INVALID_HANDLE),
205         "returned 0x%04x with 0x%08x (expected GMEM_INVALID_HANDLE with "
206         "ERROR_INVALID_HANDLE)\n", flags, GetLastError());
207     SetLastError(MAGIC_DEAD);
208     size = GlobalSize(gbl);
209     ok( (size == 0) && (GetLastError() == ERROR_INVALID_HANDLE),
210         "returned %ld with 0x%08x (expected '0' with ERROR_INVALID_HANDLE)\n",
211         size, GetLastError());
212
213     SetLastError(MAGIC_DEAD);
214     mem = GlobalLock(gbl);
215     ok( (mem == NULL) && (GetLastError() == ERROR_INVALID_HANDLE),
216         "returned %p with 0x%08x (expected NULL with ERROR_INVALID_HANDLE)\n",
217         mem, GetLastError());
218
219     /* documented on MSDN: GlobalUnlock() return FALSE on failure.
220        Win9x and wine return FALSE with ERROR_INVALID_HANDLE, but on 
221        NT 3.51 and XPsp2, TRUE with ERROR_INVALID_HANDLE is returned.
222        The similar Test for LocalUnlock() works on all Systems */
223     SetLastError(MAGIC_DEAD);
224     res = GlobalUnlock(gbl);
225     ok(GetLastError() == ERROR_INVALID_HANDLE,
226         "returned %d with %d (expected ERROR_INVALID_HANDLE)\n",
227         res, GetLastError());
228
229     gbl = GlobalAlloc(GMEM_DDESHARE, 100);
230
231     /* first free */
232     mem = GlobalFree(gbl);
233     ok(mem == NULL, "Expected NULL, got %p\n", mem);
234
235     /* invalid free */
236     if (sizeof(void *) != 8)  /* crashes on 64-bit Vista */
237     {
238         SetLastError(MAGIC_DEAD);
239         mem = GlobalFree(gbl);
240         ok(mem == gbl || broken(mem == NULL) /* nt4 */, "Expected gbl, got %p\n", mem);
241         if (mem == gbl)
242             ok(GetLastError() == ERROR_INVALID_HANDLE ||
243                GetLastError() == ERROR_INVALID_PARAMETER, /* win9x */
244                "Expected ERROR_INVALID_HANDLE or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
245     }
246
247     gbl = GlobalAlloc(GMEM_DDESHARE, 100);
248
249     res = GlobalUnlock(gbl);
250     ok(res == 1 ||
251        res == 0, /* win9x */
252        "Expected 1 or 0, got %d\n", res);
253
254     res = GlobalUnlock(gbl);
255     ok(res == 1 ||
256        res == 0, /* win9x */
257        "Expected 1 or 0, got %d\n", res);
258
259     /* GlobalSize on an invalid handle */
260     if (sizeof(void *) != 8)  /* crashes on 64-bit Vista */
261     {
262         SetLastError(MAGIC_DEAD);
263         size = GlobalSize((HGLOBAL)0xc042);
264         ok(size == 0, "Expected 0, got %ld\n", size);
265         ok(GetLastError() == ERROR_INVALID_HANDLE ||
266            GetLastError() == ERROR_INVALID_PARAMETER, /* win9x */
267            "Expected ERROR_INVALID_HANDLE or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
268     }
269
270     GlobalFree(gbl);
271
272     /* ####################################### */
273     /* Local*() functions */
274     gbl = LocalAlloc(LMEM_MOVEABLE, 0);
275     ok(gbl != NULL, "local memory not allocated for size 0\n");
276
277     gbl = LocalReAlloc(gbl, 10, LMEM_MOVEABLE);
278     ok(gbl != NULL, "Can't realloc local memory\n");
279     size = LocalSize(gbl);
280     ok(size >= 10 && size <= 16, "Memory not resized to size 10, instead size=%ld\n", size);
281
282     gbl = LocalReAlloc(gbl, 0, LMEM_MOVEABLE);
283     ok(gbl != NULL, "LocalReAlloc should not fail on size 0\n");
284
285     size = LocalSize(gbl);
286     ok(size == 0, "Memory not resized to size 0, instead size=%ld\n", size);
287     ok(LocalFree(gbl) == NULL, "Memory not freed\n");
288     size = LocalSize(gbl);
289     ok(size == 0, "Memory should have been freed, size=%ld\n", size);
290
291     gbl = LocalReAlloc(0, 10, LMEM_MOVEABLE);
292     ok(gbl == NULL, "local realloc allocated memory\n");
293
294     /* LocalLock / LocalUnlock with a valid handle */
295     gbl = LocalAlloc(LMEM_MOVEABLE, 256);
296     SetLastError(MAGIC_DEAD);
297     mem = LocalLock(gbl);      /* #1 */
298     ok(mem != NULL, "returned %p with %d (expected '!= NULL')\n", mem, GetLastError());
299     SetLastError(MAGIC_DEAD);
300     flags = LocalFlags(gbl);
301     ok( flags == 1, "returned 0x%04x with %d (expected '0x0001')\n",
302         flags, GetLastError());
303
304     SetLastError(MAGIC_DEAD);
305     msecond = LocalLock(gbl);   /* #2 */
306     ok( msecond == mem, "returned %p with %d (expected '%p')\n",
307         msecond, GetLastError(), mem);
308     SetLastError(MAGIC_DEAD);
309     flags = LocalFlags(gbl);
310     ok( flags == 2, "returned 0x%04x with %d (expected '0x0002')\n",
311         flags, GetLastError());
312     SetLastError(MAGIC_DEAD);
313
314     SetLastError(MAGIC_DEAD);
315     res = LocalUnlock(gbl);    /* #1 */
316     ok(res, "returned %d with %d (expected '!= 0')\n", res, GetLastError());
317     SetLastError(MAGIC_DEAD);
318     flags = LocalFlags(gbl);
319     ok( flags , "returned 0x%04x with %d (expected '!= 0')\n",
320         flags, GetLastError());
321
322     SetLastError(MAGIC_DEAD);
323     res = LocalUnlock(gbl);    /* #0 */
324     /* NT: ERROR_SUCCESS (documented on MSDN), 9x: untouched */
325     ok(!res && ((GetLastError() == ERROR_SUCCESS) || (GetLastError() == MAGIC_DEAD)),
326         "returned %d with %d (expected '0' with: ERROR_SUCCESS or "
327         "MAGIC_DEAD)\n", res, GetLastError());
328     SetLastError(MAGIC_DEAD);
329     flags = LocalFlags(gbl);
330     ok( !flags , "returned 0x%04x with %d (expected '0')\n",
331         flags, GetLastError());
332
333     /* Unlock an already unlocked Handle */
334     SetLastError(MAGIC_DEAD);
335     res = LocalUnlock(gbl);
336     /* NT: ERROR_NOT_LOCKED,  9x: untouched */
337     ok( !res &&
338         ((GetLastError() == ERROR_NOT_LOCKED) || (GetLastError() == MAGIC_DEAD)),
339         "returned %d with %d (expected '0' with: ERROR_NOT_LOCKED or "
340         "MAGIC_DEAD)\n", res, GetLastError());
341
342     LocalFree(gbl);
343     /* invalid handles are caught in windows: */
344     SetLastError(MAGIC_DEAD);
345     hsecond = LocalFree(gbl);       /* invalid handle: free memory twice */
346     ok( (hsecond == gbl) && (GetLastError() == ERROR_INVALID_HANDLE),
347         "returned %p with 0x%08x (expected %p with ERROR_INVALID_HANDLE)\n",
348         hsecond, GetLastError(), gbl);
349     SetLastError(MAGIC_DEAD);
350     flags = LocalFlags(gbl);
351     ok( (flags == LMEM_INVALID_HANDLE) && (GetLastError() == ERROR_INVALID_HANDLE),
352         "returned 0x%04x with 0x%08x (expected LMEM_INVALID_HANDLE with "
353         "ERROR_INVALID_HANDLE)\n", flags, GetLastError());
354     SetLastError(MAGIC_DEAD);
355     size = LocalSize(gbl);
356     ok( (size == 0) && (GetLastError() == ERROR_INVALID_HANDLE),
357         "returned %ld with 0x%08x (expected '0' with ERROR_INVALID_HANDLE)\n",
358         size, GetLastError());
359
360     SetLastError(MAGIC_DEAD);
361     mem = LocalLock(gbl);
362     ok( (mem == NULL) && (GetLastError() == ERROR_INVALID_HANDLE),
363         "returned %p with 0x%08x (expected NULL with ERROR_INVALID_HANDLE)\n",
364         mem, GetLastError());
365
366     /* This Test works the same on all Systems (GlobalUnlock() is different) */
367     SetLastError(MAGIC_DEAD);
368     res = LocalUnlock(gbl);
369     ok(!res && (GetLastError() == ERROR_INVALID_HANDLE),
370         "returned %d with %d (expected '0' with ERROR_INVALID_HANDLE)\n",
371         res, GetLastError());
372
373     /* trying to lock empty memory should give an error */
374     gbl = GlobalAlloc(GMEM_MOVEABLE|GMEM_ZEROINIT,0);
375     ok(gbl != NULL, "returned NULL\n");
376     SetLastError(MAGIC_DEAD);
377     mem = GlobalLock(gbl);
378     /* NT: ERROR_DISCARDED,  9x: untouched */
379     ok( (mem == NULL) &&
380         ((GetLastError() == ERROR_DISCARDED) || (GetLastError() == MAGIC_DEAD)),
381         "returned %p with 0x%x/%d (expected 'NULL' with: ERROR_DISCARDED or "
382         "MAGIC_DEAD)\n", mem, GetLastError(), GetLastError());
383
384     GlobalFree(gbl);
385 }
386
387 static void test_obsolete_flags(void)
388 {
389     static struct {
390         UINT flags;
391         UINT globalflags;
392     } test_global_flags[] = {
393         {GMEM_FIXED | GMEM_NOTIFY, 0},
394         {GMEM_FIXED | GMEM_DISCARDABLE, 0},
395         {GMEM_MOVEABLE | GMEM_NOTIFY, 0},
396         {GMEM_MOVEABLE | GMEM_DDESHARE, GMEM_DDESHARE},
397         {GMEM_MOVEABLE | GMEM_NOT_BANKED, 0},
398         {GMEM_MOVEABLE | GMEM_NODISCARD, 0},
399         {GMEM_MOVEABLE | GMEM_DISCARDABLE, GMEM_DISCARDABLE},
400         {GMEM_MOVEABLE | GMEM_DDESHARE | GMEM_DISCARDABLE | GMEM_LOWER | GMEM_NOCOMPACT | GMEM_NODISCARD |
401          GMEM_NOT_BANKED | GMEM_NOTIFY, GMEM_DDESHARE | GMEM_DISCARDABLE},
402     };
403
404     unsigned int i;
405     HGLOBAL gbl;
406     UINT resultflags;
407
408     UINT (WINAPI *pGlobalFlags)(HGLOBAL);
409
410     pGlobalFlags = (void *) GetProcAddress(GetModuleHandleA("kernel32"), "GlobalFlags");
411
412     if (!pGlobalFlags)
413     {
414         win_skip("GlobalFlags is not available\n");
415         return;
416     }
417
418     for (i = 0; i < sizeof(test_global_flags)/sizeof(test_global_flags[0]); i++)
419     {
420         gbl = GlobalAlloc(test_global_flags[i].flags, 4);
421         ok(gbl != NULL, "GlobalAlloc failed\n");
422
423         SetLastError(MAGIC_DEAD);
424         resultflags = pGlobalFlags(gbl);
425
426         ok( resultflags == test_global_flags[i].globalflags ||
427             broken(resultflags == (test_global_flags[i].globalflags & ~GMEM_DDESHARE)), /* win9x */
428             "%u: expected 0x%08x, but returned 0x%08x with %d\n",
429             i, test_global_flags[i].globalflags, resultflags, GetLastError() );
430
431         GlobalFree(gbl);
432     }
433 }
434
435 static void test_HeapQueryInformation(void)
436 {
437     ULONG info;
438     SIZE_T size;
439     BOOL ret;
440
441     pHeapQueryInformation = (void *)GetProcAddress(GetModuleHandle("kernel32.dll"), "HeapQueryInformation");
442     if (!pHeapQueryInformation)
443     {
444         win_skip("HeapQueryInformation is not available\n");
445         return;
446     }
447
448     if (0) /* crashes under XP */
449     {
450         size = 0;
451         ret = pHeapQueryInformation(0,
452                                 HeapCompatibilityInformation,
453                                 &info, sizeof(info), &size);
454         size = 0;
455         ret = pHeapQueryInformation(GetProcessHeap(),
456                                 HeapCompatibilityInformation,
457                                 NULL, sizeof(info), &size);
458     }
459
460     size = 0;
461     SetLastError(0xdeadbeef);
462     ret = pHeapQueryInformation(GetProcessHeap(),
463                                 HeapCompatibilityInformation,
464                                 NULL, 0, &size);
465     ok(!ret, "HeapQueryInformation should fail\n");
466     ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
467        "expected ERROR_INSUFFICIENT_BUFFER got %u\n", GetLastError());
468     ok(size == sizeof(ULONG), "expected 4, got %lu\n", size);
469
470     SetLastError(0xdeadbeef);
471     ret = pHeapQueryInformation(GetProcessHeap(),
472                                 HeapCompatibilityInformation,
473                                 NULL, 0, NULL);
474     ok(!ret, "HeapQueryInformation should fail\n");
475     ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
476        "expected ERROR_INSUFFICIENT_BUFFER got %u\n", GetLastError());
477
478     info = 0xdeadbeaf;
479     SetLastError(0xdeadbeef);
480     ret = pHeapQueryInformation(GetProcessHeap(),
481                                 HeapCompatibilityInformation,
482                                 &info, sizeof(info) + 1, NULL);
483     ok(ret, "HeapQueryInformation error %u\n", GetLastError());
484     ok(info == 0 || info == 1 || info == 2, "expected 0, 1 or 2, got %u\n", info);
485 }
486
487 static void test_heap_checks( DWORD flags )
488 {
489     BYTE old, *p, *p2;
490     BOOL ret;
491     SIZE_T i, size, large_size = 800 * 1024 + 37;
492
493     if (flags & HEAP_PAGE_ALLOCS) return;  /* no tests for that case yet */
494     trace( "testing heap flags %08x\n", flags );
495
496     p = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, 17 );
497     ok( p != NULL, "HeapAlloc failed\n" );
498
499     ret = HeapValidate( GetProcessHeap(), 0, p );
500     ok( ret, "HeapValidate failed\n" );
501
502     size = HeapSize( GetProcessHeap(), 0, p );
503     ok( size == 17, "Wrong size %lu\n", size );
504
505     ok( p[14] == 0, "wrong data %x\n", p[14] );
506     ok( p[15] == 0, "wrong data %x\n", p[15] );
507     ok( p[16] == 0, "wrong data %x\n", p[16] );
508
509     if (flags & HEAP_TAIL_CHECKING_ENABLED)
510     {
511         ok( p[17] == 0xab, "wrong padding %x\n", p[17] );
512         ok( p[18] == 0xab, "wrong padding %x\n", p[18] );
513         ok( p[19] == 0xab, "wrong padding %x\n", p[19] );
514     }
515
516     p2 = HeapReAlloc( GetProcessHeap(), HEAP_REALLOC_IN_PLACE_ONLY, p, 14 );
517     if (p2 == p)
518     {
519         if (flags & HEAP_TAIL_CHECKING_ENABLED)
520         {
521             ok( p[14] == 0xab, "wrong padding %x\n", p[14] );
522             ok( p[15] == 0xab, "wrong padding %x\n", p[15] );
523             ok( p[16] == 0xab, "wrong padding %x\n", p[16] );
524         }
525         else
526         {
527             ok( p[14] == 0, "wrong padding %x\n", p[14] );
528             ok( p[15] == 0, "wrong padding %x\n", p[15] );
529             ok( p[16] == 0, "wrong padding %x\n", p[16] );
530         }
531     }
532     else skip( "realloc in place failed\n ");
533
534     ret = HeapFree( GetProcessHeap(), 0, p );
535     ok( ret, "HeapFree failed\n" );
536
537     p = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, 17 );
538     ok( p != NULL, "HeapAlloc failed\n" );
539     old = p[17];
540     p[17] = 0xcc;
541
542     if (flags & HEAP_TAIL_CHECKING_ENABLED)
543     {
544         ret = HeapValidate( GetProcessHeap(), 0, p );
545         ok( !ret, "HeapValidate succeeded\n" );
546
547         /* other calls only check when HEAP_VALIDATE is set */
548         if (flags & HEAP_VALIDATE)
549         {
550             size = HeapSize( GetProcessHeap(), 0, p );
551             ok( size == ~(SIZE_T)0, "Wrong size %lu\n", size );
552
553             p2 = HeapReAlloc( GetProcessHeap(), 0, p, 14 );
554             ok( p2 == NULL, "HeapReAlloc succeeded\n" );
555
556             ret = HeapFree( GetProcessHeap(), 0, p );
557             ok( !ret, "HeapFree succeeded\n" );
558         }
559
560         p[17] = old;
561         size = HeapSize( GetProcessHeap(), 0, p );
562         ok( size == 17, "Wrong size %lu\n", size );
563
564         p2 = HeapReAlloc( GetProcessHeap(), 0, p, 14 );
565         ok( p2 != NULL, "HeapReAlloc failed\n" );
566         p = p2;
567     }
568
569     ret = HeapFree( GetProcessHeap(), 0, p );
570     ok( ret, "HeapFree failed\n" );
571
572     p = HeapAlloc( GetProcessHeap(), 0, 37 );
573     ok( p != NULL, "HeapAlloc failed\n" );
574     memset( p, 0xcc, 37 );
575
576     ret = HeapFree( GetProcessHeap(), 0, p );
577     ok( ret, "HeapFree failed\n" );
578
579     if (flags & HEAP_FREE_CHECKING_ENABLED)
580     {
581         ok( p[16] == 0xee, "wrong data %x\n", p[16] );
582         ok( p[17] == 0xfe, "wrong data %x\n", p[17] );
583         ok( p[18] == 0xee, "wrong data %x\n", p[18] );
584         ok( p[19] == 0xfe, "wrong data %x\n", p[19] );
585
586         ret = HeapValidate( GetProcessHeap(), 0, NULL );
587         ok( ret, "HeapValidate failed\n" );
588
589         old = p[16];
590         p[16] = 0xcc;
591         ret = HeapValidate( GetProcessHeap(), 0, NULL );
592         ok( !ret, "HeapValidate succeeded\n" );
593
594         p[16] = old;
595         ret = HeapValidate( GetProcessHeap(), 0, NULL );
596         ok( ret, "HeapValidate failed\n" );
597     }
598
599     /* now test large blocks */
600
601     p = HeapAlloc( GetProcessHeap(), 0, large_size );
602     ok( p != NULL, "HeapAlloc failed\n" );
603
604     ret = HeapValidate( GetProcessHeap(), 0, p );
605     ok( ret, "HeapValidate failed\n" );
606
607     size = HeapSize( GetProcessHeap(), 0, p );
608     ok( size == large_size, "Wrong size %lu\n", size );
609
610     ok( p[large_size - 2] == 0, "wrong data %x\n", p[large_size - 2] );
611     ok( p[large_size - 1] == 0, "wrong data %x\n", p[large_size - 1] );
612
613     if (flags & HEAP_TAIL_CHECKING_ENABLED)
614     {
615         /* Windows doesn't do tail checking on large blocks */
616         ok( p[large_size] == 0xab || broken(p[large_size] == 0), "wrong data %x\n", p[large_size] );
617         ok( p[large_size+1] == 0xab || broken(p[large_size+1] == 0), "wrong data %x\n", p[large_size+1] );
618         ok( p[large_size+2] == 0xab || broken(p[large_size+2] == 0), "wrong data %x\n", p[large_size+2] );
619         if (p[large_size] == 0xab)
620         {
621             p[large_size] = 0xcc;
622             ret = HeapValidate( GetProcessHeap(), 0, p );
623             ok( !ret, "HeapValidate succeeded\n" );
624
625             /* other calls only check when HEAP_VALIDATE is set */
626             if (flags & HEAP_VALIDATE)
627             {
628                 size = HeapSize( GetProcessHeap(), 0, p );
629                 ok( size == ~(SIZE_T)0, "Wrong size %lu\n", size );
630
631                 p2 = HeapReAlloc( GetProcessHeap(), 0, p, large_size - 3 );
632                 ok( p2 == NULL, "HeapReAlloc succeeded\n" );
633
634                 ret = HeapFree( GetProcessHeap(), 0, p );
635                 ok( !ret, "HeapFree succeeded\n" );
636             }
637             p[large_size] = 0xab;
638         }
639     }
640
641     ret = HeapFree( GetProcessHeap(), 0, p );
642     ok( ret, "HeapFree failed\n" );
643
644     /* test block sizes when tail checking */
645     if (flags & HEAP_TAIL_CHECKING_ENABLED)
646     {
647         for (size = 0; size < 64; size++)
648         {
649             p = HeapAlloc( GetProcessHeap(), 0, size );
650             for (i = 0; i < 32; i++) if (p[size + i] != 0xab) break;
651             ok( i >= 8, "only %lu tail bytes for size %lu\n", i, size );
652             HeapFree( GetProcessHeap(), 0, p );
653         }
654     }
655 }
656
657 static void test_debug_heap( const char *argv0, DWORD flags )
658 {
659     char keyname[MAX_PATH];
660     char buffer[MAX_PATH];
661     PROCESS_INFORMATION info;
662     STARTUPINFOA startup;
663     BOOL ret;
664     DWORD err;
665     HKEY hkey;
666     const char *basename;
667
668     if ((basename = strrchr( argv0, '\\' ))) basename++;
669     else basename = argv0;
670
671     sprintf( keyname, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Image File Execution Options\\%s",
672              basename );
673     if (!strcmp( keyname + strlen(keyname) - 3, ".so" )) keyname[strlen(keyname) - 3] = 0;
674
675     err = RegCreateKeyA( HKEY_LOCAL_MACHINE, keyname, &hkey );
676     ok( !err, "failed to create '%s' error %u\n", keyname, err );
677     if (err) return;
678
679     if (flags == 0xdeadbeef)  /* magic value for unsetting it */
680         RegDeleteValueA( hkey, "GlobalFlag" );
681     else
682         RegSetValueExA( hkey, "GlobalFlag", 0, REG_DWORD, (BYTE *)&flags, sizeof(flags) );
683
684     memset( &startup, 0, sizeof(startup) );
685     startup.cb = sizeof(startup);
686
687     sprintf( buffer, "%s heap.c 0x%x", argv0, flags );
688     ret = CreateProcessA( NULL, buffer, NULL, NULL, FALSE, 0, NULL, NULL, &startup, &info );
689     ok( ret, "failed to create child process error %u\n", GetLastError() );
690     if (ret)
691     {
692         winetest_wait_child_process( info.hProcess );
693         CloseHandle( info.hThread );
694         CloseHandle( info.hProcess );
695     }
696     RegDeleteValueA( hkey, "GlobalFlag" );
697     RegCloseKey( hkey );
698     RegDeleteKeyA( HKEY_LOCAL_MACHINE, keyname );
699 }
700
701 static DWORD heap_flags_from_global_flag( DWORD flag )
702 {
703     DWORD ret = 0;
704
705     if (flag & FLG_HEAP_ENABLE_TAIL_CHECK)
706         ret |= HEAP_TAIL_CHECKING_ENABLED;
707     if (flag & FLG_HEAP_ENABLE_FREE_CHECK)
708         ret |= HEAP_FREE_CHECKING_ENABLED;
709     if (flag & FLG_HEAP_VALIDATE_PARAMETERS)
710         ret |= HEAP_VALIDATE_PARAMS | HEAP_VALIDATE | HEAP_TAIL_CHECKING_ENABLED | HEAP_FREE_CHECKING_ENABLED;
711     if (flag & FLG_HEAP_VALIDATE_ALL)
712         ret |= HEAP_VALIDATE_ALL | HEAP_VALIDATE | HEAP_TAIL_CHECKING_ENABLED | HEAP_FREE_CHECKING_ENABLED;
713     if (flag & FLG_HEAP_DISABLE_COALESCING)
714         ret |= HEAP_DISABLE_COALESCE_ON_FREE;
715     if (flag & FLG_HEAP_PAGE_ALLOCS)
716         ret |= HEAP_PAGE_ALLOCS | HEAP_GROWABLE;
717     return ret;
718 }
719
720 static void test_child_heap( const char *arg )
721 {
722     struct heap_layout *heap = GetProcessHeap();
723     DWORD expected = strtoul( arg, 0, 16 );
724     DWORD expect_heap;
725
726     if (expected == 0xdeadbeef)  /* expected value comes from Session Manager global flags */
727     {
728         HKEY hkey;
729         expected = 0;
730         if (!RegOpenKeyA( HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Control\\Session Manager", &hkey ))
731         {
732             char buffer[32];
733             DWORD type, size = sizeof(buffer);
734
735             if (!RegQueryValueExA( hkey, "GlobalFlag", 0, &type, (BYTE *)buffer, &size ))
736             {
737                 if (type == REG_DWORD) expected = *(DWORD *)buffer;
738                 else if (type == REG_SZ) expected = strtoul( buffer, 0, 16 );
739             }
740             RegCloseKey( hkey );
741         }
742     }
743     if (expected && !pRtlGetNtGlobalFlags())  /* not working on NT4 */
744     {
745         win_skip( "global flags not set\n" );
746         return;
747     }
748
749     ok( pRtlGetNtGlobalFlags() == expected,
750         "%s: got global flags %08x expected %08x\n", arg, pRtlGetNtGlobalFlags(), expected );
751
752     expect_heap = heap_flags_from_global_flag( expected );
753
754     if (!(heap->flags & HEAP_GROWABLE) || heap->flags == 0xeeeeeeee)  /* vista layout */
755     {
756         if (expected & FLG_HEAP_PAGE_ALLOCS)
757             ok( heap->flags == 0xeeeeeeee, "%s: got heap flags %08x expected 0xeeeeeeee\n",
758                 arg, heap->flags );
759         else
760             ok( heap->flags == 0, "%s: got heap flags %08x expected 0\n", arg, heap->flags );
761     }
762     else
763     {
764         ok( heap->flags == (expect_heap | HEAP_GROWABLE),
765             "%s: got heap flags %08x expected %08x\n", arg, heap->flags, expect_heap );
766         ok( heap->force_flags == (expect_heap & ~0x18000080),
767             "%s: got heap force flags %08x expected %08x\n", arg, heap->force_flags, expect_heap );
768     }
769
770     test_heap_checks( expect_heap );
771 }
772
773 START_TEST(heap)
774 {
775     int argc;
776     char **argv;
777
778     pRtlGetNtGlobalFlags = (void *)GetProcAddress( GetModuleHandleA("ntdll.dll"), "RtlGetNtGlobalFlags" );
779
780     argc = winetest_get_mainargs( &argv );
781     if (argc >= 3)
782     {
783         test_child_heap( argv[2] );
784         return;
785     }
786
787     test_heap();
788     test_obsolete_flags();
789
790     /* Test both short and very long blocks */
791     test_sized_HeapAlloc(1);
792     test_sized_HeapAlloc(1 << 20);
793     test_sized_HeapReAlloc(1, 100);
794     test_sized_HeapReAlloc(1, (1 << 20));
795     test_sized_HeapReAlloc((1 << 20), (2 << 20));
796     test_sized_HeapReAlloc((1 << 20), 1);
797     test_HeapQueryInformation();
798
799     if (pRtlGetNtGlobalFlags)
800     {
801         test_debug_heap( argv[0], 0 );
802         test_debug_heap( argv[0], FLG_HEAP_ENABLE_TAIL_CHECK );
803         test_debug_heap( argv[0], FLG_HEAP_ENABLE_FREE_CHECK );
804         test_debug_heap( argv[0], FLG_HEAP_VALIDATE_PARAMETERS );
805         test_debug_heap( argv[0], FLG_HEAP_VALIDATE_ALL );
806         test_debug_heap( argv[0], FLG_POOL_ENABLE_TAGGING );
807         test_debug_heap( argv[0], FLG_HEAP_ENABLE_TAGGING );
808         test_debug_heap( argv[0], FLG_HEAP_ENABLE_TAG_BY_DLL );
809         test_debug_heap( argv[0], FLG_HEAP_DISABLE_COALESCING );
810         test_debug_heap( argv[0], FLG_HEAP_PAGE_ALLOCS );
811         test_debug_heap( argv[0], 0xdeadbeef );
812     }
813     else win_skip( "RtlGetNtGlobalFlags not found, skipping heap debug tests\n" );
814 }