OSDN Git Service

Fix ownership of DexFile in profman
authorDavid Sehr <sehr@google.com>
Thu, 9 Feb 2017 04:58:10 +0000 (20:58 -0800)
committerDavid Sehr <sehr@google.com>
Thu, 9 Feb 2017 17:41:45 +0000 (09:41 -0800)
Don't leak the DexFiles we open in profman.

Bug: 34929204
Test: make test-art-host
Change-Id: Ife29556117c4dd84dd3e970901a7fdf458e5ad98

profman/profman.cc
runtime/jit/profile_compilation_info.cc
runtime/jit/profile_compilation_info.h

index ffebb6a..b0cbed1 100644 (file)
@@ -248,8 +248,11 @@ class ProfMan FINAL {
     return result;
   }
 
-  int DumpOneProfile(const std::string& banner, const std::string& filename, int fd,
-                     const std::vector<const DexFile*>* dex_files, std::string* dump) {
+  int DumpOneProfile(const std::string& banner,
+                     const std::string& filename,
+                     int fd,
+                     const std::vector<std::unique_ptr<const DexFile>>* dex_files,
+                     std::string* dump) {
     if (!filename.empty()) {
       fd = open(filename.c_str(), O_RDWR);
       if (fd < 0) {
@@ -277,7 +280,7 @@ class ProfMan FINAL {
 
     // Open apk/zip files and and read dex files.
     MemMap::Init();  // for ZipArchive::OpenFromFd
-    std::vector<const DexFile*> dex_files;
+    std::vector<std::unique_ptr<const DexFile>> dex_files;
     assert(dex_locations_.size() == apks_fd_.size());
     static constexpr bool kVerifyChecksum = true;
     for (size_t i = 0; i < dex_locations_.size(); ++i) {
@@ -293,7 +296,7 @@ class ProfMan FINAL {
         continue;
       }
       for (std::unique_ptr<const DexFile>& dex_file : dex_files_for_location) {
-        dex_files.push_back(dex_file.release());
+        dex_files.emplace_back(std::move(dex_file));
       }
     }
 
index 1405c40..9ba2d1a 100644 (file)
@@ -597,6 +597,24 @@ uint32_t ProfileCompilationInfo::GetNumberOfResolvedClasses() const {
   return total;
 }
 
+// Produce a non-owning vector from a vector.
+template<typename T>
+const std::vector<T*>* MakeNonOwningVector(const std::vector<std::unique_ptr<T>>* owning_vector) {
+  auto non_owning_vector = new std::vector<T*>();
+  for (auto& element : *owning_vector) {
+    non_owning_vector->push_back(element.get());
+  }
+  return non_owning_vector;
+}
+
+std::string ProfileCompilationInfo::DumpInfo(
+    const std::vector<std::unique_ptr<const DexFile>>* dex_files,
+    bool print_full_dex_location) const {
+  std::unique_ptr<const std::vector<const DexFile*>> non_owning_dex_files(
+      MakeNonOwningVector(dex_files));
+  return DumpInfo(non_owning_dex_files.get(), print_full_dex_location);
+}
+
 std::string ProfileCompilationInfo::DumpInfo(const std::vector<const DexFile*>* dex_files,
                                              bool print_full_dex_location) const {
   std::ostringstream os;
index f8061bc..b1587c0 100644 (file)
@@ -17,6 +17,7 @@
 #ifndef ART_RUNTIME_JIT_PROFILE_COMPILATION_INFO_H_
 #define ART_RUNTIME_JIT_PROFILE_COMPILATION_INFO_H_
 
+#include <memory>
 #include <set>
 #include <vector>
 
@@ -72,6 +73,8 @@ class ProfileCompilationInfo {
   // If dex_files is not null then the method indices will be resolved to their
   // names.
   // This is intended for testing and debugging.
+  std::string DumpInfo(const std::vector<std::unique_ptr<const DexFile>>* dex_files,
+                       bool print_full_dex_location = true) const;
   std::string DumpInfo(const std::vector<const DexFile*>* dex_files,
                        bool print_full_dex_location = true) const;