Improve stack unwinder robustness.

Keep track of whether memory maps are readable.  Use the information
in try_get_word to try to avoid accidentally dereferencing an invalid
pointer within the current process.  (Note that I haven't ever
seen that happen during normal unwinding, but it pays to be
a little more careful.)

Refactored try_get_word a little to make it easier to pass it the
needed state for validation checks by way of a little memory_t struct.

Improved how the memory map for the current process is cached.  This is
important because we need up to date information about readable maps.
Use a 5 second cache expiration.

Improved the PC -> LR fallback logic in the unwinder so we can
eke out an extra frame sometimes.

Fixed a bug reading ELF program headers.  The phnum & phentsize
fields are half-words.  We were incorrectly interpreting
phnum as a whole word.

Used android_atomic_* operations carefully in the unwinder
to prevent possible memory races between the dumper and the dumpee.
This was highly unlikely (or even impossible due to the presence
of other barriers along the way) but the code is clearer now about
its invariants.

Fixed a bug in debuggerd where the pid was being passed to have
its stack dump taken instead of the tid, resulting in short
stacks because ptrace couldn't read the data if pid != tid.
Did a full sweep to ensure that we use pid / tid correctly everywhere.

Ported old code from debuggerd to rewind the program counter back
one instruction so that it points to the branch instruction itself
instead of the return address.

Change-Id: Icc4eb08320052975a4ae7f0f5f0ac9308a2d33d7
diff --git a/libcorkscrew/ptrace.c b/libcorkscrew/ptrace.c
index a308bb5..cbea8ca 100644
--- a/libcorkscrew/ptrace.c
+++ b/libcorkscrew/ptrace.c
@@ -34,44 +34,55 @@
 #define PAGE_MASK (~(PAGE_SIZE - 1))
 #endif
 
-bool try_get_word(pid_t tid, uintptr_t ptr, uint32_t* out_value) {
+void init_memory(memory_t* memory, const map_info_t* map_info_list) {
+    memory->tid = -1;
+    memory->map_info_list = map_info_list;
+}
+
+void init_memory_ptrace(memory_t* memory, pid_t tid) {
+    memory->tid = tid;
+    memory->map_info_list = NULL;
+}
+
+bool try_get_word(const memory_t* memory, uintptr_t ptr, uint32_t* out_value) {
+    ALOGV("try_get_word: reading word at 0x%08x", ptr);
     if (ptr & 3) {
         ALOGV("try_get_word: invalid pointer 0x%08x", ptr);
-        *out_value = 0;
+        *out_value = 0xffffffffL;
         return false;
     }
-    if (tid < 0) {
-#if 0 /*unreliable, unclear whether this is safe from a signal handler context*/
-        // Determine whether the pointer is likely to be valid before dereferencing it.
-        unsigned char vec[1];
-        while (mincore((void*)(ptr & PAGE_MASK), sizeof(uint32_t), vec)) {
-            if (errno != EAGAIN && errno != EINTR) {
-                ALOGV("try_get_word: invalid pointer 0x%08x, mincore() errno=%d", ptr, errno);
-                *out_value = 0;
-                return false;
-            }
+    if (memory->tid < 0) {
+        if (!is_readable_map(memory->map_info_list, ptr)) {
+            ALOGV("try_get_word: pointer 0x%08x not in a readable map", ptr);
+            *out_value = 0xffffffffL;
+            return false;
         }
-#endif
         *out_value = *(uint32_t*)ptr;
         return true;
     } else {
         // ptrace() returns -1 and sets errno when the operation fails.
         // To disambiguate -1 from a valid result, we clear errno beforehand.
         errno = 0;
-        *out_value = ptrace(PTRACE_PEEKTEXT, tid, (void*)ptr, NULL);
+        *out_value = ptrace(PTRACE_PEEKTEXT, memory->tid, (void*)ptr, NULL);
         if (*out_value == 0xffffffffL && errno) {
-            ALOGV("try_get_word: invalid pointer 0x%08x, ptrace() errno=%d", ptr, errno);
-            *out_value = 0;
+            ALOGV("try_get_word: invalid pointer 0x%08x reading from tid %d, "
+                    "ptrace() errno=%d", ptr, memory->tid, errno);
             return false;
         }
         return true;
     }
 }
 
+bool try_get_word_ptrace(pid_t tid, uintptr_t ptr, uint32_t* out_value) {
+    memory_t memory;
+    init_memory_ptrace(&memory, tid);
+    return try_get_word(&memory, ptr, out_value);
+}
+
 static void load_ptrace_map_info_data(pid_t pid, map_info_t* mi) {
-    if (mi->is_executable) {
+    if (mi->is_executable && mi->is_readable) {
         uint32_t elf_magic;
-        if (try_get_word(pid, mi->start, &elf_magic) && elf_magic == ELF_MAGIC) {
+        if (try_get_word_ptrace(pid, mi->start, &elf_magic) && elf_magic == ELF_MAGIC) {
             map_info_data_t* data = (map_info_data_t*)calloc(1, sizeof(map_info_data_t));
             if (data) {
                 mi->data = data;