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 // REQUIRES: linux 11 12 // TODO: Investigate these failures 13 // XFAIL: asan, tsan, ubsan 14 15 // TODO: Investigate this failure 16 // XFAIL: 32bits-on-64bits 17 18 // Basic test for _Unwind_ForcedUnwind. 19 // See libcxxabi/test/forced_unwind* tests too. 20 21 #include <assert.h> 22 #include <dlfcn.h> 23 #include <signal.h> 24 #include <stdint.h> 25 #include <stdio.h> 26 #include <stdlib.h> 27 #include <string.h> 28 #include <sys/types.h> 29 #include <unistd.h> 30 #include <unwind.h> 31 32 void foo(); 33 _Unwind_Exception ex; 34 35 _Unwind_Reason_Code stop(int version, _Unwind_Action actions, 36 _Unwind_Exception_Class exceptionClass, 37 _Unwind_Exception *exceptionObject, 38 struct _Unwind_Context *context, 39 void *stop_parameter) { 40 assert(version == 1); 41 assert((actions & _UA_FORCE_UNWIND) != 0); 42 (void)exceptionClass; 43 assert(exceptionObject == &ex); 44 assert(stop_parameter == &foo); 45 46 Dl_info info = {0, 0, 0, 0}; 47 48 // Unwind util the main is reached, above frames depend on the platform and 49 // architecture. 50 if (dladdr(reinterpret_cast<void *>(_Unwind_GetIP(context)), &info) && 51 info.dli_sname && !strcmp("main", info.dli_sname)) { 52 _Exit(0); 53 } 54 return _URC_NO_REASON; 55 } 56 57 __attribute__((noinline)) void foo() { 58 59 // Arm EHABI defines struct _Unwind_Control_Block as exception 60 // object. Ensure struct _Unwind_Exception* work there too, 61 // because _Unwind_Exception in this case is just an alias. 62 struct _Unwind_Exception *e = &ex; 63 #if defined(_LIBUNWIND_ARM_EHABI) 64 // Create a mock exception object. 65 memset(e, '\0', sizeof(*e)); 66 strcpy(reinterpret_cast<char *>(&e->exception_class), "CLNGUNW"); 67 #endif 68 _Unwind_ForcedUnwind(e, stop, (void *)&foo); 69 } 70 71 int main() { 72 foo(); 73 return -2; 74 } 75