| 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: Figure out why this fails with Memory Sanitizer. |
| 13 | // XFAIL: msan |
| 14 | |
| 15 | // Basic test for _Unwind_ForcedUnwind. |
| 16 | // See libcxxabi/test/forced_unwind* tests too. |
| 17 | |
| 18 | #undef NDEBUG |
| 19 | #include <assert.h> |
| 20 | #include <signal.h> |
| 21 | #include <stdint.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 | // Using __attribute__((section("main_func"))) is Linux specific, but then |
| 30 | // this entire test is marked as requiring Linux, so we should be good. |
| 31 | // |
| 32 | // We don't use dladdr() because on musl it's a no-op when statically linked. |
| 33 | extern char __start_main_func; |
| 34 | extern char __stop_main_func; |
| 35 | |
| 36 | void foo(); |
| 37 | _Unwind_Exception ex; |
| 38 | |
| 39 | _Unwind_Reason_Code stop(int version, _Unwind_Action actions, |
| 40 | _Unwind_Exception_Class exceptionClass, |
| 41 | _Unwind_Exception *exceptionObject, |
| 42 | struct _Unwind_Context *context, |
| 43 | void *stop_parameter) { |
| 44 | assert(version == 1); |
| 45 | assert((actions & _UA_FORCE_UNWIND) != 0); |
| 46 | (void)exceptionClass; |
| 47 | assert(exceptionObject == &ex); |
| 48 | assert(stop_parameter == &foo); |
| 49 | |
| 50 | // Unwind until the main is reached, above frames depend on the platform and |
| 51 | // architecture. |
| 52 | uintptr_t ip = _Unwind_GetIP(context); |
| 53 | if (ip >= (uintptr_t)&__start_main_func && |
| 54 | ip < (uintptr_t)&__stop_main_func) { |
| 55 | _Exit(status: 0); |
| 56 | } |
| 57 | |
| 58 | return _URC_NO_REASON; |
| 59 | } |
| 60 | |
| 61 | __attribute__((noinline)) void foo() { |
| 62 | |
| 63 | // Arm EHABI defines struct _Unwind_Control_Block as exception |
| 64 | // object. Ensure struct _Unwind_Exception* work there too, |
| 65 | // because _Unwind_Exception in this case is just an alias. |
| 66 | struct _Unwind_Exception *e = &ex; |
| 67 | #if defined(_LIBUNWIND_ARM_EHABI) |
| 68 | // Create a mock exception object. |
| 69 | memset(e, '\0', sizeof(*e)); |
| 70 | memcpy(&e->exception_class, "CLNGUNW" , sizeof(e->exception_class)); |
| 71 | #endif |
| 72 | _Unwind_ForcedUnwind(e, stop, (void *)&foo); |
| 73 | } |
| 74 | |
| 75 | __attribute__((section("main_func" ))) int main() { |
| 76 | foo(); |
| 77 | return -2; |
| 78 | } |
| 79 | |