2 * Unit test suite for heap functions
4 * Copyright 2003 Dimitrie O. Paun
5 * Copyright 2006 Detlef Riekenberg
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.
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.
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
26 #define WIN32_NO_STATUS
28 #define NTOS_MODE_USER
29 #include <ndk/ntndk.h>
31 #include "wine/test.h"
34 #define MAGIC_DEAD 0xdeadbeef
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
42 static BOOL (WINAPI
*pHeapQueryInformation
)(HANDLE
, HEAP_INFORMATION_CLASS
, PVOID
, SIZE_T
, PSIZE_T
);
43 static ULONG (WINAPI
*pRtlGetNtGlobalFlags
)(void);
53 static SIZE_T
resize_9x(SIZE_T size
)
55 DWORD dwSizeAligned
= (size
+ 3) & ~3;
56 return max(dwSizeAligned
, 12); /* at least 12 bytes */
59 static void test_sized_HeapAlloc(int nbytes
)
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");
69 static void test_sized_HeapReAlloc(int nbytes1
, int nbytes2
)
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");
82 static void test_heap(void)
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
);
97 mem
= HeapReAlloc(GetProcessHeap(), 0, NULL
, 10);
98 ok(mem
== NULL
, "memory allocated by HeapReAlloc\n");
100 for (size
= 0; size
<= 256; 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
);
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");
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
);
128 /* Global*() functions */
129 gbl
= GlobalAlloc(GMEM_MOVEABLE
, 0);
130 ok(gbl
!= NULL
, "global memory not allocated for size 0\n");
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
);
137 gbl
= GlobalReAlloc(gbl
, 0, GMEM_MOVEABLE
);
138 ok(gbl
!= NULL
, "GlobalReAlloc should not fail on size 0\n");
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
);
146 gbl
= GlobalReAlloc(0, 10, GMEM_MOVEABLE
);
147 ok(gbl
== NULL
, "global realloc allocated memory\n");
149 /* GlobalLock / GlobalUnlock with a valid handle */
150 gbl
= GlobalAlloc(GMEM_MOVEABLE
, 256);
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());
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
);
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());
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());
189 /* Unlock an already unlocked Handle */
190 SetLastError(MAGIC_DEAD
);
191 res
= GlobalUnlock(gbl
);
192 /* NT: ERROR_NOT_LOCKED, 9x: untouched */
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());
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());
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());
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());
232 gbl
= GlobalAlloc(GMEM_DDESHARE
, 100);
235 mem
= GlobalFree(gbl
);
236 ok(mem
== NULL
, "Expected NULL, got %p\n", mem
);
239 if (sizeof(void *) != 8) /* crashes on 64-bit Vista */
241 SetLastError(MAGIC_DEAD
);
242 mem
= GlobalFree(gbl
);
243 ok(mem
== gbl
|| broken(mem
== NULL
) /* nt4 */, "Expected gbl, got %p\n", mem
);
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());
250 gbl
= GlobalAlloc(GMEM_DDESHARE
, 100);
252 res
= GlobalUnlock(gbl
);
254 res
== 0, /* win9x */
255 "Expected 1 or 0, got %d\n", res
);
257 res
= GlobalUnlock(gbl
);
259 res
== 0, /* win9x */
260 "Expected 1 or 0, got %d\n", res
);
262 /* GlobalSize on an invalid handle */
263 if (sizeof(void *) != 8) /* crashes on 64-bit Vista */
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());
275 /* ####################################### */
276 /* Local*() functions */
277 gbl
= LocalAlloc(LMEM_MOVEABLE
, 0);
278 ok(gbl
!= NULL
, "local memory not allocated for size 0\n");
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
);
285 gbl
= LocalReAlloc(gbl
, 0, LMEM_MOVEABLE
);
286 ok(gbl
!= NULL
, "LocalReAlloc should not fail on size 0\n");
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
);
294 gbl
= LocalReAlloc(0, 10, LMEM_MOVEABLE
);
295 ok(gbl
== NULL
, "local realloc allocated memory\n");
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());
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
);
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());
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());
336 /* Unlock an already unlocked Handle */
337 SetLastError(MAGIC_DEAD
);
338 res
= LocalUnlock(gbl
);
339 /* NT: ERROR_NOT_LOCKED, 9x: untouched */
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());
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());
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());
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());
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 */
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());
390 static void test_obsolete_flags(void)
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
},
411 UINT (WINAPI
*pGlobalFlags
)(HGLOBAL
);
413 pGlobalFlags
= (void *) GetProcAddress(GetModuleHandleA("kernel32"), "GlobalFlags");
417 win_skip("GlobalFlags is not available\n");
421 for (i
= 0; i
< sizeof(test_global_flags
)/sizeof(test_global_flags
[0]); i
++)
423 gbl
= GlobalAlloc(test_global_flags
[i
].flags
, 4);
424 ok(gbl
!= NULL
, "GlobalAlloc failed\n");
426 SetLastError(MAGIC_DEAD
);
427 resultflags
= pGlobalFlags(gbl
);
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() );
438 static void test_HeapQueryInformation(void)
444 pHeapQueryInformation
= (void *)GetProcAddress(GetModuleHandle("kernel32.dll"), "HeapQueryInformation");
445 if (!pHeapQueryInformation
)
447 win_skip("HeapQueryInformation is not available\n");
451 if (0) /* crashes under XP */
454 ret
= pHeapQueryInformation(0,
455 HeapCompatibilityInformation
,
456 &info
, sizeof(info
), &size
);
458 ret
= pHeapQueryInformation(GetProcessHeap(),
459 HeapCompatibilityInformation
,
460 NULL
, sizeof(info
), &size
);
464 SetLastError(0xdeadbeef);
465 ret
= pHeapQueryInformation(GetProcessHeap(),
466 HeapCompatibilityInformation
,
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
);
473 SetLastError(0xdeadbeef);
474 ret
= pHeapQueryInformation(GetProcessHeap(),
475 HeapCompatibilityInformation
,
477 ok(!ret
, "HeapQueryInformation should fail\n");
478 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
479 "expected ERROR_INSUFFICIENT_BUFFER got %u\n", GetLastError());
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
);
490 static void test_heap_checks( DWORD flags
)
494 SIZE_T i
, size
, large_size
= 3000 * 1024 + 37;
496 if (flags
& HEAP_PAGE_ALLOCS
) return; /* no tests for that case yet */
497 trace( "testing heap flags %08x\n", flags
);
499 p
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, 17 );
500 ok( p
!= NULL
, "HeapAlloc failed\n" );
502 ret
= HeapValidate( GetProcessHeap(), 0, p
);
503 ok( ret
, "HeapValidate failed\n" );
505 size
= HeapSize( GetProcessHeap(), 0, p
);
506 ok( size
== 17, "Wrong size %lu\n", size
);
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] );
512 if (flags
& HEAP_TAIL_CHECKING_ENABLED
)
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] );
519 p2
= HeapReAlloc( GetProcessHeap(), HEAP_REALLOC_IN_PLACE_ONLY
, p
, 14 );
522 if (flags
& HEAP_TAIL_CHECKING_ENABLED
)
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] );
530 ok( p
[14] == 0, "wrong padding %x\n", p
[14] );
531 ok( p
[15] == 0, "wrong padding %x\n", p
[15] );
534 else skip( "realloc in place failed\n ");
536 ret
= HeapFree( GetProcessHeap(), 0, p
);
537 ok( ret
, "HeapFree failed\n" );
539 p
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, 17 );
540 ok( p
!= NULL
, "HeapAlloc failed\n" );
544 if (flags
& HEAP_TAIL_CHECKING_ENABLED
)
546 ret
= HeapValidate( GetProcessHeap(), 0, p
);
547 ok( !ret
, "HeapValidate succeeded\n" );
549 /* other calls only check when HEAP_VALIDATE is set */
550 if (flags
& HEAP_VALIDATE
)
552 size
= HeapSize( GetProcessHeap(), 0, p
);
553 ok( size
== ~(SIZE_T
)0 || broken(size
== ~0u), "Wrong size %lu\n", size
);
555 p2
= HeapReAlloc( GetProcessHeap(), 0, p
, 14 );
556 ok( p2
== NULL
, "HeapReAlloc succeeded\n" );
558 ret
= HeapFree( GetProcessHeap(), 0, p
);
559 ok( !ret
|| broken(sizeof(void*) == 8), /* not caught on xp64 */
560 "HeapFree succeeded\n" );
564 size
= HeapSize( GetProcessHeap(), 0, p
);
565 ok( size
== 17, "Wrong size %lu\n", size
);
567 p2
= HeapReAlloc( GetProcessHeap(), 0, p
, 14 );
568 ok( p2
!= NULL
, "HeapReAlloc failed\n" );
572 ret
= HeapFree( GetProcessHeap(), 0, p
);
573 ok( ret
, "HeapFree failed\n" );
575 p
= HeapAlloc( GetProcessHeap(), 0, 37 );
576 ok( p
!= NULL
, "HeapAlloc failed\n" );
577 memset( p
, 0xcc, 37 );
579 ret
= HeapFree( GetProcessHeap(), 0, p
);
580 ok( ret
, "HeapFree failed\n" );
582 if (flags
& HEAP_FREE_CHECKING_ENABLED
)
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] );
589 ret
= HeapValidate( GetProcessHeap(), 0, NULL
);
590 ok( ret
, "HeapValidate failed\n" );
594 ret
= HeapValidate( GetProcessHeap(), 0, NULL
);
595 ok( !ret
, "HeapValidate succeeded\n" );
598 ret
= HeapValidate( GetProcessHeap(), 0, NULL
);
599 ok( ret
, "HeapValidate failed\n" );
602 /* now test large blocks */
604 p
= HeapAlloc( GetProcessHeap(), 0, large_size
);
605 ok( p
!= NULL
, "HeapAlloc failed\n" );
607 ret
= HeapValidate( GetProcessHeap(), 0, p
);
608 ok( ret
, "HeapValidate failed\n" );
610 size
= HeapSize( GetProcessHeap(), 0, p
);
611 ok( size
== large_size
, "Wrong size %lu\n", size
);
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] );
616 if (flags
& HEAP_TAIL_CHECKING_ENABLED
)
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)
624 p
[large_size
] = 0xcc;
625 ret
= HeapValidate( GetProcessHeap(), 0, p
);
626 ok( !ret
, "HeapValidate succeeded\n" );
628 /* other calls only check when HEAP_VALIDATE is set */
629 if (flags
& HEAP_VALIDATE
)
631 size
= HeapSize( GetProcessHeap(), 0, p
);
632 ok( size
== ~(SIZE_T
)0, "Wrong size %lu\n", size
);
634 p2
= HeapReAlloc( GetProcessHeap(), 0, p
, large_size
- 3 );
635 ok( p2
== NULL
, "HeapReAlloc succeeded\n" );
637 ret
= HeapFree( GetProcessHeap(), 0, p
);
638 ok( !ret
, "HeapFree succeeded\n" );
640 p
[large_size
] = 0xab;
644 ret
= HeapFree( GetProcessHeap(), 0, p
);
645 ok( ret
, "HeapFree failed\n" );
647 /* test block sizes when tail checking */
648 if (flags
& HEAP_TAIL_CHECKING_ENABLED
)
650 for (size
= 0; size
< 64; size
++)
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
);
660 static void test_debug_heap( const char *argv0
, DWORD flags
)
662 char keyname
[MAX_PATH
];
663 char buffer
[MAX_PATH
];
664 PROCESS_INFORMATION info
;
665 STARTUPINFOA startup
;
669 const char *basename
;
671 if ((basename
= strrchr( argv0
, '\\' ))) basename
++;
672 else basename
= argv0
;
674 sprintf( keyname
, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Image File Execution Options\\%s",
676 if (!strcmp( keyname
+ strlen(keyname
) - 3, ".so" )) keyname
[strlen(keyname
) - 3] = 0;
678 err
= RegCreateKeyA( HKEY_LOCAL_MACHINE
, keyname
, &hkey
);
679 ok( !err
, "failed to create '%s' error %u\n", keyname
, err
);
682 if (flags
== 0xdeadbeef) /* magic value for unsetting it */
683 RegDeleteValueA( hkey
, "GlobalFlag" );
685 RegSetValueExA( hkey
, "GlobalFlag", 0, REG_DWORD
, (BYTE
*)&flags
, sizeof(flags
) );
687 memset( &startup
, 0, sizeof(startup
) );
688 startup
.cb
= sizeof(startup
);
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() );
695 winetest_wait_child_process( info
.hProcess
);
696 CloseHandle( info
.hThread
);
697 CloseHandle( info
.hProcess
);
699 RegDeleteValueA( hkey
, "GlobalFlag" );
701 RegDeleteKeyA( HKEY_LOCAL_MACHINE
, keyname
);
704 static DWORD
heap_flags_from_global_flag( DWORD flag
)
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
;
723 static void test_child_heap( const char *arg
)
725 struct heap_layout
*heap
= GetProcessHeap();
726 DWORD expected
= strtoul( arg
, 0, 16 );
729 if (expected
== 0xdeadbeef) /* expected value comes from Session Manager global flags */
733 if (!RegOpenKeyA( HKEY_LOCAL_MACHINE
, "SYSTEM\\CurrentControlSet\\Control\\Session Manager", &hkey
))
736 DWORD type
, size
= sizeof(buffer
);
738 if (!RegQueryValueExA( hkey
, "GlobalFlag", 0, &type
, (BYTE
*)buffer
, &size
))
740 if (type
== REG_DWORD
) expected
= *(DWORD
*)buffer
;
741 else if (type
== REG_SZ
) expected
= strtoul( buffer
, 0, 16 );
746 if (expected
&& !pRtlGetNtGlobalFlags()) /* not working on NT4 */
748 win_skip( "global flags not set\n" );
752 ok( pRtlGetNtGlobalFlags() == expected
,
753 "%s: got global flags %08x expected %08x\n", arg
, pRtlGetNtGlobalFlags(), expected
);
755 expect_heap
= heap_flags_from_global_flag( expected
);
757 if (!(heap
->flags
& HEAP_GROWABLE
) || heap
->pattern
== 0xffeeffee) /* vista layout */
759 ok( heap
->flags
== 0, "%s: got heap flags %08x expected 0\n", arg
, heap
->flags
);
761 else if (heap
->pattern
== 0xeeeeeeee && heap
->flags
== 0xeeeeeeee)
763 ok( expected
& FLG_HEAP_PAGE_ALLOCS
, "%s: got heap flags 0xeeeeeeee without page alloc\n", arg
);
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
;
774 test_heap_checks( expect_heap
);
782 pRtlGetNtGlobalFlags
= (void *)GetProcAddress( GetModuleHandleA("ntdll.dll"), "RtlGetNtGlobalFlags" );
784 argc
= winetest_get_mainargs( &argv
);
787 test_child_heap( argv
[2] );
792 test_obsolete_flags();
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();
803 if (pRtlGetNtGlobalFlags
)
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 );
817 else win_skip( "RtlGetNtGlobalFlags not found, skipping heap debug tests\n" );