blob: 83695bbcd97991fa7b50f292a21e2ac0b7cad903 [file] [log] [blame]
Christopher Ferris2a25c4a2017-07-07 16:35:48 -07001/*
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 Ferris2a25c4a2017-07-07 16:35:48 -070018#include <signal.h>
19#include <stdint.h>
Christopher Ferrisedccd842017-09-06 14:15:28 -070020#include <string.h>
Christopher Ferris2a25c4a2017-07-07 16:35:48 -070021#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 Ferrisa0196652017-07-18 16:09:20 -070032#include <vector>
Christopher Ferris2a25c4a2017-07-07 16:35:48 -070033
Christopher Ferrisb9de87f2017-09-20 13:37:24 -070034#include <android-base/stringprintf.h>
35
Christopher Ferrisd226a512017-07-14 10:37:19 -070036#include <unwindstack/Maps.h>
37#include <unwindstack/Memory.h>
38#include <unwindstack/Regs.h>
39#include <unwindstack/RegsGetLocal.h>
Christopher Ferrisb9de87f2017-09-20 13:37:24 -070040#include <unwindstack/Unwinder.h>
Christopher Ferrisd226a512017-07-14 10:37:19 -070041
Christopher Ferrisedccd842017-09-06 14:15:28 -070042#include "TestUtils.h"
43
Christopher Ferrisd226a512017-07-14 10:37:19 -070044namespace unwindstack {
Christopher Ferris2a25c4a2017-07-07 16:35:48 -070045
Christopher Ferrisedccd842017-09-06 14:15:28 -070046static std::atomic_bool g_ready;
47static volatile bool g_ready_for_remote;
48static volatile bool g_signal_ready_for_remote;
49static std::atomic_bool g_finish;
Christopher Ferris2a25c4a2017-07-07 16:35:48 -070050static std::atomic_uintptr_t g_ucontext;
51
Christopher Ferrisedccd842017-09-06 14:15:28 -070052static void ResetGlobals() {
53 g_ready = false;
54 g_ready_for_remote = false;
55 g_signal_ready_for_remote = false;
56 g_finish = false;
57 g_ucontext = 0;
58}
59
Christopher Ferrisb9de87f2017-09-20 13:37:24 -070060static std::vector<const char*> kFunctionOrder{"OuterFunction", "MiddleFunction", "InnerFunction"};
Christopher Ferrisa0196652017-07-18 16:09:20 -070061
Christopher Ferrisb9de87f2017-09-20 13:37:24 -070062static std::vector<const char*> kFunctionSignalOrder{"OuterFunction", "MiddleFunction",
63 "InnerFunction", "SignalOuterFunction",
64 "SignalMiddleFunction", "SignalInnerFunction"};
Christopher Ferrisa0196652017-07-18 16:09:20 -070065
66static void SignalHandler(int, siginfo_t*, void* sigcontext) {
Christopher Ferris2a25c4a2017-07-07 16:35:48 -070067 g_ucontext = reinterpret_cast<uintptr_t>(sigcontext);
68 while (!g_finish.load()) {
69 }
70}
71
Christopher Ferrisa0196652017-07-18 16:09:20 -070072extern "C" void SignalInnerFunction() {
73 g_signal_ready_for_remote = true;
74 while (!g_finish.load()) {
75 }
76}
77
78extern "C" void SignalMiddleFunction() {
79 SignalInnerFunction();
80}
81
82extern "C" void SignalOuterFunction() {
83 SignalMiddleFunction();
84}
85
86static void SignalCallerHandler(int, siginfo_t*, void*) {
87 SignalOuterFunction();
88}
89
Christopher Ferrisb9de87f2017-09-20 13:37:24 -070090static std::string ErrorMsg(const std::vector<const char*>& function_names, Unwinder& unwinder) {
91 std::string unwind;
92 for (size_t i = 0; i < unwinder.NumFrames(); i++) {
93 unwind += unwinder.FormatFrame(i) + '\n';
94 }
95
Christopher Ferris2a25c4a2017-07-07 16:35:48 -070096 return std::string(
97 "Unwind completed without finding all frames\n"
98 " Looking for function: ") +
Christopher Ferrisb9de87f2017-09-20 13:37:24 -070099 function_names.front() + "\n" + "Unwind data:\n" + unwind;
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700100}
101
Christopher Ferris5f118512017-09-01 11:17:16 -0700102static void VerifyUnwind(pid_t pid, Maps* maps, Regs* regs,
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700103 std::vector<const char*> expected_function_names) {
104 auto process_memory(Memory::CreateProcessMemory(pid));
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700105
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700106 Unwinder unwinder(512, maps, regs, process_memory);
107 unwinder.Unwind();
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700108
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700109 for (auto& frame : unwinder.frames()) {
Christopher Ferrisca9a54b2018-04-05 11:15:00 -0700110 if (frame.function_name == expected_function_names.back()) {
111 expected_function_names.pop_back();
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700112 if (expected_function_names.empty()) {
113 break;
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700114 }
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700115 }
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700116 }
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700117
118 ASSERT_TRUE(expected_function_names.empty()) << ErrorMsg(expected_function_names, unwinder);
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700119}
120
121// This test assumes that this code is compiled with optimizations turned
122// off. If this doesn't happen, then all of the calls will be optimized
123// away.
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700124extern "C" void InnerFunction(bool local, bool trigger_invalid_call) {
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700125 if (local) {
126 LocalMaps maps;
127 ASSERT_TRUE(maps.Parse());
128 std::unique_ptr<Regs> regs(Regs::CreateFromLocal());
129 RegsGetLocal(regs.get());
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700130
Christopher Ferris5f118512017-09-01 11:17:16 -0700131 VerifyUnwind(getpid(), &maps, regs.get(), kFunctionOrder);
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700132 } else {
133 g_ready_for_remote = true;
134 g_ready = true;
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700135 if (trigger_invalid_call) {
136 void (*crash_func)() = nullptr;
137 crash_func();
138 }
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700139 while (!g_finish.load()) {
140 }
141 }
142}
143
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700144extern "C" void MiddleFunction(bool local, bool trigger_invalid_call) {
145 InnerFunction(local, trigger_invalid_call);
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700146}
147
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700148extern "C" void OuterFunction(bool local, bool trigger_invalid_call) {
149 MiddleFunction(local, trigger_invalid_call);
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700150}
151
Christopher Ferrisedccd842017-09-06 14:15:28 -0700152class UnwindTest : public ::testing::Test {
153 public:
154 void SetUp() override { ResetGlobals(); }
155};
156
157TEST_F(UnwindTest, local) {
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700158 OuterFunction(true, false);
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700159}
160
Christopher Ferrisa0196652017-07-18 16:09:20 -0700161void WaitForRemote(pid_t pid, uint64_t addr, bool leave_attached, bool* completed) {
162 *completed = false;
163 // Need to sleep before attempting first ptrace. Without this, on the
Christopher Ferrisedccd842017-09-06 14:15:28 -0700164 // host it becomes impossible to attach and ptrace sets errno to EPERM.
Christopher Ferrisa0196652017-07-18 16:09:20 -0700165 usleep(1000);
Christopher Ferrisedccd842017-09-06 14:15:28 -0700166 for (size_t i = 0; i < 1000; i++) {
167 if (ptrace(PTRACE_ATTACH, pid, 0, 0) == 0) {
168 ASSERT_TRUE(TestQuiescePid(pid))
169 << "Waiting for process to quiesce failed: " << strerror(errno);
170
171 MemoryRemote memory(pid);
172 // Read the remote value to see if we are ready.
173 bool value;
Josh Gaoef35aa52017-10-18 11:44:51 -0700174 if (memory.ReadFully(addr, &value, sizeof(value)) && value) {
Christopher Ferrisedccd842017-09-06 14:15:28 -0700175 *completed = true;
Christopher Ferrisa0196652017-07-18 16:09:20 -0700176 }
Christopher Ferrisedccd842017-09-06 14:15:28 -0700177 if (!*completed || !leave_attached) {
178 ASSERT_EQ(0, ptrace(PTRACE_DETACH, pid, 0, 0));
179 }
180 if (*completed) {
181 break;
182 }
183 } else {
184 ASSERT_EQ(ESRCH, errno) << "ptrace attach failed with unexpected error: " << strerror(errno);
Christopher Ferrisa0196652017-07-18 16:09:20 -0700185 }
Christopher Ferrisedccd842017-09-06 14:15:28 -0700186 usleep(5000);
Christopher Ferrisa0196652017-07-18 16:09:20 -0700187 }
188}
189
Christopher Ferrisedccd842017-09-06 14:15:28 -0700190TEST_F(UnwindTest, remote) {
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700191 pid_t pid;
192 if ((pid = fork()) == 0) {
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700193 OuterFunction(false, false);
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700194 exit(0);
195 }
196 ASSERT_NE(-1, pid);
Christopher Ferrisedccd842017-09-06 14:15:28 -0700197 TestScopedPidReaper reap(pid);
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700198
Christopher Ferrisa0196652017-07-18 16:09:20 -0700199 bool completed;
200 WaitForRemote(pid, reinterpret_cast<uint64_t>(&g_ready_for_remote), true, &completed);
201 ASSERT_TRUE(completed) << "Timed out waiting for remote process to be ready.";
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700202
203 RemoteMaps maps(pid);
204 ASSERT_TRUE(maps.Parse());
Josh Gao0953ecd2017-08-25 13:55:06 -0700205 std::unique_ptr<Regs> regs(Regs::RemoteGet(pid));
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700206 ASSERT_TRUE(regs.get() != nullptr);
207
Christopher Ferris5f118512017-09-01 11:17:16 -0700208 VerifyUnwind(pid, &maps, regs.get(), kFunctionOrder);
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700209
Christopher Ferrisedccd842017-09-06 14:15:28 -0700210 ASSERT_EQ(0, ptrace(PTRACE_DETACH, pid, 0, 0))
211 << "ptrace detach failed with unexpected error: " << strerror(errno);
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700212}
213
Christopher Ferrisedccd842017-09-06 14:15:28 -0700214TEST_F(UnwindTest, from_context) {
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700215 std::atomic_int tid(0);
216 std::thread thread([&]() {
217 tid = syscall(__NR_gettid);
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700218 OuterFunction(false, false);
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700219 });
220
221 struct sigaction act, oldact;
222 memset(&act, 0, sizeof(act));
Christopher Ferrisa0196652017-07-18 16:09:20 -0700223 act.sa_sigaction = SignalHandler;
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700224 act.sa_flags = SA_RESTART | SA_SIGINFO | SA_ONSTACK;
225 ASSERT_EQ(0, sigaction(SIGUSR1, &act, &oldact));
226 // Wait for the tid to get set.
227 for (size_t i = 0; i < 100; i++) {
228 if (tid.load() != 0) {
229 break;
230 }
231 usleep(1000);
232 }
233 ASSERT_NE(0, tid.load());
234 // Portable tgkill method.
Christopher Ferrisa0196652017-07-18 16:09:20 -0700235 ASSERT_EQ(0, syscall(__NR_tgkill, getpid(), tid.load(), SIGUSR1)) << "Error: " << strerror(errno);
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700236
237 // Wait for context data.
238 void* ucontext;
Christopher Ferrisd226a512017-07-14 10:37:19 -0700239 for (size_t i = 0; i < 2000; i++) {
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700240 ucontext = reinterpret_cast<void*>(g_ucontext.load());
241 if (ucontext != nullptr) {
242 break;
243 }
244 usleep(1000);
245 }
246 ASSERT_TRUE(ucontext != nullptr) << "Timed out waiting for thread to respond to signal.";
247
248 LocalMaps maps;
249 ASSERT_TRUE(maps.Parse());
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800250 std::unique_ptr<Regs> regs(Regs::CreateFromUcontext(Regs::CurrentArch(), ucontext));
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700251
Christopher Ferris5f118512017-09-01 11:17:16 -0700252 VerifyUnwind(getpid(), &maps, regs.get(), kFunctionOrder);
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700253
254 ASSERT_EQ(0, sigaction(SIGUSR1, &oldact, nullptr));
255
256 g_finish = true;
257 thread.join();
258}
Christopher Ferrisd226a512017-07-14 10:37:19 -0700259
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700260static void RemoteThroughSignal(int signal, unsigned int sa_flags) {
Christopher Ferrisa0196652017-07-18 16:09:20 -0700261 pid_t pid;
262 if ((pid = fork()) == 0) {
263 struct sigaction act, oldact;
264 memset(&act, 0, sizeof(act));
265 act.sa_sigaction = SignalCallerHandler;
266 act.sa_flags = SA_RESTART | SA_ONSTACK | sa_flags;
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700267 ASSERT_EQ(0, sigaction(signal, &act, &oldact));
Christopher Ferrisa0196652017-07-18 16:09:20 -0700268
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700269 OuterFunction(false, signal == SIGSEGV);
Christopher Ferrisa0196652017-07-18 16:09:20 -0700270 exit(0);
271 }
272 ASSERT_NE(-1, pid);
Christopher Ferrisedccd842017-09-06 14:15:28 -0700273 TestScopedPidReaper reap(pid);
Christopher Ferrisa0196652017-07-18 16:09:20 -0700274
275 bool completed;
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700276 if (signal != SIGSEGV) {
277 WaitForRemote(pid, reinterpret_cast<uint64_t>(&g_ready_for_remote), false, &completed);
278 ASSERT_TRUE(completed) << "Timed out waiting for remote process to be ready.";
279 ASSERT_EQ(0, kill(pid, SIGUSR1));
280 }
Christopher Ferrisa0196652017-07-18 16:09:20 -0700281 WaitForRemote(pid, reinterpret_cast<uint64_t>(&g_signal_ready_for_remote), true, &completed);
282 ASSERT_TRUE(completed) << "Timed out waiting for remote process to be in signal handler.";
283
284 RemoteMaps maps(pid);
285 ASSERT_TRUE(maps.Parse());
Josh Gao0953ecd2017-08-25 13:55:06 -0700286 std::unique_ptr<Regs> regs(Regs::RemoteGet(pid));
Christopher Ferrisa0196652017-07-18 16:09:20 -0700287 ASSERT_TRUE(regs.get() != nullptr);
288
Christopher Ferris5f118512017-09-01 11:17:16 -0700289 VerifyUnwind(pid, &maps, regs.get(), kFunctionSignalOrder);
Christopher Ferrisa0196652017-07-18 16:09:20 -0700290
Christopher Ferrisedccd842017-09-06 14:15:28 -0700291 ASSERT_EQ(0, ptrace(PTRACE_DETACH, pid, 0, 0))
292 << "ptrace detach failed with unexpected error: " << strerror(errno);
Christopher Ferrisa0196652017-07-18 16:09:20 -0700293}
294
Christopher Ferrisedccd842017-09-06 14:15:28 -0700295TEST_F(UnwindTest, remote_through_signal) {
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700296 RemoteThroughSignal(SIGUSR1, 0);
Christopher Ferrisa0196652017-07-18 16:09:20 -0700297}
298
Christopher Ferrisedccd842017-09-06 14:15:28 -0700299TEST_F(UnwindTest, remote_through_signal_sa_siginfo) {
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700300 RemoteThroughSignal(SIGUSR1, SA_SIGINFO);
301}
302
303TEST_F(UnwindTest, remote_through_signal_with_invalid_func) {
304 RemoteThroughSignal(SIGSEGV, 0);
305}
306
307TEST_F(UnwindTest, remote_through_signal_sa_siginfo_with_invalid_func) {
308 RemoteThroughSignal(SIGSEGV, SA_SIGINFO);
Christopher Ferrisa0196652017-07-18 16:09:20 -0700309}
310
Christopher Ferrisbe788d82017-11-27 14:50:38 -0800311// Verify that using the same map while unwinding multiple threads at the
312// same time doesn't cause problems.
313TEST_F(UnwindTest, multiple_threads_unwind_same_map) {
314 static constexpr size_t kNumConcurrentThreads = 100;
315
316 LocalMaps maps;
317 ASSERT_TRUE(maps.Parse());
318 auto process_memory(Memory::CreateProcessMemory(getpid()));
319
320 std::vector<std::thread*> threads;
321
322 std::atomic_bool wait;
323 wait = true;
324 size_t frames[kNumConcurrentThreads];
325 for (size_t i = 0; i < kNumConcurrentThreads; i++) {
326 std::thread* thread = new std::thread([i, &frames, &maps, &process_memory, &wait]() {
327 while (wait)
328 ;
329 std::unique_ptr<Regs> regs(Regs::CreateFromLocal());
330 RegsGetLocal(regs.get());
331
332 Unwinder unwinder(512, &maps, regs.get(), process_memory);
333 unwinder.Unwind();
334 frames[i] = unwinder.NumFrames();
335 ASSERT_LE(3U, frames[i]) << "Failed for thread " << i;
336 });
337 threads.push_back(thread);
338 }
339 wait = false;
340 for (auto thread : threads) {
341 thread->join();
342 delete thread;
343 }
344}
345
Christopher Ferrisd226a512017-07-14 10:37:19 -0700346} // namespace unwindstack