blob: f4191b90870b3ce816c3e44e0877c60c8037ba06 [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 Ferris50d81ac2018-06-29 16:05:35 -070023#include <malloc.h>
Christopher Ferris17e91d42013-10-21 13:30:52 -070024#include <pthread.h>
25#include <signal.h>
Christopher Ferrise2960912014-03-07 19:42:19 -080026#include <stdint.h>
Christopher Ferris17e91d42013-10-21 13:30:52 -070027#include <stdio.h>
28#include <stdlib.h>
29#include <string.h>
30#include <sys/ptrace.h>
Christopher Ferris67aba682015-05-08 15:44:46 -070031#include <sys/stat.h>
Christopher Ferris17e91d42013-10-21 13:30:52 -070032#include <sys/types.h>
33#include <sys/wait.h>
34#include <time.h>
Christopher Ferrisc8bec5a2017-12-11 17:44:33 -080035#include <ucontext.h>
Christopher Ferris17e91d42013-10-21 13:30:52 -070036#include <unistd.h>
37
Christopher Ferrise2960912014-03-07 19:42:19 -080038#include <algorithm>
Christopher Ferris67aba682015-05-08 15:44:46 -070039#include <list>
Christopher Ferris2b4a63f2015-03-17 14:42:03 -070040#include <memory>
Christopher Ferris5ea2c1f2017-03-23 14:55:01 -070041#include <ostream>
Christopher Ferris2c43cff2015-03-26 19:18:36 -070042#include <string>
Christopher Ferris17e91d42013-10-21 13:30:52 -070043#include <vector>
44
Dan Albert23f750b2015-04-30 12:52:21 -070045#include <backtrace/Backtrace.h>
46#include <backtrace/BacktraceMap.h>
47
Christopher Ferrisf5e568e2017-03-22 13:18:31 -070048#include <android-base/macros.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080049#include <android-base/stringprintf.h>
Christopher Ferris3acf5772018-05-24 16:01:55 -070050#include <android-base/test_utils.h>
Elliott Hughes38488902018-07-11 11:13:16 -070051#include <android-base/threads.h>
Christopher Ferris82f3bbd2017-03-14 15:22:26 -070052#include <android-base/unique_fd.h>
Dan Albert23f750b2015-04-30 12:52:21 -070053#include <cutils/atomic.h>
Dan Albert23f750b2015-04-30 12:52:21 -070054
55#include <gtest/gtest.h>
56
57// For the THREAD_SIGNAL definition.
58#include "BacktraceCurrent.h"
Christopher Ferris50d81ac2018-06-29 16:05:35 -070059#include "BacktraceTest.h"
Christopher Ferris5ea2c1f2017-03-23 14:55:01 -070060#include "backtrace_testlib.h"
Christopher Ferris17e91d42013-10-21 13:30:52 -070061
62// Number of microseconds per milliseconds.
63#define US_PER_MSEC 1000
64
65// Number of nanoseconds in a second.
66#define NS_PER_SEC 1000000000ULL
67
68// Number of simultaneous dumping operations to perform.
Christopher Ferris3cdbfdc2014-11-08 15:57:11 -080069#define NUM_THREADS 40
Christopher Ferris17e91d42013-10-21 13:30:52 -070070
71// Number of simultaneous threads running in our forked process.
72#define NUM_PTRACE_THREADS 5
73
Christopher Ferris458f4e72018-03-23 12:51:43 -070074// The list of shared libaries that make up the backtrace library.
75static std::vector<std::string> kBacktraceLibs{"libunwindstack.so", "libbacktrace.so"};
76
Christopher Ferris46756822014-01-14 20:16:30 -080077struct thread_t {
Christopher Ferris17e91d42013-10-21 13:30:52 -070078 pid_t tid;
79 int32_t state;
80 pthread_t threadId;
Christopher Ferris2b4a63f2015-03-17 14:42:03 -070081 void* data;
Christopher Ferris46756822014-01-14 20:16:30 -080082};
Christopher Ferris17e91d42013-10-21 13:30:52 -070083
Christopher Ferris46756822014-01-14 20:16:30 -080084struct dump_thread_t {
Christopher Ferris17e91d42013-10-21 13:30:52 -070085 thread_t thread;
Christopher Ferrisbe788d82017-11-27 14:50:38 -080086 BacktraceMap* map;
Christopher Ferris20303f82014-01-10 16:33:16 -080087 Backtrace* backtrace;
Christopher Ferris17e91d42013-10-21 13:30:52 -070088 int32_t* now;
89 int32_t done;
Christopher Ferris46756822014-01-14 20:16:30 -080090};
Christopher Ferris17e91d42013-10-21 13:30:52 -070091
Christopher Ferrisb9de87f2017-09-20 13:37:24 -070092typedef Backtrace* (*create_func_t)(pid_t, pid_t, BacktraceMap*);
93typedef BacktraceMap* (*map_create_func_t)(pid_t, bool);
94
95static void VerifyLevelDump(Backtrace* backtrace, create_func_t create_func = nullptr,
96 map_create_func_t map_func = nullptr);
97static void VerifyMaxDump(Backtrace* backtrace, create_func_t create_func = nullptr,
98 map_create_func_t map_func = nullptr);
99
Christopher Ferris50d81ac2018-06-29 16:05:35 -0700100void* BacktraceTest::dl_handle_;
101int (*BacktraceTest::test_level_one_)(int, int, int, int, void (*)(void*), void*);
102int (*BacktraceTest::test_level_two_)(int, int, int, int, void (*)(void*), void*);
103int (*BacktraceTest::test_level_three_)(int, int, int, int, void (*)(void*), void*);
104int (*BacktraceTest::test_level_four_)(int, int, int, int, void (*)(void*), void*);
105int (*BacktraceTest::test_recursive_call_)(int, void (*)(void*), void*);
106void (*BacktraceTest::test_get_context_and_wait_)(void*, volatile int*);
107void (*BacktraceTest::test_signal_action_)(int, siginfo_t*, void*);
108void (*BacktraceTest::test_signal_handler_)(int);
109
110extern "C" bool GetInitialArgs(const char*** args, size_t* num_args) {
111 static const char* initial_args[] = {"--slow_threshold_ms=8000", "--deadline_threshold_ms=15000"};
112 *args = initial_args;
113 *num_args = 2;
114 return true;
115}
116
Christopher Ferris82f3bbd2017-03-14 15:22:26 -0700117static uint64_t NanoTime() {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700118 struct timespec t = { 0, 0 };
119 clock_gettime(CLOCK_MONOTONIC, &t);
120 return static_cast<uint64_t>(t.tv_sec * NS_PER_SEC + t.tv_nsec);
121}
122
Christopher Ferris82f3bbd2017-03-14 15:22:26 -0700123static std::string DumpFrames(Backtrace* backtrace) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800124 if (backtrace->NumFrames() == 0) {
Christopher Ferris97e00bb2015-04-02 14:22:31 -0700125 return " No frames to dump.\n";
Christopher Ferris20303f82014-01-10 16:33:16 -0800126 }
127
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700128 std::string frame;
Christopher Ferris20303f82014-01-10 16:33:16 -0800129 for (size_t i = 0; i < backtrace->NumFrames(); i++) {
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700130 frame += " " + backtrace->FormatFrameData(i) + '\n';
Christopher Ferris17e91d42013-10-21 13:30:52 -0700131 }
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700132 return frame;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700133}
134
Christopher Ferris82f3bbd2017-03-14 15:22:26 -0700135static void WaitForStop(pid_t pid) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700136 uint64_t start = NanoTime();
137
138 siginfo_t si;
139 while (ptrace(PTRACE_GETSIGINFO, pid, 0, &si) < 0 && (errno == EINTR || errno == ESRCH)) {
140 if ((NanoTime() - start) > NS_PER_SEC) {
141 printf("The process did not get to a stopping point in 1 second.\n");
142 break;
143 }
144 usleep(US_PER_MSEC);
145 }
146}
147
Christopher Ferris82f3bbd2017-03-14 15:22:26 -0700148static void CreateRemoteProcess(pid_t* pid) {
149 if ((*pid = fork()) == 0) {
150 while (true)
151 ;
152 _exit(0);
153 }
154 ASSERT_NE(-1, *pid);
155
156 ASSERT_TRUE(ptrace(PTRACE_ATTACH, *pid, 0, 0) == 0);
157
158 // Wait for the process to get to a stopping point.
159 WaitForStop(*pid);
160}
161
162static void FinishRemoteProcess(pid_t pid) {
163 ASSERT_TRUE(ptrace(PTRACE_DETACH, pid, 0, 0) == 0);
164
165 kill(pid, SIGKILL);
166 ASSERT_EQ(waitpid(pid, nullptr, 0), pid);
167}
168
Christopher Ferrisc8bec5a2017-12-11 17:44:33 -0800169#if !defined(__ANDROID__) || defined(__arm__)
170// On host and arm target we aren't guaranteed that we will terminate cleanly.
171#define VERIFY_NO_ERROR(error_code) \
172 ASSERT_TRUE(error_code == BACKTRACE_UNWIND_NO_ERROR || \
173 error_code == BACKTRACE_UNWIND_ERROR_UNWIND_INFO || \
174 error_code == BACKTRACE_UNWIND_ERROR_MAP_MISSING) \
175 << "Unknown error code " << std::to_string(error_code);
176#else
177#define VERIFY_NO_ERROR(error_code) ASSERT_EQ(BACKTRACE_UNWIND_NO_ERROR, error_code);
178#endif
179
Christopher Ferris82f3bbd2017-03-14 15:22:26 -0700180static bool ReadyLevelBacktrace(Backtrace* backtrace) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700181 // See if test_level_four is in the backtrace.
182 bool found = false;
Christopher Ferris46756822014-01-14 20:16:30 -0800183 for (Backtrace::const_iterator it = backtrace->begin(); it != backtrace->end(); ++it) {
184 if (it->func_name == "test_level_four") {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700185 found = true;
186 break;
187 }
188 }
189
190 return found;
191}
192
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700193static void VerifyLevelDump(Backtrace* backtrace, create_func_t, map_create_func_t) {
Christopher Ferris97e00bb2015-04-02 14:22:31 -0700194 ASSERT_GT(backtrace->NumFrames(), static_cast<size_t>(0))
195 << DumpFrames(backtrace);
196 ASSERT_LT(backtrace->NumFrames(), static_cast<size_t>(MAX_BACKTRACE_FRAMES))
197 << DumpFrames(backtrace);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700198
199 // Look through the frames starting at the highest to find the
200 // frame we want.
201 size_t frame_num = 0;
Christopher Ferris20303f82014-01-10 16:33:16 -0800202 for (size_t i = backtrace->NumFrames()-1; i > 2; i--) {
Christopher Ferris46756822014-01-14 20:16:30 -0800203 if (backtrace->GetFrame(i)->func_name == "test_level_one") {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700204 frame_num = i;
205 break;
206 }
207 }
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700208 ASSERT_LT(static_cast<size_t>(0), frame_num) << DumpFrames(backtrace);
209 ASSERT_LE(static_cast<size_t>(3), frame_num) << DumpFrames(backtrace);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700210
Christopher Ferris97e00bb2015-04-02 14:22:31 -0700211 ASSERT_EQ(backtrace->GetFrame(frame_num)->func_name, "test_level_one")
212 << DumpFrames(backtrace);
213 ASSERT_EQ(backtrace->GetFrame(frame_num-1)->func_name, "test_level_two")
214 << DumpFrames(backtrace);
215 ASSERT_EQ(backtrace->GetFrame(frame_num-2)->func_name, "test_level_three")
216 << DumpFrames(backtrace);
217 ASSERT_EQ(backtrace->GetFrame(frame_num-3)->func_name, "test_level_four")
218 << DumpFrames(backtrace);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700219}
220
Christopher Ferris82f3bbd2017-03-14 15:22:26 -0700221static void VerifyLevelBacktrace(void*) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700222 std::unique_ptr<Backtrace> backtrace(
Christopher Ferris20303f82014-01-10 16:33:16 -0800223 Backtrace::Create(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD));
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700224 ASSERT_TRUE(backtrace.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800225 ASSERT_TRUE(backtrace->Unwind(0));
Christopher Ferrisc8bec5a2017-12-11 17:44:33 -0800226 VERIFY_NO_ERROR(backtrace->GetError().error_code);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700227
Christopher Ferris20303f82014-01-10 16:33:16 -0800228 VerifyLevelDump(backtrace.get());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700229}
230
Christopher Ferris82f3bbd2017-03-14 15:22:26 -0700231static bool ReadyMaxBacktrace(Backtrace* backtrace) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800232 return (backtrace->NumFrames() == MAX_BACKTRACE_FRAMES);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700233}
234
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700235static void VerifyMaxDump(Backtrace* backtrace, create_func_t, map_create_func_t) {
Christopher Ferris97e00bb2015-04-02 14:22:31 -0700236 ASSERT_EQ(backtrace->NumFrames(), static_cast<size_t>(MAX_BACKTRACE_FRAMES))
237 << DumpFrames(backtrace);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700238 // Verify that the last frame is our recursive call.
Christopher Ferris97e00bb2015-04-02 14:22:31 -0700239 ASSERT_EQ(backtrace->GetFrame(MAX_BACKTRACE_FRAMES-1)->func_name, "test_recursive_call")
240 << DumpFrames(backtrace);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700241}
242
Christopher Ferris82f3bbd2017-03-14 15:22:26 -0700243static void VerifyMaxBacktrace(void*) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700244 std::unique_ptr<Backtrace> backtrace(
Christopher Ferris20303f82014-01-10 16:33:16 -0800245 Backtrace::Create(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD));
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700246 ASSERT_TRUE(backtrace.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800247 ASSERT_TRUE(backtrace->Unwind(0));
Christopher Ferrisc8bec5a2017-12-11 17:44:33 -0800248 ASSERT_EQ(BACKTRACE_UNWIND_ERROR_EXCEED_MAX_FRAMES_LIMIT, backtrace->GetError().error_code);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700249
Christopher Ferris20303f82014-01-10 16:33:16 -0800250 VerifyMaxDump(backtrace.get());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700251}
252
Christopher Ferris82f3bbd2017-03-14 15:22:26 -0700253static void ThreadSetState(void* data) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700254 thread_t* thread = reinterpret_cast<thread_t*>(data);
255 android_atomic_acquire_store(1, &thread->state);
256 volatile int i = 0;
257 while (thread->state) {
258 i++;
259 }
260}
261
Christopher Ferris82f3bbd2017-03-14 15:22:26 -0700262static bool WaitForNonZero(int32_t* value, uint64_t seconds) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700263 uint64_t start = NanoTime();
264 do {
265 if (android_atomic_acquire_load(value)) {
266 return true;
267 }
268 } while ((NanoTime() - start) < seconds * NS_PER_SEC);
269 return false;
270}
271
Christopher Ferris50d81ac2018-06-29 16:05:35 -0700272TEST_F(BacktraceTest, local_no_unwind_frames) {
Christopher Ferrisca09ce92015-03-31 17:28:22 -0700273 // Verify that a local unwind does not include any frames within
274 // libunwind or libbacktrace.
275 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), getpid()));
Christopher Ferris97e00bb2015-04-02 14:22:31 -0700276 ASSERT_TRUE(backtrace.get() != nullptr);
Christopher Ferrisca09ce92015-03-31 17:28:22 -0700277 ASSERT_TRUE(backtrace->Unwind(0));
Christopher Ferrisc8bec5a2017-12-11 17:44:33 -0800278 VERIFY_NO_ERROR(backtrace->GetError().error_code);
Christopher Ferrisca09ce92015-03-31 17:28:22 -0700279
280 ASSERT_TRUE(backtrace->NumFrames() != 0);
Christopher Ferris458f4e72018-03-23 12:51:43 -0700281 // None of the frames should be in the backtrace libraries.
Christopher Ferrisca09ce92015-03-31 17:28:22 -0700282 for (const auto& frame : *backtrace ) {
283 if (BacktraceMap::IsValid(frame.map)) {
284 const std::string name = basename(frame.map.name.c_str());
Christopher Ferris458f4e72018-03-23 12:51:43 -0700285 for (const auto& lib : kBacktraceLibs) {
286 ASSERT_TRUE(name != lib) << DumpFrames(backtrace.get());
287 }
Christopher Ferrisca09ce92015-03-31 17:28:22 -0700288 }
Christopher Ferrisca09ce92015-03-31 17:28:22 -0700289 }
290}
291
Christopher Ferris50d81ac2018-06-29 16:05:35 -0700292TEST_F(BacktraceTest, local_unwind_frames) {
Christopher Ferris458f4e72018-03-23 12:51:43 -0700293 // Verify that a local unwind with the skip frames disabled does include
294 // frames within the backtrace libraries.
295 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), getpid()));
296 ASSERT_TRUE(backtrace.get() != nullptr);
297 backtrace->SetSkipFrames(false);
298 ASSERT_TRUE(backtrace->Unwind(0));
299 VERIFY_NO_ERROR(backtrace->GetError().error_code);
300
301 ASSERT_TRUE(backtrace->NumFrames() != 0);
302 size_t first_frame_non_backtrace_lib = 0;
303 for (const auto& frame : *backtrace) {
304 if (BacktraceMap::IsValid(frame.map)) {
305 const std::string name = basename(frame.map.name.c_str());
306 bool found = false;
307 for (const auto& lib : kBacktraceLibs) {
308 if (name == lib) {
309 found = true;
310 break;
311 }
312 }
313 if (!found) {
314 first_frame_non_backtrace_lib = frame.num;
315 break;
316 }
317 }
318 }
319
320 ASSERT_NE(0U, first_frame_non_backtrace_lib) << "No frames found in backtrace libraries:\n"
321 << DumpFrames(backtrace.get());
322}
323
Christopher Ferris50d81ac2018-06-29 16:05:35 -0700324TEST_F(BacktraceTest, local_trace) {
325 ASSERT_NE(test_level_one_(1, 2, 3, 4, VerifyLevelBacktrace, nullptr), 0);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700326}
327
Christopher Ferris82f3bbd2017-03-14 15:22:26 -0700328static void VerifyIgnoreFrames(Backtrace* bt_all, Backtrace* bt_ign1, Backtrace* bt_ign2,
329 const char* cur_proc) {
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700330 ASSERT_EQ(bt_all->NumFrames(), bt_ign1->NumFrames() + 1) << "All backtrace:\n"
331 << DumpFrames(bt_all)
332 << "Ignore 1 backtrace:\n"
333 << DumpFrames(bt_ign1);
334 ASSERT_EQ(bt_all->NumFrames(), bt_ign2->NumFrames() + 2) << "All backtrace:\n"
335 << DumpFrames(bt_all)
336 << "Ignore 2 backtrace:\n"
337 << DumpFrames(bt_ign2);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700338
339 // Check all of the frames are the same > the current frame.
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700340 bool check = (cur_proc == nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800341 for (size_t i = 0; i < bt_ign2->NumFrames(); i++) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700342 if (check) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800343 EXPECT_EQ(bt_ign2->GetFrame(i)->pc, bt_ign1->GetFrame(i+1)->pc);
344 EXPECT_EQ(bt_ign2->GetFrame(i)->sp, bt_ign1->GetFrame(i+1)->sp);
345 EXPECT_EQ(bt_ign2->GetFrame(i)->stack_size, bt_ign1->GetFrame(i+1)->stack_size);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700346
Christopher Ferris20303f82014-01-10 16:33:16 -0800347 EXPECT_EQ(bt_ign2->GetFrame(i)->pc, bt_all->GetFrame(i+2)->pc);
348 EXPECT_EQ(bt_ign2->GetFrame(i)->sp, bt_all->GetFrame(i+2)->sp);
349 EXPECT_EQ(bt_ign2->GetFrame(i)->stack_size, bt_all->GetFrame(i+2)->stack_size);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700350 }
Christopher Ferris46756822014-01-14 20:16:30 -0800351 if (!check && bt_ign2->GetFrame(i)->func_name == cur_proc) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700352 check = true;
353 }
354 }
355}
356
Christopher Ferris82f3bbd2017-03-14 15:22:26 -0700357static void VerifyLevelIgnoreFrames(void*) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700358 std::unique_ptr<Backtrace> all(
Christopher Ferris20303f82014-01-10 16:33:16 -0800359 Backtrace::Create(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD));
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700360 ASSERT_TRUE(all.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800361 ASSERT_TRUE(all->Unwind(0));
Christopher Ferrisc8bec5a2017-12-11 17:44:33 -0800362 VERIFY_NO_ERROR(all->GetError().error_code);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700363
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700364 std::unique_ptr<Backtrace> ign1(
Christopher Ferris20303f82014-01-10 16:33:16 -0800365 Backtrace::Create(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD));
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700366 ASSERT_TRUE(ign1.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800367 ASSERT_TRUE(ign1->Unwind(1));
Christopher Ferrisc8bec5a2017-12-11 17:44:33 -0800368 VERIFY_NO_ERROR(ign1->GetError().error_code);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700369
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700370 std::unique_ptr<Backtrace> ign2(
Christopher Ferris20303f82014-01-10 16:33:16 -0800371 Backtrace::Create(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD));
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700372 ASSERT_TRUE(ign2.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800373 ASSERT_TRUE(ign2->Unwind(2));
Christopher Ferrisc8bec5a2017-12-11 17:44:33 -0800374 VERIFY_NO_ERROR(ign2->GetError().error_code);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700375
Christopher Ferris20303f82014-01-10 16:33:16 -0800376 VerifyIgnoreFrames(all.get(), ign1.get(), ign2.get(), "VerifyLevelIgnoreFrames");
Christopher Ferris17e91d42013-10-21 13:30:52 -0700377}
378
Christopher Ferris50d81ac2018-06-29 16:05:35 -0700379TEST_F(BacktraceTest, local_trace_ignore_frames) {
380 ASSERT_NE(test_level_one_(1, 2, 3, 4, VerifyLevelIgnoreFrames, nullptr), 0);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700381}
382
Christopher Ferris50d81ac2018-06-29 16:05:35 -0700383TEST_F(BacktraceTest, local_max_trace) {
384 ASSERT_NE(test_recursive_call_(MAX_BACKTRACE_FRAMES + 10, VerifyMaxBacktrace, nullptr), 0);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700385}
386
Christopher Ferris458cc662017-08-28 16:31:18 -0700387static void VerifyProcTest(pid_t pid, pid_t tid, bool (*ReadyFunc)(Backtrace*),
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700388 void (*VerifyFunc)(Backtrace*, create_func_t, map_create_func_t),
389 create_func_t create_func, map_create_func_t map_create_func) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700390 pid_t ptrace_tid;
391 if (tid < 0) {
392 ptrace_tid = pid;
393 } else {
394 ptrace_tid = tid;
395 }
396 uint64_t start = NanoTime();
397 bool verified = false;
Christopher Ferris97e00bb2015-04-02 14:22:31 -0700398 std::string last_dump;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700399 do {
400 usleep(US_PER_MSEC);
401 if (ptrace(PTRACE_ATTACH, ptrace_tid, 0, 0) == 0) {
402 // Wait for the process to get to a stopping point.
403 WaitForStop(ptrace_tid);
404
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700405 std::unique_ptr<BacktraceMap> map;
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700406 map.reset(map_create_func(pid, false));
407 std::unique_ptr<Backtrace> backtrace(create_func(pid, tid, map.get()));
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700408 ASSERT_TRUE(backtrace.get() != nullptr);
Christopher Ferris97e00bb2015-04-02 14:22:31 -0700409 ASSERT_TRUE(backtrace->Unwind(0));
Christopher Ferris20303f82014-01-10 16:33:16 -0800410 if (ReadyFunc(backtrace.get())) {
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700411 VerifyFunc(backtrace.get(), create_func, map_create_func);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700412 verified = true;
Christopher Ferris97e00bb2015-04-02 14:22:31 -0700413 } else {
414 last_dump = DumpFrames(backtrace.get());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700415 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800416
Christopher Ferris17e91d42013-10-21 13:30:52 -0700417 ASSERT_TRUE(ptrace(PTRACE_DETACH, ptrace_tid, 0, 0) == 0);
418 }
419 // If 5 seconds have passed, then we are done.
420 } while (!verified && (NanoTime() - start) <= 5 * NS_PER_SEC);
Christopher Ferris97e00bb2015-04-02 14:22:31 -0700421 ASSERT_TRUE(verified) << "Last backtrace:\n" << last_dump;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700422}
423
Christopher Ferris50d81ac2018-06-29 16:05:35 -0700424TEST_F(BacktraceTest, ptrace_trace) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700425 pid_t pid;
426 if ((pid = fork()) == 0) {
Christopher Ferris50d81ac2018-06-29 16:05:35 -0700427 ASSERT_NE(test_level_one_(1, 2, 3, 4, nullptr, nullptr), 0);
Christopher Ferrise2960912014-03-07 19:42:19 -0800428 _exit(1);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700429 }
Christopher Ferris458cc662017-08-28 16:31:18 -0700430 VerifyProcTest(pid, BACKTRACE_CURRENT_THREAD, ReadyLevelBacktrace, VerifyLevelDump,
431 Backtrace::Create, BacktraceMap::Create);
Christopher Ferrisdf290612014-01-22 19:21:07 -0800432
433 kill(pid, SIGKILL);
434 int status;
435 ASSERT_EQ(waitpid(pid, &status, 0), pid);
436}
437
Christopher Ferris50d81ac2018-06-29 16:05:35 -0700438TEST_F(BacktraceTest, ptrace_max_trace) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700439 pid_t pid;
440 if ((pid = fork()) == 0) {
Christopher Ferris50d81ac2018-06-29 16:05:35 -0700441 ASSERT_NE(test_recursive_call_(MAX_BACKTRACE_FRAMES + 10, nullptr, nullptr), 0);
Christopher Ferrise2960912014-03-07 19:42:19 -0800442 _exit(1);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700443 }
Christopher Ferris458cc662017-08-28 16:31:18 -0700444 VerifyProcTest(pid, BACKTRACE_CURRENT_THREAD, ReadyMaxBacktrace, VerifyMaxDump, Backtrace::Create,
445 BacktraceMap::Create);
446
447 kill(pid, SIGKILL);
448 int status;
449 ASSERT_EQ(waitpid(pid, &status, 0), pid);
450}
451
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700452static void VerifyProcessIgnoreFrames(Backtrace* bt_all, create_func_t create_func,
453 map_create_func_t map_create_func) {
454 std::unique_ptr<BacktraceMap> map(map_create_func(bt_all->Pid(), false));
455 std::unique_ptr<Backtrace> ign1(create_func(bt_all->Pid(), BACKTRACE_CURRENT_THREAD, map.get()));
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700456 ASSERT_TRUE(ign1.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800457 ASSERT_TRUE(ign1->Unwind(1));
Christopher Ferrisc8bec5a2017-12-11 17:44:33 -0800458 VERIFY_NO_ERROR(ign1->GetError().error_code);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700459
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700460 std::unique_ptr<Backtrace> ign2(create_func(bt_all->Pid(), BACKTRACE_CURRENT_THREAD, map.get()));
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700461 ASSERT_TRUE(ign2.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800462 ASSERT_TRUE(ign2->Unwind(2));
Christopher Ferrisc8bec5a2017-12-11 17:44:33 -0800463 VERIFY_NO_ERROR(ign2->GetError().error_code);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700464
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700465 VerifyIgnoreFrames(bt_all, ign1.get(), ign2.get(), nullptr);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700466}
467
Christopher Ferris50d81ac2018-06-29 16:05:35 -0700468TEST_F(BacktraceTest, ptrace_ignore_frames) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700469 pid_t pid;
470 if ((pid = fork()) == 0) {
Christopher Ferris50d81ac2018-06-29 16:05:35 -0700471 ASSERT_NE(test_level_one_(1, 2, 3, 4, nullptr, nullptr), 0);
Christopher Ferrise2960912014-03-07 19:42:19 -0800472 _exit(1);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700473 }
Christopher Ferris458cc662017-08-28 16:31:18 -0700474 VerifyProcTest(pid, BACKTRACE_CURRENT_THREAD, ReadyLevelBacktrace, VerifyProcessIgnoreFrames,
475 Backtrace::Create, BacktraceMap::Create);
476
477 kill(pid, SIGKILL);
478 int status;
479 ASSERT_EQ(waitpid(pid, &status, 0), pid);
480}
481
Christopher Ferris17e91d42013-10-21 13:30:52 -0700482// Create a process with multiple threads and dump all of the threads.
Christopher Ferris82f3bbd2017-03-14 15:22:26 -0700483static void* PtraceThreadLevelRun(void*) {
Christopher Ferris50d81ac2018-06-29 16:05:35 -0700484 EXPECT_NE(BacktraceTest::test_level_one_(1, 2, 3, 4, nullptr, nullptr), 0);
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700485 return nullptr;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700486}
487
Christopher Ferris82f3bbd2017-03-14 15:22:26 -0700488static void GetThreads(pid_t pid, std::vector<pid_t>* threads) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700489 // Get the list of tasks.
490 char task_path[128];
491 snprintf(task_path, sizeof(task_path), "/proc/%d/task", pid);
492
James Hawkins588a2ca2016-02-18 14:52:46 -0800493 std::unique_ptr<DIR, decltype(&closedir)> tasks_dir(opendir(task_path), closedir);
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700494 ASSERT_TRUE(tasks_dir != nullptr);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700495 struct dirent* entry;
James Hawkins588a2ca2016-02-18 14:52:46 -0800496 while ((entry = readdir(tasks_dir.get())) != nullptr) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700497 char* end;
498 pid_t tid = strtoul(entry->d_name, &end, 10);
499 if (*end == '\0') {
500 threads->push_back(tid);
501 }
502 }
Christopher Ferris17e91d42013-10-21 13:30:52 -0700503}
504
Christopher Ferris50d81ac2018-06-29 16:05:35 -0700505TEST_F(BacktraceTest, ptrace_threads) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700506 pid_t pid;
507 if ((pid = fork()) == 0) {
508 for (size_t i = 0; i < NUM_PTRACE_THREADS; i++) {
509 pthread_attr_t attr;
510 pthread_attr_init(&attr);
511 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
512
513 pthread_t thread;
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700514 ASSERT_TRUE(pthread_create(&thread, &attr, PtraceThreadLevelRun, nullptr) == 0);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700515 }
Christopher Ferris50d81ac2018-06-29 16:05:35 -0700516 ASSERT_NE(test_level_one_(1, 2, 3, 4, nullptr, nullptr), 0);
Christopher Ferrise2960912014-03-07 19:42:19 -0800517 _exit(1);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700518 }
519
520 // Check to see that all of the threads are running before unwinding.
521 std::vector<pid_t> threads;
522 uint64_t start = NanoTime();
523 do {
524 usleep(US_PER_MSEC);
525 threads.clear();
526 GetThreads(pid, &threads);
527 } while ((threads.size() != NUM_PTRACE_THREADS + 1) &&
528 ((NanoTime() - start) <= 5 * NS_PER_SEC));
529 ASSERT_EQ(threads.size(), static_cast<size_t>(NUM_PTRACE_THREADS + 1));
530
531 ASSERT_TRUE(ptrace(PTRACE_ATTACH, pid, 0, 0) == 0);
532 WaitForStop(pid);
533 for (std::vector<int>::const_iterator it = threads.begin(); it != threads.end(); ++it) {
534 // Skip the current forked process, we only care about the threads.
535 if (pid == *it) {
536 continue;
537 }
Christopher Ferris458cc662017-08-28 16:31:18 -0700538 VerifyProcTest(pid, *it, ReadyLevelBacktrace, VerifyLevelDump, Backtrace::Create,
539 BacktraceMap::Create);
540 }
541
542 FinishRemoteProcess(pid);
543}
544
Christopher Ferris17e91d42013-10-21 13:30:52 -0700545void VerifyLevelThread(void*) {
Elliott Hughes38488902018-07-11 11:13:16 -0700546 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), android::base::GetThreadId()));
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700547 ASSERT_TRUE(backtrace.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800548 ASSERT_TRUE(backtrace->Unwind(0));
Christopher Ferrisc8bec5a2017-12-11 17:44:33 -0800549 VERIFY_NO_ERROR(backtrace->GetError().error_code);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700550
Christopher Ferris20303f82014-01-10 16:33:16 -0800551 VerifyLevelDump(backtrace.get());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700552}
553
Christopher Ferris50d81ac2018-06-29 16:05:35 -0700554TEST_F(BacktraceTest, thread_current_level) {
555 ASSERT_NE(test_level_one_(1, 2, 3, 4, VerifyLevelThread, nullptr), 0);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700556}
557
Christopher Ferris82f3bbd2017-03-14 15:22:26 -0700558static void VerifyMaxThread(void*) {
Elliott Hughes38488902018-07-11 11:13:16 -0700559 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), android::base::GetThreadId()));
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700560 ASSERT_TRUE(backtrace.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800561 ASSERT_TRUE(backtrace->Unwind(0));
Christopher Ferrisc8bec5a2017-12-11 17:44:33 -0800562 ASSERT_EQ(BACKTRACE_UNWIND_ERROR_EXCEED_MAX_FRAMES_LIMIT, backtrace->GetError().error_code);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700563
Christopher Ferris20303f82014-01-10 16:33:16 -0800564 VerifyMaxDump(backtrace.get());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700565}
566
Christopher Ferris50d81ac2018-06-29 16:05:35 -0700567TEST_F(BacktraceTest, thread_current_max) {
568 ASSERT_NE(test_recursive_call_(MAX_BACKTRACE_FRAMES + 10, VerifyMaxThread, nullptr), 0);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700569}
570
Christopher Ferris82f3bbd2017-03-14 15:22:26 -0700571static void* ThreadLevelRun(void* data) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700572 thread_t* thread = reinterpret_cast<thread_t*>(data);
573
Elliott Hughes38488902018-07-11 11:13:16 -0700574 thread->tid = android::base::GetThreadId();
Christopher Ferris50d81ac2018-06-29 16:05:35 -0700575 EXPECT_NE(BacktraceTest::test_level_one_(1, 2, 3, 4, ThreadSetState, data), 0);
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700576 return nullptr;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700577}
578
Christopher Ferris50d81ac2018-06-29 16:05:35 -0700579TEST_F(BacktraceTest, thread_level_trace) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700580 pthread_attr_t attr;
581 pthread_attr_init(&attr);
582 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
583
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700584 thread_t thread_data = { 0, 0, 0, nullptr };
Christopher Ferris17e91d42013-10-21 13:30:52 -0700585 pthread_t thread;
586 ASSERT_TRUE(pthread_create(&thread, &attr, ThreadLevelRun, &thread_data) == 0);
587
588 // Wait up to 2 seconds for the tid to be set.
589 ASSERT_TRUE(WaitForNonZero(&thread_data.state, 2));
590
Christopher Ferrisaa63d9f2014-04-29 09:35:30 -0700591 // Make sure that the thread signal used is not visible when compiled for
592 // the target.
593#if !defined(__GLIBC__)
594 ASSERT_LT(THREAD_SIGNAL, SIGRTMIN);
595#endif
596
Christopher Ferris17e91d42013-10-21 13:30:52 -0700597 // Save the current signal action and make sure it is restored afterwards.
598 struct sigaction cur_action;
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700599 ASSERT_TRUE(sigaction(THREAD_SIGNAL, nullptr, &cur_action) == 0);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700600
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700601 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), thread_data.tid));
602 ASSERT_TRUE(backtrace.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800603 ASSERT_TRUE(backtrace->Unwind(0));
Christopher Ferrisc8bec5a2017-12-11 17:44:33 -0800604 VERIFY_NO_ERROR(backtrace->GetError().error_code);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700605
Christopher Ferris20303f82014-01-10 16:33:16 -0800606 VerifyLevelDump(backtrace.get());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700607
608 // Tell the thread to exit its infinite loop.
609 android_atomic_acquire_store(0, &thread_data.state);
610
611 // Verify that the old action was restored.
612 struct sigaction new_action;
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700613 ASSERT_TRUE(sigaction(THREAD_SIGNAL, nullptr, &new_action) == 0);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700614 EXPECT_EQ(cur_action.sa_sigaction, new_action.sa_sigaction);
Christopher Ferris3cdbfdc2014-11-08 15:57:11 -0800615 // The SA_RESTORER flag gets set behind our back, so a direct comparison
616 // doesn't work unless we mask the value off. Mips doesn't have this
617 // flag, so skip this on that platform.
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700618#if defined(SA_RESTORER)
Christopher Ferris3cdbfdc2014-11-08 15:57:11 -0800619 cur_action.sa_flags &= ~SA_RESTORER;
620 new_action.sa_flags &= ~SA_RESTORER;
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700621#elif defined(__GLIBC__)
622 // Our host compiler doesn't appear to define this flag for some reason.
623 cur_action.sa_flags &= ~0x04000000;
624 new_action.sa_flags &= ~0x04000000;
Christopher Ferris3cdbfdc2014-11-08 15:57:11 -0800625#endif
Christopher Ferris17e91d42013-10-21 13:30:52 -0700626 EXPECT_EQ(cur_action.sa_flags, new_action.sa_flags);
627}
628
Christopher Ferris50d81ac2018-06-29 16:05:35 -0700629TEST_F(BacktraceTest, thread_ignore_frames) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700630 pthread_attr_t attr;
631 pthread_attr_init(&attr);
632 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
633
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700634 thread_t thread_data = { 0, 0, 0, nullptr };
Christopher Ferris17e91d42013-10-21 13:30:52 -0700635 pthread_t thread;
636 ASSERT_TRUE(pthread_create(&thread, &attr, ThreadLevelRun, &thread_data) == 0);
637
638 // Wait up to 2 seconds for the tid to be set.
639 ASSERT_TRUE(WaitForNonZero(&thread_data.state, 2));
640
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700641 std::unique_ptr<Backtrace> all(Backtrace::Create(getpid(), thread_data.tid));
642 ASSERT_TRUE(all.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800643 ASSERT_TRUE(all->Unwind(0));
Christopher Ferrisc8bec5a2017-12-11 17:44:33 -0800644 VERIFY_NO_ERROR(all->GetError().error_code);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700645
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700646 std::unique_ptr<Backtrace> ign1(Backtrace::Create(getpid(), thread_data.tid));
647 ASSERT_TRUE(ign1.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800648 ASSERT_TRUE(ign1->Unwind(1));
Christopher Ferrisc8bec5a2017-12-11 17:44:33 -0800649 VERIFY_NO_ERROR(ign1->GetError().error_code);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700650
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700651 std::unique_ptr<Backtrace> ign2(Backtrace::Create(getpid(), thread_data.tid));
652 ASSERT_TRUE(ign2.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800653 ASSERT_TRUE(ign2->Unwind(2));
Christopher Ferrisc8bec5a2017-12-11 17:44:33 -0800654 VERIFY_NO_ERROR(ign2->GetError().error_code);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700655
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700656 VerifyIgnoreFrames(all.get(), ign1.get(), ign2.get(), nullptr);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700657
658 // Tell the thread to exit its infinite loop.
659 android_atomic_acquire_store(0, &thread_data.state);
660}
661
Christopher Ferris82f3bbd2017-03-14 15:22:26 -0700662static void* ThreadMaxRun(void* data) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700663 thread_t* thread = reinterpret_cast<thread_t*>(data);
664
Elliott Hughes38488902018-07-11 11:13:16 -0700665 thread->tid = android::base::GetThreadId();
Christopher Ferris50d81ac2018-06-29 16:05:35 -0700666 EXPECT_NE(BacktraceTest::test_recursive_call_(MAX_BACKTRACE_FRAMES + 10, ThreadSetState, data),
667 0);
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700668 return nullptr;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700669}
670
Christopher Ferris50d81ac2018-06-29 16:05:35 -0700671TEST_F(BacktraceTest, thread_max_trace) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700672 pthread_attr_t attr;
673 pthread_attr_init(&attr);
674 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
675
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700676 thread_t thread_data = { 0, 0, 0, nullptr };
Christopher Ferris17e91d42013-10-21 13:30:52 -0700677 pthread_t thread;
678 ASSERT_TRUE(pthread_create(&thread, &attr, ThreadMaxRun, &thread_data) == 0);
679
680 // Wait for the tid to be set.
681 ASSERT_TRUE(WaitForNonZero(&thread_data.state, 2));
682
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700683 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), thread_data.tid));
684 ASSERT_TRUE(backtrace.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800685 ASSERT_TRUE(backtrace->Unwind(0));
Christopher Ferrisc8bec5a2017-12-11 17:44:33 -0800686 ASSERT_EQ(BACKTRACE_UNWIND_ERROR_EXCEED_MAX_FRAMES_LIMIT, backtrace->GetError().error_code);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700687
Christopher Ferris20303f82014-01-10 16:33:16 -0800688 VerifyMaxDump(backtrace.get());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700689
690 // Tell the thread to exit its infinite loop.
691 android_atomic_acquire_store(0, &thread_data.state);
692}
693
Christopher Ferris82f3bbd2017-03-14 15:22:26 -0700694static void* ThreadDump(void* data) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700695 dump_thread_t* dump = reinterpret_cast<dump_thread_t*>(data);
696 while (true) {
697 if (android_atomic_acquire_load(dump->now)) {
698 break;
699 }
700 }
701
Christopher Ferris17e91d42013-10-21 13:30:52 -0700702 // The status of the actual unwind will be checked elsewhere.
Christopher Ferrisbe788d82017-11-27 14:50:38 -0800703 dump->backtrace = Backtrace::Create(getpid(), dump->thread.tid, dump->map);
Christopher Ferris20303f82014-01-10 16:33:16 -0800704 dump->backtrace->Unwind(0);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700705
706 android_atomic_acquire_store(1, &dump->done);
707
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700708 return nullptr;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700709}
710
Christopher Ferrisbe788d82017-11-27 14:50:38 -0800711static void MultipleThreadDumpTest(bool share_map) {
712 // Dump NUM_THREADS simultaneously using the same map.
Christopher Ferris17e91d42013-10-21 13:30:52 -0700713 std::vector<thread_t> runners(NUM_THREADS);
714 std::vector<dump_thread_t> dumpers(NUM_THREADS);
715
716 pthread_attr_t attr;
717 pthread_attr_init(&attr);
718 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
719 for (size_t i = 0; i < NUM_THREADS; i++) {
720 // Launch the runners, they will spin in hard loops doing nothing.
721 runners[i].tid = 0;
722 runners[i].state = 0;
723 ASSERT_TRUE(pthread_create(&runners[i].threadId, &attr, ThreadMaxRun, &runners[i]) == 0);
724 }
725
726 // Wait for tids to be set.
727 for (std::vector<thread_t>::iterator it = runners.begin(); it != runners.end(); ++it) {
Christopher Ferris3cdbfdc2014-11-08 15:57:11 -0800728 ASSERT_TRUE(WaitForNonZero(&it->state, 30));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700729 }
730
731 // Start all of the dumpers at once, they will spin until they are signalled
732 // to begin their dump run.
Christopher Ferrisbe788d82017-11-27 14:50:38 -0800733 std::unique_ptr<BacktraceMap> map;
734 if (share_map) {
735 map.reset(BacktraceMap::Create(getpid()));
736 }
Christopher Ferris17e91d42013-10-21 13:30:52 -0700737 int32_t dump_now = 0;
738 for (size_t i = 0; i < NUM_THREADS; i++) {
739 dumpers[i].thread.tid = runners[i].tid;
740 dumpers[i].thread.state = 0;
741 dumpers[i].done = 0;
742 dumpers[i].now = &dump_now;
Christopher Ferrisbe788d82017-11-27 14:50:38 -0800743 dumpers[i].map = map.get();
Christopher Ferris17e91d42013-10-21 13:30:52 -0700744
745 ASSERT_TRUE(pthread_create(&dumpers[i].thread.threadId, &attr, ThreadDump, &dumpers[i]) == 0);
746 }
747
748 // Start all of the dumpers going at once.
749 android_atomic_acquire_store(1, &dump_now);
750
751 for (size_t i = 0; i < NUM_THREADS; i++) {
Christopher Ferris3cdbfdc2014-11-08 15:57:11 -0800752 ASSERT_TRUE(WaitForNonZero(&dumpers[i].done, 30));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700753
754 // Tell the runner thread to exit its infinite loop.
755 android_atomic_acquire_store(0, &runners[i].state);
756
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700757 ASSERT_TRUE(dumpers[i].backtrace != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800758 VerifyMaxDump(dumpers[i].backtrace);
759
760 delete dumpers[i].backtrace;
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700761 dumpers[i].backtrace = nullptr;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700762 }
763}
764
Christopher Ferris50d81ac2018-06-29 16:05:35 -0700765TEST_F(BacktraceTest, thread_multiple_dump) {
Christopher Ferrisbe788d82017-11-27 14:50:38 -0800766 MultipleThreadDumpTest(false);
767}
Christopher Ferrisa2efd3a2014-05-06 15:23:59 -0700768
Christopher Ferris50d81ac2018-06-29 16:05:35 -0700769TEST_F(BacktraceTest, thread_multiple_dump_same_map) {
Christopher Ferrisbe788d82017-11-27 14:50:38 -0800770 MultipleThreadDumpTest(true);
Christopher Ferrisa2efd3a2014-05-06 15:23:59 -0700771}
772
Christopher Ferrisdf290612014-01-22 19:21:07 -0800773// This test is for UnwindMaps that should share the same map cursor when
774// multiple maps are created for the current process at the same time.
Christopher Ferris50d81ac2018-06-29 16:05:35 -0700775TEST_F(BacktraceTest, simultaneous_maps) {
Christopher Ferrisdf290612014-01-22 19:21:07 -0800776 BacktraceMap* map1 = BacktraceMap::Create(getpid());
777 BacktraceMap* map2 = BacktraceMap::Create(getpid());
778 BacktraceMap* map3 = BacktraceMap::Create(getpid());
779
780 Backtrace* back1 = Backtrace::Create(getpid(), BACKTRACE_CURRENT_THREAD, map1);
Christopher Ferris97e00bb2015-04-02 14:22:31 -0700781 ASSERT_TRUE(back1 != nullptr);
Christopher Ferrisdf290612014-01-22 19:21:07 -0800782 EXPECT_TRUE(back1->Unwind(0));
Christopher Ferrisc8bec5a2017-12-11 17:44:33 -0800783 VERIFY_NO_ERROR(back1->GetError().error_code);
Christopher Ferrisdf290612014-01-22 19:21:07 -0800784 delete back1;
785 delete map1;
786
787 Backtrace* back2 = Backtrace::Create(getpid(), BACKTRACE_CURRENT_THREAD, map2);
Christopher Ferris97e00bb2015-04-02 14:22:31 -0700788 ASSERT_TRUE(back2 != nullptr);
Christopher Ferrisdf290612014-01-22 19:21:07 -0800789 EXPECT_TRUE(back2->Unwind(0));
Christopher Ferrisc8bec5a2017-12-11 17:44:33 -0800790 VERIFY_NO_ERROR(back2->GetError().error_code);
Christopher Ferrisdf290612014-01-22 19:21:07 -0800791 delete back2;
792 delete map2;
793
794 Backtrace* back3 = Backtrace::Create(getpid(), BACKTRACE_CURRENT_THREAD, map3);
Christopher Ferris97e00bb2015-04-02 14:22:31 -0700795 ASSERT_TRUE(back3 != nullptr);
Christopher Ferrisdf290612014-01-22 19:21:07 -0800796 EXPECT_TRUE(back3->Unwind(0));
Christopher Ferrisc8bec5a2017-12-11 17:44:33 -0800797 VERIFY_NO_ERROR(back3->GetError().error_code);
Christopher Ferrisdf290612014-01-22 19:21:07 -0800798 delete back3;
799 delete map3;
800}
801
Christopher Ferris50d81ac2018-06-29 16:05:35 -0700802TEST_F(BacktraceTest, fillin_erases) {
Christopher Ferris12385e32015-02-06 13:22:01 -0800803 BacktraceMap* back_map = BacktraceMap::Create(getpid());
804
805 backtrace_map_t map;
806
807 map.start = 1;
808 map.end = 3;
809 map.flags = 1;
810 map.name = "Initialized";
811 back_map->FillIn(0, &map);
812 delete back_map;
813
814 ASSERT_FALSE(BacktraceMap::IsValid(map));
Christopher Ferris7937a362018-01-18 11:15:49 -0800815 ASSERT_EQ(static_cast<uint64_t>(0), map.start);
816 ASSERT_EQ(static_cast<uint64_t>(0), map.end);
Christopher Ferris12385e32015-02-06 13:22:01 -0800817 ASSERT_EQ(0, map.flags);
818 ASSERT_EQ("", map.name);
819}
820
Christopher Ferris50d81ac2018-06-29 16:05:35 -0700821TEST_F(BacktraceTest, format_test) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700822 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), BACKTRACE_CURRENT_THREAD));
823 ASSERT_TRUE(backtrace.get() != nullptr);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700824
Christopher Ferris20303f82014-01-10 16:33:16 -0800825 backtrace_frame_data_t frame;
Christopher Ferris46756822014-01-14 20:16:30 -0800826 frame.num = 1;
827 frame.pc = 2;
Christopher Ferris96722b02017-07-19 14:20:46 -0700828 frame.rel_pc = 2;
Christopher Ferris46756822014-01-14 20:16:30 -0800829 frame.sp = 0;
830 frame.stack_size = 0;
Christopher Ferris46756822014-01-14 20:16:30 -0800831 frame.func_offset = 0;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700832
Christopher Ferris46756822014-01-14 20:16:30 -0800833 // Check no map set.
Christopher Ferris20303f82014-01-10 16:33:16 -0800834 frame.num = 1;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700835#if defined(__LP64__)
Christopher Ferris46756822014-01-14 20:16:30 -0800836 EXPECT_EQ("#01 pc 0000000000000002 <unknown>",
Christopher Ferris17e91d42013-10-21 13:30:52 -0700837#else
Christopher Ferris46756822014-01-14 20:16:30 -0800838 EXPECT_EQ("#01 pc 00000002 <unknown>",
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 map name empty, but exists.
Christopher Ferrisda750a72015-11-30 13:36:08 -0800843 frame.pc = 0xb0020;
Christopher Ferris96722b02017-07-19 14:20:46 -0700844 frame.rel_pc = 0x20;
Christopher Ferrisda750a72015-11-30 13:36:08 -0800845 frame.map.start = 0xb0000;
846 frame.map.end = 0xbffff;
Christopher Ferris96722b02017-07-19 14:20:46 -0700847 frame.map.load_bias = 0;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700848#if defined(__LP64__)
Christopher Ferrisda750a72015-11-30 13:36:08 -0800849 EXPECT_EQ("#01 pc 0000000000000020 <anonymous:00000000000b0000>",
Christopher Ferris17e91d42013-10-21 13:30:52 -0700850#else
Christopher Ferrisda750a72015-11-30 13:36:08 -0800851 EXPECT_EQ("#01 pc 00000020 <anonymous:000b0000>",
Christopher Ferris17e91d42013-10-21 13:30:52 -0700852#endif
Christopher Ferris46756822014-01-14 20:16:30 -0800853 backtrace->FormatFrameData(&frame));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700854
Christopher Ferrisda750a72015-11-30 13:36:08 -0800855 // Check map name begins with a [.
856 frame.pc = 0xc0020;
857 frame.map.start = 0xc0000;
858 frame.map.end = 0xcffff;
Christopher Ferris96722b02017-07-19 14:20:46 -0700859 frame.map.load_bias = 0;
Christopher Ferrisda750a72015-11-30 13:36:08 -0800860 frame.map.name = "[anon:thread signal stack]";
861#if defined(__LP64__)
862 EXPECT_EQ("#01 pc 0000000000000020 [anon:thread signal stack:00000000000c0000]",
863#else
864 EXPECT_EQ("#01 pc 00000020 [anon:thread signal stack:000c0000]",
865#endif
866 backtrace->FormatFrameData(&frame));
Christopher Ferris46756822014-01-14 20:16:30 -0800867
868 // Check relative pc is set and map name is set.
869 frame.pc = 0x12345679;
Christopher Ferris96722b02017-07-19 14:20:46 -0700870 frame.rel_pc = 0x12345678;
Christopher Ferris12385e32015-02-06 13:22:01 -0800871 frame.map.name = "MapFake";
872 frame.map.start = 1;
873 frame.map.end = 1;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700874#if defined(__LP64__)
Christopher Ferris46756822014-01-14 20:16:30 -0800875 EXPECT_EQ("#01 pc 0000000012345678 MapFake",
Christopher Ferris17e91d42013-10-21 13:30:52 -0700876#else
Christopher Ferris46756822014-01-14 20:16:30 -0800877 EXPECT_EQ("#01 pc 12345678 MapFake",
Christopher Ferris17e91d42013-10-21 13:30:52 -0700878#endif
Christopher Ferris46756822014-01-14 20:16:30 -0800879 backtrace->FormatFrameData(&frame));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700880
Christopher Ferris46756822014-01-14 20:16:30 -0800881 // Check func_name is set, but no func offset.
882 frame.func_name = "ProcFake";
883#if defined(__LP64__)
884 EXPECT_EQ("#01 pc 0000000012345678 MapFake (ProcFake)",
885#else
886 EXPECT_EQ("#01 pc 12345678 MapFake (ProcFake)",
887#endif
888 backtrace->FormatFrameData(&frame));
889
890 // Check func_name is set, and func offset is non-zero.
Christopher Ferris20303f82014-01-10 16:33:16 -0800891 frame.func_offset = 645;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700892#if defined(__LP64__)
Christopher Ferris46756822014-01-14 20:16:30 -0800893 EXPECT_EQ("#01 pc 0000000012345678 MapFake (ProcFake+645)",
Christopher Ferris17e91d42013-10-21 13:30:52 -0700894#else
Christopher Ferris46756822014-01-14 20:16:30 -0800895 EXPECT_EQ("#01 pc 12345678 MapFake (ProcFake+645)",
Christopher Ferris17e91d42013-10-21 13:30:52 -0700896#endif
Christopher Ferris46756822014-01-14 20:16:30 -0800897 backtrace->FormatFrameData(&frame));
Christopher Ferris2106f4b2015-05-01 15:02:03 -0700898
Christopher Ferris96722b02017-07-19 14:20:46 -0700899 // Check func_name is set, func offset is non-zero, and load_bias is non-zero.
900 frame.rel_pc = 0x123456dc;
Christopher Ferris2106f4b2015-05-01 15:02:03 -0700901 frame.func_offset = 645;
Christopher Ferris96722b02017-07-19 14:20:46 -0700902 frame.map.load_bias = 100;
Christopher Ferris2106f4b2015-05-01 15:02:03 -0700903#if defined(__LP64__)
904 EXPECT_EQ("#01 pc 00000000123456dc MapFake (ProcFake+645)",
905#else
906 EXPECT_EQ("#01 pc 123456dc MapFake (ProcFake+645)",
907#endif
908 backtrace->FormatFrameData(&frame));
Christopher Ferrise0ab2322015-08-20 11:16:54 -0700909
910 // Check a non-zero map offset.
911 frame.map.offset = 0x1000;
912#if defined(__LP64__)
913 EXPECT_EQ("#01 pc 00000000123456dc MapFake (offset 0x1000) (ProcFake+645)",
914#else
915 EXPECT_EQ("#01 pc 123456dc MapFake (offset 0x1000) (ProcFake+645)",
916#endif
917 backtrace->FormatFrameData(&frame));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700918}
Christopher Ferrise2960912014-03-07 19:42:19 -0800919
920struct map_test_t {
Christopher Ferris7937a362018-01-18 11:15:49 -0800921 uint64_t start;
922 uint64_t end;
Christopher Ferrise2960912014-03-07 19:42:19 -0800923};
924
Christopher Ferris82f3bbd2017-03-14 15:22:26 -0700925static bool map_sort(map_test_t i, map_test_t j) { return i.start < j.start; }
Christopher Ferrise2960912014-03-07 19:42:19 -0800926
Christopher Ferrisb7de5f52017-12-01 21:37:37 -0800927static std::string GetTestMapsAsString(const std::vector<map_test_t>& maps) {
928 if (maps.size() == 0) {
929 return "No test map entries\n";
930 }
931 std::string map_txt;
932 for (auto map : maps) {
Christopher Ferris7937a362018-01-18 11:15:49 -0800933 map_txt += android::base::StringPrintf("%" PRIx64 "-%" PRIx64 "\n", map.start, map.end);
Christopher Ferrisb7de5f52017-12-01 21:37:37 -0800934 }
935 return map_txt;
936}
937
938static std::string GetMapsAsString(BacktraceMap* maps) {
939 if (maps->size() == 0) {
940 return "No map entries\n";
941 }
942 std::string map_txt;
943 for (const backtrace_map_t* map : *maps) {
944 map_txt += android::base::StringPrintf(
Christopher Ferris7937a362018-01-18 11:15:49 -0800945 "%" PRIx64 "-%" PRIx64 " flags: 0x%x offset: 0x%" PRIx64 " load_bias: 0x%" PRIx64,
Christopher Ferrisb7de5f52017-12-01 21:37:37 -0800946 map->start, map->end, map->flags, map->offset, map->load_bias);
947 if (!map->name.empty()) {
948 map_txt += ' ' + map->name;
949 }
950 map_txt += '\n';
951 }
952 return map_txt;
953}
954
Christopher Ferris82f3bbd2017-03-14 15:22:26 -0700955static void VerifyMap(pid_t pid) {
Christopher Ferrise2960912014-03-07 19:42:19 -0800956 char buffer[4096];
957 snprintf(buffer, sizeof(buffer), "/proc/%d/maps", pid);
958
959 FILE* map_file = fopen(buffer, "r");
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700960 ASSERT_TRUE(map_file != nullptr);
Christopher Ferrise2960912014-03-07 19:42:19 -0800961 std::vector<map_test_t> test_maps;
962 while (fgets(buffer, sizeof(buffer), map_file)) {
963 map_test_t map;
Christopher Ferris7937a362018-01-18 11:15:49 -0800964 ASSERT_EQ(2, sscanf(buffer, "%" SCNx64 "-%" SCNx64 " ", &map.start, &map.end));
Christopher Ferrise2960912014-03-07 19:42:19 -0800965 test_maps.push_back(map);
966 }
967 fclose(map_file);
968 std::sort(test_maps.begin(), test_maps.end(), map_sort);
969
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700970 std::unique_ptr<BacktraceMap> map(BacktraceMap::Create(pid));
Christopher Ferrise2960912014-03-07 19:42:19 -0800971
972 // Basic test that verifies that the map is in the expected order.
Christopher Ferrisb7de5f52017-12-01 21:37:37 -0800973 auto test_it = test_maps.begin();
974 for (auto it = map->begin(); it != map->end(); ++it) {
975 ASSERT_TRUE(test_it != test_maps.end()) << "Mismatch in number of maps, expected test maps:\n"
976 << GetTestMapsAsString(test_maps) << "Actual maps:\n"
977 << GetMapsAsString(map.get());
978 ASSERT_EQ(test_it->start, (*it)->start) << "Mismatch in map data, expected test maps:\n"
979 << GetTestMapsAsString(test_maps) << "Actual maps:\n"
980 << GetMapsAsString(map.get());
981 ASSERT_EQ(test_it->end, (*it)->end) << "Mismatch maps in map data, expected test maps:\n"
982 << GetTestMapsAsString(test_maps) << "Actual maps:\n"
983 << GetMapsAsString(map.get());
984 // Make sure the load bias get set to a value.
985 ASSERT_NE(static_cast<uint64_t>(-1), (*it)->load_bias) << "Found uninitialized load_bias\n"
986 << GetMapsAsString(map.get());
Christopher Ferrise2960912014-03-07 19:42:19 -0800987 ++test_it;
988 }
989 ASSERT_TRUE(test_it == test_maps.end());
990}
991
Christopher Ferris50d81ac2018-06-29 16:05:35 -0700992TEST_F(BacktraceTest, verify_map_remote) {
Christopher Ferrise2960912014-03-07 19:42:19 -0800993 pid_t pid;
Christopher Ferris82f3bbd2017-03-14 15:22:26 -0700994 CreateRemoteProcess(&pid);
Christopher Ferrise2960912014-03-07 19:42:19 -0800995
996 // The maps should match exactly since the forked process has been paused.
997 VerifyMap(pid);
998
Christopher Ferris82f3bbd2017-03-14 15:22:26 -0700999 FinishRemoteProcess(pid);
Christopher Ferris2b4a63f2015-03-17 14:42:03 -07001000}
1001
Christopher Ferris82f3bbd2017-03-14 15:22:26 -07001002static void InitMemory(uint8_t* memory, size_t bytes) {
Christopher Ferris944f4172015-05-06 16:36:34 -07001003 for (size_t i = 0; i < bytes; i++) {
1004 memory[i] = i;
1005 if (memory[i] == '\0') {
1006 // Don't use '\0' in our data so we can verify that an overread doesn't
1007 // occur by using a '\0' as the character after the read data.
1008 memory[i] = 23;
1009 }
1010 }
1011}
1012
Christopher Ferris82f3bbd2017-03-14 15:22:26 -07001013static void* ThreadReadTest(void* data) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -07001014 thread_t* thread_data = reinterpret_cast<thread_t*>(data);
1015
Elliott Hughes38488902018-07-11 11:13:16 -07001016 thread_data->tid = android::base::GetThreadId();
Christopher Ferris2b4a63f2015-03-17 14:42:03 -07001017
1018 // Create two map pages.
1019 // Mark the second page as not-readable.
1020 size_t pagesize = static_cast<size_t>(sysconf(_SC_PAGE_SIZE));
1021 uint8_t* memory;
1022 if (posix_memalign(reinterpret_cast<void**>(&memory), pagesize, 2 * pagesize) != 0) {
1023 return reinterpret_cast<void*>(-1);
1024 }
1025
1026 if (mprotect(&memory[pagesize], pagesize, PROT_NONE) != 0) {
1027 return reinterpret_cast<void*>(-1);
1028 }
1029
1030 // Set up a simple pattern in memory.
Christopher Ferris944f4172015-05-06 16:36:34 -07001031 InitMemory(memory, pagesize);
Christopher Ferris2b4a63f2015-03-17 14:42:03 -07001032
1033 thread_data->data = memory;
1034
1035 // Tell the caller it's okay to start reading memory.
1036 android_atomic_acquire_store(1, &thread_data->state);
1037
Christopher Ferris2c43cff2015-03-26 19:18:36 -07001038 // Loop waiting for the caller to finish reading the memory.
Christopher Ferris2b4a63f2015-03-17 14:42:03 -07001039 while (thread_data->state) {
1040 }
1041
Christopher Ferris2c43cff2015-03-26 19:18:36 -07001042 // Re-enable read-write on the page so that we don't crash if we try
1043 // and access data on this page when freeing the memory.
1044 if (mprotect(&memory[pagesize], pagesize, PROT_READ | PROT_WRITE) != 0) {
1045 return reinterpret_cast<void*>(-1);
1046 }
Christopher Ferris2b4a63f2015-03-17 14:42:03 -07001047 free(memory);
1048
1049 android_atomic_acquire_store(1, &thread_data->state);
1050
1051 return nullptr;
1052}
1053
Christopher Ferris7937a362018-01-18 11:15:49 -08001054static void RunReadTest(Backtrace* backtrace, uint64_t read_addr) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -07001055 size_t pagesize = static_cast<size_t>(sysconf(_SC_PAGE_SIZE));
1056
1057 // Create a page of data to use to do quick compares.
1058 uint8_t* expected = new uint8_t[pagesize];
Christopher Ferris944f4172015-05-06 16:36:34 -07001059 InitMemory(expected, pagesize);
1060
Christopher Ferrisc8bec5a2017-12-11 17:44:33 -08001061 uint8_t* data = new uint8_t[2 * pagesize];
Christopher Ferris2b4a63f2015-03-17 14:42:03 -07001062 // Verify that we can only read one page worth of data.
1063 size_t bytes_read = backtrace->Read(read_addr, data, 2 * pagesize);
1064 ASSERT_EQ(pagesize, bytes_read);
1065 ASSERT_TRUE(memcmp(data, expected, pagesize) == 0);
1066
1067 // Verify unaligned reads.
1068 for (size_t i = 1; i < sizeof(word_t); i++) {
1069 bytes_read = backtrace->Read(read_addr + i, data, 2 * sizeof(word_t));
1070 ASSERT_EQ(2 * sizeof(word_t), bytes_read);
1071 ASSERT_TRUE(memcmp(data, &expected[i], 2 * sizeof(word_t)) == 0)
1072 << "Offset at " << i << " failed";
1073 }
Christopher Ferris944f4172015-05-06 16:36:34 -07001074
1075 // Verify small unaligned reads.
1076 for (size_t i = 1; i < sizeof(word_t); i++) {
1077 for (size_t j = 1; j < sizeof(word_t); j++) {
1078 // Set one byte past what we expect to read, to guarantee we don't overread.
1079 data[j] = '\0';
1080 bytes_read = backtrace->Read(read_addr + i, data, j);
1081 ASSERT_EQ(j, bytes_read);
1082 ASSERT_TRUE(memcmp(data, &expected[i], j) == 0)
1083 << "Offset at " << i << " length " << j << " miscompared";
1084 ASSERT_EQ('\0', data[j])
1085 << "Offset at " << i << " length " << j << " wrote too much data";
1086 }
1087 }
Pirama Arumuga Nainar837eff22015-07-09 10:50:04 -07001088 delete[] data;
1089 delete[] expected;
Christopher Ferris2b4a63f2015-03-17 14:42:03 -07001090}
1091
Christopher Ferris50d81ac2018-06-29 16:05:35 -07001092TEST_F(BacktraceTest, thread_read) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -07001093 pthread_attr_t attr;
1094 pthread_attr_init(&attr);
1095 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
1096 pthread_t thread;
1097 thread_t thread_data = { 0, 0, 0, nullptr };
1098 ASSERT_TRUE(pthread_create(&thread, &attr, ThreadReadTest, &thread_data) == 0);
1099
1100 ASSERT_TRUE(WaitForNonZero(&thread_data.state, 10));
1101
1102 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), thread_data.tid));
1103 ASSERT_TRUE(backtrace.get() != nullptr);
1104
Christopher Ferris7937a362018-01-18 11:15:49 -08001105 RunReadTest(backtrace.get(), reinterpret_cast<uint64_t>(thread_data.data));
Christopher Ferris2b4a63f2015-03-17 14:42:03 -07001106
1107 android_atomic_acquire_store(0, &thread_data.state);
1108
1109 ASSERT_TRUE(WaitForNonZero(&thread_data.state, 10));
1110}
1111
Christopher Ferrisc8bec5a2017-12-11 17:44:33 -08001112// The code requires these variables are the same size.
Christopher Ferris7937a362018-01-18 11:15:49 -08001113volatile uint64_t g_ready = 0;
1114volatile uint64_t g_addr = 0;
Christopher Ferrisc8bec5a2017-12-11 17:44:33 -08001115static_assert(sizeof(g_ready) == sizeof(g_addr), "g_ready/g_addr must be same size");
Christopher Ferris2b4a63f2015-03-17 14:42:03 -07001116
Christopher Ferris82f3bbd2017-03-14 15:22:26 -07001117static void ForkedReadTest() {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -07001118 // Create two map pages.
1119 size_t pagesize = static_cast<size_t>(sysconf(_SC_PAGE_SIZE));
1120 uint8_t* memory;
1121 if (posix_memalign(reinterpret_cast<void**>(&memory), pagesize, 2 * pagesize) != 0) {
1122 perror("Failed to allocate memory\n");
1123 exit(1);
1124 }
1125
1126 // Mark the second page as not-readable.
1127 if (mprotect(&memory[pagesize], pagesize, PROT_NONE) != 0) {
1128 perror("Failed to mprotect memory\n");
1129 exit(1);
1130 }
1131
1132 // Set up a simple pattern in memory.
Christopher Ferris944f4172015-05-06 16:36:34 -07001133 InitMemory(memory, pagesize);
Christopher Ferris2b4a63f2015-03-17 14:42:03 -07001134
Christopher Ferris7937a362018-01-18 11:15:49 -08001135 g_addr = reinterpret_cast<uint64_t>(memory);
Christopher Ferris2b4a63f2015-03-17 14:42:03 -07001136 g_ready = 1;
1137
1138 while (1) {
1139 usleep(US_PER_MSEC);
1140 }
1141}
1142
Christopher Ferris50d81ac2018-06-29 16:05:35 -07001143TEST_F(BacktraceTest, process_read) {
Christopher Ferris67aba682015-05-08 15:44:46 -07001144 g_ready = 0;
Christopher Ferris2b4a63f2015-03-17 14:42:03 -07001145 pid_t pid;
1146 if ((pid = fork()) == 0) {
1147 ForkedReadTest();
1148 exit(0);
1149 }
1150 ASSERT_NE(-1, pid);
1151
1152 bool test_executed = false;
1153 uint64_t start = NanoTime();
1154 while (1) {
1155 if (ptrace(PTRACE_ATTACH, pid, 0, 0) == 0) {
1156 WaitForStop(pid);
1157
1158 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(pid, pid));
Christopher Ferris97e00bb2015-04-02 14:22:31 -07001159 ASSERT_TRUE(backtrace.get() != nullptr);
Christopher Ferris2b4a63f2015-03-17 14:42:03 -07001160
Christopher Ferris7937a362018-01-18 11:15:49 -08001161 uint64_t read_addr;
1162 size_t bytes_read = backtrace->Read(reinterpret_cast<uint64_t>(&g_ready),
Christopher Ferrisc8bec5a2017-12-11 17:44:33 -08001163 reinterpret_cast<uint8_t*>(&read_addr), sizeof(g_ready));
1164 ASSERT_EQ(sizeof(g_ready), bytes_read);
Christopher Ferris2b4a63f2015-03-17 14:42:03 -07001165 if (read_addr) {
1166 // The forked process is ready to be read.
Christopher Ferris7937a362018-01-18 11:15:49 -08001167 bytes_read = backtrace->Read(reinterpret_cast<uint64_t>(&g_addr),
Christopher Ferrisc8bec5a2017-12-11 17:44:33 -08001168 reinterpret_cast<uint8_t*>(&read_addr), sizeof(g_addr));
1169 ASSERT_EQ(sizeof(g_addr), bytes_read);
Christopher Ferris2b4a63f2015-03-17 14:42:03 -07001170
1171 RunReadTest(backtrace.get(), read_addr);
1172
1173 test_executed = true;
1174 break;
1175 }
1176 ASSERT_TRUE(ptrace(PTRACE_DETACH, pid, 0, 0) == 0);
1177 }
1178 if ((NanoTime() - start) > 5 * NS_PER_SEC) {
1179 break;
1180 }
1181 usleep(US_PER_MSEC);
1182 }
1183 kill(pid, SIGKILL);
1184 ASSERT_EQ(waitpid(pid, nullptr, 0), pid);
1185
1186 ASSERT_TRUE(test_executed);
Christopher Ferrise2960912014-03-07 19:42:19 -08001187}
1188
Christopher Ferris82f3bbd2017-03-14 15:22:26 -07001189static void VerifyFunctionsFound(const std::vector<std::string>& found_functions) {
Christopher Ferris67aba682015-05-08 15:44:46 -07001190 // We expect to find these functions in libbacktrace_test. If we don't
1191 // find them, that's a bug in the memory read handling code in libunwind.
1192 std::list<std::string> expected_functions;
1193 expected_functions.push_back("test_recursive_call");
1194 expected_functions.push_back("test_level_one");
1195 expected_functions.push_back("test_level_two");
1196 expected_functions.push_back("test_level_three");
1197 expected_functions.push_back("test_level_four");
1198 for (const auto& found_function : found_functions) {
1199 for (const auto& expected_function : expected_functions) {
1200 if (found_function == expected_function) {
1201 expected_functions.remove(found_function);
1202 break;
1203 }
1204 }
1205 }
1206 ASSERT_TRUE(expected_functions.empty()) << "Not all functions found in shared library.";
1207}
1208
Christopher Ferris3acf5772018-05-24 16:01:55 -07001209static void CopySharedLibrary(const char* tmp_dir, std::string* tmp_so_name) {
Christopher Ferris50d81ac2018-06-29 16:05:35 -07001210 std::string test_lib(testing::internal::GetArgvs()[0]);
1211 auto const value = test_lib.find_last_of('/');
1212 if (value == std::string::npos) {
1213 test_lib = "../backtrace_test_libs/";
1214 } else {
1215 test_lib = test_lib.substr(0, value + 1) + "../backtrace_test_libs/";
1216 }
1217 test_lib += "libbacktrace_test.so";
Christopher Ferris67aba682015-05-08 15:44:46 -07001218
Christopher Ferris3acf5772018-05-24 16:01:55 -07001219 *tmp_so_name = std::string(tmp_dir) + "/libbacktrace_test.so";
Christopher Ferris50d81ac2018-06-29 16:05:35 -07001220 std::string cp_cmd = android::base::StringPrintf("cp %s %s", test_lib.c_str(), tmp_dir);
Christopher Ferris3acf5772018-05-24 16:01:55 -07001221
1222 // Copy the shared so to a tempory directory.
1223 ASSERT_EQ(0, system(cp_cmd.c_str()));
Christopher Ferris67aba682015-05-08 15:44:46 -07001224}
1225
Christopher Ferris50d81ac2018-06-29 16:05:35 -07001226TEST_F(BacktraceTest, check_unreadable_elf_local) {
Christopher Ferris3acf5772018-05-24 16:01:55 -07001227 TemporaryDir td;
1228 std::string tmp_so_name;
1229 ASSERT_NO_FATAL_FAILURE(CopySharedLibrary(td.path, &tmp_so_name));
Christopher Ferris67aba682015-05-08 15:44:46 -07001230
1231 struct stat buf;
Christopher Ferris3acf5772018-05-24 16:01:55 -07001232 ASSERT_TRUE(stat(tmp_so_name.c_str(), &buf) != -1);
Christopher Ferris7937a362018-01-18 11:15:49 -08001233 uint64_t map_size = buf.st_size;
Christopher Ferris67aba682015-05-08 15:44:46 -07001234
Christopher Ferris3acf5772018-05-24 16:01:55 -07001235 int fd = open(tmp_so_name.c_str(), O_RDONLY);
Christopher Ferris67aba682015-05-08 15:44:46 -07001236 ASSERT_TRUE(fd != -1);
1237
Christopher Ferrisc8bec5a2017-12-11 17:44:33 -08001238 void* map = mmap(nullptr, map_size, PROT_READ | PROT_EXEC, MAP_PRIVATE, fd, 0);
Christopher Ferris67aba682015-05-08 15:44:46 -07001239 ASSERT_TRUE(map != MAP_FAILED);
1240 close(fd);
Christopher Ferris3acf5772018-05-24 16:01:55 -07001241 ASSERT_TRUE(unlink(tmp_so_name.c_str()) != -1);
Christopher Ferris67aba682015-05-08 15:44:46 -07001242
1243 std::vector<std::string> found_functions;
1244 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(BACKTRACE_CURRENT_PROCESS,
1245 BACKTRACE_CURRENT_THREAD));
1246 ASSERT_TRUE(backtrace.get() != nullptr);
1247
1248 // Needed before GetFunctionName will work.
1249 backtrace->Unwind(0);
1250
1251 // Loop through the entire map, and get every function we can find.
Christopher Ferris7937a362018-01-18 11:15:49 -08001252 map_size += reinterpret_cast<uint64_t>(map);
Christopher Ferris67aba682015-05-08 15:44:46 -07001253 std::string last_func;
Christopher Ferris7937a362018-01-18 11:15:49 -08001254 for (uint64_t read_addr = reinterpret_cast<uint64_t>(map); read_addr < map_size; read_addr += 4) {
1255 uint64_t offset;
Christopher Ferris67aba682015-05-08 15:44:46 -07001256 std::string func_name = backtrace->GetFunctionName(read_addr, &offset);
1257 if (!func_name.empty() && last_func != func_name) {
1258 found_functions.push_back(func_name);
1259 }
1260 last_func = func_name;
1261 }
1262
Christopher Ferris7937a362018-01-18 11:15:49 -08001263 ASSERT_TRUE(munmap(map, map_size - reinterpret_cast<uint64_t>(map)) == 0);
Christopher Ferris67aba682015-05-08 15:44:46 -07001264
1265 VerifyFunctionsFound(found_functions);
1266}
1267
Christopher Ferris50d81ac2018-06-29 16:05:35 -07001268TEST_F(BacktraceTest, check_unreadable_elf_remote) {
Christopher Ferris3acf5772018-05-24 16:01:55 -07001269 TemporaryDir td;
1270 std::string tmp_so_name;
1271 ASSERT_NO_FATAL_FAILURE(CopySharedLibrary(td.path, &tmp_so_name));
Christopher Ferris67aba682015-05-08 15:44:46 -07001272
1273 g_ready = 0;
1274
1275 struct stat buf;
Christopher Ferris3acf5772018-05-24 16:01:55 -07001276 ASSERT_TRUE(stat(tmp_so_name.c_str(), &buf) != -1);
Christopher Ferris7937a362018-01-18 11:15:49 -08001277 uint64_t map_size = buf.st_size;
Christopher Ferris67aba682015-05-08 15:44:46 -07001278
1279 pid_t pid;
1280 if ((pid = fork()) == 0) {
Christopher Ferris3acf5772018-05-24 16:01:55 -07001281 int fd = open(tmp_so_name.c_str(), O_RDONLY);
Christopher Ferris67aba682015-05-08 15:44:46 -07001282 if (fd == -1) {
Christopher Ferris3acf5772018-05-24 16:01:55 -07001283 fprintf(stderr, "Failed to open file %s: %s\n", tmp_so_name.c_str(), strerror(errno));
1284 unlink(tmp_so_name.c_str());
Christopher Ferris67aba682015-05-08 15:44:46 -07001285 exit(0);
1286 }
1287
Christopher Ferrisc8bec5a2017-12-11 17:44:33 -08001288 void* map = mmap(nullptr, map_size, PROT_READ | PROT_EXEC, MAP_PRIVATE, fd, 0);
Christopher Ferris67aba682015-05-08 15:44:46 -07001289 if (map == MAP_FAILED) {
1290 fprintf(stderr, "Failed to map in memory: %s\n", strerror(errno));
Christopher Ferris3acf5772018-05-24 16:01:55 -07001291 unlink(tmp_so_name.c_str());
Christopher Ferris67aba682015-05-08 15:44:46 -07001292 exit(0);
1293 }
1294 close(fd);
Christopher Ferris3acf5772018-05-24 16:01:55 -07001295 if (unlink(tmp_so_name.c_str()) == -1) {
Christopher Ferris67aba682015-05-08 15:44:46 -07001296 fprintf(stderr, "Failed to unlink: %s\n", strerror(errno));
1297 exit(0);
1298 }
1299
Christopher Ferris7937a362018-01-18 11:15:49 -08001300 g_addr = reinterpret_cast<uint64_t>(map);
Christopher Ferris67aba682015-05-08 15:44:46 -07001301 g_ready = 1;
1302 while (true) {
1303 usleep(US_PER_MSEC);
1304 }
1305 exit(0);
1306 }
1307 ASSERT_TRUE(pid > 0);
1308
1309 std::vector<std::string> found_functions;
1310 uint64_t start = NanoTime();
1311 while (true) {
1312 ASSERT_TRUE(ptrace(PTRACE_ATTACH, pid, 0, 0) == 0);
1313
1314 // Wait for the process to get to a stopping point.
1315 WaitForStop(pid);
1316
1317 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(pid, BACKTRACE_CURRENT_THREAD));
1318 ASSERT_TRUE(backtrace.get() != nullptr);
1319
Christopher Ferris7937a362018-01-18 11:15:49 -08001320 uint64_t read_addr;
Christopher Ferrisc8bec5a2017-12-11 17:44:33 -08001321 ASSERT_EQ(sizeof(g_ready),
Christopher Ferris7937a362018-01-18 11:15:49 -08001322 backtrace->Read(reinterpret_cast<uint64_t>(&g_ready),
Christopher Ferrisc8bec5a2017-12-11 17:44:33 -08001323 reinterpret_cast<uint8_t*>(&read_addr), sizeof(g_ready)));
Christopher Ferris67aba682015-05-08 15:44:46 -07001324 if (read_addr) {
Christopher Ferrisc8bec5a2017-12-11 17:44:33 -08001325 ASSERT_EQ(sizeof(g_addr),
Christopher Ferris7937a362018-01-18 11:15:49 -08001326 backtrace->Read(reinterpret_cast<uint64_t>(&g_addr),
1327 reinterpret_cast<uint8_t*>(&read_addr), sizeof(uint64_t)));
Christopher Ferris67aba682015-05-08 15:44:46 -07001328
1329 // Needed before GetFunctionName will work.
1330 backtrace->Unwind(0);
1331
1332 // Loop through the entire map, and get every function we can find.
1333 map_size += read_addr;
1334 std::string last_func;
1335 for (; read_addr < map_size; read_addr += 4) {
Christopher Ferris7937a362018-01-18 11:15:49 -08001336 uint64_t offset;
Christopher Ferris67aba682015-05-08 15:44:46 -07001337 std::string func_name = backtrace->GetFunctionName(read_addr, &offset);
1338 if (!func_name.empty() && last_func != func_name) {
1339 found_functions.push_back(func_name);
1340 }
1341 last_func = func_name;
1342 }
1343 break;
1344 }
1345 ASSERT_TRUE(ptrace(PTRACE_DETACH, pid, 0, 0) == 0);
1346
1347 if ((NanoTime() - start) > 5 * NS_PER_SEC) {
1348 break;
1349 }
1350 usleep(US_PER_MSEC);
1351 }
1352
1353 kill(pid, SIGKILL);
1354 ASSERT_EQ(waitpid(pid, nullptr, 0), pid);
1355
1356 VerifyFunctionsFound(found_functions);
1357}
1358
Christopher Ferris7937a362018-01-18 11:15:49 -08001359static bool FindFuncFrameInBacktrace(Backtrace* backtrace, uint64_t test_func, size_t* frame_num) {
Christopher Ferris67aba682015-05-08 15:44:46 -07001360 backtrace_map_t map;
1361 backtrace->FillInMap(test_func, &map);
1362 if (!BacktraceMap::IsValid(map)) {
1363 return false;
1364 }
1365
1366 // Loop through the frames, and find the one that is in the map.
1367 *frame_num = 0;
1368 for (Backtrace::const_iterator it = backtrace->begin(); it != backtrace->end(); ++it) {
1369 if (BacktraceMap::IsValid(it->map) && map.start == it->map.start &&
1370 it->pc >= test_func) {
1371 *frame_num = it->num;
1372 return true;
1373 }
1374 }
1375 return false;
1376}
1377
Christopher Ferris7937a362018-01-18 11:15:49 -08001378static void VerifyUnreadableElfFrame(Backtrace* backtrace, uint64_t test_func, size_t frame_num) {
Christopher Ferris67aba682015-05-08 15:44:46 -07001379 ASSERT_LT(backtrace->NumFrames(), static_cast<size_t>(MAX_BACKTRACE_FRAMES))
1380 << DumpFrames(backtrace);
1381
1382 ASSERT_TRUE(frame_num != 0) << DumpFrames(backtrace);
1383 // Make sure that there is at least one more frame above the test func call.
1384 ASSERT_LT(frame_num, backtrace->NumFrames()) << DumpFrames(backtrace);
1385
Christopher Ferris7937a362018-01-18 11:15:49 -08001386 uint64_t diff = backtrace->GetFrame(frame_num)->pc - test_func;
Christopher Ferris67aba682015-05-08 15:44:46 -07001387 ASSERT_LT(diff, 200U) << DumpFrames(backtrace);
1388}
1389
Christopher Ferris7937a362018-01-18 11:15:49 -08001390static void VerifyUnreadableElfBacktrace(void* func) {
Christopher Ferris67aba682015-05-08 15:44:46 -07001391 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(BACKTRACE_CURRENT_PROCESS,
1392 BACKTRACE_CURRENT_THREAD));
1393 ASSERT_TRUE(backtrace.get() != nullptr);
1394 ASSERT_TRUE(backtrace->Unwind(0));
Christopher Ferrisc8bec5a2017-12-11 17:44:33 -08001395 VERIFY_NO_ERROR(backtrace->GetError().error_code);
Christopher Ferris67aba682015-05-08 15:44:46 -07001396
1397 size_t frame_num;
Christopher Ferrisc8bec5a2017-12-11 17:44:33 -08001398 uint64_t test_func = reinterpret_cast<uint64_t>(func);
1399 ASSERT_TRUE(FindFuncFrameInBacktrace(backtrace.get(), test_func, &frame_num))
1400 << DumpFrames(backtrace.get());
Christopher Ferris67aba682015-05-08 15:44:46 -07001401
1402 VerifyUnreadableElfFrame(backtrace.get(), test_func, frame_num);
1403}
1404
Christopher Ferris7937a362018-01-18 11:15:49 -08001405typedef int (*test_func_t)(int, int, int, int, void (*)(void*), void*);
Christopher Ferris67aba682015-05-08 15:44:46 -07001406
Christopher Ferris50d81ac2018-06-29 16:05:35 -07001407TEST_F(BacktraceTest, unwind_through_unreadable_elf_local) {
Christopher Ferris3acf5772018-05-24 16:01:55 -07001408 TemporaryDir td;
1409 std::string tmp_so_name;
1410 ASSERT_NO_FATAL_FAILURE(CopySharedLibrary(td.path, &tmp_so_name));
1411
1412 void* lib_handle = dlopen(tmp_so_name.c_str(), RTLD_NOW);
Christopher Ferris67aba682015-05-08 15:44:46 -07001413 ASSERT_TRUE(lib_handle != nullptr);
Christopher Ferris3acf5772018-05-24 16:01:55 -07001414 ASSERT_TRUE(unlink(tmp_so_name.c_str()) != -1);
Christopher Ferris67aba682015-05-08 15:44:46 -07001415
1416 test_func_t test_func;
1417 test_func = reinterpret_cast<test_func_t>(dlsym(lib_handle, "test_level_one"));
1418 ASSERT_TRUE(test_func != nullptr);
1419
Christopher Ferris7937a362018-01-18 11:15:49 -08001420 ASSERT_NE(test_func(1, 2, 3, 4, VerifyUnreadableElfBacktrace, reinterpret_cast<void*>(test_func)),
1421 0);
Christopher Ferris67aba682015-05-08 15:44:46 -07001422}
1423
Christopher Ferris50d81ac2018-06-29 16:05:35 -07001424TEST_F(BacktraceTest, unwind_through_unreadable_elf_remote) {
Christopher Ferris3acf5772018-05-24 16:01:55 -07001425 TemporaryDir td;
1426 std::string tmp_so_name;
1427 ASSERT_NO_FATAL_FAILURE(CopySharedLibrary(td.path, &tmp_so_name));
1428
1429 void* lib_handle = dlopen(tmp_so_name.c_str(), RTLD_NOW);
Christopher Ferris67aba682015-05-08 15:44:46 -07001430 ASSERT_TRUE(lib_handle != nullptr);
Christopher Ferris3acf5772018-05-24 16:01:55 -07001431 ASSERT_TRUE(unlink(tmp_so_name.c_str()) != -1);
Christopher Ferris67aba682015-05-08 15:44:46 -07001432
1433 test_func_t test_func;
1434 test_func = reinterpret_cast<test_func_t>(dlsym(lib_handle, "test_level_one"));
1435 ASSERT_TRUE(test_func != nullptr);
1436
1437 pid_t pid;
1438 if ((pid = fork()) == 0) {
1439 test_func(1, 2, 3, 4, 0, 0);
1440 exit(0);
1441 }
1442 ASSERT_TRUE(pid > 0);
Christopher Ferris67aba682015-05-08 15:44:46 -07001443
1444 uint64_t start = NanoTime();
1445 bool done = false;
1446 while (!done) {
1447 ASSERT_TRUE(ptrace(PTRACE_ATTACH, pid, 0, 0) == 0);
1448
1449 // Wait for the process to get to a stopping point.
1450 WaitForStop(pid);
1451
1452 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(pid, BACKTRACE_CURRENT_THREAD));
1453 ASSERT_TRUE(backtrace.get() != nullptr);
1454 ASSERT_TRUE(backtrace->Unwind(0));
Christopher Ferrisc8bec5a2017-12-11 17:44:33 -08001455 VERIFY_NO_ERROR(backtrace->GetError().error_code);
Christopher Ferris67aba682015-05-08 15:44:46 -07001456
1457 size_t frame_num;
Christopher Ferris7937a362018-01-18 11:15:49 -08001458 if (FindFuncFrameInBacktrace(backtrace.get(), reinterpret_cast<uint64_t>(test_func),
Christopher Ferris3acf5772018-05-24 16:01:55 -07001459 &frame_num) &&
1460 frame_num != 0) {
Christopher Ferris7937a362018-01-18 11:15:49 -08001461 VerifyUnreadableElfFrame(backtrace.get(), reinterpret_cast<uint64_t>(test_func), frame_num);
Christopher Ferris67aba682015-05-08 15:44:46 -07001462 done = true;
1463 }
1464
1465 ASSERT_TRUE(ptrace(PTRACE_DETACH, pid, 0, 0) == 0);
1466
1467 if ((NanoTime() - start) > 5 * NS_PER_SEC) {
1468 break;
1469 }
1470 usleep(US_PER_MSEC);
1471 }
1472
1473 kill(pid, SIGKILL);
1474 ASSERT_EQ(waitpid(pid, nullptr, 0), pid);
1475
1476 ASSERT_TRUE(done) << "Test function never found in unwind.";
1477}
1478
Christopher Ferris50d81ac2018-06-29 16:05:35 -07001479TEST_F(BacktraceTest, unwind_thread_doesnt_exist) {
Christopher Ferris206a3b92016-03-09 14:35:54 -08001480 std::unique_ptr<Backtrace> backtrace(
1481 Backtrace::Create(BACKTRACE_CURRENT_PROCESS, 99999999));
1482 ASSERT_TRUE(backtrace.get() != nullptr);
1483 ASSERT_FALSE(backtrace->Unwind(0));
Yabin Cuif8808282017-12-12 18:04:10 -08001484 ASSERT_EQ(BACKTRACE_UNWIND_ERROR_THREAD_DOESNT_EXIST, backtrace->GetError().error_code);
Christopher Ferris206a3b92016-03-09 14:35:54 -08001485}
1486
Christopher Ferris50d81ac2018-06-29 16:05:35 -07001487TEST_F(BacktraceTest, local_get_function_name_before_unwind) {
Christopher Ferris82f3bbd2017-03-14 15:22:26 -07001488 std::unique_ptr<Backtrace> backtrace(
1489 Backtrace::Create(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD));
1490 ASSERT_TRUE(backtrace.get() != nullptr);
1491
1492 // Verify that trying to get a function name before doing an unwind works.
Christopher Ferris50d81ac2018-06-29 16:05:35 -07001493 uint64_t cur_func_offset = reinterpret_cast<uint64_t>(test_level_one_) + 1;
Christopher Ferris7937a362018-01-18 11:15:49 -08001494 uint64_t offset;
Christopher Ferris82f3bbd2017-03-14 15:22:26 -07001495 ASSERT_NE(std::string(""), backtrace->GetFunctionName(cur_func_offset, &offset));
1496}
1497
Christopher Ferris50d81ac2018-06-29 16:05:35 -07001498TEST_F(BacktraceTest, remote_get_function_name_before_unwind) {
Christopher Ferris82f3bbd2017-03-14 15:22:26 -07001499 pid_t pid;
1500 CreateRemoteProcess(&pid);
1501
1502 // Now create an unwind object.
1503 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(pid, pid));
1504
1505 // Verify that trying to get a function name before doing an unwind works.
Christopher Ferris50d81ac2018-06-29 16:05:35 -07001506 uint64_t cur_func_offset = reinterpret_cast<uint64_t>(test_level_one_) + 1;
Christopher Ferris7937a362018-01-18 11:15:49 -08001507 uint64_t offset;
Christopher Ferris82f3bbd2017-03-14 15:22:26 -07001508 ASSERT_NE(std::string(""), backtrace->GetFunctionName(cur_func_offset, &offset));
1509
1510 FinishRemoteProcess(pid);
1511}
1512
Christopher Ferris7937a362018-01-18 11:15:49 -08001513static void SetUcontextSp(uint64_t sp, ucontext_t* ucontext) {
Christopher Ferrisf5e568e2017-03-22 13:18:31 -07001514#if defined(__arm__)
1515 ucontext->uc_mcontext.arm_sp = sp;
1516#elif defined(__aarch64__)
1517 ucontext->uc_mcontext.sp = sp;
1518#elif defined(__i386__)
1519 ucontext->uc_mcontext.gregs[REG_ESP] = sp;
1520#elif defined(__x86_64__)
1521 ucontext->uc_mcontext.gregs[REG_RSP] = sp;
1522#else
1523 UNUSED(sp);
1524 UNUSED(ucontext);
1525 ASSERT_TRUE(false) << "Unsupported architecture";
1526#endif
1527}
1528
Christopher Ferris7937a362018-01-18 11:15:49 -08001529static void SetUcontextPc(uint64_t pc, ucontext_t* ucontext) {
Christopher Ferrisf5e568e2017-03-22 13:18:31 -07001530#if defined(__arm__)
1531 ucontext->uc_mcontext.arm_pc = pc;
1532#elif defined(__aarch64__)
1533 ucontext->uc_mcontext.pc = pc;
1534#elif defined(__i386__)
1535 ucontext->uc_mcontext.gregs[REG_EIP] = pc;
1536#elif defined(__x86_64__)
1537 ucontext->uc_mcontext.gregs[REG_RIP] = pc;
1538#else
1539 UNUSED(pc);
1540 UNUSED(ucontext);
1541 ASSERT_TRUE(false) << "Unsupported architecture";
1542#endif
1543}
1544
Christopher Ferris7937a362018-01-18 11:15:49 -08001545static void SetUcontextLr(uint64_t lr, ucontext_t* ucontext) {
Christopher Ferrisf5e568e2017-03-22 13:18:31 -07001546#if defined(__arm__)
1547 ucontext->uc_mcontext.arm_lr = lr;
1548#elif defined(__aarch64__)
1549 ucontext->uc_mcontext.regs[30] = lr;
1550#elif defined(__i386__)
1551 // The lr is on the stack.
1552 ASSERT_TRUE(lr != 0);
1553 ASSERT_TRUE(ucontext != nullptr);
1554#elif defined(__x86_64__)
1555 // The lr is on the stack.
1556 ASSERT_TRUE(lr != 0);
1557 ASSERT_TRUE(ucontext != nullptr);
1558#else
1559 UNUSED(lr);
1560 UNUSED(ucontext);
1561 ASSERT_TRUE(false) << "Unsupported architecture";
1562#endif
1563}
1564
1565static constexpr size_t DEVICE_MAP_SIZE = 1024;
1566
1567static void SetupDeviceMap(void** device_map) {
1568 // Make sure that anything in a device map will result in fails
1569 // to read.
1570 android::base::unique_fd device_fd(open("/dev/zero", O_RDONLY | O_CLOEXEC));
1571
1572 *device_map = mmap(nullptr, 1024, PROT_READ, MAP_PRIVATE, device_fd, 0);
1573 ASSERT_TRUE(*device_map != MAP_FAILED);
1574
1575 // Make sure the map is readable.
1576 ASSERT_EQ(0, reinterpret_cast<int*>(*device_map)[0]);
1577}
1578
1579static void UnwindFromDevice(Backtrace* backtrace, void* device_map) {
Christopher Ferris7937a362018-01-18 11:15:49 -08001580 uint64_t device_map_uint = reinterpret_cast<uint64_t>(device_map);
Christopher Ferrisf5e568e2017-03-22 13:18:31 -07001581
1582 backtrace_map_t map;
1583 backtrace->FillInMap(device_map_uint, &map);
1584 // Verify the flag is set.
1585 ASSERT_EQ(PROT_DEVICE_MAP, map.flags & PROT_DEVICE_MAP);
1586
1587 // Quick sanity checks.
Christopher Ferris7937a362018-01-18 11:15:49 -08001588 uint64_t offset;
Christopher Ferrisf5e568e2017-03-22 13:18:31 -07001589 ASSERT_EQ(std::string(""), backtrace->GetFunctionName(device_map_uint, &offset));
1590 ASSERT_EQ(std::string(""), backtrace->GetFunctionName(device_map_uint, &offset, &map));
1591 ASSERT_EQ(std::string(""), backtrace->GetFunctionName(0, &offset));
1592
Christopher Ferris50d81ac2018-06-29 16:05:35 -07001593 uint64_t cur_func_offset = reinterpret_cast<uint64_t>(BacktraceTest::test_level_one_) + 1;
Christopher Ferrisf5e568e2017-03-22 13:18:31 -07001594 // Now verify the device map flag actually causes the function name to be empty.
1595 backtrace->FillInMap(cur_func_offset, &map);
1596 ASSERT_TRUE((map.flags & PROT_DEVICE_MAP) == 0);
1597 ASSERT_NE(std::string(""), backtrace->GetFunctionName(cur_func_offset, &offset, &map));
1598 map.flags |= PROT_DEVICE_MAP;
1599 ASSERT_EQ(std::string(""), backtrace->GetFunctionName(cur_func_offset, &offset, &map));
1600
1601 ucontext_t ucontext;
1602
1603 // Create a context that has the pc in the device map, but the sp
1604 // in a non-device map.
1605 memset(&ucontext, 0, sizeof(ucontext));
Christopher Ferris7937a362018-01-18 11:15:49 -08001606 SetUcontextSp(reinterpret_cast<uint64_t>(&ucontext), &ucontext);
Christopher Ferrisf5e568e2017-03-22 13:18:31 -07001607 SetUcontextPc(device_map_uint, &ucontext);
1608 SetUcontextLr(cur_func_offset, &ucontext);
1609
1610 ASSERT_TRUE(backtrace->Unwind(0, &ucontext));
1611
1612 // The buffer should only be a single element.
1613 ASSERT_EQ(1U, backtrace->NumFrames());
1614 const backtrace_frame_data_t* frame = backtrace->GetFrame(0);
1615 ASSERT_EQ(device_map_uint, frame->pc);
Christopher Ferris7937a362018-01-18 11:15:49 -08001616 ASSERT_EQ(reinterpret_cast<uint64_t>(&ucontext), frame->sp);
Christopher Ferrisf5e568e2017-03-22 13:18:31 -07001617
1618 // Check what happens when skipping the first frame.
1619 ASSERT_TRUE(backtrace->Unwind(1, &ucontext));
1620 ASSERT_EQ(0U, backtrace->NumFrames());
1621
1622 // Create a context that has the sp in the device map, but the pc
1623 // in a non-device map.
1624 memset(&ucontext, 0, sizeof(ucontext));
1625 SetUcontextSp(device_map_uint, &ucontext);
1626 SetUcontextPc(cur_func_offset, &ucontext);
1627 SetUcontextLr(cur_func_offset, &ucontext);
1628
1629 ASSERT_TRUE(backtrace->Unwind(0, &ucontext));
1630
1631 // The buffer should only be a single element.
1632 ASSERT_EQ(1U, backtrace->NumFrames());
1633 frame = backtrace->GetFrame(0);
1634 ASSERT_EQ(cur_func_offset, frame->pc);
1635 ASSERT_EQ(device_map_uint, frame->sp);
1636
1637 // Check what happens when skipping the first frame.
1638 ASSERT_TRUE(backtrace->Unwind(1, &ucontext));
1639 ASSERT_EQ(0U, backtrace->NumFrames());
1640}
1641
Christopher Ferris50d81ac2018-06-29 16:05:35 -07001642TEST_F(BacktraceTest, unwind_disallow_device_map_local) {
Christopher Ferrisf5e568e2017-03-22 13:18:31 -07001643 void* device_map;
1644 SetupDeviceMap(&device_map);
1645
1646 // Now create an unwind object.
1647 std::unique_ptr<Backtrace> backtrace(
1648 Backtrace::Create(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD));
1649 ASSERT_TRUE(backtrace);
1650
1651 UnwindFromDevice(backtrace.get(), device_map);
1652
1653 munmap(device_map, DEVICE_MAP_SIZE);
1654}
1655
Christopher Ferris50d81ac2018-06-29 16:05:35 -07001656TEST_F(BacktraceTest, unwind_disallow_device_map_remote) {
Christopher Ferrisf5e568e2017-03-22 13:18:31 -07001657 void* device_map;
1658 SetupDeviceMap(&device_map);
1659
1660 // Fork a process to do a remote backtrace.
1661 pid_t pid;
1662 CreateRemoteProcess(&pid);
1663
1664 // Now create an unwind object.
Christopher Ferris086baf92017-10-17 14:12:52 -07001665 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(pid, pid));
Christopher Ferrisf5e568e2017-03-22 13:18:31 -07001666
Christopher Ferris458cc662017-08-28 16:31:18 -07001667 UnwindFromDevice(backtrace.get(), device_map);
Christopher Ferrisf5e568e2017-03-22 13:18:31 -07001668
1669 FinishRemoteProcess(pid);
1670
1671 munmap(device_map, DEVICE_MAP_SIZE);
1672}
1673
Christopher Ferris5ea2c1f2017-03-23 14:55:01 -07001674class ScopedSignalHandler {
1675 public:
1676 ScopedSignalHandler(int signal_number, void (*handler)(int)) : signal_number_(signal_number) {
1677 memset(&action_, 0, sizeof(action_));
1678 action_.sa_handler = handler;
1679 sigaction(signal_number_, &action_, &old_action_);
1680 }
1681
1682 ScopedSignalHandler(int signal_number, void (*action)(int, siginfo_t*, void*))
1683 : signal_number_(signal_number) {
1684 memset(&action_, 0, sizeof(action_));
1685 action_.sa_flags = SA_SIGINFO;
1686 action_.sa_sigaction = action;
1687 sigaction(signal_number_, &action_, &old_action_);
1688 }
1689
1690 ~ScopedSignalHandler() { sigaction(signal_number_, &old_action_, nullptr); }
1691
1692 private:
1693 struct sigaction action_;
1694 struct sigaction old_action_;
1695 const int signal_number_;
1696};
1697
1698static void SetValueAndLoop(void* data) {
1699 volatile int* value = reinterpret_cast<volatile int*>(data);
1700
1701 *value = 1;
1702 for (volatile int i = 0;; i++)
1703 ;
1704}
1705
Christopher Ferrisb9de87f2017-09-20 13:37:24 -07001706static void UnwindThroughSignal(bool use_action, create_func_t create_func,
1707 map_create_func_t map_create_func) {
Christopher Ferris5ea2c1f2017-03-23 14:55:01 -07001708 volatile int value = 0;
1709 pid_t pid;
1710 if ((pid = fork()) == 0) {
1711 if (use_action) {
Christopher Ferris50d81ac2018-06-29 16:05:35 -07001712 ScopedSignalHandler ssh(SIGUSR1, BacktraceTest::test_signal_action_);
Christopher Ferris5ea2c1f2017-03-23 14:55:01 -07001713
Christopher Ferris50d81ac2018-06-29 16:05:35 -07001714 BacktraceTest::test_level_one_(1, 2, 3, 4, SetValueAndLoop, const_cast<int*>(&value));
Christopher Ferris5ea2c1f2017-03-23 14:55:01 -07001715 } else {
Christopher Ferris50d81ac2018-06-29 16:05:35 -07001716 ScopedSignalHandler ssh(SIGUSR1, BacktraceTest::test_signal_handler_);
Christopher Ferris5ea2c1f2017-03-23 14:55:01 -07001717
Christopher Ferris50d81ac2018-06-29 16:05:35 -07001718 BacktraceTest::test_level_one_(1, 2, 3, 4, SetValueAndLoop, const_cast<int*>(&value));
Christopher Ferris5ea2c1f2017-03-23 14:55:01 -07001719 }
1720 }
1721 ASSERT_NE(-1, pid);
1722
1723 int read_value = 0;
1724 uint64_t start = NanoTime();
1725 while (read_value == 0) {
1726 usleep(1000);
1727
1728 // Loop until the remote function gets into the final function.
1729 ASSERT_TRUE(ptrace(PTRACE_ATTACH, pid, 0, 0) == 0);
1730
1731 WaitForStop(pid);
1732
Christopher Ferrisb9de87f2017-09-20 13:37:24 -07001733 std::unique_ptr<BacktraceMap> map(map_create_func(pid, false));
1734 std::unique_ptr<Backtrace> backtrace(create_func(pid, pid, map.get()));
Christopher Ferris5ea2c1f2017-03-23 14:55:01 -07001735
Christopher Ferris7937a362018-01-18 11:15:49 -08001736 size_t bytes_read = backtrace->Read(reinterpret_cast<uint64_t>(const_cast<int*>(&value)),
Christopher Ferris5ea2c1f2017-03-23 14:55:01 -07001737 reinterpret_cast<uint8_t*>(&read_value), sizeof(read_value));
1738 ASSERT_EQ(sizeof(read_value), bytes_read);
1739
1740 ASSERT_TRUE(ptrace(PTRACE_DETACH, pid, 0, 0) == 0);
1741
1742 ASSERT_TRUE(NanoTime() - start < 5 * NS_PER_SEC)
1743 << "Remote process did not execute far enough in 5 seconds.";
1744 }
1745
1746 // Now need to send a signal to the remote process.
1747 kill(pid, SIGUSR1);
1748
1749 // Wait for the process to get to the signal handler loop.
1750 Backtrace::const_iterator frame_iter;
1751 start = NanoTime();
Christopher Ferris458cc662017-08-28 16:31:18 -07001752 std::unique_ptr<BacktraceMap> map;
Christopher Ferris5ea2c1f2017-03-23 14:55:01 -07001753 std::unique_ptr<Backtrace> backtrace;
1754 while (true) {
1755 usleep(1000);
1756
1757 ASSERT_TRUE(ptrace(PTRACE_ATTACH, pid, 0, 0) == 0);
1758
1759 WaitForStop(pid);
1760
Christopher Ferrisb9de87f2017-09-20 13:37:24 -07001761 map.reset(map_create_func(pid, false));
Christopher Ferris458cc662017-08-28 16:31:18 -07001762 ASSERT_TRUE(map.get() != nullptr);
Christopher Ferrisb9de87f2017-09-20 13:37:24 -07001763 backtrace.reset(create_func(pid, pid, map.get()));
Christopher Ferris5ea2c1f2017-03-23 14:55:01 -07001764 ASSERT_TRUE(backtrace->Unwind(0));
1765 bool found = false;
1766 for (frame_iter = backtrace->begin(); frame_iter != backtrace->end(); ++frame_iter) {
1767 if (frame_iter->func_name == "test_loop_forever") {
1768 ++frame_iter;
1769 found = true;
1770 break;
1771 }
1772 }
1773 if (found) {
1774 break;
1775 }
1776
1777 ASSERT_TRUE(ptrace(PTRACE_DETACH, pid, 0, 0) == 0);
1778
1779 ASSERT_TRUE(NanoTime() - start < 5 * NS_PER_SEC)
1780 << "Remote process did not get in signal handler in 5 seconds." << std::endl
1781 << DumpFrames(backtrace.get());
1782 }
1783
1784 std::vector<std::string> names;
1785 // Loop through the frames, and save the function names.
1786 size_t frame = 0;
1787 for (; frame_iter != backtrace->end(); ++frame_iter) {
1788 if (frame_iter->func_name == "test_level_four") {
1789 frame = names.size() + 1;
1790 }
1791 names.push_back(frame_iter->func_name);
1792 }
1793 ASSERT_NE(0U, frame) << "Unable to find test_level_four in backtrace" << std::endl
1794 << DumpFrames(backtrace.get());
1795
1796 // The expected order of the frames:
1797 // test_loop_forever
1798 // test_signal_handler|test_signal_action
1799 // <OPTIONAL_FRAME> May or may not exist.
1800 // SetValueAndLoop (but the function name might be empty)
1801 // test_level_four
1802 // test_level_three
1803 // test_level_two
1804 // test_level_one
1805 ASSERT_LE(frame + 2, names.size()) << DumpFrames(backtrace.get());
1806 ASSERT_LE(2U, frame) << DumpFrames(backtrace.get());
1807 if (use_action) {
1808 ASSERT_EQ("test_signal_action", names[0]) << DumpFrames(backtrace.get());
1809 } else {
1810 ASSERT_EQ("test_signal_handler", names[0]) << DumpFrames(backtrace.get());
1811 }
1812 ASSERT_EQ("test_level_three", names[frame]) << DumpFrames(backtrace.get());
1813 ASSERT_EQ("test_level_two", names[frame + 1]) << DumpFrames(backtrace.get());
1814 ASSERT_EQ("test_level_one", names[frame + 2]) << DumpFrames(backtrace.get());
1815
1816 FinishRemoteProcess(pid);
1817}
1818
Christopher Ferris50d81ac2018-06-29 16:05:35 -07001819TEST_F(BacktraceTest, unwind_remote_through_signal_using_handler) {
Christopher Ferris458cc662017-08-28 16:31:18 -07001820 UnwindThroughSignal(false, Backtrace::Create, BacktraceMap::Create);
1821}
1822
Christopher Ferris50d81ac2018-06-29 16:05:35 -07001823TEST_F(BacktraceTest, unwind_remote_through_signal_using_action) {
Christopher Ferris458cc662017-08-28 16:31:18 -07001824 UnwindThroughSignal(true, Backtrace::Create, BacktraceMap::Create);
1825}
1826
Josh Gaocd546c12017-10-25 15:21:45 -07001827static void TestFrameSkipNumbering(create_func_t create_func, map_create_func_t map_create_func) {
1828 std::unique_ptr<BacktraceMap> map(map_create_func(getpid(), false));
Elliott Hughes38488902018-07-11 11:13:16 -07001829 std::unique_ptr<Backtrace> backtrace(
1830 create_func(getpid(), android::base::GetThreadId(), map.get()));
Josh Gaocd546c12017-10-25 15:21:45 -07001831 backtrace->Unwind(1);
1832 ASSERT_NE(0U, backtrace->NumFrames());
1833 ASSERT_EQ(0U, backtrace->GetFrame(0)->num);
1834}
1835
Christopher Ferris50d81ac2018-06-29 16:05:35 -07001836TEST_F(BacktraceTest, unwind_frame_skip_numbering) {
Josh Gaocd546c12017-10-25 15:21:45 -07001837 TestFrameSkipNumbering(Backtrace::Create, BacktraceMap::Create);
1838}
1839
Chih-Hung Hsieh67867db2016-05-18 15:53:15 -07001840#define MAX_LEAK_BYTES (32*1024UL)
Christopher Ferrise2960912014-03-07 19:42:19 -08001841
Christopher Ferris82f3bbd2017-03-14 15:22:26 -07001842static void CheckForLeak(pid_t pid, pid_t tid) {
Christopher Ferris086baf92017-10-17 14:12:52 -07001843 std::unique_ptr<BacktraceMap> map(BacktraceMap::Create(pid));
1844
Christopher Ferrise2960912014-03-07 19:42:19 -08001845 // Loop enough that even a small leak should be detectable.
Christopher Ferris50d81ac2018-06-29 16:05:35 -07001846 size_t first_allocated_bytes = 0;
1847 size_t last_allocated_bytes = 0;
Christopher Ferrise2960912014-03-07 19:42:19 -08001848 for (size_t i = 0; i < 4096; i++) {
Christopher Ferris086baf92017-10-17 14:12:52 -07001849 Backtrace* backtrace = Backtrace::Create(pid, tid, map.get());
Christopher Ferris2b4a63f2015-03-17 14:42:03 -07001850 ASSERT_TRUE(backtrace != nullptr);
Christopher Ferrise2960912014-03-07 19:42:19 -08001851 ASSERT_TRUE(backtrace->Unwind(0));
Christopher Ferrisc8bec5a2017-12-11 17:44:33 -08001852 VERIFY_NO_ERROR(backtrace->GetError().error_code);
Christopher Ferrise2960912014-03-07 19:42:19 -08001853 delete backtrace;
Christopher Ferris50d81ac2018-06-29 16:05:35 -07001854
1855 size_t allocated_bytes = mallinfo().uordblks;
1856 if (first_allocated_bytes == 0) {
1857 first_allocated_bytes = allocated_bytes;
1858 } else if (last_allocated_bytes > first_allocated_bytes) {
1859 // Check that the memory did not increase too much over the first loop.
1860 ASSERT_LE(last_allocated_bytes - first_allocated_bytes, MAX_LEAK_BYTES);
1861 }
1862 last_allocated_bytes = allocated_bytes;
Christopher Ferris5ccdfa62016-03-07 19:18:31 -08001863 }
Christopher Ferrise2960912014-03-07 19:42:19 -08001864}
1865
Christopher Ferris50d81ac2018-06-29 16:05:35 -07001866TEST_F(BacktraceTest, check_for_leak_local) {
Christopher Ferrise2960912014-03-07 19:42:19 -08001867 CheckForLeak(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD);
1868}
1869
Christopher Ferris50d81ac2018-06-29 16:05:35 -07001870TEST_F(BacktraceTest, check_for_leak_local_thread) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -07001871 thread_t thread_data = { 0, 0, 0, nullptr };
Christopher Ferrise2960912014-03-07 19:42:19 -08001872 pthread_t thread;
Christopher Ferris2b4a63f2015-03-17 14:42:03 -07001873 ASSERT_TRUE(pthread_create(&thread, nullptr, ThreadLevelRun, &thread_data) == 0);
Christopher Ferrise2960912014-03-07 19:42:19 -08001874
1875 // Wait up to 2 seconds for the tid to be set.
1876 ASSERT_TRUE(WaitForNonZero(&thread_data.state, 2));
1877
1878 CheckForLeak(BACKTRACE_CURRENT_PROCESS, thread_data.tid);
1879
1880 // Tell the thread to exit its infinite loop.
1881 android_atomic_acquire_store(0, &thread_data.state);
1882
Christopher Ferris2b4a63f2015-03-17 14:42:03 -07001883 ASSERT_TRUE(pthread_join(thread, nullptr) == 0);
Christopher Ferrise2960912014-03-07 19:42:19 -08001884}
1885
Christopher Ferris50d81ac2018-06-29 16:05:35 -07001886TEST_F(BacktraceTest, check_for_leak_remote) {
Christopher Ferrise2960912014-03-07 19:42:19 -08001887 pid_t pid;
Christopher Ferris82f3bbd2017-03-14 15:22:26 -07001888 CreateRemoteProcess(&pid);
Christopher Ferrise2960912014-03-07 19:42:19 -08001889
1890 CheckForLeak(pid, BACKTRACE_CURRENT_THREAD);
1891
Christopher Ferris82f3bbd2017-03-14 15:22:26 -07001892 FinishRemoteProcess(pid);
Christopher Ferrise2960912014-03-07 19:42:19 -08001893}