Follow-up for "Use provided dex filenames in oat file."

Address late comments for
    https://android-review.googlesource.com/1180228 .

Test: m test-art-host-gtest
Change-Id: Ib242db89a630a70b46d603feadaa1a2f3840a02a
diff --git a/runtime/oat_file.cc b/runtime/oat_file.cc
index 81356c2..547478f 100644
--- a/runtime/oat_file.cc
+++ b/runtime/oat_file.cc
@@ -602,6 +602,18 @@
     std::string_view oat_dex_file_location(dex_file_location_data, dex_file_location_size);
     std::string dex_file_location(oat_dex_file_location);
     bool is_multidex = DexFileLoader::IsMultiDexLocation(dex_file_location.c_str());
+    // Check that `is_multidex` does not clash with other indicators. The first dex location
+    // must be primary location and, if we're opening external dex files, the location must
+    // be multi-dex if and only if we already have a dex file opened for it.
+    if ((i == 0 && is_multidex) ||
+        (!external_dex_files_.empty() && (is_multidex != (i < external_dex_files_.size())))) {
+      *error_msg = StringPrintf("In oat file '%s' found unexpected %s location '%s'",
+                                GetLocation().c_str(),
+                                is_multidex ? "multi-dex" : "primary",
+                                dex_file_location.c_str());
+      return false;
+    }
+    // Remember the primary location and, if provided, the replacement from `dex_filenames`.
     if (!is_multidex) {
       primary_location = oat_dex_file_location;
       if (!dex_filenames.empty()) {
@@ -617,14 +629,7 @@
         ++dex_filenames_pos;
       }
     }
-    if ((i == 0 && is_multidex) ||
-        (!external_dex_files_.empty() && (is_multidex != (i < external_dex_files_.size())))) {
-      *error_msg = StringPrintf("In oat file '%s' found unexpected %s location '%s'",
-                                GetLocation().c_str(),
-                                is_multidex ? "multi-dex" : "primary",
-                                dex_file_location.c_str());
-      return false;
-    }
+    // Check that the base location of a multidex location matches the last seen primary location.
     if (is_multidex &&
         (!StartsWith(dex_file_location, primary_location) ||
              dex_file_location[primary_location.size()] != DexFileLoader::kMultiDexSeparator)) {
diff --git a/runtime/oat_file.h b/runtime/oat_file.h
index 6de07e3..99955f1 100644
--- a/runtime/oat_file.h
+++ b/runtime/oat_file.h
@@ -107,11 +107,10 @@
                                   ArrayRef<const std::string> dex_filenames,
                                   std::string* error_msg);
   // Open an oat file. Returns null on failure.
-  // The `dex_filenames` argument, if provided, specifies dex files to
-  // open if they are not embedded in the vdex file. This may differ
-  // from dex file locations in the oat file for cross-compilation
-  // (the dex file name is the host path and dex location is the future
-  // path on target) and testing.
+  // The `dex_filenames` argument, if provided, overrides the dex locations
+  // from oat file when opening the dex files if they are not embedded in the
+  // vdex file. These may differ for cross-compilation (the dex file name is
+  // the host path and dex location is the future path on target) and testing.
   static OatFile* Open(int zip_fd,
                        const std::string& filename,
                        const std::string& location,