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