xref: /llvm-project/compiler-rt/test/asan/TestCases/Windows/queue_user_work_item.cpp (revision 673dc3d4a0b0fbb3b9b34ae2ecbfa522627fe582)
1 // Make sure we can throw exceptions from work items executed via
2 // QueueUserWorkItem.
3 //
4 // RUN: %clangxx_asan %s -o %t.exe
5 // RUN: %run %t.exe 2>&1 | FileCheck %s
6 
7 #include <windows.h>
8 #include <stdio.h>
9 
10 void ThrowAndCatch();
11 
12 __declspec(noinline)
Throw()13 void Throw() {
14   fprintf(stderr, "Throw\n");
15 // CHECK: Throw
16   throw 1;
17 }
18 
ThrowAndCatch()19 void ThrowAndCatch() {
20   int local;
21   try {
22     Throw();
23   } catch(...) {
24     fprintf(stderr, "Catch\n");
25 // CHECK: Catch
26   }
27 }
28 
29 HANDLE done;
30 
work_item(LPVOID)31 DWORD CALLBACK work_item(LPVOID) {
32   ThrowAndCatch();
33   SetEvent(done);
34   return 0;
35 }
36 
main(int argc,char ** argv)37 int main(int argc, char **argv) {
38   done = CreateEvent(0, false, false, "job is done");
39   if (!done)
40     return 1;
41   QueueUserWorkItem(&work_item, nullptr, 0);
42   unsigned wait_result = WaitForSingleObject(done, 10 * 1000);
43   if (wait_result == WAIT_ABANDONED)
44     fprintf(stderr, "Timed out\n");
45   if (wait_result != WAIT_OBJECT_0) {
46     fprintf(stderr, "Wait for work item failed\n");
47     return 2;
48   }
49   fprintf(stderr, "Done!\n");
50 // CHECK: Done!
51 }
52