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: linux && (target={{aarch64-.+}} || target={{x86_64-.+}}) 12 13 // TODO: Investigate these failures 14 // XFAIL: asan, tsan, ubsan 15 16 // TODO: Investigate this failure 17 // XFAIL: 32bits-on-64bits 18 19 #include <assert.h> 20 #include <dlfcn.h> 21 #include <signal.h> 22 #include <stdio.h> 23 #include <stdlib.h> 24 #include <string.h> 25 #include <sys/types.h> 26 #include <unistd.h> 27 #include <unwind.h> 28 29 _Unwind_Reason_Code frame_handler(struct _Unwind_Context* ctx, void* arg) { 30 (void)arg; 31 Dl_info info = { 0, 0, 0, 0 }; 32 33 // Unwind util the main is reached, above frames deeped on the platfrom and architecture. 34 if (dladdr(reinterpret_cast<void *>(_Unwind_GetIP(ctx)), &info) && 35 info.dli_sname && !strcmp("main", info.dli_sname)) { 36 _Exit(0); 37 } 38 return _URC_NO_REASON; 39 } 40 41 void signal_handler(int signum) { 42 (void)signum; 43 _Unwind_Backtrace(frame_handler, NULL); 44 _Exit(-1); 45 } 46 47 int* faultyPointer = NULL; 48 49 __attribute__((noinline)) void crashing_leaf_func(void) { 50 *faultyPointer = 0; 51 } 52 53 int main(int, char**) { 54 signal(SIGSEGV, signal_handler); 55 crashing_leaf_func(); 56 return -2; 57 }