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
diff --git a/libbacktrace/UnwindStack.cpp b/libbacktrace/UnwindStack.cpp
index 61812ab..b4481cc 100644
--- a/libbacktrace/UnwindStack.cpp
+++ b/libbacktrace/UnwindStack.cpp
@@ -68,6 +68,32 @@
   return library == "libunwindstack.so" || library == "libbacktrace.so";
 }
 
+static void SetFrameInfo(unwindstack::Regs* regs, unwindstack::MapInfo* map_info,
+                         uint64_t adjusted_rel_pc, backtrace_frame_data_t* frame) {
+  // This will point to the adjusted absolute pc. regs->pc() is
+  // unaltered.
+  frame->pc = map_info->start + adjusted_rel_pc;
+  frame->sp = regs->sp();
+  frame->rel_pc = adjusted_rel_pc;
+  frame->stack_size = 0;
+
+  frame->map.start = map_info->start;
+  frame->map.end = map_info->end;
+  frame->map.offset = map_info->offset;
+  frame->map.flags = map_info->flags;
+  frame->map.name = map_info->name;
+
+  unwindstack::Elf* elf = map_info->elf;
+  frame->map.load_bias = elf->GetLoadBias();
+  uint64_t func_offset = 0;
+  if (elf->GetFunctionName(adjusted_rel_pc, &frame->func_name, &func_offset)) {
+    frame->func_name = demangle(frame->func_name.c_str());
+  } else {
+    frame->func_name = "";
+  }
+  frame->func_offset = func_offset;
+}
+
 static bool Unwind(unwindstack::Regs* regs, BacktraceMap* back_map,
                    std::vector<backtrace_frame_data_t>* frames, size_t num_ignore_frames) {
   UnwindStackMap* stack_map = reinterpret_cast<UnwindStackMap*>(back_map);
@@ -75,70 +101,96 @@
   bool adjust_rel_pc = false;
   size_t num_frames = 0;
   frames->clear();
+  bool return_address_attempted = false;
+  auto process_memory = stack_map->process_memory();
   while (num_frames < MAX_BACKTRACE_FRAMES) {
-    if (regs->pc() == 0) {
-      break;
-    }
     unwindstack::MapInfo* map_info = maps->Find(regs->pc());
+    bool stepped;
+    bool in_device_map = false;
     if (map_info == nullptr) {
-      break;
-    }
-
-    unwindstack::Elf* elf = map_info->GetElf(stack_map->process_memory(), true);
-    uint64_t rel_pc = elf->GetRelPc(regs->pc(), map_info);
-
-    bool skip_frame = num_frames == 0 && IsUnwindLibrary(map_info->name);
-    if (num_ignore_frames == 0 && !skip_frame) {
-      uint64_t adjusted_rel_pc = rel_pc;
-      if (adjust_rel_pc) {
-        adjusted_rel_pc = regs->GetAdjustedPc(rel_pc, elf);
-      }
-      frames->resize(num_frames + 1);
-      backtrace_frame_data_t* frame = &frames->at(num_frames);
-      frame->num = num_frames;
-      // This will point to the adjusted absolute pc. regs->pc() is
-      // unaltered.
-      frame->pc = map_info->start + adjusted_rel_pc;
-      frame->sp = regs->sp();
-      frame->rel_pc = adjusted_rel_pc;
-      frame->stack_size = 0;
-
-      frame->map.start = map_info->start;
-      frame->map.end = map_info->end;
-      frame->map.offset = map_info->offset;
-      frame->map.load_bias = elf->GetLoadBias();
-      frame->map.flags = map_info->flags;
-      frame->map.name = map_info->name;
-
-      uint64_t func_offset = 0;
-      if (elf->GetFunctionName(adjusted_rel_pc, &frame->func_name, &func_offset)) {
-        frame->func_name = demangle(frame->func_name.c_str());
+      stepped = false;
+      if (num_ignore_frames == 0) {
+        frames->resize(num_frames + 1);
+        backtrace_frame_data_t* frame = &frames->at(num_frames);
+        frame->pc = regs->pc();
+        frame->sp = regs->sp();
+        frame->rel_pc = frame->pc;
+        num_frames++;
       } else {
-        frame->func_name = "";
+        num_ignore_frames--;
       }
-      frame->func_offset = func_offset;
-      if (num_frames > 0) {
-        // Set the stack size for the previous frame.
-        backtrace_frame_data_t* prev = &frames->at(num_frames - 1);
-        prev->stack_size = frame->sp - prev->sp;
+    } else {
+      unwindstack::Elf* elf = map_info->GetElf(process_memory, true);
+      uint64_t rel_pc = elf->GetRelPc(regs->pc(), map_info);
+
+      if (frames->size() != 0 || !IsUnwindLibrary(map_info->name)) {
+        if (num_ignore_frames == 0) {
+          uint64_t adjusted_rel_pc = rel_pc;
+          if (adjust_rel_pc) {
+            adjusted_rel_pc = regs->GetAdjustedPc(rel_pc, elf);
+          }
+
+          frames->resize(num_frames + 1);
+          backtrace_frame_data_t* frame = &frames->at(num_frames);
+          frame->num = num_frames;
+          SetFrameInfo(regs, map_info, adjusted_rel_pc, frame);
+
+          if (num_frames > 0) {
+            // Set the stack size for the previous frame.
+            backtrace_frame_data_t* prev = &frames->at(num_frames - 1);
+            prev->stack_size = frame->sp - prev->sp;
+          }
+          num_frames++;
+        } else {
+          num_ignore_frames--;
+        }
       }
-      num_frames++;
-    } else if (!skip_frame && num_ignore_frames > 0) {
-      num_ignore_frames--;
+
+      if (map_info->flags & PROT_DEVICE_MAP) {
+        // Do not stop here, fall through in case we are
+        // in the speculative unwind path and need to remove
+        // some of the speculative frames.
+        stepped = false;
+        in_device_map = true;
+      } else {
+        unwindstack::MapInfo* sp_info = maps->Find(regs->sp());
+        if (sp_info->flags & PROT_DEVICE_MAP) {
+          // Do not stop here, fall through in case we are
+          // in the speculative unwind path and need to remove
+          // some of the speculative frames.
+          stepped = false;
+          in_device_map = true;
+        } else {
+          bool finished;
+          stepped = elf->Step(rel_pc + map_info->elf_offset, regs, process_memory.get(), &finished);
+          if (stepped && finished) {
+            break;
+          }
+        }
+      }
     }
     adjust_rel_pc = true;
 
-    // Do not unwind through a device map.
-    if (map_info->flags & PROT_DEVICE_MAP) {
-      break;
-    }
-    unwindstack::MapInfo* sp_info = maps->Find(regs->sp());
-    if (sp_info->flags & PROT_DEVICE_MAP) {
-      break;
-    }
-
-    if (!elf->Step(rel_pc + map_info->elf_offset, regs, stack_map->process_memory().get())) {
-      break;
+    if (!stepped) {
+      if (return_address_attempted) {
+        // Remove the speculative frame.
+        if (frames->size() > 0) {
+          frames->pop_back();
+        }
+        break;
+      } else if (in_device_map) {
+        // Do not attempt any other unwinding, pc or sp is in a device
+        // map.
+        break;
+      } else {
+        // Stepping didn't work, try this secondary method.
+        if (!regs->SetPcFromReturnAddress(process_memory.get())) {
+          break;
+        }
+        return_address_attempted = true;
+      }
+    } else {
+      return_address_attempted = false;
     }
   }