blob: fc034378781a2fa0c93fdf9d2d547b517c47b5d7 [file] [log] [blame]
Louis Dionnecb96c632022-04-03 08:55:57 -04001// -*- 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 Dionne31359e02022-03-02 17:49:13 -050010// TODO: Figure out why this fails with Memory Sanitizer.
11// XFAIL: msan
12
Florian Mayer47974e92022-08-04 15:02:52 -070013#undef NDEBUG
Logan Chiena54f0962015-05-29 15:33:38 +000014#include <assert.h>
15#include <stdlib.h>
16#include <unwind.h>
17
18#define EXPECTED_NUM_FRAMES 50
19#define NUM_FRAMES_UPPER_BOUND 100
20
21_Unwind_Reason_Code callback(_Unwind_Context *context, void *cnt) {
Jonathan Roelofs0fedff12017-07-06 15:20:12 +000022 (void)context;
Logan Chiena54f0962015-05-29 15:33:38 +000023 int *i = (int *)cnt;
24 ++*i;
25 if (*i > NUM_FRAMES_UPPER_BOUND) {
26 abort();
27 }
28 return _URC_NO_REASON;
29}
30
31void test_backtrace() {
32 int n = 0;
33 _Unwind_Backtrace(&callback, &n);
34 if (n < EXPECTED_NUM_FRAMES) {
35 abort();
36 }
37}
38
39int test(int i) {
40 if (i == 0) {
41 test_backtrace();
42 return 0;
43 } else {
44 return i + test(i - 1);
45 }
46}
47
Louis Dionnecbfe0172020-10-08 13:36:33 -040048int main(int, char**) {
Logan Chiena54f0962015-05-29 15:33:38 +000049 int total = test(50);
50 assert(total == 1275);
Louis Dionnecbfe0172020-10-08 13:36:33 -040051 return 0;
Logan Chiena54f0962015-05-29 15:33:38 +000052}