xref: /llvm-project/compiler-rt/test/asan/TestCases/uar_and_exceptions.cpp (revision 60084d490059719b950b5800715c3c0637218bc5)
1 // Test that use-after-return works with exceptions.
2 // RUN: %clangxx_asan -O0 %s -o %t
3 // RUN: %env_asan_opts=detect_stack_use_after_return=1 %run %t
4 // RUN: %clangxx_asan -O0 %s -o %t -fsanitize-address-use-after-return=always
5 // RUN: %run %t
6 
7 #include <stdio.h>
8 
9 volatile char *g;
10 
11 #ifndef FRAME_SIZE
12 # define FRAME_SIZE 100
13 #endif
14 
15 #ifndef NUM_ITER
16 # define NUM_ITER 4000
17 #endif
18 
19 #ifndef DO_THROW
20 # define DO_THROW 1
21 #endif
22 
Func(int depth)23 void Func(int depth) {
24   char frame[FRAME_SIZE];
25   g = &frame[0];
26   if (depth)
27     Func(depth - 1);
28   else if (DO_THROW)
29     throw 1;
30 }
31 
main(int argc,char ** argv)32 int main(int argc, char **argv) {
33   for (int i = 0; i < NUM_ITER; i++) {
34     try {
35       Func(argc * 100);
36     } catch(...) {
37     }
38     if ((i % (NUM_ITER / 10)) == 0)
39       fprintf(stderr, "done [%d]\n", i);
40   }
41   return 0;
42 }
43