Revert "Use libdexfile external API in libunwindstack."

This reverts commit 444e23d2fc6d7c6f799ff9e2f69c1a82d2960c5b.

The rest of the topic doesn't need to be reverted.

Reason for revert: Breaks renderscript on marlin and sailfish.

Test: Manual repro of http://b/121110092#comment1 on reported branch
Test: "atest CtsRenderscriptTestCases" on that branch
Test: mmma system/core/{libunwindstack,libbacktrace}, run host gtests
Test: Make image, flash, and reboot device.
Bug: 121110092, 119632407
Change-Id: If1976b19ce386c95bc5bd4fd6d523745c167de18
diff --git a/libunwindstack/DexFile.cpp b/libunwindstack/DexFile.cpp
index 9b0b232..8ec560c 100644
--- a/libunwindstack/DexFile.cpp
+++ b/libunwindstack/DexFile.cpp
@@ -23,7 +23,13 @@
 #include <memory>
 
 #include <android-base/unique_fd.h>
-#include <art_api/ext_dex_file.h>
+
+#include <dex/class_accessor-inl.h>
+#include <dex/code_item_accessors-inl.h>
+#include <dex/compact_dex_file.h>
+#include <dex/dex_file-inl.h>
+#include <dex/dex_file_loader.h>
+#include <dex/standard_dex_file.h>
 
 #include <unwindstack/MapInfo.h>
 #include <unwindstack/Memory.h>
@@ -32,71 +38,169 @@
 
 namespace unwindstack {
 
-std::unique_ptr<DexFile> DexFile::Create(uint64_t dex_file_offset_in_memory, Memory* memory,
-                                         MapInfo* info) {
+DexFile* DexFile::Create(uint64_t dex_file_offset_in_memory, Memory* memory, MapInfo* info) {
   if (!info->name.empty()) {
-    std::unique_ptr<DexFile> dex_file =
-        DexFileFromFile::Create(dex_file_offset_in_memory - info->start + info->offset, info->name);
-    if (dex_file) {
-      return dex_file;
+    std::unique_ptr<DexFileFromFile> dex_file(new DexFileFromFile);
+    if (dex_file->Open(dex_file_offset_in_memory - info->start + info->offset, info->name)) {
+      return dex_file.release();
     }
   }
-  return DexFileFromMemory::Create(dex_file_offset_in_memory, memory, info->name);
+
+  std::unique_ptr<DexFileFromMemory> dex_file(new DexFileFromMemory);
+  if (dex_file->Open(dex_file_offset_in_memory, memory)) {
+    return dex_file.release();
+  }
+  return nullptr;
+}
+
+DexFileFromFile::~DexFileFromFile() {
+  if (size_ != 0) {
+    munmap(mapped_memory_, size_);
+  }
 }
 
 bool DexFile::GetMethodInformation(uint64_t dex_offset, std::string* method_name,
                                    uint64_t* method_offset) {
-  art_api::dex::MethodInfo method_info = GetMethodInfoForOffset(dex_offset);
-  if (method_info.offset == 0) {
+  if (dex_file_ == nullptr) {
     return false;
   }
-  *method_name = method_info.name;
-  *method_offset = dex_offset - method_info.offset;
-  return true;
+
+  if (!dex_file_->IsInDataSection(dex_file_->Begin() + dex_offset)) {
+    return false;  // The DEX offset is not within the bytecode of this dex file.
+  }
+
+  if (dex_file_->IsCompactDexFile()) {
+    // The data section of compact dex files might be shared.
+    // Check the subrange unique to this compact dex.
+    const auto& cdex_header = dex_file_->AsCompactDexFile()->GetHeader();
+    uint32_t begin = cdex_header.data_off_ + cdex_header.OwnedDataBegin();
+    uint32_t end = cdex_header.data_off_ + cdex_header.OwnedDataEnd();
+    if (dex_offset < begin || dex_offset >= end) {
+      return false;  // The DEX offset is not within the bytecode of this dex file.
+    }
+  }
+
+  // The method data is cached in a std::map indexed by method end offset and
+  // contains the start offset and the method member index.
+  // Only cache the method data as it is searched. Do not read the entire
+  // set of method data into the cache at once.
+  // This is done because many unwinds only find a single frame with dex file
+  // info, so reading the entire method data is wasteful. However, still cache
+  // the data so that anything doing multiple unwinds will have this data
+  // cached for future use.
+
+  // First look in the method cache.
+  auto entry = method_cache_.upper_bound(dex_offset);
+  if (entry != method_cache_.end() && dex_offset >= entry->second.first) {
+    *method_name = dex_file_->PrettyMethod(entry->second.second, false);
+    *method_offset = dex_offset - entry->second.first;
+    return true;
+  }
+
+  // Check the methods we haven't cached.
+  for (; class_def_index_ < dex_file_->NumClassDefs(); class_def_index_++) {
+    art::ClassAccessor accessor(*dex_file_, dex_file_->GetClassDef(class_def_index_));
+
+    for (const art::ClassAccessor::Method& method : accessor.GetMethods()) {
+      art::CodeItemInstructionAccessor code = method.GetInstructions();
+      if (!code.HasCodeItem()) {
+        continue;
+      }
+      uint32_t offset = reinterpret_cast<const uint8_t*>(code.Insns()) - dex_file_->Begin();
+      uint32_t offset_end = offset + code.InsnsSizeInBytes();
+      uint32_t member_index = method.GetIndex();
+      method_cache_[offset_end] = std::make_pair(offset, member_index);
+      if (offset <= dex_offset && dex_offset < offset_end) {
+        *method_name = dex_file_->PrettyMethod(member_index, false);
+        *method_offset = dex_offset - offset;
+        return true;
+      }
+    }
+  }
+  return false;
 }
 
-std::unique_ptr<DexFileFromFile> DexFileFromFile::Create(uint64_t dex_file_offset_in_file,
-                                                         const std::string& file) {
+bool DexFileFromFile::Open(uint64_t dex_file_offset_in_file, const std::string& file) {
   android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(file.c_str(), O_RDONLY | O_CLOEXEC)));
   if (fd == -1) {
-    return nullptr;
+    return false;
+  }
+  struct stat buf;
+  if (fstat(fd, &buf) == -1) {
+    return false;
+  }
+  uint64_t length;
+  if (buf.st_size < 0 ||
+      __builtin_add_overflow(dex_file_offset_in_file, sizeof(art::DexFile::Header), &length) ||
+      static_cast<uint64_t>(buf.st_size) < length) {
+    return false;
   }
 
+  mapped_memory_ = mmap(nullptr, buf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
+  if (mapped_memory_ == MAP_FAILED) {
+    return false;
+  }
+  size_ = buf.st_size;
+
+  uint8_t* memory = reinterpret_cast<uint8_t*>(mapped_memory_);
+
+  art::DexFile::Header* header =
+      reinterpret_cast<art::DexFile::Header*>(&memory[dex_file_offset_in_file]);
+  if (!art::StandardDexFile::IsMagicValid(header->magic_) &&
+      !art::CompactDexFile::IsMagicValid(header->magic_)) {
+    return false;
+  }
+
+  if (__builtin_add_overflow(dex_file_offset_in_file, header->file_size_, &length) ||
+      static_cast<uint64_t>(buf.st_size) < length) {
+    return false;
+  }
+
+  art::DexFileLoader loader;
   std::string error_msg;
-  std::unique_ptr<art_api::dex::DexFile> art_dex_file =
-      OpenFromFd(fd, dex_file_offset_in_file, file, &error_msg);
-  if (art_dex_file == nullptr) {
-    return nullptr;
-  }
-
-  return std::unique_ptr<DexFileFromFile>(new DexFileFromFile(std::move(*art_dex_file.release())));
+  auto dex = loader.Open(&memory[dex_file_offset_in_file], header->file_size_, "", 0, nullptr,
+                         false, false, &error_msg);
+  dex_file_.reset(dex.release());
+  return dex_file_ != nullptr;
 }
 
-std::unique_ptr<DexFileFromMemory> DexFileFromMemory::Create(uint64_t dex_file_offset_in_memory,
-                                                             Memory* memory,
-                                                             const std::string& name) {
-  std::vector<uint8_t> backing_memory;
-
-  for (size_t size = 0;;) {
-    std::string error_msg;
-    std::unique_ptr<art_api::dex::DexFile> art_dex_file =
-        OpenFromMemory(backing_memory.data(), &size, name, &error_msg);
-
-    if (art_dex_file != nullptr) {
-      return std::unique_ptr<DexFileFromMemory>(
-          new DexFileFromMemory(std::move(*art_dex_file.release()), std::move(backing_memory)));
-    }
-
-    if (!error_msg.empty()) {
-      return nullptr;
-    }
-
-    backing_memory.resize(size);
-    if (!memory->ReadFully(dex_file_offset_in_memory, backing_memory.data(),
-                           backing_memory.size())) {
-      return nullptr;
-    }
+bool DexFileFromMemory::Open(uint64_t dex_file_offset_in_memory, Memory* memory) {
+  memory_.resize(sizeof(art::DexFile::Header));
+  if (!memory->ReadFully(dex_file_offset_in_memory, memory_.data(), memory_.size())) {
+    return false;
   }
+
+  art::DexFile::Header* header = reinterpret_cast<art::DexFile::Header*>(memory_.data());
+  uint32_t file_size = header->file_size_;
+  if (art::CompactDexFile::IsMagicValid(header->magic_)) {
+    // Compact dex file store data section separately so that it can be shared.
+    // Therefore we need to extend the read memory range to include it.
+    // TODO: This might be wasteful as we might read data in between as well.
+    //       In practice, this should be fine, as such sharing only happens on disk.
+    uint32_t computed_file_size;
+    if (__builtin_add_overflow(header->data_off_, header->data_size_, &computed_file_size)) {
+      return false;
+    }
+    if (computed_file_size > file_size) {
+      file_size = computed_file_size;
+    }
+  } else if (!art::StandardDexFile::IsMagicValid(header->magic_)) {
+    return false;
+  }
+
+  memory_.resize(file_size);
+  if (!memory->ReadFully(dex_file_offset_in_memory, memory_.data(), memory_.size())) {
+    return false;
+  }
+
+  header = reinterpret_cast<art::DexFile::Header*>(memory_.data());
+
+  art::DexFileLoader loader;
+  std::string error_msg;
+  auto dex =
+      loader.Open(memory_.data(), header->file_size_, "", 0, nullptr, false, false, &error_msg);
+  dex_file_.reset(dex.release());
+  return dex_file_ != nullptr;
 }
 
 }  // namespace unwindstack