mfplat: Read queue subscriber within the critical section.
[wine/zf.git] / dlls / kernel32 / tests / fiber.c
blob3c54351fa7bd3cd01ff33556c0f05ebbb0c2624a
1 /*
2 * Unit tests for fiber functions
4 * Copyright (c) 2010 André Hentschel
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include <stdarg.h>
23 #include <ntstatus.h>
24 #define WIN32_NO_STATUS
25 #include <winternl.h>
26 #include "wine/test.h"
28 static LPVOID (WINAPI *pCreateFiber)(SIZE_T,LPFIBER_START_ROUTINE,LPVOID);
29 static LPVOID (WINAPI *pConvertThreadToFiber)(LPVOID);
30 static BOOL (WINAPI *pConvertFiberToThread)(void);
31 static void (WINAPI *pSwitchToFiber)(LPVOID);
32 static void (WINAPI *pDeleteFiber)(LPVOID);
33 static LPVOID (WINAPI *pConvertThreadToFiberEx)(LPVOID,DWORD);
34 static LPVOID (WINAPI *pCreateFiberEx)(SIZE_T,SIZE_T,DWORD,LPFIBER_START_ROUTINE,LPVOID);
35 static BOOL (WINAPI *pIsThreadAFiber)(void);
36 static DWORD (WINAPI *pFlsAlloc)(PFLS_CALLBACK_FUNCTION);
37 static BOOL (WINAPI *pFlsFree)(DWORD);
38 static PVOID (WINAPI *pFlsGetValue)(DWORD);
39 static BOOL (WINAPI *pFlsSetValue)(DWORD,PVOID);
40 static void (WINAPI *pRtlAcquirePebLock)(void);
41 static void (WINAPI *pRtlReleasePebLock)(void);
42 static NTSTATUS (WINAPI *pRtlFlsAlloc)(PFLS_CALLBACK_FUNCTION,DWORD*);
43 static NTSTATUS (WINAPI *pRtlFlsFree)(ULONG);
44 static NTSTATUS (WINAPI *pRtlFlsSetValue)(ULONG,void *);
45 static NTSTATUS (WINAPI *pRtlFlsGetValue)(ULONG,void **);
46 static void (WINAPI *pRtlProcessFlsData)(void *fls_data, ULONG flags);
47 static void *fibers[3];
48 static BYTE testparam = 185;
49 static DWORD fls_index_to_set = FLS_OUT_OF_INDEXES;
50 static void* fls_value_to_set;
52 static int fiberCount = 0;
53 static int cbCount = 0;
55 static VOID init_funcs(void)
57 HMODULE hKernel32 = GetModuleHandleA("kernel32.dll");
58 HMODULE hntdll = GetModuleHandleA("ntdll.dll");
60 #define X(f) p##f = (void*)GetProcAddress(hKernel32, #f);
61 X(CreateFiber);
62 X(ConvertThreadToFiber);
63 X(ConvertFiberToThread);
64 X(SwitchToFiber);
65 X(DeleteFiber);
66 X(ConvertThreadToFiberEx);
67 X(CreateFiberEx);
68 X(IsThreadAFiber);
69 X(FlsAlloc);
70 X(FlsFree);
71 X(FlsGetValue);
72 X(FlsSetValue);
73 #undef X
75 #define X(f) p##f = (void*)GetProcAddress(hntdll, #f);
76 X(RtlFlsAlloc);
77 X(RtlFlsFree);
78 X(RtlFlsSetValue);
79 X(RtlFlsGetValue);
80 X(RtlProcessFlsData);
81 X(RtlAcquirePebLock);
82 X(RtlReleasePebLock);
83 #undef X
87 static VOID WINAPI FiberLocalStorageProc(PVOID lpFlsData)
89 ok(lpFlsData == fls_value_to_set,
90 "FlsData expected not to be changed, value is %p, expected %p\n",
91 lpFlsData, fls_value_to_set);
92 cbCount++;
95 static VOID WINAPI FiberMainProc(LPVOID lpFiberParameter)
97 BYTE *tparam = (BYTE *)lpFiberParameter;
98 TEB *teb = NtCurrentTeb();
100 ok(!teb->FlsSlots, "Got unexpected FlsSlots %p.\n", teb->FlsSlots);
102 fiberCount++;
103 ok(*tparam == 185, "Parameterdata expected not to be changed\n");
104 if (fls_index_to_set != FLS_OUT_OF_INDEXES)
106 void* ret;
107 BOOL bret;
109 SetLastError( 0xdeadbeef );
110 ret = pFlsGetValue(fls_index_to_set);
111 ok(ret == NULL, "FlsGetValue returned %p, expected NULL\n", ret);
112 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Got unexpected error %u.\n", GetLastError());
114 /* Set the FLS value */
115 bret = pFlsSetValue(fls_index_to_set, fls_value_to_set);
116 ok(bret, "FlsSetValue failed with error %u\n", GetLastError());
118 ok(!!teb->FlsSlots, "Got unexpected FlsSlots %p.\n", teb->FlsSlots);
120 /* Verify that FlsGetValue retrieves the value set by FlsSetValue */
121 SetLastError( 0xdeadbeef );
122 ret = pFlsGetValue(fls_index_to_set);
123 ok(ret == fls_value_to_set, "FlsGetValue returned %p, expected %p\n", ret, fls_value_to_set);
124 ok(GetLastError() == ERROR_SUCCESS, "FlsGetValue error %u\n", GetLastError());
126 pSwitchToFiber(fibers[0]);
129 static void test_ConvertThreadToFiber(void)
131 if (pConvertThreadToFiber)
133 fibers[0] = pConvertThreadToFiber(&testparam);
134 ok(fibers[0] != NULL, "ConvertThreadToFiber failed with error %u\n", GetLastError());
136 else
138 win_skip( "ConvertThreadToFiber not present\n" );
142 static void test_ConvertThreadToFiberEx(void)
144 if (pConvertThreadToFiberEx)
146 fibers[0] = pConvertThreadToFiberEx(&testparam, 0);
147 ok(fibers[0] != NULL, "ConvertThreadToFiberEx failed with error %u\n", GetLastError());
149 else
151 win_skip( "ConvertThreadToFiberEx not present\n" );
155 static void test_ConvertFiberToThread(void)
157 if (pConvertFiberToThread)
159 BOOL ret = pConvertFiberToThread();
160 ok(ret, "ConvertFiberToThread failed with error %u\n", GetLastError());
162 else
164 win_skip( "ConvertFiberToThread not present\n" );
168 static void test_FiberHandling(void)
170 fiberCount = 0;
171 fibers[0] = pCreateFiber(0,FiberMainProc,&testparam);
172 ok(fibers[0] != NULL, "CreateFiber failed with error %u\n", GetLastError());
173 pDeleteFiber(fibers[0]);
175 test_ConvertThreadToFiber();
176 test_ConvertFiberToThread();
177 if (pConvertThreadToFiberEx)
178 test_ConvertThreadToFiberEx();
179 else
180 test_ConvertThreadToFiber();
182 fibers[1] = pCreateFiber(0,FiberMainProc,&testparam);
183 ok(fibers[1] != NULL, "CreateFiber failed with error %u\n", GetLastError());
185 pSwitchToFiber(fibers[1]);
186 ok(fiberCount == 1, "Wrong fiber count: %d\n", fiberCount);
187 pDeleteFiber(fibers[1]);
189 if (pCreateFiberEx)
191 fibers[1] = pCreateFiberEx(0,0,0,FiberMainProc,&testparam);
192 ok(fibers[1] != NULL, "CreateFiberEx failed with error %u\n", GetLastError());
194 pSwitchToFiber(fibers[1]);
195 ok(fiberCount == 2, "Wrong fiber count: %d\n", fiberCount);
196 pDeleteFiber(fibers[1]);
198 else win_skip( "CreateFiberEx not present\n" );
200 if (pIsThreadAFiber) ok(pIsThreadAFiber(), "IsThreadAFiber reported FALSE\n");
201 test_ConvertFiberToThread();
202 if (pIsThreadAFiber) ok(!pIsThreadAFiber(), "IsThreadAFiber reported TRUE\n");
205 #define FLS_TEST_INDEX_COUNT 4096
207 static unsigned int check_linked_list(const LIST_ENTRY *le, const LIST_ENTRY *search_entry, unsigned int *index_found)
209 unsigned int count = 0;
210 LIST_ENTRY *entry;
212 *index_found = ~0;
214 for (entry = le->Flink; entry != le; entry = entry->Flink)
216 if (entry == search_entry)
218 ok(*index_found == ~0, "Duplicate list entry.\n");
219 *index_found = count;
221 ++count;
223 return count;
226 static unsigned int test_fls_callback_call_count;
228 static void WINAPI test_fls_callback(void *data)
230 ++test_fls_callback_call_count;
233 static unsigned int test_fls_chunk_size(unsigned int chunk_index)
235 return 0x10 << chunk_index;
238 static unsigned int test_fls_chunk_index_from_index(unsigned int index, unsigned int *index_in_chunk)
240 unsigned int chunk_index = 0;
242 while (index >= test_fls_chunk_size(chunk_index))
243 index -= test_fls_chunk_size(chunk_index++);
245 *index_in_chunk = index;
246 return chunk_index;
249 static HANDLE test_fiberlocalstorage_peb_locked_event;
250 static HANDLE test_fiberlocalstorage_done_event;
253 static DWORD WINAPI test_FiberLocalStorage_thread(void *arg)
255 pRtlAcquirePebLock();
256 SetEvent(test_fiberlocalstorage_peb_locked_event);
257 WaitForSingleObject(test_fiberlocalstorage_done_event, INFINITE);
258 pRtlReleasePebLock();
259 return 0;
262 static void test_FiberLocalStorage(void)
264 static DWORD fls_indices[FLS_TEST_INDEX_COUNT];
265 unsigned int i, j, count, entry_count, index;
266 LIST_ENTRY *fls_list_head, saved_entry;
267 TEB_FLS_DATA *fls_data, *new_fls_data;
268 GLOBAL_FLS_DATA *g_fls_data;
269 DWORD fls, fls_2, result;
270 TEB *teb = NtCurrentTeb();
271 PEB *peb = teb->Peb;
272 NTSTATUS status;
273 HANDLE hthread;
274 SIZE_T size;
275 void* val;
276 BOOL ret;
278 if (!pFlsAlloc || !pFlsSetValue || !pFlsGetValue || !pFlsFree)
280 win_skip( "Fiber Local Storage not supported\n" );
281 return;
284 if (pRtlFlsAlloc)
286 if (pRtlFlsGetValue)
288 status = pRtlFlsGetValue(0, NULL);
289 ok(status == STATUS_INVALID_PARAMETER, "Got unexpected status %#x.\n", status);
291 else
293 win_skip("RtlFlsGetValue is not available.\n");
296 for (i = 0; i < FLS_TEST_INDEX_COUNT; ++i)
298 fls_indices[i] = 0xdeadbeef;
299 status = pRtlFlsAlloc(test_fls_callback, &fls_indices[i]);
300 ok(!status || status == STATUS_NO_MEMORY, "Got unexpected status %#x.\n", status);
301 if (status)
303 ok(fls_indices[i] == 0xdeadbeef, "Got unexpected index %#x.\n", fls_indices[i]);
304 break;
306 if (pRtlFlsSetValue)
308 status = pRtlFlsSetValue(fls_indices[i], (void *)(ULONG_PTR)(i + 1));
309 ok(!status, "Got unexpected status %#x.\n", status);
312 count = i;
314 fls_data = teb->FlsSlots;
316 /* FLS limits are increased since Win10 18312. */
317 ok(count && (count <= 127 || (count > 4000 && count < 4096)), "Got unexpected count %u.\n", count);
319 if (!peb->FlsCallback)
321 ok(pRtlFlsSetValue && pRtlFlsGetValue, "Missing RtlFlsGetValue / RtlFlsSetValue.\n");
322 ok(!peb->FlsBitmap, "Got unexpected FlsBitmap %p.\n", peb->FlsBitmap);
323 ok(!peb->FlsListHead.Flink && !peb->FlsListHead.Blink, "Got nonzero FlsListHead.\n");
324 ok(!peb->FlsHighIndex, "Got unexpected FlsHighIndex %u.\n", peb->FlsHighIndex);
326 fls_list_head = fls_data->fls_list_entry.Flink;
328 entry_count = check_linked_list(fls_list_head, &fls_data->fls_list_entry, &index);
329 ok(entry_count == 1, "Got unexpected count %u.\n", entry_count);
330 ok(!index, "Got unexpected index %u.\n", index);
332 g_fls_data = CONTAINING_RECORD(fls_list_head, GLOBAL_FLS_DATA, fls_list_head);
334 ok(g_fls_data->fls_high_index == 0xfef, "Got unexpected fls_high_index %#x.\n", g_fls_data->fls_high_index);
336 for (i = 0; i < 8; ++i)
338 ok(!!g_fls_data->fls_callback_chunks[i], "Got zero fls_callback_chunks[%u].\n", i);
339 ok(g_fls_data->fls_callback_chunks[i]->count == test_fls_chunk_size(i),
340 "Got unexpected g_fls_data->fls_callback_chunks[%u]->count %u.\n",
341 i, g_fls_data->fls_callback_chunks[i]->count);
343 size = HeapSize(GetProcessHeap(), 0, g_fls_data->fls_callback_chunks[i]);
344 ok(size == sizeof(ULONG_PTR) + sizeof(FLS_CALLBACK) * test_fls_chunk_size(i),
345 "Got unexpected size %p.\n", (void *)size);
347 ok(!!fls_data->fls_data_chunks[i], "Got zero fls_data->fls_data_chunks[%u].\n", i);
348 ok(!fls_data->fls_data_chunks[i][0], "Got unexpected fls_data->fls_data_chunks[%u][0] %p.\n",
349 i, fls_data->fls_data_chunks[i][0]);
350 size = HeapSize(GetProcessHeap(), 0, fls_data->fls_data_chunks[i]);
351 ok(size == sizeof(void *) * (test_fls_chunk_size(i) + 1), "Got unexpected size %p.\n", (void *)size);
353 if (!i)
355 ok(g_fls_data->fls_callback_chunks[0]->callbacks[0].callback == (void *)~(ULONG_PTR)0,
356 "Got unexpected callback %p.\n",
357 g_fls_data->fls_callback_chunks[0]->callbacks[0].callback);
360 for (j = i ? 0 : fls_indices[0]; j < test_fls_chunk_size(i); ++j)
362 ok(!g_fls_data->fls_callback_chunks[i]->callbacks[j].unknown,
363 "Got unexpected unknown %p, i %u, j %u.\n",
364 g_fls_data->fls_callback_chunks[i]->callbacks[j].unknown, i, j);
365 ok(g_fls_data->fls_callback_chunks[i]->callbacks[j].callback == test_fls_callback,
366 "Got unexpected callback %p, i %u, j %u.\n",
367 g_fls_data->fls_callback_chunks[i]->callbacks[j].callback, i, j);
370 for (i = 0; i < count; ++i)
372 j = test_fls_chunk_index_from_index(fls_indices[i], &index);
373 ok(fls_data->fls_data_chunks[j][index + 1] == (void *)(ULONG_PTR)(i + 1),
374 "Got unexpected FLS value %p, i %u, j %u, index %u.\n",
375 fls_data->fls_data_chunks[j][index + 1], i, j, index);
377 j = test_fls_chunk_index_from_index(fls_indices[0x10], &index);
378 g_fls_data->fls_callback_chunks[j]->callbacks[index].callback = NULL;
379 status = pRtlFlsFree(fls_indices[0x10]);
380 ok(status == STATUS_INVALID_PARAMETER, "Got unexpected status %#x.\n", status);
382 g_fls_data->fls_callback_chunks[j]->callbacks[index].callback = test_fls_callback;
383 test_fls_callback_call_count = 0;
384 status = pRtlFlsFree(fls_indices[0x10]);
385 ok(!status, "Got unexpected status %#x.\n", status);
386 ok(test_fls_callback_call_count == 1, "Got unexpected callback call count %u.\n",
387 test_fls_callback_call_count);
389 ok(!fls_data->fls_data_chunks[j][0], "Got unexpected fls_data->fls_data_chunks[%u][0] %p.\n",
390 j, fls_data->fls_data_chunks[j][0]);
391 ok(!g_fls_data->fls_callback_chunks[j]->callbacks[index].callback,
392 "Got unexpected callback %p.\n",
393 g_fls_data->fls_callback_chunks[j]->callbacks[index].callback);
395 fls_data->fls_data_chunks[j][index + 1] = (void *)(ULONG_PTR)0x28;
396 status = pRtlFlsAlloc(test_fls_callback, &i);
397 ok(!status, "Got unexpected status %#x.\n", status);
398 ok(i == fls_indices[0x10], "Got unexpected index %u.\n", i);
399 ok(fls_data->fls_data_chunks[j][index + 1] == (void *)(ULONG_PTR)0x28, "Got unexpected data %p.\n",
400 fls_data->fls_data_chunks[j][index + 1]);
402 status = pRtlFlsSetValue(i, (void *)(ULONG_PTR)0x11);
403 ok(!status, "Got unexpected status %#x.\n", status);
405 teb->FlsSlots = NULL;
407 val = (void *)0xdeadbeef;
408 status = pRtlFlsGetValue(fls_indices[1], &val);
409 new_fls_data = teb->FlsSlots;
410 ok(status == STATUS_INVALID_PARAMETER, "Got unexpected status %#x.\n", status);
411 ok(val == (void *)0xdeadbeef, "Got unexpected val %p.\n", val);
412 ok(!new_fls_data, "Got unexpected teb->FlsSlots %p.\n", new_fls_data);
414 status = pRtlFlsSetValue(fls_indices[1], (void *)(ULONG_PTR)0x28);
415 new_fls_data = teb->FlsSlots;
416 ok(!status, "Got unexpected status %#x.\n", status);
417 ok(!!new_fls_data, "Got unexpected teb->FlsSlots %p.\n", new_fls_data);
419 entry_count = check_linked_list(fls_list_head, &fls_data->fls_list_entry, &index);
420 ok(entry_count == 2, "Got unexpected count %u.\n", entry_count);
421 ok(!index, "Got unexpected index %u.\n", index);
422 check_linked_list(fls_list_head, &new_fls_data->fls_list_entry, &index);
423 ok(index == 1, "Got unexpected index %u.\n", index);
425 val = (void *)0xdeadbeef;
426 status = pRtlFlsGetValue(fls_indices[2], &val);
427 ok(!status, "Got unexpected status %#x.\n", status);
428 ok(!val, "Got unexpected val %p.\n", val);
431 /* With bit 0 of flags set RtlProcessFlsData is removing FLS data from the linked list
432 * and calls FLS callbacks. With bit 1 set the memory is freed. The remaining bits do not seem
433 * to have any obvious effect. */
434 for (i = 2; i < 32; ++i)
436 pRtlProcessFlsData(new_fls_data, 1 << i);
437 size = HeapSize(GetProcessHeap(), 0, new_fls_data);
438 ok(size == sizeof(*new_fls_data), "Got unexpected size %p.\n", (void *)size);
441 if (0)
443 pRtlProcessFlsData(new_fls_data, 2);
444 entry_count = check_linked_list(fls_list_head, &fls_data->fls_list_entry, &index);
445 ok(entry_count == 2, "Got unexpected count %u.\n", entry_count);
447 /* Crashes on Windows. */
448 HeapSize(GetProcessHeap(), 0, new_fls_data);
451 test_fiberlocalstorage_peb_locked_event = CreateEventA(NULL, FALSE, FALSE, NULL);
452 test_fiberlocalstorage_done_event = CreateEventA(NULL, FALSE, FALSE, NULL);
453 hthread = CreateThread(NULL, 0, test_FiberLocalStorage_thread, NULL, 0, NULL);
454 ok(!!hthread, "CreateThread failed.\n");
455 result = WaitForSingleObject(test_fiberlocalstorage_peb_locked_event, INFINITE);
456 ok(result == WAIT_OBJECT_0, "Got unexpected result %u.\n", result);
457 teb->FlsSlots = NULL;
459 test_fls_callback_call_count = 0;
460 saved_entry = new_fls_data->fls_list_entry;
461 pRtlProcessFlsData(new_fls_data, 1);
462 ok(!teb->FlsSlots, "Got unexpected teb->FlsSlots %p.\n", teb->FlsSlots);
464 teb->FlsSlots = fls_data;
465 ok(test_fls_callback_call_count == 1, "Got unexpected callback call count %u.\n",
466 test_fls_callback_call_count);
468 SetEvent(test_fiberlocalstorage_done_event);
469 WaitForSingleObject(hthread, INFINITE);
470 CloseHandle(hthread);
471 CloseHandle(test_fiberlocalstorage_peb_locked_event);
472 CloseHandle(test_fiberlocalstorage_done_event);
474 ok(new_fls_data->fls_list_entry.Flink == saved_entry.Flink, "Got unexpected Flink %p.\n",
475 saved_entry.Flink);
476 ok(new_fls_data->fls_list_entry.Blink == saved_entry.Blink, "Got unexpected Flink %p.\n",
477 saved_entry.Blink);
478 size = HeapSize(GetProcessHeap(), 0, new_fls_data);
479 ok(size == sizeof(*new_fls_data), "Got unexpected size %p.\n", (void *)size);
480 test_fls_callback_call_count = 0;
481 i = test_fls_chunk_index_from_index(fls_indices[1], &index);
482 new_fls_data->fls_data_chunks[i][index + 1] = (void *)(ULONG_PTR)0x28;
483 pRtlProcessFlsData(new_fls_data, 2);
484 ok(!test_fls_callback_call_count, "Got unexpected callback call count %u.\n",
485 test_fls_callback_call_count);
487 if (0)
489 /* crashes on Windows. */
490 HeapSize(GetProcessHeap(), 0, new_fls_data);
493 entry_count = check_linked_list(fls_list_head, &fls_data->fls_list_entry, &index);
494 ok(entry_count == 1, "Got unexpected count %u.\n", entry_count);
495 ok(!index, "Got unexpected index %u.\n", index);
497 else
499 win_skip("Old FLS data storage layout, skipping test.\n");
500 g_fls_data = NULL;
503 if (0)
505 /* crashes on Windows. */
506 pRtlFlsGetValue(fls_indices[0], NULL);
509 for (i = 0; i < count; ++i)
511 if (pRtlFlsGetValue)
513 status = pRtlFlsGetValue(fls_indices[i], &val);
514 ok(!status, "Got unexpected status %#x.\n", status);
515 ok(val == (void *)(ULONG_PTR)(i + 1), "Got unexpected val %p, i %u.\n", val, i);
518 status = pRtlFlsFree(fls_indices[i]);
519 ok(!status, "Got unexpected status %#x, i %u.\n", status, i);
522 if (!peb->FlsCallback)
524 ok(g_fls_data->fls_high_index == 0xfef, "Got unexpected fls_high_index %#x.\n",
525 g_fls_data->fls_high_index);
527 for (i = 0; i < 8; ++i)
529 ok(!!g_fls_data->fls_callback_chunks[i], "Got zero fls_callback_chunks[%u].\n", i);
530 ok(!!fls_data->fls_data_chunks[i], "Got zero fls_data->fls_data_chunks[%u].\n", i);
534 else
536 win_skip("RtlFlsAlloc is not available.\n");
539 /* Test an unallocated index
540 * FlsFree should fail
541 * FlsGetValue and FlsSetValue should succeed
543 SetLastError( 0xdeadbeef );
544 ret = pFlsFree( 127 );
545 ok( !ret, "freeing fls index 127 (unallocated) succeeded\n" );
546 ok( GetLastError() == ERROR_INVALID_PARAMETER,
547 "freeing fls index 127 (unallocated) wrong error %u\n", GetLastError() );
549 val = pFlsGetValue( 127 );
550 ok( val == NULL,
551 "getting fls index 127 (unallocated) failed with error %u\n", GetLastError() );
553 if (pRtlFlsGetValue)
555 val = (void *)0xdeadbeef;
556 status = pRtlFlsGetValue(127, &val);
557 ok( !status, "Got unexpected status %#x.\n", status );
558 ok( !val, "Got unexpected val %p.\n", val );
561 ret = pFlsSetValue( 127, (void*) 0x217 );
562 ok( ret, "setting fls index 127 (unallocated) failed with error %u\n", GetLastError() );
564 SetLastError( 0xdeadbeef );
565 val = pFlsGetValue( 127 );
566 ok( val == (void*) 0x217, "fls index 127 (unallocated) wrong value %p\n", val );
567 ok( GetLastError() == ERROR_SUCCESS,
568 "getting fls index 127 (unallocated) failed with error %u\n", GetLastError() );
570 if (pRtlFlsGetValue)
572 val = (void *)0xdeadbeef;
573 status = pRtlFlsGetValue(127, &val);
574 ok( !status, "Got unexpected status %#x.\n", status );
575 ok( val == (void*)0x217, "Got unexpected val %p.\n", val );
578 /* FlsFree, FlsGetValue, and FlsSetValue out of bounds should return
579 * ERROR_INVALID_PARAMETER
581 SetLastError( 0xdeadbeef );
582 ret = pFlsFree( 128 );
583 ok( !ret, "freeing fls index 128 (out of bounds) succeeded\n" );
584 ok( GetLastError() == ERROR_INVALID_PARAMETER,
585 "freeing fls index 128 (out of bounds) wrong error %u\n", GetLastError() );
587 SetLastError( 0xdeadbeef );
588 ret = pFlsSetValue( 128, (void*) 0x217 );
589 ok( ret || GetLastError() == ERROR_INVALID_PARAMETER,
590 "setting fls index 128 (out of bounds) wrong error %u\n", GetLastError() );
592 SetLastError( 0xdeadbeef );
593 val = pFlsGetValue( 128 );
594 ok( GetLastError() == ERROR_INVALID_PARAMETER || val == (void *)0x217,
595 "getting fls index 128 (out of bounds) wrong error %u\n", GetLastError() );
597 /* Test index 0 */
598 SetLastError( 0xdeadbeef );
599 val = pFlsGetValue( 0 );
600 ok( !val, "fls index 0 set to %p\n", val );
601 ok( GetLastError() == ERROR_INVALID_PARAMETER, "setting fls index wrong error %u\n", GetLastError() );
602 if (pRtlFlsGetValue)
604 val = (void *)0xdeadbeef;
605 status = pRtlFlsGetValue(0, &val);
606 ok( status == STATUS_INVALID_PARAMETER, "Got unexpected status %#x.\n", status );
607 ok( val == (void*)0xdeadbeef, "Got unexpected val %p.\n", val );
610 SetLastError( 0xdeadbeef );
611 ret = pFlsSetValue( 0, (void *)0xdeadbeef );
612 ok( !ret, "setting fls index 0 succeeded\n" );
613 ok( GetLastError() == ERROR_INVALID_PARAMETER, "setting fls index wrong error %u\n", GetLastError() );
614 if (pRtlFlsSetValue)
616 status = pRtlFlsSetValue( 0, (void *)0xdeadbeef );
617 ok( status == STATUS_INVALID_PARAMETER, "Got unexpected status %#x.\n", status );
619 SetLastError( 0xdeadbeef );
620 val = pFlsGetValue( 0 );
621 ok( !val, "fls index 0 wrong value %p\n", val );
622 ok( GetLastError() == ERROR_INVALID_PARAMETER, "setting fls index wrong error %u\n", GetLastError() );
624 /* Test creating an FLS index */
625 fls = pFlsAlloc( NULL );
626 ok( fls != FLS_OUT_OF_INDEXES, "FlsAlloc failed\n" );
627 ok( fls != 0, "fls index 0 allocated\n" );
628 val = pFlsGetValue( fls );
629 ok( !val, "fls index %u wrong value %p\n", fls, val );
630 SetLastError( 0xdeadbeef );
631 ret = pFlsSetValue( fls, (void *)0xdeadbeef );
632 ok( ret, "setting fls index %u failed\n", fls );
633 ok( GetLastError() == 0xdeadbeef, "setting fls index wrong error %u\n", GetLastError() );
634 SetLastError( 0xdeadbeef );
635 val = pFlsGetValue( fls );
636 ok( val == (void *)0xdeadbeef, "fls index %u wrong value %p\n", fls, val );
637 ok( GetLastError() == ERROR_SUCCESS,
638 "getting fls index %u failed with error %u\n", fls, GetLastError() );
639 pFlsFree( fls );
641 /* Undefined behavior: verify the value is NULL after it the slot is freed */
642 SetLastError( 0xdeadbeef );
643 val = pFlsGetValue( fls );
644 ok( val == NULL, "fls index %u wrong value %p\n", fls, val );
645 ok( GetLastError() == ERROR_SUCCESS,
646 "getting fls index %u failed with error %u\n", fls, GetLastError() );
648 /* Undefined behavior: verify the value is settable after the slot is freed */
649 ret = pFlsSetValue( fls, (void *)0xdeadbabe );
650 ok( ret, "setting fls index %u failed\n", fls );
651 val = pFlsGetValue( fls );
652 ok( val == (void *)0xdeadbabe, "fls index %u wrong value %p\n", fls, val );
654 /* Try to create the same FLS index again, and verify that is initialized to NULL */
655 fls_2 = pFlsAlloc( NULL );
656 ok( fls != FLS_OUT_OF_INDEXES, "FlsAlloc failed with error %u\n", GetLastError() );
657 /* If this fails it is not an API error, but the test will be inconclusive */
658 ok( fls_2 == fls, "different FLS index allocated, was %u, now %u\n", fls, fls_2 );
660 SetLastError( 0xdeadbeef );
661 val = pFlsGetValue( fls_2 );
662 ok( val == NULL || val == (void *)0xdeadbabe, "fls index %u wrong value %p\n", fls, val );
663 ok( GetLastError() == ERROR_SUCCESS,
664 "getting fls index %u failed with error %u\n", fls_2, GetLastError() );
665 pFlsFree( fls_2 );
668 static void test_FiberLocalStorageCallback(PFLS_CALLBACK_FUNCTION cbfunc)
670 DWORD fls;
671 BOOL ret;
672 void* val, *val2;
674 if (!pFlsAlloc || !pFlsSetValue || !pFlsGetValue || !pFlsFree)
676 win_skip( "Fiber Local Storage not supported\n" );
677 return;
680 /* Test that the callback is executed */
681 cbCount = 0;
682 fls = pFlsAlloc( cbfunc );
683 ok( fls != FLS_OUT_OF_INDEXES, "FlsAlloc failed with error %u\n", GetLastError() );
685 val = (void*) 0x1587;
686 fls_value_to_set = val;
687 ret = pFlsSetValue( fls, val );
688 ok(ret, "FlsSetValue failed with error %u\n", GetLastError() );
690 val2 = pFlsGetValue( fls );
691 ok(val == val2, "FlsGetValue returned %p, expected %p\n", val2, val);
693 ret = pFlsFree( fls );
694 ok(ret, "FlsFree failed with error %u\n", GetLastError() );
695 ok( cbCount == 1, "Wrong callback count: %d\n", cbCount );
697 /* Test that callback is not executed if value is NULL */
698 cbCount = 0;
699 fls = pFlsAlloc( cbfunc );
700 ok( fls != FLS_OUT_OF_INDEXES, "FlsAlloc failed with error %u\n", GetLastError() );
702 ret = pFlsSetValue( fls, NULL );
703 ok( ret, "FlsSetValue failed with error %u\n", GetLastError() );
705 pFlsFree( fls );
706 ok( ret, "FlsFree failed with error %u\n", GetLastError() );
707 ok( cbCount == 0, "Wrong callback count: %d\n", cbCount );
710 static void test_FiberLocalStorageWithFibers(PFLS_CALLBACK_FUNCTION cbfunc)
712 void* val1 = (void*) 0x314;
713 void* val2 = (void*) 0x152;
714 BOOL ret;
716 if (!pFlsAlloc || !pFlsFree || !pFlsSetValue || !pFlsGetValue)
718 win_skip( "Fiber Local Storage not supported\n" );
719 return;
722 fls_index_to_set = pFlsAlloc(cbfunc);
723 ok(fls_index_to_set != FLS_OUT_OF_INDEXES, "FlsAlloc failed with error %u\n", GetLastError());
725 test_ConvertThreadToFiber();
727 fiberCount = 0;
728 cbCount = 0;
729 fibers[1] = pCreateFiber(0,FiberMainProc,&testparam);
730 fibers[2] = pCreateFiber(0,FiberMainProc,&testparam);
731 ok(fibers[1] != NULL, "CreateFiber failed with error %u\n", GetLastError());
732 ok(fibers[2] != NULL, "CreateFiber failed with error %u\n", GetLastError());
733 ok(fiberCount == 0, "Wrong fiber count: %d\n", fiberCount);
734 ok(cbCount == 0, "Wrong callback count: %d\n", cbCount);
736 fiberCount = 0;
737 cbCount = 0;
738 fls_value_to_set = val1;
739 pSwitchToFiber(fibers[1]);
740 ok(fiberCount == 1, "Wrong fiber count: %d\n", fiberCount);
741 ok(cbCount == 0, "Wrong callback count: %d\n", cbCount);
743 fiberCount = 0;
744 cbCount = 0;
745 fls_value_to_set = val2;
746 pSwitchToFiber(fibers[2]);
747 ok(fiberCount == 1, "Wrong fiber count: %d\n", fiberCount);
748 ok(cbCount == 0, "Wrong callback count: %d\n", cbCount);
750 fls_value_to_set = val2;
751 ret = pFlsSetValue(fls_index_to_set, fls_value_to_set);
752 ok(ret, "FlsSetValue failed\n");
753 ok(val2 == pFlsGetValue(fls_index_to_set), "FlsGetValue failed\n");
755 fiberCount = 0;
756 cbCount = 0;
757 fls_value_to_set = val1;
758 pDeleteFiber(fibers[1]);
759 ok(fiberCount == 0, "Wrong fiber count: %d\n", fiberCount);
760 ok(cbCount == 1, "Wrong callback count: %d\n", cbCount);
762 fiberCount = 0;
763 cbCount = 0;
764 fls_value_to_set = val2;
765 pFlsFree(fls_index_to_set);
766 ok(fiberCount == 0, "Wrong fiber count: %d\n", fiberCount);
767 ok(cbCount == 2, "Wrong callback count: %d\n", cbCount);
769 fiberCount = 0;
770 cbCount = 0;
771 fls_value_to_set = val1;
772 pDeleteFiber(fibers[2]);
773 ok(fiberCount == 0, "Wrong fiber count: %d\n", fiberCount);
774 ok(cbCount == 0, "Wrong callback count: %d\n", cbCount);
776 test_ConvertFiberToThread();
779 static void WINAPI fls_exit_deadlock_callback(void *arg)
781 if (arg == (void *)1)
782 Sleep(INFINITE);
783 if (arg == (void *)2)
784 /* Unfortunately this test won't affect the exit code if it fails, but
785 * at least it will print a failure message. */
786 ok(RtlDllShutdownInProgress(), "expected DLL shutdown\n");
789 static DWORD CALLBACK fls_exit_deadlock_thread(void *arg)
791 FlsSetValue((DWORD_PTR)arg, (void *)1);
792 return 0;
795 static void fls_exit_deadlock_child(void)
797 DWORD index = FlsAlloc(fls_exit_deadlock_callback);
798 FlsSetValue(index, (void *)2);
799 CreateThread(NULL, 0, fls_exit_deadlock_thread, (void *)(DWORD_PTR)index, 0, NULL);
800 Sleep(100);
801 ExitProcess(0);
804 static void test_fls_exit_deadlock(void)
806 char **argv, cmdline[MAX_PATH];
807 PROCESS_INFORMATION pi;
808 STARTUPINFOA si = {0};
809 BOOL ret;
811 /* Regression test for the following deadlock:
813 * Thread A Thread B
814 * -----------------------------
815 * ExitThread
816 * acquire FLS lock
817 * call FLS callback
818 * ExitProcess
819 * terminate thread A
820 * acquire FLS lock
822 * Thread B deadlocks on acquiring the FLS lock (in order to itself call its
823 * FLS callbacks.)
826 winetest_get_mainargs(&argv);
827 sprintf(cmdline, "%s %s fls_exit_deadlock", argv[0], argv[1]);
828 ret = CreateProcessA(NULL, cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
829 ok(ret, "failed to create child, error %u\n", GetLastError());
830 ret = WaitForSingleObject(pi.hProcess, 1000);
831 ok(!ret, "wait failed\n");
832 CloseHandle(pi.hProcess);
833 CloseHandle(pi.hThread);
836 START_TEST(fiber)
838 char **argv;
839 int argc;
841 argc = winetest_get_mainargs(&argv);
843 if (argc == 3 && !strcmp(argv[2], "fls_exit_deadlock"))
845 fls_exit_deadlock_child();
846 return;
849 init_funcs();
851 if (!pCreateFiber)
853 win_skip( "Fibers not supported by win95\n" );
854 return;
857 test_FiberHandling();
858 test_FiberLocalStorage();
859 test_FiberLocalStorageCallback(FiberLocalStorageProc);
860 test_FiberLocalStorageWithFibers(FiberLocalStorageProc);
861 test_fls_exit_deadlock();