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