oleaut32: Rewrite RollUdate to be easier to change and to support more conversions.
[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;
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        res == 0, /* win9x */
253        "Expected 1 or 0, got %d\n", res);
254
255     res = GlobalUnlock(gbl);
256     ok(res == 1 ||
257        res == 0, /* win9x */
258        "Expected 1 or 0, got %d\n", res);
259
260     /* GlobalSize on an invalid handle */
261     if (sizeof(void *) != 8)  /* crashes on 64-bit Vista */
262     {
263         SetLastError(MAGIC_DEAD);
264         size = GlobalSize((HGLOBAL)0xc042);
265         ok(size == 0, "Expected 0, got %ld\n", size);
266         ok(GetLastError() == ERROR_INVALID_HANDLE ||
267            GetLastError() == ERROR_INVALID_PARAMETER, /* win9x */
268            "Expected ERROR_INVALID_HANDLE or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
269     }
270
271     GlobalFree(gbl);
272
273     /* ####################################### */
274     /* Local*() functions */
275     gbl = LocalAlloc(LMEM_MOVEABLE, 0);
276     ok(gbl != NULL, "local memory not allocated for size 0\n");
277
278     gbl = LocalReAlloc(gbl, 10, LMEM_MOVEABLE);
279     ok(gbl != NULL, "Can't realloc local memory\n");
280     size = LocalSize(gbl);
281     ok(size >= 10 && size <= 16, "Memory not resized to size 10, instead size=%ld\n", size);
282
283     gbl = LocalReAlloc(gbl, 0, LMEM_MOVEABLE);
284     ok(gbl != NULL, "LocalReAlloc should not fail on size 0\n");
285
286     size = LocalSize(gbl);
287     ok(size == 0, "Memory not resized to size 0, instead size=%ld\n", size);
288     ok(LocalFree(gbl) == NULL, "Memory not freed\n");
289     size = LocalSize(gbl);
290     ok(size == 0, "Memory should have been freed, size=%ld\n", size);
291
292     gbl = LocalReAlloc(0, 10, LMEM_MOVEABLE);
293     ok(gbl == NULL, "local realloc allocated memory\n");
294
295     /* LocalLock / LocalUnlock with a valid handle */
296     gbl = LocalAlloc(LMEM_MOVEABLE, 256);
297     SetLastError(MAGIC_DEAD);
298     mem = LocalLock(gbl);      /* #1 */
299     ok(mem != NULL, "returned %p with %d (expected '!= NULL')\n", mem, GetLastError());
300     SetLastError(MAGIC_DEAD);
301     flags = LocalFlags(gbl);
302     ok( flags == 1, "returned 0x%04x with %d (expected '0x0001')\n",
303         flags, GetLastError());
304
305     SetLastError(MAGIC_DEAD);
306     msecond = LocalLock(gbl);   /* #2 */
307     ok( msecond == mem, "returned %p with %d (expected '%p')\n",
308         msecond, GetLastError(), mem);
309     SetLastError(MAGIC_DEAD);
310     flags = LocalFlags(gbl);
311     ok( flags == 2, "returned 0x%04x with %d (expected '0x0002')\n",
312         flags, GetLastError());
313     SetLastError(MAGIC_DEAD);
314
315     SetLastError(MAGIC_DEAD);
316     res = LocalUnlock(gbl);    /* #1 */
317     ok(res, "returned %d with %d (expected '!= 0')\n", res, GetLastError());
318     SetLastError(MAGIC_DEAD);
319     flags = LocalFlags(gbl);
320     ok( flags , "returned 0x%04x with %d (expected '!= 0')\n",
321         flags, GetLastError());
322
323     SetLastError(MAGIC_DEAD);
324     res = LocalUnlock(gbl);    /* #0 */
325     /* NT: ERROR_SUCCESS (documented on MSDN), 9x: untouched */
326     ok(!res && ((GetLastError() == ERROR_SUCCESS) || (GetLastError() == MAGIC_DEAD)),
327         "returned %d with %d (expected '0' with: ERROR_SUCCESS or "
328         "MAGIC_DEAD)\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     /* Unlock an already unlocked Handle */
335     SetLastError(MAGIC_DEAD);
336     res = LocalUnlock(gbl);
337     /* NT: ERROR_NOT_LOCKED,  9x: untouched */
338     ok( !res &&
339         ((GetLastError() == ERROR_NOT_LOCKED) || (GetLastError() == MAGIC_DEAD)),
340         "returned %d with %d (expected '0' with: ERROR_NOT_LOCKED or "
341         "MAGIC_DEAD)\n", res, GetLastError());
342
343     LocalFree(gbl);
344     /* invalid handles are caught in windows: */
345     SetLastError(MAGIC_DEAD);
346     hsecond = LocalFree(gbl);       /* invalid handle: free memory twice */
347     ok( (hsecond == gbl) && (GetLastError() == ERROR_INVALID_HANDLE),
348         "returned %p with 0x%08x (expected %p with ERROR_INVALID_HANDLE)\n",
349         hsecond, GetLastError(), gbl);
350     SetLastError(MAGIC_DEAD);
351     flags = LocalFlags(gbl);
352     ok( (flags == LMEM_INVALID_HANDLE) && (GetLastError() == ERROR_INVALID_HANDLE),
353         "returned 0x%04x with 0x%08x (expected LMEM_INVALID_HANDLE with "
354         "ERROR_INVALID_HANDLE)\n", flags, GetLastError());
355     SetLastError(MAGIC_DEAD);
356     size = LocalSize(gbl);
357     ok( (size == 0) && (GetLastError() == ERROR_INVALID_HANDLE),
358         "returned %ld with 0x%08x (expected '0' with ERROR_INVALID_HANDLE)\n",
359         size, GetLastError());
360
361     SetLastError(MAGIC_DEAD);
362     mem = LocalLock(gbl);
363     ok( (mem == NULL) && (GetLastError() == ERROR_INVALID_HANDLE),
364         "returned %p with 0x%08x (expected NULL with ERROR_INVALID_HANDLE)\n",
365         mem, GetLastError());
366
367     /* This Test works the same on all Systems (GlobalUnlock() is different) */
368     SetLastError(MAGIC_DEAD);
369     res = LocalUnlock(gbl);
370     ok(!res && (GetLastError() == ERROR_INVALID_HANDLE),
371         "returned %d with %d (expected '0' with ERROR_INVALID_HANDLE)\n",
372         res, GetLastError());
373
374     /* trying to lock empty memory should give an error */
375     gbl = GlobalAlloc(GMEM_MOVEABLE|GMEM_ZEROINIT,0);
376     ok(gbl != NULL, "returned NULL\n");
377     SetLastError(MAGIC_DEAD);
378     mem = GlobalLock(gbl);
379     /* NT: ERROR_DISCARDED,  9x: untouched */
380     ok( (mem == NULL) &&
381         ((GetLastError() == ERROR_DISCARDED) || (GetLastError() == MAGIC_DEAD)),
382         "returned %p with 0x%x/%d (expected 'NULL' with: ERROR_DISCARDED or "
383         "MAGIC_DEAD)\n", mem, GetLastError(), GetLastError());
384
385     GlobalFree(gbl);
386 }
387
388 static void test_obsolete_flags(void)
389 {
390     static struct {
391         UINT flags;
392         UINT globalflags;
393     } test_global_flags[] = {
394         {GMEM_FIXED | GMEM_NOTIFY, 0},
395         {GMEM_FIXED | GMEM_DISCARDABLE, 0},
396         {GMEM_MOVEABLE | GMEM_NOTIFY, 0},
397         {GMEM_MOVEABLE | GMEM_DDESHARE, GMEM_DDESHARE},
398         {GMEM_MOVEABLE | GMEM_NOT_BANKED, 0},
399         {GMEM_MOVEABLE | GMEM_NODISCARD, 0},
400         {GMEM_MOVEABLE | GMEM_DISCARDABLE, GMEM_DISCARDABLE},
401         {GMEM_MOVEABLE | GMEM_DDESHARE | GMEM_DISCARDABLE | GMEM_LOWER | GMEM_NOCOMPACT | GMEM_NODISCARD |
402          GMEM_NOT_BANKED | GMEM_NOTIFY, GMEM_DDESHARE | GMEM_DISCARDABLE},
403     };
404
405     unsigned int i;
406     HGLOBAL gbl;
407     UINT resultflags;
408
409     UINT (WINAPI *pGlobalFlags)(HGLOBAL);
410
411     pGlobalFlags = (void *) GetProcAddress(GetModuleHandleA("kernel32"), "GlobalFlags");
412
413     if (!pGlobalFlags)
414     {
415         win_skip("GlobalFlags is not available\n");
416         return;
417     }
418
419     for (i = 0; i < sizeof(test_global_flags)/sizeof(test_global_flags[0]); i++)
420     {
421         gbl = GlobalAlloc(test_global_flags[i].flags, 4);
422         ok(gbl != NULL, "GlobalAlloc failed\n");
423
424         SetLastError(MAGIC_DEAD);
425         resultflags = pGlobalFlags(gbl);
426
427         ok( resultflags == test_global_flags[i].globalflags ||
428             broken(resultflags == (test_global_flags[i].globalflags & ~GMEM_DDESHARE)), /* win9x */
429             "%u: expected 0x%08x, but returned 0x%08x with %d\n",
430             i, test_global_flags[i].globalflags, resultflags, GetLastError() );
431
432         GlobalFree(gbl);
433     }
434 }
435
436 static void test_HeapQueryInformation(void)
437 {
438     ULONG info;
439     SIZE_T size;
440     BOOL ret;
441
442     pHeapQueryInformation = (void *)GetProcAddress(GetModuleHandle("kernel32.dll"), "HeapQueryInformation");
443     if (!pHeapQueryInformation)
444     {
445         win_skip("HeapQueryInformation is not available\n");
446         return;
447     }
448
449     if (0) /* crashes under XP */
450     {
451         size = 0;
452         ret = pHeapQueryInformation(0,
453                                 HeapCompatibilityInformation,
454                                 &info, sizeof(info), &size);
455         size = 0;
456         ret = pHeapQueryInformation(GetProcessHeap(),
457                                 HeapCompatibilityInformation,
458                                 NULL, sizeof(info), &size);
459     }
460
461     size = 0;
462     SetLastError(0xdeadbeef);
463     ret = pHeapQueryInformation(GetProcessHeap(),
464                                 HeapCompatibilityInformation,
465                                 NULL, 0, &size);
466     ok(!ret, "HeapQueryInformation should fail\n");
467     ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
468        "expected ERROR_INSUFFICIENT_BUFFER got %u\n", GetLastError());
469     ok(size == sizeof(ULONG), "expected 4, got %lu\n", size);
470
471     SetLastError(0xdeadbeef);
472     ret = pHeapQueryInformation(GetProcessHeap(),
473                                 HeapCompatibilityInformation,
474                                 NULL, 0, NULL);
475     ok(!ret, "HeapQueryInformation should fail\n");
476     ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
477        "expected ERROR_INSUFFICIENT_BUFFER got %u\n", GetLastError());
478
479     info = 0xdeadbeaf;
480     SetLastError(0xdeadbeef);
481     ret = pHeapQueryInformation(GetProcessHeap(),
482                                 HeapCompatibilityInformation,
483                                 &info, sizeof(info) + 1, NULL);
484     ok(ret, "HeapQueryInformation error %u\n", GetLastError());
485     ok(info == 0 || info == 1 || info == 2, "expected 0, 1 or 2, got %u\n", info);
486 }
487
488 static void test_heap_checks( DWORD flags )
489 {
490     BYTE old, *p, *p2;
491     BOOL ret;
492     SIZE_T i, size, large_size = 3000 * 1024 + 37;
493
494     if (flags & HEAP_PAGE_ALLOCS) return;  /* no tests for that case yet */
495     trace( "testing heap flags %08x\n", flags );
496
497     p = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, 17 );
498     ok( p != NULL, "HeapAlloc failed\n" );
499
500     ret = HeapValidate( GetProcessHeap(), 0, p );
501     ok( ret, "HeapValidate failed\n" );
502
503     size = HeapSize( GetProcessHeap(), 0, p );
504     ok( size == 17, "Wrong size %lu\n", size );
505
506     ok( p[14] == 0, "wrong data %x\n", p[14] );
507     ok( p[15] == 0, "wrong data %x\n", p[15] );
508     ok( p[16] == 0, "wrong data %x\n", p[16] );
509
510     if (flags & HEAP_TAIL_CHECKING_ENABLED)
511     {
512         ok( p[17] == 0xab, "wrong padding %x\n", p[17] );
513         ok( p[18] == 0xab, "wrong padding %x\n", p[18] );
514         ok( p[19] == 0xab, "wrong padding %x\n", p[19] );
515     }
516
517     p2 = HeapReAlloc( GetProcessHeap(), HEAP_REALLOC_IN_PLACE_ONLY, p, 14 );
518     if (p2 == p)
519     {
520         if (flags & HEAP_TAIL_CHECKING_ENABLED)
521         {
522             ok( p[14] == 0xab, "wrong padding %x\n", p[14] );
523             ok( p[15] == 0xab, "wrong padding %x\n", p[15] );
524             ok( p[16] == 0xab, "wrong padding %x\n", p[16] );
525         }
526         else
527         {
528             ok( p[14] == 0, "wrong padding %x\n", p[14] );
529             ok( p[15] == 0, "wrong padding %x\n", p[15] );
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 || broken(size == ~0u), "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->pattern == 0xffeeffee)  /* vista layout */
755     {
756         ok( heap->flags == 0, "%s: got heap flags %08x expected 0\n", arg, heap->flags );
757     }
758     else if (heap->pattern == 0xeeeeeeee && heap->flags == 0xeeeeeeee)
759     {
760         ok( expected & FLG_HEAP_PAGE_ALLOCS, "%s: got heap flags 0xeeeeeeee without page alloc\n", arg );
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         expect_heap = heap->flags;
769     }
770
771     test_heap_checks( expect_heap );
772 }
773
774 START_TEST(heap)
775 {
776     int argc;
777     char **argv;
778
779     pRtlGetNtGlobalFlags = (void *)GetProcAddress( GetModuleHandleA("ntdll.dll"), "RtlGetNtGlobalFlags" );
780
781     argc = winetest_get_mainargs( &argv );
782     if (argc >= 3)
783     {
784         test_child_heap( argv[2] );
785         return;
786     }
787
788     test_heap();
789     test_obsolete_flags();
790
791     /* Test both short and very long blocks */
792     test_sized_HeapAlloc(1);
793     test_sized_HeapAlloc(1 << 20);
794     test_sized_HeapReAlloc(1, 100);
795     test_sized_HeapReAlloc(1, (1 << 20));
796     test_sized_HeapReAlloc((1 << 20), (2 << 20));
797     test_sized_HeapReAlloc((1 << 20), 1);
798     test_HeapQueryInformation();
799
800     if (pRtlGetNtGlobalFlags)
801     {
802         test_debug_heap( argv[0], 0 );
803         test_debug_heap( argv[0], FLG_HEAP_ENABLE_TAIL_CHECK );
804         test_debug_heap( argv[0], FLG_HEAP_ENABLE_FREE_CHECK );
805         test_debug_heap( argv[0], FLG_HEAP_VALIDATE_PARAMETERS );
806         test_debug_heap( argv[0], FLG_HEAP_VALIDATE_ALL );
807         test_debug_heap( argv[0], FLG_POOL_ENABLE_TAGGING );
808         test_debug_heap( argv[0], FLG_HEAP_ENABLE_TAGGING );
809         test_debug_heap( argv[0], FLG_HEAP_ENABLE_TAG_BY_DLL );
810         test_debug_heap( argv[0], FLG_HEAP_DISABLE_COALESCING );
811         test_debug_heap( argv[0], FLG_HEAP_PAGE_ALLOCS );
812         test_debug_heap( argv[0], 0xdeadbeef );
813     }
814     else win_skip( "RtlGetNtGlobalFlags not found, skipping heap debug tests\n" );
815 }