Pass boot image flags based on system properties when invoking dex2oat.

This makes dex2oat align with what the runtime is using.

Also, this CL swaps the priority of
"persist.device_config.runtime_native_boot.profilebootclasspath" and
"dalvik.vm.profilebootclasspath". This is to reflect the fix in
https://r.android.com/2297438.

Bug: 229268202
Test: m test-art-host-gtest-art_artd_tests
Test: -
  1. adb shell setprop dalvik.vm.profilebootclasspath true
  2. adb shell stop && adb shell start
  3. adb shell pm art optimize-package -m speed-profile -f com.google.android.youtube
  4. adb shell am start -S -W -c android.intent.category.LAUNCHER -a android.intent.action.MAIN com.google.android.youtube
  5. See the oat file and the vdex file in the process map.
Ignore-AOSP-First: ART Services.
Change-Id: I9fce9e34497dbbf0e01f9459085f9f35b32163a8
diff --git a/artd/artd.cc b/artd/artd.cc
index 208e5cc..d7ef992 100644
--- a/artd/artd.cc
+++ b/artd/artd.cc
@@ -837,6 +837,7 @@
     fd_logger.Add(*profile_file);
   }
 
+  AddBootImageFlags(args);
   AddCompilerConfigFlags(
       in_instructionSet, in_compilerFilter, in_priorityClass, in_dexoptOptions, args);
   AddPerfConfigFlags(in_priorityClass, args);
@@ -962,7 +963,7 @@
 
     if (UseJitZygoteLocked()) {
       location_str = GetJitZygoteBootImageLocation();
-    } else if (std::string value = props_->GetOrEmpty("dalvik.vm.boot-image"); !value.empty()) {
+    } else if (std::string value = GetUserDefinedBootImageLocationsLocked(); !value.empty()) {
       location_str = std::move(value);
     } else {
       std::string error_msg;
@@ -993,17 +994,35 @@
   return &cached_boot_class_path_.value();
 }
 
+bool Artd::UseJitZygote() {
+  std::lock_guard<std::mutex> lock(cache_mu_);
+  return UseJitZygoteLocked();
+}
+
 bool Artd::UseJitZygoteLocked() {
   if (!cached_use_jit_zygote_.has_value()) {
     cached_use_jit_zygote_ =
-        props_->GetBool("dalvik.vm.profilebootclasspath",
-                        "persist.device_config.runtime_native_boot.profilebootclasspath",
+        props_->GetBool("persist.device_config.runtime_native_boot.profilebootclasspath",
+                        "dalvik.vm.profilebootclasspath",
                         /*default_value=*/false);
   }
 
   return cached_use_jit_zygote_.value();
 }
 
+const std::string& Artd::GetUserDefinedBootImageLocations() {
+  std::lock_guard<std::mutex> lock(cache_mu_);
+  return GetUserDefinedBootImageLocationsLocked();
+}
+
+const std::string& Artd::GetUserDefinedBootImageLocationsLocked() {
+  if (!cached_user_defined_boot_image_locations_.has_value()) {
+    cached_user_defined_boot_image_locations_ = props_->GetOrEmpty("dalvik.vm.boot-image");
+  }
+
+  return cached_user_defined_boot_image_locations_.value();
+}
+
 bool Artd::DenyArtApexDataFiles() {
   std::lock_guard<std::mutex> lock(cache_mu_);
   return DenyArtApexDataFilesLocked();
@@ -1038,6 +1057,14 @@
   return props_->GetBool("dalvik.vm.dex2oat-swap", /*default_value=*/true);
 }
 
+void Artd::AddBootImageFlags(/*out*/ CmdlineBuilder& args) {
+  if (UseJitZygote()) {
+    args.Add("--force-jit-zygote");
+  } else {
+    args.AddIfNonEmpty("--boot-image=%s", GetUserDefinedBootImageLocations());
+  }
+}
+
 void Artd::AddCompilerConfigFlags(const std::string& instruction_set,
                                   const std::string& compiler_filter,
                                   PriorityClass priority_class,
diff --git a/artd/artd.h b/artd/artd.h
index e90d541..0801317 100644
--- a/artd/artd.h
+++ b/artd/artd.h
@@ -163,8 +163,12 @@
 
   android::base::Result<const std::vector<std::string>*> GetBootClassPath() EXCLUDES(cache_mu_);
 
+  bool UseJitZygote() EXCLUDES(cache_mu_);
   bool UseJitZygoteLocked() REQUIRES(cache_mu_);
 
+  const std::string& GetUserDefinedBootImageLocations() EXCLUDES(cache_mu_);
+  const std::string& GetUserDefinedBootImageLocationsLocked() REQUIRES(cache_mu_);
+
   bool DenyArtApexDataFiles() EXCLUDES(cache_mu_);
   bool DenyArtApexDataFilesLocked() REQUIRES(cache_mu_);
 
@@ -183,6 +187,8 @@
 
   bool ShouldCreateSwapFileForDexopt();
 
+  void AddBootImageFlags(/*out*/ art::tools::CmdlineBuilder& args);
+
   void AddCompilerConfigFlags(const std::string& instruction_set,
                               const std::string& compiler_filter,
                               aidl::com::android::server::art::PriorityClass priority_class,
@@ -196,6 +202,7 @@
   std::optional<std::vector<std::string>> cached_boot_image_locations_ GUARDED_BY(cache_mu_);
   std::optional<std::vector<std::string>> cached_boot_class_path_ GUARDED_BY(cache_mu_);
   std::optional<bool> cached_use_jit_zygote_ GUARDED_BY(cache_mu_);
+  std::optional<std::string> cached_user_defined_boot_image_locations_ GUARDED_BY(cache_mu_);
   std::optional<bool> cached_deny_art_apex_data_files_ GUARDED_BY(cache_mu_);
 
   std::mutex ofa_context_mu_;
diff --git a/artd/artd_test.cc b/artd/artd_test.cc
index f14a608..e17f71f 100644
--- a/artd/artd_test.cc
+++ b/artd/artd_test.cc
@@ -701,7 +701,9 @@
                                     Not(Contains(Flag("-Xms", _))),
                                     Not(Contains(Flag("-Xmx", _))),
                                     Not(Contains("--compile-individually")),
-                                    Not(Contains(Flag("--image-format=", _))))),
+                                    Not(Contains(Flag("--image-format=", _))),
+                                    Not(Contains("--force-jit-zygote")),
+                                    Not(Contains(Flag("--boot-image=", _))))),
                   _,
                   _))
       .WillOnce(Return(0));
@@ -728,6 +730,7 @@
   EXPECT_CALL(*mock_props_, GetProperty("dalvik.vm.dex2oat-Xmx")).WillOnce(Return("xmx"));
   EXPECT_CALL(*mock_props_, GetProperty("ro.config.low_ram")).WillOnce(Return("1"));
   EXPECT_CALL(*mock_props_, GetProperty("dalvik.vm.appimageformat")).WillOnce(Return("imgfmt"));
+  EXPECT_CALL(*mock_props_, GetProperty("dalvik.vm.boot-image")).WillOnce(Return("boot-image"));
 
   EXPECT_CALL(*mock_exec_utils_,
               DoExecAndReturnCode(
@@ -745,13 +748,32 @@
                                     Contains(Flag("-Xms", "xms")),
                                     Contains(Flag("-Xmx", "xmx")),
                                     Contains("--compile-individually"),
-                                    Contains(Flag("--image-format=", "imgfmt")))),
+                                    Contains(Flag("--image-format=", "imgfmt")),
+                                    Not(Contains("--force-jit-zygote")),
+                                    Contains(Flag("--boot-image=", "boot-image")))),
                   _,
                   _))
       .WillOnce(Return(0));
   RunDexopt();
 }
 
+TEST_F(ArtdTest, dexoptFlagsForceJitZygote) {
+  EXPECT_CALL(*mock_props_,
+              GetProperty("persist.device_config.runtime_native_boot.profilebootclasspath"))
+      .WillOnce(Return("true"));
+  ON_CALL(*mock_props_, GetProperty("dalvik.vm.boot-image")).WillByDefault(Return("boot-image"));
+
+  EXPECT_CALL(*mock_exec_utils_,
+              DoExecAndReturnCode(WhenSplitBy("--",
+                                              _,
+                                              AllOf(Contains("--force-jit-zygote"),
+                                                    Not(Contains(Flag("--boot-image=", _))))),
+                                  _,
+                                  _))
+      .WillOnce(Return(0));
+  RunDexopt();
+}
+
 static void SetDefaultResourceControlProps(MockSystemProperties* mock_props) {
   EXPECT_CALL(*mock_props, GetProperty("dalvik.vm.dex2oat-cpu-set")).WillRepeatedly(Return("0,2"));
   EXPECT_CALL(*mock_props, GetProperty("dalvik.vm.dex2oat-threads")).WillRepeatedly(Return("4"));