blob: 9fd632f0c22e5242b34e2786ac2e25165b5f153d [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 Marko21910692019-11-06 13:27:03 +000044#include "gc/space/image_space.h"
David Sehr82d046e2018-04-23 08:14:19 -070045#include "profile/profile_compilation_info.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 // Visitors take method and type references
77 template <typename MethodVisitor, typename ClassVisitor>
78 void VisitLibcoreDexes(const MethodVisitor& method_visitor,
79 const ClassVisitor& class_visitor,
80 size_t method_frequency = 1,
81 size_t class_frequency = 1) {
Vladimir Markoc526e422019-11-20 10:27:14 +000082 std::vector<std::string> dexes = GetLibCoreDexFileNames();
Vladimir Markoc0e0e5e2020-01-23 17:43:05 +000083 ArrayRef<const std::string> dexes_array(dexes);
84 VisitDexes(dexes_array, method_visitor, class_visitor, method_frequency, class_frequency);
Vladimir Markoc526e422019-11-20 10:27:14 +000085 }
86
87 // Visitors take method and type references
88 template <typename MethodVisitor, typename ClassVisitor>
Vladimir Markoc0e0e5e2020-01-23 17:43:05 +000089 void VisitDexes(ArrayRef<const std::string> dexes,
Vladimir Markoc526e422019-11-20 10:27:14 +000090 const MethodVisitor& method_visitor,
91 const ClassVisitor& class_visitor,
92 size_t method_frequency = 1,
93 size_t class_frequency = 1) {
Mathieu Chartierf70fe3d2017-06-21 15:24:02 -070094 size_t method_counter = 0;
95 size_t class_counter = 0;
Vladimir Markoc526e422019-11-20 10:27:14 +000096 for (const std::string& dex : dexes) {
Mathieu Chartierf70fe3d2017-06-21 15:24:02 -070097 std::vector<std::unique_ptr<const DexFile>> dex_files;
98 std::string error_msg;
David Sehr013fd802018-01-11 22:55:24 -080099 const ArtDexFileLoader dex_file_loader;
100 CHECK(dex_file_loader.Open(dex.c_str(),
101 dex,
102 /*verify*/ true,
103 /*verify_checksum*/ false,
104 &error_msg,
105 &dex_files))
Mathieu Chartierf70fe3d2017-06-21 15:24:02 -0700106 << error_msg;
107 for (const std::unique_ptr<const DexFile>& dex_file : dex_files) {
108 for (size_t i = 0; i < dex_file->NumMethodIds(); ++i) {
109 if (++method_counter % method_frequency == 0) {
110 method_visitor(MethodReference(dex_file.get(), i));
111 }
112 }
113 for (size_t i = 0; i < dex_file->NumTypeIds(); ++i) {
114 if (++class_counter % class_frequency == 0) {
115 class_visitor(TypeReference(dex_file.get(), dex::TypeIndex(i)));
116 }
117 }
118 }
119 }
120 }
121
122 static void WriteLine(File* file, std::string line) {
123 line += '\n';
124 EXPECT_TRUE(file->WriteFully(&line[0], line.length()));
125 }
126
Vladimir Markoc0e0e5e2020-01-23 17:43:05 +0000127 void GenerateProfile(ArrayRef<const std::string> dexes,
Vladimir Markoc526e422019-11-20 10:27:14 +0000128 File* out_file,
129 size_t method_frequency,
130 size_t type_frequency) {
Mathieu Chartierf68b6982019-06-27 09:40:20 -0700131 ProfileCompilationInfo profile;
Vladimir Markoc526e422019-11-20 10:27:14 +0000132 VisitDexes(
133 dexes,
134 [&profile](MethodReference ref) {
135 uint32_t flags = ProfileCompilationInfo::MethodHotness::kFlagHot |
136 ProfileCompilationInfo::MethodHotness::kFlagStartup;
137 EXPECT_TRUE(profile.AddMethod(
138 ProfileMethodInfo(ref),
139 static_cast<ProfileCompilationInfo::MethodHotness::Flag>(flags)));
140 },
141 [&profile](TypeReference ref) {
142 std::set<dex::TypeIndex> classes;
143 classes.insert(ref.TypeIndex());
144 EXPECT_TRUE(profile.AddClassesForDex(ref.dex_file, classes.begin(), classes.end()));
145 },
146 method_frequency,
147 type_frequency);
Mathieu Chartierf68b6982019-06-27 09:40:20 -0700148 profile.Save(out_file->Fd());
Mathieu Chartierf70fe3d2017-06-21 15:24:02 -0700149 EXPECT_EQ(out_file->Flush(), 0);
150 }
151
152 void GenerateMethods(File* out_file, size_t frequency = 1) {
153 VisitLibcoreDexes([out_file](MethodReference ref) {
Mathieu Chartierfc8b4222017-09-17 13:44:24 -0700154 WriteLine(out_file, ref.PrettyMethod());
Mathieu Chartierf70fe3d2017-06-21 15:24:02 -0700155 }, VoidFunctor(), frequency, frequency);
156 EXPECT_EQ(out_file->Flush(), 0);
157 }
158
159 void AddRuntimeArg(std::vector<std::string>& args, const std::string& arg) {
160 args.push_back("--runtime-arg");
161 args.push_back(arg);
162 }
163
Vladimir Markoa8f39182019-11-21 10:08:58 +0000164 ImageSizes CompileImageAndGetSizes(ArrayRef<const std::string> dex_files,
165 const std::vector<std::string>& extra_args) {
Mathieu Chartierf70fe3d2017-06-21 15:24:02 -0700166 ImageSizes ret;
David Srbeckycf0c6ef2020-02-05 16:25:36 +0000167 ScratchDir scratch;
168 std::string filename_prefix = scratch.GetPath() + "boot";
Vladimir Marko21910692019-11-06 13:27:03 +0000169 std::vector<std::string> local_extra_args = extra_args;
170 local_extra_args.push_back(android::base::StringPrintf("--base=0x%08x", kBaseAddress));
Mathieu Chartierf70fe3d2017-06-21 15:24:02 -0700171 std::string error_msg;
David Srbeckycf0c6ef2020-02-05 16:25:36 +0000172 if (!CompileBootImage(local_extra_args, filename_prefix, dex_files, &error_msg)) {
173 LOG(ERROR) << "Failed to compile image " << filename_prefix << error_msg;
Mathieu Chartierf70fe3d2017-06-21 15:24:02 -0700174 }
David Srbeckycf0c6ef2020-02-05 16:25:36 +0000175 std::string art_file = filename_prefix + ".art";
176 std::string oat_file = filename_prefix + ".oat";
177 std::string vdex_file = filename_prefix + ".vdex";
Vladimir Markoa5785a22018-03-08 14:25:47 +0000178 int64_t art_size = OS::GetFileSizeBytes(art_file.c_str());
179 int64_t oat_size = OS::GetFileSizeBytes(oat_file.c_str());
180 int64_t vdex_size = OS::GetFileSizeBytes(vdex_file.c_str());
181 CHECK_GT(art_size, 0u) << art_file;
182 CHECK_GT(oat_size, 0u) << oat_file;
183 CHECK_GT(vdex_size, 0u) << vdex_file;
184 ret.art_size = art_size;
185 ret.oat_size = oat_size;
186 ret.vdex_size = vdex_size;
Mathieu Chartierf70fe3d2017-06-21 15:24:02 -0700187 return ret;
188 }
189
Vladimir Markoa8f39182019-11-21 10:08:58 +0000190 MemMap ReserveCoreImageAddressSpace(/*out*/std::string* error_msg) {
191 constexpr size_t kReservationSize = 256 * MB; // This should be enough for the compiled images.
192 // Extend to both directions for maximum relocation difference.
193 static_assert(ART_BASE_ADDRESS_MIN_DELTA < 0);
194 static_assert(ART_BASE_ADDRESS_MAX_DELTA > 0);
195 static_assert(IsAligned<kPageSize>(ART_BASE_ADDRESS_MIN_DELTA));
196 static_assert(IsAligned<kPageSize>(ART_BASE_ADDRESS_MAX_DELTA));
197 constexpr size_t kExtra = ART_BASE_ADDRESS_MAX_DELTA - ART_BASE_ADDRESS_MIN_DELTA;
198 uint32_t min_relocated_address = kBaseAddress + ART_BASE_ADDRESS_MIN_DELTA;
199 return MemMap::MapAnonymous("Reservation",
200 reinterpret_cast<uint8_t*>(min_relocated_address),
201 kReservationSize + kExtra,
202 PROT_NONE,
203 /*low_4gb=*/ true,
204 /*reuse=*/ false,
205 /*reservation=*/ nullptr,
206 error_msg);
207 }
208
209 void CopyDexFiles(const std::string& dir, /*inout*/std::vector<std::string>* dex_files) {
210 CHECK(EndsWith(dir, "/"));
211 for (std::string& dex_file : *dex_files) {
212 size_t slash_pos = dex_file.rfind('/');
David Srbeckycf0c6ef2020-02-05 16:25:36 +0000213 CHECK(OS::FileExists(dex_file.c_str())) << dex_file;
Vladimir Markoa8f39182019-11-21 10:08:58 +0000214 CHECK_NE(std::string::npos, slash_pos);
215 std::string new_location = dir + dex_file.substr(slash_pos + 1u);
216 std::ifstream src_stream(dex_file, std::ios::binary);
217 std::ofstream dst_stream(new_location, std::ios::binary);
218 dst_stream << src_stream.rdbuf();
219 dex_file = new_location;
220 }
221 }
Vladimir Markod0036ac2019-11-21 11:47:12 +0000222
223 bool CompareFiles(const std::string& filename1, const std::string& filename2) {
224 std::unique_ptr<File> file1(OS::OpenFileForReading(filename1.c_str()));
225 std::unique_ptr<File> file2(OS::OpenFileForReading(filename2.c_str()));
226 // Did we open the files?
227 if (file1 == nullptr || file2 == nullptr) {
228 return false;
229 }
230 // Are they non-empty and the same length?
231 if (file1->GetLength() <= 0 || file2->GetLength() != file1->GetLength()) {
232 return false;
233 }
234 return file1->Compare(file2.get()) == 0;
235 }
Vladimir Markoc0e0e5e2020-01-23 17:43:05 +0000236
237 void AddAndroidRootToImageCompilerOptions() {
238 const char* android_root = getenv("ANDROID_ROOT");
239 CHECK(android_root != nullptr);
240 Runtime::Current()->image_compiler_options_.push_back(
241 "--android-root=" + std::string(android_root));
242 }
243
244 void EnableImageDex2Oat() {
245 Runtime::Current()->image_dex2oat_enabled_ = true;
246 }
247
248 void DisableImageDex2Oat() {
249 Runtime::Current()->image_dex2oat_enabled_ = false;
250 }
Mathieu Chartierf70fe3d2017-06-21 15:24:02 -0700251};
252
Vladimir Markoa5785a22018-03-08 14:25:47 +0000253TEST_F(Dex2oatImageTest, TestModesAndFilters) {
Roland Levillainf5dd1142018-07-03 13:29:18 +0100254 // This test crashes on the gtest-heap-poisoning configuration
255 // (AddressSanitizer + CMS/RosAlloc + heap-poisoning); see b/111061592.
256 // Temporarily disable this test on this configuration to keep
257 // our automated build/testing green while we work on a fix.
258 TEST_DISABLED_FOR_MEMORY_TOOL_WITH_HEAP_POISONING_WITHOUT_READ_BARRIERS();
Mathieu Chartierf70fe3d2017-06-21 15:24:02 -0700259 if (kIsTargetBuild) {
260 // This test is too slow for target builds.
261 return;
262 }
Vladimir Markoa8f39182019-11-21 10:08:58 +0000263 // Compile only a subset of the libcore dex files to make this test shorter.
264 std::vector<std::string> libcore_dex_files = GetLibCoreDexFileNames();
265 // The primary image must contain at least core-oj and core-libart to initialize the runtime
266 // and we also need the core-icu4j if we want to compile these with full profile.
267 ASSERT_NE(std::string::npos, libcore_dex_files[0].find("core-oj"));
268 ASSERT_NE(std::string::npos, libcore_dex_files[1].find("core-libart"));
269 ASSERT_NE(std::string::npos, libcore_dex_files[2].find("core-icu4j"));
270 ArrayRef<const std::string> dex_files =
271 ArrayRef<const std::string>(libcore_dex_files).SubArray(/*pos=*/ 0u, /*length=*/ 3u);
272
273 ImageSizes base_sizes = CompileImageAndGetSizes(dex_files, {});
Mathieu Chartierf68b6982019-06-27 09:40:20 -0700274 ImageSizes everything_sizes;
275 ImageSizes filter_sizes;
Mathieu Chartierf70fe3d2017-06-21 15:24:02 -0700276 std::cout << "Base compile sizes " << base_sizes << std::endl;
Mathieu Chartierf68b6982019-06-27 09:40:20 -0700277 // Compile all methods and classes
Vladimir Markoc0e0e5e2020-01-23 17:43:05 +0000278 std::vector<std::string> libcore_dexes = GetLibCoreDexFileNames();
279 ArrayRef<const std::string> libcore_dexes_array(libcore_dexes);
Mathieu Chartierf70fe3d2017-06-21 15:24:02 -0700280 {
Mathieu Chartierf68b6982019-06-27 09:40:20 -0700281 ScratchFile profile_file;
Vladimir Markoc0e0e5e2020-01-23 17:43:05 +0000282 GenerateProfile(libcore_dexes_array,
Vladimir Markoc526e422019-11-20 10:27:14 +0000283 profile_file.GetFile(),
284 /*method_frequency=*/ 1u,
285 /*type_frequency=*/ 1u);
Mathieu Chartierf68b6982019-06-27 09:40:20 -0700286 everything_sizes = CompileImageAndGetSizes(
Vladimir Markoa8f39182019-11-21 10:08:58 +0000287 dex_files,
Mathieu Chartierf68b6982019-06-27 09:40:20 -0700288 {"--profile-file=" + profile_file.GetFilename(),
289 "--compiler-filter=speed-profile"});
290 profile_file.Close();
291 std::cout << "All methods and classes sizes " << everything_sizes << std::endl;
Mathieu Chartierf70fe3d2017-06-21 15:24:02 -0700292 // Putting all classes as image classes should increase art size
Mathieu Chartierf68b6982019-06-27 09:40:20 -0700293 EXPECT_GE(everything_sizes.art_size, base_sizes.art_size);
Mathieu Chartierf70fe3d2017-06-21 15:24:02 -0700294 // Sanity check that dex is the same size.
Mathieu Chartierf68b6982019-06-27 09:40:20 -0700295 EXPECT_EQ(everything_sizes.vdex_size, base_sizes.vdex_size);
Mathieu Chartierf70fe3d2017-06-21 15:24:02 -0700296 }
Mathieu Chartierf70fe3d2017-06-21 15:24:02 -0700297 static size_t kMethodFrequency = 3;
298 static size_t kTypeFrequency = 4;
299 // Test compiling fewer methods and classes.
300 {
Mathieu Chartierf70fe3d2017-06-21 15:24:02 -0700301 ScratchFile profile_file;
Vladimir Markoc0e0e5e2020-01-23 17:43:05 +0000302 GenerateProfile(libcore_dexes_array,
Vladimir Markoc526e422019-11-20 10:27:14 +0000303 profile_file.GetFile(),
304 kMethodFrequency,
305 kTypeFrequency);
Mathieu Chartierf68b6982019-06-27 09:40:20 -0700306 filter_sizes = CompileImageAndGetSizes(
Vladimir Markoa8f39182019-11-21 10:08:58 +0000307 dex_files,
Mathieu Chartierf70fe3d2017-06-21 15:24:02 -0700308 {"--profile-file=" + profile_file.GetFilename(),
309 "--compiler-filter=speed-profile"});
310 profile_file.Close();
Mathieu Chartierf68b6982019-06-27 09:40:20 -0700311 std::cout << "Fewer methods and classes sizes " << filter_sizes << std::endl;
312 EXPECT_LE(filter_sizes.art_size, everything_sizes.art_size);
313 EXPECT_LE(filter_sizes.oat_size, everything_sizes.oat_size);
314 EXPECT_LE(filter_sizes.vdex_size, everything_sizes.vdex_size);
Mathieu Chartierf70fe3d2017-06-21 15:24:02 -0700315 }
Jeff Haoc23b0c02017-07-27 18:19:38 -0700316 // Test dirty image objects.
317 {
318 ScratchFile classes;
Mathieu Chartierf68b6982019-06-27 09:40:20 -0700319 VisitLibcoreDexes(VoidFunctor(),
320 [&](TypeReference ref) {
321 WriteLine(classes.GetFile(), ref.dex_file->PrettyType(ref.TypeIndex()));
322 }, /*method_frequency=*/ 1u, /*class_frequency=*/ 1u);
323 ImageSizes image_classes_sizes = CompileImageAndGetSizes(
Vladimir Markoa8f39182019-11-21 10:08:58 +0000324 dex_files,
Jeff Haoc23b0c02017-07-27 18:19:38 -0700325 {"--dirty-image-objects=" + classes.GetFilename()});
326 classes.Close();
327 std::cout << "Dirty image object sizes " << image_classes_sizes << std::endl;
328 }
Mathieu Chartierf70fe3d2017-06-21 15:24:02 -0700329}
330
Vladimir Marko21910692019-11-06 13:27:03 +0000331TEST_F(Dex2oatImageTest, TestExtension) {
Vladimir Marko21910692019-11-06 13:27:03 +0000332 std::string error_msg;
Vladimir Markoa8f39182019-11-21 10:08:58 +0000333 MemMap reservation = ReserveCoreImageAddressSpace(&error_msg);
334 ASSERT_TRUE(reservation.IsValid()) << error_msg;
Vladimir Marko21910692019-11-06 13:27:03 +0000335
David Srbeckycf0c6ef2020-02-05 16:25:36 +0000336 ScratchDir scratch;
337 const std::string& scratch_dir = scratch.GetPath();
Vladimir Marko21910692019-11-06 13:27:03 +0000338 std::string image_dir = scratch_dir + GetInstructionSetString(kRuntimeISA);
David Srbeckycf0c6ef2020-02-05 16:25:36 +0000339 int mkdir_result = mkdir(image_dir.c_str(), 0700);
Vladimir Marko21910692019-11-06 13:27:03 +0000340 ASSERT_EQ(0, mkdir_result);
341 std::string filename_prefix = image_dir + "/core";
342
343 // Copy the libcore dex files to a custom dir inside `scratch_dir` so that we do not
344 // accidentally load pre-compiled core images from their original directory based on BCP paths.
345 std::string jar_dir = scratch_dir + "jars";
346 mkdir_result = mkdir(jar_dir.c_str(), 0700);
347 ASSERT_EQ(0, mkdir_result);
348 jar_dir += '/';
349 std::vector<std::string> libcore_dex_files = GetLibCoreDexFileNames();
Vladimir Markoa8f39182019-11-21 10:08:58 +0000350 CopyDexFiles(jar_dir, &libcore_dex_files);
Vladimir Marko21910692019-11-06 13:27:03 +0000351
352 ArrayRef<const std::string> full_bcp(libcore_dex_files);
353 size_t total_dex_files = full_bcp.size();
Vladimir Markoc526e422019-11-20 10:27:14 +0000354 ASSERT_GE(total_dex_files, 5u); // 3 for "head", 1 for "tail", at least one for "mid", see below.
Vladimir Marko21910692019-11-06 13:27:03 +0000355
Vladimir Markoc526e422019-11-20 10:27:14 +0000356 // The primary image must contain at least core-oj and core-libart to initialize the runtime
357 // and we also need the core-icu4j if we want to compile these with full profile.
Vladimir Marko21910692019-11-06 13:27:03 +0000358 ASSERT_NE(std::string::npos, full_bcp[0].find("core-oj"));
359 ASSERT_NE(std::string::npos, full_bcp[1].find("core-libart"));
Vladimir Markoc526e422019-11-20 10:27:14 +0000360 ASSERT_NE(std::string::npos, full_bcp[2].find("core-icu4j"));
361 ArrayRef<const std::string> head_dex_files = full_bcp.SubArray(/*pos=*/ 0u, /*length=*/ 3u);
Vladimir Marko21910692019-11-06 13:27:03 +0000362 // Middle part is everything else except for conscrypt.
363 ASSERT_NE(std::string::npos, full_bcp[full_bcp.size() - 1u].find("conscrypt"));
364 ArrayRef<const std::string> mid_bcp =
365 full_bcp.SubArray(/*pos=*/ 0u, /*length=*/ total_dex_files - 1u);
Vladimir Markoc526e422019-11-20 10:27:14 +0000366 ArrayRef<const std::string> mid_dex_files = mid_bcp.SubArray(/*pos=*/ 3u);
Vladimir Marko21910692019-11-06 13:27:03 +0000367 // Tail is just the conscrypt.
368 ArrayRef<const std::string> tail_dex_files =
369 full_bcp.SubArray(/*pos=*/ total_dex_files - 1u, /*length=*/ 1u);
370
371 // Prepare the "head", "mid" and "tail" names and locations.
372 std::string base_name = "core.art";
373 std::string base_location = scratch_dir + base_name;
374 std::vector<std::string> expanded_mid = gc::space::ImageSpace::ExpandMultiImageLocations(
375 mid_dex_files.SubArray(/*pos=*/ 0u, /*length=*/ 1u),
376 base_location,
377 /*boot_image_extension=*/ true);
378 CHECK_EQ(1u, expanded_mid.size());
379 std::string mid_location = expanded_mid[0];
380 size_t mid_slash_pos = mid_location.rfind('/');
381 ASSERT_NE(std::string::npos, mid_slash_pos);
382 std::string mid_name = mid_location.substr(mid_slash_pos + 1u);
383 CHECK_EQ(1u, tail_dex_files.size());
384 std::vector<std::string> expanded_tail = gc::space::ImageSpace::ExpandMultiImageLocations(
385 tail_dex_files, base_location, /*boot_image_extension=*/ true);
386 CHECK_EQ(1u, expanded_tail.size());
387 std::string tail_location = expanded_tail[0];
388 size_t tail_slash_pos = tail_location.rfind('/');
389 ASSERT_NE(std::string::npos, tail_slash_pos);
390 std::string tail_name = tail_location.substr(tail_slash_pos + 1u);
391
Vladimir Markoc0e0e5e2020-01-23 17:43:05 +0000392 // Create profiles.
393 ScratchFile head_profile_file;
394 GenerateProfile(head_dex_files,
395 head_profile_file.GetFile(),
396 /*method_frequency=*/ 1u,
397 /*type_frequency=*/ 1u);
398 const std::string& head_profile_filename = head_profile_file.GetFilename();
399 ScratchFile mid_profile_file;
400 GenerateProfile(mid_dex_files,
401 mid_profile_file.GetFile(),
402 /*method_frequency=*/ 5u,
403 /*type_frequency=*/ 4u);
404 const std::string& mid_profile_filename = mid_profile_file.GetFilename();
405 ScratchFile tail_profile_file;
406 GenerateProfile(tail_dex_files,
407 tail_profile_file.GetFile(),
408 /*method_frequency=*/ 5u,
409 /*type_frequency=*/ 4u);
410 const std::string& tail_profile_filename = tail_profile_file.GetFilename();
411
Vladimir Marko21910692019-11-06 13:27:03 +0000412 // Compile the "head", i.e. the primary boot image.
Vladimir Markoc0e0e5e2020-01-23 17:43:05 +0000413 std::vector<std::string> extra_args;
414 extra_args.push_back("--profile-file=" + head_profile_filename);
Vladimir Markoc526e422019-11-20 10:27:14 +0000415 extra_args.push_back(android::base::StringPrintf("--base=0x%08x", kBaseAddress));
416 bool head_ok = CompileBootImage(extra_args, filename_prefix, head_dex_files, &error_msg);
Vladimir Marko21910692019-11-06 13:27:03 +0000417 ASSERT_TRUE(head_ok) << error_msg;
418
419 // Compile the "mid", i.e. the first extension.
420 std::string mid_bcp_string = android::base::Join(mid_bcp, ':');
Vladimir Markoc0e0e5e2020-01-23 17:43:05 +0000421 extra_args.clear();
422 extra_args.push_back("--profile-file=" + mid_profile_filename);
Vladimir Marko21910692019-11-06 13:27:03 +0000423 AddRuntimeArg(extra_args, "-Xbootclasspath:" + mid_bcp_string);
424 AddRuntimeArg(extra_args, "-Xbootclasspath-locations:" + mid_bcp_string);
425 extra_args.push_back("--boot-image=" + base_location);
426 bool mid_ok = CompileBootImage(extra_args, filename_prefix, mid_dex_files, &error_msg);
427 ASSERT_TRUE(mid_ok) << error_msg;
428
429 // Try to compile the "tail" without specifying the "mid" extension. This shall fail.
Vladimir Markoc0e0e5e2020-01-23 17:43:05 +0000430 extra_args.clear();
431 extra_args.push_back("--profile-file=" + tail_profile_filename);
Vladimir Marko21910692019-11-06 13:27:03 +0000432 std::string full_bcp_string = android::base::Join(full_bcp, ':');
Vladimir Marko21910692019-11-06 13:27:03 +0000433 AddRuntimeArg(extra_args, "-Xbootclasspath:" + full_bcp_string);
434 AddRuntimeArg(extra_args, "-Xbootclasspath-locations:" + full_bcp_string);
435 extra_args.push_back("--boot-image=" + base_location);
436 bool tail_ok = CompileBootImage(extra_args, filename_prefix, tail_dex_files, &error_msg);
437 ASSERT_FALSE(tail_ok) << error_msg;
438
439 // Now compile the tail against both "head" and "mid".
440 CHECK(StartsWith(extra_args.back(), "--boot-image="));
441 extra_args.back() = "--boot-image=" + base_location + ':' + mid_location;
442 tail_ok = CompileBootImage(extra_args, filename_prefix, tail_dex_files, &error_msg);
443 ASSERT_TRUE(tail_ok) << error_msg;
444
Vladimir Marko3ea66272019-12-12 14:28:50 +0000445 // Prepare directory for the single-image test that squashes the "mid" and "tail".
446 std::string single_dir = scratch_dir + "single";
447 mkdir_result = mkdir(single_dir.c_str(), 0700);
448 ASSERT_EQ(0, mkdir_result);
449 single_dir += '/';
450 std::string single_image_dir = single_dir + GetInstructionSetString(kRuntimeISA);
451 mkdir_result = mkdir(single_image_dir.c_str(), 0700);
452 ASSERT_EQ(0, mkdir_result);
453 std::string single_filename_prefix = single_image_dir + "/core";
454
Vladimir Markoc0e0e5e2020-01-23 17:43:05 +0000455 // The dex files for the single-image are everything not in the "head".
456 ArrayRef<const std::string> single_dex_files = full_bcp.SubArray(/*pos=*/ head_dex_files.size());
457
Vladimir Marko3ea66272019-12-12 14:28:50 +0000458 // Create a smaller profile for the single-image test that squashes the "mid" and "tail".
459 ScratchFile single_profile_file;
Vladimir Markoc0e0e5e2020-01-23 17:43:05 +0000460 GenerateProfile(single_dex_files,
Vladimir Marko3ea66272019-12-12 14:28:50 +0000461 single_profile_file.GetFile(),
462 /*method_frequency=*/ 5u,
463 /*type_frequency=*/ 4u);
Vladimir Markoc0e0e5e2020-01-23 17:43:05 +0000464 const std::string& single_profile_filename = single_profile_file.GetFilename();
Vladimir Marko3ea66272019-12-12 14:28:50 +0000465
466 // Prepare the single image name and location.
467 CHECK_GE(single_dex_files.size(), 2u);
468 std::string single_base_location = single_dir + base_name;
469 std::vector<std::string> expanded_single = gc::space::ImageSpace::ExpandMultiImageLocations(
470 single_dex_files.SubArray(/*pos=*/ 0u, /*length=*/ 1u),
471 single_base_location,
472 /*boot_image_extension=*/ true);
473 CHECK_EQ(1u, expanded_single.size());
474 std::string single_location = expanded_single[0];
475 size_t single_slash_pos = single_location.rfind('/');
476 ASSERT_NE(std::string::npos, single_slash_pos);
477 std::string single_name = single_location.substr(single_slash_pos + 1u);
478 CHECK_EQ(single_name, mid_name);
479
480 // Compile the single-image against the primary boot image.
Vladimir Markoc0e0e5e2020-01-23 17:43:05 +0000481 extra_args.clear();
482 extra_args.push_back("--profile-file=" + single_profile_filename);
Vladimir Marko3ea66272019-12-12 14:28:50 +0000483 AddRuntimeArg(extra_args, "-Xbootclasspath:" + full_bcp_string);
484 AddRuntimeArg(extra_args, "-Xbootclasspath-locations:" + full_bcp_string);
485 extra_args.push_back("--boot-image=" + base_location);
486 extra_args.push_back("--single-image");
487 extra_args.push_back("--avoid-storing-invocation"); // For comparison below.
488 error_msg.clear();
489 bool single_ok =
490 CompileBootImage(extra_args, single_filename_prefix, single_dex_files, &error_msg);
491 ASSERT_TRUE(single_ok) << error_msg;
492
Vladimir Marko21910692019-11-06 13:27:03 +0000493 reservation = MemMap::Invalid(); // Free the reserved memory for loading images.
494
495 // Try to load the boot image with different image locations.
496 std::vector<std::string> boot_class_path = libcore_dex_files;
497 std::vector<std::unique_ptr<gc::space::ImageSpace>> boot_image_spaces;
498 bool relocate = false;
499 MemMap extra_reservation;
500 auto load = [&](const std::string& image_location) {
501 boot_image_spaces.clear();
502 extra_reservation = MemMap::Invalid();
503 ScopedObjectAccess soa(Thread::Current());
504 return gc::space::ImageSpace::LoadBootImage(/*boot_class_path=*/ boot_class_path,
505 /*boot_class_path_locations=*/ libcore_dex_files,
506 image_location,
507 kRuntimeISA,
508 gc::space::ImageSpaceLoadingOrder::kSystemFirst,
509 relocate,
510 /*executable=*/ true,
511 /*is_zygote=*/ false,
512 /*extra_reservation_size=*/ 0u,
513 &boot_image_spaces,
514 &extra_reservation);
515 };
Vladimir Markoa8f39182019-11-21 10:08:58 +0000516 auto silent_load = [&](const std::string& image_location) {
517 ScopedLogSeverity quiet(LogSeverity::FATAL);
518 return load(image_location);
519 };
Vladimir Marko21910692019-11-06 13:27:03 +0000520
Vladimir Markoa8f39182019-11-21 10:08:58 +0000521 for (bool r : { false, true }) {
Vladimir Marko21910692019-11-06 13:27:03 +0000522 relocate = r;
523
524 // Load primary image with full path.
525 bool load_ok = load(base_location);
526 ASSERT_TRUE(load_ok) << error_msg;
527 ASSERT_FALSE(extra_reservation.IsValid());
528 ASSERT_EQ(head_dex_files.size(), boot_image_spaces.size());
529
530 // Fail to load primary image with just the name.
Vladimir Markoa8f39182019-11-21 10:08:58 +0000531 load_ok = silent_load(base_name);
Vladimir Marko21910692019-11-06 13:27:03 +0000532 ASSERT_FALSE(load_ok);
533
534 // Fail to load primary image with a search path.
Vladimir Markoa8f39182019-11-21 10:08:58 +0000535 load_ok = silent_load("*");
Vladimir Marko21910692019-11-06 13:27:03 +0000536 ASSERT_FALSE(load_ok);
Vladimir Markoa8f39182019-11-21 10:08:58 +0000537 load_ok = silent_load(scratch_dir + "*");
Vladimir Marko21910692019-11-06 13:27:03 +0000538 ASSERT_FALSE(load_ok);
539
540 // Load the primary and first extension with full path.
541 load_ok = load(base_location + ':' + mid_location);
542 ASSERT_TRUE(load_ok) << error_msg;
543 ASSERT_EQ(mid_bcp.size(), boot_image_spaces.size());
544
545 // Load the primary with full path and fail to load first extension without full path.
546 load_ok = load(base_location + ':' + mid_name);
547 ASSERT_TRUE(load_ok) << error_msg; // Primary image loaded successfully.
548 ASSERT_EQ(head_dex_files.size(), boot_image_spaces.size()); // But only the primary image.
549
550 // Load all the libcore images with full paths.
551 load_ok = load(base_location + ':' + mid_location + ':' + tail_location);
552 ASSERT_TRUE(load_ok) << error_msg;
553 ASSERT_EQ(full_bcp.size(), boot_image_spaces.size());
554
555 // Load the primary and first extension with full paths, fail to load second extension by name.
556 load_ok = load(base_location + ':' + mid_location + ':' + tail_name);
557 ASSERT_TRUE(load_ok) << error_msg;
558 ASSERT_EQ(mid_bcp.size(), boot_image_spaces.size());
559
560 // Load the primary with full path and fail to load first extension without full path,
561 // fail to load second extension because it depends on the first.
562 load_ok = load(base_location + ':' + mid_name + ':' + tail_location);
563 ASSERT_TRUE(load_ok) << error_msg; // Primary image loaded successfully.
564 ASSERT_EQ(head_dex_files.size(), boot_image_spaces.size()); // But only the primary image.
565
566 // Load the primary with full path and extensions with a specified search path.
567 load_ok = load(base_location + ':' + scratch_dir + '*');
568 ASSERT_TRUE(load_ok) << error_msg;
569 ASSERT_EQ(full_bcp.size(), boot_image_spaces.size());
570
571 // Load the primary with full path and fail to find extensions in BCP path.
572 load_ok = load(base_location + ":*");
573 ASSERT_TRUE(load_ok) << error_msg;
574 ASSERT_EQ(head_dex_files.size(), boot_image_spaces.size());
575 }
576
577 // Now copy the libcore dex files to the `scratch_dir` and retry loading the boot image
578 // with BCP in the scratch_dir so that the images can be found based on BCP paths.
Vladimir Markoa8f39182019-11-21 10:08:58 +0000579 CopyDexFiles(scratch_dir, &boot_class_path);
Vladimir Marko21910692019-11-06 13:27:03 +0000580
Vladimir Markoa8f39182019-11-21 10:08:58 +0000581 for (bool r : { false, true }) {
Vladimir Marko21910692019-11-06 13:27:03 +0000582 relocate = r;
583
584 // Loading the primary image with just the name now succeeds.
585 bool load_ok = load(base_name);
586 ASSERT_TRUE(load_ok) << error_msg;
Vladimir Markoc0e0e5e2020-01-23 17:43:05 +0000587 ASSERT_EQ(head_dex_files.size(), boot_image_spaces.size());
Vladimir Marko21910692019-11-06 13:27:03 +0000588
589 // Loading the primary image with a search path still fails.
Vladimir Markoa8f39182019-11-21 10:08:58 +0000590 load_ok = silent_load("*");
Vladimir Marko21910692019-11-06 13:27:03 +0000591 ASSERT_FALSE(load_ok);
Vladimir Markoa8f39182019-11-21 10:08:58 +0000592 load_ok = silent_load(scratch_dir + "*");
Vladimir Marko21910692019-11-06 13:27:03 +0000593 ASSERT_FALSE(load_ok);
594
595 // Load the primary and first extension without paths.
596 load_ok = load(base_name + ':' + mid_name);
597 ASSERT_TRUE(load_ok) << error_msg;
598 ASSERT_EQ(mid_bcp.size(), boot_image_spaces.size());
599
Vladimir Markoc0e0e5e2020-01-23 17:43:05 +0000600 // Load the primary without path and first extension with path.
601 load_ok = load(base_name + ':' + mid_location);
602 ASSERT_TRUE(load_ok) << error_msg;
603 ASSERT_EQ(mid_bcp.size(), boot_image_spaces.size());
604
Vladimir Marko21910692019-11-06 13:27:03 +0000605 // Load the primary with full path and the first extension without full path.
606 load_ok = load(base_location + ':' + mid_name);
607 ASSERT_TRUE(load_ok) << error_msg; // Loaded successfully.
608 ASSERT_EQ(mid_bcp.size(), boot_image_spaces.size()); // Including the extension.
609
610 // Load all the libcore images without paths.
611 load_ok = load(base_name + ':' + mid_name + ':' + tail_name);
612 ASSERT_TRUE(load_ok) << error_msg;
613 ASSERT_EQ(full_bcp.size(), boot_image_spaces.size());
614
615 // Load the primary and first extension with full paths and second extension by name.
616 load_ok = load(base_location + ':' + mid_location + ':' + tail_name);
617 ASSERT_TRUE(load_ok) << error_msg;
618 ASSERT_EQ(full_bcp.size(), boot_image_spaces.size());
619
620 // Load the primary with full path, first extension without path,
621 // and second extension with full path.
622 load_ok = load(base_location + ':' + mid_name + ':' + tail_location);
623 ASSERT_TRUE(load_ok) << error_msg; // Loaded successfully.
624 ASSERT_EQ(full_bcp.size(), boot_image_spaces.size()); // Including both extensions.
625
626 // Load the primary with full path and find both extensions in BCP path.
627 load_ok = load(base_location + ":*");
628 ASSERT_TRUE(load_ok) << error_msg;
629 ASSERT_EQ(full_bcp.size(), boot_image_spaces.size());
630
631 // Fail to load any images with invalid image locations (named component after search paths).
Vladimir Markoa8f39182019-11-21 10:08:58 +0000632 load_ok = silent_load(base_location + ":*:" + tail_location);
Vladimir Marko21910692019-11-06 13:27:03 +0000633 ASSERT_FALSE(load_ok);
Vladimir Markoa8f39182019-11-21 10:08:58 +0000634 load_ok = silent_load(base_location + ':' + scratch_dir + "*:" + tail_location);
Vladimir Marko21910692019-11-06 13:27:03 +0000635 ASSERT_FALSE(load_ok);
Vladimir Marko21910692019-11-06 13:27:03 +0000636
Vladimir Marko3ea66272019-12-12 14:28:50 +0000637 // Load the primary and single-image extension with full path.
638 load_ok = load(base_location + ':' + single_location);
Vladimir Markod0036ac2019-11-21 11:47:12 +0000639 ASSERT_TRUE(load_ok) << error_msg;
640 ASSERT_EQ(head_dex_files.size() + 1u, boot_image_spaces.size());
641
Vladimir Marko3ea66272019-12-12 14:28:50 +0000642 // Load the primary with full path and single-image extension with a specified search path.
643 load_ok = load(base_location + ':' + single_dir + '*');
Vladimir Markod0036ac2019-11-21 11:47:12 +0000644 ASSERT_TRUE(load_ok) << error_msg;
645 ASSERT_EQ(head_dex_files.size() + 1u, boot_image_spaces.size());
646 }
647
Vladimir Marko3ea66272019-12-12 14:28:50 +0000648 // Recompile the single-image extension using file descriptors and compare contents.
649 std::vector<std::string> expanded_single_filename_prefix =
650 gc::space::ImageSpace::ExpandMultiImageLocations(
651 single_dex_files.SubArray(/*pos=*/ 0u, /*length=*/ 1u),
652 single_filename_prefix,
653 /*boot_image_extension=*/ true);
654 CHECK_EQ(1u, expanded_single_filename_prefix.size());
655 std::string single_ext_prefix = expanded_single_filename_prefix[0];
656 std::string single_ext_prefix2 = single_ext_prefix + "2";
Vladimir Markod0036ac2019-11-21 11:47:12 +0000657 error_msg.clear();
Vladimir Marko3ea66272019-12-12 14:28:50 +0000658 single_ok = CompileBootImage(extra_args,
659 single_filename_prefix,
660 single_dex_files,
661 &error_msg,
662 /*use_fd_prefix=*/ single_ext_prefix2);
663 ASSERT_TRUE(single_ok) << error_msg;
664 EXPECT_TRUE(CompareFiles(single_ext_prefix + ".art", single_ext_prefix2 + ".art"));
665 EXPECT_TRUE(CompareFiles(single_ext_prefix + ".vdex", single_ext_prefix2 + ".vdex"));
666 EXPECT_TRUE(CompareFiles(single_ext_prefix + ".oat", single_ext_prefix2 + ".oat"));
Vladimir Markod0036ac2019-11-21 11:47:12 +0000667
Vladimir Markoc0e0e5e2020-01-23 17:43:05 +0000668 // Test parsing profile specification and creating the boot image extension on-the-fly.
669 // We must set --android-root in the image compiler options.
670 AddAndroidRootToImageCompilerOptions();
671 for (bool r : { false, true }) {
672 relocate = r;
673
674 // Try and fail to load everything as compiled extension.
675 bool load_ok = silent_load(base_location + "!" + single_profile_filename);
676 ASSERT_FALSE(load_ok);
677
678 // Try and fail to load with invalid spec, two profile name separators.
679 load_ok = silent_load(base_location + ":" + single_location + "!!arbitrary-profile-name");
680 ASSERT_FALSE(load_ok);
681
682 // Try and fail to load with invalid spec, missing profile name.
683 load_ok = silent_load(base_location + ":" + single_location + "!");
684 ASSERT_FALSE(load_ok);
685
686 // Try and fail to load with invalid spec, missing component name.
687 load_ok = silent_load(base_location + ":!" + single_profile_filename);
688 ASSERT_FALSE(load_ok);
689
690 // Load primary boot image, specifying invalid extension component and profile name.
691 load_ok = load(base_location + ":/non-existent/" + single_name + "!non-existent-profile-name");
692 ASSERT_TRUE(load_ok) << error_msg;
693 ASSERT_EQ(head_dex_files.size(), boot_image_spaces.size());
694
695 // Load primary boot image and the single extension, specifying invalid profile name.
696 // (Load extension from file.)
697 load_ok = load(base_location + ":" + single_location + "!non-existent-profile-name");
698 ASSERT_TRUE(load_ok) << error_msg;
699 ASSERT_EQ(head_dex_files.size() + 1u, boot_image_spaces.size());
700 ASSERT_EQ(single_dex_files.size(),
701 boot_image_spaces.back()->GetImageHeader().GetComponentCount());
702
703 // Load primary boot image and fail to load the single extension, specifying
704 // invalid extension component name but a valid profile file.
705 // (Running dex2oat to compile extension is disabled.)
706 ASSERT_FALSE(Runtime::Current()->IsImageDex2OatEnabled());
707 load_ok = load(base_location + ":/non-existent/" + single_name + "!" + single_profile_filename);
708 ASSERT_TRUE(load_ok) << error_msg;
709 ASSERT_EQ(head_dex_files.size(), boot_image_spaces.size());
710
711 EnableImageDex2Oat();
712
713 // Load primary boot image and the single extension, specifying invalid extension
714 // component name but a valid profile file. (Compile extension by running dex2oat.)
715 load_ok = load(base_location + ":/non-existent/" + single_name + "!" + single_profile_filename);
716 ASSERT_TRUE(load_ok) << error_msg;
717 ASSERT_EQ(head_dex_files.size() + 1u, boot_image_spaces.size());
718 ASSERT_EQ(single_dex_files.size(),
719 boot_image_spaces.back()->GetImageHeader().GetComponentCount());
720
721 // Load primary boot image and two extensions, specifying invalid extension component
722 // names but valid profile files. (Compile extensions by running dex2oat.)
723 load_ok = load(base_location + ":/non-existent/" + mid_name + "!" + mid_profile_filename
724 + ":/non-existent/" + tail_name + "!" + tail_profile_filename);
725 ASSERT_TRUE(load_ok) << error_msg;
726 ASSERT_EQ(head_dex_files.size() + 2u, boot_image_spaces.size());
727 ASSERT_EQ(mid_dex_files.size(),
728 boot_image_spaces[head_dex_files.size()]->GetImageHeader().GetComponentCount());
729 ASSERT_EQ(tail_dex_files.size(),
730 boot_image_spaces[head_dex_files.size() + 1u]->GetImageHeader().GetComponentCount());
731
732 // Load primary boot image and fail to load extensions, specifying invalid component
733 // names but valid profile file only for the second one. As we fail to load the first
734 // extension, the second extension has a missing dependency and cannot be compiled.
735 load_ok = load(base_location + ":/non-existent/" + mid_name
736 + ":/non-existent/" + tail_name + "!" + tail_profile_filename);
737 ASSERT_TRUE(load_ok) << error_msg;
738 ASSERT_EQ(head_dex_files.size(), boot_image_spaces.size());
739
740 DisableImageDex2Oat();
741 }
Vladimir Markod0036ac2019-11-21 11:47:12 +0000742}
743
Mathieu Chartierf70fe3d2017-06-21 15:24:02 -0700744} // namespace art