xref: /aosp_15_r20/art/dex2oat/dex2oat_image_test.cc (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <fstream>
18 #include <regex>
19 #include <sstream>
20 #include <string>
21 #include <vector>
22 
23 #include <sys/mman.h>
24 #include <sys/wait.h>
25 #include <unistd.h>
26 
27 #include <android-base/logging.h>
28 #include <android-base/stringprintf.h>
29 #include <android-base/strings.h>
30 
31 #include "common_runtime_test.h"
32 
33 #include "base/array_ref.h"
34 #include "base/file_utils.h"
35 #include "base/macros.h"
36 #include "base/mem_map.h"
37 #include "base/unix_file/fd_file.h"
38 #include "base/utils.h"
39 #include "dex/art_dex_file_loader.h"
40 #include "dex/dex_file-inl.h"
41 #include "dex/dex_file_loader.h"
42 #include "dex/method_reference.h"
43 #include "dex/type_reference.h"
44 #include "gc/space/image_space.h"
45 #include "runtime.h"
46 #include "scoped_thread_state_change-inl.h"
47 #include "thread-current-inl.h"
48 
49 namespace art {
50 
51 // A suitable address for loading the core images.
52 constexpr uint32_t kBaseAddress = ART_BASE_ADDRESS;
53 
54 struct ImageSizes {
55   size_t art_size = 0;
56   size_t oat_size = 0;
57   size_t vdex_size = 0;
58 };
59 
operator <<(std::ostream & os,const ImageSizes & sizes)60 std::ostream& operator<<(std::ostream& os, const ImageSizes& sizes) {
61   os << "art=" << sizes.art_size << " oat=" << sizes.oat_size << " vdex=" << sizes.vdex_size;
62   return os;
63 }
64 
65 class Dex2oatImageTest : public CommonRuntimeTest {
66  public:
TearDown()67   void TearDown() override {}
68 
69  protected:
SetUpRuntimeOptions(RuntimeOptions * options)70   void SetUpRuntimeOptions(RuntimeOptions* options) override {
71     // Disable implicit dex2oat invocations when loading image spaces.
72     options->emplace_back("-Xnoimage-dex2oat", nullptr);
73   }
74 
WriteLine(File * file,std::string line)75   static void WriteLine(File* file, std::string line) {
76     line += '\n';
77     EXPECT_TRUE(file->WriteFully(&line[0], line.length()));
78   }
79 
AddRuntimeArg(std::vector<std::string> & args,const std::string & arg)80   void AddRuntimeArg(std::vector<std::string>& args, const std::string& arg) {
81     args.push_back("--runtime-arg");
82     args.push_back(arg);
83   }
84 
CompileImageAndGetSizes(ArrayRef<const std::string> dex_files,const std::vector<std::string> & extra_args)85   ImageSizes CompileImageAndGetSizes(ArrayRef<const std::string> dex_files,
86                                      const std::vector<std::string>& extra_args) {
87     ImageSizes ret;
88     ScratchDir scratch;
89     std::string filename_prefix = scratch.GetPath() + "boot";
90     std::vector<std::string> local_extra_args = extra_args;
91     local_extra_args.push_back(android::base::StringPrintf("--base=0x%08x", kBaseAddress));
92     std::string error_msg;
93     if (!CompileBootImage(local_extra_args, filename_prefix, dex_files, &error_msg)) {
94       LOG(ERROR) << "Failed to compile image " << filename_prefix << error_msg;
95     }
96     std::string art_file = filename_prefix + ".art";
97     std::string oat_file = filename_prefix + ".oat";
98     std::string vdex_file = filename_prefix + ".vdex";
99     int64_t art_size = OS::GetFileSizeBytes(art_file.c_str());
100     int64_t oat_size = OS::GetFileSizeBytes(oat_file.c_str());
101     int64_t vdex_size = OS::GetFileSizeBytes(vdex_file.c_str());
102     CHECK_GT(art_size, 0u) << art_file;
103     CHECK_GT(oat_size, 0u) << oat_file;
104     CHECK_GT(vdex_size, 0u) << vdex_file;
105     ret.art_size = art_size;
106     ret.oat_size = oat_size;
107     ret.vdex_size = vdex_size;
108     return ret;
109   }
110 
ReserveCoreImageAddressSpace(std::string * error_msg)111   MemMap ReserveCoreImageAddressSpace(/*out*/std::string* error_msg) {
112     constexpr size_t kReservationSize = 256 * MB;  // This should be enough for the compiled images.
113     // Extend to both directions for maximum relocation difference.
114     static_assert(ART_BASE_ADDRESS_MIN_DELTA < 0);
115     static_assert(ART_BASE_ADDRESS_MAX_DELTA > 0);
116     static_assert(IsAligned<kElfSegmentAlignment>(ART_BASE_ADDRESS_MIN_DELTA));
117     static_assert(IsAligned<kElfSegmentAlignment>(ART_BASE_ADDRESS_MAX_DELTA));
118     constexpr size_t kExtra = ART_BASE_ADDRESS_MAX_DELTA - ART_BASE_ADDRESS_MIN_DELTA;
119     uint32_t min_relocated_address = kBaseAddress + ART_BASE_ADDRESS_MIN_DELTA;
120     return MemMap::MapAnonymous("Reservation",
121                                 reinterpret_cast<uint8_t*>(min_relocated_address),
122                                 kReservationSize + kExtra,
123                                 PROT_NONE,
124                                 /*low_4gb=*/ true,
125                                 /*reuse=*/ false,
126                                 /*reservation=*/ nullptr,
127                                 error_msg);
128   }
129 
CopyDexFiles(const std::string & dir,std::vector<std::string> * dex_files)130   void CopyDexFiles(const std::string& dir, /*inout*/std::vector<std::string>* dex_files) {
131     CHECK(dir.ends_with("/"));
132     for (std::string& dex_file : *dex_files) {
133       size_t slash_pos = dex_file.rfind('/');
134       CHECK(OS::FileExists(dex_file.c_str())) << dex_file;
135       CHECK_NE(std::string::npos, slash_pos);
136       std::string new_location = dir + dex_file.substr(slash_pos + 1u);
137       std::ifstream src_stream(dex_file, std::ios::binary);
138       std::ofstream dst_stream(new_location, std::ios::binary);
139       dst_stream << src_stream.rdbuf();
140       dex_file = new_location;
141     }
142   }
143 
CompareFiles(const std::string & filename1,const std::string & filename2)144   bool CompareFiles(const std::string& filename1, const std::string& filename2) {
145     std::unique_ptr<File> file1(OS::OpenFileForReading(filename1.c_str()));
146     std::unique_ptr<File> file2(OS::OpenFileForReading(filename2.c_str()));
147     // Did we open the files?
148     if (file1 == nullptr || file2 == nullptr) {
149       return false;
150     }
151     // Are they non-empty and the same length?
152     if (file1->GetLength() <= 0 || file2->GetLength() != file1->GetLength()) {
153       return false;
154     }
155     return file1->Compare(file2.get()) == 0;
156   }
157 
AddAndroidRootToImageCompilerOptions()158   void AddAndroidRootToImageCompilerOptions() {
159     const char* android_root = getenv("ANDROID_ROOT");
160     CHECK(android_root != nullptr);
161     Runtime::Current()->image_compiler_options_.push_back(
162         "--android-root=" + std::string(android_root));
163   }
164 
EnableImageDex2Oat()165   void EnableImageDex2Oat() {
166     Runtime::Current()->image_dex2oat_enabled_ = true;
167   }
168 
DisableImageDex2Oat()169   void DisableImageDex2Oat() {
170     Runtime::Current()->image_dex2oat_enabled_ = false;
171   }
172 };
173 
TEST_F(Dex2oatImageTest,TestModesAndFilters)174 TEST_F(Dex2oatImageTest, TestModesAndFilters) {
175   // This test crashes on the gtest-heap-poisoning configuration
176   // (AddressSanitizer + CMS/RosAlloc + heap-poisoning); see b/111061592.
177   // Temporarily disable this test on this configuration to keep
178   // our automated build/testing green while we work on a fix.
179   TEST_DISABLED_FOR_MEMORY_TOOL_WITH_HEAP_POISONING_WITHOUT_READ_BARRIERS();
180   if (kIsTargetBuild) {
181     // This test is too slow for target builds.
182     return;
183   }
184   // Compile only a subset of the libcore dex files to make this test shorter.
185   std::vector<std::string> libcore_dex_files = GetLibCoreDexFileNames();
186   // The primary image must contain at least core-oj and core-libart to initialize the runtime.
187   ASSERT_NE(std::string::npos, libcore_dex_files[0].find("core-oj"));
188   ASSERT_NE(std::string::npos, libcore_dex_files[1].find("core-libart"));
189   ArrayRef<const std::string> dex_files =
190       ArrayRef<const std::string>(libcore_dex_files).SubArray(/*pos=*/ 0u, /*length=*/ 2u);
191 
192   ImageSizes base_sizes = CompileImageAndGetSizes(dex_files, {});
193   ImageSizes everything_sizes;
194   ImageSizes filter_sizes;
195   std::cout << "Base compile sizes " << base_sizes << std::endl;
196   // Compile all methods and classes
197   std::vector<std::string> libcore_dexes = GetLibCoreDexFileNames();
198   ArrayRef<const std::string> libcore_dexes_array(libcore_dexes);
199   {
200     ScratchFile profile_file;
201     GenerateBootProfile(libcore_dexes_array,
202                         profile_file.GetFile(),
203                         /*method_frequency=*/ 1u,
204                         /*type_frequency=*/ 1u);
205     everything_sizes = CompileImageAndGetSizes(
206         dex_files,
207         {"--profile-file=" + profile_file.GetFilename(),
208          "--compiler-filter=speed-profile"});
209     profile_file.Close();
210     std::cout << "All methods and classes sizes " << everything_sizes << std::endl;
211     // Putting all classes as image classes should increase art size
212     EXPECT_GE(everything_sizes.art_size, base_sizes.art_size);
213     // Check that dex is the same size.
214     EXPECT_EQ(everything_sizes.vdex_size, base_sizes.vdex_size);
215   }
216   static size_t kMethodFrequency = 3;
217   static size_t kTypeFrequency = 4;
218   // Test compiling fewer methods and classes.
219   {
220     ScratchFile profile_file;
221     GenerateBootProfile(libcore_dexes_array,
222                         profile_file.GetFile(),
223                         kMethodFrequency,
224                         kTypeFrequency);
225     filter_sizes = CompileImageAndGetSizes(
226         dex_files,
227         {"--profile-file=" + profile_file.GetFilename(),
228          "--compiler-filter=speed-profile"});
229     profile_file.Close();
230     std::cout << "Fewer methods and classes sizes " << filter_sizes << std::endl;
231     EXPECT_LE(filter_sizes.art_size, everything_sizes.art_size);
232     EXPECT_LE(filter_sizes.oat_size, everything_sizes.oat_size);
233     EXPECT_LE(filter_sizes.vdex_size, everything_sizes.vdex_size);
234   }
235   // Test dirty image objects.
236   {
237     ScratchFile classes;
238     VisitDexes(libcore_dexes_array,
239                VoidFunctor(),
240                [&](TypeReference ref) {
241       WriteLine(classes.GetFile(), ref.dex_file->PrettyType(ref.TypeIndex()));
242     }, /*method_frequency=*/ 1u, /*class_frequency=*/ 1u);
243     ImageSizes image_classes_sizes = CompileImageAndGetSizes(
244         dex_files,
245         {"--dirty-image-objects=" + classes.GetFilename()});
246     classes.Close();
247     std::cout << "Dirty image object sizes " << image_classes_sizes << std::endl;
248   }
249   // Test multiple dirty image objects.
250   {
251     std::array<ScratchFile, 2> files;
252     int idx = 0;
253     VisitDexes(
254         libcore_dexes_array,
255         VoidFunctor(),
256         [&](TypeReference ref) {
257           WriteLine(files[idx].GetFile(), ref.dex_file->PrettyType(ref.TypeIndex()));
258           idx = (idx + 1) % files.size();
259         },
260         /*method_frequency=*/1u,
261         /*class_frequency=*/1u);
262     ImageSizes image_classes_sizes =
263         CompileImageAndGetSizes(dex_files,
264                                 {"--dirty-image-objects=" + files[0].GetFilename(),
265                                  "--dirty-image-objects=" + files[1].GetFilename()});
266     for (ScratchFile& file : files) {
267       file.Close();
268     }
269     std::cout << "Dirty image object sizes " << image_classes_sizes << std::endl;
270   }
271 }
272 
TEST_F(Dex2oatImageTest,TestExtension)273 TEST_F(Dex2oatImageTest, TestExtension) {
274   // TODO(b/376621099): investigate LUCI failures (timeouts?) and re-enable this test.
275   // This is probably not related to riscv64 arch, but a combination of riscv64 and running
276   // on VM, but we don't use TEST_DISABLED_ON_VM to keep running it on other VM builders.
277   TEST_DISABLED_FOR_RISCV64();
278 
279   std::string error_msg;
280   MemMap reservation = ReserveCoreImageAddressSpace(&error_msg);
281   ASSERT_TRUE(reservation.IsValid()) << error_msg;
282 
283   ScratchDir scratch;
284   const std::string& scratch_dir = scratch.GetPath();
285   std::string image_dir = scratch_dir + GetInstructionSetString(kRuntimeISA);
286   int mkdir_result = mkdir(image_dir.c_str(), 0700);
287   ASSERT_EQ(0, mkdir_result);
288   std::string filename_prefix = image_dir + "/boot";
289 
290   // Copy the libcore dex files to a custom dir inside `scratch_dir` so that we do not
291   // accidentally load pre-compiled core images from their original directory based on BCP paths.
292   std::string jar_dir = scratch_dir + "jars";
293   mkdir_result = mkdir(jar_dir.c_str(), 0700);
294   ASSERT_EQ(0, mkdir_result);
295   jar_dir += '/';
296   std::vector<std::string> libcore_dex_files = GetLibCoreDexFileNames();
297   CopyDexFiles(jar_dir, &libcore_dex_files);
298 
299   ArrayRef<const std::string> full_bcp(libcore_dex_files);
300   size_t total_dex_files = full_bcp.size();
301   ASSERT_GE(total_dex_files, 4u);  // 2 for "head", 1 for "tail", at least one for "mid", see below.
302 
303   // The primary image must contain at least core-oj and core-libart to initialize the runtime.
304   ASSERT_NE(std::string::npos, full_bcp[0].find("core-oj"));
305   ASSERT_NE(std::string::npos, full_bcp[1].find("core-libart"));
306   ArrayRef<const std::string> head_dex_files = full_bcp.SubArray(/*pos=*/ 0u, /*length=*/ 2u);
307   // Middle part is everything else except for conscrypt.
308   ASSERT_NE(std::string::npos, full_bcp[full_bcp.size() - 1u].find("conscrypt"));
309   ArrayRef<const std::string> mid_bcp =
310       full_bcp.SubArray(/*pos=*/ 0u, /*length=*/ total_dex_files - 1u);
311   ArrayRef<const std::string> mid_dex_files = mid_bcp.SubArray(/*pos=*/ 2u);
312   // Tail is just the conscrypt.
313   ArrayRef<const std::string> tail_dex_files =
314       full_bcp.SubArray(/*pos=*/ total_dex_files - 1u, /*length=*/ 1u);
315 
316   // Prepare the "head", "mid" and "tail" names and locations.
317   std::string base_name = "boot.art";
318   std::string base_location = scratch_dir + base_name;
319   std::vector<std::string> expanded_mid = gc::space::ImageSpace::ExpandMultiImageLocations(
320       mid_dex_files.SubArray(/*pos=*/ 0u, /*length=*/ 1u),
321       base_location,
322       /*boot_image_extension=*/ true);
323   CHECK_EQ(1u, expanded_mid.size());
324   std::string mid_location = expanded_mid[0];
325   size_t mid_slash_pos = mid_location.rfind('/');
326   ASSERT_NE(std::string::npos, mid_slash_pos);
327   std::string mid_name = mid_location.substr(mid_slash_pos + 1u);
328   CHECK_EQ(1u, tail_dex_files.size());
329   std::vector<std::string> expanded_tail = gc::space::ImageSpace::ExpandMultiImageLocations(
330       tail_dex_files, base_location, /*boot_image_extension=*/ true);
331   CHECK_EQ(1u, expanded_tail.size());
332   std::string tail_location = expanded_tail[0];
333   size_t tail_slash_pos = tail_location.rfind('/');
334   ASSERT_NE(std::string::npos, tail_slash_pos);
335   std::string tail_name = tail_location.substr(tail_slash_pos + 1u);
336 
337   // Create profiles.
338   ScratchFile head_profile_file;
339   GenerateBootProfile(head_dex_files,
340                       head_profile_file.GetFile(),
341                       /*method_frequency=*/ 1u,
342                       /*type_frequency=*/ 1u);
343   const std::string& head_profile_filename = head_profile_file.GetFilename();
344   ScratchFile mid_profile_file;
345   GenerateBootProfile(mid_dex_files,
346                       mid_profile_file.GetFile(),
347                       /*method_frequency=*/ 5u,
348                       /*type_frequency=*/ 4u);
349   const std::string& mid_profile_filename = mid_profile_file.GetFilename();
350   ScratchFile tail_profile_file;
351   GenerateBootProfile(tail_dex_files,
352                       tail_profile_file.GetFile(),
353                       /*method_frequency=*/ 5u,
354                       /*type_frequency=*/ 4u);
355   const std::string& tail_profile_filename = tail_profile_file.GetFilename();
356 
357   // Compile the "head", i.e. the primary boot image.
358   std::vector<std::string> extra_args;
359   extra_args.push_back("--profile-file=" + head_profile_filename);
360   extra_args.push_back(android::base::StringPrintf("--base=0x%08x", kBaseAddress));
361   bool head_ok = CompileBootImage(extra_args, filename_prefix, head_dex_files, &error_msg);
362   ASSERT_TRUE(head_ok) << error_msg;
363 
364   // Compile the "mid", i.e. the first extension.
365   std::string mid_bcp_string = android::base::Join(mid_bcp, ':');
366   extra_args.clear();
367   extra_args.push_back("--profile-file=" + mid_profile_filename);
368   AddRuntimeArg(extra_args, "-Xbootclasspath:" + mid_bcp_string);
369   AddRuntimeArg(extra_args, "-Xbootclasspath-locations:" + mid_bcp_string);
370   extra_args.push_back("--boot-image=" + base_location);
371   bool mid_ok = CompileBootImage(extra_args, filename_prefix, mid_dex_files, &error_msg);
372   ASSERT_TRUE(mid_ok) << error_msg;
373 
374   // Try to compile the "tail" without specifying the "mid" extension. This shall fail.
375   extra_args.clear();
376   extra_args.push_back("--profile-file=" + tail_profile_filename);
377   std::string full_bcp_string = android::base::Join(full_bcp, ':');
378   AddRuntimeArg(extra_args, "-Xbootclasspath:" + full_bcp_string);
379   AddRuntimeArg(extra_args, "-Xbootclasspath-locations:" + full_bcp_string);
380   extra_args.push_back("--boot-image=" + base_location);
381   bool tail_ok = CompileBootImage(extra_args, filename_prefix, tail_dex_files, &error_msg);
382   ASSERT_FALSE(tail_ok) << error_msg;
383 
384   // Now compile the tail against both "head" and "mid".
385   CHECK(extra_args.back().starts_with("--boot-image="));
386   extra_args.back() = "--boot-image=" + base_location + ':' + mid_location;
387   tail_ok = CompileBootImage(extra_args, filename_prefix, tail_dex_files, &error_msg);
388   ASSERT_TRUE(tail_ok) << error_msg;
389 
390   // Prepare directory for the single-image test that squashes the "mid" and "tail".
391   std::string single_dir = scratch_dir + "single";
392   mkdir_result = mkdir(single_dir.c_str(), 0700);
393   ASSERT_EQ(0, mkdir_result);
394   single_dir += '/';
395   std::string single_image_dir = single_dir + GetInstructionSetString(kRuntimeISA);
396   mkdir_result = mkdir(single_image_dir.c_str(), 0700);
397   ASSERT_EQ(0, mkdir_result);
398   std::string single_filename_prefix = single_image_dir + "/boot";
399 
400   // The dex files for the single-image are everything not in the "head".
401   ArrayRef<const std::string> single_dex_files = full_bcp.SubArray(/*pos=*/ head_dex_files.size());
402 
403   // Create a smaller profile for the single-image test that squashes the "mid" and "tail".
404   ScratchFile single_profile_file;
405   GenerateBootProfile(single_dex_files,
406                       single_profile_file.GetFile(),
407                       /*method_frequency=*/ 5u,
408                       /*type_frequency=*/ 4u);
409   const std::string& single_profile_filename = single_profile_file.GetFilename();
410 
411   // Prepare the single image name and location.
412   CHECK_GE(single_dex_files.size(), 2u);
413   std::string single_base_location = single_dir + base_name;
414   std::vector<std::string> expanded_single = gc::space::ImageSpace::ExpandMultiImageLocations(
415       single_dex_files.SubArray(/*pos=*/ 0u, /*length=*/ 1u),
416       single_base_location,
417       /*boot_image_extension=*/ true);
418   CHECK_EQ(1u, expanded_single.size());
419   std::string single_location = expanded_single[0];
420   size_t single_slash_pos = single_location.rfind('/');
421   ASSERT_NE(std::string::npos, single_slash_pos);
422   std::string single_name = single_location.substr(single_slash_pos + 1u);
423   CHECK_EQ(single_name, mid_name);
424 
425   // Compile the single-image against the primary boot image.
426   extra_args.clear();
427   extra_args.push_back("--profile-file=" + single_profile_filename);
428   AddRuntimeArg(extra_args, "-Xbootclasspath:" + full_bcp_string);
429   AddRuntimeArg(extra_args, "-Xbootclasspath-locations:" + full_bcp_string);
430   extra_args.push_back("--boot-image=" + base_location);
431   extra_args.push_back("--single-image");
432   extra_args.push_back("--avoid-storing-invocation");  // For comparison below.
433   error_msg.clear();
434   bool single_ok =
435       CompileBootImage(extra_args, single_filename_prefix, single_dex_files, &error_msg);
436   ASSERT_TRUE(single_ok) << error_msg;
437 
438   reservation = MemMap::Invalid();  // Free the reserved memory for loading images.
439 
440   // Try to load the boot image with different image locations.
441   std::vector<std::string> boot_class_path = libcore_dex_files;
442   std::vector<std::unique_ptr<gc::space::ImageSpace>> boot_image_spaces;
443   bool relocate = false;
444   MemMap extra_reservation;
445   auto load = [&](const std::string& image_location) {
446     boot_image_spaces.clear();
447     extra_reservation = MemMap::Invalid();
448     ScopedObjectAccess soa(Thread::Current());
449     return gc::space::ImageSpace::LoadBootImage(
450         /*boot_class_path=*/boot_class_path,
451         /*boot_class_path_locations=*/libcore_dex_files,
452         /*boot_class_path_files=*/{},
453         /*boot_class_path_image_files=*/{},
454         /*boot_class_path_vdex_files=*/{},
455         /*boot_class_path_oat_files=*/{},
456         android::base::Split(image_location, ":"),
457         kRuntimeISA,
458         relocate,
459         /*executable=*/true,
460         /*extra_reservation_size=*/0u,
461         /*allow_in_memory_compilation=*/true,
462         Runtime::GetApexVersions(ArrayRef<const std::string>(libcore_dex_files)),
463         &boot_image_spaces,
464         &extra_reservation);
465   };
466   auto silent_load = [&](const std::string& image_location) {
467     ScopedLogSeverity quiet(LogSeverity::FATAL);
468     return load(image_location);
469   };
470 
471   for (bool r : { false, true }) {
472     relocate = r;
473 
474     // Load primary image with full path.
475     bool load_ok = load(base_location);
476     ASSERT_TRUE(load_ok) << error_msg;
477     ASSERT_FALSE(extra_reservation.IsValid());
478     ASSERT_EQ(head_dex_files.size(), boot_image_spaces.size());
479 
480     // Fail to load primary image with just the name.
481     load_ok = silent_load(base_name);
482     ASSERT_FALSE(load_ok);
483 
484     // Fail to load primary image with a search path.
485     load_ok = silent_load("*");
486     ASSERT_FALSE(load_ok);
487     load_ok = silent_load(scratch_dir + "*");
488     ASSERT_FALSE(load_ok);
489 
490     // Load the primary and first extension with full path.
491     load_ok = load(ART_FORMAT("{}:{}", base_location, mid_location));
492     ASSERT_TRUE(load_ok) << error_msg;
493     ASSERT_EQ(mid_bcp.size(), boot_image_spaces.size());
494 
495     // Load the primary with full path and fail to load first extension without full path.
496     load_ok = load(ART_FORMAT("{}:{}", base_location, mid_name));
497     ASSERT_TRUE(load_ok) << error_msg;  // Primary image loaded successfully.
498     ASSERT_EQ(head_dex_files.size(), boot_image_spaces.size());  // But only the primary image.
499 
500     // Load all the libcore images with full paths.
501     load_ok = load(ART_FORMAT("{}:{}:{}", base_location, mid_location, tail_location));
502     ASSERT_TRUE(load_ok) << error_msg;
503     ASSERT_EQ(full_bcp.size(), boot_image_spaces.size());
504 
505     // Load the primary and first extension with full paths, fail to load second extension by name.
506     load_ok = load(ART_FORMAT("{}:{}:{}", base_location, mid_location, tail_name));
507     ASSERT_TRUE(load_ok) << error_msg;
508     ASSERT_EQ(mid_bcp.size(), boot_image_spaces.size());
509 
510     // Load the primary with full path and fail to load first extension without full path,
511     // fail to load second extension because it depends on the first.
512     load_ok = load(ART_FORMAT("{}:{}:{}", base_location, mid_name, tail_location));
513     ASSERT_TRUE(load_ok) << error_msg;  // Primary image loaded successfully.
514     ASSERT_EQ(head_dex_files.size(), boot_image_spaces.size());  // But only the primary image.
515 
516     // Load the primary with full path and extensions with a specified search path.
517     load_ok = load(ART_FORMAT("{}:{}*", base_location, scratch_dir));
518     ASSERT_TRUE(load_ok) << error_msg;
519     ASSERT_EQ(full_bcp.size(), boot_image_spaces.size());
520 
521     // Load the primary with full path and fail to find extensions in BCP path.
522     load_ok = load(base_location + ":*");
523     ASSERT_TRUE(load_ok) << error_msg;
524     ASSERT_EQ(head_dex_files.size(), boot_image_spaces.size());
525   }
526 
527   // Now copy the libcore dex files to the `scratch_dir` and retry loading the boot image
528   // with BCP in the scratch_dir so that the images can be found based on BCP paths.
529   CopyDexFiles(scratch_dir, &boot_class_path);
530 
531   for (bool r : { false, true }) {
532     relocate = r;
533 
534     // Loading the primary image with just the name now succeeds.
535     bool load_ok = load(base_name);
536     ASSERT_TRUE(load_ok) << error_msg;
537     ASSERT_EQ(head_dex_files.size(), boot_image_spaces.size());
538 
539     // Loading the primary image with a search path still fails.
540     load_ok = silent_load("*");
541     ASSERT_FALSE(load_ok);
542     load_ok = silent_load(scratch_dir + "*");
543     ASSERT_FALSE(load_ok);
544 
545     // Load the primary and first extension without paths.
546     load_ok = load(ART_FORMAT("{}:{}", base_name, mid_name));
547     ASSERT_TRUE(load_ok) << error_msg;
548     ASSERT_EQ(mid_bcp.size(), boot_image_spaces.size());
549 
550     // Load the primary without path and first extension with path.
551     load_ok = load(ART_FORMAT("{}:{}", base_name, mid_location));
552     ASSERT_TRUE(load_ok) << error_msg;
553     ASSERT_EQ(mid_bcp.size(), boot_image_spaces.size());
554 
555     // Load the primary with full path and the first extension without full path.
556     load_ok = load(ART_FORMAT("{}:{}", base_location, mid_name));
557     ASSERT_TRUE(load_ok) << error_msg;  // Loaded successfully.
558     ASSERT_EQ(mid_bcp.size(), boot_image_spaces.size());  // Including the extension.
559 
560     // Load all the libcore images without paths.
561     load_ok = load(ART_FORMAT("{}:{}:{}", base_name, mid_name, tail_name));
562     ASSERT_TRUE(load_ok) << error_msg;
563     ASSERT_EQ(full_bcp.size(), boot_image_spaces.size());
564 
565     // Load the primary and first extension with full paths and second extension by name.
566     load_ok = load(ART_FORMAT("{}:{}:{}", base_location, mid_location, tail_name));
567     ASSERT_TRUE(load_ok) << error_msg;
568     ASSERT_EQ(full_bcp.size(), boot_image_spaces.size());
569 
570     // Load the primary with full path, first extension without path,
571     // and second extension with full path.
572     load_ok = load(ART_FORMAT("{}:{}:{}", base_location, mid_name, tail_location));
573     ASSERT_TRUE(load_ok) << error_msg;  // Loaded successfully.
574     ASSERT_EQ(full_bcp.size(), boot_image_spaces.size());  // Including both extensions.
575 
576     // Load the primary with full path and find both extensions in BCP path.
577     load_ok = load(base_location + ":*");
578     ASSERT_TRUE(load_ok) << error_msg;
579     ASSERT_EQ(full_bcp.size(), boot_image_spaces.size());
580 
581     // Fail to load any images with invalid image locations (named component after search paths).
582     load_ok = silent_load(ART_FORMAT("{}:*:{}", base_location, tail_location));
583     ASSERT_FALSE(load_ok);
584     load_ok = silent_load(ART_FORMAT("{}:{}*:{}", base_location, scratch_dir, tail_location));
585     ASSERT_FALSE(load_ok);
586 
587     // Load the primary and single-image extension with full path.
588     load_ok = load(ART_FORMAT("{}:{}", base_location, single_location));
589     ASSERT_TRUE(load_ok) << error_msg;
590     ASSERT_EQ(head_dex_files.size() + 1u, boot_image_spaces.size());
591 
592     // Load the primary with full path and single-image extension with a specified search path.
593     load_ok = load(ART_FORMAT("{}:{}*", base_location, single_dir));
594     ASSERT_TRUE(load_ok) << error_msg;
595     ASSERT_EQ(head_dex_files.size() + 1u, boot_image_spaces.size());
596   }
597 
598   // Recompile the single-image extension using file descriptors and compare contents.
599   std::vector<std::string> expanded_single_filename_prefix =
600       gc::space::ImageSpace::ExpandMultiImageLocations(
601           single_dex_files.SubArray(/*pos=*/ 0u, /*length=*/ 1u),
602           single_filename_prefix,
603           /*boot_image_extension=*/ true);
604   CHECK_EQ(1u, expanded_single_filename_prefix.size());
605   std::string single_ext_prefix = expanded_single_filename_prefix[0];
606   std::string single_ext_prefix2 = single_ext_prefix + "2";
607   error_msg.clear();
608   single_ok = CompileBootImage(extra_args,
609                                single_filename_prefix,
610                                single_dex_files,
611                                &error_msg,
612                                /*use_fd_prefix=*/ single_ext_prefix2);
613   ASSERT_TRUE(single_ok) << error_msg;
614   EXPECT_TRUE(CompareFiles(single_ext_prefix + ".art", single_ext_prefix2 + ".art"));
615   EXPECT_TRUE(CompareFiles(single_ext_prefix + ".vdex", single_ext_prefix2 + ".vdex"));
616   EXPECT_TRUE(CompareFiles(single_ext_prefix + ".oat", single_ext_prefix2 + ".oat"));
617 
618   // Test parsing profile specification and creating the boot image extension on-the-fly.
619   // We must set --android-root in the image compiler options.
620   AddAndroidRootToImageCompilerOptions();
621   for (bool r : { false, true }) {
622     relocate = r;
623 
624     // Load primary boot image with a profile name.
625     bool load_ok = silent_load(ART_FORMAT("{}!{}", base_location, single_profile_filename));
626     ASSERT_TRUE(load_ok);
627 
628     // Try and fail to load with invalid spec, two profile name separators.
629     load_ok =
630         silent_load(ART_FORMAT("{}:{}!!arbitrary-profile-name", base_location, single_location));
631     ASSERT_FALSE(load_ok);
632 
633     // Try and fail to load with invalid spec, missing profile name.
634     load_ok = silent_load(ART_FORMAT("{}:{}!", base_location, single_location));
635     ASSERT_FALSE(load_ok);
636 
637     // Try and fail to load with invalid spec, missing component name.
638     load_ok = silent_load(ART_FORMAT("{}:!{}", base_location, single_profile_filename));
639     ASSERT_FALSE(load_ok);
640 
641     // Load primary boot image, specifying invalid extension component and profile name.
642     load_ok = load(
643         ART_FORMAT("{}:/non-existent/{}!non-existent-profile-name", base_location, single_name));
644     ASSERT_TRUE(load_ok) << error_msg;
645     ASSERT_EQ(head_dex_files.size(), boot_image_spaces.size());
646 
647     // Load primary boot image and the single extension, specifying invalid profile name.
648     // (Load extension from file.)
649     load_ok = load(ART_FORMAT("{}:{}!non-existent-profile-name", base_location, single_location));
650     ASSERT_TRUE(load_ok) << error_msg;
651     ASSERT_EQ(head_dex_files.size() + 1u, boot_image_spaces.size());
652     ASSERT_EQ(single_dex_files.size(),
653               boot_image_spaces.back()->GetImageHeader().GetComponentCount());
654 
655     // Load primary boot image and fail to load the single extension, specifying
656     // invalid extension component name but a valid profile file.
657     // (Running dex2oat to compile extension is disabled.)
658     ASSERT_FALSE(Runtime::Current()->IsImageDex2OatEnabled());
659     load_ok = load(
660         ART_FORMAT("{}:/non-existent/{}!{}", base_location, single_name, single_profile_filename));
661     ASSERT_TRUE(load_ok) << error_msg;
662     ASSERT_EQ(head_dex_files.size(), boot_image_spaces.size());
663 
664     EnableImageDex2Oat();
665 
666     // Load primary boot image and the single extension, specifying invalid extension
667     // component name but a valid profile file. (Compile extension by running dex2oat.)
668     load_ok = load(
669         ART_FORMAT("{}:/non-existent/{}!{}", base_location, single_name, single_profile_filename));
670     ASSERT_TRUE(load_ok) << error_msg;
671     ASSERT_EQ(head_dex_files.size() + 1u, boot_image_spaces.size());
672     ASSERT_EQ(single_dex_files.size(),
673               boot_image_spaces.back()->GetImageHeader().GetComponentCount());
674 
675     // Load primary boot image and two extensions, specifying invalid extension component
676     // names but valid profile files. (Compile extensions by running dex2oat.)
677     load_ok = load(ART_FORMAT("{}:/non-existent/{}!{}:/non-existent/{}!{}",
678                               base_location,
679                               mid_name,
680                               mid_profile_filename,
681                               tail_name,
682                               tail_profile_filename));
683     ASSERT_TRUE(load_ok) << error_msg;
684     ASSERT_EQ(head_dex_files.size() + 2u, boot_image_spaces.size());
685     ASSERT_EQ(mid_dex_files.size(),
686               boot_image_spaces[head_dex_files.size()]->GetImageHeader().GetComponentCount());
687     ASSERT_EQ(tail_dex_files.size(),
688               boot_image_spaces[head_dex_files.size() + 1u]->GetImageHeader().GetComponentCount());
689 
690     // Load primary boot image and fail to load extensions, specifying invalid component
691     // names but valid profile file only for the second one. As we fail to load the first
692     // extension, the second extension has a missing dependency and cannot be compiled.
693     load_ok = load(ART_FORMAT("{}:/non-existent/{}:/non-existent/{}!{}",
694                               base_location,
695                               mid_name,
696                               tail_name,
697                               tail_profile_filename));
698     ASSERT_TRUE(load_ok) << error_msg;
699     ASSERT_EQ(head_dex_files.size(), boot_image_spaces.size());
700 
701     DisableImageDex2Oat();
702   }
703 }
704 
705 }  // namespace art
706