blob: a91bd950612cfefec270c90bb3e885d0b715f50d [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>
Elliott Hughes38488902018-07-11 11:13:16 -070035#include <android-base/threads.h>
Christopher Ferrisb9de87f2017-09-20 13:37:24 -070036
Christopher Ferrisd226a512017-07-14 10:37:19 -070037#include <unwindstack/Maps.h>
38#include <unwindstack/Memory.h>
39#include <unwindstack/Regs.h>
40#include <unwindstack/RegsGetLocal.h>
Christopher Ferrisb9de87f2017-09-20 13:37:24 -070041#include <unwindstack/Unwinder.h>
Christopher Ferrisd226a512017-07-14 10:37:19 -070042
Christopher Ferrisedccd842017-09-06 14:15:28 -070043#include "TestUtils.h"
44
Christopher Ferrisd226a512017-07-14 10:37:19 -070045namespace unwindstack {
Christopher Ferris2a25c4a2017-07-07 16:35:48 -070046
Christopher Ferriseb0772f2018-12-05 15:57:02 -080047enum 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 Ferrisedccd842017-09-06 14:15:28 -070054static std::atomic_bool g_ready;
55static volatile bool g_ready_for_remote;
56static volatile bool g_signal_ready_for_remote;
57static std::atomic_bool g_finish;
Christopher Ferris2a25c4a2017-07-07 16:35:48 -070058static std::atomic_uintptr_t g_ucontext;
59
Christopher Ferrisedccd842017-09-06 14:15:28 -070060static 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 Ferrisb9de87f2017-09-20 13:37:24 -070068static std::vector<const char*> kFunctionOrder{"OuterFunction", "MiddleFunction", "InnerFunction"};
Christopher Ferrisa0196652017-07-18 16:09:20 -070069
Christopher Ferrisb9de87f2017-09-20 13:37:24 -070070static std::vector<const char*> kFunctionSignalOrder{"OuterFunction", "MiddleFunction",
71 "InnerFunction", "SignalOuterFunction",
72 "SignalMiddleFunction", "SignalInnerFunction"};
Christopher Ferrisa0196652017-07-18 16:09:20 -070073
74static void SignalHandler(int, siginfo_t*, void* sigcontext) {
Christopher Ferris2a25c4a2017-07-07 16:35:48 -070075 g_ucontext = reinterpret_cast<uintptr_t>(sigcontext);
76 while (!g_finish.load()) {
77 }
78}
79
Christopher Ferrisa0196652017-07-18 16:09:20 -070080extern "C" void SignalInnerFunction() {
81 g_signal_ready_for_remote = true;
82 while (!g_finish.load()) {
83 }
84}
85
86extern "C" void SignalMiddleFunction() {
87 SignalInnerFunction();
88}
89
90extern "C" void SignalOuterFunction() {
91 SignalMiddleFunction();
92}
93
94static void SignalCallerHandler(int, siginfo_t*, void*) {
95 SignalOuterFunction();
96}
97
Christopher Ferriseb0772f2018-12-05 15:57:02 -080098static std::string ErrorMsg(const std::vector<const char*>& function_names, Unwinder* unwinder) {
Christopher Ferrisb9de87f2017-09-20 13:37:24 -070099 std::string unwind;
Christopher Ferriseb0772f2018-12-05 15:57:02 -0800100 for (size_t i = 0; i < unwinder->NumFrames(); i++) {
101 unwind += unwinder->FormatFrame(i) + '\n';
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700102 }
103
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700104 return std::string(
105 "Unwind completed without finding all frames\n"
106 " Looking for function: ") +
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700107 function_names.front() + "\n" + "Unwind data:\n" + unwind;
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700108}
109
Christopher Ferriseb0772f2018-12-05 15:57:02 -0800110static void VerifyUnwind(Unwinder* unwinder, std::vector<const char*> expected_function_names) {
111 unwinder->Unwind();
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700112
Christopher Ferriseb0772f2018-12-05 15:57:02 -0800113 for (auto& frame : unwinder->frames()) {
Christopher Ferrisca9a54b2018-04-05 11:15:00 -0700114 if (frame.function_name == expected_function_names.back()) {
115 expected_function_names.pop_back();
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700116 if (expected_function_names.empty()) {
117 break;
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700118 }
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700119 }
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700120 }
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700121
122 ASSERT_TRUE(expected_function_names.empty()) << ErrorMsg(expected_function_names, unwinder);
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700123}
124
Christopher Ferriseb0772f2018-12-05 15:57:02 -0800125static 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 Ferris2a25c4a2017-07-07 16:35:48 -0700133// 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 Ferriseb0772f2018-12-05 15:57:02 -0800136extern "C" void InnerFunction(TestTypeEnum test_type) {
137 if (test_type == TEST_TYPE_REMOTE || test_type == TEST_TYPE_REMOTE_WITH_INVALID_CALL) {
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700138 g_ready_for_remote = true;
139 g_ready = true;
Christopher Ferriseb0772f2018-12-05 15:57:02 -0800140 if (test_type == TEST_TYPE_REMOTE_WITH_INVALID_CALL) {
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700141 void (*crash_func)() = nullptr;
142 crash_func();
143 }
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700144 while (!g_finish.load()) {
145 }
Christopher Ferriseb0772f2018-12-05 15:57:02 -0800146 return;
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700147 }
Christopher Ferriseb0772f2018-12-05 15:57:02 -0800148
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 Ferris2a25c4a2017-07-07 16:35:48 -0700166}
167
Christopher Ferriseb0772f2018-12-05 15:57:02 -0800168extern "C" void MiddleFunction(TestTypeEnum test_type) {
169 InnerFunction(test_type);
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700170}
171
Christopher Ferriseb0772f2018-12-05 15:57:02 -0800172extern "C" void OuterFunction(TestTypeEnum test_type) {
173 MiddleFunction(test_type);
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700174}
175
Christopher Ferrisedccd842017-09-06 14:15:28 -0700176class UnwindTest : public ::testing::Test {
177 public:
178 void SetUp() override { ResetGlobals(); }
179};
180
181TEST_F(UnwindTest, local) {
Christopher Ferriseb0772f2018-12-05 15:57:02 -0800182 OuterFunction(TEST_TYPE_LOCAL_UNWINDER);
183}
184
185TEST_F(UnwindTest, local_use_from_pid) {
186 OuterFunction(TEST_TYPE_LOCAL_UNWINDER_FROM_PID);
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700187}
188
Christopher Ferrisa0196652017-07-18 16:09:20 -0700189void 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 Ferrisedccd842017-09-06 14:15:28 -0700192 // host it becomes impossible to attach and ptrace sets errno to EPERM.
Christopher Ferrisa0196652017-07-18 16:09:20 -0700193 usleep(1000);
Christopher Ferrisedccd842017-09-06 14:15:28 -0700194 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 Gaoef35aa52017-10-18 11:44:51 -0700202 if (memory.ReadFully(addr, &value, sizeof(value)) && value) {
Christopher Ferrisedccd842017-09-06 14:15:28 -0700203 *completed = true;
Christopher Ferrisa0196652017-07-18 16:09:20 -0700204 }
Christopher Ferrisedccd842017-09-06 14:15:28 -0700205 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 Ferrisa0196652017-07-18 16:09:20 -0700213 }
Christopher Ferrisedccd842017-09-06 14:15:28 -0700214 usleep(5000);
Christopher Ferrisa0196652017-07-18 16:09:20 -0700215 }
216}
217
Christopher Ferrisedccd842017-09-06 14:15:28 -0700218TEST_F(UnwindTest, remote) {
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700219 pid_t pid;
220 if ((pid = fork()) == 0) {
Christopher Ferriseb0772f2018-12-05 15:57:02 -0800221 OuterFunction(TEST_TYPE_REMOTE);
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700222 exit(0);
223 }
224 ASSERT_NE(-1, pid);
Christopher Ferrisedccd842017-09-06 14:15:28 -0700225 TestScopedPidReaper reap(pid);
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700226
Christopher Ferrisa0196652017-07-18 16:09:20 -0700227 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 Ferris2a25c4a2017-07-07 16:35:48 -0700230
231 RemoteMaps maps(pid);
232 ASSERT_TRUE(maps.Parse());
Josh Gao0953ecd2017-08-25 13:55:06 -0700233 std::unique_ptr<Regs> regs(Regs::RemoteGet(pid));
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700234 ASSERT_TRUE(regs.get() != nullptr);
235
Christopher Ferris5f118512017-09-01 11:17:16 -0700236 VerifyUnwind(pid, &maps, regs.get(), kFunctionOrder);
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700237
Christopher Ferrisedccd842017-09-06 14:15:28 -0700238 ASSERT_EQ(0, ptrace(PTRACE_DETACH, pid, 0, 0))
239 << "ptrace detach failed with unexpected error: " << strerror(errno);
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700240}
241
Christopher Ferriseb0772f2018-12-05 15:57:02 -0800242TEST_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 Ferrisedccd842017-09-06 14:15:28 -0700270TEST_F(UnwindTest, from_context) {
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700271 std::atomic_int tid(0);
272 std::thread thread([&]() {
273 tid = syscall(__NR_gettid);
Christopher Ferriseb0772f2018-12-05 15:57:02 -0800274 OuterFunction(TEST_TYPE_REMOTE);
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700275 });
276
277 struct sigaction act, oldact;
278 memset(&act, 0, sizeof(act));
Christopher Ferrisa0196652017-07-18 16:09:20 -0700279 act.sa_sigaction = SignalHandler;
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700280 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 Hughes38488902018-07-11 11:13:16 -0700290 ASSERT_EQ(0, tgkill(getpid(), tid.load(), SIGUSR1)) << "Error: " << strerror(errno);
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700291
292 // Wait for context data.
293 void* ucontext;
Christopher Ferrisd226a512017-07-14 10:37:19 -0700294 for (size_t i = 0; i < 2000; i++) {
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700295 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 Ferrisd06001d2017-11-30 18:56:01 -0800305 std::unique_ptr<Regs> regs(Regs::CreateFromUcontext(Regs::CurrentArch(), ucontext));
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700306
Christopher Ferris5f118512017-09-01 11:17:16 -0700307 VerifyUnwind(getpid(), &maps, regs.get(), kFunctionOrder);
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700308
309 ASSERT_EQ(0, sigaction(SIGUSR1, &oldact, nullptr));
310
311 g_finish = true;
312 thread.join();
313}
Christopher Ferrisd226a512017-07-14 10:37:19 -0700314
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700315static void RemoteThroughSignal(int signal, unsigned int sa_flags) {
Christopher Ferrisa0196652017-07-18 16:09:20 -0700316 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 Ferrisb9de87f2017-09-20 13:37:24 -0700322 ASSERT_EQ(0, sigaction(signal, &act, &oldact));
Christopher Ferrisa0196652017-07-18 16:09:20 -0700323
Christopher Ferriseb0772f2018-12-05 15:57:02 -0800324 OuterFunction(signal != SIGSEGV ? TEST_TYPE_REMOTE : TEST_TYPE_REMOTE_WITH_INVALID_CALL);
Christopher Ferrisa0196652017-07-18 16:09:20 -0700325 exit(0);
326 }
327 ASSERT_NE(-1, pid);
Christopher Ferrisedccd842017-09-06 14:15:28 -0700328 TestScopedPidReaper reap(pid);
Christopher Ferrisa0196652017-07-18 16:09:20 -0700329
330 bool completed;
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700331 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 Ferrisa0196652017-07-18 16:09:20 -0700336 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 Gao0953ecd2017-08-25 13:55:06 -0700341 std::unique_ptr<Regs> regs(Regs::RemoteGet(pid));
Christopher Ferrisa0196652017-07-18 16:09:20 -0700342 ASSERT_TRUE(regs.get() != nullptr);
343
Christopher Ferris5f118512017-09-01 11:17:16 -0700344 VerifyUnwind(pid, &maps, regs.get(), kFunctionSignalOrder);
Christopher Ferrisa0196652017-07-18 16:09:20 -0700345
Christopher Ferrisedccd842017-09-06 14:15:28 -0700346 ASSERT_EQ(0, ptrace(PTRACE_DETACH, pid, 0, 0))
347 << "ptrace detach failed with unexpected error: " << strerror(errno);
Christopher Ferrisa0196652017-07-18 16:09:20 -0700348}
349
Christopher Ferrisedccd842017-09-06 14:15:28 -0700350TEST_F(UnwindTest, remote_through_signal) {
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700351 RemoteThroughSignal(SIGUSR1, 0);
Christopher Ferrisa0196652017-07-18 16:09:20 -0700352}
353
Christopher Ferrisedccd842017-09-06 14:15:28 -0700354TEST_F(UnwindTest, remote_through_signal_sa_siginfo) {
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700355 RemoteThroughSignal(SIGUSR1, SA_SIGINFO);
356}
357
358TEST_F(UnwindTest, remote_through_signal_with_invalid_func) {
359 RemoteThroughSignal(SIGSEGV, 0);
360}
361
362TEST_F(UnwindTest, remote_through_signal_sa_siginfo_with_invalid_func) {
363 RemoteThroughSignal(SIGSEGV, SA_SIGINFO);
Christopher Ferrisa0196652017-07-18 16:09:20 -0700364}
365
Christopher Ferrisbe788d82017-11-27 14:50:38 -0800366// Verify that using the same map while unwinding multiple threads at the
367// same time doesn't cause problems.
368TEST_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 Ferrisd226a512017-07-14 10:37:19 -0700401} // namespace unwindstack