Louis Dionne | cb96c63 | 2022-04-03 08:55:57 -0400 | [diff] [blame] | 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 | |
Louis Dionne | 31359e0 | 2022-03-02 17:49:13 -0500 | [diff] [blame] | 10 | // TODO: Figure out why this fails with Memory Sanitizer. |
| 11 | // XFAIL: msan |
| 12 | |
Logan Chien | a54f096 | 2015-05-29 15:33:38 +0000 | [diff] [blame] | 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 | |
| 20 | _Unwind_Reason_Code callback(_Unwind_Context *context, void *cnt) { |
Jonathan Roelofs | 0fedff1 | 2017-07-06 15:20:12 +0000 | [diff] [blame] | 21 | (void)context; |
Logan Chien | a54f096 | 2015-05-29 15:33:38 +0000 | [diff] [blame] | 22 | int *i = (int *)cnt; |
| 23 | ++*i; |
| 24 | if (*i > NUM_FRAMES_UPPER_BOUND) { |
| 25 | abort(); |
| 26 | } |
| 27 | return _URC_NO_REASON; |
| 28 | } |
| 29 | |
| 30 | void test_backtrace() { |
| 31 | int n = 0; |
| 32 | _Unwind_Backtrace(&callback, &n); |
| 33 | if (n < EXPECTED_NUM_FRAMES) { |
| 34 | abort(); |
| 35 | } |
| 36 | } |
| 37 | |
| 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 | |
Louis Dionne | cbfe017 | 2020-10-08 13:36:33 -0400 | [diff] [blame] | 47 | int main(int, char**) { |
Logan Chien | a54f096 | 2015-05-29 15:33:38 +0000 | [diff] [blame] | 48 | int total = test(50); |
| 49 | assert(total == 1275); |
Louis Dionne | cbfe017 | 2020-10-08 13:36:33 -0400 | [diff] [blame] | 50 | return 0; |
Logan Chien | a54f096 | 2015-05-29 15:33:38 +0000 | [diff] [blame] | 51 | } |