blob: 6ffcef12a70894200715b5a7a788e544c3903ccd [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>
24#include <vector>
25
26#include "android-base/stringprintf.h"
27
28#include "art_method-inl.h"
David Sehr891a50e2017-10-27 17:01:07 -070029#include "base/file_utils.h"
Vladimir Marko54159c62018-06-20 14:30:08 +010030#include "base/hash_set.h"
Vladimir Markod79b37b2018-11-02 13:06:22 +000031#include "base/stl_util.h"
Vladimir Marko1352f132017-04-28 15:28:29 +010032#include "base/unix_file/fd_file.h"
David Sehrc431b9d2018-03-02 12:01:51 -080033#include "base/utils.h"
Vladimir Marko1352f132017-04-28 15:28:29 +010034#include "class_linker-inl.h"
Vladimir Marko1352f132017-04-28 15:28:29 +010035#include "common_compiler_test.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070036#include "compiler_callbacks.h"
Vladimir Marko1352f132017-04-28 15:28:29 +010037#include "debug/method_debug_info.h"
38#include "dex/quick_compiler_callbacks.h"
Vladimir Markoa0431112018-06-25 09:32:54 +010039#include "driver/compiler_driver.h"
Vladimir Marko1352f132017-04-28 15:28:29 +010040#include "driver/compiler_options.h"
Vladimir Marko1352f132017-04-28 15:28:29 +010041#include "gc/space/image_space.h"
42#include "image_writer.h"
43#include "linker/buffered_output_stream.h"
Vladimir Marko74527972016-11-29 15:57:32 +000044#include "linker/elf_writer.h"
45#include "linker/elf_writer_quick.h"
Vladimir Marko1352f132017-04-28 15:28:29 +010046#include "linker/file_output_stream.h"
47#include "linker/multi_oat_relative_patcher.h"
48#include "lock_word.h"
49#include "mirror/object-inl.h"
50#include "oat_writer.h"
51#include "scoped_thread_state_change-inl.h"
52#include "signal_catcher.h"
Vladimir Marko1352f132017-04-28 15:28:29 +010053
54namespace art {
Vladimir Marko74527972016-11-29 15:57:32 +000055namespace linker {
Vladimir Marko1352f132017-04-28 15:28:29 +010056
57static const uintptr_t kRequestedImageBase = ART_BASE_ADDRESS;
58
59struct CompilationHelper {
60 std::vector<std::string> dex_file_locations;
61 std::vector<ScratchFile> image_locations;
62 std::vector<std::unique_ptr<const DexFile>> extra_dex_files;
63 std::vector<ScratchFile> image_files;
64 std::vector<ScratchFile> oat_files;
65 std::vector<ScratchFile> vdex_files;
66 std::string image_dir;
67
Vladimir Marko1352f132017-04-28 15:28:29 +010068 std::vector<size_t> GetImageObjectSectionSizes();
69
70 ~CompilationHelper();
71};
72
73class ImageTest : public CommonCompilerTest {
74 protected:
75 virtual void SetUp() {
76 ReserveImageSpace();
77 CommonCompilerTest::SetUp();
78 }
79
Mathieu Chartier1a842962018-11-13 15:09:51 -080080 void TestWriteRead(ImageHeader::StorageMode storage_mode, uint32_t max_image_block_size);
Vladimir Marko1352f132017-04-28 15:28:29 +010081
82 void Compile(ImageHeader::StorageMode storage_mode,
Mathieu Chartier1a842962018-11-13 15:09:51 -080083 uint32_t max_image_block_size,
Vladimir Marko213ee2d2018-06-22 11:56:34 +010084 /*out*/ CompilationHelper& out_helper,
Vladimir Marko1352f132017-04-28 15:28:29 +010085 const std::string& extra_dex = "",
Vladimir Markod79b37b2018-11-02 13:06:22 +000086 const std::initializer_list<std::string>& image_classes = {},
87 const std::initializer_list<std::string>& image_classes_failing_aot_clinit = {});
Vladimir Marko1352f132017-04-28 15:28:29 +010088
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010089 void SetUpRuntimeOptions(RuntimeOptions* options) override {
Vladimir Marko1352f132017-04-28 15:28:29 +010090 CommonCompilerTest::SetUpRuntimeOptions(options);
Mathieu Chartiere01b6f62017-07-19 16:55:04 -070091 QuickCompilerCallbacks* new_callbacks =
92 new QuickCompilerCallbacks(CompilerCallbacks::CallbackMode::kCompileBootImage);
93 new_callbacks->SetVerificationResults(verification_results_.get());
94 callbacks_.reset(new_callbacks);
Vladimir Marko1352f132017-04-28 15:28:29 +010095 options->push_back(std::make_pair("compilercallbacks", callbacks_.get()));
96 }
97
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010098 std::unique_ptr<HashSet<std::string>> GetImageClasses() override {
Vladimir Marko54159c62018-06-20 14:30:08 +010099 return std::make_unique<HashSet<std::string>>(image_classes_);
Vladimir Marko1352f132017-04-28 15:28:29 +0100100 }
101
Vladimir Markoa8bba7d2018-05-30 15:18:48 +0100102 ArtMethod* FindCopiedMethod(ArtMethod* origin, ObjPtr<mirror::Class> klass)
Vladimir Marko1352f132017-04-28 15:28:29 +0100103 REQUIRES_SHARED(Locks::mutator_lock_) {
104 PointerSize pointer_size = class_linker_->GetImagePointerSize();
105 for (ArtMethod& m : klass->GetCopiedMethods(pointer_size)) {
106 if (strcmp(origin->GetName(), m.GetName()) == 0 &&
107 origin->GetSignature() == m.GetSignature()) {
108 return &m;
109 }
110 }
111 return nullptr;
112 }
113
114 private:
Vladimir Marko213ee2d2018-06-22 11:56:34 +0100115 void DoCompile(ImageHeader::StorageMode storage_mode, /*out*/ CompilationHelper& out_helper);
116
Vladimir Marko54159c62018-06-20 14:30:08 +0100117 HashSet<std::string> image_classes_;
Vladimir Marko1352f132017-04-28 15:28:29 +0100118};
119
120inline CompilationHelper::~CompilationHelper() {
121 for (ScratchFile& image_file : image_files) {
122 image_file.Unlink();
123 }
124 for (ScratchFile& oat_file : oat_files) {
125 oat_file.Unlink();
126 }
127 for (ScratchFile& vdex_file : vdex_files) {
128 vdex_file.Unlink();
129 }
130 const int rmdir_result = rmdir(image_dir.c_str());
131 CHECK_EQ(0, rmdir_result);
132}
133
134inline std::vector<size_t> CompilationHelper::GetImageObjectSectionSizes() {
135 std::vector<size_t> ret;
136 for (ScratchFile& image_file : image_files) {
137 std::unique_ptr<File> file(OS::OpenFileForReading(image_file.GetFilename().c_str()));
138 CHECK(file.get() != nullptr);
139 ImageHeader image_header;
140 CHECK_EQ(file->ReadFully(&image_header, sizeof(image_header)), true);
141 CHECK(image_header.IsValid());
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100142 ret.push_back(image_header.GetObjectsSection().Size());
Vladimir Marko1352f132017-04-28 15:28:29 +0100143 }
144 return ret;
145}
146
Vladimir Marko213ee2d2018-06-22 11:56:34 +0100147inline void ImageTest::DoCompile(ImageHeader::StorageMode storage_mode,
148 /*out*/ CompilationHelper& out_helper) {
149 CompilerDriver* driver = compiler_driver_.get();
Vladimir Marko1352f132017-04-28 15:28:29 +0100150 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
151 std::vector<const DexFile*> class_path = class_linker->GetBootClassPath();
152
Vladimir Marko213ee2d2018-06-22 11:56:34 +0100153 for (const std::unique_ptr<const DexFile>& dex_file : out_helper.extra_dex_files) {
Vladimir Marko1352f132017-04-28 15:28:29 +0100154 {
155 ScopedObjectAccess soa(Thread::Current());
156 // Inject in boot class path so that the compiler driver can see it.
157 class_linker->AppendToBootClassPath(soa.Self(), *dex_file.get());
158 }
159 class_path.push_back(dex_file.get());
160 }
161
162 // Enable write for dex2dex.
163 for (const DexFile* dex_file : class_path) {
Vladimir Marko213ee2d2018-06-22 11:56:34 +0100164 out_helper.dex_file_locations.push_back(dex_file->GetLocation());
Vladimir Marko1352f132017-04-28 15:28:29 +0100165 if (dex_file->IsReadOnly()) {
166 dex_file->EnableWrite();
167 }
168 }
169 {
170 // Create a generic tmp file, to be the base of the .art and .oat temporary files.
171 ScratchFile location;
172 for (int i = 0; i < static_cast<int>(class_path.size()); ++i) {
173 std::string cur_location =
174 android::base::StringPrintf("%s-%d.art", location.GetFilename().c_str(), i);
Vladimir Marko213ee2d2018-06-22 11:56:34 +0100175 out_helper.image_locations.push_back(ScratchFile(cur_location));
Vladimir Marko1352f132017-04-28 15:28:29 +0100176 }
177 }
178 std::vector<std::string> image_filenames;
Vladimir Marko213ee2d2018-06-22 11:56:34 +0100179 for (ScratchFile& file : out_helper.image_locations) {
Vladimir Marko1352f132017-04-28 15:28:29 +0100180 std::string image_filename(GetSystemImageFilename(file.GetFilename().c_str(), kRuntimeISA));
181 image_filenames.push_back(image_filename);
182 size_t pos = image_filename.rfind('/');
183 CHECK_NE(pos, std::string::npos) << image_filename;
Vladimir Marko213ee2d2018-06-22 11:56:34 +0100184 if (out_helper.image_dir.empty()) {
185 out_helper.image_dir = image_filename.substr(0, pos);
186 int mkdir_result = mkdir(out_helper.image_dir.c_str(), 0700);
187 CHECK_EQ(0, mkdir_result) << out_helper.image_dir;
Vladimir Marko1352f132017-04-28 15:28:29 +0100188 }
Vladimir Marko213ee2d2018-06-22 11:56:34 +0100189 out_helper.image_files.push_back(ScratchFile(OS::CreateEmptyFile(image_filename.c_str())));
Vladimir Marko1352f132017-04-28 15:28:29 +0100190 }
191
192 std::vector<std::string> oat_filenames;
193 std::vector<std::string> vdex_filenames;
194 for (const std::string& image_filename : image_filenames) {
195 std::string oat_filename = ReplaceFileExtension(image_filename, "oat");
Vladimir Marko213ee2d2018-06-22 11:56:34 +0100196 out_helper.oat_files.push_back(ScratchFile(OS::CreateEmptyFile(oat_filename.c_str())));
Vladimir Marko1352f132017-04-28 15:28:29 +0100197 oat_filenames.push_back(oat_filename);
198 std::string vdex_filename = ReplaceFileExtension(image_filename, "vdex");
Vladimir Marko213ee2d2018-06-22 11:56:34 +0100199 out_helper.vdex_files.push_back(ScratchFile(OS::CreateEmptyFile(vdex_filename.c_str())));
Vladimir Marko1352f132017-04-28 15:28:29 +0100200 vdex_filenames.push_back(vdex_filename);
201 }
202
203 std::unordered_map<const DexFile*, size_t> dex_file_to_oat_index_map;
Vladimir Marko1352f132017-04-28 15:28:29 +0100204 size_t image_idx = 0;
205 for (const DexFile* dex_file : class_path) {
206 dex_file_to_oat_index_map.emplace(dex_file, image_idx);
207 ++image_idx;
208 }
209 // TODO: compile_pic should be a test argument.
Vladimir Markoa0431112018-06-25 09:32:54 +0100210 std::unique_ptr<ImageWriter> writer(new ImageWriter(*compiler_options_,
Vladimir Marko1352f132017-04-28 15:28:29 +0100211 kRequestedImageBase,
Vladimir Marko1352f132017-04-28 15:28:29 +0100212 storage_mode,
Vladimir Markod011d812018-11-29 12:35:24 +0000213 oat_filenames,
Jeff Haoc23b0c02017-07-27 18:19:38 -0700214 dex_file_to_oat_index_map,
Nicolas Geoffrayf0d30022018-11-20 17:45:38 +0000215 /*class_loader=*/ nullptr,
Vladimir Marko9c4b9702018-11-14 15:09:02 +0000216 /*dirty_image_objects=*/ nullptr));
Vladimir Marko1352f132017-04-28 15:28:29 +0100217 {
218 {
219 jobject class_loader = nullptr;
220 TimingLogger timings("ImageTest::WriteRead", false, false);
221 TimingLogger::ScopedTiming t("CompileAll", &timings);
Vladimir Marko213ee2d2018-06-22 11:56:34 +0100222 SetDexFilesForOatFile(class_path);
Nicolas Geoffray1cfea7a2017-05-24 14:44:38 +0100223 driver->CompileAll(class_loader, class_path, &timings);
Vladimir Marko1352f132017-04-28 15:28:29 +0100224
225 t.NewTiming("WriteElf");
226 SafeMap<std::string, std::string> key_value_store;
Vladimir Marko1352f132017-04-28 15:28:29 +0100227 key_value_store.Put(OatHeader::kBootClassPathKey,
228 gc::space::ImageSpace::GetMultiImageBootClassPath(
Vladimir Markod011d812018-11-29 12:35:24 +0000229 out_helper.dex_file_locations,
230 oat_filenames,
231 image_filenames));
Vladimir Marko1352f132017-04-28 15:28:29 +0100232
233 std::vector<std::unique_ptr<ElfWriter>> elf_writers;
234 std::vector<std::unique_ptr<OatWriter>> oat_writers;
Vladimir Marko213ee2d2018-06-22 11:56:34 +0100235 for (ScratchFile& oat_file : out_helper.oat_files) {
Vladimir Markoa0431112018-06-25 09:32:54 +0100236 elf_writers.emplace_back(CreateElfWriterQuick(*compiler_options_, oat_file.GetFile()));
Vladimir Marko1352f132017-04-28 15:28:29 +0100237 elf_writers.back()->Start();
Vladimir Markoa0431112018-06-25 09:32:54 +0100238 oat_writers.emplace_back(new OatWriter(*compiler_options_,
Vladimir Marko1352f132017-04-28 15:28:29 +0100239 &timings,
Mathieu Chartier603ccab2017-10-20 14:34:28 -0700240 /*profile_compilation_info*/nullptr,
241 CompactDexLevel::kCompactDexLevelNone));
Vladimir Marko1352f132017-04-28 15:28:29 +0100242 }
243
244 std::vector<OutputStream*> rodata;
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100245 std::vector<MemMap> opened_dex_files_maps;
Vladimir Marko1352f132017-04-28 15:28:29 +0100246 std::vector<std::unique_ptr<const DexFile>> opened_dex_files;
247 // Now that we have finalized key_value_store_, start writing the oat file.
248 for (size_t i = 0, size = oat_writers.size(); i != size; ++i) {
249 const DexFile* dex_file = class_path[i];
250 rodata.push_back(elf_writers[i]->StartRoData());
251 ArrayRef<const uint8_t> raw_dex_file(
252 reinterpret_cast<const uint8_t*>(&dex_file->GetHeader()),
253 dex_file->GetHeader().file_size_);
254 oat_writers[i]->AddRawDexFileSource(raw_dex_file,
255 dex_file->GetLocation().c_str(),
256 dex_file->GetLocationChecksum());
257
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100258 std::vector<MemMap> cur_opened_dex_files_maps;
Vladimir Marko1352f132017-04-28 15:28:29 +0100259 std::vector<std::unique_ptr<const DexFile>> cur_opened_dex_files;
260 bool dex_files_ok = oat_writers[i]->WriteAndOpenDexFiles(
Vladimir Marko213ee2d2018-06-22 11:56:34 +0100261 out_helper.vdex_files[i].GetFile(),
Vladimir Marko1352f132017-04-28 15:28:29 +0100262 rodata.back(),
Vladimir Markod011d812018-11-29 12:35:24 +0000263 (i == 0u) ? &key_value_store : nullptr,
Vladimir Marko1352f132017-04-28 15:28:29 +0100264 /* verify */ false, // Dex files may be dex-to-dex-ed, don't verify.
265 /* update_input_vdex */ false,
Nicolas Geoffray66ff8a82018-02-28 13:27:55 +0000266 /* copy_dex_files */ CopyOption::kOnlyIfCompressed,
Nicolas Geoffrayf3075272018-01-08 12:41:19 +0000267 &cur_opened_dex_files_maps,
Vladimir Marko1352f132017-04-28 15:28:29 +0100268 &cur_opened_dex_files);
269 ASSERT_TRUE(dex_files_ok);
270
Nicolas Geoffrayf3075272018-01-08 12:41:19 +0000271 if (!cur_opened_dex_files_maps.empty()) {
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100272 for (MemMap& cur_map : cur_opened_dex_files_maps) {
Nicolas Geoffrayf3075272018-01-08 12:41:19 +0000273 opened_dex_files_maps.push_back(std::move(cur_map));
274 }
Vladimir Marko1352f132017-04-28 15:28:29 +0100275 for (std::unique_ptr<const DexFile>& cur_dex_file : cur_opened_dex_files) {
276 // dex_file_oat_index_map_.emplace(dex_file.get(), i);
277 opened_dex_files.push_back(std::move(cur_dex_file));
278 }
279 } else {
280 ASSERT_TRUE(cur_opened_dex_files.empty());
281 }
282 }
Mathieu Chartier703b82a2018-04-10 15:52:32 -0700283 bool image_space_ok = writer->PrepareImageAddressSpace(&timings);
Vladimir Marko1352f132017-04-28 15:28:29 +0100284 ASSERT_TRUE(image_space_ok);
285
Vladimir Marko213ee2d2018-06-22 11:56:34 +0100286 DCHECK_EQ(out_helper.vdex_files.size(), out_helper.oat_files.size());
287 for (size_t i = 0, size = out_helper.oat_files.size(); i != size; ++i) {
Vladimir Markoa0431112018-06-25 09:32:54 +0100288 MultiOatRelativePatcher patcher(compiler_options_->GetInstructionSet(),
289 compiler_options_->GetInstructionSetFeatures(),
Vladimir Markoca1e0382018-04-11 09:58:41 +0000290 driver->GetCompiledMethodStorage());
Vladimir Marko1352f132017-04-28 15:28:29 +0100291 OatWriter* const oat_writer = oat_writers[i].get();
292 ElfWriter* const elf_writer = elf_writers[i].get();
293 std::vector<const DexFile*> cur_dex_files(1u, class_path[i]);
294 oat_writer->Initialize(driver, writer.get(), cur_dex_files);
David Brazdil93592f52017-12-08 10:53:27 +0000295
296 std::unique_ptr<BufferedOutputStream> vdex_out =
297 std::make_unique<BufferedOutputStream>(
Vladimir Marko213ee2d2018-06-22 11:56:34 +0100298 std::make_unique<FileOutputStream>(out_helper.vdex_files[i].GetFile()));
David Brazdil93592f52017-12-08 10:53:27 +0000299 oat_writer->WriteVerifierDeps(vdex_out.get(), nullptr);
300 oat_writer->WriteQuickeningInfo(vdex_out.get());
301 oat_writer->WriteChecksumsAndVdexHeader(vdex_out.get());
302
Vladimir Marko1352f132017-04-28 15:28:29 +0100303 oat_writer->PrepareLayout(&patcher);
Vladimir Markob066d432018-01-03 13:14:37 +0000304 elf_writer->PrepareDynamicSection(oat_writer->GetOatHeader().GetExecutableOffset(),
305 oat_writer->GetCodeSize(),
306 oat_writer->GetDataBimgRelRoSize(),
Vladimir Marko1352f132017-04-28 15:28:29 +0100307 oat_writer->GetBssSize(),
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100308 oat_writer->GetBssMethodsOffset(),
David Srbeckyec2cdf42017-12-08 16:21:25 +0000309 oat_writer->GetBssRootsOffset(),
310 oat_writer->GetVdexSize());
Vladimir Marko1352f132017-04-28 15:28:29 +0100311
312 writer->UpdateOatFileLayout(i,
313 elf_writer->GetLoadedSize(),
314 oat_writer->GetOatDataOffset(),
315 oat_writer->GetOatSize());
316
317 bool rodata_ok = oat_writer->WriteRodata(rodata[i]);
318 ASSERT_TRUE(rodata_ok);
319 elf_writer->EndRoData(rodata[i]);
320
321 OutputStream* text = elf_writer->StartText();
322 bool text_ok = oat_writer->WriteCode(text);
323 ASSERT_TRUE(text_ok);
324 elf_writer->EndText(text);
325
Vladimir Markob066d432018-01-03 13:14:37 +0000326 if (oat_writer->GetDataBimgRelRoSize() != 0u) {
327 OutputStream* data_bimg_rel_ro = elf_writer->StartDataBimgRelRo();
328 bool data_bimg_rel_ro_ok = oat_writer->WriteDataBimgRelRo(data_bimg_rel_ro);
329 ASSERT_TRUE(data_bimg_rel_ro_ok);
330 elf_writer->EndDataBimgRelRo(data_bimg_rel_ro);
331 }
332
Vladimir Markoe0669322018-09-03 15:44:54 +0100333 bool header_ok = oat_writer->WriteHeader(elf_writer->GetStream(),
Vladimir Markoc10a0c62018-11-16 11:39:22 +0000334 /*boot_image_checksum=*/ 0u);
Vladimir Marko1352f132017-04-28 15:28:29 +0100335 ASSERT_TRUE(header_ok);
336
337 writer->UpdateOatFileHeader(i, oat_writer->GetOatHeader());
338
339 elf_writer->WriteDynamicSection();
David Srbecky32210b92017-12-04 14:39:21 +0000340 elf_writer->WriteDebugInfo(oat_writer->GetDebugInfo());
Vladimir Marko1352f132017-04-28 15:28:29 +0100341
342 bool success = elf_writer->End();
343 ASSERT_TRUE(success);
344 }
345 }
346
347 bool success_image = writer->Write(kInvalidFd,
Vladimir Markod011d812018-11-29 12:35:24 +0000348 image_filenames,
349 oat_filenames);
Vladimir Marko1352f132017-04-28 15:28:29 +0100350 ASSERT_TRUE(success_image);
351
352 for (size_t i = 0, size = oat_filenames.size(); i != size; ++i) {
353 const char* oat_filename = oat_filenames[i].c_str();
354 std::unique_ptr<File> oat_file(OS::OpenFileReadWrite(oat_filename));
355 ASSERT_TRUE(oat_file != nullptr);
Vladimir Marko213ee2d2018-06-22 11:56:34 +0100356 bool success_fixup = ElfWriter::Fixup(oat_file.get(), writer->GetOatDataBegin(i));
Vladimir Marko1352f132017-04-28 15:28:29 +0100357 ASSERT_TRUE(success_fixup);
358 ASSERT_EQ(oat_file->FlushCloseOrErase(), 0) << "Could not flush and close oat file "
359 << oat_filename;
360 }
361 }
362}
363
Vladimir Markod79b37b2018-11-02 13:06:22 +0000364inline void ImageTest::Compile(
365 ImageHeader::StorageMode storage_mode,
Mathieu Chartier1a842962018-11-13 15:09:51 -0800366 uint32_t max_image_block_size,
Vladimir Markod79b37b2018-11-02 13:06:22 +0000367 CompilationHelper& helper,
368 const std::string& extra_dex,
369 const std::initializer_list<std::string>& image_classes,
370 const std::initializer_list<std::string>& image_classes_failing_aot_clinit) {
371 for (const std::string& image_class : image_classes_failing_aot_clinit) {
372 ASSERT_TRUE(ContainsElement(image_classes, image_class));
373 }
Vladimir Marko1352f132017-04-28 15:28:29 +0100374 for (const std::string& image_class : image_classes) {
375 image_classes_.insert(image_class);
376 }
Vladimir Markoa0431112018-06-25 09:32:54 +0100377 number_of_threads_ = kIsTargetBuild ? 2U : 16U;
378 CreateCompilerDriver();
Vladimir Marko1352f132017-04-28 15:28:29 +0100379 // Set inline filter values.
380 compiler_options_->SetInlineMaxCodeUnits(CompilerOptions::kDefaultInlineMaxCodeUnits);
Mathieu Chartier1a842962018-11-13 15:09:51 -0800381 compiler_options_->SetMaxImageBlockSize(max_image_block_size);
Vladimir Marko1352f132017-04-28 15:28:29 +0100382 image_classes_.clear();
383 if (!extra_dex.empty()) {
384 helper.extra_dex_files = OpenTestDexFiles(extra_dex.c_str());
385 }
Vladimir Marko213ee2d2018-06-22 11:56:34 +0100386 DoCompile(storage_mode, helper);
Vladimir Marko1352f132017-04-28 15:28:29 +0100387 if (image_classes.begin() != image_classes.end()) {
388 // Make sure the class got initialized.
389 ScopedObjectAccess soa(Thread::Current());
390 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
391 for (const std::string& image_class : image_classes) {
Vladimir Markoe9987b02018-05-22 16:26:43 +0100392 ObjPtr<mirror::Class> klass =
393 class_linker->FindSystemClass(Thread::Current(), image_class.c_str());
Vladimir Marko1352f132017-04-28 15:28:29 +0100394 EXPECT_TRUE(klass != nullptr);
Vladimir Markod79b37b2018-11-02 13:06:22 +0000395 EXPECT_TRUE(klass->IsResolved());
396 if (ContainsElement(image_classes_failing_aot_clinit, image_class)) {
397 EXPECT_FALSE(klass->IsInitialized());
398 } else {
399 EXPECT_TRUE(klass->IsInitialized());
400 }
Vladimir Marko1352f132017-04-28 15:28:29 +0100401 }
402 }
403}
404
Mathieu Chartier1a842962018-11-13 15:09:51 -0800405inline void ImageTest::TestWriteRead(ImageHeader::StorageMode storage_mode,
406 uint32_t max_image_block_size) {
Vladimir Marko1352f132017-04-28 15:28:29 +0100407 CompilationHelper helper;
Mathieu Chartier1a842962018-11-13 15:09:51 -0800408 Compile(storage_mode, max_image_block_size, /*out*/ helper);
Vladimir Marko1352f132017-04-28 15:28:29 +0100409 std::vector<uint64_t> image_file_sizes;
410 for (ScratchFile& image_file : helper.image_files) {
411 std::unique_ptr<File> file(OS::OpenFileForReading(image_file.GetFilename().c_str()));
412 ASSERT_TRUE(file.get() != nullptr);
413 ImageHeader image_header;
414 ASSERT_EQ(file->ReadFully(&image_header, sizeof(image_header)), true);
415 ASSERT_TRUE(image_header.IsValid());
Vladimir Markocd87c3e2017-09-05 13:11:57 +0100416 const auto& bitmap_section = image_header.GetImageBitmapSection();
Vladimir Marko1352f132017-04-28 15:28:29 +0100417 ASSERT_GE(bitmap_section.Offset(), sizeof(image_header));
418 ASSERT_NE(0U, bitmap_section.Size());
419
420 gc::Heap* heap = Runtime::Current()->GetHeap();
421 ASSERT_TRUE(heap->HaveContinuousSpaces());
422 gc::space::ContinuousSpace* space = heap->GetNonMovingSpace();
423 ASSERT_FALSE(space->IsImageSpace());
424 ASSERT_TRUE(space != nullptr);
425 ASSERT_TRUE(space->IsMallocSpace());
426 image_file_sizes.push_back(file->GetLength());
427 }
428
Vladimir Marko1352f132017-04-28 15:28:29 +0100429 // Need to delete the compiler since it has worker threads which are attached to runtime.
430 compiler_driver_.reset();
431
432 // Tear down old runtime before making a new one, clearing out misc state.
433
434 // Remove the reservation of the memory for use to load the image.
435 // Need to do this before we reset the runtime.
436 UnreserveImageSpace();
437
438 helper.extra_dex_files.clear();
439 runtime_.reset();
440 java_lang_dex_file_ = nullptr;
441
442 MemMap::Init();
443
444 RuntimeOptions options;
Vladimir Marko7a85e702018-12-03 18:47:23 +0000445 options.emplace_back(GetClassPathOption("-Xbootclasspath:", GetLibCoreDexFileNames()), nullptr);
446 options.emplace_back(
447 GetClassPathOption("-Xbootclasspath-locations:", GetLibCoreDexLocations()), nullptr);
Vladimir Marko1352f132017-04-28 15:28:29 +0100448 std::string image("-Ximage:");
449 image.append(helper.image_locations[0].GetFilename());
450 options.push_back(std::make_pair(image.c_str(), static_cast<void*>(nullptr)));
451 // By default the compiler this creates will not include patch information.
452 options.push_back(std::make_pair("-Xnorelocate", nullptr));
453
454 if (!Runtime::Create(options, false)) {
455 LOG(FATAL) << "Failed to create runtime";
456 return;
457 }
458 runtime_.reset(Runtime::Current());
459 // Runtime::Create acquired the mutator_lock_ that is normally given away when we Runtime::Start,
460 // give it away now and then switch to a more managable ScopedObjectAccess.
461 Thread::Current()->TransitionFromRunnableToSuspended(kNative);
462 ScopedObjectAccess soa(Thread::Current());
463 ASSERT_TRUE(runtime_.get() != nullptr);
464 class_linker_ = runtime_->GetClassLinker();
465
466 gc::Heap* heap = Runtime::Current()->GetHeap();
467 ASSERT_TRUE(heap->HasBootImageSpace());
468 ASSERT_TRUE(heap->GetNonMovingSpace()->IsMallocSpace());
469
470 // We loaded the runtime with an explicit image, so it must exist.
471 ASSERT_EQ(heap->GetBootImageSpaces().size(), image_file_sizes.size());
Vladimir Markodc4bcce2018-06-21 16:15:42 +0100472 const HashSet<std::string>& image_classes = compiler_options_->GetImageClasses();
Vladimir Marko1352f132017-04-28 15:28:29 +0100473 for (size_t i = 0; i < helper.dex_file_locations.size(); ++i) {
474 std::unique_ptr<const DexFile> dex(
475 LoadExpectSingleDexFile(helper.dex_file_locations[i].c_str()));
476 ASSERT_TRUE(dex != nullptr);
477 uint64_t image_file_size = image_file_sizes[i];
478 gc::space::ImageSpace* image_space = heap->GetBootImageSpaces()[i];
479 ASSERT_TRUE(image_space != nullptr);
480 if (storage_mode == ImageHeader::kStorageModeUncompressed) {
481 // Uncompressed, image should be smaller than file.
482 ASSERT_LE(image_space->GetImageHeader().GetImageSize(), image_file_size);
483 } else if (image_file_size > 16 * KB) {
484 // Compressed, file should be smaller than image. Not really valid for small images.
485 ASSERT_LE(image_file_size, image_space->GetImageHeader().GetImageSize());
Mathieu Chartier1a842962018-11-13 15:09:51 -0800486 // TODO: Actually validate the blocks, this is hard since the blocks are not copied over for
487 // compressed images. Add kPageSize since image_size is rounded up to this.
488 ASSERT_GT(image_space->GetImageHeader().GetBlockCount() * max_image_block_size,
489 image_space->GetImageHeader().GetImageSize() - kPageSize);
Vladimir Marko1352f132017-04-28 15:28:29 +0100490 }
491
492 image_space->VerifyImageAllocations();
493 uint8_t* image_begin = image_space->Begin();
494 uint8_t* image_end = image_space->End();
495 if (i == 0) {
496 // This check is only valid for image 0.
497 CHECK_EQ(kRequestedImageBase, reinterpret_cast<uintptr_t>(image_begin));
498 }
499 for (size_t j = 0; j < dex->NumClassDefs(); ++j) {
500 const DexFile::ClassDef& class_def = dex->GetClassDef(j);
501 const char* descriptor = dex->GetClassDescriptor(class_def);
Vladimir Markoe9987b02018-05-22 16:26:43 +0100502 ObjPtr<mirror::Class> klass = class_linker_->FindSystemClass(soa.Self(), descriptor);
Vladimir Marko1352f132017-04-28 15:28:29 +0100503 EXPECT_TRUE(klass != nullptr) << descriptor;
Vladimir Markoe9987b02018-05-22 16:26:43 +0100504 uint8_t* raw_klass = reinterpret_cast<uint8_t*>(klass.Ptr());
Vladimir Marko54159c62018-06-20 14:30:08 +0100505 if (image_classes.find(StringPiece(descriptor)) == image_classes.end()) {
Vladimir Markoe9987b02018-05-22 16:26:43 +0100506 EXPECT_TRUE(raw_klass >= image_end || raw_klass < image_begin) << descriptor;
Vladimir Marko1352f132017-04-28 15:28:29 +0100507 } else {
508 // Image classes should be located inside the image.
Vladimir Markoe9987b02018-05-22 16:26:43 +0100509 EXPECT_LT(image_begin, raw_klass) << descriptor;
510 EXPECT_LT(raw_klass, image_end) << descriptor;
Vladimir Marko1352f132017-04-28 15:28:29 +0100511 }
512 EXPECT_TRUE(Monitor::IsValidLockWord(klass->GetLockWord(false)));
513 }
514 }
515}
516
Vladimir Marko74527972016-11-29 15:57:32 +0000517} // namespace linker
Vladimir Marko1352f132017-04-28 15:28:29 +0100518} // namespace art
519
Vladimir Marko74527972016-11-29 15:57:32 +0000520#endif // ART_DEX2OAT_LINKER_IMAGE_TEST_H_