1 // TODO: Investigate these failures
2 // XFAIL: asan, tsan, ubsan
3 
4 #include <assert.h>
5 #include <stdlib.h>
6 #include <unwind.h>
7 
8 #define EXPECTED_NUM_FRAMES 50
9 #define NUM_FRAMES_UPPER_BOUND 100
10 
11 _Unwind_Reason_Code callback(_Unwind_Context *context, void *cnt) {
12   (void)context;
13   int *i = (int *)cnt;
14   ++*i;
15   if (*i > NUM_FRAMES_UPPER_BOUND) {
16     abort();
17   }
18   return _URC_NO_REASON;
19 }
20 
21 void test_backtrace() {
22   int n = 0;
23   _Unwind_Backtrace(&callback, &n);
24   if (n < EXPECTED_NUM_FRAMES) {
25     abort();
26   }
27 }
28 
29 int test(int i) {
30   if (i == 0) {
31     test_backtrace();
32     return 0;
33   } else {
34     return i + test(i - 1);
35   }
36 }
37 
38 int main(int, char**) {
39   int total = test(50);
40   assert(total == 1275);
41   return 0;
42 }
43