blob: 1186669291d824c2329791afeee9e7e3e7bc402f [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
80 void TestWriteRead(ImageHeader::StorageMode storage_mode);
81
82 void Compile(ImageHeader::StorageMode storage_mode,
Vladimir Marko213ee2d2018-06-22 11:56:34 +010083 /*out*/ CompilationHelper& out_helper,
Vladimir Marko1352f132017-04-28 15:28:29 +010084 const std::string& extra_dex = "",
Vladimir Markod79b37b2018-11-02 13:06:22 +000085 const std::initializer_list<std::string>& image_classes = {},
86 const std::initializer_list<std::string>& image_classes_failing_aot_clinit = {});
Vladimir Marko1352f132017-04-28 15:28:29 +010087
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010088 void SetUpRuntimeOptions(RuntimeOptions* options) override {
Vladimir Marko1352f132017-04-28 15:28:29 +010089 CommonCompilerTest::SetUpRuntimeOptions(options);
Mathieu Chartiere01b6f62017-07-19 16:55:04 -070090 QuickCompilerCallbacks* new_callbacks =
91 new QuickCompilerCallbacks(CompilerCallbacks::CallbackMode::kCompileBootImage);
92 new_callbacks->SetVerificationResults(verification_results_.get());
93 callbacks_.reset(new_callbacks);
Vladimir Marko1352f132017-04-28 15:28:29 +010094 options->push_back(std::make_pair("compilercallbacks", callbacks_.get()));
95 }
96
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010097 std::unique_ptr<HashSet<std::string>> GetImageClasses() override {
Vladimir Marko54159c62018-06-20 14:30:08 +010098 return std::make_unique<HashSet<std::string>>(image_classes_);
Vladimir Marko1352f132017-04-28 15:28:29 +010099 }
100
Vladimir Markoa8bba7d2018-05-30 15:18:48 +0100101 ArtMethod* FindCopiedMethod(ArtMethod* origin, ObjPtr<mirror::Class> klass)
Vladimir Marko1352f132017-04-28 15:28:29 +0100102 REQUIRES_SHARED(Locks::mutator_lock_) {
103 PointerSize pointer_size = class_linker_->GetImagePointerSize();
104 for (ArtMethod& m : klass->GetCopiedMethods(pointer_size)) {
105 if (strcmp(origin->GetName(), m.GetName()) == 0 &&
106 origin->GetSignature() == m.GetSignature()) {
107 return &m;
108 }
109 }
110 return nullptr;
111 }
112
113 private:
Vladimir Marko213ee2d2018-06-22 11:56:34 +0100114 void DoCompile(ImageHeader::StorageMode storage_mode, /*out*/ CompilationHelper& out_helper);
115
Vladimir Marko54159c62018-06-20 14:30:08 +0100116 HashSet<std::string> image_classes_;
Vladimir Marko1352f132017-04-28 15:28:29 +0100117};
118
119inline CompilationHelper::~CompilationHelper() {
120 for (ScratchFile& image_file : image_files) {
121 image_file.Unlink();
122 }
123 for (ScratchFile& oat_file : oat_files) {
124 oat_file.Unlink();
125 }
126 for (ScratchFile& vdex_file : vdex_files) {
127 vdex_file.Unlink();
128 }
129 const int rmdir_result = rmdir(image_dir.c_str());
130 CHECK_EQ(0, rmdir_result);
131}
132
133inline std::vector<size_t> CompilationHelper::GetImageObjectSectionSizes() {
134 std::vector<size_t> ret;
135 for (ScratchFile& image_file : image_files) {
136 std::unique_ptr<File> file(OS::OpenFileForReading(image_file.GetFilename().c_str()));
137 CHECK(file.get() != nullptr);
138 ImageHeader image_header;
139 CHECK_EQ(file->ReadFully(&image_header, sizeof(image_header)), true);
140 CHECK(image_header.IsValid());
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100141 ret.push_back(image_header.GetObjectsSection().Size());
Vladimir Marko1352f132017-04-28 15:28:29 +0100142 }
143 return ret;
144}
145
Vladimir Marko213ee2d2018-06-22 11:56:34 +0100146inline void ImageTest::DoCompile(ImageHeader::StorageMode storage_mode,
147 /*out*/ CompilationHelper& out_helper) {
148 CompilerDriver* driver = compiler_driver_.get();
Vladimir Marko1352f132017-04-28 15:28:29 +0100149 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
150 std::vector<const DexFile*> class_path = class_linker->GetBootClassPath();
151
Vladimir Marko213ee2d2018-06-22 11:56:34 +0100152 for (const std::unique_ptr<const DexFile>& dex_file : out_helper.extra_dex_files) {
Vladimir Marko1352f132017-04-28 15:28:29 +0100153 {
154 ScopedObjectAccess soa(Thread::Current());
155 // Inject in boot class path so that the compiler driver can see it.
156 class_linker->AppendToBootClassPath(soa.Self(), *dex_file.get());
157 }
158 class_path.push_back(dex_file.get());
159 }
160
161 // Enable write for dex2dex.
162 for (const DexFile* dex_file : class_path) {
Vladimir Marko213ee2d2018-06-22 11:56:34 +0100163 out_helper.dex_file_locations.push_back(dex_file->GetLocation());
Vladimir Marko1352f132017-04-28 15:28:29 +0100164 if (dex_file->IsReadOnly()) {
165 dex_file->EnableWrite();
166 }
167 }
168 {
169 // Create a generic tmp file, to be the base of the .art and .oat temporary files.
170 ScratchFile location;
171 for (int i = 0; i < static_cast<int>(class_path.size()); ++i) {
172 std::string cur_location =
173 android::base::StringPrintf("%s-%d.art", location.GetFilename().c_str(), i);
Vladimir Marko213ee2d2018-06-22 11:56:34 +0100174 out_helper.image_locations.push_back(ScratchFile(cur_location));
Vladimir Marko1352f132017-04-28 15:28:29 +0100175 }
176 }
177 std::vector<std::string> image_filenames;
Vladimir Marko213ee2d2018-06-22 11:56:34 +0100178 for (ScratchFile& file : out_helper.image_locations) {
Vladimir Marko1352f132017-04-28 15:28:29 +0100179 std::string image_filename(GetSystemImageFilename(file.GetFilename().c_str(), kRuntimeISA));
180 image_filenames.push_back(image_filename);
181 size_t pos = image_filename.rfind('/');
182 CHECK_NE(pos, std::string::npos) << image_filename;
Vladimir Marko213ee2d2018-06-22 11:56:34 +0100183 if (out_helper.image_dir.empty()) {
184 out_helper.image_dir = image_filename.substr(0, pos);
185 int mkdir_result = mkdir(out_helper.image_dir.c_str(), 0700);
186 CHECK_EQ(0, mkdir_result) << out_helper.image_dir;
Vladimir Marko1352f132017-04-28 15:28:29 +0100187 }
Vladimir Marko213ee2d2018-06-22 11:56:34 +0100188 out_helper.image_files.push_back(ScratchFile(OS::CreateEmptyFile(image_filename.c_str())));
Vladimir Marko1352f132017-04-28 15:28:29 +0100189 }
190
191 std::vector<std::string> oat_filenames;
192 std::vector<std::string> vdex_filenames;
193 for (const std::string& image_filename : image_filenames) {
194 std::string oat_filename = ReplaceFileExtension(image_filename, "oat");
Vladimir Marko213ee2d2018-06-22 11:56:34 +0100195 out_helper.oat_files.push_back(ScratchFile(OS::CreateEmptyFile(oat_filename.c_str())));
Vladimir Marko1352f132017-04-28 15:28:29 +0100196 oat_filenames.push_back(oat_filename);
197 std::string vdex_filename = ReplaceFileExtension(image_filename, "vdex");
Vladimir Marko213ee2d2018-06-22 11:56:34 +0100198 out_helper.vdex_files.push_back(ScratchFile(OS::CreateEmptyFile(vdex_filename.c_str())));
Vladimir Marko1352f132017-04-28 15:28:29 +0100199 vdex_filenames.push_back(vdex_filename);
200 }
201
202 std::unordered_map<const DexFile*, size_t> dex_file_to_oat_index_map;
203 std::vector<const char*> oat_filename_vector;
204 for (const std::string& file : oat_filenames) {
205 oat_filename_vector.push_back(file.c_str());
206 }
207 std::vector<const char*> image_filename_vector;
208 for (const std::string& file : image_filenames) {
209 image_filename_vector.push_back(file.c_str());
210 }
211 size_t image_idx = 0;
212 for (const DexFile* dex_file : class_path) {
213 dex_file_to_oat_index_map.emplace(dex_file, image_idx);
214 ++image_idx;
215 }
216 // TODO: compile_pic should be a test argument.
Vladimir Markoa0431112018-06-25 09:32:54 +0100217 std::unique_ptr<ImageWriter> writer(new ImageWriter(*compiler_options_,
Vladimir Marko1352f132017-04-28 15:28:29 +0100218 kRequestedImageBase,
Vladimir Marko1352f132017-04-28 15:28:29 +0100219 storage_mode,
220 oat_filename_vector,
Jeff Haoc23b0c02017-07-27 18:19:38 -0700221 dex_file_to_oat_index_map,
Nicolas Geoffrayf0d30022018-11-20 17:45:38 +0000222 /*class_loader=*/ nullptr,
Vladimir Marko9c4b9702018-11-14 15:09:02 +0000223 /*dirty_image_objects=*/ nullptr));
Vladimir Marko1352f132017-04-28 15:28:29 +0100224 {
225 {
226 jobject class_loader = nullptr;
227 TimingLogger timings("ImageTest::WriteRead", false, false);
228 TimingLogger::ScopedTiming t("CompileAll", &timings);
Vladimir Marko213ee2d2018-06-22 11:56:34 +0100229 SetDexFilesForOatFile(class_path);
Nicolas Geoffray1cfea7a2017-05-24 14:44:38 +0100230 driver->CompileAll(class_loader, class_path, &timings);
Vladimir Marko1352f132017-04-28 15:28:29 +0100231
232 t.NewTiming("WriteElf");
233 SafeMap<std::string, std::string> key_value_store;
234 std::vector<const char*> dex_filename_vector;
235 for (size_t i = 0; i < class_path.size(); ++i) {
236 dex_filename_vector.push_back("");
237 }
238 key_value_store.Put(OatHeader::kBootClassPathKey,
239 gc::space::ImageSpace::GetMultiImageBootClassPath(
240 dex_filename_vector,
241 oat_filename_vector,
242 image_filename_vector));
243
244 std::vector<std::unique_ptr<ElfWriter>> elf_writers;
245 std::vector<std::unique_ptr<OatWriter>> oat_writers;
Vladimir Marko213ee2d2018-06-22 11:56:34 +0100246 for (ScratchFile& oat_file : out_helper.oat_files) {
Vladimir Markoa0431112018-06-25 09:32:54 +0100247 elf_writers.emplace_back(CreateElfWriterQuick(*compiler_options_, oat_file.GetFile()));
Vladimir Marko1352f132017-04-28 15:28:29 +0100248 elf_writers.back()->Start();
Vladimir Markoa0431112018-06-25 09:32:54 +0100249 oat_writers.emplace_back(new OatWriter(*compiler_options_,
Vladimir Marko1352f132017-04-28 15:28:29 +0100250 &timings,
Mathieu Chartier603ccab2017-10-20 14:34:28 -0700251 /*profile_compilation_info*/nullptr,
252 CompactDexLevel::kCompactDexLevelNone));
Vladimir Marko1352f132017-04-28 15:28:29 +0100253 }
254
255 std::vector<OutputStream*> rodata;
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100256 std::vector<MemMap> opened_dex_files_maps;
Vladimir Marko1352f132017-04-28 15:28:29 +0100257 std::vector<std::unique_ptr<const DexFile>> opened_dex_files;
258 // Now that we have finalized key_value_store_, start writing the oat file.
259 for (size_t i = 0, size = oat_writers.size(); i != size; ++i) {
260 const DexFile* dex_file = class_path[i];
261 rodata.push_back(elf_writers[i]->StartRoData());
262 ArrayRef<const uint8_t> raw_dex_file(
263 reinterpret_cast<const uint8_t*>(&dex_file->GetHeader()),
264 dex_file->GetHeader().file_size_);
265 oat_writers[i]->AddRawDexFileSource(raw_dex_file,
266 dex_file->GetLocation().c_str(),
267 dex_file->GetLocationChecksum());
268
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100269 std::vector<MemMap> cur_opened_dex_files_maps;
Vladimir Marko1352f132017-04-28 15:28:29 +0100270 std::vector<std::unique_ptr<const DexFile>> cur_opened_dex_files;
271 bool dex_files_ok = oat_writers[i]->WriteAndOpenDexFiles(
Vladimir Marko213ee2d2018-06-22 11:56:34 +0100272 out_helper.vdex_files[i].GetFile(),
Vladimir Marko1352f132017-04-28 15:28:29 +0100273 rodata.back(),
Vladimir Marko1352f132017-04-28 15:28:29 +0100274 &key_value_store,
275 /* verify */ false, // Dex files may be dex-to-dex-ed, don't verify.
276 /* update_input_vdex */ false,
Nicolas Geoffray66ff8a82018-02-28 13:27:55 +0000277 /* copy_dex_files */ CopyOption::kOnlyIfCompressed,
Nicolas Geoffrayf3075272018-01-08 12:41:19 +0000278 &cur_opened_dex_files_maps,
Vladimir Marko1352f132017-04-28 15:28:29 +0100279 &cur_opened_dex_files);
280 ASSERT_TRUE(dex_files_ok);
281
Nicolas Geoffrayf3075272018-01-08 12:41:19 +0000282 if (!cur_opened_dex_files_maps.empty()) {
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100283 for (MemMap& cur_map : cur_opened_dex_files_maps) {
Nicolas Geoffrayf3075272018-01-08 12:41:19 +0000284 opened_dex_files_maps.push_back(std::move(cur_map));
285 }
Vladimir Marko1352f132017-04-28 15:28:29 +0100286 for (std::unique_ptr<const DexFile>& cur_dex_file : cur_opened_dex_files) {
287 // dex_file_oat_index_map_.emplace(dex_file.get(), i);
288 opened_dex_files.push_back(std::move(cur_dex_file));
289 }
290 } else {
291 ASSERT_TRUE(cur_opened_dex_files.empty());
292 }
293 }
Mathieu Chartier703b82a2018-04-10 15:52:32 -0700294 bool image_space_ok = writer->PrepareImageAddressSpace(&timings);
Vladimir Marko1352f132017-04-28 15:28:29 +0100295 ASSERT_TRUE(image_space_ok);
296
Vladimir Marko213ee2d2018-06-22 11:56:34 +0100297 DCHECK_EQ(out_helper.vdex_files.size(), out_helper.oat_files.size());
298 for (size_t i = 0, size = out_helper.oat_files.size(); i != size; ++i) {
Vladimir Markoa0431112018-06-25 09:32:54 +0100299 MultiOatRelativePatcher patcher(compiler_options_->GetInstructionSet(),
300 compiler_options_->GetInstructionSetFeatures(),
Vladimir Markoca1e0382018-04-11 09:58:41 +0000301 driver->GetCompiledMethodStorage());
Vladimir Marko1352f132017-04-28 15:28:29 +0100302 OatWriter* const oat_writer = oat_writers[i].get();
303 ElfWriter* const elf_writer = elf_writers[i].get();
304 std::vector<const DexFile*> cur_dex_files(1u, class_path[i]);
305 oat_writer->Initialize(driver, writer.get(), cur_dex_files);
David Brazdil93592f52017-12-08 10:53:27 +0000306
307 std::unique_ptr<BufferedOutputStream> vdex_out =
308 std::make_unique<BufferedOutputStream>(
Vladimir Marko213ee2d2018-06-22 11:56:34 +0100309 std::make_unique<FileOutputStream>(out_helper.vdex_files[i].GetFile()));
David Brazdil93592f52017-12-08 10:53:27 +0000310 oat_writer->WriteVerifierDeps(vdex_out.get(), nullptr);
311 oat_writer->WriteQuickeningInfo(vdex_out.get());
312 oat_writer->WriteChecksumsAndVdexHeader(vdex_out.get());
313
Vladimir Marko1352f132017-04-28 15:28:29 +0100314 oat_writer->PrepareLayout(&patcher);
Vladimir Markob066d432018-01-03 13:14:37 +0000315 elf_writer->PrepareDynamicSection(oat_writer->GetOatHeader().GetExecutableOffset(),
316 oat_writer->GetCodeSize(),
317 oat_writer->GetDataBimgRelRoSize(),
Vladimir Marko1352f132017-04-28 15:28:29 +0100318 oat_writer->GetBssSize(),
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100319 oat_writer->GetBssMethodsOffset(),
David Srbeckyec2cdf42017-12-08 16:21:25 +0000320 oat_writer->GetBssRootsOffset(),
321 oat_writer->GetVdexSize());
Vladimir Marko1352f132017-04-28 15:28:29 +0100322
323 writer->UpdateOatFileLayout(i,
324 elf_writer->GetLoadedSize(),
325 oat_writer->GetOatDataOffset(),
326 oat_writer->GetOatSize());
327
328 bool rodata_ok = oat_writer->WriteRodata(rodata[i]);
329 ASSERT_TRUE(rodata_ok);
330 elf_writer->EndRoData(rodata[i]);
331
332 OutputStream* text = elf_writer->StartText();
333 bool text_ok = oat_writer->WriteCode(text);
334 ASSERT_TRUE(text_ok);
335 elf_writer->EndText(text);
336
Vladimir Markob066d432018-01-03 13:14:37 +0000337 if (oat_writer->GetDataBimgRelRoSize() != 0u) {
338 OutputStream* data_bimg_rel_ro = elf_writer->StartDataBimgRelRo();
339 bool data_bimg_rel_ro_ok = oat_writer->WriteDataBimgRelRo(data_bimg_rel_ro);
340 ASSERT_TRUE(data_bimg_rel_ro_ok);
341 elf_writer->EndDataBimgRelRo(data_bimg_rel_ro);
342 }
343
Vladimir Markoe0669322018-09-03 15:44:54 +0100344 bool header_ok = oat_writer->WriteHeader(elf_writer->GetStream(),
345 /* image_file_location_oat_checksum */ 0u);
Vladimir Marko1352f132017-04-28 15:28:29 +0100346 ASSERT_TRUE(header_ok);
347
348 writer->UpdateOatFileHeader(i, oat_writer->GetOatHeader());
349
350 elf_writer->WriteDynamicSection();
David Srbecky32210b92017-12-04 14:39:21 +0000351 elf_writer->WriteDebugInfo(oat_writer->GetDebugInfo());
Vladimir Marko1352f132017-04-28 15:28:29 +0100352
353 bool success = elf_writer->End();
354 ASSERT_TRUE(success);
355 }
356 }
357
358 bool success_image = writer->Write(kInvalidFd,
359 image_filename_vector,
360 oat_filename_vector);
361 ASSERT_TRUE(success_image);
362
363 for (size_t i = 0, size = oat_filenames.size(); i != size; ++i) {
364 const char* oat_filename = oat_filenames[i].c_str();
365 std::unique_ptr<File> oat_file(OS::OpenFileReadWrite(oat_filename));
366 ASSERT_TRUE(oat_file != nullptr);
Vladimir Marko213ee2d2018-06-22 11:56:34 +0100367 bool success_fixup = ElfWriter::Fixup(oat_file.get(), writer->GetOatDataBegin(i));
Vladimir Marko1352f132017-04-28 15:28:29 +0100368 ASSERT_TRUE(success_fixup);
369 ASSERT_EQ(oat_file->FlushCloseOrErase(), 0) << "Could not flush and close oat file "
370 << oat_filename;
371 }
372 }
373}
374
Vladimir Markod79b37b2018-11-02 13:06:22 +0000375inline void ImageTest::Compile(
376 ImageHeader::StorageMode storage_mode,
377 CompilationHelper& helper,
378 const std::string& extra_dex,
379 const std::initializer_list<std::string>& image_classes,
380 const std::initializer_list<std::string>& image_classes_failing_aot_clinit) {
381 for (const std::string& image_class : image_classes_failing_aot_clinit) {
382 ASSERT_TRUE(ContainsElement(image_classes, image_class));
383 }
Vladimir Marko1352f132017-04-28 15:28:29 +0100384 for (const std::string& image_class : image_classes) {
385 image_classes_.insert(image_class);
386 }
Vladimir Markoa0431112018-06-25 09:32:54 +0100387 number_of_threads_ = kIsTargetBuild ? 2U : 16U;
388 CreateCompilerDriver();
Vladimir Marko1352f132017-04-28 15:28:29 +0100389 // Set inline filter values.
390 compiler_options_->SetInlineMaxCodeUnits(CompilerOptions::kDefaultInlineMaxCodeUnits);
391 image_classes_.clear();
392 if (!extra_dex.empty()) {
393 helper.extra_dex_files = OpenTestDexFiles(extra_dex.c_str());
394 }
Vladimir Marko213ee2d2018-06-22 11:56:34 +0100395 DoCompile(storage_mode, helper);
Vladimir Marko1352f132017-04-28 15:28:29 +0100396 if (image_classes.begin() != image_classes.end()) {
397 // Make sure the class got initialized.
398 ScopedObjectAccess soa(Thread::Current());
399 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
400 for (const std::string& image_class : image_classes) {
Vladimir Markoe9987b02018-05-22 16:26:43 +0100401 ObjPtr<mirror::Class> klass =
402 class_linker->FindSystemClass(Thread::Current(), image_class.c_str());
Vladimir Marko1352f132017-04-28 15:28:29 +0100403 EXPECT_TRUE(klass != nullptr);
Vladimir Markod79b37b2018-11-02 13:06:22 +0000404 EXPECT_TRUE(klass->IsResolved());
405 if (ContainsElement(image_classes_failing_aot_clinit, image_class)) {
406 EXPECT_FALSE(klass->IsInitialized());
407 } else {
408 EXPECT_TRUE(klass->IsInitialized());
409 }
Vladimir Marko1352f132017-04-28 15:28:29 +0100410 }
411 }
412}
413
414inline void ImageTest::TestWriteRead(ImageHeader::StorageMode storage_mode) {
415 CompilationHelper helper;
416 Compile(storage_mode, /*out*/ helper);
417 std::vector<uint64_t> image_file_sizes;
418 for (ScratchFile& image_file : helper.image_files) {
419 std::unique_ptr<File> file(OS::OpenFileForReading(image_file.GetFilename().c_str()));
420 ASSERT_TRUE(file.get() != nullptr);
421 ImageHeader image_header;
422 ASSERT_EQ(file->ReadFully(&image_header, sizeof(image_header)), true);
423 ASSERT_TRUE(image_header.IsValid());
Vladimir Markocd87c3e2017-09-05 13:11:57 +0100424 const auto& bitmap_section = image_header.GetImageBitmapSection();
Vladimir Marko1352f132017-04-28 15:28:29 +0100425 ASSERT_GE(bitmap_section.Offset(), sizeof(image_header));
426 ASSERT_NE(0U, bitmap_section.Size());
427
428 gc::Heap* heap = Runtime::Current()->GetHeap();
429 ASSERT_TRUE(heap->HaveContinuousSpaces());
430 gc::space::ContinuousSpace* space = heap->GetNonMovingSpace();
431 ASSERT_FALSE(space->IsImageSpace());
432 ASSERT_TRUE(space != nullptr);
433 ASSERT_TRUE(space->IsMallocSpace());
434 image_file_sizes.push_back(file->GetLength());
435 }
436
Vladimir Marko1352f132017-04-28 15:28:29 +0100437 // Need to delete the compiler since it has worker threads which are attached to runtime.
438 compiler_driver_.reset();
439
440 // Tear down old runtime before making a new one, clearing out misc state.
441
442 // Remove the reservation of the memory for use to load the image.
443 // Need to do this before we reset the runtime.
444 UnreserveImageSpace();
445
446 helper.extra_dex_files.clear();
447 runtime_.reset();
448 java_lang_dex_file_ = nullptr;
449
450 MemMap::Init();
451
452 RuntimeOptions options;
453 std::string image("-Ximage:");
454 image.append(helper.image_locations[0].GetFilename());
455 options.push_back(std::make_pair(image.c_str(), static_cast<void*>(nullptr)));
456 // By default the compiler this creates will not include patch information.
457 options.push_back(std::make_pair("-Xnorelocate", nullptr));
458
459 if (!Runtime::Create(options, false)) {
460 LOG(FATAL) << "Failed to create runtime";
461 return;
462 }
463 runtime_.reset(Runtime::Current());
464 // Runtime::Create acquired the mutator_lock_ that is normally given away when we Runtime::Start,
465 // give it away now and then switch to a more managable ScopedObjectAccess.
466 Thread::Current()->TransitionFromRunnableToSuspended(kNative);
467 ScopedObjectAccess soa(Thread::Current());
468 ASSERT_TRUE(runtime_.get() != nullptr);
469 class_linker_ = runtime_->GetClassLinker();
470
471 gc::Heap* heap = Runtime::Current()->GetHeap();
472 ASSERT_TRUE(heap->HasBootImageSpace());
473 ASSERT_TRUE(heap->GetNonMovingSpace()->IsMallocSpace());
474
475 // We loaded the runtime with an explicit image, so it must exist.
476 ASSERT_EQ(heap->GetBootImageSpaces().size(), image_file_sizes.size());
Vladimir Markodc4bcce2018-06-21 16:15:42 +0100477 const HashSet<std::string>& image_classes = compiler_options_->GetImageClasses();
Vladimir Marko1352f132017-04-28 15:28:29 +0100478 for (size_t i = 0; i < helper.dex_file_locations.size(); ++i) {
479 std::unique_ptr<const DexFile> dex(
480 LoadExpectSingleDexFile(helper.dex_file_locations[i].c_str()));
481 ASSERT_TRUE(dex != nullptr);
482 uint64_t image_file_size = image_file_sizes[i];
483 gc::space::ImageSpace* image_space = heap->GetBootImageSpaces()[i];
484 ASSERT_TRUE(image_space != nullptr);
485 if (storage_mode == ImageHeader::kStorageModeUncompressed) {
486 // Uncompressed, image should be smaller than file.
487 ASSERT_LE(image_space->GetImageHeader().GetImageSize(), image_file_size);
488 } else if (image_file_size > 16 * KB) {
489 // Compressed, file should be smaller than image. Not really valid for small images.
490 ASSERT_LE(image_file_size, image_space->GetImageHeader().GetImageSize());
491 }
492
493 image_space->VerifyImageAllocations();
494 uint8_t* image_begin = image_space->Begin();
495 uint8_t* image_end = image_space->End();
496 if (i == 0) {
497 // This check is only valid for image 0.
498 CHECK_EQ(kRequestedImageBase, reinterpret_cast<uintptr_t>(image_begin));
499 }
500 for (size_t j = 0; j < dex->NumClassDefs(); ++j) {
501 const DexFile::ClassDef& class_def = dex->GetClassDef(j);
502 const char* descriptor = dex->GetClassDescriptor(class_def);
Vladimir Markoe9987b02018-05-22 16:26:43 +0100503 ObjPtr<mirror::Class> klass = class_linker_->FindSystemClass(soa.Self(), descriptor);
Vladimir Marko1352f132017-04-28 15:28:29 +0100504 EXPECT_TRUE(klass != nullptr) << descriptor;
Vladimir Markoe9987b02018-05-22 16:26:43 +0100505 uint8_t* raw_klass = reinterpret_cast<uint8_t*>(klass.Ptr());
Vladimir Marko54159c62018-06-20 14:30:08 +0100506 if (image_classes.find(StringPiece(descriptor)) == image_classes.end()) {
Vladimir Markoe9987b02018-05-22 16:26:43 +0100507 EXPECT_TRUE(raw_klass >= image_end || raw_klass < image_begin) << descriptor;
Vladimir Marko1352f132017-04-28 15:28:29 +0100508 } else {
509 // Image classes should be located inside the image.
Vladimir Markoe9987b02018-05-22 16:26:43 +0100510 EXPECT_LT(image_begin, raw_klass) << descriptor;
511 EXPECT_LT(raw_klass, image_end) << descriptor;
Vladimir Marko1352f132017-04-28 15:28:29 +0100512 }
513 EXPECT_TRUE(Monitor::IsValidLockWord(klass->GetLockWord(false)));
514 }
515 }
516}
517
Vladimir Marko74527972016-11-29 15:57:32 +0000518} // namespace linker
Vladimir Marko1352f132017-04-28 15:28:29 +0100519} // namespace art
520
Vladimir Marko74527972016-11-29 15:57:32 +0000521#endif // ART_DEX2OAT_LINKER_IMAGE_TEST_H_