Daniel Kiss | 6bf52ee | 2021-08-11 10:11:30 +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 | // REQUIRES: linux |
| 11 | |
Louis Dionne | 31359e0 | 2022-03-02 17:49:13 -0500 | [diff] [blame] | 12 | // TODO: Figure out why this fails with Memory Sanitizer. |
| 13 | // XFAIL: msan |
| 14 | |
Daniel Kiss | 6bf52ee | 2021-08-11 10:11:30 +0200 | [diff] [blame] | 15 | // Basic test for _Unwind_ForcedUnwind. |
| 16 | // See libcxxabi/test/forced_unwind* tests too. |
| 17 | |
Florian Mayer | 47974e9 | 2022-08-04 15:02:52 -0700 | [diff] [blame] | 18 | #undef NDEBUG |
Daniel Kiss | 6bf52ee | 2021-08-11 10:11:30 +0200 | [diff] [blame] | 19 | #include <assert.h> |
| 20 | #include <dlfcn.h> |
| 21 | #include <signal.h> |
| 22 | #include <stdint.h> |
| 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
| 26 | #include <sys/types.h> |
| 27 | #include <unistd.h> |
| 28 | #include <unwind.h> |
| 29 | |
| 30 | void foo(); |
| 31 | _Unwind_Exception ex; |
| 32 | |
| 33 | _Unwind_Reason_Code stop(int version, _Unwind_Action actions, |
Daniel Kiss | 0517d86 | 2021-09-02 11:30:26 +0200 | [diff] [blame] | 34 | _Unwind_Exception_Class exceptionClass, |
Daniel Kiss | 6bf52ee | 2021-08-11 10:11:30 +0200 | [diff] [blame] | 35 | _Unwind_Exception *exceptionObject, |
| 36 | struct _Unwind_Context *context, |
| 37 | void *stop_parameter) { |
| 38 | assert(version == 1); |
| 39 | assert((actions & _UA_FORCE_UNWIND) != 0); |
| 40 | (void)exceptionClass; |
| 41 | assert(exceptionObject == &ex); |
| 42 | assert(stop_parameter == &foo); |
| 43 | |
| 44 | Dl_info info = {0, 0, 0, 0}; |
| 45 | |
| 46 | // Unwind util the main is reached, above frames depend on the platform and |
| 47 | // architecture. |
| 48 | if (dladdr(reinterpret_cast<void *>(_Unwind_GetIP(context)), &info) && |
| 49 | info.dli_sname && !strcmp("main", info.dli_sname)) { |
| 50 | _Exit(0); |
| 51 | } |
| 52 | return _URC_NO_REASON; |
| 53 | } |
| 54 | |
| 55 | __attribute__((noinline)) void foo() { |
| 56 | |
| 57 | // Arm EHABI defines struct _Unwind_Control_Block as exception |
| 58 | // object. Ensure struct _Unwind_Exception* work there too, |
| 59 | // because _Unwind_Exception in this case is just an alias. |
| 60 | struct _Unwind_Exception *e = &ex; |
| 61 | #if defined(_LIBUNWIND_ARM_EHABI) |
| 62 | // Create a mock exception object. |
| 63 | memset(e, '\0', sizeof(*e)); |
Daniel Kiss | 0517d86 | 2021-09-02 11:30:26 +0200 | [diff] [blame] | 64 | strcpy(reinterpret_cast<char *>(&e->exception_class), "CLNGUNW"); |
Daniel Kiss | 6bf52ee | 2021-08-11 10:11:30 +0200 | [diff] [blame] | 65 | #endif |
| 66 | _Unwind_ForcedUnwind(e, stop, (void *)&foo); |
| 67 | } |
| 68 | |
| 69 | int main() { |
| 70 | foo(); |
| 71 | return -2; |
| 72 | } |