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