blob: ce04817db462a7b2c06c353d8b20057b683dfdc8 [file] [log] [blame]
Christopher Ferris17e91d42013-10-21 13:30:52 -07001/*
2 * Copyright (C) 2013 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
Christopher Ferrisca09ce92015-03-31 17:28:22 -070017#define _GNU_SOURCE 1
Christopher Ferris17e91d42013-10-21 13:30:52 -070018#include <dirent.h>
Christopher Ferris67aba682015-05-08 15:44:46 -070019#include <dlfcn.h>
Christopher Ferris17e91d42013-10-21 13:30:52 -070020#include <errno.h>
Christopher Ferris67aba682015-05-08 15:44:46 -070021#include <fcntl.h>
Christopher Ferrise2960912014-03-07 19:42:19 -080022#include <inttypes.h>
Christopher Ferris17e91d42013-10-21 13:30:52 -070023#include <pthread.h>
24#include <signal.h>
Christopher Ferrise2960912014-03-07 19:42:19 -080025#include <stdint.h>
Christopher Ferris17e91d42013-10-21 13:30:52 -070026#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29#include <sys/ptrace.h>
Christopher Ferris67aba682015-05-08 15:44:46 -070030#include <sys/stat.h>
Christopher Ferris17e91d42013-10-21 13:30:52 -070031#include <sys/types.h>
32#include <sys/wait.h>
33#include <time.h>
34#include <unistd.h>
35
Christopher Ferrise2960912014-03-07 19:42:19 -080036#include <algorithm>
Christopher Ferris67aba682015-05-08 15:44:46 -070037#include <list>
Christopher Ferris2b4a63f2015-03-17 14:42:03 -070038#include <memory>
Christopher Ferris2c43cff2015-03-26 19:18:36 -070039#include <string>
Christopher Ferris17e91d42013-10-21 13:30:52 -070040#include <vector>
41
Dan Albert23f750b2015-04-30 12:52:21 -070042#include <backtrace/Backtrace.h>
43#include <backtrace/BacktraceMap.h>
44
Christopher Ferris67aba682015-05-08 15:44:46 -070045#include <base/stringprintf.h>
Dan Albert23f750b2015-04-30 12:52:21 -070046#include <cutils/atomic.h>
47#include <cutils/threads.h>
48
49#include <gtest/gtest.h>
50
51// For the THREAD_SIGNAL definition.
52#include "BacktraceCurrent.h"
Christopher Ferris17e91d42013-10-21 13:30:52 -070053#include "thread_utils.h"
54
55// Number of microseconds per milliseconds.
56#define US_PER_MSEC 1000
57
58// Number of nanoseconds in a second.
59#define NS_PER_SEC 1000000000ULL
60
61// Number of simultaneous dumping operations to perform.
Christopher Ferris3cdbfdc2014-11-08 15:57:11 -080062#define NUM_THREADS 40
Christopher Ferris17e91d42013-10-21 13:30:52 -070063
64// Number of simultaneous threads running in our forked process.
65#define NUM_PTRACE_THREADS 5
66
Christopher Ferris46756822014-01-14 20:16:30 -080067struct thread_t {
Christopher Ferris17e91d42013-10-21 13:30:52 -070068 pid_t tid;
69 int32_t state;
70 pthread_t threadId;
Christopher Ferris2b4a63f2015-03-17 14:42:03 -070071 void* data;
Christopher Ferris46756822014-01-14 20:16:30 -080072};
Christopher Ferris17e91d42013-10-21 13:30:52 -070073
Christopher Ferris46756822014-01-14 20:16:30 -080074struct dump_thread_t {
Christopher Ferris17e91d42013-10-21 13:30:52 -070075 thread_t thread;
Christopher Ferris20303f82014-01-10 16:33:16 -080076 Backtrace* backtrace;
Christopher Ferris17e91d42013-10-21 13:30:52 -070077 int32_t* now;
78 int32_t done;
Christopher Ferris46756822014-01-14 20:16:30 -080079};
Christopher Ferris17e91d42013-10-21 13:30:52 -070080
81extern "C" {
82// Prototypes for functions in the test library.
83int test_level_one(int, int, int, int, void (*)(void*), void*);
84
85int test_recursive_call(int, void (*)(void*), void*);
86}
87
88uint64_t NanoTime() {
89 struct timespec t = { 0, 0 };
90 clock_gettime(CLOCK_MONOTONIC, &t);
91 return static_cast<uint64_t>(t.tv_sec * NS_PER_SEC + t.tv_nsec);
92}
93
Christopher Ferris2c43cff2015-03-26 19:18:36 -070094std::string DumpFrames(Backtrace* backtrace) {
Christopher Ferris20303f82014-01-10 16:33:16 -080095 if (backtrace->NumFrames() == 0) {
Christopher Ferris97e00bb2015-04-02 14:22:31 -070096 return " No frames to dump.\n";
Christopher Ferris20303f82014-01-10 16:33:16 -080097 }
98
Christopher Ferris2c43cff2015-03-26 19:18:36 -070099 std::string frame;
Christopher Ferris20303f82014-01-10 16:33:16 -0800100 for (size_t i = 0; i < backtrace->NumFrames(); i++) {
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700101 frame += " " + backtrace->FormatFrameData(i) + '\n';
Christopher Ferris17e91d42013-10-21 13:30:52 -0700102 }
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700103 return frame;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700104}
105
106void WaitForStop(pid_t pid) {
107 uint64_t start = NanoTime();
108
109 siginfo_t si;
110 while (ptrace(PTRACE_GETSIGINFO, pid, 0, &si) < 0 && (errno == EINTR || errno == ESRCH)) {
111 if ((NanoTime() - start) > NS_PER_SEC) {
112 printf("The process did not get to a stopping point in 1 second.\n");
113 break;
114 }
115 usleep(US_PER_MSEC);
116 }
117}
118
Christopher Ferris20303f82014-01-10 16:33:16 -0800119bool ReadyLevelBacktrace(Backtrace* backtrace) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700120 // See if test_level_four is in the backtrace.
121 bool found = false;
Christopher Ferris46756822014-01-14 20:16:30 -0800122 for (Backtrace::const_iterator it = backtrace->begin(); it != backtrace->end(); ++it) {
123 if (it->func_name == "test_level_four") {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700124 found = true;
125 break;
126 }
127 }
128
129 return found;
130}
131
Christopher Ferris20303f82014-01-10 16:33:16 -0800132void VerifyLevelDump(Backtrace* backtrace) {
Christopher Ferris97e00bb2015-04-02 14:22:31 -0700133 ASSERT_GT(backtrace->NumFrames(), static_cast<size_t>(0))
134 << DumpFrames(backtrace);
135 ASSERT_LT(backtrace->NumFrames(), static_cast<size_t>(MAX_BACKTRACE_FRAMES))
136 << DumpFrames(backtrace);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700137
138 // Look through the frames starting at the highest to find the
139 // frame we want.
140 size_t frame_num = 0;
Christopher Ferris20303f82014-01-10 16:33:16 -0800141 for (size_t i = backtrace->NumFrames()-1; i > 2; i--) {
Christopher Ferris46756822014-01-14 20:16:30 -0800142 if (backtrace->GetFrame(i)->func_name == "test_level_one") {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700143 frame_num = i;
144 break;
145 }
146 }
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700147 ASSERT_LT(static_cast<size_t>(0), frame_num) << DumpFrames(backtrace);
148 ASSERT_LE(static_cast<size_t>(3), frame_num) << DumpFrames(backtrace);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700149
Christopher Ferris97e00bb2015-04-02 14:22:31 -0700150 ASSERT_EQ(backtrace->GetFrame(frame_num)->func_name, "test_level_one")
151 << DumpFrames(backtrace);
152 ASSERT_EQ(backtrace->GetFrame(frame_num-1)->func_name, "test_level_two")
153 << DumpFrames(backtrace);
154 ASSERT_EQ(backtrace->GetFrame(frame_num-2)->func_name, "test_level_three")
155 << DumpFrames(backtrace);
156 ASSERT_EQ(backtrace->GetFrame(frame_num-3)->func_name, "test_level_four")
157 << DumpFrames(backtrace);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700158}
159
160void VerifyLevelBacktrace(void*) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700161 std::unique_ptr<Backtrace> backtrace(
Christopher Ferris20303f82014-01-10 16:33:16 -0800162 Backtrace::Create(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD));
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700163 ASSERT_TRUE(backtrace.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800164 ASSERT_TRUE(backtrace->Unwind(0));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700165
Christopher Ferris20303f82014-01-10 16:33:16 -0800166 VerifyLevelDump(backtrace.get());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700167}
168
Christopher Ferris20303f82014-01-10 16:33:16 -0800169bool ReadyMaxBacktrace(Backtrace* backtrace) {
170 return (backtrace->NumFrames() == MAX_BACKTRACE_FRAMES);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700171}
172
Christopher Ferris20303f82014-01-10 16:33:16 -0800173void VerifyMaxDump(Backtrace* backtrace) {
Christopher Ferris97e00bb2015-04-02 14:22:31 -0700174 ASSERT_EQ(backtrace->NumFrames(), static_cast<size_t>(MAX_BACKTRACE_FRAMES))
175 << DumpFrames(backtrace);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700176 // Verify that the last frame is our recursive call.
Christopher Ferris97e00bb2015-04-02 14:22:31 -0700177 ASSERT_EQ(backtrace->GetFrame(MAX_BACKTRACE_FRAMES-1)->func_name, "test_recursive_call")
178 << DumpFrames(backtrace);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700179}
180
181void VerifyMaxBacktrace(void*) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700182 std::unique_ptr<Backtrace> backtrace(
Christopher Ferris20303f82014-01-10 16:33:16 -0800183 Backtrace::Create(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD));
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700184 ASSERT_TRUE(backtrace.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800185 ASSERT_TRUE(backtrace->Unwind(0));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700186
Christopher Ferris20303f82014-01-10 16:33:16 -0800187 VerifyMaxDump(backtrace.get());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700188}
189
190void ThreadSetState(void* data) {
191 thread_t* thread = reinterpret_cast<thread_t*>(data);
192 android_atomic_acquire_store(1, &thread->state);
193 volatile int i = 0;
194 while (thread->state) {
195 i++;
196 }
197}
198
Christopher Ferris20303f82014-01-10 16:33:16 -0800199void VerifyThreadTest(pid_t tid, void (*VerifyFunc)(Backtrace*)) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700200 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), tid));
201 ASSERT_TRUE(backtrace.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800202 ASSERT_TRUE(backtrace->Unwind(0));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700203
Christopher Ferris20303f82014-01-10 16:33:16 -0800204 VerifyFunc(backtrace.get());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700205}
206
207bool WaitForNonZero(int32_t* value, uint64_t seconds) {
208 uint64_t start = NanoTime();
209 do {
210 if (android_atomic_acquire_load(value)) {
211 return true;
212 }
213 } while ((NanoTime() - start) < seconds * NS_PER_SEC);
214 return false;
215}
216
Christopher Ferrisca09ce92015-03-31 17:28:22 -0700217TEST(libbacktrace, local_no_unwind_frames) {
218 // Verify that a local unwind does not include any frames within
219 // libunwind or libbacktrace.
220 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), getpid()));
Christopher Ferris97e00bb2015-04-02 14:22:31 -0700221 ASSERT_TRUE(backtrace.get() != nullptr);
Christopher Ferrisca09ce92015-03-31 17:28:22 -0700222 ASSERT_TRUE(backtrace->Unwind(0));
223
224 ASSERT_TRUE(backtrace->NumFrames() != 0);
225 for (const auto& frame : *backtrace ) {
226 if (BacktraceMap::IsValid(frame.map)) {
227 const std::string name = basename(frame.map.name.c_str());
228 ASSERT_TRUE(name != "libunwind.so" && name != "libbacktrace.so")
229 << DumpFrames(backtrace.get());
230 }
231 break;
232 }
233}
234
Christopher Ferris17e91d42013-10-21 13:30:52 -0700235TEST(libbacktrace, local_trace) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700236 ASSERT_NE(test_level_one(1, 2, 3, 4, VerifyLevelBacktrace, nullptr), 0);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700237}
238
239void VerifyIgnoreFrames(
Christopher Ferris20303f82014-01-10 16:33:16 -0800240 Backtrace* bt_all, Backtrace* bt_ign1,
241 Backtrace* bt_ign2, const char* cur_proc) {
Christopher Ferris97e00bb2015-04-02 14:22:31 -0700242 EXPECT_EQ(bt_all->NumFrames(), bt_ign1->NumFrames() + 1)
243 << "All backtrace:\n" << DumpFrames(bt_all) << "Ignore 1 backtrace:\n" << DumpFrames(bt_ign1);
244 EXPECT_EQ(bt_all->NumFrames(), bt_ign2->NumFrames() + 2)
245 << "All backtrace:\n" << DumpFrames(bt_all) << "Ignore 2 backtrace:\n" << DumpFrames(bt_ign2);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700246
247 // Check all of the frames are the same > the current frame.
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700248 bool check = (cur_proc == nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800249 for (size_t i = 0; i < bt_ign2->NumFrames(); i++) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700250 if (check) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800251 EXPECT_EQ(bt_ign2->GetFrame(i)->pc, bt_ign1->GetFrame(i+1)->pc);
252 EXPECT_EQ(bt_ign2->GetFrame(i)->sp, bt_ign1->GetFrame(i+1)->sp);
253 EXPECT_EQ(bt_ign2->GetFrame(i)->stack_size, bt_ign1->GetFrame(i+1)->stack_size);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700254
Christopher Ferris20303f82014-01-10 16:33:16 -0800255 EXPECT_EQ(bt_ign2->GetFrame(i)->pc, bt_all->GetFrame(i+2)->pc);
256 EXPECT_EQ(bt_ign2->GetFrame(i)->sp, bt_all->GetFrame(i+2)->sp);
257 EXPECT_EQ(bt_ign2->GetFrame(i)->stack_size, bt_all->GetFrame(i+2)->stack_size);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700258 }
Christopher Ferris46756822014-01-14 20:16:30 -0800259 if (!check && bt_ign2->GetFrame(i)->func_name == cur_proc) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700260 check = true;
261 }
262 }
263}
264
265void VerifyLevelIgnoreFrames(void*) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700266 std::unique_ptr<Backtrace> all(
Christopher Ferris20303f82014-01-10 16:33:16 -0800267 Backtrace::Create(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD));
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700268 ASSERT_TRUE(all.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800269 ASSERT_TRUE(all->Unwind(0));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700270
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700271 std::unique_ptr<Backtrace> ign1(
Christopher Ferris20303f82014-01-10 16:33:16 -0800272 Backtrace::Create(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD));
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700273 ASSERT_TRUE(ign1.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800274 ASSERT_TRUE(ign1->Unwind(1));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700275
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700276 std::unique_ptr<Backtrace> ign2(
Christopher Ferris20303f82014-01-10 16:33:16 -0800277 Backtrace::Create(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD));
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700278 ASSERT_TRUE(ign2.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800279 ASSERT_TRUE(ign2->Unwind(2));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700280
Christopher Ferris20303f82014-01-10 16:33:16 -0800281 VerifyIgnoreFrames(all.get(), ign1.get(), ign2.get(), "VerifyLevelIgnoreFrames");
Christopher Ferris17e91d42013-10-21 13:30:52 -0700282}
283
284TEST(libbacktrace, local_trace_ignore_frames) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700285 ASSERT_NE(test_level_one(1, 2, 3, 4, VerifyLevelIgnoreFrames, nullptr), 0);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700286}
287
288TEST(libbacktrace, local_max_trace) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700289 ASSERT_NE(test_recursive_call(MAX_BACKTRACE_FRAMES+10, VerifyMaxBacktrace, nullptr), 0);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700290}
291
Christopher Ferrisdf290612014-01-22 19:21:07 -0800292void VerifyProcTest(pid_t pid, pid_t tid, bool share_map,
Christopher Ferris20303f82014-01-10 16:33:16 -0800293 bool (*ReadyFunc)(Backtrace*),
294 void (*VerifyFunc)(Backtrace*)) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700295 pid_t ptrace_tid;
296 if (tid < 0) {
297 ptrace_tid = pid;
298 } else {
299 ptrace_tid = tid;
300 }
301 uint64_t start = NanoTime();
302 bool verified = false;
Christopher Ferris97e00bb2015-04-02 14:22:31 -0700303 std::string last_dump;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700304 do {
305 usleep(US_PER_MSEC);
306 if (ptrace(PTRACE_ATTACH, ptrace_tid, 0, 0) == 0) {
307 // Wait for the process to get to a stopping point.
308 WaitForStop(ptrace_tid);
309
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700310 std::unique_ptr<BacktraceMap> map;
Christopher Ferrisdf290612014-01-22 19:21:07 -0800311 if (share_map) {
312 map.reset(BacktraceMap::Create(pid));
313 }
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700314 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(pid, tid, map.get()));
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700315 ASSERT_TRUE(backtrace.get() != nullptr);
Christopher Ferris97e00bb2015-04-02 14:22:31 -0700316 ASSERT_TRUE(backtrace->Unwind(0));
Christopher Ferris20303f82014-01-10 16:33:16 -0800317 if (ReadyFunc(backtrace.get())) {
318 VerifyFunc(backtrace.get());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700319 verified = true;
Christopher Ferris97e00bb2015-04-02 14:22:31 -0700320 } else {
321 last_dump = DumpFrames(backtrace.get());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700322 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800323
Christopher Ferris17e91d42013-10-21 13:30:52 -0700324 ASSERT_TRUE(ptrace(PTRACE_DETACH, ptrace_tid, 0, 0) == 0);
325 }
326 // If 5 seconds have passed, then we are done.
327 } while (!verified && (NanoTime() - start) <= 5 * NS_PER_SEC);
Christopher Ferris97e00bb2015-04-02 14:22:31 -0700328 ASSERT_TRUE(verified) << "Last backtrace:\n" << last_dump;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700329}
330
331TEST(libbacktrace, ptrace_trace) {
332 pid_t pid;
333 if ((pid = fork()) == 0) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700334 ASSERT_NE(test_level_one(1, 2, 3, 4, nullptr, nullptr), 0);
Christopher Ferrise2960912014-03-07 19:42:19 -0800335 _exit(1);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700336 }
Christopher Ferrisdf290612014-01-22 19:21:07 -0800337 VerifyProcTest(pid, BACKTRACE_CURRENT_THREAD, false, ReadyLevelBacktrace, VerifyLevelDump);
338
339 kill(pid, SIGKILL);
340 int status;
341 ASSERT_EQ(waitpid(pid, &status, 0), pid);
342}
343
344TEST(libbacktrace, ptrace_trace_shared_map) {
345 pid_t pid;
346 if ((pid = fork()) == 0) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700347 ASSERT_NE(test_level_one(1, 2, 3, 4, nullptr, nullptr), 0);
Christopher Ferrise2960912014-03-07 19:42:19 -0800348 _exit(1);
Christopher Ferrisdf290612014-01-22 19:21:07 -0800349 }
350
351 VerifyProcTest(pid, BACKTRACE_CURRENT_THREAD, true, ReadyLevelBacktrace, VerifyLevelDump);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700352
353 kill(pid, SIGKILL);
354 int status;
355 ASSERT_EQ(waitpid(pid, &status, 0), pid);
356}
357
358TEST(libbacktrace, ptrace_max_trace) {
359 pid_t pid;
360 if ((pid = fork()) == 0) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700361 ASSERT_NE(test_recursive_call(MAX_BACKTRACE_FRAMES+10, nullptr, nullptr), 0);
Christopher Ferrise2960912014-03-07 19:42:19 -0800362 _exit(1);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700363 }
Christopher Ferrisdf290612014-01-22 19:21:07 -0800364 VerifyProcTest(pid, BACKTRACE_CURRENT_THREAD, false, ReadyMaxBacktrace, VerifyMaxDump);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700365
366 kill(pid, SIGKILL);
367 int status;
368 ASSERT_EQ(waitpid(pid, &status, 0), pid);
369}
370
Christopher Ferris20303f82014-01-10 16:33:16 -0800371void VerifyProcessIgnoreFrames(Backtrace* bt_all) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700372 std::unique_ptr<Backtrace> ign1(Backtrace::Create(bt_all->Pid(), BACKTRACE_CURRENT_THREAD));
373 ASSERT_TRUE(ign1.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800374 ASSERT_TRUE(ign1->Unwind(1));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700375
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700376 std::unique_ptr<Backtrace> ign2(Backtrace::Create(bt_all->Pid(), BACKTRACE_CURRENT_THREAD));
377 ASSERT_TRUE(ign2.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800378 ASSERT_TRUE(ign2->Unwind(2));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700379
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700380 VerifyIgnoreFrames(bt_all, ign1.get(), ign2.get(), nullptr);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700381}
382
383TEST(libbacktrace, ptrace_ignore_frames) {
384 pid_t pid;
385 if ((pid = fork()) == 0) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700386 ASSERT_NE(test_level_one(1, 2, 3, 4, nullptr, nullptr), 0);
Christopher Ferrise2960912014-03-07 19:42:19 -0800387 _exit(1);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700388 }
Christopher Ferrisdf290612014-01-22 19:21:07 -0800389 VerifyProcTest(pid, BACKTRACE_CURRENT_THREAD, false, ReadyLevelBacktrace, VerifyProcessIgnoreFrames);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700390
391 kill(pid, SIGKILL);
392 int status;
393 ASSERT_EQ(waitpid(pid, &status, 0), pid);
394}
395
396// Create a process with multiple threads and dump all of the threads.
397void* PtraceThreadLevelRun(void*) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700398 EXPECT_NE(test_level_one(1, 2, 3, 4, nullptr, nullptr), 0);
399 return nullptr;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700400}
401
402void GetThreads(pid_t pid, std::vector<pid_t>* threads) {
403 // Get the list of tasks.
404 char task_path[128];
405 snprintf(task_path, sizeof(task_path), "/proc/%d/task", pid);
406
407 DIR* tasks_dir = opendir(task_path);
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700408 ASSERT_TRUE(tasks_dir != nullptr);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700409 struct dirent* entry;
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700410 while ((entry = readdir(tasks_dir)) != nullptr) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700411 char* end;
412 pid_t tid = strtoul(entry->d_name, &end, 10);
413 if (*end == '\0') {
414 threads->push_back(tid);
415 }
416 }
417 closedir(tasks_dir);
418}
419
420TEST(libbacktrace, ptrace_threads) {
421 pid_t pid;
422 if ((pid = fork()) == 0) {
423 for (size_t i = 0; i < NUM_PTRACE_THREADS; i++) {
424 pthread_attr_t attr;
425 pthread_attr_init(&attr);
426 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
427
428 pthread_t thread;
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700429 ASSERT_TRUE(pthread_create(&thread, &attr, PtraceThreadLevelRun, nullptr) == 0);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700430 }
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700431 ASSERT_NE(test_level_one(1, 2, 3, 4, nullptr, nullptr), 0);
Christopher Ferrise2960912014-03-07 19:42:19 -0800432 _exit(1);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700433 }
434
435 // Check to see that all of the threads are running before unwinding.
436 std::vector<pid_t> threads;
437 uint64_t start = NanoTime();
438 do {
439 usleep(US_PER_MSEC);
440 threads.clear();
441 GetThreads(pid, &threads);
442 } while ((threads.size() != NUM_PTRACE_THREADS + 1) &&
443 ((NanoTime() - start) <= 5 * NS_PER_SEC));
444 ASSERT_EQ(threads.size(), static_cast<size_t>(NUM_PTRACE_THREADS + 1));
445
446 ASSERT_TRUE(ptrace(PTRACE_ATTACH, pid, 0, 0) == 0);
447 WaitForStop(pid);
448 for (std::vector<int>::const_iterator it = threads.begin(); it != threads.end(); ++it) {
449 // Skip the current forked process, we only care about the threads.
450 if (pid == *it) {
451 continue;
452 }
Christopher Ferrisdf290612014-01-22 19:21:07 -0800453 VerifyProcTest(pid, *it, false, ReadyLevelBacktrace, VerifyLevelDump);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700454 }
455 ASSERT_TRUE(ptrace(PTRACE_DETACH, pid, 0, 0) == 0);
456
457 kill(pid, SIGKILL);
458 int status;
459 ASSERT_EQ(waitpid(pid, &status, 0), pid);
460}
461
462void VerifyLevelThread(void*) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700463 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), gettid()));
464 ASSERT_TRUE(backtrace.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800465 ASSERT_TRUE(backtrace->Unwind(0));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700466
Christopher Ferris20303f82014-01-10 16:33:16 -0800467 VerifyLevelDump(backtrace.get());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700468}
469
470TEST(libbacktrace, thread_current_level) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700471 ASSERT_NE(test_level_one(1, 2, 3, 4, VerifyLevelThread, nullptr), 0);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700472}
473
474void VerifyMaxThread(void*) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700475 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), gettid()));
476 ASSERT_TRUE(backtrace.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800477 ASSERT_TRUE(backtrace->Unwind(0));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700478
Christopher Ferris20303f82014-01-10 16:33:16 -0800479 VerifyMaxDump(backtrace.get());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700480}
481
482TEST(libbacktrace, thread_current_max) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700483 ASSERT_NE(test_recursive_call(MAX_BACKTRACE_FRAMES+10, VerifyMaxThread, nullptr), 0);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700484}
485
486void* ThreadLevelRun(void* data) {
487 thread_t* thread = reinterpret_cast<thread_t*>(data);
488
489 thread->tid = gettid();
490 EXPECT_NE(test_level_one(1, 2, 3, 4, ThreadSetState, data), 0);
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700491 return nullptr;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700492}
493
494TEST(libbacktrace, thread_level_trace) {
495 pthread_attr_t attr;
496 pthread_attr_init(&attr);
497 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
498
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700499 thread_t thread_data = { 0, 0, 0, nullptr };
Christopher Ferris17e91d42013-10-21 13:30:52 -0700500 pthread_t thread;
501 ASSERT_TRUE(pthread_create(&thread, &attr, ThreadLevelRun, &thread_data) == 0);
502
503 // Wait up to 2 seconds for the tid to be set.
504 ASSERT_TRUE(WaitForNonZero(&thread_data.state, 2));
505
Christopher Ferrisaa63d9f2014-04-29 09:35:30 -0700506 // Make sure that the thread signal used is not visible when compiled for
507 // the target.
508#if !defined(__GLIBC__)
509 ASSERT_LT(THREAD_SIGNAL, SIGRTMIN);
510#endif
511
Christopher Ferris17e91d42013-10-21 13:30:52 -0700512 // Save the current signal action and make sure it is restored afterwards.
513 struct sigaction cur_action;
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700514 ASSERT_TRUE(sigaction(THREAD_SIGNAL, nullptr, &cur_action) == 0);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700515
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700516 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), thread_data.tid));
517 ASSERT_TRUE(backtrace.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800518 ASSERT_TRUE(backtrace->Unwind(0));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700519
Christopher Ferris20303f82014-01-10 16:33:16 -0800520 VerifyLevelDump(backtrace.get());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700521
522 // Tell the thread to exit its infinite loop.
523 android_atomic_acquire_store(0, &thread_data.state);
524
525 // Verify that the old action was restored.
526 struct sigaction new_action;
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700527 ASSERT_TRUE(sigaction(THREAD_SIGNAL, nullptr, &new_action) == 0);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700528 EXPECT_EQ(cur_action.sa_sigaction, new_action.sa_sigaction);
Christopher Ferris3cdbfdc2014-11-08 15:57:11 -0800529 // The SA_RESTORER flag gets set behind our back, so a direct comparison
530 // doesn't work unless we mask the value off. Mips doesn't have this
531 // flag, so skip this on that platform.
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700532#if defined(SA_RESTORER)
Christopher Ferris3cdbfdc2014-11-08 15:57:11 -0800533 cur_action.sa_flags &= ~SA_RESTORER;
534 new_action.sa_flags &= ~SA_RESTORER;
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700535#elif defined(__GLIBC__)
536 // Our host compiler doesn't appear to define this flag for some reason.
537 cur_action.sa_flags &= ~0x04000000;
538 new_action.sa_flags &= ~0x04000000;
Christopher Ferris3cdbfdc2014-11-08 15:57:11 -0800539#endif
Christopher Ferris17e91d42013-10-21 13:30:52 -0700540 EXPECT_EQ(cur_action.sa_flags, new_action.sa_flags);
541}
542
543TEST(libbacktrace, thread_ignore_frames) {
544 pthread_attr_t attr;
545 pthread_attr_init(&attr);
546 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
547
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700548 thread_t thread_data = { 0, 0, 0, nullptr };
Christopher Ferris17e91d42013-10-21 13:30:52 -0700549 pthread_t thread;
550 ASSERT_TRUE(pthread_create(&thread, &attr, ThreadLevelRun, &thread_data) == 0);
551
552 // Wait up to 2 seconds for the tid to be set.
553 ASSERT_TRUE(WaitForNonZero(&thread_data.state, 2));
554
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700555 std::unique_ptr<Backtrace> all(Backtrace::Create(getpid(), thread_data.tid));
556 ASSERT_TRUE(all.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800557 ASSERT_TRUE(all->Unwind(0));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700558
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700559 std::unique_ptr<Backtrace> ign1(Backtrace::Create(getpid(), thread_data.tid));
560 ASSERT_TRUE(ign1.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800561 ASSERT_TRUE(ign1->Unwind(1));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700562
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700563 std::unique_ptr<Backtrace> ign2(Backtrace::Create(getpid(), thread_data.tid));
564 ASSERT_TRUE(ign2.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800565 ASSERT_TRUE(ign2->Unwind(2));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700566
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700567 VerifyIgnoreFrames(all.get(), ign1.get(), ign2.get(), nullptr);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700568
569 // Tell the thread to exit its infinite loop.
570 android_atomic_acquire_store(0, &thread_data.state);
571}
572
573void* ThreadMaxRun(void* data) {
574 thread_t* thread = reinterpret_cast<thread_t*>(data);
575
576 thread->tid = gettid();
577 EXPECT_NE(test_recursive_call(MAX_BACKTRACE_FRAMES+10, ThreadSetState, data), 0);
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700578 return nullptr;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700579}
580
581TEST(libbacktrace, thread_max_trace) {
582 pthread_attr_t attr;
583 pthread_attr_init(&attr);
584 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
585
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700586 thread_t thread_data = { 0, 0, 0, nullptr };
Christopher Ferris17e91d42013-10-21 13:30:52 -0700587 pthread_t thread;
588 ASSERT_TRUE(pthread_create(&thread, &attr, ThreadMaxRun, &thread_data) == 0);
589
590 // Wait for the tid to be set.
591 ASSERT_TRUE(WaitForNonZero(&thread_data.state, 2));
592
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700593 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), thread_data.tid));
594 ASSERT_TRUE(backtrace.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800595 ASSERT_TRUE(backtrace->Unwind(0));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700596
Christopher Ferris20303f82014-01-10 16:33:16 -0800597 VerifyMaxDump(backtrace.get());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700598
599 // Tell the thread to exit its infinite loop.
600 android_atomic_acquire_store(0, &thread_data.state);
601}
602
603void* ThreadDump(void* data) {
604 dump_thread_t* dump = reinterpret_cast<dump_thread_t*>(data);
605 while (true) {
606 if (android_atomic_acquire_load(dump->now)) {
607 break;
608 }
609 }
610
Christopher Ferris17e91d42013-10-21 13:30:52 -0700611 // The status of the actual unwind will be checked elsewhere.
Christopher Ferris20303f82014-01-10 16:33:16 -0800612 dump->backtrace = Backtrace::Create(getpid(), dump->thread.tid);
613 dump->backtrace->Unwind(0);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700614
615 android_atomic_acquire_store(1, &dump->done);
616
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700617 return nullptr;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700618}
619
620TEST(libbacktrace, thread_multiple_dump) {
621 // Dump NUM_THREADS simultaneously.
622 std::vector<thread_t> runners(NUM_THREADS);
623 std::vector<dump_thread_t> dumpers(NUM_THREADS);
624
625 pthread_attr_t attr;
626 pthread_attr_init(&attr);
627 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
628 for (size_t i = 0; i < NUM_THREADS; i++) {
629 // Launch the runners, they will spin in hard loops doing nothing.
630 runners[i].tid = 0;
631 runners[i].state = 0;
632 ASSERT_TRUE(pthread_create(&runners[i].threadId, &attr, ThreadMaxRun, &runners[i]) == 0);
633 }
634
635 // Wait for tids to be set.
636 for (std::vector<thread_t>::iterator it = runners.begin(); it != runners.end(); ++it) {
Christopher Ferris3cdbfdc2014-11-08 15:57:11 -0800637 ASSERT_TRUE(WaitForNonZero(&it->state, 30));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700638 }
639
640 // Start all of the dumpers at once, they will spin until they are signalled
641 // to begin their dump run.
642 int32_t dump_now = 0;
643 for (size_t i = 0; i < NUM_THREADS; i++) {
644 dumpers[i].thread.tid = runners[i].tid;
645 dumpers[i].thread.state = 0;
646 dumpers[i].done = 0;
647 dumpers[i].now = &dump_now;
648
649 ASSERT_TRUE(pthread_create(&dumpers[i].thread.threadId, &attr, ThreadDump, &dumpers[i]) == 0);
650 }
651
652 // Start all of the dumpers going at once.
653 android_atomic_acquire_store(1, &dump_now);
654
655 for (size_t i = 0; i < NUM_THREADS; i++) {
Christopher Ferris3cdbfdc2014-11-08 15:57:11 -0800656 ASSERT_TRUE(WaitForNonZero(&dumpers[i].done, 30));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700657
658 // Tell the runner thread to exit its infinite loop.
659 android_atomic_acquire_store(0, &runners[i].state);
660
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700661 ASSERT_TRUE(dumpers[i].backtrace != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800662 VerifyMaxDump(dumpers[i].backtrace);
663
664 delete dumpers[i].backtrace;
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700665 dumpers[i].backtrace = nullptr;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700666 }
667}
668
Christopher Ferrisa2efd3a2014-05-06 15:23:59 -0700669TEST(libbacktrace, thread_multiple_dump_same_thread) {
670 pthread_attr_t attr;
671 pthread_attr_init(&attr);
672 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
673 thread_t runner;
674 runner.tid = 0;
675 runner.state = 0;
676 ASSERT_TRUE(pthread_create(&runner.threadId, &attr, ThreadMaxRun, &runner) == 0);
677
678 // Wait for tids to be set.
Christopher Ferris3cdbfdc2014-11-08 15:57:11 -0800679 ASSERT_TRUE(WaitForNonZero(&runner.state, 30));
Christopher Ferrisa2efd3a2014-05-06 15:23:59 -0700680
681 // Start all of the dumpers at once, they will spin until they are signalled
682 // to begin their dump run.
683 int32_t dump_now = 0;
684 // Dump the same thread NUM_THREADS simultaneously.
685 std::vector<dump_thread_t> dumpers(NUM_THREADS);
686 for (size_t i = 0; i < NUM_THREADS; i++) {
687 dumpers[i].thread.tid = runner.tid;
688 dumpers[i].thread.state = 0;
689 dumpers[i].done = 0;
690 dumpers[i].now = &dump_now;
691
692 ASSERT_TRUE(pthread_create(&dumpers[i].thread.threadId, &attr, ThreadDump, &dumpers[i]) == 0);
693 }
694
695 // Start all of the dumpers going at once.
696 android_atomic_acquire_store(1, &dump_now);
697
698 for (size_t i = 0; i < NUM_THREADS; i++) {
Christopher Ferris3cdbfdc2014-11-08 15:57:11 -0800699 ASSERT_TRUE(WaitForNonZero(&dumpers[i].done, 30));
Christopher Ferrisa2efd3a2014-05-06 15:23:59 -0700700
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700701 ASSERT_TRUE(dumpers[i].backtrace != nullptr);
Christopher Ferrisa2efd3a2014-05-06 15:23:59 -0700702 VerifyMaxDump(dumpers[i].backtrace);
703
704 delete dumpers[i].backtrace;
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700705 dumpers[i].backtrace = nullptr;
Christopher Ferrisa2efd3a2014-05-06 15:23:59 -0700706 }
707
708 // Tell the runner thread to exit its infinite loop.
709 android_atomic_acquire_store(0, &runner.state);
710}
711
Christopher Ferrisdf290612014-01-22 19:21:07 -0800712// This test is for UnwindMaps that should share the same map cursor when
713// multiple maps are created for the current process at the same time.
714TEST(libbacktrace, simultaneous_maps) {
715 BacktraceMap* map1 = BacktraceMap::Create(getpid());
716 BacktraceMap* map2 = BacktraceMap::Create(getpid());
717 BacktraceMap* map3 = BacktraceMap::Create(getpid());
718
719 Backtrace* back1 = Backtrace::Create(getpid(), BACKTRACE_CURRENT_THREAD, map1);
Christopher Ferris97e00bb2015-04-02 14:22:31 -0700720 ASSERT_TRUE(back1 != nullptr);
Christopher Ferrisdf290612014-01-22 19:21:07 -0800721 EXPECT_TRUE(back1->Unwind(0));
722 delete back1;
723 delete map1;
724
725 Backtrace* back2 = Backtrace::Create(getpid(), BACKTRACE_CURRENT_THREAD, map2);
Christopher Ferris97e00bb2015-04-02 14:22:31 -0700726 ASSERT_TRUE(back2 != nullptr);
Christopher Ferrisdf290612014-01-22 19:21:07 -0800727 EXPECT_TRUE(back2->Unwind(0));
728 delete back2;
729 delete map2;
730
731 Backtrace* back3 = Backtrace::Create(getpid(), BACKTRACE_CURRENT_THREAD, map3);
Christopher Ferris97e00bb2015-04-02 14:22:31 -0700732 ASSERT_TRUE(back3 != nullptr);
Christopher Ferrisdf290612014-01-22 19:21:07 -0800733 EXPECT_TRUE(back3->Unwind(0));
734 delete back3;
735 delete map3;
736}
737
Christopher Ferris12385e32015-02-06 13:22:01 -0800738TEST(libbacktrace, fillin_erases) {
739 BacktraceMap* back_map = BacktraceMap::Create(getpid());
740
741 backtrace_map_t map;
742
743 map.start = 1;
744 map.end = 3;
745 map.flags = 1;
746 map.name = "Initialized";
747 back_map->FillIn(0, &map);
748 delete back_map;
749
750 ASSERT_FALSE(BacktraceMap::IsValid(map));
751 ASSERT_EQ(static_cast<uintptr_t>(0), map.start);
752 ASSERT_EQ(static_cast<uintptr_t>(0), map.end);
753 ASSERT_EQ(0, map.flags);
754 ASSERT_EQ("", map.name);
755}
756
Christopher Ferris17e91d42013-10-21 13:30:52 -0700757TEST(libbacktrace, format_test) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700758 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), BACKTRACE_CURRENT_THREAD));
759 ASSERT_TRUE(backtrace.get() != nullptr);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700760
Christopher Ferris20303f82014-01-10 16:33:16 -0800761 backtrace_frame_data_t frame;
Christopher Ferris46756822014-01-14 20:16:30 -0800762 frame.num = 1;
763 frame.pc = 2;
764 frame.sp = 0;
765 frame.stack_size = 0;
Christopher Ferris46756822014-01-14 20:16:30 -0800766 frame.func_offset = 0;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700767
Christopher Ferris46756822014-01-14 20:16:30 -0800768 // Check no map set.
Christopher Ferris20303f82014-01-10 16:33:16 -0800769 frame.num = 1;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700770#if defined(__LP64__)
Christopher Ferris46756822014-01-14 20:16:30 -0800771 EXPECT_EQ("#01 pc 0000000000000002 <unknown>",
Christopher Ferris17e91d42013-10-21 13:30:52 -0700772#else
Christopher Ferris46756822014-01-14 20:16:30 -0800773 EXPECT_EQ("#01 pc 00000002 <unknown>",
Christopher Ferris17e91d42013-10-21 13:30:52 -0700774#endif
Christopher Ferris46756822014-01-14 20:16:30 -0800775 backtrace->FormatFrameData(&frame));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700776
Christopher Ferris46756822014-01-14 20:16:30 -0800777 // Check map name empty, but exists.
Christopher Ferrisda750a72015-11-30 13:36:08 -0800778 frame.pc = 0xb0020;
779 frame.map.start = 0xb0000;
780 frame.map.end = 0xbffff;
Christopher Ferris2106f4b2015-05-01 15:02:03 -0700781 frame.map.load_base = 0;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700782#if defined(__LP64__)
Christopher Ferrisda750a72015-11-30 13:36:08 -0800783 EXPECT_EQ("#01 pc 0000000000000020 <anonymous:00000000000b0000>",
Christopher Ferris17e91d42013-10-21 13:30:52 -0700784#else
Christopher Ferrisda750a72015-11-30 13:36:08 -0800785 EXPECT_EQ("#01 pc 00000020 <anonymous:000b0000>",
Christopher Ferris17e91d42013-10-21 13:30:52 -0700786#endif
Christopher Ferris46756822014-01-14 20:16:30 -0800787 backtrace->FormatFrameData(&frame));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700788
Christopher Ferrisda750a72015-11-30 13:36:08 -0800789 // Check map name begins with a [.
790 frame.pc = 0xc0020;
791 frame.map.start = 0xc0000;
792 frame.map.end = 0xcffff;
793 frame.map.load_base = 0;
794 frame.map.name = "[anon:thread signal stack]";
795#if defined(__LP64__)
796 EXPECT_EQ("#01 pc 0000000000000020 [anon:thread signal stack:00000000000c0000]",
797#else
798 EXPECT_EQ("#01 pc 00000020 [anon:thread signal stack:000c0000]",
799#endif
800 backtrace->FormatFrameData(&frame));
Christopher Ferris46756822014-01-14 20:16:30 -0800801
802 // Check relative pc is set and map name is set.
803 frame.pc = 0x12345679;
Christopher Ferris12385e32015-02-06 13:22:01 -0800804 frame.map.name = "MapFake";
805 frame.map.start = 1;
806 frame.map.end = 1;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700807#if defined(__LP64__)
Christopher Ferris46756822014-01-14 20:16:30 -0800808 EXPECT_EQ("#01 pc 0000000012345678 MapFake",
Christopher Ferris17e91d42013-10-21 13:30:52 -0700809#else
Christopher Ferris46756822014-01-14 20:16:30 -0800810 EXPECT_EQ("#01 pc 12345678 MapFake",
Christopher Ferris17e91d42013-10-21 13:30:52 -0700811#endif
Christopher Ferris46756822014-01-14 20:16:30 -0800812 backtrace->FormatFrameData(&frame));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700813
Christopher Ferris46756822014-01-14 20:16:30 -0800814 // Check func_name is set, but no func offset.
815 frame.func_name = "ProcFake";
816#if defined(__LP64__)
817 EXPECT_EQ("#01 pc 0000000012345678 MapFake (ProcFake)",
818#else
819 EXPECT_EQ("#01 pc 12345678 MapFake (ProcFake)",
820#endif
821 backtrace->FormatFrameData(&frame));
822
823 // Check func_name is set, and func offset is non-zero.
Christopher Ferris20303f82014-01-10 16:33:16 -0800824 frame.func_offset = 645;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700825#if defined(__LP64__)
Christopher Ferris46756822014-01-14 20:16:30 -0800826 EXPECT_EQ("#01 pc 0000000012345678 MapFake (ProcFake+645)",
Christopher Ferris17e91d42013-10-21 13:30:52 -0700827#else
Christopher Ferris46756822014-01-14 20:16:30 -0800828 EXPECT_EQ("#01 pc 12345678 MapFake (ProcFake+645)",
Christopher Ferris17e91d42013-10-21 13:30:52 -0700829#endif
Christopher Ferris46756822014-01-14 20:16:30 -0800830 backtrace->FormatFrameData(&frame));
Christopher Ferris2106f4b2015-05-01 15:02:03 -0700831
832 // Check func_name is set, func offset is non-zero, and load_base is non-zero.
833 frame.func_offset = 645;
834 frame.map.load_base = 100;
835#if defined(__LP64__)
836 EXPECT_EQ("#01 pc 00000000123456dc MapFake (ProcFake+645)",
837#else
838 EXPECT_EQ("#01 pc 123456dc MapFake (ProcFake+645)",
839#endif
840 backtrace->FormatFrameData(&frame));
Christopher Ferrise0ab2322015-08-20 11:16:54 -0700841
842 // Check a non-zero map offset.
843 frame.map.offset = 0x1000;
844#if defined(__LP64__)
845 EXPECT_EQ("#01 pc 00000000123456dc MapFake (offset 0x1000) (ProcFake+645)",
846#else
847 EXPECT_EQ("#01 pc 123456dc MapFake (offset 0x1000) (ProcFake+645)",
848#endif
849 backtrace->FormatFrameData(&frame));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700850}
Christopher Ferrise2960912014-03-07 19:42:19 -0800851
852struct map_test_t {
853 uintptr_t start;
854 uintptr_t end;
855};
856
857bool map_sort(map_test_t i, map_test_t j) {
858 return i.start < j.start;
859}
860
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700861void VerifyMap(pid_t pid) {
Christopher Ferrise2960912014-03-07 19:42:19 -0800862 char buffer[4096];
863 snprintf(buffer, sizeof(buffer), "/proc/%d/maps", pid);
864
865 FILE* map_file = fopen(buffer, "r");
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700866 ASSERT_TRUE(map_file != nullptr);
Christopher Ferrise2960912014-03-07 19:42:19 -0800867 std::vector<map_test_t> test_maps;
868 while (fgets(buffer, sizeof(buffer), map_file)) {
869 map_test_t map;
870 ASSERT_EQ(2, sscanf(buffer, "%" SCNxPTR "-%" SCNxPTR " ", &map.start, &map.end));
871 test_maps.push_back(map);
872 }
873 fclose(map_file);
874 std::sort(test_maps.begin(), test_maps.end(), map_sort);
875
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700876 std::unique_ptr<BacktraceMap> map(BacktraceMap::Create(pid));
Christopher Ferrise2960912014-03-07 19:42:19 -0800877
878 // Basic test that verifies that the map is in the expected order.
879 std::vector<map_test_t>::const_iterator test_it = test_maps.begin();
880 for (BacktraceMap::const_iterator it = map->begin(); it != map->end(); ++it) {
881 ASSERT_TRUE(test_it != test_maps.end());
882 ASSERT_EQ(test_it->start, it->start);
883 ASSERT_EQ(test_it->end, it->end);
884 ++test_it;
885 }
886 ASSERT_TRUE(test_it == test_maps.end());
887}
888
889TEST(libbacktrace, verify_map_remote) {
890 pid_t pid;
891
892 if ((pid = fork()) == 0) {
893 while (true) {
894 }
895 _exit(0);
896 }
897 ASSERT_LT(0, pid);
898
899 ASSERT_TRUE(ptrace(PTRACE_ATTACH, pid, 0, 0) == 0);
900
901 // Wait for the process to get to a stopping point.
902 WaitForStop(pid);
903
904 // The maps should match exactly since the forked process has been paused.
905 VerifyMap(pid);
906
907 ASSERT_TRUE(ptrace(PTRACE_DETACH, pid, 0, 0) == 0);
908
909 kill(pid, SIGKILL);
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700910 ASSERT_EQ(waitpid(pid, nullptr, 0), pid);
911}
912
Christopher Ferris944f4172015-05-06 16:36:34 -0700913void InitMemory(uint8_t* memory, size_t bytes) {
914 for (size_t i = 0; i < bytes; i++) {
915 memory[i] = i;
916 if (memory[i] == '\0') {
917 // Don't use '\0' in our data so we can verify that an overread doesn't
918 // occur by using a '\0' as the character after the read data.
919 memory[i] = 23;
920 }
921 }
922}
923
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700924void* ThreadReadTest(void* data) {
925 thread_t* thread_data = reinterpret_cast<thread_t*>(data);
926
927 thread_data->tid = gettid();
928
929 // Create two map pages.
930 // Mark the second page as not-readable.
931 size_t pagesize = static_cast<size_t>(sysconf(_SC_PAGE_SIZE));
932 uint8_t* memory;
933 if (posix_memalign(reinterpret_cast<void**>(&memory), pagesize, 2 * pagesize) != 0) {
934 return reinterpret_cast<void*>(-1);
935 }
936
937 if (mprotect(&memory[pagesize], pagesize, PROT_NONE) != 0) {
938 return reinterpret_cast<void*>(-1);
939 }
940
941 // Set up a simple pattern in memory.
Christopher Ferris944f4172015-05-06 16:36:34 -0700942 InitMemory(memory, pagesize);
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700943
944 thread_data->data = memory;
945
946 // Tell the caller it's okay to start reading memory.
947 android_atomic_acquire_store(1, &thread_data->state);
948
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700949 // Loop waiting for the caller to finish reading the memory.
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700950 while (thread_data->state) {
951 }
952
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700953 // Re-enable read-write on the page so that we don't crash if we try
954 // and access data on this page when freeing the memory.
955 if (mprotect(&memory[pagesize], pagesize, PROT_READ | PROT_WRITE) != 0) {
956 return reinterpret_cast<void*>(-1);
957 }
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700958 free(memory);
959
960 android_atomic_acquire_store(1, &thread_data->state);
961
962 return nullptr;
963}
964
965void RunReadTest(Backtrace* backtrace, uintptr_t read_addr) {
966 size_t pagesize = static_cast<size_t>(sysconf(_SC_PAGE_SIZE));
967
968 // Create a page of data to use to do quick compares.
969 uint8_t* expected = new uint8_t[pagesize];
Christopher Ferris944f4172015-05-06 16:36:34 -0700970 InitMemory(expected, pagesize);
971
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700972 uint8_t* data = new uint8_t[2*pagesize];
973 // Verify that we can only read one page worth of data.
974 size_t bytes_read = backtrace->Read(read_addr, data, 2 * pagesize);
975 ASSERT_EQ(pagesize, bytes_read);
976 ASSERT_TRUE(memcmp(data, expected, pagesize) == 0);
977
978 // Verify unaligned reads.
979 for (size_t i = 1; i < sizeof(word_t); i++) {
980 bytes_read = backtrace->Read(read_addr + i, data, 2 * sizeof(word_t));
981 ASSERT_EQ(2 * sizeof(word_t), bytes_read);
982 ASSERT_TRUE(memcmp(data, &expected[i], 2 * sizeof(word_t)) == 0)
983 << "Offset at " << i << " failed";
984 }
Christopher Ferris944f4172015-05-06 16:36:34 -0700985
986 // Verify small unaligned reads.
987 for (size_t i = 1; i < sizeof(word_t); i++) {
988 for (size_t j = 1; j < sizeof(word_t); j++) {
989 // Set one byte past what we expect to read, to guarantee we don't overread.
990 data[j] = '\0';
991 bytes_read = backtrace->Read(read_addr + i, data, j);
992 ASSERT_EQ(j, bytes_read);
993 ASSERT_TRUE(memcmp(data, &expected[i], j) == 0)
994 << "Offset at " << i << " length " << j << " miscompared";
995 ASSERT_EQ('\0', data[j])
996 << "Offset at " << i << " length " << j << " wrote too much data";
997 }
998 }
Pirama Arumuga Nainar837eff22015-07-09 10:50:04 -0700999 delete[] data;
1000 delete[] expected;
Christopher Ferris2b4a63f2015-03-17 14:42:03 -07001001}
1002
1003TEST(libbacktrace, thread_read) {
1004 pthread_attr_t attr;
1005 pthread_attr_init(&attr);
1006 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
1007 pthread_t thread;
1008 thread_t thread_data = { 0, 0, 0, nullptr };
1009 ASSERT_TRUE(pthread_create(&thread, &attr, ThreadReadTest, &thread_data) == 0);
1010
1011 ASSERT_TRUE(WaitForNonZero(&thread_data.state, 10));
1012
1013 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), thread_data.tid));
1014 ASSERT_TRUE(backtrace.get() != nullptr);
1015
1016 RunReadTest(backtrace.get(), reinterpret_cast<uintptr_t>(thread_data.data));
1017
1018 android_atomic_acquire_store(0, &thread_data.state);
1019
1020 ASSERT_TRUE(WaitForNonZero(&thread_data.state, 10));
1021}
1022
1023volatile uintptr_t g_ready = 0;
1024volatile uintptr_t g_addr = 0;
1025
1026void ForkedReadTest() {
1027 // Create two map pages.
1028 size_t pagesize = static_cast<size_t>(sysconf(_SC_PAGE_SIZE));
1029 uint8_t* memory;
1030 if (posix_memalign(reinterpret_cast<void**>(&memory), pagesize, 2 * pagesize) != 0) {
1031 perror("Failed to allocate memory\n");
1032 exit(1);
1033 }
1034
1035 // Mark the second page as not-readable.
1036 if (mprotect(&memory[pagesize], pagesize, PROT_NONE) != 0) {
1037 perror("Failed to mprotect memory\n");
1038 exit(1);
1039 }
1040
1041 // Set up a simple pattern in memory.
Christopher Ferris944f4172015-05-06 16:36:34 -07001042 InitMemory(memory, pagesize);
Christopher Ferris2b4a63f2015-03-17 14:42:03 -07001043
1044 g_addr = reinterpret_cast<uintptr_t>(memory);
1045 g_ready = 1;
1046
1047 while (1) {
1048 usleep(US_PER_MSEC);
1049 }
1050}
1051
1052TEST(libbacktrace, process_read) {
Christopher Ferris67aba682015-05-08 15:44:46 -07001053 g_ready = 0;
Christopher Ferris2b4a63f2015-03-17 14:42:03 -07001054 pid_t pid;
1055 if ((pid = fork()) == 0) {
1056 ForkedReadTest();
1057 exit(0);
1058 }
1059 ASSERT_NE(-1, pid);
1060
1061 bool test_executed = false;
1062 uint64_t start = NanoTime();
1063 while (1) {
1064 if (ptrace(PTRACE_ATTACH, pid, 0, 0) == 0) {
1065 WaitForStop(pid);
1066
1067 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(pid, pid));
Christopher Ferris97e00bb2015-04-02 14:22:31 -07001068 ASSERT_TRUE(backtrace.get() != nullptr);
Christopher Ferris2b4a63f2015-03-17 14:42:03 -07001069
1070 uintptr_t read_addr;
1071 size_t bytes_read = backtrace->Read(reinterpret_cast<uintptr_t>(&g_ready),
1072 reinterpret_cast<uint8_t*>(&read_addr),
1073 sizeof(uintptr_t));
1074 ASSERT_EQ(sizeof(uintptr_t), bytes_read);
1075 if (read_addr) {
1076 // The forked process is ready to be read.
1077 bytes_read = backtrace->Read(reinterpret_cast<uintptr_t>(&g_addr),
1078 reinterpret_cast<uint8_t*>(&read_addr),
1079 sizeof(uintptr_t));
1080 ASSERT_EQ(sizeof(uintptr_t), bytes_read);
1081
1082 RunReadTest(backtrace.get(), read_addr);
1083
1084 test_executed = true;
1085 break;
1086 }
1087 ASSERT_TRUE(ptrace(PTRACE_DETACH, pid, 0, 0) == 0);
1088 }
1089 if ((NanoTime() - start) > 5 * NS_PER_SEC) {
1090 break;
1091 }
1092 usleep(US_PER_MSEC);
1093 }
1094 kill(pid, SIGKILL);
1095 ASSERT_EQ(waitpid(pid, nullptr, 0), pid);
1096
1097 ASSERT_TRUE(test_executed);
Christopher Ferrise2960912014-03-07 19:42:19 -08001098}
1099
Christopher Ferris67aba682015-05-08 15:44:46 -07001100void VerifyFunctionsFound(const std::vector<std::string>& found_functions) {
1101 // We expect to find these functions in libbacktrace_test. If we don't
1102 // find them, that's a bug in the memory read handling code in libunwind.
1103 std::list<std::string> expected_functions;
1104 expected_functions.push_back("test_recursive_call");
1105 expected_functions.push_back("test_level_one");
1106 expected_functions.push_back("test_level_two");
1107 expected_functions.push_back("test_level_three");
1108 expected_functions.push_back("test_level_four");
1109 for (const auto& found_function : found_functions) {
1110 for (const auto& expected_function : expected_functions) {
1111 if (found_function == expected_function) {
1112 expected_functions.remove(found_function);
1113 break;
1114 }
1115 }
1116 }
1117 ASSERT_TRUE(expected_functions.empty()) << "Not all functions found in shared library.";
1118}
1119
1120const char* CopySharedLibrary() {
1121#if defined(__LP64__)
1122 const char* lib_name = "lib64";
1123#else
1124 const char* lib_name = "lib";
1125#endif
1126
1127#if defined(__BIONIC__)
1128 const char* tmp_so_name = "/data/local/tmp/libbacktrace_test.so";
1129 std::string cp_cmd = android::base::StringPrintf("cp /system/%s/libbacktrace_test.so %s",
1130 lib_name, tmp_so_name);
1131#else
1132 const char* tmp_so_name = "/tmp/libbacktrace_test.so";
1133 if (getenv("ANDROID_HOST_OUT") == NULL) {
1134 fprintf(stderr, "ANDROID_HOST_OUT not set, make sure you run lunch.");
1135 return nullptr;
1136 }
1137 std::string cp_cmd = android::base::StringPrintf("cp %s/%s/libbacktrace_test.so %s",
1138 getenv("ANDROID_HOST_OUT"), lib_name,
1139 tmp_so_name);
1140#endif
1141
1142 // Copy the shared so to a tempory directory.
1143 system(cp_cmd.c_str());
1144
1145 return tmp_so_name;
1146}
1147
1148TEST(libbacktrace, check_unreadable_elf_local) {
1149 const char* tmp_so_name = CopySharedLibrary();
1150 ASSERT_TRUE(tmp_so_name != nullptr);
1151
1152 struct stat buf;
1153 ASSERT_TRUE(stat(tmp_so_name, &buf) != -1);
1154 uintptr_t map_size = buf.st_size;
1155
1156 int fd = open(tmp_so_name, O_RDONLY);
1157 ASSERT_TRUE(fd != -1);
1158
1159 void* map = mmap(NULL, map_size, PROT_READ, MAP_PRIVATE, fd, 0);
1160 ASSERT_TRUE(map != MAP_FAILED);
1161 close(fd);
1162 ASSERT_TRUE(unlink(tmp_so_name) != -1);
1163
1164 std::vector<std::string> found_functions;
1165 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(BACKTRACE_CURRENT_PROCESS,
1166 BACKTRACE_CURRENT_THREAD));
1167 ASSERT_TRUE(backtrace.get() != nullptr);
1168
1169 // Needed before GetFunctionName will work.
1170 backtrace->Unwind(0);
1171
1172 // Loop through the entire map, and get every function we can find.
1173 map_size += reinterpret_cast<uintptr_t>(map);
1174 std::string last_func;
1175 for (uintptr_t read_addr = reinterpret_cast<uintptr_t>(map);
1176 read_addr < map_size; read_addr += 4) {
1177 uintptr_t offset;
1178 std::string func_name = backtrace->GetFunctionName(read_addr, &offset);
1179 if (!func_name.empty() && last_func != func_name) {
1180 found_functions.push_back(func_name);
1181 }
1182 last_func = func_name;
1183 }
1184
1185 ASSERT_TRUE(munmap(map, map_size - reinterpret_cast<uintptr_t>(map)) == 0);
1186
1187 VerifyFunctionsFound(found_functions);
1188}
1189
1190TEST(libbacktrace, check_unreadable_elf_remote) {
1191 const char* tmp_so_name = CopySharedLibrary();
1192 ASSERT_TRUE(tmp_so_name != nullptr);
1193
1194 g_ready = 0;
1195
1196 struct stat buf;
1197 ASSERT_TRUE(stat(tmp_so_name, &buf) != -1);
1198 uintptr_t map_size = buf.st_size;
1199
1200 pid_t pid;
1201 if ((pid = fork()) == 0) {
1202 int fd = open(tmp_so_name, O_RDONLY);
1203 if (fd == -1) {
1204 fprintf(stderr, "Failed to open file %s: %s\n", tmp_so_name, strerror(errno));
1205 unlink(tmp_so_name);
1206 exit(0);
1207 }
1208
1209 void* map = mmap(NULL, map_size, PROT_READ, MAP_PRIVATE, fd, 0);
1210 if (map == MAP_FAILED) {
1211 fprintf(stderr, "Failed to map in memory: %s\n", strerror(errno));
1212 unlink(tmp_so_name);
1213 exit(0);
1214 }
1215 close(fd);
1216 if (unlink(tmp_so_name) == -1) {
1217 fprintf(stderr, "Failed to unlink: %s\n", strerror(errno));
1218 exit(0);
1219 }
1220
1221 g_addr = reinterpret_cast<uintptr_t>(map);
1222 g_ready = 1;
1223 while (true) {
1224 usleep(US_PER_MSEC);
1225 }
1226 exit(0);
1227 }
1228 ASSERT_TRUE(pid > 0);
1229
1230 std::vector<std::string> found_functions;
1231 uint64_t start = NanoTime();
1232 while (true) {
1233 ASSERT_TRUE(ptrace(PTRACE_ATTACH, pid, 0, 0) == 0);
1234
1235 // Wait for the process to get to a stopping point.
1236 WaitForStop(pid);
1237
1238 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(pid, BACKTRACE_CURRENT_THREAD));
1239 ASSERT_TRUE(backtrace.get() != nullptr);
1240
1241 uintptr_t read_addr;
1242 ASSERT_EQ(sizeof(uintptr_t), backtrace->Read(reinterpret_cast<uintptr_t>(&g_ready), reinterpret_cast<uint8_t*>(&read_addr), sizeof(uintptr_t)));
1243 if (read_addr) {
1244 ASSERT_EQ(sizeof(uintptr_t), backtrace->Read(reinterpret_cast<uintptr_t>(&g_addr), reinterpret_cast<uint8_t*>(&read_addr), sizeof(uintptr_t)));
1245
1246 // Needed before GetFunctionName will work.
1247 backtrace->Unwind(0);
1248
1249 // Loop through the entire map, and get every function we can find.
1250 map_size += read_addr;
1251 std::string last_func;
1252 for (; read_addr < map_size; read_addr += 4) {
1253 uintptr_t offset;
1254 std::string func_name = backtrace->GetFunctionName(read_addr, &offset);
1255 if (!func_name.empty() && last_func != func_name) {
1256 found_functions.push_back(func_name);
1257 }
1258 last_func = func_name;
1259 }
1260 break;
1261 }
1262 ASSERT_TRUE(ptrace(PTRACE_DETACH, pid, 0, 0) == 0);
1263
1264 if ((NanoTime() - start) > 5 * NS_PER_SEC) {
1265 break;
1266 }
1267 usleep(US_PER_MSEC);
1268 }
1269
1270 kill(pid, SIGKILL);
1271 ASSERT_EQ(waitpid(pid, nullptr, 0), pid);
1272
1273 VerifyFunctionsFound(found_functions);
1274}
1275
1276bool FindFuncFrameInBacktrace(Backtrace* backtrace, uintptr_t test_func, size_t* frame_num) {
1277 backtrace_map_t map;
1278 backtrace->FillInMap(test_func, &map);
1279 if (!BacktraceMap::IsValid(map)) {
1280 return false;
1281 }
1282
1283 // Loop through the frames, and find the one that is in the map.
1284 *frame_num = 0;
1285 for (Backtrace::const_iterator it = backtrace->begin(); it != backtrace->end(); ++it) {
1286 if (BacktraceMap::IsValid(it->map) && map.start == it->map.start &&
1287 it->pc >= test_func) {
1288 *frame_num = it->num;
1289 return true;
1290 }
1291 }
1292 return false;
1293}
1294
1295void VerifyUnreadableElfFrame(Backtrace* backtrace, uintptr_t test_func, size_t frame_num) {
1296 ASSERT_LT(backtrace->NumFrames(), static_cast<size_t>(MAX_BACKTRACE_FRAMES))
1297 << DumpFrames(backtrace);
1298
1299 ASSERT_TRUE(frame_num != 0) << DumpFrames(backtrace);
1300 // Make sure that there is at least one more frame above the test func call.
1301 ASSERT_LT(frame_num, backtrace->NumFrames()) << DumpFrames(backtrace);
1302
1303 uintptr_t diff = backtrace->GetFrame(frame_num)->pc - test_func;
1304 ASSERT_LT(diff, 200U) << DumpFrames(backtrace);
1305}
1306
1307void VerifyUnreadableElfBacktrace(uintptr_t test_func) {
1308 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(BACKTRACE_CURRENT_PROCESS,
1309 BACKTRACE_CURRENT_THREAD));
1310 ASSERT_TRUE(backtrace.get() != nullptr);
1311 ASSERT_TRUE(backtrace->Unwind(0));
1312
1313 size_t frame_num;
1314 ASSERT_TRUE(FindFuncFrameInBacktrace(backtrace.get(), test_func, &frame_num));
1315
1316 VerifyUnreadableElfFrame(backtrace.get(), test_func, frame_num);
1317}
1318
1319typedef int (*test_func_t)(int, int, int, int, void (*)(uintptr_t), uintptr_t);
1320
1321TEST(libbacktrace, unwind_through_unreadable_elf_local) {
1322 const char* tmp_so_name = CopySharedLibrary();
1323 ASSERT_TRUE(tmp_so_name != nullptr);
1324 void* lib_handle = dlopen(tmp_so_name, RTLD_NOW);
1325 ASSERT_TRUE(lib_handle != nullptr);
1326 ASSERT_TRUE(unlink(tmp_so_name) != -1);
1327
1328 test_func_t test_func;
1329 test_func = reinterpret_cast<test_func_t>(dlsym(lib_handle, "test_level_one"));
1330 ASSERT_TRUE(test_func != nullptr);
1331
1332 ASSERT_NE(test_func(1, 2, 3, 4, VerifyUnreadableElfBacktrace,
1333 reinterpret_cast<uintptr_t>(test_func)), 0);
1334
1335 ASSERT_TRUE(dlclose(lib_handle) == 0);
1336}
1337
1338TEST(libbacktrace, unwind_through_unreadable_elf_remote) {
1339 const char* tmp_so_name = CopySharedLibrary();
1340 ASSERT_TRUE(tmp_so_name != nullptr);
1341 void* lib_handle = dlopen(tmp_so_name, RTLD_NOW);
1342 ASSERT_TRUE(lib_handle != nullptr);
1343 ASSERT_TRUE(unlink(tmp_so_name) != -1);
1344
1345 test_func_t test_func;
1346 test_func = reinterpret_cast<test_func_t>(dlsym(lib_handle, "test_level_one"));
1347 ASSERT_TRUE(test_func != nullptr);
1348
1349 pid_t pid;
1350 if ((pid = fork()) == 0) {
1351 test_func(1, 2, 3, 4, 0, 0);
1352 exit(0);
1353 }
1354 ASSERT_TRUE(pid > 0);
1355 ASSERT_TRUE(dlclose(lib_handle) == 0);
1356
1357 uint64_t start = NanoTime();
1358 bool done = false;
1359 while (!done) {
1360 ASSERT_TRUE(ptrace(PTRACE_ATTACH, pid, 0, 0) == 0);
1361
1362 // Wait for the process to get to a stopping point.
1363 WaitForStop(pid);
1364
1365 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(pid, BACKTRACE_CURRENT_THREAD));
1366 ASSERT_TRUE(backtrace.get() != nullptr);
1367 ASSERT_TRUE(backtrace->Unwind(0));
1368
1369 size_t frame_num;
1370 if (FindFuncFrameInBacktrace(backtrace.get(),
1371 reinterpret_cast<uintptr_t>(test_func), &frame_num)) {
1372
1373 VerifyUnreadableElfFrame(backtrace.get(), reinterpret_cast<uintptr_t>(test_func), frame_num);
1374 done = true;
1375 }
1376
1377 ASSERT_TRUE(ptrace(PTRACE_DETACH, pid, 0, 0) == 0);
1378
1379 if ((NanoTime() - start) > 5 * NS_PER_SEC) {
1380 break;
1381 }
1382 usleep(US_PER_MSEC);
1383 }
1384
1385 kill(pid, SIGKILL);
1386 ASSERT_EQ(waitpid(pid, nullptr, 0), pid);
1387
1388 ASSERT_TRUE(done) << "Test function never found in unwind.";
1389}
1390
Christopher Ferrise2960912014-03-07 19:42:19 -08001391#if defined(ENABLE_PSS_TESTS)
1392#include "GetPss.h"
1393
1394#define MAX_LEAK_BYTES 32*1024UL
1395
Christopher Ferris2b4a63f2015-03-17 14:42:03 -07001396void CheckForLeak(pid_t pid, pid_t tid) {
Christopher Ferrise2960912014-03-07 19:42:19 -08001397 // Do a few runs to get the PSS stable.
1398 for (size_t i = 0; i < 100; i++) {
1399 Backtrace* backtrace = Backtrace::Create(pid, tid);
Christopher Ferris2b4a63f2015-03-17 14:42:03 -07001400 ASSERT_TRUE(backtrace != nullptr);
Christopher Ferrise2960912014-03-07 19:42:19 -08001401 ASSERT_TRUE(backtrace->Unwind(0));
1402 delete backtrace;
1403 }
1404 size_t stable_pss = GetPssBytes();
Christopher Ferris2c43cff2015-03-26 19:18:36 -07001405 ASSERT_TRUE(stable_pss != 0);
Christopher Ferrise2960912014-03-07 19:42:19 -08001406
1407 // Loop enough that even a small leak should be detectable.
1408 for (size_t i = 0; i < 4096; i++) {
1409 Backtrace* backtrace = Backtrace::Create(pid, tid);
Christopher Ferris2b4a63f2015-03-17 14:42:03 -07001410 ASSERT_TRUE(backtrace != nullptr);
Christopher Ferrise2960912014-03-07 19:42:19 -08001411 ASSERT_TRUE(backtrace->Unwind(0));
1412 delete backtrace;
1413 }
1414 size_t new_pss = GetPssBytes();
Christopher Ferris2c43cff2015-03-26 19:18:36 -07001415 ASSERT_TRUE(new_pss != 0);
Christopher Ferrise2960912014-03-07 19:42:19 -08001416 size_t abs_diff = (new_pss > stable_pss) ? new_pss - stable_pss : stable_pss - new_pss;
1417 // As long as the new pss is within a certain amount, consider everything okay.
1418 ASSERT_LE(abs_diff, MAX_LEAK_BYTES);
1419}
1420
1421TEST(libbacktrace, check_for_leak_local) {
1422 CheckForLeak(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD);
1423}
1424
1425TEST(libbacktrace, check_for_leak_local_thread) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -07001426 thread_t thread_data = { 0, 0, 0, nullptr };
Christopher Ferrise2960912014-03-07 19:42:19 -08001427 pthread_t thread;
Christopher Ferris2b4a63f2015-03-17 14:42:03 -07001428 ASSERT_TRUE(pthread_create(&thread, nullptr, ThreadLevelRun, &thread_data) == 0);
Christopher Ferrise2960912014-03-07 19:42:19 -08001429
1430 // Wait up to 2 seconds for the tid to be set.
1431 ASSERT_TRUE(WaitForNonZero(&thread_data.state, 2));
1432
1433 CheckForLeak(BACKTRACE_CURRENT_PROCESS, thread_data.tid);
1434
1435 // Tell the thread to exit its infinite loop.
1436 android_atomic_acquire_store(0, &thread_data.state);
1437
Christopher Ferris2b4a63f2015-03-17 14:42:03 -07001438 ASSERT_TRUE(pthread_join(thread, nullptr) == 0);
Christopher Ferrise2960912014-03-07 19:42:19 -08001439}
1440
1441TEST(libbacktrace, check_for_leak_remote) {
1442 pid_t pid;
1443
1444 if ((pid = fork()) == 0) {
1445 while (true) {
1446 }
1447 _exit(0);
1448 }
1449 ASSERT_LT(0, pid);
1450
1451 ASSERT_TRUE(ptrace(PTRACE_ATTACH, pid, 0, 0) == 0);
1452
1453 // Wait for the process to get to a stopping point.
1454 WaitForStop(pid);
1455
1456 CheckForLeak(pid, BACKTRACE_CURRENT_THREAD);
1457
1458 ASSERT_TRUE(ptrace(PTRACE_DETACH, pid, 0, 0) == 0);
1459
1460 kill(pid, SIGKILL);
Christopher Ferris2b4a63f2015-03-17 14:42:03 -07001461 ASSERT_EQ(waitpid(pid, nullptr, 0), pid);
Christopher Ferrise2960912014-03-07 19:42:19 -08001462}
1463#endif
Christopher Ferris67aba682015-05-08 15:44:46 -07001464