blob: a4f920ad94df4b0efa7b7d174417340c91cd2f9b [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 Ferrisd226a512017-07-14 10:37:19 -070034#include <unwindstack/Elf.h>
35#include <unwindstack/MapInfo.h>
36#include <unwindstack/Maps.h>
37#include <unwindstack/Memory.h>
38#include <unwindstack/Regs.h>
39#include <unwindstack/RegsGetLocal.h>
40
Christopher Ferrisedccd842017-09-06 14:15:28 -070041#include "TestUtils.h"
42
Christopher Ferrisd226a512017-07-14 10:37:19 -070043namespace unwindstack {
Christopher Ferris2a25c4a2017-07-07 16:35:48 -070044
Christopher Ferrisedccd842017-09-06 14:15:28 -070045static std::atomic_bool g_ready;
46static volatile bool g_ready_for_remote;
47static volatile bool g_signal_ready_for_remote;
48static std::atomic_bool g_finish;
Christopher Ferris2a25c4a2017-07-07 16:35:48 -070049static std::atomic_uintptr_t g_ucontext;
50
Christopher Ferrisedccd842017-09-06 14:15:28 -070051static void ResetGlobals() {
52 g_ready = false;
53 g_ready_for_remote = false;
54 g_signal_ready_for_remote = false;
55 g_finish = false;
56 g_ucontext = 0;
57}
58
Christopher Ferrisa0196652017-07-18 16:09:20 -070059static std::vector<const char*> kFunctionOrder{"InnerFunction", "MiddleFunction", "OuterFunction"};
60
61static std::vector<const char*> kFunctionSignalOrder{"SignalInnerFunction", "SignalMiddleFunction",
62 "SignalOuterFunction", "InnerFunction",
63 "MiddleFunction", "OuterFunction"};
64
65static void SignalHandler(int, siginfo_t*, void* sigcontext) {
Christopher Ferris2a25c4a2017-07-07 16:35:48 -070066 g_ucontext = reinterpret_cast<uintptr_t>(sigcontext);
67 while (!g_finish.load()) {
68 }
69}
70
Christopher Ferrisa0196652017-07-18 16:09:20 -070071extern "C" void SignalInnerFunction() {
72 g_signal_ready_for_remote = true;
73 while (!g_finish.load()) {
74 }
75}
76
77extern "C" void SignalMiddleFunction() {
78 SignalInnerFunction();
79}
80
81extern "C" void SignalOuterFunction() {
82 SignalMiddleFunction();
83}
84
85static void SignalCallerHandler(int, siginfo_t*, void*) {
86 SignalOuterFunction();
87}
88
89static std::string ErrorMsg(const std::vector<const char*>& function_names, size_t index,
Christopher Ferris2a25c4a2017-07-07 16:35:48 -070090 std::stringstream& unwind_stream) {
91 return std::string(
92 "Unwind completed without finding all frames\n"
93 " Looking for function: ") +
94 function_names[index] + "\n" + "Unwind data:\n" + unwind_stream.str();
95}
96
Christopher Ferris5f118512017-09-01 11:17:16 -070097static void VerifyUnwind(pid_t pid, Maps* maps, Regs* regs,
Christopher Ferrisa0196652017-07-18 16:09:20 -070098 std::vector<const char*>& function_names) {
Christopher Ferris2a25c4a2017-07-07 16:35:48 -070099 size_t function_name_index = 0;
100
Christopher Ferris5f118512017-09-01 11:17:16 -0700101 auto process_memory = Memory::CreateProcessMemory(pid);
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700102 std::stringstream unwind_stream;
103 unwind_stream << std::hex;
104 for (size_t frame_num = 0; frame_num < 64; frame_num++) {
105 ASSERT_NE(0U, regs->pc()) << ErrorMsg(function_names, function_name_index, unwind_stream);
106 MapInfo* map_info = maps->Find(regs->pc());
107 ASSERT_TRUE(map_info != nullptr) << ErrorMsg(function_names, function_name_index, unwind_stream);
108
Christopher Ferris5f118512017-09-01 11:17:16 -0700109 Elf* elf = map_info->GetElf(process_memory, true);
Christopher Ferrisd226a512017-07-14 10:37:19 -0700110 uint64_t rel_pc = elf->GetRelPc(regs->pc(), map_info);
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700111 uint64_t adjusted_rel_pc = rel_pc;
112 if (frame_num != 0) {
113 adjusted_rel_pc = regs->GetAdjustedPc(rel_pc, elf);
114 }
115 unwind_stream << " PC: 0x" << regs->pc() << " Rel: 0x" << adjusted_rel_pc;
116 unwind_stream << " Map: ";
117 if (!map_info->name.empty()) {
118 unwind_stream << map_info->name;
119 } else {
120 unwind_stream << " anonymous";
121 }
122 unwind_stream << "<" << map_info->start << "-" << map_info->end << ">";
123
124 std::string name;
125 uint64_t func_offset;
126 if (elf->GetFunctionName(adjusted_rel_pc, &name, &func_offset)) {
127 if (name == function_names[function_name_index]) {
Christopher Ferrisa0196652017-07-18 16:09:20 -0700128 if (++function_name_index == function_names.size()) {
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700129 return;
130 }
131 }
132 unwind_stream << " " << name;
133 }
134 unwind_stream << "\n";
Christopher Ferris5f118512017-09-01 11:17:16 -0700135 ASSERT_TRUE(elf->Step(rel_pc + map_info->elf_offset, regs, process_memory.get()))
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700136 << ErrorMsg(function_names, function_name_index, unwind_stream);
137 }
138 ASSERT_TRUE(false) << ErrorMsg(function_names, function_name_index, unwind_stream);
139}
140
141// This test assumes that this code is compiled with optimizations turned
142// off. If this doesn't happen, then all of the calls will be optimized
143// away.
144extern "C" void InnerFunction(bool local) {
145 if (local) {
146 LocalMaps maps;
147 ASSERT_TRUE(maps.Parse());
148 std::unique_ptr<Regs> regs(Regs::CreateFromLocal());
149 RegsGetLocal(regs.get());
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700150
Christopher Ferris5f118512017-09-01 11:17:16 -0700151 VerifyUnwind(getpid(), &maps, regs.get(), kFunctionOrder);
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700152 } else {
153 g_ready_for_remote = true;
154 g_ready = true;
155 while (!g_finish.load()) {
156 }
157 }
158}
159
160extern "C" void MiddleFunction(bool local) {
161 InnerFunction(local);
162}
163
164extern "C" void OuterFunction(bool local) {
165 MiddleFunction(local);
166}
167
Christopher Ferrisedccd842017-09-06 14:15:28 -0700168class UnwindTest : public ::testing::Test {
169 public:
170 void SetUp() override { ResetGlobals(); }
171};
172
173TEST_F(UnwindTest, local) {
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700174 OuterFunction(true);
175}
176
Christopher Ferrisa0196652017-07-18 16:09:20 -0700177void WaitForRemote(pid_t pid, uint64_t addr, bool leave_attached, bool* completed) {
178 *completed = false;
179 // Need to sleep before attempting first ptrace. Without this, on the
Christopher Ferrisedccd842017-09-06 14:15:28 -0700180 // host it becomes impossible to attach and ptrace sets errno to EPERM.
Christopher Ferrisa0196652017-07-18 16:09:20 -0700181 usleep(1000);
Christopher Ferrisedccd842017-09-06 14:15:28 -0700182 for (size_t i = 0; i < 1000; i++) {
183 if (ptrace(PTRACE_ATTACH, pid, 0, 0) == 0) {
184 ASSERT_TRUE(TestQuiescePid(pid))
185 << "Waiting for process to quiesce failed: " << strerror(errno);
186
187 MemoryRemote memory(pid);
188 // Read the remote value to see if we are ready.
189 bool value;
190 if (memory.Read(addr, &value, sizeof(value)) && value) {
191 *completed = true;
Christopher Ferrisa0196652017-07-18 16:09:20 -0700192 }
Christopher Ferrisedccd842017-09-06 14:15:28 -0700193 if (!*completed || !leave_attached) {
194 ASSERT_EQ(0, ptrace(PTRACE_DETACH, pid, 0, 0));
195 }
196 if (*completed) {
197 break;
198 }
199 } else {
200 ASSERT_EQ(ESRCH, errno) << "ptrace attach failed with unexpected error: " << strerror(errno);
Christopher Ferrisa0196652017-07-18 16:09:20 -0700201 }
Christopher Ferrisedccd842017-09-06 14:15:28 -0700202 usleep(5000);
Christopher Ferrisa0196652017-07-18 16:09:20 -0700203 }
204}
205
Christopher Ferrisedccd842017-09-06 14:15:28 -0700206TEST_F(UnwindTest, remote) {
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700207 pid_t pid;
208 if ((pid = fork()) == 0) {
209 OuterFunction(false);
210 exit(0);
211 }
212 ASSERT_NE(-1, pid);
Christopher Ferrisedccd842017-09-06 14:15:28 -0700213 TestScopedPidReaper reap(pid);
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700214
Christopher Ferrisa0196652017-07-18 16:09:20 -0700215 bool completed;
216 WaitForRemote(pid, reinterpret_cast<uint64_t>(&g_ready_for_remote), true, &completed);
217 ASSERT_TRUE(completed) << "Timed out waiting for remote process to be ready.";
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700218
219 RemoteMaps maps(pid);
220 ASSERT_TRUE(maps.Parse());
Josh Gao0953ecd2017-08-25 13:55:06 -0700221 std::unique_ptr<Regs> regs(Regs::RemoteGet(pid));
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700222 ASSERT_TRUE(regs.get() != nullptr);
223
Christopher Ferris5f118512017-09-01 11:17:16 -0700224 VerifyUnwind(pid, &maps, regs.get(), kFunctionOrder);
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700225
Christopher Ferrisedccd842017-09-06 14:15:28 -0700226 ASSERT_EQ(0, ptrace(PTRACE_DETACH, pid, 0, 0))
227 << "ptrace detach failed with unexpected error: " << strerror(errno);
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700228}
229
Christopher Ferrisedccd842017-09-06 14:15:28 -0700230TEST_F(UnwindTest, from_context) {
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700231 std::atomic_int tid(0);
232 std::thread thread([&]() {
233 tid = syscall(__NR_gettid);
234 OuterFunction(false);
235 });
236
237 struct sigaction act, oldact;
238 memset(&act, 0, sizeof(act));
Christopher Ferrisa0196652017-07-18 16:09:20 -0700239 act.sa_sigaction = SignalHandler;
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700240 act.sa_flags = SA_RESTART | SA_SIGINFO | SA_ONSTACK;
241 ASSERT_EQ(0, sigaction(SIGUSR1, &act, &oldact));
242 // Wait for the tid to get set.
243 for (size_t i = 0; i < 100; i++) {
244 if (tid.load() != 0) {
245 break;
246 }
247 usleep(1000);
248 }
249 ASSERT_NE(0, tid.load());
250 // Portable tgkill method.
Christopher Ferrisa0196652017-07-18 16:09:20 -0700251 ASSERT_EQ(0, syscall(__NR_tgkill, getpid(), tid.load(), SIGUSR1)) << "Error: " << strerror(errno);
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700252
253 // Wait for context data.
254 void* ucontext;
Christopher Ferrisd226a512017-07-14 10:37:19 -0700255 for (size_t i = 0; i < 2000; i++) {
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700256 ucontext = reinterpret_cast<void*>(g_ucontext.load());
257 if (ucontext != nullptr) {
258 break;
259 }
260 usleep(1000);
261 }
262 ASSERT_TRUE(ucontext != nullptr) << "Timed out waiting for thread to respond to signal.";
263
264 LocalMaps maps;
265 ASSERT_TRUE(maps.Parse());
Josh Gao0953ecd2017-08-25 13:55:06 -0700266 std::unique_ptr<Regs> regs(Regs::CreateFromUcontext(Regs::CurrentMachineType(), ucontext));
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700267
Christopher Ferris5f118512017-09-01 11:17:16 -0700268 VerifyUnwind(getpid(), &maps, regs.get(), kFunctionOrder);
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700269
270 ASSERT_EQ(0, sigaction(SIGUSR1, &oldact, nullptr));
271
272 g_finish = true;
273 thread.join();
274}
Christopher Ferrisd226a512017-07-14 10:37:19 -0700275
Christopher Ferrisa0196652017-07-18 16:09:20 -0700276static void RemoteThroughSignal(unsigned int sa_flags) {
Christopher Ferrisa0196652017-07-18 16:09:20 -0700277 pid_t pid;
278 if ((pid = fork()) == 0) {
279 struct sigaction act, oldact;
280 memset(&act, 0, sizeof(act));
281 act.sa_sigaction = SignalCallerHandler;
282 act.sa_flags = SA_RESTART | SA_ONSTACK | sa_flags;
283 ASSERT_EQ(0, sigaction(SIGUSR1, &act, &oldact));
284
285 OuterFunction(false);
286 exit(0);
287 }
288 ASSERT_NE(-1, pid);
Christopher Ferrisedccd842017-09-06 14:15:28 -0700289 TestScopedPidReaper reap(pid);
Christopher Ferrisa0196652017-07-18 16:09:20 -0700290
291 bool completed;
292 WaitForRemote(pid, reinterpret_cast<uint64_t>(&g_ready_for_remote), false, &completed);
293 ASSERT_TRUE(completed) << "Timed out waiting for remote process to be ready.";
294 ASSERT_EQ(0, kill(pid, SIGUSR1));
295 WaitForRemote(pid, reinterpret_cast<uint64_t>(&g_signal_ready_for_remote), true, &completed);
296 ASSERT_TRUE(completed) << "Timed out waiting for remote process to be in signal handler.";
297
298 RemoteMaps maps(pid);
299 ASSERT_TRUE(maps.Parse());
Josh Gao0953ecd2017-08-25 13:55:06 -0700300 std::unique_ptr<Regs> regs(Regs::RemoteGet(pid));
Christopher Ferrisa0196652017-07-18 16:09:20 -0700301 ASSERT_TRUE(regs.get() != nullptr);
302
Christopher Ferris5f118512017-09-01 11:17:16 -0700303 VerifyUnwind(pid, &maps, regs.get(), kFunctionSignalOrder);
Christopher Ferrisa0196652017-07-18 16:09:20 -0700304
Christopher Ferrisedccd842017-09-06 14:15:28 -0700305 ASSERT_EQ(0, ptrace(PTRACE_DETACH, pid, 0, 0))
306 << "ptrace detach failed with unexpected error: " << strerror(errno);
Christopher Ferrisa0196652017-07-18 16:09:20 -0700307}
308
Christopher Ferrisedccd842017-09-06 14:15:28 -0700309TEST_F(UnwindTest, remote_through_signal) {
Christopher Ferrisa0196652017-07-18 16:09:20 -0700310 RemoteThroughSignal(0);
311}
312
Christopher Ferrisedccd842017-09-06 14:15:28 -0700313TEST_F(UnwindTest, remote_through_signal_sa_siginfo) {
Christopher Ferrisa0196652017-07-18 16:09:20 -0700314 RemoteThroughSignal(SA_SIGINFO);
315}
316
Christopher Ferrisd226a512017-07-14 10:37:19 -0700317} // namespace unwindstack