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