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