blob: 24e48cdb471fed470f6f9a26ab876e0eb7f65d19 [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 Ferrisf5e568e2017-03-22 13:18:31 -070045#include <android-base/macros.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080046#include <android-base/stringprintf.h>
Christopher Ferris82f3bbd2017-03-14 15:22:26 -070047#include <android-base/unique_fd.h>
Dan Albert23f750b2015-04-30 12:52:21 -070048#include <cutils/atomic.h>
49#include <cutils/threads.h>
50
51#include <gtest/gtest.h>
52
53// For the THREAD_SIGNAL definition.
54#include "BacktraceCurrent.h"
Christopher Ferris17e91d42013-10-21 13:30:52 -070055#include "thread_utils.h"
56
57// Number of microseconds per milliseconds.
58#define US_PER_MSEC 1000
59
60// Number of nanoseconds in a second.
61#define NS_PER_SEC 1000000000ULL
62
63// Number of simultaneous dumping operations to perform.
Christopher Ferris3cdbfdc2014-11-08 15:57:11 -080064#define NUM_THREADS 40
Christopher Ferris17e91d42013-10-21 13:30:52 -070065
66// Number of simultaneous threads running in our forked process.
67#define NUM_PTRACE_THREADS 5
68
Christopher Ferris46756822014-01-14 20:16:30 -080069struct thread_t {
Christopher Ferris17e91d42013-10-21 13:30:52 -070070 pid_t tid;
71 int32_t state;
72 pthread_t threadId;
Christopher Ferris2b4a63f2015-03-17 14:42:03 -070073 void* data;
Christopher Ferris46756822014-01-14 20:16:30 -080074};
Christopher Ferris17e91d42013-10-21 13:30:52 -070075
Christopher Ferris46756822014-01-14 20:16:30 -080076struct dump_thread_t {
Christopher Ferris17e91d42013-10-21 13:30:52 -070077 thread_t thread;
Christopher Ferris20303f82014-01-10 16:33:16 -080078 Backtrace* backtrace;
Christopher Ferris17e91d42013-10-21 13:30:52 -070079 int32_t* now;
80 int32_t done;
Christopher Ferris46756822014-01-14 20:16:30 -080081};
Christopher Ferris17e91d42013-10-21 13:30:52 -070082
83extern "C" {
84// Prototypes for functions in the test library.
85int test_level_one(int, int, int, int, void (*)(void*), void*);
86
87int test_recursive_call(int, void (*)(void*), void*);
88}
89
Christopher Ferris82f3bbd2017-03-14 15:22:26 -070090static uint64_t NanoTime() {
Christopher Ferris17e91d42013-10-21 13:30:52 -070091 struct timespec t = { 0, 0 };
92 clock_gettime(CLOCK_MONOTONIC, &t);
93 return static_cast<uint64_t>(t.tv_sec * NS_PER_SEC + t.tv_nsec);
94}
95
Christopher Ferris82f3bbd2017-03-14 15:22:26 -070096static std::string DumpFrames(Backtrace* backtrace) {
Christopher Ferris20303f82014-01-10 16:33:16 -080097 if (backtrace->NumFrames() == 0) {
Christopher Ferris97e00bb2015-04-02 14:22:31 -070098 return " No frames to dump.\n";
Christopher Ferris20303f82014-01-10 16:33:16 -080099 }
100
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700101 std::string frame;
Christopher Ferris20303f82014-01-10 16:33:16 -0800102 for (size_t i = 0; i < backtrace->NumFrames(); i++) {
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700103 frame += " " + backtrace->FormatFrameData(i) + '\n';
Christopher Ferris17e91d42013-10-21 13:30:52 -0700104 }
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700105 return frame;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700106}
107
Christopher Ferris82f3bbd2017-03-14 15:22:26 -0700108static void WaitForStop(pid_t pid) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700109 uint64_t start = NanoTime();
110
111 siginfo_t si;
112 while (ptrace(PTRACE_GETSIGINFO, pid, 0, &si) < 0 && (errno == EINTR || errno == ESRCH)) {
113 if ((NanoTime() - start) > NS_PER_SEC) {
114 printf("The process did not get to a stopping point in 1 second.\n");
115 break;
116 }
117 usleep(US_PER_MSEC);
118 }
119}
120
Christopher Ferris82f3bbd2017-03-14 15:22:26 -0700121static void CreateRemoteProcess(pid_t* pid) {
122 if ((*pid = fork()) == 0) {
123 while (true)
124 ;
125 _exit(0);
126 }
127 ASSERT_NE(-1, *pid);
128
129 ASSERT_TRUE(ptrace(PTRACE_ATTACH, *pid, 0, 0) == 0);
130
131 // Wait for the process to get to a stopping point.
132 WaitForStop(*pid);
133}
134
135static void FinishRemoteProcess(pid_t pid) {
136 ASSERT_TRUE(ptrace(PTRACE_DETACH, pid, 0, 0) == 0);
137
138 kill(pid, SIGKILL);
139 ASSERT_EQ(waitpid(pid, nullptr, 0), pid);
140}
141
142static bool ReadyLevelBacktrace(Backtrace* backtrace) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700143 // See if test_level_four is in the backtrace.
144 bool found = false;
Christopher Ferris46756822014-01-14 20:16:30 -0800145 for (Backtrace::const_iterator it = backtrace->begin(); it != backtrace->end(); ++it) {
146 if (it->func_name == "test_level_four") {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700147 found = true;
148 break;
149 }
150 }
151
152 return found;
153}
154
Christopher Ferris82f3bbd2017-03-14 15:22:26 -0700155static void VerifyLevelDump(Backtrace* backtrace) {
Christopher Ferris97e00bb2015-04-02 14:22:31 -0700156 ASSERT_GT(backtrace->NumFrames(), static_cast<size_t>(0))
157 << DumpFrames(backtrace);
158 ASSERT_LT(backtrace->NumFrames(), static_cast<size_t>(MAX_BACKTRACE_FRAMES))
159 << DumpFrames(backtrace);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700160
161 // Look through the frames starting at the highest to find the
162 // frame we want.
163 size_t frame_num = 0;
Christopher Ferris20303f82014-01-10 16:33:16 -0800164 for (size_t i = backtrace->NumFrames()-1; i > 2; i--) {
Christopher Ferris46756822014-01-14 20:16:30 -0800165 if (backtrace->GetFrame(i)->func_name == "test_level_one") {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700166 frame_num = i;
167 break;
168 }
169 }
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700170 ASSERT_LT(static_cast<size_t>(0), frame_num) << DumpFrames(backtrace);
171 ASSERT_LE(static_cast<size_t>(3), frame_num) << DumpFrames(backtrace);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700172
Christopher Ferris97e00bb2015-04-02 14:22:31 -0700173 ASSERT_EQ(backtrace->GetFrame(frame_num)->func_name, "test_level_one")
174 << DumpFrames(backtrace);
175 ASSERT_EQ(backtrace->GetFrame(frame_num-1)->func_name, "test_level_two")
176 << DumpFrames(backtrace);
177 ASSERT_EQ(backtrace->GetFrame(frame_num-2)->func_name, "test_level_three")
178 << DumpFrames(backtrace);
179 ASSERT_EQ(backtrace->GetFrame(frame_num-3)->func_name, "test_level_four")
180 << DumpFrames(backtrace);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700181}
182
Christopher Ferris82f3bbd2017-03-14 15:22:26 -0700183static void VerifyLevelBacktrace(void*) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700184 std::unique_ptr<Backtrace> backtrace(
Christopher Ferris20303f82014-01-10 16:33:16 -0800185 Backtrace::Create(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD));
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700186 ASSERT_TRUE(backtrace.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800187 ASSERT_TRUE(backtrace->Unwind(0));
Christopher Ferris206a3b92016-03-09 14:35:54 -0800188 ASSERT_EQ(BACKTRACE_UNWIND_NO_ERROR, backtrace->GetError());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700189
Christopher Ferris20303f82014-01-10 16:33:16 -0800190 VerifyLevelDump(backtrace.get());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700191}
192
Christopher Ferris82f3bbd2017-03-14 15:22:26 -0700193static bool ReadyMaxBacktrace(Backtrace* backtrace) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800194 return (backtrace->NumFrames() == MAX_BACKTRACE_FRAMES);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700195}
196
Christopher Ferris82f3bbd2017-03-14 15:22:26 -0700197static void VerifyMaxDump(Backtrace* backtrace) {
Christopher Ferris97e00bb2015-04-02 14:22:31 -0700198 ASSERT_EQ(backtrace->NumFrames(), static_cast<size_t>(MAX_BACKTRACE_FRAMES))
199 << DumpFrames(backtrace);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700200 // Verify that the last frame is our recursive call.
Christopher Ferris97e00bb2015-04-02 14:22:31 -0700201 ASSERT_EQ(backtrace->GetFrame(MAX_BACKTRACE_FRAMES-1)->func_name, "test_recursive_call")
202 << DumpFrames(backtrace);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700203}
204
Christopher Ferris82f3bbd2017-03-14 15:22:26 -0700205static void VerifyMaxBacktrace(void*) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700206 std::unique_ptr<Backtrace> backtrace(
Christopher Ferris20303f82014-01-10 16:33:16 -0800207 Backtrace::Create(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD));
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700208 ASSERT_TRUE(backtrace.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800209 ASSERT_TRUE(backtrace->Unwind(0));
Christopher Ferris206a3b92016-03-09 14:35:54 -0800210 ASSERT_EQ(BACKTRACE_UNWIND_NO_ERROR, backtrace->GetError());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700211
Christopher Ferris20303f82014-01-10 16:33:16 -0800212 VerifyMaxDump(backtrace.get());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700213}
214
Christopher Ferris82f3bbd2017-03-14 15:22:26 -0700215static void ThreadSetState(void* data) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700216 thread_t* thread = reinterpret_cast<thread_t*>(data);
217 android_atomic_acquire_store(1, &thread->state);
218 volatile int i = 0;
219 while (thread->state) {
220 i++;
221 }
222}
223
Christopher Ferris82f3bbd2017-03-14 15:22:26 -0700224static bool WaitForNonZero(int32_t* value, uint64_t seconds) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700225 uint64_t start = NanoTime();
226 do {
227 if (android_atomic_acquire_load(value)) {
228 return true;
229 }
230 } while ((NanoTime() - start) < seconds * NS_PER_SEC);
231 return false;
232}
233
Christopher Ferrisca09ce92015-03-31 17:28:22 -0700234TEST(libbacktrace, local_no_unwind_frames) {
235 // Verify that a local unwind does not include any frames within
236 // libunwind or libbacktrace.
237 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), getpid()));
Christopher Ferris97e00bb2015-04-02 14:22:31 -0700238 ASSERT_TRUE(backtrace.get() != nullptr);
Christopher Ferrisca09ce92015-03-31 17:28:22 -0700239 ASSERT_TRUE(backtrace->Unwind(0));
Christopher Ferris206a3b92016-03-09 14:35:54 -0800240 ASSERT_EQ(BACKTRACE_UNWIND_NO_ERROR, backtrace->GetError());
Christopher Ferrisca09ce92015-03-31 17:28:22 -0700241
242 ASSERT_TRUE(backtrace->NumFrames() != 0);
243 for (const auto& frame : *backtrace ) {
244 if (BacktraceMap::IsValid(frame.map)) {
245 const std::string name = basename(frame.map.name.c_str());
246 ASSERT_TRUE(name != "libunwind.so" && name != "libbacktrace.so")
247 << DumpFrames(backtrace.get());
248 }
249 break;
250 }
251}
252
Christopher Ferris17e91d42013-10-21 13:30:52 -0700253TEST(libbacktrace, local_trace) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700254 ASSERT_NE(test_level_one(1, 2, 3, 4, VerifyLevelBacktrace, nullptr), 0);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700255}
256
Christopher Ferris82f3bbd2017-03-14 15:22:26 -0700257static void VerifyIgnoreFrames(Backtrace* bt_all, Backtrace* bt_ign1, Backtrace* bt_ign2,
258 const char* cur_proc) {
Christopher Ferris97e00bb2015-04-02 14:22:31 -0700259 EXPECT_EQ(bt_all->NumFrames(), bt_ign1->NumFrames() + 1)
260 << "All backtrace:\n" << DumpFrames(bt_all) << "Ignore 1 backtrace:\n" << DumpFrames(bt_ign1);
261 EXPECT_EQ(bt_all->NumFrames(), bt_ign2->NumFrames() + 2)
262 << "All backtrace:\n" << DumpFrames(bt_all) << "Ignore 2 backtrace:\n" << DumpFrames(bt_ign2);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700263
264 // Check all of the frames are the same > the current frame.
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700265 bool check = (cur_proc == nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800266 for (size_t i = 0; i < bt_ign2->NumFrames(); i++) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700267 if (check) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800268 EXPECT_EQ(bt_ign2->GetFrame(i)->pc, bt_ign1->GetFrame(i+1)->pc);
269 EXPECT_EQ(bt_ign2->GetFrame(i)->sp, bt_ign1->GetFrame(i+1)->sp);
270 EXPECT_EQ(bt_ign2->GetFrame(i)->stack_size, bt_ign1->GetFrame(i+1)->stack_size);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700271
Christopher Ferris20303f82014-01-10 16:33:16 -0800272 EXPECT_EQ(bt_ign2->GetFrame(i)->pc, bt_all->GetFrame(i+2)->pc);
273 EXPECT_EQ(bt_ign2->GetFrame(i)->sp, bt_all->GetFrame(i+2)->sp);
274 EXPECT_EQ(bt_ign2->GetFrame(i)->stack_size, bt_all->GetFrame(i+2)->stack_size);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700275 }
Christopher Ferris46756822014-01-14 20:16:30 -0800276 if (!check && bt_ign2->GetFrame(i)->func_name == cur_proc) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700277 check = true;
278 }
279 }
280}
281
Christopher Ferris82f3bbd2017-03-14 15:22:26 -0700282static void VerifyLevelIgnoreFrames(void*) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700283 std::unique_ptr<Backtrace> all(
Christopher Ferris20303f82014-01-10 16:33:16 -0800284 Backtrace::Create(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD));
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700285 ASSERT_TRUE(all.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800286 ASSERT_TRUE(all->Unwind(0));
Christopher Ferris206a3b92016-03-09 14:35:54 -0800287 ASSERT_EQ(BACKTRACE_UNWIND_NO_ERROR, all->GetError());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700288
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700289 std::unique_ptr<Backtrace> ign1(
Christopher Ferris20303f82014-01-10 16:33:16 -0800290 Backtrace::Create(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD));
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700291 ASSERT_TRUE(ign1.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800292 ASSERT_TRUE(ign1->Unwind(1));
Christopher Ferris206a3b92016-03-09 14:35:54 -0800293 ASSERT_EQ(BACKTRACE_UNWIND_NO_ERROR, ign1->GetError());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700294
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700295 std::unique_ptr<Backtrace> ign2(
Christopher Ferris20303f82014-01-10 16:33:16 -0800296 Backtrace::Create(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD));
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700297 ASSERT_TRUE(ign2.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800298 ASSERT_TRUE(ign2->Unwind(2));
Christopher Ferris206a3b92016-03-09 14:35:54 -0800299 ASSERT_EQ(BACKTRACE_UNWIND_NO_ERROR, ign2->GetError());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700300
Christopher Ferris20303f82014-01-10 16:33:16 -0800301 VerifyIgnoreFrames(all.get(), ign1.get(), ign2.get(), "VerifyLevelIgnoreFrames");
Christopher Ferris17e91d42013-10-21 13:30:52 -0700302}
303
304TEST(libbacktrace, local_trace_ignore_frames) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700305 ASSERT_NE(test_level_one(1, 2, 3, 4, VerifyLevelIgnoreFrames, nullptr), 0);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700306}
307
308TEST(libbacktrace, local_max_trace) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700309 ASSERT_NE(test_recursive_call(MAX_BACKTRACE_FRAMES+10, VerifyMaxBacktrace, nullptr), 0);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700310}
311
Christopher Ferris82f3bbd2017-03-14 15:22:26 -0700312static void VerifyProcTest(pid_t pid, pid_t tid, bool share_map, bool (*ReadyFunc)(Backtrace*),
313 void (*VerifyFunc)(Backtrace*)) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700314 pid_t ptrace_tid;
315 if (tid < 0) {
316 ptrace_tid = pid;
317 } else {
318 ptrace_tid = tid;
319 }
320 uint64_t start = NanoTime();
321 bool verified = false;
Christopher Ferris97e00bb2015-04-02 14:22:31 -0700322 std::string last_dump;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700323 do {
324 usleep(US_PER_MSEC);
325 if (ptrace(PTRACE_ATTACH, ptrace_tid, 0, 0) == 0) {
326 // Wait for the process to get to a stopping point.
327 WaitForStop(ptrace_tid);
328
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700329 std::unique_ptr<BacktraceMap> map;
Christopher Ferrisdf290612014-01-22 19:21:07 -0800330 if (share_map) {
331 map.reset(BacktraceMap::Create(pid));
332 }
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700333 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(pid, tid, map.get()));
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700334 ASSERT_TRUE(backtrace.get() != nullptr);
Christopher Ferris97e00bb2015-04-02 14:22:31 -0700335 ASSERT_TRUE(backtrace->Unwind(0));
Christopher Ferris206a3b92016-03-09 14:35:54 -0800336 ASSERT_EQ(BACKTRACE_UNWIND_NO_ERROR, backtrace->GetError());
Christopher Ferris20303f82014-01-10 16:33:16 -0800337 if (ReadyFunc(backtrace.get())) {
338 VerifyFunc(backtrace.get());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700339 verified = true;
Christopher Ferris97e00bb2015-04-02 14:22:31 -0700340 } else {
341 last_dump = DumpFrames(backtrace.get());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700342 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800343
Christopher Ferris17e91d42013-10-21 13:30:52 -0700344 ASSERT_TRUE(ptrace(PTRACE_DETACH, ptrace_tid, 0, 0) == 0);
345 }
346 // If 5 seconds have passed, then we are done.
347 } while (!verified && (NanoTime() - start) <= 5 * NS_PER_SEC);
Christopher Ferris97e00bb2015-04-02 14:22:31 -0700348 ASSERT_TRUE(verified) << "Last backtrace:\n" << last_dump;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700349}
350
351TEST(libbacktrace, ptrace_trace) {
352 pid_t pid;
353 if ((pid = fork()) == 0) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700354 ASSERT_NE(test_level_one(1, 2, 3, 4, nullptr, nullptr), 0);
Christopher Ferrise2960912014-03-07 19:42:19 -0800355 _exit(1);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700356 }
Christopher Ferrisdf290612014-01-22 19:21:07 -0800357 VerifyProcTest(pid, BACKTRACE_CURRENT_THREAD, false, ReadyLevelBacktrace, VerifyLevelDump);
358
359 kill(pid, SIGKILL);
360 int status;
361 ASSERT_EQ(waitpid(pid, &status, 0), pid);
362}
363
364TEST(libbacktrace, ptrace_trace_shared_map) {
365 pid_t pid;
366 if ((pid = fork()) == 0) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700367 ASSERT_NE(test_level_one(1, 2, 3, 4, nullptr, nullptr), 0);
Christopher Ferrise2960912014-03-07 19:42:19 -0800368 _exit(1);
Christopher Ferrisdf290612014-01-22 19:21:07 -0800369 }
370
371 VerifyProcTest(pid, BACKTRACE_CURRENT_THREAD, true, ReadyLevelBacktrace, VerifyLevelDump);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700372
373 kill(pid, SIGKILL);
374 int status;
375 ASSERT_EQ(waitpid(pid, &status, 0), pid);
376}
377
378TEST(libbacktrace, ptrace_max_trace) {
379 pid_t pid;
380 if ((pid = fork()) == 0) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700381 ASSERT_NE(test_recursive_call(MAX_BACKTRACE_FRAMES+10, nullptr, nullptr), 0);
Christopher Ferrise2960912014-03-07 19:42:19 -0800382 _exit(1);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700383 }
Christopher Ferrisdf290612014-01-22 19:21:07 -0800384 VerifyProcTest(pid, BACKTRACE_CURRENT_THREAD, false, ReadyMaxBacktrace, VerifyMaxDump);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700385
386 kill(pid, SIGKILL);
387 int status;
388 ASSERT_EQ(waitpid(pid, &status, 0), pid);
389}
390
Christopher Ferris82f3bbd2017-03-14 15:22:26 -0700391static void VerifyProcessIgnoreFrames(Backtrace* bt_all) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700392 std::unique_ptr<Backtrace> ign1(Backtrace::Create(bt_all->Pid(), BACKTRACE_CURRENT_THREAD));
393 ASSERT_TRUE(ign1.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800394 ASSERT_TRUE(ign1->Unwind(1));
Christopher Ferris206a3b92016-03-09 14:35:54 -0800395 ASSERT_EQ(BACKTRACE_UNWIND_NO_ERROR, ign1->GetError());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700396
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700397 std::unique_ptr<Backtrace> ign2(Backtrace::Create(bt_all->Pid(), BACKTRACE_CURRENT_THREAD));
398 ASSERT_TRUE(ign2.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800399 ASSERT_TRUE(ign2->Unwind(2));
Christopher Ferris206a3b92016-03-09 14:35:54 -0800400 ASSERT_EQ(BACKTRACE_UNWIND_NO_ERROR, ign2->GetError());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700401
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700402 VerifyIgnoreFrames(bt_all, ign1.get(), ign2.get(), nullptr);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700403}
404
405TEST(libbacktrace, ptrace_ignore_frames) {
406 pid_t pid;
407 if ((pid = fork()) == 0) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700408 ASSERT_NE(test_level_one(1, 2, 3, 4, nullptr, nullptr), 0);
Christopher Ferrise2960912014-03-07 19:42:19 -0800409 _exit(1);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700410 }
Christopher Ferrisdf290612014-01-22 19:21:07 -0800411 VerifyProcTest(pid, BACKTRACE_CURRENT_THREAD, false, ReadyLevelBacktrace, VerifyProcessIgnoreFrames);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700412
413 kill(pid, SIGKILL);
414 int status;
415 ASSERT_EQ(waitpid(pid, &status, 0), pid);
416}
417
418// Create a process with multiple threads and dump all of the threads.
Christopher Ferris82f3bbd2017-03-14 15:22:26 -0700419static void* PtraceThreadLevelRun(void*) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700420 EXPECT_NE(test_level_one(1, 2, 3, 4, nullptr, nullptr), 0);
421 return nullptr;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700422}
423
Christopher Ferris82f3bbd2017-03-14 15:22:26 -0700424static void GetThreads(pid_t pid, std::vector<pid_t>* threads) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700425 // Get the list of tasks.
426 char task_path[128];
427 snprintf(task_path, sizeof(task_path), "/proc/%d/task", pid);
428
James Hawkins588a2ca2016-02-18 14:52:46 -0800429 std::unique_ptr<DIR, decltype(&closedir)> tasks_dir(opendir(task_path), closedir);
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700430 ASSERT_TRUE(tasks_dir != nullptr);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700431 struct dirent* entry;
James Hawkins588a2ca2016-02-18 14:52:46 -0800432 while ((entry = readdir(tasks_dir.get())) != nullptr) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700433 char* end;
434 pid_t tid = strtoul(entry->d_name, &end, 10);
435 if (*end == '\0') {
436 threads->push_back(tid);
437 }
438 }
Christopher Ferris17e91d42013-10-21 13:30:52 -0700439}
440
441TEST(libbacktrace, ptrace_threads) {
442 pid_t pid;
443 if ((pid = fork()) == 0) {
444 for (size_t i = 0; i < NUM_PTRACE_THREADS; i++) {
445 pthread_attr_t attr;
446 pthread_attr_init(&attr);
447 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
448
449 pthread_t thread;
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700450 ASSERT_TRUE(pthread_create(&thread, &attr, PtraceThreadLevelRun, nullptr) == 0);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700451 }
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700452 ASSERT_NE(test_level_one(1, 2, 3, 4, nullptr, nullptr), 0);
Christopher Ferrise2960912014-03-07 19:42:19 -0800453 _exit(1);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700454 }
455
456 // Check to see that all of the threads are running before unwinding.
457 std::vector<pid_t> threads;
458 uint64_t start = NanoTime();
459 do {
460 usleep(US_PER_MSEC);
461 threads.clear();
462 GetThreads(pid, &threads);
463 } while ((threads.size() != NUM_PTRACE_THREADS + 1) &&
464 ((NanoTime() - start) <= 5 * NS_PER_SEC));
465 ASSERT_EQ(threads.size(), static_cast<size_t>(NUM_PTRACE_THREADS + 1));
466
467 ASSERT_TRUE(ptrace(PTRACE_ATTACH, pid, 0, 0) == 0);
468 WaitForStop(pid);
469 for (std::vector<int>::const_iterator it = threads.begin(); it != threads.end(); ++it) {
470 // Skip the current forked process, we only care about the threads.
471 if (pid == *it) {
472 continue;
473 }
Christopher Ferrisdf290612014-01-22 19:21:07 -0800474 VerifyProcTest(pid, *it, false, ReadyLevelBacktrace, VerifyLevelDump);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700475 }
Christopher Ferris17e91d42013-10-21 13:30:52 -0700476
Christopher Ferris82f3bbd2017-03-14 15:22:26 -0700477 FinishRemoteProcess(pid);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700478}
479
480void VerifyLevelThread(void*) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700481 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), gettid()));
482 ASSERT_TRUE(backtrace.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800483 ASSERT_TRUE(backtrace->Unwind(0));
Christopher Ferris206a3b92016-03-09 14:35:54 -0800484 ASSERT_EQ(BACKTRACE_UNWIND_NO_ERROR, backtrace->GetError());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700485
Christopher Ferris20303f82014-01-10 16:33:16 -0800486 VerifyLevelDump(backtrace.get());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700487}
488
489TEST(libbacktrace, thread_current_level) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700490 ASSERT_NE(test_level_one(1, 2, 3, 4, VerifyLevelThread, nullptr), 0);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700491}
492
Christopher Ferris82f3bbd2017-03-14 15:22:26 -0700493static void VerifyMaxThread(void*) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700494 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), gettid()));
495 ASSERT_TRUE(backtrace.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800496 ASSERT_TRUE(backtrace->Unwind(0));
Christopher Ferris206a3b92016-03-09 14:35:54 -0800497 ASSERT_EQ(BACKTRACE_UNWIND_NO_ERROR, backtrace->GetError());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700498
Christopher Ferris20303f82014-01-10 16:33:16 -0800499 VerifyMaxDump(backtrace.get());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700500}
501
502TEST(libbacktrace, thread_current_max) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700503 ASSERT_NE(test_recursive_call(MAX_BACKTRACE_FRAMES+10, VerifyMaxThread, nullptr), 0);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700504}
505
Christopher Ferris82f3bbd2017-03-14 15:22:26 -0700506static void* ThreadLevelRun(void* data) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700507 thread_t* thread = reinterpret_cast<thread_t*>(data);
508
509 thread->tid = gettid();
510 EXPECT_NE(test_level_one(1, 2, 3, 4, ThreadSetState, data), 0);
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700511 return nullptr;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700512}
513
514TEST(libbacktrace, thread_level_trace) {
515 pthread_attr_t attr;
516 pthread_attr_init(&attr);
517 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
518
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700519 thread_t thread_data = { 0, 0, 0, nullptr };
Christopher Ferris17e91d42013-10-21 13:30:52 -0700520 pthread_t thread;
521 ASSERT_TRUE(pthread_create(&thread, &attr, ThreadLevelRun, &thread_data) == 0);
522
523 // Wait up to 2 seconds for the tid to be set.
524 ASSERT_TRUE(WaitForNonZero(&thread_data.state, 2));
525
Christopher Ferrisaa63d9f2014-04-29 09:35:30 -0700526 // Make sure that the thread signal used is not visible when compiled for
527 // the target.
528#if !defined(__GLIBC__)
529 ASSERT_LT(THREAD_SIGNAL, SIGRTMIN);
530#endif
531
Christopher Ferris17e91d42013-10-21 13:30:52 -0700532 // Save the current signal action and make sure it is restored afterwards.
533 struct sigaction cur_action;
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700534 ASSERT_TRUE(sigaction(THREAD_SIGNAL, nullptr, &cur_action) == 0);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700535
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700536 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), thread_data.tid));
537 ASSERT_TRUE(backtrace.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800538 ASSERT_TRUE(backtrace->Unwind(0));
Christopher Ferris206a3b92016-03-09 14:35:54 -0800539 ASSERT_EQ(BACKTRACE_UNWIND_NO_ERROR, backtrace->GetError());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700540
Christopher Ferris20303f82014-01-10 16:33:16 -0800541 VerifyLevelDump(backtrace.get());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700542
543 // Tell the thread to exit its infinite loop.
544 android_atomic_acquire_store(0, &thread_data.state);
545
546 // Verify that the old action was restored.
547 struct sigaction new_action;
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700548 ASSERT_TRUE(sigaction(THREAD_SIGNAL, nullptr, &new_action) == 0);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700549 EXPECT_EQ(cur_action.sa_sigaction, new_action.sa_sigaction);
Christopher Ferris3cdbfdc2014-11-08 15:57:11 -0800550 // The SA_RESTORER flag gets set behind our back, so a direct comparison
551 // doesn't work unless we mask the value off. Mips doesn't have this
552 // flag, so skip this on that platform.
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700553#if defined(SA_RESTORER)
Christopher Ferris3cdbfdc2014-11-08 15:57:11 -0800554 cur_action.sa_flags &= ~SA_RESTORER;
555 new_action.sa_flags &= ~SA_RESTORER;
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700556#elif defined(__GLIBC__)
557 // Our host compiler doesn't appear to define this flag for some reason.
558 cur_action.sa_flags &= ~0x04000000;
559 new_action.sa_flags &= ~0x04000000;
Christopher Ferris3cdbfdc2014-11-08 15:57:11 -0800560#endif
Christopher Ferris17e91d42013-10-21 13:30:52 -0700561 EXPECT_EQ(cur_action.sa_flags, new_action.sa_flags);
562}
563
564TEST(libbacktrace, thread_ignore_frames) {
565 pthread_attr_t attr;
566 pthread_attr_init(&attr);
567 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
568
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700569 thread_t thread_data = { 0, 0, 0, nullptr };
Christopher Ferris17e91d42013-10-21 13:30:52 -0700570 pthread_t thread;
571 ASSERT_TRUE(pthread_create(&thread, &attr, ThreadLevelRun, &thread_data) == 0);
572
573 // Wait up to 2 seconds for the tid to be set.
574 ASSERT_TRUE(WaitForNonZero(&thread_data.state, 2));
575
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700576 std::unique_ptr<Backtrace> all(Backtrace::Create(getpid(), thread_data.tid));
577 ASSERT_TRUE(all.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800578 ASSERT_TRUE(all->Unwind(0));
Christopher Ferris206a3b92016-03-09 14:35:54 -0800579 ASSERT_EQ(BACKTRACE_UNWIND_NO_ERROR, all->GetError());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700580
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700581 std::unique_ptr<Backtrace> ign1(Backtrace::Create(getpid(), thread_data.tid));
582 ASSERT_TRUE(ign1.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800583 ASSERT_TRUE(ign1->Unwind(1));
Christopher Ferris206a3b92016-03-09 14:35:54 -0800584 ASSERT_EQ(BACKTRACE_UNWIND_NO_ERROR, ign1->GetError());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700585
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700586 std::unique_ptr<Backtrace> ign2(Backtrace::Create(getpid(), thread_data.tid));
587 ASSERT_TRUE(ign2.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800588 ASSERT_TRUE(ign2->Unwind(2));
Christopher Ferris206a3b92016-03-09 14:35:54 -0800589 ASSERT_EQ(BACKTRACE_UNWIND_NO_ERROR, ign2->GetError());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700590
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700591 VerifyIgnoreFrames(all.get(), ign1.get(), ign2.get(), nullptr);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700592
593 // Tell the thread to exit its infinite loop.
594 android_atomic_acquire_store(0, &thread_data.state);
595}
596
Christopher Ferris82f3bbd2017-03-14 15:22:26 -0700597static void* ThreadMaxRun(void* data) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700598 thread_t* thread = reinterpret_cast<thread_t*>(data);
599
600 thread->tid = gettid();
601 EXPECT_NE(test_recursive_call(MAX_BACKTRACE_FRAMES+10, ThreadSetState, data), 0);
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700602 return nullptr;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700603}
604
605TEST(libbacktrace, thread_max_trace) {
606 pthread_attr_t attr;
607 pthread_attr_init(&attr);
608 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
609
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700610 thread_t thread_data = { 0, 0, 0, nullptr };
Christopher Ferris17e91d42013-10-21 13:30:52 -0700611 pthread_t thread;
612 ASSERT_TRUE(pthread_create(&thread, &attr, ThreadMaxRun, &thread_data) == 0);
613
614 // Wait for the tid to be set.
615 ASSERT_TRUE(WaitForNonZero(&thread_data.state, 2));
616
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700617 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), thread_data.tid));
618 ASSERT_TRUE(backtrace.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800619 ASSERT_TRUE(backtrace->Unwind(0));
Christopher Ferris206a3b92016-03-09 14:35:54 -0800620 ASSERT_EQ(BACKTRACE_UNWIND_NO_ERROR, backtrace->GetError());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700621
Christopher Ferris20303f82014-01-10 16:33:16 -0800622 VerifyMaxDump(backtrace.get());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700623
624 // Tell the thread to exit its infinite loop.
625 android_atomic_acquire_store(0, &thread_data.state);
626}
627
Christopher Ferris82f3bbd2017-03-14 15:22:26 -0700628static void* ThreadDump(void* data) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700629 dump_thread_t* dump = reinterpret_cast<dump_thread_t*>(data);
630 while (true) {
631 if (android_atomic_acquire_load(dump->now)) {
632 break;
633 }
634 }
635
Christopher Ferris17e91d42013-10-21 13:30:52 -0700636 // The status of the actual unwind will be checked elsewhere.
Christopher Ferris20303f82014-01-10 16:33:16 -0800637 dump->backtrace = Backtrace::Create(getpid(), dump->thread.tid);
638 dump->backtrace->Unwind(0);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700639
640 android_atomic_acquire_store(1, &dump->done);
641
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700642 return nullptr;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700643}
644
645TEST(libbacktrace, thread_multiple_dump) {
646 // Dump NUM_THREADS simultaneously.
647 std::vector<thread_t> runners(NUM_THREADS);
648 std::vector<dump_thread_t> dumpers(NUM_THREADS);
649
650 pthread_attr_t attr;
651 pthread_attr_init(&attr);
652 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
653 for (size_t i = 0; i < NUM_THREADS; i++) {
654 // Launch the runners, they will spin in hard loops doing nothing.
655 runners[i].tid = 0;
656 runners[i].state = 0;
657 ASSERT_TRUE(pthread_create(&runners[i].threadId, &attr, ThreadMaxRun, &runners[i]) == 0);
658 }
659
660 // Wait for tids to be set.
661 for (std::vector<thread_t>::iterator it = runners.begin(); it != runners.end(); ++it) {
Christopher Ferris3cdbfdc2014-11-08 15:57:11 -0800662 ASSERT_TRUE(WaitForNonZero(&it->state, 30));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700663 }
664
665 // Start all of the dumpers at once, they will spin until they are signalled
666 // to begin their dump run.
667 int32_t dump_now = 0;
668 for (size_t i = 0; i < NUM_THREADS; i++) {
669 dumpers[i].thread.tid = runners[i].tid;
670 dumpers[i].thread.state = 0;
671 dumpers[i].done = 0;
672 dumpers[i].now = &dump_now;
673
674 ASSERT_TRUE(pthread_create(&dumpers[i].thread.threadId, &attr, ThreadDump, &dumpers[i]) == 0);
675 }
676
677 // Start all of the dumpers going at once.
678 android_atomic_acquire_store(1, &dump_now);
679
680 for (size_t i = 0; i < NUM_THREADS; i++) {
Christopher Ferris3cdbfdc2014-11-08 15:57:11 -0800681 ASSERT_TRUE(WaitForNonZero(&dumpers[i].done, 30));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700682
683 // Tell the runner thread to exit its infinite loop.
684 android_atomic_acquire_store(0, &runners[i].state);
685
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700686 ASSERT_TRUE(dumpers[i].backtrace != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800687 VerifyMaxDump(dumpers[i].backtrace);
688
689 delete dumpers[i].backtrace;
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700690 dumpers[i].backtrace = nullptr;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700691 }
692}
693
Christopher Ferrisa2efd3a2014-05-06 15:23:59 -0700694TEST(libbacktrace, thread_multiple_dump_same_thread) {
695 pthread_attr_t attr;
696 pthread_attr_init(&attr);
697 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
698 thread_t runner;
699 runner.tid = 0;
700 runner.state = 0;
701 ASSERT_TRUE(pthread_create(&runner.threadId, &attr, ThreadMaxRun, &runner) == 0);
702
703 // Wait for tids to be set.
Christopher Ferris3cdbfdc2014-11-08 15:57:11 -0800704 ASSERT_TRUE(WaitForNonZero(&runner.state, 30));
Christopher Ferrisa2efd3a2014-05-06 15:23:59 -0700705
706 // Start all of the dumpers at once, they will spin until they are signalled
707 // to begin their dump run.
708 int32_t dump_now = 0;
709 // Dump the same thread NUM_THREADS simultaneously.
710 std::vector<dump_thread_t> dumpers(NUM_THREADS);
711 for (size_t i = 0; i < NUM_THREADS; i++) {
712 dumpers[i].thread.tid = runner.tid;
713 dumpers[i].thread.state = 0;
714 dumpers[i].done = 0;
715 dumpers[i].now = &dump_now;
716
717 ASSERT_TRUE(pthread_create(&dumpers[i].thread.threadId, &attr, ThreadDump, &dumpers[i]) == 0);
718 }
719
720 // Start all of the dumpers going at once.
721 android_atomic_acquire_store(1, &dump_now);
722
723 for (size_t i = 0; i < NUM_THREADS; i++) {
Christopher Ferris3cdbfdc2014-11-08 15:57:11 -0800724 ASSERT_TRUE(WaitForNonZero(&dumpers[i].done, 30));
Christopher Ferrisa2efd3a2014-05-06 15:23:59 -0700725
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700726 ASSERT_TRUE(dumpers[i].backtrace != nullptr);
Christopher Ferrisa2efd3a2014-05-06 15:23:59 -0700727 VerifyMaxDump(dumpers[i].backtrace);
728
729 delete dumpers[i].backtrace;
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700730 dumpers[i].backtrace = nullptr;
Christopher Ferrisa2efd3a2014-05-06 15:23:59 -0700731 }
732
733 // Tell the runner thread to exit its infinite loop.
734 android_atomic_acquire_store(0, &runner.state);
735}
736
Christopher Ferrisdf290612014-01-22 19:21:07 -0800737// This test is for UnwindMaps that should share the same map cursor when
738// multiple maps are created for the current process at the same time.
739TEST(libbacktrace, simultaneous_maps) {
740 BacktraceMap* map1 = BacktraceMap::Create(getpid());
741 BacktraceMap* map2 = BacktraceMap::Create(getpid());
742 BacktraceMap* map3 = BacktraceMap::Create(getpid());
743
744 Backtrace* back1 = Backtrace::Create(getpid(), BACKTRACE_CURRENT_THREAD, map1);
Christopher Ferris97e00bb2015-04-02 14:22:31 -0700745 ASSERT_TRUE(back1 != nullptr);
Christopher Ferrisdf290612014-01-22 19:21:07 -0800746 EXPECT_TRUE(back1->Unwind(0));
Christopher Ferris206a3b92016-03-09 14:35:54 -0800747 ASSERT_EQ(BACKTRACE_UNWIND_NO_ERROR, back1->GetError());
Christopher Ferrisdf290612014-01-22 19:21:07 -0800748 delete back1;
749 delete map1;
750
751 Backtrace* back2 = Backtrace::Create(getpid(), BACKTRACE_CURRENT_THREAD, map2);
Christopher Ferris97e00bb2015-04-02 14:22:31 -0700752 ASSERT_TRUE(back2 != nullptr);
Christopher Ferrisdf290612014-01-22 19:21:07 -0800753 EXPECT_TRUE(back2->Unwind(0));
Christopher Ferris206a3b92016-03-09 14:35:54 -0800754 ASSERT_EQ(BACKTRACE_UNWIND_NO_ERROR, back2->GetError());
Christopher Ferrisdf290612014-01-22 19:21:07 -0800755 delete back2;
756 delete map2;
757
758 Backtrace* back3 = Backtrace::Create(getpid(), BACKTRACE_CURRENT_THREAD, map3);
Christopher Ferris97e00bb2015-04-02 14:22:31 -0700759 ASSERT_TRUE(back3 != nullptr);
Christopher Ferrisdf290612014-01-22 19:21:07 -0800760 EXPECT_TRUE(back3->Unwind(0));
Christopher Ferris206a3b92016-03-09 14:35:54 -0800761 ASSERT_EQ(BACKTRACE_UNWIND_NO_ERROR, back3->GetError());
Christopher Ferrisdf290612014-01-22 19:21:07 -0800762 delete back3;
763 delete map3;
764}
765
Christopher Ferris12385e32015-02-06 13:22:01 -0800766TEST(libbacktrace, fillin_erases) {
767 BacktraceMap* back_map = BacktraceMap::Create(getpid());
768
769 backtrace_map_t map;
770
771 map.start = 1;
772 map.end = 3;
773 map.flags = 1;
774 map.name = "Initialized";
775 back_map->FillIn(0, &map);
776 delete back_map;
777
778 ASSERT_FALSE(BacktraceMap::IsValid(map));
779 ASSERT_EQ(static_cast<uintptr_t>(0), map.start);
780 ASSERT_EQ(static_cast<uintptr_t>(0), map.end);
781 ASSERT_EQ(0, map.flags);
782 ASSERT_EQ("", map.name);
783}
784
Christopher Ferris17e91d42013-10-21 13:30:52 -0700785TEST(libbacktrace, format_test) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700786 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), BACKTRACE_CURRENT_THREAD));
787 ASSERT_TRUE(backtrace.get() != nullptr);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700788
Christopher Ferris20303f82014-01-10 16:33:16 -0800789 backtrace_frame_data_t frame;
Christopher Ferris46756822014-01-14 20:16:30 -0800790 frame.num = 1;
791 frame.pc = 2;
792 frame.sp = 0;
793 frame.stack_size = 0;
Christopher Ferris46756822014-01-14 20:16:30 -0800794 frame.func_offset = 0;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700795
Christopher Ferris46756822014-01-14 20:16:30 -0800796 // Check no map set.
Christopher Ferris20303f82014-01-10 16:33:16 -0800797 frame.num = 1;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700798#if defined(__LP64__)
Christopher Ferris46756822014-01-14 20:16:30 -0800799 EXPECT_EQ("#01 pc 0000000000000002 <unknown>",
Christopher Ferris17e91d42013-10-21 13:30:52 -0700800#else
Christopher Ferris46756822014-01-14 20:16:30 -0800801 EXPECT_EQ("#01 pc 00000002 <unknown>",
Christopher Ferris17e91d42013-10-21 13:30:52 -0700802#endif
Christopher Ferris46756822014-01-14 20:16:30 -0800803 backtrace->FormatFrameData(&frame));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700804
Christopher Ferris46756822014-01-14 20:16:30 -0800805 // Check map name empty, but exists.
Christopher Ferrisda750a72015-11-30 13:36:08 -0800806 frame.pc = 0xb0020;
807 frame.map.start = 0xb0000;
808 frame.map.end = 0xbffff;
Christopher Ferris2106f4b2015-05-01 15:02:03 -0700809 frame.map.load_base = 0;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700810#if defined(__LP64__)
Christopher Ferrisda750a72015-11-30 13:36:08 -0800811 EXPECT_EQ("#01 pc 0000000000000020 <anonymous:00000000000b0000>",
Christopher Ferris17e91d42013-10-21 13:30:52 -0700812#else
Christopher Ferrisda750a72015-11-30 13:36:08 -0800813 EXPECT_EQ("#01 pc 00000020 <anonymous:000b0000>",
Christopher Ferris17e91d42013-10-21 13:30:52 -0700814#endif
Christopher Ferris46756822014-01-14 20:16:30 -0800815 backtrace->FormatFrameData(&frame));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700816
Christopher Ferrisda750a72015-11-30 13:36:08 -0800817 // Check map name begins with a [.
818 frame.pc = 0xc0020;
819 frame.map.start = 0xc0000;
820 frame.map.end = 0xcffff;
821 frame.map.load_base = 0;
822 frame.map.name = "[anon:thread signal stack]";
823#if defined(__LP64__)
824 EXPECT_EQ("#01 pc 0000000000000020 [anon:thread signal stack:00000000000c0000]",
825#else
826 EXPECT_EQ("#01 pc 00000020 [anon:thread signal stack:000c0000]",
827#endif
828 backtrace->FormatFrameData(&frame));
Christopher Ferris46756822014-01-14 20:16:30 -0800829
830 // Check relative pc is set and map name is set.
831 frame.pc = 0x12345679;
Christopher Ferris12385e32015-02-06 13:22:01 -0800832 frame.map.name = "MapFake";
833 frame.map.start = 1;
834 frame.map.end = 1;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700835#if defined(__LP64__)
Christopher Ferris46756822014-01-14 20:16:30 -0800836 EXPECT_EQ("#01 pc 0000000012345678 MapFake",
Christopher Ferris17e91d42013-10-21 13:30:52 -0700837#else
Christopher Ferris46756822014-01-14 20:16:30 -0800838 EXPECT_EQ("#01 pc 12345678 MapFake",
Christopher Ferris17e91d42013-10-21 13:30:52 -0700839#endif
Christopher Ferris46756822014-01-14 20:16:30 -0800840 backtrace->FormatFrameData(&frame));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700841
Christopher Ferris46756822014-01-14 20:16:30 -0800842 // Check func_name is set, but no func offset.
843 frame.func_name = "ProcFake";
844#if defined(__LP64__)
845 EXPECT_EQ("#01 pc 0000000012345678 MapFake (ProcFake)",
846#else
847 EXPECT_EQ("#01 pc 12345678 MapFake (ProcFake)",
848#endif
849 backtrace->FormatFrameData(&frame));
850
851 // Check func_name is set, and func offset is non-zero.
Christopher Ferris20303f82014-01-10 16:33:16 -0800852 frame.func_offset = 645;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700853#if defined(__LP64__)
Christopher Ferris46756822014-01-14 20:16:30 -0800854 EXPECT_EQ("#01 pc 0000000012345678 MapFake (ProcFake+645)",
Christopher Ferris17e91d42013-10-21 13:30:52 -0700855#else
Christopher Ferris46756822014-01-14 20:16:30 -0800856 EXPECT_EQ("#01 pc 12345678 MapFake (ProcFake+645)",
Christopher Ferris17e91d42013-10-21 13:30:52 -0700857#endif
Christopher Ferris46756822014-01-14 20:16:30 -0800858 backtrace->FormatFrameData(&frame));
Christopher Ferris2106f4b2015-05-01 15:02:03 -0700859
860 // Check func_name is set, func offset is non-zero, and load_base is non-zero.
861 frame.func_offset = 645;
862 frame.map.load_base = 100;
863#if defined(__LP64__)
864 EXPECT_EQ("#01 pc 00000000123456dc MapFake (ProcFake+645)",
865#else
866 EXPECT_EQ("#01 pc 123456dc MapFake (ProcFake+645)",
867#endif
868 backtrace->FormatFrameData(&frame));
Christopher Ferrise0ab2322015-08-20 11:16:54 -0700869
870 // Check a non-zero map offset.
871 frame.map.offset = 0x1000;
872#if defined(__LP64__)
873 EXPECT_EQ("#01 pc 00000000123456dc MapFake (offset 0x1000) (ProcFake+645)",
874#else
875 EXPECT_EQ("#01 pc 123456dc MapFake (offset 0x1000) (ProcFake+645)",
876#endif
877 backtrace->FormatFrameData(&frame));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700878}
Christopher Ferrise2960912014-03-07 19:42:19 -0800879
880struct map_test_t {
881 uintptr_t start;
882 uintptr_t end;
883};
884
Christopher Ferris82f3bbd2017-03-14 15:22:26 -0700885static bool map_sort(map_test_t i, map_test_t j) { return i.start < j.start; }
Christopher Ferrise2960912014-03-07 19:42:19 -0800886
Christopher Ferris82f3bbd2017-03-14 15:22:26 -0700887static void VerifyMap(pid_t pid) {
Christopher Ferrise2960912014-03-07 19:42:19 -0800888 char buffer[4096];
889 snprintf(buffer, sizeof(buffer), "/proc/%d/maps", pid);
890
891 FILE* map_file = fopen(buffer, "r");
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700892 ASSERT_TRUE(map_file != nullptr);
Christopher Ferrise2960912014-03-07 19:42:19 -0800893 std::vector<map_test_t> test_maps;
894 while (fgets(buffer, sizeof(buffer), map_file)) {
895 map_test_t map;
896 ASSERT_EQ(2, sscanf(buffer, "%" SCNxPTR "-%" SCNxPTR " ", &map.start, &map.end));
897 test_maps.push_back(map);
898 }
899 fclose(map_file);
900 std::sort(test_maps.begin(), test_maps.end(), map_sort);
901
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700902 std::unique_ptr<BacktraceMap> map(BacktraceMap::Create(pid));
Christopher Ferrise2960912014-03-07 19:42:19 -0800903
904 // Basic test that verifies that the map is in the expected order.
Christopher Ferris3a140042016-06-15 15:49:50 -0700905 ScopedBacktraceMapIteratorLock lock(map.get());
Christopher Ferrise2960912014-03-07 19:42:19 -0800906 std::vector<map_test_t>::const_iterator test_it = test_maps.begin();
907 for (BacktraceMap::const_iterator it = map->begin(); it != map->end(); ++it) {
908 ASSERT_TRUE(test_it != test_maps.end());
909 ASSERT_EQ(test_it->start, it->start);
910 ASSERT_EQ(test_it->end, it->end);
911 ++test_it;
912 }
913 ASSERT_TRUE(test_it == test_maps.end());
914}
915
916TEST(libbacktrace, verify_map_remote) {
917 pid_t pid;
Christopher Ferris82f3bbd2017-03-14 15:22:26 -0700918 CreateRemoteProcess(&pid);
Christopher Ferrise2960912014-03-07 19:42:19 -0800919
920 // The maps should match exactly since the forked process has been paused.
921 VerifyMap(pid);
922
Christopher Ferris82f3bbd2017-03-14 15:22:26 -0700923 FinishRemoteProcess(pid);
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700924}
925
Christopher Ferris82f3bbd2017-03-14 15:22:26 -0700926static void InitMemory(uint8_t* memory, size_t bytes) {
Christopher Ferris944f4172015-05-06 16:36:34 -0700927 for (size_t i = 0; i < bytes; i++) {
928 memory[i] = i;
929 if (memory[i] == '\0') {
930 // Don't use '\0' in our data so we can verify that an overread doesn't
931 // occur by using a '\0' as the character after the read data.
932 memory[i] = 23;
933 }
934 }
935}
936
Christopher Ferris82f3bbd2017-03-14 15:22:26 -0700937static void* ThreadReadTest(void* data) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700938 thread_t* thread_data = reinterpret_cast<thread_t*>(data);
939
940 thread_data->tid = gettid();
941
942 // Create two map pages.
943 // Mark the second page as not-readable.
944 size_t pagesize = static_cast<size_t>(sysconf(_SC_PAGE_SIZE));
945 uint8_t* memory;
946 if (posix_memalign(reinterpret_cast<void**>(&memory), pagesize, 2 * pagesize) != 0) {
947 return reinterpret_cast<void*>(-1);
948 }
949
950 if (mprotect(&memory[pagesize], pagesize, PROT_NONE) != 0) {
951 return reinterpret_cast<void*>(-1);
952 }
953
954 // Set up a simple pattern in memory.
Christopher Ferris944f4172015-05-06 16:36:34 -0700955 InitMemory(memory, pagesize);
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700956
957 thread_data->data = memory;
958
959 // Tell the caller it's okay to start reading memory.
960 android_atomic_acquire_store(1, &thread_data->state);
961
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700962 // Loop waiting for the caller to finish reading the memory.
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700963 while (thread_data->state) {
964 }
965
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700966 // Re-enable read-write on the page so that we don't crash if we try
967 // and access data on this page when freeing the memory.
968 if (mprotect(&memory[pagesize], pagesize, PROT_READ | PROT_WRITE) != 0) {
969 return reinterpret_cast<void*>(-1);
970 }
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700971 free(memory);
972
973 android_atomic_acquire_store(1, &thread_data->state);
974
975 return nullptr;
976}
977
Christopher Ferris82f3bbd2017-03-14 15:22:26 -0700978static void RunReadTest(Backtrace* backtrace, uintptr_t read_addr) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700979 size_t pagesize = static_cast<size_t>(sysconf(_SC_PAGE_SIZE));
980
981 // Create a page of data to use to do quick compares.
982 uint8_t* expected = new uint8_t[pagesize];
Christopher Ferris944f4172015-05-06 16:36:34 -0700983 InitMemory(expected, pagesize);
984
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700985 uint8_t* data = new uint8_t[2*pagesize];
986 // Verify that we can only read one page worth of data.
987 size_t bytes_read = backtrace->Read(read_addr, data, 2 * pagesize);
988 ASSERT_EQ(pagesize, bytes_read);
989 ASSERT_TRUE(memcmp(data, expected, pagesize) == 0);
990
991 // Verify unaligned reads.
992 for (size_t i = 1; i < sizeof(word_t); i++) {
993 bytes_read = backtrace->Read(read_addr + i, data, 2 * sizeof(word_t));
994 ASSERT_EQ(2 * sizeof(word_t), bytes_read);
995 ASSERT_TRUE(memcmp(data, &expected[i], 2 * sizeof(word_t)) == 0)
996 << "Offset at " << i << " failed";
997 }
Christopher Ferris944f4172015-05-06 16:36:34 -0700998
999 // Verify small unaligned reads.
1000 for (size_t i = 1; i < sizeof(word_t); i++) {
1001 for (size_t j = 1; j < sizeof(word_t); j++) {
1002 // Set one byte past what we expect to read, to guarantee we don't overread.
1003 data[j] = '\0';
1004 bytes_read = backtrace->Read(read_addr + i, data, j);
1005 ASSERT_EQ(j, bytes_read);
1006 ASSERT_TRUE(memcmp(data, &expected[i], j) == 0)
1007 << "Offset at " << i << " length " << j << " miscompared";
1008 ASSERT_EQ('\0', data[j])
1009 << "Offset at " << i << " length " << j << " wrote too much data";
1010 }
1011 }
Pirama Arumuga Nainar837eff22015-07-09 10:50:04 -07001012 delete[] data;
1013 delete[] expected;
Christopher Ferris2b4a63f2015-03-17 14:42:03 -07001014}
1015
1016TEST(libbacktrace, thread_read) {
1017 pthread_attr_t attr;
1018 pthread_attr_init(&attr);
1019 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
1020 pthread_t thread;
1021 thread_t thread_data = { 0, 0, 0, nullptr };
1022 ASSERT_TRUE(pthread_create(&thread, &attr, ThreadReadTest, &thread_data) == 0);
1023
1024 ASSERT_TRUE(WaitForNonZero(&thread_data.state, 10));
1025
1026 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), thread_data.tid));
1027 ASSERT_TRUE(backtrace.get() != nullptr);
1028
1029 RunReadTest(backtrace.get(), reinterpret_cast<uintptr_t>(thread_data.data));
1030
1031 android_atomic_acquire_store(0, &thread_data.state);
1032
1033 ASSERT_TRUE(WaitForNonZero(&thread_data.state, 10));
1034}
1035
1036volatile uintptr_t g_ready = 0;
1037volatile uintptr_t g_addr = 0;
1038
Christopher Ferris82f3bbd2017-03-14 15:22:26 -07001039static void ForkedReadTest() {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -07001040 // Create two map pages.
1041 size_t pagesize = static_cast<size_t>(sysconf(_SC_PAGE_SIZE));
1042 uint8_t* memory;
1043 if (posix_memalign(reinterpret_cast<void**>(&memory), pagesize, 2 * pagesize) != 0) {
1044 perror("Failed to allocate memory\n");
1045 exit(1);
1046 }
1047
1048 // Mark the second page as not-readable.
1049 if (mprotect(&memory[pagesize], pagesize, PROT_NONE) != 0) {
1050 perror("Failed to mprotect memory\n");
1051 exit(1);
1052 }
1053
1054 // Set up a simple pattern in memory.
Christopher Ferris944f4172015-05-06 16:36:34 -07001055 InitMemory(memory, pagesize);
Christopher Ferris2b4a63f2015-03-17 14:42:03 -07001056
1057 g_addr = reinterpret_cast<uintptr_t>(memory);
1058 g_ready = 1;
1059
1060 while (1) {
1061 usleep(US_PER_MSEC);
1062 }
1063}
1064
1065TEST(libbacktrace, process_read) {
Christopher Ferris67aba682015-05-08 15:44:46 -07001066 g_ready = 0;
Christopher Ferris2b4a63f2015-03-17 14:42:03 -07001067 pid_t pid;
1068 if ((pid = fork()) == 0) {
1069 ForkedReadTest();
1070 exit(0);
1071 }
1072 ASSERT_NE(-1, pid);
1073
1074 bool test_executed = false;
1075 uint64_t start = NanoTime();
1076 while (1) {
1077 if (ptrace(PTRACE_ATTACH, pid, 0, 0) == 0) {
1078 WaitForStop(pid);
1079
1080 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(pid, pid));
Christopher Ferris97e00bb2015-04-02 14:22:31 -07001081 ASSERT_TRUE(backtrace.get() != nullptr);
Christopher Ferris2b4a63f2015-03-17 14:42:03 -07001082
1083 uintptr_t read_addr;
1084 size_t bytes_read = backtrace->Read(reinterpret_cast<uintptr_t>(&g_ready),
1085 reinterpret_cast<uint8_t*>(&read_addr),
1086 sizeof(uintptr_t));
1087 ASSERT_EQ(sizeof(uintptr_t), bytes_read);
1088 if (read_addr) {
1089 // The forked process is ready to be read.
1090 bytes_read = backtrace->Read(reinterpret_cast<uintptr_t>(&g_addr),
1091 reinterpret_cast<uint8_t*>(&read_addr),
1092 sizeof(uintptr_t));
1093 ASSERT_EQ(sizeof(uintptr_t), bytes_read);
1094
1095 RunReadTest(backtrace.get(), read_addr);
1096
1097 test_executed = true;
1098 break;
1099 }
1100 ASSERT_TRUE(ptrace(PTRACE_DETACH, pid, 0, 0) == 0);
1101 }
1102 if ((NanoTime() - start) > 5 * NS_PER_SEC) {
1103 break;
1104 }
1105 usleep(US_PER_MSEC);
1106 }
1107 kill(pid, SIGKILL);
1108 ASSERT_EQ(waitpid(pid, nullptr, 0), pid);
1109
1110 ASSERT_TRUE(test_executed);
Christopher Ferrise2960912014-03-07 19:42:19 -08001111}
1112
Christopher Ferris82f3bbd2017-03-14 15:22:26 -07001113static void VerifyFunctionsFound(const std::vector<std::string>& found_functions) {
Christopher Ferris67aba682015-05-08 15:44:46 -07001114 // We expect to find these functions in libbacktrace_test. If we don't
1115 // find them, that's a bug in the memory read handling code in libunwind.
1116 std::list<std::string> expected_functions;
1117 expected_functions.push_back("test_recursive_call");
1118 expected_functions.push_back("test_level_one");
1119 expected_functions.push_back("test_level_two");
1120 expected_functions.push_back("test_level_three");
1121 expected_functions.push_back("test_level_four");
1122 for (const auto& found_function : found_functions) {
1123 for (const auto& expected_function : expected_functions) {
1124 if (found_function == expected_function) {
1125 expected_functions.remove(found_function);
1126 break;
1127 }
1128 }
1129 }
1130 ASSERT_TRUE(expected_functions.empty()) << "Not all functions found in shared library.";
1131}
1132
Christopher Ferris82f3bbd2017-03-14 15:22:26 -07001133static const char* CopySharedLibrary() {
Christopher Ferris67aba682015-05-08 15:44:46 -07001134#if defined(__LP64__)
1135 const char* lib_name = "lib64";
1136#else
1137 const char* lib_name = "lib";
1138#endif
1139
1140#if defined(__BIONIC__)
1141 const char* tmp_so_name = "/data/local/tmp/libbacktrace_test.so";
1142 std::string cp_cmd = android::base::StringPrintf("cp /system/%s/libbacktrace_test.so %s",
1143 lib_name, tmp_so_name);
1144#else
1145 const char* tmp_so_name = "/tmp/libbacktrace_test.so";
1146 if (getenv("ANDROID_HOST_OUT") == NULL) {
1147 fprintf(stderr, "ANDROID_HOST_OUT not set, make sure you run lunch.");
1148 return nullptr;
1149 }
1150 std::string cp_cmd = android::base::StringPrintf("cp %s/%s/libbacktrace_test.so %s",
1151 getenv("ANDROID_HOST_OUT"), lib_name,
1152 tmp_so_name);
1153#endif
1154
1155 // Copy the shared so to a tempory directory.
1156 system(cp_cmd.c_str());
1157
1158 return tmp_so_name;
1159}
1160
1161TEST(libbacktrace, check_unreadable_elf_local) {
1162 const char* tmp_so_name = CopySharedLibrary();
1163 ASSERT_TRUE(tmp_so_name != nullptr);
1164
1165 struct stat buf;
1166 ASSERT_TRUE(stat(tmp_so_name, &buf) != -1);
1167 uintptr_t map_size = buf.st_size;
1168
1169 int fd = open(tmp_so_name, O_RDONLY);
1170 ASSERT_TRUE(fd != -1);
1171
Christopher Ferris61c48ac2016-01-15 16:08:58 -08001172 void* map = mmap(NULL, map_size, PROT_READ | PROT_EXEC, MAP_PRIVATE, fd, 0);
Christopher Ferris67aba682015-05-08 15:44:46 -07001173 ASSERT_TRUE(map != MAP_FAILED);
1174 close(fd);
1175 ASSERT_TRUE(unlink(tmp_so_name) != -1);
1176
1177 std::vector<std::string> found_functions;
1178 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(BACKTRACE_CURRENT_PROCESS,
1179 BACKTRACE_CURRENT_THREAD));
1180 ASSERT_TRUE(backtrace.get() != nullptr);
1181
1182 // Needed before GetFunctionName will work.
1183 backtrace->Unwind(0);
1184
1185 // Loop through the entire map, and get every function we can find.
1186 map_size += reinterpret_cast<uintptr_t>(map);
1187 std::string last_func;
1188 for (uintptr_t read_addr = reinterpret_cast<uintptr_t>(map);
1189 read_addr < map_size; read_addr += 4) {
1190 uintptr_t offset;
1191 std::string func_name = backtrace->GetFunctionName(read_addr, &offset);
1192 if (!func_name.empty() && last_func != func_name) {
1193 found_functions.push_back(func_name);
1194 }
1195 last_func = func_name;
1196 }
1197
1198 ASSERT_TRUE(munmap(map, map_size - reinterpret_cast<uintptr_t>(map)) == 0);
1199
1200 VerifyFunctionsFound(found_functions);
1201}
1202
1203TEST(libbacktrace, check_unreadable_elf_remote) {
1204 const char* tmp_so_name = CopySharedLibrary();
1205 ASSERT_TRUE(tmp_so_name != nullptr);
1206
1207 g_ready = 0;
1208
1209 struct stat buf;
1210 ASSERT_TRUE(stat(tmp_so_name, &buf) != -1);
1211 uintptr_t map_size = buf.st_size;
1212
1213 pid_t pid;
1214 if ((pid = fork()) == 0) {
1215 int fd = open(tmp_so_name, O_RDONLY);
1216 if (fd == -1) {
1217 fprintf(stderr, "Failed to open file %s: %s\n", tmp_so_name, strerror(errno));
1218 unlink(tmp_so_name);
1219 exit(0);
1220 }
1221
Christopher Ferris61c48ac2016-01-15 16:08:58 -08001222 void* map = mmap(NULL, map_size, PROT_READ | PROT_EXEC, MAP_PRIVATE, fd, 0);
Christopher Ferris67aba682015-05-08 15:44:46 -07001223 if (map == MAP_FAILED) {
1224 fprintf(stderr, "Failed to map in memory: %s\n", strerror(errno));
1225 unlink(tmp_so_name);
1226 exit(0);
1227 }
1228 close(fd);
1229 if (unlink(tmp_so_name) == -1) {
1230 fprintf(stderr, "Failed to unlink: %s\n", strerror(errno));
1231 exit(0);
1232 }
1233
1234 g_addr = reinterpret_cast<uintptr_t>(map);
1235 g_ready = 1;
1236 while (true) {
1237 usleep(US_PER_MSEC);
1238 }
1239 exit(0);
1240 }
1241 ASSERT_TRUE(pid > 0);
1242
1243 std::vector<std::string> found_functions;
1244 uint64_t start = NanoTime();
1245 while (true) {
1246 ASSERT_TRUE(ptrace(PTRACE_ATTACH, pid, 0, 0) == 0);
1247
1248 // Wait for the process to get to a stopping point.
1249 WaitForStop(pid);
1250
1251 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(pid, BACKTRACE_CURRENT_THREAD));
1252 ASSERT_TRUE(backtrace.get() != nullptr);
1253
1254 uintptr_t read_addr;
1255 ASSERT_EQ(sizeof(uintptr_t), backtrace->Read(reinterpret_cast<uintptr_t>(&g_ready), reinterpret_cast<uint8_t*>(&read_addr), sizeof(uintptr_t)));
1256 if (read_addr) {
1257 ASSERT_EQ(sizeof(uintptr_t), backtrace->Read(reinterpret_cast<uintptr_t>(&g_addr), reinterpret_cast<uint8_t*>(&read_addr), sizeof(uintptr_t)));
1258
1259 // Needed before GetFunctionName will work.
1260 backtrace->Unwind(0);
1261
1262 // Loop through the entire map, and get every function we can find.
1263 map_size += read_addr;
1264 std::string last_func;
1265 for (; read_addr < map_size; read_addr += 4) {
1266 uintptr_t offset;
1267 std::string func_name = backtrace->GetFunctionName(read_addr, &offset);
1268 if (!func_name.empty() && last_func != func_name) {
1269 found_functions.push_back(func_name);
1270 }
1271 last_func = func_name;
1272 }
1273 break;
1274 }
1275 ASSERT_TRUE(ptrace(PTRACE_DETACH, pid, 0, 0) == 0);
1276
1277 if ((NanoTime() - start) > 5 * NS_PER_SEC) {
1278 break;
1279 }
1280 usleep(US_PER_MSEC);
1281 }
1282
1283 kill(pid, SIGKILL);
1284 ASSERT_EQ(waitpid(pid, nullptr, 0), pid);
1285
1286 VerifyFunctionsFound(found_functions);
1287}
1288
Christopher Ferris82f3bbd2017-03-14 15:22:26 -07001289static bool FindFuncFrameInBacktrace(Backtrace* backtrace, uintptr_t test_func, size_t* frame_num) {
Christopher Ferris67aba682015-05-08 15:44:46 -07001290 backtrace_map_t map;
1291 backtrace->FillInMap(test_func, &map);
1292 if (!BacktraceMap::IsValid(map)) {
1293 return false;
1294 }
1295
1296 // Loop through the frames, and find the one that is in the map.
1297 *frame_num = 0;
1298 for (Backtrace::const_iterator it = backtrace->begin(); it != backtrace->end(); ++it) {
1299 if (BacktraceMap::IsValid(it->map) && map.start == it->map.start &&
1300 it->pc >= test_func) {
1301 *frame_num = it->num;
1302 return true;
1303 }
1304 }
1305 return false;
1306}
1307
Christopher Ferris82f3bbd2017-03-14 15:22:26 -07001308static void VerifyUnreadableElfFrame(Backtrace* backtrace, uintptr_t test_func, size_t frame_num) {
Christopher Ferris67aba682015-05-08 15:44:46 -07001309 ASSERT_LT(backtrace->NumFrames(), static_cast<size_t>(MAX_BACKTRACE_FRAMES))
1310 << DumpFrames(backtrace);
1311
1312 ASSERT_TRUE(frame_num != 0) << DumpFrames(backtrace);
1313 // Make sure that there is at least one more frame above the test func call.
1314 ASSERT_LT(frame_num, backtrace->NumFrames()) << DumpFrames(backtrace);
1315
1316 uintptr_t diff = backtrace->GetFrame(frame_num)->pc - test_func;
1317 ASSERT_LT(diff, 200U) << DumpFrames(backtrace);
1318}
1319
Christopher Ferris82f3bbd2017-03-14 15:22:26 -07001320static void VerifyUnreadableElfBacktrace(uintptr_t test_func) {
Christopher Ferris67aba682015-05-08 15:44:46 -07001321 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(BACKTRACE_CURRENT_PROCESS,
1322 BACKTRACE_CURRENT_THREAD));
1323 ASSERT_TRUE(backtrace.get() != nullptr);
1324 ASSERT_TRUE(backtrace->Unwind(0));
Christopher Ferris206a3b92016-03-09 14:35:54 -08001325 ASSERT_EQ(BACKTRACE_UNWIND_NO_ERROR, backtrace->GetError());
Christopher Ferris67aba682015-05-08 15:44:46 -07001326
1327 size_t frame_num;
1328 ASSERT_TRUE(FindFuncFrameInBacktrace(backtrace.get(), test_func, &frame_num));
1329
1330 VerifyUnreadableElfFrame(backtrace.get(), test_func, frame_num);
1331}
1332
1333typedef int (*test_func_t)(int, int, int, int, void (*)(uintptr_t), uintptr_t);
1334
1335TEST(libbacktrace, unwind_through_unreadable_elf_local) {
1336 const char* tmp_so_name = CopySharedLibrary();
1337 ASSERT_TRUE(tmp_so_name != nullptr);
1338 void* lib_handle = dlopen(tmp_so_name, RTLD_NOW);
1339 ASSERT_TRUE(lib_handle != nullptr);
1340 ASSERT_TRUE(unlink(tmp_so_name) != -1);
1341
1342 test_func_t test_func;
1343 test_func = reinterpret_cast<test_func_t>(dlsym(lib_handle, "test_level_one"));
1344 ASSERT_TRUE(test_func != nullptr);
1345
1346 ASSERT_NE(test_func(1, 2, 3, 4, VerifyUnreadableElfBacktrace,
1347 reinterpret_cast<uintptr_t>(test_func)), 0);
1348
1349 ASSERT_TRUE(dlclose(lib_handle) == 0);
1350}
1351
1352TEST(libbacktrace, unwind_through_unreadable_elf_remote) {
1353 const char* tmp_so_name = CopySharedLibrary();
1354 ASSERT_TRUE(tmp_so_name != nullptr);
1355 void* lib_handle = dlopen(tmp_so_name, RTLD_NOW);
1356 ASSERT_TRUE(lib_handle != nullptr);
1357 ASSERT_TRUE(unlink(tmp_so_name) != -1);
1358
1359 test_func_t test_func;
1360 test_func = reinterpret_cast<test_func_t>(dlsym(lib_handle, "test_level_one"));
1361 ASSERT_TRUE(test_func != nullptr);
1362
1363 pid_t pid;
1364 if ((pid = fork()) == 0) {
1365 test_func(1, 2, 3, 4, 0, 0);
1366 exit(0);
1367 }
1368 ASSERT_TRUE(pid > 0);
1369 ASSERT_TRUE(dlclose(lib_handle) == 0);
1370
1371 uint64_t start = NanoTime();
1372 bool done = false;
1373 while (!done) {
1374 ASSERT_TRUE(ptrace(PTRACE_ATTACH, pid, 0, 0) == 0);
1375
1376 // Wait for the process to get to a stopping point.
1377 WaitForStop(pid);
1378
1379 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(pid, BACKTRACE_CURRENT_THREAD));
1380 ASSERT_TRUE(backtrace.get() != nullptr);
1381 ASSERT_TRUE(backtrace->Unwind(0));
Christopher Ferris206a3b92016-03-09 14:35:54 -08001382 ASSERT_EQ(BACKTRACE_UNWIND_NO_ERROR, backtrace->GetError());
Christopher Ferris67aba682015-05-08 15:44:46 -07001383
1384 size_t frame_num;
1385 if (FindFuncFrameInBacktrace(backtrace.get(),
1386 reinterpret_cast<uintptr_t>(test_func), &frame_num)) {
1387
1388 VerifyUnreadableElfFrame(backtrace.get(), reinterpret_cast<uintptr_t>(test_func), frame_num);
1389 done = true;
1390 }
1391
1392 ASSERT_TRUE(ptrace(PTRACE_DETACH, pid, 0, 0) == 0);
1393
1394 if ((NanoTime() - start) > 5 * NS_PER_SEC) {
1395 break;
1396 }
1397 usleep(US_PER_MSEC);
1398 }
1399
1400 kill(pid, SIGKILL);
1401 ASSERT_EQ(waitpid(pid, nullptr, 0), pid);
1402
1403 ASSERT_TRUE(done) << "Test function never found in unwind.";
1404}
1405
Christopher Ferris206a3b92016-03-09 14:35:54 -08001406TEST(libbacktrace, unwind_thread_doesnt_exist) {
1407 std::unique_ptr<Backtrace> backtrace(
1408 Backtrace::Create(BACKTRACE_CURRENT_PROCESS, 99999999));
1409 ASSERT_TRUE(backtrace.get() != nullptr);
1410 ASSERT_FALSE(backtrace->Unwind(0));
1411 ASSERT_EQ(BACKTRACE_UNWIND_ERROR_THREAD_DOESNT_EXIST, backtrace->GetError());
1412}
1413
Christopher Ferris82f3bbd2017-03-14 15:22:26 -07001414TEST(libbacktrace, local_get_function_name_before_unwind) {
1415 std::unique_ptr<Backtrace> backtrace(
1416 Backtrace::Create(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD));
1417 ASSERT_TRUE(backtrace.get() != nullptr);
1418
1419 // Verify that trying to get a function name before doing an unwind works.
1420 uintptr_t cur_func_offset = reinterpret_cast<uintptr_t>(&test_level_one) + 1;
1421 size_t offset;
1422 ASSERT_NE(std::string(""), backtrace->GetFunctionName(cur_func_offset, &offset));
1423}
1424
1425TEST(libbacktrace, remote_get_function_name_before_unwind) {
1426 pid_t pid;
1427 CreateRemoteProcess(&pid);
1428
1429 // Now create an unwind object.
1430 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(pid, pid));
1431
1432 // Verify that trying to get a function name before doing an unwind works.
1433 uintptr_t cur_func_offset = reinterpret_cast<uintptr_t>(&test_level_one) + 1;
1434 size_t offset;
1435 ASSERT_NE(std::string(""), backtrace->GetFunctionName(cur_func_offset, &offset));
1436
1437 FinishRemoteProcess(pid);
1438}
1439
Christopher Ferrisf5e568e2017-03-22 13:18:31 -07001440static void SetUcontextSp(uintptr_t sp, ucontext_t* ucontext) {
1441#if defined(__arm__)
1442 ucontext->uc_mcontext.arm_sp = sp;
1443#elif defined(__aarch64__)
1444 ucontext->uc_mcontext.sp = sp;
1445#elif defined(__i386__)
1446 ucontext->uc_mcontext.gregs[REG_ESP] = sp;
1447#elif defined(__x86_64__)
1448 ucontext->uc_mcontext.gregs[REG_RSP] = sp;
1449#else
1450 UNUSED(sp);
1451 UNUSED(ucontext);
1452 ASSERT_TRUE(false) << "Unsupported architecture";
1453#endif
1454}
1455
1456static void SetUcontextPc(uintptr_t pc, ucontext_t* ucontext) {
1457#if defined(__arm__)
1458 ucontext->uc_mcontext.arm_pc = pc;
1459#elif defined(__aarch64__)
1460 ucontext->uc_mcontext.pc = pc;
1461#elif defined(__i386__)
1462 ucontext->uc_mcontext.gregs[REG_EIP] = pc;
1463#elif defined(__x86_64__)
1464 ucontext->uc_mcontext.gregs[REG_RIP] = pc;
1465#else
1466 UNUSED(pc);
1467 UNUSED(ucontext);
1468 ASSERT_TRUE(false) << "Unsupported architecture";
1469#endif
1470}
1471
1472static void SetUcontextLr(uintptr_t lr, ucontext_t* ucontext) {
1473#if defined(__arm__)
1474 ucontext->uc_mcontext.arm_lr = lr;
1475#elif defined(__aarch64__)
1476 ucontext->uc_mcontext.regs[30] = lr;
1477#elif defined(__i386__)
1478 // The lr is on the stack.
1479 ASSERT_TRUE(lr != 0);
1480 ASSERT_TRUE(ucontext != nullptr);
1481#elif defined(__x86_64__)
1482 // The lr is on the stack.
1483 ASSERT_TRUE(lr != 0);
1484 ASSERT_TRUE(ucontext != nullptr);
1485#else
1486 UNUSED(lr);
1487 UNUSED(ucontext);
1488 ASSERT_TRUE(false) << "Unsupported architecture";
1489#endif
1490}
1491
1492static constexpr size_t DEVICE_MAP_SIZE = 1024;
1493
1494static void SetupDeviceMap(void** device_map) {
1495 // Make sure that anything in a device map will result in fails
1496 // to read.
1497 android::base::unique_fd device_fd(open("/dev/zero", O_RDONLY | O_CLOEXEC));
1498
1499 *device_map = mmap(nullptr, 1024, PROT_READ, MAP_PRIVATE, device_fd, 0);
1500 ASSERT_TRUE(*device_map != MAP_FAILED);
1501
1502 // Make sure the map is readable.
1503 ASSERT_EQ(0, reinterpret_cast<int*>(*device_map)[0]);
1504}
1505
1506static void UnwindFromDevice(Backtrace* backtrace, void* device_map) {
1507 uintptr_t device_map_uint = reinterpret_cast<uintptr_t>(device_map);
1508
1509 backtrace_map_t map;
1510 backtrace->FillInMap(device_map_uint, &map);
1511 // Verify the flag is set.
1512 ASSERT_EQ(PROT_DEVICE_MAP, map.flags & PROT_DEVICE_MAP);
1513
1514 // Quick sanity checks.
1515 size_t offset;
1516 ASSERT_EQ(std::string(""), backtrace->GetFunctionName(device_map_uint, &offset));
1517 ASSERT_EQ(std::string(""), backtrace->GetFunctionName(device_map_uint, &offset, &map));
1518 ASSERT_EQ(std::string(""), backtrace->GetFunctionName(0, &offset));
1519
1520 uintptr_t cur_func_offset = reinterpret_cast<uintptr_t>(&test_level_one) + 1;
1521 // Now verify the device map flag actually causes the function name to be empty.
1522 backtrace->FillInMap(cur_func_offset, &map);
1523 ASSERT_TRUE((map.flags & PROT_DEVICE_MAP) == 0);
1524 ASSERT_NE(std::string(""), backtrace->GetFunctionName(cur_func_offset, &offset, &map));
1525 map.flags |= PROT_DEVICE_MAP;
1526 ASSERT_EQ(std::string(""), backtrace->GetFunctionName(cur_func_offset, &offset, &map));
1527
1528 ucontext_t ucontext;
1529
1530 // Create a context that has the pc in the device map, but the sp
1531 // in a non-device map.
1532 memset(&ucontext, 0, sizeof(ucontext));
1533 SetUcontextSp(reinterpret_cast<uintptr_t>(&ucontext), &ucontext);
1534 SetUcontextPc(device_map_uint, &ucontext);
1535 SetUcontextLr(cur_func_offset, &ucontext);
1536
1537 ASSERT_TRUE(backtrace->Unwind(0, &ucontext));
1538
1539 // The buffer should only be a single element.
1540 ASSERT_EQ(1U, backtrace->NumFrames());
1541 const backtrace_frame_data_t* frame = backtrace->GetFrame(0);
1542 ASSERT_EQ(device_map_uint, frame->pc);
1543 ASSERT_EQ(reinterpret_cast<uintptr_t>(&ucontext), frame->sp);
1544
1545 // Check what happens when skipping the first frame.
1546 ASSERT_TRUE(backtrace->Unwind(1, &ucontext));
1547 ASSERT_EQ(0U, backtrace->NumFrames());
1548
1549 // Create a context that has the sp in the device map, but the pc
1550 // in a non-device map.
1551 memset(&ucontext, 0, sizeof(ucontext));
1552 SetUcontextSp(device_map_uint, &ucontext);
1553 SetUcontextPc(cur_func_offset, &ucontext);
1554 SetUcontextLr(cur_func_offset, &ucontext);
1555
1556 ASSERT_TRUE(backtrace->Unwind(0, &ucontext));
1557
1558 // The buffer should only be a single element.
1559 ASSERT_EQ(1U, backtrace->NumFrames());
1560 frame = backtrace->GetFrame(0);
1561 ASSERT_EQ(cur_func_offset, frame->pc);
1562 ASSERT_EQ(device_map_uint, frame->sp);
1563
1564 // Check what happens when skipping the first frame.
1565 ASSERT_TRUE(backtrace->Unwind(1, &ucontext));
1566 ASSERT_EQ(0U, backtrace->NumFrames());
1567}
1568
1569TEST(libbacktrace, unwind_disallow_device_map_local) {
1570 void* device_map;
1571 SetupDeviceMap(&device_map);
1572
1573 // Now create an unwind object.
1574 std::unique_ptr<Backtrace> backtrace(
1575 Backtrace::Create(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD));
1576 ASSERT_TRUE(backtrace);
1577
1578 UnwindFromDevice(backtrace.get(), device_map);
1579
1580 munmap(device_map, DEVICE_MAP_SIZE);
1581}
1582
1583TEST(libbacktrace, unwind_disallow_device_map_remote) {
1584 void* device_map;
1585 SetupDeviceMap(&device_map);
1586
1587 // Fork a process to do a remote backtrace.
1588 pid_t pid;
1589 CreateRemoteProcess(&pid);
1590
1591 // Now create an unwind object.
1592 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(pid, pid));
1593
1594 // TODO: Currently unwind from context doesn't work on remote
1595 // unwind. Keep this test because the new unwinder should support
1596 // this eventually, or we can delete this test.
1597 // properly with unwind from context.
1598 // UnwindFromDevice(backtrace.get(), device_map);
1599
1600 FinishRemoteProcess(pid);
1601
1602 munmap(device_map, DEVICE_MAP_SIZE);
1603}
1604
Christopher Ferrise2960912014-03-07 19:42:19 -08001605#if defined(ENABLE_PSS_TESTS)
1606#include "GetPss.h"
1607
Chih-Hung Hsieh67867db2016-05-18 15:53:15 -07001608#define MAX_LEAK_BYTES (32*1024UL)
Christopher Ferrise2960912014-03-07 19:42:19 -08001609
Christopher Ferris82f3bbd2017-03-14 15:22:26 -07001610static void CheckForLeak(pid_t pid, pid_t tid) {
Christopher Ferrise2960912014-03-07 19:42:19 -08001611 // Do a few runs to get the PSS stable.
1612 for (size_t i = 0; i < 100; i++) {
1613 Backtrace* backtrace = Backtrace::Create(pid, tid);
Christopher Ferris2b4a63f2015-03-17 14:42:03 -07001614 ASSERT_TRUE(backtrace != nullptr);
Christopher Ferrise2960912014-03-07 19:42:19 -08001615 ASSERT_TRUE(backtrace->Unwind(0));
Christopher Ferris206a3b92016-03-09 14:35:54 -08001616 ASSERT_EQ(BACKTRACE_UNWIND_NO_ERROR, backtrace->GetError());
Christopher Ferrise2960912014-03-07 19:42:19 -08001617 delete backtrace;
1618 }
1619 size_t stable_pss = GetPssBytes();
Christopher Ferris2c43cff2015-03-26 19:18:36 -07001620 ASSERT_TRUE(stable_pss != 0);
Christopher Ferrise2960912014-03-07 19:42:19 -08001621
1622 // Loop enough that even a small leak should be detectable.
1623 for (size_t i = 0; i < 4096; i++) {
1624 Backtrace* backtrace = Backtrace::Create(pid, tid);
Christopher Ferris2b4a63f2015-03-17 14:42:03 -07001625 ASSERT_TRUE(backtrace != nullptr);
Christopher Ferrise2960912014-03-07 19:42:19 -08001626 ASSERT_TRUE(backtrace->Unwind(0));
Christopher Ferris206a3b92016-03-09 14:35:54 -08001627 ASSERT_EQ(BACKTRACE_UNWIND_NO_ERROR, backtrace->GetError());
Christopher Ferrise2960912014-03-07 19:42:19 -08001628 delete backtrace;
1629 }
1630 size_t new_pss = GetPssBytes();
Christopher Ferris2c43cff2015-03-26 19:18:36 -07001631 ASSERT_TRUE(new_pss != 0);
Christopher Ferris5ccdfa62016-03-07 19:18:31 -08001632 if (new_pss > stable_pss) {
1633 ASSERT_LE(new_pss - stable_pss, MAX_LEAK_BYTES);
1634 }
Christopher Ferrise2960912014-03-07 19:42:19 -08001635}
1636
1637TEST(libbacktrace, check_for_leak_local) {
1638 CheckForLeak(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD);
1639}
1640
1641TEST(libbacktrace, check_for_leak_local_thread) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -07001642 thread_t thread_data = { 0, 0, 0, nullptr };
Christopher Ferrise2960912014-03-07 19:42:19 -08001643 pthread_t thread;
Christopher Ferris2b4a63f2015-03-17 14:42:03 -07001644 ASSERT_TRUE(pthread_create(&thread, nullptr, ThreadLevelRun, &thread_data) == 0);
Christopher Ferrise2960912014-03-07 19:42:19 -08001645
1646 // Wait up to 2 seconds for the tid to be set.
1647 ASSERT_TRUE(WaitForNonZero(&thread_data.state, 2));
1648
1649 CheckForLeak(BACKTRACE_CURRENT_PROCESS, thread_data.tid);
1650
1651 // Tell the thread to exit its infinite loop.
1652 android_atomic_acquire_store(0, &thread_data.state);
1653
Christopher Ferris2b4a63f2015-03-17 14:42:03 -07001654 ASSERT_TRUE(pthread_join(thread, nullptr) == 0);
Christopher Ferrise2960912014-03-07 19:42:19 -08001655}
1656
1657TEST(libbacktrace, check_for_leak_remote) {
1658 pid_t pid;
Christopher Ferris82f3bbd2017-03-14 15:22:26 -07001659 CreateRemoteProcess(&pid);
Christopher Ferrise2960912014-03-07 19:42:19 -08001660
1661 CheckForLeak(pid, BACKTRACE_CURRENT_THREAD);
1662
Christopher Ferris82f3bbd2017-03-14 15:22:26 -07001663 FinishRemoteProcess(pid);
Christopher Ferrise2960912014-03-07 19:42:19 -08001664}
1665#endif