2 //===----------------------------------------------------------------------===//
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
8 //===----------------------------------------------------------------------===//
12 // TODO: Figure out why this fails with Memory Sanitizer.
15 // Basic test for _Unwind_ForcedUnwind.
16 // See libcxxabi/test/forced_unwind* tests too.
25 #include <sys/types.h>
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.
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
;
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
) {
45 assert((actions
& _UA_FORCE_UNWIND
) != 0);
47 assert(exceptionObject
== &ex
);
48 assert(stop_parameter
== &foo
);
50 // Unwind until the main is reached, above frames depend on the platform and
52 uintptr_t ip
= _Unwind_GetIP(context
);
53 if (ip
>= (uintptr_t)&__start_main_func
&&
54 ip
< (uintptr_t)&__stop_main_func
) {
58 return _URC_NO_REASON
;
61 __attribute__((noinline
)) void foo() {
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
));
72 _Unwind_ForcedUnwind(e
, stop
, (void *)&foo
);
75 __attribute__((section("main_func"))) int main() {