blob: 84ec8b3ee1cfdf16510468720cc0fe869e615c5e [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 Gampe5678db52017-06-08 14:11:18 -070022#include "base/stl_util.h"
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -080023#include "base/unix_file/fd_file.h"
Andreas Gampe53c913b2014-08-12 23:19:23 -070024#include "class_linker.h"
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080025#include "common_compiler_test.h"
Vladimir Marko20f85592015-03-19 10:07:02 +000026#include "compiled_method.h"
Ian Rogerse63db272014-07-15 15:36:11 -070027#include "compiler.h"
David Srbecky4fda4eb2016-02-05 13:34:46 +000028#include "debug/method_debug_info.h"
Ian Rogerse63db272014-07-15 15:36:11 -070029#include "dex/quick_compiler_callbacks.h"
Mathieu Chartier5bdab122015-01-26 18:30:19 -080030#include "dex/verification_results.h"
Vladimir Marko20f85592015-03-19 10:07:02 +000031#include "driver/compiler_driver.h"
32#include "driver/compiler_options.h"
Vladimir Marko10c13562015-11-25 14:33:36 +000033#include "elf_writer.h"
34#include "elf_writer_quick.h"
Vladimir Marko7624d252014-05-02 14:40:15 +010035#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffrayf54e5df2016-12-01 10:45:08 +000036#include "linker/buffered_output_stream.h"
37#include "linker/file_output_stream.h"
Vladimir Marko944da602016-02-19 12:27:55 +000038#include "linker/multi_oat_relative_patcher.h"
Vladimir Marko131980f2015-12-03 18:29:23 +000039#include "linker/vector_output_stream.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080040#include "mirror/class-inl.h"
Ian Rogerse63db272014-07-15 15:36:11 -070041#include "mirror/object-inl.h"
David Srbecky4fda4eb2016-02-05 13:34:46 +000042#include "mirror/object_array-inl.h"
Vladimir Marko7624d252014-05-02 14:40:15 +010043#include "oat_file-inl.h"
Ian Rogerse63db272014-07-15 15:36:11 -070044#include "oat_writer.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070045#include "scoped_thread_state_change-inl.h"
Vladimir Marko9bdf1082016-01-21 12:15:52 +000046#include "utils/test_dex_file_builder.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070047
Brian Carlstrome24fa612011-09-29 00:53:55 -070048namespace art {
49
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -080050NO_RETURN static void Usage(const char* fmt, ...) {
51 va_list ap;
52 va_start(ap, fmt);
53 std::string error;
Andreas Gampe46ee31b2016-12-14 10:11:49 -080054 android::base::StringAppendV(&error, fmt, ap);
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -080055 LOG(FATAL) << error;
56 va_end(ap);
57 UNREACHABLE();
58}
59
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080060class OatTest : public CommonCompilerTest {
Logan Chieneeb7edf2012-03-09 20:38:39 +080061 protected:
Brian Carlstromba150c32013-08-27 17:31:03 -070062 static const bool kCompile = false; // DISABLED_ due to the time to compile libcore
63
Mathieu Chartiere401d142015-04-22 13:56:20 -070064 void CheckMethod(ArtMethod* method,
Logan Chieneeb7edf2012-03-09 20:38:39 +080065 const OatFile::OatMethod& oat_method,
Richard Uhlerfbef44d2014-12-23 09:48:51 -080066 const DexFile& dex_file)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070067 REQUIRES_SHARED(Locks::mutator_lock_) {
Logan Chieneeb7edf2012-03-09 20:38:39 +080068 const CompiledMethod* compiled_method =
Richard Uhlerfbef44d2014-12-23 09:48:51 -080069 compiler_driver_->GetCompiledMethod(MethodReference(&dex_file,
Brian Carlstrom51c24672013-07-11 16:00:56 -070070 method->GetDexMethodIndex()));
Logan Chieneeb7edf2012-03-09 20:38:39 +080071
Ian Rogersd4c4d952014-10-16 20:31:53 -070072 if (compiled_method == nullptr) {
David Sehr709b0702016-10-13 09:12:37 -070073 EXPECT_TRUE(oat_method.GetQuickCode() == nullptr) << method->PrettyMethod() << " "
Ian Rogersd4c4d952014-10-16 20:31:53 -070074 << oat_method.GetQuickCode();
Ian Rogersef7d42f2014-01-06 12:55:46 -080075 EXPECT_EQ(oat_method.GetFrameSizeInBytes(), 0U);
Logan Chieneeb7edf2012-03-09 20:38:39 +080076 EXPECT_EQ(oat_method.GetCoreSpillMask(), 0U);
77 EXPECT_EQ(oat_method.GetFpSpillMask(), 0U);
78 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -080079 const void* quick_oat_code = oat_method.GetQuickCode();
David Sehr709b0702016-10-13 09:12:37 -070080 EXPECT_TRUE(quick_oat_code != nullptr) << method->PrettyMethod();
Elliott Hughes956af0f2014-12-11 14:34:28 -080081 EXPECT_EQ(oat_method.GetFrameSizeInBytes(), compiled_method->GetFrameSizeInBytes());
82 EXPECT_EQ(oat_method.GetCoreSpillMask(), compiled_method->GetCoreSpillMask());
83 EXPECT_EQ(oat_method.GetFpSpillMask(), compiled_method->GetFpSpillMask());
84 uintptr_t oat_code_aligned = RoundDown(reinterpret_cast<uintptr_t>(quick_oat_code), 2);
85 quick_oat_code = reinterpret_cast<const void*>(oat_code_aligned);
Vladimir Marko35831e82015-09-11 11:59:18 +010086 ArrayRef<const uint8_t> quick_code = compiled_method->GetQuickCode();
87 EXPECT_FALSE(quick_code.empty());
88 size_t code_size = quick_code.size() * sizeof(quick_code[0]);
Elliott Hughes956af0f2014-12-11 14:34:28 -080089 EXPECT_EQ(0, memcmp(quick_oat_code, &quick_code[0], code_size))
David Sehr709b0702016-10-13 09:12:37 -070090 << method->PrettyMethod() << " " << code_size;
Elliott Hughes956af0f2014-12-11 14:34:28 -080091 CHECK_EQ(0, memcmp(quick_oat_code, &quick_code[0], code_size));
Logan Chieneeb7edf2012-03-09 20:38:39 +080092 }
93 }
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -080094
95 void SetupCompiler(Compiler::Kind compiler_kind,
96 InstructionSet insn_set,
97 const std::vector<std::string>& compiler_options,
98 /*out*/std::string* error_msg) {
99 ASSERT_TRUE(error_msg != nullptr);
Andreas Gampe0415b4e2015-01-06 15:17:07 -0800100 insn_features_ = InstructionSetFeatures::FromVariant(insn_set, "default", error_msg);
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800101 ASSERT_TRUE(insn_features_ != nullptr) << error_msg;
102 compiler_options_.reset(new CompilerOptions);
103 for (const std::string& option : compiler_options) {
104 compiler_options_->ParseCompilerOption(option, Usage);
105 }
106 verification_results_.reset(new VerificationResults(compiler_options_.get()));
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800107 callbacks_.reset(new QuickCompilerCallbacks(verification_results_.get(),
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800108 CompilerCallbacks::CallbackMode::kCompileApp));
109 Runtime::Current()->SetCompilerCallbacks(callbacks_.get());
110 timer_.reset(new CumulativeLogger("Compilation times"));
111 compiler_driver_.reset(new CompilerDriver(compiler_options_.get(),
112 verification_results_.get(),
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800113 compiler_kind,
114 insn_set,
115 insn_features_.get(),
Vladimir Marko944da602016-02-19 12:27:55 +0000116 /* image_classes */ nullptr,
117 /* compiled_classes */ nullptr,
118 /* compiled_methods */ nullptr,
119 /* thread_count */ 2,
120 /* dump_stats */ true,
121 /* dump_passes */ true,
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800122 timer_.get(),
Vladimir Marko944da602016-02-19 12:27:55 +0000123 /* swap_fd */ -1,
124 /* profile_compilation_info */ nullptr));
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800125 }
126
David Brazdil7b49e6c2016-09-01 11:06:18 +0100127 bool WriteElf(File* vdex_file,
128 File* oat_file,
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800129 const std::vector<const DexFile*>& dex_files,
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800130 SafeMap<std::string, std::string>& key_value_store,
131 bool verify) {
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800132 TimingLogger timings("WriteElf", false, false);
Jeff Hao608f2ce2016-10-19 11:17:11 -0700133 OatWriter oat_writer(/*compiling_boot_image*/false,
134 &timings,
135 /*profile_compilation_info*/nullptr);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000136 for (const DexFile* dex_file : dex_files) {
137 ArrayRef<const uint8_t> raw_dex_file(
138 reinterpret_cast<const uint8_t*>(&dex_file->GetHeader()),
139 dex_file->GetHeader().file_size_);
140 if (!oat_writer.AddRawDexFileSource(raw_dex_file,
141 dex_file->GetLocation().c_str(),
142 dex_file->GetLocationChecksum())) {
143 return false;
144 }
145 }
David Brazdil7b49e6c2016-09-01 11:06:18 +0100146 return DoWriteElf(vdex_file, oat_file, oat_writer, key_value_store, verify);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000147 }
148
David Brazdil7b49e6c2016-09-01 11:06:18 +0100149 bool WriteElf(File* vdex_file,
150 File* oat_file,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000151 const std::vector<const char*>& dex_filenames,
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800152 SafeMap<std::string, std::string>& key_value_store,
Jeff Haod9df7802017-02-06 16:41:16 -0800153 bool verify,
154 ProfileCompilationInfo* profile_compilation_info) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000155 TimingLogger timings("WriteElf", false, false);
Jeff Haod9df7802017-02-06 16:41:16 -0800156 OatWriter oat_writer(/*compiling_boot_image*/false, &timings, profile_compilation_info);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000157 for (const char* dex_filename : dex_filenames) {
158 if (!oat_writer.AddDexFileSource(dex_filename, dex_filename)) {
159 return false;
160 }
161 }
David Brazdil7b49e6c2016-09-01 11:06:18 +0100162 return DoWriteElf(vdex_file, oat_file, oat_writer, key_value_store, verify);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000163 }
164
David Brazdil7b49e6c2016-09-01 11:06:18 +0100165 bool WriteElf(File* vdex_file,
166 File* oat_file,
Andreas Gampe43e10b02016-07-15 17:17:34 -0700167 File&& zip_fd,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000168 const char* location,
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800169 SafeMap<std::string, std::string>& key_value_store,
170 bool verify) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000171 TimingLogger timings("WriteElf", false, false);
Jeff Hao608f2ce2016-10-19 11:17:11 -0700172 OatWriter oat_writer(/*compiling_boot_image*/false,
173 &timings,
174 /*profile_compilation_info*/nullptr);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000175 if (!oat_writer.AddZippedDexFilesSource(std::move(zip_fd), location)) {
176 return false;
177 }
David Brazdil7b49e6c2016-09-01 11:06:18 +0100178 return DoWriteElf(vdex_file, oat_file, oat_writer, key_value_store, verify);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000179 }
180
David Brazdil7b49e6c2016-09-01 11:06:18 +0100181 bool DoWriteElf(File* vdex_file,
182 File* oat_file,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000183 OatWriter& oat_writer,
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800184 SafeMap<std::string, std::string>& key_value_store,
185 bool verify) {
Vladimir Marko10c13562015-11-25 14:33:36 +0000186 std::unique_ptr<ElfWriter> elf_writer = CreateElfWriterQuick(
187 compiler_driver_->GetInstructionSet(),
David Srbecky5d811202016-03-08 13:21:22 +0000188 compiler_driver_->GetInstructionSetFeatures(),
Vladimir Marko10c13562015-11-25 14:33:36 +0000189 &compiler_driver_->GetCompilerOptions(),
David Brazdil7b49e6c2016-09-01 11:06:18 +0100190 oat_file);
Vladimir Marko10c13562015-11-25 14:33:36 +0000191 elf_writer->Start();
David Brazdil7b49e6c2016-09-01 11:06:18 +0100192 OutputStream* oat_rodata = elf_writer->StartRoData();
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000193 std::unique_ptr<MemMap> opened_dex_files_map;
194 std::vector<std::unique_ptr<const DexFile>> opened_dex_files;
David Brazdil7b49e6c2016-09-01 11:06:18 +0100195 if (!oat_writer.WriteAndOpenDexFiles(kIsVdexEnabled ? vdex_file : oat_file,
196 oat_rodata,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000197 compiler_driver_->GetInstructionSet(),
198 compiler_driver_->GetInstructionSetFeatures(),
199 &key_value_store,
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800200 verify,
Nicolas Geoffray81f57d12016-12-20 13:17:09 +0000201 /* update_input_vdex */ false,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000202 &opened_dex_files_map,
203 &opened_dex_files)) {
204 return false;
205 }
Vladimir Markoaad75c62016-10-03 08:46:48 +0000206
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000207 Runtime* runtime = Runtime::Current();
208 ClassLinker* const class_linker = runtime->GetClassLinker();
209 std::vector<const DexFile*> dex_files;
210 for (const std::unique_ptr<const DexFile>& dex_file : opened_dex_files) {
211 dex_files.push_back(dex_file.get());
212 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierf284d442016-06-02 11:48:30 -0700213 class_linker->RegisterDexFile(*dex_file, nullptr);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000214 }
Vladimir Marko944da602016-02-19 12:27:55 +0000215 linker::MultiOatRelativePatcher patcher(compiler_driver_->GetInstructionSet(),
216 instruction_set_features_.get());
Nicolas Geoffray4acefd32016-10-24 13:14:58 +0100217 oat_writer.Initialize(compiler_driver_.get(), nullptr, dex_files);
218 oat_writer.PrepareLayout(&patcher);
Vladimir Marko944da602016-02-19 12:27:55 +0000219 size_t rodata_size = oat_writer.GetOatHeader().GetExecutableOffset();
David Brazdil7b49e6c2016-09-01 11:06:18 +0100220 size_t text_size = oat_writer.GetOatSize() - rodata_size;
Vladimir Markoaad75c62016-10-03 08:46:48 +0000221 elf_writer->PrepareDynamicSection(rodata_size,
222 text_size,
223 oat_writer.GetBssSize(),
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100224 oat_writer.GetBssMethodsOffset(),
Vladimir Markoaad75c62016-10-03 08:46:48 +0000225 oat_writer.GetBssRootsOffset());
Vladimir Marko944da602016-02-19 12:27:55 +0000226
Nicolas Geoffrayf54e5df2016-12-01 10:45:08 +0000227 if (kIsVdexEnabled) {
228 std::unique_ptr<BufferedOutputStream> vdex_out(
229 MakeUnique<BufferedOutputStream>(MakeUnique<FileOutputStream>(vdex_file)));
230 if (!oat_writer.WriteVerifierDeps(vdex_out.get(), nullptr)) {
231 return false;
232 }
233 if (!oat_writer.WriteChecksumsAndVdexHeader(vdex_out.get())) {
234 return false;
235 }
236 }
237
David Brazdil7b49e6c2016-09-01 11:06:18 +0100238 if (!oat_writer.WriteRodata(oat_rodata)) {
Vladimir Marko10c13562015-11-25 14:33:36 +0000239 return false;
240 }
David Brazdil7b49e6c2016-09-01 11:06:18 +0100241 elf_writer->EndRoData(oat_rodata);
Vladimir Marko10c13562015-11-25 14:33:36 +0000242
243 OutputStream* text = elf_writer->StartText();
244 if (!oat_writer.WriteCode(text)) {
245 return false;
246 }
247 elf_writer->EndText(text);
248
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000249 if (!oat_writer.WriteHeader(elf_writer->GetStream(), 42U, 4096U, 0)) {
250 return false;
251 }
252
Vladimir Marko10c13562015-11-25 14:33:36 +0000253 elf_writer->WriteDynamicSection();
Vladimir Marko49b0f452015-12-10 13:49:19 +0000254 elf_writer->WriteDebugInfo(oat_writer.GetMethodDebugInfo());
Vladimir Marko10c13562015-11-25 14:33:36 +0000255
Vladimir Markoaad75c62016-10-03 08:46:48 +0000256 if (!elf_writer->End()) {
257 return false;
258 }
259
260 opened_dex_files_maps_.emplace_back(std::move(opened_dex_files_map));
261 for (std::unique_ptr<const DexFile>& dex_file : opened_dex_files) {
262 opened_dex_files_.emplace_back(dex_file.release());
263 }
264 return true;
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800265 }
266
Jeff Haod9df7802017-02-06 16:41:16 -0800267 void TestDexFileInput(bool verify, bool low_4gb, bool use_profile);
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800268 void TestZipFileInput(bool verify);
Jeff Hao41b2f532017-03-02 16:36:31 -0800269 void TestZipFileInputWithEmptyDex();
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800270
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800271 std::unique_ptr<const InstructionSetFeatures> insn_features_;
272 std::unique_ptr<QuickCompilerCallbacks> callbacks_;
Vladimir Markoaad75c62016-10-03 08:46:48 +0000273
274 std::vector<std::unique_ptr<MemMap>> opened_dex_files_maps_;
275 std::vector<std::unique_ptr<const DexFile>> opened_dex_files_;
Logan Chieneeb7edf2012-03-09 20:38:39 +0800276};
Brian Carlstrome24fa612011-09-29 00:53:55 -0700277
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000278class ZipBuilder {
279 public:
280 explicit ZipBuilder(File* zip_file) : zip_file_(zip_file) { }
281
282 bool AddFile(const char* location, const void* data, size_t size) {
283 off_t offset = lseek(zip_file_->Fd(), 0, SEEK_CUR);
284 if (offset == static_cast<off_t>(-1)) {
285 return false;
286 }
287
288 ZipFileHeader file_header;
289 file_header.crc32 = crc32(0u, reinterpret_cast<const Bytef*>(data), size);
290 file_header.compressed_size = size;
291 file_header.uncompressed_size = size;
292 file_header.filename_length = strlen(location);
293
294 if (!zip_file_->WriteFully(&file_header, sizeof(file_header)) ||
295 !zip_file_->WriteFully(location, file_header.filename_length) ||
296 !zip_file_->WriteFully(data, size)) {
297 return false;
298 }
299
300 CentralDirectoryFileHeader cdfh;
301 cdfh.crc32 = file_header.crc32;
302 cdfh.compressed_size = size;
303 cdfh.uncompressed_size = size;
304 cdfh.filename_length = file_header.filename_length;
305 cdfh.relative_offset_of_local_file_header = offset;
306 file_data_.push_back(FileData { cdfh, location });
307 return true;
308 }
309
310 bool Finish() {
311 off_t offset = lseek(zip_file_->Fd(), 0, SEEK_CUR);
312 if (offset == static_cast<off_t>(-1)) {
313 return false;
314 }
315
316 size_t central_directory_size = 0u;
317 for (const FileData& file_data : file_data_) {
318 if (!zip_file_->WriteFully(&file_data.cdfh, sizeof(file_data.cdfh)) ||
319 !zip_file_->WriteFully(file_data.location, file_data.cdfh.filename_length)) {
320 return false;
321 }
322 central_directory_size += sizeof(file_data.cdfh) + file_data.cdfh.filename_length;
323 }
324 EndOfCentralDirectoryRecord eocd_record;
325 eocd_record.number_of_central_directory_records_on_this_disk = file_data_.size();
326 eocd_record.total_number_of_central_directory_records = file_data_.size();
327 eocd_record.size_of_central_directory = central_directory_size;
328 eocd_record.offset_of_start_of_central_directory = offset;
329 return
330 zip_file_->WriteFully(&eocd_record, sizeof(eocd_record)) &&
331 zip_file_->Flush() == 0;
332 }
333
334 private:
335 struct PACKED(1) ZipFileHeader {
336 uint32_t signature = 0x04034b50;
337 uint16_t version_needed_to_extract = 10;
338 uint16_t general_purpose_bit_flag = 0;
339 uint16_t compression_method = 0; // 0 = store only.
340 uint16_t file_last_modification_time = 0u;
341 uint16_t file_last_modification_date = 0u;
342 uint32_t crc32;
343 uint32_t compressed_size;
344 uint32_t uncompressed_size;
345 uint16_t filename_length;
346 uint16_t extra_field_length = 0u; // No extra fields.
347 };
348
349 struct PACKED(1) CentralDirectoryFileHeader {
350 uint32_t signature = 0x02014b50;
351 uint16_t version_made_by = 10;
352 uint16_t version_needed_to_extract = 10;
353 uint16_t general_purpose_bit_flag = 0;
354 uint16_t compression_method = 0; // 0 = store only.
355 uint16_t file_last_modification_time = 0u;
356 uint16_t file_last_modification_date = 0u;
357 uint32_t crc32;
358 uint32_t compressed_size;
359 uint32_t uncompressed_size;
360 uint16_t filename_length;
361 uint16_t extra_field_length = 0u; // No extra fields.
362 uint16_t file_comment_length = 0u; // No file comment.
363 uint16_t disk_number_where_file_starts = 0u;
364 uint16_t internal_file_attributes = 0u;
365 uint32_t external_file_attributes = 0u;
366 uint32_t relative_offset_of_local_file_header;
367 };
368
369 struct PACKED(1) EndOfCentralDirectoryRecord {
370 uint32_t signature = 0x06054b50;
371 uint16_t number_of_this_disk = 0u;
372 uint16_t disk_where_central_directory_starts = 0u;
373 uint16_t number_of_central_directory_records_on_this_disk;
374 uint16_t total_number_of_central_directory_records;
375 uint32_t size_of_central_directory;
376 uint32_t offset_of_start_of_central_directory;
377 uint16_t comment_length = 0u; // No file comment.
378 };
379
380 struct FileData {
381 CentralDirectoryFileHeader cdfh;
382 const char* location;
383 };
384
385 File* zip_file_;
386 std::vector<FileData> file_data_;
387};
388
Brian Carlstrome24fa612011-09-29 00:53:55 -0700389TEST_F(OatTest, WriteRead) {
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800390 TimingLogger timings("OatTest::WriteRead", false, false);
Jesse Wilson254db0f2011-11-16 16:44:11 -0500391 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700392
Ian Rogersef7d42f2014-01-06 12:55:46 -0800393 // TODO: make selectable.
Elliott Hughes956af0f2014-12-11 14:34:28 -0800394 Compiler::Kind compiler_kind = Compiler::kQuick;
Ian Rogersa073c672013-07-30 17:43:55 -0700395 InstructionSet insn_set = kIsTargetBuild ? kThumb2 : kX86;
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700396 std::string error_msg;
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800397 SetupCompiler(compiler_kind, insn_set, std::vector<std::string>(), /*out*/ &error_msg);
398
Ian Rogersd4c4d952014-10-16 20:31:53 -0700399 jobject class_loader = nullptr;
Brian Carlstromba150c32013-08-27 17:31:03 -0700400 if (kCompile) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800401 TimingLogger timings2("OatTest::WriteRead", false, false);
Vladimir Markod1eaf0d2015-10-29 12:18:29 +0000402 compiler_driver_->SetDexFilesForOatFile(class_linker->GetBootClassPath());
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +0000403 compiler_driver_->CompileAll(
404 class_loader, class_linker->GetBootClassPath(), /* verifier_deps */ nullptr, &timings2);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700405 }
406
David Brazdil7b49e6c2016-09-01 11:06:18 +0100407 ScratchFile tmp_oat, tmp_vdex(tmp_oat, ".vdex");
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700408 SafeMap<std::string, std::string> key_value_store;
409 key_value_store.Put(OatHeader::kImageLocationKey, "lue.art");
David Brazdil7b49e6c2016-09-01 11:06:18 +0100410 bool success = WriteElf(tmp_vdex.GetFile(),
411 tmp_oat.GetFile(),
412 class_linker->GetBootClassPath(),
413 key_value_store,
414 false);
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700415 ASSERT_TRUE(success);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700416
Brian Carlstromba150c32013-08-27 17:31:03 -0700417 if (kCompile) { // OatWriter strips the code, regenerate to compare
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +0000418 compiler_driver_->CompileAll(
419 class_loader, class_linker->GetBootClassPath(), /* verifier_deps */ nullptr, &timings);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700420 }
David Brazdil7b49e6c2016-09-01 11:06:18 +0100421 std::unique_ptr<OatFile> oat_file(OatFile::Open(tmp_oat.GetFilename(),
422 tmp_oat.GetFilename(),
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800423 nullptr,
424 nullptr,
425 false,
426 /*low_4gb*/true,
427 nullptr,
428 &error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700429 ASSERT_TRUE(oat_file.get() != nullptr) << error_msg;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700430 const OatHeader& oat_header = oat_file->GetOatHeader();
Brian Carlstromf852fb22012-10-19 11:01:58 -0700431 ASSERT_TRUE(oat_header.IsValid());
Yi Kong9515b512015-11-11 14:36:07 +0000432 ASSERT_EQ(class_linker->GetBootClassPath().size(), oat_header.GetDexFileCount()); // core
Brian Carlstrom28db0122012-10-18 16:20:41 -0700433 ASSERT_EQ(42U, oat_header.GetImageFileLocationOatChecksum());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800434 ASSERT_EQ(4096U, oat_header.GetImageFileLocationOatDataBegin());
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700435 ASSERT_EQ("lue.art", std::string(oat_header.GetStoreValueByKey(OatHeader::kImageLocationKey)));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700436
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800437 ASSERT_TRUE(java_lang_dex_file_ != nullptr);
438 const DexFile& dex_file = *java_lang_dex_file_;
439 uint32_t dex_file_checksum = dex_file.GetLocationChecksum();
440 const OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(dex_file.GetLocation().c_str(),
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700441 &dex_file_checksum);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700442 ASSERT_TRUE(oat_dex_file != nullptr);
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800443 CHECK_EQ(dex_file.GetLocationChecksum(), oat_dex_file->GetDexFileLocationChecksum());
Vladimir Markof4da6752014-08-01 19:04:18 +0100444 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700445 auto pointer_size = class_linker->GetImagePointerSize();
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800446 for (size_t i = 0; i < dex_file.NumClassDefs(); i++) {
447 const DexFile::ClassDef& class_def = dex_file.GetClassDef(i);
448 const uint8_t* class_data = dex_file.GetClassData(class_def);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700449
Brian Carlstromba150c32013-08-27 17:31:03 -0700450 size_t num_virtual_methods = 0;
Ian Rogersd4c4d952014-10-16 20:31:53 -0700451 if (class_data != nullptr) {
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800452 ClassDataItemIterator it(dex_file, class_data);
Ian Rogers0571d352011-11-03 19:51:38 -0700453 num_virtual_methods = it.NumVirtualMethods();
454 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700455
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800456 const char* descriptor = dex_file.GetClassDescriptor(class_def);
Mathieu Chartier9865bde2015-12-21 09:58:16 -0800457 mirror::Class* klass = class_linker->FindClass(soa.Self(),
458 descriptor,
459 ScopedNullHandle<mirror::ClassLoader>());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700460
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100461 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(i);
462 CHECK_EQ(mirror::Class::Status::kStatusNotReady, oat_class.GetStatus()) << descriptor;
Brian Carlstromba150c32013-08-27 17:31:03 -0700463 CHECK_EQ(kCompile ? OatClassType::kOatClassAllCompiled : OatClassType::kOatClassNoneCompiled,
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100464 oat_class.GetType()) << descriptor;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700465
466 size_t method_index = 0;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700467 for (auto& m : klass->GetDirectMethods(pointer_size)) {
468 CheckMethod(&m, oat_class.GetOatMethod(method_index), dex_file);
469 ++method_index;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700470 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700471 size_t visited_virtuals = 0;
Alex Lighte64300b2015-12-15 15:02:47 -0800472 // TODO We should also check copied methods in this test.
473 for (auto& m : klass->GetDeclaredVirtualMethods(pointer_size)) {
Alex Lightfcea56f2016-02-17 11:59:05 -0800474 if (!klass->IsInterface()) {
Alex Light36121492016-02-22 13:43:29 -0800475 EXPECT_FALSE(m.IsCopied());
Alex Lightfcea56f2016-02-17 11:59:05 -0800476 }
Alex Lighte64300b2015-12-15 15:02:47 -0800477 CheckMethod(&m, oat_class.GetOatMethod(method_index), dex_file);
478 ++method_index;
479 ++visited_virtuals;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700480 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700481 EXPECT_EQ(visited_virtuals, num_virtual_methods);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700482 }
483}
484
Brian Carlstrom341df942012-06-27 12:29:22 -0700485TEST_F(OatTest, OatHeaderSizeCheck) {
486 // If this test is failing and you have to update these constants,
487 // it is time to update OatHeader::kOatVersion
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100488 EXPECT_EQ(76U, sizeof(OatHeader));
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800489 EXPECT_EQ(4U, sizeof(OatMethodOffsets));
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700490 EXPECT_EQ(24U, sizeof(OatQuickMethodHeader));
Nicolas Geoffrayb048cb72017-01-23 22:50:24 +0000491 EXPECT_EQ(161 * static_cast<size_t>(GetInstructionSetPointerSize(kRuntimeISA)),
Andreas Gampe542451c2016-07-26 09:02:02 -0700492 sizeof(QuickEntryPoints));
Brian Carlstrom341df942012-06-27 12:29:22 -0700493}
494
Brian Carlstromf852fb22012-10-19 11:01:58 -0700495TEST_F(OatTest, OatHeaderIsValid) {
Jeff Hao5d3baf62016-05-23 19:17:04 -0700496 InstructionSet insn_set = kX86;
497 std::string error_msg;
498 std::unique_ptr<const InstructionSetFeatures> insn_features(
499 InstructionSetFeatures::FromVariant(insn_set, "default", &error_msg));
500 ASSERT_TRUE(insn_features.get() != nullptr) << error_msg;
501 std::unique_ptr<OatHeader> oat_header(OatHeader::Create(insn_set,
502 insn_features.get(),
503 0u,
504 nullptr));
505 ASSERT_NE(oat_header.get(), nullptr);
506 ASSERT_TRUE(oat_header->IsValid());
Brian Carlstromf852fb22012-10-19 11:01:58 -0700507
Jeff Hao5d3baf62016-05-23 19:17:04 -0700508 char* magic = const_cast<char*>(oat_header->GetMagic());
509 strcpy(magic, ""); // bad magic
510 ASSERT_FALSE(oat_header->IsValid());
511 strcpy(magic, "oat\n000"); // bad version
512 ASSERT_FALSE(oat_header->IsValid());
Brian Carlstromf852fb22012-10-19 11:01:58 -0700513}
514
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800515TEST_F(OatTest, EmptyTextSection) {
516 TimingLogger timings("OatTest::EmptyTextSection", false, false);
517
518 // TODO: make selectable.
519 Compiler::Kind compiler_kind = Compiler::kQuick;
520 InstructionSet insn_set = kRuntimeISA;
521 if (insn_set == kArm) insn_set = kThumb2;
522 std::string error_msg;
523 std::vector<std::string> compiler_options;
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100524 compiler_options.push_back("--compiler-filter=extract");
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800525 SetupCompiler(compiler_kind, insn_set, compiler_options, /*out*/ &error_msg);
526
527 jobject class_loader;
528 {
529 ScopedObjectAccess soa(Thread::Current());
530 class_loader = LoadDex("Main");
531 }
532 ASSERT_TRUE(class_loader != nullptr);
533 std::vector<const DexFile*> dex_files = GetDexFiles(class_loader);
534 ASSERT_TRUE(!dex_files.empty());
535
536 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
537 for (const DexFile* dex_file : dex_files) {
538 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartier0795f232016-09-27 18:43:30 -0700539 class_linker->RegisterDexFile(*dex_file,
Mathieu Chartier1cc62e42016-10-03 18:01:28 -0700540 soa.Decode<mirror::ClassLoader>(class_loader).Ptr());
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800541 }
542 compiler_driver_->SetDexFilesForOatFile(dex_files);
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +0000543 compiler_driver_->CompileAll(class_loader, dex_files, /* verifier_deps */ nullptr, &timings);
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800544
David Brazdil7b49e6c2016-09-01 11:06:18 +0100545 ScratchFile tmp_oat, tmp_vdex(tmp_oat, ".vdex");
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800546 SafeMap<std::string, std::string> key_value_store;
547 key_value_store.Put(OatHeader::kImageLocationKey, "test.art");
David Brazdil7b49e6c2016-09-01 11:06:18 +0100548 bool success = WriteElf(tmp_vdex.GetFile(), tmp_oat.GetFile(), dex_files, key_value_store, false);
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800549 ASSERT_TRUE(success);
550
David Brazdil7b49e6c2016-09-01 11:06:18 +0100551 std::unique_ptr<OatFile> oat_file(OatFile::Open(tmp_oat.GetFilename(),
552 tmp_oat.GetFilename(),
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800553 nullptr,
554 nullptr,
555 false,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800556 /*low_4gb*/false,
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800557 nullptr,
558 &error_msg));
559 ASSERT_TRUE(oat_file != nullptr);
David Brazdil7b49e6c2016-09-01 11:06:18 +0100560 EXPECT_LT(static_cast<size_t>(oat_file->Size()),
561 static_cast<size_t>(tmp_oat.GetFile()->GetLength()));
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800562}
563
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800564static void MaybeModifyDexFileToFail(bool verify, std::unique_ptr<const DexFile>& data) {
565 // If in verify mode (= fail the verifier mode), make sure we fail early. We'll fail already
566 // because of the missing map, but that may lead to out of bounds reads.
567 if (verify) {
568 const_cast<DexFile::Header*>(&data->GetHeader())->checksum_++;
569 }
570}
571
Jeff Haod9df7802017-02-06 16:41:16 -0800572void OatTest::TestDexFileInput(bool verify, bool low_4gb, bool use_profile) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000573 TimingLogger timings("OatTest::DexFileInput", false, false);
574
575 std::vector<const char*> input_filenames;
576
577 ScratchFile dex_file1;
578 TestDexFileBuilder builder1;
579 builder1.AddField("Lsome.TestClass;", "int", "someField");
580 builder1.AddMethod("Lsome.TestClass;", "()I", "foo");
581 std::unique_ptr<const DexFile> dex_file1_data = builder1.Build(dex_file1.GetFilename());
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800582
583 MaybeModifyDexFileToFail(verify, dex_file1_data);
584
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000585 bool success = dex_file1.GetFile()->WriteFully(&dex_file1_data->GetHeader(),
586 dex_file1_data->GetHeader().file_size_);
587 ASSERT_TRUE(success);
588 success = dex_file1.GetFile()->Flush() == 0;
589 ASSERT_TRUE(success);
590 input_filenames.push_back(dex_file1.GetFilename().c_str());
591
592 ScratchFile dex_file2;
593 TestDexFileBuilder builder2;
594 builder2.AddField("Land.AnotherTestClass;", "boolean", "someOtherField");
595 builder2.AddMethod("Land.AnotherTestClass;", "()J", "bar");
596 std::unique_ptr<const DexFile> dex_file2_data = builder2.Build(dex_file2.GetFilename());
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800597
598 MaybeModifyDexFileToFail(verify, dex_file2_data);
599
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000600 success = dex_file2.GetFile()->WriteFully(&dex_file2_data->GetHeader(),
601 dex_file2_data->GetHeader().file_size_);
602 ASSERT_TRUE(success);
603 success = dex_file2.GetFile()->Flush() == 0;
604 ASSERT_TRUE(success);
605 input_filenames.push_back(dex_file2.GetFilename().c_str());
606
David Brazdil7b49e6c2016-09-01 11:06:18 +0100607 ScratchFile oat_file, vdex_file(oat_file, ".vdex");
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000608 SafeMap<std::string, std::string> key_value_store;
609 key_value_store.Put(OatHeader::kImageLocationKey, "test.art");
Jeff Haod9df7802017-02-06 16:41:16 -0800610 std::unique_ptr<ProfileCompilationInfo>
611 profile_compilation_info(use_profile ? new ProfileCompilationInfo() : nullptr);
David Brazdil7b49e6c2016-09-01 11:06:18 +0100612 success = WriteElf(vdex_file.GetFile(),
613 oat_file.GetFile(),
614 input_filenames,
615 key_value_store,
Jeff Haod9df7802017-02-06 16:41:16 -0800616 verify,
617 profile_compilation_info.get());
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800618
619 // In verify mode, we expect failure.
620 if (verify) {
621 ASSERT_FALSE(success);
622 return;
623 }
624
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000625 ASSERT_TRUE(success);
626
627 std::string error_msg;
628 std::unique_ptr<OatFile> opened_oat_file(OatFile::Open(oat_file.GetFilename(),
629 oat_file.GetFilename(),
630 nullptr,
631 nullptr,
632 false,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800633 low_4gb,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000634 nullptr,
635 &error_msg));
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800636 if (low_4gb) {
637 uintptr_t begin = reinterpret_cast<uintptr_t>(opened_oat_file->Begin());
638 EXPECT_EQ(begin, static_cast<uint32_t>(begin));
639 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000640 ASSERT_TRUE(opened_oat_file != nullptr);
641 ASSERT_EQ(2u, opened_oat_file->GetOatDexFiles().size());
642 std::unique_ptr<const DexFile> opened_dex_file1 =
643 opened_oat_file->GetOatDexFiles()[0]->OpenDexFile(&error_msg);
644 std::unique_ptr<const DexFile> opened_dex_file2 =
645 opened_oat_file->GetOatDexFiles()[1]->OpenDexFile(&error_msg);
646
647 ASSERT_EQ(dex_file1_data->GetHeader().file_size_, opened_dex_file1->GetHeader().file_size_);
648 ASSERT_EQ(0, memcmp(&dex_file1_data->GetHeader(),
649 &opened_dex_file1->GetHeader(),
650 dex_file1_data->GetHeader().file_size_));
651 ASSERT_EQ(dex_file1_data->GetLocation(), opened_dex_file1->GetLocation());
652
653 ASSERT_EQ(dex_file2_data->GetHeader().file_size_, opened_dex_file2->GetHeader().file_size_);
654 ASSERT_EQ(0, memcmp(&dex_file2_data->GetHeader(),
655 &opened_dex_file2->GetHeader(),
656 dex_file2_data->GetHeader().file_size_));
657 ASSERT_EQ(dex_file2_data->GetLocation(), opened_dex_file2->GetLocation());
658}
659
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800660TEST_F(OatTest, DexFileInputCheckOutput) {
Jeff Haod9df7802017-02-06 16:41:16 -0800661 TestDexFileInput(/*verify*/false, /*low_4gb*/false, /*use_profile*/false);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800662}
663
664TEST_F(OatTest, DexFileInputCheckOutputLow4GB) {
Jeff Haod9df7802017-02-06 16:41:16 -0800665 TestDexFileInput(/*verify*/false, /*low_4gb*/true, /*use_profile*/false);
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800666}
667
668TEST_F(OatTest, DexFileInputCheckVerifier) {
Jeff Haod9df7802017-02-06 16:41:16 -0800669 TestDexFileInput(/*verify*/true, /*low_4gb*/false, /*use_profile*/false);
670}
671
672TEST_F(OatTest, DexFileFailsVerifierWithLayout) {
673 TestDexFileInput(/*verify*/true, /*low_4gb*/false, /*use_profile*/true);
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800674}
675
676void OatTest::TestZipFileInput(bool verify) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000677 TimingLogger timings("OatTest::DexFileInput", false, false);
678
679 ScratchFile zip_file;
680 ZipBuilder zip_builder(zip_file.GetFile());
681
682 ScratchFile dex_file1;
683 TestDexFileBuilder builder1;
684 builder1.AddField("Lsome.TestClass;", "long", "someField");
685 builder1.AddMethod("Lsome.TestClass;", "()D", "foo");
686 std::unique_ptr<const DexFile> dex_file1_data = builder1.Build(dex_file1.GetFilename());
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800687
688 MaybeModifyDexFileToFail(verify, dex_file1_data);
689
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000690 bool success = dex_file1.GetFile()->WriteFully(&dex_file1_data->GetHeader(),
691 dex_file1_data->GetHeader().file_size_);
692 ASSERT_TRUE(success);
693 success = dex_file1.GetFile()->Flush() == 0;
694 ASSERT_TRUE(success);
695 success = zip_builder.AddFile("classes.dex",
696 &dex_file1_data->GetHeader(),
697 dex_file1_data->GetHeader().file_size_);
698 ASSERT_TRUE(success);
699
700 ScratchFile dex_file2;
701 TestDexFileBuilder builder2;
702 builder2.AddField("Land.AnotherTestClass;", "boolean", "someOtherField");
703 builder2.AddMethod("Land.AnotherTestClass;", "()J", "bar");
704 std::unique_ptr<const DexFile> dex_file2_data = builder2.Build(dex_file2.GetFilename());
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800705
706 MaybeModifyDexFileToFail(verify, dex_file2_data);
707
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000708 success = dex_file2.GetFile()->WriteFully(&dex_file2_data->GetHeader(),
709 dex_file2_data->GetHeader().file_size_);
710 ASSERT_TRUE(success);
711 success = dex_file2.GetFile()->Flush() == 0;
712 ASSERT_TRUE(success);
713 success = zip_builder.AddFile("classes2.dex",
714 &dex_file2_data->GetHeader(),
715 dex_file2_data->GetHeader().file_size_);
716 ASSERT_TRUE(success);
717
718 success = zip_builder.Finish();
719 ASSERT_TRUE(success) << strerror(errno);
720
721 SafeMap<std::string, std::string> key_value_store;
722 key_value_store.Put(OatHeader::kImageLocationKey, "test.art");
723 {
724 // Test using the AddDexFileSource() interface with the zip file.
725 std::vector<const char*> input_filenames { zip_file.GetFilename().c_str() }; // NOLINT [readability/braces] [4]
726
David Brazdil7b49e6c2016-09-01 11:06:18 +0100727 ScratchFile oat_file, vdex_file(oat_file, ".vdex");
Jeff Haod9df7802017-02-06 16:41:16 -0800728 success = WriteElf(vdex_file.GetFile(), oat_file.GetFile(), input_filenames,
729 key_value_store, verify, /*profile_compilation_info*/nullptr);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000730
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800731 if (verify) {
732 ASSERT_FALSE(success);
733 } else {
734 ASSERT_TRUE(success);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000735
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800736 std::string error_msg;
737 std::unique_ptr<OatFile> opened_oat_file(OatFile::Open(oat_file.GetFilename(),
738 oat_file.GetFilename(),
739 nullptr,
740 nullptr,
741 false,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800742 /*low_4gb*/false,
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800743 nullptr,
744 &error_msg));
745 ASSERT_TRUE(opened_oat_file != nullptr);
746 ASSERT_EQ(2u, opened_oat_file->GetOatDexFiles().size());
747 std::unique_ptr<const DexFile> opened_dex_file1 =
748 opened_oat_file->GetOatDexFiles()[0]->OpenDexFile(&error_msg);
749 std::unique_ptr<const DexFile> opened_dex_file2 =
750 opened_oat_file->GetOatDexFiles()[1]->OpenDexFile(&error_msg);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000751
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800752 ASSERT_EQ(dex_file1_data->GetHeader().file_size_, opened_dex_file1->GetHeader().file_size_);
753 ASSERT_EQ(0, memcmp(&dex_file1_data->GetHeader(),
754 &opened_dex_file1->GetHeader(),
755 dex_file1_data->GetHeader().file_size_));
756 ASSERT_EQ(DexFile::GetMultiDexLocation(0, zip_file.GetFilename().c_str()),
757 opened_dex_file1->GetLocation());
758
759 ASSERT_EQ(dex_file2_data->GetHeader().file_size_, opened_dex_file2->GetHeader().file_size_);
760 ASSERT_EQ(0, memcmp(&dex_file2_data->GetHeader(),
761 &opened_dex_file2->GetHeader(),
762 dex_file2_data->GetHeader().file_size_));
763 ASSERT_EQ(DexFile::GetMultiDexLocation(1, zip_file.GetFilename().c_str()),
764 opened_dex_file2->GetLocation());
765 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000766 }
767
768 {
769 // Test using the AddZipDexFileSource() interface with the zip file handle.
Andreas Gampe43e10b02016-07-15 17:17:34 -0700770 File zip_fd(dup(zip_file.GetFd()), /* check_usage */ false);
771 ASSERT_NE(-1, zip_fd.Fd());
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000772
David Brazdil7b49e6c2016-09-01 11:06:18 +0100773 ScratchFile oat_file, vdex_file(oat_file, ".vdex");
774 success = WriteElf(vdex_file.GetFile(),
775 oat_file.GetFile(),
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000776 std::move(zip_fd),
777 zip_file.GetFilename().c_str(),
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800778 key_value_store,
779 verify);
780 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;
786 std::unique_ptr<OatFile> opened_oat_file(OatFile::Open(oat_file.GetFilename(),
787 oat_file.GetFilename(),
788 nullptr,
789 nullptr,
790 false,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800791 /*low_4gb*/false,
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800792 nullptr,
793 &error_msg));
794 ASSERT_TRUE(opened_oat_file != nullptr);
795 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_));
805 ASSERT_EQ(DexFile::GetMultiDexLocation(0, zip_file.GetFilename().c_str()),
806 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_));
812 ASSERT_EQ(DexFile::GetMultiDexLocation(1, zip_file.GetFilename().c_str()),
813 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) {
819 TestZipFileInput(false);
820}
821
822TEST_F(OatTest, ZipFileInputCheckVerifier) {
823 TestZipFileInput(true);
824}
825
Jeff Hao41b2f532017-03-02 16:36:31 -0800826void OatTest::TestZipFileInputWithEmptyDex() {
827 ScratchFile zip_file;
828 ZipBuilder zip_builder(zip_file.GetFile());
829 bool success = zip_builder.AddFile("classes.dex", nullptr, 0);
830 ASSERT_TRUE(success);
831 success = zip_builder.Finish();
832 ASSERT_TRUE(success) << strerror(errno);
833
834 SafeMap<std::string, std::string> key_value_store;
835 key_value_store.Put(OatHeader::kImageLocationKey, "test.art");
836 std::vector<const char*> input_filenames { zip_file.GetFilename().c_str() }; // NOLINT [readability/braces] [4]
837 ScratchFile oat_file, vdex_file(oat_file, ".vdex");
838 std::unique_ptr<ProfileCompilationInfo> profile_compilation_info(new ProfileCompilationInfo());
839 success = WriteElf(vdex_file.GetFile(), oat_file.GetFile(), input_filenames,
840 key_value_store, /*verify*/false, profile_compilation_info.get());
841 ASSERT_FALSE(success);
842}
843
844TEST_F(OatTest, ZipFileInputWithEmptyDex) {
845 TestZipFileInputWithEmptyDex();
846}
847
Jeff Hao5d3baf62016-05-23 19:17:04 -0700848TEST_F(OatTest, UpdateChecksum) {
849 InstructionSet insn_set = kX86;
850 std::string error_msg;
851 std::unique_ptr<const InstructionSetFeatures> insn_features(
852 InstructionSetFeatures::FromVariant(insn_set, "default", &error_msg));
853 ASSERT_TRUE(insn_features.get() != nullptr) << error_msg;
854 std::unique_ptr<OatHeader> oat_header(OatHeader::Create(insn_set,
855 insn_features.get(),
856 0u,
857 nullptr));
858 // The starting adler32 value is 1.
859 EXPECT_EQ(1U, oat_header->GetChecksum());
860
861 oat_header->UpdateChecksum(OatHeader::kOatMagic, sizeof(OatHeader::kOatMagic));
862 EXPECT_EQ(64291151U, oat_header->GetChecksum());
863
864 // Make sure that null data does not reset the checksum.
865 oat_header->UpdateChecksum(nullptr, 0);
866 EXPECT_EQ(64291151U, oat_header->GetChecksum());
867
868 oat_header->UpdateChecksum(OatHeader::kOatMagic, sizeof(OatHeader::kOatMagic));
869 EXPECT_EQ(216138397U, oat_header->GetChecksum());
870}
871
Brian Carlstrome24fa612011-09-29 00:53:55 -0700872} // namespace art