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