Louis Dionne | 31359e0 | 2022-03-02 17:49:13 -0500 | [diff] [blame^] | 1 | // TODO: Figure out why this fails with Memory Sanitizer. |
| 2 | // XFAIL: msan |
| 3 | |
Logan Chien | a54f096 | 2015-05-29 15:33:38 +0000 | [diff] [blame] | 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) { |
Jonathan Roelofs | 0fedff1 | 2017-07-06 15:20:12 +0000 | [diff] [blame] | 12 | (void)context; |
Logan Chien | a54f096 | 2015-05-29 15:33:38 +0000 | [diff] [blame] | 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 | |
Louis Dionne | cbfe017 | 2020-10-08 13:36:33 -0400 | [diff] [blame] | 38 | int main(int, char**) { |
Logan Chien | a54f096 | 2015-05-29 15:33:38 +0000 | [diff] [blame] | 39 | int total = test(50); |
| 40 | assert(total == 1275); |
Louis Dionne | cbfe017 | 2020-10-08 13:36:33 -0400 | [diff] [blame] | 41 | return 0; |
Logan Chien | a54f096 | 2015-05-29 15:33:38 +0000 | [diff] [blame] | 42 | } |