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