1// Make sure that ASan works with SEH in both Clang and MSVC. MSVC uses a
2// different EH personality depending on the -GS setting, so test both -GS+ and
3// -GS-.
4//
5// RUN: cl -c %s -Fo%t.obj -DCOMPILE_SEH
6// RUN: %clangxx_asan -o %t.exe %s %t.obj
7// RUN: %run %t.exe
8//
9// RUN: cl -GS- -c %s -Fo%t.obj -DCOMPILE_SEH
10// RUN: %clangxx_asan -o %t.exe %s %t.obj
11// RUN: %run %t.exe
12//
13// RUN: %clang_cl_asan %s -DCOMPILE_SEH -Fe%t.exe
14// RUN: %run %t.exe
15
16#include <windows.h>
17#include <assert.h>
18#include <stdio.h>
19
20// Should just "#include <sanitizer/asan_interface.h>" when C++ exceptions are
21// supported and we don't need to use CL.
22extern "C" bool __asan_address_is_poisoned(void *p);
23
24void ThrowAndCatch();
25
26#if defined(COMPILE_SEH)
27__declspec(noinline)
28void Throw() {
29  int local, zero = 0;
30  fprintf(stderr, "Throw:  %p\n", &local);
31  local = 5 / zero;
32}
33
34__declspec(noinline)
35void ThrowAndCatch() {
36  int local;
37  __try {
38    Throw();
39  } __except(EXCEPTION_EXECUTE_HANDLER) {
40    fprintf(stderr, "__except:  %p\n", &local);
41  }
42}
43#endif
44
45#if defined(__clang__)
46int main() {
47  char x[32];
48  fprintf(stderr, "Before: %p poisoned: %d\n", &x,
49          __asan_address_is_poisoned(x + 32));
50  assert(__asan_address_is_poisoned(x + 32));
51  ThrowAndCatch();
52  fprintf(stderr, "After:  %p poisoned: %d\n",  &x,
53          __asan_address_is_poisoned(x + 32));
54  // FIXME: Invert this assertion once we fix
55  // https://code.google.com/p/address-sanitizer/issues/detail?id=258
56  assert(!__asan_address_is_poisoned(x + 32));
57}
58#endif
59