blob: e5eb9e34e00a2c90511f54fe31835a3ad64f02a5 [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 Ferris5ea2c1f2017-03-23 14:55:01 -070039#include <ostream>
Christopher Ferris2c43cff2015-03-26 19:18:36 -070040#include <string>
Christopher Ferris17e91d42013-10-21 13:30:52 -070041#include <vector>
42
Dan Albert23f750b2015-04-30 12:52:21 -070043#include <backtrace/Backtrace.h>
44#include <backtrace/BacktraceMap.h>
45
Christopher Ferrisf5e568e2017-03-22 13:18:31 -070046#include <android-base/macros.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080047#include <android-base/stringprintf.h>
Christopher Ferris82f3bbd2017-03-14 15:22:26 -070048#include <android-base/unique_fd.h>
Dan Albert23f750b2015-04-30 12:52:21 -070049#include <cutils/atomic.h>
50#include <cutils/threads.h>
51
52#include <gtest/gtest.h>
53
54// For the THREAD_SIGNAL definition.
55#include "BacktraceCurrent.h"
Christopher Ferris5ea2c1f2017-03-23 14:55:01 -070056#include "backtrace_testlib.h"
Christopher Ferris17e91d42013-10-21 13:30:52 -070057#include "thread_utils.h"
58
59// Number of microseconds per milliseconds.
60#define US_PER_MSEC 1000
61
62// Number of nanoseconds in a second.
63#define NS_PER_SEC 1000000000ULL
64
65// Number of simultaneous dumping operations to perform.
Christopher Ferris3cdbfdc2014-11-08 15:57:11 -080066#define NUM_THREADS 40
Christopher Ferris17e91d42013-10-21 13:30:52 -070067
68// Number of simultaneous threads running in our forked process.
69#define NUM_PTRACE_THREADS 5
70
Christopher Ferris46756822014-01-14 20:16:30 -080071struct thread_t {
Christopher Ferris17e91d42013-10-21 13:30:52 -070072 pid_t tid;
73 int32_t state;
74 pthread_t threadId;
Christopher Ferris2b4a63f2015-03-17 14:42:03 -070075 void* data;
Christopher Ferris46756822014-01-14 20:16:30 -080076};
Christopher Ferris17e91d42013-10-21 13:30:52 -070077
Christopher Ferris46756822014-01-14 20:16:30 -080078struct dump_thread_t {
Christopher Ferris17e91d42013-10-21 13:30:52 -070079 thread_t thread;
Christopher Ferris20303f82014-01-10 16:33:16 -080080 Backtrace* backtrace;
Christopher Ferris17e91d42013-10-21 13:30:52 -070081 int32_t* now;
82 int32_t done;
Christopher Ferris46756822014-01-14 20:16:30 -080083};
Christopher Ferris17e91d42013-10-21 13:30:52 -070084
Christopher Ferrisb9de87f2017-09-20 13:37:24 -070085typedef Backtrace* (*create_func_t)(pid_t, pid_t, BacktraceMap*);
86typedef BacktraceMap* (*map_create_func_t)(pid_t, bool);
87
88static void VerifyLevelDump(Backtrace* backtrace, create_func_t create_func = nullptr,
89 map_create_func_t map_func = nullptr);
90static void VerifyMaxDump(Backtrace* backtrace, create_func_t create_func = nullptr,
91 map_create_func_t map_func = nullptr);
92
Christopher Ferris82f3bbd2017-03-14 15:22:26 -070093static uint64_t NanoTime() {
Christopher Ferris17e91d42013-10-21 13:30:52 -070094 struct timespec t = { 0, 0 };
95 clock_gettime(CLOCK_MONOTONIC, &t);
96 return static_cast<uint64_t>(t.tv_sec * NS_PER_SEC + t.tv_nsec);
97}
98
Christopher Ferris82f3bbd2017-03-14 15:22:26 -070099static std::string DumpFrames(Backtrace* backtrace) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800100 if (backtrace->NumFrames() == 0) {
Christopher Ferris97e00bb2015-04-02 14:22:31 -0700101 return " No frames to dump.\n";
Christopher Ferris20303f82014-01-10 16:33:16 -0800102 }
103
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700104 std::string frame;
Christopher Ferris20303f82014-01-10 16:33:16 -0800105 for (size_t i = 0; i < backtrace->NumFrames(); i++) {
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700106 frame += " " + backtrace->FormatFrameData(i) + '\n';
Christopher Ferris17e91d42013-10-21 13:30:52 -0700107 }
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700108 return frame;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700109}
110
Christopher Ferris82f3bbd2017-03-14 15:22:26 -0700111static void WaitForStop(pid_t pid) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700112 uint64_t start = NanoTime();
113
114 siginfo_t si;
115 while (ptrace(PTRACE_GETSIGINFO, pid, 0, &si) < 0 && (errno == EINTR || errno == ESRCH)) {
116 if ((NanoTime() - start) > NS_PER_SEC) {
117 printf("The process did not get to a stopping point in 1 second.\n");
118 break;
119 }
120 usleep(US_PER_MSEC);
121 }
122}
123
Christopher Ferris82f3bbd2017-03-14 15:22:26 -0700124static void CreateRemoteProcess(pid_t* pid) {
125 if ((*pid = fork()) == 0) {
126 while (true)
127 ;
128 _exit(0);
129 }
130 ASSERT_NE(-1, *pid);
131
132 ASSERT_TRUE(ptrace(PTRACE_ATTACH, *pid, 0, 0) == 0);
133
134 // Wait for the process to get to a stopping point.
135 WaitForStop(*pid);
136}
137
138static void FinishRemoteProcess(pid_t pid) {
139 ASSERT_TRUE(ptrace(PTRACE_DETACH, pid, 0, 0) == 0);
140
141 kill(pid, SIGKILL);
142 ASSERT_EQ(waitpid(pid, nullptr, 0), pid);
143}
144
145static bool ReadyLevelBacktrace(Backtrace* backtrace) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700146 // See if test_level_four is in the backtrace.
147 bool found = false;
Christopher Ferris46756822014-01-14 20:16:30 -0800148 for (Backtrace::const_iterator it = backtrace->begin(); it != backtrace->end(); ++it) {
149 if (it->func_name == "test_level_four") {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700150 found = true;
151 break;
152 }
153 }
154
155 return found;
156}
157
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700158static void VerifyLevelDump(Backtrace* backtrace, create_func_t, map_create_func_t) {
Christopher Ferris97e00bb2015-04-02 14:22:31 -0700159 ASSERT_GT(backtrace->NumFrames(), static_cast<size_t>(0))
160 << DumpFrames(backtrace);
161 ASSERT_LT(backtrace->NumFrames(), static_cast<size_t>(MAX_BACKTRACE_FRAMES))
162 << DumpFrames(backtrace);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700163
164 // Look through the frames starting at the highest to find the
165 // frame we want.
166 size_t frame_num = 0;
Christopher Ferris20303f82014-01-10 16:33:16 -0800167 for (size_t i = backtrace->NumFrames()-1; i > 2; i--) {
Christopher Ferris46756822014-01-14 20:16:30 -0800168 if (backtrace->GetFrame(i)->func_name == "test_level_one") {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700169 frame_num = i;
170 break;
171 }
172 }
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700173 ASSERT_LT(static_cast<size_t>(0), frame_num) << DumpFrames(backtrace);
174 ASSERT_LE(static_cast<size_t>(3), frame_num) << DumpFrames(backtrace);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700175
Christopher Ferris97e00bb2015-04-02 14:22:31 -0700176 ASSERT_EQ(backtrace->GetFrame(frame_num)->func_name, "test_level_one")
177 << DumpFrames(backtrace);
178 ASSERT_EQ(backtrace->GetFrame(frame_num-1)->func_name, "test_level_two")
179 << DumpFrames(backtrace);
180 ASSERT_EQ(backtrace->GetFrame(frame_num-2)->func_name, "test_level_three")
181 << DumpFrames(backtrace);
182 ASSERT_EQ(backtrace->GetFrame(frame_num-3)->func_name, "test_level_four")
183 << DumpFrames(backtrace);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700184}
185
Christopher Ferris82f3bbd2017-03-14 15:22:26 -0700186static void VerifyLevelBacktrace(void*) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700187 std::unique_ptr<Backtrace> backtrace(
Christopher Ferris20303f82014-01-10 16:33:16 -0800188 Backtrace::Create(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD));
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700189 ASSERT_TRUE(backtrace.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800190 ASSERT_TRUE(backtrace->Unwind(0));
Christopher Ferris206a3b92016-03-09 14:35:54 -0800191 ASSERT_EQ(BACKTRACE_UNWIND_NO_ERROR, backtrace->GetError());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700192
Christopher Ferris20303f82014-01-10 16:33:16 -0800193 VerifyLevelDump(backtrace.get());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700194}
195
Christopher Ferris82f3bbd2017-03-14 15:22:26 -0700196static bool ReadyMaxBacktrace(Backtrace* backtrace) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800197 return (backtrace->NumFrames() == MAX_BACKTRACE_FRAMES);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700198}
199
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700200static void VerifyMaxDump(Backtrace* backtrace, create_func_t, map_create_func_t) {
Christopher Ferris97e00bb2015-04-02 14:22:31 -0700201 ASSERT_EQ(backtrace->NumFrames(), static_cast<size_t>(MAX_BACKTRACE_FRAMES))
202 << DumpFrames(backtrace);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700203 // Verify that the last frame is our recursive call.
Christopher Ferris97e00bb2015-04-02 14:22:31 -0700204 ASSERT_EQ(backtrace->GetFrame(MAX_BACKTRACE_FRAMES-1)->func_name, "test_recursive_call")
205 << DumpFrames(backtrace);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700206}
207
Christopher Ferris82f3bbd2017-03-14 15:22:26 -0700208static void VerifyMaxBacktrace(void*) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700209 std::unique_ptr<Backtrace> backtrace(
Christopher Ferris20303f82014-01-10 16:33:16 -0800210 Backtrace::Create(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD));
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700211 ASSERT_TRUE(backtrace.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800212 ASSERT_TRUE(backtrace->Unwind(0));
Christopher Ferris206a3b92016-03-09 14:35:54 -0800213 ASSERT_EQ(BACKTRACE_UNWIND_NO_ERROR, backtrace->GetError());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700214
Christopher Ferris20303f82014-01-10 16:33:16 -0800215 VerifyMaxDump(backtrace.get());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700216}
217
Christopher Ferris82f3bbd2017-03-14 15:22:26 -0700218static void ThreadSetState(void* data) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700219 thread_t* thread = reinterpret_cast<thread_t*>(data);
220 android_atomic_acquire_store(1, &thread->state);
221 volatile int i = 0;
222 while (thread->state) {
223 i++;
224 }
225}
226
Christopher Ferris82f3bbd2017-03-14 15:22:26 -0700227static bool WaitForNonZero(int32_t* value, uint64_t seconds) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700228 uint64_t start = NanoTime();
229 do {
230 if (android_atomic_acquire_load(value)) {
231 return true;
232 }
233 } while ((NanoTime() - start) < seconds * NS_PER_SEC);
234 return false;
235}
236
Christopher Ferrisca09ce92015-03-31 17:28:22 -0700237TEST(libbacktrace, local_no_unwind_frames) {
238 // Verify that a local unwind does not include any frames within
239 // libunwind or libbacktrace.
240 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), getpid()));
Christopher Ferris97e00bb2015-04-02 14:22:31 -0700241 ASSERT_TRUE(backtrace.get() != nullptr);
Christopher Ferrisca09ce92015-03-31 17:28:22 -0700242 ASSERT_TRUE(backtrace->Unwind(0));
Christopher Ferris206a3b92016-03-09 14:35:54 -0800243 ASSERT_EQ(BACKTRACE_UNWIND_NO_ERROR, backtrace->GetError());
Christopher Ferrisca09ce92015-03-31 17:28:22 -0700244
245 ASSERT_TRUE(backtrace->NumFrames() != 0);
246 for (const auto& frame : *backtrace ) {
247 if (BacktraceMap::IsValid(frame.map)) {
248 const std::string name = basename(frame.map.name.c_str());
249 ASSERT_TRUE(name != "libunwind.so" && name != "libbacktrace.so")
250 << DumpFrames(backtrace.get());
251 }
252 break;
253 }
254}
255
Christopher Ferris17e91d42013-10-21 13:30:52 -0700256TEST(libbacktrace, local_trace) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700257 ASSERT_NE(test_level_one(1, 2, 3, 4, VerifyLevelBacktrace, nullptr), 0);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700258}
259
Christopher Ferris82f3bbd2017-03-14 15:22:26 -0700260static void VerifyIgnoreFrames(Backtrace* bt_all, Backtrace* bt_ign1, Backtrace* bt_ign2,
261 const char* cur_proc) {
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700262 ASSERT_EQ(bt_all->NumFrames(), bt_ign1->NumFrames() + 1) << "All backtrace:\n"
263 << DumpFrames(bt_all)
264 << "Ignore 1 backtrace:\n"
265 << DumpFrames(bt_ign1);
266 ASSERT_EQ(bt_all->NumFrames(), bt_ign2->NumFrames() + 2) << "All backtrace:\n"
267 << DumpFrames(bt_all)
268 << "Ignore 2 backtrace:\n"
269 << DumpFrames(bt_ign2);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700270
271 // Check all of the frames are the same > the current frame.
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700272 bool check = (cur_proc == nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800273 for (size_t i = 0; i < bt_ign2->NumFrames(); i++) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700274 if (check) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800275 EXPECT_EQ(bt_ign2->GetFrame(i)->pc, bt_ign1->GetFrame(i+1)->pc);
276 EXPECT_EQ(bt_ign2->GetFrame(i)->sp, bt_ign1->GetFrame(i+1)->sp);
277 EXPECT_EQ(bt_ign2->GetFrame(i)->stack_size, bt_ign1->GetFrame(i+1)->stack_size);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700278
Christopher Ferris20303f82014-01-10 16:33:16 -0800279 EXPECT_EQ(bt_ign2->GetFrame(i)->pc, bt_all->GetFrame(i+2)->pc);
280 EXPECT_EQ(bt_ign2->GetFrame(i)->sp, bt_all->GetFrame(i+2)->sp);
281 EXPECT_EQ(bt_ign2->GetFrame(i)->stack_size, bt_all->GetFrame(i+2)->stack_size);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700282 }
Christopher Ferris46756822014-01-14 20:16:30 -0800283 if (!check && bt_ign2->GetFrame(i)->func_name == cur_proc) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700284 check = true;
285 }
286 }
287}
288
Christopher Ferris82f3bbd2017-03-14 15:22:26 -0700289static void VerifyLevelIgnoreFrames(void*) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700290 std::unique_ptr<Backtrace> all(
Christopher Ferris20303f82014-01-10 16:33:16 -0800291 Backtrace::Create(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD));
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700292 ASSERT_TRUE(all.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800293 ASSERT_TRUE(all->Unwind(0));
Christopher Ferris206a3b92016-03-09 14:35:54 -0800294 ASSERT_EQ(BACKTRACE_UNWIND_NO_ERROR, all->GetError());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700295
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700296 std::unique_ptr<Backtrace> ign1(
Christopher Ferris20303f82014-01-10 16:33:16 -0800297 Backtrace::Create(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD));
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700298 ASSERT_TRUE(ign1.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800299 ASSERT_TRUE(ign1->Unwind(1));
Christopher Ferris206a3b92016-03-09 14:35:54 -0800300 ASSERT_EQ(BACKTRACE_UNWIND_NO_ERROR, ign1->GetError());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700301
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700302 std::unique_ptr<Backtrace> ign2(
Christopher Ferris20303f82014-01-10 16:33:16 -0800303 Backtrace::Create(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD));
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700304 ASSERT_TRUE(ign2.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800305 ASSERT_TRUE(ign2->Unwind(2));
Christopher Ferris206a3b92016-03-09 14:35:54 -0800306 ASSERT_EQ(BACKTRACE_UNWIND_NO_ERROR, ign2->GetError());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700307
Christopher Ferris20303f82014-01-10 16:33:16 -0800308 VerifyIgnoreFrames(all.get(), ign1.get(), ign2.get(), "VerifyLevelIgnoreFrames");
Christopher Ferris17e91d42013-10-21 13:30:52 -0700309}
310
311TEST(libbacktrace, local_trace_ignore_frames) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700312 ASSERT_NE(test_level_one(1, 2, 3, 4, VerifyLevelIgnoreFrames, nullptr), 0);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700313}
314
315TEST(libbacktrace, local_max_trace) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700316 ASSERT_NE(test_recursive_call(MAX_BACKTRACE_FRAMES+10, VerifyMaxBacktrace, nullptr), 0);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700317}
318
Christopher Ferris458cc662017-08-28 16:31:18 -0700319static void VerifyProcTest(pid_t pid, pid_t tid, bool (*ReadyFunc)(Backtrace*),
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700320 void (*VerifyFunc)(Backtrace*, create_func_t, map_create_func_t),
321 create_func_t create_func, map_create_func_t map_create_func) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700322 pid_t ptrace_tid;
323 if (tid < 0) {
324 ptrace_tid = pid;
325 } else {
326 ptrace_tid = tid;
327 }
328 uint64_t start = NanoTime();
329 bool verified = false;
Christopher Ferris97e00bb2015-04-02 14:22:31 -0700330 std::string last_dump;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700331 do {
332 usleep(US_PER_MSEC);
333 if (ptrace(PTRACE_ATTACH, ptrace_tid, 0, 0) == 0) {
334 // Wait for the process to get to a stopping point.
335 WaitForStop(ptrace_tid);
336
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700337 std::unique_ptr<BacktraceMap> map;
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700338 map.reset(map_create_func(pid, false));
339 std::unique_ptr<Backtrace> backtrace(create_func(pid, tid, map.get()));
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700340 ASSERT_TRUE(backtrace.get() != nullptr);
Christopher Ferris97e00bb2015-04-02 14:22:31 -0700341 ASSERT_TRUE(backtrace->Unwind(0));
Christopher Ferris206a3b92016-03-09 14:35:54 -0800342 ASSERT_EQ(BACKTRACE_UNWIND_NO_ERROR, backtrace->GetError());
Christopher Ferris20303f82014-01-10 16:33:16 -0800343 if (ReadyFunc(backtrace.get())) {
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700344 VerifyFunc(backtrace.get(), create_func, map_create_func);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700345 verified = true;
Christopher Ferris97e00bb2015-04-02 14:22:31 -0700346 } else {
347 last_dump = DumpFrames(backtrace.get());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700348 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800349
Christopher Ferris17e91d42013-10-21 13:30:52 -0700350 ASSERT_TRUE(ptrace(PTRACE_DETACH, ptrace_tid, 0, 0) == 0);
351 }
352 // If 5 seconds have passed, then we are done.
353 } while (!verified && (NanoTime() - start) <= 5 * NS_PER_SEC);
Christopher Ferris97e00bb2015-04-02 14:22:31 -0700354 ASSERT_TRUE(verified) << "Last backtrace:\n" << last_dump;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700355}
356
357TEST(libbacktrace, ptrace_trace) {
358 pid_t pid;
359 if ((pid = fork()) == 0) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700360 ASSERT_NE(test_level_one(1, 2, 3, 4, nullptr, nullptr), 0);
Christopher Ferrise2960912014-03-07 19:42:19 -0800361 _exit(1);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700362 }
Christopher Ferris458cc662017-08-28 16:31:18 -0700363 VerifyProcTest(pid, BACKTRACE_CURRENT_THREAD, ReadyLevelBacktrace, VerifyLevelDump,
364 Backtrace::Create, BacktraceMap::Create);
Christopher Ferrisdf290612014-01-22 19:21:07 -0800365
366 kill(pid, SIGKILL);
367 int status;
368 ASSERT_EQ(waitpid(pid, &status, 0), pid);
369}
370
Christopher Ferris458cc662017-08-28 16:31:18 -0700371TEST(libbacktrace, ptrace_trace_new) {
Christopher Ferrisdf290612014-01-22 19:21:07 -0800372 pid_t pid;
373 if ((pid = fork()) == 0) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700374 ASSERT_NE(test_level_one(1, 2, 3, 4, nullptr, nullptr), 0);
Christopher Ferrise2960912014-03-07 19:42:19 -0800375 _exit(1);
Christopher Ferrisdf290612014-01-22 19:21:07 -0800376 }
Christopher Ferris458cc662017-08-28 16:31:18 -0700377 VerifyProcTest(pid, BACKTRACE_CURRENT_THREAD, ReadyLevelBacktrace, VerifyLevelDump,
378 Backtrace::CreateNew, BacktraceMap::CreateNew);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700379
380 kill(pid, SIGKILL);
381 int status;
382 ASSERT_EQ(waitpid(pid, &status, 0), pid);
383}
384
385TEST(libbacktrace, ptrace_max_trace) {
386 pid_t pid;
387 if ((pid = fork()) == 0) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700388 ASSERT_NE(test_recursive_call(MAX_BACKTRACE_FRAMES+10, nullptr, nullptr), 0);
Christopher Ferrise2960912014-03-07 19:42:19 -0800389 _exit(1);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700390 }
Christopher Ferris458cc662017-08-28 16:31:18 -0700391 VerifyProcTest(pid, BACKTRACE_CURRENT_THREAD, ReadyMaxBacktrace, VerifyMaxDump, Backtrace::Create,
392 BacktraceMap::Create);
393
394 kill(pid, SIGKILL);
395 int status;
396 ASSERT_EQ(waitpid(pid, &status, 0), pid);
397}
398
399TEST(libbacktrace, ptrace_max_trace_new) {
400 pid_t pid;
401 if ((pid = fork()) == 0) {
402 ASSERT_NE(test_recursive_call(MAX_BACKTRACE_FRAMES + 10, nullptr, nullptr), 0);
403 _exit(1);
404 }
405 VerifyProcTest(pid, BACKTRACE_CURRENT_THREAD, ReadyMaxBacktrace, VerifyMaxDump,
406 Backtrace::CreateNew, BacktraceMap::CreateNew);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700407
408 kill(pid, SIGKILL);
409 int status;
410 ASSERT_EQ(waitpid(pid, &status, 0), pid);
411}
412
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700413static void VerifyProcessIgnoreFrames(Backtrace* bt_all, create_func_t create_func,
414 map_create_func_t map_create_func) {
415 std::unique_ptr<BacktraceMap> map(map_create_func(bt_all->Pid(), false));
416 std::unique_ptr<Backtrace> ign1(create_func(bt_all->Pid(), BACKTRACE_CURRENT_THREAD, map.get()));
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700417 ASSERT_TRUE(ign1.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800418 ASSERT_TRUE(ign1->Unwind(1));
Christopher Ferris206a3b92016-03-09 14:35:54 -0800419 ASSERT_EQ(BACKTRACE_UNWIND_NO_ERROR, ign1->GetError());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700420
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700421 std::unique_ptr<Backtrace> ign2(create_func(bt_all->Pid(), BACKTRACE_CURRENT_THREAD, map.get()));
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700422 ASSERT_TRUE(ign2.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800423 ASSERT_TRUE(ign2->Unwind(2));
Christopher Ferris206a3b92016-03-09 14:35:54 -0800424 ASSERT_EQ(BACKTRACE_UNWIND_NO_ERROR, ign2->GetError());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700425
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700426 VerifyIgnoreFrames(bt_all, ign1.get(), ign2.get(), nullptr);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700427}
428
429TEST(libbacktrace, ptrace_ignore_frames) {
430 pid_t pid;
431 if ((pid = fork()) == 0) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700432 ASSERT_NE(test_level_one(1, 2, 3, 4, nullptr, nullptr), 0);
Christopher Ferrise2960912014-03-07 19:42:19 -0800433 _exit(1);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700434 }
Christopher Ferris458cc662017-08-28 16:31:18 -0700435 VerifyProcTest(pid, BACKTRACE_CURRENT_THREAD, ReadyLevelBacktrace, VerifyProcessIgnoreFrames,
436 Backtrace::Create, BacktraceMap::Create);
437
438 kill(pid, SIGKILL);
439 int status;
440 ASSERT_EQ(waitpid(pid, &status, 0), pid);
441}
442
443TEST(libbacktrace, ptrace_ignore_frames_new) {
444 pid_t pid;
445 if ((pid = fork()) == 0) {
446 ASSERT_NE(test_level_one(1, 2, 3, 4, nullptr, nullptr), 0);
447 _exit(1);
448 }
449 VerifyProcTest(pid, BACKTRACE_CURRENT_THREAD, ReadyLevelBacktrace, VerifyProcessIgnoreFrames,
450 Backtrace::CreateNew, BacktraceMap::CreateNew);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700451
452 kill(pid, SIGKILL);
453 int status;
454 ASSERT_EQ(waitpid(pid, &status, 0), pid);
455}
456
457// Create a process with multiple threads and dump all of the threads.
Christopher Ferris82f3bbd2017-03-14 15:22:26 -0700458static void* PtraceThreadLevelRun(void*) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700459 EXPECT_NE(test_level_one(1, 2, 3, 4, nullptr, nullptr), 0);
460 return nullptr;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700461}
462
Christopher Ferris82f3bbd2017-03-14 15:22:26 -0700463static void GetThreads(pid_t pid, std::vector<pid_t>* threads) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700464 // Get the list of tasks.
465 char task_path[128];
466 snprintf(task_path, sizeof(task_path), "/proc/%d/task", pid);
467
James Hawkins588a2ca2016-02-18 14:52:46 -0800468 std::unique_ptr<DIR, decltype(&closedir)> tasks_dir(opendir(task_path), closedir);
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700469 ASSERT_TRUE(tasks_dir != nullptr);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700470 struct dirent* entry;
James Hawkins588a2ca2016-02-18 14:52:46 -0800471 while ((entry = readdir(tasks_dir.get())) != nullptr) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700472 char* end;
473 pid_t tid = strtoul(entry->d_name, &end, 10);
474 if (*end == '\0') {
475 threads->push_back(tid);
476 }
477 }
Christopher Ferris17e91d42013-10-21 13:30:52 -0700478}
479
480TEST(libbacktrace, ptrace_threads) {
481 pid_t pid;
482 if ((pid = fork()) == 0) {
483 for (size_t i = 0; i < NUM_PTRACE_THREADS; i++) {
484 pthread_attr_t attr;
485 pthread_attr_init(&attr);
486 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
487
488 pthread_t thread;
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700489 ASSERT_TRUE(pthread_create(&thread, &attr, PtraceThreadLevelRun, nullptr) == 0);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700490 }
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700491 ASSERT_NE(test_level_one(1, 2, 3, 4, nullptr, nullptr), 0);
Christopher Ferrise2960912014-03-07 19:42:19 -0800492 _exit(1);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700493 }
494
495 // Check to see that all of the threads are running before unwinding.
496 std::vector<pid_t> threads;
497 uint64_t start = NanoTime();
498 do {
499 usleep(US_PER_MSEC);
500 threads.clear();
501 GetThreads(pid, &threads);
502 } while ((threads.size() != NUM_PTRACE_THREADS + 1) &&
503 ((NanoTime() - start) <= 5 * NS_PER_SEC));
504 ASSERT_EQ(threads.size(), static_cast<size_t>(NUM_PTRACE_THREADS + 1));
505
506 ASSERT_TRUE(ptrace(PTRACE_ATTACH, pid, 0, 0) == 0);
507 WaitForStop(pid);
508 for (std::vector<int>::const_iterator it = threads.begin(); it != threads.end(); ++it) {
509 // Skip the current forked process, we only care about the threads.
510 if (pid == *it) {
511 continue;
512 }
Christopher Ferris458cc662017-08-28 16:31:18 -0700513 VerifyProcTest(pid, *it, ReadyLevelBacktrace, VerifyLevelDump, Backtrace::Create,
514 BacktraceMap::Create);
515 }
516
517 FinishRemoteProcess(pid);
518}
519
520TEST(libbacktrace, ptrace_threads_new) {
521 pid_t pid;
522 if ((pid = fork()) == 0) {
523 for (size_t i = 0; i < NUM_PTRACE_THREADS; i++) {
524 pthread_attr_t attr;
525 pthread_attr_init(&attr);
526 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
527
528 pthread_t thread;
529 ASSERT_TRUE(pthread_create(&thread, &attr, PtraceThreadLevelRun, nullptr) == 0);
530 }
531 ASSERT_NE(test_level_one(1, 2, 3, 4, nullptr, nullptr), 0);
532 _exit(1);
533 }
534
535 // Check to see that all of the threads are running before unwinding.
536 std::vector<pid_t> threads;
537 uint64_t start = NanoTime();
538 do {
539 usleep(US_PER_MSEC);
540 threads.clear();
541 GetThreads(pid, &threads);
542 } while ((threads.size() != NUM_PTRACE_THREADS + 1) && ((NanoTime() - start) <= 5 * NS_PER_SEC));
543 ASSERT_EQ(threads.size(), static_cast<size_t>(NUM_PTRACE_THREADS + 1));
544
545 ASSERT_TRUE(ptrace(PTRACE_ATTACH, pid, 0, 0) == 0);
546 WaitForStop(pid);
547 for (std::vector<int>::const_iterator it = threads.begin(); it != threads.end(); ++it) {
548 // Skip the current forked process, we only care about the threads.
549 if (pid == *it) {
550 continue;
551 }
552 VerifyProcTest(pid, *it, ReadyLevelBacktrace, VerifyLevelDump, Backtrace::CreateNew,
553 BacktraceMap::CreateNew);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700554 }
Christopher Ferris17e91d42013-10-21 13:30:52 -0700555
Christopher Ferris82f3bbd2017-03-14 15:22:26 -0700556 FinishRemoteProcess(pid);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700557}
558
559void VerifyLevelThread(void*) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700560 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), gettid()));
561 ASSERT_TRUE(backtrace.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800562 ASSERT_TRUE(backtrace->Unwind(0));
Christopher Ferris206a3b92016-03-09 14:35:54 -0800563 ASSERT_EQ(BACKTRACE_UNWIND_NO_ERROR, backtrace->GetError());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700564
Christopher Ferris20303f82014-01-10 16:33:16 -0800565 VerifyLevelDump(backtrace.get());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700566}
567
568TEST(libbacktrace, thread_current_level) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700569 ASSERT_NE(test_level_one(1, 2, 3, 4, VerifyLevelThread, nullptr), 0);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700570}
571
Christopher Ferris82f3bbd2017-03-14 15:22:26 -0700572static void VerifyMaxThread(void*) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700573 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), gettid()));
574 ASSERT_TRUE(backtrace.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800575 ASSERT_TRUE(backtrace->Unwind(0));
Christopher Ferris206a3b92016-03-09 14:35:54 -0800576 ASSERT_EQ(BACKTRACE_UNWIND_NO_ERROR, backtrace->GetError());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700577
Christopher Ferris20303f82014-01-10 16:33:16 -0800578 VerifyMaxDump(backtrace.get());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700579}
580
581TEST(libbacktrace, thread_current_max) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700582 ASSERT_NE(test_recursive_call(MAX_BACKTRACE_FRAMES+10, VerifyMaxThread, nullptr), 0);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700583}
584
Christopher Ferris82f3bbd2017-03-14 15:22:26 -0700585static void* ThreadLevelRun(void* data) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700586 thread_t* thread = reinterpret_cast<thread_t*>(data);
587
588 thread->tid = gettid();
589 EXPECT_NE(test_level_one(1, 2, 3, 4, ThreadSetState, data), 0);
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700590 return nullptr;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700591}
592
593TEST(libbacktrace, thread_level_trace) {
594 pthread_attr_t attr;
595 pthread_attr_init(&attr);
596 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
597
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700598 thread_t thread_data = { 0, 0, 0, nullptr };
Christopher Ferris17e91d42013-10-21 13:30:52 -0700599 pthread_t thread;
600 ASSERT_TRUE(pthread_create(&thread, &attr, ThreadLevelRun, &thread_data) == 0);
601
602 // Wait up to 2 seconds for the tid to be set.
603 ASSERT_TRUE(WaitForNonZero(&thread_data.state, 2));
604
Christopher Ferrisaa63d9f2014-04-29 09:35:30 -0700605 // Make sure that the thread signal used is not visible when compiled for
606 // the target.
607#if !defined(__GLIBC__)
608 ASSERT_LT(THREAD_SIGNAL, SIGRTMIN);
609#endif
610
Christopher Ferris17e91d42013-10-21 13:30:52 -0700611 // Save the current signal action and make sure it is restored afterwards.
612 struct sigaction cur_action;
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700613 ASSERT_TRUE(sigaction(THREAD_SIGNAL, nullptr, &cur_action) == 0);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700614
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700615 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), thread_data.tid));
616 ASSERT_TRUE(backtrace.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800617 ASSERT_TRUE(backtrace->Unwind(0));
Christopher Ferris206a3b92016-03-09 14:35:54 -0800618 ASSERT_EQ(BACKTRACE_UNWIND_NO_ERROR, backtrace->GetError());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700619
Christopher Ferris20303f82014-01-10 16:33:16 -0800620 VerifyLevelDump(backtrace.get());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700621
622 // Tell the thread to exit its infinite loop.
623 android_atomic_acquire_store(0, &thread_data.state);
624
625 // Verify that the old action was restored.
626 struct sigaction new_action;
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700627 ASSERT_TRUE(sigaction(THREAD_SIGNAL, nullptr, &new_action) == 0);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700628 EXPECT_EQ(cur_action.sa_sigaction, new_action.sa_sigaction);
Christopher Ferris3cdbfdc2014-11-08 15:57:11 -0800629 // The SA_RESTORER flag gets set behind our back, so a direct comparison
630 // doesn't work unless we mask the value off. Mips doesn't have this
631 // flag, so skip this on that platform.
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700632#if defined(SA_RESTORER)
Christopher Ferris3cdbfdc2014-11-08 15:57:11 -0800633 cur_action.sa_flags &= ~SA_RESTORER;
634 new_action.sa_flags &= ~SA_RESTORER;
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700635#elif defined(__GLIBC__)
636 // Our host compiler doesn't appear to define this flag for some reason.
637 cur_action.sa_flags &= ~0x04000000;
638 new_action.sa_flags &= ~0x04000000;
Christopher Ferris3cdbfdc2014-11-08 15:57:11 -0800639#endif
Christopher Ferris17e91d42013-10-21 13:30:52 -0700640 EXPECT_EQ(cur_action.sa_flags, new_action.sa_flags);
641}
642
643TEST(libbacktrace, thread_ignore_frames) {
644 pthread_attr_t attr;
645 pthread_attr_init(&attr);
646 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
647
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700648 thread_t thread_data = { 0, 0, 0, nullptr };
Christopher Ferris17e91d42013-10-21 13:30:52 -0700649 pthread_t thread;
650 ASSERT_TRUE(pthread_create(&thread, &attr, ThreadLevelRun, &thread_data) == 0);
651
652 // Wait up to 2 seconds for the tid to be set.
653 ASSERT_TRUE(WaitForNonZero(&thread_data.state, 2));
654
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700655 std::unique_ptr<Backtrace> all(Backtrace::Create(getpid(), thread_data.tid));
656 ASSERT_TRUE(all.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800657 ASSERT_TRUE(all->Unwind(0));
Christopher Ferris206a3b92016-03-09 14:35:54 -0800658 ASSERT_EQ(BACKTRACE_UNWIND_NO_ERROR, all->GetError());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700659
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700660 std::unique_ptr<Backtrace> ign1(Backtrace::Create(getpid(), thread_data.tid));
661 ASSERT_TRUE(ign1.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800662 ASSERT_TRUE(ign1->Unwind(1));
Christopher Ferris206a3b92016-03-09 14:35:54 -0800663 ASSERT_EQ(BACKTRACE_UNWIND_NO_ERROR, ign1->GetError());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700664
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700665 std::unique_ptr<Backtrace> ign2(Backtrace::Create(getpid(), thread_data.tid));
666 ASSERT_TRUE(ign2.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800667 ASSERT_TRUE(ign2->Unwind(2));
Christopher Ferris206a3b92016-03-09 14:35:54 -0800668 ASSERT_EQ(BACKTRACE_UNWIND_NO_ERROR, ign2->GetError());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700669
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700670 VerifyIgnoreFrames(all.get(), ign1.get(), ign2.get(), nullptr);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700671
672 // Tell the thread to exit its infinite loop.
673 android_atomic_acquire_store(0, &thread_data.state);
674}
675
Christopher Ferris82f3bbd2017-03-14 15:22:26 -0700676static void* ThreadMaxRun(void* data) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700677 thread_t* thread = reinterpret_cast<thread_t*>(data);
678
679 thread->tid = gettid();
680 EXPECT_NE(test_recursive_call(MAX_BACKTRACE_FRAMES+10, ThreadSetState, data), 0);
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700681 return nullptr;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700682}
683
684TEST(libbacktrace, thread_max_trace) {
685 pthread_attr_t attr;
686 pthread_attr_init(&attr);
687 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
688
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700689 thread_t thread_data = { 0, 0, 0, nullptr };
Christopher Ferris17e91d42013-10-21 13:30:52 -0700690 pthread_t thread;
691 ASSERT_TRUE(pthread_create(&thread, &attr, ThreadMaxRun, &thread_data) == 0);
692
693 // Wait for the tid to be set.
694 ASSERT_TRUE(WaitForNonZero(&thread_data.state, 2));
695
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700696 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), thread_data.tid));
697 ASSERT_TRUE(backtrace.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800698 ASSERT_TRUE(backtrace->Unwind(0));
Christopher Ferris206a3b92016-03-09 14:35:54 -0800699 ASSERT_EQ(BACKTRACE_UNWIND_NO_ERROR, backtrace->GetError());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700700
Christopher Ferris20303f82014-01-10 16:33:16 -0800701 VerifyMaxDump(backtrace.get());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700702
703 // Tell the thread to exit its infinite loop.
704 android_atomic_acquire_store(0, &thread_data.state);
705}
706
Christopher Ferris82f3bbd2017-03-14 15:22:26 -0700707static void* ThreadDump(void* data) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700708 dump_thread_t* dump = reinterpret_cast<dump_thread_t*>(data);
709 while (true) {
710 if (android_atomic_acquire_load(dump->now)) {
711 break;
712 }
713 }
714
Christopher Ferris17e91d42013-10-21 13:30:52 -0700715 // The status of the actual unwind will be checked elsewhere.
Christopher Ferris20303f82014-01-10 16:33:16 -0800716 dump->backtrace = Backtrace::Create(getpid(), dump->thread.tid);
717 dump->backtrace->Unwind(0);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700718
719 android_atomic_acquire_store(1, &dump->done);
720
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700721 return nullptr;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700722}
723
724TEST(libbacktrace, thread_multiple_dump) {
725 // Dump NUM_THREADS simultaneously.
726 std::vector<thread_t> runners(NUM_THREADS);
727 std::vector<dump_thread_t> dumpers(NUM_THREADS);
728
729 pthread_attr_t attr;
730 pthread_attr_init(&attr);
731 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
732 for (size_t i = 0; i < NUM_THREADS; i++) {
733 // Launch the runners, they will spin in hard loops doing nothing.
734 runners[i].tid = 0;
735 runners[i].state = 0;
736 ASSERT_TRUE(pthread_create(&runners[i].threadId, &attr, ThreadMaxRun, &runners[i]) == 0);
737 }
738
739 // Wait for tids to be set.
740 for (std::vector<thread_t>::iterator it = runners.begin(); it != runners.end(); ++it) {
Christopher Ferris3cdbfdc2014-11-08 15:57:11 -0800741 ASSERT_TRUE(WaitForNonZero(&it->state, 30));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700742 }
743
744 // Start all of the dumpers at once, they will spin until they are signalled
745 // to begin their dump run.
746 int32_t dump_now = 0;
747 for (size_t i = 0; i < NUM_THREADS; i++) {
748 dumpers[i].thread.tid = runners[i].tid;
749 dumpers[i].thread.state = 0;
750 dumpers[i].done = 0;
751 dumpers[i].now = &dump_now;
752
753 ASSERT_TRUE(pthread_create(&dumpers[i].thread.threadId, &attr, ThreadDump, &dumpers[i]) == 0);
754 }
755
756 // Start all of the dumpers going at once.
757 android_atomic_acquire_store(1, &dump_now);
758
759 for (size_t i = 0; i < NUM_THREADS; i++) {
Christopher Ferris3cdbfdc2014-11-08 15:57:11 -0800760 ASSERT_TRUE(WaitForNonZero(&dumpers[i].done, 30));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700761
762 // Tell the runner thread to exit its infinite loop.
763 android_atomic_acquire_store(0, &runners[i].state);
764
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700765 ASSERT_TRUE(dumpers[i].backtrace != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800766 VerifyMaxDump(dumpers[i].backtrace);
767
768 delete dumpers[i].backtrace;
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700769 dumpers[i].backtrace = nullptr;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700770 }
771}
772
Christopher Ferrisa2efd3a2014-05-06 15:23:59 -0700773TEST(libbacktrace, thread_multiple_dump_same_thread) {
774 pthread_attr_t attr;
775 pthread_attr_init(&attr);
776 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
777 thread_t runner;
778 runner.tid = 0;
779 runner.state = 0;
780 ASSERT_TRUE(pthread_create(&runner.threadId, &attr, ThreadMaxRun, &runner) == 0);
781
782 // Wait for tids to be set.
Christopher Ferris3cdbfdc2014-11-08 15:57:11 -0800783 ASSERT_TRUE(WaitForNonZero(&runner.state, 30));
Christopher Ferrisa2efd3a2014-05-06 15:23:59 -0700784
785 // Start all of the dumpers at once, they will spin until they are signalled
786 // to begin their dump run.
787 int32_t dump_now = 0;
788 // Dump the same thread NUM_THREADS simultaneously.
789 std::vector<dump_thread_t> dumpers(NUM_THREADS);
790 for (size_t i = 0; i < NUM_THREADS; i++) {
791 dumpers[i].thread.tid = runner.tid;
792 dumpers[i].thread.state = 0;
793 dumpers[i].done = 0;
794 dumpers[i].now = &dump_now;
795
796 ASSERT_TRUE(pthread_create(&dumpers[i].thread.threadId, &attr, ThreadDump, &dumpers[i]) == 0);
797 }
798
799 // Start all of the dumpers going at once.
800 android_atomic_acquire_store(1, &dump_now);
801
802 for (size_t i = 0; i < NUM_THREADS; i++) {
Christopher Ferris3cdbfdc2014-11-08 15:57:11 -0800803 ASSERT_TRUE(WaitForNonZero(&dumpers[i].done, 30));
Christopher Ferrisa2efd3a2014-05-06 15:23:59 -0700804
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700805 ASSERT_TRUE(dumpers[i].backtrace != nullptr);
Christopher Ferrisa2efd3a2014-05-06 15:23:59 -0700806 VerifyMaxDump(dumpers[i].backtrace);
807
808 delete dumpers[i].backtrace;
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700809 dumpers[i].backtrace = nullptr;
Christopher Ferrisa2efd3a2014-05-06 15:23:59 -0700810 }
811
812 // Tell the runner thread to exit its infinite loop.
813 android_atomic_acquire_store(0, &runner.state);
814}
815
Christopher Ferrisdf290612014-01-22 19:21:07 -0800816// This test is for UnwindMaps that should share the same map cursor when
817// multiple maps are created for the current process at the same time.
818TEST(libbacktrace, simultaneous_maps) {
819 BacktraceMap* map1 = BacktraceMap::Create(getpid());
820 BacktraceMap* map2 = BacktraceMap::Create(getpid());
821 BacktraceMap* map3 = BacktraceMap::Create(getpid());
822
823 Backtrace* back1 = Backtrace::Create(getpid(), BACKTRACE_CURRENT_THREAD, map1);
Christopher Ferris97e00bb2015-04-02 14:22:31 -0700824 ASSERT_TRUE(back1 != nullptr);
Christopher Ferrisdf290612014-01-22 19:21:07 -0800825 EXPECT_TRUE(back1->Unwind(0));
Christopher Ferris206a3b92016-03-09 14:35:54 -0800826 ASSERT_EQ(BACKTRACE_UNWIND_NO_ERROR, back1->GetError());
Christopher Ferrisdf290612014-01-22 19:21:07 -0800827 delete back1;
828 delete map1;
829
830 Backtrace* back2 = Backtrace::Create(getpid(), BACKTRACE_CURRENT_THREAD, map2);
Christopher Ferris97e00bb2015-04-02 14:22:31 -0700831 ASSERT_TRUE(back2 != nullptr);
Christopher Ferrisdf290612014-01-22 19:21:07 -0800832 EXPECT_TRUE(back2->Unwind(0));
Christopher Ferris206a3b92016-03-09 14:35:54 -0800833 ASSERT_EQ(BACKTRACE_UNWIND_NO_ERROR, back2->GetError());
Christopher Ferrisdf290612014-01-22 19:21:07 -0800834 delete back2;
835 delete map2;
836
837 Backtrace* back3 = Backtrace::Create(getpid(), BACKTRACE_CURRENT_THREAD, map3);
Christopher Ferris97e00bb2015-04-02 14:22:31 -0700838 ASSERT_TRUE(back3 != nullptr);
Christopher Ferrisdf290612014-01-22 19:21:07 -0800839 EXPECT_TRUE(back3->Unwind(0));
Christopher Ferris206a3b92016-03-09 14:35:54 -0800840 ASSERT_EQ(BACKTRACE_UNWIND_NO_ERROR, back3->GetError());
Christopher Ferrisdf290612014-01-22 19:21:07 -0800841 delete back3;
842 delete map3;
843}
844
Christopher Ferris12385e32015-02-06 13:22:01 -0800845TEST(libbacktrace, fillin_erases) {
846 BacktraceMap* back_map = BacktraceMap::Create(getpid());
847
848 backtrace_map_t map;
849
850 map.start = 1;
851 map.end = 3;
852 map.flags = 1;
853 map.name = "Initialized";
854 back_map->FillIn(0, &map);
855 delete back_map;
856
857 ASSERT_FALSE(BacktraceMap::IsValid(map));
858 ASSERT_EQ(static_cast<uintptr_t>(0), map.start);
859 ASSERT_EQ(static_cast<uintptr_t>(0), map.end);
860 ASSERT_EQ(0, map.flags);
861 ASSERT_EQ("", map.name);
862}
863
Christopher Ferris17e91d42013-10-21 13:30:52 -0700864TEST(libbacktrace, format_test) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700865 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), BACKTRACE_CURRENT_THREAD));
866 ASSERT_TRUE(backtrace.get() != nullptr);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700867
Christopher Ferris20303f82014-01-10 16:33:16 -0800868 backtrace_frame_data_t frame;
Christopher Ferris46756822014-01-14 20:16:30 -0800869 frame.num = 1;
870 frame.pc = 2;
Christopher Ferris96722b02017-07-19 14:20:46 -0700871 frame.rel_pc = 2;
Christopher Ferris46756822014-01-14 20:16:30 -0800872 frame.sp = 0;
873 frame.stack_size = 0;
Christopher Ferris46756822014-01-14 20:16:30 -0800874 frame.func_offset = 0;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700875
Christopher Ferris46756822014-01-14 20:16:30 -0800876 // Check no map set.
Christopher Ferris20303f82014-01-10 16:33:16 -0800877 frame.num = 1;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700878#if defined(__LP64__)
Christopher Ferris46756822014-01-14 20:16:30 -0800879 EXPECT_EQ("#01 pc 0000000000000002 <unknown>",
Christopher Ferris17e91d42013-10-21 13:30:52 -0700880#else
Christopher Ferris46756822014-01-14 20:16:30 -0800881 EXPECT_EQ("#01 pc 00000002 <unknown>",
Christopher Ferris17e91d42013-10-21 13:30:52 -0700882#endif
Christopher Ferris46756822014-01-14 20:16:30 -0800883 backtrace->FormatFrameData(&frame));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700884
Christopher Ferris46756822014-01-14 20:16:30 -0800885 // Check map name empty, but exists.
Christopher Ferrisda750a72015-11-30 13:36:08 -0800886 frame.pc = 0xb0020;
Christopher Ferris96722b02017-07-19 14:20:46 -0700887 frame.rel_pc = 0x20;
Christopher Ferrisda750a72015-11-30 13:36:08 -0800888 frame.map.start = 0xb0000;
889 frame.map.end = 0xbffff;
Christopher Ferris96722b02017-07-19 14:20:46 -0700890 frame.map.load_bias = 0;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700891#if defined(__LP64__)
Christopher Ferrisda750a72015-11-30 13:36:08 -0800892 EXPECT_EQ("#01 pc 0000000000000020 <anonymous:00000000000b0000>",
Christopher Ferris17e91d42013-10-21 13:30:52 -0700893#else
Christopher Ferrisda750a72015-11-30 13:36:08 -0800894 EXPECT_EQ("#01 pc 00000020 <anonymous:000b0000>",
Christopher Ferris17e91d42013-10-21 13:30:52 -0700895#endif
Christopher Ferris46756822014-01-14 20:16:30 -0800896 backtrace->FormatFrameData(&frame));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700897
Christopher Ferrisda750a72015-11-30 13:36:08 -0800898 // Check map name begins with a [.
899 frame.pc = 0xc0020;
900 frame.map.start = 0xc0000;
901 frame.map.end = 0xcffff;
Christopher Ferris96722b02017-07-19 14:20:46 -0700902 frame.map.load_bias = 0;
Christopher Ferrisda750a72015-11-30 13:36:08 -0800903 frame.map.name = "[anon:thread signal stack]";
904#if defined(__LP64__)
905 EXPECT_EQ("#01 pc 0000000000000020 [anon:thread signal stack:00000000000c0000]",
906#else
907 EXPECT_EQ("#01 pc 00000020 [anon:thread signal stack:000c0000]",
908#endif
909 backtrace->FormatFrameData(&frame));
Christopher Ferris46756822014-01-14 20:16:30 -0800910
911 // Check relative pc is set and map name is set.
912 frame.pc = 0x12345679;
Christopher Ferris96722b02017-07-19 14:20:46 -0700913 frame.rel_pc = 0x12345678;
Christopher Ferris12385e32015-02-06 13:22:01 -0800914 frame.map.name = "MapFake";
915 frame.map.start = 1;
916 frame.map.end = 1;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700917#if defined(__LP64__)
Christopher Ferris46756822014-01-14 20:16:30 -0800918 EXPECT_EQ("#01 pc 0000000012345678 MapFake",
Christopher Ferris17e91d42013-10-21 13:30:52 -0700919#else
Christopher Ferris46756822014-01-14 20:16:30 -0800920 EXPECT_EQ("#01 pc 12345678 MapFake",
Christopher Ferris17e91d42013-10-21 13:30:52 -0700921#endif
Christopher Ferris46756822014-01-14 20:16:30 -0800922 backtrace->FormatFrameData(&frame));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700923
Christopher Ferris46756822014-01-14 20:16:30 -0800924 // Check func_name is set, but no func offset.
925 frame.func_name = "ProcFake";
926#if defined(__LP64__)
927 EXPECT_EQ("#01 pc 0000000012345678 MapFake (ProcFake)",
928#else
929 EXPECT_EQ("#01 pc 12345678 MapFake (ProcFake)",
930#endif
931 backtrace->FormatFrameData(&frame));
932
933 // Check func_name is set, and func offset is non-zero.
Christopher Ferris20303f82014-01-10 16:33:16 -0800934 frame.func_offset = 645;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700935#if defined(__LP64__)
Christopher Ferris46756822014-01-14 20:16:30 -0800936 EXPECT_EQ("#01 pc 0000000012345678 MapFake (ProcFake+645)",
Christopher Ferris17e91d42013-10-21 13:30:52 -0700937#else
Christopher Ferris46756822014-01-14 20:16:30 -0800938 EXPECT_EQ("#01 pc 12345678 MapFake (ProcFake+645)",
Christopher Ferris17e91d42013-10-21 13:30:52 -0700939#endif
Christopher Ferris46756822014-01-14 20:16:30 -0800940 backtrace->FormatFrameData(&frame));
Christopher Ferris2106f4b2015-05-01 15:02:03 -0700941
Christopher Ferris96722b02017-07-19 14:20:46 -0700942 // Check func_name is set, func offset is non-zero, and load_bias is non-zero.
943 frame.rel_pc = 0x123456dc;
Christopher Ferris2106f4b2015-05-01 15:02:03 -0700944 frame.func_offset = 645;
Christopher Ferris96722b02017-07-19 14:20:46 -0700945 frame.map.load_bias = 100;
Christopher Ferris2106f4b2015-05-01 15:02:03 -0700946#if defined(__LP64__)
947 EXPECT_EQ("#01 pc 00000000123456dc MapFake (ProcFake+645)",
948#else
949 EXPECT_EQ("#01 pc 123456dc MapFake (ProcFake+645)",
950#endif
951 backtrace->FormatFrameData(&frame));
Christopher Ferrise0ab2322015-08-20 11:16:54 -0700952
953 // Check a non-zero map offset.
954 frame.map.offset = 0x1000;
955#if defined(__LP64__)
956 EXPECT_EQ("#01 pc 00000000123456dc MapFake (offset 0x1000) (ProcFake+645)",
957#else
958 EXPECT_EQ("#01 pc 123456dc MapFake (offset 0x1000) (ProcFake+645)",
959#endif
960 backtrace->FormatFrameData(&frame));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700961}
Christopher Ferrise2960912014-03-07 19:42:19 -0800962
963struct map_test_t {
964 uintptr_t start;
965 uintptr_t end;
966};
967
Christopher Ferris82f3bbd2017-03-14 15:22:26 -0700968static bool map_sort(map_test_t i, map_test_t j) { return i.start < j.start; }
Christopher Ferrise2960912014-03-07 19:42:19 -0800969
Christopher Ferris82f3bbd2017-03-14 15:22:26 -0700970static void VerifyMap(pid_t pid) {
Christopher Ferrise2960912014-03-07 19:42:19 -0800971 char buffer[4096];
972 snprintf(buffer, sizeof(buffer), "/proc/%d/maps", pid);
973
974 FILE* map_file = fopen(buffer, "r");
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700975 ASSERT_TRUE(map_file != nullptr);
Christopher Ferrise2960912014-03-07 19:42:19 -0800976 std::vector<map_test_t> test_maps;
977 while (fgets(buffer, sizeof(buffer), map_file)) {
978 map_test_t map;
979 ASSERT_EQ(2, sscanf(buffer, "%" SCNxPTR "-%" SCNxPTR " ", &map.start, &map.end));
980 test_maps.push_back(map);
981 }
982 fclose(map_file);
983 std::sort(test_maps.begin(), test_maps.end(), map_sort);
984
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700985 std::unique_ptr<BacktraceMap> map(BacktraceMap::Create(pid));
Christopher Ferrise2960912014-03-07 19:42:19 -0800986
987 // Basic test that verifies that the map is in the expected order.
Christopher Ferris3a140042016-06-15 15:49:50 -0700988 ScopedBacktraceMapIteratorLock lock(map.get());
Christopher Ferrise2960912014-03-07 19:42:19 -0800989 std::vector<map_test_t>::const_iterator test_it = test_maps.begin();
990 for (BacktraceMap::const_iterator it = map->begin(); it != map->end(); ++it) {
991 ASSERT_TRUE(test_it != test_maps.end());
992 ASSERT_EQ(test_it->start, it->start);
993 ASSERT_EQ(test_it->end, it->end);
994 ++test_it;
995 }
996 ASSERT_TRUE(test_it == test_maps.end());
997}
998
999TEST(libbacktrace, verify_map_remote) {
1000 pid_t pid;
Christopher Ferris82f3bbd2017-03-14 15:22:26 -07001001 CreateRemoteProcess(&pid);
Christopher Ferrise2960912014-03-07 19:42:19 -08001002
1003 // The maps should match exactly since the forked process has been paused.
1004 VerifyMap(pid);
1005
Christopher Ferris82f3bbd2017-03-14 15:22:26 -07001006 FinishRemoteProcess(pid);
Christopher Ferris2b4a63f2015-03-17 14:42:03 -07001007}
1008
Christopher Ferris82f3bbd2017-03-14 15:22:26 -07001009static void InitMemory(uint8_t* memory, size_t bytes) {
Christopher Ferris944f4172015-05-06 16:36:34 -07001010 for (size_t i = 0; i < bytes; i++) {
1011 memory[i] = i;
1012 if (memory[i] == '\0') {
1013 // Don't use '\0' in our data so we can verify that an overread doesn't
1014 // occur by using a '\0' as the character after the read data.
1015 memory[i] = 23;
1016 }
1017 }
1018}
1019
Christopher Ferris82f3bbd2017-03-14 15:22:26 -07001020static void* ThreadReadTest(void* data) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -07001021 thread_t* thread_data = reinterpret_cast<thread_t*>(data);
1022
1023 thread_data->tid = gettid();
1024
1025 // Create two map pages.
1026 // Mark the second page as not-readable.
1027 size_t pagesize = static_cast<size_t>(sysconf(_SC_PAGE_SIZE));
1028 uint8_t* memory;
1029 if (posix_memalign(reinterpret_cast<void**>(&memory), pagesize, 2 * pagesize) != 0) {
1030 return reinterpret_cast<void*>(-1);
1031 }
1032
1033 if (mprotect(&memory[pagesize], pagesize, PROT_NONE) != 0) {
1034 return reinterpret_cast<void*>(-1);
1035 }
1036
1037 // Set up a simple pattern in memory.
Christopher Ferris944f4172015-05-06 16:36:34 -07001038 InitMemory(memory, pagesize);
Christopher Ferris2b4a63f2015-03-17 14:42:03 -07001039
1040 thread_data->data = memory;
1041
1042 // Tell the caller it's okay to start reading memory.
1043 android_atomic_acquire_store(1, &thread_data->state);
1044
Christopher Ferris2c43cff2015-03-26 19:18:36 -07001045 // Loop waiting for the caller to finish reading the memory.
Christopher Ferris2b4a63f2015-03-17 14:42:03 -07001046 while (thread_data->state) {
1047 }
1048
Christopher Ferris2c43cff2015-03-26 19:18:36 -07001049 // Re-enable read-write on the page so that we don't crash if we try
1050 // and access data on this page when freeing the memory.
1051 if (mprotect(&memory[pagesize], pagesize, PROT_READ | PROT_WRITE) != 0) {
1052 return reinterpret_cast<void*>(-1);
1053 }
Christopher Ferris2b4a63f2015-03-17 14:42:03 -07001054 free(memory);
1055
1056 android_atomic_acquire_store(1, &thread_data->state);
1057
1058 return nullptr;
1059}
1060
Christopher Ferris82f3bbd2017-03-14 15:22:26 -07001061static void RunReadTest(Backtrace* backtrace, uintptr_t read_addr) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -07001062 size_t pagesize = static_cast<size_t>(sysconf(_SC_PAGE_SIZE));
1063
1064 // Create a page of data to use to do quick compares.
1065 uint8_t* expected = new uint8_t[pagesize];
Christopher Ferris944f4172015-05-06 16:36:34 -07001066 InitMemory(expected, pagesize);
1067
Christopher Ferris2b4a63f2015-03-17 14:42:03 -07001068 uint8_t* data = new uint8_t[2*pagesize];
1069 // Verify that we can only read one page worth of data.
1070 size_t bytes_read = backtrace->Read(read_addr, data, 2 * pagesize);
1071 ASSERT_EQ(pagesize, bytes_read);
1072 ASSERT_TRUE(memcmp(data, expected, pagesize) == 0);
1073
1074 // Verify unaligned reads.
1075 for (size_t i = 1; i < sizeof(word_t); i++) {
1076 bytes_read = backtrace->Read(read_addr + i, data, 2 * sizeof(word_t));
1077 ASSERT_EQ(2 * sizeof(word_t), bytes_read);
1078 ASSERT_TRUE(memcmp(data, &expected[i], 2 * sizeof(word_t)) == 0)
1079 << "Offset at " << i << " failed";
1080 }
Christopher Ferris944f4172015-05-06 16:36:34 -07001081
1082 // Verify small unaligned reads.
1083 for (size_t i = 1; i < sizeof(word_t); i++) {
1084 for (size_t j = 1; j < sizeof(word_t); j++) {
1085 // Set one byte past what we expect to read, to guarantee we don't overread.
1086 data[j] = '\0';
1087 bytes_read = backtrace->Read(read_addr + i, data, j);
1088 ASSERT_EQ(j, bytes_read);
1089 ASSERT_TRUE(memcmp(data, &expected[i], j) == 0)
1090 << "Offset at " << i << " length " << j << " miscompared";
1091 ASSERT_EQ('\0', data[j])
1092 << "Offset at " << i << " length " << j << " wrote too much data";
1093 }
1094 }
Pirama Arumuga Nainar837eff22015-07-09 10:50:04 -07001095 delete[] data;
1096 delete[] expected;
Christopher Ferris2b4a63f2015-03-17 14:42:03 -07001097}
1098
1099TEST(libbacktrace, thread_read) {
1100 pthread_attr_t attr;
1101 pthread_attr_init(&attr);
1102 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
1103 pthread_t thread;
1104 thread_t thread_data = { 0, 0, 0, nullptr };
1105 ASSERT_TRUE(pthread_create(&thread, &attr, ThreadReadTest, &thread_data) == 0);
1106
1107 ASSERT_TRUE(WaitForNonZero(&thread_data.state, 10));
1108
1109 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), thread_data.tid));
1110 ASSERT_TRUE(backtrace.get() != nullptr);
1111
1112 RunReadTest(backtrace.get(), reinterpret_cast<uintptr_t>(thread_data.data));
1113
1114 android_atomic_acquire_store(0, &thread_data.state);
1115
1116 ASSERT_TRUE(WaitForNonZero(&thread_data.state, 10));
1117}
1118
1119volatile uintptr_t g_ready = 0;
1120volatile uintptr_t g_addr = 0;
1121
Christopher Ferris82f3bbd2017-03-14 15:22:26 -07001122static void ForkedReadTest() {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -07001123 // Create two map pages.
1124 size_t pagesize = static_cast<size_t>(sysconf(_SC_PAGE_SIZE));
1125 uint8_t* memory;
1126 if (posix_memalign(reinterpret_cast<void**>(&memory), pagesize, 2 * pagesize) != 0) {
1127 perror("Failed to allocate memory\n");
1128 exit(1);
1129 }
1130
1131 // Mark the second page as not-readable.
1132 if (mprotect(&memory[pagesize], pagesize, PROT_NONE) != 0) {
1133 perror("Failed to mprotect memory\n");
1134 exit(1);
1135 }
1136
1137 // Set up a simple pattern in memory.
Christopher Ferris944f4172015-05-06 16:36:34 -07001138 InitMemory(memory, pagesize);
Christopher Ferris2b4a63f2015-03-17 14:42:03 -07001139
1140 g_addr = reinterpret_cast<uintptr_t>(memory);
1141 g_ready = 1;
1142
1143 while (1) {
1144 usleep(US_PER_MSEC);
1145 }
1146}
1147
1148TEST(libbacktrace, process_read) {
Christopher Ferris67aba682015-05-08 15:44:46 -07001149 g_ready = 0;
Christopher Ferris2b4a63f2015-03-17 14:42:03 -07001150 pid_t pid;
1151 if ((pid = fork()) == 0) {
1152 ForkedReadTest();
1153 exit(0);
1154 }
1155 ASSERT_NE(-1, pid);
1156
1157 bool test_executed = false;
1158 uint64_t start = NanoTime();
1159 while (1) {
1160 if (ptrace(PTRACE_ATTACH, pid, 0, 0) == 0) {
1161 WaitForStop(pid);
1162
1163 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(pid, pid));
Christopher Ferris97e00bb2015-04-02 14:22:31 -07001164 ASSERT_TRUE(backtrace.get() != nullptr);
Christopher Ferris2b4a63f2015-03-17 14:42:03 -07001165
1166 uintptr_t read_addr;
1167 size_t bytes_read = backtrace->Read(reinterpret_cast<uintptr_t>(&g_ready),
1168 reinterpret_cast<uint8_t*>(&read_addr),
1169 sizeof(uintptr_t));
1170 ASSERT_EQ(sizeof(uintptr_t), bytes_read);
1171 if (read_addr) {
1172 // The forked process is ready to be read.
1173 bytes_read = backtrace->Read(reinterpret_cast<uintptr_t>(&g_addr),
1174 reinterpret_cast<uint8_t*>(&read_addr),
1175 sizeof(uintptr_t));
1176 ASSERT_EQ(sizeof(uintptr_t), bytes_read);
1177
1178 RunReadTest(backtrace.get(), read_addr);
1179
1180 test_executed = true;
1181 break;
1182 }
1183 ASSERT_TRUE(ptrace(PTRACE_DETACH, pid, 0, 0) == 0);
1184 }
1185 if ((NanoTime() - start) > 5 * NS_PER_SEC) {
1186 break;
1187 }
1188 usleep(US_PER_MSEC);
1189 }
1190 kill(pid, SIGKILL);
1191 ASSERT_EQ(waitpid(pid, nullptr, 0), pid);
1192
1193 ASSERT_TRUE(test_executed);
Christopher Ferrise2960912014-03-07 19:42:19 -08001194}
1195
Christopher Ferris82f3bbd2017-03-14 15:22:26 -07001196static void VerifyFunctionsFound(const std::vector<std::string>& found_functions) {
Christopher Ferris67aba682015-05-08 15:44:46 -07001197 // We expect to find these functions in libbacktrace_test. If we don't
1198 // find them, that's a bug in the memory read handling code in libunwind.
1199 std::list<std::string> expected_functions;
1200 expected_functions.push_back("test_recursive_call");
1201 expected_functions.push_back("test_level_one");
1202 expected_functions.push_back("test_level_two");
1203 expected_functions.push_back("test_level_three");
1204 expected_functions.push_back("test_level_four");
1205 for (const auto& found_function : found_functions) {
1206 for (const auto& expected_function : expected_functions) {
1207 if (found_function == expected_function) {
1208 expected_functions.remove(found_function);
1209 break;
1210 }
1211 }
1212 }
1213 ASSERT_TRUE(expected_functions.empty()) << "Not all functions found in shared library.";
1214}
1215
Christopher Ferris82f3bbd2017-03-14 15:22:26 -07001216static const char* CopySharedLibrary() {
Christopher Ferris67aba682015-05-08 15:44:46 -07001217#if defined(__LP64__)
1218 const char* lib_name = "lib64";
1219#else
1220 const char* lib_name = "lib";
1221#endif
1222
1223#if defined(__BIONIC__)
1224 const char* tmp_so_name = "/data/local/tmp/libbacktrace_test.so";
1225 std::string cp_cmd = android::base::StringPrintf("cp /system/%s/libbacktrace_test.so %s",
1226 lib_name, tmp_so_name);
1227#else
1228 const char* tmp_so_name = "/tmp/libbacktrace_test.so";
1229 if (getenv("ANDROID_HOST_OUT") == NULL) {
1230 fprintf(stderr, "ANDROID_HOST_OUT not set, make sure you run lunch.");
1231 return nullptr;
1232 }
1233 std::string cp_cmd = android::base::StringPrintf("cp %s/%s/libbacktrace_test.so %s",
1234 getenv("ANDROID_HOST_OUT"), lib_name,
1235 tmp_so_name);
1236#endif
1237
1238 // Copy the shared so to a tempory directory.
1239 system(cp_cmd.c_str());
1240
1241 return tmp_so_name;
1242}
1243
1244TEST(libbacktrace, check_unreadable_elf_local) {
1245 const char* tmp_so_name = CopySharedLibrary();
1246 ASSERT_TRUE(tmp_so_name != nullptr);
1247
1248 struct stat buf;
1249 ASSERT_TRUE(stat(tmp_so_name, &buf) != -1);
1250 uintptr_t map_size = buf.st_size;
1251
1252 int fd = open(tmp_so_name, O_RDONLY);
1253 ASSERT_TRUE(fd != -1);
1254
Christopher Ferris61c48ac2016-01-15 16:08:58 -08001255 void* map = mmap(NULL, map_size, PROT_READ | PROT_EXEC, MAP_PRIVATE, fd, 0);
Christopher Ferris67aba682015-05-08 15:44:46 -07001256 ASSERT_TRUE(map != MAP_FAILED);
1257 close(fd);
1258 ASSERT_TRUE(unlink(tmp_so_name) != -1);
1259
1260 std::vector<std::string> found_functions;
1261 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(BACKTRACE_CURRENT_PROCESS,
1262 BACKTRACE_CURRENT_THREAD));
1263 ASSERT_TRUE(backtrace.get() != nullptr);
1264
1265 // Needed before GetFunctionName will work.
1266 backtrace->Unwind(0);
1267
1268 // Loop through the entire map, and get every function we can find.
1269 map_size += reinterpret_cast<uintptr_t>(map);
1270 std::string last_func;
1271 for (uintptr_t read_addr = reinterpret_cast<uintptr_t>(map);
1272 read_addr < map_size; read_addr += 4) {
1273 uintptr_t offset;
1274 std::string func_name = backtrace->GetFunctionName(read_addr, &offset);
1275 if (!func_name.empty() && last_func != func_name) {
1276 found_functions.push_back(func_name);
1277 }
1278 last_func = func_name;
1279 }
1280
1281 ASSERT_TRUE(munmap(map, map_size - reinterpret_cast<uintptr_t>(map)) == 0);
1282
1283 VerifyFunctionsFound(found_functions);
1284}
1285
1286TEST(libbacktrace, check_unreadable_elf_remote) {
1287 const char* tmp_so_name = CopySharedLibrary();
1288 ASSERT_TRUE(tmp_so_name != nullptr);
1289
1290 g_ready = 0;
1291
1292 struct stat buf;
1293 ASSERT_TRUE(stat(tmp_so_name, &buf) != -1);
1294 uintptr_t map_size = buf.st_size;
1295
1296 pid_t pid;
1297 if ((pid = fork()) == 0) {
1298 int fd = open(tmp_so_name, O_RDONLY);
1299 if (fd == -1) {
1300 fprintf(stderr, "Failed to open file %s: %s\n", tmp_so_name, strerror(errno));
1301 unlink(tmp_so_name);
1302 exit(0);
1303 }
1304
Christopher Ferris61c48ac2016-01-15 16:08:58 -08001305 void* map = mmap(NULL, map_size, PROT_READ | PROT_EXEC, MAP_PRIVATE, fd, 0);
Christopher Ferris67aba682015-05-08 15:44:46 -07001306 if (map == MAP_FAILED) {
1307 fprintf(stderr, "Failed to map in memory: %s\n", strerror(errno));
1308 unlink(tmp_so_name);
1309 exit(0);
1310 }
1311 close(fd);
1312 if (unlink(tmp_so_name) == -1) {
1313 fprintf(stderr, "Failed to unlink: %s\n", strerror(errno));
1314 exit(0);
1315 }
1316
1317 g_addr = reinterpret_cast<uintptr_t>(map);
1318 g_ready = 1;
1319 while (true) {
1320 usleep(US_PER_MSEC);
1321 }
1322 exit(0);
1323 }
1324 ASSERT_TRUE(pid > 0);
1325
1326 std::vector<std::string> found_functions;
1327 uint64_t start = NanoTime();
1328 while (true) {
1329 ASSERT_TRUE(ptrace(PTRACE_ATTACH, pid, 0, 0) == 0);
1330
1331 // Wait for the process to get to a stopping point.
1332 WaitForStop(pid);
1333
1334 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(pid, BACKTRACE_CURRENT_THREAD));
1335 ASSERT_TRUE(backtrace.get() != nullptr);
1336
1337 uintptr_t read_addr;
1338 ASSERT_EQ(sizeof(uintptr_t), backtrace->Read(reinterpret_cast<uintptr_t>(&g_ready), reinterpret_cast<uint8_t*>(&read_addr), sizeof(uintptr_t)));
1339 if (read_addr) {
1340 ASSERT_EQ(sizeof(uintptr_t), backtrace->Read(reinterpret_cast<uintptr_t>(&g_addr), reinterpret_cast<uint8_t*>(&read_addr), sizeof(uintptr_t)));
1341
1342 // Needed before GetFunctionName will work.
1343 backtrace->Unwind(0);
1344
1345 // Loop through the entire map, and get every function we can find.
1346 map_size += read_addr;
1347 std::string last_func;
1348 for (; read_addr < map_size; read_addr += 4) {
1349 uintptr_t offset;
1350 std::string func_name = backtrace->GetFunctionName(read_addr, &offset);
1351 if (!func_name.empty() && last_func != func_name) {
1352 found_functions.push_back(func_name);
1353 }
1354 last_func = func_name;
1355 }
1356 break;
1357 }
1358 ASSERT_TRUE(ptrace(PTRACE_DETACH, pid, 0, 0) == 0);
1359
1360 if ((NanoTime() - start) > 5 * NS_PER_SEC) {
1361 break;
1362 }
1363 usleep(US_PER_MSEC);
1364 }
1365
1366 kill(pid, SIGKILL);
1367 ASSERT_EQ(waitpid(pid, nullptr, 0), pid);
1368
1369 VerifyFunctionsFound(found_functions);
1370}
1371
Christopher Ferris82f3bbd2017-03-14 15:22:26 -07001372static bool FindFuncFrameInBacktrace(Backtrace* backtrace, uintptr_t test_func, size_t* frame_num) {
Christopher Ferris67aba682015-05-08 15:44:46 -07001373 backtrace_map_t map;
1374 backtrace->FillInMap(test_func, &map);
1375 if (!BacktraceMap::IsValid(map)) {
1376 return false;
1377 }
1378
1379 // Loop through the frames, and find the one that is in the map.
1380 *frame_num = 0;
1381 for (Backtrace::const_iterator it = backtrace->begin(); it != backtrace->end(); ++it) {
1382 if (BacktraceMap::IsValid(it->map) && map.start == it->map.start &&
1383 it->pc >= test_func) {
1384 *frame_num = it->num;
1385 return true;
1386 }
1387 }
1388 return false;
1389}
1390
Christopher Ferris82f3bbd2017-03-14 15:22:26 -07001391static void VerifyUnreadableElfFrame(Backtrace* backtrace, uintptr_t test_func, size_t frame_num) {
Christopher Ferris67aba682015-05-08 15:44:46 -07001392 ASSERT_LT(backtrace->NumFrames(), static_cast<size_t>(MAX_BACKTRACE_FRAMES))
1393 << DumpFrames(backtrace);
1394
1395 ASSERT_TRUE(frame_num != 0) << DumpFrames(backtrace);
1396 // Make sure that there is at least one more frame above the test func call.
1397 ASSERT_LT(frame_num, backtrace->NumFrames()) << DumpFrames(backtrace);
1398
1399 uintptr_t diff = backtrace->GetFrame(frame_num)->pc - test_func;
1400 ASSERT_LT(diff, 200U) << DumpFrames(backtrace);
1401}
1402
Christopher Ferris82f3bbd2017-03-14 15:22:26 -07001403static void VerifyUnreadableElfBacktrace(uintptr_t test_func) {
Christopher Ferris67aba682015-05-08 15:44:46 -07001404 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(BACKTRACE_CURRENT_PROCESS,
1405 BACKTRACE_CURRENT_THREAD));
1406 ASSERT_TRUE(backtrace.get() != nullptr);
1407 ASSERT_TRUE(backtrace->Unwind(0));
Christopher Ferris206a3b92016-03-09 14:35:54 -08001408 ASSERT_EQ(BACKTRACE_UNWIND_NO_ERROR, backtrace->GetError());
Christopher Ferris67aba682015-05-08 15:44:46 -07001409
1410 size_t frame_num;
1411 ASSERT_TRUE(FindFuncFrameInBacktrace(backtrace.get(), test_func, &frame_num));
1412
1413 VerifyUnreadableElfFrame(backtrace.get(), test_func, frame_num);
1414}
1415
1416typedef int (*test_func_t)(int, int, int, int, void (*)(uintptr_t), uintptr_t);
1417
1418TEST(libbacktrace, unwind_through_unreadable_elf_local) {
1419 const char* tmp_so_name = CopySharedLibrary();
1420 ASSERT_TRUE(tmp_so_name != nullptr);
1421 void* lib_handle = dlopen(tmp_so_name, RTLD_NOW);
1422 ASSERT_TRUE(lib_handle != nullptr);
1423 ASSERT_TRUE(unlink(tmp_so_name) != -1);
1424
1425 test_func_t test_func;
1426 test_func = reinterpret_cast<test_func_t>(dlsym(lib_handle, "test_level_one"));
1427 ASSERT_TRUE(test_func != nullptr);
1428
1429 ASSERT_NE(test_func(1, 2, 3, 4, VerifyUnreadableElfBacktrace,
1430 reinterpret_cast<uintptr_t>(test_func)), 0);
1431
1432 ASSERT_TRUE(dlclose(lib_handle) == 0);
1433}
1434
1435TEST(libbacktrace, unwind_through_unreadable_elf_remote) {
1436 const char* tmp_so_name = CopySharedLibrary();
1437 ASSERT_TRUE(tmp_so_name != nullptr);
1438 void* lib_handle = dlopen(tmp_so_name, RTLD_NOW);
1439 ASSERT_TRUE(lib_handle != nullptr);
1440 ASSERT_TRUE(unlink(tmp_so_name) != -1);
1441
1442 test_func_t test_func;
1443 test_func = reinterpret_cast<test_func_t>(dlsym(lib_handle, "test_level_one"));
1444 ASSERT_TRUE(test_func != nullptr);
1445
1446 pid_t pid;
1447 if ((pid = fork()) == 0) {
1448 test_func(1, 2, 3, 4, 0, 0);
1449 exit(0);
1450 }
1451 ASSERT_TRUE(pid > 0);
1452 ASSERT_TRUE(dlclose(lib_handle) == 0);
1453
1454 uint64_t start = NanoTime();
1455 bool done = false;
1456 while (!done) {
1457 ASSERT_TRUE(ptrace(PTRACE_ATTACH, pid, 0, 0) == 0);
1458
1459 // Wait for the process to get to a stopping point.
1460 WaitForStop(pid);
1461
1462 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(pid, BACKTRACE_CURRENT_THREAD));
1463 ASSERT_TRUE(backtrace.get() != nullptr);
1464 ASSERT_TRUE(backtrace->Unwind(0));
Christopher Ferris206a3b92016-03-09 14:35:54 -08001465 ASSERT_EQ(BACKTRACE_UNWIND_NO_ERROR, backtrace->GetError());
Christopher Ferris67aba682015-05-08 15:44:46 -07001466
1467 size_t frame_num;
1468 if (FindFuncFrameInBacktrace(backtrace.get(),
1469 reinterpret_cast<uintptr_t>(test_func), &frame_num)) {
1470
1471 VerifyUnreadableElfFrame(backtrace.get(), reinterpret_cast<uintptr_t>(test_func), frame_num);
1472 done = true;
1473 }
1474
1475 ASSERT_TRUE(ptrace(PTRACE_DETACH, pid, 0, 0) == 0);
1476
1477 if ((NanoTime() - start) > 5 * NS_PER_SEC) {
1478 break;
1479 }
1480 usleep(US_PER_MSEC);
1481 }
1482
1483 kill(pid, SIGKILL);
1484 ASSERT_EQ(waitpid(pid, nullptr, 0), pid);
1485
1486 ASSERT_TRUE(done) << "Test function never found in unwind.";
1487}
1488
Christopher Ferris206a3b92016-03-09 14:35:54 -08001489TEST(libbacktrace, unwind_thread_doesnt_exist) {
1490 std::unique_ptr<Backtrace> backtrace(
1491 Backtrace::Create(BACKTRACE_CURRENT_PROCESS, 99999999));
1492 ASSERT_TRUE(backtrace.get() != nullptr);
1493 ASSERT_FALSE(backtrace->Unwind(0));
1494 ASSERT_EQ(BACKTRACE_UNWIND_ERROR_THREAD_DOESNT_EXIST, backtrace->GetError());
1495}
1496
Christopher Ferris82f3bbd2017-03-14 15:22:26 -07001497TEST(libbacktrace, local_get_function_name_before_unwind) {
1498 std::unique_ptr<Backtrace> backtrace(
1499 Backtrace::Create(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD));
1500 ASSERT_TRUE(backtrace.get() != nullptr);
1501
1502 // Verify that trying to get a function name before doing an unwind works.
1503 uintptr_t cur_func_offset = reinterpret_cast<uintptr_t>(&test_level_one) + 1;
1504 size_t offset;
1505 ASSERT_NE(std::string(""), backtrace->GetFunctionName(cur_func_offset, &offset));
1506}
1507
1508TEST(libbacktrace, remote_get_function_name_before_unwind) {
1509 pid_t pid;
1510 CreateRemoteProcess(&pid);
1511
1512 // Now create an unwind object.
1513 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(pid, pid));
1514
1515 // Verify that trying to get a function name before doing an unwind works.
1516 uintptr_t cur_func_offset = reinterpret_cast<uintptr_t>(&test_level_one) + 1;
1517 size_t offset;
1518 ASSERT_NE(std::string(""), backtrace->GetFunctionName(cur_func_offset, &offset));
1519
1520 FinishRemoteProcess(pid);
1521}
1522
Christopher Ferrisf5e568e2017-03-22 13:18:31 -07001523static void SetUcontextSp(uintptr_t sp, ucontext_t* ucontext) {
1524#if defined(__arm__)
1525 ucontext->uc_mcontext.arm_sp = sp;
1526#elif defined(__aarch64__)
1527 ucontext->uc_mcontext.sp = sp;
1528#elif defined(__i386__)
1529 ucontext->uc_mcontext.gregs[REG_ESP] = sp;
1530#elif defined(__x86_64__)
1531 ucontext->uc_mcontext.gregs[REG_RSP] = sp;
1532#else
1533 UNUSED(sp);
1534 UNUSED(ucontext);
1535 ASSERT_TRUE(false) << "Unsupported architecture";
1536#endif
1537}
1538
1539static void SetUcontextPc(uintptr_t pc, ucontext_t* ucontext) {
1540#if defined(__arm__)
1541 ucontext->uc_mcontext.arm_pc = pc;
1542#elif defined(__aarch64__)
1543 ucontext->uc_mcontext.pc = pc;
1544#elif defined(__i386__)
1545 ucontext->uc_mcontext.gregs[REG_EIP] = pc;
1546#elif defined(__x86_64__)
1547 ucontext->uc_mcontext.gregs[REG_RIP] = pc;
1548#else
1549 UNUSED(pc);
1550 UNUSED(ucontext);
1551 ASSERT_TRUE(false) << "Unsupported architecture";
1552#endif
1553}
1554
1555static void SetUcontextLr(uintptr_t lr, ucontext_t* ucontext) {
1556#if defined(__arm__)
1557 ucontext->uc_mcontext.arm_lr = lr;
1558#elif defined(__aarch64__)
1559 ucontext->uc_mcontext.regs[30] = lr;
1560#elif defined(__i386__)
1561 // The lr is on the stack.
1562 ASSERT_TRUE(lr != 0);
1563 ASSERT_TRUE(ucontext != nullptr);
1564#elif defined(__x86_64__)
1565 // The lr is on the stack.
1566 ASSERT_TRUE(lr != 0);
1567 ASSERT_TRUE(ucontext != nullptr);
1568#else
1569 UNUSED(lr);
1570 UNUSED(ucontext);
1571 ASSERT_TRUE(false) << "Unsupported architecture";
1572#endif
1573}
1574
1575static constexpr size_t DEVICE_MAP_SIZE = 1024;
1576
1577static void SetupDeviceMap(void** device_map) {
1578 // Make sure that anything in a device map will result in fails
1579 // to read.
1580 android::base::unique_fd device_fd(open("/dev/zero", O_RDONLY | O_CLOEXEC));
1581
1582 *device_map = mmap(nullptr, 1024, PROT_READ, MAP_PRIVATE, device_fd, 0);
1583 ASSERT_TRUE(*device_map != MAP_FAILED);
1584
1585 // Make sure the map is readable.
1586 ASSERT_EQ(0, reinterpret_cast<int*>(*device_map)[0]);
1587}
1588
1589static void UnwindFromDevice(Backtrace* backtrace, void* device_map) {
1590 uintptr_t device_map_uint = reinterpret_cast<uintptr_t>(device_map);
1591
1592 backtrace_map_t map;
1593 backtrace->FillInMap(device_map_uint, &map);
1594 // Verify the flag is set.
1595 ASSERT_EQ(PROT_DEVICE_MAP, map.flags & PROT_DEVICE_MAP);
1596
1597 // Quick sanity checks.
1598 size_t offset;
1599 ASSERT_EQ(std::string(""), backtrace->GetFunctionName(device_map_uint, &offset));
1600 ASSERT_EQ(std::string(""), backtrace->GetFunctionName(device_map_uint, &offset, &map));
1601 ASSERT_EQ(std::string(""), backtrace->GetFunctionName(0, &offset));
1602
1603 uintptr_t cur_func_offset = reinterpret_cast<uintptr_t>(&test_level_one) + 1;
1604 // Now verify the device map flag actually causes the function name to be empty.
1605 backtrace->FillInMap(cur_func_offset, &map);
1606 ASSERT_TRUE((map.flags & PROT_DEVICE_MAP) == 0);
1607 ASSERT_NE(std::string(""), backtrace->GetFunctionName(cur_func_offset, &offset, &map));
1608 map.flags |= PROT_DEVICE_MAP;
1609 ASSERT_EQ(std::string(""), backtrace->GetFunctionName(cur_func_offset, &offset, &map));
1610
1611 ucontext_t ucontext;
1612
1613 // Create a context that has the pc in the device map, but the sp
1614 // in a non-device map.
1615 memset(&ucontext, 0, sizeof(ucontext));
1616 SetUcontextSp(reinterpret_cast<uintptr_t>(&ucontext), &ucontext);
1617 SetUcontextPc(device_map_uint, &ucontext);
1618 SetUcontextLr(cur_func_offset, &ucontext);
1619
1620 ASSERT_TRUE(backtrace->Unwind(0, &ucontext));
1621
1622 // The buffer should only be a single element.
1623 ASSERT_EQ(1U, backtrace->NumFrames());
1624 const backtrace_frame_data_t* frame = backtrace->GetFrame(0);
1625 ASSERT_EQ(device_map_uint, frame->pc);
1626 ASSERT_EQ(reinterpret_cast<uintptr_t>(&ucontext), frame->sp);
1627
1628 // Check what happens when skipping the first frame.
1629 ASSERT_TRUE(backtrace->Unwind(1, &ucontext));
1630 ASSERT_EQ(0U, backtrace->NumFrames());
1631
1632 // Create a context that has the sp in the device map, but the pc
1633 // in a non-device map.
1634 memset(&ucontext, 0, sizeof(ucontext));
1635 SetUcontextSp(device_map_uint, &ucontext);
1636 SetUcontextPc(cur_func_offset, &ucontext);
1637 SetUcontextLr(cur_func_offset, &ucontext);
1638
1639 ASSERT_TRUE(backtrace->Unwind(0, &ucontext));
1640
1641 // The buffer should only be a single element.
1642 ASSERT_EQ(1U, backtrace->NumFrames());
1643 frame = backtrace->GetFrame(0);
1644 ASSERT_EQ(cur_func_offset, frame->pc);
1645 ASSERT_EQ(device_map_uint, frame->sp);
1646
1647 // Check what happens when skipping the first frame.
1648 ASSERT_TRUE(backtrace->Unwind(1, &ucontext));
1649 ASSERT_EQ(0U, backtrace->NumFrames());
1650}
1651
1652TEST(libbacktrace, unwind_disallow_device_map_local) {
1653 void* device_map;
1654 SetupDeviceMap(&device_map);
1655
1656 // Now create an unwind object.
1657 std::unique_ptr<Backtrace> backtrace(
1658 Backtrace::Create(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD));
1659 ASSERT_TRUE(backtrace);
1660
1661 UnwindFromDevice(backtrace.get(), device_map);
1662
1663 munmap(device_map, DEVICE_MAP_SIZE);
1664}
1665
Christopher Ferris458cc662017-08-28 16:31:18 -07001666TEST(libbacktrace, unwind_disallow_device_map_remote_new) {
Christopher Ferrisf5e568e2017-03-22 13:18:31 -07001667 void* device_map;
1668 SetupDeviceMap(&device_map);
1669
1670 // Fork a process to do a remote backtrace.
1671 pid_t pid;
1672 CreateRemoteProcess(&pid);
1673
1674 // Now create an unwind object.
Christopher Ferris458cc662017-08-28 16:31:18 -07001675 std::unique_ptr<BacktraceMap> map(BacktraceMap::CreateNew(pid));
1676 ASSERT_TRUE(map.get() != nullptr);
1677 std::unique_ptr<Backtrace> backtrace(Backtrace::CreateNew(pid, pid, map.get()));
Christopher Ferrisf5e568e2017-03-22 13:18:31 -07001678
Christopher Ferris458cc662017-08-28 16:31:18 -07001679 UnwindFromDevice(backtrace.get(), device_map);
Christopher Ferrisf5e568e2017-03-22 13:18:31 -07001680
1681 FinishRemoteProcess(pid);
1682
1683 munmap(device_map, DEVICE_MAP_SIZE);
1684}
1685
Christopher Ferris5ea2c1f2017-03-23 14:55:01 -07001686class ScopedSignalHandler {
1687 public:
1688 ScopedSignalHandler(int signal_number, void (*handler)(int)) : signal_number_(signal_number) {
1689 memset(&action_, 0, sizeof(action_));
1690 action_.sa_handler = handler;
1691 sigaction(signal_number_, &action_, &old_action_);
1692 }
1693
1694 ScopedSignalHandler(int signal_number, void (*action)(int, siginfo_t*, void*))
1695 : signal_number_(signal_number) {
1696 memset(&action_, 0, sizeof(action_));
1697 action_.sa_flags = SA_SIGINFO;
1698 action_.sa_sigaction = action;
1699 sigaction(signal_number_, &action_, &old_action_);
1700 }
1701
1702 ~ScopedSignalHandler() { sigaction(signal_number_, &old_action_, nullptr); }
1703
1704 private:
1705 struct sigaction action_;
1706 struct sigaction old_action_;
1707 const int signal_number_;
1708};
1709
1710static void SetValueAndLoop(void* data) {
1711 volatile int* value = reinterpret_cast<volatile int*>(data);
1712
1713 *value = 1;
1714 for (volatile int i = 0;; i++)
1715 ;
1716}
1717
Christopher Ferrisb9de87f2017-09-20 13:37:24 -07001718static void UnwindThroughSignal(bool use_action, create_func_t create_func,
1719 map_create_func_t map_create_func) {
Christopher Ferris5ea2c1f2017-03-23 14:55:01 -07001720 volatile int value = 0;
1721 pid_t pid;
1722 if ((pid = fork()) == 0) {
1723 if (use_action) {
1724 ScopedSignalHandler ssh(SIGUSR1, test_signal_action);
1725
1726 test_level_one(1, 2, 3, 4, SetValueAndLoop, const_cast<int*>(&value));
1727 } else {
1728 ScopedSignalHandler ssh(SIGUSR1, test_signal_handler);
1729
1730 test_level_one(1, 2, 3, 4, SetValueAndLoop, const_cast<int*>(&value));
1731 }
1732 }
1733 ASSERT_NE(-1, pid);
1734
1735 int read_value = 0;
1736 uint64_t start = NanoTime();
1737 while (read_value == 0) {
1738 usleep(1000);
1739
1740 // Loop until the remote function gets into the final function.
1741 ASSERT_TRUE(ptrace(PTRACE_ATTACH, pid, 0, 0) == 0);
1742
1743 WaitForStop(pid);
1744
Christopher Ferrisb9de87f2017-09-20 13:37:24 -07001745 std::unique_ptr<BacktraceMap> map(map_create_func(pid, false));
1746 std::unique_ptr<Backtrace> backtrace(create_func(pid, pid, map.get()));
Christopher Ferris5ea2c1f2017-03-23 14:55:01 -07001747
1748 size_t bytes_read = backtrace->Read(reinterpret_cast<uintptr_t>(const_cast<int*>(&value)),
1749 reinterpret_cast<uint8_t*>(&read_value), sizeof(read_value));
1750 ASSERT_EQ(sizeof(read_value), bytes_read);
1751
1752 ASSERT_TRUE(ptrace(PTRACE_DETACH, pid, 0, 0) == 0);
1753
1754 ASSERT_TRUE(NanoTime() - start < 5 * NS_PER_SEC)
1755 << "Remote process did not execute far enough in 5 seconds.";
1756 }
1757
1758 // Now need to send a signal to the remote process.
1759 kill(pid, SIGUSR1);
1760
1761 // Wait for the process to get to the signal handler loop.
1762 Backtrace::const_iterator frame_iter;
1763 start = NanoTime();
Christopher Ferris458cc662017-08-28 16:31:18 -07001764 std::unique_ptr<BacktraceMap> map;
Christopher Ferris5ea2c1f2017-03-23 14:55:01 -07001765 std::unique_ptr<Backtrace> backtrace;
1766 while (true) {
1767 usleep(1000);
1768
1769 ASSERT_TRUE(ptrace(PTRACE_ATTACH, pid, 0, 0) == 0);
1770
1771 WaitForStop(pid);
1772
Christopher Ferrisb9de87f2017-09-20 13:37:24 -07001773 map.reset(map_create_func(pid, false));
Christopher Ferris458cc662017-08-28 16:31:18 -07001774 ASSERT_TRUE(map.get() != nullptr);
Christopher Ferrisb9de87f2017-09-20 13:37:24 -07001775 backtrace.reset(create_func(pid, pid, map.get()));
Christopher Ferris5ea2c1f2017-03-23 14:55:01 -07001776 ASSERT_TRUE(backtrace->Unwind(0));
1777 bool found = false;
1778 for (frame_iter = backtrace->begin(); frame_iter != backtrace->end(); ++frame_iter) {
1779 if (frame_iter->func_name == "test_loop_forever") {
1780 ++frame_iter;
1781 found = true;
1782 break;
1783 }
1784 }
1785 if (found) {
1786 break;
1787 }
1788
1789 ASSERT_TRUE(ptrace(PTRACE_DETACH, pid, 0, 0) == 0);
1790
1791 ASSERT_TRUE(NanoTime() - start < 5 * NS_PER_SEC)
1792 << "Remote process did not get in signal handler in 5 seconds." << std::endl
1793 << DumpFrames(backtrace.get());
1794 }
1795
1796 std::vector<std::string> names;
1797 // Loop through the frames, and save the function names.
1798 size_t frame = 0;
1799 for (; frame_iter != backtrace->end(); ++frame_iter) {
1800 if (frame_iter->func_name == "test_level_four") {
1801 frame = names.size() + 1;
1802 }
1803 names.push_back(frame_iter->func_name);
1804 }
1805 ASSERT_NE(0U, frame) << "Unable to find test_level_four in backtrace" << std::endl
1806 << DumpFrames(backtrace.get());
1807
1808 // The expected order of the frames:
1809 // test_loop_forever
1810 // test_signal_handler|test_signal_action
1811 // <OPTIONAL_FRAME> May or may not exist.
1812 // SetValueAndLoop (but the function name might be empty)
1813 // test_level_four
1814 // test_level_three
1815 // test_level_two
1816 // test_level_one
1817 ASSERT_LE(frame + 2, names.size()) << DumpFrames(backtrace.get());
1818 ASSERT_LE(2U, frame) << DumpFrames(backtrace.get());
1819 if (use_action) {
1820 ASSERT_EQ("test_signal_action", names[0]) << DumpFrames(backtrace.get());
1821 } else {
1822 ASSERT_EQ("test_signal_handler", names[0]) << DumpFrames(backtrace.get());
1823 }
1824 ASSERT_EQ("test_level_three", names[frame]) << DumpFrames(backtrace.get());
1825 ASSERT_EQ("test_level_two", names[frame + 1]) << DumpFrames(backtrace.get());
1826 ASSERT_EQ("test_level_one", names[frame + 2]) << DumpFrames(backtrace.get());
1827
1828 FinishRemoteProcess(pid);
1829}
1830
Christopher Ferris96722b02017-07-19 14:20:46 -07001831TEST(libbacktrace, unwind_remote_through_signal_using_handler) {
Christopher Ferris458cc662017-08-28 16:31:18 -07001832 UnwindThroughSignal(false, Backtrace::Create, BacktraceMap::Create);
1833}
1834
1835TEST(libbacktrace, unwind_remote_through_signal_using_handler_new) {
1836 UnwindThroughSignal(false, Backtrace::CreateNew, BacktraceMap::CreateNew);
Christopher Ferris96722b02017-07-19 14:20:46 -07001837}
Christopher Ferris5ea2c1f2017-03-23 14:55:01 -07001838
Christopher Ferris96722b02017-07-19 14:20:46 -07001839TEST(libbacktrace, unwind_remote_through_signal_using_action) {
Christopher Ferris458cc662017-08-28 16:31:18 -07001840 UnwindThroughSignal(true, Backtrace::Create, BacktraceMap::Create);
1841}
1842
1843TEST(libbacktrace, unwind_remote_through_signal_using_action_new) {
1844 UnwindThroughSignal(true, Backtrace::CreateNew, BacktraceMap::CreateNew);
Christopher Ferris96722b02017-07-19 14:20:46 -07001845}
Christopher Ferris5ea2c1f2017-03-23 14:55:01 -07001846
Christopher Ferrise2960912014-03-07 19:42:19 -08001847#if defined(ENABLE_PSS_TESTS)
1848#include "GetPss.h"
1849
Chih-Hung Hsieh67867db2016-05-18 15:53:15 -07001850#define MAX_LEAK_BYTES (32*1024UL)
Christopher Ferrise2960912014-03-07 19:42:19 -08001851
Christopher Ferris82f3bbd2017-03-14 15:22:26 -07001852static void CheckForLeak(pid_t pid, pid_t tid) {
Christopher Ferrise2960912014-03-07 19:42:19 -08001853 // Do a few runs to get the PSS stable.
1854 for (size_t i = 0; i < 100; i++) {
1855 Backtrace* backtrace = Backtrace::Create(pid, tid);
Christopher Ferris2b4a63f2015-03-17 14:42:03 -07001856 ASSERT_TRUE(backtrace != nullptr);
Christopher Ferrise2960912014-03-07 19:42:19 -08001857 ASSERT_TRUE(backtrace->Unwind(0));
Christopher Ferris206a3b92016-03-09 14:35:54 -08001858 ASSERT_EQ(BACKTRACE_UNWIND_NO_ERROR, backtrace->GetError());
Christopher Ferrise2960912014-03-07 19:42:19 -08001859 delete backtrace;
1860 }
1861 size_t stable_pss = GetPssBytes();
Christopher Ferris2c43cff2015-03-26 19:18:36 -07001862 ASSERT_TRUE(stable_pss != 0);
Christopher Ferrise2960912014-03-07 19:42:19 -08001863
1864 // Loop enough that even a small leak should be detectable.
1865 for (size_t i = 0; i < 4096; i++) {
1866 Backtrace* backtrace = Backtrace::Create(pid, tid);
Christopher Ferris2b4a63f2015-03-17 14:42:03 -07001867 ASSERT_TRUE(backtrace != nullptr);
Christopher Ferrise2960912014-03-07 19:42:19 -08001868 ASSERT_TRUE(backtrace->Unwind(0));
Christopher Ferris206a3b92016-03-09 14:35:54 -08001869 ASSERT_EQ(BACKTRACE_UNWIND_NO_ERROR, backtrace->GetError());
Christopher Ferrise2960912014-03-07 19:42:19 -08001870 delete backtrace;
1871 }
1872 size_t new_pss = GetPssBytes();
Christopher Ferris2c43cff2015-03-26 19:18:36 -07001873 ASSERT_TRUE(new_pss != 0);
Christopher Ferris5ccdfa62016-03-07 19:18:31 -08001874 if (new_pss > stable_pss) {
1875 ASSERT_LE(new_pss - stable_pss, MAX_LEAK_BYTES);
1876 }
Christopher Ferrise2960912014-03-07 19:42:19 -08001877}
1878
1879TEST(libbacktrace, check_for_leak_local) {
1880 CheckForLeak(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD);
1881}
1882
1883TEST(libbacktrace, check_for_leak_local_thread) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -07001884 thread_t thread_data = { 0, 0, 0, nullptr };
Christopher Ferrise2960912014-03-07 19:42:19 -08001885 pthread_t thread;
Christopher Ferris2b4a63f2015-03-17 14:42:03 -07001886 ASSERT_TRUE(pthread_create(&thread, nullptr, ThreadLevelRun, &thread_data) == 0);
Christopher Ferrise2960912014-03-07 19:42:19 -08001887
1888 // Wait up to 2 seconds for the tid to be set.
1889 ASSERT_TRUE(WaitForNonZero(&thread_data.state, 2));
1890
1891 CheckForLeak(BACKTRACE_CURRENT_PROCESS, thread_data.tid);
1892
1893 // Tell the thread to exit its infinite loop.
1894 android_atomic_acquire_store(0, &thread_data.state);
1895
Christopher Ferris2b4a63f2015-03-17 14:42:03 -07001896 ASSERT_TRUE(pthread_join(thread, nullptr) == 0);
Christopher Ferrise2960912014-03-07 19:42:19 -08001897}
1898
1899TEST(libbacktrace, check_for_leak_remote) {
1900 pid_t pid;
Christopher Ferris82f3bbd2017-03-14 15:22:26 -07001901 CreateRemoteProcess(&pid);
Christopher Ferrise2960912014-03-07 19:42:19 -08001902
1903 CheckForLeak(pid, BACKTRACE_CURRENT_THREAD);
1904
Christopher Ferris82f3bbd2017-03-14 15:22:26 -07001905 FinishRemoteProcess(pid);
Christopher Ferrise2960912014-03-07 19:42:19 -08001906}
1907#endif