Allow heap tags in libmemunreachable

Fixes libmemunreachable to allow for heap tagging. We now explicitly
untag any pointers returned by malloc_iterate. Similarly - when
traversing memory word-by-word to look for known pointers, we
strip the pointer tag.

We may have mixed tagged and untagged heap pointers if the zygote
disables heap tagging due to compat reasons. This change should allow us
to gracefully handle this case as well.

Test: memunreachable_test memunreachable_unit_test
Bug: 135754954
Change-Id: I817d67aa16377f0f4c50c72f4b67f87978cbd25a
diff --git a/HeapWalker.cpp b/HeapWalker.cpp
index 7cae048..28ab13e 100644
--- a/HeapWalker.cpp
+++ b/HeapWalker.cpp
@@ -29,11 +29,20 @@
 #include "log.h"
 
 namespace android {
+static inline uintptr_t UntagAddress(uintptr_t addr) {
+#if defined(__aarch64__)
+  constexpr uintptr_t mask = (static_cast<uintptr_t>(1) << 56) - 1;
+  addr = addr & mask;
+#endif
+  return addr;
+}
 
 bool HeapWalker::Allocation(uintptr_t begin, uintptr_t end) {
   if (end == begin) {
     end = begin + 1;
   }
+  begin = UntagAddress(begin);
+  end = UntagAddress(end);
   Range range{begin, end};
   if (valid_mappings_range_.end != 0 &&
       (begin < valid_mappings_range_.begin || end > valid_mappings_range_.end)) {
@@ -72,6 +81,7 @@
   // for example mprotect(PROT_NONE) on a native heap page.  If so, it will be
   // caught and handled by mmaping a zero page over the faulting page.
   uintptr_t value = ReadWordAtAddressUnsafe(word_ptr);
+  value = UntagAddress(value);
   walking_ptr_ = 0;
   if (value >= valid_allocations_range_.begin && value < valid_allocations_range_.end) {
     AllocationMap::iterator it = allocations_.find(Range{value, value + 1});