dex2oat: Expect boot image profiles for boot image.

The profile loading is currently permissive, so this
does not really matter but we intend to make it more
strict soon.

Also clean up ScratchFile to avoid dex2oat_image_test
error logs related to ScratchFile destruction.

Test: m test-art-host-gtest
Test: testrunner.py --host --optimizing
Bug: 148067697
Change-Id: I34526ddb0389444d54b9754afbedeb671c7b05a8
diff --git a/dex2oat/dex2oat.cc b/dex2oat/dex2oat.cc
index cd87eb4..02afa7c 100644
--- a/dex2oat/dex2oat.cc
+++ b/dex2oat/dex2oat.cc
@@ -2348,7 +2348,8 @@
     // default profile arena). However the setup logic is messy and needs
     // cleaning up before that (e.g. the oat writers are created before the
     // runtime).
-    profile_compilation_info_.reset(new ProfileCompilationInfo());
+    bool for_boot_image = IsBootImage() || IsBootImageExtension();
+    profile_compilation_info_.reset(new ProfileCompilationInfo(for_boot_image));
     // Dex2oat only uses the reference profile and that is not updated concurrently by the app or
     // other processes. So we don't need to lock (as we have to do in profman or when writing the
     // profile info).
diff --git a/dex2oat/dex2oat_image_test.cc b/dex2oat/dex2oat_image_test.cc
index 20841f7..a44a3e4 100644
--- a/dex2oat/dex2oat_image_test.cc
+++ b/dex2oat/dex2oat_image_test.cc
@@ -199,10 +199,10 @@
   ArrayRef<const std::string> libcore_dexes_array(libcore_dexes);
   {
     ScratchFile profile_file;
-    GenerateProfile(libcore_dexes_array,
-                    profile_file.GetFile(),
-                    /*method_frequency=*/ 1u,
-                    /*type_frequency=*/ 1u);
+    GenerateBootProfile(libcore_dexes_array,
+                        profile_file.GetFile(),
+                        /*method_frequency=*/ 1u,
+                        /*type_frequency=*/ 1u);
     everything_sizes = CompileImageAndGetSizes(
         dex_files,
         {"--profile-file=" + profile_file.GetFilename(),
@@ -219,10 +219,10 @@
   // Test compiling fewer methods and classes.
   {
     ScratchFile profile_file;
-    GenerateProfile(libcore_dexes_array,
-                    profile_file.GetFile(),
-                    kMethodFrequency,
-                    kTypeFrequency);
+    GenerateBootProfile(libcore_dexes_array,
+                        profile_file.GetFile(),
+                        kMethodFrequency,
+                        kTypeFrequency);
     filter_sizes = CompileImageAndGetSizes(
         dex_files,
         {"--profile-file=" + profile_file.GetFilename(),
@@ -310,22 +310,22 @@
 
   // Create profiles.
   ScratchFile head_profile_file;
-  GenerateProfile(head_dex_files,
-                  head_profile_file.GetFile(),
-                  /*method_frequency=*/ 1u,
-                  /*type_frequency=*/ 1u);
+  GenerateBootProfile(head_dex_files,
+                      head_profile_file.GetFile(),
+                      /*method_frequency=*/ 1u,
+                      /*type_frequency=*/ 1u);
   const std::string& head_profile_filename = head_profile_file.GetFilename();
   ScratchFile mid_profile_file;
-  GenerateProfile(mid_dex_files,
-                  mid_profile_file.GetFile(),
-                  /*method_frequency=*/ 5u,
-                  /*type_frequency=*/ 4u);
+  GenerateBootProfile(mid_dex_files,
+                      mid_profile_file.GetFile(),
+                      /*method_frequency=*/ 5u,
+                      /*type_frequency=*/ 4u);
   const std::string& mid_profile_filename = mid_profile_file.GetFilename();
   ScratchFile tail_profile_file;
-  GenerateProfile(tail_dex_files,
-                  tail_profile_file.GetFile(),
-                  /*method_frequency=*/ 5u,
-                  /*type_frequency=*/ 4u);
+  GenerateBootProfile(tail_dex_files,
+                      tail_profile_file.GetFile(),
+                      /*method_frequency=*/ 5u,
+                      /*type_frequency=*/ 4u);
   const std::string& tail_profile_filename = tail_profile_file.GetFilename();
 
   // Compile the "head", i.e. the primary boot image.
@@ -376,10 +376,10 @@
 
   // Create a smaller profile for the single-image test that squashes the "mid" and "tail".
   ScratchFile single_profile_file;
-  GenerateProfile(single_dex_files,
-                  single_profile_file.GetFile(),
-                  /*method_frequency=*/ 5u,
-                  /*type_frequency=*/ 4u);
+  GenerateBootProfile(single_dex_files,
+                      single_profile_file.GetFile(),
+                      /*method_frequency=*/ 5u,
+                      /*type_frequency=*/ 4u);
   const std::string& single_profile_filename = single_profile_file.GetFilename();
 
   // Prepare the single image name and location.
diff --git a/libartbase/base/common_art_test.cc b/libartbase/base/common_art_test.cc
index 042ce55..698ff87 100644
--- a/libartbase/base/common_art_test.cc
+++ b/libartbase/base/common_art_test.cc
@@ -126,10 +126,11 @@
 }
 
 void ScratchFile::Close() {
-  if (file_.get() != nullptr) {
+  if (file_ != nullptr) {
     if (file_->FlushCloseOrErase() != 0) {
       PLOG(WARNING) << "Error closing scratch file.";
     }
+    file_.reset();
   }
 }
 
diff --git a/runtime/common_runtime_test.cc b/runtime/common_runtime_test.cc
index 4509573..0d21de1 100644
--- a/runtime/common_runtime_test.cc
+++ b/runtime/common_runtime_test.cc
@@ -571,8 +571,9 @@
 void CommonRuntimeTestImpl::GenerateProfile(ArrayRef<const std::string> dexes,
                                             File* out_file,
                                             size_t method_frequency,
-                                            size_t type_frequency) {
-  ProfileCompilationInfo profile;
+                                            size_t type_frequency,
+                                            bool for_boot_image) {
+  ProfileCompilationInfo profile(for_boot_image);
   VisitDexes(
       dexes,
       [&profile](MethodReference ref) {
diff --git a/runtime/common_runtime_test.h b/runtime/common_runtime_test.h
index cc23620..cc347a1 100644
--- a/runtime/common_runtime_test.h
+++ b/runtime/common_runtime_test.h
@@ -174,7 +174,15 @@
   void GenerateProfile(ArrayRef<const std::string> dexes,
                        File* out_file,
                        size_t method_frequency = 1u,
-                       size_t type_frequency = 1u);
+                       size_t type_frequency = 1u,
+                       bool for_boot_image = false);
+  void GenerateBootProfile(ArrayRef<const std::string> dexes,
+                           File* out_file,
+                           size_t method_frequency = 1u,
+                           size_t type_frequency = 1u) {
+    return GenerateProfile(
+        dexes, out_file, method_frequency, type_frequency, /*for_boot_image=*/ true);
+  }
 
   std::unique_ptr<Runtime> runtime_;
 
diff --git a/runtime/gc/space/image_space_test.cc b/runtime/gc/space/image_space_test.cc
index 0b22a6f..885dd21 100644
--- a/runtime/gc/space/image_space_test.cc
+++ b/runtime/gc/space/image_space_test.cc
@@ -79,7 +79,7 @@
     std::string jar_name = GetTestDexFileName(base_name);
     ArrayRef<const std::string> dex_files(&jar_name, /*size=*/ 1u);
     ScratchFile profile_file;
-    GenerateProfile(dex_files, profile_file.GetFile());
+    GenerateBootProfile(dex_files, profile_file.GetFile());
     std::vector<std::string> extra_args = {
         "--profile-file=" + profile_file.GetFilename(),
         "--runtime-arg",