Daniel Kiss | 163101b | 2020-09-16 23:03:19 +0200 | [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 | |
| 10 | // Ensure that leaf function can be unwund. |
| 11 | // REQUIRES: x86_64-linux |
| 12 | |
| 13 | #include <assert.h> |
| 14 | #include <dlfcn.h> |
| 15 | #include <signal.h> |
| 16 | #include <stdio.h> |
| 17 | #include <stdlib.h> |
| 18 | #include <string.h> |
| 19 | #include <sys/types.h> |
| 20 | #include <unistd.h> |
| 21 | #include <unwind.h> |
| 22 | |
| 23 | _Unwind_Reason_Code frame_handler(struct _Unwind_Context* ctx, void* arg) { |
| 24 | (void)arg; |
| 25 | Dl_info info = { 0, 0, 0, 0 }; |
| 26 | assert(dladdr((void*)_Unwind_GetIP(ctx), &info)); |
| 27 | |
| 28 | // Unwind util the main is reached, above frames deeped on the platfrom and architecture. |
| 29 | if(info.dli_sname && !strcmp("main", info.dli_sname)) { |
| 30 | _Exit(0); |
| 31 | } |
| 32 | return _URC_NO_REASON; |
| 33 | } |
| 34 | |
| 35 | void signal_handler(int signum) { |
| 36 | (void)signum; |
| 37 | _Unwind_Backtrace(frame_handler, NULL); |
| 38 | _Exit(-1); |
| 39 | } |
| 40 | |
| 41 | int* faultyPointer = NULL; |
| 42 | |
| 43 | __attribute__((noinline)) void crashing_leaf_func(void) { |
| 44 | *faultyPointer = 0; |
| 45 | } |
| 46 | |
| 47 | int main() { |
| 48 | signal(SIGSEGV, signal_handler); |
| 49 | crashing_leaf_func(); |
| 50 | return -2; |
| 51 | } |