blob: 440b3a47cbaf64de6a5fe9d04464bc1216d1f3c1 [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 Marko1352f132017-04-28 15:28:29 +010031#include "base/unix_file/fd_file.h"
David Sehrc431b9d2018-03-02 12:01:51 -080032#include "base/utils.h"
Vladimir Marko1352f132017-04-28 15:28:29 +010033#include "class_linker-inl.h"
Vladimir Marko1352f132017-04-28 15:28:29 +010034#include "common_compiler_test.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070035#include "compiler_callbacks.h"
Vladimir Marko1352f132017-04-28 15:28:29 +010036#include "debug/method_debug_info.h"
37#include "dex/quick_compiler_callbacks.h"
Vladimir Markoa0431112018-06-25 09:32:54 +010038#include "driver/compiler_driver.h"
Vladimir Marko1352f132017-04-28 15:28:29 +010039#include "driver/compiler_options.h"
Vladimir Marko1352f132017-04-28 15:28:29 +010040#include "gc/space/image_space.h"
41#include "image_writer.h"
42#include "linker/buffered_output_stream.h"
Vladimir Marko74527972016-11-29 15:57:32 +000043#include "linker/elf_writer.h"
44#include "linker/elf_writer_quick.h"
Vladimir Marko1352f132017-04-28 15:28:29 +010045#include "linker/file_output_stream.h"
46#include "linker/multi_oat_relative_patcher.h"
47#include "lock_word.h"
48#include "mirror/object-inl.h"
49#include "oat_writer.h"
50#include "scoped_thread_state_change-inl.h"
51#include "signal_catcher.h"
Vladimir Marko1352f132017-04-28 15:28:29 +010052
53namespace art {
Vladimir Marko74527972016-11-29 15:57:32 +000054namespace linker {
Vladimir Marko1352f132017-04-28 15:28:29 +010055
56static const uintptr_t kRequestedImageBase = ART_BASE_ADDRESS;
57
58struct CompilationHelper {
59 std::vector<std::string> dex_file_locations;
60 std::vector<ScratchFile> image_locations;
61 std::vector<std::unique_ptr<const DexFile>> extra_dex_files;
62 std::vector<ScratchFile> image_files;
63 std::vector<ScratchFile> oat_files;
64 std::vector<ScratchFile> vdex_files;
65 std::string image_dir;
66
Vladimir Marko1352f132017-04-28 15:28:29 +010067 std::vector<size_t> GetImageObjectSectionSizes();
68
69 ~CompilationHelper();
70};
71
72class ImageTest : public CommonCompilerTest {
73 protected:
74 virtual void SetUp() {
75 ReserveImageSpace();
76 CommonCompilerTest::SetUp();
77 }
78
79 void TestWriteRead(ImageHeader::StorageMode storage_mode);
80
81 void Compile(ImageHeader::StorageMode storage_mode,
Vladimir Marko213ee2d2018-06-22 11:56:34 +010082 /*out*/ CompilationHelper& out_helper,
Vladimir Marko1352f132017-04-28 15:28:29 +010083 const std::string& extra_dex = "",
84 const std::initializer_list<std::string>& image_classes = {});
85
86 void SetUpRuntimeOptions(RuntimeOptions* options) OVERRIDE {
87 CommonCompilerTest::SetUpRuntimeOptions(options);
Mathieu Chartiere01b6f62017-07-19 16:55:04 -070088 QuickCompilerCallbacks* new_callbacks =
89 new QuickCompilerCallbacks(CompilerCallbacks::CallbackMode::kCompileBootImage);
90 new_callbacks->SetVerificationResults(verification_results_.get());
91 callbacks_.reset(new_callbacks);
Vladimir Marko1352f132017-04-28 15:28:29 +010092 options->push_back(std::make_pair("compilercallbacks", callbacks_.get()));
93 }
94
Vladimir Marko54159c62018-06-20 14:30:08 +010095 std::unique_ptr<HashSet<std::string>> GetImageClasses() OVERRIDE {
96 return std::make_unique<HashSet<std::string>>(image_classes_);
Vladimir Marko1352f132017-04-28 15:28:29 +010097 }
98
Vladimir Markoa8bba7d2018-05-30 15:18:48 +010099 ArtMethod* FindCopiedMethod(ArtMethod* origin, ObjPtr<mirror::Class> klass)
Vladimir Marko1352f132017-04-28 15:28:29 +0100100 REQUIRES_SHARED(Locks::mutator_lock_) {
101 PointerSize pointer_size = class_linker_->GetImagePointerSize();
102 for (ArtMethod& m : klass->GetCopiedMethods(pointer_size)) {
103 if (strcmp(origin->GetName(), m.GetName()) == 0 &&
104 origin->GetSignature() == m.GetSignature()) {
105 return &m;
106 }
107 }
108 return nullptr;
109 }
110
111 private:
Vladimir Marko213ee2d2018-06-22 11:56:34 +0100112 void DoCompile(ImageHeader::StorageMode storage_mode, /*out*/ CompilationHelper& out_helper);
113
Vladimir Marko54159c62018-06-20 14:30:08 +0100114 HashSet<std::string> image_classes_;
Vladimir Marko1352f132017-04-28 15:28:29 +0100115};
116
117inline CompilationHelper::~CompilationHelper() {
118 for (ScratchFile& image_file : image_files) {
119 image_file.Unlink();
120 }
121 for (ScratchFile& oat_file : oat_files) {
122 oat_file.Unlink();
123 }
124 for (ScratchFile& vdex_file : vdex_files) {
125 vdex_file.Unlink();
126 }
127 const int rmdir_result = rmdir(image_dir.c_str());
128 CHECK_EQ(0, rmdir_result);
129}
130
131inline std::vector<size_t> CompilationHelper::GetImageObjectSectionSizes() {
132 std::vector<size_t> ret;
133 for (ScratchFile& image_file : image_files) {
134 std::unique_ptr<File> file(OS::OpenFileForReading(image_file.GetFilename().c_str()));
135 CHECK(file.get() != nullptr);
136 ImageHeader image_header;
137 CHECK_EQ(file->ReadFully(&image_header, sizeof(image_header)), true);
138 CHECK(image_header.IsValid());
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100139 ret.push_back(image_header.GetObjectsSection().Size());
Vladimir Marko1352f132017-04-28 15:28:29 +0100140 }
141 return ret;
142}
143
Vladimir Marko213ee2d2018-06-22 11:56:34 +0100144inline void ImageTest::DoCompile(ImageHeader::StorageMode storage_mode,
145 /*out*/ CompilationHelper& out_helper) {
146 CompilerDriver* driver = compiler_driver_.get();
Vladimir Marko1352f132017-04-28 15:28:29 +0100147 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
148 std::vector<const DexFile*> class_path = class_linker->GetBootClassPath();
149
Vladimir Marko213ee2d2018-06-22 11:56:34 +0100150 for (const std::unique_ptr<const DexFile>& dex_file : out_helper.extra_dex_files) {
Vladimir Marko1352f132017-04-28 15:28:29 +0100151 {
152 ScopedObjectAccess soa(Thread::Current());
153 // Inject in boot class path so that the compiler driver can see it.
154 class_linker->AppendToBootClassPath(soa.Self(), *dex_file.get());
155 }
156 class_path.push_back(dex_file.get());
157 }
158
159 // Enable write for dex2dex.
160 for (const DexFile* dex_file : class_path) {
Vladimir Marko213ee2d2018-06-22 11:56:34 +0100161 out_helper.dex_file_locations.push_back(dex_file->GetLocation());
Vladimir Marko1352f132017-04-28 15:28:29 +0100162 if (dex_file->IsReadOnly()) {
163 dex_file->EnableWrite();
164 }
165 }
166 {
167 // Create a generic tmp file, to be the base of the .art and .oat temporary files.
168 ScratchFile location;
169 for (int i = 0; i < static_cast<int>(class_path.size()); ++i) {
170 std::string cur_location =
171 android::base::StringPrintf("%s-%d.art", location.GetFilename().c_str(), i);
Vladimir Marko213ee2d2018-06-22 11:56:34 +0100172 out_helper.image_locations.push_back(ScratchFile(cur_location));
Vladimir Marko1352f132017-04-28 15:28:29 +0100173 }
174 }
175 std::vector<std::string> image_filenames;
Vladimir Marko213ee2d2018-06-22 11:56:34 +0100176 for (ScratchFile& file : out_helper.image_locations) {
Vladimir Marko1352f132017-04-28 15:28:29 +0100177 std::string image_filename(GetSystemImageFilename(file.GetFilename().c_str(), kRuntimeISA));
178 image_filenames.push_back(image_filename);
179 size_t pos = image_filename.rfind('/');
180 CHECK_NE(pos, std::string::npos) << image_filename;
Vladimir Marko213ee2d2018-06-22 11:56:34 +0100181 if (out_helper.image_dir.empty()) {
182 out_helper.image_dir = image_filename.substr(0, pos);
183 int mkdir_result = mkdir(out_helper.image_dir.c_str(), 0700);
184 CHECK_EQ(0, mkdir_result) << out_helper.image_dir;
Vladimir Marko1352f132017-04-28 15:28:29 +0100185 }
Vladimir Marko213ee2d2018-06-22 11:56:34 +0100186 out_helper.image_files.push_back(ScratchFile(OS::CreateEmptyFile(image_filename.c_str())));
Vladimir Marko1352f132017-04-28 15:28:29 +0100187 }
188
189 std::vector<std::string> oat_filenames;
190 std::vector<std::string> vdex_filenames;
191 for (const std::string& image_filename : image_filenames) {
192 std::string oat_filename = ReplaceFileExtension(image_filename, "oat");
Vladimir Marko213ee2d2018-06-22 11:56:34 +0100193 out_helper.oat_files.push_back(ScratchFile(OS::CreateEmptyFile(oat_filename.c_str())));
Vladimir Marko1352f132017-04-28 15:28:29 +0100194 oat_filenames.push_back(oat_filename);
195 std::string vdex_filename = ReplaceFileExtension(image_filename, "vdex");
Vladimir Marko213ee2d2018-06-22 11:56:34 +0100196 out_helper.vdex_files.push_back(ScratchFile(OS::CreateEmptyFile(vdex_filename.c_str())));
Vladimir Marko1352f132017-04-28 15:28:29 +0100197 vdex_filenames.push_back(vdex_filename);
198 }
199
200 std::unordered_map<const DexFile*, size_t> dex_file_to_oat_index_map;
201 std::vector<const char*> oat_filename_vector;
202 for (const std::string& file : oat_filenames) {
203 oat_filename_vector.push_back(file.c_str());
204 }
205 std::vector<const char*> image_filename_vector;
206 for (const std::string& file : image_filenames) {
207 image_filename_vector.push_back(file.c_str());
208 }
209 size_t image_idx = 0;
210 for (const DexFile* dex_file : class_path) {
211 dex_file_to_oat_index_map.emplace(dex_file, image_idx);
212 ++image_idx;
213 }
214 // TODO: compile_pic should be a test argument.
Vladimir Markoa0431112018-06-25 09:32:54 +0100215 std::unique_ptr<ImageWriter> writer(new ImageWriter(*compiler_options_,
Vladimir Marko1352f132017-04-28 15:28:29 +0100216 kRequestedImageBase,
217 /*compile_pic*/false,
218 /*compile_app_image*/false,
219 storage_mode,
220 oat_filename_vector,
Jeff Haoc23b0c02017-07-27 18:19:38 -0700221 dex_file_to_oat_index_map,
222 /*dirty_image_objects*/nullptr));
Vladimir Marko1352f132017-04-28 15:28:29 +0100223 {
224 {
225 jobject class_loader = nullptr;
226 TimingLogger timings("ImageTest::WriteRead", false, false);
227 TimingLogger::ScopedTiming t("CompileAll", &timings);
Vladimir Marko213ee2d2018-06-22 11:56:34 +0100228 SetDexFilesForOatFile(class_path);
Nicolas Geoffray1cfea7a2017-05-24 14:44:38 +0100229 driver->CompileAll(class_loader, class_path, &timings);
Vladimir Marko1352f132017-04-28 15:28:29 +0100230
231 t.NewTiming("WriteElf");
232 SafeMap<std::string, std::string> key_value_store;
233 std::vector<const char*> dex_filename_vector;
234 for (size_t i = 0; i < class_path.size(); ++i) {
235 dex_filename_vector.push_back("");
236 }
237 key_value_store.Put(OatHeader::kBootClassPathKey,
238 gc::space::ImageSpace::GetMultiImageBootClassPath(
239 dex_filename_vector,
240 oat_filename_vector,
241 image_filename_vector));
242
243 std::vector<std::unique_ptr<ElfWriter>> elf_writers;
244 std::vector<std::unique_ptr<OatWriter>> oat_writers;
Vladimir Marko213ee2d2018-06-22 11:56:34 +0100245 for (ScratchFile& oat_file : out_helper.oat_files) {
Vladimir Markoa0431112018-06-25 09:32:54 +0100246 elf_writers.emplace_back(CreateElfWriterQuick(*compiler_options_, oat_file.GetFile()));
Vladimir Marko1352f132017-04-28 15:28:29 +0100247 elf_writers.back()->Start();
Vladimir Markoa0431112018-06-25 09:32:54 +0100248 oat_writers.emplace_back(new OatWriter(*compiler_options_,
Vladimir Marko1352f132017-04-28 15:28:29 +0100249 &timings,
Mathieu Chartier603ccab2017-10-20 14:34:28 -0700250 /*profile_compilation_info*/nullptr,
251 CompactDexLevel::kCompactDexLevelNone));
Vladimir Marko1352f132017-04-28 15:28:29 +0100252 }
253
254 std::vector<OutputStream*> rodata;
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100255 std::vector<MemMap> opened_dex_files_maps;
Vladimir Marko1352f132017-04-28 15:28:29 +0100256 std::vector<std::unique_ptr<const DexFile>> opened_dex_files;
257 // Now that we have finalized key_value_store_, start writing the oat file.
258 for (size_t i = 0, size = oat_writers.size(); i != size; ++i) {
259 const DexFile* dex_file = class_path[i];
260 rodata.push_back(elf_writers[i]->StartRoData());
261 ArrayRef<const uint8_t> raw_dex_file(
262 reinterpret_cast<const uint8_t*>(&dex_file->GetHeader()),
263 dex_file->GetHeader().file_size_);
264 oat_writers[i]->AddRawDexFileSource(raw_dex_file,
265 dex_file->GetLocation().c_str(),
266 dex_file->GetLocationChecksum());
267
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100268 std::vector<MemMap> cur_opened_dex_files_maps;
Vladimir Marko1352f132017-04-28 15:28:29 +0100269 std::vector<std::unique_ptr<const DexFile>> cur_opened_dex_files;
270 bool dex_files_ok = oat_writers[i]->WriteAndOpenDexFiles(
Vladimir Marko213ee2d2018-06-22 11:56:34 +0100271 out_helper.vdex_files[i].GetFile(),
Vladimir Marko1352f132017-04-28 15:28:29 +0100272 rodata.back(),
Vladimir Marko1352f132017-04-28 15:28:29 +0100273 &key_value_store,
274 /* verify */ false, // Dex files may be dex-to-dex-ed, don't verify.
275 /* update_input_vdex */ false,
Nicolas Geoffray66ff8a82018-02-28 13:27:55 +0000276 /* copy_dex_files */ CopyOption::kOnlyIfCompressed,
Nicolas Geoffrayf3075272018-01-08 12:41:19 +0000277 &cur_opened_dex_files_maps,
Vladimir Marko1352f132017-04-28 15:28:29 +0100278 &cur_opened_dex_files);
279 ASSERT_TRUE(dex_files_ok);
280
Nicolas Geoffrayf3075272018-01-08 12:41:19 +0000281 if (!cur_opened_dex_files_maps.empty()) {
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100282 for (MemMap& cur_map : cur_opened_dex_files_maps) {
Nicolas Geoffrayf3075272018-01-08 12:41:19 +0000283 opened_dex_files_maps.push_back(std::move(cur_map));
284 }
Vladimir Marko1352f132017-04-28 15:28:29 +0100285 for (std::unique_ptr<const DexFile>& cur_dex_file : cur_opened_dex_files) {
286 // dex_file_oat_index_map_.emplace(dex_file.get(), i);
287 opened_dex_files.push_back(std::move(cur_dex_file));
288 }
289 } else {
290 ASSERT_TRUE(cur_opened_dex_files.empty());
291 }
292 }
Mathieu Chartier703b82a2018-04-10 15:52:32 -0700293 bool image_space_ok = writer->PrepareImageAddressSpace(&timings);
Vladimir Marko1352f132017-04-28 15:28:29 +0100294 ASSERT_TRUE(image_space_ok);
295
Vladimir Marko213ee2d2018-06-22 11:56:34 +0100296 DCHECK_EQ(out_helper.vdex_files.size(), out_helper.oat_files.size());
297 for (size_t i = 0, size = out_helper.oat_files.size(); i != size; ++i) {
Vladimir Markoa0431112018-06-25 09:32:54 +0100298 MultiOatRelativePatcher patcher(compiler_options_->GetInstructionSet(),
299 compiler_options_->GetInstructionSetFeatures(),
Vladimir Markoca1e0382018-04-11 09:58:41 +0000300 driver->GetCompiledMethodStorage());
Vladimir Marko1352f132017-04-28 15:28:29 +0100301 OatWriter* const oat_writer = oat_writers[i].get();
302 ElfWriter* const elf_writer = elf_writers[i].get();
303 std::vector<const DexFile*> cur_dex_files(1u, class_path[i]);
304 oat_writer->Initialize(driver, writer.get(), cur_dex_files);
David Brazdil93592f52017-12-08 10:53:27 +0000305
306 std::unique_ptr<BufferedOutputStream> vdex_out =
307 std::make_unique<BufferedOutputStream>(
Vladimir Marko213ee2d2018-06-22 11:56:34 +0100308 std::make_unique<FileOutputStream>(out_helper.vdex_files[i].GetFile()));
David Brazdil93592f52017-12-08 10:53:27 +0000309 oat_writer->WriteVerifierDeps(vdex_out.get(), nullptr);
310 oat_writer->WriteQuickeningInfo(vdex_out.get());
311 oat_writer->WriteChecksumsAndVdexHeader(vdex_out.get());
312
Vladimir Marko1352f132017-04-28 15:28:29 +0100313 oat_writer->PrepareLayout(&patcher);
Vladimir Markob066d432018-01-03 13:14:37 +0000314 elf_writer->PrepareDynamicSection(oat_writer->GetOatHeader().GetExecutableOffset(),
315 oat_writer->GetCodeSize(),
316 oat_writer->GetDataBimgRelRoSize(),
Vladimir Marko1352f132017-04-28 15:28:29 +0100317 oat_writer->GetBssSize(),
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100318 oat_writer->GetBssMethodsOffset(),
David Srbeckyec2cdf42017-12-08 16:21:25 +0000319 oat_writer->GetBssRootsOffset(),
320 oat_writer->GetVdexSize());
Vladimir Marko1352f132017-04-28 15:28:29 +0100321
322 writer->UpdateOatFileLayout(i,
323 elf_writer->GetLoadedSize(),
324 oat_writer->GetOatDataOffset(),
325 oat_writer->GetOatSize());
326
327 bool rodata_ok = oat_writer->WriteRodata(rodata[i]);
328 ASSERT_TRUE(rodata_ok);
329 elf_writer->EndRoData(rodata[i]);
330
331 OutputStream* text = elf_writer->StartText();
332 bool text_ok = oat_writer->WriteCode(text);
333 ASSERT_TRUE(text_ok);
334 elf_writer->EndText(text);
335
Vladimir Markob066d432018-01-03 13:14:37 +0000336 if (oat_writer->GetDataBimgRelRoSize() != 0u) {
337 OutputStream* data_bimg_rel_ro = elf_writer->StartDataBimgRelRo();
338 bool data_bimg_rel_ro_ok = oat_writer->WriteDataBimgRelRo(data_bimg_rel_ro);
339 ASSERT_TRUE(data_bimg_rel_ro_ok);
340 elf_writer->EndDataBimgRelRo(data_bimg_rel_ro);
341 }
342
Vladimir Marko1352f132017-04-28 15:28:29 +0100343 bool header_ok = oat_writer->WriteHeader(elf_writer->GetStream(), 0u, 0u, 0u);
344 ASSERT_TRUE(header_ok);
345
346 writer->UpdateOatFileHeader(i, oat_writer->GetOatHeader());
347
348 elf_writer->WriteDynamicSection();
David Srbecky32210b92017-12-04 14:39:21 +0000349 elf_writer->WriteDebugInfo(oat_writer->GetDebugInfo());
Vladimir Marko1352f132017-04-28 15:28:29 +0100350
351 bool success = elf_writer->End();
352 ASSERT_TRUE(success);
353 }
354 }
355
356 bool success_image = writer->Write(kInvalidFd,
357 image_filename_vector,
358 oat_filename_vector);
359 ASSERT_TRUE(success_image);
360
361 for (size_t i = 0, size = oat_filenames.size(); i != size; ++i) {
362 const char* oat_filename = oat_filenames[i].c_str();
363 std::unique_ptr<File> oat_file(OS::OpenFileReadWrite(oat_filename));
364 ASSERT_TRUE(oat_file != nullptr);
Vladimir Marko213ee2d2018-06-22 11:56:34 +0100365 bool success_fixup = ElfWriter::Fixup(oat_file.get(), writer->GetOatDataBegin(i));
Vladimir Marko1352f132017-04-28 15:28:29 +0100366 ASSERT_TRUE(success_fixup);
367 ASSERT_EQ(oat_file->FlushCloseOrErase(), 0) << "Could not flush and close oat file "
368 << oat_filename;
369 }
370 }
371}
372
373inline void ImageTest::Compile(ImageHeader::StorageMode storage_mode,
374 CompilationHelper& helper,
375 const std::string& extra_dex,
376 const std::initializer_list<std::string>& image_classes) {
377 for (const std::string& image_class : image_classes) {
378 image_classes_.insert(image_class);
379 }
Vladimir Markoa0431112018-06-25 09:32:54 +0100380 number_of_threads_ = kIsTargetBuild ? 2U : 16U;
381 CreateCompilerDriver();
Vladimir Marko1352f132017-04-28 15:28:29 +0100382 // Set inline filter values.
383 compiler_options_->SetInlineMaxCodeUnits(CompilerOptions::kDefaultInlineMaxCodeUnits);
384 image_classes_.clear();
385 if (!extra_dex.empty()) {
386 helper.extra_dex_files = OpenTestDexFiles(extra_dex.c_str());
387 }
Vladimir Marko213ee2d2018-06-22 11:56:34 +0100388 DoCompile(storage_mode, helper);
Vladimir Marko1352f132017-04-28 15:28:29 +0100389 if (image_classes.begin() != image_classes.end()) {
390 // Make sure the class got initialized.
391 ScopedObjectAccess soa(Thread::Current());
392 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
393 for (const std::string& image_class : image_classes) {
Vladimir Markoe9987b02018-05-22 16:26:43 +0100394 ObjPtr<mirror::Class> klass =
395 class_linker->FindSystemClass(Thread::Current(), image_class.c_str());
Vladimir Marko1352f132017-04-28 15:28:29 +0100396 EXPECT_TRUE(klass != nullptr);
397 EXPECT_TRUE(klass->IsInitialized());
398 }
399 }
400}
401
402inline void ImageTest::TestWriteRead(ImageHeader::StorageMode storage_mode) {
403 CompilationHelper helper;
404 Compile(storage_mode, /*out*/ helper);
405 std::vector<uint64_t> image_file_sizes;
406 for (ScratchFile& image_file : helper.image_files) {
407 std::unique_ptr<File> file(OS::OpenFileForReading(image_file.GetFilename().c_str()));
408 ASSERT_TRUE(file.get() != nullptr);
409 ImageHeader image_header;
410 ASSERT_EQ(file->ReadFully(&image_header, sizeof(image_header)), true);
411 ASSERT_TRUE(image_header.IsValid());
Vladimir Markocd87c3e2017-09-05 13:11:57 +0100412 const auto& bitmap_section = image_header.GetImageBitmapSection();
Vladimir Marko1352f132017-04-28 15:28:29 +0100413 ASSERT_GE(bitmap_section.Offset(), sizeof(image_header));
414 ASSERT_NE(0U, bitmap_section.Size());
415
416 gc::Heap* heap = Runtime::Current()->GetHeap();
417 ASSERT_TRUE(heap->HaveContinuousSpaces());
418 gc::space::ContinuousSpace* space = heap->GetNonMovingSpace();
419 ASSERT_FALSE(space->IsImageSpace());
420 ASSERT_TRUE(space != nullptr);
421 ASSERT_TRUE(space->IsMallocSpace());
422 image_file_sizes.push_back(file->GetLength());
423 }
424
Vladimir Marko1352f132017-04-28 15:28:29 +0100425 // Need to delete the compiler since it has worker threads which are attached to runtime.
426 compiler_driver_.reset();
427
428 // Tear down old runtime before making a new one, clearing out misc state.
429
430 // Remove the reservation of the memory for use to load the image.
431 // Need to do this before we reset the runtime.
432 UnreserveImageSpace();
433
434 helper.extra_dex_files.clear();
435 runtime_.reset();
436 java_lang_dex_file_ = nullptr;
437
438 MemMap::Init();
439
440 RuntimeOptions options;
441 std::string image("-Ximage:");
442 image.append(helper.image_locations[0].GetFilename());
443 options.push_back(std::make_pair(image.c_str(), static_cast<void*>(nullptr)));
444 // By default the compiler this creates will not include patch information.
445 options.push_back(std::make_pair("-Xnorelocate", nullptr));
446
447 if (!Runtime::Create(options, false)) {
448 LOG(FATAL) << "Failed to create runtime";
449 return;
450 }
451 runtime_.reset(Runtime::Current());
452 // Runtime::Create acquired the mutator_lock_ that is normally given away when we Runtime::Start,
453 // give it away now and then switch to a more managable ScopedObjectAccess.
454 Thread::Current()->TransitionFromRunnableToSuspended(kNative);
455 ScopedObjectAccess soa(Thread::Current());
456 ASSERT_TRUE(runtime_.get() != nullptr);
457 class_linker_ = runtime_->GetClassLinker();
458
459 gc::Heap* heap = Runtime::Current()->GetHeap();
460 ASSERT_TRUE(heap->HasBootImageSpace());
461 ASSERT_TRUE(heap->GetNonMovingSpace()->IsMallocSpace());
462
463 // We loaded the runtime with an explicit image, so it must exist.
464 ASSERT_EQ(heap->GetBootImageSpaces().size(), image_file_sizes.size());
Vladimir Markodc4bcce2018-06-21 16:15:42 +0100465 const HashSet<std::string>& image_classes = compiler_options_->GetImageClasses();
Vladimir Marko1352f132017-04-28 15:28:29 +0100466 for (size_t i = 0; i < helper.dex_file_locations.size(); ++i) {
467 std::unique_ptr<const DexFile> dex(
468 LoadExpectSingleDexFile(helper.dex_file_locations[i].c_str()));
469 ASSERT_TRUE(dex != nullptr);
470 uint64_t image_file_size = image_file_sizes[i];
471 gc::space::ImageSpace* image_space = heap->GetBootImageSpaces()[i];
472 ASSERT_TRUE(image_space != nullptr);
473 if (storage_mode == ImageHeader::kStorageModeUncompressed) {
474 // Uncompressed, image should be smaller than file.
475 ASSERT_LE(image_space->GetImageHeader().GetImageSize(), image_file_size);
476 } else if (image_file_size > 16 * KB) {
477 // Compressed, file should be smaller than image. Not really valid for small images.
478 ASSERT_LE(image_file_size, image_space->GetImageHeader().GetImageSize());
479 }
480
481 image_space->VerifyImageAllocations();
482 uint8_t* image_begin = image_space->Begin();
483 uint8_t* image_end = image_space->End();
484 if (i == 0) {
485 // This check is only valid for image 0.
486 CHECK_EQ(kRequestedImageBase, reinterpret_cast<uintptr_t>(image_begin));
487 }
488 for (size_t j = 0; j < dex->NumClassDefs(); ++j) {
489 const DexFile::ClassDef& class_def = dex->GetClassDef(j);
490 const char* descriptor = dex->GetClassDescriptor(class_def);
Vladimir Markoe9987b02018-05-22 16:26:43 +0100491 ObjPtr<mirror::Class> klass = class_linker_->FindSystemClass(soa.Self(), descriptor);
Vladimir Marko1352f132017-04-28 15:28:29 +0100492 EXPECT_TRUE(klass != nullptr) << descriptor;
Vladimir Markoe9987b02018-05-22 16:26:43 +0100493 uint8_t* raw_klass = reinterpret_cast<uint8_t*>(klass.Ptr());
Vladimir Marko54159c62018-06-20 14:30:08 +0100494 if (image_classes.find(StringPiece(descriptor)) == image_classes.end()) {
Vladimir Markoe9987b02018-05-22 16:26:43 +0100495 EXPECT_TRUE(raw_klass >= image_end || raw_klass < image_begin) << descriptor;
Vladimir Marko1352f132017-04-28 15:28:29 +0100496 } else {
497 // Image classes should be located inside the image.
Vladimir Markoe9987b02018-05-22 16:26:43 +0100498 EXPECT_LT(image_begin, raw_klass) << descriptor;
499 EXPECT_LT(raw_klass, image_end) << descriptor;
Vladimir Marko1352f132017-04-28 15:28:29 +0100500 }
501 EXPECT_TRUE(Monitor::IsValidLockWord(klass->GetLockWord(false)));
502 }
503 }
504}
505
Vladimir Marko74527972016-11-29 15:57:32 +0000506} // namespace linker
Vladimir Marko1352f132017-04-28 15:28:29 +0100507} // namespace art
508
Vladimir Marko74527972016-11-29 15:57:32 +0000509#endif // ART_DEX2OAT_LINKER_IMAGE_TEST_H_