blob: c46aa18703aaaadbd186a5b3014958ba31e91afe [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
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 Carlstrome24fa612011-09-29 00:53:55 -070016
Andreas Gampe46ee31b2016-12-14 10:11:49 -080017#include "android-base/stringprintf.h"
18
Ian Rogersd582fa42014-11-05 23:46:43 -080019#include "arch/instruction_set_features.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070020#include "art_method-inl.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070021#include "base/enums.h"
Andreas Gampedfcd82c2018-10-16 20:22:37 -070022#include "base/file_utils.h"
Andreas Gampe5678db52017-06-08 14:11:18 -070023#include "base/stl_util.h"
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -080024#include "base/unix_file/fd_file.h"
Andreas Gampe53c913b2014-08-12 23:19:23 -070025#include "class_linker.h"
Vladimir Marko815d5e52019-03-04 10:18:31 +000026#include "common_compiler_driver_test.h"
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010027#include "compiled_method-inl.h"
Ian Rogerse63db272014-07-15 15:36:11 -070028#include "compiler.h"
David Srbecky4fda4eb2016-02-05 13:34:46 +000029#include "debug/method_debug_info.h"
Mathieu Chartierbee5e2d2018-08-02 11:00:16 -070030#include "dex/class_accessor-inl.h"
David Sehr9e734c72018-01-04 17:56:19 -080031#include "dex/dex_file_loader.h"
Ian Rogerse63db272014-07-15 15:36:11 -070032#include "dex/quick_compiler_callbacks.h"
David Sehr312f3b22018-03-19 08:39:26 -070033#include "dex/test_dex_file_builder.h"
Mathieu Chartier5bdab122015-01-26 18:30:19 -080034#include "dex/verification_results.h"
Vladimir Marko20f85592015-03-19 10:07:02 +000035#include "driver/compiler_driver.h"
36#include "driver/compiler_options.h"
Vladimir Marko7624d252014-05-02 14:40:15 +010037#include "entrypoints/quick/quick_entrypoints.h"
Vladimir Marko74527972016-11-29 15:57:32 +000038#include "linker/elf_writer.h"
39#include "linker/elf_writer_quick.h"
Vladimir Marko944da602016-02-19 12:27:55 +000040#include "linker/multi_oat_relative_patcher.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080041#include "mirror/class-inl.h"
Ian Rogerse63db272014-07-15 15:36:11 -070042#include "mirror/object-inl.h"
David Srbecky4fda4eb2016-02-05 13:34:46 +000043#include "mirror/object_array-inl.h"
Vladimir Marko7624d252014-05-02 14:40:15 +010044#include "oat_file-inl.h"
Ian Rogerse63db272014-07-15 15:36:11 -070045#include "oat_writer.h"
David Sehr82d046e2018-04-23 08:14:19 -070046#include "profile/profile_compilation_info.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070047#include "scoped_thread_state_change-inl.h"
David Srbecky2faab002019-02-12 16:35:48 +000048#include "stream/buffered_output_stream.h"
49#include "stream/file_output_stream.h"
50#include "stream/vector_output_stream.h"
David Brazdil93592f52017-12-08 10:53:27 +000051#include "vdex_file.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070052
Brian Carlstrome24fa612011-09-29 00:53:55 -070053namespace art {
Vladimir Marko74527972016-11-29 15:57:32 +000054namespace linker {
Brian Carlstrome24fa612011-09-29 00:53:55 -070055
Vladimir Marko815d5e52019-03-04 10:18:31 +000056class OatTest : public CommonCompilerDriverTest {
Logan Chieneeb7edf2012-03-09 20:38:39 +080057 protected:
Brian Carlstromba150c32013-08-27 17:31:03 -070058 static const bool kCompile = false; // DISABLED_ due to the time to compile libcore
59
Mathieu Chartiere401d142015-04-22 13:56:20 -070060 void CheckMethod(ArtMethod* method,
Logan Chieneeb7edf2012-03-09 20:38:39 +080061 const OatFile::OatMethod& oat_method,
Richard Uhlerfbef44d2014-12-23 09:48:51 -080062 const DexFile& dex_file)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070063 REQUIRES_SHARED(Locks::mutator_lock_) {
Logan Chieneeb7edf2012-03-09 20:38:39 +080064 const CompiledMethod* compiled_method =
Richard Uhlerfbef44d2014-12-23 09:48:51 -080065 compiler_driver_->GetCompiledMethod(MethodReference(&dex_file,
Brian Carlstrom51c24672013-07-11 16:00:56 -070066 method->GetDexMethodIndex()));
Logan Chieneeb7edf2012-03-09 20:38:39 +080067
Ian Rogersd4c4d952014-10-16 20:31:53 -070068 if (compiled_method == nullptr) {
David Sehr709b0702016-10-13 09:12:37 -070069 EXPECT_TRUE(oat_method.GetQuickCode() == nullptr) << method->PrettyMethod() << " "
Ian Rogersd4c4d952014-10-16 20:31:53 -070070 << oat_method.GetQuickCode();
Ian Rogersef7d42f2014-01-06 12:55:46 -080071 EXPECT_EQ(oat_method.GetFrameSizeInBytes(), 0U);
Logan Chieneeb7edf2012-03-09 20:38:39 +080072 EXPECT_EQ(oat_method.GetCoreSpillMask(), 0U);
73 EXPECT_EQ(oat_method.GetFpSpillMask(), 0U);
74 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -080075 const void* quick_oat_code = oat_method.GetQuickCode();
David Sehr709b0702016-10-13 09:12:37 -070076 EXPECT_TRUE(quick_oat_code != nullptr) << method->PrettyMethod();
Elliott Hughes956af0f2014-12-11 14:34:28 -080077 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 Marko35831e82015-09-11 11:59:18 +010079 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 Hughes956af0f2014-12-11 14:34:28 -080082 EXPECT_EQ(0, memcmp(quick_oat_code, &quick_code[0], code_size))
David Sehr709b0702016-10-13 09:12:37 -070083 << method->PrettyMethod() << " " << code_size;
Elliott Hughes956af0f2014-12-11 14:34:28 -080084 CHECK_EQ(0, memcmp(quick_oat_code, &quick_code[0], code_size));
Logan Chieneeb7edf2012-03-09 20:38:39 +080085 }
86 }
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -080087
Vladimir Markoa0431112018-06-25 09:32:54 +010088 void SetupCompiler(const std::vector<std::string>& compiler_options) {
89 std::string error_msg;
Andreas Gampe097f34c2017-08-23 08:57:51 -070090 if (!compiler_options_->ParseCompilerOptions(compiler_options,
Vladimir Markof4efa9e2018-10-17 14:12:45 +010091 /*ignore_unrecognized=*/ false,
Vladimir Markoa0431112018-06-25 09:32:54 +010092 &error_msg)) {
93 LOG(FATAL) << error_msg;
Andreas Gampe097f34c2017-08-23 08:57:51 -070094 UNREACHABLE();
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -080095 }
Mathieu Chartiere01b6f62017-07-19 16:55:04 -070096 callbacks_.reset(new QuickCompilerCallbacks(CompilerCallbacks::CallbackMode::kCompileApp));
97 callbacks_->SetVerificationResults(verification_results_.get());
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -080098 Runtime::Current()->SetCompilerCallbacks(callbacks_.get());
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -080099 }
100
David Brazdil7b49e6c2016-09-01 11:06:18 +0100101 bool WriteElf(File* vdex_file,
102 File* oat_file,
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800103 const std::vector<const DexFile*>& dex_files,
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800104 SafeMap<std::string, std::string>& key_value_store,
105 bool verify) {
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800106 TimingLogger timings("WriteElf", false, false);
Vladimir Markoa0431112018-06-25 09:32:54 +0100107 ClearBootImageOption();
108 OatWriter oat_writer(*compiler_options_,
Jeff Hao608f2ce2016-10-19 11:17:11 -0700109 &timings,
Mathieu Chartier603ccab2017-10-20 14:34:28 -0700110 /*profile_compilation_info*/nullptr,
111 CompactDexLevel::kCompactDexLevelNone);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000112 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 Cross2b41cca2018-11-16 22:43:41 -0800122 return DoWriteElf(
123 vdex_file, oat_file, oat_writer, key_value_store, verify, CopyOption::kOnlyIfCompressed);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000124 }
125
David Brazdil7b49e6c2016-09-01 11:06:18 +0100126 bool WriteElf(File* vdex_file,
127 File* oat_file,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000128 const std::vector<const char*>& dex_filenames,
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800129 SafeMap<std::string, std::string>& key_value_store,
Jeff Haod9df7802017-02-06 16:41:16 -0800130 bool verify,
Colin Cross2b41cca2018-11-16 22:43:41 -0800131 CopyOption copy,
Jeff Haod9df7802017-02-06 16:41:16 -0800132 ProfileCompilationInfo* profile_compilation_info) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000133 TimingLogger timings("WriteElf", false, false);
Vladimir Markoa0431112018-06-25 09:32:54 +0100134 ClearBootImageOption();
135 OatWriter oat_writer(*compiler_options_,
Mathieu Chartier603ccab2017-10-20 14:34:28 -0700136 &timings,
137 profile_compilation_info,
138 CompactDexLevel::kCompactDexLevelNone);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000139 for (const char* dex_filename : dex_filenames) {
140 if (!oat_writer.AddDexFileSource(dex_filename, dex_filename)) {
141 return false;
142 }
143 }
Colin Cross2b41cca2018-11-16 22:43:41 -0800144 return DoWriteElf(vdex_file, oat_file, oat_writer, key_value_store, verify, copy);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000145 }
146
David Brazdil7b49e6c2016-09-01 11:06:18 +0100147 bool WriteElf(File* vdex_file,
148 File* oat_file,
Andreas Gampe43e10b02016-07-15 17:17:34 -0700149 File&& zip_fd,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000150 const char* location,
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800151 SafeMap<std::string, std::string>& key_value_store,
Colin Cross2b41cca2018-11-16 22:43:41 -0800152 bool verify,
153 CopyOption copy) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000154 TimingLogger timings("WriteElf", false, false);
Vladimir Markoa0431112018-06-25 09:32:54 +0100155 ClearBootImageOption();
156 OatWriter oat_writer(*compiler_options_,
Jeff Hao608f2ce2016-10-19 11:17:11 -0700157 &timings,
Mathieu Chartier603ccab2017-10-20 14:34:28 -0700158 /*profile_compilation_info*/nullptr,
159 CompactDexLevel::kCompactDexLevelNone);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000160 if (!oat_writer.AddZippedDexFilesSource(std::move(zip_fd), location)) {
161 return false;
162 }
Colin Cross2b41cca2018-11-16 22:43:41 -0800163 return DoWriteElf(vdex_file, oat_file, oat_writer, key_value_store, verify, copy);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000164 }
165
David Brazdil7b49e6c2016-09-01 11:06:18 +0100166 bool DoWriteElf(File* vdex_file,
167 File* oat_file,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000168 OatWriter& oat_writer,
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800169 SafeMap<std::string, std::string>& key_value_store,
Colin Cross2b41cca2018-11-16 22:43:41 -0800170 bool verify,
171 CopyOption copy) {
Vladimir Marko10c13562015-11-25 14:33:36 +0000172 std::unique_ptr<ElfWriter> elf_writer = CreateElfWriterQuick(
Vladimir Markoa0431112018-06-25 09:32:54 +0100173 compiler_driver_->GetCompilerOptions(),
David Brazdil7b49e6c2016-09-01 11:06:18 +0100174 oat_file);
Vladimir Marko10c13562015-11-25 14:33:36 +0000175 elf_writer->Start();
David Brazdil7b49e6c2016-09-01 11:06:18 +0100176 OutputStream* oat_rodata = elf_writer->StartRoData();
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100177 std::vector<MemMap> opened_dex_files_maps;
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000178 std::vector<std::unique_ptr<const DexFile>> opened_dex_files;
Mathieu Chartier792111c2018-02-15 13:02:15 -0800179 if (!oat_writer.WriteAndOpenDexFiles(
180 vdex_file,
181 oat_rodata,
Mathieu Chartier792111c2018-02-15 13:02:15 -0800182 &key_value_store,
183 verify,
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100184 /*update_input_vdex=*/ false,
Colin Cross2b41cca2018-11-16 22:43:41 -0800185 copy,
Mathieu Chartier792111c2018-02-15 13:02:15 -0800186 &opened_dex_files_maps,
187 &opened_dex_files)) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000188 return false;
189 }
Vladimir Markoaad75c62016-10-03 08:46:48 +0000190
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000191 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 Chartierf284d442016-06-02 11:48:30 -0700197 class_linker->RegisterDexFile(*dex_file, nullptr);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000198 }
Vladimir Markoa0431112018-06-25 09:32:54 +0100199 MultiOatRelativePatcher patcher(compiler_options_->GetInstructionSet(),
200 compiler_options_->GetInstructionSetFeatures(),
Vladimir Markoca1e0382018-04-11 09:58:41 +0000201 compiler_driver_->GetCompiledMethodStorage());
Nicolas Geoffray4acefd32016-10-24 13:14:58 +0100202 oat_writer.Initialize(compiler_driver_.get(), nullptr, dex_files);
203 oat_writer.PrepareLayout(&patcher);
Vladimir Markob066d432018-01-03 13:14:37 +0000204 elf_writer->PrepareDynamicSection(oat_writer.GetOatHeader().GetExecutableOffset(),
205 oat_writer.GetCodeSize(),
206 oat_writer.GetDataBimgRelRoSize(),
Vladimir Markoaad75c62016-10-03 08:46:48 +0000207 oat_writer.GetBssSize(),
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100208 oat_writer.GetBssMethodsOffset(),
David Srbeckyec2cdf42017-12-08 16:21:25 +0000209 oat_writer.GetBssRootsOffset(),
210 oat_writer.GetVdexSize());
Vladimir Marko944da602016-02-19 12:27:55 +0000211
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +0000212 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 Brazdil93592f52017-12-08 10:53:27 +0000217 if (!oat_writer.WriteQuickeningInfo(vdex_out.get())) {
218 return false;
219 }
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +0000220 if (!oat_writer.WriteChecksumsAndVdexHeader(vdex_out.get())) {
221 return false;
Nicolas Geoffrayf54e5df2016-12-01 10:45:08 +0000222 }
223
David Brazdil7b49e6c2016-09-01 11:06:18 +0100224 if (!oat_writer.WriteRodata(oat_rodata)) {
Vladimir Marko10c13562015-11-25 14:33:36 +0000225 return false;
226 }
David Brazdil7b49e6c2016-09-01 11:06:18 +0100227 elf_writer->EndRoData(oat_rodata);
Vladimir Marko10c13562015-11-25 14:33:36 +0000228
229 OutputStream* text = elf_writer->StartText();
230 if (!oat_writer.WriteCode(text)) {
231 return false;
232 }
233 elf_writer->EndText(text);
234
Vladimir Markob066d432018-01-03 13:14:37 +0000235 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 Marko0ace5632018-12-14 11:11:47 +0000243 if (!oat_writer.WriteHeader(elf_writer->GetStream())) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000244 return false;
245 }
246
Vladimir Marko10c13562015-11-25 14:33:36 +0000247 elf_writer->WriteDynamicSection();
David Srbecky32210b92017-12-04 14:39:21 +0000248 elf_writer->WriteDebugInfo(oat_writer.GetDebugInfo());
Vladimir Marko10c13562015-11-25 14:33:36 +0000249
Vladimir Markoaad75c62016-10-03 08:46:48 +0000250 if (!elf_writer->End()) {
251 return false;
252 }
253
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100254 for (MemMap& map : opened_dex_files_maps) {
Nicolas Geoffrayf3075272018-01-08 12:41:19 +0000255 opened_dex_files_maps_.emplace_back(std::move(map));
256 }
Vladimir Markoaad75c62016-10-03 08:46:48 +0000257 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 Chartier07ddb6f2015-11-05 11:16:34 -0800261 }
262
Jeff Haod9df7802017-02-06 16:41:16 -0800263 void TestDexFileInput(bool verify, bool low_4gb, bool use_profile);
Colin Cross2b41cca2018-11-16 22:43:41 -0800264 void TestZipFileInput(bool verify, CopyOption copy);
Jeff Hao41b2f532017-03-02 16:36:31 -0800265 void TestZipFileInputWithEmptyDex();
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800266
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800267 std::unique_ptr<QuickCompilerCallbacks> callbacks_;
Vladimir Markoaad75c62016-10-03 08:46:48 +0000268
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100269 std::vector<MemMap> opened_dex_files_maps_;
Vladimir Markoaad75c62016-10-03 08:46:48 +0000270 std::vector<std::unique_ptr<const DexFile>> opened_dex_files_;
Logan Chieneeb7edf2012-03-09 20:38:39 +0800271};
Brian Carlstrome24fa612011-09-29 00:53:55 -0700272
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000273class 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 Carlstrome24fa612011-09-29 00:53:55 -0700384TEST_F(OatTest, WriteRead) {
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800385 TimingLogger timings("OatTest::WriteRead", false, false);
Jesse Wilson254db0f2011-11-16 16:44:11 -0500386 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700387
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700388 std::string error_msg;
Vladimir Markoa0431112018-06-25 09:32:54 +0100389 SetupCompiler(std::vector<std::string>());
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800390
Ian Rogersd4c4d952014-10-16 20:31:53 -0700391 jobject class_loader = nullptr;
Brian Carlstromba150c32013-08-27 17:31:03 -0700392 if (kCompile) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800393 TimingLogger timings2("OatTest::WriteRead", false, false);
Vladimir Marko2afaff72018-11-30 17:01:50 +0000394 CompileAll(class_loader, class_linker->GetBootClassPath(), &timings2);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700395 }
396
Vladimir Markof0499c92018-03-09 12:42:09 +0000397 ScratchFile tmp_base, tmp_oat(tmp_base, ".oat"), tmp_vdex(tmp_base, ".vdex");
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700398 SafeMap<std::string, std::string> key_value_store;
Vladimir Marko0ace5632018-12-14 11:11:47 +0000399 key_value_store.Put(OatHeader::kBootClassPathChecksumsKey, "testkey");
David Brazdil7b49e6c2016-09-01 11:06:18 +0100400 bool success = WriteElf(tmp_vdex.GetFile(),
401 tmp_oat.GetFile(),
402 class_linker->GetBootClassPath(),
403 key_value_store,
404 false);
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700405 ASSERT_TRUE(success);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700406
Brian Carlstromba150c32013-08-27 17:31:03 -0700407 if (kCompile) { // OatWriter strips the code, regenerate to compare
Vladimir Marko2afaff72018-11-30 17:01:50 +0000408 CompileAll(class_loader, class_linker->GetBootClassPath(), &timings);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700409 }
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100410 std::unique_ptr<OatFile> oat_file(OatFile::Open(/*zip_fd=*/ -1,
Nicolas Geoffray30025092018-04-19 14:43:29 +0100411 tmp_oat.GetFilename(),
David Brazdil7b49e6c2016-09-01 11:06:18 +0100412 tmp_oat.GetFilename(),
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100413 /*executable=*/ false,
414 /*low_4gb=*/ true,
415 /*abs_dex_location=*/ nullptr,
416 /*reservation=*/ nullptr,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800417 &error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700418 ASSERT_TRUE(oat_file.get() != nullptr) << error_msg;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700419 const OatHeader& oat_header = oat_file->GetOatHeader();
Brian Carlstromf852fb22012-10-19 11:01:58 -0700420 ASSERT_TRUE(oat_header.IsValid());
Yi Kong9515b512015-11-11 14:36:07 +0000421 ASSERT_EQ(class_linker->GetBootClassPath().size(), oat_header.GetDexFileCount()); // core
Vladimir Marko0ace5632018-12-14 11:11:47 +0000422 ASSERT_TRUE(oat_header.GetStoreValueByKey(OatHeader::kBootClassPathChecksumsKey) != nullptr);
423 ASSERT_STREQ("testkey", oat_header.GetStoreValueByKey(OatHeader::kBootClassPathChecksumsKey));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700424
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800425 ASSERT_TRUE(java_lang_dex_file_ != nullptr);
426 const DexFile& dex_file = *java_lang_dex_file_;
427 uint32_t dex_file_checksum = dex_file.GetLocationChecksum();
Andreas Gampeb40d3612018-06-26 15:49:42 -0700428 const OatDexFile* oat_dex_file = oat_file->GetOatDexFile(dex_file.GetLocation().c_str(),
429 &dex_file_checksum);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700430 ASSERT_TRUE(oat_dex_file != nullptr);
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800431 CHECK_EQ(dex_file.GetLocationChecksum(), oat_dex_file->GetDexFileLocationChecksum());
Vladimir Markof4da6752014-08-01 19:04:18 +0100432 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700433 auto pointer_size = class_linker->GetImagePointerSize();
Mathieu Chartierbee5e2d2018-08-02 11:00:16 -0700434 for (ClassAccessor accessor : dex_file.GetClasses()) {
435 size_t num_virtual_methods = accessor.NumVirtualMethods();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700436
Mathieu Chartierbee5e2d2018-08-02 11:00:16 -0700437 const char* descriptor = accessor.GetDescriptor();
Vladimir Markoe9987b02018-05-22 16:26:43 +0100438 ObjPtr<mirror::Class> klass = class_linker->FindClass(soa.Self(),
439 descriptor,
440 ScopedNullHandle<mirror::ClassLoader>());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700441
Mathieu Chartierbee5e2d2018-08-02 11:00:16 -0700442 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(accessor.GetClassDefIndex());
Vladimir Marko2c64a832018-01-04 11:31:56 +0000443 CHECK_EQ(ClassStatus::kNotReady, oat_class.GetStatus()) << descriptor;
Brian Carlstromba150c32013-08-27 17:31:03 -0700444 CHECK_EQ(kCompile ? OatClassType::kOatClassAllCompiled : OatClassType::kOatClassNoneCompiled,
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100445 oat_class.GetType()) << descriptor;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700446
447 size_t method_index = 0;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700448 for (auto& m : klass->GetDirectMethods(pointer_size)) {
449 CheckMethod(&m, oat_class.GetOatMethod(method_index), dex_file);
450 ++method_index;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700451 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700452 size_t visited_virtuals = 0;
Alex Lighte64300b2015-12-15 15:02:47 -0800453 // TODO We should also check copied methods in this test.
454 for (auto& m : klass->GetDeclaredVirtualMethods(pointer_size)) {
Alex Lightfcea56f2016-02-17 11:59:05 -0800455 if (!klass->IsInterface()) {
Alex Light36121492016-02-22 13:43:29 -0800456 EXPECT_FALSE(m.IsCopied());
Alex Lightfcea56f2016-02-17 11:59:05 -0800457 }
Alex Lighte64300b2015-12-15 15:02:47 -0800458 CheckMethod(&m, oat_class.GetOatMethod(method_index), dex_file);
459 ++method_index;
460 ++visited_virtuals;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700461 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700462 EXPECT_EQ(visited_virtuals, num_virtual_methods);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700463 }
464}
465
Brian Carlstrom341df942012-06-27 12:29:22 -0700466TEST_F(OatTest, OatHeaderSizeCheck) {
467 // If this test is failing and you have to update these constants,
468 // it is time to update OatHeader::kOatVersion
Vladimir Marko98fb0832019-03-13 16:52:11 +0000469 EXPECT_EQ(56U, sizeof(OatHeader));
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800470 EXPECT_EQ(4U, sizeof(OatMethodOffsets));
Nicolas Geoffray57083762019-03-05 09:24:45 +0000471 EXPECT_EQ(8U, sizeof(OatQuickMethodHeader));
Alex Lightd109e302018-06-27 10:25:41 -0700472 EXPECT_EQ(166 * static_cast<size_t>(GetInstructionSetPointerSize(kRuntimeISA)),
Andreas Gampe542451c2016-07-26 09:02:02 -0700473 sizeof(QuickEntryPoints));
Brian Carlstrom341df942012-06-27 12:29:22 -0700474}
475
Brian Carlstromf852fb22012-10-19 11:01:58 -0700476TEST_F(OatTest, OatHeaderIsValid) {
Vladimir Marko33bff252017-11-01 14:35:42 +0000477 InstructionSet insn_set = InstructionSet::kX86;
Jeff Hao5d3baf62016-05-23 19:17:04 -0700478 std::string error_msg;
479 std::unique_ptr<const InstructionSetFeatures> insn_features(
480 InstructionSetFeatures::FromVariant(insn_set, "default", &error_msg));
481 ASSERT_TRUE(insn_features.get() != nullptr) << error_msg;
482 std::unique_ptr<OatHeader> oat_header(OatHeader::Create(insn_set,
483 insn_features.get(),
484 0u,
485 nullptr));
486 ASSERT_NE(oat_header.get(), nullptr);
487 ASSERT_TRUE(oat_header->IsValid());
Brian Carlstromf852fb22012-10-19 11:01:58 -0700488
Jeff Hao5d3baf62016-05-23 19:17:04 -0700489 char* magic = const_cast<char*>(oat_header->GetMagic());
490 strcpy(magic, ""); // bad magic
491 ASSERT_FALSE(oat_header->IsValid());
492 strcpy(magic, "oat\n000"); // bad version
493 ASSERT_FALSE(oat_header->IsValid());
Brian Carlstromf852fb22012-10-19 11:01:58 -0700494}
495
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800496TEST_F(OatTest, EmptyTextSection) {
497 TimingLogger timings("OatTest::EmptyTextSection", false, false);
498
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800499 std::vector<std::string> compiler_options;
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100500 compiler_options.push_back("--compiler-filter=extract");
Vladimir Markoa0431112018-06-25 09:32:54 +0100501 SetupCompiler(compiler_options);
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800502
503 jobject class_loader;
504 {
505 ScopedObjectAccess soa(Thread::Current());
506 class_loader = LoadDex("Main");
507 }
508 ASSERT_TRUE(class_loader != nullptr);
509 std::vector<const DexFile*> dex_files = GetDexFiles(class_loader);
510 ASSERT_TRUE(!dex_files.empty());
511
512 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
513 for (const DexFile* dex_file : dex_files) {
514 ScopedObjectAccess soa(Thread::Current());
Vladimir Markobcf17522018-06-01 13:14:32 +0100515 class_linker->RegisterDexFile(*dex_file, soa.Decode<mirror::ClassLoader>(class_loader));
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800516 }
Vladimir Marko2afaff72018-11-30 17:01:50 +0000517 CompileAll(class_loader, dex_files, &timings);
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800518
Vladimir Markof0499c92018-03-09 12:42:09 +0000519 ScratchFile tmp_base, tmp_oat(tmp_base, ".oat"), tmp_vdex(tmp_base, ".vdex");
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800520 SafeMap<std::string, std::string> key_value_store;
Vladimir Markof0499c92018-03-09 12:42:09 +0000521 bool success = WriteElf(tmp_vdex.GetFile(),
522 tmp_oat.GetFile(),
523 dex_files,
524 key_value_store,
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100525 /*verify=*/ false);
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800526 ASSERT_TRUE(success);
527
Vladimir Markoa0431112018-06-25 09:32:54 +0100528 std::string error_msg;
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100529 std::unique_ptr<OatFile> oat_file(OatFile::Open(/*zip_fd=*/ -1,
Nicolas Geoffray30025092018-04-19 14:43:29 +0100530 tmp_oat.GetFilename(),
David Brazdil7b49e6c2016-09-01 11:06:18 +0100531 tmp_oat.GetFilename(),
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100532 /*executable=*/ false,
533 /*low_4gb=*/ false,
534 /*abs_dex_location=*/ nullptr,
535 /*reservation=*/ nullptr,
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800536 &error_msg));
537 ASSERT_TRUE(oat_file != nullptr);
David Brazdil7b49e6c2016-09-01 11:06:18 +0100538 EXPECT_LT(static_cast<size_t>(oat_file->Size()),
539 static_cast<size_t>(tmp_oat.GetFile()->GetLength()));
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800540}
541
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800542static void MaybeModifyDexFileToFail(bool verify, std::unique_ptr<const DexFile>& data) {
543 // If in verify mode (= fail the verifier mode), make sure we fail early. We'll fail already
544 // because of the missing map, but that may lead to out of bounds reads.
545 if (verify) {
546 const_cast<DexFile::Header*>(&data->GetHeader())->checksum_++;
547 }
548}
549
Jeff Haod9df7802017-02-06 16:41:16 -0800550void OatTest::TestDexFileInput(bool verify, bool low_4gb, bool use_profile) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000551 TimingLogger timings("OatTest::DexFileInput", false, false);
552
553 std::vector<const char*> input_filenames;
554
555 ScratchFile dex_file1;
556 TestDexFileBuilder builder1;
557 builder1.AddField("Lsome.TestClass;", "int", "someField");
558 builder1.AddMethod("Lsome.TestClass;", "()I", "foo");
559 std::unique_ptr<const DexFile> dex_file1_data = builder1.Build(dex_file1.GetFilename());
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800560
561 MaybeModifyDexFileToFail(verify, dex_file1_data);
562
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000563 bool success = dex_file1.GetFile()->WriteFully(&dex_file1_data->GetHeader(),
564 dex_file1_data->GetHeader().file_size_);
565 ASSERT_TRUE(success);
566 success = dex_file1.GetFile()->Flush() == 0;
567 ASSERT_TRUE(success);
568 input_filenames.push_back(dex_file1.GetFilename().c_str());
569
570 ScratchFile dex_file2;
571 TestDexFileBuilder builder2;
572 builder2.AddField("Land.AnotherTestClass;", "boolean", "someOtherField");
573 builder2.AddMethod("Land.AnotherTestClass;", "()J", "bar");
574 std::unique_ptr<const DexFile> dex_file2_data = builder2.Build(dex_file2.GetFilename());
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800575
576 MaybeModifyDexFileToFail(verify, dex_file2_data);
577
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000578 success = dex_file2.GetFile()->WriteFully(&dex_file2_data->GetHeader(),
579 dex_file2_data->GetHeader().file_size_);
580 ASSERT_TRUE(success);
581 success = dex_file2.GetFile()->Flush() == 0;
582 ASSERT_TRUE(success);
583 input_filenames.push_back(dex_file2.GetFilename().c_str());
584
Vladimir Markof0499c92018-03-09 12:42:09 +0000585 ScratchFile tmp_base, tmp_oat(tmp_base, ".oat"), tmp_vdex(tmp_base, ".vdex");
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000586 SafeMap<std::string, std::string> key_value_store;
Jeff Haod9df7802017-02-06 16:41:16 -0800587 std::unique_ptr<ProfileCompilationInfo>
588 profile_compilation_info(use_profile ? new ProfileCompilationInfo() : nullptr);
Vladimir Markof0499c92018-03-09 12:42:09 +0000589 success = WriteElf(tmp_vdex.GetFile(),
590 tmp_oat.GetFile(),
David Brazdil7b49e6c2016-09-01 11:06:18 +0100591 input_filenames,
592 key_value_store,
Jeff Haod9df7802017-02-06 16:41:16 -0800593 verify,
Colin Cross2b41cca2018-11-16 22:43:41 -0800594 CopyOption::kOnlyIfCompressed,
Jeff Haod9df7802017-02-06 16:41:16 -0800595 profile_compilation_info.get());
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800596
597 // In verify mode, we expect failure.
598 if (verify) {
599 ASSERT_FALSE(success);
600 return;
601 }
602
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000603 ASSERT_TRUE(success);
604
605 std::string error_msg;
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100606 std::unique_ptr<OatFile> opened_oat_file(OatFile::Open(/*zip_fd=*/ -1,
Nicolas Geoffray30025092018-04-19 14:43:29 +0100607 tmp_oat.GetFilename(),
Vladimir Markof0499c92018-03-09 12:42:09 +0000608 tmp_oat.GetFilename(),
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100609 /*executable=*/ false,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800610 low_4gb,
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100611 /*abs_dex_location=*/ nullptr,
612 /*reservation=*/ nullptr,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000613 &error_msg));
Vladimir Markof0499c92018-03-09 12:42:09 +0000614 ASSERT_TRUE(opened_oat_file != nullptr) << error_msg;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800615 if (low_4gb) {
616 uintptr_t begin = reinterpret_cast<uintptr_t>(opened_oat_file->Begin());
617 EXPECT_EQ(begin, static_cast<uint32_t>(begin));
618 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000619 ASSERT_EQ(2u, opened_oat_file->GetOatDexFiles().size());
620 std::unique_ptr<const DexFile> opened_dex_file1 =
621 opened_oat_file->GetOatDexFiles()[0]->OpenDexFile(&error_msg);
622 std::unique_ptr<const DexFile> opened_dex_file2 =
623 opened_oat_file->GetOatDexFiles()[1]->OpenDexFile(&error_msg);
624
Nicolas Geoffray6f73f4a2017-11-23 12:51:47 +0000625 ASSERT_EQ(opened_oat_file->GetOatDexFiles()[0]->GetDexFileLocationChecksum(),
626 dex_file1_data->GetHeader().checksum_);
627 ASSERT_EQ(opened_oat_file->GetOatDexFiles()[1]->GetDexFileLocationChecksum(),
628 dex_file2_data->GetHeader().checksum_);
629
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000630 ASSERT_EQ(dex_file1_data->GetHeader().file_size_, opened_dex_file1->GetHeader().file_size_);
631 ASSERT_EQ(0, memcmp(&dex_file1_data->GetHeader(),
632 &opened_dex_file1->GetHeader(),
633 dex_file1_data->GetHeader().file_size_));
634 ASSERT_EQ(dex_file1_data->GetLocation(), opened_dex_file1->GetLocation());
635
636 ASSERT_EQ(dex_file2_data->GetHeader().file_size_, opened_dex_file2->GetHeader().file_size_);
637 ASSERT_EQ(0, memcmp(&dex_file2_data->GetHeader(),
638 &opened_dex_file2->GetHeader(),
639 dex_file2_data->GetHeader().file_size_));
640 ASSERT_EQ(dex_file2_data->GetLocation(), opened_dex_file2->GetLocation());
David Brazdil93592f52017-12-08 10:53:27 +0000641
Nicolas Geoffray3a293552018-03-02 10:52:16 +0000642 const VdexFile::DexSectionHeader &vdex_header =
643 opened_oat_file->GetVdexFile()->GetDexSectionHeader();
Mathieu Chartier210531f2018-01-12 10:15:51 -0800644 if (!compiler_driver_->GetCompilerOptions().IsQuickeningCompilationEnabled()) {
645 // If quickening is enabled we will always write the table since there is no special logic that
646 // checks for all methods not being quickened (not worth the complexity).
647 ASSERT_EQ(vdex_header.GetQuickeningInfoSize(), 0u);
648 }
David Brazdil93592f52017-12-08 10:53:27 +0000649
Vladimir Markof0499c92018-03-09 12:42:09 +0000650 int64_t actual_vdex_size = tmp_vdex.GetFile()->GetLength();
David Brazdil93592f52017-12-08 10:53:27 +0000651 ASSERT_GE(actual_vdex_size, 0);
Nicolas Geoffray3a293552018-03-02 10:52:16 +0000652 ASSERT_EQ((uint64_t) actual_vdex_size, opened_oat_file->GetVdexFile()->GetComputedFileSize());
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000653}
654
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800655TEST_F(OatTest, DexFileInputCheckOutput) {
Jeff Haod9df7802017-02-06 16:41:16 -0800656 TestDexFileInput(/*verify*/false, /*low_4gb*/false, /*use_profile*/false);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800657}
658
659TEST_F(OatTest, DexFileInputCheckOutputLow4GB) {
Jeff Haod9df7802017-02-06 16:41:16 -0800660 TestDexFileInput(/*verify*/false, /*low_4gb*/true, /*use_profile*/false);
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800661}
662
663TEST_F(OatTest, DexFileInputCheckVerifier) {
Jeff Haod9df7802017-02-06 16:41:16 -0800664 TestDexFileInput(/*verify*/true, /*low_4gb*/false, /*use_profile*/false);
665}
666
667TEST_F(OatTest, DexFileFailsVerifierWithLayout) {
668 TestDexFileInput(/*verify*/true, /*low_4gb*/false, /*use_profile*/true);
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800669}
670
Colin Cross2b41cca2018-11-16 22:43:41 -0800671void OatTest::TestZipFileInput(bool verify, CopyOption copy) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000672 TimingLogger timings("OatTest::DexFileInput", false, false);
673
674 ScratchFile zip_file;
675 ZipBuilder zip_builder(zip_file.GetFile());
676
677 ScratchFile dex_file1;
678 TestDexFileBuilder builder1;
679 builder1.AddField("Lsome.TestClass;", "long", "someField");
680 builder1.AddMethod("Lsome.TestClass;", "()D", "foo");
681 std::unique_ptr<const DexFile> dex_file1_data = builder1.Build(dex_file1.GetFilename());
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800682
683 MaybeModifyDexFileToFail(verify, dex_file1_data);
684
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000685 bool success = dex_file1.GetFile()->WriteFully(&dex_file1_data->GetHeader(),
686 dex_file1_data->GetHeader().file_size_);
687 ASSERT_TRUE(success);
688 success = dex_file1.GetFile()->Flush() == 0;
689 ASSERT_TRUE(success);
690 success = zip_builder.AddFile("classes.dex",
691 &dex_file1_data->GetHeader(),
692 dex_file1_data->GetHeader().file_size_);
693 ASSERT_TRUE(success);
694
695 ScratchFile dex_file2;
696 TestDexFileBuilder builder2;
697 builder2.AddField("Land.AnotherTestClass;", "boolean", "someOtherField");
698 builder2.AddMethod("Land.AnotherTestClass;", "()J", "bar");
699 std::unique_ptr<const DexFile> dex_file2_data = builder2.Build(dex_file2.GetFilename());
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800700
701 MaybeModifyDexFileToFail(verify, dex_file2_data);
702
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000703 success = dex_file2.GetFile()->WriteFully(&dex_file2_data->GetHeader(),
704 dex_file2_data->GetHeader().file_size_);
705 ASSERT_TRUE(success);
706 success = dex_file2.GetFile()->Flush() == 0;
707 ASSERT_TRUE(success);
708 success = zip_builder.AddFile("classes2.dex",
709 &dex_file2_data->GetHeader(),
710 dex_file2_data->GetHeader().file_size_);
711 ASSERT_TRUE(success);
712
713 success = zip_builder.Finish();
714 ASSERT_TRUE(success) << strerror(errno);
715
716 SafeMap<std::string, std::string> key_value_store;
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000717 {
718 // Test using the AddDexFileSource() interface with the zip file.
Igor Murashkin5573c372017-11-16 13:34:30 -0800719 std::vector<const char*> input_filenames = { zip_file.GetFilename().c_str() };
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000720
Vladimir Markof0499c92018-03-09 12:42:09 +0000721 ScratchFile tmp_base, tmp_oat(tmp_base, ".oat"), tmp_vdex(tmp_base, ".vdex");
722 success = WriteElf(tmp_vdex.GetFile(),
723 tmp_oat.GetFile(),
724 input_filenames,
725 key_value_store,
726 verify,
Colin Cross2b41cca2018-11-16 22:43:41 -0800727 copy,
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100728 /*profile_compilation_info=*/ nullptr);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000729
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800730 if (verify) {
731 ASSERT_FALSE(success);
732 } else {
733 ASSERT_TRUE(success);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000734
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800735 std::string error_msg;
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100736 std::unique_ptr<OatFile> opened_oat_file(OatFile::Open(/*zip_fd=*/ -1,
Nicolas Geoffray30025092018-04-19 14:43:29 +0100737 tmp_oat.GetFilename(),
Vladimir Markof0499c92018-03-09 12:42:09 +0000738 tmp_oat.GetFilename(),
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100739 /*executable=*/ false,
740 /*low_4gb=*/ false,
741 /*abs_dex_location=*/ nullptr,
742 /*reservation=*/ nullptr,
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800743 &error_msg));
Vladimir Markof0499c92018-03-09 12:42:09 +0000744 ASSERT_TRUE(opened_oat_file != nullptr) << error_msg;
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800745 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 Marko9bdf1082016-01-21 12:15:52 +0000750
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800751 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_));
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700755 ASSERT_EQ(DexFileLoader::GetMultiDexLocation(0, zip_file.GetFilename().c_str()),
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800756 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_));
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700762 ASSERT_EQ(DexFileLoader::GetMultiDexLocation(1, zip_file.GetFilename().c_str()),
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800763 opened_dex_file2->GetLocation());
764 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000765 }
766
767 {
768 // Test using the AddZipDexFileSource() interface with the zip file handle.
Andreas Gampedfcd82c2018-10-16 20:22:37 -0700769 File zip_fd(DupCloexec(zip_file.GetFd()), /*check_usage=*/ false);
Andreas Gampe43e10b02016-07-15 17:17:34 -0700770 ASSERT_NE(-1, zip_fd.Fd());
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000771
Vladimir Markof0499c92018-03-09 12:42:09 +0000772 ScratchFile tmp_base, tmp_oat(tmp_base, ".oat"), tmp_vdex(tmp_base, ".vdex");
773 success = WriteElf(tmp_vdex.GetFile(),
774 tmp_oat.GetFile(),
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000775 std::move(zip_fd),
776 zip_file.GetFilename().c_str(),
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800777 key_value_store,
Colin Cross2b41cca2018-11-16 22:43:41 -0800778 verify,
779 copy);
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800780 if (verify) {
781 ASSERT_FALSE(success);
782 } else {
783 ASSERT_TRUE(success);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000784
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800785 std::string error_msg;
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100786 std::unique_ptr<OatFile> opened_oat_file(OatFile::Open(/*zip_fd=*/ -1,
Nicolas Geoffray30025092018-04-19 14:43:29 +0100787 tmp_oat.GetFilename(),
Vladimir Markof0499c92018-03-09 12:42:09 +0000788 tmp_oat.GetFilename(),
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100789 /*executable=*/ false,
790 /*low_4gb=*/ false,
791 /*abs_dex_location=*/ nullptr,
792 /*reservation=*/ nullptr,
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800793 &error_msg));
Vladimir Markof0499c92018-03-09 12:42:09 +0000794 ASSERT_TRUE(opened_oat_file != nullptr) << error_msg;
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800795 ASSERT_EQ(2u, opened_oat_file->GetOatDexFiles().size());
796 std::unique_ptr<const DexFile> opened_dex_file1 =
797 opened_oat_file->GetOatDexFiles()[0]->OpenDexFile(&error_msg);
798 std::unique_ptr<const DexFile> opened_dex_file2 =
799 opened_oat_file->GetOatDexFiles()[1]->OpenDexFile(&error_msg);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000800
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800801 ASSERT_EQ(dex_file1_data->GetHeader().file_size_, opened_dex_file1->GetHeader().file_size_);
802 ASSERT_EQ(0, memcmp(&dex_file1_data->GetHeader(),
803 &opened_dex_file1->GetHeader(),
804 dex_file1_data->GetHeader().file_size_));
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700805 ASSERT_EQ(DexFileLoader::GetMultiDexLocation(0, zip_file.GetFilename().c_str()),
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800806 opened_dex_file1->GetLocation());
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000807
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800808 ASSERT_EQ(dex_file2_data->GetHeader().file_size_, opened_dex_file2->GetHeader().file_size_);
809 ASSERT_EQ(0, memcmp(&dex_file2_data->GetHeader(),
810 &opened_dex_file2->GetHeader(),
811 dex_file2_data->GetHeader().file_size_));
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700812 ASSERT_EQ(DexFileLoader::GetMultiDexLocation(1, zip_file.GetFilename().c_str()),
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800813 opened_dex_file2->GetLocation());
814 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000815 }
816}
817
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800818TEST_F(OatTest, ZipFileInputCheckOutput) {
Colin Cross2b41cca2018-11-16 22:43:41 -0800819 TestZipFileInput(false, CopyOption::kOnlyIfCompressed);
820}
821
822TEST_F(OatTest, ZipFileInputCheckOutputWithoutCopy) {
823 TestZipFileInput(false, CopyOption::kNever);
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800824}
825
826TEST_F(OatTest, ZipFileInputCheckVerifier) {
Colin Cross2b41cca2018-11-16 22:43:41 -0800827 TestZipFileInput(true, CopyOption::kOnlyIfCompressed);
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800828}
829
Jeff Hao41b2f532017-03-02 16:36:31 -0800830void OatTest::TestZipFileInputWithEmptyDex() {
831 ScratchFile zip_file;
832 ZipBuilder zip_builder(zip_file.GetFile());
833 bool success = zip_builder.AddFile("classes.dex", nullptr, 0);
834 ASSERT_TRUE(success);
835 success = zip_builder.Finish();
836 ASSERT_TRUE(success) << strerror(errno);
837
838 SafeMap<std::string, std::string> key_value_store;
Igor Murashkin5573c372017-11-16 13:34:30 -0800839 std::vector<const char*> input_filenames = { zip_file.GetFilename().c_str() };
Jeff Hao41b2f532017-03-02 16:36:31 -0800840 ScratchFile oat_file, vdex_file(oat_file, ".vdex");
841 std::unique_ptr<ProfileCompilationInfo> profile_compilation_info(new ProfileCompilationInfo());
Vladimir Markof0499c92018-03-09 12:42:09 +0000842 success = WriteElf(vdex_file.GetFile(),
843 oat_file.GetFile(),
844 input_filenames,
845 key_value_store,
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100846 /*verify=*/ false,
Colin Cross2b41cca2018-11-16 22:43:41 -0800847 CopyOption::kOnlyIfCompressed,
Vladimir Markof0499c92018-03-09 12:42:09 +0000848 profile_compilation_info.get());
Jeff Hao41b2f532017-03-02 16:36:31 -0800849 ASSERT_FALSE(success);
850}
851
852TEST_F(OatTest, ZipFileInputWithEmptyDex) {
853 TestZipFileInputWithEmptyDex();
854}
855
Vladimir Marko74527972016-11-29 15:57:32 +0000856} // namespace linker
Brian Carlstrome24fa612011-09-29 00:53:55 -0700857} // namespace art