runtime: update oat_file_assistant for on device signing

Use odex files from ART APEX data directory if present. This is
specifically to enable loading system_server from the ART APEX data
directory.

Bug: 160683548
Test: manual
Change-Id: I5daf751b83f35386420f037e0b207f601565d5c7
diff --git a/runtime/oat_file_assistant.cc b/runtime/oat_file_assistant.cc
index f23f29a..8cfb8d5 100644
--- a/runtime/oat_file_assistant.cc
+++ b/runtime/oat_file_assistant.cc
@@ -476,12 +476,10 @@
   return true;
 }
 
-static bool DexLocationToOdexNames(const std::string& location,
-                                   InstructionSet isa,
-                                   std::string* odex_filename,
-                                   std::string* oat_dir,
-                                   std::string* isa_dir,
-                                   std::string* error_msg) {
+bool OatFileAssistant::DexLocationToOdexFilename(const std::string& location,
+                                                 InstructionSet isa,
+                                                 std::string* odex_filename,
+                                                 std::string* error_msg) {
   CHECK(odex_filename != nullptr);
   CHECK(error_msg != nullptr);
 
@@ -500,14 +498,9 @@
   std::string dir = location.substr(0, pos+1);
   // Add the oat directory.
   dir += "oat";
-  if (oat_dir != nullptr) {
-    *oat_dir = dir;
-  }
+
   // Add the isa directory
   dir += "/" + std::string(GetInstructionSetString(isa));
-  if (isa_dir != nullptr) {
-    *isa_dir = dir;
-  }
 
   // Get the base part of the file without the extension.
   std::string file = location.substr(pos+1);
@@ -522,13 +515,6 @@
   return true;
 }
 
-bool OatFileAssistant::DexLocationToOdexFilename(const std::string& location,
-                                                 InstructionSet isa,
-                                                 std::string* odex_filename,
-                                                 std::string* error_msg) {
-  return DexLocationToOdexNames(location, isa, odex_filename, nullptr, nullptr, error_msg);
-}
-
 bool OatFileAssistant::DexLocationToOatFilename(const std::string& location,
                                                 InstructionSet isa,
                                                 std::string* oat_filename,
@@ -536,6 +522,14 @@
   CHECK(oat_filename != nullptr);
   CHECK(error_msg != nullptr);
 
+  // Check if `location` could have an oat file in the ART APEX data directory. If so, and the
+  // file exists, use it.
+  std::string apex_data_file = GetApexDataOdexFilename(location, isa);
+  if (!apex_data_file.empty() && OS::FileExists(apex_data_file.c_str(), /*check_file_type=*/true)) {
+    *oat_filename = apex_data_file;
+    return true;
+  }
+
   // If ANDROID_DATA is not set, return false instead of aborting.
   // This can occur for preopt when using a class loader context.
   if (GetAndroidDataSafe(error_msg).empty()) {