[go: nahoru, domu]

blob: f4191b90870b3ce816c3e44e0877c60c8037ba06 [file] [log] [blame]
Christopher Ferris20bc65f2013-10-29 20:56:521/*
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 Ferrisba413b62015-04-02 02:59:5317#define _GNU_SOURCE 1
Christopher Ferris20bc65f2013-10-29 20:56:5218#include <dirent.h>
Christopher Ferris386a23e2015-05-13 18:47:2419#include <dlfcn.h>
Christopher Ferris20bc65f2013-10-29 20:56:5220#include <errno.h>
Christopher Ferris386a23e2015-05-13 18:47:2421#include <fcntl.h>
Christopher Ferrisf994e562014-04-04 03:19:3922#include <inttypes.h>
Christopher Ferris489cb162018-10-03 21:21:2723#include <malloc.h>
Christopher Ferris20bc65f2013-10-29 20:56:5224#include <pthread.h>
25#include <signal.h>
Christopher Ferrisf994e562014-04-04 03:19:3926#include <stdint.h>
Christopher Ferris20bc65f2013-10-29 20:56:5227#include <stdio.h>
28#include <stdlib.h>
29#include <string.h>
30#include <sys/ptrace.h>
Christopher Ferris386a23e2015-05-13 18:47:2431#include <sys/stat.h>
Christopher Ferris20bc65f2013-10-29 20:56:5232#include <sys/types.h>
33#include <sys/wait.h>
34#include <time.h>
Christopher Ferris703a27f2018-01-30 15:44:3635#include <ucontext.h>
Christopher Ferris20bc65f2013-10-29 20:56:5236#include <unistd.h>
37
Christopher Ferrisf994e562014-04-04 03:19:3938#include <algorithm>
Christopher Ferris386a23e2015-05-13 18:47:2439#include <list>
Christopher Ferrisb487b312015-03-18 10:57:3840#include <memory>
Christopher Ferrise622fb82017-03-24 17:31:0541#include <ostream>
Christopher Ferriscc629982015-03-31 23:01:2842#include <string>
Christopher Ferris20bc65f2013-10-29 20:56:5243#include <vector>
44
Dan Albert66f3dc72015-05-01 01:31:4545#include <backtrace/Backtrace.h>
46#include <backtrace/BacktraceMap.h>
47
Christopher Ferris96e504b2017-03-23 01:18:3848#include <android-base/macros.h>
Elliott Hughes35a19fc2015-12-07 23:59:4449#include <android-base/stringprintf.h>
Christopher Ferriscd230412018-05-25 01:45:5050#include <android-base/test_utils.h>
Elliott Hughesad07f892018-07-11 23:17:4951#include <android-base/threads.h>
Christopher Ferris25dc7302017-03-22 17:41:0152#include <android-base/unique_fd.h>
Dan Albert66f3dc72015-05-01 01:31:4553#include <cutils/atomic.h>
Dan Albert66f3dc72015-05-01 01:31:4554
55#include <gtest/gtest.h>
56
57// For the THREAD_SIGNAL definition.
58#include "BacktraceCurrent.h"
Christopher Ferris489cb162018-10-03 21:21:2759#include "BacktraceTest.h"
Christopher Ferrise622fb82017-03-24 17:31:0560#include "backtrace_testlib.h"
Christopher Ferris20bc65f2013-10-29 20:56:5261
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 Ferris4f07bf92014-11-14 19:39:0469#define NUM_THREADS 40
Christopher Ferris20bc65f2013-10-29 20:56:5270
71// Number of simultaneous threads running in our forked process.
72#define NUM_PTRACE_THREADS 5
73
Christopher Ferris66371492018-03-24 19:32:1574// The list of shared libaries that make up the backtrace library.
75static std::vector<std::string> kBacktraceLibs{"libunwindstack.so", "libbacktrace.so"};
76
Christopher Ferrisabf39bb2014-01-17 19:56:0477struct thread_t {
Christopher Ferris20bc65f2013-10-29 20:56:5278 pid_t tid;
79 int32_t state;
80 pthread_t threadId;
Christopher Ferrisb487b312015-03-18 10:57:3881 void* data;
Christopher Ferrisabf39bb2014-01-17 19:56:0482};
Christopher Ferris20bc65f2013-10-29 20:56:5283
Christopher Ferrisabf39bb2014-01-17 19:56:0484struct dump_thread_t {
Christopher Ferris20bc65f2013-10-29 20:56:5285 thread_t thread;
Christopher Ferris219eb8d2017-11-29 03:07:0886 BacktraceMap* map;
Christopher Ferris737b6a12014-01-14 02:12:0487 Backtrace* backtrace;
Christopher Ferris20bc65f2013-10-29 20:56:5288 int32_t* now;
89 int32_t done;
Christopher Ferrisabf39bb2014-01-17 19:56:0490};
Christopher Ferris20bc65f2013-10-29 20:56:5291
Christopher Ferris73a04f12017-09-25 19:24:0792typedef 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 Ferris489cb162018-10-03 21:21:27100void* 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 Ferris25dc7302017-03-22 17:41:01117static uint64_t NanoTime() {
Christopher Ferris20bc65f2013-10-29 20:56:52118 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 Ferris25dc7302017-03-22 17:41:01123static std::string DumpFrames(Backtrace* backtrace) {
Christopher Ferris737b6a12014-01-14 02:12:04124 if (backtrace->NumFrames() == 0) {
Christopher Ferris04f818f2015-04-03 14:52:56125 return " No frames to dump.\n";
Christopher Ferris737b6a12014-01-14 02:12:04126 }
127
Christopher Ferriscc629982015-03-31 23:01:28128 std::string frame;
Christopher Ferris737b6a12014-01-14 02:12:04129 for (size_t i = 0; i < backtrace->NumFrames(); i++) {
Christopher Ferriscc629982015-03-31 23:01:28130 frame += " " + backtrace->FormatFrameData(i) + '\n';
Christopher Ferris20bc65f2013-10-29 20:56:52131 }
Christopher Ferriscc629982015-03-31 23:01:28132 return frame;
Christopher Ferris20bc65f2013-10-29 20:56:52133}
134
Christopher Ferris25dc7302017-03-22 17:41:01135static void WaitForStop(pid_t pid) {
Christopher Ferris20bc65f2013-10-29 20:56:52136 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 Ferris25dc7302017-03-22 17:41:01148static 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 Ferris703a27f2018-01-30 15:44:36169#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 Ferris25dc7302017-03-22 17:41:01180static bool ReadyLevelBacktrace(Backtrace* backtrace) {
Christopher Ferris20bc65f2013-10-29 20:56:52181 // See if test_level_four is in the backtrace.
182 bool found = false;
Christopher Ferrisabf39bb2014-01-17 19:56:04183 for (Backtrace::const_iterator it = backtrace->begin(); it != backtrace->end(); ++it) {
184 if (it->func_name == "test_level_four") {
Christopher Ferris20bc65f2013-10-29 20:56:52185 found = true;
186 break;
187 }
188 }
189
190 return found;
191}
192
Christopher Ferris73a04f12017-09-25 19:24:07193static void VerifyLevelDump(Backtrace* backtrace, create_func_t, map_create_func_t) {
Christopher Ferris04f818f2015-04-03 14:52:56194 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 Ferris20bc65f2013-10-29 20:56:52198
199 // Look through the frames starting at the highest to find the
200 // frame we want.
201 size_t frame_num = 0;
Christopher Ferris737b6a12014-01-14 02:12:04202 for (size_t i = backtrace->NumFrames()-1; i > 2; i--) {
Christopher Ferrisabf39bb2014-01-17 19:56:04203 if (backtrace->GetFrame(i)->func_name == "test_level_one") {
Christopher Ferris20bc65f2013-10-29 20:56:52204 frame_num = i;
205 break;
206 }
207 }
Christopher Ferriscc629982015-03-31 23:01:28208 ASSERT_LT(static_cast<size_t>(0), frame_num) << DumpFrames(backtrace);
209 ASSERT_LE(static_cast<size_t>(3), frame_num) << DumpFrames(backtrace);
Christopher Ferris20bc65f2013-10-29 20:56:52210
Christopher Ferris04f818f2015-04-03 14:52:56211 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 Ferris20bc65f2013-10-29 20:56:52219}
220
Christopher Ferris25dc7302017-03-22 17:41:01221static void VerifyLevelBacktrace(void*) {
Christopher Ferrisb487b312015-03-18 10:57:38222 std::unique_ptr<Backtrace> backtrace(
Christopher Ferris737b6a12014-01-14 02:12:04223 Backtrace::Create(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD));
Christopher Ferrisb487b312015-03-18 10:57:38224 ASSERT_TRUE(backtrace.get() != nullptr);
Christopher Ferris737b6a12014-01-14 02:12:04225 ASSERT_TRUE(backtrace->Unwind(0));
Christopher Ferris703a27f2018-01-30 15:44:36226 VERIFY_NO_ERROR(backtrace->GetError().error_code);
Christopher Ferris20bc65f2013-10-29 20:56:52227
Christopher Ferris737b6a12014-01-14 02:12:04228 VerifyLevelDump(backtrace.get());
Christopher Ferris20bc65f2013-10-29 20:56:52229}
230
Christopher Ferris25dc7302017-03-22 17:41:01231static bool ReadyMaxBacktrace(Backtrace* backtrace) {
Christopher Ferris737b6a12014-01-14 02:12:04232 return (backtrace->NumFrames() == MAX_BACKTRACE_FRAMES);
Christopher Ferris20bc65f2013-10-29 20:56:52233}
234
Christopher Ferris73a04f12017-09-25 19:24:07235static void VerifyMaxDump(Backtrace* backtrace, create_func_t, map_create_func_t) {
Christopher Ferris04f818f2015-04-03 14:52:56236 ASSERT_EQ(backtrace->NumFrames(), static_cast<size_t>(MAX_BACKTRACE_FRAMES))
237 << DumpFrames(backtrace);
Christopher Ferris20bc65f2013-10-29 20:56:52238 // Verify that the last frame is our recursive call.
Christopher Ferris04f818f2015-04-03 14:52:56239 ASSERT_EQ(backtrace->GetFrame(MAX_BACKTRACE_FRAMES-1)->func_name, "test_recursive_call")
240 << DumpFrames(backtrace);
Christopher Ferris20bc65f2013-10-29 20:56:52241}
242
Christopher Ferris25dc7302017-03-22 17:41:01243static void VerifyMaxBacktrace(void*) {
Christopher Ferrisb487b312015-03-18 10:57:38244 std::unique_ptr<Backtrace> backtrace(
Christopher Ferris737b6a12014-01-14 02:12:04245 Backtrace::Create(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD));
Christopher Ferrisb487b312015-03-18 10:57:38246 ASSERT_TRUE(backtrace.get() != nullptr);
Christopher Ferris737b6a12014-01-14 02:12:04247 ASSERT_TRUE(backtrace->Unwind(0));
Christopher Ferris703a27f2018-01-30 15:44:36248 ASSERT_EQ(BACKTRACE_UNWIND_ERROR_EXCEED_MAX_FRAMES_LIMIT, backtrace->GetError().error_code);
Christopher Ferris20bc65f2013-10-29 20:56:52249
Christopher Ferris737b6a12014-01-14 02:12:04250 VerifyMaxDump(backtrace.get());
Christopher Ferris20bc65f2013-10-29 20:56:52251}
252
Christopher Ferris25dc7302017-03-22 17:41:01253static void ThreadSetState(void* data) {
Christopher Ferris20bc65f2013-10-29 20:56:52254 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 Ferris25dc7302017-03-22 17:41:01262static bool WaitForNonZero(int32_t* value, uint64_t seconds) {
Christopher Ferris20bc65f2013-10-29 20:56:52263 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 Ferris489cb162018-10-03 21:21:27272TEST_F(BacktraceTest, local_no_unwind_frames) {
Christopher Ferrisba413b62015-04-02 02:59:53273 // 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 Ferris04f818f2015-04-03 14:52:56276 ASSERT_TRUE(backtrace.get() != nullptr);
Christopher Ferrisba413b62015-04-02 02:59:53277 ASSERT_TRUE(backtrace->Unwind(0));
Christopher Ferris703a27f2018-01-30 15:44:36278 VERIFY_NO_ERROR(backtrace->GetError().error_code);
Christopher Ferrisba413b62015-04-02 02:59:53279
280 ASSERT_TRUE(backtrace->NumFrames() != 0);
Christopher Ferris66371492018-03-24 19:32:15281 // None of the frames should be in the backtrace libraries.
Christopher Ferrisba413b62015-04-02 02:59:53282 for (const auto& frame : *backtrace ) {
283 if (BacktraceMap::IsValid(frame.map)) {
284 const std::string name = basename(frame.map.name.c_str());
Christopher Ferris66371492018-03-24 19:32:15285 for (const auto& lib : kBacktraceLibs) {
286 ASSERT_TRUE(name != lib) << DumpFrames(backtrace.get());
287 }
Christopher Ferrisba413b62015-04-02 02:59:53288 }
Christopher Ferrisba413b62015-04-02 02:59:53289 }
290}
291
Christopher Ferris489cb162018-10-03 21:21:27292TEST_F(BacktraceTest, local_unwind_frames) {
Christopher Ferris66371492018-03-24 19:32:15293 // 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 Ferris489cb162018-10-03 21:21:27324TEST_F(BacktraceTest, local_trace) {
325 ASSERT_NE(test_level_one_(1, 2, 3, 4, VerifyLevelBacktrace, nullptr), 0);
Christopher Ferris20bc65f2013-10-29 20:56:52326}
327
Christopher Ferris25dc7302017-03-22 17:41:01328static void VerifyIgnoreFrames(Backtrace* bt_all, Backtrace* bt_ign1, Backtrace* bt_ign2,
329 const char* cur_proc) {
Christopher Ferris73a04f12017-09-25 19:24:07330 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 Ferris20bc65f2013-10-29 20:56:52338
339 // Check all of the frames are the same > the current frame.
Christopher Ferrisb487b312015-03-18 10:57:38340 bool check = (cur_proc == nullptr);
Christopher Ferris737b6a12014-01-14 02:12:04341 for (size_t i = 0; i < bt_ign2->NumFrames(); i++) {
Christopher Ferris20bc65f2013-10-29 20:56:52342 if (check) {
Christopher Ferris737b6a12014-01-14 02:12:04343 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 Ferris20bc65f2013-10-29 20:56:52346
Christopher Ferris737b6a12014-01-14 02:12:04347 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 Ferris20bc65f2013-10-29 20:56:52350 }
Christopher Ferrisabf39bb2014-01-17 19:56:04351 if (!check && bt_ign2->GetFrame(i)->func_name == cur_proc) {
Christopher Ferris20bc65f2013-10-29 20:56:52352 check = true;
353 }
354 }
355}
356
Christopher Ferris25dc7302017-03-22 17:41:01357static void VerifyLevelIgnoreFrames(void*) {
Christopher Ferrisb487b312015-03-18 10:57:38358 std::unique_ptr<Backtrace> all(
Christopher Ferris737b6a12014-01-14 02:12:04359 Backtrace::Create(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD));
Christopher Ferrisb487b312015-03-18 10:57:38360 ASSERT_TRUE(all.get() != nullptr);
Christopher Ferris737b6a12014-01-14 02:12:04361 ASSERT_TRUE(all->Unwind(0));
Christopher Ferris703a27f2018-01-30 15:44:36362 VERIFY_NO_ERROR(all->GetError().error_code);
Christopher Ferris20bc65f2013-10-29 20:56:52363
Christopher Ferrisb487b312015-03-18 10:57:38364 std::unique_ptr<Backtrace> ign1(
Christopher Ferris737b6a12014-01-14 02:12:04365 Backtrace::Create(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD));
Christopher Ferrisb487b312015-03-18 10:57:38366 ASSERT_TRUE(ign1.get() != nullptr);
Christopher Ferris737b6a12014-01-14 02:12:04367 ASSERT_TRUE(ign1->Unwind(1));
Christopher Ferris703a27f2018-01-30 15:44:36368 VERIFY_NO_ERROR(ign1->GetError().error_code);
Christopher Ferris20bc65f2013-10-29 20:56:52369
Christopher Ferrisb487b312015-03-18 10:57:38370 std::unique_ptr<Backtrace> ign2(
Christopher Ferris737b6a12014-01-14 02:12:04371 Backtrace::Create(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD));
Christopher Ferrisb487b312015-03-18 10:57:38372 ASSERT_TRUE(ign2.get() != nullptr);
Christopher Ferris737b6a12014-01-14 02:12:04373 ASSERT_TRUE(ign2->Unwind(2));
Christopher Ferris703a27f2018-01-30 15:44:36374 VERIFY_NO_ERROR(ign2->GetError().error_code);
Christopher Ferris20bc65f2013-10-29 20:56:52375
Christopher Ferris737b6a12014-01-14 02:12:04376 VerifyIgnoreFrames(all.get(), ign1.get(), ign2.get(), "VerifyLevelIgnoreFrames");
Christopher Ferris20bc65f2013-10-29 20:56:52377}
378
Christopher Ferris489cb162018-10-03 21:21:27379TEST_F(BacktraceTest, local_trace_ignore_frames) {
380 ASSERT_NE(test_level_one_(1, 2, 3, 4, VerifyLevelIgnoreFrames, nullptr), 0);
Christopher Ferris20bc65f2013-10-29 20:56:52381}
382
Christopher Ferris489cb162018-10-03 21:21:27383TEST_F(BacktraceTest, local_max_trace) {
384 ASSERT_NE(test_recursive_call_(MAX_BACKTRACE_FRAMES + 10, VerifyMaxBacktrace, nullptr), 0);
Christopher Ferris20bc65f2013-10-29 20:56:52385}
386
Christopher Ferris55d10c62017-08-29 17:38:31387static void VerifyProcTest(pid_t pid, pid_t tid, bool (*ReadyFunc)(Backtrace*),
Christopher Ferris73a04f12017-09-25 19:24:07388 void (*VerifyFunc)(Backtrace*, create_func_t, map_create_func_t),
389 create_func_t create_func, map_create_func_t map_create_func) {
Christopher Ferris20bc65f2013-10-29 20:56:52390 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 Ferris04f818f2015-04-03 14:52:56398 std::string last_dump;
Christopher Ferris20bc65f2013-10-29 20:56:52399 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 Ferrisb487b312015-03-18 10:57:38405 std::unique_ptr<BacktraceMap> map;
Christopher Ferris73a04f12017-09-25 19:24:07406 map.reset(map_create_func(pid, false));
407 std::unique_ptr<Backtrace> backtrace(create_func(pid, tid, map.get()));
Christopher Ferrisb487b312015-03-18 10:57:38408 ASSERT_TRUE(backtrace.get() != nullptr);
Christopher Ferris04f818f2015-04-03 14:52:56409 ASSERT_TRUE(backtrace->Unwind(0));
Christopher Ferris737b6a12014-01-14 02:12:04410 if (ReadyFunc(backtrace.get())) {
Christopher Ferris73a04f12017-09-25 19:24:07411 VerifyFunc(backtrace.get(), create_func, map_create_func);
Christopher Ferris20bc65f2013-10-29 20:56:52412 verified = true;
Christopher Ferris04f818f2015-04-03 14:52:56413 } else {
414 last_dump = DumpFrames(backtrace.get());
Christopher Ferris20bc65f2013-10-29 20:56:52415 }
Christopher Ferris737b6a12014-01-14 02:12:04416
Christopher Ferris20bc65f2013-10-29 20:56:52417 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 Ferris04f818f2015-04-03 14:52:56421 ASSERT_TRUE(verified) << "Last backtrace:\n" << last_dump;
Christopher Ferris20bc65f2013-10-29 20:56:52422}
423
Christopher Ferris489cb162018-10-03 21:21:27424TEST_F(BacktraceTest, ptrace_trace) {
Christopher Ferris20bc65f2013-10-29 20:56:52425 pid_t pid;
426 if ((pid = fork()) == 0) {
Christopher Ferris489cb162018-10-03 21:21:27427 ASSERT_NE(test_level_one_(1, 2, 3, 4, nullptr, nullptr), 0);
Christopher Ferrisf994e562014-04-04 03:19:39428 _exit(1);
Christopher Ferris20bc65f2013-10-29 20:56:52429 }
Christopher Ferris55d10c62017-08-29 17:38:31430 VerifyProcTest(pid, BACKTRACE_CURRENT_THREAD, ReadyLevelBacktrace, VerifyLevelDump,
431 Backtrace::Create, BacktraceMap::Create);
Christopher Ferris18e41fe2014-01-29 12:19:59432
433 kill(pid, SIGKILL);
434 int status;
435 ASSERT_EQ(waitpid(pid, &status, 0), pid);
436}
437
Christopher Ferris489cb162018-10-03 21:21:27438TEST_F(BacktraceTest, ptrace_max_trace) {
Christopher Ferris20bc65f2013-10-29 20:56:52439 pid_t pid;
440 if ((pid = fork()) == 0) {
Christopher Ferris489cb162018-10-03 21:21:27441 ASSERT_NE(test_recursive_call_(MAX_BACKTRACE_FRAMES + 10, nullptr, nullptr), 0);
Christopher Ferrisf994e562014-04-04 03:19:39442 _exit(1);
Christopher Ferris20bc65f2013-10-29 20:56:52443 }
Christopher Ferris55d10c62017-08-29 17:38:31444 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 Ferris73a04f12017-09-25 19:24:07452static 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 Ferrisb487b312015-03-18 10:57:38456 ASSERT_TRUE(ign1.get() != nullptr);
Christopher Ferris737b6a12014-01-14 02:12:04457 ASSERT_TRUE(ign1->Unwind(1));
Christopher Ferris703a27f2018-01-30 15:44:36458 VERIFY_NO_ERROR(ign1->GetError().error_code);
Christopher Ferris20bc65f2013-10-29 20:56:52459
Christopher Ferris73a04f12017-09-25 19:24:07460 std::unique_ptr<Backtrace> ign2(create_func(bt_all->Pid(), BACKTRACE_CURRENT_THREAD, map.get()));
Christopher Ferrisb487b312015-03-18 10:57:38461 ASSERT_TRUE(ign2.get() != nullptr);
Christopher Ferris737b6a12014-01-14 02:12:04462 ASSERT_TRUE(ign2->Unwind(2));
Christopher Ferris703a27f2018-01-30 15:44:36463 VERIFY_NO_ERROR(ign2->GetError().error_code);
Christopher Ferris20bc65f2013-10-29 20:56:52464
Christopher Ferrisb487b312015-03-18 10:57:38465 VerifyIgnoreFrames(bt_all, ign1.get(), ign2.get(), nullptr);
Christopher Ferris20bc65f2013-10-29 20:56:52466}
467
Christopher Ferris489cb162018-10-03 21:21:27468TEST_F(BacktraceTest, ptrace_ignore_frames) {
Christopher Ferris20bc65f2013-10-29 20:56:52469 pid_t pid;
470 if ((pid = fork()) == 0) {
Christopher Ferris489cb162018-10-03 21:21:27471 ASSERT_NE(test_level_one_(1, 2, 3, 4, nullptr, nullptr), 0);
Christopher Ferrisf994e562014-04-04 03:19:39472 _exit(1);
Christopher Ferris20bc65f2013-10-29 20:56:52473 }
Christopher Ferris55d10c62017-08-29 17:38:31474 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 Ferris20bc65f2013-10-29 20:56:52482// Create a process with multiple threads and dump all of the threads.
Christopher Ferris25dc7302017-03-22 17:41:01483static void* PtraceThreadLevelRun(void*) {
Christopher Ferris489cb162018-10-03 21:21:27484 EXPECT_NE(BacktraceTest::test_level_one_(1, 2, 3, 4, nullptr, nullptr), 0);
Christopher Ferrisb487b312015-03-18 10:57:38485 return nullptr;
Christopher Ferris20bc65f2013-10-29 20:56:52486}
487
Christopher Ferris25dc7302017-03-22 17:41:01488static void GetThreads(pid_t pid, std::vector<pid_t>* threads) {
Christopher Ferris20bc65f2013-10-29 20:56:52489 // Get the list of tasks.
490 char task_path[128];
491 snprintf(task_path, sizeof(task_path), "/proc/%d/task", pid);
492
James Hawkins85c96812016-02-19 19:10:30493 std::unique_ptr<DIR, decltype(&closedir)> tasks_dir(opendir(task_path), closedir);
Christopher Ferrisb487b312015-03-18 10:57:38494 ASSERT_TRUE(tasks_dir != nullptr);
Christopher Ferris20bc65f2013-10-29 20:56:52495 struct dirent* entry;
James Hawkins85c96812016-02-19 19:10:30496 while ((entry = readdir(tasks_dir.get())) != nullptr) {
Christopher Ferris20bc65f2013-10-29 20:56:52497 char* end;
498 pid_t tid = strtoul(entry->d_name, &end, 10);
499 if (*end == '\0') {
500 threads->push_back(tid);
501 }
502 }
Christopher Ferris20bc65f2013-10-29 20:56:52503}
504
Christopher Ferris489cb162018-10-03 21:21:27505TEST_F(BacktraceTest, ptrace_threads) {
Christopher Ferris20bc65f2013-10-29 20:56:52506 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 Ferrisb487b312015-03-18 10:57:38514 ASSERT_TRUE(pthread_create(&thread, &attr, PtraceThreadLevelRun, nullptr) == 0);
Christopher Ferris20bc65f2013-10-29 20:56:52515 }
Christopher Ferris489cb162018-10-03 21:21:27516 ASSERT_NE(test_level_one_(1, 2, 3, 4, nullptr, nullptr), 0);
Christopher Ferrisf994e562014-04-04 03:19:39517 _exit(1);
Christopher Ferris20bc65f2013-10-29 20:56:52518 }
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 Ferris55d10c62017-08-29 17:38:31538 VerifyProcTest(pid, *it, ReadyLevelBacktrace, VerifyLevelDump, Backtrace::Create,
539 BacktraceMap::Create);
540 }
541
542 FinishRemoteProcess(pid);
543}
544
Christopher Ferris20bc65f2013-10-29 20:56:52545void VerifyLevelThread(void*) {
Elliott Hughesad07f892018-07-11 23:17:49546 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), android::base::GetThreadId()));
Christopher Ferrisb487b312015-03-18 10:57:38547 ASSERT_TRUE(backtrace.get() != nullptr);
Christopher Ferris737b6a12014-01-14 02:12:04548 ASSERT_TRUE(backtrace->Unwind(0));
Christopher Ferris703a27f2018-01-30 15:44:36549 VERIFY_NO_ERROR(backtrace->GetError().error_code);
Christopher Ferris20bc65f2013-10-29 20:56:52550
Christopher Ferris737b6a12014-01-14 02:12:04551 VerifyLevelDump(backtrace.get());
Christopher Ferris20bc65f2013-10-29 20:56:52552}
553
Christopher Ferris489cb162018-10-03 21:21:27554TEST_F(BacktraceTest, thread_current_level) {
555 ASSERT_NE(test_level_one_(1, 2, 3, 4, VerifyLevelThread, nullptr), 0);
Christopher Ferris20bc65f2013-10-29 20:56:52556}
557
Christopher Ferris25dc7302017-03-22 17:41:01558static void VerifyMaxThread(void*) {
Elliott Hughesad07f892018-07-11 23:17:49559 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), android::base::GetThreadId()));
Christopher Ferrisb487b312015-03-18 10:57:38560 ASSERT_TRUE(backtrace.get() != nullptr);
Christopher Ferris737b6a12014-01-14 02:12:04561 ASSERT_TRUE(backtrace->Unwind(0));
Christopher Ferris703a27f2018-01-30 15:44:36562 ASSERT_EQ(BACKTRACE_UNWIND_ERROR_EXCEED_MAX_FRAMES_LIMIT, backtrace->GetError().error_code);
Christopher Ferris20bc65f2013-10-29 20:56:52563
Christopher Ferris737b6a12014-01-14 02:12:04564 VerifyMaxDump(backtrace.get());
Christopher Ferris20bc65f2013-10-29 20:56:52565}
566
Christopher Ferris489cb162018-10-03 21:21:27567TEST_F(BacktraceTest, thread_current_max) {
568 ASSERT_NE(test_recursive_call_(MAX_BACKTRACE_FRAMES + 10, VerifyMaxThread, nullptr), 0);
Christopher Ferris20bc65f2013-10-29 20:56:52569}
570
Christopher Ferris25dc7302017-03-22 17:41:01571static void* ThreadLevelRun(void* data) {
Christopher Ferris20bc65f2013-10-29 20:56:52572 thread_t* thread = reinterpret_cast<thread_t*>(data);
573
Elliott Hughesad07f892018-07-11 23:17:49574 thread->tid = android::base::GetThreadId();
Christopher Ferris489cb162018-10-03 21:21:27575 EXPECT_NE(BacktraceTest::test_level_one_(1, 2, 3, 4, ThreadSetState, data), 0);
Christopher Ferrisb487b312015-03-18 10:57:38576 return nullptr;
Christopher Ferris20bc65f2013-10-29 20:56:52577}
578
Christopher Ferris489cb162018-10-03 21:21:27579TEST_F(BacktraceTest, thread_level_trace) {
Christopher Ferris20bc65f2013-10-29 20:56:52580 pthread_attr_t attr;
581 pthread_attr_init(&attr);
582 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
583
Christopher Ferrisb487b312015-03-18 10:57:38584 thread_t thread_data = { 0, 0, 0, nullptr };
Christopher Ferris20bc65f2013-10-29 20:56:52585 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 Ferris3f212cf2014-05-01 00:24:30591 // 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 Ferris20bc65f2013-10-29 20:56:52597 // Save the current signal action and make sure it is restored afterwards.
598 struct sigaction cur_action;
Christopher Ferrisb487b312015-03-18 10:57:38599 ASSERT_TRUE(sigaction(THREAD_SIGNAL, nullptr, &cur_action) == 0);
Christopher Ferris20bc65f2013-10-29 20:56:52600
Christopher Ferrisb487b312015-03-18 10:57:38601 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), thread_data.tid));
602 ASSERT_TRUE(backtrace.get() != nullptr);
Christopher Ferris737b6a12014-01-14 02:12:04603 ASSERT_TRUE(backtrace->Unwind(0));
Christopher Ferris703a27f2018-01-30 15:44:36604 VERIFY_NO_ERROR(backtrace->GetError().error_code);
Christopher Ferris20bc65f2013-10-29 20:56:52605
Christopher Ferris737b6a12014-01-14 02:12:04606 VerifyLevelDump(backtrace.get());
Christopher Ferris20bc65f2013-10-29 20:56:52607
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 Ferrisb487b312015-03-18 10:57:38613 ASSERT_TRUE(sigaction(THREAD_SIGNAL, nullptr, &new_action) == 0);
Christopher Ferris20bc65f2013-10-29 20:56:52614 EXPECT_EQ(cur_action.sa_sigaction, new_action.sa_sigaction);
Christopher Ferris4f07bf92014-11-14 19:39:04615 // 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 Ferriscc629982015-03-31 23:01:28618#if defined(SA_RESTORER)
Christopher Ferris4f07bf92014-11-14 19:39:04619 cur_action.sa_flags &= ~SA_RESTORER;
620 new_action.sa_flags &= ~SA_RESTORER;
Christopher Ferriscc629982015-03-31 23:01:28621#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 Ferris4f07bf92014-11-14 19:39:04625#endif
Christopher Ferris20bc65f2013-10-29 20:56:52626 EXPECT_EQ(cur_action.sa_flags, new_action.sa_flags);
627}
628
Christopher Ferris489cb162018-10-03 21:21:27629TEST_F(BacktraceTest, thread_ignore_frames) {
Christopher Ferris20bc65f2013-10-29 20:56:52630 pthread_attr_t attr;
631 pthread_attr_init(&attr);
632 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
633
Christopher Ferrisb487b312015-03-18 10:57:38634 thread_t thread_data = { 0, 0, 0, nullptr };
Christopher Ferris20bc65f2013-10-29 20:56:52635 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 Ferrisb487b312015-03-18 10:57:38641 std::unique_ptr<Backtrace> all(Backtrace::Create(getpid(), thread_data.tid));
642 ASSERT_TRUE(all.get() != nullptr);
Christopher Ferris737b6a12014-01-14 02:12:04643 ASSERT_TRUE(all->Unwind(0));
Christopher Ferris703a27f2018-01-30 15:44:36644 VERIFY_NO_ERROR(all->GetError().error_code);
Christopher Ferris20bc65f2013-10-29 20:56:52645
Christopher Ferrisb487b312015-03-18 10:57:38646 std::unique_ptr<Backtrace> ign1(Backtrace::Create(getpid(), thread_data.tid));
647 ASSERT_TRUE(ign1.get() != nullptr);
Christopher Ferris737b6a12014-01-14 02:12:04648 ASSERT_TRUE(ign1->Unwind(1));
Christopher Ferris703a27f2018-01-30 15:44:36649 VERIFY_NO_ERROR(ign1->GetError().error_code);
Christopher Ferris20bc65f2013-10-29 20:56:52650
Christopher Ferrisb487b312015-03-18 10:57:38651 std::unique_ptr<Backtrace> ign2(Backtrace::Create(getpid(), thread_data.tid));
652 ASSERT_TRUE(ign2.get() != nullptr);
Christopher Ferris737b6a12014-01-14 02:12:04653 ASSERT_TRUE(ign2->Unwind(2));
Christopher Ferris703a27f2018-01-30 15:44:36654 VERIFY_NO_ERROR(ign2->GetError().error_code);
Christopher Ferris20bc65f2013-10-29 20:56:52655
Christopher Ferrisb487b312015-03-18 10:57:38656 VerifyIgnoreFrames(all.get(), ign1.get(), ign2.get(), nullptr);
Christopher Ferris20bc65f2013-10-29 20:56:52657
658 // Tell the thread to exit its infinite loop.
659 android_atomic_acquire_store(0, &thread_data.state);
660}
661
Christopher Ferris25dc7302017-03-22 17:41:01662static void* ThreadMaxRun(void* data) {
Christopher Ferris20bc65f2013-10-29 20:56:52663 thread_t* thread = reinterpret_cast<thread_t*>(data);
664
Elliott Hughesad07f892018-07-11 23:17:49665 thread->tid = android::base::GetThreadId();
Christopher Ferris489cb162018-10-03 21:21:27666 EXPECT_NE(BacktraceTest::test_recursive_call_(MAX_BACKTRACE_FRAMES + 10, ThreadSetState, data),
667 0);
Christopher Ferrisb487b312015-03-18 10:57:38668 return nullptr;
Christopher Ferris20bc65f2013-10-29 20:56:52669}
670
Christopher Ferris489cb162018-10-03 21:21:27671TEST_F(BacktraceTest, thread_max_trace) {
Christopher Ferris20bc65f2013-10-29 20:56:52672 pthread_attr_t attr;
673 pthread_attr_init(&attr);
674 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
675
Christopher Ferrisb487b312015-03-18 10:57:38676 thread_t thread_data = { 0, 0, 0, nullptr };
Christopher Ferris20bc65f2013-10-29 20:56:52677 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 Ferrisb487b312015-03-18 10:57:38683 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), thread_data.tid));
684 ASSERT_TRUE(backtrace.get() != nullptr);
Christopher Ferris737b6a12014-01-14 02:12:04685 ASSERT_TRUE(backtrace->Unwind(0));
Christopher Ferris703a27f2018-01-30 15:44:36686 ASSERT_EQ(BACKTRACE_UNWIND_ERROR_EXCEED_MAX_FRAMES_LIMIT, backtrace->GetError().error_code);
Christopher Ferris20bc65f2013-10-29 20:56:52687
Christopher Ferris737b6a12014-01-14 02:12:04688 VerifyMaxDump(backtrace.get());
Christopher Ferris20bc65f2013-10-29 20:56:52689
690 // Tell the thread to exit its infinite loop.
691 android_atomic_acquire_store(0, &thread_data.state);
692}
693
Christopher Ferris25dc7302017-03-22 17:41:01694static void* ThreadDump(void* data) {
Christopher Ferris20bc65f2013-10-29 20:56:52695 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 Ferris20bc65f2013-10-29 20:56:52702 // The status of the actual unwind will be checked elsewhere.
Christopher Ferris219eb8d2017-11-29 03:07:08703 dump->backtrace = Backtrace::Create(getpid(), dump->thread.tid, dump->map);
Christopher Ferris737b6a12014-01-14 02:12:04704 dump->backtrace->Unwind(0);
Christopher Ferris20bc65f2013-10-29 20:56:52705
706 android_atomic_acquire_store(1, &dump->done);
707
Christopher Ferrisb487b312015-03-18 10:57:38708 return nullptr;
Christopher Ferris20bc65f2013-10-29 20:56:52709}
710
Christopher Ferris219eb8d2017-11-29 03:07:08711static void MultipleThreadDumpTest(bool share_map) {
712 // Dump NUM_THREADS simultaneously using the same map.
Christopher Ferris20bc65f2013-10-29 20:56:52713 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 Ferris4f07bf92014-11-14 19:39:04728 ASSERT_TRUE(WaitForNonZero(&it->state, 30));
Christopher Ferris20bc65f2013-10-29 20:56:52729 }
730
731 // Start all of the dumpers at once, they will spin until they are signalled
732 // to begin their dump run.
Christopher Ferris219eb8d2017-11-29 03:07:08733 std::unique_ptr<BacktraceMap> map;
734 if (share_map) {
735 map.reset(BacktraceMap::Create(getpid()));
736 }
Christopher Ferris20bc65f2013-10-29 20:56:52737 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 Ferris219eb8d2017-11-29 03:07:08743 dumpers[i].map = map.get();
Christopher Ferris20bc65f2013-10-29 20:56:52744
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 Ferris4f07bf92014-11-14 19:39:04752 ASSERT_TRUE(WaitForNonZero(&dumpers[i].done, 30));
Christopher Ferris20bc65f2013-10-29 20:56:52753
754 // Tell the runner thread to exit its infinite loop.
755 android_atomic_acquire_store(0, &runners[i].state);
756
Christopher Ferrisb487b312015-03-18 10:57:38757 ASSERT_TRUE(dumpers[i].backtrace != nullptr);
Christopher Ferris737b6a12014-01-14 02:12:04758 VerifyMaxDump(dumpers[i].backtrace);
759
760 delete dumpers[i].backtrace;
Christopher Ferrisb487b312015-03-18 10:57:38761 dumpers[i].backtrace = nullptr;
Christopher Ferris20bc65f2013-10-29 20:56:52762 }
763}
764
Christopher Ferris489cb162018-10-03 21:21:27765TEST_F(BacktraceTest, thread_multiple_dump) {
Christopher Ferris219eb8d2017-11-29 03:07:08766 MultipleThreadDumpTest(false);
767}
Christopher Ferrise73a2462014-05-09 16:45:35768
Christopher Ferris489cb162018-10-03 21:21:27769TEST_F(BacktraceTest, thread_multiple_dump_same_map) {
Christopher Ferris219eb8d2017-11-29 03:07:08770 MultipleThreadDumpTest(true);
Christopher Ferrise73a2462014-05-09 16:45:35771}
772
Christopher Ferris18e41fe2014-01-29 12:19:59773// 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 Ferris489cb162018-10-03 21:21:27775TEST_F(BacktraceTest, simultaneous_maps) {
Christopher Ferris18e41fe2014-01-29 12:19:59776 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 Ferris04f818f2015-04-03 14:52:56781 ASSERT_TRUE(back1 != nullptr);
Christopher Ferris18e41fe2014-01-29 12:19:59782 EXPECT_TRUE(back1->Unwind(0));
Christopher Ferris703a27f2018-01-30 15:44:36783 VERIFY_NO_ERROR(back1->GetError().error_code);
Christopher Ferris18e41fe2014-01-29 12:19:59784 delete back1;
785 delete map1;
786
787 Backtrace* back2 = Backtrace::Create(getpid(), BACKTRACE_CURRENT_THREAD, map2);
Christopher Ferris04f818f2015-04-03 14:52:56788 ASSERT_TRUE(back2 != nullptr);
Christopher Ferris18e41fe2014-01-29 12:19:59789 EXPECT_TRUE(back2->Unwind(0));
Christopher Ferris703a27f2018-01-30 15:44:36790 VERIFY_NO_ERROR(back2->GetError().error_code);
Christopher Ferris18e41fe2014-01-29 12:19:59791 delete back2;
792 delete map2;
793
794 Backtrace* back3 = Backtrace::Create(getpid(), BACKTRACE_CURRENT_THREAD, map3);
Christopher Ferris04f818f2015-04-03 14:52:56795 ASSERT_TRUE(back3 != nullptr);
Christopher Ferris18e41fe2014-01-29 12:19:59796 EXPECT_TRUE(back3->Unwind(0));
Christopher Ferris703a27f2018-01-30 15:44:36797 VERIFY_NO_ERROR(back3->GetError().error_code);
Christopher Ferris18e41fe2014-01-29 12:19:59798 delete back3;
799 delete map3;
800}
801
Christopher Ferris489cb162018-10-03 21:21:27802TEST_F(BacktraceTest, fillin_erases) {
Christopher Ferris8df2ce72015-02-09 04:13:37803 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 Ferris103f4572018-01-19 23:38:38815 ASSERT_EQ(static_cast<uint64_t>(0), map.start);
816 ASSERT_EQ(static_cast<uint64_t>(0), map.end);
Christopher Ferris8df2ce72015-02-09 04:13:37817 ASSERT_EQ(0, map.flags);
818 ASSERT_EQ("", map.name);
819}
820
Christopher Ferris489cb162018-10-03 21:21:27821TEST_F(BacktraceTest, format_test) {
Christopher Ferrisb487b312015-03-18 10:57:38822 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), BACKTRACE_CURRENT_THREAD));
823 ASSERT_TRUE(backtrace.get() != nullptr);
Christopher Ferris20bc65f2013-10-29 20:56:52824
Christopher Ferris737b6a12014-01-14 02:12:04825 backtrace_frame_data_t frame;
Christopher Ferrisabf39bb2014-01-17 19:56:04826 frame.num = 1;
827 frame.pc = 2;
Christopher Ferris9dccd512017-07-20 04:08:49828 frame.rel_pc = 2;
Christopher Ferrisabf39bb2014-01-17 19:56:04829 frame.sp = 0;
830 frame.stack_size = 0;
Christopher Ferrisabf39bb2014-01-17 19:56:04831 frame.func_offset = 0;
Christopher Ferris20bc65f2013-10-29 20:56:52832
Christopher Ferrisabf39bb2014-01-17 19:56:04833 // Check no map set.
Christopher Ferris737b6a12014-01-14 02:12:04834 frame.num = 1;
Christopher Ferris20bc65f2013-10-29 20:56:52835#if defined(__LP64__)
Christopher Ferrisabf39bb2014-01-17 19:56:04836 EXPECT_EQ("#01 pc 0000000000000002 <unknown>",
Christopher Ferris20bc65f2013-10-29 20:56:52837#else
Christopher Ferrisabf39bb2014-01-17 19:56:04838 EXPECT_EQ("#01 pc 00000002 <unknown>",
Christopher Ferris20bc65f2013-10-29 20:56:52839#endif
Christopher Ferrisabf39bb2014-01-17 19:56:04840 backtrace->FormatFrameData(&frame));
Christopher Ferris20bc65f2013-10-29 20:56:52841
Christopher Ferrisabf39bb2014-01-17 19:56:04842 // Check map name empty, but exists.
Christopher Ferrisd63bd3d2015-12-01 00:42:38843 frame.pc = 0xb0020;
Christopher Ferris9dccd512017-07-20 04:08:49844 frame.rel_pc = 0x20;
Christopher Ferrisd63bd3d2015-12-01 00:42:38845 frame.map.start = 0xb0000;
846 frame.map.end = 0xbffff;
Christopher Ferris9dccd512017-07-20 04:08:49847 frame.map.load_bias = 0;
Christopher Ferris20bc65f2013-10-29 20:56:52848#if defined(__LP64__)
Christopher Ferrisd63bd3d2015-12-01 00:42:38849 EXPECT_EQ("#01 pc 0000000000000020 <anonymous:00000000000b0000>",
Christopher Ferris20bc65f2013-10-29 20:56:52850#else
Christopher Ferrisd63bd3d2015-12-01 00:42:38851 EXPECT_EQ("#01 pc 00000020 <anonymous:000b0000>",
Christopher Ferris20bc65f2013-10-29 20:56:52852#endif
Christopher Ferrisabf39bb2014-01-17 19:56:04853 backtrace->FormatFrameData(&frame));
Christopher Ferris20bc65f2013-10-29 20:56:52854
Christopher Ferrisd63bd3d2015-12-01 00:42:38855 // Check map name begins with a [.
856 frame.pc = 0xc0020;
857 frame.map.start = 0xc0000;
858 frame.map.end = 0xcffff;
Christopher Ferris9dccd512017-07-20 04:08:49859 frame.map.load_bias = 0;
Christopher Ferrisd63bd3d2015-12-01 00:42:38860 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 Ferrisabf39bb2014-01-17 19:56:04867
868 // Check relative pc is set and map name is set.
869 frame.pc = 0x12345679;
Christopher Ferris9dccd512017-07-20 04:08:49870 frame.rel_pc = 0x12345678;
Christopher Ferris8df2ce72015-02-09 04:13:37871 frame.map.name = "MapFake";
872 frame.map.start = 1;
873 frame.map.end = 1;
Christopher Ferris20bc65f2013-10-29 20:56:52874#if defined(__LP64__)
Christopher Ferrisabf39bb2014-01-17 19:56:04875 EXPECT_EQ("#01 pc 0000000012345678 MapFake",
Christopher Ferris20bc65f2013-10-29 20:56:52876#else
Christopher Ferrisabf39bb2014-01-17 19:56:04877 EXPECT_EQ("#01 pc 12345678 MapFake",
Christopher Ferris20bc65f2013-10-29 20:56:52878#endif
Christopher Ferrisabf39bb2014-01-17 19:56:04879 backtrace->FormatFrameData(&frame));
Christopher Ferris20bc65f2013-10-29 20:56:52880
Christopher Ferrisabf39bb2014-01-17 19:56:04881 // 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 Ferris737b6a12014-01-14 02:12:04891 frame.func_offset = 645;
Christopher Ferris20bc65f2013-10-29 20:56:52892#if defined(__LP64__)
Christopher Ferrisabf39bb2014-01-17 19:56:04893 EXPECT_EQ("#01 pc 0000000012345678 MapFake (ProcFake+645)",
Christopher Ferris20bc65f2013-10-29 20:56:52894#else
Christopher Ferrisabf39bb2014-01-17 19:56:04895 EXPECT_EQ("#01 pc 12345678 MapFake (ProcFake+645)",
Christopher Ferris20bc65f2013-10-29 20:56:52896#endif
Christopher Ferrisabf39bb2014-01-17 19:56:04897 backtrace->FormatFrameData(&frame));
Christopher Ferrisc53388d2015-05-02 00:46:19898
Christopher Ferris9dccd512017-07-20 04:08:49899 // Check func_name is set, func offset is non-zero, and load_bias is non-zero.
900 frame.rel_pc = 0x123456dc;
Christopher Ferrisc53388d2015-05-02 00:46:19901 frame.func_offset = 645;
Christopher Ferris9dccd512017-07-20 04:08:49902 frame.map.load_bias = 100;
Christopher Ferrisc53388d2015-05-02 00:46:19903#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 Ferris136db2a2015-08-20 22:01:38909
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 Ferris20bc65f2013-10-29 20:56:52918}
Christopher Ferrisf994e562014-04-04 03:19:39919
920struct map_test_t {
Christopher Ferris103f4572018-01-19 23:38:38921 uint64_t start;
922 uint64_t end;
Christopher Ferrisf994e562014-04-04 03:19:39923};
924
Christopher Ferris25dc7302017-03-22 17:41:01925static bool map_sort(map_test_t i, map_test_t j) { return i.start < j.start; }
Christopher Ferrisf994e562014-04-04 03:19:39926
Christopher Ferris4102ba02017-12-05 23:43:27927static 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 Ferris103f4572018-01-19 23:38:38933 map_txt += android::base::StringPrintf("%" PRIx64 "-%" PRIx64 "\n", map.start, map.end);
Christopher Ferris4102ba02017-12-05 23:43:27934 }
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 Ferris103f4572018-01-19 23:38:38945 "%" PRIx64 "-%" PRIx64 " flags: 0x%x offset: 0x%" PRIx64 " load_bias: 0x%" PRIx64,
Christopher Ferris4102ba02017-12-05 23:43:27946 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 Ferris25dc7302017-03-22 17:41:01955static void VerifyMap(pid_t pid) {
Christopher Ferrisf994e562014-04-04 03:19:39956 char buffer[4096];
957 snprintf(buffer, sizeof(buffer), "/proc/%d/maps", pid);
958
959 FILE* map_file = fopen(buffer, "r");
Christopher Ferrisb487b312015-03-18 10:57:38960 ASSERT_TRUE(map_file != nullptr);
Christopher Ferrisf994e562014-04-04 03:19:39961 std::vector<map_test_t> test_maps;
962 while (fgets(buffer, sizeof(buffer), map_file)) {
963 map_test_t map;
Christopher Ferris103f4572018-01-19 23:38:38964 ASSERT_EQ(2, sscanf(buffer, "%" SCNx64 "-%" SCNx64 " ", &map.start, &map.end));
Christopher Ferrisf994e562014-04-04 03:19:39965 test_maps.push_back(map);
966 }
967 fclose(map_file);
968 std::sort(test_maps.begin(), test_maps.end(), map_sort);
969
Christopher Ferrisb487b312015-03-18 10:57:38970 std::unique_ptr<BacktraceMap> map(BacktraceMap::Create(pid));
Christopher Ferrisf994e562014-04-04 03:19:39971
972 // Basic test that verifies that the map is in the expected order.
Christopher Ferris4102ba02017-12-05 23:43:27973 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 Ferrisf994e562014-04-04 03:19:39987 ++test_it;
988 }
989 ASSERT_TRUE(test_it == test_maps.end());
990}
991
Christopher Ferris489cb162018-10-03 21:21:27992TEST_F(BacktraceTest, verify_map_remote) {
Christopher Ferrisf994e562014-04-04 03:19:39993 pid_t pid;
Christopher Ferris25dc7302017-03-22 17:41:01994 CreateRemoteProcess(&pid);
Christopher Ferrisf994e562014-04-04 03:19:39995
996 // The maps should match exactly since the forked process has been paused.
997 VerifyMap(pid);
998
Christopher Ferris25dc7302017-03-22 17:41:01999 FinishRemoteProcess(pid);
Christopher Ferrisb487b312015-03-18 10:57:381000}
1001
Christopher Ferris25dc7302017-03-22 17:41:011002static void InitMemory(uint8_t* memory, size_t bytes) {
Christopher Ferris3e601aa2015-05-07 08:18:211003 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 Ferris25dc7302017-03-22 17:41:011013static void* ThreadReadTest(void* data) {
Christopher Ferrisb487b312015-03-18 10:57:381014 thread_t* thread_data = reinterpret_cast<thread_t*>(data);
1015
Elliott Hughesad07f892018-07-11 23:17:491016 thread_data->tid = android::base::GetThreadId();
Christopher Ferrisb487b312015-03-18 10:57:381017
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 Ferris3e601aa2015-05-07 08:18:211031 InitMemory(memory, pagesize);
Christopher Ferrisb487b312015-03-18 10:57:381032
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 Ferriscc629982015-03-31 23:01:281038 // Loop waiting for the caller to finish reading the memory.
Christopher Ferrisb487b312015-03-18 10:57:381039 while (thread_data->state) {
1040 }
1041
Christopher Ferriscc629982015-03-31 23:01:281042 // 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 Ferrisb487b312015-03-18 10:57:381047 free(memory);
1048
1049 android_atomic_acquire_store(1, &thread_data->state);
1050
1051 return nullptr;
1052}
1053
Christopher Ferris103f4572018-01-19 23:38:381054static void RunReadTest(Backtrace* backtrace, uint64_t read_addr) {
Christopher Ferrisb487b312015-03-18 10:57:381055 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 Ferris3e601aa2015-05-07 08:18:211059 InitMemory(expected, pagesize);
1060
Christopher Ferris703a27f2018-01-30 15:44:361061 uint8_t* data = new uint8_t[2 * pagesize];
Christopher Ferrisb487b312015-03-18 10:57:381062 // 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 Ferris3e601aa2015-05-07 08:18:211074
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 Nainara69a7fd2015-07-09 23:18:141088 delete[] data;
1089 delete[] expected;
Christopher Ferrisb487b312015-03-18 10:57:381090}
1091
Christopher Ferris489cb162018-10-03 21:21:271092TEST_F(BacktraceTest, thread_read) {
Christopher Ferrisb487b312015-03-18 10:57:381093 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 Ferris103f4572018-01-19 23:38:381105 RunReadTest(backtrace.get(), reinterpret_cast<uint64_t>(thread_data.data));
Christopher Ferrisb487b312015-03-18 10:57:381106
1107 android_atomic_acquire_store(0, &thread_data.state);
1108
1109 ASSERT_TRUE(WaitForNonZero(&thread_data.state, 10));
1110}
1111
Christopher Ferris703a27f2018-01-30 15:44:361112// The code requires these variables are the same size.
Christopher Ferris103f4572018-01-19 23:38:381113volatile uint64_t g_ready = 0;
1114volatile uint64_t g_addr = 0;
Christopher Ferris703a27f2018-01-30 15:44:361115static_assert(sizeof(g_ready) == sizeof(g_addr), "g_ready/g_addr must be same size");
Christopher Ferrisb487b312015-03-18 10:57:381116
Christopher Ferris25dc7302017-03-22 17:41:011117static void ForkedReadTest() {
Christopher Ferrisb487b312015-03-18 10:57:381118 // 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 Ferris3e601aa2015-05-07 08:18:211133 InitMemory(memory, pagesize);
Christopher Ferrisb487b312015-03-18 10:57:381134
Christopher Ferris103f4572018-01-19 23:38:381135 g_addr = reinterpret_cast<uint64_t>(memory);
Christopher Ferrisb487b312015-03-18 10:57:381136 g_ready = 1;
1137
1138 while (1) {
1139 usleep(US_PER_MSEC);
1140 }
1141}
1142
Christopher Ferris489cb162018-10-03 21:21:271143TEST_F(BacktraceTest, process_read) {
Christopher Ferris386a23e2015-05-13 18:47:241144 g_ready = 0;
Christopher Ferrisb487b312015-03-18 10:57:381145 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 Ferris04f818f2015-04-03 14:52:561159 ASSERT_TRUE(backtrace.get() != nullptr);
Christopher Ferrisb487b312015-03-18 10:57:381160
Christopher Ferris103f4572018-01-19 23:38:381161 uint64_t read_addr;
1162 size_t bytes_read = backtrace->Read(reinterpret_cast<uint64_t>(&g_ready),
Christopher Ferris703a27f2018-01-30 15:44:361163 reinterpret_cast<uint8_t*>(&read_addr), sizeof(g_ready));
1164 ASSERT_EQ(sizeof(g_ready), bytes_read);
Christopher Ferrisb487b312015-03-18 10:57:381165 if (read_addr) {
1166 // The forked process is ready to be read.
Christopher Ferris103f4572018-01-19 23:38:381167 bytes_read = backtrace->Read(reinterpret_cast<uint64_t>(&g_addr),
Christopher Ferris703a27f2018-01-30 15:44:361168 reinterpret_cast<uint8_t*>(&read_addr), sizeof(g_addr));
1169 ASSERT_EQ(sizeof(g_addr), bytes_read);
Christopher Ferrisb487b312015-03-18 10:57:381170
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 Ferrisf994e562014-04-04 03:19:391187}
1188
Christopher Ferris25dc7302017-03-22 17:41:011189static void VerifyFunctionsFound(const std::vector<std::string>& found_functions) {
Christopher Ferris386a23e2015-05-13 18:47:241190 // 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 Ferriscd230412018-05-25 01:45:501209static void CopySharedLibrary(const char* tmp_dir, std::string* tmp_so_name) {
Christopher Ferris489cb162018-10-03 21:21:271210 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 Ferris386a23e2015-05-13 18:47:241218
Christopher Ferriscd230412018-05-25 01:45:501219 *tmp_so_name = std::string(tmp_dir) + "/libbacktrace_test.so";
Christopher Ferris489cb162018-10-03 21:21:271220 std::string cp_cmd = android::base::StringPrintf("cp %s %s", test_lib.c_str(), tmp_dir);
Christopher Ferriscd230412018-05-25 01:45:501221
1222 // Copy the shared so to a tempory directory.
1223 ASSERT_EQ(0, system(cp_cmd.c_str()));
Christopher Ferris386a23e2015-05-13 18:47:241224}
1225
Christopher Ferris489cb162018-10-03 21:21:271226TEST_F(BacktraceTest, check_unreadable_elf_local) {
Christopher Ferriscd230412018-05-25 01:45:501227 TemporaryDir td;
1228 std::string tmp_so_name;
1229 ASSERT_NO_FATAL_FAILURE(CopySharedLibrary(td.path, &tmp_so_name));
Christopher Ferris386a23e2015-05-13 18:47:241230
1231 struct stat buf;
Christopher Ferriscd230412018-05-25 01:45:501232 ASSERT_TRUE(stat(tmp_so_name.c_str(), &buf) != -1);
Christopher Ferris103f4572018-01-19 23:38:381233 uint64_t map_size = buf.st_size;
Christopher Ferris386a23e2015-05-13 18:47:241234
Christopher Ferriscd230412018-05-25 01:45:501235 int fd = open(tmp_so_name.c_str(), O_RDONLY);
Christopher Ferris386a23e2015-05-13 18:47:241236 ASSERT_TRUE(fd != -1);
1237
Christopher Ferris703a27f2018-01-30 15:44:361238 void* map = mmap(nullptr, map_size, PROT_READ | PROT_EXEC, MAP_PRIVATE, fd, 0);
Christopher Ferris386a23e2015-05-13 18:47:241239 ASSERT_TRUE(map != MAP_FAILED);
1240 close(fd);
Christopher Ferriscd230412018-05-25 01:45:501241 ASSERT_TRUE(unlink(tmp_so_name.c_str()) != -1);
Christopher Ferris386a23e2015-05-13 18:47:241242
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 Ferris103f4572018-01-19 23:38:381252 map_size += reinterpret_cast<uint64_t>(map);
Christopher Ferris386a23e2015-05-13 18:47:241253 std::string last_func;
Christopher Ferris103f4572018-01-19 23:38:381254 for (uint64_t read_addr = reinterpret_cast<uint64_t>(map); read_addr < map_size; read_addr += 4) {
1255 uint64_t offset;
Christopher Ferris386a23e2015-05-13 18:47:241256 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 Ferris103f4572018-01-19 23:38:381263 ASSERT_TRUE(munmap(map, map_size - reinterpret_cast<uint64_t>(map)) == 0);
Christopher Ferris386a23e2015-05-13 18:47:241264
1265 VerifyFunctionsFound(found_functions);
1266}
1267
Christopher Ferris489cb162018-10-03 21:21:271268TEST_F(BacktraceTest, check_unreadable_elf_remote) {
Christopher Ferriscd230412018-05-25 01:45:501269 TemporaryDir td;
1270 std::string tmp_so_name;
1271 ASSERT_NO_FATAL_FAILURE(CopySharedLibrary(td.path, &tmp_so_name));
Christopher Ferris386a23e2015-05-13 18:47:241272
1273 g_ready = 0;
1274
1275 struct stat buf;
Christopher Ferriscd230412018-05-25 01:45:501276 ASSERT_TRUE(stat(tmp_so_name.c_str(), &buf) != -1);
Christopher Ferris103f4572018-01-19 23:38:381277 uint64_t map_size = buf.st_size;
Christopher Ferris386a23e2015-05-13 18:47:241278
1279 pid_t pid;
1280 if ((pid = fork()) == 0) {
Christopher Ferriscd230412018-05-25 01:45:501281 int fd = open(tmp_so_name.c_str(), O_RDONLY);
Christopher Ferris386a23e2015-05-13 18:47:241282 if (fd == -1) {
Christopher Ferriscd230412018-05-25 01:45:501283 fprintf(stderr, "Failed to open file %s: %s\n", tmp_so_name.c_str(), strerror(errno));
1284 unlink(tmp_so_name.c_str());
Christopher Ferris386a23e2015-05-13 18:47:241285 exit(0);
1286 }
1287
Christopher Ferris703a27f2018-01-30 15:44:361288 void* map = mmap(nullptr, map_size, PROT_READ | PROT_EXEC, MAP_PRIVATE, fd, 0);
Christopher Ferris386a23e2015-05-13 18:47:241289 if (map == MAP_FAILED) {
1290 fprintf(stderr, "Failed to map in memory: %s\n", strerror(errno));
Christopher Ferriscd230412018-05-25 01:45:501291 unlink(tmp_so_name.c_str());
Christopher Ferris386a23e2015-05-13 18:47:241292 exit(0);
1293 }
1294 close(fd);
Christopher Ferriscd230412018-05-25 01:45:501295 if (unlink(tmp_so_name.c_str()) == -1) {
Christopher Ferris386a23e2015-05-13 18:47:241296 fprintf(stderr, "Failed to unlink: %s\n", strerror(errno));
1297 exit(0);
1298 }
1299
Christopher Ferris103f4572018-01-19 23:38:381300 g_addr = reinterpret_cast<uint64_t>(map);
Christopher Ferris386a23e2015-05-13 18:47:241301 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 Ferris103f4572018-01-19 23:38:381320 uint64_t read_addr;
Christopher Ferris703a27f2018-01-30 15:44:361321 ASSERT_EQ(sizeof(g_ready),
Christopher Ferris103f4572018-01-19 23:38:381322 backtrace->Read(reinterpret_cast<uint64_t>(&g_ready),
Christopher Ferris703a27f2018-01-30 15:44:361323 reinterpret_cast<uint8_t*>(&read_addr), sizeof(g_ready)));
Christopher Ferris386a23e2015-05-13 18:47:241324 if (read_addr) {
Christopher Ferris703a27f2018-01-30 15:44:361325 ASSERT_EQ(sizeof(g_addr),
Christopher Ferris103f4572018-01-19 23:38:381326 backtrace->Read(reinterpret_cast<uint64_t>(&g_addr),
1327 reinterpret_cast<uint8_t*>(&read_addr), sizeof(uint64_t)));
Christopher Ferris386a23e2015-05-13 18:47:241328
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 Ferris103f4572018-01-19 23:38:381336 uint64_t offset;
Christopher Ferris386a23e2015-05-13 18:47:241337 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 Ferris103f4572018-01-19 23:38:381359static bool FindFuncFrameInBacktrace(Backtrace* backtrace, uint64_t test_func, size_t* frame_num) {
Christopher Ferris386a23e2015-05-13 18:47:241360 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 Ferris103f4572018-01-19 23:38:381378static void VerifyUnreadableElfFrame(Backtrace* backtrace, uint64_t test_func, size_t frame_num) {
Christopher Ferris386a23e2015-05-13 18:47:241379 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 Ferris103f4572018-01-19 23:38:381386 uint64_t diff = backtrace->GetFrame(frame_num)->pc - test_func;
Christopher Ferris386a23e2015-05-13 18:47:241387 ASSERT_LT(diff, 200U) << DumpFrames(backtrace);
1388}
1389
Christopher Ferris103f4572018-01-19 23:38:381390static void VerifyUnreadableElfBacktrace(void* func) {
Christopher Ferris386a23e2015-05-13 18:47:241391 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 Ferris703a27f2018-01-30 15:44:361395 VERIFY_NO_ERROR(backtrace->GetError().error_code);
Christopher Ferris386a23e2015-05-13 18:47:241396
1397 size_t frame_num;
Christopher Ferris703a27f2018-01-30 15:44:361398 uint64_t test_func = reinterpret_cast<uint64_t>(func);
1399 ASSERT_TRUE(FindFuncFrameInBacktrace(backtrace.get(), test_func, &frame_num))
1400 << DumpFrames(backtrace.get());
Christopher Ferris386a23e2015-05-13 18:47:241401
1402 VerifyUnreadableElfFrame(backtrace.get(), test_func, frame_num);
1403}
1404
Christopher Ferris103f4572018-01-19 23:38:381405typedef int (*test_func_t)(int, int, int, int, void (*)(void*), void*);
Christopher Ferris386a23e2015-05-13 18:47:241406
Christopher Ferris489cb162018-10-03 21:21:271407TEST_F(BacktraceTest, unwind_through_unreadable_elf_local) {
Christopher Ferriscd230412018-05-25 01:45:501408 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 Ferris386a23e2015-05-13 18:47:241413 ASSERT_TRUE(lib_handle != nullptr);
Christopher Ferriscd230412018-05-25 01:45:501414 ASSERT_TRUE(unlink(tmp_so_name.c_str()) != -1);
Christopher Ferris386a23e2015-05-13 18:47:241415
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 Ferris103f4572018-01-19 23:38:381420 ASSERT_NE(test_func(1, 2, 3, 4, VerifyUnreadableElfBacktrace, reinterpret_cast<void*>(test_func)),
1421 0);
Christopher Ferris386a23e2015-05-13 18:47:241422}
1423
Christopher Ferris489cb162018-10-03 21:21:271424TEST_F(BacktraceTest, unwind_through_unreadable_elf_remote) {
Christopher Ferriscd230412018-05-25 01:45:501425 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 Ferris386a23e2015-05-13 18:47:241430 ASSERT_TRUE(lib_handle != nullptr);
Christopher Ferriscd230412018-05-25 01:45:501431 ASSERT_TRUE(unlink(tmp_so_name.c_str()) != -1);
Christopher Ferris386a23e2015-05-13 18:47:241432
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 Ferris386a23e2015-05-13 18:47:241443
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 Ferris703a27f2018-01-30 15:44:361455 VERIFY_NO_ERROR(backtrace->GetError().error_code);
Christopher Ferris386a23e2015-05-13 18:47:241456
1457 size_t frame_num;
Christopher Ferris103f4572018-01-19 23:38:381458 if (FindFuncFrameInBacktrace(backtrace.get(), reinterpret_cast<uint64_t>(test_func),
Christopher Ferriscd230412018-05-25 01:45:501459 &frame_num) &&
1460 frame_num != 0) {
Christopher Ferris103f4572018-01-19 23:38:381461 VerifyUnreadableElfFrame(backtrace.get(), reinterpret_cast<uint64_t>(test_func), frame_num);
Christopher Ferris386a23e2015-05-13 18:47:241462 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 Ferris489cb162018-10-03 21:21:271479TEST_F(BacktraceTest, unwind_thread_doesnt_exist) {
Christopher Ferris5297f492016-03-10 22:32:051480 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 Cuicc1ac012017-12-16 20:43:541484 ASSERT_EQ(BACKTRACE_UNWIND_ERROR_THREAD_DOESNT_EXIST, backtrace->GetError().error_code);
Christopher Ferris5297f492016-03-10 22:32:051485}
1486
Christopher Ferris489cb162018-10-03 21:21:271487TEST_F(BacktraceTest, local_get_function_name_before_unwind) {
Christopher Ferris25dc7302017-03-22 17:41:011488 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 Ferris489cb162018-10-03 21:21:271493 uint64_t cur_func_offset = reinterpret_cast<uint64_t>(test_level_one_) + 1;
Christopher Ferris103f4572018-01-19 23:38:381494 uint64_t offset;
Christopher Ferris25dc7302017-03-22 17:41:011495 ASSERT_NE(std::string(""), backtrace->GetFunctionName(cur_func_offset, &offset));
1496}
1497
Christopher Ferris489cb162018-10-03 21:21:271498TEST_F(BacktraceTest, remote_get_function_name_before_unwind) {
Christopher Ferris25dc7302017-03-22 17:41:011499 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 Ferris489cb162018-10-03 21:21:271506 uint64_t cur_func_offset = reinterpret_cast<uint64_t>(test_level_one_) + 1;
Christopher Ferris103f4572018-01-19 23:38:381507 uint64_t offset;
Christopher Ferris25dc7302017-03-22 17:41:011508 ASSERT_NE(std::string(""), backtrace->GetFunctionName(cur_func_offset, &offset));
1509
1510 FinishRemoteProcess(pid);
1511}
1512
Christopher Ferris103f4572018-01-19 23:38:381513static void SetUcontextSp(uint64_t sp, ucontext_t* ucontext) {
Christopher Ferris96e504b2017-03-23 01:18:381514#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 Ferris103f4572018-01-19 23:38:381529static void SetUcontextPc(uint64_t pc, ucontext_t* ucontext) {
Christopher Ferris96e504b2017-03-23 01:18:381530#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 Ferris103f4572018-01-19 23:38:381545static void SetUcontextLr(uint64_t lr, ucontext_t* ucontext) {
Christopher Ferris96e504b2017-03-23 01:18:381546#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 Ferris103f4572018-01-19 23:38:381580 uint64_t device_map_uint = reinterpret_cast<uint64_t>(device_map);
Christopher Ferris96e504b2017-03-23 01:18:381581
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 Ferris103f4572018-01-19 23:38:381588 uint64_t offset;
Christopher Ferris96e504b2017-03-23 01:18:381589 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 Ferris489cb162018-10-03 21:21:271593 uint64_t cur_func_offset = reinterpret_cast<uint64_t>(BacktraceTest::test_level_one_) + 1;
Christopher Ferris96e504b2017-03-23 01:18:381594 // 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 Ferris103f4572018-01-19 23:38:381606 SetUcontextSp(reinterpret_cast<uint64_t>(&ucontext), &ucontext);
Christopher Ferris96e504b2017-03-23 01:18:381607 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 Ferris103f4572018-01-19 23:38:381616 ASSERT_EQ(reinterpret_cast<uint64_t>(&ucontext), frame->sp);
Christopher Ferris96e504b2017-03-23 01:18:381617
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 Ferris489cb162018-10-03 21:21:271642TEST_F(BacktraceTest, unwind_disallow_device_map_local) {
Christopher Ferris96e504b2017-03-23 01:18:381643 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 Ferris489cb162018-10-03 21:21:271656TEST_F(BacktraceTest, unwind_disallow_device_map_remote) {
Christopher Ferris96e504b2017-03-23 01:18:381657 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 Ferrisb0d1e7e2017-10-31 00:48:511665 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(pid, pid));
Christopher Ferris96e504b2017-03-23 01:18:381666
Christopher Ferris55d10c62017-08-29 17:38:311667 UnwindFromDevice(backtrace.get(), device_map);
Christopher Ferris96e504b2017-03-23 01:18:381668
1669 FinishRemoteProcess(pid);
1670
1671 munmap(device_map, DEVICE_MAP_SIZE);
1672}
1673
Christopher Ferrise622fb82017-03-24 17:31:051674class 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 Ferris73a04f12017-09-25 19:24:071706static void UnwindThroughSignal(bool use_action, create_func_t create_func,
1707 map_create_func_t map_create_func) {
Christopher Ferrise622fb82017-03-24 17:31:051708 volatile int value = 0;
1709 pid_t pid;
1710 if ((pid = fork()) == 0) {
1711 if (use_action) {
Christopher Ferris489cb162018-10-03 21:21:271712 ScopedSignalHandler ssh(SIGUSR1, BacktraceTest::test_signal_action_);
Christopher Ferrise622fb82017-03-24 17:31:051713
Christopher Ferris489cb162018-10-03 21:21:271714 BacktraceTest::test_level_one_(1, 2, 3, 4, SetValueAndLoop, const_cast<int*>(&value));
Christopher Ferrise622fb82017-03-24 17:31:051715 } else {
Christopher Ferris489cb162018-10-03 21:21:271716 ScopedSignalHandler ssh(SIGUSR1, BacktraceTest::test_signal_handler_);
Christopher Ferrise622fb82017-03-24 17:31:051717
Christopher Ferris489cb162018-10-03 21:21:271718 BacktraceTest::test_level_one_(1, 2, 3, 4, SetValueAndLoop, const_cast<int*>(&value));
Christopher Ferrise622fb82017-03-24 17:31:051719 }
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 Ferris73a04f12017-09-25 19:24:071733 std::unique_ptr<BacktraceMap> map(map_create_func(pid, false));
1734 std::unique_ptr<Backtrace> backtrace(create_func(pid, pid, map.get()));
Christopher Ferrise622fb82017-03-24 17:31:051735
Christopher Ferris103f4572018-01-19 23:38:381736 size_t bytes_read = backtrace->Read(reinterpret_cast<uint64_t>(const_cast<int*>(&value)),
Christopher Ferrise622fb82017-03-24 17:31:051737 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 Ferris55d10c62017-08-29 17:38:311752 std::unique_ptr<BacktraceMap> map;
Christopher Ferrise622fb82017-03-24 17:31:051753 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 Ferris73a04f12017-09-25 19:24:071761 map.reset(map_create_func(pid, false));
Christopher Ferris55d10c62017-08-29 17:38:311762 ASSERT_TRUE(map.get() != nullptr);
Christopher Ferris73a04f12017-09-25 19:24:071763 backtrace.reset(create_func(pid, pid, map.get()));
Christopher Ferrise622fb82017-03-24 17:31:051764 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 Ferris489cb162018-10-03 21:21:271819TEST_F(BacktraceTest, unwind_remote_through_signal_using_handler) {
Christopher Ferris55d10c62017-08-29 17:38:311820 UnwindThroughSignal(false, Backtrace::Create, BacktraceMap::Create);
1821}
1822
Christopher Ferris489cb162018-10-03 21:21:271823TEST_F(BacktraceTest, unwind_remote_through_signal_using_action) {
Christopher Ferris55d10c62017-08-29 17:38:311824 UnwindThroughSignal(true, Backtrace::Create, BacktraceMap::Create);
1825}
1826
Josh Gaoc4c75932017-10-27 00:17:541827static 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 Hughesad07f892018-07-11 23:17:491829 std::unique_ptr<Backtrace> backtrace(
1830 create_func(getpid(), android::base::GetThreadId(), map.get()));
Josh Gaoc4c75932017-10-27 00:17:541831 backtrace->Unwind(1);
1832 ASSERT_NE(0U, backtrace->NumFrames());
1833 ASSERT_EQ(0U, backtrace->GetFrame(0)->num);
1834}
1835
Christopher Ferris489cb162018-10-03 21:21:271836TEST_F(BacktraceTest, unwind_frame_skip_numbering) {
Josh Gaoc4c75932017-10-27 00:17:541837 TestFrameSkipNumbering(Backtrace::Create, BacktraceMap::Create);
1838}
1839
Chih-Hung Hsieh29690dc2016-05-19 00:25:511840#define MAX_LEAK_BYTES (32*1024UL)
Christopher Ferrisf994e562014-04-04 03:19:391841
Christopher Ferris25dc7302017-03-22 17:41:011842static void CheckForLeak(pid_t pid, pid_t tid) {
Christopher Ferrisb0d1e7e2017-10-31 00:48:511843 std::unique_ptr<BacktraceMap> map(BacktraceMap::Create(pid));
1844
Christopher Ferrisf994e562014-04-04 03:19:391845 // Loop enough that even a small leak should be detectable.
Christopher Ferris489cb162018-10-03 21:21:271846 size_t first_allocated_bytes = 0;
1847 size_t last_allocated_bytes = 0;
Christopher Ferrisf994e562014-04-04 03:19:391848 for (size_t i = 0; i < 4096; i++) {
Christopher Ferrisb0d1e7e2017-10-31 00:48:511849 Backtrace* backtrace = Backtrace::Create(pid, tid, map.get());
Christopher Ferrisb487b312015-03-18 10:57:381850 ASSERT_TRUE(backtrace != nullptr);
Christopher Ferrisf994e562014-04-04 03:19:391851 ASSERT_TRUE(backtrace->Unwind(0));
Christopher Ferris703a27f2018-01-30 15:44:361852 VERIFY_NO_ERROR(backtrace->GetError().error_code);
Christopher Ferrisf994e562014-04-04 03:19:391853 delete backtrace;
Christopher Ferris489cb162018-10-03 21:21:271854
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 Ferrisf48f6b62016-03-08 19:45:131863 }
Christopher Ferrisf994e562014-04-04 03:19:391864}
1865
Christopher Ferris489cb162018-10-03 21:21:271866TEST_F(BacktraceTest, check_for_leak_local) {
Christopher Ferrisf994e562014-04-04 03:19:391867 CheckForLeak(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD);
1868}
1869
Christopher Ferris489cb162018-10-03 21:21:271870TEST_F(BacktraceTest, check_for_leak_local_thread) {
Christopher Ferrisb487b312015-03-18 10:57:381871 thread_t thread_data = { 0, 0, 0, nullptr };
Christopher Ferrisf994e562014-04-04 03:19:391872 pthread_t thread;
Christopher Ferrisb487b312015-03-18 10:57:381873 ASSERT_TRUE(pthread_create(&thread, nullptr, ThreadLevelRun, &thread_data) == 0);
Christopher Ferrisf994e562014-04-04 03:19:391874
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 Ferrisb487b312015-03-18 10:57:381883 ASSERT_TRUE(pthread_join(thread, nullptr) == 0);
Christopher Ferrisf994e562014-04-04 03:19:391884}
1885
Christopher Ferris489cb162018-10-03 21:21:271886TEST_F(BacktraceTest, check_for_leak_remote) {
Christopher Ferrisf994e562014-04-04 03:19:391887 pid_t pid;
Christopher Ferris25dc7302017-03-22 17:41:011888 CreateRemoteProcess(&pid);
Christopher Ferrisf994e562014-04-04 03:19:391889
1890 CheckForLeak(pid, BACKTRACE_CURRENT_THREAD);
1891
Christopher Ferris25dc7302017-03-22 17:41:011892 FinishRemoteProcess(pid);
Christopher Ferrisf994e562014-04-04 03:19:391893}