• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <setjmp.h>
8 #include <stdlib.h>
9 #include <stdio.h>
10 #include <string.h>
11 #include <sanitizer/asan_interface.h>
12 
13 __attribute__((noinline))
Throw()14 void Throw() {
15   int local;
16   fprintf(stderr, "Throw:  %p\n", &local);
17   throw 1;
18 }
19 
20 __attribute__((noinline))
ThrowAndCatch()21 void ThrowAndCatch() {
22   int local;
23   try {
24     Throw();
25   } catch(...) {
26     fprintf(stderr, "Catch:  %p\n", &local);
27   }
28 }
29 
TestThrow()30 void TestThrow() {
31   char x[32];
32   fprintf(stderr, "Before: %p poisoned: %d\n", &x,
33           __asan_address_is_poisoned(x + 32));
34   ThrowAndCatch();
35   fprintf(stderr, "After:  %p poisoned: %d\n",  &x,
36           __asan_address_is_poisoned(x + 32));
37   // FIXME: Invert this assertion once we fix
38   // https://code.google.com/p/address-sanitizer/issues/detail?id=258
39   assert(!__asan_address_is_poisoned(x + 32));
40 }
41 
TestThrowInline()42 void TestThrowInline() {
43   char x[32];
44   fprintf(stderr, "Before: %p poisoned: %d\n", &x,
45           __asan_address_is_poisoned(x + 32));
46   try {
47     Throw();
48   } catch(...) {
49     fprintf(stderr, "Catch\n");
50   }
51   fprintf(stderr, "After:  %p poisoned: %d\n",  &x,
52           __asan_address_is_poisoned(x + 32));
53   // FIXME: Invert this assertion once we fix
54   // https://code.google.com/p/address-sanitizer/issues/detail?id=258
55   assert(!__asan_address_is_poisoned(x + 32));
56 }
57 
58 static jmp_buf buf;
59 
TestLongJmp()60 void TestLongJmp() {
61   char x[32];
62   fprintf(stderr, "\nTestLongJmp\n");
63   fprintf(stderr, "Before: %p poisoned: %d\n", &x,
64           __asan_address_is_poisoned(x + 32));
65   if (0 == setjmp(buf))
66     longjmp(buf, 1);
67   fprintf(stderr, "After:  %p poisoned: %d\n",  &x,
68           __asan_address_is_poisoned(x + 32));
69   // FIXME: Invert this assertion once we fix
70   // https://code.google.com/p/address-sanitizer/issues/detail?id=258
71   assert(!__asan_address_is_poisoned(x + 32));
72 }
73 
main(int argc,char ** argv)74 int main(int argc, char **argv) {
75   TestThrow();
76   TestThrowInline();
77   TestLongJmp();
78 }
79