2022-04-03 08:55:57 -04:00
|
|
|
// -*- C++ -*-
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2022-03-02 17:49:13 -05:00
|
|
|
// TODO: Figure out why this fails with Memory Sanitizer.
|
|
|
|
// XFAIL: msan
|
|
|
|
|
2023-11-29 17:20:20 -05:00
|
|
|
// This test fails on older llvm, when built with picolibc.
|
|
|
|
// XFAIL: clang-16 && LIBCXX-PICOLIBC-FIXME
|
|
|
|
|
2022-08-04 15:02:52 -07:00
|
|
|
#undef NDEBUG
|
2015-05-29 15:33:38 +00:00
|
|
|
#include <assert.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unwind.h>
|
|
|
|
|
|
|
|
#define EXPECTED_NUM_FRAMES 50
|
|
|
|
#define NUM_FRAMES_UPPER_BOUND 100
|
|
|
|
|
2024-01-09 10:39:14 -05:00
|
|
|
__attribute__((noinline)) _Unwind_Reason_Code callback(_Unwind_Context *context,
|
|
|
|
void *cnt) {
|
2017-07-06 15:20:12 +00:00
|
|
|
(void)context;
|
2015-05-29 15:33:38 +00:00
|
|
|
int *i = (int *)cnt;
|
|
|
|
++*i;
|
|
|
|
if (*i > NUM_FRAMES_UPPER_BOUND) {
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
return _URC_NO_REASON;
|
|
|
|
}
|
|
|
|
|
2024-01-09 10:39:14 -05:00
|
|
|
__attribute__((noinline)) void test_backtrace() {
|
2015-05-29 15:33:38 +00:00
|
|
|
int n = 0;
|
|
|
|
_Unwind_Backtrace(&callback, &n);
|
|
|
|
if (n < EXPECTED_NUM_FRAMES) {
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-09 10:39:14 -05:00
|
|
|
// These functions are effectively the same, but we have to be careful to avoid
|
|
|
|
// unwanted optimizations that would mess with the number of frames we expect.
|
|
|
|
// Surprisingly, slapping `noinline` is not sufficient -- we also have to avoid
|
|
|
|
// writing the function in a way that the compiler can easily spot tail
|
|
|
|
// recursion.
|
|
|
|
__attribute__((noinline)) int test1(int i);
|
|
|
|
__attribute__((noinline)) int test2(int i);
|
|
|
|
|
|
|
|
__attribute__((noinline)) int test1(int i) {
|
|
|
|
if (i == 0) {
|
|
|
|
test_backtrace();
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return i + test2(i - 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
__attribute__((noinline)) int test2(int i) {
|
2015-05-29 15:33:38 +00:00
|
|
|
if (i == 0) {
|
|
|
|
test_backtrace();
|
|
|
|
return 0;
|
|
|
|
} else {
|
2024-01-09 10:39:14 -05:00
|
|
|
return i + test1(i - 1);
|
2015-05-29 15:33:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-08 13:36:33 -04:00
|
|
|
int main(int, char**) {
|
2024-01-09 10:39:14 -05:00
|
|
|
int total = test1(50);
|
2015-05-29 15:33:38 +00:00
|
|
|
assert(total == 1275);
|
2020-10-08 13:36:33 -04:00
|
|
|
return 0;
|
2015-05-29 15:33:38 +00:00
|
|
|
}
|