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
30 #include "wine/test.h"
32 #define MAGIC_DEAD 0xdeadbeef
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
40 static BOOL (WINAPI
*pHeapQueryInformation
)(HANDLE
, HEAP_INFORMATION_CLASS
, PVOID
, SIZE_T
, PSIZE_T
);
41 static ULONG (WINAPI
*pRtlGetNtGlobalFlags
)(void);
51 static SIZE_T
resize_9x(SIZE_T size
)
53 DWORD dwSizeAligned
= (size
+ 3) & ~3;
54 return max(dwSizeAligned
, 12); /* at least 12 bytes */
57 static void test_sized_HeapAlloc(int nbytes
)
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");
67 static void test_sized_HeapReAlloc(int nbytes1
, int nbytes2
)
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");
80 static void test_heap(void)
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
);
95 mem
= HeapReAlloc(GetProcessHeap(), 0, NULL
, 10);
96 ok(mem
== NULL
, "memory allocated by HeapReAlloc\n");
98 for (size
= 0; size
<= 256; 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
);
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");
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
);
126 /* Global*() functions */
127 gbl
= GlobalAlloc(GMEM_MOVEABLE
, 0);
128 ok(gbl
!= NULL
, "global memory not allocated for size 0\n");
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
);
135 gbl
= GlobalReAlloc(gbl
, 0, GMEM_MOVEABLE
);
136 ok(gbl
!= NULL
, "GlobalReAlloc should not fail on size 0\n");
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
);
144 gbl
= GlobalReAlloc(0, 10, GMEM_MOVEABLE
);
145 ok(gbl
== NULL
, "global realloc allocated memory\n");
147 /* GlobalLock / GlobalUnlock with a valid handle */
148 gbl
= GlobalAlloc(GMEM_MOVEABLE
, 256);
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());
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
);
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());
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());
187 /* Unlock an already unlocked Handle */
188 SetLastError(MAGIC_DEAD
);
189 res
= GlobalUnlock(gbl
);
190 /* NT: ERROR_NOT_LOCKED, 9x: untouched */
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());
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());
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());
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());
230 gbl
= GlobalAlloc(GMEM_DDESHARE
, 100);
233 mem
= GlobalFree(gbl
);
234 ok(mem
== NULL
, "Expected NULL, got %p\n", mem
);
237 if (sizeof(void *) != 8) /* crashes on 64-bit Vista */
239 SetLastError(MAGIC_DEAD
);
240 mem
= GlobalFree(gbl
);
241 ok(mem
== gbl
|| broken(mem
== NULL
) /* nt4 */, "Expected gbl, got %p\n", mem
);
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());
248 gbl
= GlobalAlloc(GMEM_DDESHARE
, 100);
250 res
= GlobalUnlock(gbl
);
252 res
== 0, /* win9x */
253 "Expected 1 or 0, got %d\n", res
);
255 res
= GlobalUnlock(gbl
);
257 res
== 0, /* win9x */
258 "Expected 1 or 0, got %d\n", res
);
260 /* GlobalSize on an invalid handle */
261 if (sizeof(void *) != 8) /* crashes on 64-bit Vista */
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());
273 /* ####################################### */
274 /* Local*() functions */
275 gbl
= LocalAlloc(LMEM_MOVEABLE
, 0);
276 ok(gbl
!= NULL
, "local memory not allocated for size 0\n");
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
);
283 gbl
= LocalReAlloc(gbl
, 0, LMEM_MOVEABLE
);
284 ok(gbl
!= NULL
, "LocalReAlloc should not fail on size 0\n");
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
);
292 gbl
= LocalReAlloc(0, 10, LMEM_MOVEABLE
);
293 ok(gbl
== NULL
, "local realloc allocated memory\n");
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());
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
);
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());
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());
334 /* Unlock an already unlocked Handle */
335 SetLastError(MAGIC_DEAD
);
336 res
= LocalUnlock(gbl
);
337 /* NT: ERROR_NOT_LOCKED, 9x: untouched */
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());
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());
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());
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());
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 */
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());
388 static void test_obsolete_flags(void)
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
},
409 UINT (WINAPI
*pGlobalFlags
)(HGLOBAL
);
411 pGlobalFlags
= (void *) GetProcAddress(GetModuleHandleA("kernel32"), "GlobalFlags");
415 win_skip("GlobalFlags is not available\n");
419 for (i
= 0; i
< sizeof(test_global_flags
)/sizeof(test_global_flags
[0]); i
++)
421 gbl
= GlobalAlloc(test_global_flags
[i
].flags
, 4);
422 ok(gbl
!= NULL
, "GlobalAlloc failed\n");
424 SetLastError(MAGIC_DEAD
);
425 resultflags
= pGlobalFlags(gbl
);
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() );
436 static void test_HeapQueryInformation(void)
442 pHeapQueryInformation
= (void *)GetProcAddress(GetModuleHandle("kernel32.dll"), "HeapQueryInformation");
443 if (!pHeapQueryInformation
)
445 win_skip("HeapQueryInformation is not available\n");
449 if (0) /* crashes under XP */
452 ret
= pHeapQueryInformation(0,
453 HeapCompatibilityInformation
,
454 &info
, sizeof(info
), &size
);
456 ret
= pHeapQueryInformation(GetProcessHeap(),
457 HeapCompatibilityInformation
,
458 NULL
, sizeof(info
), &size
);
462 SetLastError(0xdeadbeef);
463 ret
= pHeapQueryInformation(GetProcessHeap(),
464 HeapCompatibilityInformation
,
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
);
471 SetLastError(0xdeadbeef);
472 ret
= pHeapQueryInformation(GetProcessHeap(),
473 HeapCompatibilityInformation
,
475 ok(!ret
, "HeapQueryInformation should fail\n");
476 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
477 "expected ERROR_INSUFFICIENT_BUFFER got %u\n", GetLastError());
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
);
488 static void test_heap_checks( DWORD flags
)
492 SIZE_T i
, size
, large_size
= 3000 * 1024 + 37;
494 if (flags
& HEAP_PAGE_ALLOCS
) return; /* no tests for that case yet */
495 trace( "testing heap flags %08x\n", flags
);
497 p
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, 17 );
498 ok( p
!= NULL
, "HeapAlloc failed\n" );
500 ret
= HeapValidate( GetProcessHeap(), 0, p
);
501 ok( ret
, "HeapValidate failed\n" );
503 size
= HeapSize( GetProcessHeap(), 0, p
);
504 ok( size
== 17, "Wrong size %lu\n", size
);
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] );
510 if (flags
& HEAP_TAIL_CHECKING_ENABLED
)
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] );
517 p2
= HeapReAlloc( GetProcessHeap(), HEAP_REALLOC_IN_PLACE_ONLY
, p
, 14 );
520 if (flags
& HEAP_TAIL_CHECKING_ENABLED
)
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] );
528 ok( p
[14] == 0, "wrong padding %x\n", p
[14] );
529 ok( p
[15] == 0, "wrong padding %x\n", p
[15] );
532 else skip( "realloc in place failed\n");
534 ret
= HeapFree( GetProcessHeap(), 0, p
);
535 ok( ret
, "HeapFree failed\n" );
537 p
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, 17 );
538 ok( p
!= NULL
, "HeapAlloc failed\n" );
542 if (flags
& HEAP_TAIL_CHECKING_ENABLED
)
544 ret
= HeapValidate( GetProcessHeap(), 0, p
);
545 ok( !ret
, "HeapValidate succeeded\n" );
547 /* other calls only check when HEAP_VALIDATE is set */
548 if (flags
& HEAP_VALIDATE
)
550 size
= HeapSize( GetProcessHeap(), 0, p
);
551 ok( size
== ~(SIZE_T
)0 || broken(size
== ~0u), "Wrong size %lu\n", size
);
553 p2
= HeapReAlloc( GetProcessHeap(), 0, p
, 14 );
554 ok( p2
== NULL
, "HeapReAlloc succeeded\n" );
556 ret
= HeapFree( GetProcessHeap(), 0, p
);
557 ok( !ret
|| broken(sizeof(void*) == 8), /* not caught on xp64 */
558 "HeapFree succeeded\n" );
562 size
= HeapSize( GetProcessHeap(), 0, p
);
563 ok( size
== 17, "Wrong size %lu\n", size
);
565 p2
= HeapReAlloc( GetProcessHeap(), 0, p
, 14 );
566 ok( p2
!= NULL
, "HeapReAlloc failed\n" );
570 ret
= HeapFree( GetProcessHeap(), 0, p
);
571 ok( ret
, "HeapFree failed\n" );
573 p
= HeapAlloc( GetProcessHeap(), 0, 37 );
574 ok( p
!= NULL
, "HeapAlloc failed\n" );
575 memset( p
, 0xcc, 37 );
577 ret
= HeapFree( GetProcessHeap(), 0, p
);
578 ok( ret
, "HeapFree failed\n" );
580 if (flags
& HEAP_FREE_CHECKING_ENABLED
)
582 ok( p
[16] == 0xee, "wrong data %x\n", p
[16] );
583 ok( p
[17] == 0xfe, "wrong data %x\n", p
[17] );
584 ok( p
[18] == 0xee, "wrong data %x\n", p
[18] );
585 ok( p
[19] == 0xfe, "wrong data %x\n", p
[19] );
587 ret
= HeapValidate( GetProcessHeap(), 0, NULL
);
588 ok( ret
, "HeapValidate failed\n" );
592 ret
= HeapValidate( GetProcessHeap(), 0, NULL
);
593 ok( !ret
, "HeapValidate succeeded\n" );
596 ret
= HeapValidate( GetProcessHeap(), 0, NULL
);
597 ok( ret
, "HeapValidate failed\n" );
600 /* now test large blocks */
602 p
= HeapAlloc( GetProcessHeap(), 0, large_size
);
603 ok( p
!= NULL
, "HeapAlloc failed\n" );
605 ret
= HeapValidate( GetProcessHeap(), 0, p
);
606 ok( ret
, "HeapValidate failed\n" );
608 size
= HeapSize( GetProcessHeap(), 0, p
);
609 ok( size
== large_size
, "Wrong size %lu\n", size
);
611 ok( p
[large_size
- 2] == 0, "wrong data %x\n", p
[large_size
- 2] );
612 ok( p
[large_size
- 1] == 0, "wrong data %x\n", p
[large_size
- 1] );
614 if (flags
& HEAP_TAIL_CHECKING_ENABLED
)
616 /* Windows doesn't do tail checking on large blocks */
617 ok( p
[large_size
] == 0xab || broken(p
[large_size
] == 0), "wrong data %x\n", p
[large_size
] );
618 ok( p
[large_size
+1] == 0xab || broken(p
[large_size
+1] == 0), "wrong data %x\n", p
[large_size
+1] );
619 ok( p
[large_size
+2] == 0xab || broken(p
[large_size
+2] == 0), "wrong data %x\n", p
[large_size
+2] );
620 if (p
[large_size
] == 0xab)
622 p
[large_size
] = 0xcc;
623 ret
= HeapValidate( GetProcessHeap(), 0, p
);
624 ok( !ret
, "HeapValidate succeeded\n" );
626 /* other calls only check when HEAP_VALIDATE is set */
627 if (flags
& HEAP_VALIDATE
)
629 size
= HeapSize( GetProcessHeap(), 0, p
);
630 ok( size
== ~(SIZE_T
)0, "Wrong size %lu\n", size
);
632 p2
= HeapReAlloc( GetProcessHeap(), 0, p
, large_size
- 3 );
633 ok( p2
== NULL
, "HeapReAlloc succeeded\n" );
635 ret
= HeapFree( GetProcessHeap(), 0, p
);
636 ok( !ret
, "HeapFree succeeded\n" );
638 p
[large_size
] = 0xab;
642 ret
= HeapFree( GetProcessHeap(), 0, p
);
643 ok( ret
, "HeapFree failed\n" );
645 /* test block sizes when tail checking */
646 if (flags
& HEAP_TAIL_CHECKING_ENABLED
)
648 for (size
= 0; size
< 64; size
++)
650 p
= HeapAlloc( GetProcessHeap(), 0, size
);
651 for (i
= 0; i
< 32; i
++) if (p
[size
+ i
] != 0xab) break;
652 ok( i
>= 8, "only %lu tail bytes for size %lu\n", i
, size
);
653 HeapFree( GetProcessHeap(), 0, p
);
658 static void test_debug_heap( const char *argv0
, DWORD flags
)
660 char keyname
[MAX_PATH
];
661 char buffer
[MAX_PATH
];
662 PROCESS_INFORMATION info
;
663 STARTUPINFOA startup
;
667 const char *basename
;
669 if ((basename
= strrchr( argv0
, '\\' ))) basename
++;
670 else basename
= argv0
;
672 sprintf( keyname
, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Image File Execution Options\\%s",
674 if (!strcmp( keyname
+ strlen(keyname
) - 3, ".so" )) keyname
[strlen(keyname
) - 3] = 0;
676 err
= RegCreateKeyA( HKEY_LOCAL_MACHINE
, keyname
, &hkey
);
677 ok( !err
, "failed to create '%s' error %u\n", keyname
, err
);
680 if (flags
== 0xdeadbeef) /* magic value for unsetting it */
681 RegDeleteValueA( hkey
, "GlobalFlag" );
683 RegSetValueExA( hkey
, "GlobalFlag", 0, REG_DWORD
, (BYTE
*)&flags
, sizeof(flags
) );
685 memset( &startup
, 0, sizeof(startup
) );
686 startup
.cb
= sizeof(startup
);
688 sprintf( buffer
, "%s heap.c 0x%x", argv0
, flags
);
689 ret
= CreateProcessA( NULL
, buffer
, NULL
, NULL
, FALSE
, 0, NULL
, NULL
, &startup
, &info
);
690 ok( ret
, "failed to create child process error %u\n", GetLastError() );
693 winetest_wait_child_process( info
.hProcess
);
694 CloseHandle( info
.hThread
);
695 CloseHandle( info
.hProcess
);
697 RegDeleteValueA( hkey
, "GlobalFlag" );
699 RegDeleteKeyA( HKEY_LOCAL_MACHINE
, keyname
);
702 static DWORD
heap_flags_from_global_flag( DWORD flag
)
706 if (flag
& FLG_HEAP_ENABLE_TAIL_CHECK
)
707 ret
|= HEAP_TAIL_CHECKING_ENABLED
;
708 if (flag
& FLG_HEAP_ENABLE_FREE_CHECK
)
709 ret
|= HEAP_FREE_CHECKING_ENABLED
;
710 if (flag
& FLG_HEAP_VALIDATE_PARAMETERS
)
711 ret
|= HEAP_VALIDATE_PARAMS
| HEAP_VALIDATE
| HEAP_TAIL_CHECKING_ENABLED
| HEAP_FREE_CHECKING_ENABLED
;
712 if (flag
& FLG_HEAP_VALIDATE_ALL
)
713 ret
|= HEAP_VALIDATE_ALL
| HEAP_VALIDATE
| HEAP_TAIL_CHECKING_ENABLED
| HEAP_FREE_CHECKING_ENABLED
;
714 if (flag
& FLG_HEAP_DISABLE_COALESCING
)
715 ret
|= HEAP_DISABLE_COALESCE_ON_FREE
;
716 if (flag
& FLG_HEAP_PAGE_ALLOCS
)
717 ret
|= HEAP_PAGE_ALLOCS
| HEAP_GROWABLE
;
721 static void test_child_heap( const char *arg
)
723 struct heap_layout
*heap
= GetProcessHeap();
724 DWORD expected
= strtoul( arg
, 0, 16 );
727 if (expected
== 0xdeadbeef) /* expected value comes from Session Manager global flags */
731 if (!RegOpenKeyA( HKEY_LOCAL_MACHINE
, "SYSTEM\\CurrentControlSet\\Control\\Session Manager", &hkey
))
734 DWORD type
, size
= sizeof(buffer
);
736 if (!RegQueryValueExA( hkey
, "GlobalFlag", 0, &type
, (BYTE
*)buffer
, &size
))
738 if (type
== REG_DWORD
) expected
= *(DWORD
*)buffer
;
739 else if (type
== REG_SZ
) expected
= strtoul( buffer
, 0, 16 );
744 if (expected
&& !pRtlGetNtGlobalFlags()) /* not working on NT4 */
746 win_skip( "global flags not set\n" );
750 ok( pRtlGetNtGlobalFlags() == expected
,
751 "%s: got global flags %08x expected %08x\n", arg
, pRtlGetNtGlobalFlags(), expected
);
753 expect_heap
= heap_flags_from_global_flag( expected
);
755 if (!(heap
->flags
& HEAP_GROWABLE
) || heap
->pattern
== 0xffeeffee) /* vista layout */
757 ok( heap
->flags
== 0, "%s: got heap flags %08x expected 0\n", arg
, heap
->flags
);
759 else if (heap
->pattern
== 0xeeeeeeee && heap
->flags
== 0xeeeeeeee)
761 ok( expected
& FLG_HEAP_PAGE_ALLOCS
, "%s: got heap flags 0xeeeeeeee without page alloc\n", arg
);
765 ok( heap
->flags
== (expect_heap
| HEAP_GROWABLE
),
766 "%s: got heap flags %08x expected %08x\n", arg
, heap
->flags
, expect_heap
);
767 ok( heap
->force_flags
== (expect_heap
& ~0x18000080),
768 "%s: got heap force flags %08x expected %08x\n", arg
, heap
->force_flags
, expect_heap
);
769 expect_heap
= heap
->flags
;
772 test_heap_checks( expect_heap
);
780 pRtlGetNtGlobalFlags
= (void *)GetProcAddress( GetModuleHandleA("ntdll.dll"), "RtlGetNtGlobalFlags" );
782 argc
= winetest_get_mainargs( &argv
);
785 test_child_heap( argv
[2] );
790 test_obsolete_flags();
792 /* Test both short and very long blocks */
793 test_sized_HeapAlloc(1);
794 test_sized_HeapAlloc(1 << 20);
795 test_sized_HeapReAlloc(1, 100);
796 test_sized_HeapReAlloc(1, (1 << 20));
797 test_sized_HeapReAlloc((1 << 20), (2 << 20));
798 test_sized_HeapReAlloc((1 << 20), 1);
799 test_HeapQueryInformation();
801 if (pRtlGetNtGlobalFlags
)
803 test_debug_heap( argv
[0], 0 );
804 test_debug_heap( argv
[0], FLG_HEAP_ENABLE_TAIL_CHECK
);
805 test_debug_heap( argv
[0], FLG_HEAP_ENABLE_FREE_CHECK
);
806 test_debug_heap( argv
[0], FLG_HEAP_VALIDATE_PARAMETERS
);
807 test_debug_heap( argv
[0], FLG_HEAP_VALIDATE_ALL
);
808 test_debug_heap( argv
[0], FLG_POOL_ENABLE_TAGGING
);
809 test_debug_heap( argv
[0], FLG_HEAP_ENABLE_TAGGING
);
810 test_debug_heap( argv
[0], FLG_HEAP_ENABLE_TAG_BY_DLL
);
811 test_debug_heap( argv
[0], FLG_HEAP_DISABLE_COALESCING
);
812 test_debug_heap( argv
[0], FLG_HEAP_PAGE_ALLOCS
);
813 test_debug_heap( argv
[0], 0xdeadbeef );
815 else win_skip( "RtlGetNtGlobalFlags not found, skipping heap debug tests\n" );