Home | History | Annotate | Download | only in libbacktrace
      1 /*
      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 
     17 #define _GNU_SOURCE 1
     18 #include <dirent.h>
     19 #include <dlfcn.h>
     20 #include <errno.h>
     21 #include <fcntl.h>
     22 #include <inttypes.h>
     23 #include <pthread.h>
     24 #include <signal.h>
     25 #include <stdint.h>
     26 #include <stdio.h>
     27 #include <stdlib.h>
     28 #include <string.h>
     29 #include <sys/ptrace.h>
     30 #include <sys/stat.h>
     31 #include <sys/types.h>
     32 #include <sys/wait.h>
     33 #include <time.h>
     34 #include <unistd.h>
     35 
     36 #include <algorithm>
     37 #include <list>
     38 #include <memory>
     39 #include <string>
     40 #include <vector>
     41 
     42 #include <backtrace/Backtrace.h>
     43 #include <backtrace/BacktraceMap.h>
     44 
     45 #include <base/stringprintf.h>
     46 #include <cutils/atomic.h>
     47 #include <cutils/threads.h>
     48 
     49 #include <gtest/gtest.h>
     50 
     51 // For the THREAD_SIGNAL definition.
     52 #include "BacktraceCurrent.h"
     53 #include "thread_utils.h"
     54 
     55 // Number of microseconds per milliseconds.
     56 #define US_PER_MSEC             1000
     57 
     58 // Number of nanoseconds in a second.
     59 #define NS_PER_SEC              1000000000ULL
     60 
     61 // Number of simultaneous dumping operations to perform.
     62 #define NUM_THREADS  40
     63 
     64 // Number of simultaneous threads running in our forked process.
     65 #define NUM_PTRACE_THREADS 5
     66 
     67 struct thread_t {
     68   pid_t tid;
     69   int32_t state;
     70   pthread_t threadId;
     71   void* data;
     72 };
     73 
     74 struct dump_thread_t {
     75   thread_t thread;
     76   Backtrace* backtrace;
     77   int32_t* now;
     78   int32_t done;
     79 };
     80 
     81 extern "C" {
     82 // Prototypes for functions in the test library.
     83 int test_level_one(int, int, int, int, void (*)(void*), void*);
     84 
     85 int test_recursive_call(int, void (*)(void*), void*);
     86 }
     87 
     88 uint64_t NanoTime() {
     89   struct timespec t = { 0, 0 };
     90   clock_gettime(CLOCK_MONOTONIC, &t);
     91   return static_cast<uint64_t>(t.tv_sec * NS_PER_SEC + t.tv_nsec);
     92 }
     93 
     94 std::string DumpFrames(Backtrace* backtrace) {
     95   if (backtrace->NumFrames() == 0) {
     96     return "   No frames to dump.\n";
     97   }
     98 
     99   std::string frame;
    100   for (size_t i = 0; i < backtrace->NumFrames(); i++) {
    101     frame += "   " + backtrace->FormatFrameData(i) + '\n';
    102   }
    103   return frame;
    104 }
    105 
    106 void WaitForStop(pid_t pid) {
    107   uint64_t start = NanoTime();
    108 
    109   siginfo_t si;
    110   while (ptrace(PTRACE_GETSIGINFO, pid, 0, &si) < 0 && (errno == EINTR || errno == ESRCH)) {
    111     if ((NanoTime() - start) > NS_PER_SEC) {
    112       printf("The process did not get to a stopping point in 1 second.\n");
    113       break;
    114     }
    115     usleep(US_PER_MSEC);
    116   }
    117 }
    118 
    119 bool ReadyLevelBacktrace(Backtrace* backtrace) {
    120   // See if test_level_four is in the backtrace.
    121   bool found = false;
    122   for (Backtrace::const_iterator it = backtrace->begin(); it != backtrace->end(); ++it) {
    123     if (it->func_name == "test_level_four") {
    124       found = true;
    125       break;
    126     }
    127   }
    128 
    129   return found;
    130 }
    131 
    132 void VerifyLevelDump(Backtrace* backtrace) {
    133   ASSERT_GT(backtrace->NumFrames(), static_cast<size_t>(0))
    134     << DumpFrames(backtrace);
    135   ASSERT_LT(backtrace->NumFrames(), static_cast<size_t>(MAX_BACKTRACE_FRAMES))
    136     << DumpFrames(backtrace);
    137 
    138   // Look through the frames starting at the highest to find the
    139   // frame we want.
    140   size_t frame_num = 0;
    141   for (size_t i = backtrace->NumFrames()-1; i > 2; i--) {
    142     if (backtrace->GetFrame(i)->func_name == "test_level_one") {
    143       frame_num = i;
    144       break;
    145     }
    146   }
    147   ASSERT_LT(static_cast<size_t>(0), frame_num) << DumpFrames(backtrace);
    148   ASSERT_LE(static_cast<size_t>(3), frame_num) << DumpFrames(backtrace);
    149 
    150   ASSERT_EQ(backtrace->GetFrame(frame_num)->func_name, "test_level_one")
    151     << DumpFrames(backtrace);
    152   ASSERT_EQ(backtrace->GetFrame(frame_num-1)->func_name, "test_level_two")
    153     << DumpFrames(backtrace);
    154   ASSERT_EQ(backtrace->GetFrame(frame_num-2)->func_name, "test_level_three")
    155     << DumpFrames(backtrace);
    156   ASSERT_EQ(backtrace->GetFrame(frame_num-3)->func_name, "test_level_four")
    157     << DumpFrames(backtrace);
    158 }
    159 
    160 void VerifyLevelBacktrace(void*) {
    161   std::unique_ptr<Backtrace> backtrace(
    162       Backtrace::Create(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD));
    163   ASSERT_TRUE(backtrace.get() != nullptr);
    164   ASSERT_TRUE(backtrace->Unwind(0));
    165 
    166   VerifyLevelDump(backtrace.get());
    167 }
    168 
    169 bool ReadyMaxBacktrace(Backtrace* backtrace) {
    170   return (backtrace->NumFrames() == MAX_BACKTRACE_FRAMES);
    171 }
    172 
    173 void VerifyMaxDump(Backtrace* backtrace) {
    174   ASSERT_EQ(backtrace->NumFrames(), static_cast<size_t>(MAX_BACKTRACE_FRAMES))
    175     << DumpFrames(backtrace);
    176   // Verify that the last frame is our recursive call.
    177   ASSERT_EQ(backtrace->GetFrame(MAX_BACKTRACE_FRAMES-1)->func_name, "test_recursive_call")
    178     << DumpFrames(backtrace);
    179 }
    180 
    181 void VerifyMaxBacktrace(void*) {
    182   std::unique_ptr<Backtrace> backtrace(
    183       Backtrace::Create(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD));
    184   ASSERT_TRUE(backtrace.get() != nullptr);
    185   ASSERT_TRUE(backtrace->Unwind(0));
    186 
    187   VerifyMaxDump(backtrace.get());
    188 }
    189 
    190 void ThreadSetState(void* data) {
    191   thread_t* thread = reinterpret_cast<thread_t*>(data);
    192   android_atomic_acquire_store(1, &thread->state);
    193   volatile int i = 0;
    194   while (thread->state) {
    195     i++;
    196   }
    197 }
    198 
    199 void VerifyThreadTest(pid_t tid, void (*VerifyFunc)(Backtrace*)) {
    200   std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), tid));
    201   ASSERT_TRUE(backtrace.get() != nullptr);
    202   ASSERT_TRUE(backtrace->Unwind(0));
    203 
    204   VerifyFunc(backtrace.get());
    205 }
    206 
    207 bool WaitForNonZero(int32_t* value, uint64_t seconds) {
    208   uint64_t start = NanoTime();
    209   do {
    210     if (android_atomic_acquire_load(value)) {
    211       return true;
    212     }
    213   } while ((NanoTime() - start) < seconds * NS_PER_SEC);
    214   return false;
    215 }
    216 
    217 TEST(libbacktrace, local_no_unwind_frames) {
    218   // Verify that a local unwind does not include any frames within
    219   // libunwind or libbacktrace.
    220   std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), getpid()));
    221   ASSERT_TRUE(backtrace.get() != nullptr);
    222   ASSERT_TRUE(backtrace->Unwind(0));
    223 
    224   ASSERT_TRUE(backtrace->NumFrames() != 0);
    225   for (const auto& frame : *backtrace ) {
    226     if (BacktraceMap::IsValid(frame.map)) {
    227       const std::string name = basename(frame.map.name.c_str());
    228       ASSERT_TRUE(name != "libunwind.so" && name != "libbacktrace.so")
    229         << DumpFrames(backtrace.get());
    230     }
    231     break;
    232   }
    233 }
    234 
    235 TEST(libbacktrace, local_trace) {
    236   ASSERT_NE(test_level_one(1, 2, 3, 4, VerifyLevelBacktrace, nullptr), 0);
    237 }
    238 
    239 void VerifyIgnoreFrames(
    240     Backtrace* bt_all, Backtrace* bt_ign1,
    241     Backtrace* bt_ign2, const char* cur_proc) {
    242   EXPECT_EQ(bt_all->NumFrames(), bt_ign1->NumFrames() + 1)
    243     << "All backtrace:\n" << DumpFrames(bt_all) << "Ignore 1 backtrace:\n" << DumpFrames(bt_ign1);
    244   EXPECT_EQ(bt_all->NumFrames(), bt_ign2->NumFrames() + 2)
    245     << "All backtrace:\n" << DumpFrames(bt_all) << "Ignore 2 backtrace:\n" << DumpFrames(bt_ign2);
    246 
    247   // Check all of the frames are the same > the current frame.
    248   bool check = (cur_proc == nullptr);
    249   for (size_t i = 0; i < bt_ign2->NumFrames(); i++) {
    250     if (check) {
    251       EXPECT_EQ(bt_ign2->GetFrame(i)->pc, bt_ign1->GetFrame(i+1)->pc);
    252       EXPECT_EQ(bt_ign2->GetFrame(i)->sp, bt_ign1->GetFrame(i+1)->sp);
    253       EXPECT_EQ(bt_ign2->GetFrame(i)->stack_size, bt_ign1->GetFrame(i+1)->stack_size);
    254 
    255       EXPECT_EQ(bt_ign2->GetFrame(i)->pc, bt_all->GetFrame(i+2)->pc);
    256       EXPECT_EQ(bt_ign2->GetFrame(i)->sp, bt_all->GetFrame(i+2)->sp);
    257       EXPECT_EQ(bt_ign2->GetFrame(i)->stack_size, bt_all->GetFrame(i+2)->stack_size);
    258     }
    259     if (!check && bt_ign2->GetFrame(i)->func_name == cur_proc) {
    260       check = true;
    261     }
    262   }
    263 }
    264 
    265 void VerifyLevelIgnoreFrames(void*) {
    266   std::unique_ptr<Backtrace> all(
    267       Backtrace::Create(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD));
    268   ASSERT_TRUE(all.get() != nullptr);
    269   ASSERT_TRUE(all->Unwind(0));
    270 
    271   std::unique_ptr<Backtrace> ign1(
    272       Backtrace::Create(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD));
    273   ASSERT_TRUE(ign1.get() != nullptr);
    274   ASSERT_TRUE(ign1->Unwind(1));
    275 
    276   std::unique_ptr<Backtrace> ign2(
    277       Backtrace::Create(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD));
    278   ASSERT_TRUE(ign2.get() != nullptr);
    279   ASSERT_TRUE(ign2->Unwind(2));
    280 
    281   VerifyIgnoreFrames(all.get(), ign1.get(), ign2.get(), "VerifyLevelIgnoreFrames");
    282 }
    283 
    284 TEST(libbacktrace, local_trace_ignore_frames) {
    285   ASSERT_NE(test_level_one(1, 2, 3, 4, VerifyLevelIgnoreFrames, nullptr), 0);
    286 }
    287 
    288 TEST(libbacktrace, local_max_trace) {
    289   ASSERT_NE(test_recursive_call(MAX_BACKTRACE_FRAMES+10, VerifyMaxBacktrace, nullptr), 0);
    290 }
    291 
    292 void VerifyProcTest(pid_t pid, pid_t tid, bool share_map,
    293                     bool (*ReadyFunc)(Backtrace*),
    294                     void (*VerifyFunc)(Backtrace*)) {
    295   pid_t ptrace_tid;
    296   if (tid < 0) {
    297     ptrace_tid = pid;
    298   } else {
    299     ptrace_tid = tid;
    300   }
    301   uint64_t start = NanoTime();
    302   bool verified = false;
    303   std::string last_dump;
    304   do {
    305     usleep(US_PER_MSEC);
    306     if (ptrace(PTRACE_ATTACH, ptrace_tid, 0, 0) == 0) {
    307       // Wait for the process to get to a stopping point.
    308       WaitForStop(ptrace_tid);
    309 
    310       std::unique_ptr<BacktraceMap> map;
    311       if (share_map) {
    312         map.reset(BacktraceMap::Create(pid));
    313       }
    314       std::unique_ptr<Backtrace> backtrace(Backtrace::Create(pid, tid, map.get()));
    315       ASSERT_TRUE(backtrace.get() != nullptr);
    316       ASSERT_TRUE(backtrace->Unwind(0));
    317       if (ReadyFunc(backtrace.get())) {
    318         VerifyFunc(backtrace.get());
    319         verified = true;
    320       } else {
    321         last_dump = DumpFrames(backtrace.get());
    322       }
    323 
    324       ASSERT_TRUE(ptrace(PTRACE_DETACH, ptrace_tid, 0, 0) == 0);
    325     }
    326     // If 5 seconds have passed, then we are done.
    327   } while (!verified && (NanoTime() - start) <= 5 * NS_PER_SEC);
    328   ASSERT_TRUE(verified) << "Last backtrace:\n" << last_dump;
    329 }
    330 
    331 TEST(libbacktrace, ptrace_trace) {
    332   pid_t pid;
    333   if ((pid = fork()) == 0) {
    334     ASSERT_NE(test_level_one(1, 2, 3, 4, nullptr, nullptr), 0);
    335     _exit(1);
    336   }
    337   VerifyProcTest(pid, BACKTRACE_CURRENT_THREAD, false, ReadyLevelBacktrace, VerifyLevelDump);
    338 
    339   kill(pid, SIGKILL);
    340   int status;
    341   ASSERT_EQ(waitpid(pid, &status, 0), pid);
    342 }
    343 
    344 TEST(libbacktrace, ptrace_trace_shared_map) {
    345   pid_t pid;
    346   if ((pid = fork()) == 0) {
    347     ASSERT_NE(test_level_one(1, 2, 3, 4, nullptr, nullptr), 0);
    348     _exit(1);
    349   }
    350 
    351   VerifyProcTest(pid, BACKTRACE_CURRENT_THREAD, true, ReadyLevelBacktrace, VerifyLevelDump);
    352 
    353   kill(pid, SIGKILL);
    354   int status;
    355   ASSERT_EQ(waitpid(pid, &status, 0), pid);
    356 }
    357 
    358 TEST(libbacktrace, ptrace_max_trace) {
    359   pid_t pid;
    360   if ((pid = fork()) == 0) {
    361     ASSERT_NE(test_recursive_call(MAX_BACKTRACE_FRAMES+10, nullptr, nullptr), 0);
    362     _exit(1);
    363   }
    364   VerifyProcTest(pid, BACKTRACE_CURRENT_THREAD, false, ReadyMaxBacktrace, VerifyMaxDump);
    365 
    366   kill(pid, SIGKILL);
    367   int status;
    368   ASSERT_EQ(waitpid(pid, &status, 0), pid);
    369 }
    370 
    371 void VerifyProcessIgnoreFrames(Backtrace* bt_all) {
    372   std::unique_ptr<Backtrace> ign1(Backtrace::Create(bt_all->Pid(), BACKTRACE_CURRENT_THREAD));
    373   ASSERT_TRUE(ign1.get() != nullptr);
    374   ASSERT_TRUE(ign1->Unwind(1));
    375 
    376   std::unique_ptr<Backtrace> ign2(Backtrace::Create(bt_all->Pid(), BACKTRACE_CURRENT_THREAD));
    377   ASSERT_TRUE(ign2.get() != nullptr);
    378   ASSERT_TRUE(ign2->Unwind(2));
    379 
    380   VerifyIgnoreFrames(bt_all, ign1.get(), ign2.get(), nullptr);
    381 }
    382 
    383 TEST(libbacktrace, ptrace_ignore_frames) {
    384   pid_t pid;
    385   if ((pid = fork()) == 0) {
    386     ASSERT_NE(test_level_one(1, 2, 3, 4, nullptr, nullptr), 0);
    387     _exit(1);
    388   }
    389   VerifyProcTest(pid, BACKTRACE_CURRENT_THREAD, false, ReadyLevelBacktrace, VerifyProcessIgnoreFrames);
    390 
    391   kill(pid, SIGKILL);
    392   int status;
    393   ASSERT_EQ(waitpid(pid, &status, 0), pid);
    394 }
    395 
    396 // Create a process with multiple threads and dump all of the threads.
    397 void* PtraceThreadLevelRun(void*) {
    398   EXPECT_NE(test_level_one(1, 2, 3, 4, nullptr, nullptr), 0);
    399   return nullptr;
    400 }
    401 
    402 void GetThreads(pid_t pid, std::vector<pid_t>* threads) {
    403   // Get the list of tasks.
    404   char task_path[128];
    405   snprintf(task_path, sizeof(task_path), "/proc/%d/task", pid);
    406 
    407   DIR* tasks_dir = opendir(task_path);
    408   ASSERT_TRUE(tasks_dir != nullptr);
    409   struct dirent* entry;
    410   while ((entry = readdir(tasks_dir)) != nullptr) {
    411     char* end;
    412     pid_t tid = strtoul(entry->d_name, &end, 10);
    413     if (*end == '\0') {
    414       threads->push_back(tid);
    415     }
    416   }
    417   closedir(tasks_dir);
    418 }
    419 
    420 TEST(libbacktrace, ptrace_threads) {
    421   pid_t pid;
    422   if ((pid = fork()) == 0) {
    423     for (size_t i = 0; i < NUM_PTRACE_THREADS; i++) {
    424       pthread_attr_t attr;
    425       pthread_attr_init(&attr);
    426       pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
    427 
    428       pthread_t thread;
    429       ASSERT_TRUE(pthread_create(&thread, &attr, PtraceThreadLevelRun, nullptr) == 0);
    430     }
    431     ASSERT_NE(test_level_one(1, 2, 3, 4, nullptr, nullptr), 0);
    432     _exit(1);
    433   }
    434 
    435   // Check to see that all of the threads are running before unwinding.
    436   std::vector<pid_t> threads;
    437   uint64_t start = NanoTime();
    438   do {
    439     usleep(US_PER_MSEC);
    440     threads.clear();
    441     GetThreads(pid, &threads);
    442   } while ((threads.size() != NUM_PTRACE_THREADS + 1) &&
    443       ((NanoTime() - start) <= 5 * NS_PER_SEC));
    444   ASSERT_EQ(threads.size(), static_cast<size_t>(NUM_PTRACE_THREADS + 1));
    445 
    446   ASSERT_TRUE(ptrace(PTRACE_ATTACH, pid, 0, 0) == 0);
    447   WaitForStop(pid);
    448   for (std::vector<int>::const_iterator it = threads.begin(); it != threads.end(); ++it) {
    449     // Skip the current forked process, we only care about the threads.
    450     if (pid == *it) {
    451       continue;
    452     }
    453     VerifyProcTest(pid, *it, false, ReadyLevelBacktrace, VerifyLevelDump);
    454   }
    455   ASSERT_TRUE(ptrace(PTRACE_DETACH, pid, 0, 0) == 0);
    456 
    457   kill(pid, SIGKILL);
    458   int status;
    459   ASSERT_EQ(waitpid(pid, &status, 0), pid);
    460 }
    461 
    462 void VerifyLevelThread(void*) {
    463   std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), gettid()));
    464   ASSERT_TRUE(backtrace.get() != nullptr);
    465   ASSERT_TRUE(backtrace->Unwind(0));
    466 
    467   VerifyLevelDump(backtrace.get());
    468 }
    469 
    470 TEST(libbacktrace, thread_current_level) {
    471   ASSERT_NE(test_level_one(1, 2, 3, 4, VerifyLevelThread, nullptr), 0);
    472 }
    473 
    474 void VerifyMaxThread(void*) {
    475   std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), gettid()));
    476   ASSERT_TRUE(backtrace.get() != nullptr);
    477   ASSERT_TRUE(backtrace->Unwind(0));
    478 
    479   VerifyMaxDump(backtrace.get());
    480 }
    481 
    482 TEST(libbacktrace, thread_current_max) {
    483   ASSERT_NE(test_recursive_call(MAX_BACKTRACE_FRAMES+10, VerifyMaxThread, nullptr), 0);
    484 }
    485 
    486 void* ThreadLevelRun(void* data) {
    487   thread_t* thread = reinterpret_cast<thread_t*>(data);
    488 
    489   thread->tid = gettid();
    490   EXPECT_NE(test_level_one(1, 2, 3, 4, ThreadSetState, data), 0);
    491   return nullptr;
    492 }
    493 
    494 TEST(libbacktrace, thread_level_trace) {
    495   pthread_attr_t attr;
    496   pthread_attr_init(&attr);
    497   pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
    498 
    499   thread_t thread_data = { 0, 0, 0, nullptr };
    500   pthread_t thread;
    501   ASSERT_TRUE(pthread_create(&thread, &attr, ThreadLevelRun, &thread_data) == 0);
    502 
    503   // Wait up to 2 seconds for the tid to be set.
    504   ASSERT_TRUE(WaitForNonZero(&thread_data.state, 2));
    505 
    506   // Make sure that the thread signal used is not visible when compiled for
    507   // the target.
    508 #if !defined(__GLIBC__)
    509   ASSERT_LT(THREAD_SIGNAL, SIGRTMIN);
    510 #endif
    511 
    512   // Save the current signal action and make sure it is restored afterwards.
    513   struct sigaction cur_action;
    514   ASSERT_TRUE(sigaction(THREAD_SIGNAL, nullptr, &cur_action) == 0);
    515 
    516   std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), thread_data.tid));
    517   ASSERT_TRUE(backtrace.get() != nullptr);
    518   ASSERT_TRUE(backtrace->Unwind(0));
    519 
    520   VerifyLevelDump(backtrace.get());
    521 
    522   // Tell the thread to exit its infinite loop.
    523   android_atomic_acquire_store(0, &thread_data.state);
    524 
    525   // Verify that the old action was restored.
    526   struct sigaction new_action;
    527   ASSERT_TRUE(sigaction(THREAD_SIGNAL, nullptr, &new_action) == 0);
    528   EXPECT_EQ(cur_action.sa_sigaction, new_action.sa_sigaction);
    529   // The SA_RESTORER flag gets set behind our back, so a direct comparison
    530   // doesn't work unless we mask the value off. Mips doesn't have this
    531   // flag, so skip this on that platform.
    532 #if defined(SA_RESTORER)
    533   cur_action.sa_flags &= ~SA_RESTORER;
    534   new_action.sa_flags &= ~SA_RESTORER;
    535 #elif defined(__GLIBC__)
    536   // Our host compiler doesn't appear to define this flag for some reason.
    537   cur_action.sa_flags &= ~0x04000000;
    538   new_action.sa_flags &= ~0x04000000;
    539 #endif
    540   EXPECT_EQ(cur_action.sa_flags, new_action.sa_flags);
    541 }
    542 
    543 TEST(libbacktrace, thread_ignore_frames) {
    544   pthread_attr_t attr;
    545   pthread_attr_init(&attr);
    546   pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
    547 
    548   thread_t thread_data = { 0, 0, 0, nullptr };
    549   pthread_t thread;
    550   ASSERT_TRUE(pthread_create(&thread, &attr, ThreadLevelRun, &thread_data) == 0);
    551 
    552   // Wait up to 2 seconds for the tid to be set.
    553   ASSERT_TRUE(WaitForNonZero(&thread_data.state, 2));
    554 
    555   std::unique_ptr<Backtrace> all(Backtrace::Create(getpid(), thread_data.tid));
    556   ASSERT_TRUE(all.get() != nullptr);
    557   ASSERT_TRUE(all->Unwind(0));
    558 
    559   std::unique_ptr<Backtrace> ign1(Backtrace::Create(getpid(), thread_data.tid));
    560   ASSERT_TRUE(ign1.get() != nullptr);
    561   ASSERT_TRUE(ign1->Unwind(1));
    562 
    563   std::unique_ptr<Backtrace> ign2(Backtrace::Create(getpid(), thread_data.tid));
    564   ASSERT_TRUE(ign2.get() != nullptr);
    565   ASSERT_TRUE(ign2->Unwind(2));
    566 
    567   VerifyIgnoreFrames(all.get(), ign1.get(), ign2.get(), nullptr);
    568 
    569   // Tell the thread to exit its infinite loop.
    570   android_atomic_acquire_store(0, &thread_data.state);
    571 }
    572 
    573 void* ThreadMaxRun(void* data) {
    574   thread_t* thread = reinterpret_cast<thread_t*>(data);
    575 
    576   thread->tid = gettid();
    577   EXPECT_NE(test_recursive_call(MAX_BACKTRACE_FRAMES+10, ThreadSetState, data), 0);
    578   return nullptr;
    579 }
    580 
    581 TEST(libbacktrace, thread_max_trace) {
    582   pthread_attr_t attr;
    583   pthread_attr_init(&attr);
    584   pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
    585 
    586   thread_t thread_data = { 0, 0, 0, nullptr };
    587   pthread_t thread;
    588   ASSERT_TRUE(pthread_create(&thread, &attr, ThreadMaxRun, &thread_data) == 0);
    589 
    590   // Wait for the tid to be set.
    591   ASSERT_TRUE(WaitForNonZero(&thread_data.state, 2));
    592 
    593   std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), thread_data.tid));
    594   ASSERT_TRUE(backtrace.get() != nullptr);
    595   ASSERT_TRUE(backtrace->Unwind(0));
    596 
    597   VerifyMaxDump(backtrace.get());
    598 
    599   // Tell the thread to exit its infinite loop.
    600   android_atomic_acquire_store(0, &thread_data.state);
    601 }
    602 
    603 void* ThreadDump(void* data) {
    604   dump_thread_t* dump = reinterpret_cast<dump_thread_t*>(data);
    605   while (true) {
    606     if (android_atomic_acquire_load(dump->now)) {
    607       break;
    608     }
    609   }
    610 
    611   // The status of the actual unwind will be checked elsewhere.
    612   dump->backtrace = Backtrace::Create(getpid(), dump->thread.tid);
    613   dump->backtrace->Unwind(0);
    614 
    615   android_atomic_acquire_store(1, &dump->done);
    616 
    617   return nullptr;
    618 }
    619 
    620 TEST(libbacktrace, thread_multiple_dump) {
    621   // Dump NUM_THREADS simultaneously.
    622   std::vector<thread_t> runners(NUM_THREADS);
    623   std::vector<dump_thread_t> dumpers(NUM_THREADS);
    624 
    625   pthread_attr_t attr;
    626   pthread_attr_init(&attr);
    627   pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
    628   for (size_t i = 0; i < NUM_THREADS; i++) {
    629     // Launch the runners, they will spin in hard loops doing nothing.
    630     runners[i].tid = 0;
    631     runners[i].state = 0;
    632     ASSERT_TRUE(pthread_create(&runners[i].threadId, &attr, ThreadMaxRun, &runners[i]) == 0);
    633   }
    634 
    635   // Wait for tids to be set.
    636   for (std::vector<thread_t>::iterator it = runners.begin(); it != runners.end(); ++it) {
    637     ASSERT_TRUE(WaitForNonZero(&it->state, 30));
    638   }
    639 
    640   // Start all of the dumpers at once, they will spin until they are signalled
    641   // to begin their dump run.
    642   int32_t dump_now = 0;
    643   for (size_t i = 0; i < NUM_THREADS; i++) {
    644     dumpers[i].thread.tid = runners[i].tid;
    645     dumpers[i].thread.state = 0;
    646     dumpers[i].done = 0;
    647     dumpers[i].now = &dump_now;
    648 
    649     ASSERT_TRUE(pthread_create(&dumpers[i].thread.threadId, &attr, ThreadDump, &dumpers[i]) == 0);
    650   }
    651 
    652   // Start all of the dumpers going at once.
    653   android_atomic_acquire_store(1, &dump_now);
    654 
    655   for (size_t i = 0; i < NUM_THREADS; i++) {
    656     ASSERT_TRUE(WaitForNonZero(&dumpers[i].done, 30));
    657 
    658     // Tell the runner thread to exit its infinite loop.
    659     android_atomic_acquire_store(0, &runners[i].state);
    660 
    661     ASSERT_TRUE(dumpers[i].backtrace != nullptr);
    662     VerifyMaxDump(dumpers[i].backtrace);
    663 
    664     delete dumpers[i].backtrace;
    665     dumpers[i].backtrace = nullptr;
    666   }
    667 }
    668 
    669 TEST(libbacktrace, thread_multiple_dump_same_thread) {
    670   pthread_attr_t attr;
    671   pthread_attr_init(&attr);
    672   pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
    673   thread_t runner;
    674   runner.tid = 0;
    675   runner.state = 0;
    676   ASSERT_TRUE(pthread_create(&runner.threadId, &attr, ThreadMaxRun, &runner) == 0);
    677 
    678   // Wait for tids to be set.
    679   ASSERT_TRUE(WaitForNonZero(&runner.state, 30));
    680 
    681   // Start all of the dumpers at once, they will spin until they are signalled
    682   // to begin their dump run.
    683   int32_t dump_now = 0;
    684   // Dump the same thread NUM_THREADS simultaneously.
    685   std::vector<dump_thread_t> dumpers(NUM_THREADS);
    686   for (size_t i = 0; i < NUM_THREADS; i++) {
    687     dumpers[i].thread.tid = runner.tid;
    688     dumpers[i].thread.state = 0;
    689     dumpers[i].done = 0;
    690     dumpers[i].now = &dump_now;
    691 
    692     ASSERT_TRUE(pthread_create(&dumpers[i].thread.threadId, &attr, ThreadDump, &dumpers[i]) == 0);
    693   }
    694 
    695   // Start all of the dumpers going at once.
    696   android_atomic_acquire_store(1, &dump_now);
    697 
    698   for (size_t i = 0; i < NUM_THREADS; i++) {
    699     ASSERT_TRUE(WaitForNonZero(&dumpers[i].done, 30));
    700 
    701     ASSERT_TRUE(dumpers[i].backtrace != nullptr);
    702     VerifyMaxDump(dumpers[i].backtrace);
    703 
    704     delete dumpers[i].backtrace;
    705     dumpers[i].backtrace = nullptr;
    706   }
    707 
    708   // Tell the runner thread to exit its infinite loop.
    709   android_atomic_acquire_store(0, &runner.state);
    710 }
    711 
    712 // This test is for UnwindMaps that should share the same map cursor when
    713 // multiple maps are created for the current process at the same time.
    714 TEST(libbacktrace, simultaneous_maps) {
    715   BacktraceMap* map1 = BacktraceMap::Create(getpid());
    716   BacktraceMap* map2 = BacktraceMap::Create(getpid());
    717   BacktraceMap* map3 = BacktraceMap::Create(getpid());
    718 
    719   Backtrace* back1 = Backtrace::Create(getpid(), BACKTRACE_CURRENT_THREAD, map1);
    720   ASSERT_TRUE(back1 != nullptr);
    721   EXPECT_TRUE(back1->Unwind(0));
    722   delete back1;
    723   delete map1;
    724 
    725   Backtrace* back2 = Backtrace::Create(getpid(), BACKTRACE_CURRENT_THREAD, map2);
    726   ASSERT_TRUE(back2 != nullptr);
    727   EXPECT_TRUE(back2->Unwind(0));
    728   delete back2;
    729   delete map2;
    730 
    731   Backtrace* back3 = Backtrace::Create(getpid(), BACKTRACE_CURRENT_THREAD, map3);
    732   ASSERT_TRUE(back3 != nullptr);
    733   EXPECT_TRUE(back3->Unwind(0));
    734   delete back3;
    735   delete map3;
    736 }
    737 
    738 TEST(libbacktrace, fillin_erases) {
    739   BacktraceMap* back_map = BacktraceMap::Create(getpid());
    740 
    741   backtrace_map_t map;
    742 
    743   map.start = 1;
    744   map.end = 3;
    745   map.flags = 1;
    746   map.name = "Initialized";
    747   back_map->FillIn(0, &map);
    748   delete back_map;
    749 
    750   ASSERT_FALSE(BacktraceMap::IsValid(map));
    751   ASSERT_EQ(static_cast<uintptr_t>(0), map.start);
    752   ASSERT_EQ(static_cast<uintptr_t>(0), map.end);
    753   ASSERT_EQ(0, map.flags);
    754   ASSERT_EQ("", map.name);
    755 }
    756 
    757 TEST(libbacktrace, format_test) {
    758   std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), BACKTRACE_CURRENT_THREAD));
    759   ASSERT_TRUE(backtrace.get() != nullptr);
    760 
    761   backtrace_frame_data_t frame;
    762   frame.num = 1;
    763   frame.pc = 2;
    764   frame.sp = 0;
    765   frame.stack_size = 0;
    766   frame.func_offset = 0;
    767 
    768   // Check no map set.
    769   frame.num = 1;
    770 #if defined(__LP64__)
    771   EXPECT_EQ("#01 pc 0000000000000002  <unknown>",
    772 #else
    773   EXPECT_EQ("#01 pc 00000002  <unknown>",
    774 #endif
    775             backtrace->FormatFrameData(&frame));
    776 
    777   // Check map name empty, but exists.
    778   frame.map.start = 1;
    779   frame.map.end = 1;
    780   frame.map.load_base = 0;
    781 #if defined(__LP64__)
    782   EXPECT_EQ("#01 pc 0000000000000001  <unknown>",
    783 #else
    784   EXPECT_EQ("#01 pc 00000001  <unknown>",
    785 #endif
    786             backtrace->FormatFrameData(&frame));
    787 
    788 
    789   // Check relative pc is set and map name is set.
    790   frame.pc = 0x12345679;
    791   frame.map.name = "MapFake";
    792   frame.map.start =  1;
    793   frame.map.end =  1;
    794 #if defined(__LP64__)
    795   EXPECT_EQ("#01 pc 0000000012345678  MapFake",
    796 #else
    797   EXPECT_EQ("#01 pc 12345678  MapFake",
    798 #endif
    799             backtrace->FormatFrameData(&frame));
    800 
    801   // Check func_name is set, but no func offset.
    802   frame.func_name = "ProcFake";
    803 #if defined(__LP64__)
    804   EXPECT_EQ("#01 pc 0000000012345678  MapFake (ProcFake)",
    805 #else
    806   EXPECT_EQ("#01 pc 12345678  MapFake (ProcFake)",
    807 #endif
    808             backtrace->FormatFrameData(&frame));
    809 
    810   // Check func_name is set, and func offset is non-zero.
    811   frame.func_offset = 645;
    812 #if defined(__LP64__)
    813   EXPECT_EQ("#01 pc 0000000012345678  MapFake (ProcFake+645)",
    814 #else
    815   EXPECT_EQ("#01 pc 12345678  MapFake (ProcFake+645)",
    816 #endif
    817             backtrace->FormatFrameData(&frame));
    818 
    819   // Check func_name is set, func offset is non-zero, and load_base is non-zero.
    820   frame.func_offset = 645;
    821   frame.map.load_base = 100;
    822 #if defined(__LP64__)
    823   EXPECT_EQ("#01 pc 00000000123456dc  MapFake (ProcFake+645)",
    824 #else
    825   EXPECT_EQ("#01 pc 123456dc  MapFake (ProcFake+645)",
    826 #endif
    827             backtrace->FormatFrameData(&frame));
    828 
    829   // Check a non-zero map offset.
    830   frame.map.offset = 0x1000;
    831 #if defined(__LP64__)
    832   EXPECT_EQ("#01 pc 00000000123456dc  MapFake (offset 0x1000) (ProcFake+645)",
    833 #else
    834   EXPECT_EQ("#01 pc 123456dc  MapFake (offset 0x1000) (ProcFake+645)",
    835 #endif
    836             backtrace->FormatFrameData(&frame));
    837 }
    838 
    839 struct map_test_t {
    840   uintptr_t start;
    841   uintptr_t end;
    842 };
    843 
    844 bool map_sort(map_test_t i, map_test_t j) {
    845   return i.start < j.start;
    846 }
    847 
    848 void VerifyMap(pid_t pid) {
    849   char buffer[4096];
    850   snprintf(buffer, sizeof(buffer), "/proc/%d/maps", pid);
    851 
    852   FILE* map_file = fopen(buffer, "r");
    853   ASSERT_TRUE(map_file != nullptr);
    854   std::vector<map_test_t> test_maps;
    855   while (fgets(buffer, sizeof(buffer), map_file)) {
    856     map_test_t map;
    857     ASSERT_EQ(2, sscanf(buffer, "%" SCNxPTR "-%" SCNxPTR " ", &map.start, &map.end));
    858     test_maps.push_back(map);
    859   }
    860   fclose(map_file);
    861   std::sort(test_maps.begin(), test_maps.end(), map_sort);
    862 
    863   std::unique_ptr<BacktraceMap> map(BacktraceMap::Create(pid));
    864 
    865   // Basic test that verifies that the map is in the expected order.
    866   std::vector<map_test_t>::const_iterator test_it = test_maps.begin();
    867   for (BacktraceMap::const_iterator it = map->begin(); it != map->end(); ++it) {
    868     ASSERT_TRUE(test_it != test_maps.end());
    869     ASSERT_EQ(test_it->start, it->start);
    870     ASSERT_EQ(test_it->end, it->end);
    871     ++test_it;
    872   }
    873   ASSERT_TRUE(test_it == test_maps.end());
    874 }
    875 
    876 TEST(libbacktrace, verify_map_remote) {
    877   pid_t pid;
    878 
    879   if ((pid = fork()) == 0) {
    880     while (true) {
    881     }
    882     _exit(0);
    883   }
    884   ASSERT_LT(0, pid);
    885 
    886   ASSERT_TRUE(ptrace(PTRACE_ATTACH, pid, 0, 0) == 0);
    887 
    888   // Wait for the process to get to a stopping point.
    889   WaitForStop(pid);
    890 
    891   // The maps should match exactly since the forked process has been paused.
    892   VerifyMap(pid);
    893 
    894   ASSERT_TRUE(ptrace(PTRACE_DETACH, pid, 0, 0) == 0);
    895 
    896   kill(pid, SIGKILL);
    897   ASSERT_EQ(waitpid(pid, nullptr, 0), pid);
    898 }
    899 
    900 void InitMemory(uint8_t* memory, size_t bytes) {
    901   for (size_t i = 0; i < bytes; i++) {
    902     memory[i] = i;
    903     if (memory[i] == '\0') {
    904       // Don't use '\0' in our data so we can verify that an overread doesn't
    905       // occur by using a '\0' as the character after the read data.
    906       memory[i] = 23;
    907     }
    908   }
    909 }
    910 
    911 void* ThreadReadTest(void* data) {
    912   thread_t* thread_data = reinterpret_cast<thread_t*>(data);
    913 
    914   thread_data->tid = gettid();
    915 
    916   // Create two map pages.
    917   // Mark the second page as not-readable.
    918   size_t pagesize = static_cast<size_t>(sysconf(_SC_PAGE_SIZE));
    919   uint8_t* memory;
    920   if (posix_memalign(reinterpret_cast<void**>(&memory), pagesize, 2 * pagesize) != 0) {
    921     return reinterpret_cast<void*>(-1);
    922   }
    923 
    924   if (mprotect(&memory[pagesize], pagesize, PROT_NONE) != 0) {
    925     return reinterpret_cast<void*>(-1);
    926   }
    927 
    928   // Set up a simple pattern in memory.
    929   InitMemory(memory, pagesize);
    930 
    931   thread_data->data = memory;
    932 
    933   // Tell the caller it's okay to start reading memory.
    934   android_atomic_acquire_store(1, &thread_data->state);
    935 
    936   // Loop waiting for the caller to finish reading the memory.
    937   while (thread_data->state) {
    938   }
    939 
    940   // Re-enable read-write on the page so that we don't crash if we try
    941   // and access data on this page when freeing the memory.
    942   if (mprotect(&memory[pagesize], pagesize, PROT_READ | PROT_WRITE) != 0) {
    943     return reinterpret_cast<void*>(-1);
    944   }
    945   free(memory);
    946 
    947   android_atomic_acquire_store(1, &thread_data->state);
    948 
    949   return nullptr;
    950 }
    951 
    952 void RunReadTest(Backtrace* backtrace, uintptr_t read_addr) {
    953   size_t pagesize = static_cast<size_t>(sysconf(_SC_PAGE_SIZE));
    954 
    955   // Create a page of data to use to do quick compares.
    956   uint8_t* expected = new uint8_t[pagesize];
    957   InitMemory(expected, pagesize);
    958 
    959   uint8_t* data = new uint8_t[2*pagesize];
    960   // Verify that we can only read one page worth of data.
    961   size_t bytes_read = backtrace->Read(read_addr, data, 2 * pagesize);
    962   ASSERT_EQ(pagesize, bytes_read);
    963   ASSERT_TRUE(memcmp(data, expected, pagesize) == 0);
    964 
    965   // Verify unaligned reads.
    966   for (size_t i = 1; i < sizeof(word_t); i++) {
    967     bytes_read = backtrace->Read(read_addr + i, data, 2 * sizeof(word_t));
    968     ASSERT_EQ(2 * sizeof(word_t), bytes_read);
    969     ASSERT_TRUE(memcmp(data, &expected[i], 2 * sizeof(word_t)) == 0)
    970         << "Offset at " << i << " failed";
    971   }
    972 
    973   // Verify small unaligned reads.
    974   for (size_t i = 1; i < sizeof(word_t); i++) {
    975     for (size_t j = 1; j < sizeof(word_t); j++) {
    976       // Set one byte past what we expect to read, to guarantee we don't overread.
    977       data[j] = '\0';
    978       bytes_read = backtrace->Read(read_addr + i, data, j);
    979       ASSERT_EQ(j, bytes_read);
    980       ASSERT_TRUE(memcmp(data, &expected[i], j) == 0)
    981           << "Offset at " << i << " length " << j << " miscompared";
    982       ASSERT_EQ('\0', data[j])
    983           << "Offset at " << i << " length " << j << " wrote too much data";
    984     }
    985   }
    986   delete data;
    987   delete expected;
    988 }
    989 
    990 TEST(libbacktrace, thread_read) {
    991   pthread_attr_t attr;
    992   pthread_attr_init(&attr);
    993   pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
    994   pthread_t thread;
    995   thread_t thread_data = { 0, 0, 0, nullptr };
    996   ASSERT_TRUE(pthread_create(&thread, &attr, ThreadReadTest, &thread_data) == 0);
    997 
    998   ASSERT_TRUE(WaitForNonZero(&thread_data.state, 10));
    999 
   1000   std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), thread_data.tid));
   1001   ASSERT_TRUE(backtrace.get() != nullptr);
   1002 
   1003   RunReadTest(backtrace.get(), reinterpret_cast<uintptr_t>(thread_data.data));
   1004 
   1005   android_atomic_acquire_store(0, &thread_data.state);
   1006 
   1007   ASSERT_TRUE(WaitForNonZero(&thread_data.state, 10));
   1008 }
   1009 
   1010 volatile uintptr_t g_ready = 0;
   1011 volatile uintptr_t g_addr = 0;
   1012 
   1013 void ForkedReadTest() {
   1014   // Create two map pages.
   1015   size_t pagesize = static_cast<size_t>(sysconf(_SC_PAGE_SIZE));
   1016   uint8_t* memory;
   1017   if (posix_memalign(reinterpret_cast<void**>(&memory), pagesize, 2 * pagesize) != 0) {
   1018     perror("Failed to allocate memory\n");
   1019     exit(1);
   1020   }
   1021 
   1022   // Mark the second page as not-readable.
   1023   if (mprotect(&memory[pagesize], pagesize, PROT_NONE) != 0) {
   1024     perror("Failed to mprotect memory\n");
   1025     exit(1);
   1026   }
   1027 
   1028   // Set up a simple pattern in memory.
   1029   InitMemory(memory, pagesize);
   1030 
   1031   g_addr = reinterpret_cast<uintptr_t>(memory);
   1032   g_ready = 1;
   1033 
   1034   while (1) {
   1035     usleep(US_PER_MSEC);
   1036   }
   1037 }
   1038 
   1039 TEST(libbacktrace, process_read) {
   1040   g_ready = 0;
   1041   pid_t pid;
   1042   if ((pid = fork()) == 0) {
   1043     ForkedReadTest();
   1044     exit(0);
   1045   }
   1046   ASSERT_NE(-1, pid);
   1047 
   1048   bool test_executed = false;
   1049   uint64_t start = NanoTime();
   1050   while (1) {
   1051     if (ptrace(PTRACE_ATTACH, pid, 0, 0) == 0) {
   1052       WaitForStop(pid);
   1053 
   1054       std::unique_ptr<Backtrace> backtrace(Backtrace::Create(pid, pid));
   1055       ASSERT_TRUE(backtrace.get() != nullptr);
   1056 
   1057       uintptr_t read_addr;
   1058       size_t bytes_read = backtrace->Read(reinterpret_cast<uintptr_t>(&g_ready),
   1059                                           reinterpret_cast<uint8_t*>(&read_addr),
   1060                                           sizeof(uintptr_t));
   1061       ASSERT_EQ(sizeof(uintptr_t), bytes_read);
   1062       if (read_addr) {
   1063         // The forked process is ready to be read.
   1064         bytes_read = backtrace->Read(reinterpret_cast<uintptr_t>(&g_addr),
   1065                                      reinterpret_cast<uint8_t*>(&read_addr),
   1066                                      sizeof(uintptr_t));
   1067         ASSERT_EQ(sizeof(uintptr_t), bytes_read);
   1068 
   1069         RunReadTest(backtrace.get(), read_addr);
   1070 
   1071         test_executed = true;
   1072         break;
   1073       }
   1074       ASSERT_TRUE(ptrace(PTRACE_DETACH, pid, 0, 0) == 0);
   1075     }
   1076     if ((NanoTime() - start) > 5 * NS_PER_SEC) {
   1077       break;
   1078     }
   1079     usleep(US_PER_MSEC);
   1080   }
   1081   kill(pid, SIGKILL);
   1082   ASSERT_EQ(waitpid(pid, nullptr, 0), pid);
   1083 
   1084   ASSERT_TRUE(test_executed);
   1085 }
   1086 
   1087 void VerifyFunctionsFound(const std::vector<std::string>& found_functions) {
   1088   // We expect to find these functions in libbacktrace_test. If we don't
   1089   // find them, that's a bug in the memory read handling code in libunwind.
   1090   std::list<std::string> expected_functions;
   1091   expected_functions.push_back("test_recursive_call");
   1092   expected_functions.push_back("test_level_one");
   1093   expected_functions.push_back("test_level_two");
   1094   expected_functions.push_back("test_level_three");
   1095   expected_functions.push_back("test_level_four");
   1096   for (const auto& found_function : found_functions) {
   1097     for (const auto& expected_function : expected_functions) {
   1098       if (found_function == expected_function) {
   1099         expected_functions.remove(found_function);
   1100         break;
   1101       }
   1102     }
   1103   }
   1104   ASSERT_TRUE(expected_functions.empty()) << "Not all functions found in shared library.";
   1105 }
   1106 
   1107 const char* CopySharedLibrary() {
   1108 #if defined(__LP64__)
   1109   const char* lib_name = "lib64";
   1110 #else
   1111   const char* lib_name = "lib";
   1112 #endif
   1113 
   1114 #if defined(__BIONIC__)
   1115   const char* tmp_so_name = "/data/local/tmp/libbacktrace_test.so";
   1116   std::string cp_cmd = android::base::StringPrintf("cp /system/%s/libbacktrace_test.so %s",
   1117                                                    lib_name, tmp_so_name);
   1118 #else
   1119   const char* tmp_so_name = "/tmp/libbacktrace_test.so";
   1120   if (getenv("ANDROID_HOST_OUT") == NULL) {
   1121     fprintf(stderr, "ANDROID_HOST_OUT not set, make sure you run lunch.");
   1122     return nullptr;
   1123   }
   1124   std::string cp_cmd = android::base::StringPrintf("cp %s/%s/libbacktrace_test.so %s",
   1125                                                    getenv("ANDROID_HOST_OUT"), lib_name,
   1126                                                    tmp_so_name);
   1127 #endif
   1128 
   1129   // Copy the shared so to a tempory directory.
   1130   system(cp_cmd.c_str());
   1131 
   1132   return tmp_so_name;
   1133 }
   1134 
   1135 TEST(libbacktrace, check_unreadable_elf_local) {
   1136   const char* tmp_so_name = CopySharedLibrary();
   1137   ASSERT_TRUE(tmp_so_name != nullptr);
   1138 
   1139   struct stat buf;
   1140   ASSERT_TRUE(stat(tmp_so_name, &buf) != -1);
   1141   uintptr_t map_size = buf.st_size;
   1142 
   1143   int fd = open(tmp_so_name, O_RDONLY);
   1144   ASSERT_TRUE(fd != -1);
   1145 
   1146   void* map = mmap(NULL, map_size, PROT_READ, MAP_PRIVATE, fd, 0);
   1147   ASSERT_TRUE(map != MAP_FAILED);
   1148   close(fd);
   1149   ASSERT_TRUE(unlink(tmp_so_name) != -1);
   1150 
   1151   std::vector<std::string> found_functions;
   1152   std::unique_ptr<Backtrace> backtrace(Backtrace::Create(BACKTRACE_CURRENT_PROCESS,
   1153                                                          BACKTRACE_CURRENT_THREAD));
   1154   ASSERT_TRUE(backtrace.get() != nullptr);
   1155 
   1156   // Needed before GetFunctionName will work.
   1157   backtrace->Unwind(0);
   1158 
   1159   // Loop through the entire map, and get every function we can find.
   1160   map_size += reinterpret_cast<uintptr_t>(map);
   1161   std::string last_func;
   1162   for (uintptr_t read_addr = reinterpret_cast<uintptr_t>(map);
   1163        read_addr < map_size; read_addr += 4) {
   1164     uintptr_t offset;
   1165     std::string func_name = backtrace->GetFunctionName(read_addr, &offset);
   1166     if (!func_name.empty() && last_func != func_name) {
   1167       found_functions.push_back(func_name);
   1168     }
   1169     last_func = func_name;
   1170   }
   1171 
   1172   ASSERT_TRUE(munmap(map, map_size - reinterpret_cast<uintptr_t>(map)) == 0);
   1173 
   1174   VerifyFunctionsFound(found_functions);
   1175 }
   1176 
   1177 TEST(libbacktrace, check_unreadable_elf_remote) {
   1178   const char* tmp_so_name = CopySharedLibrary();
   1179   ASSERT_TRUE(tmp_so_name != nullptr);
   1180 
   1181   g_ready = 0;
   1182 
   1183   struct stat buf;
   1184   ASSERT_TRUE(stat(tmp_so_name, &buf) != -1);
   1185   uintptr_t map_size = buf.st_size;
   1186 
   1187   pid_t pid;
   1188   if ((pid = fork()) == 0) {
   1189     int fd = open(tmp_so_name, O_RDONLY);
   1190     if (fd == -1) {
   1191       fprintf(stderr, "Failed to open file %s: %s\n", tmp_so_name, strerror(errno));
   1192       unlink(tmp_so_name);
   1193       exit(0);
   1194     }
   1195 
   1196     void* map = mmap(NULL, map_size, PROT_READ, MAP_PRIVATE, fd, 0);
   1197     if (map == MAP_FAILED) {
   1198       fprintf(stderr, "Failed to map in memory: %s\n", strerror(errno));
   1199       unlink(tmp_so_name);
   1200       exit(0);
   1201     }
   1202     close(fd);
   1203     if (unlink(tmp_so_name) == -1) {
   1204       fprintf(stderr, "Failed to unlink: %s\n", strerror(errno));
   1205       exit(0);
   1206     }
   1207 
   1208     g_addr = reinterpret_cast<uintptr_t>(map);
   1209     g_ready = 1;
   1210     while (true) {
   1211       usleep(US_PER_MSEC);
   1212     }
   1213     exit(0);
   1214   }
   1215   ASSERT_TRUE(pid > 0);
   1216 
   1217   std::vector<std::string> found_functions;
   1218   uint64_t start = NanoTime();
   1219   while (true) {
   1220     ASSERT_TRUE(ptrace(PTRACE_ATTACH, pid, 0, 0) == 0);
   1221 
   1222     // Wait for the process to get to a stopping point.
   1223     WaitForStop(pid);
   1224 
   1225     std::unique_ptr<Backtrace> backtrace(Backtrace::Create(pid, BACKTRACE_CURRENT_THREAD));
   1226     ASSERT_TRUE(backtrace.get() != nullptr);
   1227 
   1228     uintptr_t read_addr;
   1229     ASSERT_EQ(sizeof(uintptr_t), backtrace->Read(reinterpret_cast<uintptr_t>(&g_ready), reinterpret_cast<uint8_t*>(&read_addr), sizeof(uintptr_t)));
   1230     if (read_addr) {
   1231       ASSERT_EQ(sizeof(uintptr_t), backtrace->Read(reinterpret_cast<uintptr_t>(&g_addr), reinterpret_cast<uint8_t*>(&read_addr), sizeof(uintptr_t)));
   1232 
   1233       // Needed before GetFunctionName will work.
   1234       backtrace->Unwind(0);
   1235 
   1236       // Loop through the entire map, and get every function we can find.
   1237       map_size += read_addr;
   1238       std::string last_func;
   1239       for (; read_addr < map_size; read_addr += 4) {
   1240         uintptr_t offset;
   1241         std::string func_name = backtrace->GetFunctionName(read_addr, &offset);
   1242         if (!func_name.empty() && last_func != func_name) {
   1243           found_functions.push_back(func_name);
   1244         }
   1245         last_func = func_name;
   1246       }
   1247       break;
   1248     }
   1249     ASSERT_TRUE(ptrace(PTRACE_DETACH, pid, 0, 0) == 0);
   1250 
   1251     if ((NanoTime() - start) > 5 * NS_PER_SEC) {
   1252       break;
   1253     }
   1254     usleep(US_PER_MSEC);
   1255   }
   1256 
   1257   kill(pid, SIGKILL);
   1258   ASSERT_EQ(waitpid(pid, nullptr, 0), pid);
   1259 
   1260   VerifyFunctionsFound(found_functions);
   1261 }
   1262 
   1263 bool FindFuncFrameInBacktrace(Backtrace* backtrace, uintptr_t test_func, size_t* frame_num) {
   1264   backtrace_map_t map;
   1265   backtrace->FillInMap(test_func, &map);
   1266   if (!BacktraceMap::IsValid(map)) {
   1267     return false;
   1268   }
   1269 
   1270   // Loop through the frames, and find the one that is in the map.
   1271   *frame_num = 0;
   1272   for (Backtrace::const_iterator it = backtrace->begin(); it != backtrace->end(); ++it) {
   1273     if (BacktraceMap::IsValid(it->map) && map.start == it->map.start &&
   1274         it->pc >= test_func) {
   1275       *frame_num = it->num;
   1276       return true;
   1277     }
   1278   }
   1279   return false;
   1280 }
   1281 
   1282 void VerifyUnreadableElfFrame(Backtrace* backtrace, uintptr_t test_func, size_t frame_num) {
   1283   ASSERT_LT(backtrace->NumFrames(), static_cast<size_t>(MAX_BACKTRACE_FRAMES))
   1284     << DumpFrames(backtrace);
   1285 
   1286   ASSERT_TRUE(frame_num != 0) << DumpFrames(backtrace);
   1287   // Make sure that there is at least one more frame above the test func call.
   1288   ASSERT_LT(frame_num, backtrace->NumFrames()) << DumpFrames(backtrace);
   1289 
   1290   uintptr_t diff = backtrace->GetFrame(frame_num)->pc - test_func;
   1291   ASSERT_LT(diff, 200U) << DumpFrames(backtrace);
   1292 }
   1293 
   1294 void VerifyUnreadableElfBacktrace(uintptr_t test_func) {
   1295   std::unique_ptr<Backtrace> backtrace(Backtrace::Create(BACKTRACE_CURRENT_PROCESS,
   1296                                                          BACKTRACE_CURRENT_THREAD));
   1297   ASSERT_TRUE(backtrace.get() != nullptr);
   1298   ASSERT_TRUE(backtrace->Unwind(0));
   1299 
   1300   size_t frame_num;
   1301   ASSERT_TRUE(FindFuncFrameInBacktrace(backtrace.get(), test_func, &frame_num));
   1302 
   1303   VerifyUnreadableElfFrame(backtrace.get(), test_func, frame_num);
   1304 }
   1305 
   1306 typedef int (*test_func_t)(int, int, int, int, void (*)(uintptr_t), uintptr_t);
   1307 
   1308 TEST(libbacktrace, unwind_through_unreadable_elf_local) {
   1309   const char* tmp_so_name = CopySharedLibrary();
   1310   ASSERT_TRUE(tmp_so_name != nullptr);
   1311   void* lib_handle = dlopen(tmp_so_name, RTLD_NOW);
   1312   ASSERT_TRUE(lib_handle != nullptr);
   1313   ASSERT_TRUE(unlink(tmp_so_name) != -1);
   1314 
   1315   test_func_t test_func;
   1316   test_func = reinterpret_cast<test_func_t>(dlsym(lib_handle, "test_level_one"));
   1317   ASSERT_TRUE(test_func != nullptr);
   1318 
   1319   ASSERT_NE(test_func(1, 2, 3, 4, VerifyUnreadableElfBacktrace,
   1320                       reinterpret_cast<uintptr_t>(test_func)), 0);
   1321 
   1322   ASSERT_TRUE(dlclose(lib_handle) == 0);
   1323 }
   1324 
   1325 TEST(libbacktrace, unwind_through_unreadable_elf_remote) {
   1326   const char* tmp_so_name = CopySharedLibrary();
   1327   ASSERT_TRUE(tmp_so_name != nullptr);
   1328   void* lib_handle = dlopen(tmp_so_name, RTLD_NOW);
   1329   ASSERT_TRUE(lib_handle != nullptr);
   1330   ASSERT_TRUE(unlink(tmp_so_name) != -1);
   1331 
   1332   test_func_t test_func;
   1333   test_func = reinterpret_cast<test_func_t>(dlsym(lib_handle, "test_level_one"));
   1334   ASSERT_TRUE(test_func != nullptr);
   1335 
   1336   pid_t pid;
   1337   if ((pid = fork()) == 0) {
   1338     test_func(1, 2, 3, 4, 0, 0);
   1339     exit(0);
   1340   }
   1341   ASSERT_TRUE(pid > 0);
   1342   ASSERT_TRUE(dlclose(lib_handle) == 0);
   1343 
   1344   uint64_t start = NanoTime();
   1345   bool done = false;
   1346   while (!done) {
   1347     ASSERT_TRUE(ptrace(PTRACE_ATTACH, pid, 0, 0) == 0);
   1348 
   1349     // Wait for the process to get to a stopping point.
   1350     WaitForStop(pid);
   1351 
   1352     std::unique_ptr<Backtrace> backtrace(Backtrace::Create(pid, BACKTRACE_CURRENT_THREAD));
   1353     ASSERT_TRUE(backtrace.get() != nullptr);
   1354     ASSERT_TRUE(backtrace->Unwind(0));
   1355 
   1356     size_t frame_num;
   1357     if (FindFuncFrameInBacktrace(backtrace.get(),
   1358                                  reinterpret_cast<uintptr_t>(test_func), &frame_num)) {
   1359 
   1360       VerifyUnreadableElfFrame(backtrace.get(), reinterpret_cast<uintptr_t>(test_func), frame_num);
   1361       done = true;
   1362     }
   1363 
   1364     ASSERT_TRUE(ptrace(PTRACE_DETACH, pid, 0, 0) == 0);
   1365 
   1366     if ((NanoTime() - start) > 5 * NS_PER_SEC) {
   1367       break;
   1368     }
   1369     usleep(US_PER_MSEC);
   1370   }
   1371 
   1372   kill(pid, SIGKILL);
   1373   ASSERT_EQ(waitpid(pid, nullptr, 0), pid);
   1374 
   1375   ASSERT_TRUE(done) << "Test function never found in unwind.";
   1376 }
   1377 
   1378 #if defined(ENABLE_PSS_TESTS)
   1379 #include "GetPss.h"
   1380 
   1381 #define MAX_LEAK_BYTES 32*1024UL
   1382 
   1383 void CheckForLeak(pid_t pid, pid_t tid) {
   1384   // Do a few runs to get the PSS stable.
   1385   for (size_t i = 0; i < 100; i++) {
   1386     Backtrace* backtrace = Backtrace::Create(pid, tid);
   1387     ASSERT_TRUE(backtrace != nullptr);
   1388     ASSERT_TRUE(backtrace->Unwind(0));
   1389     delete backtrace;
   1390   }
   1391   size_t stable_pss = GetPssBytes();
   1392   ASSERT_TRUE(stable_pss != 0);
   1393 
   1394   // Loop enough that even a small leak should be detectable.
   1395   for (size_t i = 0; i < 4096; i++) {
   1396     Backtrace* backtrace = Backtrace::Create(pid, tid);
   1397     ASSERT_TRUE(backtrace != nullptr);
   1398     ASSERT_TRUE(backtrace->Unwind(0));
   1399     delete backtrace;
   1400   }
   1401   size_t new_pss = GetPssBytes();
   1402   ASSERT_TRUE(new_pss != 0);
   1403   size_t abs_diff = (new_pss > stable_pss) ? new_pss - stable_pss : stable_pss - new_pss;
   1404   // As long as the new pss is within a certain amount, consider everything okay.
   1405   ASSERT_LE(abs_diff, MAX_LEAK_BYTES);
   1406 }
   1407 
   1408 TEST(libbacktrace, check_for_leak_local) {
   1409   CheckForLeak(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD);
   1410 }
   1411 
   1412 TEST(libbacktrace, check_for_leak_local_thread) {
   1413   thread_t thread_data = { 0, 0, 0, nullptr };
   1414   pthread_t thread;
   1415   ASSERT_TRUE(pthread_create(&thread, nullptr, ThreadLevelRun, &thread_data) == 0);
   1416 
   1417   // Wait up to 2 seconds for the tid to be set.
   1418   ASSERT_TRUE(WaitForNonZero(&thread_data.state, 2));
   1419 
   1420   CheckForLeak(BACKTRACE_CURRENT_PROCESS, thread_data.tid);
   1421 
   1422   // Tell the thread to exit its infinite loop.
   1423   android_atomic_acquire_store(0, &thread_data.state);
   1424 
   1425   ASSERT_TRUE(pthread_join(thread, nullptr) == 0);
   1426 }
   1427 
   1428 TEST(libbacktrace, check_for_leak_remote) {
   1429   pid_t pid;
   1430 
   1431   if ((pid = fork()) == 0) {
   1432     while (true) {
   1433     }
   1434     _exit(0);
   1435   }
   1436   ASSERT_LT(0, pid);
   1437 
   1438   ASSERT_TRUE(ptrace(PTRACE_ATTACH, pid, 0, 0) == 0);
   1439 
   1440   // Wait for the process to get to a stopping point.
   1441   WaitForStop(pid);
   1442 
   1443   CheckForLeak(pid, BACKTRACE_CURRENT_THREAD);
   1444 
   1445   ASSERT_TRUE(ptrace(PTRACE_DETACH, pid, 0, 0) == 0);
   1446 
   1447   kill(pid, SIGKILL);
   1448   ASSERT_EQ(waitpid(pid, nullptr, 0), pid);
   1449 }
   1450 #endif
   1451 
   1452