Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 1 | /* |
| 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 Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 16 | |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 17 | #include "image.h" |
| 18 | |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 19 | #include <memory> |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 20 | #include <string> |
| 21 | #include <vector> |
| 22 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 23 | #include "android-base/stringprintf.h" |
| 24 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 25 | #include "base/unix_file/fd_file.h" |
Vladimir Marko | 3481ba2 | 2015-04-13 12:22:36 +0100 | [diff] [blame] | 26 | #include "class_linker-inl.h" |
Calin Juravle | 857f058 | 2016-12-20 14:36:59 +0000 | [diff] [blame] | 27 | #include "compiler_callbacks.h" |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 28 | #include "common_compiler_test.h" |
David Srbecky | 4fda4eb | 2016-02-05 13:34:46 +0000 | [diff] [blame] | 29 | #include "debug/method_debug_info.h" |
Calin Juravle | 857f058 | 2016-12-20 14:36:59 +0000 | [diff] [blame] | 30 | #include "dex/quick_compiler_callbacks.h" |
Andreas Gampe | 3f41a01 | 2016-02-18 16:53:41 -0800 | [diff] [blame] | 31 | #include "driver/compiler_options.h" |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 32 | #include "elf_writer.h" |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 33 | #include "elf_writer_quick.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 34 | #include "gc/space/image_space.h" |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 35 | #include "image_writer.h" |
Nicolas Geoffray | f54e5df | 2016-12-01 10:45:08 +0000 | [diff] [blame] | 36 | #include "linker/buffered_output_stream.h" |
| 37 | #include "linker/file_output_stream.h" |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 38 | #include "linker/multi_oat_relative_patcher.h" |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 39 | #include "lock_word.h" |
| 40 | #include "mirror/object-inl.h" |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 41 | #include "oat_writer.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 42 | #include "scoped_thread_state_change-inl.h" |
Brian Carlstrom | 51c2467 | 2013-07-11 16:00:56 -0700 | [diff] [blame] | 43 | #include "signal_catcher.h" |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 44 | #include "utils.h" |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 45 | |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 46 | namespace art { |
| 47 | |
Mathieu Chartier | 496577f | 2016-09-20 15:33:31 -0700 | [diff] [blame] | 48 | static const uintptr_t kRequestedImageBase = ART_BASE_ADDRESS; |
| 49 | |
| 50 | struct CompilationHelper { |
| 51 | std::vector<std::string> dex_file_locations; |
| 52 | std::vector<ScratchFile> image_locations; |
| 53 | std::vector<std::unique_ptr<const DexFile>> extra_dex_files; |
| 54 | std::vector<ScratchFile> image_files; |
| 55 | std::vector<ScratchFile> oat_files; |
| 56 | std::vector<ScratchFile> vdex_files; |
| 57 | std::string image_dir; |
| 58 | |
| 59 | void Compile(CompilerDriver* driver, |
| 60 | ImageHeader::StorageMode storage_mode); |
| 61 | |
| 62 | std::vector<size_t> GetImageObjectSectionSizes(); |
| 63 | |
| 64 | ~CompilationHelper(); |
| 65 | }; |
| 66 | |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 67 | class ImageTest : public CommonCompilerTest { |
Ian Rogers | 10c5b78 | 2013-01-10 10:40:53 -0800 | [diff] [blame] | 68 | protected: |
| 69 | virtual void SetUp() { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 70 | ReserveImageSpace(); |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 71 | CommonCompilerTest::SetUp(); |
Ian Rogers | 10c5b78 | 2013-01-10 10:40:53 -0800 | [diff] [blame] | 72 | } |
Mathieu Chartier | 496577f | 2016-09-20 15:33:31 -0700 | [diff] [blame] | 73 | |
Mathieu Chartier | ceb07b3 | 2015-12-10 09:33:21 -0800 | [diff] [blame] | 74 | void TestWriteRead(ImageHeader::StorageMode storage_mode); |
Mathieu Chartier | 496577f | 2016-09-20 15:33:31 -0700 | [diff] [blame] | 75 | |
| 76 | void Compile(ImageHeader::StorageMode storage_mode, |
| 77 | CompilationHelper& out_helper, |
| 78 | const std::string& extra_dex = "", |
| 79 | const std::string& image_class = ""); |
| 80 | |
Calin Juravle | 857f058 | 2016-12-20 14:36:59 +0000 | [diff] [blame] | 81 | void SetUpRuntimeOptions(RuntimeOptions* options) OVERRIDE { |
| 82 | CommonCompilerTest::SetUpRuntimeOptions(options); |
| 83 | callbacks_.reset(new QuickCompilerCallbacks( |
| 84 | verification_results_.get(), |
| 85 | CompilerCallbacks::CallbackMode::kCompileBootImage)); |
| 86 | options->push_back(std::make_pair("compilercallbacks", callbacks_.get())); |
| 87 | } |
| 88 | |
Mathieu Chartier | 496577f | 2016-09-20 15:33:31 -0700 | [diff] [blame] | 89 | std::unordered_set<std::string>* GetImageClasses() OVERRIDE { |
| 90 | return new std::unordered_set<std::string>(image_classes_); |
| 91 | } |
| 92 | |
| 93 | private: |
| 94 | std::unordered_set<std::string> image_classes_; |
Ian Rogers | 10c5b78 | 2013-01-10 10:40:53 -0800 | [diff] [blame] | 95 | }; |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 96 | |
Mathieu Chartier | 496577f | 2016-09-20 15:33:31 -0700 | [diff] [blame] | 97 | CompilationHelper::~CompilationHelper() { |
| 98 | for (ScratchFile& image_file : image_files) { |
| 99 | image_file.Unlink(); |
| 100 | } |
| 101 | for (ScratchFile& oat_file : oat_files) { |
| 102 | oat_file.Unlink(); |
| 103 | } |
| 104 | for (ScratchFile& vdex_file : vdex_files) { |
| 105 | vdex_file.Unlink(); |
| 106 | } |
| 107 | const int rmdir_result = rmdir(image_dir.c_str()); |
| 108 | CHECK_EQ(0, rmdir_result); |
| 109 | } |
Andreas Gampe | 3f41a01 | 2016-02-18 16:53:41 -0800 | [diff] [blame] | 110 | |
Mathieu Chartier | 496577f | 2016-09-20 15:33:31 -0700 | [diff] [blame] | 111 | std::vector<size_t> CompilationHelper::GetImageObjectSectionSizes() { |
| 112 | std::vector<size_t> ret; |
| 113 | for (ScratchFile& image_file : image_files) { |
| 114 | std::unique_ptr<File> file(OS::OpenFileForReading(image_file.GetFilename().c_str())); |
| 115 | CHECK(file.get() != nullptr); |
| 116 | ImageHeader image_header; |
| 117 | CHECK_EQ(file->ReadFully(&image_header, sizeof(image_header)), true); |
| 118 | CHECK(image_header.IsValid()); |
| 119 | ret.push_back(image_header.GetImageSize()); |
| 120 | } |
| 121 | return ret; |
| 122 | } |
Andreas Gampe | 3f41a01 | 2016-02-18 16:53:41 -0800 | [diff] [blame] | 123 | |
Mathieu Chartier | 496577f | 2016-09-20 15:33:31 -0700 | [diff] [blame] | 124 | void CompilationHelper::Compile(CompilerDriver* driver, |
| 125 | ImageHeader::StorageMode storage_mode) { |
Mathieu Chartier | ceb07b3 | 2015-12-10 09:33:21 -0800 | [diff] [blame] | 126 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Mathieu Chartier | 496577f | 2016-09-20 15:33:31 -0700 | [diff] [blame] | 127 | std::vector<const DexFile*> class_path = class_linker->GetBootClassPath(); |
| 128 | |
| 129 | for (const std::unique_ptr<const DexFile>& dex_file : extra_dex_files) { |
| 130 | { |
| 131 | ScopedObjectAccess soa(Thread::Current()); |
| 132 | // Inject in boot class path so that the compiler driver can see it. |
| 133 | class_linker->AppendToBootClassPath(soa.Self(), *dex_file.get()); |
| 134 | } |
| 135 | class_path.push_back(dex_file.get()); |
| 136 | } |
Mathieu Chartier | 866d874 | 2016-09-21 15:24:18 -0700 | [diff] [blame] | 137 | |
Mathieu Chartier | ceb07b3 | 2015-12-10 09:33:21 -0800 | [diff] [blame] | 138 | // Enable write for dex2dex. |
Mathieu Chartier | 496577f | 2016-09-20 15:33:31 -0700 | [diff] [blame] | 139 | for (const DexFile* dex_file : class_path) { |
| 140 | dex_file_locations.push_back(dex_file->GetLocation()); |
| 141 | if (dex_file->IsReadOnly()) { |
| 142 | dex_file->EnableWrite(); |
| 143 | } |
Mathieu Chartier | ceb07b3 | 2015-12-10 09:33:21 -0800 | [diff] [blame] | 144 | } |
Mathieu Chartier | 866d874 | 2016-09-21 15:24:18 -0700 | [diff] [blame] | 145 | { |
Mathieu Chartier | 496577f | 2016-09-20 15:33:31 -0700 | [diff] [blame] | 146 | // Create a generic tmp file, to be the base of the .art and .oat temporary files. |
Mathieu Chartier | 866d874 | 2016-09-21 15:24:18 -0700 | [diff] [blame] | 147 | ScratchFile location; |
Mathieu Chartier | 496577f | 2016-09-20 15:33:31 -0700 | [diff] [blame] | 148 | for (int i = 0; i < static_cast<int>(class_path.size()); ++i) { |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 149 | std::string cur_location = |
| 150 | android::base::StringPrintf("%s-%d.art", location.GetFilename().c_str(), i); |
Mathieu Chartier | 866d874 | 2016-09-21 15:24:18 -0700 | [diff] [blame] | 151 | image_locations.push_back(ScratchFile(cur_location)); |
| 152 | } |
| 153 | } |
| 154 | std::vector<std::string> image_filenames; |
Mathieu Chartier | 866d874 | 2016-09-21 15:24:18 -0700 | [diff] [blame] | 155 | for (ScratchFile& file : image_locations) { |
| 156 | std::string image_filename(GetSystemImageFilename(file.GetFilename().c_str(), kRuntimeISA)); |
| 157 | image_filenames.push_back(image_filename); |
| 158 | size_t pos = image_filename.rfind('/'); |
| 159 | CHECK_NE(pos, std::string::npos) << image_filename; |
| 160 | if (image_dir.empty()) { |
| 161 | image_dir = image_filename.substr(0, pos); |
| 162 | int mkdir_result = mkdir(image_dir.c_str(), 0700); |
| 163 | CHECK_EQ(0, mkdir_result) << image_dir; |
| 164 | } |
| 165 | image_files.push_back(ScratchFile(OS::CreateEmptyFile(image_filename.c_str()))); |
| 166 | } |
Brian Carlstrom | 0e12bdc | 2014-05-14 17:44:28 -0700 | [diff] [blame] | 167 | |
Mathieu Chartier | 866d874 | 2016-09-21 15:24:18 -0700 | [diff] [blame] | 168 | std::vector<std::string> oat_filenames; |
Mathieu Chartier | 866d874 | 2016-09-21 15:24:18 -0700 | [diff] [blame] | 169 | std::vector<std::string> vdex_filenames; |
Mathieu Chartier | 866d874 | 2016-09-21 15:24:18 -0700 | [diff] [blame] | 170 | for (const std::string& image_filename : image_filenames) { |
| 171 | std::string oat_filename = ReplaceFileExtension(image_filename, "oat"); |
| 172 | oat_files.push_back(ScratchFile(OS::CreateEmptyFile(oat_filename.c_str()))); |
| 173 | oat_filenames.push_back(oat_filename); |
| 174 | std::string vdex_filename = ReplaceFileExtension(image_filename, "vdex"); |
| 175 | vdex_files.push_back(ScratchFile(OS::CreateEmptyFile(vdex_filename.c_str()))); |
| 176 | vdex_filenames.push_back(vdex_filename); |
| 177 | } |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 178 | |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 179 | std::unordered_map<const DexFile*, size_t> dex_file_to_oat_index_map; |
Mathieu Chartier | 866d874 | 2016-09-21 15:24:18 -0700 | [diff] [blame] | 180 | std::vector<const char*> oat_filename_vector; |
| 181 | for (const std::string& file : oat_filenames) { |
| 182 | oat_filename_vector.push_back(file.c_str()); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 183 | } |
Mathieu Chartier | 866d874 | 2016-09-21 15:24:18 -0700 | [diff] [blame] | 184 | std::vector<const char*> image_filename_vector; |
| 185 | for (const std::string& file : image_filenames) { |
| 186 | image_filename_vector.push_back(file.c_str()); |
| 187 | } |
| 188 | size_t image_idx = 0; |
Mathieu Chartier | 496577f | 2016-09-20 15:33:31 -0700 | [diff] [blame] | 189 | for (const DexFile* dex_file : class_path) { |
Mathieu Chartier | 866d874 | 2016-09-21 15:24:18 -0700 | [diff] [blame] | 190 | dex_file_to_oat_index_map.emplace(dex_file, image_idx); |
| 191 | ++image_idx; |
| 192 | } |
| 193 | // TODO: compile_pic should be a test argument. |
Mathieu Chartier | 496577f | 2016-09-20 15:33:31 -0700 | [diff] [blame] | 194 | std::unique_ptr<ImageWriter> writer(new ImageWriter(*driver, |
| 195 | kRequestedImageBase, |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 196 | /*compile_pic*/false, |
Mathieu Chartier | ceb07b3 | 2015-12-10 09:33:21 -0800 | [diff] [blame] | 197 | /*compile_app_image*/false, |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 198 | storage_mode, |
| 199 | oat_filename_vector, |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 200 | dex_file_to_oat_index_map)); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 201 | { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 202 | { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 203 | jobject class_loader = nullptr; |
Ian Rogers | 5fe9af7 | 2013-11-14 00:17:20 -0800 | [diff] [blame] | 204 | TimingLogger timings("ImageTest::WriteRead", false, false); |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 205 | TimingLogger::ScopedTiming t("CompileAll", &timings); |
Mathieu Chartier | 496577f | 2016-09-20 15:33:31 -0700 | [diff] [blame] | 206 | driver->SetDexFilesForOatFile(class_path); |
Nicolas Geoffray | 6bb7f1b | 2016-11-03 10:52:49 +0000 | [diff] [blame] | 207 | driver->CompileAll(class_loader, class_path, /* verifier_deps */ nullptr, &timings); |
Brian Carlstrom | 9639160 | 2013-06-13 19:49:50 -0700 | [diff] [blame] | 208 | |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 209 | t.NewTiming("WriteElf"); |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 210 | SafeMap<std::string, std::string> key_value_store; |
Mathieu Chartier | 866d874 | 2016-09-21 15:24:18 -0700 | [diff] [blame] | 211 | std::vector<const char*> dex_filename_vector; |
Mathieu Chartier | 496577f | 2016-09-20 15:33:31 -0700 | [diff] [blame] | 212 | for (size_t i = 0; i < class_path.size(); ++i) { |
Mathieu Chartier | 866d874 | 2016-09-21 15:24:18 -0700 | [diff] [blame] | 213 | dex_filename_vector.push_back(""); |
| 214 | } |
| 215 | key_value_store.Put(OatHeader::kBootClassPathKey, |
| 216 | gc::space::ImageSpace::GetMultiImageBootClassPath( |
| 217 | dex_filename_vector, |
| 218 | oat_filename_vector, |
| 219 | image_filename_vector)); |
| 220 | |
Mathieu Chartier | 866d874 | 2016-09-21 15:24:18 -0700 | [diff] [blame] | 221 | std::vector<std::unique_ptr<ElfWriter>> elf_writers; |
| 222 | std::vector<std::unique_ptr<OatWriter>> oat_writers; |
| 223 | for (ScratchFile& oat_file : oat_files) { |
Mathieu Chartier | 496577f | 2016-09-20 15:33:31 -0700 | [diff] [blame] | 224 | elf_writers.emplace_back(CreateElfWriterQuick(driver->GetInstructionSet(), |
| 225 | driver->GetInstructionSetFeatures(), |
| 226 | &driver->GetCompilerOptions(), |
| 227 | oat_file.GetFile())); |
Mathieu Chartier | 866d874 | 2016-09-21 15:24:18 -0700 | [diff] [blame] | 228 | elf_writers.back()->Start(); |
Jeff Hao | 608f2ce | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 229 | oat_writers.emplace_back(new OatWriter(/*compiling_boot_image*/true, |
| 230 | &timings, |
| 231 | /*profile_compilation_info*/nullptr)); |
Mathieu Chartier | 866d874 | 2016-09-21 15:24:18 -0700 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | std::vector<OutputStream*> rodata; |
| 235 | std::vector<std::unique_ptr<MemMap>> opened_dex_files_map; |
| 236 | std::vector<std::unique_ptr<const DexFile>> opened_dex_files; |
| 237 | // Now that we have finalized key_value_store_, start writing the oat file. |
| 238 | for (size_t i = 0, size = oat_writers.size(); i != size; ++i) { |
Mathieu Chartier | 496577f | 2016-09-20 15:33:31 -0700 | [diff] [blame] | 239 | const DexFile* dex_file = class_path[i]; |
Mathieu Chartier | 866d874 | 2016-09-21 15:24:18 -0700 | [diff] [blame] | 240 | rodata.push_back(elf_writers[i]->StartRoData()); |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 241 | ArrayRef<const uint8_t> raw_dex_file( |
| 242 | reinterpret_cast<const uint8_t*>(&dex_file->GetHeader()), |
| 243 | dex_file->GetHeader().file_size_); |
Mathieu Chartier | 866d874 | 2016-09-21 15:24:18 -0700 | [diff] [blame] | 244 | oat_writers[i]->AddRawDexFileSource(raw_dex_file, |
| 245 | dex_file->GetLocation().c_str(), |
| 246 | dex_file->GetLocationChecksum()); |
| 247 | |
| 248 | std::unique_ptr<MemMap> cur_opened_dex_files_map; |
| 249 | std::vector<std::unique_ptr<const DexFile>> cur_opened_dex_files; |
| 250 | bool dex_files_ok = oat_writers[i]->WriteAndOpenDexFiles( |
| 251 | kIsVdexEnabled ? vdex_files[i].GetFile() : oat_files[i].GetFile(), |
| 252 | rodata.back(), |
Mathieu Chartier | 496577f | 2016-09-20 15:33:31 -0700 | [diff] [blame] | 253 | driver->GetInstructionSet(), |
| 254 | driver->GetInstructionSetFeatures(), |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 255 | &key_value_store, |
| 256 | /* verify */ false, // Dex files may be dex-to-dex-ed, don't verify. |
Nicolas Geoffray | 81f57d1 | 2016-12-20 13:17:09 +0000 | [diff] [blame] | 257 | /* update_input_vdex */ false, |
Mathieu Chartier | 866d874 | 2016-09-21 15:24:18 -0700 | [diff] [blame] | 258 | &cur_opened_dex_files_map, |
| 259 | &cur_opened_dex_files); |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 260 | ASSERT_TRUE(dex_files_ok); |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 261 | |
Mathieu Chartier | 866d874 | 2016-09-21 15:24:18 -0700 | [diff] [blame] | 262 | if (cur_opened_dex_files_map != nullptr) { |
| 263 | opened_dex_files_map.push_back(std::move(cur_opened_dex_files_map)); |
| 264 | for (std::unique_ptr<const DexFile>& cur_dex_file : cur_opened_dex_files) { |
| 265 | // dex_file_oat_index_map_.emplace(dex_file.get(), i); |
| 266 | opened_dex_files.push_back(std::move(cur_dex_file)); |
| 267 | } |
| 268 | } else { |
| 269 | ASSERT_TRUE(cur_opened_dex_files.empty()); |
| 270 | } |
| 271 | } |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 272 | bool image_space_ok = writer->PrepareImageAddressSpace(); |
| 273 | ASSERT_TRUE(image_space_ok); |
| 274 | |
Nicolas Geoffray | f54e5df | 2016-12-01 10:45:08 +0000 | [diff] [blame] | 275 | if (kIsVdexEnabled) { |
| 276 | for (size_t i = 0, size = vdex_files.size(); i != size; ++i) { |
| 277 | std::unique_ptr<BufferedOutputStream> vdex_out( |
| 278 | MakeUnique<BufferedOutputStream>( |
| 279 | MakeUnique<FileOutputStream>(vdex_files[i].GetFile()))); |
| 280 | oat_writers[i]->WriteVerifierDeps(vdex_out.get(), nullptr); |
| 281 | oat_writers[i]->WriteChecksumsAndVdexHeader(vdex_out.get()); |
| 282 | } |
| 283 | } |
| 284 | |
Mathieu Chartier | 866d874 | 2016-09-21 15:24:18 -0700 | [diff] [blame] | 285 | for (size_t i = 0, size = oat_files.size(); i != size; ++i) { |
Mathieu Chartier | 496577f | 2016-09-20 15:33:31 -0700 | [diff] [blame] | 286 | linker::MultiOatRelativePatcher patcher(driver->GetInstructionSet(), |
| 287 | driver->GetInstructionSetFeatures()); |
Mathieu Chartier | 866d874 | 2016-09-21 15:24:18 -0700 | [diff] [blame] | 288 | OatWriter* const oat_writer = oat_writers[i].get(); |
| 289 | ElfWriter* const elf_writer = elf_writers[i].get(); |
Mathieu Chartier | 496577f | 2016-09-20 15:33:31 -0700 | [diff] [blame] | 290 | std::vector<const DexFile*> cur_dex_files(1u, class_path[i]); |
Nicolas Geoffray | 4acefd3 | 2016-10-24 13:14:58 +0100 | [diff] [blame] | 291 | oat_writer->Initialize(driver, writer.get(), cur_dex_files); |
| 292 | oat_writer->PrepareLayout(&patcher); |
Mathieu Chartier | 866d874 | 2016-09-21 15:24:18 -0700 | [diff] [blame] | 293 | size_t rodata_size = oat_writer->GetOatHeader().GetExecutableOffset(); |
| 294 | size_t text_size = oat_writer->GetOatSize() - rodata_size; |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 295 | elf_writer->PrepareDynamicSection(rodata_size, |
| 296 | text_size, |
| 297 | oat_writer->GetBssSize(), |
| 298 | oat_writer->GetBssRootsOffset()); |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 299 | |
Mathieu Chartier | 866d874 | 2016-09-21 15:24:18 -0700 | [diff] [blame] | 300 | writer->UpdateOatFileLayout(i, |
| 301 | elf_writer->GetLoadedSize(), |
| 302 | oat_writer->GetOatDataOffset(), |
| 303 | oat_writer->GetOatSize()); |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 304 | |
Mathieu Chartier | 866d874 | 2016-09-21 15:24:18 -0700 | [diff] [blame] | 305 | bool rodata_ok = oat_writer->WriteRodata(rodata[i]); |
| 306 | ASSERT_TRUE(rodata_ok); |
| 307 | elf_writer->EndRoData(rodata[i]); |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 308 | |
Mathieu Chartier | 866d874 | 2016-09-21 15:24:18 -0700 | [diff] [blame] | 309 | OutputStream* text = elf_writer->StartText(); |
| 310 | bool text_ok = oat_writer->WriteCode(text); |
| 311 | ASSERT_TRUE(text_ok); |
| 312 | elf_writer->EndText(text); |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 313 | |
Mathieu Chartier | 866d874 | 2016-09-21 15:24:18 -0700 | [diff] [blame] | 314 | bool header_ok = oat_writer->WriteHeader(elf_writer->GetStream(), 0u, 0u, 0u); |
| 315 | ASSERT_TRUE(header_ok); |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 316 | |
Mathieu Chartier | 866d874 | 2016-09-21 15:24:18 -0700 | [diff] [blame] | 317 | writer->UpdateOatFileHeader(i, oat_writer->GetOatHeader()); |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 318 | |
Mathieu Chartier | 866d874 | 2016-09-21 15:24:18 -0700 | [diff] [blame] | 319 | elf_writer->WriteDynamicSection(); |
| 320 | elf_writer->WriteDebugInfo(oat_writer->GetMethodDebugInfo()); |
| 321 | elf_writer->WritePatchLocations(oat_writer->GetAbsolutePatchLocations()); |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 322 | |
Mathieu Chartier | 866d874 | 2016-09-21 15:24:18 -0700 | [diff] [blame] | 323 | bool success = elf_writer->End(); |
| 324 | ASSERT_TRUE(success); |
| 325 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 326 | } |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 327 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 328 | bool success_image = writer->Write(kInvalidFd, |
Mathieu Chartier | 866d874 | 2016-09-21 15:24:18 -0700 | [diff] [blame] | 329 | image_filename_vector, |
| 330 | oat_filename_vector); |
jeffhao | 8161c03 | 2012-10-31 15:50:00 -0700 | [diff] [blame] | 331 | ASSERT_TRUE(success_image); |
Andreas Gampe | 4303ba9 | 2014-11-06 01:00:46 -0800 | [diff] [blame] | 332 | |
Mathieu Chartier | 866d874 | 2016-09-21 15:24:18 -0700 | [diff] [blame] | 333 | for (size_t i = 0, size = oat_filenames.size(); i != size; ++i) { |
| 334 | const char* oat_filename = oat_filenames[i].c_str(); |
| 335 | std::unique_ptr<File> oat_file(OS::OpenFileReadWrite(oat_filename)); |
| 336 | ASSERT_TRUE(oat_file != nullptr); |
| 337 | bool success_fixup = ElfWriter::Fixup(oat_file.get(), |
| 338 | writer->GetOatDataBegin(i)); |
| 339 | ASSERT_TRUE(success_fixup); |
| 340 | ASSERT_EQ(oat_file->FlushCloseOrErase(), 0) << "Could not flush and close oat file " |
| 341 | << oat_filename; |
| 342 | } |
jeffhao | 8161c03 | 2012-10-31 15:50:00 -0700 | [diff] [blame] | 343 | } |
Mathieu Chartier | 496577f | 2016-09-20 15:33:31 -0700 | [diff] [blame] | 344 | } |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 345 | |
Mathieu Chartier | 496577f | 2016-09-20 15:33:31 -0700 | [diff] [blame] | 346 | void ImageTest::Compile(ImageHeader::StorageMode storage_mode, |
| 347 | CompilationHelper& helper, |
| 348 | const std::string& extra_dex, |
| 349 | const std::string& image_class) { |
| 350 | if (!image_class.empty()) { |
| 351 | image_classes_.insert(image_class); |
| 352 | } |
| 353 | CreateCompilerDriver(Compiler::kOptimizing, kRuntimeISA, kIsTargetBuild ? 2U : 16U); |
| 354 | // Set inline filter values. |
| 355 | compiler_options_->SetInlineDepthLimit(CompilerOptions::kDefaultInlineDepthLimit); |
| 356 | compiler_options_->SetInlineMaxCodeUnits(CompilerOptions::kDefaultInlineMaxCodeUnits); |
| 357 | image_classes_.clear(); |
| 358 | if (!extra_dex.empty()) { |
| 359 | helper.extra_dex_files = OpenTestDexFiles(extra_dex.c_str()); |
| 360 | } |
| 361 | helper.Compile(compiler_driver_.get(), storage_mode); |
| 362 | if (!image_class.empty()) { |
| 363 | // Make sure the class got initialized. |
| 364 | ScopedObjectAccess soa(Thread::Current()); |
| 365 | ClassLinker* const class_linker = Runtime::Current()->GetClassLinker(); |
| 366 | mirror::Class* klass = class_linker->FindSystemClass(Thread::Current(), image_class.c_str()); |
| 367 | EXPECT_TRUE(klass != nullptr); |
| 368 | EXPECT_TRUE(klass->IsInitialized()); |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | void ImageTest::TestWriteRead(ImageHeader::StorageMode storage_mode) { |
| 373 | CompilationHelper helper; |
| 374 | Compile(storage_mode, /*out*/ helper); |
Mathieu Chartier | 866d874 | 2016-09-21 15:24:18 -0700 | [diff] [blame] | 375 | std::vector<uint64_t> image_file_sizes; |
Mathieu Chartier | 496577f | 2016-09-20 15:33:31 -0700 | [diff] [blame] | 376 | for (ScratchFile& image_file : helper.image_files) { |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 377 | std::unique_ptr<File> file(OS::OpenFileForReading(image_file.GetFilename().c_str())); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 378 | ASSERT_TRUE(file.get() != nullptr); |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 379 | ImageHeader image_header; |
Andreas Gampe | 4303ba9 | 2014-11-06 01:00:46 -0800 | [diff] [blame] | 380 | ASSERT_EQ(file->ReadFully(&image_header, sizeof(image_header)), true); |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 381 | ASSERT_TRUE(image_header.IsValid()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 382 | const auto& bitmap_section = image_header.GetImageSection(ImageHeader::kSectionImageBitmap); |
| 383 | ASSERT_GE(bitmap_section.Offset(), sizeof(image_header)); |
| 384 | ASSERT_NE(0U, bitmap_section.Size()); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 385 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 386 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
Mathieu Chartier | a9d82fe | 2016-01-25 20:06:11 -0800 | [diff] [blame] | 387 | ASSERT_TRUE(heap->HaveContinuousSpaces()); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 388 | gc::space::ContinuousSpace* space = heap->GetNonMovingSpace(); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 389 | ASSERT_FALSE(space->IsImageSpace()); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 390 | ASSERT_TRUE(space != nullptr); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 391 | ASSERT_TRUE(space->IsMallocSpace()); |
Mathieu Chartier | 866d874 | 2016-09-21 15:24:18 -0700 | [diff] [blame] | 392 | image_file_sizes.push_back(file->GetLength()); |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 393 | } |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 394 | |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 395 | ASSERT_TRUE(compiler_driver_->GetImageClasses() != nullptr); |
Andreas Gampe | b1fcead | 2015-04-20 18:53:51 -0700 | [diff] [blame] | 396 | std::unordered_set<std::string> image_classes(*compiler_driver_->GetImageClasses()); |
Brian Carlstrom | 9639160 | 2013-06-13 19:49:50 -0700 | [diff] [blame] | 397 | |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 398 | // Need to delete the compiler since it has worker threads which are attached to runtime. |
Ian Rogers | 1212a02 | 2013-03-04 10:48:41 -0800 | [diff] [blame] | 399 | compiler_driver_.reset(); |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 400 | |
Ian Rogers | 10c5b78 | 2013-01-10 10:40:53 -0800 | [diff] [blame] | 401 | // Tear down old runtime before making a new one, clearing out misc state. |
Mathieu Chartier | 6e88ef6 | 2014-10-14 15:01:24 -0700 | [diff] [blame] | 402 | |
| 403 | // Remove the reservation of the memory for use to load the image. |
| 404 | // Need to do this before we reset the runtime. |
| 405 | UnreserveImageSpace(); |
Mathieu Chartier | 6e88ef6 | 2014-10-14 15:01:24 -0700 | [diff] [blame] | 406 | |
Mathieu Chartier | 496577f | 2016-09-20 15:33:31 -0700 | [diff] [blame] | 407 | helper.extra_dex_files.clear(); |
Ian Rogers | 10c5b78 | 2013-01-10 10:40:53 -0800 | [diff] [blame] | 408 | runtime_.reset(); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 409 | java_lang_dex_file_ = nullptr; |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 410 | |
Mathieu Chartier | 6e88ef6 | 2014-10-14 15:01:24 -0700 | [diff] [blame] | 411 | MemMap::Init(); |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 412 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 413 | RuntimeOptions options; |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 414 | std::string image("-Ximage:"); |
Mathieu Chartier | 496577f | 2016-09-20 15:33:31 -0700 | [diff] [blame] | 415 | image.append(helper.image_locations[0].GetFilename()); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 416 | options.push_back(std::make_pair(image.c_str(), static_cast<void*>(nullptr))); |
Alex Light | 6e183f2 | 2014-07-18 14:57:04 -0700 | [diff] [blame] | 417 | // By default the compiler this creates will not include patch information. |
| 418 | options.push_back(std::make_pair("-Xnorelocate", nullptr)); |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 419 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 420 | if (!Runtime::Create(options, false)) { |
| 421 | LOG(FATAL) << "Failed to create runtime"; |
| 422 | return; |
| 423 | } |
| 424 | runtime_.reset(Runtime::Current()); |
| 425 | // Runtime::Create acquired the mutator_lock_ that is normally given away when we Runtime::Start, |
| 426 | // give it away now and then switch to a more managable ScopedObjectAccess. |
| 427 | Thread::Current()->TransitionFromRunnableToSuspended(kNative); |
| 428 | ScopedObjectAccess soa(Thread::Current()); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 429 | ASSERT_TRUE(runtime_.get() != nullptr); |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 430 | class_linker_ = runtime_->GetClassLinker(); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 431 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 432 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 433 | ASSERT_TRUE(heap->HasBootImageSpace()); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 434 | ASSERT_TRUE(heap->GetNonMovingSpace()->IsMallocSpace()); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 435 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 436 | // We loaded the runtime with an explicit image, so it must exist. |
Mathieu Chartier | 866d874 | 2016-09-21 15:24:18 -0700 | [diff] [blame] | 437 | ASSERT_EQ(heap->GetBootImageSpaces().size(), image_file_sizes.size()); |
Mathieu Chartier | 496577f | 2016-09-20 15:33:31 -0700 | [diff] [blame] | 438 | for (size_t i = 0; i < helper.dex_file_locations.size(); ++i) { |
Mathieu Chartier | 866d874 | 2016-09-21 15:24:18 -0700 | [diff] [blame] | 439 | std::unique_ptr<const DexFile> dex( |
Mathieu Chartier | 496577f | 2016-09-20 15:33:31 -0700 | [diff] [blame] | 440 | LoadExpectSingleDexFile(helper.dex_file_locations[i].c_str())); |
| 441 | ASSERT_TRUE(dex != nullptr); |
Mathieu Chartier | 866d874 | 2016-09-21 15:24:18 -0700 | [diff] [blame] | 442 | uint64_t image_file_size = image_file_sizes[i]; |
| 443 | gc::space::ImageSpace* image_space = heap->GetBootImageSpaces()[i]; |
| 444 | ASSERT_TRUE(image_space != nullptr); |
| 445 | if (storage_mode == ImageHeader::kStorageModeUncompressed) { |
| 446 | // Uncompressed, image should be smaller than file. |
| 447 | ASSERT_LE(image_space->GetImageHeader().GetImageSize(), image_file_size); |
Mathieu Chartier | 496577f | 2016-09-20 15:33:31 -0700 | [diff] [blame] | 448 | } else if (image_file_size > 16 * KB) { |
| 449 | // Compressed, file should be smaller than image. Not really valid for small images. |
Mathieu Chartier | 866d874 | 2016-09-21 15:24:18 -0700 | [diff] [blame] | 450 | ASSERT_LE(image_file_size, image_space->GetImageHeader().GetImageSize()); |
Brian Carlstrom | 9639160 | 2013-06-13 19:49:50 -0700 | [diff] [blame] | 451 | } |
Mathieu Chartier | 866d874 | 2016-09-21 15:24:18 -0700 | [diff] [blame] | 452 | |
| 453 | image_space->VerifyImageAllocations(); |
| 454 | uint8_t* image_begin = image_space->Begin(); |
| 455 | uint8_t* image_end = image_space->End(); |
| 456 | if (i == 0) { |
| 457 | // This check is only valid for image 0. |
Mathieu Chartier | 496577f | 2016-09-20 15:33:31 -0700 | [diff] [blame] | 458 | CHECK_EQ(kRequestedImageBase, reinterpret_cast<uintptr_t>(image_begin)); |
Mathieu Chartier | 866d874 | 2016-09-21 15:24:18 -0700 | [diff] [blame] | 459 | } |
| 460 | for (size_t j = 0; j < dex->NumClassDefs(); ++j) { |
| 461 | const DexFile::ClassDef& class_def = dex->GetClassDef(j); |
| 462 | const char* descriptor = dex->GetClassDescriptor(class_def); |
| 463 | mirror::Class* klass = class_linker_->FindSystemClass(soa.Self(), descriptor); |
| 464 | EXPECT_TRUE(klass != nullptr) << descriptor; |
| 465 | if (image_classes.find(descriptor) == image_classes.end()) { |
| 466 | EXPECT_TRUE(reinterpret_cast<uint8_t*>(klass) >= image_end || |
| 467 | reinterpret_cast<uint8_t*>(klass) < image_begin) << descriptor; |
| 468 | } else { |
| 469 | // Image classes should be located inside the image. |
| 470 | EXPECT_LT(image_begin, reinterpret_cast<uint8_t*>(klass)) << descriptor; |
| 471 | EXPECT_LT(reinterpret_cast<uint8_t*>(klass), image_end) << descriptor; |
| 472 | } |
| 473 | EXPECT_TRUE(Monitor::IsValidLockWord(klass->GetLockWord(false))); |
| 474 | } |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 475 | } |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 476 | } |
| 477 | |
Mathieu Chartier | ceb07b3 | 2015-12-10 09:33:21 -0800 | [diff] [blame] | 478 | TEST_F(ImageTest, WriteReadUncompressed) { |
| 479 | TestWriteRead(ImageHeader::kStorageModeUncompressed); |
| 480 | } |
| 481 | |
| 482 | TEST_F(ImageTest, WriteReadLZ4) { |
| 483 | TestWriteRead(ImageHeader::kStorageModeLZ4); |
| 484 | } |
| 485 | |
Mathieu Chartier | a6e81ed | 2016-02-25 13:52:10 -0800 | [diff] [blame] | 486 | TEST_F(ImageTest, WriteReadLZ4HC) { |
| 487 | TestWriteRead(ImageHeader::kStorageModeLZ4HC); |
| 488 | } |
| 489 | |
Mathieu Chartier | 496577f | 2016-09-20 15:33:31 -0700 | [diff] [blame] | 490 | TEST_F(ImageTest, TestImageLayout) { |
| 491 | std::vector<size_t> image_sizes; |
| 492 | std::vector<size_t> image_sizes_extra; |
| 493 | // Compile multi-image with ImageLayoutA being the last image. |
| 494 | { |
| 495 | CompilationHelper helper; |
| 496 | Compile(ImageHeader::kStorageModeUncompressed, helper, "ImageLayoutA", "LMyClass;"); |
| 497 | image_sizes = helper.GetImageObjectSectionSizes(); |
| 498 | } |
| 499 | TearDown(); |
| 500 | runtime_.reset(); |
| 501 | SetUp(); |
| 502 | // Compile multi-image with ImageLayoutB being the last image. |
| 503 | { |
| 504 | CompilationHelper helper; |
| 505 | Compile(ImageHeader::kStorageModeUncompressed, helper, "ImageLayoutB", "LMyClass;"); |
| 506 | image_sizes_extra = helper.GetImageObjectSectionSizes(); |
| 507 | } |
| 508 | // Make sure that the new stuff in the clinit in ImageLayoutB is in the last image and not in the |
| 509 | // first two images. |
| 510 | ASSERT_EQ(image_sizes.size(), image_sizes.size()); |
| 511 | // Sizes of the images should be the same. These sizes are for the whole image unrounded. |
| 512 | for (size_t i = 0; i < image_sizes.size() - 1; ++i) { |
| 513 | EXPECT_EQ(image_sizes[i], image_sizes_extra[i]); |
| 514 | } |
| 515 | // Last image should be larger since it has a hash map and a string. |
| 516 | EXPECT_LT(image_sizes.back(), image_sizes_extra.back()); |
| 517 | } |
| 518 | |
Brian Carlstrom | 179486a | 2013-09-03 11:51:42 -0700 | [diff] [blame] | 519 | TEST_F(ImageTest, ImageHeaderIsValid) { |
| 520 | uint32_t image_begin = ART_BASE_ADDRESS; |
| 521 | uint32_t image_size_ = 16 * KB; |
Brian Carlstrom | 179486a | 2013-09-03 11:51:42 -0700 | [diff] [blame] | 522 | uint32_t image_roots = ART_BASE_ADDRESS + (1 * KB); |
| 523 | uint32_t oat_checksum = 0; |
| 524 | uint32_t oat_file_begin = ART_BASE_ADDRESS + (4 * KB); // page aligned |
| 525 | uint32_t oat_data_begin = ART_BASE_ADDRESS + (8 * KB); // page aligned |
| 526 | uint32_t oat_data_end = ART_BASE_ADDRESS + (9 * KB); |
| 527 | uint32_t oat_file_end = ART_BASE_ADDRESS + (10 * KB); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 528 | ImageSection sections[ImageHeader::kSectionCount]; |
Brian Carlstrom | 179486a | 2013-09-03 11:51:42 -0700 | [diff] [blame] | 529 | ImageHeader image_header(image_begin, |
| 530 | image_size_, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 531 | sections, |
Brian Carlstrom | 179486a | 2013-09-03 11:51:42 -0700 | [diff] [blame] | 532 | image_roots, |
| 533 | oat_checksum, |
| 534 | oat_file_begin, |
| 535 | oat_data_begin, |
| 536 | oat_data_end, |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 537 | oat_file_end, |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 538 | /*boot_image_begin*/0U, |
| 539 | /*boot_image_size*/0U, |
| 540 | /*boot_oat_begin*/0U, |
| 541 | /*boot_oat_size_*/0U, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 542 | sizeof(void*), |
Mathieu Chartier | ceb07b3 | 2015-12-10 09:33:21 -0800 | [diff] [blame] | 543 | /*compile_pic*/false, |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 544 | /*is_pic*/false, |
Mathieu Chartier | ceb07b3 | 2015-12-10 09:33:21 -0800 | [diff] [blame] | 545 | ImageHeader::kDefaultStorageMode, |
| 546 | /*data_size*/0u); |
Brian Carlstrom | 179486a | 2013-09-03 11:51:42 -0700 | [diff] [blame] | 547 | ASSERT_TRUE(image_header.IsValid()); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 548 | ASSERT_TRUE(!image_header.IsAppImage()); |
Brian Carlstrom | 179486a | 2013-09-03 11:51:42 -0700 | [diff] [blame] | 549 | |
| 550 | char* magic = const_cast<char*>(image_header.GetMagic()); |
| 551 | strcpy(magic, ""); // bad magic |
| 552 | ASSERT_FALSE(image_header.IsValid()); |
| 553 | strcpy(magic, "art\n000"); // bad version |
| 554 | ASSERT_FALSE(image_header.IsValid()); |
| 555 | } |
| 556 | |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 557 | } // namespace art |