push 8b07bf1f08b23b9893a622b47d2be359556765b1
[wine/hacks.git] / dlls / kernel32 / tests / heap.c
blobc59d304b246c78a3d4d100fa3c7e3c5eb07c9e2c
1 /*
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
22 #include <stdarg.h>
23 #include <stdlib.h>
25 #include "windef.h"
26 #include "winbase.h"
27 #include "wine/test.h"
29 #define MAGIC_DEAD 0xdeadbeef
31 static BOOL (WINAPI *pHeapQueryInformation)(HANDLE, HEAP_INFORMATION_CLASS, PVOID, SIZE_T, PSIZE_T);
33 static SIZE_T resize_9x(SIZE_T size)
35 DWORD dwSizeAligned = (size + 3) & ~3;
36 return max(dwSizeAligned, 12); /* at least 12 bytes */
39 static void test_sized_HeapAlloc(int nbytes)
41 int success;
42 char *buf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, nbytes);
43 ok(buf != NULL, "allocate failed\n");
44 ok(buf[0] == 0, "buffer not zeroed\n");
45 success = HeapFree(GetProcessHeap(), 0, buf);
46 ok(success, "free failed\n");
49 static void test_sized_HeapReAlloc(int nbytes1, int nbytes2)
51 int success;
52 char *buf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, nbytes1);
53 ok(buf != NULL, "allocate failed\n");
54 ok(buf[0] == 0, "buffer not zeroed\n");
55 buf = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, buf, nbytes2);
56 ok(buf != NULL, "reallocate failed\n");
57 ok(buf[nbytes2-1] == 0, "buffer not zeroed\n");
58 success = HeapFree(GetProcessHeap(), 0, buf);
59 ok(success, "free failed\n");
62 static void test_heap(void)
64 LPVOID mem;
65 LPVOID msecond;
66 DWORD res;
67 UINT flags;
68 HGLOBAL gbl;
69 HGLOBAL hsecond;
70 SIZE_T size;
72 /* Heap*() functions */
73 mem = HeapAlloc(GetProcessHeap(), 0, 0);
74 ok(mem != NULL, "memory not allocated for size 0\n");
76 mem = HeapReAlloc(GetProcessHeap(), 0, NULL, 10);
77 ok(mem == NULL, "memory allocated by HeapReAlloc\n");
79 for (size = 0; size <= 256; size++)
81 SIZE_T heap_size;
82 mem = HeapAlloc(GetProcessHeap(), 0, size);
83 heap_size = HeapSize(GetProcessHeap(), 0, mem);
84 ok(heap_size == size || heap_size == resize_9x(size),
85 "HeapSize returned %lu instead of %lu or %lu\n", heap_size, size, resize_9x(size));
86 HeapFree(GetProcessHeap(), 0, mem);
89 /* test some border cases of HeapAlloc and HeapReAlloc */
90 mem = HeapAlloc(GetProcessHeap(), 0, 0);
91 ok(mem != NULL, "memory not allocated for size 0\n");
92 msecond = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, mem, ~(SIZE_T)0 - 7);
93 ok(msecond == NULL, "HeapReAlloc(~0 - 7) should have failed\n");
94 msecond = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, mem, ~(SIZE_T)0);
95 ok(msecond == NULL, "HeapReAlloc(~0) should have failed\n");
96 HeapFree(GetProcessHeap(), 0, mem);
97 mem = HeapAlloc(GetProcessHeap(), 0, ~(SIZE_T)0);
98 ok(mem == NULL, "memory allocated for size ~0\n");
100 /* large blocks must be 16-byte aligned */
101 mem = HeapAlloc(GetProcessHeap(), 0, 512 * 1024);
102 ok( mem != NULL, "failed for size 512K\n" );
103 ok( (ULONG_PTR)mem % 16 == 0 || broken((ULONG_PTR)mem % 16) /* win9x */,
104 "512K block not 16-byte aligned\n" );
105 HeapFree(GetProcessHeap(), 0, mem);
107 /* Global*() functions */
108 gbl = GlobalAlloc(GMEM_MOVEABLE, 0);
109 ok(gbl != NULL, "global memory not allocated for size 0\n");
111 gbl = GlobalReAlloc(gbl, 10, GMEM_MOVEABLE);
112 ok(gbl != NULL, "Can't realloc global memory\n");
113 size = GlobalSize(gbl);
114 ok(size >= 10 && size <= 16, "Memory not resized to size 10, instead size=%ld\n", size);
116 gbl = GlobalReAlloc(gbl, 0, GMEM_MOVEABLE);
117 ok(gbl != NULL, "GlobalReAlloc should not fail on size 0\n");
119 size = GlobalSize(gbl);
120 ok(size == 0, "Memory not resized to size 0, instead size=%ld\n", size);
121 ok(GlobalFree(gbl) == NULL, "Memory not freed\n");
122 size = GlobalSize(gbl);
123 ok(size == 0, "Memory should have been freed, size=%ld\n", size);
125 gbl = GlobalReAlloc(0, 10, GMEM_MOVEABLE);
126 ok(gbl == NULL, "global realloc allocated memory\n");
128 /* GlobalLock / GlobalUnlock with a valid handle */
129 gbl = GlobalAlloc(GMEM_MOVEABLE, 256);
131 SetLastError(MAGIC_DEAD);
132 mem = GlobalLock(gbl); /* #1 */
133 ok(mem != NULL, "returned %p with %d (expected '!= NULL')\n", mem, GetLastError());
134 SetLastError(MAGIC_DEAD);
135 flags = GlobalFlags(gbl);
136 ok( flags == 1, "returned 0x%04x with %d (expected '0x0001')\n",
137 flags, GetLastError());
139 SetLastError(MAGIC_DEAD);
140 msecond = GlobalLock(gbl); /* #2 */
141 ok( msecond == mem, "returned %p with %d (expected '%p')\n",
142 msecond, GetLastError(), mem);
143 SetLastError(MAGIC_DEAD);
144 flags = GlobalFlags(gbl);
145 ok( flags == 2, "returned 0x%04x with %d (expected '0x0002')\n",
146 flags, GetLastError());
147 SetLastError(MAGIC_DEAD);
149 SetLastError(MAGIC_DEAD);
150 res = GlobalUnlock(gbl); /* #1 */
151 ok(res, "returned %d with %d (expected '!= 0')\n", res, GetLastError());
152 SetLastError(MAGIC_DEAD);
153 flags = GlobalFlags(gbl);
154 ok( flags , "returned 0x%04x with %d (expected '!= 0')\n",
155 flags, GetLastError());
157 SetLastError(MAGIC_DEAD);
158 res = GlobalUnlock(gbl); /* #0 */
159 /* NT: ERROR_SUCCESS (documented on MSDN), 9x: untouched */
160 ok(!res && ((GetLastError() == ERROR_SUCCESS) || (GetLastError() == MAGIC_DEAD)),
161 "returned %d with %d (expected '0' with: ERROR_SUCCESS or "
162 "MAGIC_DEAD)\n", res, GetLastError());
163 SetLastError(MAGIC_DEAD);
164 flags = GlobalFlags(gbl);
165 ok( !flags , "returned 0x%04x with %d (expected '0')\n",
166 flags, GetLastError());
168 /* Unlock an already unlocked Handle */
169 SetLastError(MAGIC_DEAD);
170 res = GlobalUnlock(gbl);
171 /* NT: ERROR_NOT_LOCKED, 9x: untouched */
172 ok( !res &&
173 ((GetLastError() == ERROR_NOT_LOCKED) || (GetLastError() == MAGIC_DEAD)),
174 "returned %d with %d (expected '0' with: ERROR_NOT_LOCKED or "
175 "MAGIC_DEAD)\n", res, GetLastError());
177 GlobalFree(gbl);
178 /* invalid handles are caught in windows: */
179 SetLastError(MAGIC_DEAD);
180 hsecond = GlobalFree(gbl); /* invalid handle: free memory twice */
181 ok( (hsecond == gbl) && (GetLastError() == ERROR_INVALID_HANDLE),
182 "returned %p with 0x%08x (expected %p with ERROR_INVALID_HANDLE)\n",
183 hsecond, GetLastError(), gbl);
184 SetLastError(MAGIC_DEAD);
185 flags = GlobalFlags(gbl);
186 ok( (flags == GMEM_INVALID_HANDLE) && (GetLastError() == ERROR_INVALID_HANDLE),
187 "returned 0x%04x with 0x%08x (expected GMEM_INVALID_HANDLE with "
188 "ERROR_INVALID_HANDLE)\n", flags, GetLastError());
189 SetLastError(MAGIC_DEAD);
190 size = GlobalSize(gbl);
191 ok( (size == 0) && (GetLastError() == ERROR_INVALID_HANDLE),
192 "returned %ld with 0x%08x (expected '0' with ERROR_INVALID_HANDLE)\n",
193 size, GetLastError());
195 SetLastError(MAGIC_DEAD);
196 mem = GlobalLock(gbl);
197 ok( (mem == NULL) && (GetLastError() == ERROR_INVALID_HANDLE),
198 "returned %p with 0x%08x (expected NULL with ERROR_INVALID_HANDLE)\n",
199 mem, GetLastError());
201 /* documented on MSDN: GlobalUnlock() return FALSE on failure.
202 Win9x and wine return FALSE with ERROR_INVALID_HANDLE, but on
203 NT 3.51 and XPsp2, TRUE with ERROR_INVALID_HANDLE is returned.
204 The similar Test for LocalUnlock() works on all Systems */
205 SetLastError(MAGIC_DEAD);
206 res = GlobalUnlock(gbl);
207 ok(GetLastError() == ERROR_INVALID_HANDLE,
208 "returned %d with %d (expected ERROR_INVALID_HANDLE)\n",
209 res, GetLastError());
211 gbl = GlobalAlloc(GMEM_DDESHARE, 100);
213 /* first free */
214 mem = GlobalFree(gbl);
215 ok(mem == NULL, "Expected NULL, got %p\n", mem);
217 /* invalid free */
218 if (sizeof(void *) != 8) /* crashes on 64-bit Vista */
220 SetLastError(MAGIC_DEAD);
221 mem = GlobalFree(gbl);
222 ok(mem == gbl || broken(mem == NULL) /* nt4 */, "Expected gbl, got %p\n", mem);
223 if (mem == gbl)
224 ok(GetLastError() == ERROR_INVALID_HANDLE ||
225 GetLastError() == ERROR_INVALID_PARAMETER, /* win9x */
226 "Expected ERROR_INVALID_HANDLE or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
229 gbl = GlobalAlloc(GMEM_DDESHARE, 100);
231 res = GlobalUnlock(gbl);
232 ok(res == 1 ||
233 res == 0, /* win9x */
234 "Expected 1 or 0, got %d\n", res);
236 res = GlobalUnlock(gbl);
237 ok(res == 1 ||
238 res == 0, /* win9x */
239 "Expected 1 or 0, got %d\n", res);
241 /* GlobalSize on an invalid handle */
242 if (sizeof(void *) != 8) /* crashes on 64-bit Vista */
244 SetLastError(MAGIC_DEAD);
245 size = GlobalSize((HGLOBAL)0xc042);
246 ok(size == 0, "Expected 0, got %ld\n", size);
247 ok(GetLastError() == ERROR_INVALID_HANDLE ||
248 GetLastError() == ERROR_INVALID_PARAMETER, /* win9x */
249 "Expected ERROR_INVALID_HANDLE or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
252 /* ####################################### */
253 /* Local*() functions */
254 gbl = LocalAlloc(LMEM_MOVEABLE, 0);
255 ok(gbl != NULL, "local memory not allocated for size 0\n");
257 gbl = LocalReAlloc(gbl, 10, LMEM_MOVEABLE);
258 ok(gbl != NULL, "Can't realloc local memory\n");
259 size = LocalSize(gbl);
260 ok(size >= 10 && size <= 16, "Memory not resized to size 10, instead size=%ld\n", size);
262 gbl = LocalReAlloc(gbl, 0, LMEM_MOVEABLE);
263 ok(gbl != NULL, "LocalReAlloc should not fail on size 0\n");
265 size = LocalSize(gbl);
266 ok(size == 0, "Memory not resized to size 0, instead size=%ld\n", size);
267 ok(LocalFree(gbl) == NULL, "Memory not freed\n");
268 size = LocalSize(gbl);
269 ok(size == 0, "Memory should have been freed, size=%ld\n", size);
271 gbl = LocalReAlloc(0, 10, LMEM_MOVEABLE);
272 ok(gbl == NULL, "local realloc allocated memory\n");
274 /* LocalLock / LocalUnlock with a valid handle */
275 gbl = LocalAlloc(LMEM_MOVEABLE, 256);
276 SetLastError(MAGIC_DEAD);
277 mem = LocalLock(gbl); /* #1 */
278 ok(mem != NULL, "returned %p with %d (expected '!= NULL')\n", mem, GetLastError());
279 SetLastError(MAGIC_DEAD);
280 flags = LocalFlags(gbl);
281 ok( flags == 1, "returned 0x%04x with %d (expected '0x0001')\n",
282 flags, GetLastError());
284 SetLastError(MAGIC_DEAD);
285 msecond = LocalLock(gbl); /* #2 */
286 ok( msecond == mem, "returned %p with %d (expected '%p')\n",
287 msecond, GetLastError(), mem);
288 SetLastError(MAGIC_DEAD);
289 flags = LocalFlags(gbl);
290 ok( flags == 2, "returned 0x%04x with %d (expected '0x0002')\n",
291 flags, GetLastError());
292 SetLastError(MAGIC_DEAD);
294 SetLastError(MAGIC_DEAD);
295 res = LocalUnlock(gbl); /* #1 */
296 ok(res, "returned %d with %d (expected '!= 0')\n", res, GetLastError());
297 SetLastError(MAGIC_DEAD);
298 flags = LocalFlags(gbl);
299 ok( flags , "returned 0x%04x with %d (expected '!= 0')\n",
300 flags, GetLastError());
302 SetLastError(MAGIC_DEAD);
303 res = LocalUnlock(gbl); /* #0 */
304 /* NT: ERROR_SUCCESS (documented on MSDN), 9x: untouched */
305 ok(!res && ((GetLastError() == ERROR_SUCCESS) || (GetLastError() == MAGIC_DEAD)),
306 "returned %d with %d (expected '0' with: ERROR_SUCCESS or "
307 "MAGIC_DEAD)\n", res, GetLastError());
308 SetLastError(MAGIC_DEAD);
309 flags = LocalFlags(gbl);
310 ok( !flags , "returned 0x%04x with %d (expected '0')\n",
311 flags, GetLastError());
313 /* Unlock an already unlocked Handle */
314 SetLastError(MAGIC_DEAD);
315 res = LocalUnlock(gbl);
316 /* NT: ERROR_NOT_LOCKED, 9x: untouched */
317 ok( !res &&
318 ((GetLastError() == ERROR_NOT_LOCKED) || (GetLastError() == MAGIC_DEAD)),
319 "returned %d with %d (expected '0' with: ERROR_NOT_LOCKED or "
320 "MAGIC_DEAD)\n", res, GetLastError());
322 LocalFree(gbl);
323 /* invalid handles are caught in windows: */
324 SetLastError(MAGIC_DEAD);
325 hsecond = LocalFree(gbl); /* invalid handle: free memory twice */
326 ok( (hsecond == gbl) && (GetLastError() == ERROR_INVALID_HANDLE),
327 "returned %p with 0x%08x (expected %p with ERROR_INVALID_HANDLE)\n",
328 hsecond, GetLastError(), gbl);
329 SetLastError(MAGIC_DEAD);
330 flags = LocalFlags(gbl);
331 ok( (flags == LMEM_INVALID_HANDLE) && (GetLastError() == ERROR_INVALID_HANDLE),
332 "returned 0x%04x with 0x%08x (expected LMEM_INVALID_HANDLE with "
333 "ERROR_INVALID_HANDLE)\n", flags, GetLastError());
334 SetLastError(MAGIC_DEAD);
335 size = LocalSize(gbl);
336 ok( (size == 0) && (GetLastError() == ERROR_INVALID_HANDLE),
337 "returned %ld with 0x%08x (expected '0' with ERROR_INVALID_HANDLE)\n",
338 size, GetLastError());
340 SetLastError(MAGIC_DEAD);
341 mem = LocalLock(gbl);
342 ok( (mem == NULL) && (GetLastError() == ERROR_INVALID_HANDLE),
343 "returned %p with 0x%08x (expected NULL with ERROR_INVALID_HANDLE)\n",
344 mem, GetLastError());
346 /* This Test works the same on all Systems (GlobalUnlock() is different) */
347 SetLastError(MAGIC_DEAD);
348 res = LocalUnlock(gbl);
349 ok(!res && (GetLastError() == ERROR_INVALID_HANDLE),
350 "returned %d with %d (expected '0' with ERROR_INVALID_HANDLE)\n",
351 res, GetLastError());
353 /* trying to lock empty memory should give an error */
354 gbl = GlobalAlloc(GMEM_MOVEABLE|GMEM_ZEROINIT,0);
355 ok(gbl != NULL, "returned NULL\n");
356 SetLastError(MAGIC_DEAD);
357 mem = GlobalLock(gbl);
358 /* NT: ERROR_DISCARDED, 9x: untouched */
359 ok( (mem == NULL) &&
360 ((GetLastError() == ERROR_DISCARDED) || (GetLastError() == MAGIC_DEAD)),
361 "returned %p with 0x%x/%d (expected 'NULL' with: ERROR_DISCARDED or "
362 "MAGIC_DEAD)\n", mem, GetLastError(), GetLastError());
364 GlobalFree(gbl);
367 static void test_obsolete_flags(void)
369 static struct {
370 UINT flags;
371 UINT globalflags;
372 } test_global_flags[] = {
373 {GMEM_FIXED | GMEM_NOTIFY, 0},
374 {GMEM_FIXED | GMEM_DISCARDABLE, 0},
375 {GMEM_MOVEABLE | GMEM_NOTIFY, 0},
376 {GMEM_MOVEABLE | GMEM_DDESHARE, GMEM_DDESHARE},
377 {GMEM_MOVEABLE | GMEM_NOT_BANKED, 0},
378 {GMEM_MOVEABLE | GMEM_NODISCARD, 0},
379 {GMEM_MOVEABLE | GMEM_DISCARDABLE, GMEM_DISCARDABLE},
380 {GMEM_MOVEABLE | GMEM_DDESHARE | GMEM_DISCARDABLE | GMEM_LOWER | GMEM_NOCOMPACT | GMEM_NODISCARD |
381 GMEM_NOT_BANKED | GMEM_NOTIFY, GMEM_DDESHARE | GMEM_DISCARDABLE},
384 unsigned int i;
385 HGLOBAL gbl;
386 UINT resultflags;
388 UINT (WINAPI *pGlobalFlags)(HGLOBAL);
390 pGlobalFlags = (void *) GetProcAddress(GetModuleHandleA("kernel32"), "GlobalFlags");
392 if (!pGlobalFlags)
394 win_skip("GlobalFlags is not available\n");
395 return;
398 for (i = 0; i < sizeof(test_global_flags)/sizeof(test_global_flags[0]); i++)
400 gbl = GlobalAlloc(test_global_flags[i].flags, 4);
401 ok(gbl != NULL, "GlobalAlloc failed\n");
403 SetLastError(MAGIC_DEAD);
404 resultflags = pGlobalFlags(gbl);
406 ok( resultflags == test_global_flags[i].globalflags ||
407 broken(resultflags == (test_global_flags[i].globalflags & ~GMEM_DDESHARE)), /* win9x */
408 "%u: expected 0x%08x, but returned 0x%08x with %d\n",
409 i, test_global_flags[i].globalflags, resultflags, GetLastError() );
411 GlobalFree(gbl);
415 static void test_HeapQueryInformation(void)
417 ULONG info;
418 SIZE_T size;
419 BOOL ret;
421 pHeapQueryInformation = (void *)GetProcAddress(GetModuleHandle("kernel32.dll"), "HeapQueryInformation");
422 if (!pHeapQueryInformation)
424 win_skip("HeapQueryInformation is not available\n");
425 return;
428 if (0) /* crashes under XP */
430 size = 0;
431 ret = pHeapQueryInformation(0,
432 HeapCompatibilityInformation,
433 &info, sizeof(info), &size);
434 size = 0;
435 ret = pHeapQueryInformation(GetProcessHeap(),
436 HeapCompatibilityInformation,
437 NULL, sizeof(info), &size);
440 size = 0;
441 SetLastError(0xdeadbeef);
442 ret = pHeapQueryInformation(GetProcessHeap(),
443 HeapCompatibilityInformation,
444 NULL, 0, &size);
445 ok(!ret, "HeapQueryInformation should fail\n");
446 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
447 "expected ERROR_INSUFFICIENT_BUFFER got %u\n", GetLastError());
448 ok(size == sizeof(ULONG), "expected 4, got %lu\n", size);
450 SetLastError(0xdeadbeef);
451 ret = pHeapQueryInformation(GetProcessHeap(),
452 HeapCompatibilityInformation,
453 NULL, 0, NULL);
454 ok(!ret, "HeapQueryInformation should fail\n");
455 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
456 "expected ERROR_INSUFFICIENT_BUFFER got %u\n", GetLastError());
458 info = 0xdeadbeaf;
459 SetLastError(0xdeadbeef);
460 ret = pHeapQueryInformation(GetProcessHeap(),
461 HeapCompatibilityInformation,
462 &info, sizeof(info) + 1, NULL);
463 ok(ret, "HeapQueryInformation error %u\n", GetLastError());
464 ok(info == 0 || info == 1 || info == 2, "expected 0, 1 or 2, got %u\n", info);
467 START_TEST(heap)
469 test_heap();
470 test_obsolete_flags();
472 /* Test both short and very long blocks */
473 test_sized_HeapAlloc(1);
474 test_sized_HeapAlloc(1 << 20);
475 test_sized_HeapReAlloc(1, 100);
476 test_sized_HeapReAlloc(1, (1 << 20));
477 test_sized_HeapReAlloc((1 << 20), (2 << 20));
478 test_sized_HeapReAlloc((1 << 20), 1);
479 test_HeapQueryInformation();