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 | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 16 | |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 17 | #include "arch/instruction_set_features.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 18 | #include "art_method-inl.h" |
Mathieu Chartier | 07ddb6f | 2015-11-05 11:16:34 -0800 | [diff] [blame] | 19 | #include "base/unix_file/fd_file.h" |
Andreas Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 20 | #include "class_linker.h" |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 21 | #include "common_compiler_test.h" |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 22 | #include "compiled_method.h" |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 23 | #include "compiler.h" |
Mathieu Chartier | 5bdab12 | 2015-01-26 18:30:19 -0800 | [diff] [blame] | 24 | #include "dex/pass_manager.h" |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 25 | #include "dex/quick/dex_file_to_method_inliner_map.h" |
| 26 | #include "dex/quick_compiler_callbacks.h" |
Mathieu Chartier | 5bdab12 | 2015-01-26 18:30:19 -0800 | [diff] [blame] | 27 | #include "dex/verification_results.h" |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 28 | #include "driver/compiler_driver.h" |
| 29 | #include "driver/compiler_options.h" |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 30 | #include "dwarf/method_debug_info.h" |
| 31 | #include "elf_writer.h" |
| 32 | #include "elf_writer_quick.h" |
Vladimir Marko | 7624d25 | 2014-05-02 14:40:15 +0100 | [diff] [blame] | 33 | #include "entrypoints/quick/quick_entrypoints.h" |
Vladimir Marko | 131980f | 2015-12-03 18:29:23 +0000 | [diff] [blame] | 34 | #include "linker/vector_output_stream.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 35 | #include "mirror/class-inl.h" |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 36 | #include "mirror/object_array-inl.h" |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 37 | #include "mirror/object-inl.h" |
Vladimir Marko | 7624d25 | 2014-05-02 14:40:15 +0100 | [diff] [blame] | 38 | #include "oat_file-inl.h" |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 39 | #include "oat_writer.h" |
| 40 | #include "scoped_thread_state_change.h" |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 41 | #include "utils/test_dex_file_builder.h" |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 42 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 43 | namespace art { |
| 44 | |
Mathieu Chartier | 07ddb6f | 2015-11-05 11:16:34 -0800 | [diff] [blame] | 45 | NO_RETURN static void Usage(const char* fmt, ...) { |
| 46 | va_list ap; |
| 47 | va_start(ap, fmt); |
| 48 | std::string error; |
| 49 | StringAppendV(&error, fmt, ap); |
| 50 | LOG(FATAL) << error; |
| 51 | va_end(ap); |
| 52 | UNREACHABLE(); |
| 53 | } |
| 54 | |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 55 | class OatTest : public CommonCompilerTest { |
Logan Chien | eeb7edf | 2012-03-09 20:38:39 +0800 | [diff] [blame] | 56 | protected: |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 57 | static const bool kCompile = false; // DISABLED_ due to the time to compile libcore |
| 58 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 59 | void CheckMethod(ArtMethod* method, |
Logan Chien | eeb7edf | 2012-03-09 20:38:39 +0800 | [diff] [blame] | 60 | const OatFile::OatMethod& oat_method, |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 61 | const DexFile& dex_file) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 62 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Logan Chien | eeb7edf | 2012-03-09 20:38:39 +0800 | [diff] [blame] | 63 | const CompiledMethod* compiled_method = |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 64 | compiler_driver_->GetCompiledMethod(MethodReference(&dex_file, |
Brian Carlstrom | 51c2467 | 2013-07-11 16:00:56 -0700 | [diff] [blame] | 65 | method->GetDexMethodIndex())); |
Logan Chien | eeb7edf | 2012-03-09 20:38:39 +0800 | [diff] [blame] | 66 | |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 67 | if (compiled_method == nullptr) { |
| 68 | EXPECT_TRUE(oat_method.GetQuickCode() == nullptr) << PrettyMethod(method) << " " |
| 69 | << oat_method.GetQuickCode(); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 70 | EXPECT_EQ(oat_method.GetFrameSizeInBytes(), 0U); |
Logan Chien | eeb7edf | 2012-03-09 20:38:39 +0800 | [diff] [blame] | 71 | EXPECT_EQ(oat_method.GetCoreSpillMask(), 0U); |
| 72 | EXPECT_EQ(oat_method.GetFpSpillMask(), 0U); |
| 73 | } else { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 74 | const void* quick_oat_code = oat_method.GetQuickCode(); |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 75 | EXPECT_TRUE(quick_oat_code != nullptr) << PrettyMethod(method); |
| 76 | EXPECT_EQ(oat_method.GetFrameSizeInBytes(), compiled_method->GetFrameSizeInBytes()); |
| 77 | EXPECT_EQ(oat_method.GetCoreSpillMask(), compiled_method->GetCoreSpillMask()); |
| 78 | EXPECT_EQ(oat_method.GetFpSpillMask(), compiled_method->GetFpSpillMask()); |
| 79 | uintptr_t oat_code_aligned = RoundDown(reinterpret_cast<uintptr_t>(quick_oat_code), 2); |
| 80 | quick_oat_code = reinterpret_cast<const void*>(oat_code_aligned); |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 81 | ArrayRef<const uint8_t> quick_code = compiled_method->GetQuickCode(); |
| 82 | EXPECT_FALSE(quick_code.empty()); |
| 83 | size_t code_size = quick_code.size() * sizeof(quick_code[0]); |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 84 | EXPECT_EQ(0, memcmp(quick_oat_code, &quick_code[0], code_size)) |
| 85 | << PrettyMethod(method) << " " << code_size; |
| 86 | CHECK_EQ(0, memcmp(quick_oat_code, &quick_code[0], code_size)); |
Logan Chien | eeb7edf | 2012-03-09 20:38:39 +0800 | [diff] [blame] | 87 | } |
| 88 | } |
Mathieu Chartier | 07ddb6f | 2015-11-05 11:16:34 -0800 | [diff] [blame] | 89 | |
| 90 | void SetupCompiler(Compiler::Kind compiler_kind, |
| 91 | InstructionSet insn_set, |
| 92 | const std::vector<std::string>& compiler_options, |
| 93 | /*out*/std::string* error_msg) { |
| 94 | ASSERT_TRUE(error_msg != nullptr); |
| 95 | insn_features_.reset(InstructionSetFeatures::FromVariant(insn_set, "default", error_msg)); |
| 96 | ASSERT_TRUE(insn_features_ != nullptr) << error_msg; |
| 97 | compiler_options_.reset(new CompilerOptions); |
| 98 | for (const std::string& option : compiler_options) { |
| 99 | compiler_options_->ParseCompilerOption(option, Usage); |
| 100 | } |
| 101 | verification_results_.reset(new VerificationResults(compiler_options_.get())); |
| 102 | method_inliner_map_.reset(new DexFileToMethodInlinerMap); |
| 103 | callbacks_.reset(new QuickCompilerCallbacks(verification_results_.get(), |
| 104 | method_inliner_map_.get(), |
| 105 | CompilerCallbacks::CallbackMode::kCompileApp)); |
| 106 | Runtime::Current()->SetCompilerCallbacks(callbacks_.get()); |
| 107 | timer_.reset(new CumulativeLogger("Compilation times")); |
| 108 | compiler_driver_.reset(new CompilerDriver(compiler_options_.get(), |
| 109 | verification_results_.get(), |
| 110 | method_inliner_map_.get(), |
| 111 | compiler_kind, |
| 112 | insn_set, |
| 113 | insn_features_.get(), |
| 114 | false, |
| 115 | nullptr, |
| 116 | nullptr, |
| 117 | nullptr, |
| 118 | 2, |
| 119 | true, |
| 120 | true, |
Mathieu Chartier | 07ddb6f | 2015-11-05 11:16:34 -0800 | [diff] [blame] | 121 | timer_.get(), |
| 122 | -1, |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame] | 123 | nullptr, |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 124 | nullptr)); |
Mathieu Chartier | 07ddb6f | 2015-11-05 11:16:34 -0800 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | bool WriteElf(File* file, |
| 128 | const std::vector<const DexFile*>& dex_files, |
Andreas Gampe | 3a2bd29 | 2016-01-26 17:23:47 -0800 | [diff] [blame] | 129 | SafeMap<std::string, std::string>& key_value_store, |
| 130 | bool verify) { |
Mathieu Chartier | 07ddb6f | 2015-11-05 11:16:34 -0800 | [diff] [blame] | 131 | TimingLogger timings("WriteElf", false, false); |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 132 | OatWriter oat_writer(/*compiling_boot_image*/false, &timings); |
| 133 | for (const DexFile* dex_file : dex_files) { |
| 134 | ArrayRef<const uint8_t> raw_dex_file( |
| 135 | reinterpret_cast<const uint8_t*>(&dex_file->GetHeader()), |
| 136 | dex_file->GetHeader().file_size_); |
| 137 | if (!oat_writer.AddRawDexFileSource(raw_dex_file, |
| 138 | dex_file->GetLocation().c_str(), |
| 139 | dex_file->GetLocationChecksum())) { |
| 140 | return false; |
| 141 | } |
| 142 | } |
Andreas Gampe | 3a2bd29 | 2016-01-26 17:23:47 -0800 | [diff] [blame] | 143 | return DoWriteElf(file, oat_writer, key_value_store, verify); |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | bool WriteElf(File* file, |
| 147 | const std::vector<const char*>& dex_filenames, |
Andreas Gampe | 3a2bd29 | 2016-01-26 17:23:47 -0800 | [diff] [blame] | 148 | SafeMap<std::string, std::string>& key_value_store, |
| 149 | bool verify) { |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 150 | TimingLogger timings("WriteElf", false, false); |
| 151 | OatWriter oat_writer(/*compiling_boot_image*/false, &timings); |
| 152 | for (const char* dex_filename : dex_filenames) { |
| 153 | if (!oat_writer.AddDexFileSource(dex_filename, dex_filename)) { |
| 154 | return false; |
| 155 | } |
| 156 | } |
Andreas Gampe | 3a2bd29 | 2016-01-26 17:23:47 -0800 | [diff] [blame] | 157 | return DoWriteElf(file, oat_writer, key_value_store, verify); |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | bool WriteElf(File* file, |
| 161 | ScopedFd&& zip_fd, |
| 162 | const char* location, |
Andreas Gampe | 3a2bd29 | 2016-01-26 17:23:47 -0800 | [diff] [blame] | 163 | SafeMap<std::string, std::string>& key_value_store, |
| 164 | bool verify) { |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 165 | TimingLogger timings("WriteElf", false, false); |
| 166 | OatWriter oat_writer(/*compiling_boot_image*/false, &timings); |
| 167 | if (!oat_writer.AddZippedDexFilesSource(std::move(zip_fd), location)) { |
| 168 | return false; |
| 169 | } |
Andreas Gampe | 3a2bd29 | 2016-01-26 17:23:47 -0800 | [diff] [blame] | 170 | return DoWriteElf(file, oat_writer, key_value_store, verify); |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | bool DoWriteElf(File* file, |
| 174 | OatWriter& oat_writer, |
Andreas Gampe | 3a2bd29 | 2016-01-26 17:23:47 -0800 | [diff] [blame] | 175 | SafeMap<std::string, std::string>& key_value_store, |
| 176 | bool verify) { |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 177 | std::unique_ptr<ElfWriter> elf_writer = CreateElfWriterQuick( |
| 178 | compiler_driver_->GetInstructionSet(), |
| 179 | &compiler_driver_->GetCompilerOptions(), |
| 180 | file); |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 181 | elf_writer->Start(); |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 182 | OutputStream* rodata = elf_writer->StartRoData(); |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 183 | std::unique_ptr<MemMap> opened_dex_files_map; |
| 184 | std::vector<std::unique_ptr<const DexFile>> opened_dex_files; |
| 185 | if (!oat_writer.WriteAndOpenDexFiles(rodata, |
| 186 | file, |
| 187 | compiler_driver_->GetInstructionSet(), |
| 188 | compiler_driver_->GetInstructionSetFeatures(), |
| 189 | &key_value_store, |
Andreas Gampe | 3a2bd29 | 2016-01-26 17:23:47 -0800 | [diff] [blame] | 190 | verify, |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 191 | &opened_dex_files_map, |
| 192 | &opened_dex_files)) { |
| 193 | return false; |
| 194 | } |
| 195 | Runtime* runtime = Runtime::Current(); |
| 196 | ClassLinker* const class_linker = runtime->GetClassLinker(); |
| 197 | std::vector<const DexFile*> dex_files; |
| 198 | for (const std::unique_ptr<const DexFile>& dex_file : opened_dex_files) { |
| 199 | dex_files.push_back(dex_file.get()); |
| 200 | ScopedObjectAccess soa(Thread::Current()); |
| 201 | class_linker->RegisterDexFile(*dex_file, runtime->GetLinearAlloc()); |
| 202 | } |
| 203 | oat_writer.PrepareLayout(compiler_driver_.get(), nullptr, dex_files); |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 204 | if (!oat_writer.WriteRodata(rodata)) { |
| 205 | return false; |
| 206 | } |
| 207 | elf_writer->EndRoData(rodata); |
| 208 | |
| 209 | OutputStream* text = elf_writer->StartText(); |
| 210 | if (!oat_writer.WriteCode(text)) { |
| 211 | return false; |
| 212 | } |
| 213 | elf_writer->EndText(text); |
| 214 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 215 | if (!oat_writer.WriteHeader(elf_writer->GetStream(), 42U, 4096U, 0)) { |
| 216 | return false; |
| 217 | } |
| 218 | |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 219 | elf_writer->SetBssSize(oat_writer.GetBssSize()); |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 220 | elf_writer->WriteDynamicSection(); |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 221 | elf_writer->WriteDebugInfo(oat_writer.GetMethodDebugInfo()); |
| 222 | elf_writer->WritePatchLocations(oat_writer.GetAbsolutePatchLocations()); |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 223 | |
| 224 | return elf_writer->End(); |
Mathieu Chartier | 07ddb6f | 2015-11-05 11:16:34 -0800 | [diff] [blame] | 225 | } |
| 226 | |
Andreas Gampe | 3a2bd29 | 2016-01-26 17:23:47 -0800 | [diff] [blame] | 227 | void TestDexFileInput(bool verify); |
| 228 | void TestZipFileInput(bool verify); |
| 229 | |
Mathieu Chartier | 07ddb6f | 2015-11-05 11:16:34 -0800 | [diff] [blame] | 230 | std::unique_ptr<const InstructionSetFeatures> insn_features_; |
| 231 | std::unique_ptr<QuickCompilerCallbacks> callbacks_; |
Logan Chien | eeb7edf | 2012-03-09 20:38:39 +0800 | [diff] [blame] | 232 | }; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 233 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 234 | class ZipBuilder { |
| 235 | public: |
| 236 | explicit ZipBuilder(File* zip_file) : zip_file_(zip_file) { } |
| 237 | |
| 238 | bool AddFile(const char* location, const void* data, size_t size) { |
| 239 | off_t offset = lseek(zip_file_->Fd(), 0, SEEK_CUR); |
| 240 | if (offset == static_cast<off_t>(-1)) { |
| 241 | return false; |
| 242 | } |
| 243 | |
| 244 | ZipFileHeader file_header; |
| 245 | file_header.crc32 = crc32(0u, reinterpret_cast<const Bytef*>(data), size); |
| 246 | file_header.compressed_size = size; |
| 247 | file_header.uncompressed_size = size; |
| 248 | file_header.filename_length = strlen(location); |
| 249 | |
| 250 | if (!zip_file_->WriteFully(&file_header, sizeof(file_header)) || |
| 251 | !zip_file_->WriteFully(location, file_header.filename_length) || |
| 252 | !zip_file_->WriteFully(data, size)) { |
| 253 | return false; |
| 254 | } |
| 255 | |
| 256 | CentralDirectoryFileHeader cdfh; |
| 257 | cdfh.crc32 = file_header.crc32; |
| 258 | cdfh.compressed_size = size; |
| 259 | cdfh.uncompressed_size = size; |
| 260 | cdfh.filename_length = file_header.filename_length; |
| 261 | cdfh.relative_offset_of_local_file_header = offset; |
| 262 | file_data_.push_back(FileData { cdfh, location }); |
| 263 | return true; |
| 264 | } |
| 265 | |
| 266 | bool Finish() { |
| 267 | off_t offset = lseek(zip_file_->Fd(), 0, SEEK_CUR); |
| 268 | if (offset == static_cast<off_t>(-1)) { |
| 269 | return false; |
| 270 | } |
| 271 | |
| 272 | size_t central_directory_size = 0u; |
| 273 | for (const FileData& file_data : file_data_) { |
| 274 | if (!zip_file_->WriteFully(&file_data.cdfh, sizeof(file_data.cdfh)) || |
| 275 | !zip_file_->WriteFully(file_data.location, file_data.cdfh.filename_length)) { |
| 276 | return false; |
| 277 | } |
| 278 | central_directory_size += sizeof(file_data.cdfh) + file_data.cdfh.filename_length; |
| 279 | } |
| 280 | EndOfCentralDirectoryRecord eocd_record; |
| 281 | eocd_record.number_of_central_directory_records_on_this_disk = file_data_.size(); |
| 282 | eocd_record.total_number_of_central_directory_records = file_data_.size(); |
| 283 | eocd_record.size_of_central_directory = central_directory_size; |
| 284 | eocd_record.offset_of_start_of_central_directory = offset; |
| 285 | return |
| 286 | zip_file_->WriteFully(&eocd_record, sizeof(eocd_record)) && |
| 287 | zip_file_->Flush() == 0; |
| 288 | } |
| 289 | |
| 290 | private: |
| 291 | struct PACKED(1) ZipFileHeader { |
| 292 | uint32_t signature = 0x04034b50; |
| 293 | uint16_t version_needed_to_extract = 10; |
| 294 | uint16_t general_purpose_bit_flag = 0; |
| 295 | uint16_t compression_method = 0; // 0 = store only. |
| 296 | uint16_t file_last_modification_time = 0u; |
| 297 | uint16_t file_last_modification_date = 0u; |
| 298 | uint32_t crc32; |
| 299 | uint32_t compressed_size; |
| 300 | uint32_t uncompressed_size; |
| 301 | uint16_t filename_length; |
| 302 | uint16_t extra_field_length = 0u; // No extra fields. |
| 303 | }; |
| 304 | |
| 305 | struct PACKED(1) CentralDirectoryFileHeader { |
| 306 | uint32_t signature = 0x02014b50; |
| 307 | uint16_t version_made_by = 10; |
| 308 | uint16_t version_needed_to_extract = 10; |
| 309 | uint16_t general_purpose_bit_flag = 0; |
| 310 | uint16_t compression_method = 0; // 0 = store only. |
| 311 | uint16_t file_last_modification_time = 0u; |
| 312 | uint16_t file_last_modification_date = 0u; |
| 313 | uint32_t crc32; |
| 314 | uint32_t compressed_size; |
| 315 | uint32_t uncompressed_size; |
| 316 | uint16_t filename_length; |
| 317 | uint16_t extra_field_length = 0u; // No extra fields. |
| 318 | uint16_t file_comment_length = 0u; // No file comment. |
| 319 | uint16_t disk_number_where_file_starts = 0u; |
| 320 | uint16_t internal_file_attributes = 0u; |
| 321 | uint32_t external_file_attributes = 0u; |
| 322 | uint32_t relative_offset_of_local_file_header; |
| 323 | }; |
| 324 | |
| 325 | struct PACKED(1) EndOfCentralDirectoryRecord { |
| 326 | uint32_t signature = 0x06054b50; |
| 327 | uint16_t number_of_this_disk = 0u; |
| 328 | uint16_t disk_where_central_directory_starts = 0u; |
| 329 | uint16_t number_of_central_directory_records_on_this_disk; |
| 330 | uint16_t total_number_of_central_directory_records; |
| 331 | uint32_t size_of_central_directory; |
| 332 | uint32_t offset_of_start_of_central_directory; |
| 333 | uint16_t comment_length = 0u; // No file comment. |
| 334 | }; |
| 335 | |
| 336 | struct FileData { |
| 337 | CentralDirectoryFileHeader cdfh; |
| 338 | const char* location; |
| 339 | }; |
| 340 | |
| 341 | File* zip_file_; |
| 342 | std::vector<FileData> file_data_; |
| 343 | }; |
| 344 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 345 | TEST_F(OatTest, WriteRead) { |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 346 | TimingLogger timings("OatTest::WriteRead", false, false); |
Jesse Wilson | 254db0f | 2011-11-16 16:44:11 -0500 | [diff] [blame] | 347 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 348 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 349 | // TODO: make selectable. |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 350 | Compiler::Kind compiler_kind = Compiler::kQuick; |
Ian Rogers | a073c67 | 2013-07-30 17:43:55 -0700 | [diff] [blame] | 351 | InstructionSet insn_set = kIsTargetBuild ? kThumb2 : kX86; |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 352 | std::string error_msg; |
Mathieu Chartier | 07ddb6f | 2015-11-05 11:16:34 -0800 | [diff] [blame] | 353 | SetupCompiler(compiler_kind, insn_set, std::vector<std::string>(), /*out*/ &error_msg); |
| 354 | |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 355 | jobject class_loader = nullptr; |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 356 | if (kCompile) { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 357 | TimingLogger timings2("OatTest::WriteRead", false, false); |
Vladimir Marko | d1eaf0d | 2015-10-29 12:18:29 +0000 | [diff] [blame] | 358 | compiler_driver_->SetDexFilesForOatFile(class_linker->GetBootClassPath()); |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 359 | compiler_driver_->CompileAll(class_loader, class_linker->GetBootClassPath(), &timings2); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | ScratchFile tmp; |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 363 | SafeMap<std::string, std::string> key_value_store; |
| 364 | key_value_store.Put(OatHeader::kImageLocationKey, "lue.art"); |
Andreas Gampe | 3a2bd29 | 2016-01-26 17:23:47 -0800 | [diff] [blame] | 365 | bool success = WriteElf(tmp.GetFile(), class_linker->GetBootClassPath(), key_value_store, false); |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 366 | ASSERT_TRUE(success); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 367 | |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 368 | if (kCompile) { // OatWriter strips the code, regenerate to compare |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 369 | compiler_driver_->CompileAll(class_loader, class_linker->GetBootClassPath(), &timings); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 370 | } |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 371 | std::unique_ptr<OatFile> oat_file(OatFile::Open(tmp.GetFilename(), tmp.GetFilename(), nullptr, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 372 | nullptr, false, nullptr, &error_msg)); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 373 | ASSERT_TRUE(oat_file.get() != nullptr) << error_msg; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 374 | const OatHeader& oat_header = oat_file->GetOatHeader(); |
Brian Carlstrom | f852fb2 | 2012-10-19 11:01:58 -0700 | [diff] [blame] | 375 | ASSERT_TRUE(oat_header.IsValid()); |
Yi Kong | 9515b51 | 2015-11-11 14:36:07 +0000 | [diff] [blame] | 376 | ASSERT_EQ(class_linker->GetBootClassPath().size(), oat_header.GetDexFileCount()); // core |
Brian Carlstrom | 28db012 | 2012-10-18 16:20:41 -0700 | [diff] [blame] | 377 | ASSERT_EQ(42U, oat_header.GetImageFileLocationOatChecksum()); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 378 | ASSERT_EQ(4096U, oat_header.GetImageFileLocationOatDataBegin()); |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 379 | ASSERT_EQ("lue.art", std::string(oat_header.GetStoreValueByKey(OatHeader::kImageLocationKey))); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 380 | |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 381 | ASSERT_TRUE(java_lang_dex_file_ != nullptr); |
| 382 | const DexFile& dex_file = *java_lang_dex_file_; |
| 383 | uint32_t dex_file_checksum = dex_file.GetLocationChecksum(); |
| 384 | const OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(dex_file.GetLocation().c_str(), |
Brian Carlstrom | 756ee4e | 2013-10-03 15:46:12 -0700 | [diff] [blame] | 385 | &dex_file_checksum); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 386 | ASSERT_TRUE(oat_dex_file != nullptr); |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 387 | CHECK_EQ(dex_file.GetLocationChecksum(), oat_dex_file->GetDexFileLocationChecksum()); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 388 | ScopedObjectAccess soa(Thread::Current()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 389 | auto pointer_size = class_linker->GetImagePointerSize(); |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 390 | for (size_t i = 0; i < dex_file.NumClassDefs(); i++) { |
| 391 | const DexFile::ClassDef& class_def = dex_file.GetClassDef(i); |
| 392 | const uint8_t* class_data = dex_file.GetClassData(class_def); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 393 | |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 394 | size_t num_virtual_methods = 0; |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 395 | if (class_data != nullptr) { |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 396 | ClassDataItemIterator it(dex_file, class_data); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 397 | num_virtual_methods = it.NumVirtualMethods(); |
| 398 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 399 | |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 400 | const char* descriptor = dex_file.GetClassDescriptor(class_def); |
Mathieu Chartier | 9865bde | 2015-12-21 09:58:16 -0800 | [diff] [blame] | 401 | mirror::Class* klass = class_linker->FindClass(soa.Self(), |
| 402 | descriptor, |
| 403 | ScopedNullHandle<mirror::ClassLoader>()); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 404 | |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 405 | const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(i); |
| 406 | CHECK_EQ(mirror::Class::Status::kStatusNotReady, oat_class.GetStatus()) << descriptor; |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 407 | CHECK_EQ(kCompile ? OatClassType::kOatClassAllCompiled : OatClassType::kOatClassNoneCompiled, |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 408 | oat_class.GetType()) << descriptor; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 409 | |
| 410 | size_t method_index = 0; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 411 | for (auto& m : klass->GetDirectMethods(pointer_size)) { |
| 412 | CheckMethod(&m, oat_class.GetOatMethod(method_index), dex_file); |
| 413 | ++method_index; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 414 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 415 | size_t visited_virtuals = 0; |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 416 | // TODO We should also check copied methods in this test. |
| 417 | for (auto& m : klass->GetDeclaredVirtualMethods(pointer_size)) { |
| 418 | EXPECT_FALSE(m.IsMiranda()); |
| 419 | CheckMethod(&m, oat_class.GetOatMethod(method_index), dex_file); |
| 420 | ++method_index; |
| 421 | ++visited_virtuals; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 422 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 423 | EXPECT_EQ(visited_virtuals, num_virtual_methods); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 424 | } |
| 425 | } |
| 426 | |
Brian Carlstrom | 341df94 | 2012-06-27 12:29:22 -0700 | [diff] [blame] | 427 | TEST_F(OatTest, OatHeaderSizeCheck) { |
| 428 | // If this test is failing and you have to update these constants, |
| 429 | // it is time to update OatHeader::kOatVersion |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 430 | EXPECT_EQ(72U, sizeof(OatHeader)); |
Mathieu Chartier | 957ca1c | 2014-11-21 16:51:29 -0800 | [diff] [blame] | 431 | EXPECT_EQ(4U, sizeof(OatMethodOffsets)); |
| 432 | EXPECT_EQ(28U, sizeof(OatQuickMethodHeader)); |
Roland Levillain | 7c1559a | 2015-12-15 10:55:36 +0000 | [diff] [blame] | 433 | EXPECT_EQ(132 * GetInstructionSetPointerSize(kRuntimeISA), sizeof(QuickEntryPoints)); |
Brian Carlstrom | 341df94 | 2012-06-27 12:29:22 -0700 | [diff] [blame] | 434 | } |
| 435 | |
Brian Carlstrom | f852fb2 | 2012-10-19 11:01:58 -0700 | [diff] [blame] | 436 | TEST_F(OatTest, OatHeaderIsValid) { |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 437 | InstructionSet insn_set = kX86; |
| 438 | std::string error_msg; |
| 439 | std::unique_ptr<const InstructionSetFeatures> insn_features( |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 440 | InstructionSetFeatures::FromVariant(insn_set, "default", &error_msg)); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 441 | ASSERT_TRUE(insn_features.get() != nullptr) << error_msg; |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 442 | std::unique_ptr<OatHeader> oat_header(OatHeader::Create(insn_set, |
| 443 | insn_features.get(), |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 444 | 0u, |
Andreas Gampe | 928f72b | 2014-09-09 19:53:48 -0700 | [diff] [blame] | 445 | nullptr)); |
| 446 | ASSERT_NE(oat_header.get(), nullptr); |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 447 | ASSERT_TRUE(oat_header->IsValid()); |
Brian Carlstrom | f852fb2 | 2012-10-19 11:01:58 -0700 | [diff] [blame] | 448 | |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 449 | char* magic = const_cast<char*>(oat_header->GetMagic()); |
Brian Carlstrom | f852fb2 | 2012-10-19 11:01:58 -0700 | [diff] [blame] | 450 | strcpy(magic, ""); // bad magic |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 451 | ASSERT_FALSE(oat_header->IsValid()); |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 452 | strcpy(magic, "oat\n000"); // bad version |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 453 | ASSERT_FALSE(oat_header->IsValid()); |
Brian Carlstrom | f852fb2 | 2012-10-19 11:01:58 -0700 | [diff] [blame] | 454 | } |
| 455 | |
Mathieu Chartier | 07ddb6f | 2015-11-05 11:16:34 -0800 | [diff] [blame] | 456 | TEST_F(OatTest, EmptyTextSection) { |
| 457 | TimingLogger timings("OatTest::EmptyTextSection", false, false); |
| 458 | |
| 459 | // TODO: make selectable. |
| 460 | Compiler::Kind compiler_kind = Compiler::kQuick; |
| 461 | InstructionSet insn_set = kRuntimeISA; |
| 462 | if (insn_set == kArm) insn_set = kThumb2; |
| 463 | std::string error_msg; |
| 464 | std::vector<std::string> compiler_options; |
| 465 | compiler_options.push_back("--compiler-filter=verify-at-runtime"); |
| 466 | SetupCompiler(compiler_kind, insn_set, compiler_options, /*out*/ &error_msg); |
| 467 | |
| 468 | jobject class_loader; |
| 469 | { |
| 470 | ScopedObjectAccess soa(Thread::Current()); |
| 471 | class_loader = LoadDex("Main"); |
| 472 | } |
| 473 | ASSERT_TRUE(class_loader != nullptr); |
| 474 | std::vector<const DexFile*> dex_files = GetDexFiles(class_loader); |
| 475 | ASSERT_TRUE(!dex_files.empty()); |
| 476 | |
| 477 | ClassLinker* const class_linker = Runtime::Current()->GetClassLinker(); |
| 478 | for (const DexFile* dex_file : dex_files) { |
| 479 | ScopedObjectAccess soa(Thread::Current()); |
| 480 | class_linker->RegisterDexFile( |
| 481 | *dex_file, |
| 482 | class_linker->GetOrCreateAllocatorForClassLoader( |
| 483 | soa.Decode<mirror::ClassLoader*>(class_loader))); |
| 484 | } |
| 485 | compiler_driver_->SetDexFilesForOatFile(dex_files); |
| 486 | compiler_driver_->CompileAll(class_loader, dex_files, &timings); |
| 487 | |
| 488 | ScratchFile tmp; |
| 489 | SafeMap<std::string, std::string> key_value_store; |
| 490 | key_value_store.Put(OatHeader::kImageLocationKey, "test.art"); |
Andreas Gampe | 3a2bd29 | 2016-01-26 17:23:47 -0800 | [diff] [blame] | 491 | bool success = WriteElf(tmp.GetFile(), dex_files, key_value_store, false); |
Mathieu Chartier | 07ddb6f | 2015-11-05 11:16:34 -0800 | [diff] [blame] | 492 | ASSERT_TRUE(success); |
| 493 | |
| 494 | std::unique_ptr<OatFile> oat_file(OatFile::Open(tmp.GetFilename(), |
| 495 | tmp.GetFilename(), |
| 496 | nullptr, |
| 497 | nullptr, |
| 498 | false, |
| 499 | nullptr, |
| 500 | &error_msg)); |
| 501 | ASSERT_TRUE(oat_file != nullptr); |
| 502 | EXPECT_LT(static_cast<size_t>(oat_file->Size()), static_cast<size_t>(tmp.GetFile()->GetLength())); |
| 503 | } |
| 504 | |
Andreas Gampe | 3a2bd29 | 2016-01-26 17:23:47 -0800 | [diff] [blame] | 505 | static void MaybeModifyDexFileToFail(bool verify, std::unique_ptr<const DexFile>& data) { |
| 506 | // If in verify mode (= fail the verifier mode), make sure we fail early. We'll fail already |
| 507 | // because of the missing map, but that may lead to out of bounds reads. |
| 508 | if (verify) { |
| 509 | const_cast<DexFile::Header*>(&data->GetHeader())->checksum_++; |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | void OatTest::TestDexFileInput(bool verify) { |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 514 | TimingLogger timings("OatTest::DexFileInput", false, false); |
| 515 | |
| 516 | std::vector<const char*> input_filenames; |
| 517 | |
| 518 | ScratchFile dex_file1; |
| 519 | TestDexFileBuilder builder1; |
| 520 | builder1.AddField("Lsome.TestClass;", "int", "someField"); |
| 521 | builder1.AddMethod("Lsome.TestClass;", "()I", "foo"); |
| 522 | std::unique_ptr<const DexFile> dex_file1_data = builder1.Build(dex_file1.GetFilename()); |
Andreas Gampe | 3a2bd29 | 2016-01-26 17:23:47 -0800 | [diff] [blame] | 523 | |
| 524 | MaybeModifyDexFileToFail(verify, dex_file1_data); |
| 525 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 526 | bool success = dex_file1.GetFile()->WriteFully(&dex_file1_data->GetHeader(), |
| 527 | dex_file1_data->GetHeader().file_size_); |
| 528 | ASSERT_TRUE(success); |
| 529 | success = dex_file1.GetFile()->Flush() == 0; |
| 530 | ASSERT_TRUE(success); |
| 531 | input_filenames.push_back(dex_file1.GetFilename().c_str()); |
| 532 | |
| 533 | ScratchFile dex_file2; |
| 534 | TestDexFileBuilder builder2; |
| 535 | builder2.AddField("Land.AnotherTestClass;", "boolean", "someOtherField"); |
| 536 | builder2.AddMethod("Land.AnotherTestClass;", "()J", "bar"); |
| 537 | std::unique_ptr<const DexFile> dex_file2_data = builder2.Build(dex_file2.GetFilename()); |
Andreas Gampe | 3a2bd29 | 2016-01-26 17:23:47 -0800 | [diff] [blame] | 538 | |
| 539 | MaybeModifyDexFileToFail(verify, dex_file2_data); |
| 540 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 541 | success = dex_file2.GetFile()->WriteFully(&dex_file2_data->GetHeader(), |
| 542 | dex_file2_data->GetHeader().file_size_); |
| 543 | ASSERT_TRUE(success); |
| 544 | success = dex_file2.GetFile()->Flush() == 0; |
| 545 | ASSERT_TRUE(success); |
| 546 | input_filenames.push_back(dex_file2.GetFilename().c_str()); |
| 547 | |
| 548 | ScratchFile oat_file; |
| 549 | SafeMap<std::string, std::string> key_value_store; |
| 550 | key_value_store.Put(OatHeader::kImageLocationKey, "test.art"); |
Andreas Gampe | 3a2bd29 | 2016-01-26 17:23:47 -0800 | [diff] [blame] | 551 | success = WriteElf(oat_file.GetFile(), input_filenames, key_value_store, verify); |
| 552 | |
| 553 | // In verify mode, we expect failure. |
| 554 | if (verify) { |
| 555 | ASSERT_FALSE(success); |
| 556 | return; |
| 557 | } |
| 558 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 559 | ASSERT_TRUE(success); |
| 560 | |
| 561 | std::string error_msg; |
| 562 | std::unique_ptr<OatFile> opened_oat_file(OatFile::Open(oat_file.GetFilename(), |
| 563 | oat_file.GetFilename(), |
| 564 | nullptr, |
| 565 | nullptr, |
| 566 | false, |
| 567 | nullptr, |
| 568 | &error_msg)); |
| 569 | ASSERT_TRUE(opened_oat_file != nullptr); |
| 570 | ASSERT_EQ(2u, opened_oat_file->GetOatDexFiles().size()); |
| 571 | std::unique_ptr<const DexFile> opened_dex_file1 = |
| 572 | opened_oat_file->GetOatDexFiles()[0]->OpenDexFile(&error_msg); |
| 573 | std::unique_ptr<const DexFile> opened_dex_file2 = |
| 574 | opened_oat_file->GetOatDexFiles()[1]->OpenDexFile(&error_msg); |
| 575 | |
| 576 | ASSERT_EQ(dex_file1_data->GetHeader().file_size_, opened_dex_file1->GetHeader().file_size_); |
| 577 | ASSERT_EQ(0, memcmp(&dex_file1_data->GetHeader(), |
| 578 | &opened_dex_file1->GetHeader(), |
| 579 | dex_file1_data->GetHeader().file_size_)); |
| 580 | ASSERT_EQ(dex_file1_data->GetLocation(), opened_dex_file1->GetLocation()); |
| 581 | |
| 582 | ASSERT_EQ(dex_file2_data->GetHeader().file_size_, opened_dex_file2->GetHeader().file_size_); |
| 583 | ASSERT_EQ(0, memcmp(&dex_file2_data->GetHeader(), |
| 584 | &opened_dex_file2->GetHeader(), |
| 585 | dex_file2_data->GetHeader().file_size_)); |
| 586 | ASSERT_EQ(dex_file2_data->GetLocation(), opened_dex_file2->GetLocation()); |
| 587 | } |
| 588 | |
Andreas Gampe | 3a2bd29 | 2016-01-26 17:23:47 -0800 | [diff] [blame] | 589 | TEST_F(OatTest, DexFileInputCheckOutput) { |
| 590 | TestDexFileInput(false); |
| 591 | } |
| 592 | |
| 593 | TEST_F(OatTest, DexFileInputCheckVerifier) { |
| 594 | TestDexFileInput(true); |
| 595 | } |
| 596 | |
| 597 | void OatTest::TestZipFileInput(bool verify) { |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 598 | TimingLogger timings("OatTest::DexFileInput", false, false); |
| 599 | |
| 600 | ScratchFile zip_file; |
| 601 | ZipBuilder zip_builder(zip_file.GetFile()); |
| 602 | |
| 603 | ScratchFile dex_file1; |
| 604 | TestDexFileBuilder builder1; |
| 605 | builder1.AddField("Lsome.TestClass;", "long", "someField"); |
| 606 | builder1.AddMethod("Lsome.TestClass;", "()D", "foo"); |
| 607 | std::unique_ptr<const DexFile> dex_file1_data = builder1.Build(dex_file1.GetFilename()); |
Andreas Gampe | 3a2bd29 | 2016-01-26 17:23:47 -0800 | [diff] [blame] | 608 | |
| 609 | MaybeModifyDexFileToFail(verify, dex_file1_data); |
| 610 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 611 | bool success = dex_file1.GetFile()->WriteFully(&dex_file1_data->GetHeader(), |
| 612 | dex_file1_data->GetHeader().file_size_); |
| 613 | ASSERT_TRUE(success); |
| 614 | success = dex_file1.GetFile()->Flush() == 0; |
| 615 | ASSERT_TRUE(success); |
| 616 | success = zip_builder.AddFile("classes.dex", |
| 617 | &dex_file1_data->GetHeader(), |
| 618 | dex_file1_data->GetHeader().file_size_); |
| 619 | ASSERT_TRUE(success); |
| 620 | |
| 621 | ScratchFile dex_file2; |
| 622 | TestDexFileBuilder builder2; |
| 623 | builder2.AddField("Land.AnotherTestClass;", "boolean", "someOtherField"); |
| 624 | builder2.AddMethod("Land.AnotherTestClass;", "()J", "bar"); |
| 625 | std::unique_ptr<const DexFile> dex_file2_data = builder2.Build(dex_file2.GetFilename()); |
Andreas Gampe | 3a2bd29 | 2016-01-26 17:23:47 -0800 | [diff] [blame] | 626 | |
| 627 | MaybeModifyDexFileToFail(verify, dex_file2_data); |
| 628 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 629 | success = dex_file2.GetFile()->WriteFully(&dex_file2_data->GetHeader(), |
| 630 | dex_file2_data->GetHeader().file_size_); |
| 631 | ASSERT_TRUE(success); |
| 632 | success = dex_file2.GetFile()->Flush() == 0; |
| 633 | ASSERT_TRUE(success); |
| 634 | success = zip_builder.AddFile("classes2.dex", |
| 635 | &dex_file2_data->GetHeader(), |
| 636 | dex_file2_data->GetHeader().file_size_); |
| 637 | ASSERT_TRUE(success); |
| 638 | |
| 639 | success = zip_builder.Finish(); |
| 640 | ASSERT_TRUE(success) << strerror(errno); |
| 641 | |
| 642 | SafeMap<std::string, std::string> key_value_store; |
| 643 | key_value_store.Put(OatHeader::kImageLocationKey, "test.art"); |
| 644 | { |
| 645 | // Test using the AddDexFileSource() interface with the zip file. |
| 646 | std::vector<const char*> input_filenames { zip_file.GetFilename().c_str() }; // NOLINT [readability/braces] [4] |
| 647 | |
| 648 | ScratchFile oat_file; |
Andreas Gampe | 3a2bd29 | 2016-01-26 17:23:47 -0800 | [diff] [blame] | 649 | success = WriteElf(oat_file.GetFile(), input_filenames, key_value_store, verify); |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 650 | |
Andreas Gampe | 3a2bd29 | 2016-01-26 17:23:47 -0800 | [diff] [blame] | 651 | if (verify) { |
| 652 | ASSERT_FALSE(success); |
| 653 | } else { |
| 654 | ASSERT_TRUE(success); |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 655 | |
Andreas Gampe | 3a2bd29 | 2016-01-26 17:23:47 -0800 | [diff] [blame] | 656 | std::string error_msg; |
| 657 | std::unique_ptr<OatFile> opened_oat_file(OatFile::Open(oat_file.GetFilename(), |
| 658 | oat_file.GetFilename(), |
| 659 | nullptr, |
| 660 | nullptr, |
| 661 | false, |
| 662 | nullptr, |
| 663 | &error_msg)); |
| 664 | ASSERT_TRUE(opened_oat_file != nullptr); |
| 665 | ASSERT_EQ(2u, opened_oat_file->GetOatDexFiles().size()); |
| 666 | std::unique_ptr<const DexFile> opened_dex_file1 = |
| 667 | opened_oat_file->GetOatDexFiles()[0]->OpenDexFile(&error_msg); |
| 668 | std::unique_ptr<const DexFile> opened_dex_file2 = |
| 669 | opened_oat_file->GetOatDexFiles()[1]->OpenDexFile(&error_msg); |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 670 | |
Andreas Gampe | 3a2bd29 | 2016-01-26 17:23:47 -0800 | [diff] [blame] | 671 | ASSERT_EQ(dex_file1_data->GetHeader().file_size_, opened_dex_file1->GetHeader().file_size_); |
| 672 | ASSERT_EQ(0, memcmp(&dex_file1_data->GetHeader(), |
| 673 | &opened_dex_file1->GetHeader(), |
| 674 | dex_file1_data->GetHeader().file_size_)); |
| 675 | ASSERT_EQ(DexFile::GetMultiDexLocation(0, zip_file.GetFilename().c_str()), |
| 676 | opened_dex_file1->GetLocation()); |
| 677 | |
| 678 | ASSERT_EQ(dex_file2_data->GetHeader().file_size_, opened_dex_file2->GetHeader().file_size_); |
| 679 | ASSERT_EQ(0, memcmp(&dex_file2_data->GetHeader(), |
| 680 | &opened_dex_file2->GetHeader(), |
| 681 | dex_file2_data->GetHeader().file_size_)); |
| 682 | ASSERT_EQ(DexFile::GetMultiDexLocation(1, zip_file.GetFilename().c_str()), |
| 683 | opened_dex_file2->GetLocation()); |
| 684 | } |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | { |
| 688 | // Test using the AddZipDexFileSource() interface with the zip file handle. |
| 689 | ScopedFd zip_fd(dup(zip_file.GetFd())); |
| 690 | ASSERT_NE(-1, zip_fd.get()); |
| 691 | |
| 692 | ScratchFile oat_file; |
| 693 | success = WriteElf(oat_file.GetFile(), |
| 694 | std::move(zip_fd), |
| 695 | zip_file.GetFilename().c_str(), |
Andreas Gampe | 3a2bd29 | 2016-01-26 17:23:47 -0800 | [diff] [blame] | 696 | key_value_store, |
| 697 | verify); |
| 698 | if (verify) { |
| 699 | ASSERT_FALSE(success); |
| 700 | } else { |
| 701 | ASSERT_TRUE(success); |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 702 | |
Andreas Gampe | 3a2bd29 | 2016-01-26 17:23:47 -0800 | [diff] [blame] | 703 | std::string error_msg; |
| 704 | std::unique_ptr<OatFile> opened_oat_file(OatFile::Open(oat_file.GetFilename(), |
| 705 | oat_file.GetFilename(), |
| 706 | nullptr, |
| 707 | nullptr, |
| 708 | false, |
| 709 | nullptr, |
| 710 | &error_msg)); |
| 711 | ASSERT_TRUE(opened_oat_file != nullptr); |
| 712 | ASSERT_EQ(2u, opened_oat_file->GetOatDexFiles().size()); |
| 713 | std::unique_ptr<const DexFile> opened_dex_file1 = |
| 714 | opened_oat_file->GetOatDexFiles()[0]->OpenDexFile(&error_msg); |
| 715 | std::unique_ptr<const DexFile> opened_dex_file2 = |
| 716 | opened_oat_file->GetOatDexFiles()[1]->OpenDexFile(&error_msg); |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 717 | |
Andreas Gampe | 3a2bd29 | 2016-01-26 17:23:47 -0800 | [diff] [blame] | 718 | ASSERT_EQ(dex_file1_data->GetHeader().file_size_, opened_dex_file1->GetHeader().file_size_); |
| 719 | ASSERT_EQ(0, memcmp(&dex_file1_data->GetHeader(), |
| 720 | &opened_dex_file1->GetHeader(), |
| 721 | dex_file1_data->GetHeader().file_size_)); |
| 722 | ASSERT_EQ(DexFile::GetMultiDexLocation(0, zip_file.GetFilename().c_str()), |
| 723 | opened_dex_file1->GetLocation()); |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 724 | |
Andreas Gampe | 3a2bd29 | 2016-01-26 17:23:47 -0800 | [diff] [blame] | 725 | ASSERT_EQ(dex_file2_data->GetHeader().file_size_, opened_dex_file2->GetHeader().file_size_); |
| 726 | ASSERT_EQ(0, memcmp(&dex_file2_data->GetHeader(), |
| 727 | &opened_dex_file2->GetHeader(), |
| 728 | dex_file2_data->GetHeader().file_size_)); |
| 729 | ASSERT_EQ(DexFile::GetMultiDexLocation(1, zip_file.GetFilename().c_str()), |
| 730 | opened_dex_file2->GetLocation()); |
| 731 | } |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 732 | } |
| 733 | } |
| 734 | |
Andreas Gampe | 3a2bd29 | 2016-01-26 17:23:47 -0800 | [diff] [blame] | 735 | TEST_F(OatTest, ZipFileInputCheckOutput) { |
| 736 | TestZipFileInput(false); |
| 737 | } |
| 738 | |
| 739 | TEST_F(OatTest, ZipFileInputCheckVerifier) { |
| 740 | TestZipFileInput(true); |
| 741 | } |
| 742 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 743 | } // namespace art |