blob: c0d6b05c2a72948209a23728cc5c99105e50c08f [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
Logan Chiena54f0962015-05-29 15:33:38 +000013#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 Roelofs0fedff12017-07-06 15:20:12 +000021 (void)context;
Logan Chiena54f0962015-05-29 15:33:38 +000022 int *i = (int *)cnt;
23 ++*i;
24 if (*i > NUM_FRAMES_UPPER_BOUND) {
25 abort();
26 }
27 return _URC_NO_REASON;
28}
29
30void test_backtrace() {
31 int n = 0;
32 _Unwind_Backtrace(&callback, &n);
33 if (n < EXPECTED_NUM_FRAMES) {
34 abort();
35 }
36}
37
38int 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 Dionnecbfe0172020-10-08 13:36:33 -040047int main(int, char**) {
Logan Chiena54f0962015-05-29 15:33:38 +000048 int total = test(50);
49 assert(total == 1275);
Louis Dionnecbfe0172020-10-08 13:36:33 -040050 return 0;
Logan Chiena54f0962015-05-29 15:33:38 +000051}