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