Move map data into backtrace data proper.

The backtrace structure used to include a pointer to a backtrace_map_t
that represented the map data for a particular pc. This introduced a
race condition where the pointer could be discarded, but the backtrace
structure still contained a pointer to garbage memory. Now all of the map
information is right in the structure.

Bug: 19028453
Change-Id: If7088a73f3c6bf1f3bc8cdd2bb4b62e7cab831c0
diff --git a/include/backtrace/Backtrace.h b/include/backtrace/Backtrace.h
index e07d322..e2d718b 100644
--- a/include/backtrace/Backtrace.h
+++ b/include/backtrace/Backtrace.h
@@ -39,7 +39,7 @@
   uintptr_t pc;           // The absolute pc.
   uintptr_t sp;           // The top of the stack.
   size_t stack_size;      // The size of the stack, zero indicate an unknown stack size.
-  const backtrace_map_t* map;   // The map associated with the given pc.
+  backtrace_map_t map;    // The map associated with the given pc.
   std::string func_name;  // The function name associated with this pc, NULL if not found.
   uintptr_t func_offset;  // pc relative to the start of the function, only valid if func_name is not NULL.
 };
@@ -78,8 +78,8 @@
   // If the string is empty, then no valid function name was found.
   virtual std::string GetFunctionName(uintptr_t pc, uintptr_t* offset);
 
-  // Find the map associated with the given pc.
-  virtual const backtrace_map_t* FindMap(uintptr_t pc);
+  // Fill in the map data associated with the given pc.
+  virtual void FillInMap(uintptr_t pc, backtrace_map_t* map);
 
   // Read the data at a specific address.
   virtual bool ReadWord(uintptr_t ptr, word_t* out_value) = 0;
diff --git a/include/backtrace/BacktraceMap.h b/include/backtrace/BacktraceMap.h
index 4ed23a8..da96307 100644
--- a/include/backtrace/BacktraceMap.h
+++ b/include/backtrace/BacktraceMap.h
@@ -33,6 +33,8 @@
 #include <string>
 
 struct backtrace_map_t {
+  backtrace_map_t(): start(0), end(0), flags(0) {}
+
   uintptr_t start;
   uintptr_t end;
   int flags;
@@ -48,15 +50,16 @@
 
   virtual ~BacktraceMap();
 
-  // Get the map data structure for the given address.
-  virtual const backtrace_map_t* Find(uintptr_t addr);
+  // Fill in the map data structure for the given address.
+  virtual void FillIn(uintptr_t addr, backtrace_map_t* map);
 
   // The flags returned are the same flags as used by the mmap call.
   // The values are PROT_*.
   int GetFlags(uintptr_t pc) {
-    const backtrace_map_t* map = Find(pc);
-    if (map) {
-      return map->flags;
+    backtrace_map_t map;
+    FillIn(pc, &map);
+    if (IsValid(map)) {
+      return map.flags;
     }
     return PROT_NONE;
   }
@@ -75,6 +78,10 @@
 
   virtual bool Build();
 
+  static inline bool IsValid(const backtrace_map_t& map) {
+    return map.end > 0;
+  }
+
 protected:
   BacktraceMap(pid_t pid);