blob: 2acdf256e197d7781c769bf1759d268a8203d03f [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"
38#include "driver/compiler_options.h"
Vladimir Marko1352f132017-04-28 15:28:29 +010039#include "gc/space/image_space.h"
40#include "image_writer.h"
41#include "linker/buffered_output_stream.h"
Vladimir Marko74527972016-11-29 15:57:32 +000042#include "linker/elf_writer.h"
43#include "linker/elf_writer_quick.h"
Vladimir Marko1352f132017-04-28 15:28:29 +010044#include "linker/file_output_stream.h"
45#include "linker/multi_oat_relative_patcher.h"
46#include "lock_word.h"
47#include "mirror/object-inl.h"
48#include "oat_writer.h"
49#include "scoped_thread_state_change-inl.h"
50#include "signal_catcher.h"
Vladimir Marko1352f132017-04-28 15:28:29 +010051
52namespace art {
Vladimir Marko74527972016-11-29 15:57:32 +000053namespace linker {
Vladimir Marko1352f132017-04-28 15:28:29 +010054
55static const uintptr_t kRequestedImageBase = ART_BASE_ADDRESS;
56
57struct CompilationHelper {
58 std::vector<std::string> dex_file_locations;
59 std::vector<ScratchFile> image_locations;
60 std::vector<std::unique_ptr<const DexFile>> extra_dex_files;
61 std::vector<ScratchFile> image_files;
62 std::vector<ScratchFile> oat_files;
63 std::vector<ScratchFile> vdex_files;
64 std::string image_dir;
65
66 void Compile(CompilerDriver* driver,
67 ImageHeader::StorageMode storage_mode);
68
69 std::vector<size_t> GetImageObjectSectionSizes();
70
71 ~CompilationHelper();
72};
73
74class ImageTest : public CommonCompilerTest {
75 protected:
76 virtual void SetUp() {
77 ReserveImageSpace();
78 CommonCompilerTest::SetUp();
79 }
80
81 void TestWriteRead(ImageHeader::StorageMode storage_mode);
82
83 void Compile(ImageHeader::StorageMode storage_mode,
84 CompilationHelper& out_helper,
85 const std::string& extra_dex = "",
86 const std::initializer_list<std::string>& image_classes = {});
87
88 void SetUpRuntimeOptions(RuntimeOptions* options) OVERRIDE {
89 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
Vladimir Marko54159c62018-06-20 14:30:08 +010097 std::unique_ptr<HashSet<std::string>> GetImageClasses() OVERRIDE {
98 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 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
144inline void CompilationHelper::Compile(CompilerDriver* driver,
145 ImageHeader::StorageMode storage_mode) {
146 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
147 std::vector<const DexFile*> class_path = class_linker->GetBootClassPath();
148
149 for (const std::unique_ptr<const DexFile>& dex_file : extra_dex_files) {
150 {
151 ScopedObjectAccess soa(Thread::Current());
152 // Inject in boot class path so that the compiler driver can see it.
153 class_linker->AppendToBootClassPath(soa.Self(), *dex_file.get());
154 }
155 class_path.push_back(dex_file.get());
156 }
157
158 // Enable write for dex2dex.
159 for (const DexFile* dex_file : class_path) {
160 dex_file_locations.push_back(dex_file->GetLocation());
161 if (dex_file->IsReadOnly()) {
162 dex_file->EnableWrite();
163 }
164 }
165 {
166 // Create a generic tmp file, to be the base of the .art and .oat temporary files.
167 ScratchFile location;
168 for (int i = 0; i < static_cast<int>(class_path.size()); ++i) {
169 std::string cur_location =
170 android::base::StringPrintf("%s-%d.art", location.GetFilename().c_str(), i);
171 image_locations.push_back(ScratchFile(cur_location));
172 }
173 }
174 std::vector<std::string> image_filenames;
175 for (ScratchFile& file : image_locations) {
176 std::string image_filename(GetSystemImageFilename(file.GetFilename().c_str(), kRuntimeISA));
177 image_filenames.push_back(image_filename);
178 size_t pos = image_filename.rfind('/');
179 CHECK_NE(pos, std::string::npos) << image_filename;
180 if (image_dir.empty()) {
181 image_dir = image_filename.substr(0, pos);
182 int mkdir_result = mkdir(image_dir.c_str(), 0700);
183 CHECK_EQ(0, mkdir_result) << image_dir;
184 }
185 image_files.push_back(ScratchFile(OS::CreateEmptyFile(image_filename.c_str())));
186 }
187
188 std::vector<std::string> oat_filenames;
189 std::vector<std::string> vdex_filenames;
190 for (const std::string& image_filename : image_filenames) {
191 std::string oat_filename = ReplaceFileExtension(image_filename, "oat");
192 oat_files.push_back(ScratchFile(OS::CreateEmptyFile(oat_filename.c_str())));
193 oat_filenames.push_back(oat_filename);
194 std::string vdex_filename = ReplaceFileExtension(image_filename, "vdex");
195 vdex_files.push_back(ScratchFile(OS::CreateEmptyFile(vdex_filename.c_str())));
196 vdex_filenames.push_back(vdex_filename);
197 }
198
199 std::unordered_map<const DexFile*, size_t> dex_file_to_oat_index_map;
200 std::vector<const char*> oat_filename_vector;
201 for (const std::string& file : oat_filenames) {
202 oat_filename_vector.push_back(file.c_str());
203 }
204 std::vector<const char*> image_filename_vector;
205 for (const std::string& file : image_filenames) {
206 image_filename_vector.push_back(file.c_str());
207 }
208 size_t image_idx = 0;
209 for (const DexFile* dex_file : class_path) {
210 dex_file_to_oat_index_map.emplace(dex_file, image_idx);
211 ++image_idx;
212 }
213 // TODO: compile_pic should be a test argument.
214 std::unique_ptr<ImageWriter> writer(new ImageWriter(*driver,
215 kRequestedImageBase,
216 /*compile_pic*/false,
217 /*compile_app_image*/false,
218 storage_mode,
219 oat_filename_vector,
Jeff Haoc23b0c02017-07-27 18:19:38 -0700220 dex_file_to_oat_index_map,
221 /*dirty_image_objects*/nullptr));
Vladimir Marko1352f132017-04-28 15:28:29 +0100222 {
223 {
224 jobject class_loader = nullptr;
225 TimingLogger timings("ImageTest::WriteRead", false, false);
226 TimingLogger::ScopedTiming t("CompileAll", &timings);
227 driver->SetDexFilesForOatFile(class_path);
Nicolas Geoffray1cfea7a2017-05-24 14:44:38 +0100228 driver->CompileAll(class_loader, class_path, &timings);
Vladimir Marko1352f132017-04-28 15:28:29 +0100229
230 t.NewTiming("WriteElf");
231 SafeMap<std::string, std::string> key_value_store;
232 std::vector<const char*> dex_filename_vector;
233 for (size_t i = 0; i < class_path.size(); ++i) {
234 dex_filename_vector.push_back("");
235 }
236 key_value_store.Put(OatHeader::kBootClassPathKey,
237 gc::space::ImageSpace::GetMultiImageBootClassPath(
238 dex_filename_vector,
239 oat_filename_vector,
240 image_filename_vector));
241
242 std::vector<std::unique_ptr<ElfWriter>> elf_writers;
243 std::vector<std::unique_ptr<OatWriter>> oat_writers;
244 for (ScratchFile& oat_file : oat_files) {
245 elf_writers.emplace_back(CreateElfWriterQuick(driver->GetInstructionSet(),
246 driver->GetInstructionSetFeatures(),
247 &driver->GetCompilerOptions(),
248 oat_file.GetFile()));
249 elf_writers.back()->Start();
250 oat_writers.emplace_back(new OatWriter(/*compiling_boot_image*/true,
251 &timings,
Mathieu Chartier603ccab2017-10-20 14:34:28 -0700252 /*profile_compilation_info*/nullptr,
253 CompactDexLevel::kCompactDexLevelNone));
Vladimir Marko1352f132017-04-28 15:28:29 +0100254 }
255
256 std::vector<OutputStream*> rodata;
Nicolas Geoffrayf3075272018-01-08 12:41:19 +0000257 std::vector<std::unique_ptr<MemMap>> opened_dex_files_maps;
Vladimir Marko1352f132017-04-28 15:28:29 +0100258 std::vector<std::unique_ptr<const DexFile>> opened_dex_files;
259 // Now that we have finalized key_value_store_, start writing the oat file.
260 for (size_t i = 0, size = oat_writers.size(); i != size; ++i) {
261 const DexFile* dex_file = class_path[i];
262 rodata.push_back(elf_writers[i]->StartRoData());
263 ArrayRef<const uint8_t> raw_dex_file(
264 reinterpret_cast<const uint8_t*>(&dex_file->GetHeader()),
265 dex_file->GetHeader().file_size_);
266 oat_writers[i]->AddRawDexFileSource(raw_dex_file,
267 dex_file->GetLocation().c_str(),
268 dex_file->GetLocationChecksum());
269
Nicolas Geoffrayf3075272018-01-08 12:41:19 +0000270 std::vector<std::unique_ptr<MemMap>> cur_opened_dex_files_maps;
Vladimir Marko1352f132017-04-28 15:28:29 +0100271 std::vector<std::unique_ptr<const DexFile>> cur_opened_dex_files;
272 bool dex_files_ok = oat_writers[i]->WriteAndOpenDexFiles(
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +0000273 vdex_files[i].GetFile(),
Vladimir Marko1352f132017-04-28 15:28:29 +0100274 rodata.back(),
275 driver->GetInstructionSet(),
276 driver->GetInstructionSetFeatures(),
277 &key_value_store,
278 /* verify */ false, // Dex files may be dex-to-dex-ed, don't verify.
279 /* update_input_vdex */ false,
Nicolas Geoffray66ff8a82018-02-28 13:27:55 +0000280 /* copy_dex_files */ CopyOption::kOnlyIfCompressed,
Nicolas Geoffrayf3075272018-01-08 12:41:19 +0000281 &cur_opened_dex_files_maps,
Vladimir Marko1352f132017-04-28 15:28:29 +0100282 &cur_opened_dex_files);
283 ASSERT_TRUE(dex_files_ok);
284
Nicolas Geoffrayf3075272018-01-08 12:41:19 +0000285 if (!cur_opened_dex_files_maps.empty()) {
286 for (std::unique_ptr<MemMap>& cur_map : cur_opened_dex_files_maps) {
287 opened_dex_files_maps.push_back(std::move(cur_map));
288 }
Vladimir Marko1352f132017-04-28 15:28:29 +0100289 for (std::unique_ptr<const DexFile>& cur_dex_file : cur_opened_dex_files) {
290 // dex_file_oat_index_map_.emplace(dex_file.get(), i);
291 opened_dex_files.push_back(std::move(cur_dex_file));
292 }
293 } else {
294 ASSERT_TRUE(cur_opened_dex_files.empty());
295 }
296 }
Mathieu Chartier703b82a2018-04-10 15:52:32 -0700297 bool image_space_ok = writer->PrepareImageAddressSpace(&timings);
Vladimir Marko1352f132017-04-28 15:28:29 +0100298 ASSERT_TRUE(image_space_ok);
299
David Brazdil93592f52017-12-08 10:53:27 +0000300 DCHECK_EQ(vdex_files.size(), oat_files.size());
Vladimir Marko1352f132017-04-28 15:28:29 +0100301 for (size_t i = 0, size = oat_files.size(); i != size; ++i) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +0100302 MultiOatRelativePatcher patcher(driver->GetInstructionSet(),
Vladimir Markoca1e0382018-04-11 09:58:41 +0000303 driver->GetInstructionSetFeatures(),
304 driver->GetCompiledMethodStorage());
Vladimir Marko1352f132017-04-28 15:28:29 +0100305 OatWriter* const oat_writer = oat_writers[i].get();
306 ElfWriter* const elf_writer = elf_writers[i].get();
307 std::vector<const DexFile*> cur_dex_files(1u, class_path[i]);
308 oat_writer->Initialize(driver, writer.get(), cur_dex_files);
David Brazdil93592f52017-12-08 10:53:27 +0000309
310 std::unique_ptr<BufferedOutputStream> vdex_out =
311 std::make_unique<BufferedOutputStream>(
312 std::make_unique<FileOutputStream>(vdex_files[i].GetFile()));
313 oat_writer->WriteVerifierDeps(vdex_out.get(), nullptr);
314 oat_writer->WriteQuickeningInfo(vdex_out.get());
315 oat_writer->WriteChecksumsAndVdexHeader(vdex_out.get());
316
Vladimir Marko1352f132017-04-28 15:28:29 +0100317 oat_writer->PrepareLayout(&patcher);
Vladimir Markob066d432018-01-03 13:14:37 +0000318 elf_writer->PrepareDynamicSection(oat_writer->GetOatHeader().GetExecutableOffset(),
319 oat_writer->GetCodeSize(),
320 oat_writer->GetDataBimgRelRoSize(),
Vladimir Marko1352f132017-04-28 15:28:29 +0100321 oat_writer->GetBssSize(),
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100322 oat_writer->GetBssMethodsOffset(),
David Srbeckyec2cdf42017-12-08 16:21:25 +0000323 oat_writer->GetBssRootsOffset(),
324 oat_writer->GetVdexSize());
Vladimir Marko1352f132017-04-28 15:28:29 +0100325
326 writer->UpdateOatFileLayout(i,
327 elf_writer->GetLoadedSize(),
328 oat_writer->GetOatDataOffset(),
329 oat_writer->GetOatSize());
330
331 bool rodata_ok = oat_writer->WriteRodata(rodata[i]);
332 ASSERT_TRUE(rodata_ok);
333 elf_writer->EndRoData(rodata[i]);
334
335 OutputStream* text = elf_writer->StartText();
336 bool text_ok = oat_writer->WriteCode(text);
337 ASSERT_TRUE(text_ok);
338 elf_writer->EndText(text);
339
Vladimir Markob066d432018-01-03 13:14:37 +0000340 if (oat_writer->GetDataBimgRelRoSize() != 0u) {
341 OutputStream* data_bimg_rel_ro = elf_writer->StartDataBimgRelRo();
342 bool data_bimg_rel_ro_ok = oat_writer->WriteDataBimgRelRo(data_bimg_rel_ro);
343 ASSERT_TRUE(data_bimg_rel_ro_ok);
344 elf_writer->EndDataBimgRelRo(data_bimg_rel_ro);
345 }
346
Vladimir Marko1352f132017-04-28 15:28:29 +0100347 bool header_ok = oat_writer->WriteHeader(elf_writer->GetStream(), 0u, 0u, 0u);
348 ASSERT_TRUE(header_ok);
349
350 writer->UpdateOatFileHeader(i, oat_writer->GetOatHeader());
351
352 elf_writer->WriteDynamicSection();
David Srbecky32210b92017-12-04 14:39:21 +0000353 elf_writer->WriteDebugInfo(oat_writer->GetDebugInfo());
Vladimir Marko1352f132017-04-28 15:28:29 +0100354
355 bool success = elf_writer->End();
356 ASSERT_TRUE(success);
357 }
358 }
359
360 bool success_image = writer->Write(kInvalidFd,
361 image_filename_vector,
362 oat_filename_vector);
363 ASSERT_TRUE(success_image);
364
365 for (size_t i = 0, size = oat_filenames.size(); i != size; ++i) {
366 const char* oat_filename = oat_filenames[i].c_str();
367 std::unique_ptr<File> oat_file(OS::OpenFileReadWrite(oat_filename));
368 ASSERT_TRUE(oat_file != nullptr);
369 bool success_fixup = ElfWriter::Fixup(oat_file.get(),
370 writer->GetOatDataBegin(i));
371 ASSERT_TRUE(success_fixup);
372 ASSERT_EQ(oat_file->FlushCloseOrErase(), 0) << "Could not flush and close oat file "
373 << oat_filename;
374 }
375 }
376}
377
378inline void ImageTest::Compile(ImageHeader::StorageMode storage_mode,
379 CompilationHelper& helper,
380 const std::string& extra_dex,
381 const std::initializer_list<std::string>& image_classes) {
382 for (const std::string& image_class : image_classes) {
383 image_classes_.insert(image_class);
384 }
385 CreateCompilerDriver(Compiler::kOptimizing, kRuntimeISA, kIsTargetBuild ? 2U : 16U);
386 // Set inline filter values.
387 compiler_options_->SetInlineMaxCodeUnits(CompilerOptions::kDefaultInlineMaxCodeUnits);
388 image_classes_.clear();
389 if (!extra_dex.empty()) {
390 helper.extra_dex_files = OpenTestDexFiles(extra_dex.c_str());
391 }
392 helper.Compile(compiler_driver_.get(), storage_mode);
393 if (image_classes.begin() != image_classes.end()) {
394 // Make sure the class got initialized.
395 ScopedObjectAccess soa(Thread::Current());
396 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
397 for (const std::string& image_class : image_classes) {
Vladimir Markoe9987b02018-05-22 16:26:43 +0100398 ObjPtr<mirror::Class> klass =
399 class_linker->FindSystemClass(Thread::Current(), image_class.c_str());
Vladimir Marko1352f132017-04-28 15:28:29 +0100400 EXPECT_TRUE(klass != nullptr);
401 EXPECT_TRUE(klass->IsInitialized());
402 }
403 }
404}
405
406inline void ImageTest::TestWriteRead(ImageHeader::StorageMode storage_mode) {
407 CompilationHelper helper;
408 Compile(storage_mode, /*out*/ helper);
409 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;
445 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());
483 }
484
485 image_space->VerifyImageAllocations();
486 uint8_t* image_begin = image_space->Begin();
487 uint8_t* image_end = image_space->End();
488 if (i == 0) {
489 // This check is only valid for image 0.
490 CHECK_EQ(kRequestedImageBase, reinterpret_cast<uintptr_t>(image_begin));
491 }
492 for (size_t j = 0; j < dex->NumClassDefs(); ++j) {
493 const DexFile::ClassDef& class_def = dex->GetClassDef(j);
494 const char* descriptor = dex->GetClassDescriptor(class_def);
Vladimir Markoe9987b02018-05-22 16:26:43 +0100495 ObjPtr<mirror::Class> klass = class_linker_->FindSystemClass(soa.Self(), descriptor);
Vladimir Marko1352f132017-04-28 15:28:29 +0100496 EXPECT_TRUE(klass != nullptr) << descriptor;
Vladimir Markoe9987b02018-05-22 16:26:43 +0100497 uint8_t* raw_klass = reinterpret_cast<uint8_t*>(klass.Ptr());
Vladimir Marko54159c62018-06-20 14:30:08 +0100498 if (image_classes.find(StringPiece(descriptor)) == image_classes.end()) {
Vladimir Markoe9987b02018-05-22 16:26:43 +0100499 EXPECT_TRUE(raw_klass >= image_end || raw_klass < image_begin) << descriptor;
Vladimir Marko1352f132017-04-28 15:28:29 +0100500 } else {
501 // Image classes should be located inside the image.
Vladimir Markoe9987b02018-05-22 16:26:43 +0100502 EXPECT_LT(image_begin, raw_klass) << descriptor;
503 EXPECT_LT(raw_klass, image_end) << descriptor;
Vladimir Marko1352f132017-04-28 15:28:29 +0100504 }
505 EXPECT_TRUE(Monitor::IsValidLockWord(klass->GetLockWord(false)));
506 }
507 }
508}
509
Vladimir Marko74527972016-11-29 15:57:32 +0000510} // namespace linker
Vladimir Marko1352f132017-04-28 15:28:29 +0100511} // namespace art
512
Vladimir Marko74527972016-11-29 15:57:32 +0000513#endif // ART_DEX2OAT_LINKER_IMAGE_TEST_H_