Check external dex file checksum in `OatFile::Setup`.

And strenghten `class_def_index` check in `FindOatClass()`.

Also refactor .bss metadata reading to work around cpplint
flagging >500 non-comment lines in the function and change
tags to flag different .bss type sections.

(cherry picked from commit 5f1fafb6c9ec93e18a2d4da9c1d4d1264f9ba93d)

Test: m test-art-host-gtest
Test: testrunner.py --host --optimizing
Bug: 190491023
Merged-In: I85a0d9a750425b4e884dad12f47e414ccb38efe6
Change-Id: I1c502936e34fe22945eb10803c94067af7b402f3
diff --git a/runtime/oat_file.cc b/runtime/oat_file.cc
index 7809095..63778c7 100644
--- a/runtime/oat_file.cc
+++ b/runtime/oat_file.cc
@@ -828,6 +828,16 @@
           external_dex_files_.push_back(std::move(dex_file));
         }
       }
+      // Defensively verify external dex file checksum.
+      if (dex_file_checksum != external_dex_files_[i]->GetLocationChecksum()) {
+        *error_msg = StringPrintf("In oat file '%s', dex file checksum 0x%08x does not match"
+                                      " checksum 0x%08x of external dex file '%s'",
+                                  GetLocation().c_str(),
+                                  dex_file_checksum,
+                                  external_dex_files_[i]->GetLocationChecksum(),
+                                  external_dex_files_[i]->GetLocation().c_str());
+        return false;
+      }
       dex_file_pointer = external_dex_files_[i]->Begin();
     } else {
       // Do not support mixed-mode oat files.
@@ -961,16 +971,14 @@
     const IndexBssMapping* public_type_bss_mapping;
     const IndexBssMapping* package_type_bss_mapping;
     const IndexBssMapping* string_bss_mapping;
-    if (!ReadIndexBssMapping(
-            this, &oat, i, dex_file_location, "method", &method_bss_mapping, error_msg) ||
-        !ReadIndexBssMapping(
-            this, &oat, i, dex_file_location, "type", &type_bss_mapping, error_msg) ||
-        !ReadIndexBssMapping(
-            this, &oat, i, dex_file_location, "type", &public_type_bss_mapping, error_msg) ||
-        !ReadIndexBssMapping(
-            this, &oat, i, dex_file_location, "type", &package_type_bss_mapping, error_msg) ||
-        !ReadIndexBssMapping(
-            this, &oat, i, dex_file_location, "string", &string_bss_mapping, error_msg)) {
+    auto read_index_bss_mapping = [&](const char* tag, /*out*/const IndexBssMapping** mapping) {
+      return ReadIndexBssMapping(this, &oat, i, dex_file_location, tag, mapping, error_msg);
+    };
+    if (!read_index_bss_mapping("method", &method_bss_mapping) ||
+        !read_index_bss_mapping("type", &type_bss_mapping) ||
+        !read_index_bss_mapping("public type", &public_type_bss_mapping) ||
+        !read_index_bss_mapping("package type", &package_type_bss_mapping) ||
+        !read_index_bss_mapping("string", &string_bss_mapping)) {
       return false;
     }
 
@@ -1041,41 +1049,15 @@
       const std::string& dex_file_location = linker != nullptr ?
                                                  linker->GetBootClassPath()[i]->GetLocation() :
                                                  "No runtime/linker therefore no DexFile location";
-      if (!ReadIndexBssMapping(this,
-                               &bcp_info_begin,
-                               i,
-                               dex_file_location,
-                               "method",
-                               &bcp_bss_info_[i].method_bss_mapping,
-                               error_msg) ||
-          !ReadIndexBssMapping(this,
-                               &bcp_info_begin,
-                               i,
-                               dex_file_location,
-                               "type",
-                               &bcp_bss_info_[i].type_bss_mapping,
-                               error_msg) ||
-          !ReadIndexBssMapping(this,
-                               &bcp_info_begin,
-                               i,
-                               dex_file_location,
-                               "type",
-                               &bcp_bss_info_[i].public_type_bss_mapping,
-                               error_msg) ||
-          !ReadIndexBssMapping(this,
-                               &bcp_info_begin,
-                               i,
-                               dex_file_location,
-                               "type",
-                               &bcp_bss_info_[i].package_type_bss_mapping,
-                               error_msg) ||
-          !ReadIndexBssMapping(this,
-                               &bcp_info_begin,
-                               i,
-                               dex_file_location,
-                               "string",
-                               &bcp_bss_info_[i].string_bss_mapping,
-                               error_msg)) {
+      auto read_index_bss_mapping = [&](const char* tag, /*out*/const IndexBssMapping** mapping) {
+        return ReadIndexBssMapping(
+            this, &bcp_info_begin, i, dex_file_location, tag, mapping, error_msg);
+      };
+      if (!read_index_bss_mapping("method", &bcp_bss_info_[i].method_bss_mapping) ||
+          !read_index_bss_mapping("type", &bcp_bss_info_[i].type_bss_mapping) ||
+          !read_index_bss_mapping("public type", &bcp_bss_info_[i].public_type_bss_mapping) ||
+          !read_index_bss_mapping("package type", &bcp_bss_info_[i].package_type_bss_mapping) ||
+          !read_index_bss_mapping("string", &bcp_bss_info_[i].string_bss_mapping)) {
         return false;
       }
     }
@@ -2476,7 +2458,7 @@
 OatFile::OatClass OatFile::FindOatClass(const DexFile& dex_file,
                                         uint16_t class_def_idx,
                                         bool* found) {
-  DCHECK_NE(class_def_idx, DexFile::kDexNoIndex16);
+  CHECK_LT(class_def_idx, dex_file.NumClassDefs());
   const OatDexFile* oat_dex_file = dex_file.GetOatDexFile();
   if (oat_dex_file == nullptr || oat_dex_file->GetOatFile() == nullptr) {
     *found = false;