Support FDs for class loader context dex files

When compiling secondary dex files, dex2oat/dexoptanalyzer must open
all dex files in the given class loader context. However, these tools
do not have the SELinux permission to open app data files and instead
rely on installd to open them and pass file descriptors via command
line arguments.

This patch extends ClassLoaderContext::OpenDexFiles to support opening
dex files from a provided list of FDs, assuming the order corresponds
to the flattened class loader context. FDs can be passed to dex2oat/
dexoptanalyzer using a new '--class-loader-context-fds=' command line
argument. The format is a colon-separated list of integers.

dexoptanalyzer is also extended with a separate mode in which
dexopt-needed analysis is not performed, only the class loader context
is flattened and list of its dex files is printed to standard output
as a colon-separated list of paths. This mode is enabled with
'--flatten-class-loader-context' and is used by installd to obtain a
list of files it should open for dex2oat/dexoptanalyzer.

Bug: 126674985
Test: atest installd_dexopt_test
Change-Id: I46a671c90d14ad8615508c106a88ac1ee8a4ef28
diff --git a/runtime/class_loader_context.cc b/runtime/class_loader_context.cc
index c5988f6..61843f4 100644
--- a/runtime/class_loader_context.cc
+++ b/runtime/class_loader_context.cc
@@ -365,7 +365,9 @@
 
 // Opens requested class path files and appends them to opened_dex_files. If the dex files have
 // been stripped, this opens them from their oat files (which get added to opened_oat_files).
-bool ClassLoaderContext::OpenDexFiles(InstructionSet isa, const std::string& classpath_dir) {
+bool ClassLoaderContext::OpenDexFiles(InstructionSet isa,
+                                      const std::string& classpath_dir,
+                                      const std::vector<int>& fds) {
   if (dex_files_open_attempted_) {
     // Do not attempt to re-open the files if we already tried.
     return dex_files_open_result_;
@@ -388,6 +390,7 @@
   std::vector<ClassLoaderInfo*> work_list;
   CHECK(class_loader_chain_ != nullptr);
   work_list.push_back(class_loader_chain_.get());
+  size_t dex_file_index = 0;
   while (!work_list.empty()) {
     ClassLoaderInfo* info = work_list.back();
     work_list.pop_back();
@@ -399,34 +402,59 @@
         location = classpath_dir + (classpath_dir.back() == '/' ? "" : "/") + location;
       }
 
+      // If file descriptors were provided for the class loader context dex paths,
+      // get the descriptor which correponds to this dex path. We assume the `fds`
+      // vector follows the same order as a flattened class loader context.
+      int fd = -1;
+      if (!fds.empty()) {
+        if (dex_file_index >= fds.size()) {
+          LOG(WARNING) << "Number of FDs is smaller than number of dex files in the context";
+          dex_files_open_result_ = false;
+          return false;
+        }
+
+        fd = fds[dex_file_index++];
+        DCHECK_GE(fd, 0);
+      }
+
       std::string error_msg;
       // When opening the dex files from the context we expect their checksum to match their
       // contents. So pass true to verify_checksum.
-      if (!dex_file_loader.Open(location.c_str(),
-                                location.c_str(),
-                                Runtime::Current()->IsVerificationEnabled(),
-                                /*verify_checksum=*/ true,
-                                &error_msg,
-                                &info->opened_dex_files)) {
-        // If we fail to open the dex file because it's been stripped, try to open the dex file
-        // from its corresponding oat file.
-        // This could happen when we need to recompile a pre-build whose dex code has been stripped.
-        // (for example, if the pre-build is only quicken and we want to re-compile it
-        // speed-profile).
-        // TODO(calin): Use the vdex directly instead of going through the oat file.
-        OatFileAssistant oat_file_assistant(location.c_str(), isa, false);
-        std::unique_ptr<OatFile> oat_file(oat_file_assistant.GetBestOatFile());
-        std::vector<std::unique_ptr<const DexFile>> oat_dex_files;
-        if (oat_file != nullptr &&
-            OatFileAssistant::LoadDexFiles(*oat_file, location, &oat_dex_files)) {
-          info->opened_oat_files.push_back(std::move(oat_file));
-          info->opened_dex_files.insert(info->opened_dex_files.end(),
-                                        std::make_move_iterator(oat_dex_files.begin()),
-                                        std::make_move_iterator(oat_dex_files.end()));
-        } else {
-          LOG(WARNING) << "Could not open dex files from location: " << location;
-          dex_files_open_result_ = false;
+      if (fd < 0) {
+        if (!dex_file_loader.Open(location.c_str(),
+                                  location.c_str(),
+                                  Runtime::Current()->IsVerificationEnabled(),
+                                  /*verify_checksum=*/ true,
+                                  &error_msg,
+                                  &info->opened_dex_files)) {
+          // If we fail to open the dex file because it's been stripped, try to
+          // open the dex file from its corresponding oat file.
+          // This could happen when we need to recompile a pre-build whose dex
+          // code has been stripped (for example, if the pre-build is only
+          // quicken and we want to re-compile it speed-profile).
+          // TODO(calin): Use the vdex directly instead of going through the oat file.
+          OatFileAssistant oat_file_assistant(location.c_str(), isa, false);
+          std::unique_ptr<OatFile> oat_file(oat_file_assistant.GetBestOatFile());
+          std::vector<std::unique_ptr<const DexFile>> oat_dex_files;
+          if (oat_file != nullptr &&
+              OatFileAssistant::LoadDexFiles(*oat_file, location, &oat_dex_files)) {
+            info->opened_oat_files.push_back(std::move(oat_file));
+            info->opened_dex_files.insert(info->opened_dex_files.end(),
+                                          std::make_move_iterator(oat_dex_files.begin()),
+                                          std::make_move_iterator(oat_dex_files.end()));
+          } else {
+            LOG(WARNING) << "Could not open dex files from location: " << location;
+            dex_files_open_result_ = false;
+          }
         }
+      } else if (!dex_file_loader.Open(fd,
+                                       location.c_str(),
+                                       Runtime::Current()->IsVerificationEnabled(),
+                                       /*verify_checksum=*/ true,
+                                       &error_msg,
+                                       &info->opened_dex_files)) {
+        LOG(WARNING) << "Could not open dex files from fd " << fd << " for location: " << location;
+        dex_files_open_result_ = false;
       }
     }
 
@@ -450,6 +478,14 @@
     AddToWorkList(info, work_list);
   }
 
+  // Check that if file descriptors were provided, there were exactly as many
+  // as we have encountered while iterating over this class loader context.
+  if (dex_file_index != fds.size()) {
+    LOG(WARNING) << fds.size() << " FDs provided but only " << dex_file_index
+        << " dex files are in the class loader context";
+    dex_files_open_result_ = false;
+  }
+
   return dex_files_open_result_;
 }
 
@@ -759,6 +795,25 @@
   return result;
 }
 
+std::string ClassLoaderContext::FlattenDexPaths() const {
+  if (class_loader_chain_ == nullptr) {
+    return "";
+  }
+
+  std::vector<std::string> result;
+  std::vector<ClassLoaderInfo*> work_list;
+  work_list.push_back(class_loader_chain_.get());
+  while (!work_list.empty()) {
+    ClassLoaderInfo* info = work_list.back();
+    work_list.pop_back();
+    for (const std::string& dex_path : info->classpath) {
+      result.push_back(dex_path);
+    }
+    AddToWorkList(info, work_list);
+  }
+  return FlattenClasspath(result);
+}
+
 const char* ClassLoaderContext::GetClassLoaderTypeName(ClassLoaderType type) {
   switch (type) {
     case kPathClassLoader: return kPathClassLoaderString;
diff --git a/runtime/class_loader_context.h b/runtime/class_loader_context.h
index 5a89c4e..f8387ce 100644
--- a/runtime/class_loader_context.h
+++ b/runtime/class_loader_context.h
@@ -57,6 +57,10 @@
   // Returns true if all dex files where successfully opened.
   // It may be called only once per ClassLoaderContext. Subsequent calls will return the same
   // result without doing anything.
+  // If `context_fds` is an empty vector, files will be opened using the class path locations as
+  // filenames. Otherwise `context_fds` is expected to contain file descriptors to class path dex
+  // files, following the order of dex file locations in a flattened class loader context. If their
+  // number (size of `context_fds`) does not match the number of dex files, OpenDexFiles will fail.
   //
   // This will replace the class path locations with the locations of the opened dex files.
   // (Note that one dex file can contain multidexes. Each multidex will be added to the classpath
@@ -69,7 +73,9 @@
   // TODO(calin): we're forced to complicate the flow in this class with a different
   // OpenDexFiles step because the current dex2oat flow requires the dex files be opened before
   // the class loader is created. Consider reworking the dex2oat part.
-  bool OpenDexFiles(InstructionSet isa, const std::string& classpath_dir);
+  bool OpenDexFiles(InstructionSet isa,
+                    const std::string& classpath_dir,
+                    const std::vector<int>& context_fds = std::vector<int>());
 
   // Remove the specified compilation sources from all classpaths present in this context.
   // Should only be called before the first call to OpenDexFiles().
@@ -118,6 +124,10 @@
   // Should only be called if OpenDexFiles() returned true.
   std::vector<const DexFile*> FlattenOpenedDexFiles() const;
 
+  // Return a colon-separated list of dex file locations from this class loader
+  // context after flattening.
+  std::string FlattenDexPaths() const;
+
   // Verifies that the current context is identical to the context encoded as `context_spec`.
   // Identical means:
   //    - the number and type of the class loaders from the chain matches
@@ -127,8 +137,8 @@
   // Names are only verified if verify_names is true.
   // Checksums are only verified if verify_checksums is true.
   VerificationResult VerifyClassLoaderContextMatch(const std::string& context_spec,
-                                     bool verify_names = true,
-                                     bool verify_checksums = true) const;
+                                                   bool verify_names = true,
+                                                   bool verify_checksums = true) const;
 
   // Creates the class loader context from the given string.
   // The format: ClassLoaderType1[ClasspathElem1:ClasspathElem2...];ClassLoaderType2[...]...
diff --git a/runtime/oat_file_assistant.cc b/runtime/oat_file_assistant.cc
index ca09339..5529434 100644
--- a/runtime/oat_file_assistant.cc
+++ b/runtime/oat_file_assistant.cc
@@ -181,12 +181,14 @@
 int OatFileAssistant::GetDexOptNeeded(CompilerFilter::Filter target,
                                       bool profile_changed,
                                       bool downgrade,
-                                      ClassLoaderContext* class_loader_context) {
+                                      ClassLoaderContext* class_loader_context,
+                                      const std::vector<int>& context_fds) {
   OatFileInfo& info = GetBestInfo();
   DexOptNeeded dexopt_needed = info.GetDexOptNeeded(target,
                                                     profile_changed,
                                                     downgrade,
-                                                    class_loader_context);
+                                                    class_loader_context,
+                                                    context_fds);
   if (info.IsOatLocation() || dexopt_needed == kDex2OatFromScratch) {
     return dexopt_needed;
   }
@@ -748,10 +750,11 @@
     CompilerFilter::Filter target,
     bool profile_changed,
     bool downgrade,
-    ClassLoaderContext* context) {
+    ClassLoaderContext* context,
+    const std::vector<int>& context_fds) {
 
   bool filter_okay = CompilerFilterIsOkay(target, profile_changed, downgrade);
-  bool class_loader_context_okay = ClassLoaderContextIsOkay(context);
+  bool class_loader_context_okay = ClassLoaderContextIsOkay(context, context_fds);
 
   // Only check the filter and relocation if the class loader context is ok.
   // If it is not, we will return kDex2OatFromScratch as the compilation needs to be redone.
@@ -838,7 +841,8 @@
     CompilerFilter::IsAsGoodAs(current, target);
 }
 
-bool OatFileAssistant::OatFileInfo::ClassLoaderContextIsOkay(ClassLoaderContext* context) {
+bool OatFileAssistant::OatFileInfo::ClassLoaderContextIsOkay(ClassLoaderContext* context,
+                                                             const std::vector<int>& context_fds) {
   if (context == nullptr) {
     VLOG(oat) << "ClassLoaderContext check ignored: null context";
     return true;
@@ -855,12 +859,11 @@
       ? oat_file_assistant_->dex_location_.substr(0, dir_index)
       : "";
 
-  if (!context->OpenDexFiles(oat_file_assistant_->isa_, classpath_dir)) {
+  if (!context->OpenDexFiles(oat_file_assistant_->isa_, classpath_dir, context_fds)) {
     VLOG(oat) << "ClassLoaderContext check failed: dex files from the context could not be opened";
     return false;
   }
 
-
   const bool result = context->VerifyClassLoaderContextMatch(file->GetClassLoaderContext()) !=
       ClassLoaderContext::VerificationResult::kMismatch;
   if (!result) {
diff --git a/runtime/oat_file_assistant.h b/runtime/oat_file_assistant.h
index def55b8..85e5917 100644
--- a/runtime/oat_file_assistant.h
+++ b/runtime/oat_file_assistant.h
@@ -146,7 +146,8 @@
   int GetDexOptNeeded(CompilerFilter::Filter target_compiler_filter,
                       bool profile_changed = false,
                       bool downgrade = false,
-                      ClassLoaderContext* context = nullptr);
+                      ClassLoaderContext* context = nullptr,
+                      const std::vector<int>& context_fds = std::vector<int>());
 
   // Returns true if there is up-to-date code for this dex location,
   // irrespective of the compiler filter of the up-to-date code.
@@ -288,7 +289,8 @@
     DexOptNeeded GetDexOptNeeded(CompilerFilter::Filter target_compiler_filter,
                                  bool profile_changed,
                                  bool downgrade,
-                                 ClassLoaderContext* context);
+                                 ClassLoaderContext* context,
+                                 const std::vector<int>& context_fds);
 
     // Returns the loaded file.
     // Loads the file if needed. Returns null if the file failed to load.
@@ -329,7 +331,7 @@
     // compiler filter.
     bool CompilerFilterIsOkay(CompilerFilter::Filter target, bool profile_changed, bool downgrade);
 
-    bool ClassLoaderContextIsOkay(ClassLoaderContext* context);
+    bool ClassLoaderContextIsOkay(ClassLoaderContext* context, const std::vector<int>& context_fds);
 
     // Release the loaded oat file.
     // Returns null if the oat file hasn't been loaded.