Fix oatdump .data.bimg.rel.ro for intrinsics.

The Integer.valueOf() intrinsic is using entries that
point to the boot image live objects array data rather
than a start of an Object. So, we need to check for
such entries before we look at the type of the Object.

Test: Additional test in oatdump_app_test
Test: m test-art-host-gtest
Bug: 71526895
Change-Id: I49ab3b416aa2b4912b9fd2043805e900ad76f0f2
diff --git a/oatdump/oatdump.cc b/oatdump/oatdump.cc
index c3abe6c..e623f77 100644
--- a/oatdump/oatdump.cc
+++ b/oatdump/oatdump.cc
@@ -1639,6 +1639,24 @@
     }
   }
 
+  std::pair<const uint8_t*, const uint8_t*> GetBootImageLiveObjectsDataRange(gc::Heap* heap) const
+      REQUIRES_SHARED(Locks::mutator_lock_) {
+    const std::vector<gc::space::ImageSpace*>& boot_image_spaces = heap->GetBootImageSpaces();
+    const ImageHeader& main_header = boot_image_spaces[0]->GetImageHeader();
+    ObjPtr<mirror::ObjectArray<mirror::Object>> boot_image_live_objects =
+        ObjPtr<mirror::ObjectArray<mirror::Object>>::DownCast(
+            main_header.GetImageRoot<kWithoutReadBarrier>(ImageHeader::kBootImageLiveObjects));
+    DCHECK(boot_image_live_objects != nullptr);
+    DCHECK(heap->ObjectIsInBootImageSpace(boot_image_live_objects));
+    const uint8_t* boot_image_live_objects_address =
+        reinterpret_cast<const uint8_t*>(boot_image_live_objects.Ptr());
+    uint32_t begin_offset = mirror::ObjectArray<mirror::Object>::OffsetOfElement(0).Uint32Value();
+    uint32_t end_offset = mirror::ObjectArray<mirror::Object>::OffsetOfElement(
+        boot_image_live_objects->GetLength()).Uint32Value();
+    return std::make_pair(boot_image_live_objects_address + begin_offset,
+                          boot_image_live_objects_address + end_offset);
+  }
+
   void DumpDataBimgRelRoEntries(std::ostream& os) {
     os << ".data.bimg.rel.ro: ";
     if (oat_file_.GetBootImageRelocations().empty()) {
@@ -1652,28 +1670,39 @@
       const std::vector<gc::space::ImageSpace*>& boot_image_spaces =
           runtime->GetHeap()->GetBootImageSpaces();
       ScopedObjectAccess soa(Thread::Current());
+      auto live_objects = GetBootImageLiveObjectsDataRange(runtime->GetHeap());
+      const uint8_t* live_objects_begin = live_objects.first;
+      const uint8_t* live_objects_end = live_objects.second;
       for (const uint32_t& object_offset : oat_file_.GetBootImageRelocations()) {
         uint32_t entry_index = &object_offset - oat_file_.GetBootImageRelocations().data();
         uint32_t entry_offset = entry_index * sizeof(oat_file_.GetBootImageRelocations()[0]);
         os << StringPrintf("  0x%x: 0x%08x", entry_offset, object_offset);
-        uint8_t* object = boot_image_spaces[0]->Begin() + object_offset;
+        uint8_t* address = boot_image_spaces[0]->Begin() + object_offset;
         bool found = false;
         for (gc::space::ImageSpace* space : boot_image_spaces) {
-          uint64_t local_offset = object - space->Begin();
+          uint64_t local_offset = address - space->Begin();
           if (local_offset < space->GetImageHeader().GetImageSize()) {
             if (space->GetImageHeader().GetObjectsSection().Contains(local_offset)) {
-              ObjPtr<mirror::Object> o = reinterpret_cast<mirror::Object*>(object);
-              if (o->IsString()) {
-                os << "   String: " << o->AsString()->ToModifiedUtf8();
-              } else if (o->IsClass()) {
-                os << "   Class: " << o->AsClass()->PrettyDescriptor();
-              } else {
-                os << StringPrintf("   0x%08x %s",
+              if (address >= live_objects_begin && address < live_objects_end) {
+                size_t index =
+                    (address - live_objects_begin) / sizeof(mirror::HeapReference<mirror::Object>);
+                os << StringPrintf("   0x%08x BootImageLiveObject[%zu]",
                                    object_offset,
-                                   o->GetClass()->PrettyDescriptor().c_str());
+                                   index);
+              } else {
+                ObjPtr<mirror::Object> o = reinterpret_cast<mirror::Object*>(address);
+                if (o->IsString()) {
+                  os << "   String: " << o->AsString()->ToModifiedUtf8();
+                } else if (o->IsClass()) {
+                  os << "   Class: " << o->AsClass()->PrettyDescriptor();
+                } else {
+                  os << StringPrintf("   0x%08x %s",
+                                     object_offset,
+                                     o->GetClass()->PrettyDescriptor().c_str());
+                }
               }
             } else if (space->GetImageHeader().GetMethodsSection().Contains(local_offset)) {
-              ArtMethod* m = reinterpret_cast<ArtMethod*>(object);
+              ArtMethod* m = reinterpret_cast<ArtMethod*>(address);
               os << "   ArtMethod: " << m->PrettyMethod();
             } else {
               os << StringPrintf("   0x%08x <unexpected section in %s>",
diff --git a/oatdump/oatdump_app_test.cc b/oatdump/oatdump_app_test.cc
index 2b04a0d..83e5f51 100644
--- a/oatdump/oatdump_app_test.cc
+++ b/oatdump/oatdump_app_test.cc
@@ -28,4 +28,16 @@
   ASSERT_TRUE(Exec(kStatic, kModeOatWithBootImage, {}, kListAndCode));
 }
 
+TEST_F(OatDumpTest, TestAppImageWithBootImage) {
+  const std::string app_image_arg = "--app-image-file=" + GetAppImageName();
+  ASSERT_TRUE(GenerateAppOdexFile(kDynamic, {"--runtime-arg", "-Xmx64M", app_image_arg}));
+  ASSERT_TRUE(Exec(kDynamic, kModeAppImage, {}, kListAndCode));
+}
+TEST_F(OatDumpTest, TestAppImageWithBootImageStatic) {
+  TEST_DISABLED_FOR_NON_STATIC_HOST_BUILDS();
+  const std::string app_image_arg = "--app-image-file=" + GetAppImageName();
+  ASSERT_TRUE(GenerateAppOdexFile(kStatic, {"--runtime-arg", "-Xmx64M", app_image_arg}));
+  ASSERT_TRUE(Exec(kStatic, kModeAppImage, {}, kListAndCode));
+}
+
 }  // namespace art
diff --git a/oatdump/oatdump_test.h b/oatdump/oatdump_test.h
index 3ead8de..c4f2967 100644
--- a/oatdump/oatdump_test.h
+++ b/oatdump/oatdump_test.h
@@ -92,6 +92,7 @@
     kModeOat,
     kModeCoreOat,
     kModeOatWithBootImage,
+    kModeAppImage,
     kModeArt,
     kModeSymbolize,
   };
@@ -108,6 +109,10 @@
     return "ProfileTestMultiDex";
   }
 
+  std::string GetAppImageName() {
+    return tmp_dir_ + "/" + GetAppBaseName() + ".art";
+  }
+
   std::string GetAppOdexName() {
     return tmp_dir_ + "/" + GetAppBaseName() + ".odex";
   }
@@ -200,6 +205,17 @@
         exec_argv.push_back("--instruction-set=" + std::string(
             GetInstructionSetString(kRuntimeISA)));
         exec_argv.push_back("--oat-file=" + GetAppOdexName());
+      } else if (mode == kModeAppImage) {
+        exec_argv.push_back("--runtime-arg");
+        exec_argv.push_back(GetClassPathOption("-Xbootclasspath:", GetLibCoreDexFileNames()));
+        exec_argv.push_back("--runtime-arg");
+        exec_argv.push_back(
+            GetClassPathOption("-Xbootclasspath-locations:", GetLibCoreDexLocations()));
+        exec_argv.push_back("--image=" + GetCoreArtLocation());
+        exec_argv.push_back("--instruction-set=" + std::string(
+            GetInstructionSetString(kRuntimeISA)));
+        exec_argv.push_back("--app-oat=" + GetAppOdexName());
+        exec_argv.push_back("--app-image=" + GetAppImageName());
       } else if (mode == kModeCoreOat) {
         exec_argv.push_back("--oat-file=" + core_oat_location_);
       } else {
diff --git a/test/ProfileTestMultiDex/Second.java b/test/ProfileTestMultiDex/Second.java
index 4b3c7a4..9f5dc66 100644
--- a/test/ProfileTestMultiDex/Second.java
+++ b/test/ProfileTestMultiDex/Second.java
@@ -30,3 +30,11 @@
   int getValue() { return 24; }
 }
 
+class TestIntrinsicOatdump {
+  Integer valueOf(int i) {
+    // ProfileTestMultiDex is used also for testing oatdump for apps.
+    // This is a regression test that oatdump can handle .data.bimg.rel.ro
+    // entries pointing to the middle of the "boot image live objects" array.
+    return Integer.valueOf(i);
+  }
+}