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