blob: 2978fb40928de4a0fab0b66616daf408b2252fee [file] [log] [blame]
Mathieu Chartierf70fe3d2017-06-21 15:24:02 -07001/*
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
Vladimir Marko21910692019-11-06 13:27:03 +000017#include <fstream>
Igor Murashkin5573c372017-11-16 13:34:30 -080018#include <regex>
Mathieu Chartierf70fe3d2017-06-21 15:24:02 -070019#include <sstream>
20#include <string>
21#include <vector>
22
Vladimir Marko21910692019-11-06 13:27:03 +000023#include <sys/mman.h>
Mathieu Chartierf70fe3d2017-06-21 15:24:02 -070024#include <sys/wait.h>
25#include <unistd.h>
26
Andreas Gampe57943812017-12-06 21:39:13 -080027#include <android-base/logging.h>
Vladimir Marko21910692019-11-06 13:27:03 +000028#include <android-base/stringprintf.h>
29#include <android-base/strings.h>
Andreas Gampe57943812017-12-06 21:39:13 -080030
Mathieu Chartierf70fe3d2017-06-21 15:24:02 -070031#include "common_runtime_test.h"
32
Vladimir Marko21910692019-11-06 13:27:03 +000033#include "base/array_ref.h"
David Sehr891a50e2017-10-27 17:01:07 -070034#include "base/file_utils.h"
Mathieu Chartierf70fe3d2017-06-21 15:24:02 -070035#include "base/macros.h"
Vladimir Marko21910692019-11-06 13:27:03 +000036#include "base/mem_map.h"
37#include "base/string_view_cpp20.h"
Mathieu Chartierf70fe3d2017-06-21 15:24:02 -070038#include "base/unix_file/fd_file.h"
David Sehrc431b9d2018-03-02 12:01:51 -080039#include "base/utils.h"
David Sehr013fd802018-01-11 22:55:24 -080040#include "dex/art_dex_file_loader.h"
David Sehr9e734c72018-01-04 17:56:19 -080041#include "dex/dex_file-inl.h"
42#include "dex/dex_file_loader.h"
David Sehr312f3b22018-03-19 08:39:26 -070043#include "dex/method_reference.h"
Vladimir Markofdd46842020-03-25 14:57:17 +000044#include "dex/type_reference.h"
Vladimir Marko21910692019-11-06 13:27:03 +000045#include "gc/space/image_space.h"
Mathieu Chartierf70fe3d2017-06-21 15:24:02 -070046#include "runtime.h"
Vladimir Marko21910692019-11-06 13:27:03 +000047#include "scoped_thread_state_change-inl.h"
48#include "thread-current-inl.h"
Mathieu Chartierf70fe3d2017-06-21 15:24:02 -070049
50namespace art {
51
Vladimir Marko21910692019-11-06 13:27:03 +000052// A suitable address for loading the core images.
53constexpr uint32_t kBaseAddress = ART_BASE_ADDRESS;
54
Mathieu Chartierf70fe3d2017-06-21 15:24:02 -070055struct ImageSizes {
56 size_t art_size = 0;
57 size_t oat_size = 0;
58 size_t vdex_size = 0;
59};
60
61std::ostream& operator<<(std::ostream& os, const ImageSizes& sizes) {
62 os << "art=" << sizes.art_size << " oat=" << sizes.oat_size << " vdex=" << sizes.vdex_size;
63 return os;
64}
65
66class Dex2oatImageTest : public CommonRuntimeTest {
67 public:
Roland Levillainf73caca2018-08-24 17:19:07 +010068 void TearDown() override {}
Mathieu Chartierf70fe3d2017-06-21 15:24:02 -070069
70 protected:
Vladimir Markoa8f39182019-11-21 10:08:58 +000071 void SetUpRuntimeOptions(RuntimeOptions* options) override {
72 // Disable implicit dex2oat invocations when loading image spaces.
73 options->emplace_back("-Xnoimage-dex2oat", nullptr);
74 }
75
Mathieu Chartierf70fe3d2017-06-21 15:24:02 -070076 static void WriteLine(File* file, std::string line) {
77 line += '\n';
78 EXPECT_TRUE(file->WriteFully(&line[0], line.length()));
79 }
80
Mathieu Chartierf70fe3d2017-06-21 15:24:02 -070081 void AddRuntimeArg(std::vector<std::string>& args, const std::string& arg) {
82 args.push_back("--runtime-arg");
83 args.push_back(arg);
84 }
85
Vladimir Markoa8f39182019-11-21 10:08:58 +000086 ImageSizes CompileImageAndGetSizes(ArrayRef<const std::string> dex_files,
87 const std::vector<std::string>& extra_args) {
Mathieu Chartierf70fe3d2017-06-21 15:24:02 -070088 ImageSizes ret;
David Srbeckycf0c6ef2020-02-05 16:25:36 +000089 ScratchDir scratch;
90 std::string filename_prefix = scratch.GetPath() + "boot";
Vladimir Marko21910692019-11-06 13:27:03 +000091 std::vector<std::string> local_extra_args = extra_args;
92 local_extra_args.push_back(android::base::StringPrintf("--base=0x%08x", kBaseAddress));
Mathieu Chartierf70fe3d2017-06-21 15:24:02 -070093 std::string error_msg;
David Srbeckycf0c6ef2020-02-05 16:25:36 +000094 if (!CompileBootImage(local_extra_args, filename_prefix, dex_files, &error_msg)) {
95 LOG(ERROR) << "Failed to compile image " << filename_prefix << error_msg;
Mathieu Chartierf70fe3d2017-06-21 15:24:02 -070096 }
David Srbeckycf0c6ef2020-02-05 16:25:36 +000097 std::string art_file = filename_prefix + ".art";
98 std::string oat_file = filename_prefix + ".oat";
99 std::string vdex_file = filename_prefix + ".vdex";
Vladimir Markoa5785a22018-03-08 14:25:47 +0000100 int64_t art_size = OS::GetFileSizeBytes(art_file.c_str());
101 int64_t oat_size = OS::GetFileSizeBytes(oat_file.c_str());
102 int64_t vdex_size = OS::GetFileSizeBytes(vdex_file.c_str());
103 CHECK_GT(art_size, 0u) << art_file;
104 CHECK_GT(oat_size, 0u) << oat_file;
105 CHECK_GT(vdex_size, 0u) << vdex_file;
106 ret.art_size = art_size;
107 ret.oat_size = oat_size;
108 ret.vdex_size = vdex_size;
Mathieu Chartierf70fe3d2017-06-21 15:24:02 -0700109 return ret;
110 }
111
Vladimir Markoa8f39182019-11-21 10:08:58 +0000112 MemMap ReserveCoreImageAddressSpace(/*out*/std::string* error_msg) {
113 constexpr size_t kReservationSize = 256 * MB; // This should be enough for the compiled images.
114 // Extend to both directions for maximum relocation difference.
115 static_assert(ART_BASE_ADDRESS_MIN_DELTA < 0);
116 static_assert(ART_BASE_ADDRESS_MAX_DELTA > 0);
117 static_assert(IsAligned<kPageSize>(ART_BASE_ADDRESS_MIN_DELTA));
118 static_assert(IsAligned<kPageSize>(ART_BASE_ADDRESS_MAX_DELTA));
119 constexpr size_t kExtra = ART_BASE_ADDRESS_MAX_DELTA - ART_BASE_ADDRESS_MIN_DELTA;
120 uint32_t min_relocated_address = kBaseAddress + ART_BASE_ADDRESS_MIN_DELTA;
121 return MemMap::MapAnonymous("Reservation",
122 reinterpret_cast<uint8_t*>(min_relocated_address),
123 kReservationSize + kExtra,
124 PROT_NONE,
125 /*low_4gb=*/ true,
126 /*reuse=*/ false,
127 /*reservation=*/ nullptr,
128 error_msg);
129 }
130
131 void CopyDexFiles(const std::string& dir, /*inout*/std::vector<std::string>* dex_files) {
132 CHECK(EndsWith(dir, "/"));
133 for (std::string& dex_file : *dex_files) {
134 size_t slash_pos = dex_file.rfind('/');
David Srbeckycf0c6ef2020-02-05 16:25:36 +0000135 CHECK(OS::FileExists(dex_file.c_str())) << dex_file;
Vladimir Markoa8f39182019-11-21 10:08:58 +0000136 CHECK_NE(std::string::npos, slash_pos);
137 std::string new_location = dir + dex_file.substr(slash_pos + 1u);
138 std::ifstream src_stream(dex_file, std::ios::binary);
139 std::ofstream dst_stream(new_location, std::ios::binary);
140 dst_stream << src_stream.rdbuf();
141 dex_file = new_location;
142 }
143 }
Vladimir Markod0036ac2019-11-21 11:47:12 +0000144
145 bool CompareFiles(const std::string& filename1, const std::string& filename2) {
146 std::unique_ptr<File> file1(OS::OpenFileForReading(filename1.c_str()));
147 std::unique_ptr<File> file2(OS::OpenFileForReading(filename2.c_str()));
148 // Did we open the files?
149 if (file1 == nullptr || file2 == nullptr) {
150 return false;
151 }
152 // Are they non-empty and the same length?
153 if (file1->GetLength() <= 0 || file2->GetLength() != file1->GetLength()) {
154 return false;
155 }
156 return file1->Compare(file2.get()) == 0;
157 }
Vladimir Markoc0e0e5e2020-01-23 17:43:05 +0000158
159 void AddAndroidRootToImageCompilerOptions() {
160 const char* android_root = getenv("ANDROID_ROOT");
161 CHECK(android_root != nullptr);
162 Runtime::Current()->image_compiler_options_.push_back(
163 "--android-root=" + std::string(android_root));
164 }
165
166 void EnableImageDex2Oat() {
167 Runtime::Current()->image_dex2oat_enabled_ = true;
168 }
169
170 void DisableImageDex2Oat() {
171 Runtime::Current()->image_dex2oat_enabled_ = false;
172 }
Mathieu Chartierf70fe3d2017-06-21 15:24:02 -0700173};
174
Vladimir Markoa5785a22018-03-08 14:25:47 +0000175TEST_F(Dex2oatImageTest, TestModesAndFilters) {
Roland Levillainf5dd1142018-07-03 13:29:18 +0100176 // This test crashes on the gtest-heap-poisoning configuration
177 // (AddressSanitizer + CMS/RosAlloc + heap-poisoning); see b/111061592.
178 // Temporarily disable this test on this configuration to keep
179 // our automated build/testing green while we work on a fix.
180 TEST_DISABLED_FOR_MEMORY_TOOL_WITH_HEAP_POISONING_WITHOUT_READ_BARRIERS();
Mathieu Chartierf70fe3d2017-06-21 15:24:02 -0700181 if (kIsTargetBuild) {
182 // This test is too slow for target builds.
183 return;
184 }
Vladimir Markoa8f39182019-11-21 10:08:58 +0000185 // Compile only a subset of the libcore dex files to make this test shorter.
186 std::vector<std::string> libcore_dex_files = GetLibCoreDexFileNames();
Vladimir Marko66210132020-05-05 14:29:30 +0100187 // The primary image must contain at least core-oj and core-libart to initialize the runtime.
Vladimir Markoa8f39182019-11-21 10:08:58 +0000188 ASSERT_NE(std::string::npos, libcore_dex_files[0].find("core-oj"));
189 ASSERT_NE(std::string::npos, libcore_dex_files[1].find("core-libart"));
Vladimir Markoa8f39182019-11-21 10:08:58 +0000190 ArrayRef<const std::string> dex_files =
Vladimir Marko66210132020-05-05 14:29:30 +0100191 ArrayRef<const std::string>(libcore_dex_files).SubArray(/*pos=*/ 0u, /*length=*/ 2u);
Vladimir Markoa8f39182019-11-21 10:08:58 +0000192
193 ImageSizes base_sizes = CompileImageAndGetSizes(dex_files, {});
Mathieu Chartierf68b6982019-06-27 09:40:20 -0700194 ImageSizes everything_sizes;
195 ImageSizes filter_sizes;
Mathieu Chartierf70fe3d2017-06-21 15:24:02 -0700196 std::cout << "Base compile sizes " << base_sizes << std::endl;
Mathieu Chartierf68b6982019-06-27 09:40:20 -0700197 // Compile all methods and classes
Vladimir Markoc0e0e5e2020-01-23 17:43:05 +0000198 std::vector<std::string> libcore_dexes = GetLibCoreDexFileNames();
199 ArrayRef<const std::string> libcore_dexes_array(libcore_dexes);
Mathieu Chartierf70fe3d2017-06-21 15:24:02 -0700200 {
Mathieu Chartierf68b6982019-06-27 09:40:20 -0700201 ScratchFile profile_file;
Vladimir Markoa2209802021-04-23 13:28:29 +0000202 GenerateBootProfile(libcore_dexes_array,
203 profile_file.GetFile(),
204 /*method_frequency=*/ 1u,
205 /*type_frequency=*/ 1u);
Mathieu Chartierf68b6982019-06-27 09:40:20 -0700206 everything_sizes = CompileImageAndGetSizes(
Vladimir Markoa8f39182019-11-21 10:08:58 +0000207 dex_files,
Mathieu Chartierf68b6982019-06-27 09:40:20 -0700208 {"--profile-file=" + profile_file.GetFilename(),
209 "--compiler-filter=speed-profile"});
210 profile_file.Close();
211 std::cout << "All methods and classes sizes " << everything_sizes << std::endl;
Mathieu Chartierf70fe3d2017-06-21 15:24:02 -0700212 // Putting all classes as image classes should increase art size
Mathieu Chartierf68b6982019-06-27 09:40:20 -0700213 EXPECT_GE(everything_sizes.art_size, base_sizes.art_size);
Ian Pedowitz14464672020-07-24 09:40:26 -0700214 // Check that dex is the same size.
Mathieu Chartierf68b6982019-06-27 09:40:20 -0700215 EXPECT_EQ(everything_sizes.vdex_size, base_sizes.vdex_size);
Mathieu Chartierf70fe3d2017-06-21 15:24:02 -0700216 }
Mathieu Chartierf70fe3d2017-06-21 15:24:02 -0700217 static size_t kMethodFrequency = 3;
218 static size_t kTypeFrequency = 4;
219 // Test compiling fewer methods and classes.
220 {
Mathieu Chartierf70fe3d2017-06-21 15:24:02 -0700221 ScratchFile profile_file;
Vladimir Markoa2209802021-04-23 13:28:29 +0000222 GenerateBootProfile(libcore_dexes_array,
223 profile_file.GetFile(),
224 kMethodFrequency,
225 kTypeFrequency);
Mathieu Chartierf68b6982019-06-27 09:40:20 -0700226 filter_sizes = CompileImageAndGetSizes(
Vladimir Markoa8f39182019-11-21 10:08:58 +0000227 dex_files,
Mathieu Chartierf70fe3d2017-06-21 15:24:02 -0700228 {"--profile-file=" + profile_file.GetFilename(),
229 "--compiler-filter=speed-profile"});
230 profile_file.Close();
Mathieu Chartierf68b6982019-06-27 09:40:20 -0700231 std::cout << "Fewer methods and classes sizes " << filter_sizes << std::endl;
232 EXPECT_LE(filter_sizes.art_size, everything_sizes.art_size);
233 EXPECT_LE(filter_sizes.oat_size, everything_sizes.oat_size);
234 EXPECT_LE(filter_sizes.vdex_size, everything_sizes.vdex_size);
Mathieu Chartierf70fe3d2017-06-21 15:24:02 -0700235 }
Jeff Haoc23b0c02017-07-27 18:19:38 -0700236 // Test dirty image objects.
237 {
238 ScratchFile classes;
Vladimir Markofdd46842020-03-25 14:57:17 +0000239 VisitDexes(libcore_dexes_array,
240 VoidFunctor(),
241 [&](TypeReference ref) {
Mathieu Chartierf68b6982019-06-27 09:40:20 -0700242 WriteLine(classes.GetFile(), ref.dex_file->PrettyType(ref.TypeIndex()));
243 }, /*method_frequency=*/ 1u, /*class_frequency=*/ 1u);
244 ImageSizes image_classes_sizes = CompileImageAndGetSizes(
Vladimir Markoa8f39182019-11-21 10:08:58 +0000245 dex_files,
Jeff Haoc23b0c02017-07-27 18:19:38 -0700246 {"--dirty-image-objects=" + classes.GetFilename()});
247 classes.Close();
248 std::cout << "Dirty image object sizes " << image_classes_sizes << std::endl;
249 }
Mathieu Chartierf70fe3d2017-06-21 15:24:02 -0700250}
251
Vladimir Marko21910692019-11-06 13:27:03 +0000252TEST_F(Dex2oatImageTest, TestExtension) {
Vladimir Marko21910692019-11-06 13:27:03 +0000253 std::string error_msg;
Vladimir Markoa8f39182019-11-21 10:08:58 +0000254 MemMap reservation = ReserveCoreImageAddressSpace(&error_msg);
255 ASSERT_TRUE(reservation.IsValid()) << error_msg;
Vladimir Marko21910692019-11-06 13:27:03 +0000256
David Srbeckycf0c6ef2020-02-05 16:25:36 +0000257 ScratchDir scratch;
258 const std::string& scratch_dir = scratch.GetPath();
Vladimir Marko21910692019-11-06 13:27:03 +0000259 std::string image_dir = scratch_dir + GetInstructionSetString(kRuntimeISA);
David Srbeckycf0c6ef2020-02-05 16:25:36 +0000260 int mkdir_result = mkdir(image_dir.c_str(), 0700);
Vladimir Marko21910692019-11-06 13:27:03 +0000261 ASSERT_EQ(0, mkdir_result);
David Srbecky6355d692020-03-26 14:10:26 +0000262 std::string filename_prefix = image_dir + "/boot";
Vladimir Marko21910692019-11-06 13:27:03 +0000263
264 // Copy the libcore dex files to a custom dir inside `scratch_dir` so that we do not
265 // accidentally load pre-compiled core images from their original directory based on BCP paths.
266 std::string jar_dir = scratch_dir + "jars";
267 mkdir_result = mkdir(jar_dir.c_str(), 0700);
268 ASSERT_EQ(0, mkdir_result);
269 jar_dir += '/';
270 std::vector<std::string> libcore_dex_files = GetLibCoreDexFileNames();
Vladimir Markoa8f39182019-11-21 10:08:58 +0000271 CopyDexFiles(jar_dir, &libcore_dex_files);
Vladimir Marko21910692019-11-06 13:27:03 +0000272
273 ArrayRef<const std::string> full_bcp(libcore_dex_files);
274 size_t total_dex_files = full_bcp.size();
Vladimir Marko66210132020-05-05 14:29:30 +0100275 ASSERT_GE(total_dex_files, 4u); // 2 for "head", 1 for "tail", at least one for "mid", see below.
Vladimir Marko21910692019-11-06 13:27:03 +0000276
Vladimir Marko66210132020-05-05 14:29:30 +0100277 // The primary image must contain at least core-oj and core-libart to initialize the runtime.
Vladimir Marko21910692019-11-06 13:27:03 +0000278 ASSERT_NE(std::string::npos, full_bcp[0].find("core-oj"));
279 ASSERT_NE(std::string::npos, full_bcp[1].find("core-libart"));
Vladimir Marko66210132020-05-05 14:29:30 +0100280 ArrayRef<const std::string> head_dex_files = full_bcp.SubArray(/*pos=*/ 0u, /*length=*/ 2u);
Vladimir Marko21910692019-11-06 13:27:03 +0000281 // Middle part is everything else except for conscrypt.
282 ASSERT_NE(std::string::npos, full_bcp[full_bcp.size() - 1u].find("conscrypt"));
283 ArrayRef<const std::string> mid_bcp =
284 full_bcp.SubArray(/*pos=*/ 0u, /*length=*/ total_dex_files - 1u);
Vladimir Marko66210132020-05-05 14:29:30 +0100285 ArrayRef<const std::string> mid_dex_files = mid_bcp.SubArray(/*pos=*/ 2u);
Vladimir Marko21910692019-11-06 13:27:03 +0000286 // Tail is just the conscrypt.
287 ArrayRef<const std::string> tail_dex_files =
288 full_bcp.SubArray(/*pos=*/ total_dex_files - 1u, /*length=*/ 1u);
289
290 // Prepare the "head", "mid" and "tail" names and locations.
David Srbecky6355d692020-03-26 14:10:26 +0000291 std::string base_name = "boot.art";
Vladimir Marko21910692019-11-06 13:27:03 +0000292 std::string base_location = scratch_dir + base_name;
293 std::vector<std::string> expanded_mid = gc::space::ImageSpace::ExpandMultiImageLocations(
294 mid_dex_files.SubArray(/*pos=*/ 0u, /*length=*/ 1u),
295 base_location,
296 /*boot_image_extension=*/ true);
297 CHECK_EQ(1u, expanded_mid.size());
298 std::string mid_location = expanded_mid[0];
299 size_t mid_slash_pos = mid_location.rfind('/');
300 ASSERT_NE(std::string::npos, mid_slash_pos);
301 std::string mid_name = mid_location.substr(mid_slash_pos + 1u);
302 CHECK_EQ(1u, tail_dex_files.size());
303 std::vector<std::string> expanded_tail = gc::space::ImageSpace::ExpandMultiImageLocations(
304 tail_dex_files, base_location, /*boot_image_extension=*/ true);
305 CHECK_EQ(1u, expanded_tail.size());
306 std::string tail_location = expanded_tail[0];
307 size_t tail_slash_pos = tail_location.rfind('/');
308 ASSERT_NE(std::string::npos, tail_slash_pos);
309 std::string tail_name = tail_location.substr(tail_slash_pos + 1u);
310
Vladimir Markoc0e0e5e2020-01-23 17:43:05 +0000311 // Create profiles.
312 ScratchFile head_profile_file;
Vladimir Markoa2209802021-04-23 13:28:29 +0000313 GenerateBootProfile(head_dex_files,
314 head_profile_file.GetFile(),
315 /*method_frequency=*/ 1u,
316 /*type_frequency=*/ 1u);
Vladimir Markoc0e0e5e2020-01-23 17:43:05 +0000317 const std::string& head_profile_filename = head_profile_file.GetFilename();
318 ScratchFile mid_profile_file;
Vladimir Markoa2209802021-04-23 13:28:29 +0000319 GenerateBootProfile(mid_dex_files,
320 mid_profile_file.GetFile(),
321 /*method_frequency=*/ 5u,
322 /*type_frequency=*/ 4u);
Vladimir Markoc0e0e5e2020-01-23 17:43:05 +0000323 const std::string& mid_profile_filename = mid_profile_file.GetFilename();
324 ScratchFile tail_profile_file;
Vladimir Markoa2209802021-04-23 13:28:29 +0000325 GenerateBootProfile(tail_dex_files,
326 tail_profile_file.GetFile(),
327 /*method_frequency=*/ 5u,
328 /*type_frequency=*/ 4u);
Vladimir Markoc0e0e5e2020-01-23 17:43:05 +0000329 const std::string& tail_profile_filename = tail_profile_file.GetFilename();
330
Vladimir Marko21910692019-11-06 13:27:03 +0000331 // Compile the "head", i.e. the primary boot image.
Vladimir Markoc0e0e5e2020-01-23 17:43:05 +0000332 std::vector<std::string> extra_args;
333 extra_args.push_back("--profile-file=" + head_profile_filename);
Vladimir Markoc526e422019-11-20 10:27:14 +0000334 extra_args.push_back(android::base::StringPrintf("--base=0x%08x", kBaseAddress));
335 bool head_ok = CompileBootImage(extra_args, filename_prefix, head_dex_files, &error_msg);
Vladimir Marko21910692019-11-06 13:27:03 +0000336 ASSERT_TRUE(head_ok) << error_msg;
337
338 // Compile the "mid", i.e. the first extension.
339 std::string mid_bcp_string = android::base::Join(mid_bcp, ':');
Vladimir Markoc0e0e5e2020-01-23 17:43:05 +0000340 extra_args.clear();
341 extra_args.push_back("--profile-file=" + mid_profile_filename);
Vladimir Marko21910692019-11-06 13:27:03 +0000342 AddRuntimeArg(extra_args, "-Xbootclasspath:" + mid_bcp_string);
343 AddRuntimeArg(extra_args, "-Xbootclasspath-locations:" + mid_bcp_string);
344 extra_args.push_back("--boot-image=" + base_location);
345 bool mid_ok = CompileBootImage(extra_args, filename_prefix, mid_dex_files, &error_msg);
346 ASSERT_TRUE(mid_ok) << error_msg;
347
348 // Try to compile the "tail" without specifying the "mid" extension. This shall fail.
Vladimir Markoc0e0e5e2020-01-23 17:43:05 +0000349 extra_args.clear();
350 extra_args.push_back("--profile-file=" + tail_profile_filename);
Vladimir Marko21910692019-11-06 13:27:03 +0000351 std::string full_bcp_string = android::base::Join(full_bcp, ':');
Vladimir Marko21910692019-11-06 13:27:03 +0000352 AddRuntimeArg(extra_args, "-Xbootclasspath:" + full_bcp_string);
353 AddRuntimeArg(extra_args, "-Xbootclasspath-locations:" + full_bcp_string);
354 extra_args.push_back("--boot-image=" + base_location);
355 bool tail_ok = CompileBootImage(extra_args, filename_prefix, tail_dex_files, &error_msg);
356 ASSERT_FALSE(tail_ok) << error_msg;
357
358 // Now compile the tail against both "head" and "mid".
359 CHECK(StartsWith(extra_args.back(), "--boot-image="));
360 extra_args.back() = "--boot-image=" + base_location + ':' + mid_location;
361 tail_ok = CompileBootImage(extra_args, filename_prefix, tail_dex_files, &error_msg);
362 ASSERT_TRUE(tail_ok) << error_msg;
363
Vladimir Marko3ea66272019-12-12 14:28:50 +0000364 // Prepare directory for the single-image test that squashes the "mid" and "tail".
365 std::string single_dir = scratch_dir + "single";
366 mkdir_result = mkdir(single_dir.c_str(), 0700);
367 ASSERT_EQ(0, mkdir_result);
368 single_dir += '/';
369 std::string single_image_dir = single_dir + GetInstructionSetString(kRuntimeISA);
370 mkdir_result = mkdir(single_image_dir.c_str(), 0700);
371 ASSERT_EQ(0, mkdir_result);
David Srbecky6355d692020-03-26 14:10:26 +0000372 std::string single_filename_prefix = single_image_dir + "/boot";
Vladimir Marko3ea66272019-12-12 14:28:50 +0000373
Vladimir Markoc0e0e5e2020-01-23 17:43:05 +0000374 // The dex files for the single-image are everything not in the "head".
375 ArrayRef<const std::string> single_dex_files = full_bcp.SubArray(/*pos=*/ head_dex_files.size());
376
Vladimir Marko3ea66272019-12-12 14:28:50 +0000377 // Create a smaller profile for the single-image test that squashes the "mid" and "tail".
378 ScratchFile single_profile_file;
Vladimir Markoa2209802021-04-23 13:28:29 +0000379 GenerateBootProfile(single_dex_files,
380 single_profile_file.GetFile(),
381 /*method_frequency=*/ 5u,
382 /*type_frequency=*/ 4u);
Vladimir Markoc0e0e5e2020-01-23 17:43:05 +0000383 const std::string& single_profile_filename = single_profile_file.GetFilename();
Vladimir Marko3ea66272019-12-12 14:28:50 +0000384
385 // Prepare the single image name and location.
386 CHECK_GE(single_dex_files.size(), 2u);
387 std::string single_base_location = single_dir + base_name;
388 std::vector<std::string> expanded_single = gc::space::ImageSpace::ExpandMultiImageLocations(
389 single_dex_files.SubArray(/*pos=*/ 0u, /*length=*/ 1u),
390 single_base_location,
391 /*boot_image_extension=*/ true);
392 CHECK_EQ(1u, expanded_single.size());
393 std::string single_location = expanded_single[0];
394 size_t single_slash_pos = single_location.rfind('/');
395 ASSERT_NE(std::string::npos, single_slash_pos);
396 std::string single_name = single_location.substr(single_slash_pos + 1u);
397 CHECK_EQ(single_name, mid_name);
398
399 // Compile the single-image against the primary boot image.
Vladimir Markoc0e0e5e2020-01-23 17:43:05 +0000400 extra_args.clear();
401 extra_args.push_back("--profile-file=" + single_profile_filename);
Vladimir Marko3ea66272019-12-12 14:28:50 +0000402 AddRuntimeArg(extra_args, "-Xbootclasspath:" + full_bcp_string);
403 AddRuntimeArg(extra_args, "-Xbootclasspath-locations:" + full_bcp_string);
404 extra_args.push_back("--boot-image=" + base_location);
405 extra_args.push_back("--single-image");
406 extra_args.push_back("--avoid-storing-invocation"); // For comparison below.
407 error_msg.clear();
408 bool single_ok =
409 CompileBootImage(extra_args, single_filename_prefix, single_dex_files, &error_msg);
410 ASSERT_TRUE(single_ok) << error_msg;
411
Vladimir Marko21910692019-11-06 13:27:03 +0000412 reservation = MemMap::Invalid(); // Free the reserved memory for loading images.
413
414 // Try to load the boot image with different image locations.
415 std::vector<std::string> boot_class_path = libcore_dex_files;
416 std::vector<std::unique_ptr<gc::space::ImageSpace>> boot_image_spaces;
417 bool relocate = false;
418 MemMap extra_reservation;
419 auto load = [&](const std::string& image_location) {
420 boot_image_spaces.clear();
421 extra_reservation = MemMap::Invalid();
422 ScopedObjectAccess soa(Thread::Current());
423 return gc::space::ImageSpace::LoadBootImage(/*boot_class_path=*/ boot_class_path,
424 /*boot_class_path_locations=*/ libcore_dex_files,
Victor Hsieha09d8b72021-05-24 14:21:55 -0700425 /*boot_class_path_fds=*/ std::vector<int>(),
Victor Hsiehce9b9022021-07-21 10:44:06 -0700426 /*boot_class_path_image_fds=*/ std::vector<int>(),
427 /*boot_class_path_vdex_fds=*/ std::vector<int>(),
428 /*boot_class_path_oat_fds=*/ std::vector<int>(),
Victor Hsieh61ffd042021-05-20 15:14:25 -0700429 android::base::Split(image_location, ":"),
Vladimir Marko21910692019-11-06 13:27:03 +0000430 kRuntimeISA,
Vladimir Marko21910692019-11-06 13:27:03 +0000431 relocate,
432 /*executable=*/ true,
Vladimir Marko21910692019-11-06 13:27:03 +0000433 /*extra_reservation_size=*/ 0u,
434 &boot_image_spaces,
435 &extra_reservation);
436 };
Vladimir Markoa8f39182019-11-21 10:08:58 +0000437 auto silent_load = [&](const std::string& image_location) {
438 ScopedLogSeverity quiet(LogSeverity::FATAL);
439 return load(image_location);
440 };
Vladimir Marko21910692019-11-06 13:27:03 +0000441
Vladimir Markoa8f39182019-11-21 10:08:58 +0000442 for (bool r : { false, true }) {
Vladimir Marko21910692019-11-06 13:27:03 +0000443 relocate = r;
444
445 // Load primary image with full path.
446 bool load_ok = load(base_location);
447 ASSERT_TRUE(load_ok) << error_msg;
448 ASSERT_FALSE(extra_reservation.IsValid());
449 ASSERT_EQ(head_dex_files.size(), boot_image_spaces.size());
450
451 // Fail to load primary image with just the name.
Vladimir Markoa8f39182019-11-21 10:08:58 +0000452 load_ok = silent_load(base_name);
Vladimir Marko21910692019-11-06 13:27:03 +0000453 ASSERT_FALSE(load_ok);
454
455 // Fail to load primary image with a search path.
Vladimir Markoa8f39182019-11-21 10:08:58 +0000456 load_ok = silent_load("*");
Vladimir Marko21910692019-11-06 13:27:03 +0000457 ASSERT_FALSE(load_ok);
Vladimir Markoa8f39182019-11-21 10:08:58 +0000458 load_ok = silent_load(scratch_dir + "*");
Vladimir Marko21910692019-11-06 13:27:03 +0000459 ASSERT_FALSE(load_ok);
460
461 // Load the primary and first extension with full path.
462 load_ok = load(base_location + ':' + mid_location);
463 ASSERT_TRUE(load_ok) << error_msg;
464 ASSERT_EQ(mid_bcp.size(), boot_image_spaces.size());
465
466 // Load the primary with full path and fail to load first extension without full path.
467 load_ok = load(base_location + ':' + mid_name);
468 ASSERT_TRUE(load_ok) << error_msg; // Primary image loaded successfully.
469 ASSERT_EQ(head_dex_files.size(), boot_image_spaces.size()); // But only the primary image.
470
471 // Load all the libcore images with full paths.
472 load_ok = load(base_location + ':' + mid_location + ':' + tail_location);
473 ASSERT_TRUE(load_ok) << error_msg;
474 ASSERT_EQ(full_bcp.size(), boot_image_spaces.size());
475
476 // Load the primary and first extension with full paths, fail to load second extension by name.
477 load_ok = load(base_location + ':' + mid_location + ':' + tail_name);
478 ASSERT_TRUE(load_ok) << error_msg;
479 ASSERT_EQ(mid_bcp.size(), boot_image_spaces.size());
480
481 // Load the primary with full path and fail to load first extension without full path,
482 // fail to load second extension because it depends on the first.
483 load_ok = load(base_location + ':' + mid_name + ':' + tail_location);
484 ASSERT_TRUE(load_ok) << error_msg; // Primary image loaded successfully.
485 ASSERT_EQ(head_dex_files.size(), boot_image_spaces.size()); // But only the primary image.
486
487 // Load the primary with full path and extensions with a specified search path.
488 load_ok = load(base_location + ':' + scratch_dir + '*');
489 ASSERT_TRUE(load_ok) << error_msg;
490 ASSERT_EQ(full_bcp.size(), boot_image_spaces.size());
491
492 // Load the primary with full path and fail to find extensions in BCP path.
493 load_ok = load(base_location + ":*");
494 ASSERT_TRUE(load_ok) << error_msg;
495 ASSERT_EQ(head_dex_files.size(), boot_image_spaces.size());
496 }
497
498 // Now copy the libcore dex files to the `scratch_dir` and retry loading the boot image
499 // with BCP in the scratch_dir so that the images can be found based on BCP paths.
Vladimir Markoa8f39182019-11-21 10:08:58 +0000500 CopyDexFiles(scratch_dir, &boot_class_path);
Vladimir Marko21910692019-11-06 13:27:03 +0000501
Vladimir Markoa8f39182019-11-21 10:08:58 +0000502 for (bool r : { false, true }) {
Vladimir Marko21910692019-11-06 13:27:03 +0000503 relocate = r;
504
505 // Loading the primary image with just the name now succeeds.
506 bool load_ok = load(base_name);
507 ASSERT_TRUE(load_ok) << error_msg;
Vladimir Markoc0e0e5e2020-01-23 17:43:05 +0000508 ASSERT_EQ(head_dex_files.size(), boot_image_spaces.size());
Vladimir Marko21910692019-11-06 13:27:03 +0000509
510 // Loading the primary image with a search path still fails.
Vladimir Markoa8f39182019-11-21 10:08:58 +0000511 load_ok = silent_load("*");
Vladimir Marko21910692019-11-06 13:27:03 +0000512 ASSERT_FALSE(load_ok);
Vladimir Markoa8f39182019-11-21 10:08:58 +0000513 load_ok = silent_load(scratch_dir + "*");
Vladimir Marko21910692019-11-06 13:27:03 +0000514 ASSERT_FALSE(load_ok);
515
516 // Load the primary and first extension without paths.
517 load_ok = load(base_name + ':' + mid_name);
518 ASSERT_TRUE(load_ok) << error_msg;
519 ASSERT_EQ(mid_bcp.size(), boot_image_spaces.size());
520
Vladimir Markoc0e0e5e2020-01-23 17:43:05 +0000521 // Load the primary without path and first extension with path.
522 load_ok = load(base_name + ':' + mid_location);
523 ASSERT_TRUE(load_ok) << error_msg;
524 ASSERT_EQ(mid_bcp.size(), boot_image_spaces.size());
525
Vladimir Marko21910692019-11-06 13:27:03 +0000526 // Load the primary with full path and the first extension without full path.
527 load_ok = load(base_location + ':' + mid_name);
528 ASSERT_TRUE(load_ok) << error_msg; // Loaded successfully.
529 ASSERT_EQ(mid_bcp.size(), boot_image_spaces.size()); // Including the extension.
530
531 // Load all the libcore images without paths.
532 load_ok = load(base_name + ':' + mid_name + ':' + tail_name);
533 ASSERT_TRUE(load_ok) << error_msg;
534 ASSERT_EQ(full_bcp.size(), boot_image_spaces.size());
535
536 // Load the primary and first extension with full paths and second extension by name.
537 load_ok = load(base_location + ':' + mid_location + ':' + tail_name);
538 ASSERT_TRUE(load_ok) << error_msg;
539 ASSERT_EQ(full_bcp.size(), boot_image_spaces.size());
540
541 // Load the primary with full path, first extension without path,
542 // and second extension with full path.
543 load_ok = load(base_location + ':' + mid_name + ':' + tail_location);
544 ASSERT_TRUE(load_ok) << error_msg; // Loaded successfully.
545 ASSERT_EQ(full_bcp.size(), boot_image_spaces.size()); // Including both extensions.
546
547 // Load the primary with full path and find both extensions in BCP path.
548 load_ok = load(base_location + ":*");
549 ASSERT_TRUE(load_ok) << error_msg;
550 ASSERT_EQ(full_bcp.size(), boot_image_spaces.size());
551
552 // Fail to load any images with invalid image locations (named component after search paths).
Vladimir Markoa8f39182019-11-21 10:08:58 +0000553 load_ok = silent_load(base_location + ":*:" + tail_location);
Vladimir Marko21910692019-11-06 13:27:03 +0000554 ASSERT_FALSE(load_ok);
Vladimir Markoa8f39182019-11-21 10:08:58 +0000555 load_ok = silent_load(base_location + ':' + scratch_dir + "*:" + tail_location);
Vladimir Marko21910692019-11-06 13:27:03 +0000556 ASSERT_FALSE(load_ok);
Vladimir Marko21910692019-11-06 13:27:03 +0000557
Vladimir Marko3ea66272019-12-12 14:28:50 +0000558 // Load the primary and single-image extension with full path.
559 load_ok = load(base_location + ':' + single_location);
Vladimir Markod0036ac2019-11-21 11:47:12 +0000560 ASSERT_TRUE(load_ok) << error_msg;
561 ASSERT_EQ(head_dex_files.size() + 1u, boot_image_spaces.size());
562
Vladimir Marko3ea66272019-12-12 14:28:50 +0000563 // Load the primary with full path and single-image extension with a specified search path.
564 load_ok = load(base_location + ':' + single_dir + '*');
Vladimir Markod0036ac2019-11-21 11:47:12 +0000565 ASSERT_TRUE(load_ok) << error_msg;
566 ASSERT_EQ(head_dex_files.size() + 1u, boot_image_spaces.size());
567 }
568
Vladimir Marko3ea66272019-12-12 14:28:50 +0000569 // Recompile the single-image extension using file descriptors and compare contents.
570 std::vector<std::string> expanded_single_filename_prefix =
571 gc::space::ImageSpace::ExpandMultiImageLocations(
572 single_dex_files.SubArray(/*pos=*/ 0u, /*length=*/ 1u),
573 single_filename_prefix,
574 /*boot_image_extension=*/ true);
575 CHECK_EQ(1u, expanded_single_filename_prefix.size());
576 std::string single_ext_prefix = expanded_single_filename_prefix[0];
577 std::string single_ext_prefix2 = single_ext_prefix + "2";
Vladimir Markod0036ac2019-11-21 11:47:12 +0000578 error_msg.clear();
Vladimir Marko3ea66272019-12-12 14:28:50 +0000579 single_ok = CompileBootImage(extra_args,
580 single_filename_prefix,
581 single_dex_files,
582 &error_msg,
583 /*use_fd_prefix=*/ single_ext_prefix2);
584 ASSERT_TRUE(single_ok) << error_msg;
585 EXPECT_TRUE(CompareFiles(single_ext_prefix + ".art", single_ext_prefix2 + ".art"));
586 EXPECT_TRUE(CompareFiles(single_ext_prefix + ".vdex", single_ext_prefix2 + ".vdex"));
587 EXPECT_TRUE(CompareFiles(single_ext_prefix + ".oat", single_ext_prefix2 + ".oat"));
Vladimir Markod0036ac2019-11-21 11:47:12 +0000588
Vladimir Markoc0e0e5e2020-01-23 17:43:05 +0000589 // Test parsing profile specification and creating the boot image extension on-the-fly.
590 // We must set --android-root in the image compiler options.
591 AddAndroidRootToImageCompilerOptions();
592 for (bool r : { false, true }) {
593 relocate = r;
594
Jiakai Zhangfb8cdb52021-12-06 18:51:28 +0000595 // Load primary boot image with a profile name.
Vladimir Markoc0e0e5e2020-01-23 17:43:05 +0000596 bool load_ok = silent_load(base_location + "!" + single_profile_filename);
Jiakai Zhangfb8cdb52021-12-06 18:51:28 +0000597 ASSERT_TRUE(load_ok);
Vladimir Markoc0e0e5e2020-01-23 17:43:05 +0000598
599 // Try and fail to load with invalid spec, two profile name separators.
600 load_ok = silent_load(base_location + ":" + single_location + "!!arbitrary-profile-name");
601 ASSERT_FALSE(load_ok);
602
603 // Try and fail to load with invalid spec, missing profile name.
604 load_ok = silent_load(base_location + ":" + single_location + "!");
605 ASSERT_FALSE(load_ok);
606
607 // Try and fail to load with invalid spec, missing component name.
608 load_ok = silent_load(base_location + ":!" + single_profile_filename);
609 ASSERT_FALSE(load_ok);
610
611 // Load primary boot image, specifying invalid extension component and profile name.
612 load_ok = load(base_location + ":/non-existent/" + single_name + "!non-existent-profile-name");
613 ASSERT_TRUE(load_ok) << error_msg;
614 ASSERT_EQ(head_dex_files.size(), boot_image_spaces.size());
615
616 // Load primary boot image and the single extension, specifying invalid profile name.
617 // (Load extension from file.)
618 load_ok = load(base_location + ":" + single_location + "!non-existent-profile-name");
619 ASSERT_TRUE(load_ok) << error_msg;
620 ASSERT_EQ(head_dex_files.size() + 1u, boot_image_spaces.size());
621 ASSERT_EQ(single_dex_files.size(),
622 boot_image_spaces.back()->GetImageHeader().GetComponentCount());
623
624 // Load primary boot image and fail to load the single extension, specifying
625 // invalid extension component name but a valid profile file.
626 // (Running dex2oat to compile extension is disabled.)
627 ASSERT_FALSE(Runtime::Current()->IsImageDex2OatEnabled());
628 load_ok = load(base_location + ":/non-existent/" + single_name + "!" + single_profile_filename);
629 ASSERT_TRUE(load_ok) << error_msg;
630 ASSERT_EQ(head_dex_files.size(), boot_image_spaces.size());
631
632 EnableImageDex2Oat();
633
634 // Load primary boot image and the single extension, specifying invalid extension
635 // component name but a valid profile file. (Compile extension by running dex2oat.)
636 load_ok = load(base_location + ":/non-existent/" + single_name + "!" + single_profile_filename);
637 ASSERT_TRUE(load_ok) << error_msg;
638 ASSERT_EQ(head_dex_files.size() + 1u, boot_image_spaces.size());
639 ASSERT_EQ(single_dex_files.size(),
640 boot_image_spaces.back()->GetImageHeader().GetComponentCount());
641
642 // Load primary boot image and two extensions, specifying invalid extension component
643 // names but valid profile files. (Compile extensions by running dex2oat.)
644 load_ok = load(base_location + ":/non-existent/" + mid_name + "!" + mid_profile_filename
645 + ":/non-existent/" + tail_name + "!" + tail_profile_filename);
646 ASSERT_TRUE(load_ok) << error_msg;
647 ASSERT_EQ(head_dex_files.size() + 2u, boot_image_spaces.size());
648 ASSERT_EQ(mid_dex_files.size(),
649 boot_image_spaces[head_dex_files.size()]->GetImageHeader().GetComponentCount());
650 ASSERT_EQ(tail_dex_files.size(),
651 boot_image_spaces[head_dex_files.size() + 1u]->GetImageHeader().GetComponentCount());
652
653 // Load primary boot image and fail to load extensions, specifying invalid component
654 // names but valid profile file only for the second one. As we fail to load the first
655 // extension, the second extension has a missing dependency and cannot be compiled.
656 load_ok = load(base_location + ":/non-existent/" + mid_name
657 + ":/non-existent/" + tail_name + "!" + tail_profile_filename);
658 ASSERT_TRUE(load_ok) << error_msg;
659 ASSERT_EQ(head_dex_files.size(), boot_image_spaces.size());
660
661 DisableImageDex2Oat();
662 }
Vladimir Markod0036ac2019-11-21 11:47:12 +0000663}
664
Mathieu Chartierf70fe3d2017-06-21 15:24:02 -0700665} // namespace art