1 // -*- C++ -*-
2 //===----------------------------------------------------------------------===//
3 //
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //
8 //===----------------------------------------------------------------------===//
9 
10 // TODO: Figure out why this fails with Memory Sanitizer.
11 // XFAIL: msan
12 
13 #include <assert.h>
14 #include <stdlib.h>
15 #include <unwind.h>
16 
17 #define EXPECTED_NUM_FRAMES 50
18 #define NUM_FRAMES_UPPER_BOUND 100
19 
callback(_Unwind_Context * context,void * cnt)20 _Unwind_Reason_Code callback(_Unwind_Context *context, void *cnt) {
21   (void)context;
22   int *i = (int *)cnt;
23   ++*i;
24   if (*i > NUM_FRAMES_UPPER_BOUND) {
25     abort();
26   }
27   return _URC_NO_REASON;
28 }
29 
test_backtrace()30 void test_backtrace() {
31   int n = 0;
32   _Unwind_Backtrace(&callback, &n);
33   if (n < EXPECTED_NUM_FRAMES) {
34     abort();
35   }
36 }
37 
test(int i)38 int test(int i) {
39   if (i == 0) {
40     test_backtrace();
41     return 0;
42   } else {
43     return i + test(i - 1);
44   }
45 }
46 
main(int,char **)47 int main(int, char**) {
48   int total = test(50);
49   assert(total == 1275);
50   return 0;
51 }
52