Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [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 | */ |
| 16 | |
Vladimir Marko | 7452797 | 2016-11-29 15:57:32 +0000 | [diff] [blame] | 17 | #ifndef ART_DEX2OAT_LINKER_IMAGE_TEST_H_ |
| 18 | #define ART_DEX2OAT_LINKER_IMAGE_TEST_H_ |
Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 19 | |
| 20 | #include "image.h" |
| 21 | |
| 22 | #include <memory> |
| 23 | #include <string> |
| 24 | #include <vector> |
| 25 | |
| 26 | #include "android-base/stringprintf.h" |
| 27 | |
| 28 | #include "art_method-inl.h" |
David Sehr | 891a50e | 2017-10-27 17:01:07 -0700 | [diff] [blame] | 29 | #include "base/file_utils.h" |
Vladimir Marko | 54159c6 | 2018-06-20 14:30:08 +0100 | [diff] [blame] | 30 | #include "base/hash_set.h" |
Vladimir Marko | d79b37b | 2018-11-02 13:06:22 +0000 | [diff] [blame] | 31 | #include "base/stl_util.h" |
Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 32 | #include "base/unix_file/fd_file.h" |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 33 | #include "base/utils.h" |
Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 34 | #include "class_linker-inl.h" |
Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 35 | #include "common_compiler_test.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 36 | #include "compiler_callbacks.h" |
Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 37 | #include "debug/method_debug_info.h" |
| 38 | #include "dex/quick_compiler_callbacks.h" |
Vladimir Marko | a043111 | 2018-06-25 09:32:54 +0100 | [diff] [blame] | 39 | #include "driver/compiler_driver.h" |
Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 40 | #include "driver/compiler_options.h" |
Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 41 | #include "gc/space/image_space.h" |
| 42 | #include "image_writer.h" |
| 43 | #include "linker/buffered_output_stream.h" |
Vladimir Marko | 7452797 | 2016-11-29 15:57:32 +0000 | [diff] [blame] | 44 | #include "linker/elf_writer.h" |
| 45 | #include "linker/elf_writer_quick.h" |
Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 46 | #include "linker/file_output_stream.h" |
| 47 | #include "linker/multi_oat_relative_patcher.h" |
| 48 | #include "lock_word.h" |
| 49 | #include "mirror/object-inl.h" |
| 50 | #include "oat_writer.h" |
| 51 | #include "scoped_thread_state_change-inl.h" |
| 52 | #include "signal_catcher.h" |
Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 53 | |
| 54 | namespace art { |
Vladimir Marko | 7452797 | 2016-11-29 15:57:32 +0000 | [diff] [blame] | 55 | namespace linker { |
Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 56 | |
| 57 | static const uintptr_t kRequestedImageBase = ART_BASE_ADDRESS; |
| 58 | |
| 59 | struct CompilationHelper { |
| 60 | std::vector<std::string> dex_file_locations; |
| 61 | std::vector<ScratchFile> image_locations; |
| 62 | std::vector<std::unique_ptr<const DexFile>> extra_dex_files; |
| 63 | std::vector<ScratchFile> image_files; |
| 64 | std::vector<ScratchFile> oat_files; |
| 65 | std::vector<ScratchFile> vdex_files; |
| 66 | std::string image_dir; |
| 67 | |
Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 68 | std::vector<size_t> GetImageObjectSectionSizes(); |
| 69 | |
| 70 | ~CompilationHelper(); |
| 71 | }; |
| 72 | |
| 73 | class ImageTest : public CommonCompilerTest { |
| 74 | protected: |
| 75 | virtual void SetUp() { |
| 76 | ReserveImageSpace(); |
| 77 | CommonCompilerTest::SetUp(); |
| 78 | } |
| 79 | |
Mathieu Chartier | 1a84296 | 2018-11-13 15:09:51 -0800 | [diff] [blame] | 80 | void TestWriteRead(ImageHeader::StorageMode storage_mode, uint32_t max_image_block_size); |
Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 81 | |
| 82 | void Compile(ImageHeader::StorageMode storage_mode, |
Mathieu Chartier | 1a84296 | 2018-11-13 15:09:51 -0800 | [diff] [blame] | 83 | uint32_t max_image_block_size, |
Vladimir Marko | 213ee2d | 2018-06-22 11:56:34 +0100 | [diff] [blame] | 84 | /*out*/ CompilationHelper& out_helper, |
Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 85 | const std::string& extra_dex = "", |
Vladimir Marko | d79b37b | 2018-11-02 13:06:22 +0000 | [diff] [blame] | 86 | const std::initializer_list<std::string>& image_classes = {}, |
| 87 | const std::initializer_list<std::string>& image_classes_failing_aot_clinit = {}); |
Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 88 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 89 | void SetUpRuntimeOptions(RuntimeOptions* options) override { |
Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 90 | CommonCompilerTest::SetUpRuntimeOptions(options); |
Mathieu Chartier | e01b6f6 | 2017-07-19 16:55:04 -0700 | [diff] [blame] | 91 | QuickCompilerCallbacks* new_callbacks = |
| 92 | new QuickCompilerCallbacks(CompilerCallbacks::CallbackMode::kCompileBootImage); |
| 93 | new_callbacks->SetVerificationResults(verification_results_.get()); |
| 94 | callbacks_.reset(new_callbacks); |
Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 95 | options->push_back(std::make_pair("compilercallbacks", callbacks_.get())); |
| 96 | } |
| 97 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 98 | std::unique_ptr<HashSet<std::string>> GetImageClasses() override { |
Vladimir Marko | 54159c6 | 2018-06-20 14:30:08 +0100 | [diff] [blame] | 99 | return std::make_unique<HashSet<std::string>>(image_classes_); |
Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 100 | } |
| 101 | |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 102 | ArtMethod* FindCopiedMethod(ArtMethod* origin, ObjPtr<mirror::Class> klass) |
Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 103 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 104 | PointerSize pointer_size = class_linker_->GetImagePointerSize(); |
| 105 | for (ArtMethod& m : klass->GetCopiedMethods(pointer_size)) { |
| 106 | if (strcmp(origin->GetName(), m.GetName()) == 0 && |
| 107 | origin->GetSignature() == m.GetSignature()) { |
| 108 | return &m; |
| 109 | } |
| 110 | } |
| 111 | return nullptr; |
| 112 | } |
| 113 | |
| 114 | private: |
Vladimir Marko | 213ee2d | 2018-06-22 11:56:34 +0100 | [diff] [blame] | 115 | void DoCompile(ImageHeader::StorageMode storage_mode, /*out*/ CompilationHelper& out_helper); |
| 116 | |
Vladimir Marko | 54159c6 | 2018-06-20 14:30:08 +0100 | [diff] [blame] | 117 | HashSet<std::string> image_classes_; |
Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 118 | }; |
| 119 | |
| 120 | inline CompilationHelper::~CompilationHelper() { |
| 121 | for (ScratchFile& image_file : image_files) { |
| 122 | image_file.Unlink(); |
| 123 | } |
| 124 | for (ScratchFile& oat_file : oat_files) { |
| 125 | oat_file.Unlink(); |
| 126 | } |
| 127 | for (ScratchFile& vdex_file : vdex_files) { |
| 128 | vdex_file.Unlink(); |
| 129 | } |
| 130 | const int rmdir_result = rmdir(image_dir.c_str()); |
| 131 | CHECK_EQ(0, rmdir_result); |
| 132 | } |
| 133 | |
| 134 | inline std::vector<size_t> CompilationHelper::GetImageObjectSectionSizes() { |
| 135 | std::vector<size_t> ret; |
| 136 | for (ScratchFile& image_file : image_files) { |
| 137 | std::unique_ptr<File> file(OS::OpenFileForReading(image_file.GetFilename().c_str())); |
| 138 | CHECK(file.get() != nullptr); |
| 139 | ImageHeader image_header; |
| 140 | CHECK_EQ(file->ReadFully(&image_header, sizeof(image_header)), true); |
| 141 | CHECK(image_header.IsValid()); |
Vladimir Marko | 6cfbdbc | 2017-07-25 13:26:39 +0100 | [diff] [blame] | 142 | ret.push_back(image_header.GetObjectsSection().Size()); |
Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 143 | } |
| 144 | return ret; |
| 145 | } |
| 146 | |
Vladimir Marko | 213ee2d | 2018-06-22 11:56:34 +0100 | [diff] [blame] | 147 | inline void ImageTest::DoCompile(ImageHeader::StorageMode storage_mode, |
| 148 | /*out*/ CompilationHelper& out_helper) { |
| 149 | CompilerDriver* driver = compiler_driver_.get(); |
Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 150 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 151 | std::vector<const DexFile*> class_path = class_linker->GetBootClassPath(); |
| 152 | |
Vladimir Marko | 213ee2d | 2018-06-22 11:56:34 +0100 | [diff] [blame] | 153 | for (const std::unique_ptr<const DexFile>& dex_file : out_helper.extra_dex_files) { |
Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 154 | { |
| 155 | ScopedObjectAccess soa(Thread::Current()); |
| 156 | // Inject in boot class path so that the compiler driver can see it. |
| 157 | class_linker->AppendToBootClassPath(soa.Self(), *dex_file.get()); |
| 158 | } |
| 159 | class_path.push_back(dex_file.get()); |
| 160 | } |
| 161 | |
| 162 | // Enable write for dex2dex. |
| 163 | for (const DexFile* dex_file : class_path) { |
Vladimir Marko | 213ee2d | 2018-06-22 11:56:34 +0100 | [diff] [blame] | 164 | out_helper.dex_file_locations.push_back(dex_file->GetLocation()); |
Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 165 | if (dex_file->IsReadOnly()) { |
| 166 | dex_file->EnableWrite(); |
| 167 | } |
| 168 | } |
| 169 | { |
| 170 | // Create a generic tmp file, to be the base of the .art and .oat temporary files. |
| 171 | ScratchFile location; |
Vladimir Marko | 91f1032 | 2018-12-07 18:04:10 +0000 | [diff] [blame] | 172 | std::vector<std::string> image_locations = |
| 173 | gc::space::ImageSpace::ExpandMultiImageLocations(out_helper.dex_file_locations, |
| 174 | location.GetFilename() + ".art"); |
| 175 | for (size_t i = 0u; i != class_path.size(); ++i) { |
| 176 | out_helper.image_locations.push_back(ScratchFile(image_locations[i])); |
Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 177 | } |
| 178 | } |
| 179 | std::vector<std::string> image_filenames; |
Vladimir Marko | 213ee2d | 2018-06-22 11:56:34 +0100 | [diff] [blame] | 180 | for (ScratchFile& file : out_helper.image_locations) { |
Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 181 | std::string image_filename(GetSystemImageFilename(file.GetFilename().c_str(), kRuntimeISA)); |
| 182 | image_filenames.push_back(image_filename); |
| 183 | size_t pos = image_filename.rfind('/'); |
| 184 | CHECK_NE(pos, std::string::npos) << image_filename; |
Vladimir Marko | 213ee2d | 2018-06-22 11:56:34 +0100 | [diff] [blame] | 185 | if (out_helper.image_dir.empty()) { |
| 186 | out_helper.image_dir = image_filename.substr(0, pos); |
| 187 | int mkdir_result = mkdir(out_helper.image_dir.c_str(), 0700); |
| 188 | CHECK_EQ(0, mkdir_result) << out_helper.image_dir; |
Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 189 | } |
Vladimir Marko | 213ee2d | 2018-06-22 11:56:34 +0100 | [diff] [blame] | 190 | out_helper.image_files.push_back(ScratchFile(OS::CreateEmptyFile(image_filename.c_str()))); |
Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | std::vector<std::string> oat_filenames; |
| 194 | std::vector<std::string> vdex_filenames; |
| 195 | for (const std::string& image_filename : image_filenames) { |
| 196 | std::string oat_filename = ReplaceFileExtension(image_filename, "oat"); |
Vladimir Marko | 213ee2d | 2018-06-22 11:56:34 +0100 | [diff] [blame] | 197 | out_helper.oat_files.push_back(ScratchFile(OS::CreateEmptyFile(oat_filename.c_str()))); |
Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 198 | oat_filenames.push_back(oat_filename); |
| 199 | std::string vdex_filename = ReplaceFileExtension(image_filename, "vdex"); |
Vladimir Marko | 213ee2d | 2018-06-22 11:56:34 +0100 | [diff] [blame] | 200 | out_helper.vdex_files.push_back(ScratchFile(OS::CreateEmptyFile(vdex_filename.c_str()))); |
Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 201 | vdex_filenames.push_back(vdex_filename); |
| 202 | } |
| 203 | |
| 204 | std::unordered_map<const DexFile*, size_t> dex_file_to_oat_index_map; |
Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 205 | size_t image_idx = 0; |
| 206 | for (const DexFile* dex_file : class_path) { |
| 207 | dex_file_to_oat_index_map.emplace(dex_file, image_idx); |
| 208 | ++image_idx; |
| 209 | } |
| 210 | // TODO: compile_pic should be a test argument. |
Vladimir Marko | a043111 | 2018-06-25 09:32:54 +0100 | [diff] [blame] | 211 | std::unique_ptr<ImageWriter> writer(new ImageWriter(*compiler_options_, |
Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 212 | kRequestedImageBase, |
Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 213 | storage_mode, |
Vladimir Marko | d011d81 | 2018-11-29 12:35:24 +0000 | [diff] [blame] | 214 | oat_filenames, |
Jeff Hao | c23b0c0 | 2017-07-27 18:19:38 -0700 | [diff] [blame] | 215 | dex_file_to_oat_index_map, |
Nicolas Geoffray | f0d3002 | 2018-11-20 17:45:38 +0000 | [diff] [blame] | 216 | /*class_loader=*/ nullptr, |
Vladimir Marko | 9c4b970 | 2018-11-14 15:09:02 +0000 | [diff] [blame] | 217 | /*dirty_image_objects=*/ nullptr)); |
Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 218 | { |
| 219 | { |
| 220 | jobject class_loader = nullptr; |
| 221 | TimingLogger timings("ImageTest::WriteRead", false, false); |
Vladimir Marko | 2afaff7 | 2018-11-30 17:01:50 +0000 | [diff] [blame] | 222 | CompileAll(class_loader, class_path, &timings); |
Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 223 | |
Vladimir Marko | 2afaff7 | 2018-11-30 17:01:50 +0000 | [diff] [blame] | 224 | TimingLogger::ScopedTiming t("WriteElf", &timings); |
Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 225 | SafeMap<std::string, std::string> key_value_store; |
Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 226 | key_value_store.Put(OatHeader::kBootClassPathKey, |
Vladimir Marko | 91f1032 | 2018-12-07 18:04:10 +0000 | [diff] [blame] | 227 | android::base::Join(out_helper.dex_file_locations, ':')); |
Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 228 | |
| 229 | std::vector<std::unique_ptr<ElfWriter>> elf_writers; |
| 230 | std::vector<std::unique_ptr<OatWriter>> oat_writers; |
Vladimir Marko | 213ee2d | 2018-06-22 11:56:34 +0100 | [diff] [blame] | 231 | for (ScratchFile& oat_file : out_helper.oat_files) { |
Vladimir Marko | a043111 | 2018-06-25 09:32:54 +0100 | [diff] [blame] | 232 | elf_writers.emplace_back(CreateElfWriterQuick(*compiler_options_, oat_file.GetFile())); |
Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 233 | elf_writers.back()->Start(); |
Vladimir Marko | a043111 | 2018-06-25 09:32:54 +0100 | [diff] [blame] | 234 | oat_writers.emplace_back(new OatWriter(*compiler_options_, |
Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 235 | &timings, |
Mathieu Chartier | 603ccab | 2017-10-20 14:34:28 -0700 | [diff] [blame] | 236 | /*profile_compilation_info*/nullptr, |
| 237 | CompactDexLevel::kCompactDexLevelNone)); |
Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | std::vector<OutputStream*> rodata; |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 241 | std::vector<MemMap> opened_dex_files_maps; |
Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 242 | std::vector<std::unique_ptr<const DexFile>> opened_dex_files; |
| 243 | // Now that we have finalized key_value_store_, start writing the oat file. |
| 244 | for (size_t i = 0, size = oat_writers.size(); i != size; ++i) { |
| 245 | const DexFile* dex_file = class_path[i]; |
| 246 | rodata.push_back(elf_writers[i]->StartRoData()); |
| 247 | ArrayRef<const uint8_t> raw_dex_file( |
| 248 | reinterpret_cast<const uint8_t*>(&dex_file->GetHeader()), |
| 249 | dex_file->GetHeader().file_size_); |
| 250 | oat_writers[i]->AddRawDexFileSource(raw_dex_file, |
| 251 | dex_file->GetLocation().c_str(), |
| 252 | dex_file->GetLocationChecksum()); |
| 253 | |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 254 | std::vector<MemMap> cur_opened_dex_files_maps; |
Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 255 | std::vector<std::unique_ptr<const DexFile>> cur_opened_dex_files; |
| 256 | bool dex_files_ok = oat_writers[i]->WriteAndOpenDexFiles( |
Vladimir Marko | 213ee2d | 2018-06-22 11:56:34 +0100 | [diff] [blame] | 257 | out_helper.vdex_files[i].GetFile(), |
Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 258 | rodata.back(), |
Vladimir Marko | d011d81 | 2018-11-29 12:35:24 +0000 | [diff] [blame] | 259 | (i == 0u) ? &key_value_store : nullptr, |
Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 260 | /* verify */ false, // Dex files may be dex-to-dex-ed, don't verify. |
| 261 | /* update_input_vdex */ false, |
Nicolas Geoffray | 66ff8a8 | 2018-02-28 13:27:55 +0000 | [diff] [blame] | 262 | /* copy_dex_files */ CopyOption::kOnlyIfCompressed, |
Nicolas Geoffray | f307527 | 2018-01-08 12:41:19 +0000 | [diff] [blame] | 263 | &cur_opened_dex_files_maps, |
Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 264 | &cur_opened_dex_files); |
| 265 | ASSERT_TRUE(dex_files_ok); |
| 266 | |
Nicolas Geoffray | f307527 | 2018-01-08 12:41:19 +0000 | [diff] [blame] | 267 | if (!cur_opened_dex_files_maps.empty()) { |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 268 | for (MemMap& cur_map : cur_opened_dex_files_maps) { |
Nicolas Geoffray | f307527 | 2018-01-08 12:41:19 +0000 | [diff] [blame] | 269 | opened_dex_files_maps.push_back(std::move(cur_map)); |
| 270 | } |
Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 271 | for (std::unique_ptr<const DexFile>& cur_dex_file : cur_opened_dex_files) { |
| 272 | // dex_file_oat_index_map_.emplace(dex_file.get(), i); |
| 273 | opened_dex_files.push_back(std::move(cur_dex_file)); |
| 274 | } |
| 275 | } else { |
| 276 | ASSERT_TRUE(cur_opened_dex_files.empty()); |
| 277 | } |
| 278 | } |
Mathieu Chartier | 703b82a | 2018-04-10 15:52:32 -0700 | [diff] [blame] | 279 | bool image_space_ok = writer->PrepareImageAddressSpace(&timings); |
Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 280 | ASSERT_TRUE(image_space_ok); |
| 281 | |
Vladimir Marko | 213ee2d | 2018-06-22 11:56:34 +0100 | [diff] [blame] | 282 | DCHECK_EQ(out_helper.vdex_files.size(), out_helper.oat_files.size()); |
| 283 | for (size_t i = 0, size = out_helper.oat_files.size(); i != size; ++i) { |
Vladimir Marko | a043111 | 2018-06-25 09:32:54 +0100 | [diff] [blame] | 284 | MultiOatRelativePatcher patcher(compiler_options_->GetInstructionSet(), |
| 285 | compiler_options_->GetInstructionSetFeatures(), |
Vladimir Marko | ca1e038 | 2018-04-11 09:58:41 +0000 | [diff] [blame] | 286 | driver->GetCompiledMethodStorage()); |
Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 287 | OatWriter* const oat_writer = oat_writers[i].get(); |
| 288 | ElfWriter* const elf_writer = elf_writers[i].get(); |
| 289 | std::vector<const DexFile*> cur_dex_files(1u, class_path[i]); |
| 290 | oat_writer->Initialize(driver, writer.get(), cur_dex_files); |
David Brazdil | 93592f5 | 2017-12-08 10:53:27 +0000 | [diff] [blame] | 291 | |
| 292 | std::unique_ptr<BufferedOutputStream> vdex_out = |
| 293 | std::make_unique<BufferedOutputStream>( |
Vladimir Marko | 213ee2d | 2018-06-22 11:56:34 +0100 | [diff] [blame] | 294 | std::make_unique<FileOutputStream>(out_helper.vdex_files[i].GetFile())); |
David Brazdil | 93592f5 | 2017-12-08 10:53:27 +0000 | [diff] [blame] | 295 | oat_writer->WriteVerifierDeps(vdex_out.get(), nullptr); |
| 296 | oat_writer->WriteQuickeningInfo(vdex_out.get()); |
| 297 | oat_writer->WriteChecksumsAndVdexHeader(vdex_out.get()); |
| 298 | |
Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 299 | oat_writer->PrepareLayout(&patcher); |
Vladimir Marko | b066d43 | 2018-01-03 13:14:37 +0000 | [diff] [blame] | 300 | elf_writer->PrepareDynamicSection(oat_writer->GetOatHeader().GetExecutableOffset(), |
| 301 | oat_writer->GetCodeSize(), |
| 302 | oat_writer->GetDataBimgRelRoSize(), |
Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 303 | oat_writer->GetBssSize(), |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 304 | oat_writer->GetBssMethodsOffset(), |
David Srbecky | ec2cdf4 | 2017-12-08 16:21:25 +0000 | [diff] [blame] | 305 | oat_writer->GetBssRootsOffset(), |
| 306 | oat_writer->GetVdexSize()); |
Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 307 | |
| 308 | writer->UpdateOatFileLayout(i, |
| 309 | elf_writer->GetLoadedSize(), |
| 310 | oat_writer->GetOatDataOffset(), |
| 311 | oat_writer->GetOatSize()); |
| 312 | |
| 313 | bool rodata_ok = oat_writer->WriteRodata(rodata[i]); |
| 314 | ASSERT_TRUE(rodata_ok); |
| 315 | elf_writer->EndRoData(rodata[i]); |
| 316 | |
| 317 | OutputStream* text = elf_writer->StartText(); |
| 318 | bool text_ok = oat_writer->WriteCode(text); |
| 319 | ASSERT_TRUE(text_ok); |
| 320 | elf_writer->EndText(text); |
| 321 | |
Vladimir Marko | b066d43 | 2018-01-03 13:14:37 +0000 | [diff] [blame] | 322 | if (oat_writer->GetDataBimgRelRoSize() != 0u) { |
| 323 | OutputStream* data_bimg_rel_ro = elf_writer->StartDataBimgRelRo(); |
| 324 | bool data_bimg_rel_ro_ok = oat_writer->WriteDataBimgRelRo(data_bimg_rel_ro); |
| 325 | ASSERT_TRUE(data_bimg_rel_ro_ok); |
| 326 | elf_writer->EndDataBimgRelRo(data_bimg_rel_ro); |
| 327 | } |
| 328 | |
Vladimir Marko | 0ace563 | 2018-12-14 11:11:47 +0000 | [diff] [blame] | 329 | bool header_ok = oat_writer->WriteHeader(elf_writer->GetStream()); |
Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 330 | ASSERT_TRUE(header_ok); |
| 331 | |
| 332 | writer->UpdateOatFileHeader(i, oat_writer->GetOatHeader()); |
| 333 | |
| 334 | elf_writer->WriteDynamicSection(); |
David Srbecky | 32210b9 | 2017-12-04 14:39:21 +0000 | [diff] [blame] | 335 | elf_writer->WriteDebugInfo(oat_writer->GetDebugInfo()); |
Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 336 | |
| 337 | bool success = elf_writer->End(); |
| 338 | ASSERT_TRUE(success); |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | bool success_image = writer->Write(kInvalidFd, |
Vladimir Marko | d011d81 | 2018-11-29 12:35:24 +0000 | [diff] [blame] | 343 | image_filenames, |
| 344 | oat_filenames); |
Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 345 | ASSERT_TRUE(success_image); |
| 346 | |
| 347 | for (size_t i = 0, size = oat_filenames.size(); i != size; ++i) { |
| 348 | const char* oat_filename = oat_filenames[i].c_str(); |
| 349 | std::unique_ptr<File> oat_file(OS::OpenFileReadWrite(oat_filename)); |
| 350 | ASSERT_TRUE(oat_file != nullptr); |
Vladimir Marko | 213ee2d | 2018-06-22 11:56:34 +0100 | [diff] [blame] | 351 | bool success_fixup = ElfWriter::Fixup(oat_file.get(), writer->GetOatDataBegin(i)); |
Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 352 | ASSERT_TRUE(success_fixup); |
| 353 | ASSERT_EQ(oat_file->FlushCloseOrErase(), 0) << "Could not flush and close oat file " |
| 354 | << oat_filename; |
| 355 | } |
| 356 | } |
| 357 | } |
| 358 | |
Vladimir Marko | d79b37b | 2018-11-02 13:06:22 +0000 | [diff] [blame] | 359 | inline void ImageTest::Compile( |
| 360 | ImageHeader::StorageMode storage_mode, |
Mathieu Chartier | 1a84296 | 2018-11-13 15:09:51 -0800 | [diff] [blame] | 361 | uint32_t max_image_block_size, |
Vladimir Marko | d79b37b | 2018-11-02 13:06:22 +0000 | [diff] [blame] | 362 | CompilationHelper& helper, |
| 363 | const std::string& extra_dex, |
| 364 | const std::initializer_list<std::string>& image_classes, |
| 365 | const std::initializer_list<std::string>& image_classes_failing_aot_clinit) { |
| 366 | for (const std::string& image_class : image_classes_failing_aot_clinit) { |
| 367 | ASSERT_TRUE(ContainsElement(image_classes, image_class)); |
| 368 | } |
Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 369 | for (const std::string& image_class : image_classes) { |
| 370 | image_classes_.insert(image_class); |
| 371 | } |
Vladimir Marko | a043111 | 2018-06-25 09:32:54 +0100 | [diff] [blame] | 372 | number_of_threads_ = kIsTargetBuild ? 2U : 16U; |
| 373 | CreateCompilerDriver(); |
Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 374 | // Set inline filter values. |
| 375 | compiler_options_->SetInlineMaxCodeUnits(CompilerOptions::kDefaultInlineMaxCodeUnits); |
Mathieu Chartier | 1a84296 | 2018-11-13 15:09:51 -0800 | [diff] [blame] | 376 | compiler_options_->SetMaxImageBlockSize(max_image_block_size); |
Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 377 | image_classes_.clear(); |
| 378 | if (!extra_dex.empty()) { |
| 379 | helper.extra_dex_files = OpenTestDexFiles(extra_dex.c_str()); |
| 380 | } |
Vladimir Marko | 213ee2d | 2018-06-22 11:56:34 +0100 | [diff] [blame] | 381 | DoCompile(storage_mode, helper); |
Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 382 | if (image_classes.begin() != image_classes.end()) { |
| 383 | // Make sure the class got initialized. |
| 384 | ScopedObjectAccess soa(Thread::Current()); |
| 385 | ClassLinker* const class_linker = Runtime::Current()->GetClassLinker(); |
| 386 | for (const std::string& image_class : image_classes) { |
Vladimir Marko | e9987b0 | 2018-05-22 16:26:43 +0100 | [diff] [blame] | 387 | ObjPtr<mirror::Class> klass = |
| 388 | class_linker->FindSystemClass(Thread::Current(), image_class.c_str()); |
Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 389 | EXPECT_TRUE(klass != nullptr); |
Vladimir Marko | d79b37b | 2018-11-02 13:06:22 +0000 | [diff] [blame] | 390 | EXPECT_TRUE(klass->IsResolved()); |
| 391 | if (ContainsElement(image_classes_failing_aot_clinit, image_class)) { |
| 392 | EXPECT_FALSE(klass->IsInitialized()); |
| 393 | } else { |
| 394 | EXPECT_TRUE(klass->IsInitialized()); |
| 395 | } |
Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 396 | } |
| 397 | } |
| 398 | } |
| 399 | |
Mathieu Chartier | 1a84296 | 2018-11-13 15:09:51 -0800 | [diff] [blame] | 400 | inline void ImageTest::TestWriteRead(ImageHeader::StorageMode storage_mode, |
| 401 | uint32_t max_image_block_size) { |
Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 402 | CompilationHelper helper; |
Mathieu Chartier | 1a84296 | 2018-11-13 15:09:51 -0800 | [diff] [blame] | 403 | Compile(storage_mode, max_image_block_size, /*out*/ helper); |
Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 404 | std::vector<uint64_t> image_file_sizes; |
| 405 | for (ScratchFile& image_file : helper.image_files) { |
| 406 | std::unique_ptr<File> file(OS::OpenFileForReading(image_file.GetFilename().c_str())); |
| 407 | ASSERT_TRUE(file.get() != nullptr); |
| 408 | ImageHeader image_header; |
| 409 | ASSERT_EQ(file->ReadFully(&image_header, sizeof(image_header)), true); |
| 410 | ASSERT_TRUE(image_header.IsValid()); |
Vladimir Marko | cd87c3e | 2017-09-05 13:11:57 +0100 | [diff] [blame] | 411 | const auto& bitmap_section = image_header.GetImageBitmapSection(); |
Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 412 | ASSERT_GE(bitmap_section.Offset(), sizeof(image_header)); |
| 413 | ASSERT_NE(0U, bitmap_section.Size()); |
| 414 | |
| 415 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
| 416 | ASSERT_TRUE(heap->HaveContinuousSpaces()); |
| 417 | gc::space::ContinuousSpace* space = heap->GetNonMovingSpace(); |
| 418 | ASSERT_FALSE(space->IsImageSpace()); |
| 419 | ASSERT_TRUE(space != nullptr); |
| 420 | ASSERT_TRUE(space->IsMallocSpace()); |
| 421 | image_file_sizes.push_back(file->GetLength()); |
| 422 | } |
| 423 | |
Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 424 | // Need to delete the compiler since it has worker threads which are attached to runtime. |
| 425 | compiler_driver_.reset(); |
| 426 | |
| 427 | // Tear down old runtime before making a new one, clearing out misc state. |
| 428 | |
| 429 | // Remove the reservation of the memory for use to load the image. |
| 430 | // Need to do this before we reset the runtime. |
| 431 | UnreserveImageSpace(); |
| 432 | |
| 433 | helper.extra_dex_files.clear(); |
| 434 | runtime_.reset(); |
| 435 | java_lang_dex_file_ = nullptr; |
| 436 | |
| 437 | MemMap::Init(); |
| 438 | |
| 439 | RuntimeOptions options; |
Vladimir Marko | 7a85e70 | 2018-12-03 18:47:23 +0000 | [diff] [blame] | 440 | options.emplace_back(GetClassPathOption("-Xbootclasspath:", GetLibCoreDexFileNames()), nullptr); |
| 441 | options.emplace_back( |
| 442 | GetClassPathOption("-Xbootclasspath-locations:", GetLibCoreDexLocations()), nullptr); |
Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 443 | std::string image("-Ximage:"); |
| 444 | image.append(helper.image_locations[0].GetFilename()); |
| 445 | options.push_back(std::make_pair(image.c_str(), static_cast<void*>(nullptr))); |
| 446 | // By default the compiler this creates will not include patch information. |
| 447 | options.push_back(std::make_pair("-Xnorelocate", nullptr)); |
| 448 | |
| 449 | if (!Runtime::Create(options, false)) { |
| 450 | LOG(FATAL) << "Failed to create runtime"; |
| 451 | return; |
| 452 | } |
| 453 | runtime_.reset(Runtime::Current()); |
| 454 | // Runtime::Create acquired the mutator_lock_ that is normally given away when we Runtime::Start, |
| 455 | // give it away now and then switch to a more managable ScopedObjectAccess. |
| 456 | Thread::Current()->TransitionFromRunnableToSuspended(kNative); |
| 457 | ScopedObjectAccess soa(Thread::Current()); |
| 458 | ASSERT_TRUE(runtime_.get() != nullptr); |
| 459 | class_linker_ = runtime_->GetClassLinker(); |
| 460 | |
| 461 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
| 462 | ASSERT_TRUE(heap->HasBootImageSpace()); |
| 463 | ASSERT_TRUE(heap->GetNonMovingSpace()->IsMallocSpace()); |
| 464 | |
| 465 | // We loaded the runtime with an explicit image, so it must exist. |
| 466 | ASSERT_EQ(heap->GetBootImageSpaces().size(), image_file_sizes.size()); |
Vladimir Marko | dc4bcce | 2018-06-21 16:15:42 +0100 | [diff] [blame] | 467 | const HashSet<std::string>& image_classes = compiler_options_->GetImageClasses(); |
Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 468 | for (size_t i = 0; i < helper.dex_file_locations.size(); ++i) { |
| 469 | std::unique_ptr<const DexFile> dex( |
| 470 | LoadExpectSingleDexFile(helper.dex_file_locations[i].c_str())); |
| 471 | ASSERT_TRUE(dex != nullptr); |
| 472 | uint64_t image_file_size = image_file_sizes[i]; |
| 473 | gc::space::ImageSpace* image_space = heap->GetBootImageSpaces()[i]; |
| 474 | ASSERT_TRUE(image_space != nullptr); |
| 475 | if (storage_mode == ImageHeader::kStorageModeUncompressed) { |
| 476 | // Uncompressed, image should be smaller than file. |
| 477 | ASSERT_LE(image_space->GetImageHeader().GetImageSize(), image_file_size); |
| 478 | } else if (image_file_size > 16 * KB) { |
| 479 | // Compressed, file should be smaller than image. Not really valid for small images. |
| 480 | ASSERT_LE(image_file_size, image_space->GetImageHeader().GetImageSize()); |
Mathieu Chartier | 1a84296 | 2018-11-13 15:09:51 -0800 | [diff] [blame] | 481 | // TODO: Actually validate the blocks, this is hard since the blocks are not copied over for |
| 482 | // compressed images. Add kPageSize since image_size is rounded up to this. |
| 483 | ASSERT_GT(image_space->GetImageHeader().GetBlockCount() * max_image_block_size, |
| 484 | image_space->GetImageHeader().GetImageSize() - kPageSize); |
Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | image_space->VerifyImageAllocations(); |
| 488 | uint8_t* image_begin = image_space->Begin(); |
| 489 | uint8_t* image_end = image_space->End(); |
| 490 | if (i == 0) { |
| 491 | // This check is only valid for image 0. |
| 492 | CHECK_EQ(kRequestedImageBase, reinterpret_cast<uintptr_t>(image_begin)); |
| 493 | } |
| 494 | for (size_t j = 0; j < dex->NumClassDefs(); ++j) { |
| 495 | const DexFile::ClassDef& class_def = dex->GetClassDef(j); |
| 496 | const char* descriptor = dex->GetClassDescriptor(class_def); |
Vladimir Marko | e9987b0 | 2018-05-22 16:26:43 +0100 | [diff] [blame] | 497 | ObjPtr<mirror::Class> klass = class_linker_->FindSystemClass(soa.Self(), descriptor); |
Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 498 | EXPECT_TRUE(klass != nullptr) << descriptor; |
Vladimir Marko | e9987b0 | 2018-05-22 16:26:43 +0100 | [diff] [blame] | 499 | uint8_t* raw_klass = reinterpret_cast<uint8_t*>(klass.Ptr()); |
Vladimir Marko | 54159c6 | 2018-06-20 14:30:08 +0100 | [diff] [blame] | 500 | if (image_classes.find(StringPiece(descriptor)) == image_classes.end()) { |
Vladimir Marko | e9987b0 | 2018-05-22 16:26:43 +0100 | [diff] [blame] | 501 | EXPECT_TRUE(raw_klass >= image_end || raw_klass < image_begin) << descriptor; |
Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 502 | } else { |
| 503 | // Image classes should be located inside the image. |
Vladimir Marko | e9987b0 | 2018-05-22 16:26:43 +0100 | [diff] [blame] | 504 | EXPECT_LT(image_begin, raw_klass) << descriptor; |
| 505 | EXPECT_LT(raw_klass, image_end) << descriptor; |
Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 506 | } |
| 507 | EXPECT_TRUE(Monitor::IsValidLockWord(klass->GetLockWord(false))); |
| 508 | } |
| 509 | } |
| 510 | } |
| 511 | |
Vladimir Marko | 7452797 | 2016-11-29 15:57:32 +0000 | [diff] [blame] | 512 | } // namespace linker |
Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 513 | } // namespace art |
| 514 | |
Vladimir Marko | 7452797 | 2016-11-29 15:57:32 +0000 | [diff] [blame] | 515 | #endif // ART_DEX2OAT_LINKER_IMAGE_TEST_H_ |