Merge "Fix errno handling to avoid being overwritten"
diff --git a/oatdump/oatdump.cc b/oatdump/oatdump.cc
index 14ff1ae..35f8ee2 100644
--- a/oatdump/oatdump.cc
+++ b/oatdump/oatdump.cc
@@ -1619,6 +1619,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()) {
@@ -1632,28 +1650,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/runtime/class_linker.cc b/runtime/class_linker.cc
index 65fe4e4..89c6a0b 100644
--- a/runtime/class_linker.cc
+++ b/runtime/class_linker.cc
@@ -4635,6 +4635,20 @@
   const OatDexFile* oat_dex_file = dex_file.GetOatDexFile();
   // In case we run without an image there won't be a backing oat file.
   if (oat_dex_file == nullptr || oat_dex_file->GetOatFile() == nullptr) {
+    if (!kIsDebugBuild && klass->GetClassLoader() == nullptr) {
+      // For boot classpath classes in the case we're not using a default boot image:
+      // we don't have the infrastructure yet to query verification data on individual
+      // boot vdex files, so it's simpler for now to consider all boot classpath classes
+      // verified. This should be taken into account when measuring boot time and app
+      // startup compare to the (current) production system where both:
+      // 1) updatable boot classpath classes, and
+      // 2) classes in /system referencing updatable classes
+      // will be verified at runtime.
+      if (!Runtime::Current()->IsUsingDefaultBootImageLocation()) {
+        oat_file_class_status = ClassStatus::kVerified;
+        return true;
+      }
+    }
     return false;
   }
 
diff --git a/runtime/runtime.cc b/runtime/runtime.cc
index 1465b14..5173f4d 100644
--- a/runtime/runtime.cc
+++ b/runtime/runtime.cc
@@ -1124,6 +1124,12 @@
                 runtime_options.GetOrDefault(Opt::StackDumpLockProfThreshold));
 
   image_location_ = runtime_options.GetOrDefault(Opt::Image);
+  {
+    std::string error_msg;
+    is_using_default_boot_image_location_ =
+        (image_location_.compare(GetDefaultBootImageLocation(&error_msg)) == 0);
+  }
+
   SetInstructionSet(runtime_options.GetOrDefault(Opt::ImageInstructionSet));
   boot_class_path_ = runtime_options.ReleaseOrDefault(Opt::BootClassPath);
   boot_class_path_locations_ = runtime_options.ReleaseOrDefault(Opt::BootClassPathLocations);
diff --git a/runtime/runtime.h b/runtime/runtime.h
index f550b84..ace0eea 100644
--- a/runtime/runtime.h
+++ b/runtime/runtime.h
@@ -186,8 +186,7 @@
   }
 
   bool IsUsingDefaultBootImageLocation() const {
-    std::string error_msg;
-    return GetImageLocation().compare(GetDefaultBootImageLocation(&error_msg)) == 0;
+    return is_using_default_boot_image_location_;
   }
 
   // Starts a runtime, which may cause threads to be started and code to run.
@@ -915,6 +914,7 @@
   std::vector<std::string> compiler_options_;
   std::vector<std::string> image_compiler_options_;
   std::string image_location_;
+  bool is_using_default_boot_image_location_;
 
   std::vector<std::string> boot_class_path_;
   std::vector<std::string> boot_class_path_locations_;
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);
+  }
+}