blob: 2fc3a3880634a68a92d8ddd5bdb8b40734388b18 [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>
18#include <string.h>
19
20#include <signal.h>
21#include <stdint.h>
22#include <sys/ptrace.h>
23#include <sys/syscall.h>
24#include <unistd.h>
25
26#include <gtest/gtest.h>
27
28#include <atomic>
29#include <memory>
30#include <sstream>
31#include <string>
32#include <thread>
Christopher Ferrisa0196652017-07-18 16:09:20 -070033#include <vector>
Christopher Ferris2a25c4a2017-07-07 16:35:48 -070034
Christopher Ferrisd226a512017-07-14 10:37:19 -070035#include <unwindstack/Elf.h>
36#include <unwindstack/MapInfo.h>
37#include <unwindstack/Maps.h>
38#include <unwindstack/Memory.h>
39#include <unwindstack/Regs.h>
40#include <unwindstack/RegsGetLocal.h>
41
42namespace unwindstack {
Christopher Ferris2a25c4a2017-07-07 16:35:48 -070043
44static std::atomic_bool g_ready(false);
45static volatile bool g_ready_for_remote = false;
Christopher Ferrisa0196652017-07-18 16:09:20 -070046static volatile bool g_signal_ready_for_remote = false;
Christopher Ferris2a25c4a2017-07-07 16:35:48 -070047static std::atomic_bool g_finish(false);
48static std::atomic_uintptr_t g_ucontext;
49
Christopher Ferrisa0196652017-07-18 16:09:20 -070050static std::vector<const char*> kFunctionOrder{"InnerFunction", "MiddleFunction", "OuterFunction"};
51
52static std::vector<const char*> kFunctionSignalOrder{"SignalInnerFunction", "SignalMiddleFunction",
53 "SignalOuterFunction", "InnerFunction",
54 "MiddleFunction", "OuterFunction"};
55
56static void SignalHandler(int, siginfo_t*, void* sigcontext) {
Christopher Ferris2a25c4a2017-07-07 16:35:48 -070057 g_ucontext = reinterpret_cast<uintptr_t>(sigcontext);
58 while (!g_finish.load()) {
59 }
60}
61
Christopher Ferrisa0196652017-07-18 16:09:20 -070062extern "C" void SignalInnerFunction() {
63 g_signal_ready_for_remote = true;
64 while (!g_finish.load()) {
65 }
66}
67
68extern "C" void SignalMiddleFunction() {
69 SignalInnerFunction();
70}
71
72extern "C" void SignalOuterFunction() {
73 SignalMiddleFunction();
74}
75
76static void SignalCallerHandler(int, siginfo_t*, void*) {
77 SignalOuterFunction();
78}
79
80static std::string ErrorMsg(const std::vector<const char*>& function_names, size_t index,
Christopher Ferris2a25c4a2017-07-07 16:35:48 -070081 std::stringstream& unwind_stream) {
82 return std::string(
83 "Unwind completed without finding all frames\n"
84 " Looking for function: ") +
85 function_names[index] + "\n" + "Unwind data:\n" + unwind_stream.str();
86}
87
Christopher Ferrisa0196652017-07-18 16:09:20 -070088static void VerifyUnwind(pid_t pid, Memory* memory, Maps* maps, Regs* regs,
89 std::vector<const char*>& function_names) {
Christopher Ferris2a25c4a2017-07-07 16:35:48 -070090 size_t function_name_index = 0;
91
92 std::stringstream unwind_stream;
93 unwind_stream << std::hex;
94 for (size_t frame_num = 0; frame_num < 64; frame_num++) {
95 ASSERT_NE(0U, regs->pc()) << ErrorMsg(function_names, function_name_index, unwind_stream);
96 MapInfo* map_info = maps->Find(regs->pc());
97 ASSERT_TRUE(map_info != nullptr) << ErrorMsg(function_names, function_name_index, unwind_stream);
98
99 Elf* elf = map_info->GetElf(pid, true);
Christopher Ferrisd226a512017-07-14 10:37:19 -0700100 uint64_t rel_pc = elf->GetRelPc(regs->pc(), map_info);
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700101 uint64_t adjusted_rel_pc = rel_pc;
102 if (frame_num != 0) {
103 adjusted_rel_pc = regs->GetAdjustedPc(rel_pc, elf);
104 }
105 unwind_stream << " PC: 0x" << regs->pc() << " Rel: 0x" << adjusted_rel_pc;
106 unwind_stream << " Map: ";
107 if (!map_info->name.empty()) {
108 unwind_stream << map_info->name;
109 } else {
110 unwind_stream << " anonymous";
111 }
112 unwind_stream << "<" << map_info->start << "-" << map_info->end << ">";
113
114 std::string name;
115 uint64_t func_offset;
116 if (elf->GetFunctionName(adjusted_rel_pc, &name, &func_offset)) {
117 if (name == function_names[function_name_index]) {
Christopher Ferrisa0196652017-07-18 16:09:20 -0700118 if (++function_name_index == function_names.size()) {
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700119 return;
120 }
121 }
122 unwind_stream << " " << name;
123 }
124 unwind_stream << "\n";
125 ASSERT_TRUE(elf->Step(rel_pc + map_info->elf_offset, regs, memory))
126 << ErrorMsg(function_names, function_name_index, unwind_stream);
127 }
128 ASSERT_TRUE(false) << ErrorMsg(function_names, function_name_index, unwind_stream);
129}
130
131// This test assumes that this code is compiled with optimizations turned
132// off. If this doesn't happen, then all of the calls will be optimized
133// away.
134extern "C" void InnerFunction(bool local) {
135 if (local) {
136 LocalMaps maps;
137 ASSERT_TRUE(maps.Parse());
138 std::unique_ptr<Regs> regs(Regs::CreateFromLocal());
139 RegsGetLocal(regs.get());
140 MemoryLocal memory;
141
Christopher Ferrisa0196652017-07-18 16:09:20 -0700142 VerifyUnwind(getpid(), &memory, &maps, regs.get(), kFunctionOrder);
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700143 } else {
144 g_ready_for_remote = true;
145 g_ready = true;
146 while (!g_finish.load()) {
147 }
148 }
149}
150
151extern "C" void MiddleFunction(bool local) {
152 InnerFunction(local);
153}
154
155extern "C" void OuterFunction(bool local) {
156 MiddleFunction(local);
157}
158
159TEST(UnwindTest, local) {
160 OuterFunction(true);
161}
162
Christopher Ferrisa0196652017-07-18 16:09:20 -0700163void WaitForRemote(pid_t pid, uint64_t addr, bool leave_attached, bool* completed) {
164 *completed = false;
165 // Need to sleep before attempting first ptrace. Without this, on the
166 // host it becomes impossible to attach and ptrace set errno to EPERM.
167 usleep(1000);
168 for (size_t i = 0; i < 100; i++) {
169 ASSERT_EQ(0, ptrace(PTRACE_ATTACH, pid, 0, 0));
170 for (size_t j = 0; j < 100; j++) {
171 siginfo_t si;
172 if (ptrace(PTRACE_GETSIGINFO, pid, 0, &si) == 0) {
173 MemoryRemote memory(pid);
174 // Read the remote value to see if we are ready.
175 bool value;
176 if (memory.Read(addr, &value, sizeof(value)) && value) {
177 *completed = true;
178 break;
179 }
180 }
181 usleep(1000);
182 }
183 if (leave_attached && *completed) {
184 break;
185 }
186 ASSERT_EQ(0, ptrace(PTRACE_DETACH, pid, 0, 0));
187 if (*completed) {
188 break;
189 }
190 usleep(1000);
191 }
192}
193
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700194TEST(UnwindTest, remote) {
195 pid_t pid;
196 if ((pid = fork()) == 0) {
197 OuterFunction(false);
198 exit(0);
199 }
200 ASSERT_NE(-1, pid);
201
Christopher Ferrisa0196652017-07-18 16:09:20 -0700202 bool completed;
203 WaitForRemote(pid, reinterpret_cast<uint64_t>(&g_ready_for_remote), true, &completed);
204 ASSERT_TRUE(completed) << "Timed out waiting for remote process to be ready.";
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700205
206 RemoteMaps maps(pid);
207 ASSERT_TRUE(maps.Parse());
208 MemoryRemote memory(pid);
Josh Gao0953ecd2017-08-25 13:55:06 -0700209 std::unique_ptr<Regs> regs(Regs::RemoteGet(pid));
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700210 ASSERT_TRUE(regs.get() != nullptr);
211
Christopher Ferrisa0196652017-07-18 16:09:20 -0700212 VerifyUnwind(pid, &memory, &maps, regs.get(), kFunctionOrder);
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700213
214 ASSERT_EQ(0, ptrace(PTRACE_DETACH, pid, 0, 0));
215
216 kill(pid, SIGKILL);
217 ASSERT_EQ(pid, wait(nullptr));
218}
219
220TEST(UnwindTest, from_context) {
221 std::atomic_int tid(0);
222 std::thread thread([&]() {
223 tid = syscall(__NR_gettid);
224 OuterFunction(false);
225 });
226
227 struct sigaction act, oldact;
228 memset(&act, 0, sizeof(act));
Christopher Ferrisa0196652017-07-18 16:09:20 -0700229 act.sa_sigaction = SignalHandler;
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700230 act.sa_flags = SA_RESTART | SA_SIGINFO | SA_ONSTACK;
231 ASSERT_EQ(0, sigaction(SIGUSR1, &act, &oldact));
232 // Wait for the tid to get set.
233 for (size_t i = 0; i < 100; i++) {
234 if (tid.load() != 0) {
235 break;
236 }
237 usleep(1000);
238 }
239 ASSERT_NE(0, tid.load());
240 // Portable tgkill method.
Christopher Ferrisa0196652017-07-18 16:09:20 -0700241 ASSERT_EQ(0, syscall(__NR_tgkill, getpid(), tid.load(), SIGUSR1)) << "Error: " << strerror(errno);
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700242
243 // Wait for context data.
244 void* ucontext;
Christopher Ferrisd226a512017-07-14 10:37:19 -0700245 for (size_t i = 0; i < 2000; i++) {
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700246 ucontext = reinterpret_cast<void*>(g_ucontext.load());
247 if (ucontext != nullptr) {
248 break;
249 }
250 usleep(1000);
251 }
252 ASSERT_TRUE(ucontext != nullptr) << "Timed out waiting for thread to respond to signal.";
253
254 LocalMaps maps;
255 ASSERT_TRUE(maps.Parse());
Josh Gao0953ecd2017-08-25 13:55:06 -0700256 std::unique_ptr<Regs> regs(Regs::CreateFromUcontext(Regs::CurrentMachineType(), ucontext));
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700257 MemoryLocal memory;
258
Christopher Ferrisa0196652017-07-18 16:09:20 -0700259 VerifyUnwind(tid.load(), &memory, &maps, regs.get(), kFunctionOrder);
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700260
261 ASSERT_EQ(0, sigaction(SIGUSR1, &oldact, nullptr));
262
263 g_finish = true;
264 thread.join();
265}
Christopher Ferrisd226a512017-07-14 10:37:19 -0700266
Christopher Ferrisa0196652017-07-18 16:09:20 -0700267static void RemoteThroughSignal(unsigned int sa_flags) {
268 g_ready = false;
269 g_signal_ready_for_remote = false;
270 g_finish = false;
271
272 pid_t pid;
273 if ((pid = fork()) == 0) {
274 struct sigaction act, oldact;
275 memset(&act, 0, sizeof(act));
276 act.sa_sigaction = SignalCallerHandler;
277 act.sa_flags = SA_RESTART | SA_ONSTACK | sa_flags;
278 ASSERT_EQ(0, sigaction(SIGUSR1, &act, &oldact));
279
280 OuterFunction(false);
281 exit(0);
282 }
283 ASSERT_NE(-1, pid);
284
285 bool completed;
286 WaitForRemote(pid, reinterpret_cast<uint64_t>(&g_ready_for_remote), false, &completed);
287 ASSERT_TRUE(completed) << "Timed out waiting for remote process to be ready.";
288 ASSERT_EQ(0, kill(pid, SIGUSR1));
289 WaitForRemote(pid, reinterpret_cast<uint64_t>(&g_signal_ready_for_remote), true, &completed);
290 ASSERT_TRUE(completed) << "Timed out waiting for remote process to be in signal handler.";
291
292 RemoteMaps maps(pid);
293 ASSERT_TRUE(maps.Parse());
294 MemoryRemote memory(pid);
Josh Gao0953ecd2017-08-25 13:55:06 -0700295 std::unique_ptr<Regs> regs(Regs::RemoteGet(pid));
Christopher Ferrisa0196652017-07-18 16:09:20 -0700296 ASSERT_TRUE(regs.get() != nullptr);
297
298 VerifyUnwind(pid, &memory, &maps, regs.get(), kFunctionSignalOrder);
299
300 ASSERT_EQ(0, ptrace(PTRACE_DETACH, pid, 0, 0));
301
302 kill(pid, SIGKILL);
303 ASSERT_EQ(pid, wait(nullptr));
304}
305
306TEST(UnwindTest, remote_through_signal) {
307 RemoteThroughSignal(0);
308}
309
310TEST(UnwindTest, remote_through_signal_sa_siginfo) {
311 RemoteThroughSignal(SA_SIGINFO);
312}
313
Christopher Ferrisd226a512017-07-14 10:37:19 -0700314} // namespace unwindstack