blob: a4f47c521858a62933d04bc57093870f40e2fa6b [file] [log] [blame]
Louis Dionne31359e02022-03-02 17:49:13 -05001// TODO: Figure out why this fails with Memory Sanitizer.
2// XFAIL: msan
3
Logan Chiena54f0962015-05-29 15:33:38 +00004#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) {
Jonathan Roelofs0fedff12017-07-06 15:20:12 +000012 (void)context;
Logan Chiena54f0962015-05-29 15:33:38 +000013 int *i = (int *)cnt;
14 ++*i;
15 if (*i > NUM_FRAMES_UPPER_BOUND) {
16 abort();
17 }
18 return _URC_NO_REASON;
19}
20
21void test_backtrace() {
22 int n = 0;
23 _Unwind_Backtrace(&callback, &n);
24 if (n < EXPECTED_NUM_FRAMES) {
25 abort();
26 }
27}
28
29int test(int i) {
30 if (i == 0) {
31 test_backtrace();
32 return 0;
33 } else {
34 return i + test(i - 1);
35 }
36}
37
Louis Dionnecbfe0172020-10-08 13:36:33 -040038int main(int, char**) {
Logan Chiena54f0962015-05-29 15:33:38 +000039 int total = test(50);
40 assert(total == 1275);
Louis Dionnecbfe0172020-10-08 13:36:33 -040041 return 0;
Logan Chiena54f0962015-05-29 15:33:38 +000042}