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