throw_catch.cc revision 2d1fdb26e458c4ddc04155c1d421bced3ba90cd0
1// RUN: %clangxx_asan -O %s -o %t && %run %t
2
3#include <assert.h>
4#include <setjmp.h>
5#include <stdlib.h>
6#include <stdio.h>
7#include <string.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  ThrowAndCatch();
32  fprintf(stderr, "After:  %p poisoned: %d\n",  &x,
33          __asan_address_is_poisoned(x + 32));
34  // FIXME: Invert this assertion once we fix
35  // https://code.google.com/p/address-sanitizer/issues/detail?id=258
36  assert(!__asan_address_is_poisoned(x + 32));
37}
38
39void TestThrowInline() {
40  char x[32];
41  fprintf(stderr, "Before: %p poisoned: %d\n", &x,
42          __asan_address_is_poisoned(x + 32));
43  try {
44    Throw();
45  } catch(...) {
46    fprintf(stderr, "Catch\n");
47  }
48  fprintf(stderr, "After:  %p poisoned: %d\n",  &x,
49          __asan_address_is_poisoned(x + 32));
50  // FIXME: Invert this assertion once we fix
51  // https://code.google.com/p/address-sanitizer/issues/detail?id=258
52  assert(!__asan_address_is_poisoned(x + 32));
53}
54
55static jmp_buf buf;
56
57void TestLongJmp() {
58  char x[32];
59  fprintf(stderr, "\nTestLongJmp\n");
60  fprintf(stderr, "Before: %p poisoned: %d\n", &x,
61          __asan_address_is_poisoned(x + 32));
62  if (0 == setjmp(buf))
63    longjmp(buf, 1);
64  fprintf(stderr, "After:  %p poisoned: %d\n",  &x,
65          __asan_address_is_poisoned(x + 32));
66  // FIXME: Invert this assertion once we fix
67  // https://code.google.com/p/address-sanitizer/issues/detail?id=258
68  assert(!__asan_address_is_poisoned(x + 32));
69}
70
71int main(int argc, char **argv) {
72  TestThrow();
73  TestThrowInline();
74  TestLongJmp();
75}
76