blob: 3fddbde4b70a62cb56cbd8405f503dfef9ece1a6 [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) {
68 EXPECT_TRUE(oat_method.GetQuickCode() == nullptr) << PrettyMethod(method) << " "
69 << 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();
Elliott Hughes956af0f2014-12-11 14:34:28 -080075 EXPECT_TRUE(quick_oat_code != nullptr) << PrettyMethod(method);
76 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))
85 << PrettyMethod(method) << " " << code_size;
86 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);
95 insn_features_.reset(InstructionSetFeatures::FromVariant(insn_set, "default", error_msg));
96 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);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000128 OatWriter oat_writer(/*compiling_boot_image*/false, &timings);
129 for (const DexFile* dex_file : dex_files) {
130 ArrayRef<const uint8_t> raw_dex_file(
131 reinterpret_cast<const uint8_t*>(&dex_file->GetHeader()),
132 dex_file->GetHeader().file_size_);
133 if (!oat_writer.AddRawDexFileSource(raw_dex_file,
134 dex_file->GetLocation().c_str(),
135 dex_file->GetLocationChecksum())) {
136 return false;
137 }
138 }
David Brazdil7b49e6c2016-09-01 11:06:18 +0100139 return DoWriteElf(vdex_file, oat_file, oat_writer, key_value_store, verify);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000140 }
141
David Brazdil7b49e6c2016-09-01 11:06:18 +0100142 bool WriteElf(File* vdex_file,
143 File* oat_file,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000144 const std::vector<const char*>& dex_filenames,
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800145 SafeMap<std::string, std::string>& key_value_store,
146 bool verify) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000147 TimingLogger timings("WriteElf", false, false);
148 OatWriter oat_writer(/*compiling_boot_image*/false, &timings);
149 for (const char* dex_filename : dex_filenames) {
150 if (!oat_writer.AddDexFileSource(dex_filename, dex_filename)) {
151 return false;
152 }
153 }
David Brazdil7b49e6c2016-09-01 11:06:18 +0100154 return DoWriteElf(vdex_file, oat_file, oat_writer, key_value_store, verify);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000155 }
156
David Brazdil7b49e6c2016-09-01 11:06:18 +0100157 bool WriteElf(File* vdex_file,
158 File* oat_file,
Andreas Gampe43e10b02016-07-15 17:17:34 -0700159 File&& zip_fd,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000160 const char* location,
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800161 SafeMap<std::string, std::string>& key_value_store,
162 bool verify) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000163 TimingLogger timings("WriteElf", false, false);
164 OatWriter oat_writer(/*compiling_boot_image*/false, &timings);
165 if (!oat_writer.AddZippedDexFilesSource(std::move(zip_fd), location)) {
166 return false;
167 }
David Brazdil7b49e6c2016-09-01 11:06:18 +0100168 return DoWriteElf(vdex_file, oat_file, oat_writer, key_value_store, verify);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000169 }
170
David Brazdil7b49e6c2016-09-01 11:06:18 +0100171 bool DoWriteElf(File* vdex_file,
172 File* oat_file,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000173 OatWriter& oat_writer,
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800174 SafeMap<std::string, std::string>& key_value_store,
175 bool verify) {
Vladimir Marko10c13562015-11-25 14:33:36 +0000176 std::unique_ptr<ElfWriter> elf_writer = CreateElfWriterQuick(
177 compiler_driver_->GetInstructionSet(),
David Srbecky5d811202016-03-08 13:21:22 +0000178 compiler_driver_->GetInstructionSetFeatures(),
Vladimir Marko10c13562015-11-25 14:33:36 +0000179 &compiler_driver_->GetCompilerOptions(),
David Brazdil7b49e6c2016-09-01 11:06:18 +0100180 oat_file);
Vladimir Marko10c13562015-11-25 14:33:36 +0000181 elf_writer->Start();
David Brazdil7b49e6c2016-09-01 11:06:18 +0100182 OutputStream* oat_rodata = elf_writer->StartRoData();
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000183 std::unique_ptr<MemMap> opened_dex_files_map;
184 std::vector<std::unique_ptr<const DexFile>> opened_dex_files;
David Brazdil7b49e6c2016-09-01 11:06:18 +0100185 if (!oat_writer.WriteAndOpenDexFiles(kIsVdexEnabled ? vdex_file : oat_file,
186 oat_rodata,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000187 compiler_driver_->GetInstructionSet(),
188 compiler_driver_->GetInstructionSetFeatures(),
189 &key_value_store,
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800190 verify,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000191 &opened_dex_files_map,
192 &opened_dex_files)) {
193 return false;
194 }
195 Runtime* runtime = Runtime::Current();
196 ClassLinker* const class_linker = runtime->GetClassLinker();
197 std::vector<const DexFile*> dex_files;
198 for (const std::unique_ptr<const DexFile>& dex_file : opened_dex_files) {
199 dex_files.push_back(dex_file.get());
200 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierf284d442016-06-02 11:48:30 -0700201 class_linker->RegisterDexFile(*dex_file, nullptr);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000202 }
Vladimir Marko944da602016-02-19 12:27:55 +0000203 linker::MultiOatRelativePatcher patcher(compiler_driver_->GetInstructionSet(),
204 instruction_set_features_.get());
205 oat_writer.PrepareLayout(compiler_driver_.get(), nullptr, dex_files, &patcher);
206 size_t rodata_size = oat_writer.GetOatHeader().GetExecutableOffset();
David Brazdil7b49e6c2016-09-01 11:06:18 +0100207 size_t text_size = oat_writer.GetOatSize() - rodata_size;
Vladimir Marko63dccbbe2016-09-21 13:51:10 +0100208 elf_writer->PrepareDynamicSection(rodata_size,
209 text_size,
210 oat_writer.GetBssSize(),
211 oat_writer.GetBssRootsOffset());
Vladimir Marko944da602016-02-19 12:27:55 +0000212
David Brazdil7b49e6c2016-09-01 11:06:18 +0100213 if (!oat_writer.WriteRodata(oat_rodata)) {
Vladimir Marko10c13562015-11-25 14:33:36 +0000214 return false;
215 }
David Brazdil7b49e6c2016-09-01 11:06:18 +0100216 elf_writer->EndRoData(oat_rodata);
Vladimir Marko10c13562015-11-25 14:33:36 +0000217
218 OutputStream* text = elf_writer->StartText();
219 if (!oat_writer.WriteCode(text)) {
220 return false;
221 }
222 elf_writer->EndText(text);
223
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000224 if (!oat_writer.WriteHeader(elf_writer->GetStream(), 42U, 4096U, 0)) {
225 return false;
226 }
227
Vladimir Marko10c13562015-11-25 14:33:36 +0000228 elf_writer->WriteDynamicSection();
Vladimir Marko49b0f452015-12-10 13:49:19 +0000229 elf_writer->WriteDebugInfo(oat_writer.GetMethodDebugInfo());
230 elf_writer->WritePatchLocations(oat_writer.GetAbsolutePatchLocations());
Vladimir Marko10c13562015-11-25 14:33:36 +0000231
232 return elf_writer->End();
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800233 }
234
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800235 void TestDexFileInput(bool verify, bool low_4gb);
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800236 void TestZipFileInput(bool verify);
237
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800238 std::unique_ptr<const InstructionSetFeatures> insn_features_;
239 std::unique_ptr<QuickCompilerCallbacks> callbacks_;
Logan Chieneeb7edf2012-03-09 20:38:39 +0800240};
Brian Carlstrome24fa612011-09-29 00:53:55 -0700241
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000242class ZipBuilder {
243 public:
244 explicit ZipBuilder(File* zip_file) : zip_file_(zip_file) { }
245
246 bool AddFile(const char* location, const void* data, size_t size) {
247 off_t offset = lseek(zip_file_->Fd(), 0, SEEK_CUR);
248 if (offset == static_cast<off_t>(-1)) {
249 return false;
250 }
251
252 ZipFileHeader file_header;
253 file_header.crc32 = crc32(0u, reinterpret_cast<const Bytef*>(data), size);
254 file_header.compressed_size = size;
255 file_header.uncompressed_size = size;
256 file_header.filename_length = strlen(location);
257
258 if (!zip_file_->WriteFully(&file_header, sizeof(file_header)) ||
259 !zip_file_->WriteFully(location, file_header.filename_length) ||
260 !zip_file_->WriteFully(data, size)) {
261 return false;
262 }
263
264 CentralDirectoryFileHeader cdfh;
265 cdfh.crc32 = file_header.crc32;
266 cdfh.compressed_size = size;
267 cdfh.uncompressed_size = size;
268 cdfh.filename_length = file_header.filename_length;
269 cdfh.relative_offset_of_local_file_header = offset;
270 file_data_.push_back(FileData { cdfh, location });
271 return true;
272 }
273
274 bool Finish() {
275 off_t offset = lseek(zip_file_->Fd(), 0, SEEK_CUR);
276 if (offset == static_cast<off_t>(-1)) {
277 return false;
278 }
279
280 size_t central_directory_size = 0u;
281 for (const FileData& file_data : file_data_) {
282 if (!zip_file_->WriteFully(&file_data.cdfh, sizeof(file_data.cdfh)) ||
283 !zip_file_->WriteFully(file_data.location, file_data.cdfh.filename_length)) {
284 return false;
285 }
286 central_directory_size += sizeof(file_data.cdfh) + file_data.cdfh.filename_length;
287 }
288 EndOfCentralDirectoryRecord eocd_record;
289 eocd_record.number_of_central_directory_records_on_this_disk = file_data_.size();
290 eocd_record.total_number_of_central_directory_records = file_data_.size();
291 eocd_record.size_of_central_directory = central_directory_size;
292 eocd_record.offset_of_start_of_central_directory = offset;
293 return
294 zip_file_->WriteFully(&eocd_record, sizeof(eocd_record)) &&
295 zip_file_->Flush() == 0;
296 }
297
298 private:
299 struct PACKED(1) ZipFileHeader {
300 uint32_t signature = 0x04034b50;
301 uint16_t version_needed_to_extract = 10;
302 uint16_t general_purpose_bit_flag = 0;
303 uint16_t compression_method = 0; // 0 = store only.
304 uint16_t file_last_modification_time = 0u;
305 uint16_t file_last_modification_date = 0u;
306 uint32_t crc32;
307 uint32_t compressed_size;
308 uint32_t uncompressed_size;
309 uint16_t filename_length;
310 uint16_t extra_field_length = 0u; // No extra fields.
311 };
312
313 struct PACKED(1) CentralDirectoryFileHeader {
314 uint32_t signature = 0x02014b50;
315 uint16_t version_made_by = 10;
316 uint16_t version_needed_to_extract = 10;
317 uint16_t general_purpose_bit_flag = 0;
318 uint16_t compression_method = 0; // 0 = store only.
319 uint16_t file_last_modification_time = 0u;
320 uint16_t file_last_modification_date = 0u;
321 uint32_t crc32;
322 uint32_t compressed_size;
323 uint32_t uncompressed_size;
324 uint16_t filename_length;
325 uint16_t extra_field_length = 0u; // No extra fields.
326 uint16_t file_comment_length = 0u; // No file comment.
327 uint16_t disk_number_where_file_starts = 0u;
328 uint16_t internal_file_attributes = 0u;
329 uint32_t external_file_attributes = 0u;
330 uint32_t relative_offset_of_local_file_header;
331 };
332
333 struct PACKED(1) EndOfCentralDirectoryRecord {
334 uint32_t signature = 0x06054b50;
335 uint16_t number_of_this_disk = 0u;
336 uint16_t disk_where_central_directory_starts = 0u;
337 uint16_t number_of_central_directory_records_on_this_disk;
338 uint16_t total_number_of_central_directory_records;
339 uint32_t size_of_central_directory;
340 uint32_t offset_of_start_of_central_directory;
341 uint16_t comment_length = 0u; // No file comment.
342 };
343
344 struct FileData {
345 CentralDirectoryFileHeader cdfh;
346 const char* location;
347 };
348
349 File* zip_file_;
350 std::vector<FileData> file_data_;
351};
352
Brian Carlstrome24fa612011-09-29 00:53:55 -0700353TEST_F(OatTest, WriteRead) {
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800354 TimingLogger timings("OatTest::WriteRead", false, false);
Jesse Wilson254db0f2011-11-16 16:44:11 -0500355 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700356
Ian Rogersef7d42f2014-01-06 12:55:46 -0800357 // TODO: make selectable.
Elliott Hughes956af0f2014-12-11 14:34:28 -0800358 Compiler::Kind compiler_kind = Compiler::kQuick;
Ian Rogersa073c672013-07-30 17:43:55 -0700359 InstructionSet insn_set = kIsTargetBuild ? kThumb2 : kX86;
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700360 std::string error_msg;
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800361 SetupCompiler(compiler_kind, insn_set, std::vector<std::string>(), /*out*/ &error_msg);
362
Ian Rogersd4c4d952014-10-16 20:31:53 -0700363 jobject class_loader = nullptr;
Brian Carlstromba150c32013-08-27 17:31:03 -0700364 if (kCompile) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800365 TimingLogger timings2("OatTest::WriteRead", false, false);
Vladimir Markod1eaf0d2015-10-29 12:18:29 +0000366 compiler_driver_->SetDexFilesForOatFile(class_linker->GetBootClassPath());
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800367 compiler_driver_->CompileAll(class_loader, class_linker->GetBootClassPath(), &timings2);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700368 }
369
David Brazdil7b49e6c2016-09-01 11:06:18 +0100370 ScratchFile tmp_oat, tmp_vdex(tmp_oat, ".vdex");
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700371 SafeMap<std::string, std::string> key_value_store;
372 key_value_store.Put(OatHeader::kImageLocationKey, "lue.art");
David Brazdil7b49e6c2016-09-01 11:06:18 +0100373 bool success = WriteElf(tmp_vdex.GetFile(),
374 tmp_oat.GetFile(),
375 class_linker->GetBootClassPath(),
376 key_value_store,
377 false);
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700378 ASSERT_TRUE(success);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700379
Brian Carlstromba150c32013-08-27 17:31:03 -0700380 if (kCompile) { // OatWriter strips the code, regenerate to compare
Ian Rogers3d504072014-03-01 09:16:49 -0800381 compiler_driver_->CompileAll(class_loader, class_linker->GetBootClassPath(), &timings);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700382 }
David Brazdil7b49e6c2016-09-01 11:06:18 +0100383 std::unique_ptr<OatFile> oat_file(OatFile::Open(tmp_oat.GetFilename(),
384 tmp_oat.GetFilename(),
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800385 nullptr,
386 nullptr,
387 false,
388 /*low_4gb*/true,
389 nullptr,
390 &error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700391 ASSERT_TRUE(oat_file.get() != nullptr) << error_msg;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700392 const OatHeader& oat_header = oat_file->GetOatHeader();
Brian Carlstromf852fb22012-10-19 11:01:58 -0700393 ASSERT_TRUE(oat_header.IsValid());
Yi Kong9515b512015-11-11 14:36:07 +0000394 ASSERT_EQ(class_linker->GetBootClassPath().size(), oat_header.GetDexFileCount()); // core
Brian Carlstrom28db0122012-10-18 16:20:41 -0700395 ASSERT_EQ(42U, oat_header.GetImageFileLocationOatChecksum());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800396 ASSERT_EQ(4096U, oat_header.GetImageFileLocationOatDataBegin());
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700397 ASSERT_EQ("lue.art", std::string(oat_header.GetStoreValueByKey(OatHeader::kImageLocationKey)));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700398
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800399 ASSERT_TRUE(java_lang_dex_file_ != nullptr);
400 const DexFile& dex_file = *java_lang_dex_file_;
401 uint32_t dex_file_checksum = dex_file.GetLocationChecksum();
402 const OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(dex_file.GetLocation().c_str(),
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700403 &dex_file_checksum);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700404 ASSERT_TRUE(oat_dex_file != nullptr);
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800405 CHECK_EQ(dex_file.GetLocationChecksum(), oat_dex_file->GetDexFileLocationChecksum());
Vladimir Markof4da6752014-08-01 19:04:18 +0100406 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700407 auto pointer_size = class_linker->GetImagePointerSize();
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800408 for (size_t i = 0; i < dex_file.NumClassDefs(); i++) {
409 const DexFile::ClassDef& class_def = dex_file.GetClassDef(i);
410 const uint8_t* class_data = dex_file.GetClassData(class_def);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700411
Brian Carlstromba150c32013-08-27 17:31:03 -0700412 size_t num_virtual_methods = 0;
Ian Rogersd4c4d952014-10-16 20:31:53 -0700413 if (class_data != nullptr) {
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800414 ClassDataItemIterator it(dex_file, class_data);
Ian Rogers0571d352011-11-03 19:51:38 -0700415 num_virtual_methods = it.NumVirtualMethods();
416 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700417
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800418 const char* descriptor = dex_file.GetClassDescriptor(class_def);
Mathieu Chartier9865bde2015-12-21 09:58:16 -0800419 mirror::Class* klass = class_linker->FindClass(soa.Self(),
420 descriptor,
421 ScopedNullHandle<mirror::ClassLoader>());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700422
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100423 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(i);
424 CHECK_EQ(mirror::Class::Status::kStatusNotReady, oat_class.GetStatus()) << descriptor;
Brian Carlstromba150c32013-08-27 17:31:03 -0700425 CHECK_EQ(kCompile ? OatClassType::kOatClassAllCompiled : OatClassType::kOatClassNoneCompiled,
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100426 oat_class.GetType()) << descriptor;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700427
428 size_t method_index = 0;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700429 for (auto& m : klass->GetDirectMethods(pointer_size)) {
430 CheckMethod(&m, oat_class.GetOatMethod(method_index), dex_file);
431 ++method_index;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700432 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700433 size_t visited_virtuals = 0;
Alex Lighte64300b2015-12-15 15:02:47 -0800434 // TODO We should also check copied methods in this test.
435 for (auto& m : klass->GetDeclaredVirtualMethods(pointer_size)) {
Alex Lightfcea56f2016-02-17 11:59:05 -0800436 if (!klass->IsInterface()) {
Alex Light36121492016-02-22 13:43:29 -0800437 EXPECT_FALSE(m.IsCopied());
Alex Lightfcea56f2016-02-17 11:59:05 -0800438 }
Alex Lighte64300b2015-12-15 15:02:47 -0800439 CheckMethod(&m, oat_class.GetOatMethod(method_index), dex_file);
440 ++method_index;
441 ++visited_virtuals;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700442 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700443 EXPECT_EQ(visited_virtuals, num_virtual_methods);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700444 }
445}
446
Brian Carlstrom341df942012-06-27 12:29:22 -0700447TEST_F(OatTest, OatHeaderSizeCheck) {
448 // If this test is failing and you have to update these constants,
449 // it is time to update OatHeader::kOatVersion
Elliott Hughes956af0f2014-12-11 14:34:28 -0800450 EXPECT_EQ(72U, sizeof(OatHeader));
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800451 EXPECT_EQ(4U, sizeof(OatMethodOffsets));
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100452 EXPECT_EQ(20U, sizeof(OatQuickMethodHeader));
Vladimir Marko05846472016-09-14 12:49:57 +0100453 EXPECT_EQ(163 * static_cast<size_t>(GetInstructionSetPointerSize(kRuntimeISA)),
Andreas Gampe542451c2016-07-26 09:02:02 -0700454 sizeof(QuickEntryPoints));
Brian Carlstrom341df942012-06-27 12:29:22 -0700455}
456
Brian Carlstromf852fb22012-10-19 11:01:58 -0700457TEST_F(OatTest, OatHeaderIsValid) {
Jeff Hao5d3baf62016-05-23 19:17:04 -0700458 InstructionSet insn_set = kX86;
459 std::string error_msg;
460 std::unique_ptr<const InstructionSetFeatures> insn_features(
461 InstructionSetFeatures::FromVariant(insn_set, "default", &error_msg));
462 ASSERT_TRUE(insn_features.get() != nullptr) << error_msg;
463 std::unique_ptr<OatHeader> oat_header(OatHeader::Create(insn_set,
464 insn_features.get(),
465 0u,
466 nullptr));
467 ASSERT_NE(oat_header.get(), nullptr);
468 ASSERT_TRUE(oat_header->IsValid());
Brian Carlstromf852fb22012-10-19 11:01:58 -0700469
Jeff Hao5d3baf62016-05-23 19:17:04 -0700470 char* magic = const_cast<char*>(oat_header->GetMagic());
471 strcpy(magic, ""); // bad magic
472 ASSERT_FALSE(oat_header->IsValid());
473 strcpy(magic, "oat\n000"); // bad version
474 ASSERT_FALSE(oat_header->IsValid());
Brian Carlstromf852fb22012-10-19 11:01:58 -0700475}
476
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800477TEST_F(OatTest, EmptyTextSection) {
478 TimingLogger timings("OatTest::EmptyTextSection", false, false);
479
480 // TODO: make selectable.
481 Compiler::Kind compiler_kind = Compiler::kQuick;
482 InstructionSet insn_set = kRuntimeISA;
483 if (insn_set == kArm) insn_set = kThumb2;
484 std::string error_msg;
485 std::vector<std::string> compiler_options;
486 compiler_options.push_back("--compiler-filter=verify-at-runtime");
487 SetupCompiler(compiler_kind, insn_set, compiler_options, /*out*/ &error_msg);
488
489 jobject class_loader;
490 {
491 ScopedObjectAccess soa(Thread::Current());
492 class_loader = LoadDex("Main");
493 }
494 ASSERT_TRUE(class_loader != nullptr);
495 std::vector<const DexFile*> dex_files = GetDexFiles(class_loader);
496 ASSERT_TRUE(!dex_files.empty());
497
498 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
499 for (const DexFile* dex_file : dex_files) {
500 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartier0795f232016-09-27 18:43:30 -0700501 class_linker->RegisterDexFile(*dex_file,
502 soa.Decode<mirror::ClassLoader>(class_loader).Decode());
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800503 }
504 compiler_driver_->SetDexFilesForOatFile(dex_files);
505 compiler_driver_->CompileAll(class_loader, dex_files, &timings);
506
David Brazdil7b49e6c2016-09-01 11:06:18 +0100507 ScratchFile tmp_oat, tmp_vdex(tmp_oat, ".vdex");
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800508 SafeMap<std::string, std::string> key_value_store;
509 key_value_store.Put(OatHeader::kImageLocationKey, "test.art");
David Brazdil7b49e6c2016-09-01 11:06:18 +0100510 bool success = WriteElf(tmp_vdex.GetFile(), tmp_oat.GetFile(), dex_files, key_value_store, false);
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800511 ASSERT_TRUE(success);
512
David Brazdil7b49e6c2016-09-01 11:06:18 +0100513 std::unique_ptr<OatFile> oat_file(OatFile::Open(tmp_oat.GetFilename(),
514 tmp_oat.GetFilename(),
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800515 nullptr,
516 nullptr,
517 false,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800518 /*low_4gb*/false,
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800519 nullptr,
520 &error_msg));
521 ASSERT_TRUE(oat_file != nullptr);
David Brazdil7b49e6c2016-09-01 11:06:18 +0100522 EXPECT_LT(static_cast<size_t>(oat_file->Size()),
523 static_cast<size_t>(tmp_oat.GetFile()->GetLength()));
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800524}
525
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800526static void MaybeModifyDexFileToFail(bool verify, std::unique_ptr<const DexFile>& data) {
527 // If in verify mode (= fail the verifier mode), make sure we fail early. We'll fail already
528 // because of the missing map, but that may lead to out of bounds reads.
529 if (verify) {
530 const_cast<DexFile::Header*>(&data->GetHeader())->checksum_++;
531 }
532}
533
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800534void OatTest::TestDexFileInput(bool verify, bool low_4gb) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000535 TimingLogger timings("OatTest::DexFileInput", false, false);
536
537 std::vector<const char*> input_filenames;
538
539 ScratchFile dex_file1;
540 TestDexFileBuilder builder1;
541 builder1.AddField("Lsome.TestClass;", "int", "someField");
542 builder1.AddMethod("Lsome.TestClass;", "()I", "foo");
543 std::unique_ptr<const DexFile> dex_file1_data = builder1.Build(dex_file1.GetFilename());
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800544
545 MaybeModifyDexFileToFail(verify, dex_file1_data);
546
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000547 bool success = dex_file1.GetFile()->WriteFully(&dex_file1_data->GetHeader(),
548 dex_file1_data->GetHeader().file_size_);
549 ASSERT_TRUE(success);
550 success = dex_file1.GetFile()->Flush() == 0;
551 ASSERT_TRUE(success);
552 input_filenames.push_back(dex_file1.GetFilename().c_str());
553
554 ScratchFile dex_file2;
555 TestDexFileBuilder builder2;
556 builder2.AddField("Land.AnotherTestClass;", "boolean", "someOtherField");
557 builder2.AddMethod("Land.AnotherTestClass;", "()J", "bar");
558 std::unique_ptr<const DexFile> dex_file2_data = builder2.Build(dex_file2.GetFilename());
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800559
560 MaybeModifyDexFileToFail(verify, dex_file2_data);
561
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000562 success = dex_file2.GetFile()->WriteFully(&dex_file2_data->GetHeader(),
563 dex_file2_data->GetHeader().file_size_);
564 ASSERT_TRUE(success);
565 success = dex_file2.GetFile()->Flush() == 0;
566 ASSERT_TRUE(success);
567 input_filenames.push_back(dex_file2.GetFilename().c_str());
568
David Brazdil7b49e6c2016-09-01 11:06:18 +0100569 ScratchFile oat_file, vdex_file(oat_file, ".vdex");
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000570 SafeMap<std::string, std::string> key_value_store;
571 key_value_store.Put(OatHeader::kImageLocationKey, "test.art");
David Brazdil7b49e6c2016-09-01 11:06:18 +0100572 success = WriteElf(vdex_file.GetFile(),
573 oat_file.GetFile(),
574 input_filenames,
575 key_value_store,
576 verify);
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800577
578 // In verify mode, we expect failure.
579 if (verify) {
580 ASSERT_FALSE(success);
581 return;
582 }
583
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000584 ASSERT_TRUE(success);
585
586 std::string error_msg;
587 std::unique_ptr<OatFile> opened_oat_file(OatFile::Open(oat_file.GetFilename(),
588 oat_file.GetFilename(),
589 nullptr,
590 nullptr,
591 false,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800592 low_4gb,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000593 nullptr,
594 &error_msg));
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800595 if (low_4gb) {
596 uintptr_t begin = reinterpret_cast<uintptr_t>(opened_oat_file->Begin());
597 EXPECT_EQ(begin, static_cast<uint32_t>(begin));
598 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000599 ASSERT_TRUE(opened_oat_file != nullptr);
600 ASSERT_EQ(2u, opened_oat_file->GetOatDexFiles().size());
601 std::unique_ptr<const DexFile> opened_dex_file1 =
602 opened_oat_file->GetOatDexFiles()[0]->OpenDexFile(&error_msg);
603 std::unique_ptr<const DexFile> opened_dex_file2 =
604 opened_oat_file->GetOatDexFiles()[1]->OpenDexFile(&error_msg);
605
606 ASSERT_EQ(dex_file1_data->GetHeader().file_size_, opened_dex_file1->GetHeader().file_size_);
607 ASSERT_EQ(0, memcmp(&dex_file1_data->GetHeader(),
608 &opened_dex_file1->GetHeader(),
609 dex_file1_data->GetHeader().file_size_));
610 ASSERT_EQ(dex_file1_data->GetLocation(), opened_dex_file1->GetLocation());
611
612 ASSERT_EQ(dex_file2_data->GetHeader().file_size_, opened_dex_file2->GetHeader().file_size_);
613 ASSERT_EQ(0, memcmp(&dex_file2_data->GetHeader(),
614 &opened_dex_file2->GetHeader(),
615 dex_file2_data->GetHeader().file_size_));
616 ASSERT_EQ(dex_file2_data->GetLocation(), opened_dex_file2->GetLocation());
617}
618
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800619TEST_F(OatTest, DexFileInputCheckOutput) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800620 TestDexFileInput(false, /*low_4gb*/false);
621}
622
623TEST_F(OatTest, DexFileInputCheckOutputLow4GB) {
624 TestDexFileInput(false, /*low_4gb*/true);
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800625}
626
627TEST_F(OatTest, DexFileInputCheckVerifier) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800628 TestDexFileInput(true, /*low_4gb*/false);
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800629}
630
631void OatTest::TestZipFileInput(bool verify) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000632 TimingLogger timings("OatTest::DexFileInput", false, false);
633
634 ScratchFile zip_file;
635 ZipBuilder zip_builder(zip_file.GetFile());
636
637 ScratchFile dex_file1;
638 TestDexFileBuilder builder1;
639 builder1.AddField("Lsome.TestClass;", "long", "someField");
640 builder1.AddMethod("Lsome.TestClass;", "()D", "foo");
641 std::unique_ptr<const DexFile> dex_file1_data = builder1.Build(dex_file1.GetFilename());
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800642
643 MaybeModifyDexFileToFail(verify, dex_file1_data);
644
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000645 bool success = dex_file1.GetFile()->WriteFully(&dex_file1_data->GetHeader(),
646 dex_file1_data->GetHeader().file_size_);
647 ASSERT_TRUE(success);
648 success = dex_file1.GetFile()->Flush() == 0;
649 ASSERT_TRUE(success);
650 success = zip_builder.AddFile("classes.dex",
651 &dex_file1_data->GetHeader(),
652 dex_file1_data->GetHeader().file_size_);
653 ASSERT_TRUE(success);
654
655 ScratchFile dex_file2;
656 TestDexFileBuilder builder2;
657 builder2.AddField("Land.AnotherTestClass;", "boolean", "someOtherField");
658 builder2.AddMethod("Land.AnotherTestClass;", "()J", "bar");
659 std::unique_ptr<const DexFile> dex_file2_data = builder2.Build(dex_file2.GetFilename());
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800660
661 MaybeModifyDexFileToFail(verify, dex_file2_data);
662
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000663 success = dex_file2.GetFile()->WriteFully(&dex_file2_data->GetHeader(),
664 dex_file2_data->GetHeader().file_size_);
665 ASSERT_TRUE(success);
666 success = dex_file2.GetFile()->Flush() == 0;
667 ASSERT_TRUE(success);
668 success = zip_builder.AddFile("classes2.dex",
669 &dex_file2_data->GetHeader(),
670 dex_file2_data->GetHeader().file_size_);
671 ASSERT_TRUE(success);
672
673 success = zip_builder.Finish();
674 ASSERT_TRUE(success) << strerror(errno);
675
676 SafeMap<std::string, std::string> key_value_store;
677 key_value_store.Put(OatHeader::kImageLocationKey, "test.art");
678 {
679 // Test using the AddDexFileSource() interface with the zip file.
680 std::vector<const char*> input_filenames { zip_file.GetFilename().c_str() }; // NOLINT [readability/braces] [4]
681
David Brazdil7b49e6c2016-09-01 11:06:18 +0100682 ScratchFile oat_file, vdex_file(oat_file, ".vdex");
683 success = WriteElf(vdex_file.GetFile(), oat_file.GetFile(),
684 input_filenames, key_value_store, verify);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000685
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800686 if (verify) {
687 ASSERT_FALSE(success);
688 } else {
689 ASSERT_TRUE(success);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000690
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800691 std::string error_msg;
692 std::unique_ptr<OatFile> opened_oat_file(OatFile::Open(oat_file.GetFilename(),
693 oat_file.GetFilename(),
694 nullptr,
695 nullptr,
696 false,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800697 /*low_4gb*/false,
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800698 nullptr,
699 &error_msg));
700 ASSERT_TRUE(opened_oat_file != nullptr);
701 ASSERT_EQ(2u, opened_oat_file->GetOatDexFiles().size());
702 std::unique_ptr<const DexFile> opened_dex_file1 =
703 opened_oat_file->GetOatDexFiles()[0]->OpenDexFile(&error_msg);
704 std::unique_ptr<const DexFile> opened_dex_file2 =
705 opened_oat_file->GetOatDexFiles()[1]->OpenDexFile(&error_msg);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000706
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800707 ASSERT_EQ(dex_file1_data->GetHeader().file_size_, opened_dex_file1->GetHeader().file_size_);
708 ASSERT_EQ(0, memcmp(&dex_file1_data->GetHeader(),
709 &opened_dex_file1->GetHeader(),
710 dex_file1_data->GetHeader().file_size_));
711 ASSERT_EQ(DexFile::GetMultiDexLocation(0, zip_file.GetFilename().c_str()),
712 opened_dex_file1->GetLocation());
713
714 ASSERT_EQ(dex_file2_data->GetHeader().file_size_, opened_dex_file2->GetHeader().file_size_);
715 ASSERT_EQ(0, memcmp(&dex_file2_data->GetHeader(),
716 &opened_dex_file2->GetHeader(),
717 dex_file2_data->GetHeader().file_size_));
718 ASSERT_EQ(DexFile::GetMultiDexLocation(1, zip_file.GetFilename().c_str()),
719 opened_dex_file2->GetLocation());
720 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000721 }
722
723 {
724 // Test using the AddZipDexFileSource() interface with the zip file handle.
Andreas Gampe43e10b02016-07-15 17:17:34 -0700725 File zip_fd(dup(zip_file.GetFd()), /* check_usage */ false);
726 ASSERT_NE(-1, zip_fd.Fd());
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000727
David Brazdil7b49e6c2016-09-01 11:06:18 +0100728 ScratchFile oat_file, vdex_file(oat_file, ".vdex");
729 success = WriteElf(vdex_file.GetFile(),
730 oat_file.GetFile(),
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000731 std::move(zip_fd),
732 zip_file.GetFilename().c_str(),
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800733 key_value_store,
734 verify);
735 if (verify) {
736 ASSERT_FALSE(success);
737 } else {
738 ASSERT_TRUE(success);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000739
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800740 std::string error_msg;
741 std::unique_ptr<OatFile> opened_oat_file(OatFile::Open(oat_file.GetFilename(),
742 oat_file.GetFilename(),
743 nullptr,
744 nullptr,
745 false,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800746 /*low_4gb*/false,
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800747 nullptr,
748 &error_msg));
749 ASSERT_TRUE(opened_oat_file != nullptr);
750 ASSERT_EQ(2u, opened_oat_file->GetOatDexFiles().size());
751 std::unique_ptr<const DexFile> opened_dex_file1 =
752 opened_oat_file->GetOatDexFiles()[0]->OpenDexFile(&error_msg);
753 std::unique_ptr<const DexFile> opened_dex_file2 =
754 opened_oat_file->GetOatDexFiles()[1]->OpenDexFile(&error_msg);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000755
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800756 ASSERT_EQ(dex_file1_data->GetHeader().file_size_, opened_dex_file1->GetHeader().file_size_);
757 ASSERT_EQ(0, memcmp(&dex_file1_data->GetHeader(),
758 &opened_dex_file1->GetHeader(),
759 dex_file1_data->GetHeader().file_size_));
760 ASSERT_EQ(DexFile::GetMultiDexLocation(0, zip_file.GetFilename().c_str()),
761 opened_dex_file1->GetLocation());
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000762
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800763 ASSERT_EQ(dex_file2_data->GetHeader().file_size_, opened_dex_file2->GetHeader().file_size_);
764 ASSERT_EQ(0, memcmp(&dex_file2_data->GetHeader(),
765 &opened_dex_file2->GetHeader(),
766 dex_file2_data->GetHeader().file_size_));
767 ASSERT_EQ(DexFile::GetMultiDexLocation(1, zip_file.GetFilename().c_str()),
768 opened_dex_file2->GetLocation());
769 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000770 }
771}
772
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800773TEST_F(OatTest, ZipFileInputCheckOutput) {
774 TestZipFileInput(false);
775}
776
777TEST_F(OatTest, ZipFileInputCheckVerifier) {
778 TestZipFileInput(true);
779}
780
Jeff Hao5d3baf62016-05-23 19:17:04 -0700781TEST_F(OatTest, UpdateChecksum) {
782 InstructionSet insn_set = kX86;
783 std::string error_msg;
784 std::unique_ptr<const InstructionSetFeatures> insn_features(
785 InstructionSetFeatures::FromVariant(insn_set, "default", &error_msg));
786 ASSERT_TRUE(insn_features.get() != nullptr) << error_msg;
787 std::unique_ptr<OatHeader> oat_header(OatHeader::Create(insn_set,
788 insn_features.get(),
789 0u,
790 nullptr));
791 // The starting adler32 value is 1.
792 EXPECT_EQ(1U, oat_header->GetChecksum());
793
794 oat_header->UpdateChecksum(OatHeader::kOatMagic, sizeof(OatHeader::kOatMagic));
795 EXPECT_EQ(64291151U, oat_header->GetChecksum());
796
797 // Make sure that null data does not reset the checksum.
798 oat_header->UpdateChecksum(nullptr, 0);
799 EXPECT_EQ(64291151U, oat_header->GetChecksum());
800
801 oat_header->UpdateChecksum(OatHeader::kOatMagic, sizeof(OatHeader::kOatMagic));
802 EXPECT_EQ(216138397U, oat_header->GetChecksum());
803}
804
Brian Carlstrome24fa612011-09-29 00:53:55 -0700805} // namespace art