blob: 431ab90f3962735d0e1049dee4e6494c79cca462 [file] [log] [blame]
Vladimir Marko1352f132017-04-28 15:28:29 +01001/*
2 * Copyright (C) 2011 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 Marko74527972016-11-29 15:57:32 +000017#ifndef ART_DEX2OAT_LINKER_IMAGE_TEST_H_
18#define ART_DEX2OAT_LINKER_IMAGE_TEST_H_
Vladimir Marko1352f132017-04-28 15:28:29 +010019
20#include "image.h"
21
22#include <memory>
23#include <string>
Vladimir Marko2ef01102019-02-05 15:05:10 +000024#include <string_view>
Vladimir Marko1352f132017-04-28 15:28:29 +010025#include <vector>
26
27#include "android-base/stringprintf.h"
28
29#include "art_method-inl.h"
David Sehr891a50e2017-10-27 17:01:07 -070030#include "base/file_utils.h"
Vladimir Marko54159c62018-06-20 14:30:08 +010031#include "base/hash_set.h"
Vladimir Markod79b37b2018-11-02 13:06:22 +000032#include "base/stl_util.h"
Vladimir Marko1352f132017-04-28 15:28:29 +010033#include "base/unix_file/fd_file.h"
David Sehrc431b9d2018-03-02 12:01:51 -080034#include "base/utils.h"
Vladimir Marko1352f132017-04-28 15:28:29 +010035#include "class_linker-inl.h"
Vladimir Marko1352f132017-04-28 15:28:29 +010036#include "common_compiler_test.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070037#include "compiler_callbacks.h"
Vladimir Marko1352f132017-04-28 15:28:29 +010038#include "debug/method_debug_info.h"
39#include "dex/quick_compiler_callbacks.h"
Andreas Gampead1aa632019-01-02 10:30:54 -080040#include "dex/signature-inl.h"
Vladimir Markoa0431112018-06-25 09:32:54 +010041#include "driver/compiler_driver.h"
Vladimir Marko1352f132017-04-28 15:28:29 +010042#include "driver/compiler_options.h"
Vladimir Marko1352f132017-04-28 15:28:29 +010043#include "gc/space/image_space.h"
44#include "image_writer.h"
45#include "linker/buffered_output_stream.h"
Vladimir Marko74527972016-11-29 15:57:32 +000046#include "linker/elf_writer.h"
47#include "linker/elf_writer_quick.h"
Vladimir Marko1352f132017-04-28 15:28:29 +010048#include "linker/file_output_stream.h"
49#include "linker/multi_oat_relative_patcher.h"
50#include "lock_word.h"
51#include "mirror/object-inl.h"
52#include "oat_writer.h"
53#include "scoped_thread_state_change-inl.h"
54#include "signal_catcher.h"
Vladimir Marko1352f132017-04-28 15:28:29 +010055
56namespace art {
Vladimir Marko74527972016-11-29 15:57:32 +000057namespace linker {
Vladimir Marko1352f132017-04-28 15:28:29 +010058
59static const uintptr_t kRequestedImageBase = ART_BASE_ADDRESS;
60
61struct CompilationHelper {
62 std::vector<std::string> dex_file_locations;
63 std::vector<ScratchFile> image_locations;
64 std::vector<std::unique_ptr<const DexFile>> extra_dex_files;
65 std::vector<ScratchFile> image_files;
66 std::vector<ScratchFile> oat_files;
67 std::vector<ScratchFile> vdex_files;
68 std::string image_dir;
69
Vladimir Marko1352f132017-04-28 15:28:29 +010070 std::vector<size_t> GetImageObjectSectionSizes();
71
72 ~CompilationHelper();
73};
74
75class ImageTest : public CommonCompilerTest {
76 protected:
77 virtual void SetUp() {
78 ReserveImageSpace();
79 CommonCompilerTest::SetUp();
80 }
81
Mathieu Chartier1a842962018-11-13 15:09:51 -080082 void TestWriteRead(ImageHeader::StorageMode storage_mode, uint32_t max_image_block_size);
Vladimir Marko1352f132017-04-28 15:28:29 +010083
84 void Compile(ImageHeader::StorageMode storage_mode,
Mathieu Chartier1a842962018-11-13 15:09:51 -080085 uint32_t max_image_block_size,
Vladimir Marko213ee2d2018-06-22 11:56:34 +010086 /*out*/ CompilationHelper& out_helper,
Vladimir Marko1352f132017-04-28 15:28:29 +010087 const std::string& extra_dex = "",
Vladimir Markod79b37b2018-11-02 13:06:22 +000088 const std::initializer_list<std::string>& image_classes = {},
89 const std::initializer_list<std::string>& image_classes_failing_aot_clinit = {});
Vladimir Marko1352f132017-04-28 15:28:29 +010090
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010091 void SetUpRuntimeOptions(RuntimeOptions* options) override {
Vladimir Marko1352f132017-04-28 15:28:29 +010092 CommonCompilerTest::SetUpRuntimeOptions(options);
Mathieu Chartiere01b6f62017-07-19 16:55:04 -070093 QuickCompilerCallbacks* new_callbacks =
94 new QuickCompilerCallbacks(CompilerCallbacks::CallbackMode::kCompileBootImage);
95 new_callbacks->SetVerificationResults(verification_results_.get());
96 callbacks_.reset(new_callbacks);
Vladimir Marko1352f132017-04-28 15:28:29 +010097 options->push_back(std::make_pair("compilercallbacks", callbacks_.get()));
98 }
99
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100100 std::unique_ptr<HashSet<std::string>> GetImageClasses() override {
Vladimir Marko54159c62018-06-20 14:30:08 +0100101 return std::make_unique<HashSet<std::string>>(image_classes_);
Vladimir Marko1352f132017-04-28 15:28:29 +0100102 }
103
Vladimir Markoa8bba7d2018-05-30 15:18:48 +0100104 ArtMethod* FindCopiedMethod(ArtMethod* origin, ObjPtr<mirror::Class> klass)
Vladimir Marko1352f132017-04-28 15:28:29 +0100105 REQUIRES_SHARED(Locks::mutator_lock_) {
106 PointerSize pointer_size = class_linker_->GetImagePointerSize();
107 for (ArtMethod& m : klass->GetCopiedMethods(pointer_size)) {
108 if (strcmp(origin->GetName(), m.GetName()) == 0 &&
109 origin->GetSignature() == m.GetSignature()) {
110 return &m;
111 }
112 }
113 return nullptr;
114 }
115
116 private:
Vladimir Marko213ee2d2018-06-22 11:56:34 +0100117 void DoCompile(ImageHeader::StorageMode storage_mode, /*out*/ CompilationHelper& out_helper);
118
Vladimir Marko54159c62018-06-20 14:30:08 +0100119 HashSet<std::string> image_classes_;
Vladimir Marko1352f132017-04-28 15:28:29 +0100120};
121
122inline CompilationHelper::~CompilationHelper() {
123 for (ScratchFile& image_file : image_files) {
124 image_file.Unlink();
125 }
126 for (ScratchFile& oat_file : oat_files) {
127 oat_file.Unlink();
128 }
129 for (ScratchFile& vdex_file : vdex_files) {
130 vdex_file.Unlink();
131 }
132 const int rmdir_result = rmdir(image_dir.c_str());
133 CHECK_EQ(0, rmdir_result);
134}
135
136inline std::vector<size_t> CompilationHelper::GetImageObjectSectionSizes() {
137 std::vector<size_t> ret;
138 for (ScratchFile& image_file : image_files) {
139 std::unique_ptr<File> file(OS::OpenFileForReading(image_file.GetFilename().c_str()));
140 CHECK(file.get() != nullptr);
141 ImageHeader image_header;
142 CHECK_EQ(file->ReadFully(&image_header, sizeof(image_header)), true);
143 CHECK(image_header.IsValid());
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100144 ret.push_back(image_header.GetObjectsSection().Size());
Vladimir Marko1352f132017-04-28 15:28:29 +0100145 }
146 return ret;
147}
148
Vladimir Marko213ee2d2018-06-22 11:56:34 +0100149inline void ImageTest::DoCompile(ImageHeader::StorageMode storage_mode,
150 /*out*/ CompilationHelper& out_helper) {
151 CompilerDriver* driver = compiler_driver_.get();
Vladimir Marko1352f132017-04-28 15:28:29 +0100152 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
153 std::vector<const DexFile*> class_path = class_linker->GetBootClassPath();
154
Vladimir Marko213ee2d2018-06-22 11:56:34 +0100155 for (const std::unique_ptr<const DexFile>& dex_file : out_helper.extra_dex_files) {
Vladimir Marko1352f132017-04-28 15:28:29 +0100156 {
157 ScopedObjectAccess soa(Thread::Current());
158 // Inject in boot class path so that the compiler driver can see it.
159 class_linker->AppendToBootClassPath(soa.Self(), *dex_file.get());
160 }
161 class_path.push_back(dex_file.get());
162 }
163
164 // Enable write for dex2dex.
165 for (const DexFile* dex_file : class_path) {
Vladimir Marko213ee2d2018-06-22 11:56:34 +0100166 out_helper.dex_file_locations.push_back(dex_file->GetLocation());
Vladimir Marko1352f132017-04-28 15:28:29 +0100167 if (dex_file->IsReadOnly()) {
168 dex_file->EnableWrite();
169 }
170 }
171 {
172 // Create a generic tmp file, to be the base of the .art and .oat temporary files.
173 ScratchFile location;
Vladimir Marko91f10322018-12-07 18:04:10 +0000174 std::vector<std::string> image_locations =
175 gc::space::ImageSpace::ExpandMultiImageLocations(out_helper.dex_file_locations,
176 location.GetFilename() + ".art");
177 for (size_t i = 0u; i != class_path.size(); ++i) {
178 out_helper.image_locations.push_back(ScratchFile(image_locations[i]));
Vladimir Marko1352f132017-04-28 15:28:29 +0100179 }
180 }
181 std::vector<std::string> image_filenames;
Vladimir Marko213ee2d2018-06-22 11:56:34 +0100182 for (ScratchFile& file : out_helper.image_locations) {
Vladimir Marko1352f132017-04-28 15:28:29 +0100183 std::string image_filename(GetSystemImageFilename(file.GetFilename().c_str(), kRuntimeISA));
184 image_filenames.push_back(image_filename);
185 size_t pos = image_filename.rfind('/');
186 CHECK_NE(pos, std::string::npos) << image_filename;
Vladimir Marko213ee2d2018-06-22 11:56:34 +0100187 if (out_helper.image_dir.empty()) {
188 out_helper.image_dir = image_filename.substr(0, pos);
189 int mkdir_result = mkdir(out_helper.image_dir.c_str(), 0700);
190 CHECK_EQ(0, mkdir_result) << out_helper.image_dir;
Vladimir Marko1352f132017-04-28 15:28:29 +0100191 }
Vladimir Marko213ee2d2018-06-22 11:56:34 +0100192 out_helper.image_files.push_back(ScratchFile(OS::CreateEmptyFile(image_filename.c_str())));
Vladimir Marko1352f132017-04-28 15:28:29 +0100193 }
194
195 std::vector<std::string> oat_filenames;
196 std::vector<std::string> vdex_filenames;
197 for (const std::string& image_filename : image_filenames) {
198 std::string oat_filename = ReplaceFileExtension(image_filename, "oat");
Vladimir Marko213ee2d2018-06-22 11:56:34 +0100199 out_helper.oat_files.push_back(ScratchFile(OS::CreateEmptyFile(oat_filename.c_str())));
Vladimir Marko1352f132017-04-28 15:28:29 +0100200 oat_filenames.push_back(oat_filename);
201 std::string vdex_filename = ReplaceFileExtension(image_filename, "vdex");
Vladimir Marko213ee2d2018-06-22 11:56:34 +0100202 out_helper.vdex_files.push_back(ScratchFile(OS::CreateEmptyFile(vdex_filename.c_str())));
Vladimir Marko1352f132017-04-28 15:28:29 +0100203 vdex_filenames.push_back(vdex_filename);
204 }
205
206 std::unordered_map<const DexFile*, size_t> dex_file_to_oat_index_map;
Vladimir Marko1352f132017-04-28 15:28:29 +0100207 size_t image_idx = 0;
208 for (const DexFile* dex_file : class_path) {
209 dex_file_to_oat_index_map.emplace(dex_file, image_idx);
210 ++image_idx;
211 }
212 // TODO: compile_pic should be a test argument.
Vladimir Markoa0431112018-06-25 09:32:54 +0100213 std::unique_ptr<ImageWriter> writer(new ImageWriter(*compiler_options_,
Vladimir Marko1352f132017-04-28 15:28:29 +0100214 kRequestedImageBase,
Vladimir Marko1352f132017-04-28 15:28:29 +0100215 storage_mode,
Vladimir Markod011d812018-11-29 12:35:24 +0000216 oat_filenames,
Jeff Haoc23b0c02017-07-27 18:19:38 -0700217 dex_file_to_oat_index_map,
Nicolas Geoffrayf0d30022018-11-20 17:45:38 +0000218 /*class_loader=*/ nullptr,
Vladimir Marko9c4b9702018-11-14 15:09:02 +0000219 /*dirty_image_objects=*/ nullptr));
Vladimir Marko1352f132017-04-28 15:28:29 +0100220 {
221 {
222 jobject class_loader = nullptr;
223 TimingLogger timings("ImageTest::WriteRead", false, false);
Vladimir Marko2afaff72018-11-30 17:01:50 +0000224 CompileAll(class_loader, class_path, &timings);
Vladimir Marko1352f132017-04-28 15:28:29 +0100225
Vladimir Marko2afaff72018-11-30 17:01:50 +0000226 TimingLogger::ScopedTiming t("WriteElf", &timings);
Vladimir Marko1352f132017-04-28 15:28:29 +0100227 SafeMap<std::string, std::string> key_value_store;
Vladimir Marko1352f132017-04-28 15:28:29 +0100228 key_value_store.Put(OatHeader::kBootClassPathKey,
Vladimir Marko91f10322018-12-07 18:04:10 +0000229 android::base::Join(out_helper.dex_file_locations, ':'));
Vladimir Marko1352f132017-04-28 15:28:29 +0100230
231 std::vector<std::unique_ptr<ElfWriter>> elf_writers;
232 std::vector<std::unique_ptr<OatWriter>> oat_writers;
Vladimir Marko213ee2d2018-06-22 11:56:34 +0100233 for (ScratchFile& oat_file : out_helper.oat_files) {
Vladimir Markoa0431112018-06-25 09:32:54 +0100234 elf_writers.emplace_back(CreateElfWriterQuick(*compiler_options_, oat_file.GetFile()));
Vladimir Marko1352f132017-04-28 15:28:29 +0100235 elf_writers.back()->Start();
Vladimir Markoa0431112018-06-25 09:32:54 +0100236 oat_writers.emplace_back(new OatWriter(*compiler_options_,
Vladimir Marko1352f132017-04-28 15:28:29 +0100237 &timings,
Mathieu Chartier603ccab2017-10-20 14:34:28 -0700238 /*profile_compilation_info*/nullptr,
239 CompactDexLevel::kCompactDexLevelNone));
Vladimir Marko1352f132017-04-28 15:28:29 +0100240 }
241
242 std::vector<OutputStream*> rodata;
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100243 std::vector<MemMap> opened_dex_files_maps;
Vladimir Marko1352f132017-04-28 15:28:29 +0100244 std::vector<std::unique_ptr<const DexFile>> opened_dex_files;
245 // Now that we have finalized key_value_store_, start writing the oat file.
246 for (size_t i = 0, size = oat_writers.size(); i != size; ++i) {
247 const DexFile* dex_file = class_path[i];
248 rodata.push_back(elf_writers[i]->StartRoData());
249 ArrayRef<const uint8_t> raw_dex_file(
250 reinterpret_cast<const uint8_t*>(&dex_file->GetHeader()),
251 dex_file->GetHeader().file_size_);
252 oat_writers[i]->AddRawDexFileSource(raw_dex_file,
253 dex_file->GetLocation().c_str(),
254 dex_file->GetLocationChecksum());
255
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100256 std::vector<MemMap> cur_opened_dex_files_maps;
Vladimir Marko1352f132017-04-28 15:28:29 +0100257 std::vector<std::unique_ptr<const DexFile>> cur_opened_dex_files;
258 bool dex_files_ok = oat_writers[i]->WriteAndOpenDexFiles(
Vladimir Marko213ee2d2018-06-22 11:56:34 +0100259 out_helper.vdex_files[i].GetFile(),
Vladimir Marko1352f132017-04-28 15:28:29 +0100260 rodata.back(),
Vladimir Markod011d812018-11-29 12:35:24 +0000261 (i == 0u) ? &key_value_store : nullptr,
Vladimir Marko1352f132017-04-28 15:28:29 +0100262 /* verify */ false, // Dex files may be dex-to-dex-ed, don't verify.
263 /* update_input_vdex */ false,
Nicolas Geoffray66ff8a82018-02-28 13:27:55 +0000264 /* copy_dex_files */ CopyOption::kOnlyIfCompressed,
Nicolas Geoffrayf3075272018-01-08 12:41:19 +0000265 &cur_opened_dex_files_maps,
Vladimir Marko1352f132017-04-28 15:28:29 +0100266 &cur_opened_dex_files);
267 ASSERT_TRUE(dex_files_ok);
268
Nicolas Geoffrayf3075272018-01-08 12:41:19 +0000269 if (!cur_opened_dex_files_maps.empty()) {
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100270 for (MemMap& cur_map : cur_opened_dex_files_maps) {
Nicolas Geoffrayf3075272018-01-08 12:41:19 +0000271 opened_dex_files_maps.push_back(std::move(cur_map));
272 }
Vladimir Marko1352f132017-04-28 15:28:29 +0100273 for (std::unique_ptr<const DexFile>& cur_dex_file : cur_opened_dex_files) {
274 // dex_file_oat_index_map_.emplace(dex_file.get(), i);
275 opened_dex_files.push_back(std::move(cur_dex_file));
276 }
277 } else {
278 ASSERT_TRUE(cur_opened_dex_files.empty());
279 }
280 }
Mathieu Chartier703b82a2018-04-10 15:52:32 -0700281 bool image_space_ok = writer->PrepareImageAddressSpace(&timings);
Vladimir Marko1352f132017-04-28 15:28:29 +0100282 ASSERT_TRUE(image_space_ok);
283
Vladimir Marko213ee2d2018-06-22 11:56:34 +0100284 DCHECK_EQ(out_helper.vdex_files.size(), out_helper.oat_files.size());
285 for (size_t i = 0, size = out_helper.oat_files.size(); i != size; ++i) {
Vladimir Markoa0431112018-06-25 09:32:54 +0100286 MultiOatRelativePatcher patcher(compiler_options_->GetInstructionSet(),
287 compiler_options_->GetInstructionSetFeatures(),
Vladimir Markoca1e0382018-04-11 09:58:41 +0000288 driver->GetCompiledMethodStorage());
Vladimir Marko1352f132017-04-28 15:28:29 +0100289 OatWriter* const oat_writer = oat_writers[i].get();
290 ElfWriter* const elf_writer = elf_writers[i].get();
291 std::vector<const DexFile*> cur_dex_files(1u, class_path[i]);
292 oat_writer->Initialize(driver, writer.get(), cur_dex_files);
David Brazdil93592f52017-12-08 10:53:27 +0000293
294 std::unique_ptr<BufferedOutputStream> vdex_out =
295 std::make_unique<BufferedOutputStream>(
Vladimir Marko213ee2d2018-06-22 11:56:34 +0100296 std::make_unique<FileOutputStream>(out_helper.vdex_files[i].GetFile()));
David Brazdil93592f52017-12-08 10:53:27 +0000297 oat_writer->WriteVerifierDeps(vdex_out.get(), nullptr);
298 oat_writer->WriteQuickeningInfo(vdex_out.get());
299 oat_writer->WriteChecksumsAndVdexHeader(vdex_out.get());
300
Vladimir Marko1352f132017-04-28 15:28:29 +0100301 oat_writer->PrepareLayout(&patcher);
Vladimir Markob066d432018-01-03 13:14:37 +0000302 elf_writer->PrepareDynamicSection(oat_writer->GetOatHeader().GetExecutableOffset(),
303 oat_writer->GetCodeSize(),
304 oat_writer->GetDataBimgRelRoSize(),
Vladimir Marko1352f132017-04-28 15:28:29 +0100305 oat_writer->GetBssSize(),
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100306 oat_writer->GetBssMethodsOffset(),
David Srbeckyec2cdf42017-12-08 16:21:25 +0000307 oat_writer->GetBssRootsOffset(),
308 oat_writer->GetVdexSize());
Vladimir Marko1352f132017-04-28 15:28:29 +0100309
310 writer->UpdateOatFileLayout(i,
311 elf_writer->GetLoadedSize(),
312 oat_writer->GetOatDataOffset(),
313 oat_writer->GetOatSize());
314
315 bool rodata_ok = oat_writer->WriteRodata(rodata[i]);
316 ASSERT_TRUE(rodata_ok);
317 elf_writer->EndRoData(rodata[i]);
318
319 OutputStream* text = elf_writer->StartText();
320 bool text_ok = oat_writer->WriteCode(text);
321 ASSERT_TRUE(text_ok);
322 elf_writer->EndText(text);
323
Vladimir Markob066d432018-01-03 13:14:37 +0000324 if (oat_writer->GetDataBimgRelRoSize() != 0u) {
325 OutputStream* data_bimg_rel_ro = elf_writer->StartDataBimgRelRo();
326 bool data_bimg_rel_ro_ok = oat_writer->WriteDataBimgRelRo(data_bimg_rel_ro);
327 ASSERT_TRUE(data_bimg_rel_ro_ok);
328 elf_writer->EndDataBimgRelRo(data_bimg_rel_ro);
329 }
330
Vladimir Marko0ace5632018-12-14 11:11:47 +0000331 bool header_ok = oat_writer->WriteHeader(elf_writer->GetStream());
Vladimir Marko1352f132017-04-28 15:28:29 +0100332 ASSERT_TRUE(header_ok);
333
334 writer->UpdateOatFileHeader(i, oat_writer->GetOatHeader());
335
336 elf_writer->WriteDynamicSection();
David Srbecky32210b92017-12-04 14:39:21 +0000337 elf_writer->WriteDebugInfo(oat_writer->GetDebugInfo());
Vladimir Marko1352f132017-04-28 15:28:29 +0100338
339 bool success = elf_writer->End();
340 ASSERT_TRUE(success);
341 }
342 }
343
344 bool success_image = writer->Write(kInvalidFd,
Vladimir Markod011d812018-11-29 12:35:24 +0000345 image_filenames,
346 oat_filenames);
Vladimir Marko1352f132017-04-28 15:28:29 +0100347 ASSERT_TRUE(success_image);
348
349 for (size_t i = 0, size = oat_filenames.size(); i != size; ++i) {
350 const char* oat_filename = oat_filenames[i].c_str();
351 std::unique_ptr<File> oat_file(OS::OpenFileReadWrite(oat_filename));
352 ASSERT_TRUE(oat_file != nullptr);
Vladimir Marko213ee2d2018-06-22 11:56:34 +0100353 bool success_fixup = ElfWriter::Fixup(oat_file.get(), writer->GetOatDataBegin(i));
Vladimir Marko1352f132017-04-28 15:28:29 +0100354 ASSERT_TRUE(success_fixup);
355 ASSERT_EQ(oat_file->FlushCloseOrErase(), 0) << "Could not flush and close oat file "
356 << oat_filename;
357 }
358 }
359}
360
Vladimir Markod79b37b2018-11-02 13:06:22 +0000361inline void ImageTest::Compile(
362 ImageHeader::StorageMode storage_mode,
Mathieu Chartier1a842962018-11-13 15:09:51 -0800363 uint32_t max_image_block_size,
Vladimir Markod79b37b2018-11-02 13:06:22 +0000364 CompilationHelper& helper,
365 const std::string& extra_dex,
366 const std::initializer_list<std::string>& image_classes,
367 const std::initializer_list<std::string>& image_classes_failing_aot_clinit) {
368 for (const std::string& image_class : image_classes_failing_aot_clinit) {
369 ASSERT_TRUE(ContainsElement(image_classes, image_class));
370 }
Vladimir Marko1352f132017-04-28 15:28:29 +0100371 for (const std::string& image_class : image_classes) {
372 image_classes_.insert(image_class);
373 }
Vladimir Markoa0431112018-06-25 09:32:54 +0100374 number_of_threads_ = kIsTargetBuild ? 2U : 16U;
375 CreateCompilerDriver();
Vladimir Marko1352f132017-04-28 15:28:29 +0100376 // Set inline filter values.
377 compiler_options_->SetInlineMaxCodeUnits(CompilerOptions::kDefaultInlineMaxCodeUnits);
Mathieu Chartier1a842962018-11-13 15:09:51 -0800378 compiler_options_->SetMaxImageBlockSize(max_image_block_size);
Vladimir Marko1352f132017-04-28 15:28:29 +0100379 image_classes_.clear();
380 if (!extra_dex.empty()) {
381 helper.extra_dex_files = OpenTestDexFiles(extra_dex.c_str());
382 }
Vladimir Marko213ee2d2018-06-22 11:56:34 +0100383 DoCompile(storage_mode, helper);
Vladimir Marko1352f132017-04-28 15:28:29 +0100384 if (image_classes.begin() != image_classes.end()) {
385 // Make sure the class got initialized.
386 ScopedObjectAccess soa(Thread::Current());
387 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
388 for (const std::string& image_class : image_classes) {
Vladimir Markoe9987b02018-05-22 16:26:43 +0100389 ObjPtr<mirror::Class> klass =
390 class_linker->FindSystemClass(Thread::Current(), image_class.c_str());
Vladimir Marko1352f132017-04-28 15:28:29 +0100391 EXPECT_TRUE(klass != nullptr);
Vladimir Markod79b37b2018-11-02 13:06:22 +0000392 EXPECT_TRUE(klass->IsResolved());
393 if (ContainsElement(image_classes_failing_aot_clinit, image_class)) {
394 EXPECT_FALSE(klass->IsInitialized());
395 } else {
396 EXPECT_TRUE(klass->IsInitialized());
397 }
Vladimir Marko1352f132017-04-28 15:28:29 +0100398 }
399 }
400}
401
Mathieu Chartier1a842962018-11-13 15:09:51 -0800402inline void ImageTest::TestWriteRead(ImageHeader::StorageMode storage_mode,
403 uint32_t max_image_block_size) {
Vladimir Marko1352f132017-04-28 15:28:29 +0100404 CompilationHelper helper;
Mathieu Chartier1a842962018-11-13 15:09:51 -0800405 Compile(storage_mode, max_image_block_size, /*out*/ helper);
Vladimir Marko1352f132017-04-28 15:28:29 +0100406 std::vector<uint64_t> image_file_sizes;
407 for (ScratchFile& image_file : helper.image_files) {
408 std::unique_ptr<File> file(OS::OpenFileForReading(image_file.GetFilename().c_str()));
409 ASSERT_TRUE(file.get() != nullptr);
410 ImageHeader image_header;
411 ASSERT_EQ(file->ReadFully(&image_header, sizeof(image_header)), true);
412 ASSERT_TRUE(image_header.IsValid());
Vladimir Markocd87c3e2017-09-05 13:11:57 +0100413 const auto& bitmap_section = image_header.GetImageBitmapSection();
Vladimir Marko1352f132017-04-28 15:28:29 +0100414 ASSERT_GE(bitmap_section.Offset(), sizeof(image_header));
415 ASSERT_NE(0U, bitmap_section.Size());
416
417 gc::Heap* heap = Runtime::Current()->GetHeap();
418 ASSERT_TRUE(heap->HaveContinuousSpaces());
419 gc::space::ContinuousSpace* space = heap->GetNonMovingSpace();
420 ASSERT_FALSE(space->IsImageSpace());
421 ASSERT_TRUE(space != nullptr);
422 ASSERT_TRUE(space->IsMallocSpace());
423 image_file_sizes.push_back(file->GetLength());
424 }
425
Vladimir Marko1352f132017-04-28 15:28:29 +0100426 // Need to delete the compiler since it has worker threads which are attached to runtime.
427 compiler_driver_.reset();
428
429 // Tear down old runtime before making a new one, clearing out misc state.
430
431 // Remove the reservation of the memory for use to load the image.
432 // Need to do this before we reset the runtime.
433 UnreserveImageSpace();
434
435 helper.extra_dex_files.clear();
436 runtime_.reset();
437 java_lang_dex_file_ = nullptr;
438
439 MemMap::Init();
440
441 RuntimeOptions options;
Vladimir Marko7a85e702018-12-03 18:47:23 +0000442 options.emplace_back(GetClassPathOption("-Xbootclasspath:", GetLibCoreDexFileNames()), nullptr);
443 options.emplace_back(
444 GetClassPathOption("-Xbootclasspath-locations:", GetLibCoreDexLocations()), nullptr);
Vladimir Marko1352f132017-04-28 15:28:29 +0100445 std::string image("-Ximage:");
446 image.append(helper.image_locations[0].GetFilename());
447 options.push_back(std::make_pair(image.c_str(), static_cast<void*>(nullptr)));
448 // By default the compiler this creates will not include patch information.
449 options.push_back(std::make_pair("-Xnorelocate", nullptr));
450
451 if (!Runtime::Create(options, false)) {
452 LOG(FATAL) << "Failed to create runtime";
453 return;
454 }
455 runtime_.reset(Runtime::Current());
456 // Runtime::Create acquired the mutator_lock_ that is normally given away when we Runtime::Start,
457 // give it away now and then switch to a more managable ScopedObjectAccess.
458 Thread::Current()->TransitionFromRunnableToSuspended(kNative);
459 ScopedObjectAccess soa(Thread::Current());
460 ASSERT_TRUE(runtime_.get() != nullptr);
461 class_linker_ = runtime_->GetClassLinker();
462
463 gc::Heap* heap = Runtime::Current()->GetHeap();
464 ASSERT_TRUE(heap->HasBootImageSpace());
465 ASSERT_TRUE(heap->GetNonMovingSpace()->IsMallocSpace());
466
467 // We loaded the runtime with an explicit image, so it must exist.
468 ASSERT_EQ(heap->GetBootImageSpaces().size(), image_file_sizes.size());
Vladimir Markodc4bcce2018-06-21 16:15:42 +0100469 const HashSet<std::string>& image_classes = compiler_options_->GetImageClasses();
Vladimir Marko1352f132017-04-28 15:28:29 +0100470 for (size_t i = 0; i < helper.dex_file_locations.size(); ++i) {
471 std::unique_ptr<const DexFile> dex(
472 LoadExpectSingleDexFile(helper.dex_file_locations[i].c_str()));
473 ASSERT_TRUE(dex != nullptr);
474 uint64_t image_file_size = image_file_sizes[i];
475 gc::space::ImageSpace* image_space = heap->GetBootImageSpaces()[i];
476 ASSERT_TRUE(image_space != nullptr);
477 if (storage_mode == ImageHeader::kStorageModeUncompressed) {
478 // Uncompressed, image should be smaller than file.
479 ASSERT_LE(image_space->GetImageHeader().GetImageSize(), image_file_size);
480 } else if (image_file_size > 16 * KB) {
481 // Compressed, file should be smaller than image. Not really valid for small images.
482 ASSERT_LE(image_file_size, image_space->GetImageHeader().GetImageSize());
Mathieu Chartier1a842962018-11-13 15:09:51 -0800483 // TODO: Actually validate the blocks, this is hard since the blocks are not copied over for
484 // compressed images. Add kPageSize since image_size is rounded up to this.
485 ASSERT_GT(image_space->GetImageHeader().GetBlockCount() * max_image_block_size,
486 image_space->GetImageHeader().GetImageSize() - kPageSize);
Vladimir Marko1352f132017-04-28 15:28:29 +0100487 }
488
489 image_space->VerifyImageAllocations();
490 uint8_t* image_begin = image_space->Begin();
491 uint8_t* image_end = image_space->End();
492 if (i == 0) {
493 // This check is only valid for image 0.
494 CHECK_EQ(kRequestedImageBase, reinterpret_cast<uintptr_t>(image_begin));
495 }
496 for (size_t j = 0; j < dex->NumClassDefs(); ++j) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800497 const dex::ClassDef& class_def = dex->GetClassDef(j);
Vladimir Marko1352f132017-04-28 15:28:29 +0100498 const char* descriptor = dex->GetClassDescriptor(class_def);
Vladimir Markoe9987b02018-05-22 16:26:43 +0100499 ObjPtr<mirror::Class> klass = class_linker_->FindSystemClass(soa.Self(), descriptor);
Vladimir Marko1352f132017-04-28 15:28:29 +0100500 EXPECT_TRUE(klass != nullptr) << descriptor;
Vladimir Markoe9987b02018-05-22 16:26:43 +0100501 uint8_t* raw_klass = reinterpret_cast<uint8_t*>(klass.Ptr());
Vladimir Marko2ef01102019-02-05 15:05:10 +0000502 if (image_classes.find(std::string_view(descriptor)) == image_classes.end()) {
Vladimir Markoe9987b02018-05-22 16:26:43 +0100503 EXPECT_TRUE(raw_klass >= image_end || raw_klass < image_begin) << descriptor;
Vladimir Marko1352f132017-04-28 15:28:29 +0100504 } else {
505 // Image classes should be located inside the image.
Vladimir Markoe9987b02018-05-22 16:26:43 +0100506 EXPECT_LT(image_begin, raw_klass) << descriptor;
507 EXPECT_LT(raw_klass, image_end) << descriptor;
Vladimir Marko1352f132017-04-28 15:28:29 +0100508 }
509 EXPECT_TRUE(Monitor::IsValidLockWord(klass->GetLockWord(false)));
510 }
511 }
512}
513
Vladimir Marko74527972016-11-29 15:57:32 +0000514} // namespace linker
Vladimir Marko1352f132017-04-28 15:28:29 +0100515} // namespace art
516
Vladimir Marko74527972016-11-29 15:57:32 +0000517#endif // ART_DEX2OAT_LINKER_IMAGE_TEST_H_