1 // Make sure we can throw exceptions from work items executed via
2 // QueueUserWorkItem.
3 //
4 // Clang doesn't support exceptions on Windows yet, so for the time being we
5 // build this program in two parts: the code with exceptions is built with CL,
6 // the rest is built with Clang.  This represents the typical scenario when we
7 // build a large project using "clang-cl -fallback -fsanitize=address".
8 //
9 // RUN: cl -c %s -Fo%t.obj
10 // RUN: %clangxx_asan -o %t.exe %s %t.obj
11 // RUN: %run %t.exe 2>&1 | FileCheck %s
12 
13 #include <windows.h>
14 #include <stdio.h>
15 
16 void ThrowAndCatch();
17 
18 #if !defined(__clang__)
19 __declspec(noinline)
Throw()20 void Throw() {
21   fprintf(stderr, "Throw\n");
22 // CHECK: Throw
23   throw 1;
24 }
25 
ThrowAndCatch()26 void ThrowAndCatch() {
27   int local;
28   try {
29     Throw();
30   } catch(...) {
31     fprintf(stderr, "Catch\n");
32 // CHECK: Catch
33   }
34 }
35 #else
36 
37 HANDLE done;
38 
work_item(LPVOID)39 DWORD CALLBACK work_item(LPVOID) {
40   ThrowAndCatch();
41   SetEvent(done);
42   return 0;
43 }
44 
main(int argc,char ** argv)45 int main(int argc, char **argv) {
46   done = CreateEvent(0, false, false, "job is done");
47   if (!done)
48     return 1;
49   QueueUserWorkItem(&work_item, nullptr, 0);
50   if (WAIT_OBJECT_0 != WaitForSingleObject(done, INFINITE))
51     return 2;
52   fprintf(stderr, "Done!\n");
53 // CHECK: Done!
54 }
55 #endif
56