Revert "Merge "Use libdexfile external API in simpleperf." am: f8a943e3ee am: 95d21eb5ff"

This reverts commit 4ea8a009e0f24b258d2cd372991ffa14f6682bab.

Reason for revert: Need to Cherry-pick revert to QT-Release Branch

Change-Id: I8b00ec55bff40b6e85ef6bc0fca1cceb887db733
(cherry picked from commit 872e449151845ffe72029b10e63aa688116356ba)
diff --git a/perfprofd/symbolizer.cc b/perfprofd/symbolizer.cc
index 7244539..58ff280 100644
--- a/perfprofd/symbolizer.cc
+++ b/perfprofd/symbolizer.cc
@@ -35,7 +35,7 @@
 struct SimpleperfSymbolizer : public Symbolizer {
   // For simplicity, we assume non-overlapping symbols.
   struct Symbol {
-    Symbol(std::string_view n, uint64_t l) : name(n), length(l) {}
+    Symbol(const std::string& n, uint64_t l) : name(n), length(l) {}
 
     std::string name;
     uint64_t length;
@@ -175,3 +175,4 @@
 }
 
 }  // namespace perfprofd
+
diff --git a/simpleperf/Android.bp b/simpleperf/Android.bp
index c97ba09..759aee5 100644
--- a/simpleperf/Android.bp
+++ b/simpleperf/Android.bp
@@ -98,12 +98,6 @@
 cc_defaults {
     name: "libsimpleperf_dex_read_static_reqs_defaults",
     defaults: ["libdexfile_static_defaults"],
-    static_libs: [
-        "libdexfile_external",
-        "libdexfile_support",
-    ],
-    header_libs: ["libdexfile_external_headers"],
-    export_header_lib_headers: ["libdexfile_external_headers"],
 }
 
 cc_library_static {
diff --git a/simpleperf/Android.mk b/simpleperf/Android.mk
index 8afb4d4..4b82136 100644
--- a/simpleperf/Android.mk
+++ b/simpleperf/Android.mk
@@ -26,8 +26,8 @@
                             -DUSE_BIONIC_UAPI_HEADERS -I bionic/libc/kernel \
                             -fvisibility=hidden \
 
-simpleperf_cflags_host_darwin := -I $(LOCAL_PATH)/nonlinux_support/include -DNO_LIBDEXFILE_SUPPORT
-simpleperf_cflags_host_windows := -I $(LOCAL_PATH)/nonlinux_support/include -DNO_LIBDEXFILE_SUPPORT
+simpleperf_cflags_host_darwin := -I $(LOCAL_PATH)/nonlinux_support/include
+simpleperf_cflags_host_windows := -I $(LOCAL_PATH)/nonlinux_support/include
 
 
 LLVM_ROOT_PATH := external/llvm
@@ -36,8 +36,6 @@
 simpleperf_static_libraries_target := \
   libbacktrace \
   libunwindstack \
-  libdexfile_support \
-  libdexfile_external \
   libdexfile \
   libziparchive \
   libz \
@@ -79,8 +77,6 @@
   libprocinfo \
   libbacktrace \
   libunwindstack \
-  libdexfile_support \
-  libdexfile_external \
   libdexfile \
   libcutils \
   libevent \
diff --git a/simpleperf/dso.cpp b/simpleperf/dso.cpp
index 7fa7283..d2c674b 100644
--- a/simpleperf/dso.cpp
+++ b/simpleperf/dso.cpp
@@ -174,7 +174,7 @@
 
 static OneTimeFreeAllocator symbol_name_allocator;
 
-Symbol::Symbol(std::string_view name, uint64_t addr, uint64_t len)
+Symbol::Symbol(const std::string& name, uint64_t addr, uint64_t len)
     : addr(addr),
       len(len),
       name_(symbol_name_allocator.AllocateString(name)),
diff --git a/simpleperf/dso.h b/simpleperf/dso.h
index 7e7f558..5bf898f 100644
--- a/simpleperf/dso.h
+++ b/simpleperf/dso.h
@@ -59,7 +59,7 @@
   // TODO: make len uint32_t.
   uint64_t len;
 
-  Symbol(std::string_view name, uint64_t addr, uint64_t len);
+  Symbol(const std::string& name, uint64_t addr, uint64_t len);
   const char* Name() const { return name_; }
 
   const char* DemangledName() const;
diff --git a/simpleperf/read_dex_file.cpp b/simpleperf/read_dex_file.cpp
index 78e9654..53180af 100644
--- a/simpleperf/read_dex_file.cpp
+++ b/simpleperf/read_dex_file.cpp
@@ -17,63 +17,68 @@
 #include "read_dex_file.h"
 
 #include <fcntl.h>
+#include <sys/mman.h>
+#include <unistd.h>
 
-#include <algorithm>
-#include <iterator>
+#include <functional>
 #include <string>
-#include <utility>
-#include <vector>
 
+#include <android-base/file.h>
 #include <android-base/logging.h>
 #include <android-base/unique_fd.h>
-#include <art_api/ext_dex_file.h>
 
-static bool ReadSymbols(
-    const std::vector<uint64_t>& dex_file_offsets, std::vector<DexFileSymbol>* symbols,
-    const std::function<std::unique_ptr<art_api::dex::DexFile>(uint64_t offset)>& open_file_cb) {
+#include <dex/class_accessor-inl.h>
+#include <dex/code_item_accessors-inl.h>
+#include <dex/dex_file_loader.h>
+#include <dex/dex_file.h>
+
+static bool OpenDexFiles(void* addr, uint64_t size, std::vector<uint64_t> dex_file_offsets,
+                         const std::function<void (const art::DexFile&, uint64_t)>& callback) {
+  bool result = true;
   for (uint64_t offset : dex_file_offsets) {
-    std::unique_ptr<art_api::dex::DexFile> dex_file = open_file_cb(offset);
-    if (dex_file == nullptr) {
-      return false;
+    if (offset >= size || size - offset < sizeof(art::DexFile::Header)) {
+      result = false;
+      break;
     }
-
-    std::vector<art_api::dex::MethodInfo> file_syms = dex_file->GetAllMethodInfos(false);
-
-    // Adjust offsets to be from the start of the combined file.
-    for (art_api::dex::MethodInfo& sym : file_syms) {
-      sym.offset += offset;
+    auto header = reinterpret_cast<art::DexFile::Header*>(static_cast<char*>(addr) + offset);
+    if (size - offset < header->file_size_) {
+      result = false;
+      break;
     }
-
-    if (symbols->empty()) {
-      *symbols = std::move(file_syms);
-    } else {
-      symbols->reserve(symbols->size() + file_syms.size());
-      std::move(std::begin(file_syms), std::end(file_syms), std::back_inserter(*symbols));
+    art::DexFileLoader loader;
+    std::string error;
+    std::unique_ptr<const art::DexFile> dex_file = loader.Open(reinterpret_cast<uint8_t*>(header),
+                                                               header->file_size_, "", 0, nullptr,
+                                                               false, false, &error);
+    if (!dex_file) {
+      result = false;
+      break;
     }
+    callback(*dex_file, offset);
   }
-
-  return true;
+  return result;
 }
 
 bool ReadSymbolsFromDexFileInMemory(void* addr, uint64_t size,
                                     const std::vector<uint64_t>& dex_file_offsets,
                                     std::vector<DexFileSymbol>* symbols) {
-  return ReadSymbols(
-      dex_file_offsets, symbols, [&](uint64_t offset) -> std::unique_ptr<art_api::dex::DexFile> {
-        size_t max_file_size;
-        if (__builtin_sub_overflow(size, offset, &max_file_size)) {
-          return nullptr;
+  auto dexfile_callback = [&](const art::DexFile& dex_file, uint64_t dex_file_offset) {
+    for (art::ClassAccessor accessor : dex_file.GetClasses()) {
+      for (const art::ClassAccessor::Method& method : accessor.GetMethods()) {
+        art::CodeItemInstructionAccessor code = method.GetInstructions();
+        if (!code.HasCodeItem()) {
+          continue;
         }
-        uint8_t* file_addr = static_cast<uint8_t*>(addr) + offset;
-        std::string error_msg;
-        std::unique_ptr<art_api::dex::DexFile> dex_file =
-            art_api::dex::DexFile::OpenFromMemory(file_addr, &max_file_size, "", &error_msg);
-        if (dex_file == nullptr) {
-          LOG(WARNING) << "Failed to read dex file symbols: " << error_msg;
-          return nullptr;
-        }
-        return dex_file;
-      });
+        symbols->resize(symbols->size() + 1);
+        DexFileSymbol& symbol = symbols->back();
+        symbol.offset = reinterpret_cast<const uint8_t*>(code.Insns()) - dex_file.Begin() +
+            dex_file_offset;
+        symbol.len = code.InsnsSizeInBytes();
+        symbol.name = dex_file.PrettyMethod(method.GetIndex(), false);
+      }
+    }
+  };
+  return OpenDexFiles(addr, size, dex_file_offsets, dexfile_callback);
 }
 
 bool ReadSymbolsFromDexFile(const std::string& file_path,
@@ -83,16 +88,16 @@
   if (fd == -1) {
     return false;
   }
-  return ReadSymbols(
-      dex_file_offsets, symbols, [&](uint64_t offset) -> std::unique_ptr<art_api::dex::DexFile> {
-        std::string error_msg;
-        std::unique_ptr<art_api::dex::DexFile> dex_file =
-            art_api::dex::DexFile::OpenFromFd(fd, offset, file_path, &error_msg);
-        if (dex_file == nullptr) {
-          LOG(WARNING) << "Failed to read dex file symbols from '" << file_path
-                       << "': " << error_msg;
-          return nullptr;
-        }
-        return dex_file;
-      });
+  struct stat buf;
+  if (fstat(fd, &buf) == -1 || buf.st_size < 0) {
+    return false;
+  }
+  uint64_t file_size = buf.st_size;
+  void* addr = mmap(nullptr, file_size, PROT_READ, MAP_PRIVATE, fd, 0);
+  if (addr == MAP_FAILED) {
+    return false;
+  }
+  bool result = ReadSymbolsFromDexFileInMemory(addr, file_size, dex_file_offsets, symbols);
+  munmap(addr, file_size);
+  return result;
 }
diff --git a/simpleperf/read_dex_file.h b/simpleperf/read_dex_file.h
index 5d54f65..2efcde1 100644
--- a/simpleperf/read_dex_file.h
+++ b/simpleperf/read_dex_file.h
@@ -22,19 +22,11 @@
 #include <string>
 #include <vector>
 
-#ifndef NO_LIBDEXFILE_SUPPORT
-#include <art_api/ext_dex_file.h>
-#endif
-
-#ifndef NO_LIBDEXFILE_SUPPORT
-typedef art_api::dex::MethodInfo DexFileSymbol;
-#else
 struct DexFileSymbol {
   uint64_t offset;
   uint64_t len;
   std::string name;
 };
-#endif
 
 bool ReadSymbolsFromDexFileInMemory(void* addr, uint64_t size,
                                     const std::vector<uint64_t>& dex_file_offsets,
diff --git a/simpleperf/read_dex_file_test.cpp b/simpleperf/read_dex_file_test.cpp
index 7e72768..6b90b8b 100644
--- a/simpleperf/read_dex_file_test.cpp
+++ b/simpleperf/read_dex_file_test.cpp
@@ -24,6 +24,10 @@
 #include "test_util.h"
 #include "utils.h"
 
+bool operator==(const DexFileSymbol& s1, const DexFileSymbol& s2) {
+  return s1.offset == s2.offset && s1.len == s2.len && s1.name == s2.name;
+}
+
 TEST(read_dex_file, smoke) {
   std::vector<DexFileSymbol> symbols;
   ASSERT_TRUE(ReadSymbolsFromDexFile(GetTestData("base.vdex"), {0x28}, &symbols));
@@ -31,7 +35,6 @@
   DexFileSymbol target;
   target.offset = 0x6c77e;
   target.len = 0x16;
-  target.name = art_api::dex::DexString(
-      "com.example.simpleperf.simpleperfexamplewithnative.MixActivity$1.run");
+  target.name = "com.example.simpleperf.simpleperfexamplewithnative.MixActivity$1.run";
   ASSERT_NE(std::find(symbols.begin(), symbols.end(), target), symbols.end());
 }
diff --git a/simpleperf/utils.cpp b/simpleperf/utils.cpp
index 6de8811..6e35a79 100644
--- a/simpleperf/utils.cpp
+++ b/simpleperf/utils.cpp
@@ -46,7 +46,7 @@
   end_ = nullptr;
 }
 
-const char* OneTimeFreeAllocator::AllocateString(std::string_view s) {
+const char* OneTimeFreeAllocator::AllocateString(const std::string& s) {
   size_t size = s.size() + 1;
   if (cur_ + size > end_) {
     size_t alloc_size = std::max(size, unit_size_);
@@ -55,7 +55,7 @@
     cur_ = p;
     end_ = p + alloc_size;
   }
-  strcpy(cur_, s.data());
+  strcpy(cur_, s.c_str());
   const char* result = cur_;
   cur_ += size;
   return result;
diff --git a/simpleperf/utils.h b/simpleperf/utils.h
index 3ca8f5c..7fceecd 100644
--- a/simpleperf/utils.h
+++ b/simpleperf/utils.h
@@ -54,7 +54,7 @@
   }
 
   void Clear();
-  const char* AllocateString(std::string_view s);
+  const char* AllocateString(const std::string& s);
 
  private:
   const size_t unit_size_;