Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | #include <errno.h> |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 18 | #include <signal.h> |
| 19 | #include <stdint.h> |
Christopher Ferris | edccd84 | 2017-09-06 14:15:28 -0700 | [diff] [blame] | 20 | #include <string.h> |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 21 | #include <sys/ptrace.h> |
| 22 | #include <sys/syscall.h> |
| 23 | #include <unistd.h> |
| 24 | |
| 25 | #include <gtest/gtest.h> |
| 26 | |
| 27 | #include <atomic> |
| 28 | #include <memory> |
| 29 | #include <sstream> |
| 30 | #include <string> |
| 31 | #include <thread> |
Christopher Ferris | a019665 | 2017-07-18 16:09:20 -0700 | [diff] [blame] | 32 | #include <vector> |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 33 | |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 34 | #include <android-base/stringprintf.h> |
Elliott Hughes | 3848890 | 2018-07-11 11:13:16 -0700 | [diff] [blame] | 35 | #include <android-base/threads.h> |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 36 | |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 37 | #include <unwindstack/Maps.h> |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 38 | #include <unwindstack/Regs.h> |
| 39 | #include <unwindstack/RegsGetLocal.h> |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 40 | #include <unwindstack/Unwinder.h> |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 41 | |
Casey Dahlin | 6b95a0e | 2019-03-12 17:50:52 -0700 | [diff] [blame] | 42 | #include "MemoryRemote.h" |
Christopher Ferris | edccd84 | 2017-09-06 14:15:28 -0700 | [diff] [blame] | 43 | #include "TestUtils.h" |
| 44 | |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 45 | namespace unwindstack { |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 46 | |
Christopher Ferris | eb0772f | 2018-12-05 15:57:02 -0800 | [diff] [blame] | 47 | enum TestTypeEnum : uint8_t { |
| 48 | TEST_TYPE_LOCAL_UNWINDER = 0, |
| 49 | TEST_TYPE_LOCAL_UNWINDER_FROM_PID, |
Christopher Ferris | bc6a7e5 | 2019-01-18 16:33:27 -0800 | [diff] [blame] | 50 | TEST_TYPE_LOCAL_WAIT_FOR_FINISH, |
Christopher Ferris | eb0772f | 2018-12-05 15:57:02 -0800 | [diff] [blame] | 51 | TEST_TYPE_REMOTE, |
| 52 | TEST_TYPE_REMOTE_WITH_INVALID_CALL, |
| 53 | }; |
| 54 | |
Christopher Ferris | edccd84 | 2017-09-06 14:15:28 -0700 | [diff] [blame] | 55 | static std::atomic_bool g_ready; |
| 56 | static volatile bool g_ready_for_remote; |
| 57 | static volatile bool g_signal_ready_for_remote; |
| 58 | static std::atomic_bool g_finish; |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 59 | static std::atomic_uintptr_t g_ucontext; |
| 60 | |
Christopher Ferris | edccd84 | 2017-09-06 14:15:28 -0700 | [diff] [blame] | 61 | static void ResetGlobals() { |
| 62 | g_ready = false; |
| 63 | g_ready_for_remote = false; |
| 64 | g_signal_ready_for_remote = false; |
| 65 | g_finish = false; |
| 66 | g_ucontext = 0; |
| 67 | } |
| 68 | |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 69 | static std::vector<const char*> kFunctionOrder{"OuterFunction", "MiddleFunction", "InnerFunction"}; |
Christopher Ferris | a019665 | 2017-07-18 16:09:20 -0700 | [diff] [blame] | 70 | |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 71 | static std::vector<const char*> kFunctionSignalOrder{"OuterFunction", "MiddleFunction", |
| 72 | "InnerFunction", "SignalOuterFunction", |
| 73 | "SignalMiddleFunction", "SignalInnerFunction"}; |
Christopher Ferris | a019665 | 2017-07-18 16:09:20 -0700 | [diff] [blame] | 74 | |
| 75 | static void SignalHandler(int, siginfo_t*, void* sigcontext) { |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 76 | g_ucontext = reinterpret_cast<uintptr_t>(sigcontext); |
| 77 | while (!g_finish.load()) { |
| 78 | } |
| 79 | } |
| 80 | |
Christopher Ferris | a019665 | 2017-07-18 16:09:20 -0700 | [diff] [blame] | 81 | extern "C" void SignalInnerFunction() { |
| 82 | g_signal_ready_for_remote = true; |
Christopher Ferris | bc6a7e5 | 2019-01-18 16:33:27 -0800 | [diff] [blame] | 83 | // Avoid any function calls because not every instruction will be |
| 84 | // unwindable. |
| 85 | // This method of looping is only used when testing a remote unwind. |
| 86 | while (true) { |
Christopher Ferris | a019665 | 2017-07-18 16:09:20 -0700 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | |
| 90 | extern "C" void SignalMiddleFunction() { |
| 91 | SignalInnerFunction(); |
| 92 | } |
| 93 | |
| 94 | extern "C" void SignalOuterFunction() { |
| 95 | SignalMiddleFunction(); |
| 96 | } |
| 97 | |
| 98 | static void SignalCallerHandler(int, siginfo_t*, void*) { |
| 99 | SignalOuterFunction(); |
| 100 | } |
| 101 | |
Christopher Ferris | eb0772f | 2018-12-05 15:57:02 -0800 | [diff] [blame] | 102 | static std::string ErrorMsg(const std::vector<const char*>& function_names, Unwinder* unwinder) { |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 103 | std::string unwind; |
Christopher Ferris | eb0772f | 2018-12-05 15:57:02 -0800 | [diff] [blame] | 104 | for (size_t i = 0; i < unwinder->NumFrames(); i++) { |
| 105 | unwind += unwinder->FormatFrame(i) + '\n'; |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 108 | return std::string( |
| 109 | "Unwind completed without finding all frames\n" |
| 110 | " Looking for function: ") + |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 111 | function_names.front() + "\n" + "Unwind data:\n" + unwind; |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Christopher Ferris | eb0772f | 2018-12-05 15:57:02 -0800 | [diff] [blame] | 114 | static void VerifyUnwind(Unwinder* unwinder, std::vector<const char*> expected_function_names) { |
| 115 | unwinder->Unwind(); |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 116 | |
Christopher Ferris | eb0772f | 2018-12-05 15:57:02 -0800 | [diff] [blame] | 117 | for (auto& frame : unwinder->frames()) { |
Christopher Ferris | ca9a54b | 2018-04-05 11:15:00 -0700 | [diff] [blame] | 118 | if (frame.function_name == expected_function_names.back()) { |
| 119 | expected_function_names.pop_back(); |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 120 | if (expected_function_names.empty()) { |
| 121 | break; |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 122 | } |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 123 | } |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 124 | } |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 125 | |
| 126 | ASSERT_TRUE(expected_function_names.empty()) << ErrorMsg(expected_function_names, unwinder); |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Christopher Ferris | eb0772f | 2018-12-05 15:57:02 -0800 | [diff] [blame] | 129 | static void VerifyUnwind(pid_t pid, Maps* maps, Regs* regs, |
| 130 | std::vector<const char*> expected_function_names) { |
| 131 | auto process_memory(Memory::CreateProcessMemory(pid)); |
| 132 | |
| 133 | Unwinder unwinder(512, maps, regs, process_memory); |
| 134 | VerifyUnwind(&unwinder, expected_function_names); |
| 135 | } |
| 136 | |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 137 | // This test assumes that this code is compiled with optimizations turned |
| 138 | // off. If this doesn't happen, then all of the calls will be optimized |
| 139 | // away. |
Christopher Ferris | eb0772f | 2018-12-05 15:57:02 -0800 | [diff] [blame] | 140 | extern "C" void InnerFunction(TestTypeEnum test_type) { |
Christopher Ferris | bc6a7e5 | 2019-01-18 16:33:27 -0800 | [diff] [blame] | 141 | if (test_type == TEST_TYPE_LOCAL_WAIT_FOR_FINISH) { |
| 142 | while (!g_finish.load()) { |
| 143 | } |
| 144 | return; |
| 145 | } |
Christopher Ferris | eb0772f | 2018-12-05 15:57:02 -0800 | [diff] [blame] | 146 | if (test_type == TEST_TYPE_REMOTE || test_type == TEST_TYPE_REMOTE_WITH_INVALID_CALL) { |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 147 | g_ready_for_remote = true; |
| 148 | g_ready = true; |
Christopher Ferris | eb0772f | 2018-12-05 15:57:02 -0800 | [diff] [blame] | 149 | if (test_type == TEST_TYPE_REMOTE_WITH_INVALID_CALL) { |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 150 | void (*crash_func)() = nullptr; |
| 151 | crash_func(); |
| 152 | } |
Christopher Ferris | bc6a7e5 | 2019-01-18 16:33:27 -0800 | [diff] [blame] | 153 | // Avoid any function calls because not every instruction will be |
| 154 | // unwindable. |
| 155 | // This method of looping is only used when testing a remote unwind. |
| 156 | while (true) { |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 157 | } |
Christopher Ferris | eb0772f | 2018-12-05 15:57:02 -0800 | [diff] [blame] | 158 | return; |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 159 | } |
Christopher Ferris | eb0772f | 2018-12-05 15:57:02 -0800 | [diff] [blame] | 160 | |
| 161 | std::unique_ptr<Unwinder> unwinder; |
| 162 | std::unique_ptr<Regs> regs(Regs::CreateFromLocal()); |
| 163 | RegsGetLocal(regs.get()); |
| 164 | std::unique_ptr<Maps> maps; |
| 165 | |
| 166 | if (test_type == TEST_TYPE_LOCAL_UNWINDER) { |
| 167 | maps.reset(new LocalMaps()); |
| 168 | ASSERT_TRUE(maps->Parse()); |
| 169 | auto process_memory(Memory::CreateProcessMemory(getpid())); |
| 170 | unwinder.reset(new Unwinder(512, maps.get(), regs.get(), process_memory)); |
| 171 | } else { |
| 172 | UnwinderFromPid* unwinder_from_pid = new UnwinderFromPid(512, getpid()); |
David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame] | 173 | ASSERT_TRUE(unwinder_from_pid->Init(regs->Arch())); |
Christopher Ferris | eb0772f | 2018-12-05 15:57:02 -0800 | [diff] [blame] | 174 | unwinder_from_pid->SetRegs(regs.get()); |
| 175 | unwinder.reset(unwinder_from_pid); |
| 176 | } |
| 177 | VerifyUnwind(unwinder.get(), kFunctionOrder); |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 178 | } |
| 179 | |
Christopher Ferris | eb0772f | 2018-12-05 15:57:02 -0800 | [diff] [blame] | 180 | extern "C" void MiddleFunction(TestTypeEnum test_type) { |
| 181 | InnerFunction(test_type); |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Christopher Ferris | eb0772f | 2018-12-05 15:57:02 -0800 | [diff] [blame] | 184 | extern "C" void OuterFunction(TestTypeEnum test_type) { |
| 185 | MiddleFunction(test_type); |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Christopher Ferris | edccd84 | 2017-09-06 14:15:28 -0700 | [diff] [blame] | 188 | class UnwindTest : public ::testing::Test { |
| 189 | public: |
| 190 | void SetUp() override { ResetGlobals(); } |
| 191 | }; |
| 192 | |
| 193 | TEST_F(UnwindTest, local) { |
Christopher Ferris | eb0772f | 2018-12-05 15:57:02 -0800 | [diff] [blame] | 194 | OuterFunction(TEST_TYPE_LOCAL_UNWINDER); |
| 195 | } |
| 196 | |
| 197 | TEST_F(UnwindTest, local_use_from_pid) { |
| 198 | OuterFunction(TEST_TYPE_LOCAL_UNWINDER_FROM_PID); |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 199 | } |
| 200 | |
Christopher Ferris | e1f7a63 | 2019-01-24 12:22:03 -0800 | [diff] [blame] | 201 | static void LocalUnwind(void* data) { |
| 202 | TestTypeEnum* test_type = reinterpret_cast<TestTypeEnum*>(data); |
| 203 | OuterFunction(*test_type); |
| 204 | } |
| 205 | |
| 206 | TEST_F(UnwindTest, local_check_for_leak) { |
| 207 | TestTypeEnum test_type = TEST_TYPE_LOCAL_UNWINDER; |
| 208 | TestCheckForLeaks(LocalUnwind, &test_type); |
| 209 | } |
| 210 | |
| 211 | TEST_F(UnwindTest, local_use_from_pid_check_for_leak) { |
| 212 | TestTypeEnum test_type = TEST_TYPE_LOCAL_UNWINDER_FROM_PID; |
| 213 | TestCheckForLeaks(LocalUnwind, &test_type); |
| 214 | } |
| 215 | |
Christopher Ferris | a019665 | 2017-07-18 16:09:20 -0700 | [diff] [blame] | 216 | void WaitForRemote(pid_t pid, uint64_t addr, bool leave_attached, bool* completed) { |
| 217 | *completed = false; |
| 218 | // Need to sleep before attempting first ptrace. Without this, on the |
Christopher Ferris | edccd84 | 2017-09-06 14:15:28 -0700 | [diff] [blame] | 219 | // host it becomes impossible to attach and ptrace sets errno to EPERM. |
Christopher Ferris | a019665 | 2017-07-18 16:09:20 -0700 | [diff] [blame] | 220 | usleep(1000); |
Christopher Ferris | edccd84 | 2017-09-06 14:15:28 -0700 | [diff] [blame] | 221 | for (size_t i = 0; i < 1000; i++) { |
| 222 | if (ptrace(PTRACE_ATTACH, pid, 0, 0) == 0) { |
| 223 | ASSERT_TRUE(TestQuiescePid(pid)) |
| 224 | << "Waiting for process to quiesce failed: " << strerror(errno); |
| 225 | |
| 226 | MemoryRemote memory(pid); |
| 227 | // Read the remote value to see if we are ready. |
| 228 | bool value; |
Josh Gao | ef35aa5 | 2017-10-18 11:44:51 -0700 | [diff] [blame] | 229 | if (memory.ReadFully(addr, &value, sizeof(value)) && value) { |
Christopher Ferris | edccd84 | 2017-09-06 14:15:28 -0700 | [diff] [blame] | 230 | *completed = true; |
Christopher Ferris | a019665 | 2017-07-18 16:09:20 -0700 | [diff] [blame] | 231 | } |
Christopher Ferris | edccd84 | 2017-09-06 14:15:28 -0700 | [diff] [blame] | 232 | if (!*completed || !leave_attached) { |
| 233 | ASSERT_EQ(0, ptrace(PTRACE_DETACH, pid, 0, 0)); |
| 234 | } |
| 235 | if (*completed) { |
| 236 | break; |
| 237 | } |
| 238 | } else { |
| 239 | ASSERT_EQ(ESRCH, errno) << "ptrace attach failed with unexpected error: " << strerror(errno); |
Christopher Ferris | a019665 | 2017-07-18 16:09:20 -0700 | [diff] [blame] | 240 | } |
Christopher Ferris | edccd84 | 2017-09-06 14:15:28 -0700 | [diff] [blame] | 241 | usleep(5000); |
Christopher Ferris | a019665 | 2017-07-18 16:09:20 -0700 | [diff] [blame] | 242 | } |
| 243 | } |
| 244 | |
Christopher Ferris | edccd84 | 2017-09-06 14:15:28 -0700 | [diff] [blame] | 245 | TEST_F(UnwindTest, remote) { |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 246 | pid_t pid; |
| 247 | if ((pid = fork()) == 0) { |
Christopher Ferris | eb0772f | 2018-12-05 15:57:02 -0800 | [diff] [blame] | 248 | OuterFunction(TEST_TYPE_REMOTE); |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 249 | exit(0); |
| 250 | } |
| 251 | ASSERT_NE(-1, pid); |
Christopher Ferris | edccd84 | 2017-09-06 14:15:28 -0700 | [diff] [blame] | 252 | TestScopedPidReaper reap(pid); |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 253 | |
Christopher Ferris | a019665 | 2017-07-18 16:09:20 -0700 | [diff] [blame] | 254 | bool completed; |
| 255 | WaitForRemote(pid, reinterpret_cast<uint64_t>(&g_ready_for_remote), true, &completed); |
| 256 | ASSERT_TRUE(completed) << "Timed out waiting for remote process to be ready."; |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 257 | |
| 258 | RemoteMaps maps(pid); |
| 259 | ASSERT_TRUE(maps.Parse()); |
Josh Gao | 0953ecd | 2017-08-25 13:55:06 -0700 | [diff] [blame] | 260 | std::unique_ptr<Regs> regs(Regs::RemoteGet(pid)); |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 261 | ASSERT_TRUE(regs.get() != nullptr); |
| 262 | |
Christopher Ferris | 5f11851 | 2017-09-01 11:17:16 -0700 | [diff] [blame] | 263 | VerifyUnwind(pid, &maps, regs.get(), kFunctionOrder); |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 264 | |
Christopher Ferris | edccd84 | 2017-09-06 14:15:28 -0700 | [diff] [blame] | 265 | ASSERT_EQ(0, ptrace(PTRACE_DETACH, pid, 0, 0)) |
| 266 | << "ptrace detach failed with unexpected error: " << strerror(errno); |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 267 | } |
| 268 | |
Christopher Ferris | eb0772f | 2018-12-05 15:57:02 -0800 | [diff] [blame] | 269 | TEST_F(UnwindTest, unwind_from_pid_remote) { |
| 270 | pid_t pid; |
| 271 | if ((pid = fork()) == 0) { |
| 272 | OuterFunction(TEST_TYPE_REMOTE); |
| 273 | exit(0); |
| 274 | } |
| 275 | ASSERT_NE(-1, pid); |
| 276 | TestScopedPidReaper reap(pid); |
| 277 | |
| 278 | bool completed; |
| 279 | WaitForRemote(pid, reinterpret_cast<uint64_t>(&g_ready_for_remote), true, &completed); |
| 280 | ASSERT_TRUE(completed) << "Timed out waiting for remote process to be ready."; |
| 281 | |
| 282 | std::unique_ptr<Regs> regs(Regs::RemoteGet(pid)); |
| 283 | ASSERT_TRUE(regs.get() != nullptr); |
| 284 | |
| 285 | UnwinderFromPid unwinder(512, pid); |
David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame] | 286 | ASSERT_TRUE(unwinder.Init(regs->Arch())); |
Christopher Ferris | eb0772f | 2018-12-05 15:57:02 -0800 | [diff] [blame] | 287 | unwinder.SetRegs(regs.get()); |
| 288 | |
| 289 | VerifyUnwind(&unwinder, kFunctionOrder); |
| 290 | |
| 291 | // Verify that calling the same object works again. |
| 292 | |
| 293 | ASSERT_EQ(0, ptrace(PTRACE_DETACH, pid, 0, 0)) |
| 294 | << "ptrace detach failed with unexpected error: " << strerror(errno); |
| 295 | } |
| 296 | |
Christopher Ferris | e1f7a63 | 2019-01-24 12:22:03 -0800 | [diff] [blame] | 297 | static void RemoteCheckForLeaks(void (*unwind_func)(void*)) { |
| 298 | pid_t pid; |
| 299 | if ((pid = fork()) == 0) { |
| 300 | OuterFunction(TEST_TYPE_REMOTE); |
| 301 | exit(0); |
| 302 | } |
| 303 | ASSERT_NE(-1, pid); |
| 304 | TestScopedPidReaper reap(pid); |
| 305 | |
| 306 | bool completed; |
| 307 | WaitForRemote(pid, reinterpret_cast<uint64_t>(&g_ready_for_remote), true, &completed); |
| 308 | ASSERT_TRUE(completed) << "Timed out waiting for remote process to be ready."; |
| 309 | |
| 310 | TestCheckForLeaks(unwind_func, &pid); |
| 311 | |
| 312 | ASSERT_EQ(0, ptrace(PTRACE_DETACH, pid, 0, 0)) |
| 313 | << "ptrace detach failed with unexpected error: " << strerror(errno); |
| 314 | } |
| 315 | |
| 316 | static void RemoteUnwind(void* data) { |
| 317 | pid_t* pid = reinterpret_cast<pid_t*>(data); |
| 318 | |
| 319 | RemoteMaps maps(*pid); |
| 320 | ASSERT_TRUE(maps.Parse()); |
| 321 | std::unique_ptr<Regs> regs(Regs::RemoteGet(*pid)); |
| 322 | ASSERT_TRUE(regs.get() != nullptr); |
| 323 | |
| 324 | VerifyUnwind(*pid, &maps, regs.get(), kFunctionOrder); |
| 325 | } |
| 326 | |
| 327 | TEST_F(UnwindTest, remote_check_for_leaks) { |
| 328 | RemoteCheckForLeaks(RemoteUnwind); |
| 329 | } |
| 330 | |
| 331 | static void RemoteUnwindFromPid(void* data) { |
| 332 | pid_t* pid = reinterpret_cast<pid_t*>(data); |
| 333 | |
| 334 | std::unique_ptr<Regs> regs(Regs::RemoteGet(*pid)); |
| 335 | ASSERT_TRUE(regs.get() != nullptr); |
| 336 | |
| 337 | UnwinderFromPid unwinder(512, *pid); |
David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame] | 338 | ASSERT_TRUE(unwinder.Init(regs->Arch())); |
Christopher Ferris | e1f7a63 | 2019-01-24 12:22:03 -0800 | [diff] [blame] | 339 | unwinder.SetRegs(regs.get()); |
| 340 | |
| 341 | VerifyUnwind(&unwinder, kFunctionOrder); |
| 342 | } |
| 343 | |
| 344 | TEST_F(UnwindTest, remote_unwind_for_pid_check_for_leaks) { |
| 345 | RemoteCheckForLeaks(RemoteUnwindFromPid); |
| 346 | } |
| 347 | |
Christopher Ferris | edccd84 | 2017-09-06 14:15:28 -0700 | [diff] [blame] | 348 | TEST_F(UnwindTest, from_context) { |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 349 | std::atomic_int tid(0); |
| 350 | std::thread thread([&]() { |
| 351 | tid = syscall(__NR_gettid); |
Christopher Ferris | bc6a7e5 | 2019-01-18 16:33:27 -0800 | [diff] [blame] | 352 | OuterFunction(TEST_TYPE_LOCAL_WAIT_FOR_FINISH); |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 353 | }); |
| 354 | |
| 355 | struct sigaction act, oldact; |
| 356 | memset(&act, 0, sizeof(act)); |
Christopher Ferris | a019665 | 2017-07-18 16:09:20 -0700 | [diff] [blame] | 357 | act.sa_sigaction = SignalHandler; |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 358 | act.sa_flags = SA_RESTART | SA_SIGINFO | SA_ONSTACK; |
| 359 | ASSERT_EQ(0, sigaction(SIGUSR1, &act, &oldact)); |
| 360 | // Wait for the tid to get set. |
| 361 | for (size_t i = 0; i < 100; i++) { |
| 362 | if (tid.load() != 0) { |
| 363 | break; |
| 364 | } |
| 365 | usleep(1000); |
| 366 | } |
| 367 | ASSERT_NE(0, tid.load()); |
Elliott Hughes | 3848890 | 2018-07-11 11:13:16 -0700 | [diff] [blame] | 368 | ASSERT_EQ(0, tgkill(getpid(), tid.load(), SIGUSR1)) << "Error: " << strerror(errno); |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 369 | |
| 370 | // Wait for context data. |
| 371 | void* ucontext; |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 372 | for (size_t i = 0; i < 2000; i++) { |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 373 | ucontext = reinterpret_cast<void*>(g_ucontext.load()); |
| 374 | if (ucontext != nullptr) { |
| 375 | break; |
| 376 | } |
| 377 | usleep(1000); |
| 378 | } |
| 379 | ASSERT_TRUE(ucontext != nullptr) << "Timed out waiting for thread to respond to signal."; |
| 380 | |
| 381 | LocalMaps maps; |
| 382 | ASSERT_TRUE(maps.Parse()); |
Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 383 | std::unique_ptr<Regs> regs(Regs::CreateFromUcontext(Regs::CurrentArch(), ucontext)); |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 384 | |
Christopher Ferris | 5f11851 | 2017-09-01 11:17:16 -0700 | [diff] [blame] | 385 | VerifyUnwind(getpid(), &maps, regs.get(), kFunctionOrder); |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 386 | |
| 387 | ASSERT_EQ(0, sigaction(SIGUSR1, &oldact, nullptr)); |
| 388 | |
| 389 | g_finish = true; |
| 390 | thread.join(); |
| 391 | } |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 392 | |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 393 | static void RemoteThroughSignal(int signal, unsigned int sa_flags) { |
Christopher Ferris | a019665 | 2017-07-18 16:09:20 -0700 | [diff] [blame] | 394 | pid_t pid; |
| 395 | if ((pid = fork()) == 0) { |
| 396 | struct sigaction act, oldact; |
| 397 | memset(&act, 0, sizeof(act)); |
| 398 | act.sa_sigaction = SignalCallerHandler; |
| 399 | act.sa_flags = SA_RESTART | SA_ONSTACK | sa_flags; |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 400 | ASSERT_EQ(0, sigaction(signal, &act, &oldact)); |
Christopher Ferris | a019665 | 2017-07-18 16:09:20 -0700 | [diff] [blame] | 401 | |
Christopher Ferris | eb0772f | 2018-12-05 15:57:02 -0800 | [diff] [blame] | 402 | OuterFunction(signal != SIGSEGV ? TEST_TYPE_REMOTE : TEST_TYPE_REMOTE_WITH_INVALID_CALL); |
Christopher Ferris | a019665 | 2017-07-18 16:09:20 -0700 | [diff] [blame] | 403 | exit(0); |
| 404 | } |
| 405 | ASSERT_NE(-1, pid); |
Christopher Ferris | edccd84 | 2017-09-06 14:15:28 -0700 | [diff] [blame] | 406 | TestScopedPidReaper reap(pid); |
Christopher Ferris | a019665 | 2017-07-18 16:09:20 -0700 | [diff] [blame] | 407 | |
| 408 | bool completed; |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 409 | if (signal != SIGSEGV) { |
| 410 | WaitForRemote(pid, reinterpret_cast<uint64_t>(&g_ready_for_remote), false, &completed); |
| 411 | ASSERT_TRUE(completed) << "Timed out waiting for remote process to be ready."; |
| 412 | ASSERT_EQ(0, kill(pid, SIGUSR1)); |
| 413 | } |
Christopher Ferris | a019665 | 2017-07-18 16:09:20 -0700 | [diff] [blame] | 414 | WaitForRemote(pid, reinterpret_cast<uint64_t>(&g_signal_ready_for_remote), true, &completed); |
| 415 | ASSERT_TRUE(completed) << "Timed out waiting for remote process to be in signal handler."; |
| 416 | |
| 417 | RemoteMaps maps(pid); |
| 418 | ASSERT_TRUE(maps.Parse()); |
Josh Gao | 0953ecd | 2017-08-25 13:55:06 -0700 | [diff] [blame] | 419 | std::unique_ptr<Regs> regs(Regs::RemoteGet(pid)); |
Christopher Ferris | a019665 | 2017-07-18 16:09:20 -0700 | [diff] [blame] | 420 | ASSERT_TRUE(regs.get() != nullptr); |
| 421 | |
Christopher Ferris | 5f11851 | 2017-09-01 11:17:16 -0700 | [diff] [blame] | 422 | VerifyUnwind(pid, &maps, regs.get(), kFunctionSignalOrder); |
Christopher Ferris | a019665 | 2017-07-18 16:09:20 -0700 | [diff] [blame] | 423 | |
Christopher Ferris | edccd84 | 2017-09-06 14:15:28 -0700 | [diff] [blame] | 424 | ASSERT_EQ(0, ptrace(PTRACE_DETACH, pid, 0, 0)) |
| 425 | << "ptrace detach failed with unexpected error: " << strerror(errno); |
Christopher Ferris | a019665 | 2017-07-18 16:09:20 -0700 | [diff] [blame] | 426 | } |
| 427 | |
Christopher Ferris | edccd84 | 2017-09-06 14:15:28 -0700 | [diff] [blame] | 428 | TEST_F(UnwindTest, remote_through_signal) { |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 429 | RemoteThroughSignal(SIGUSR1, 0); |
Christopher Ferris | a019665 | 2017-07-18 16:09:20 -0700 | [diff] [blame] | 430 | } |
| 431 | |
Christopher Ferris | edccd84 | 2017-09-06 14:15:28 -0700 | [diff] [blame] | 432 | TEST_F(UnwindTest, remote_through_signal_sa_siginfo) { |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 433 | RemoteThroughSignal(SIGUSR1, SA_SIGINFO); |
| 434 | } |
| 435 | |
| 436 | TEST_F(UnwindTest, remote_through_signal_with_invalid_func) { |
| 437 | RemoteThroughSignal(SIGSEGV, 0); |
| 438 | } |
| 439 | |
| 440 | TEST_F(UnwindTest, remote_through_signal_sa_siginfo_with_invalid_func) { |
| 441 | RemoteThroughSignal(SIGSEGV, SA_SIGINFO); |
Christopher Ferris | a019665 | 2017-07-18 16:09:20 -0700 | [diff] [blame] | 442 | } |
| 443 | |
Christopher Ferris | be788d8 | 2017-11-27 14:50:38 -0800 | [diff] [blame] | 444 | // Verify that using the same map while unwinding multiple threads at the |
| 445 | // same time doesn't cause problems. |
| 446 | TEST_F(UnwindTest, multiple_threads_unwind_same_map) { |
| 447 | static constexpr size_t kNumConcurrentThreads = 100; |
| 448 | |
| 449 | LocalMaps maps; |
| 450 | ASSERT_TRUE(maps.Parse()); |
| 451 | auto process_memory(Memory::CreateProcessMemory(getpid())); |
| 452 | |
| 453 | std::vector<std::thread*> threads; |
| 454 | |
| 455 | std::atomic_bool wait; |
| 456 | wait = true; |
| 457 | size_t frames[kNumConcurrentThreads]; |
| 458 | for (size_t i = 0; i < kNumConcurrentThreads; i++) { |
| 459 | std::thread* thread = new std::thread([i, &frames, &maps, &process_memory, &wait]() { |
| 460 | while (wait) |
| 461 | ; |
| 462 | std::unique_ptr<Regs> regs(Regs::CreateFromLocal()); |
| 463 | RegsGetLocal(regs.get()); |
| 464 | |
| 465 | Unwinder unwinder(512, &maps, regs.get(), process_memory); |
| 466 | unwinder.Unwind(); |
| 467 | frames[i] = unwinder.NumFrames(); |
| 468 | ASSERT_LE(3U, frames[i]) << "Failed for thread " << i; |
| 469 | }); |
| 470 | threads.push_back(thread); |
| 471 | } |
| 472 | wait = false; |
| 473 | for (auto thread : threads) { |
| 474 | thread->join(); |
| 475 | delete thread; |
| 476 | } |
| 477 | } |
| 478 | |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 479 | } // namespace unwindstack |