blob: 94585769b473bd80df868b0767bacedf27925aec [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
Ian Rogersd582fa42014-11-05 23:46:43 -080017#include "arch/instruction_set_features.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070018#include "art_method-inl.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070019#include "base/enums.h"
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -080020#include "base/unix_file/fd_file.h"
Andreas Gampe53c913b2014-08-12 23:19:23 -070021#include "class_linker.h"
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080022#include "common_compiler_test.h"
Vladimir Marko20f85592015-03-19 10:07:02 +000023#include "compiled_method.h"
Ian Rogerse63db272014-07-15 15:36:11 -070024#include "compiler.h"
David Srbecky4fda4eb2016-02-05 13:34:46 +000025#include "debug/method_debug_info.h"
Ian Rogerse63db272014-07-15 15:36:11 -070026#include "dex/quick_compiler_callbacks.h"
Mathieu Chartier5bdab122015-01-26 18:30:19 -080027#include "dex/verification_results.h"
Vladimir Marko20f85592015-03-19 10:07:02 +000028#include "driver/compiler_driver.h"
29#include "driver/compiler_options.h"
Vladimir Marko10c13562015-11-25 14:33:36 +000030#include "elf_writer.h"
31#include "elf_writer_quick.h"
Vladimir Marko7624d252014-05-02 14:40:15 +010032#include "entrypoints/quick/quick_entrypoints.h"
Vladimir Marko944da602016-02-19 12:27:55 +000033#include "linker/multi_oat_relative_patcher.h"
Vladimir Marko131980f2015-12-03 18:29:23 +000034#include "linker/vector_output_stream.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080035#include "mirror/class-inl.h"
Ian Rogerse63db272014-07-15 15:36:11 -070036#include "mirror/object-inl.h"
David Srbecky4fda4eb2016-02-05 13:34:46 +000037#include "mirror/object_array-inl.h"
Vladimir Marko7624d252014-05-02 14:40:15 +010038#include "oat_file-inl.h"
Ian Rogerse63db272014-07-15 15:36:11 -070039#include "oat_writer.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070040#include "scoped_thread_state_change-inl.h"
Vladimir Marko9bdf1082016-01-21 12:15:52 +000041#include "utils/test_dex_file_builder.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070042
Brian Carlstrome24fa612011-09-29 00:53:55 -070043namespace art {
44
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -080045NO_RETURN static void Usage(const char* fmt, ...) {
46 va_list ap;
47 va_start(ap, fmt);
48 std::string error;
49 StringAppendV(&error, fmt, ap);
50 LOG(FATAL) << error;
51 va_end(ap);
52 UNREACHABLE();
53}
54
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080055class OatTest : public CommonCompilerTest {
Logan Chieneeb7edf2012-03-09 20:38:39 +080056 protected:
Brian Carlstromba150c32013-08-27 17:31:03 -070057 static const bool kCompile = false; // DISABLED_ due to the time to compile libcore
58
Mathieu Chartiere401d142015-04-22 13:56:20 -070059 void CheckMethod(ArtMethod* method,
Logan Chieneeb7edf2012-03-09 20:38:39 +080060 const OatFile::OatMethod& oat_method,
Richard Uhlerfbef44d2014-12-23 09:48:51 -080061 const DexFile& dex_file)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070062 REQUIRES_SHARED(Locks::mutator_lock_) {
Logan Chieneeb7edf2012-03-09 20:38:39 +080063 const CompiledMethod* compiled_method =
Richard Uhlerfbef44d2014-12-23 09:48:51 -080064 compiler_driver_->GetCompiledMethod(MethodReference(&dex_file,
Brian Carlstrom51c24672013-07-11 16:00:56 -070065 method->GetDexMethodIndex()));
Logan Chieneeb7edf2012-03-09 20:38:39 +080066
Ian Rogersd4c4d952014-10-16 20:31:53 -070067 if (compiled_method == nullptr) {
David Sehr709b0702016-10-13 09:12:37 -070068 EXPECT_TRUE(oat_method.GetQuickCode() == nullptr) << method->PrettyMethod() << " "
Ian Rogersd4c4d952014-10-16 20:31:53 -070069 << oat_method.GetQuickCode();
Ian Rogersef7d42f2014-01-06 12:55:46 -080070 EXPECT_EQ(oat_method.GetFrameSizeInBytes(), 0U);
Logan Chieneeb7edf2012-03-09 20:38:39 +080071 EXPECT_EQ(oat_method.GetCoreSpillMask(), 0U);
72 EXPECT_EQ(oat_method.GetFpSpillMask(), 0U);
73 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -080074 const void* quick_oat_code = oat_method.GetQuickCode();
David Sehr709b0702016-10-13 09:12:37 -070075 EXPECT_TRUE(quick_oat_code != nullptr) << method->PrettyMethod();
Elliott Hughes956af0f2014-12-11 14:34:28 -080076 EXPECT_EQ(oat_method.GetFrameSizeInBytes(), compiled_method->GetFrameSizeInBytes());
77 EXPECT_EQ(oat_method.GetCoreSpillMask(), compiled_method->GetCoreSpillMask());
78 EXPECT_EQ(oat_method.GetFpSpillMask(), compiled_method->GetFpSpillMask());
79 uintptr_t oat_code_aligned = RoundDown(reinterpret_cast<uintptr_t>(quick_oat_code), 2);
80 quick_oat_code = reinterpret_cast<const void*>(oat_code_aligned);
Vladimir Marko35831e82015-09-11 11:59:18 +010081 ArrayRef<const uint8_t> quick_code = compiled_method->GetQuickCode();
82 EXPECT_FALSE(quick_code.empty());
83 size_t code_size = quick_code.size() * sizeof(quick_code[0]);
Elliott Hughes956af0f2014-12-11 14:34:28 -080084 EXPECT_EQ(0, memcmp(quick_oat_code, &quick_code[0], code_size))
David Sehr709b0702016-10-13 09:12:37 -070085 << method->PrettyMethod() << " " << code_size;
Elliott Hughes956af0f2014-12-11 14:34:28 -080086 CHECK_EQ(0, memcmp(quick_oat_code, &quick_code[0], code_size));
Logan Chieneeb7edf2012-03-09 20:38:39 +080087 }
88 }
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -080089
90 void SetupCompiler(Compiler::Kind compiler_kind,
91 InstructionSet insn_set,
92 const std::vector<std::string>& compiler_options,
93 /*out*/std::string* error_msg) {
94 ASSERT_TRUE(error_msg != nullptr);
Andreas Gampe0415b4e2015-01-06 15:17:07 -080095 insn_features_ = InstructionSetFeatures::FromVariant(insn_set, "default", error_msg);
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -080096 ASSERT_TRUE(insn_features_ != nullptr) << error_msg;
97 compiler_options_.reset(new CompilerOptions);
98 for (const std::string& option : compiler_options) {
99 compiler_options_->ParseCompilerOption(option, Usage);
100 }
101 verification_results_.reset(new VerificationResults(compiler_options_.get()));
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800102 callbacks_.reset(new QuickCompilerCallbacks(verification_results_.get(),
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800103 CompilerCallbacks::CallbackMode::kCompileApp));
104 Runtime::Current()->SetCompilerCallbacks(callbacks_.get());
105 timer_.reset(new CumulativeLogger("Compilation times"));
106 compiler_driver_.reset(new CompilerDriver(compiler_options_.get(),
107 verification_results_.get(),
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800108 compiler_kind,
109 insn_set,
110 insn_features_.get(),
Vladimir Marko944da602016-02-19 12:27:55 +0000111 /* image_classes */ nullptr,
112 /* compiled_classes */ nullptr,
113 /* compiled_methods */ nullptr,
114 /* thread_count */ 2,
115 /* dump_stats */ true,
116 /* dump_passes */ true,
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800117 timer_.get(),
Vladimir Marko944da602016-02-19 12:27:55 +0000118 /* swap_fd */ -1,
119 /* profile_compilation_info */ nullptr));
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800120 }
121
David Brazdil7b49e6c2016-09-01 11:06:18 +0100122 bool WriteElf(File* vdex_file,
123 File* oat_file,
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800124 const std::vector<const DexFile*>& dex_files,
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800125 SafeMap<std::string, std::string>& key_value_store,
126 bool verify) {
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800127 TimingLogger timings("WriteElf", false, false);
Jeff Hao608f2ce2016-10-19 11:17:11 -0700128 OatWriter oat_writer(/*compiling_boot_image*/false,
129 &timings,
130 /*profile_compilation_info*/nullptr);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000131 for (const DexFile* dex_file : dex_files) {
132 ArrayRef<const uint8_t> raw_dex_file(
133 reinterpret_cast<const uint8_t*>(&dex_file->GetHeader()),
134 dex_file->GetHeader().file_size_);
135 if (!oat_writer.AddRawDexFileSource(raw_dex_file,
136 dex_file->GetLocation().c_str(),
137 dex_file->GetLocationChecksum())) {
138 return false;
139 }
140 }
David Brazdil7b49e6c2016-09-01 11:06:18 +0100141 return DoWriteElf(vdex_file, oat_file, oat_writer, key_value_store, verify);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000142 }
143
David Brazdil7b49e6c2016-09-01 11:06:18 +0100144 bool WriteElf(File* vdex_file,
145 File* oat_file,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000146 const std::vector<const char*>& dex_filenames,
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800147 SafeMap<std::string, std::string>& key_value_store,
148 bool verify) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000149 TimingLogger timings("WriteElf", false, false);
Jeff Hao608f2ce2016-10-19 11:17:11 -0700150 OatWriter oat_writer(/*compiling_boot_image*/false,
151 &timings,
152 /*profile_compilation_info*/nullptr);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000153 for (const char* dex_filename : dex_filenames) {
154 if (!oat_writer.AddDexFileSource(dex_filename, dex_filename)) {
155 return false;
156 }
157 }
David Brazdil7b49e6c2016-09-01 11:06:18 +0100158 return DoWriteElf(vdex_file, oat_file, oat_writer, key_value_store, verify);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000159 }
160
David Brazdil7b49e6c2016-09-01 11:06:18 +0100161 bool WriteElf(File* vdex_file,
162 File* oat_file,
Andreas Gampe43e10b02016-07-15 17:17:34 -0700163 File&& zip_fd,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000164 const char* location,
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800165 SafeMap<std::string, std::string>& key_value_store,
166 bool verify) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000167 TimingLogger timings("WriteElf", false, false);
Jeff Hao608f2ce2016-10-19 11:17:11 -0700168 OatWriter oat_writer(/*compiling_boot_image*/false,
169 &timings,
170 /*profile_compilation_info*/nullptr);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000171 if (!oat_writer.AddZippedDexFilesSource(std::move(zip_fd), location)) {
172 return false;
173 }
David Brazdil7b49e6c2016-09-01 11:06:18 +0100174 return DoWriteElf(vdex_file, oat_file, oat_writer, key_value_store, verify);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000175 }
176
David Brazdil7b49e6c2016-09-01 11:06:18 +0100177 bool DoWriteElf(File* vdex_file,
178 File* oat_file,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000179 OatWriter& oat_writer,
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800180 SafeMap<std::string, std::string>& key_value_store,
181 bool verify) {
Vladimir Marko10c13562015-11-25 14:33:36 +0000182 std::unique_ptr<ElfWriter> elf_writer = CreateElfWriterQuick(
183 compiler_driver_->GetInstructionSet(),
David Srbecky5d811202016-03-08 13:21:22 +0000184 compiler_driver_->GetInstructionSetFeatures(),
Vladimir Marko10c13562015-11-25 14:33:36 +0000185 &compiler_driver_->GetCompilerOptions(),
David Brazdil7b49e6c2016-09-01 11:06:18 +0100186 oat_file);
Vladimir Marko10c13562015-11-25 14:33:36 +0000187 elf_writer->Start();
David Brazdil7b49e6c2016-09-01 11:06:18 +0100188 OutputStream* oat_rodata = elf_writer->StartRoData();
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000189 std::unique_ptr<MemMap> opened_dex_files_map;
190 std::vector<std::unique_ptr<const DexFile>> opened_dex_files;
David Brazdil7b49e6c2016-09-01 11:06:18 +0100191 if (!oat_writer.WriteAndOpenDexFiles(kIsVdexEnabled ? vdex_file : oat_file,
192 oat_rodata,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000193 compiler_driver_->GetInstructionSet(),
194 compiler_driver_->GetInstructionSetFeatures(),
195 &key_value_store,
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800196 verify,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000197 &opened_dex_files_map,
198 &opened_dex_files)) {
199 return false;
200 }
Vladimir Markoaad75c62016-10-03 08:46:48 +0000201
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000202 Runtime* runtime = Runtime::Current();
203 ClassLinker* const class_linker = runtime->GetClassLinker();
204 std::vector<const DexFile*> dex_files;
205 for (const std::unique_ptr<const DexFile>& dex_file : opened_dex_files) {
206 dex_files.push_back(dex_file.get());
207 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierf284d442016-06-02 11:48:30 -0700208 class_linker->RegisterDexFile(*dex_file, nullptr);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000209 }
Vladimir Marko944da602016-02-19 12:27:55 +0000210 linker::MultiOatRelativePatcher patcher(compiler_driver_->GetInstructionSet(),
211 instruction_set_features_.get());
Nicolas Geoffray4acefd32016-10-24 13:14:58 +0100212 oat_writer.Initialize(compiler_driver_.get(), nullptr, dex_files);
213 oat_writer.PrepareLayout(&patcher);
Vladimir Marko944da602016-02-19 12:27:55 +0000214 size_t rodata_size = oat_writer.GetOatHeader().GetExecutableOffset();
David Brazdil7b49e6c2016-09-01 11:06:18 +0100215 size_t text_size = oat_writer.GetOatSize() - rodata_size;
Vladimir Markoaad75c62016-10-03 08:46:48 +0000216 elf_writer->PrepareDynamicSection(rodata_size,
217 text_size,
218 oat_writer.GetBssSize(),
219 oat_writer.GetBssRootsOffset());
Vladimir Marko944da602016-02-19 12:27:55 +0000220
David Brazdil7b49e6c2016-09-01 11:06:18 +0100221 if (!oat_writer.WriteRodata(oat_rodata)) {
Vladimir Marko10c13562015-11-25 14:33:36 +0000222 return false;
223 }
David Brazdil7b49e6c2016-09-01 11:06:18 +0100224 elf_writer->EndRoData(oat_rodata);
Vladimir Marko10c13562015-11-25 14:33:36 +0000225
226 OutputStream* text = elf_writer->StartText();
227 if (!oat_writer.WriteCode(text)) {
228 return false;
229 }
230 elf_writer->EndText(text);
231
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000232 if (!oat_writer.WriteHeader(elf_writer->GetStream(), 42U, 4096U, 0)) {
233 return false;
234 }
235
Vladimir Marko10c13562015-11-25 14:33:36 +0000236 elf_writer->WriteDynamicSection();
Vladimir Marko49b0f452015-12-10 13:49:19 +0000237 elf_writer->WriteDebugInfo(oat_writer.GetMethodDebugInfo());
238 elf_writer->WritePatchLocations(oat_writer.GetAbsolutePatchLocations());
Vladimir Marko10c13562015-11-25 14:33:36 +0000239
Vladimir Markoaad75c62016-10-03 08:46:48 +0000240 if (!elf_writer->End()) {
241 return false;
242 }
243
244 opened_dex_files_maps_.emplace_back(std::move(opened_dex_files_map));
245 for (std::unique_ptr<const DexFile>& dex_file : opened_dex_files) {
246 opened_dex_files_.emplace_back(dex_file.release());
247 }
248 return true;
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800249 }
250
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800251 void TestDexFileInput(bool verify, bool low_4gb);
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800252 void TestZipFileInput(bool verify);
253
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800254 std::unique_ptr<const InstructionSetFeatures> insn_features_;
255 std::unique_ptr<QuickCompilerCallbacks> callbacks_;
Vladimir Markoaad75c62016-10-03 08:46:48 +0000256
257 std::vector<std::unique_ptr<MemMap>> opened_dex_files_maps_;
258 std::vector<std::unique_ptr<const DexFile>> opened_dex_files_;
Logan Chieneeb7edf2012-03-09 20:38:39 +0800259};
Brian Carlstrome24fa612011-09-29 00:53:55 -0700260
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000261class ZipBuilder {
262 public:
263 explicit ZipBuilder(File* zip_file) : zip_file_(zip_file) { }
264
265 bool AddFile(const char* location, const void* data, size_t size) {
266 off_t offset = lseek(zip_file_->Fd(), 0, SEEK_CUR);
267 if (offset == static_cast<off_t>(-1)) {
268 return false;
269 }
270
271 ZipFileHeader file_header;
272 file_header.crc32 = crc32(0u, reinterpret_cast<const Bytef*>(data), size);
273 file_header.compressed_size = size;
274 file_header.uncompressed_size = size;
275 file_header.filename_length = strlen(location);
276
277 if (!zip_file_->WriteFully(&file_header, sizeof(file_header)) ||
278 !zip_file_->WriteFully(location, file_header.filename_length) ||
279 !zip_file_->WriteFully(data, size)) {
280 return false;
281 }
282
283 CentralDirectoryFileHeader cdfh;
284 cdfh.crc32 = file_header.crc32;
285 cdfh.compressed_size = size;
286 cdfh.uncompressed_size = size;
287 cdfh.filename_length = file_header.filename_length;
288 cdfh.relative_offset_of_local_file_header = offset;
289 file_data_.push_back(FileData { cdfh, location });
290 return true;
291 }
292
293 bool Finish() {
294 off_t offset = lseek(zip_file_->Fd(), 0, SEEK_CUR);
295 if (offset == static_cast<off_t>(-1)) {
296 return false;
297 }
298
299 size_t central_directory_size = 0u;
300 for (const FileData& file_data : file_data_) {
301 if (!zip_file_->WriteFully(&file_data.cdfh, sizeof(file_data.cdfh)) ||
302 !zip_file_->WriteFully(file_data.location, file_data.cdfh.filename_length)) {
303 return false;
304 }
305 central_directory_size += sizeof(file_data.cdfh) + file_data.cdfh.filename_length;
306 }
307 EndOfCentralDirectoryRecord eocd_record;
308 eocd_record.number_of_central_directory_records_on_this_disk = file_data_.size();
309 eocd_record.total_number_of_central_directory_records = file_data_.size();
310 eocd_record.size_of_central_directory = central_directory_size;
311 eocd_record.offset_of_start_of_central_directory = offset;
312 return
313 zip_file_->WriteFully(&eocd_record, sizeof(eocd_record)) &&
314 zip_file_->Flush() == 0;
315 }
316
317 private:
318 struct PACKED(1) ZipFileHeader {
319 uint32_t signature = 0x04034b50;
320 uint16_t version_needed_to_extract = 10;
321 uint16_t general_purpose_bit_flag = 0;
322 uint16_t compression_method = 0; // 0 = store only.
323 uint16_t file_last_modification_time = 0u;
324 uint16_t file_last_modification_date = 0u;
325 uint32_t crc32;
326 uint32_t compressed_size;
327 uint32_t uncompressed_size;
328 uint16_t filename_length;
329 uint16_t extra_field_length = 0u; // No extra fields.
330 };
331
332 struct PACKED(1) CentralDirectoryFileHeader {
333 uint32_t signature = 0x02014b50;
334 uint16_t version_made_by = 10;
335 uint16_t version_needed_to_extract = 10;
336 uint16_t general_purpose_bit_flag = 0;
337 uint16_t compression_method = 0; // 0 = store only.
338 uint16_t file_last_modification_time = 0u;
339 uint16_t file_last_modification_date = 0u;
340 uint32_t crc32;
341 uint32_t compressed_size;
342 uint32_t uncompressed_size;
343 uint16_t filename_length;
344 uint16_t extra_field_length = 0u; // No extra fields.
345 uint16_t file_comment_length = 0u; // No file comment.
346 uint16_t disk_number_where_file_starts = 0u;
347 uint16_t internal_file_attributes = 0u;
348 uint32_t external_file_attributes = 0u;
349 uint32_t relative_offset_of_local_file_header;
350 };
351
352 struct PACKED(1) EndOfCentralDirectoryRecord {
353 uint32_t signature = 0x06054b50;
354 uint16_t number_of_this_disk = 0u;
355 uint16_t disk_where_central_directory_starts = 0u;
356 uint16_t number_of_central_directory_records_on_this_disk;
357 uint16_t total_number_of_central_directory_records;
358 uint32_t size_of_central_directory;
359 uint32_t offset_of_start_of_central_directory;
360 uint16_t comment_length = 0u; // No file comment.
361 };
362
363 struct FileData {
364 CentralDirectoryFileHeader cdfh;
365 const char* location;
366 };
367
368 File* zip_file_;
369 std::vector<FileData> file_data_;
370};
371
Brian Carlstrome24fa612011-09-29 00:53:55 -0700372TEST_F(OatTest, WriteRead) {
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800373 TimingLogger timings("OatTest::WriteRead", false, false);
Jesse Wilson254db0f2011-11-16 16:44:11 -0500374 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700375
Ian Rogersef7d42f2014-01-06 12:55:46 -0800376 // TODO: make selectable.
Elliott Hughes956af0f2014-12-11 14:34:28 -0800377 Compiler::Kind compiler_kind = Compiler::kQuick;
Ian Rogersa073c672013-07-30 17:43:55 -0700378 InstructionSet insn_set = kIsTargetBuild ? kThumb2 : kX86;
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700379 std::string error_msg;
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800380 SetupCompiler(compiler_kind, insn_set, std::vector<std::string>(), /*out*/ &error_msg);
381
Ian Rogersd4c4d952014-10-16 20:31:53 -0700382 jobject class_loader = nullptr;
Brian Carlstromba150c32013-08-27 17:31:03 -0700383 if (kCompile) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800384 TimingLogger timings2("OatTest::WriteRead", false, false);
Vladimir Markod1eaf0d2015-10-29 12:18:29 +0000385 compiler_driver_->SetDexFilesForOatFile(class_linker->GetBootClassPath());
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +0000386 compiler_driver_->CompileAll(
387 class_loader, class_linker->GetBootClassPath(), /* verifier_deps */ nullptr, &timings2);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700388 }
389
David Brazdil7b49e6c2016-09-01 11:06:18 +0100390 ScratchFile tmp_oat, tmp_vdex(tmp_oat, ".vdex");
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700391 SafeMap<std::string, std::string> key_value_store;
392 key_value_store.Put(OatHeader::kImageLocationKey, "lue.art");
David Brazdil7b49e6c2016-09-01 11:06:18 +0100393 bool success = WriteElf(tmp_vdex.GetFile(),
394 tmp_oat.GetFile(),
395 class_linker->GetBootClassPath(),
396 key_value_store,
397 false);
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700398 ASSERT_TRUE(success);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700399
Brian Carlstromba150c32013-08-27 17:31:03 -0700400 if (kCompile) { // OatWriter strips the code, regenerate to compare
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +0000401 compiler_driver_->CompileAll(
402 class_loader, class_linker->GetBootClassPath(), /* verifier_deps */ nullptr, &timings);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700403 }
David Brazdil7b49e6c2016-09-01 11:06:18 +0100404 std::unique_ptr<OatFile> oat_file(OatFile::Open(tmp_oat.GetFilename(),
405 tmp_oat.GetFilename(),
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800406 nullptr,
407 nullptr,
408 false,
409 /*low_4gb*/true,
410 nullptr,
411 &error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700412 ASSERT_TRUE(oat_file.get() != nullptr) << error_msg;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700413 const OatHeader& oat_header = oat_file->GetOatHeader();
Brian Carlstromf852fb22012-10-19 11:01:58 -0700414 ASSERT_TRUE(oat_header.IsValid());
Yi Kong9515b512015-11-11 14:36:07 +0000415 ASSERT_EQ(class_linker->GetBootClassPath().size(), oat_header.GetDexFileCount()); // core
Brian Carlstrom28db0122012-10-18 16:20:41 -0700416 ASSERT_EQ(42U, oat_header.GetImageFileLocationOatChecksum());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800417 ASSERT_EQ(4096U, oat_header.GetImageFileLocationOatDataBegin());
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700418 ASSERT_EQ("lue.art", std::string(oat_header.GetStoreValueByKey(OatHeader::kImageLocationKey)));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700419
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800420 ASSERT_TRUE(java_lang_dex_file_ != nullptr);
421 const DexFile& dex_file = *java_lang_dex_file_;
422 uint32_t dex_file_checksum = dex_file.GetLocationChecksum();
423 const OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(dex_file.GetLocation().c_str(),
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700424 &dex_file_checksum);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700425 ASSERT_TRUE(oat_dex_file != nullptr);
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800426 CHECK_EQ(dex_file.GetLocationChecksum(), oat_dex_file->GetDexFileLocationChecksum());
Vladimir Markof4da6752014-08-01 19:04:18 +0100427 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700428 auto pointer_size = class_linker->GetImagePointerSize();
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800429 for (size_t i = 0; i < dex_file.NumClassDefs(); i++) {
430 const DexFile::ClassDef& class_def = dex_file.GetClassDef(i);
431 const uint8_t* class_data = dex_file.GetClassData(class_def);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700432
Brian Carlstromba150c32013-08-27 17:31:03 -0700433 size_t num_virtual_methods = 0;
Ian Rogersd4c4d952014-10-16 20:31:53 -0700434 if (class_data != nullptr) {
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800435 ClassDataItemIterator it(dex_file, class_data);
Ian Rogers0571d352011-11-03 19:51:38 -0700436 num_virtual_methods = it.NumVirtualMethods();
437 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700438
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800439 const char* descriptor = dex_file.GetClassDescriptor(class_def);
Mathieu Chartier9865bde2015-12-21 09:58:16 -0800440 mirror::Class* klass = class_linker->FindClass(soa.Self(),
441 descriptor,
442 ScopedNullHandle<mirror::ClassLoader>());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700443
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100444 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(i);
445 CHECK_EQ(mirror::Class::Status::kStatusNotReady, oat_class.GetStatus()) << descriptor;
Brian Carlstromba150c32013-08-27 17:31:03 -0700446 CHECK_EQ(kCompile ? OatClassType::kOatClassAllCompiled : OatClassType::kOatClassNoneCompiled,
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100447 oat_class.GetType()) << descriptor;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700448
449 size_t method_index = 0;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700450 for (auto& m : klass->GetDirectMethods(pointer_size)) {
451 CheckMethod(&m, oat_class.GetOatMethod(method_index), dex_file);
452 ++method_index;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700453 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700454 size_t visited_virtuals = 0;
Alex Lighte64300b2015-12-15 15:02:47 -0800455 // TODO We should also check copied methods in this test.
456 for (auto& m : klass->GetDeclaredVirtualMethods(pointer_size)) {
Alex Lightfcea56f2016-02-17 11:59:05 -0800457 if (!klass->IsInterface()) {
Alex Light36121492016-02-22 13:43:29 -0800458 EXPECT_FALSE(m.IsCopied());
Alex Lightfcea56f2016-02-17 11:59:05 -0800459 }
Alex Lighte64300b2015-12-15 15:02:47 -0800460 CheckMethod(&m, oat_class.GetOatMethod(method_index), dex_file);
461 ++method_index;
462 ++visited_virtuals;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700463 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700464 EXPECT_EQ(visited_virtuals, num_virtual_methods);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700465 }
466}
467
Brian Carlstrom341df942012-06-27 12:29:22 -0700468TEST_F(OatTest, OatHeaderSizeCheck) {
469 // If this test is failing and you have to update these constants,
470 // it is time to update OatHeader::kOatVersion
Elliott Hughes956af0f2014-12-11 14:34:28 -0800471 EXPECT_EQ(72U, sizeof(OatHeader));
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800472 EXPECT_EQ(4U, sizeof(OatMethodOffsets));
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100473 EXPECT_EQ(20U, sizeof(OatQuickMethodHeader));
Igor Murashkinaf1e2992016-10-12 17:44:50 -0700474 EXPECT_EQ(164 * static_cast<size_t>(GetInstructionSetPointerSize(kRuntimeISA)),
Andreas Gampe542451c2016-07-26 09:02:02 -0700475 sizeof(QuickEntryPoints));
Brian Carlstrom341df942012-06-27 12:29:22 -0700476}
477
Brian Carlstromf852fb22012-10-19 11:01:58 -0700478TEST_F(OatTest, OatHeaderIsValid) {
Jeff Hao5d3baf62016-05-23 19:17:04 -0700479 InstructionSet insn_set = kX86;
480 std::string error_msg;
481 std::unique_ptr<const InstructionSetFeatures> insn_features(
482 InstructionSetFeatures::FromVariant(insn_set, "default", &error_msg));
483 ASSERT_TRUE(insn_features.get() != nullptr) << error_msg;
484 std::unique_ptr<OatHeader> oat_header(OatHeader::Create(insn_set,
485 insn_features.get(),
486 0u,
487 nullptr));
488 ASSERT_NE(oat_header.get(), nullptr);
489 ASSERT_TRUE(oat_header->IsValid());
Brian Carlstromf852fb22012-10-19 11:01:58 -0700490
Jeff Hao5d3baf62016-05-23 19:17:04 -0700491 char* magic = const_cast<char*>(oat_header->GetMagic());
492 strcpy(magic, ""); // bad magic
493 ASSERT_FALSE(oat_header->IsValid());
494 strcpy(magic, "oat\n000"); // bad version
495 ASSERT_FALSE(oat_header->IsValid());
Brian Carlstromf852fb22012-10-19 11:01:58 -0700496}
497
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800498TEST_F(OatTest, EmptyTextSection) {
499 TimingLogger timings("OatTest::EmptyTextSection", false, false);
500
501 // TODO: make selectable.
502 Compiler::Kind compiler_kind = Compiler::kQuick;
503 InstructionSet insn_set = kRuntimeISA;
504 if (insn_set == kArm) insn_set = kThumb2;
505 std::string error_msg;
506 std::vector<std::string> compiler_options;
507 compiler_options.push_back("--compiler-filter=verify-at-runtime");
508 SetupCompiler(compiler_kind, insn_set, compiler_options, /*out*/ &error_msg);
509
510 jobject class_loader;
511 {
512 ScopedObjectAccess soa(Thread::Current());
513 class_loader = LoadDex("Main");
514 }
515 ASSERT_TRUE(class_loader != nullptr);
516 std::vector<const DexFile*> dex_files = GetDexFiles(class_loader);
517 ASSERT_TRUE(!dex_files.empty());
518
519 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
520 for (const DexFile* dex_file : dex_files) {
521 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartier0795f232016-09-27 18:43:30 -0700522 class_linker->RegisterDexFile(*dex_file,
Mathieu Chartier1cc62e42016-10-03 18:01:28 -0700523 soa.Decode<mirror::ClassLoader>(class_loader).Ptr());
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800524 }
525 compiler_driver_->SetDexFilesForOatFile(dex_files);
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +0000526 compiler_driver_->CompileAll(class_loader, dex_files, /* verifier_deps */ nullptr, &timings);
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800527
David Brazdil7b49e6c2016-09-01 11:06:18 +0100528 ScratchFile tmp_oat, tmp_vdex(tmp_oat, ".vdex");
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800529 SafeMap<std::string, std::string> key_value_store;
530 key_value_store.Put(OatHeader::kImageLocationKey, "test.art");
David Brazdil7b49e6c2016-09-01 11:06:18 +0100531 bool success = WriteElf(tmp_vdex.GetFile(), tmp_oat.GetFile(), dex_files, key_value_store, false);
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800532 ASSERT_TRUE(success);
533
David Brazdil7b49e6c2016-09-01 11:06:18 +0100534 std::unique_ptr<OatFile> oat_file(OatFile::Open(tmp_oat.GetFilename(),
535 tmp_oat.GetFilename(),
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800536 nullptr,
537 nullptr,
538 false,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800539 /*low_4gb*/false,
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800540 nullptr,
541 &error_msg));
542 ASSERT_TRUE(oat_file != nullptr);
David Brazdil7b49e6c2016-09-01 11:06:18 +0100543 EXPECT_LT(static_cast<size_t>(oat_file->Size()),
544 static_cast<size_t>(tmp_oat.GetFile()->GetLength()));
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800545}
546
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800547static void MaybeModifyDexFileToFail(bool verify, std::unique_ptr<const DexFile>& data) {
548 // If in verify mode (= fail the verifier mode), make sure we fail early. We'll fail already
549 // because of the missing map, but that may lead to out of bounds reads.
550 if (verify) {
551 const_cast<DexFile::Header*>(&data->GetHeader())->checksum_++;
552 }
553}
554
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800555void OatTest::TestDexFileInput(bool verify, bool low_4gb) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000556 TimingLogger timings("OatTest::DexFileInput", false, false);
557
558 std::vector<const char*> input_filenames;
559
560 ScratchFile dex_file1;
561 TestDexFileBuilder builder1;
562 builder1.AddField("Lsome.TestClass;", "int", "someField");
563 builder1.AddMethod("Lsome.TestClass;", "()I", "foo");
564 std::unique_ptr<const DexFile> dex_file1_data = builder1.Build(dex_file1.GetFilename());
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800565
566 MaybeModifyDexFileToFail(verify, dex_file1_data);
567
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000568 bool success = dex_file1.GetFile()->WriteFully(&dex_file1_data->GetHeader(),
569 dex_file1_data->GetHeader().file_size_);
570 ASSERT_TRUE(success);
571 success = dex_file1.GetFile()->Flush() == 0;
572 ASSERT_TRUE(success);
573 input_filenames.push_back(dex_file1.GetFilename().c_str());
574
575 ScratchFile dex_file2;
576 TestDexFileBuilder builder2;
577 builder2.AddField("Land.AnotherTestClass;", "boolean", "someOtherField");
578 builder2.AddMethod("Land.AnotherTestClass;", "()J", "bar");
579 std::unique_ptr<const DexFile> dex_file2_data = builder2.Build(dex_file2.GetFilename());
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800580
581 MaybeModifyDexFileToFail(verify, dex_file2_data);
582
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000583 success = dex_file2.GetFile()->WriteFully(&dex_file2_data->GetHeader(),
584 dex_file2_data->GetHeader().file_size_);
585 ASSERT_TRUE(success);
586 success = dex_file2.GetFile()->Flush() == 0;
587 ASSERT_TRUE(success);
588 input_filenames.push_back(dex_file2.GetFilename().c_str());
589
David Brazdil7b49e6c2016-09-01 11:06:18 +0100590 ScratchFile oat_file, vdex_file(oat_file, ".vdex");
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000591 SafeMap<std::string, std::string> key_value_store;
592 key_value_store.Put(OatHeader::kImageLocationKey, "test.art");
David Brazdil7b49e6c2016-09-01 11:06:18 +0100593 success = WriteElf(vdex_file.GetFile(),
594 oat_file.GetFile(),
595 input_filenames,
596 key_value_store,
597 verify);
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800598
599 // In verify mode, we expect failure.
600 if (verify) {
601 ASSERT_FALSE(success);
602 return;
603 }
604
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000605 ASSERT_TRUE(success);
606
607 std::string error_msg;
608 std::unique_ptr<OatFile> opened_oat_file(OatFile::Open(oat_file.GetFilename(),
609 oat_file.GetFilename(),
610 nullptr,
611 nullptr,
612 false,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800613 low_4gb,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000614 nullptr,
615 &error_msg));
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800616 if (low_4gb) {
617 uintptr_t begin = reinterpret_cast<uintptr_t>(opened_oat_file->Begin());
618 EXPECT_EQ(begin, static_cast<uint32_t>(begin));
619 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000620 ASSERT_TRUE(opened_oat_file != nullptr);
621 ASSERT_EQ(2u, opened_oat_file->GetOatDexFiles().size());
622 std::unique_ptr<const DexFile> opened_dex_file1 =
623 opened_oat_file->GetOatDexFiles()[0]->OpenDexFile(&error_msg);
624 std::unique_ptr<const DexFile> opened_dex_file2 =
625 opened_oat_file->GetOatDexFiles()[1]->OpenDexFile(&error_msg);
626
627 ASSERT_EQ(dex_file1_data->GetHeader().file_size_, opened_dex_file1->GetHeader().file_size_);
628 ASSERT_EQ(0, memcmp(&dex_file1_data->GetHeader(),
629 &opened_dex_file1->GetHeader(),
630 dex_file1_data->GetHeader().file_size_));
631 ASSERT_EQ(dex_file1_data->GetLocation(), opened_dex_file1->GetLocation());
632
633 ASSERT_EQ(dex_file2_data->GetHeader().file_size_, opened_dex_file2->GetHeader().file_size_);
634 ASSERT_EQ(0, memcmp(&dex_file2_data->GetHeader(),
635 &opened_dex_file2->GetHeader(),
636 dex_file2_data->GetHeader().file_size_));
637 ASSERT_EQ(dex_file2_data->GetLocation(), opened_dex_file2->GetLocation());
638}
639
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800640TEST_F(OatTest, DexFileInputCheckOutput) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800641 TestDexFileInput(false, /*low_4gb*/false);
642}
643
644TEST_F(OatTest, DexFileInputCheckOutputLow4GB) {
645 TestDexFileInput(false, /*low_4gb*/true);
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800646}
647
648TEST_F(OatTest, DexFileInputCheckVerifier) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800649 TestDexFileInput(true, /*low_4gb*/false);
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800650}
651
652void OatTest::TestZipFileInput(bool verify) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000653 TimingLogger timings("OatTest::DexFileInput", false, false);
654
655 ScratchFile zip_file;
656 ZipBuilder zip_builder(zip_file.GetFile());
657
658 ScratchFile dex_file1;
659 TestDexFileBuilder builder1;
660 builder1.AddField("Lsome.TestClass;", "long", "someField");
661 builder1.AddMethod("Lsome.TestClass;", "()D", "foo");
662 std::unique_ptr<const DexFile> dex_file1_data = builder1.Build(dex_file1.GetFilename());
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800663
664 MaybeModifyDexFileToFail(verify, dex_file1_data);
665
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000666 bool success = dex_file1.GetFile()->WriteFully(&dex_file1_data->GetHeader(),
667 dex_file1_data->GetHeader().file_size_);
668 ASSERT_TRUE(success);
669 success = dex_file1.GetFile()->Flush() == 0;
670 ASSERT_TRUE(success);
671 success = zip_builder.AddFile("classes.dex",
672 &dex_file1_data->GetHeader(),
673 dex_file1_data->GetHeader().file_size_);
674 ASSERT_TRUE(success);
675
676 ScratchFile dex_file2;
677 TestDexFileBuilder builder2;
678 builder2.AddField("Land.AnotherTestClass;", "boolean", "someOtherField");
679 builder2.AddMethod("Land.AnotherTestClass;", "()J", "bar");
680 std::unique_ptr<const DexFile> dex_file2_data = builder2.Build(dex_file2.GetFilename());
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800681
682 MaybeModifyDexFileToFail(verify, dex_file2_data);
683
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000684 success = dex_file2.GetFile()->WriteFully(&dex_file2_data->GetHeader(),
685 dex_file2_data->GetHeader().file_size_);
686 ASSERT_TRUE(success);
687 success = dex_file2.GetFile()->Flush() == 0;
688 ASSERT_TRUE(success);
689 success = zip_builder.AddFile("classes2.dex",
690 &dex_file2_data->GetHeader(),
691 dex_file2_data->GetHeader().file_size_);
692 ASSERT_TRUE(success);
693
694 success = zip_builder.Finish();
695 ASSERT_TRUE(success) << strerror(errno);
696
697 SafeMap<std::string, std::string> key_value_store;
698 key_value_store.Put(OatHeader::kImageLocationKey, "test.art");
699 {
700 // Test using the AddDexFileSource() interface with the zip file.
701 std::vector<const char*> input_filenames { zip_file.GetFilename().c_str() }; // NOLINT [readability/braces] [4]
702
David Brazdil7b49e6c2016-09-01 11:06:18 +0100703 ScratchFile oat_file, vdex_file(oat_file, ".vdex");
704 success = WriteElf(vdex_file.GetFile(), oat_file.GetFile(),
705 input_filenames, key_value_store, verify);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000706
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800707 if (verify) {
708 ASSERT_FALSE(success);
709 } else {
710 ASSERT_TRUE(success);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000711
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800712 std::string error_msg;
713 std::unique_ptr<OatFile> opened_oat_file(OatFile::Open(oat_file.GetFilename(),
714 oat_file.GetFilename(),
715 nullptr,
716 nullptr,
717 false,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800718 /*low_4gb*/false,
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800719 nullptr,
720 &error_msg));
721 ASSERT_TRUE(opened_oat_file != nullptr);
722 ASSERT_EQ(2u, opened_oat_file->GetOatDexFiles().size());
723 std::unique_ptr<const DexFile> opened_dex_file1 =
724 opened_oat_file->GetOatDexFiles()[0]->OpenDexFile(&error_msg);
725 std::unique_ptr<const DexFile> opened_dex_file2 =
726 opened_oat_file->GetOatDexFiles()[1]->OpenDexFile(&error_msg);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000727
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800728 ASSERT_EQ(dex_file1_data->GetHeader().file_size_, opened_dex_file1->GetHeader().file_size_);
729 ASSERT_EQ(0, memcmp(&dex_file1_data->GetHeader(),
730 &opened_dex_file1->GetHeader(),
731 dex_file1_data->GetHeader().file_size_));
732 ASSERT_EQ(DexFile::GetMultiDexLocation(0, zip_file.GetFilename().c_str()),
733 opened_dex_file1->GetLocation());
734
735 ASSERT_EQ(dex_file2_data->GetHeader().file_size_, opened_dex_file2->GetHeader().file_size_);
736 ASSERT_EQ(0, memcmp(&dex_file2_data->GetHeader(),
737 &opened_dex_file2->GetHeader(),
738 dex_file2_data->GetHeader().file_size_));
739 ASSERT_EQ(DexFile::GetMultiDexLocation(1, zip_file.GetFilename().c_str()),
740 opened_dex_file2->GetLocation());
741 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000742 }
743
744 {
745 // Test using the AddZipDexFileSource() interface with the zip file handle.
Andreas Gampe43e10b02016-07-15 17:17:34 -0700746 File zip_fd(dup(zip_file.GetFd()), /* check_usage */ false);
747 ASSERT_NE(-1, zip_fd.Fd());
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000748
David Brazdil7b49e6c2016-09-01 11:06:18 +0100749 ScratchFile oat_file, vdex_file(oat_file, ".vdex");
750 success = WriteElf(vdex_file.GetFile(),
751 oat_file.GetFile(),
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000752 std::move(zip_fd),
753 zip_file.GetFilename().c_str(),
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800754 key_value_store,
755 verify);
756 if (verify) {
757 ASSERT_FALSE(success);
758 } else {
759 ASSERT_TRUE(success);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000760
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800761 std::string error_msg;
762 std::unique_ptr<OatFile> opened_oat_file(OatFile::Open(oat_file.GetFilename(),
763 oat_file.GetFilename(),
764 nullptr,
765 nullptr,
766 false,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800767 /*low_4gb*/false,
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800768 nullptr,
769 &error_msg));
770 ASSERT_TRUE(opened_oat_file != nullptr);
771 ASSERT_EQ(2u, opened_oat_file->GetOatDexFiles().size());
772 std::unique_ptr<const DexFile> opened_dex_file1 =
773 opened_oat_file->GetOatDexFiles()[0]->OpenDexFile(&error_msg);
774 std::unique_ptr<const DexFile> opened_dex_file2 =
775 opened_oat_file->GetOatDexFiles()[1]->OpenDexFile(&error_msg);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000776
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800777 ASSERT_EQ(dex_file1_data->GetHeader().file_size_, opened_dex_file1->GetHeader().file_size_);
778 ASSERT_EQ(0, memcmp(&dex_file1_data->GetHeader(),
779 &opened_dex_file1->GetHeader(),
780 dex_file1_data->GetHeader().file_size_));
781 ASSERT_EQ(DexFile::GetMultiDexLocation(0, zip_file.GetFilename().c_str()),
782 opened_dex_file1->GetLocation());
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000783
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800784 ASSERT_EQ(dex_file2_data->GetHeader().file_size_, opened_dex_file2->GetHeader().file_size_);
785 ASSERT_EQ(0, memcmp(&dex_file2_data->GetHeader(),
786 &opened_dex_file2->GetHeader(),
787 dex_file2_data->GetHeader().file_size_));
788 ASSERT_EQ(DexFile::GetMultiDexLocation(1, zip_file.GetFilename().c_str()),
789 opened_dex_file2->GetLocation());
790 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000791 }
792}
793
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800794TEST_F(OatTest, ZipFileInputCheckOutput) {
795 TestZipFileInput(false);
796}
797
798TEST_F(OatTest, ZipFileInputCheckVerifier) {
799 TestZipFileInput(true);
800}
801
Jeff Hao5d3baf62016-05-23 19:17:04 -0700802TEST_F(OatTest, UpdateChecksum) {
803 InstructionSet insn_set = kX86;
804 std::string error_msg;
805 std::unique_ptr<const InstructionSetFeatures> insn_features(
806 InstructionSetFeatures::FromVariant(insn_set, "default", &error_msg));
807 ASSERT_TRUE(insn_features.get() != nullptr) << error_msg;
808 std::unique_ptr<OatHeader> oat_header(OatHeader::Create(insn_set,
809 insn_features.get(),
810 0u,
811 nullptr));
812 // The starting adler32 value is 1.
813 EXPECT_EQ(1U, oat_header->GetChecksum());
814
815 oat_header->UpdateChecksum(OatHeader::kOatMagic, sizeof(OatHeader::kOatMagic));
816 EXPECT_EQ(64291151U, oat_header->GetChecksum());
817
818 // Make sure that null data does not reset the checksum.
819 oat_header->UpdateChecksum(nullptr, 0);
820 EXPECT_EQ(64291151U, oat_header->GetChecksum());
821
822 oat_header->UpdateChecksum(OatHeader::kOatMagic, sizeof(OatHeader::kOatMagic));
823 EXPECT_EQ(216138397U, oat_header->GetChecksum());
824}
825
Brian Carlstrome24fa612011-09-29 00:53:55 -0700826} // namespace art