Sergey Melnikov | c45087b | 2013-01-25 16:40:13 +0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* |
| 18 | * Contributed by: Intel Corporation |
| 19 | */ |
| 20 | |
| 21 | #include <gtest/gtest.h> |
| 22 | |
Elliott Hughes | bee1993 | 2014-09-17 17:21:20 -0700 | [diff] [blame] | 23 | #include <dlfcn.h> |
| 24 | #include <signal.h> |
| 25 | #include <stdio.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <string.h> |
| 28 | #include <sys/types.h> |
| 29 | #include <unistd.h> |
| 30 | #include <unwind.h> |
| 31 | |
Elliott Hughes | 71ba589 | 2018-02-07 12:44:45 -0800 | [diff] [blame] | 32 | #include "SignalUtils.h" |
Elliott Hughes | bee1993 | 2014-09-17 17:21:20 -0700 | [diff] [blame] | 33 | |
Pavel Chupin | 50321e2 | 2014-09-26 16:02:09 +0400 | [diff] [blame] | 34 | #define noinline __attribute__((__noinline__)) |
| 35 | #define __unused __attribute__((__unused__)) |
Elliott Hughes | bee1993 | 2014-09-17 17:21:20 -0700 | [diff] [blame] | 36 | |
Yabin Cui | 33ac04a | 2015-09-22 11:16:15 -0700 | [diff] [blame] | 37 | _Unwind_Reason_Code FrameCounter(_Unwind_Context* ctx __unused, void* arg) { |
Elliott Hughes | bee1993 | 2014-09-17 17:21:20 -0700 | [diff] [blame] | 38 | int* count_ptr = reinterpret_cast<int*>(arg); |
| 39 | |
| 40 | #if SHOW_FRAME_LOCATIONS |
| 41 | void* ip = reinterpret_cast<void*>(_Unwind_GetIP(ctx)); |
| 42 | |
| 43 | const char* symbol = "<unknown>"; |
| 44 | int offset = 0; |
| 45 | |
Christopher Ferris | 2a39188 | 2024-12-19 13:44:35 -0800 | [diff] [blame] | 46 | Dl_info info = {}; |
Elliott Hughes | bee1993 | 2014-09-17 17:21:20 -0700 | [diff] [blame] | 47 | if (dladdr(ip, &info) != 0) { |
| 48 | symbol = info.dli_sname; |
| 49 | if (info.dli_saddr != nullptr) { |
| 50 | offset = static_cast<int>(reinterpret_cast<char*>(ip) - reinterpret_cast<char*>(info.dli_saddr)); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | fprintf(stderr, " #%02d %p %s%+d (%s)\n", *count_ptr, ip, symbol, offset, info.dli_fname ? info.dli_fname : "??"); |
| 55 | fflush(stderr); |
| 56 | #endif |
| 57 | |
| 58 | ++*count_ptr; |
| 59 | return _URC_NO_REASON; |
Sergey Melnikov | c45087b | 2013-01-25 16:40:13 +0400 | [diff] [blame] | 60 | } |
| 61 | |
Elliott Hughes | bee1993 | 2014-09-17 17:21:20 -0700 | [diff] [blame] | 62 | static int noinline unwind_one_frame_deeper() { |
| 63 | int count = 0; |
| 64 | _Unwind_Backtrace(FrameCounter, &count); |
| 65 | return count; |
| 66 | } |
| 67 | |
Peter Collingbourne | 26d83ba | 2021-06-04 14:35:13 -0700 | [diff] [blame] | 68 | static void UnwindTest() { |
Elliott Hughes | bee1993 | 2014-09-17 17:21:20 -0700 | [diff] [blame] | 69 | int count = 0; |
| 70 | _Unwind_Backtrace(FrameCounter, &count); |
| 71 | int deeper_count = unwind_one_frame_deeper(); |
| 72 | ASSERT_EQ(count + 1, deeper_count); |
| 73 | } |
| 74 | |
Peter Collingbourne | 26d83ba | 2021-06-04 14:35:13 -0700 | [diff] [blame] | 75 | TEST(stack_unwinding, easy) { |
| 76 | UnwindTest(); |
| 77 | } |
| 78 | |
| 79 | TEST(stack_unwinding, thread) { |
| 80 | pthread_t thread; |
| 81 | ASSERT_EQ(0, pthread_create(&thread, nullptr, [](void*) -> void* { |
| 82 | UnwindTest(); |
| 83 | return nullptr; |
| 84 | }, nullptr)); |
| 85 | void *retval; |
| 86 | ASSERT_EQ(0, pthread_join(thread, &retval)); |
| 87 | EXPECT_EQ(nullptr, retval); |
| 88 | } |
| 89 | |
Christopher Ferris | 05d2621 | 2015-02-03 15:16:03 -0800 | [diff] [blame] | 90 | struct UnwindData { |
| 91 | volatile bool signal_handler_complete = false; |
| 92 | int expected_frame_count = 0; |
| 93 | int handler_frame_count = 0; |
| 94 | int handler_one_deeper_frame_count = 0; |
| 95 | }; |
| 96 | |
| 97 | static UnwindData g_unwind_data; |
Elliott Hughes | bee1993 | 2014-09-17 17:21:20 -0700 | [diff] [blame] | 98 | |
Ryan Prichard | cdace2f | 2021-02-10 07:08:18 +0000 | [diff] [blame] | 99 | static void noinline UnwindSignalHandler(int) { |
Christopher Ferris | 05d2621 | 2015-02-03 15:16:03 -0800 | [diff] [blame] | 100 | _Unwind_Backtrace(FrameCounter, &g_unwind_data.handler_frame_count); |
Elliott Hughes | bee1993 | 2014-09-17 17:21:20 -0700 | [diff] [blame] | 101 | |
Christopher Ferris | 05d2621 | 2015-02-03 15:16:03 -0800 | [diff] [blame] | 102 | g_unwind_data.handler_one_deeper_frame_count = unwind_one_frame_deeper(); |
| 103 | g_unwind_data.signal_handler_complete = true; |
| 104 | } |
| 105 | |
| 106 | static void verify_unwind_data(const UnwindData& unwind_data) { |
Christopher Ferris | ac2f3dd | 2017-03-22 19:05:50 -0700 | [diff] [blame] | 107 | // In order to avoid a false positive, the caller must have at least 2 frames |
| 108 | // outside of the signal handler. This avoids a case where the only frame |
| 109 | // right after the signal handler winds up being garbage. |
| 110 | EXPECT_GT(unwind_data.handler_frame_count, unwind_data.expected_frame_count + 1); |
| 111 | |
Christopher Ferris | 05d2621 | 2015-02-03 15:16:03 -0800 | [diff] [blame] | 112 | EXPECT_EQ(unwind_data.handler_frame_count + 1, unwind_data.handler_one_deeper_frame_count); |
Elliott Hughes | bee1993 | 2014-09-17 17:21:20 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Peter Collingbourne | 26d83ba | 2021-06-04 14:35:13 -0700 | [diff] [blame] | 115 | static void noinline SignalUnwindTest() { |
Christopher Ferris | 05d2621 | 2015-02-03 15:16:03 -0800 | [diff] [blame] | 116 | g_unwind_data = {}; |
Elliott Hughes | bee1993 | 2014-09-17 17:21:20 -0700 | [diff] [blame] | 117 | |
Christopher Ferris | 05d2621 | 2015-02-03 15:16:03 -0800 | [diff] [blame] | 118 | _Unwind_Backtrace(FrameCounter, &g_unwind_data.expected_frame_count); |
Christopher Ferris | ac2f3dd | 2017-03-22 19:05:50 -0700 | [diff] [blame] | 119 | ASSERT_LE(2, g_unwind_data.expected_frame_count) |
| 120 | << "The current call must contain at least 2 frames for the test to be valid."; |
Christopher Ferris | 05d2621 | 2015-02-03 15:16:03 -0800 | [diff] [blame] | 121 | |
Elliott Hughes | bee1993 | 2014-09-17 17:21:20 -0700 | [diff] [blame] | 122 | ASSERT_EQ(0, kill(getpid(), SIGUSR1)); |
Christopher Ferris | 05d2621 | 2015-02-03 15:16:03 -0800 | [diff] [blame] | 123 | while (!g_unwind_data.signal_handler_complete) {} |
| 124 | |
| 125 | verify_unwind_data(g_unwind_data); |
Elliott Hughes | bee1993 | 2014-09-17 17:21:20 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Christopher Ferris | ac2f3dd | 2017-03-22 19:05:50 -0700 | [diff] [blame] | 128 | TEST(stack_unwinding, unwind_through_signal_frame) { |
| 129 | ScopedSignalHandler ssh(SIGUSR1, UnwindSignalHandler); |
| 130 | |
Peter Collingbourne | 26d83ba | 2021-06-04 14:35:13 -0700 | [diff] [blame] | 131 | SignalUnwindTest(); |
Christopher Ferris | ac2f3dd | 2017-03-22 19:05:50 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Pavel Chupin | 50321e2 | 2014-09-26 16:02:09 +0400 | [diff] [blame] | 134 | // On LP32, the SA_SIGINFO flag gets you __restore_rt instead of __restore. |
| 135 | TEST(stack_unwinding, unwind_through_signal_frame_SA_SIGINFO) { |
Pavel Chupin | 50321e2 | 2014-09-26 16:02:09 +0400 | [diff] [blame] | 136 | ScopedSignalHandler ssh(SIGUSR1, UnwindSignalHandler, SA_SIGINFO); |
Elliott Hughes | bee1993 | 2014-09-17 17:21:20 -0700 | [diff] [blame] | 137 | |
Peter Collingbourne | 26d83ba | 2021-06-04 14:35:13 -0700 | [diff] [blame] | 138 | SignalUnwindTest(); |
Sergey Melnikov | c45087b | 2013-01-25 16:40:13 +0400 | [diff] [blame] | 139 | } |