xref: /llvm-project/compiler-rt/test/asan/TestCases/throw_catch.cpp (revision c4992bf593a4fd2fd250c5ebe31aa9f26cc9ed40)
1*c4992bf5SKevin Athey // RUN: %clangxx_asan -fsanitize-address-use-after-return=never -O %s -o %t && %run %t
2673dc3d4SNico Weber 
3673dc3d4SNico Weber #include <assert.h>
4673dc3d4SNico Weber #include <stdio.h>
5673dc3d4SNico Weber #include <sanitizer/asan_interface.h>
6673dc3d4SNico Weber 
7673dc3d4SNico Weber __attribute__((noinline))
Throw()8673dc3d4SNico Weber void Throw() {
9673dc3d4SNico Weber   int local;
10673dc3d4SNico Weber   fprintf(stderr, "Throw:  %p\n", &local);
11673dc3d4SNico Weber   throw 1;
12673dc3d4SNico Weber }
13673dc3d4SNico Weber 
14673dc3d4SNico Weber __attribute__((noinline))
ThrowAndCatch()15673dc3d4SNico Weber void ThrowAndCatch() {
16673dc3d4SNico Weber   int local;
17673dc3d4SNico Weber   try {
18673dc3d4SNico Weber     Throw();
19673dc3d4SNico Weber   } catch(...) {
20673dc3d4SNico Weber     fprintf(stderr, "Catch:  %p\n", &local);
21673dc3d4SNico Weber   }
22673dc3d4SNico Weber }
23673dc3d4SNico Weber 
24673dc3d4SNico Weber __attribute__((noinline))
TestThrow()25673dc3d4SNico Weber void TestThrow() {
26673dc3d4SNico Weber   char x[32];
27673dc3d4SNico Weber   fprintf(stderr, "Before: %p poisoned: %d\n", &x,
28673dc3d4SNico Weber           __asan_address_is_poisoned(x + 32));
29673dc3d4SNico Weber   assert(__asan_address_is_poisoned(x + 32));
30673dc3d4SNico Weber   ThrowAndCatch();
31673dc3d4SNico Weber   fprintf(stderr, "After:  %p poisoned: %d\n",  &x,
32673dc3d4SNico Weber           __asan_address_is_poisoned(x + 32));
33673dc3d4SNico Weber   assert(!__asan_address_is_poisoned(x + 32));
34673dc3d4SNico Weber }
35673dc3d4SNico Weber 
36673dc3d4SNico Weber __attribute__((noinline))
TestThrowInline()37673dc3d4SNico Weber void TestThrowInline() {
38673dc3d4SNico Weber   char x[32];
39673dc3d4SNico Weber   fprintf(stderr, "Before: %p poisoned: %d\n", &x,
40673dc3d4SNico Weber           __asan_address_is_poisoned(x + 32));
41673dc3d4SNico Weber   assert(__asan_address_is_poisoned(x + 32));
42673dc3d4SNico Weber   try {
43673dc3d4SNico Weber     Throw();
44673dc3d4SNico Weber   } catch(...) {
45673dc3d4SNico Weber     fprintf(stderr, "Catch\n");
46673dc3d4SNico Weber   }
47673dc3d4SNico Weber   fprintf(stderr, "After:  %p poisoned: %d\n",  &x,
48673dc3d4SNico Weber           __asan_address_is_poisoned(x + 32));
49673dc3d4SNico Weber   assert(!__asan_address_is_poisoned(x + 32));
50673dc3d4SNico Weber }
51673dc3d4SNico Weber 
main(int argc,char ** argv)52673dc3d4SNico Weber int main(int argc, char **argv) {
53673dc3d4SNico Weber   TestThrowInline();
54673dc3d4SNico Weber   TestThrow();
55673dc3d4SNico Weber }
56