blob: 421a1d5e0fd76aaadf5e26c9274ef6a955ca95b4 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
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 */
Brian Carlstromdb4d5402011-08-09 12:18:28 -070016
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080017#include "image.h"
18
Ian Rogers700a4022014-05-19 16:49:03 -070019#include <memory>
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070020#include <string>
21#include <vector>
22
Ian Rogerse63db272014-07-15 15:36:11 -070023#include "base/unix_file/fd_file.h"
Vladimir Marko3481ba22015-04-13 12:22:36 +010024#include "class_linker-inl.h"
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080025#include "common_compiler_test.h"
David Srbecky4fda4eb2016-02-05 13:34:46 +000026#include "debug/method_debug_info.h"
Andreas Gampe3f41a012016-02-18 16:53:41 -080027#include "driver/compiler_options.h"
Tong Shen62d1ca32014-09-03 17:24:56 -070028#include "elf_writer.h"
Vladimir Marko10c13562015-11-25 14:33:36 +000029#include "elf_writer_quick.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070030#include "gc/space/image_space.h"
Ian Rogerse63db272014-07-15 15:36:11 -070031#include "image_writer.h"
Vladimir Marko944da602016-02-19 12:27:55 +000032#include "linker/multi_oat_relative_patcher.h"
Ian Rogersd9c4fc92013-10-01 19:45:43 -070033#include "lock_word.h"
34#include "mirror/object-inl.h"
Ian Rogerse63db272014-07-15 15:36:11 -070035#include "oat_writer.h"
36#include "scoped_thread_state_change.h"
Brian Carlstrom51c24672013-07-11 16:00:56 -070037#include "signal_catcher.h"
buzbeec143c552011-08-20 17:38:58 -070038#include "utils.h"
Brian Carlstromdb4d5402011-08-09 12:18:28 -070039
Brian Carlstromdb4d5402011-08-09 12:18:28 -070040namespace art {
41
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080042class ImageTest : public CommonCompilerTest {
Ian Rogers10c5b782013-01-10 10:40:53 -080043 protected:
44 virtual void SetUp() {
Brian Carlstrom700c8d32012-11-05 10:42:02 -080045 ReserveImageSpace();
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080046 CommonCompilerTest::SetUp();
Ian Rogers10c5b782013-01-10 10:40:53 -080047 }
Mathieu Chartierceb07b32015-12-10 09:33:21 -080048 void TestWriteRead(ImageHeader::StorageMode storage_mode);
Ian Rogers10c5b782013-01-10 10:40:53 -080049};
Brian Carlstromdb4d5402011-08-09 12:18:28 -070050
Mathieu Chartierceb07b32015-12-10 09:33:21 -080051void ImageTest::TestWriteRead(ImageHeader::StorageMode storage_mode) {
Andreas Gampe3f41a012016-02-18 16:53:41 -080052 CreateCompilerDriver(Compiler::kOptimizing, kRuntimeISA, kIsTargetBuild ? 2U : 16U);
53
54 // Set inline filter values.
55 compiler_options_->SetInlineDepthLimit(CompilerOptions::kDefaultInlineDepthLimit);
56 compiler_options_->SetInlineMaxCodeUnits(CompilerOptions::kDefaultInlineMaxCodeUnits);
57
Mathieu Chartierceb07b32015-12-10 09:33:21 -080058 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartier866d8742016-09-21 15:24:18 -070059 const std::vector<const DexFile*>& boot_class_path = class_linker->GetBootClassPath();
60 const size_t num_images = boot_class_path.size();
61
Mathieu Chartierceb07b32015-12-10 09:33:21 -080062 // Enable write for dex2dex.
Mathieu Chartier866d8742016-09-21 15:24:18 -070063 for (const DexFile* dex_file : boot_class_path) {
Mathieu Chartierceb07b32015-12-10 09:33:21 -080064 dex_file->EnableWrite();
65 }
Brian Carlstrom0e12bdc2014-05-14 17:44:28 -070066 // Create a generic location tmp file, to be the base of the .art and .oat temporary files.
Mathieu Chartier866d8742016-09-21 15:24:18 -070067 std::vector<ScratchFile> image_locations;
68 {
69 ScratchFile location;
70 for (int i = 0; i < static_cast<int>(num_images); ++i) {
71 std::string cur_location(StringPrintf("%s-%d.art", location.GetFilename().c_str(), i));
72 image_locations.push_back(ScratchFile(cur_location));
73 }
74 }
75 std::vector<std::string> image_filenames;
76 std::vector<ScratchFile> image_files;
77 std::string image_dir;
78 for (ScratchFile& file : image_locations) {
79 std::string image_filename(GetSystemImageFilename(file.GetFilename().c_str(), kRuntimeISA));
80 image_filenames.push_back(image_filename);
81 size_t pos = image_filename.rfind('/');
82 CHECK_NE(pos, std::string::npos) << image_filename;
83 if (image_dir.empty()) {
84 image_dir = image_filename.substr(0, pos);
85 int mkdir_result = mkdir(image_dir.c_str(), 0700);
86 CHECK_EQ(0, mkdir_result) << image_dir;
87 }
88 image_files.push_back(ScratchFile(OS::CreateEmptyFile(image_filename.c_str())));
89 }
Brian Carlstrom0e12bdc2014-05-14 17:44:28 -070090
Mathieu Chartier866d8742016-09-21 15:24:18 -070091 std::vector<std::string> oat_filenames;
92 std::vector<ScratchFile> oat_files;
93 std::vector<std::string> vdex_filenames;
94 std::vector<ScratchFile> vdex_files;
95 for (const std::string& image_filename : image_filenames) {
96 std::string oat_filename = ReplaceFileExtension(image_filename, "oat");
97 oat_files.push_back(ScratchFile(OS::CreateEmptyFile(oat_filename.c_str())));
98 oat_filenames.push_back(oat_filename);
99 std::string vdex_filename = ReplaceFileExtension(image_filename, "vdex");
100 vdex_files.push_back(ScratchFile(OS::CreateEmptyFile(vdex_filename.c_str())));
101 vdex_filenames.push_back(vdex_filename);
102 }
David Brazdil7b49e6c2016-09-01 11:06:18 +0100103
Vladimir Markof4da6752014-08-01 19:04:18 +0100104 const uintptr_t requested_image_base = ART_BASE_ADDRESS;
Vladimir Marko944da602016-02-19 12:27:55 +0000105 std::unordered_map<const DexFile*, size_t> dex_file_to_oat_index_map;
Mathieu Chartier866d8742016-09-21 15:24:18 -0700106 std::vector<const char*> oat_filename_vector;
107 for (const std::string& file : oat_filenames) {
108 oat_filename_vector.push_back(file.c_str());
Jeff Haodcdc85b2015-12-04 14:06:18 -0800109 }
Mathieu Chartier866d8742016-09-21 15:24:18 -0700110 std::vector<const char*> image_filename_vector;
111 for (const std::string& file : image_filenames) {
112 image_filename_vector.push_back(file.c_str());
113 }
114 size_t image_idx = 0;
115 for (const DexFile* dex_file : boot_class_path) {
116 dex_file_to_oat_index_map.emplace(dex_file, image_idx);
117 ++image_idx;
118 }
119 // TODO: compile_pic should be a test argument.
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800120 std::unique_ptr<ImageWriter> writer(new ImageWriter(*compiler_driver_,
121 requested_image_base,
122 /*compile_pic*/false,
Mathieu Chartierceb07b32015-12-10 09:33:21 -0800123 /*compile_app_image*/false,
Jeff Haodcdc85b2015-12-04 14:06:18 -0800124 storage_mode,
125 oat_filename_vector,
Vladimir Marko944da602016-02-19 12:27:55 +0000126 dex_file_to_oat_index_map));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700127 {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800128 {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700129 jobject class_loader = nullptr;
Ian Rogers5fe9af72013-11-14 00:17:20 -0800130 TimingLogger timings("ImageTest::WriteRead", false, false);
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700131 TimingLogger::ScopedTiming t("CompileAll", &timings);
Vladimir Markod1eaf0d2015-10-29 12:18:29 +0000132 compiler_driver_->SetDexFilesForOatFile(class_linker->GetBootClassPath());
Ian Rogers3d504072014-03-01 09:16:49 -0800133 compiler_driver_->CompileAll(class_loader, class_linker->GetBootClassPath(), &timings);
Brian Carlstrom96391602013-06-13 19:49:50 -0700134
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700135 t.NewTiming("WriteElf");
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700136 SafeMap<std::string, std::string> key_value_store;
Mathieu Chartier866d8742016-09-21 15:24:18 -0700137 std::vector<const char*> dex_filename_vector;
138 for (size_t i = 0; i < boot_class_path.size(); ++i) {
139 dex_filename_vector.push_back("");
140 }
141 key_value_store.Put(OatHeader::kBootClassPathKey,
142 gc::space::ImageSpace::GetMultiImageBootClassPath(
143 dex_filename_vector,
144 oat_filename_vector,
145 image_filename_vector));
146
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000147 const std::vector<const DexFile*>& dex_files = class_linker->GetBootClassPath();
Mathieu Chartier866d8742016-09-21 15:24:18 -0700148 std::vector<std::unique_ptr<ElfWriter>> elf_writers;
149 std::vector<std::unique_ptr<OatWriter>> oat_writers;
150 for (ScratchFile& oat_file : oat_files) {
151 elf_writers.emplace_back(CreateElfWriterQuick(compiler_driver_->GetInstructionSet(),
152 compiler_driver_->GetInstructionSetFeatures(),
153 &compiler_driver_->GetCompilerOptions(),
154 oat_file.GetFile()));
155 elf_writers.back()->Start();
156 oat_writers.emplace_back(new OatWriter(/*compiling_boot_image*/true, &timings));
157 }
158
159 std::vector<OutputStream*> rodata;
160 std::vector<std::unique_ptr<MemMap>> opened_dex_files_map;
161 std::vector<std::unique_ptr<const DexFile>> opened_dex_files;
162 // Now that we have finalized key_value_store_, start writing the oat file.
163 for (size_t i = 0, size = oat_writers.size(); i != size; ++i) {
164 const DexFile* dex_file = dex_files[i];
165 rodata.push_back(elf_writers[i]->StartRoData());
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000166 ArrayRef<const uint8_t> raw_dex_file(
167 reinterpret_cast<const uint8_t*>(&dex_file->GetHeader()),
168 dex_file->GetHeader().file_size_);
Mathieu Chartier866d8742016-09-21 15:24:18 -0700169 oat_writers[i]->AddRawDexFileSource(raw_dex_file,
170 dex_file->GetLocation().c_str(),
171 dex_file->GetLocationChecksum());
172
173 std::unique_ptr<MemMap> cur_opened_dex_files_map;
174 std::vector<std::unique_ptr<const DexFile>> cur_opened_dex_files;
175 bool dex_files_ok = oat_writers[i]->WriteAndOpenDexFiles(
176 kIsVdexEnabled ? vdex_files[i].GetFile() : oat_files[i].GetFile(),
177 rodata.back(),
David Brazdil7b49e6c2016-09-01 11:06:18 +0100178 compiler_driver_->GetInstructionSet(),
179 compiler_driver_->GetInstructionSetFeatures(),
180 &key_value_store,
181 /* verify */ false, // Dex files may be dex-to-dex-ed, don't verify.
Mathieu Chartier866d8742016-09-21 15:24:18 -0700182 &cur_opened_dex_files_map,
183 &cur_opened_dex_files);
David Brazdil7b49e6c2016-09-01 11:06:18 +0100184 ASSERT_TRUE(dex_files_ok);
Vladimir Marko944da602016-02-19 12:27:55 +0000185
Mathieu Chartier866d8742016-09-21 15:24:18 -0700186 if (cur_opened_dex_files_map != nullptr) {
187 opened_dex_files_map.push_back(std::move(cur_opened_dex_files_map));
188 for (std::unique_ptr<const DexFile>& cur_dex_file : cur_opened_dex_files) {
189 // dex_file_oat_index_map_.emplace(dex_file.get(), i);
190 opened_dex_files.push_back(std::move(cur_dex_file));
191 }
192 } else {
193 ASSERT_TRUE(cur_opened_dex_files.empty());
194 }
195 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000196 bool image_space_ok = writer->PrepareImageAddressSpace();
197 ASSERT_TRUE(image_space_ok);
198
Mathieu Chartier866d8742016-09-21 15:24:18 -0700199 for (size_t i = 0, size = oat_files.size(); i != size; ++i) {
200 linker::MultiOatRelativePatcher patcher(compiler_driver_->GetInstructionSet(),
201 instruction_set_features_.get());
202 OatWriter* const oat_writer = oat_writers[i].get();
203 ElfWriter* const elf_writer = elf_writers[i].get();
204 std::vector<const DexFile*> cur_dex_files(1u, dex_files[i]);
205 oat_writer->PrepareLayout(compiler_driver_.get(), writer.get(), cur_dex_files, &patcher);
206 size_t rodata_size = oat_writer->GetOatHeader().GetExecutableOffset();
207 size_t text_size = oat_writer->GetOatSize() - rodata_size;
208 elf_writer->SetLoadedSectionSizes(rodata_size, text_size, oat_writer->GetBssSize());
Vladimir Marko944da602016-02-19 12:27:55 +0000209
Mathieu Chartier866d8742016-09-21 15:24:18 -0700210 writer->UpdateOatFileLayout(i,
211 elf_writer->GetLoadedSize(),
212 oat_writer->GetOatDataOffset(),
213 oat_writer->GetOatSize());
Vladimir Marko944da602016-02-19 12:27:55 +0000214
Mathieu Chartier866d8742016-09-21 15:24:18 -0700215 bool rodata_ok = oat_writer->WriteRodata(rodata[i]);
216 ASSERT_TRUE(rodata_ok);
217 elf_writer->EndRoData(rodata[i]);
Vladimir Marko10c13562015-11-25 14:33:36 +0000218
Mathieu Chartier866d8742016-09-21 15:24:18 -0700219 OutputStream* text = elf_writer->StartText();
220 bool text_ok = oat_writer->WriteCode(text);
221 ASSERT_TRUE(text_ok);
222 elf_writer->EndText(text);
Vladimir Marko10c13562015-11-25 14:33:36 +0000223
Mathieu Chartier866d8742016-09-21 15:24:18 -0700224 bool header_ok = oat_writer->WriteHeader(elf_writer->GetStream(), 0u, 0u, 0u);
225 ASSERT_TRUE(header_ok);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000226
Mathieu Chartier866d8742016-09-21 15:24:18 -0700227 writer->UpdateOatFileHeader(i, oat_writer->GetOatHeader());
Vladimir Marko944da602016-02-19 12:27:55 +0000228
Mathieu Chartier866d8742016-09-21 15:24:18 -0700229 elf_writer->WriteDynamicSection();
230 elf_writer->WriteDebugInfo(oat_writer->GetMethodDebugInfo());
231 elf_writer->WritePatchLocations(oat_writer->GetAbsolutePatchLocations());
Vladimir Marko10c13562015-11-25 14:33:36 +0000232
Mathieu Chartier866d8742016-09-21 15:24:18 -0700233 bool success = elf_writer->End();
234 ASSERT_TRUE(success);
235 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700236 }
Ian Rogers0571d352011-11-03 19:51:38 -0700237 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700238
jeffhao8161c032012-10-31 15:50:00 -0700239 {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800240 bool success_image = writer->Write(kInvalidFd,
Mathieu Chartier866d8742016-09-21 15:24:18 -0700241 image_filename_vector,
242 oat_filename_vector);
jeffhao8161c032012-10-31 15:50:00 -0700243 ASSERT_TRUE(success_image);
Andreas Gampe4303ba92014-11-06 01:00:46 -0800244
Mathieu Chartier866d8742016-09-21 15:24:18 -0700245 for (size_t i = 0, size = oat_filenames.size(); i != size; ++i) {
246 const char* oat_filename = oat_filenames[i].c_str();
247 std::unique_ptr<File> oat_file(OS::OpenFileReadWrite(oat_filename));
248 ASSERT_TRUE(oat_file != nullptr);
249 bool success_fixup = ElfWriter::Fixup(oat_file.get(),
250 writer->GetOatDataBegin(i));
251 ASSERT_TRUE(success_fixup);
252 ASSERT_EQ(oat_file->FlushCloseOrErase(), 0) << "Could not flush and close oat file "
253 << oat_filename;
254 }
jeffhao8161c032012-10-31 15:50:00 -0700255 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700256
Mathieu Chartier866d8742016-09-21 15:24:18 -0700257 std::vector<uint64_t> image_file_sizes;
258 for (ScratchFile& image_file : image_files) {
Ian Rogers700a4022014-05-19 16:49:03 -0700259 std::unique_ptr<File> file(OS::OpenFileForReading(image_file.GetFilename().c_str()));
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700260 ASSERT_TRUE(file.get() != nullptr);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700261 ImageHeader image_header;
Andreas Gampe4303ba92014-11-06 01:00:46 -0800262 ASSERT_EQ(file->ReadFully(&image_header, sizeof(image_header)), true);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700263 ASSERT_TRUE(image_header.IsValid());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700264 const auto& bitmap_section = image_header.GetImageSection(ImageHeader::kSectionImageBitmap);
265 ASSERT_GE(bitmap_section.Offset(), sizeof(image_header));
266 ASSERT_NE(0U, bitmap_section.Size());
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700267
Ian Rogers1d54e732013-05-02 21:10:01 -0700268 gc::Heap* heap = Runtime::Current()->GetHeap();
Mathieu Chartiera9d82fe2016-01-25 20:06:11 -0800269 ASSERT_TRUE(heap->HaveContinuousSpaces());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700270 gc::space::ContinuousSpace* space = heap->GetNonMovingSpace();
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700271 ASSERT_FALSE(space->IsImageSpace());
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700272 ASSERT_TRUE(space != nullptr);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700273 ASSERT_TRUE(space->IsMallocSpace());
Mathieu Chartier866d8742016-09-21 15:24:18 -0700274 image_file_sizes.push_back(file->GetLength());
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700275 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700276
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700277 ASSERT_TRUE(compiler_driver_->GetImageClasses() != nullptr);
Andreas Gampeb1fcead2015-04-20 18:53:51 -0700278 std::unordered_set<std::string> image_classes(*compiler_driver_->GetImageClasses());
Brian Carlstrom96391602013-06-13 19:49:50 -0700279
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700280 // Need to delete the compiler since it has worker threads which are attached to runtime.
Ian Rogers1212a022013-03-04 10:48:41 -0800281 compiler_driver_.reset();
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700282
Ian Rogers10c5b782013-01-10 10:40:53 -0800283 // Tear down old runtime before making a new one, clearing out misc state.
Mathieu Chartier6e88ef62014-10-14 15:01:24 -0700284
285 // Remove the reservation of the memory for use to load the image.
286 // Need to do this before we reset the runtime.
287 UnreserveImageSpace();
288 writer.reset(nullptr);
289
Ian Rogers10c5b782013-01-10 10:40:53 -0800290 runtime_.reset();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700291 java_lang_dex_file_ = nullptr;
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700292
Mathieu Chartier6e88ef62014-10-14 15:01:24 -0700293 MemMap::Init();
Brian Carlstrom8a436592011-08-15 21:27:23 -0700294
Ian Rogerse63db272014-07-15 15:36:11 -0700295 RuntimeOptions options;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700296 std::string image("-Ximage:");
Mathieu Chartier866d8742016-09-21 15:24:18 -0700297 image.append(image_locations[0].GetFilename());
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700298 options.push_back(std::make_pair(image.c_str(), static_cast<void*>(nullptr)));
Alex Light6e183f22014-07-18 14:57:04 -0700299 // By default the compiler this creates will not include patch information.
300 options.push_back(std::make_pair("-Xnorelocate", nullptr));
Brian Carlstrom8a436592011-08-15 21:27:23 -0700301
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700302 if (!Runtime::Create(options, false)) {
303 LOG(FATAL) << "Failed to create runtime";
304 return;
305 }
306 runtime_.reset(Runtime::Current());
307 // Runtime::Create acquired the mutator_lock_ that is normally given away when we Runtime::Start,
308 // give it away now and then switch to a more managable ScopedObjectAccess.
309 Thread::Current()->TransitionFromRunnableToSuspended(kNative);
310 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700311 ASSERT_TRUE(runtime_.get() != nullptr);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700312 class_linker_ = runtime_->GetClassLinker();
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700313
Ian Rogers1d54e732013-05-02 21:10:01 -0700314 gc::Heap* heap = Runtime::Current()->GetHeap();
Jeff Haodcdc85b2015-12-04 14:06:18 -0800315 ASSERT_TRUE(heap->HasBootImageSpace());
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700316 ASSERT_TRUE(heap->GetNonMovingSpace()->IsMallocSpace());
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700317
Jeff Haodcdc85b2015-12-04 14:06:18 -0800318 // We loaded the runtime with an explicit image, so it must exist.
Mathieu Chartier866d8742016-09-21 15:24:18 -0700319 ASSERT_EQ(heap->GetBootImageSpaces().size(), image_file_sizes.size());
320 for (size_t i = 0; i < image_file_sizes.size(); ++i) {
321 std::unique_ptr<const DexFile> dex(
322 LoadExpectSingleDexFile(GetLibCoreDexFileNames()[i].c_str()));
323 uint64_t image_file_size = image_file_sizes[i];
324 gc::space::ImageSpace* image_space = heap->GetBootImageSpaces()[i];
325 ASSERT_TRUE(image_space != nullptr);
326 if (storage_mode == ImageHeader::kStorageModeUncompressed) {
327 // Uncompressed, image should be smaller than file.
328 ASSERT_LE(image_space->GetImageHeader().GetImageSize(), image_file_size);
Brian Carlstrom96391602013-06-13 19:49:50 -0700329 } else {
Mathieu Chartier866d8742016-09-21 15:24:18 -0700330 // Compressed, file should be smaller than image.
331 ASSERT_LE(image_file_size, image_space->GetImageHeader().GetImageSize());
Brian Carlstrom96391602013-06-13 19:49:50 -0700332 }
Mathieu Chartier866d8742016-09-21 15:24:18 -0700333
334 image_space->VerifyImageAllocations();
335 uint8_t* image_begin = image_space->Begin();
336 uint8_t* image_end = image_space->End();
337 if (i == 0) {
338 // This check is only valid for image 0.
339 CHECK_EQ(requested_image_base, reinterpret_cast<uintptr_t>(image_begin));
340 }
341 for (size_t j = 0; j < dex->NumClassDefs(); ++j) {
342 const DexFile::ClassDef& class_def = dex->GetClassDef(j);
343 const char* descriptor = dex->GetClassDescriptor(class_def);
344 mirror::Class* klass = class_linker_->FindSystemClass(soa.Self(), descriptor);
345 EXPECT_TRUE(klass != nullptr) << descriptor;
346 if (image_classes.find(descriptor) == image_classes.end()) {
347 EXPECT_TRUE(reinterpret_cast<uint8_t*>(klass) >= image_end ||
348 reinterpret_cast<uint8_t*>(klass) < image_begin) << descriptor;
349 } else {
350 // Image classes should be located inside the image.
351 EXPECT_LT(image_begin, reinterpret_cast<uint8_t*>(klass)) << descriptor;
352 EXPECT_LT(reinterpret_cast<uint8_t*>(klass), image_end) << descriptor;
353 }
354 EXPECT_TRUE(Monitor::IsValidLockWord(klass->GetLockWord(false)));
355 }
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700356 }
Brian Carlstrom0e12bdc2014-05-14 17:44:28 -0700357
Mathieu Chartier866d8742016-09-21 15:24:18 -0700358 for (ScratchFile& image_file : image_files) {
359 image_file.Unlink();
360 }
361 for (ScratchFile& oat_file : oat_files) {
362 oat_file.Unlink();
363 }
364 for (ScratchFile& vdex_file : vdex_files) {
365 vdex_file.Unlink();
366 }
Brian Carlstrom0e12bdc2014-05-14 17:44:28 -0700367 int rmdir_result = rmdir(image_dir.c_str());
368 CHECK_EQ(0, rmdir_result);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700369}
370
Mathieu Chartierceb07b32015-12-10 09:33:21 -0800371TEST_F(ImageTest, WriteReadUncompressed) {
372 TestWriteRead(ImageHeader::kStorageModeUncompressed);
373}
374
375TEST_F(ImageTest, WriteReadLZ4) {
376 TestWriteRead(ImageHeader::kStorageModeLZ4);
377}
378
Mathieu Chartiera6e81ed2016-02-25 13:52:10 -0800379TEST_F(ImageTest, WriteReadLZ4HC) {
380 TestWriteRead(ImageHeader::kStorageModeLZ4HC);
381}
382
Brian Carlstrom179486a2013-09-03 11:51:42 -0700383TEST_F(ImageTest, ImageHeaderIsValid) {
384 uint32_t image_begin = ART_BASE_ADDRESS;
385 uint32_t image_size_ = 16 * KB;
Brian Carlstrom179486a2013-09-03 11:51:42 -0700386 uint32_t image_roots = ART_BASE_ADDRESS + (1 * KB);
387 uint32_t oat_checksum = 0;
388 uint32_t oat_file_begin = ART_BASE_ADDRESS + (4 * KB); // page aligned
389 uint32_t oat_data_begin = ART_BASE_ADDRESS + (8 * KB); // page aligned
390 uint32_t oat_data_end = ART_BASE_ADDRESS + (9 * KB);
391 uint32_t oat_file_end = ART_BASE_ADDRESS + (10 * KB);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700392 ImageSection sections[ImageHeader::kSectionCount];
Brian Carlstrom179486a2013-09-03 11:51:42 -0700393 ImageHeader image_header(image_begin,
394 image_size_,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700395 sections,
Brian Carlstrom179486a2013-09-03 11:51:42 -0700396 image_roots,
397 oat_checksum,
398 oat_file_begin,
399 oat_data_begin,
400 oat_data_end,
Igor Murashkin46774762014-10-22 11:37:02 -0700401 oat_file_end,
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800402 /*boot_image_begin*/0U,
403 /*boot_image_size*/0U,
404 /*boot_oat_begin*/0U,
405 /*boot_oat_size_*/0U,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700406 sizeof(void*),
Mathieu Chartierceb07b32015-12-10 09:33:21 -0800407 /*compile_pic*/false,
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800408 /*is_pic*/false,
Mathieu Chartierceb07b32015-12-10 09:33:21 -0800409 ImageHeader::kDefaultStorageMode,
410 /*data_size*/0u);
Brian Carlstrom179486a2013-09-03 11:51:42 -0700411 ASSERT_TRUE(image_header.IsValid());
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800412 ASSERT_TRUE(!image_header.IsAppImage());
Brian Carlstrom179486a2013-09-03 11:51:42 -0700413
414 char* magic = const_cast<char*>(image_header.GetMagic());
415 strcpy(magic, ""); // bad magic
416 ASSERT_FALSE(image_header.IsValid());
417 strcpy(magic, "art\n000"); // bad version
418 ASSERT_FALSE(image_header.IsValid());
419}
420
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700421} // namespace art