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