blob: 40374553dfbd4b27425ef7d42372762102b5208b [file] [log] [blame]
Daniel Kiss163101b2020-09-16 23:03:19 +02001// -*- 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
10// Ensure that leaf function can be unwund.
Louis Dionneed4a85e2021-06-18 13:33:14 -040011// REQUIRES: linux && (target={{aarch64-.+}} || target={{x86_64-.+}})
Daniel Kiss163101b2020-09-16 23:03:19 +020012
Louis Dionne9915ba52021-09-30 15:11:48 -040013// TODO: Investigate these failures
14// XFAIL: asan, tsan, ubsan
15
Daniel Kiss163101b2020-09-16 23:03:19 +020016#include <assert.h>
17#include <dlfcn.h>
18#include <signal.h>
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22#include <sys/types.h>
23#include <unistd.h>
24#include <unwind.h>
25
26_Unwind_Reason_Code frame_handler(struct _Unwind_Context* ctx, void* arg) {
27 (void)arg;
28 Dl_info info = { 0, 0, 0, 0 };
Daniel Kiss163101b2020-09-16 23:03:19 +020029
30 // Unwind util the main is reached, above frames deeped on the platfrom and architecture.
Ryan Pricharda684bed2021-01-13 16:38:36 -080031 if (dladdr(reinterpret_cast<void *>(_Unwind_GetIP(ctx)), &info) &&
32 info.dli_sname && !strcmp("main", info.dli_sname)) {
Daniel Kiss163101b2020-09-16 23:03:19 +020033 _Exit(0);
34 }
35 return _URC_NO_REASON;
36}
37
38void signal_handler(int signum) {
39 (void)signum;
40 _Unwind_Backtrace(frame_handler, NULL);
41 _Exit(-1);
42}
43
44int* faultyPointer = NULL;
45
46__attribute__((noinline)) void crashing_leaf_func(void) {
47 *faultyPointer = 0;
48}
49
Louis Dionnecbfe0172020-10-08 13:36:33 -040050int main(int, char**) {
Daniel Kiss163101b2020-09-16 23:03:19 +020051 signal(SIGSEGV, signal_handler);
52 crashing_leaf_func();
53 return -2;
54}