1// RUN: %clangxx_asan -O %s -o %t && %run %t
2
3// Clang doesn't support exceptions on Windows yet.
4// XFAIL: win32
5
6#include <assert.h>
7#include <stdio.h>
8#include <sanitizer/asan_interface.h>
9
10__attribute__((noinline))
11void Throw() {
12  int local;
13  fprintf(stderr, "Throw:  %p\n", &local);
14  throw 1;
15}
16
17__attribute__((noinline))
18void ThrowAndCatch() {
19  int local;
20  try {
21    Throw();
22  } catch(...) {
23    fprintf(stderr, "Catch:  %p\n", &local);
24  }
25}
26
27void TestThrow() {
28  char x[32];
29  fprintf(stderr, "Before: %p poisoned: %d\n", &x,
30          __asan_address_is_poisoned(x + 32));
31  assert(__asan_address_is_poisoned(x + 32));
32  ThrowAndCatch();
33  fprintf(stderr, "After:  %p poisoned: %d\n",  &x,
34          __asan_address_is_poisoned(x + 32));
35  // FIXME: Invert this assertion once we fix
36  // https://code.google.com/p/address-sanitizer/issues/detail?id=258
37  // This assertion works only w/o UAR.
38  if (!__asan_get_current_fake_stack())
39    assert(!__asan_address_is_poisoned(x + 32));
40}
41
42void TestThrowInline() {
43  char x[32];
44  fprintf(stderr, "Before: %p poisoned: %d\n", &x,
45          __asan_address_is_poisoned(x + 32));
46  assert(__asan_address_is_poisoned(x + 32));
47  try {
48    Throw();
49  } catch(...) {
50    fprintf(stderr, "Catch\n");
51  }
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  // This assertion works only w/o UAR.
57  if (!__asan_get_current_fake_stack())
58    assert(!__asan_address_is_poisoned(x + 32));
59}
60
61int main(int argc, char **argv) {
62  TestThrowInline();
63  TestThrow();
64}
65