summaryrefslogtreecommitdiffstats
path: root/libbacktrace/backtrace_test.cpp
diff options
context:
space:
mode:
authorChristopher Ferris <cferris@google.com>2017-09-20 13:37:24 -0700
committerChristopher Ferris <cferris@google.com>2017-09-22 16:55:12 -0700
commitb9de87f7edefd7a2473134b267716c5fd750e89f (patch)
treeb172908cd472d59887c35a040b61c7fe6d61196a /libbacktrace/backtrace_test.cpp
parent0bb8dcb23ea63995fa7afe84be58c6e9407585a8 (diff)
downloadsystem_core-b9de87f7edefd7a2473134b267716c5fd750e89f.tar.gz
system_core-b9de87f7edefd7a2473134b267716c5fd750e89f.tar.bz2
system_core-b9de87f7edefd7a2473134b267716c5fd750e89f.zip
Add a new unwind method on error.
If a function crashes by jumping into unexecutable code, the old method could not unwind through that. Add a fallback method to set the pc from the default return address location. In addition, add a new finished check for steps. This will provide a method to indicate that this step is the last step. This prevents cases where the fallback method might be triggered incorrectly. Update the libbacktrace code to unwind using the new methodology. Update the unwind tool to use the new unwind methodology. Add a new option to crasher that calls through a null function. Create a new object, Unwinder, that encapsulates the a basic unwind. For now, libbacktrace will still use the custom code. Added new unit tests to cover the new cases. Also add a test that crashes calling a nullptr as a function, and then has call frames in the signal stack. Bug: 65842173 Test: Pass all unit tests, verify crasher dumps properly. Change-Id: Ia18430ab107e9f7bdf0e14a9b74710b1280bd7f4
Diffstat (limited to 'libbacktrace/backtrace_test.cpp')
-rw-r--r--libbacktrace/backtrace_test.cpp56
1 files changed, 34 insertions, 22 deletions
diff --git a/libbacktrace/backtrace_test.cpp b/libbacktrace/backtrace_test.cpp
index 9fe2d1cb5..e5eb9e34e 100644
--- a/libbacktrace/backtrace_test.cpp
+++ b/libbacktrace/backtrace_test.cpp
@@ -82,6 +82,14 @@ struct dump_thread_t {
int32_t done;
};
+typedef Backtrace* (*create_func_t)(pid_t, pid_t, BacktraceMap*);
+typedef BacktraceMap* (*map_create_func_t)(pid_t, bool);
+
+static void VerifyLevelDump(Backtrace* backtrace, create_func_t create_func = nullptr,
+ map_create_func_t map_func = nullptr);
+static void VerifyMaxDump(Backtrace* backtrace, create_func_t create_func = nullptr,
+ map_create_func_t map_func = nullptr);
+
static uint64_t NanoTime() {
struct timespec t = { 0, 0 };
clock_gettime(CLOCK_MONOTONIC, &t);
@@ -147,7 +155,7 @@ static bool ReadyLevelBacktrace(Backtrace* backtrace) {
return found;
}
-static void VerifyLevelDump(Backtrace* backtrace) {
+static void VerifyLevelDump(Backtrace* backtrace, create_func_t, map_create_func_t) {
ASSERT_GT(backtrace->NumFrames(), static_cast<size_t>(0))
<< DumpFrames(backtrace);
ASSERT_LT(backtrace->NumFrames(), static_cast<size_t>(MAX_BACKTRACE_FRAMES))
@@ -189,7 +197,7 @@ static bool ReadyMaxBacktrace(Backtrace* backtrace) {
return (backtrace->NumFrames() == MAX_BACKTRACE_FRAMES);
}
-static void VerifyMaxDump(Backtrace* backtrace) {
+static void VerifyMaxDump(Backtrace* backtrace, create_func_t, map_create_func_t) {
ASSERT_EQ(backtrace->NumFrames(), static_cast<size_t>(MAX_BACKTRACE_FRAMES))
<< DumpFrames(backtrace);
// Verify that the last frame is our recursive call.
@@ -251,10 +259,14 @@ TEST(libbacktrace, local_trace) {
static void VerifyIgnoreFrames(Backtrace* bt_all, Backtrace* bt_ign1, Backtrace* bt_ign2,
const char* cur_proc) {
- EXPECT_EQ(bt_all->NumFrames(), bt_ign1->NumFrames() + 1)
- << "All backtrace:\n" << DumpFrames(bt_all) << "Ignore 1 backtrace:\n" << DumpFrames(bt_ign1);
- EXPECT_EQ(bt_all->NumFrames(), bt_ign2->NumFrames() + 2)
- << "All backtrace:\n" << DumpFrames(bt_all) << "Ignore 2 backtrace:\n" << DumpFrames(bt_ign2);
+ ASSERT_EQ(bt_all->NumFrames(), bt_ign1->NumFrames() + 1) << "All backtrace:\n"
+ << DumpFrames(bt_all)
+ << "Ignore 1 backtrace:\n"
+ << DumpFrames(bt_ign1);
+ ASSERT_EQ(bt_all->NumFrames(), bt_ign2->NumFrames() + 2) << "All backtrace:\n"
+ << DumpFrames(bt_all)
+ << "Ignore 2 backtrace:\n"
+ << DumpFrames(bt_ign2);
// Check all of the frames are the same > the current frame.
bool check = (cur_proc == nullptr);
@@ -305,9 +317,8 @@ TEST(libbacktrace, local_max_trace) {
}
static void VerifyProcTest(pid_t pid, pid_t tid, bool (*ReadyFunc)(Backtrace*),
- void (*VerifyFunc)(Backtrace*),
- Backtrace* (*back_func)(pid_t, pid_t, BacktraceMap*),
- BacktraceMap* (*map_func)(pid_t, bool)) {
+ void (*VerifyFunc)(Backtrace*, create_func_t, map_create_func_t),
+ create_func_t create_func, map_create_func_t map_create_func) {
pid_t ptrace_tid;
if (tid < 0) {
ptrace_tid = pid;
@@ -324,13 +335,13 @@ static void VerifyProcTest(pid_t pid, pid_t tid, bool (*ReadyFunc)(Backtrace*),
WaitForStop(ptrace_tid);
std::unique_ptr<BacktraceMap> map;
- map.reset(map_func(pid, false));
- std::unique_ptr<Backtrace> backtrace(back_func(pid, tid, map.get()));
+ map.reset(map_create_func(pid, false));
+ std::unique_ptr<Backtrace> backtrace(create_func(pid, tid, map.get()));
ASSERT_TRUE(backtrace.get() != nullptr);
ASSERT_TRUE(backtrace->Unwind(0));
ASSERT_EQ(BACKTRACE_UNWIND_NO_ERROR, backtrace->GetError());
if (ReadyFunc(backtrace.get())) {
- VerifyFunc(backtrace.get());
+ VerifyFunc(backtrace.get(), create_func, map_create_func);
verified = true;
} else {
last_dump = DumpFrames(backtrace.get());
@@ -399,13 +410,15 @@ TEST(libbacktrace, ptrace_max_trace_new) {
ASSERT_EQ(waitpid(pid, &status, 0), pid);
}
-static void VerifyProcessIgnoreFrames(Backtrace* bt_all) {
- std::unique_ptr<Backtrace> ign1(Backtrace::Create(bt_all->Pid(), BACKTRACE_CURRENT_THREAD));
+static void VerifyProcessIgnoreFrames(Backtrace* bt_all, create_func_t create_func,
+ map_create_func_t map_create_func) {
+ std::unique_ptr<BacktraceMap> map(map_create_func(bt_all->Pid(), false));
+ std::unique_ptr<Backtrace> ign1(create_func(bt_all->Pid(), BACKTRACE_CURRENT_THREAD, map.get()));
ASSERT_TRUE(ign1.get() != nullptr);
ASSERT_TRUE(ign1->Unwind(1));
ASSERT_EQ(BACKTRACE_UNWIND_NO_ERROR, ign1->GetError());
- std::unique_ptr<Backtrace> ign2(Backtrace::Create(bt_all->Pid(), BACKTRACE_CURRENT_THREAD));
+ std::unique_ptr<Backtrace> ign2(create_func(bt_all->Pid(), BACKTRACE_CURRENT_THREAD, map.get()));
ASSERT_TRUE(ign2.get() != nullptr);
ASSERT_TRUE(ign2->Unwind(2));
ASSERT_EQ(BACKTRACE_UNWIND_NO_ERROR, ign2->GetError());
@@ -1702,9 +1715,8 @@ static void SetValueAndLoop(void* data) {
;
}
-static void UnwindThroughSignal(bool use_action,
- Backtrace* (*back_func)(pid_t, pid_t, BacktraceMap*),
- BacktraceMap* (*map_func)(pid_t, bool)) {
+static void UnwindThroughSignal(bool use_action, create_func_t create_func,
+ map_create_func_t map_create_func) {
volatile int value = 0;
pid_t pid;
if ((pid = fork()) == 0) {
@@ -1730,8 +1742,8 @@ static void UnwindThroughSignal(bool use_action,
WaitForStop(pid);
- std::unique_ptr<BacktraceMap> map(map_func(pid, false));
- std::unique_ptr<Backtrace> backtrace(back_func(pid, pid, map.get()));
+ std::unique_ptr<BacktraceMap> map(map_create_func(pid, false));
+ std::unique_ptr<Backtrace> backtrace(create_func(pid, pid, map.get()));
size_t bytes_read = backtrace->Read(reinterpret_cast<uintptr_t>(const_cast<int*>(&value)),
reinterpret_cast<uint8_t*>(&read_value), sizeof(read_value));
@@ -1758,9 +1770,9 @@ static void UnwindThroughSignal(bool use_action,
WaitForStop(pid);
- map.reset(map_func(pid, false));
+ map.reset(map_create_func(pid, false));
ASSERT_TRUE(map.get() != nullptr);
- backtrace.reset(back_func(pid, pid, map.get()));
+ backtrace.reset(create_func(pid, pid, map.get()));
ASSERT_TRUE(backtrace->Unwind(0));
bool found = false;
for (frame_iter = backtrace->begin(); frame_iter != backtrace->end(); ++frame_iter) {