comparison libunwind/test/unwind_leaffunction.pass.cpp @ 236:c4bab56944e8 llvm-original

LLVM 16
author kono
date Wed, 09 Nov 2022 17:45:10 +0900
parents 5f17cb93ff66
children 1f2b6ac9f198
comparison
equal deleted inserted replaced
232:70dce7da266c 236:c4bab56944e8
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 // 7 //
8 //===----------------------------------------------------------------------===// 8 //===----------------------------------------------------------------------===//
9 9
10 // Ensure that leaf function can be unwund. 10 // Ensure that leaf function can be unwund.
11 // REQUIRES: linux && (target={{aarch64-.+}} || target={{x86_64-.+}}) 11 // REQUIRES: linux && (target={{aarch64-.+}} || target={{s390x-.+}} || target={{x86_64-.+}})
12 12
13 // TODO: Figure out why this fails with Memory Sanitizer.
14 // XFAIL: msan
15
16 #undef NDEBUG
13 #include <assert.h> 17 #include <assert.h>
14 #include <dlfcn.h> 18 #include <dlfcn.h>
15 #include <signal.h> 19 #include <signal.h>
16 #include <stdio.h> 20 #include <stdio.h>
17 #include <stdlib.h> 21 #include <stdlib.h>
22 26
23 _Unwind_Reason_Code frame_handler(struct _Unwind_Context* ctx, void* arg) { 27 _Unwind_Reason_Code frame_handler(struct _Unwind_Context* ctx, void* arg) {
24 (void)arg; 28 (void)arg;
25 Dl_info info = { 0, 0, 0, 0 }; 29 Dl_info info = { 0, 0, 0, 0 };
26 30
27 // Unwind util the main is reached, above frames deeped on the platfrom and architecture. 31 // Unwind until the main is reached, above frames deeped on the platform and
32 // architecture.
28 if (dladdr(reinterpret_cast<void *>(_Unwind_GetIP(ctx)), &info) && 33 if (dladdr(reinterpret_cast<void *>(_Unwind_GetIP(ctx)), &info) &&
29 info.dli_sname && !strcmp("main", info.dli_sname)) { 34 info.dli_sname && !strcmp("main", info.dli_sname)) {
30 _Exit(0); 35 _Exit(0);
31 } 36 }
32 return _URC_NO_REASON; 37 return _URC_NO_REASON;
36 (void)signum; 41 (void)signum;
37 _Unwind_Backtrace(frame_handler, NULL); 42 _Unwind_Backtrace(frame_handler, NULL);
38 _Exit(-1); 43 _Exit(-1);
39 } 44 }
40 45
41 int* faultyPointer = NULL;
42
43 __attribute__((noinline)) void crashing_leaf_func(void) { 46 __attribute__((noinline)) void crashing_leaf_func(void) {
44 *faultyPointer = 0; 47 // libunwind searches for the address before the return address which points
48 // to the trap instruction. NOP guarantees the trap instruction is not the
49 // first instruction of the function.
50 // We should keep this here for other unwinders that also decrement pc.
51 __asm__ __volatile__("nop");
52 __builtin_trap();
45 } 53 }
46 54
47 int main(int, char**) { 55 int main(int, char**) {
48 signal(SIGSEGV, signal_handler); 56 signal(SIGTRAP, signal_handler);
57 signal(SIGILL, signal_handler);
49 crashing_leaf_func(); 58 crashing_leaf_func();
50 return -2; 59 return -2;
51 } 60 }