blob: 377eddcf74aaf0f48d01f968bd87f3c4d22cb24f [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
Andreas Gampe46ee31b2016-12-14 10:11:49 -080023#include "android-base/stringprintf.h"
24
Ian Rogerse63db272014-07-15 15:36:11 -070025#include "base/unix_file/fd_file.h"
Vladimir Marko3481ba22015-04-13 12:22:36 +010026#include "class_linker-inl.h"
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080027#include "common_compiler_test.h"
David Srbecky4fda4eb2016-02-05 13:34:46 +000028#include "debug/method_debug_info.h"
Andreas Gampe3f41a012016-02-18 16:53:41 -080029#include "driver/compiler_options.h"
Tong Shen62d1ca32014-09-03 17:24:56 -070030#include "elf_writer.h"
Vladimir Marko10c13562015-11-25 14:33:36 +000031#include "elf_writer_quick.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070032#include "gc/space/image_space.h"
Ian Rogerse63db272014-07-15 15:36:11 -070033#include "image_writer.h"
Nicolas Geoffrayf54e5df2016-12-01 10:45:08 +000034#include "linker/buffered_output_stream.h"
35#include "linker/file_output_stream.h"
Vladimir Marko944da602016-02-19 12:27:55 +000036#include "linker/multi_oat_relative_patcher.h"
Ian Rogersd9c4fc92013-10-01 19:45:43 -070037#include "lock_word.h"
38#include "mirror/object-inl.h"
Ian Rogerse63db272014-07-15 15:36:11 -070039#include "oat_writer.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070040#include "scoped_thread_state_change-inl.h"
Brian Carlstrom51c24672013-07-11 16:00:56 -070041#include "signal_catcher.h"
buzbeec143c552011-08-20 17:38:58 -070042#include "utils.h"
Brian Carlstromdb4d5402011-08-09 12:18:28 -070043
Brian Carlstromdb4d5402011-08-09 12:18:28 -070044namespace art {
45
Mathieu Chartier496577f2016-09-20 15:33:31 -070046static const uintptr_t kRequestedImageBase = ART_BASE_ADDRESS;
47
48struct CompilationHelper {
49 std::vector<std::string> dex_file_locations;
50 std::vector<ScratchFile> image_locations;
51 std::vector<std::unique_ptr<const DexFile>> extra_dex_files;
52 std::vector<ScratchFile> image_files;
53 std::vector<ScratchFile> oat_files;
54 std::vector<ScratchFile> vdex_files;
55 std::string image_dir;
56
57 void Compile(CompilerDriver* driver,
58 ImageHeader::StorageMode storage_mode);
59
60 std::vector<size_t> GetImageObjectSectionSizes();
61
62 ~CompilationHelper();
63};
64
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080065class ImageTest : public CommonCompilerTest {
Ian Rogers10c5b782013-01-10 10:40:53 -080066 protected:
67 virtual void SetUp() {
Brian Carlstrom700c8d32012-11-05 10:42:02 -080068 ReserveImageSpace();
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080069 CommonCompilerTest::SetUp();
Ian Rogers10c5b782013-01-10 10:40:53 -080070 }
Mathieu Chartier496577f2016-09-20 15:33:31 -070071
Mathieu Chartierceb07b32015-12-10 09:33:21 -080072 void TestWriteRead(ImageHeader::StorageMode storage_mode);
Mathieu Chartier496577f2016-09-20 15:33:31 -070073
74 void Compile(ImageHeader::StorageMode storage_mode,
75 CompilationHelper& out_helper,
76 const std::string& extra_dex = "",
77 const std::string& image_class = "");
78
79 std::unordered_set<std::string>* GetImageClasses() OVERRIDE {
80 return new std::unordered_set<std::string>(image_classes_);
81 }
82
83 private:
84 std::unordered_set<std::string> image_classes_;
Ian Rogers10c5b782013-01-10 10:40:53 -080085};
Brian Carlstromdb4d5402011-08-09 12:18:28 -070086
Mathieu Chartier496577f2016-09-20 15:33:31 -070087CompilationHelper::~CompilationHelper() {
88 for (ScratchFile& image_file : image_files) {
89 image_file.Unlink();
90 }
91 for (ScratchFile& oat_file : oat_files) {
92 oat_file.Unlink();
93 }
94 for (ScratchFile& vdex_file : vdex_files) {
95 vdex_file.Unlink();
96 }
97 const int rmdir_result = rmdir(image_dir.c_str());
98 CHECK_EQ(0, rmdir_result);
99}
Andreas Gampe3f41a012016-02-18 16:53:41 -0800100
Mathieu Chartier496577f2016-09-20 15:33:31 -0700101std::vector<size_t> CompilationHelper::GetImageObjectSectionSizes() {
102 std::vector<size_t> ret;
103 for (ScratchFile& image_file : image_files) {
104 std::unique_ptr<File> file(OS::OpenFileForReading(image_file.GetFilename().c_str()));
105 CHECK(file.get() != nullptr);
106 ImageHeader image_header;
107 CHECK_EQ(file->ReadFully(&image_header, sizeof(image_header)), true);
108 CHECK(image_header.IsValid());
109 ret.push_back(image_header.GetImageSize());
110 }
111 return ret;
112}
Andreas Gampe3f41a012016-02-18 16:53:41 -0800113
Mathieu Chartier496577f2016-09-20 15:33:31 -0700114void CompilationHelper::Compile(CompilerDriver* driver,
115 ImageHeader::StorageMode storage_mode) {
Mathieu Chartierceb07b32015-12-10 09:33:21 -0800116 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartier496577f2016-09-20 15:33:31 -0700117 std::vector<const DexFile*> class_path = class_linker->GetBootClassPath();
118
119 for (const std::unique_ptr<const DexFile>& dex_file : extra_dex_files) {
120 {
121 ScopedObjectAccess soa(Thread::Current());
122 // Inject in boot class path so that the compiler driver can see it.
123 class_linker->AppendToBootClassPath(soa.Self(), *dex_file.get());
124 }
125 class_path.push_back(dex_file.get());
126 }
Mathieu Chartier866d8742016-09-21 15:24:18 -0700127
Mathieu Chartierceb07b32015-12-10 09:33:21 -0800128 // Enable write for dex2dex.
Mathieu Chartier496577f2016-09-20 15:33:31 -0700129 for (const DexFile* dex_file : class_path) {
130 dex_file_locations.push_back(dex_file->GetLocation());
131 if (dex_file->IsReadOnly()) {
132 dex_file->EnableWrite();
133 }
Mathieu Chartierceb07b32015-12-10 09:33:21 -0800134 }
Mathieu Chartier866d8742016-09-21 15:24:18 -0700135 {
Mathieu Chartier496577f2016-09-20 15:33:31 -0700136 // Create a generic tmp file, to be the base of the .art and .oat temporary files.
Mathieu Chartier866d8742016-09-21 15:24:18 -0700137 ScratchFile location;
Mathieu Chartier496577f2016-09-20 15:33:31 -0700138 for (int i = 0; i < static_cast<int>(class_path.size()); ++i) {
Andreas Gampe46ee31b2016-12-14 10:11:49 -0800139 std::string cur_location =
140 android::base::StringPrintf("%s-%d.art", location.GetFilename().c_str(), i);
Mathieu Chartier866d8742016-09-21 15:24:18 -0700141 image_locations.push_back(ScratchFile(cur_location));
142 }
143 }
144 std::vector<std::string> image_filenames;
Mathieu Chartier866d8742016-09-21 15:24:18 -0700145 for (ScratchFile& file : image_locations) {
146 std::string image_filename(GetSystemImageFilename(file.GetFilename().c_str(), kRuntimeISA));
147 image_filenames.push_back(image_filename);
148 size_t pos = image_filename.rfind('/');
149 CHECK_NE(pos, std::string::npos) << image_filename;
150 if (image_dir.empty()) {
151 image_dir = image_filename.substr(0, pos);
152 int mkdir_result = mkdir(image_dir.c_str(), 0700);
153 CHECK_EQ(0, mkdir_result) << image_dir;
154 }
155 image_files.push_back(ScratchFile(OS::CreateEmptyFile(image_filename.c_str())));
156 }
Brian Carlstrom0e12bdc2014-05-14 17:44:28 -0700157
Mathieu Chartier866d8742016-09-21 15:24:18 -0700158 std::vector<std::string> oat_filenames;
Mathieu Chartier866d8742016-09-21 15:24:18 -0700159 std::vector<std::string> vdex_filenames;
Mathieu Chartier866d8742016-09-21 15:24:18 -0700160 for (const std::string& image_filename : image_filenames) {
161 std::string oat_filename = ReplaceFileExtension(image_filename, "oat");
162 oat_files.push_back(ScratchFile(OS::CreateEmptyFile(oat_filename.c_str())));
163 oat_filenames.push_back(oat_filename);
164 std::string vdex_filename = ReplaceFileExtension(image_filename, "vdex");
165 vdex_files.push_back(ScratchFile(OS::CreateEmptyFile(vdex_filename.c_str())));
166 vdex_filenames.push_back(vdex_filename);
167 }
David Brazdil7b49e6c2016-09-01 11:06:18 +0100168
Vladimir Marko944da602016-02-19 12:27:55 +0000169 std::unordered_map<const DexFile*, size_t> dex_file_to_oat_index_map;
Mathieu Chartier866d8742016-09-21 15:24:18 -0700170 std::vector<const char*> oat_filename_vector;
171 for (const std::string& file : oat_filenames) {
172 oat_filename_vector.push_back(file.c_str());
Jeff Haodcdc85b2015-12-04 14:06:18 -0800173 }
Mathieu Chartier866d8742016-09-21 15:24:18 -0700174 std::vector<const char*> image_filename_vector;
175 for (const std::string& file : image_filenames) {
176 image_filename_vector.push_back(file.c_str());
177 }
178 size_t image_idx = 0;
Mathieu Chartier496577f2016-09-20 15:33:31 -0700179 for (const DexFile* dex_file : class_path) {
Mathieu Chartier866d8742016-09-21 15:24:18 -0700180 dex_file_to_oat_index_map.emplace(dex_file, image_idx);
181 ++image_idx;
182 }
183 // TODO: compile_pic should be a test argument.
Mathieu Chartier496577f2016-09-20 15:33:31 -0700184 std::unique_ptr<ImageWriter> writer(new ImageWriter(*driver,
185 kRequestedImageBase,
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800186 /*compile_pic*/false,
Mathieu Chartierceb07b32015-12-10 09:33:21 -0800187 /*compile_app_image*/false,
Jeff Haodcdc85b2015-12-04 14:06:18 -0800188 storage_mode,
189 oat_filename_vector,
Vladimir Marko944da602016-02-19 12:27:55 +0000190 dex_file_to_oat_index_map));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700191 {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800192 {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700193 jobject class_loader = nullptr;
Ian Rogers5fe9af72013-11-14 00:17:20 -0800194 TimingLogger timings("ImageTest::WriteRead", false, false);
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700195 TimingLogger::ScopedTiming t("CompileAll", &timings);
Mathieu Chartier496577f2016-09-20 15:33:31 -0700196 driver->SetDexFilesForOatFile(class_path);
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +0000197 driver->CompileAll(class_loader, class_path, /* verifier_deps */ nullptr, &timings);
Brian Carlstrom96391602013-06-13 19:49:50 -0700198
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700199 t.NewTiming("WriteElf");
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700200 SafeMap<std::string, std::string> key_value_store;
Mathieu Chartier866d8742016-09-21 15:24:18 -0700201 std::vector<const char*> dex_filename_vector;
Mathieu Chartier496577f2016-09-20 15:33:31 -0700202 for (size_t i = 0; i < class_path.size(); ++i) {
Mathieu Chartier866d8742016-09-21 15:24:18 -0700203 dex_filename_vector.push_back("");
204 }
205 key_value_store.Put(OatHeader::kBootClassPathKey,
206 gc::space::ImageSpace::GetMultiImageBootClassPath(
207 dex_filename_vector,
208 oat_filename_vector,
209 image_filename_vector));
210
Mathieu Chartier866d8742016-09-21 15:24:18 -0700211 std::vector<std::unique_ptr<ElfWriter>> elf_writers;
212 std::vector<std::unique_ptr<OatWriter>> oat_writers;
213 for (ScratchFile& oat_file : oat_files) {
Mathieu Chartier496577f2016-09-20 15:33:31 -0700214 elf_writers.emplace_back(CreateElfWriterQuick(driver->GetInstructionSet(),
215 driver->GetInstructionSetFeatures(),
216 &driver->GetCompilerOptions(),
217 oat_file.GetFile()));
Mathieu Chartier866d8742016-09-21 15:24:18 -0700218 elf_writers.back()->Start();
Jeff Hao608f2ce2016-10-19 11:17:11 -0700219 oat_writers.emplace_back(new OatWriter(/*compiling_boot_image*/true,
220 &timings,
221 /*profile_compilation_info*/nullptr));
Mathieu Chartier866d8742016-09-21 15:24:18 -0700222 }
223
224 std::vector<OutputStream*> rodata;
225 std::vector<std::unique_ptr<MemMap>> opened_dex_files_map;
226 std::vector<std::unique_ptr<const DexFile>> opened_dex_files;
227 // Now that we have finalized key_value_store_, start writing the oat file.
228 for (size_t i = 0, size = oat_writers.size(); i != size; ++i) {
Mathieu Chartier496577f2016-09-20 15:33:31 -0700229 const DexFile* dex_file = class_path[i];
Mathieu Chartier866d8742016-09-21 15:24:18 -0700230 rodata.push_back(elf_writers[i]->StartRoData());
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000231 ArrayRef<const uint8_t> raw_dex_file(
232 reinterpret_cast<const uint8_t*>(&dex_file->GetHeader()),
233 dex_file->GetHeader().file_size_);
Mathieu Chartier866d8742016-09-21 15:24:18 -0700234 oat_writers[i]->AddRawDexFileSource(raw_dex_file,
235 dex_file->GetLocation().c_str(),
236 dex_file->GetLocationChecksum());
237
238 std::unique_ptr<MemMap> cur_opened_dex_files_map;
239 std::vector<std::unique_ptr<const DexFile>> cur_opened_dex_files;
240 bool dex_files_ok = oat_writers[i]->WriteAndOpenDexFiles(
241 kIsVdexEnabled ? vdex_files[i].GetFile() : oat_files[i].GetFile(),
242 rodata.back(),
Mathieu Chartier496577f2016-09-20 15:33:31 -0700243 driver->GetInstructionSet(),
244 driver->GetInstructionSetFeatures(),
David Brazdil7b49e6c2016-09-01 11:06:18 +0100245 &key_value_store,
246 /* verify */ false, // Dex files may be dex-to-dex-ed, don't verify.
Nicolas Geoffray81f57d12016-12-20 13:17:09 +0000247 /* update_input_vdex */ false,
Mathieu Chartier866d8742016-09-21 15:24:18 -0700248 &cur_opened_dex_files_map,
249 &cur_opened_dex_files);
David Brazdil7b49e6c2016-09-01 11:06:18 +0100250 ASSERT_TRUE(dex_files_ok);
Vladimir Marko944da602016-02-19 12:27:55 +0000251
Mathieu Chartier866d8742016-09-21 15:24:18 -0700252 if (cur_opened_dex_files_map != nullptr) {
253 opened_dex_files_map.push_back(std::move(cur_opened_dex_files_map));
254 for (std::unique_ptr<const DexFile>& cur_dex_file : cur_opened_dex_files) {
255 // dex_file_oat_index_map_.emplace(dex_file.get(), i);
256 opened_dex_files.push_back(std::move(cur_dex_file));
257 }
258 } else {
259 ASSERT_TRUE(cur_opened_dex_files.empty());
260 }
261 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000262 bool image_space_ok = writer->PrepareImageAddressSpace();
263 ASSERT_TRUE(image_space_ok);
264
Nicolas Geoffrayf54e5df2016-12-01 10:45:08 +0000265 if (kIsVdexEnabled) {
266 for (size_t i = 0, size = vdex_files.size(); i != size; ++i) {
267 std::unique_ptr<BufferedOutputStream> vdex_out(
268 MakeUnique<BufferedOutputStream>(
269 MakeUnique<FileOutputStream>(vdex_files[i].GetFile())));
270 oat_writers[i]->WriteVerifierDeps(vdex_out.get(), nullptr);
271 oat_writers[i]->WriteChecksumsAndVdexHeader(vdex_out.get());
272 }
273 }
274
Mathieu Chartier866d8742016-09-21 15:24:18 -0700275 for (size_t i = 0, size = oat_files.size(); i != size; ++i) {
Mathieu Chartier496577f2016-09-20 15:33:31 -0700276 linker::MultiOatRelativePatcher patcher(driver->GetInstructionSet(),
277 driver->GetInstructionSetFeatures());
Mathieu Chartier866d8742016-09-21 15:24:18 -0700278 OatWriter* const oat_writer = oat_writers[i].get();
279 ElfWriter* const elf_writer = elf_writers[i].get();
Mathieu Chartier496577f2016-09-20 15:33:31 -0700280 std::vector<const DexFile*> cur_dex_files(1u, class_path[i]);
Nicolas Geoffray4acefd32016-10-24 13:14:58 +0100281 oat_writer->Initialize(driver, writer.get(), cur_dex_files);
282 oat_writer->PrepareLayout(&patcher);
Mathieu Chartier866d8742016-09-21 15:24:18 -0700283 size_t rodata_size = oat_writer->GetOatHeader().GetExecutableOffset();
284 size_t text_size = oat_writer->GetOatSize() - rodata_size;
Vladimir Markoaad75c62016-10-03 08:46:48 +0000285 elf_writer->PrepareDynamicSection(rodata_size,
286 text_size,
287 oat_writer->GetBssSize(),
288 oat_writer->GetBssRootsOffset());
Vladimir Marko944da602016-02-19 12:27:55 +0000289
Mathieu Chartier866d8742016-09-21 15:24:18 -0700290 writer->UpdateOatFileLayout(i,
291 elf_writer->GetLoadedSize(),
292 oat_writer->GetOatDataOffset(),
293 oat_writer->GetOatSize());
Vladimir Marko944da602016-02-19 12:27:55 +0000294
Mathieu Chartier866d8742016-09-21 15:24:18 -0700295 bool rodata_ok = oat_writer->WriteRodata(rodata[i]);
296 ASSERT_TRUE(rodata_ok);
297 elf_writer->EndRoData(rodata[i]);
Vladimir Marko10c13562015-11-25 14:33:36 +0000298
Mathieu Chartier866d8742016-09-21 15:24:18 -0700299 OutputStream* text = elf_writer->StartText();
300 bool text_ok = oat_writer->WriteCode(text);
301 ASSERT_TRUE(text_ok);
302 elf_writer->EndText(text);
Vladimir Marko10c13562015-11-25 14:33:36 +0000303
Mathieu Chartier866d8742016-09-21 15:24:18 -0700304 bool header_ok = oat_writer->WriteHeader(elf_writer->GetStream(), 0u, 0u, 0u);
305 ASSERT_TRUE(header_ok);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000306
Mathieu Chartier866d8742016-09-21 15:24:18 -0700307 writer->UpdateOatFileHeader(i, oat_writer->GetOatHeader());
Vladimir Marko944da602016-02-19 12:27:55 +0000308
Mathieu Chartier866d8742016-09-21 15:24:18 -0700309 elf_writer->WriteDynamicSection();
310 elf_writer->WriteDebugInfo(oat_writer->GetMethodDebugInfo());
311 elf_writer->WritePatchLocations(oat_writer->GetAbsolutePatchLocations());
Vladimir Marko10c13562015-11-25 14:33:36 +0000312
Mathieu Chartier866d8742016-09-21 15:24:18 -0700313 bool success = elf_writer->End();
314 ASSERT_TRUE(success);
315 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700316 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700317
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800318 bool success_image = writer->Write(kInvalidFd,
Mathieu Chartier866d8742016-09-21 15:24:18 -0700319 image_filename_vector,
320 oat_filename_vector);
jeffhao8161c032012-10-31 15:50:00 -0700321 ASSERT_TRUE(success_image);
Andreas Gampe4303ba92014-11-06 01:00:46 -0800322
Mathieu Chartier866d8742016-09-21 15:24:18 -0700323 for (size_t i = 0, size = oat_filenames.size(); i != size; ++i) {
324 const char* oat_filename = oat_filenames[i].c_str();
325 std::unique_ptr<File> oat_file(OS::OpenFileReadWrite(oat_filename));
326 ASSERT_TRUE(oat_file != nullptr);
327 bool success_fixup = ElfWriter::Fixup(oat_file.get(),
328 writer->GetOatDataBegin(i));
329 ASSERT_TRUE(success_fixup);
330 ASSERT_EQ(oat_file->FlushCloseOrErase(), 0) << "Could not flush and close oat file "
331 << oat_filename;
332 }
jeffhao8161c032012-10-31 15:50:00 -0700333 }
Mathieu Chartier496577f2016-09-20 15:33:31 -0700334}
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700335
Mathieu Chartier496577f2016-09-20 15:33:31 -0700336void ImageTest::Compile(ImageHeader::StorageMode storage_mode,
337 CompilationHelper& helper,
338 const std::string& extra_dex,
339 const std::string& image_class) {
340 if (!image_class.empty()) {
341 image_classes_.insert(image_class);
342 }
343 CreateCompilerDriver(Compiler::kOptimizing, kRuntimeISA, kIsTargetBuild ? 2U : 16U);
344 // Set inline filter values.
345 compiler_options_->SetInlineDepthLimit(CompilerOptions::kDefaultInlineDepthLimit);
346 compiler_options_->SetInlineMaxCodeUnits(CompilerOptions::kDefaultInlineMaxCodeUnits);
347 image_classes_.clear();
348 if (!extra_dex.empty()) {
349 helper.extra_dex_files = OpenTestDexFiles(extra_dex.c_str());
350 }
351 helper.Compile(compiler_driver_.get(), storage_mode);
352 if (!image_class.empty()) {
353 // Make sure the class got initialized.
354 ScopedObjectAccess soa(Thread::Current());
355 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
356 mirror::Class* klass = class_linker->FindSystemClass(Thread::Current(), image_class.c_str());
357 EXPECT_TRUE(klass != nullptr);
358 EXPECT_TRUE(klass->IsInitialized());
359 }
360}
361
362void ImageTest::TestWriteRead(ImageHeader::StorageMode storage_mode) {
363 CompilationHelper helper;
364 Compile(storage_mode, /*out*/ helper);
Mathieu Chartier866d8742016-09-21 15:24:18 -0700365 std::vector<uint64_t> image_file_sizes;
Mathieu Chartier496577f2016-09-20 15:33:31 -0700366 for (ScratchFile& image_file : helper.image_files) {
Ian Rogers700a4022014-05-19 16:49:03 -0700367 std::unique_ptr<File> file(OS::OpenFileForReading(image_file.GetFilename().c_str()));
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700368 ASSERT_TRUE(file.get() != nullptr);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700369 ImageHeader image_header;
Andreas Gampe4303ba92014-11-06 01:00:46 -0800370 ASSERT_EQ(file->ReadFully(&image_header, sizeof(image_header)), true);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700371 ASSERT_TRUE(image_header.IsValid());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700372 const auto& bitmap_section = image_header.GetImageSection(ImageHeader::kSectionImageBitmap);
373 ASSERT_GE(bitmap_section.Offset(), sizeof(image_header));
374 ASSERT_NE(0U, bitmap_section.Size());
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700375
Ian Rogers1d54e732013-05-02 21:10:01 -0700376 gc::Heap* heap = Runtime::Current()->GetHeap();
Mathieu Chartiera9d82fe2016-01-25 20:06:11 -0800377 ASSERT_TRUE(heap->HaveContinuousSpaces());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700378 gc::space::ContinuousSpace* space = heap->GetNonMovingSpace();
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700379 ASSERT_FALSE(space->IsImageSpace());
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700380 ASSERT_TRUE(space != nullptr);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700381 ASSERT_TRUE(space->IsMallocSpace());
Mathieu Chartier866d8742016-09-21 15:24:18 -0700382 image_file_sizes.push_back(file->GetLength());
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700383 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700384
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700385 ASSERT_TRUE(compiler_driver_->GetImageClasses() != nullptr);
Andreas Gampeb1fcead2015-04-20 18:53:51 -0700386 std::unordered_set<std::string> image_classes(*compiler_driver_->GetImageClasses());
Brian Carlstrom96391602013-06-13 19:49:50 -0700387
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700388 // Need to delete the compiler since it has worker threads which are attached to runtime.
Ian Rogers1212a022013-03-04 10:48:41 -0800389 compiler_driver_.reset();
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700390
Ian Rogers10c5b782013-01-10 10:40:53 -0800391 // Tear down old runtime before making a new one, clearing out misc state.
Mathieu Chartier6e88ef62014-10-14 15:01:24 -0700392
393 // Remove the reservation of the memory for use to load the image.
394 // Need to do this before we reset the runtime.
395 UnreserveImageSpace();
Mathieu Chartier6e88ef62014-10-14 15:01:24 -0700396
Mathieu Chartier496577f2016-09-20 15:33:31 -0700397 helper.extra_dex_files.clear();
Ian Rogers10c5b782013-01-10 10:40:53 -0800398 runtime_.reset();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700399 java_lang_dex_file_ = nullptr;
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700400
Mathieu Chartier6e88ef62014-10-14 15:01:24 -0700401 MemMap::Init();
Brian Carlstrom8a436592011-08-15 21:27:23 -0700402
Ian Rogerse63db272014-07-15 15:36:11 -0700403 RuntimeOptions options;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700404 std::string image("-Ximage:");
Mathieu Chartier496577f2016-09-20 15:33:31 -0700405 image.append(helper.image_locations[0].GetFilename());
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700406 options.push_back(std::make_pair(image.c_str(), static_cast<void*>(nullptr)));
Alex Light6e183f22014-07-18 14:57:04 -0700407 // By default the compiler this creates will not include patch information.
408 options.push_back(std::make_pair("-Xnorelocate", nullptr));
Brian Carlstrom8a436592011-08-15 21:27:23 -0700409
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700410 if (!Runtime::Create(options, false)) {
411 LOG(FATAL) << "Failed to create runtime";
412 return;
413 }
414 runtime_.reset(Runtime::Current());
415 // Runtime::Create acquired the mutator_lock_ that is normally given away when we Runtime::Start,
416 // give it away now and then switch to a more managable ScopedObjectAccess.
417 Thread::Current()->TransitionFromRunnableToSuspended(kNative);
418 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700419 ASSERT_TRUE(runtime_.get() != nullptr);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700420 class_linker_ = runtime_->GetClassLinker();
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700421
Ian Rogers1d54e732013-05-02 21:10:01 -0700422 gc::Heap* heap = Runtime::Current()->GetHeap();
Jeff Haodcdc85b2015-12-04 14:06:18 -0800423 ASSERT_TRUE(heap->HasBootImageSpace());
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700424 ASSERT_TRUE(heap->GetNonMovingSpace()->IsMallocSpace());
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700425
Jeff Haodcdc85b2015-12-04 14:06:18 -0800426 // We loaded the runtime with an explicit image, so it must exist.
Mathieu Chartier866d8742016-09-21 15:24:18 -0700427 ASSERT_EQ(heap->GetBootImageSpaces().size(), image_file_sizes.size());
Mathieu Chartier496577f2016-09-20 15:33:31 -0700428 for (size_t i = 0; i < helper.dex_file_locations.size(); ++i) {
Mathieu Chartier866d8742016-09-21 15:24:18 -0700429 std::unique_ptr<const DexFile> dex(
Mathieu Chartier496577f2016-09-20 15:33:31 -0700430 LoadExpectSingleDexFile(helper.dex_file_locations[i].c_str()));
431 ASSERT_TRUE(dex != nullptr);
Mathieu Chartier866d8742016-09-21 15:24:18 -0700432 uint64_t image_file_size = image_file_sizes[i];
433 gc::space::ImageSpace* image_space = heap->GetBootImageSpaces()[i];
434 ASSERT_TRUE(image_space != nullptr);
435 if (storage_mode == ImageHeader::kStorageModeUncompressed) {
436 // Uncompressed, image should be smaller than file.
437 ASSERT_LE(image_space->GetImageHeader().GetImageSize(), image_file_size);
Mathieu Chartier496577f2016-09-20 15:33:31 -0700438 } else if (image_file_size > 16 * KB) {
439 // Compressed, file should be smaller than image. Not really valid for small images.
Mathieu Chartier866d8742016-09-21 15:24:18 -0700440 ASSERT_LE(image_file_size, image_space->GetImageHeader().GetImageSize());
Brian Carlstrom96391602013-06-13 19:49:50 -0700441 }
Mathieu Chartier866d8742016-09-21 15:24:18 -0700442
443 image_space->VerifyImageAllocations();
444 uint8_t* image_begin = image_space->Begin();
445 uint8_t* image_end = image_space->End();
446 if (i == 0) {
447 // This check is only valid for image 0.
Mathieu Chartier496577f2016-09-20 15:33:31 -0700448 CHECK_EQ(kRequestedImageBase, reinterpret_cast<uintptr_t>(image_begin));
Mathieu Chartier866d8742016-09-21 15:24:18 -0700449 }
450 for (size_t j = 0; j < dex->NumClassDefs(); ++j) {
451 const DexFile::ClassDef& class_def = dex->GetClassDef(j);
452 const char* descriptor = dex->GetClassDescriptor(class_def);
453 mirror::Class* klass = class_linker_->FindSystemClass(soa.Self(), descriptor);
454 EXPECT_TRUE(klass != nullptr) << descriptor;
455 if (image_classes.find(descriptor) == image_classes.end()) {
456 EXPECT_TRUE(reinterpret_cast<uint8_t*>(klass) >= image_end ||
457 reinterpret_cast<uint8_t*>(klass) < image_begin) << descriptor;
458 } else {
459 // Image classes should be located inside the image.
460 EXPECT_LT(image_begin, reinterpret_cast<uint8_t*>(klass)) << descriptor;
461 EXPECT_LT(reinterpret_cast<uint8_t*>(klass), image_end) << descriptor;
462 }
463 EXPECT_TRUE(Monitor::IsValidLockWord(klass->GetLockWord(false)));
464 }
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700465 }
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700466}
467
Mathieu Chartierceb07b32015-12-10 09:33:21 -0800468TEST_F(ImageTest, WriteReadUncompressed) {
469 TestWriteRead(ImageHeader::kStorageModeUncompressed);
470}
471
472TEST_F(ImageTest, WriteReadLZ4) {
473 TestWriteRead(ImageHeader::kStorageModeLZ4);
474}
475
Mathieu Chartiera6e81ed2016-02-25 13:52:10 -0800476TEST_F(ImageTest, WriteReadLZ4HC) {
477 TestWriteRead(ImageHeader::kStorageModeLZ4HC);
478}
479
Mathieu Chartier496577f2016-09-20 15:33:31 -0700480TEST_F(ImageTest, TestImageLayout) {
481 std::vector<size_t> image_sizes;
482 std::vector<size_t> image_sizes_extra;
483 // Compile multi-image with ImageLayoutA being the last image.
484 {
485 CompilationHelper helper;
486 Compile(ImageHeader::kStorageModeUncompressed, helper, "ImageLayoutA", "LMyClass;");
487 image_sizes = helper.GetImageObjectSectionSizes();
488 }
489 TearDown();
490 runtime_.reset();
491 SetUp();
492 // Compile multi-image with ImageLayoutB being the last image.
493 {
494 CompilationHelper helper;
495 Compile(ImageHeader::kStorageModeUncompressed, helper, "ImageLayoutB", "LMyClass;");
496 image_sizes_extra = helper.GetImageObjectSectionSizes();
497 }
498 // Make sure that the new stuff in the clinit in ImageLayoutB is in the last image and not in the
499 // first two images.
500 ASSERT_EQ(image_sizes.size(), image_sizes.size());
501 // Sizes of the images should be the same. These sizes are for the whole image unrounded.
502 for (size_t i = 0; i < image_sizes.size() - 1; ++i) {
503 EXPECT_EQ(image_sizes[i], image_sizes_extra[i]);
504 }
505 // Last image should be larger since it has a hash map and a string.
506 EXPECT_LT(image_sizes.back(), image_sizes_extra.back());
507}
508
Brian Carlstrom179486a2013-09-03 11:51:42 -0700509TEST_F(ImageTest, ImageHeaderIsValid) {
510 uint32_t image_begin = ART_BASE_ADDRESS;
511 uint32_t image_size_ = 16 * KB;
Brian Carlstrom179486a2013-09-03 11:51:42 -0700512 uint32_t image_roots = ART_BASE_ADDRESS + (1 * KB);
513 uint32_t oat_checksum = 0;
514 uint32_t oat_file_begin = ART_BASE_ADDRESS + (4 * KB); // page aligned
515 uint32_t oat_data_begin = ART_BASE_ADDRESS + (8 * KB); // page aligned
516 uint32_t oat_data_end = ART_BASE_ADDRESS + (9 * KB);
517 uint32_t oat_file_end = ART_BASE_ADDRESS + (10 * KB);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700518 ImageSection sections[ImageHeader::kSectionCount];
Brian Carlstrom179486a2013-09-03 11:51:42 -0700519 ImageHeader image_header(image_begin,
520 image_size_,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700521 sections,
Brian Carlstrom179486a2013-09-03 11:51:42 -0700522 image_roots,
523 oat_checksum,
524 oat_file_begin,
525 oat_data_begin,
526 oat_data_end,
Igor Murashkin46774762014-10-22 11:37:02 -0700527 oat_file_end,
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800528 /*boot_image_begin*/0U,
529 /*boot_image_size*/0U,
530 /*boot_oat_begin*/0U,
531 /*boot_oat_size_*/0U,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700532 sizeof(void*),
Mathieu Chartierceb07b32015-12-10 09:33:21 -0800533 /*compile_pic*/false,
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800534 /*is_pic*/false,
Mathieu Chartierceb07b32015-12-10 09:33:21 -0800535 ImageHeader::kDefaultStorageMode,
536 /*data_size*/0u);
Brian Carlstrom179486a2013-09-03 11:51:42 -0700537 ASSERT_TRUE(image_header.IsValid());
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800538 ASSERT_TRUE(!image_header.IsAppImage());
Brian Carlstrom179486a2013-09-03 11:51:42 -0700539
540 char* magic = const_cast<char*>(image_header.GetMagic());
541 strcpy(magic, ""); // bad magic
542 ASSERT_FALSE(image_header.IsValid());
543 strcpy(magic, "art\n000"); // bad version
544 ASSERT_FALSE(image_header.IsValid());
545}
546
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700547} // namespace art