annotate libunwind/test/forceunwind.pass.cpp @ 236:c4bab56944e8 llvm-original

LLVM 16
author kono
date Wed, 09 Nov 2022 17:45:10 +0900
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
236
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
1 // -*- C++ -*-
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
2 //===----------------------------------------------------------------------===//
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
3 //
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
5 // See https://llvm.org/LICENSE.txt for license information.
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
7 //
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
8 //===----------------------------------------------------------------------===//
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
9
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
10 // REQUIRES: linux
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
11
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
12 // TODO: Figure out why this fails with Memory Sanitizer.
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
13 // XFAIL: msan
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
14
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
15 // Basic test for _Unwind_ForcedUnwind.
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
16 // See libcxxabi/test/forced_unwind* tests too.
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
17
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
18 #undef NDEBUG
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
19 #include <assert.h>
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
20 #include <dlfcn.h>
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
21 #include <signal.h>
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
22 #include <stdint.h>
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
23 #include <stdio.h>
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
24 #include <stdlib.h>
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
25 #include <string.h>
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
26 #include <sys/types.h>
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
27 #include <unistd.h>
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
28 #include <unwind.h>
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
29
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
30 void foo();
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
31 _Unwind_Exception ex;
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
32
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
33 _Unwind_Reason_Code stop(int version, _Unwind_Action actions,
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
34 _Unwind_Exception_Class exceptionClass,
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
35 _Unwind_Exception *exceptionObject,
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
36 struct _Unwind_Context *context,
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
37 void *stop_parameter) {
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
38 assert(version == 1);
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
39 assert((actions & _UA_FORCE_UNWIND) != 0);
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
40 (void)exceptionClass;
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
41 assert(exceptionObject == &ex);
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
42 assert(stop_parameter == &foo);
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
43
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
44 Dl_info info = {0, 0, 0, 0};
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
45
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
46 // Unwind util the main is reached, above frames depend on the platform and
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
47 // architecture.
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
48 if (dladdr(reinterpret_cast<void *>(_Unwind_GetIP(context)), &info) &&
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
49 info.dli_sname && !strcmp("main", info.dli_sname)) {
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
50 _Exit(0);
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
51 }
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
52 return _URC_NO_REASON;
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
53 }
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
54
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
55 __attribute__((noinline)) void foo() {
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
56
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
57 // Arm EHABI defines struct _Unwind_Control_Block as exception
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
58 // object. Ensure struct _Unwind_Exception* work there too,
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
59 // because _Unwind_Exception in this case is just an alias.
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
60 struct _Unwind_Exception *e = &ex;
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
61 #if defined(_LIBUNWIND_ARM_EHABI)
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
62 // Create a mock exception object.
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
63 memset(e, '\0', sizeof(*e));
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
64 strcpy(reinterpret_cast<char *>(&e->exception_class), "CLNGUNW");
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
65 #endif
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
66 _Unwind_ForcedUnwind(e, stop, (void *)&foo);
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
67 }
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
68
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
69 int main() {
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
70 foo();
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
71 return -2;
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
72 }