• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Clang doesn't support SEH on Windows yet, so for the time being we
2 // build this program in two parts: the code with SEH is built with CL,
3 // the rest is built with Clang.  This represents the typical scenario when we
4 // build a large project using "clang-cl -fallback -fsanitize=address".
5 //
6 // RUN: %clang_cl_asan -O0 %p/dll_host.cc -Fe%t
7 //
8 // Check both -GS and -GS- builds:
9 // RUN: cl -LD -c %s -Fo%t.obj
10 // RUN: %clang_cl_asan -LD -O0 %s -Fe%t.dll %t.obj
11 // RUN: %run %t %t.dll
12 //
13 // RUN: cl -LD -GS- -c %s -Fo%t.obj
14 // RUN: %clang_cl_asan -LD -O0 %s -Fe%t.dll %t.obj
15 // RUN: %run %t %t.dll
16 
17 #include <windows.h>
18 #include <assert.h>
19 #include <stdio.h>
20 
21 // Should just "#include <sanitizer/asan_interface.h>" when C++ exceptions are
22 // supported and we don't need to use CL.
23 extern "C" bool __asan_address_is_poisoned(void *p);
24 
25 void ThrowAndCatch();
26 
27 #if !defined(__clang__)
28 __declspec(noinline)
Throw()29 void Throw() {
30   int local, zero = 0;
31   fprintf(stderr, "Throw:  %p\n", &local);
32   local = 5 / zero;
33 }
34 
35 __declspec(noinline)
ThrowAndCatch()36 void ThrowAndCatch() {
37   int local;
38   __try {
39     Throw();
40   } __except(EXCEPTION_EXECUTE_HANDLER) {
41     fprintf(stderr, "__except:  %p\n", &local);
42   }
43 }
44 #else
45 
46 extern "C" __declspec(dllexport)
test_function()47 int test_function() {
48   char x[32];
49   fprintf(stderr, "Before: %p poisoned: %d\n", &x,
50           __asan_address_is_poisoned(x + 32));
51   assert(__asan_address_is_poisoned(x + 32));
52   ThrowAndCatch();
53   fprintf(stderr, "After:  %p poisoned: %d\n",  &x,
54           __asan_address_is_poisoned(x + 32));
55   // FIXME: Invert this assertion once we fix
56   // https://code.google.com/p/address-sanitizer/issues/detail?id=258
57   assert(!__asan_address_is_poisoned(x + 32));
58   return 0;
59 }
60 #endif
61