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