blob: ce044e8e549d163414601efb601cacb9e1abc5f2 [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/dex_file_to_method_inliner_map.h"
27#include "dex/quick_compiler_callbacks.h"
Mathieu Chartier5bdab122015-01-26 18:30:19 -080028#include "dex/verification_results.h"
Vladimir Marko20f85592015-03-19 10:07:02 +000029#include "driver/compiler_driver.h"
30#include "driver/compiler_options.h"
Vladimir Marko10c13562015-11-25 14:33:36 +000031#include "elf_writer.h"
32#include "elf_writer_quick.h"
Vladimir Marko7624d252014-05-02 14:40:15 +010033#include "entrypoints/quick/quick_entrypoints.h"
Vladimir Marko944da602016-02-19 12:27:55 +000034#include "linker/multi_oat_relative_patcher.h"
Vladimir Marko131980f2015-12-03 18:29:23 +000035#include "linker/vector_output_stream.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080036#include "mirror/class-inl.h"
Ian Rogerse63db272014-07-15 15:36:11 -070037#include "mirror/object-inl.h"
David Srbecky4fda4eb2016-02-05 13:34:46 +000038#include "mirror/object_array-inl.h"
Vladimir Marko7624d252014-05-02 14:40:15 +010039#include "oat_file-inl.h"
Ian Rogerse63db272014-07-15 15:36:11 -070040#include "oat_writer.h"
41#include "scoped_thread_state_change.h"
Vladimir Marko9bdf1082016-01-21 12:15:52 +000042#include "utils/test_dex_file_builder.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070043
Brian Carlstrome24fa612011-09-29 00:53:55 -070044namespace art {
45
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -080046NO_RETURN static void Usage(const char* fmt, ...) {
47 va_list ap;
48 va_start(ap, fmt);
49 std::string error;
50 StringAppendV(&error, fmt, ap);
51 LOG(FATAL) << error;
52 va_end(ap);
53 UNREACHABLE();
54}
55
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080056class OatTest : public CommonCompilerTest {
Logan Chieneeb7edf2012-03-09 20:38:39 +080057 protected:
Brian Carlstromba150c32013-08-27 17:31:03 -070058 static const bool kCompile = false; // DISABLED_ due to the time to compile libcore
59
Mathieu Chartiere401d142015-04-22 13:56:20 -070060 void CheckMethod(ArtMethod* method,
Logan Chieneeb7edf2012-03-09 20:38:39 +080061 const OatFile::OatMethod& oat_method,
Richard Uhlerfbef44d2014-12-23 09:48:51 -080062 const DexFile& dex_file)
Mathieu Chartier90443472015-07-16 20:32:27 -070063 SHARED_REQUIRES(Locks::mutator_lock_) {
Logan Chieneeb7edf2012-03-09 20:38:39 +080064 const CompiledMethod* compiled_method =
Richard Uhlerfbef44d2014-12-23 09:48:51 -080065 compiler_driver_->GetCompiledMethod(MethodReference(&dex_file,
Brian Carlstrom51c24672013-07-11 16:00:56 -070066 method->GetDexMethodIndex()));
Logan Chieneeb7edf2012-03-09 20:38:39 +080067
Ian Rogersd4c4d952014-10-16 20:31:53 -070068 if (compiled_method == nullptr) {
69 EXPECT_TRUE(oat_method.GetQuickCode() == nullptr) << PrettyMethod(method) << " "
70 << oat_method.GetQuickCode();
Ian Rogersef7d42f2014-01-06 12:55:46 -080071 EXPECT_EQ(oat_method.GetFrameSizeInBytes(), 0U);
Logan Chieneeb7edf2012-03-09 20:38:39 +080072 EXPECT_EQ(oat_method.GetCoreSpillMask(), 0U);
73 EXPECT_EQ(oat_method.GetFpSpillMask(), 0U);
74 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -080075 const void* quick_oat_code = oat_method.GetQuickCode();
Elliott Hughes956af0f2014-12-11 14:34:28 -080076 EXPECT_TRUE(quick_oat_code != nullptr) << PrettyMethod(method);
77 EXPECT_EQ(oat_method.GetFrameSizeInBytes(), compiled_method->GetFrameSizeInBytes());
78 EXPECT_EQ(oat_method.GetCoreSpillMask(), compiled_method->GetCoreSpillMask());
79 EXPECT_EQ(oat_method.GetFpSpillMask(), compiled_method->GetFpSpillMask());
80 uintptr_t oat_code_aligned = RoundDown(reinterpret_cast<uintptr_t>(quick_oat_code), 2);
81 quick_oat_code = reinterpret_cast<const void*>(oat_code_aligned);
Vladimir Marko35831e82015-09-11 11:59:18 +010082 ArrayRef<const uint8_t> quick_code = compiled_method->GetQuickCode();
83 EXPECT_FALSE(quick_code.empty());
84 size_t code_size = quick_code.size() * sizeof(quick_code[0]);
Elliott Hughes956af0f2014-12-11 14:34:28 -080085 EXPECT_EQ(0, memcmp(quick_oat_code, &quick_code[0], code_size))
86 << PrettyMethod(method) << " " << code_size;
87 CHECK_EQ(0, memcmp(quick_oat_code, &quick_code[0], code_size));
Logan Chieneeb7edf2012-03-09 20:38:39 +080088 }
89 }
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -080090
91 void SetupCompiler(Compiler::Kind compiler_kind,
92 InstructionSet insn_set,
93 const std::vector<std::string>& compiler_options,
94 /*out*/std::string* error_msg) {
95 ASSERT_TRUE(error_msg != nullptr);
96 insn_features_.reset(InstructionSetFeatures::FromVariant(insn_set, "default", error_msg));
97 ASSERT_TRUE(insn_features_ != nullptr) << error_msg;
98 compiler_options_.reset(new CompilerOptions);
99 for (const std::string& option : compiler_options) {
100 compiler_options_->ParseCompilerOption(option, Usage);
101 }
102 verification_results_.reset(new VerificationResults(compiler_options_.get()));
103 method_inliner_map_.reset(new DexFileToMethodInlinerMap);
104 callbacks_.reset(new QuickCompilerCallbacks(verification_results_.get(),
105 method_inliner_map_.get(),
106 CompilerCallbacks::CallbackMode::kCompileApp));
107 Runtime::Current()->SetCompilerCallbacks(callbacks_.get());
108 timer_.reset(new CumulativeLogger("Compilation times"));
109 compiler_driver_.reset(new CompilerDriver(compiler_options_.get(),
110 verification_results_.get(),
111 method_inliner_map_.get(),
112 compiler_kind,
113 insn_set,
114 insn_features_.get(),
Vladimir Marko944da602016-02-19 12:27:55 +0000115 /* boot_image */ false,
Mathieu Chartier91288d82016-04-28 09:44:54 -0700116 /* app_image */ false,
Vladimir Marko944da602016-02-19 12:27:55 +0000117 /* image_classes */ nullptr,
118 /* compiled_classes */ nullptr,
119 /* compiled_methods */ nullptr,
120 /* thread_count */ 2,
121 /* dump_stats */ true,
122 /* dump_passes */ true,
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800123 timer_.get(),
Vladimir Marko944da602016-02-19 12:27:55 +0000124 /* swap_fd */ -1,
125 /* profile_compilation_info */ nullptr));
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800126 }
127
128 bool WriteElf(File* file,
129 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);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000133 OatWriter oat_writer(/*compiling_boot_image*/false, &timings);
134 for (const DexFile* dex_file : dex_files) {
135 ArrayRef<const uint8_t> raw_dex_file(
136 reinterpret_cast<const uint8_t*>(&dex_file->GetHeader()),
137 dex_file->GetHeader().file_size_);
138 if (!oat_writer.AddRawDexFileSource(raw_dex_file,
139 dex_file->GetLocation().c_str(),
140 dex_file->GetLocationChecksum())) {
141 return false;
142 }
143 }
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800144 return DoWriteElf(file, oat_writer, key_value_store, verify);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000145 }
146
147 bool WriteElf(File* file,
148 const std::vector<const char*>& dex_filenames,
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800149 SafeMap<std::string, std::string>& key_value_store,
150 bool verify) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000151 TimingLogger timings("WriteElf", false, false);
152 OatWriter oat_writer(/*compiling_boot_image*/false, &timings);
153 for (const char* dex_filename : dex_filenames) {
154 if (!oat_writer.AddDexFileSource(dex_filename, dex_filename)) {
155 return false;
156 }
157 }
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800158 return DoWriteElf(file, oat_writer, key_value_store, verify);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000159 }
160
161 bool WriteElf(File* file,
Andreas Gampe43e10b02016-07-15 17:17:34 -0700162 File&& zip_fd,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000163 const char* location,
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800164 SafeMap<std::string, std::string>& key_value_store,
165 bool verify) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000166 TimingLogger timings("WriteElf", false, false);
167 OatWriter oat_writer(/*compiling_boot_image*/false, &timings);
168 if (!oat_writer.AddZippedDexFilesSource(std::move(zip_fd), location)) {
169 return false;
170 }
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800171 return DoWriteElf(file, oat_writer, key_value_store, verify);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000172 }
173
174 bool DoWriteElf(File* file,
175 OatWriter& oat_writer,
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800176 SafeMap<std::string, std::string>& key_value_store,
177 bool verify) {
Vladimir Marko10c13562015-11-25 14:33:36 +0000178 std::unique_ptr<ElfWriter> elf_writer = CreateElfWriterQuick(
179 compiler_driver_->GetInstructionSet(),
David Srbecky5d811202016-03-08 13:21:22 +0000180 compiler_driver_->GetInstructionSetFeatures(),
Vladimir Marko10c13562015-11-25 14:33:36 +0000181 &compiler_driver_->GetCompilerOptions(),
182 file);
Vladimir Marko10c13562015-11-25 14:33:36 +0000183 elf_writer->Start();
Vladimir Marko10c13562015-11-25 14:33:36 +0000184 OutputStream* rodata = elf_writer->StartRoData();
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000185 std::unique_ptr<MemMap> opened_dex_files_map;
186 std::vector<std::unique_ptr<const DexFile>> opened_dex_files;
187 if (!oat_writer.WriteAndOpenDexFiles(rodata,
188 file,
189 compiler_driver_->GetInstructionSet(),
190 compiler_driver_->GetInstructionSetFeatures(),
191 &key_value_store,
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800192 verify,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000193 &opened_dex_files_map,
194 &opened_dex_files)) {
195 return false;
196 }
197 Runtime* runtime = Runtime::Current();
198 ClassLinker* const class_linker = runtime->GetClassLinker();
199 std::vector<const DexFile*> dex_files;
200 for (const std::unique_ptr<const DexFile>& dex_file : opened_dex_files) {
201 dex_files.push_back(dex_file.get());
202 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierf284d442016-06-02 11:48:30 -0700203 class_linker->RegisterDexFile(*dex_file, nullptr);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000204 }
Vladimir Marko944da602016-02-19 12:27:55 +0000205 linker::MultiOatRelativePatcher patcher(compiler_driver_->GetInstructionSet(),
206 instruction_set_features_.get());
207 oat_writer.PrepareLayout(compiler_driver_.get(), nullptr, dex_files, &patcher);
208 size_t rodata_size = oat_writer.GetOatHeader().GetExecutableOffset();
209 size_t text_size = oat_writer.GetSize() - rodata_size;
210 elf_writer->SetLoadedSectionSizes(rodata_size, text_size, oat_writer.GetBssSize());
211
Vladimir Marko10c13562015-11-25 14:33:36 +0000212 if (!oat_writer.WriteRodata(rodata)) {
213 return false;
214 }
215 elf_writer->EndRoData(rodata);
216
217 OutputStream* text = elf_writer->StartText();
218 if (!oat_writer.WriteCode(text)) {
219 return false;
220 }
221 elf_writer->EndText(text);
222
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000223 if (!oat_writer.WriteHeader(elf_writer->GetStream(), 42U, 4096U, 0)) {
224 return false;
225 }
226
Vladimir Marko10c13562015-11-25 14:33:36 +0000227 elf_writer->WriteDynamicSection();
Vladimir Marko49b0f452015-12-10 13:49:19 +0000228 elf_writer->WriteDebugInfo(oat_writer.GetMethodDebugInfo());
229 elf_writer->WritePatchLocations(oat_writer.GetAbsolutePatchLocations());
Vladimir Marko10c13562015-11-25 14:33:36 +0000230
231 return elf_writer->End();
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800232 }
233
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800234 void TestDexFileInput(bool verify, bool low_4gb);
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800235 void TestZipFileInput(bool verify);
236
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800237 std::unique_ptr<const InstructionSetFeatures> insn_features_;
238 std::unique_ptr<QuickCompilerCallbacks> callbacks_;
Logan Chieneeb7edf2012-03-09 20:38:39 +0800239};
Brian Carlstrome24fa612011-09-29 00:53:55 -0700240
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000241class ZipBuilder {
242 public:
243 explicit ZipBuilder(File* zip_file) : zip_file_(zip_file) { }
244
245 bool AddFile(const char* location, const void* data, size_t size) {
246 off_t offset = lseek(zip_file_->Fd(), 0, SEEK_CUR);
247 if (offset == static_cast<off_t>(-1)) {
248 return false;
249 }
250
251 ZipFileHeader file_header;
252 file_header.crc32 = crc32(0u, reinterpret_cast<const Bytef*>(data), size);
253 file_header.compressed_size = size;
254 file_header.uncompressed_size = size;
255 file_header.filename_length = strlen(location);
256
257 if (!zip_file_->WriteFully(&file_header, sizeof(file_header)) ||
258 !zip_file_->WriteFully(location, file_header.filename_length) ||
259 !zip_file_->WriteFully(data, size)) {
260 return false;
261 }
262
263 CentralDirectoryFileHeader cdfh;
264 cdfh.crc32 = file_header.crc32;
265 cdfh.compressed_size = size;
266 cdfh.uncompressed_size = size;
267 cdfh.filename_length = file_header.filename_length;
268 cdfh.relative_offset_of_local_file_header = offset;
269 file_data_.push_back(FileData { cdfh, location });
270 return true;
271 }
272
273 bool Finish() {
274 off_t offset = lseek(zip_file_->Fd(), 0, SEEK_CUR);
275 if (offset == static_cast<off_t>(-1)) {
276 return false;
277 }
278
279 size_t central_directory_size = 0u;
280 for (const FileData& file_data : file_data_) {
281 if (!zip_file_->WriteFully(&file_data.cdfh, sizeof(file_data.cdfh)) ||
282 !zip_file_->WriteFully(file_data.location, file_data.cdfh.filename_length)) {
283 return false;
284 }
285 central_directory_size += sizeof(file_data.cdfh) + file_data.cdfh.filename_length;
286 }
287 EndOfCentralDirectoryRecord eocd_record;
288 eocd_record.number_of_central_directory_records_on_this_disk = file_data_.size();
289 eocd_record.total_number_of_central_directory_records = file_data_.size();
290 eocd_record.size_of_central_directory = central_directory_size;
291 eocd_record.offset_of_start_of_central_directory = offset;
292 return
293 zip_file_->WriteFully(&eocd_record, sizeof(eocd_record)) &&
294 zip_file_->Flush() == 0;
295 }
296
297 private:
298 struct PACKED(1) ZipFileHeader {
299 uint32_t signature = 0x04034b50;
300 uint16_t version_needed_to_extract = 10;
301 uint16_t general_purpose_bit_flag = 0;
302 uint16_t compression_method = 0; // 0 = store only.
303 uint16_t file_last_modification_time = 0u;
304 uint16_t file_last_modification_date = 0u;
305 uint32_t crc32;
306 uint32_t compressed_size;
307 uint32_t uncompressed_size;
308 uint16_t filename_length;
309 uint16_t extra_field_length = 0u; // No extra fields.
310 };
311
312 struct PACKED(1) CentralDirectoryFileHeader {
313 uint32_t signature = 0x02014b50;
314 uint16_t version_made_by = 10;
315 uint16_t version_needed_to_extract = 10;
316 uint16_t general_purpose_bit_flag = 0;
317 uint16_t compression_method = 0; // 0 = store only.
318 uint16_t file_last_modification_time = 0u;
319 uint16_t file_last_modification_date = 0u;
320 uint32_t crc32;
321 uint32_t compressed_size;
322 uint32_t uncompressed_size;
323 uint16_t filename_length;
324 uint16_t extra_field_length = 0u; // No extra fields.
325 uint16_t file_comment_length = 0u; // No file comment.
326 uint16_t disk_number_where_file_starts = 0u;
327 uint16_t internal_file_attributes = 0u;
328 uint32_t external_file_attributes = 0u;
329 uint32_t relative_offset_of_local_file_header;
330 };
331
332 struct PACKED(1) EndOfCentralDirectoryRecord {
333 uint32_t signature = 0x06054b50;
334 uint16_t number_of_this_disk = 0u;
335 uint16_t disk_where_central_directory_starts = 0u;
336 uint16_t number_of_central_directory_records_on_this_disk;
337 uint16_t total_number_of_central_directory_records;
338 uint32_t size_of_central_directory;
339 uint32_t offset_of_start_of_central_directory;
340 uint16_t comment_length = 0u; // No file comment.
341 };
342
343 struct FileData {
344 CentralDirectoryFileHeader cdfh;
345 const char* location;
346 };
347
348 File* zip_file_;
349 std::vector<FileData> file_data_;
350};
351
Brian Carlstrome24fa612011-09-29 00:53:55 -0700352TEST_F(OatTest, WriteRead) {
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800353 TimingLogger timings("OatTest::WriteRead", false, false);
Jesse Wilson254db0f2011-11-16 16:44:11 -0500354 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700355
Ian Rogersef7d42f2014-01-06 12:55:46 -0800356 // TODO: make selectable.
Elliott Hughes956af0f2014-12-11 14:34:28 -0800357 Compiler::Kind compiler_kind = Compiler::kQuick;
Ian Rogersa073c672013-07-30 17:43:55 -0700358 InstructionSet insn_set = kIsTargetBuild ? kThumb2 : kX86;
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700359 std::string error_msg;
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800360 SetupCompiler(compiler_kind, insn_set, std::vector<std::string>(), /*out*/ &error_msg);
361
Ian Rogersd4c4d952014-10-16 20:31:53 -0700362 jobject class_loader = nullptr;
Brian Carlstromba150c32013-08-27 17:31:03 -0700363 if (kCompile) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800364 TimingLogger timings2("OatTest::WriteRead", false, false);
Vladimir Markod1eaf0d2015-10-29 12:18:29 +0000365 compiler_driver_->SetDexFilesForOatFile(class_linker->GetBootClassPath());
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800366 compiler_driver_->CompileAll(class_loader, class_linker->GetBootClassPath(), &timings2);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700367 }
368
369 ScratchFile tmp;
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700370 SafeMap<std::string, std::string> key_value_store;
371 key_value_store.Put(OatHeader::kImageLocationKey, "lue.art");
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800372 bool success = WriteElf(tmp.GetFile(), class_linker->GetBootClassPath(), key_value_store, false);
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700373 ASSERT_TRUE(success);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700374
Brian Carlstromba150c32013-08-27 17:31:03 -0700375 if (kCompile) { // OatWriter strips the code, regenerate to compare
Ian Rogers3d504072014-03-01 09:16:49 -0800376 compiler_driver_->CompileAll(class_loader, class_linker->GetBootClassPath(), &timings);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700377 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800378 std::unique_ptr<OatFile> oat_file(OatFile::Open(tmp.GetFilename(),
379 tmp.GetFilename(),
380 nullptr,
381 nullptr,
382 false,
383 /*low_4gb*/true,
384 nullptr,
385 &error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700386 ASSERT_TRUE(oat_file.get() != nullptr) << error_msg;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700387 const OatHeader& oat_header = oat_file->GetOatHeader();
Brian Carlstromf852fb22012-10-19 11:01:58 -0700388 ASSERT_TRUE(oat_header.IsValid());
Yi Kong9515b512015-11-11 14:36:07 +0000389 ASSERT_EQ(class_linker->GetBootClassPath().size(), oat_header.GetDexFileCount()); // core
Brian Carlstrom28db0122012-10-18 16:20:41 -0700390 ASSERT_EQ(42U, oat_header.GetImageFileLocationOatChecksum());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800391 ASSERT_EQ(4096U, oat_header.GetImageFileLocationOatDataBegin());
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700392 ASSERT_EQ("lue.art", std::string(oat_header.GetStoreValueByKey(OatHeader::kImageLocationKey)));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700393
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800394 ASSERT_TRUE(java_lang_dex_file_ != nullptr);
395 const DexFile& dex_file = *java_lang_dex_file_;
396 uint32_t dex_file_checksum = dex_file.GetLocationChecksum();
397 const OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(dex_file.GetLocation().c_str(),
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700398 &dex_file_checksum);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700399 ASSERT_TRUE(oat_dex_file != nullptr);
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800400 CHECK_EQ(dex_file.GetLocationChecksum(), oat_dex_file->GetDexFileLocationChecksum());
Vladimir Markof4da6752014-08-01 19:04:18 +0100401 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700402 auto pointer_size = class_linker->GetImagePointerSize();
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800403 for (size_t i = 0; i < dex_file.NumClassDefs(); i++) {
404 const DexFile::ClassDef& class_def = dex_file.GetClassDef(i);
405 const uint8_t* class_data = dex_file.GetClassData(class_def);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700406
Brian Carlstromba150c32013-08-27 17:31:03 -0700407 size_t num_virtual_methods = 0;
Ian Rogersd4c4d952014-10-16 20:31:53 -0700408 if (class_data != nullptr) {
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800409 ClassDataItemIterator it(dex_file, class_data);
Ian Rogers0571d352011-11-03 19:51:38 -0700410 num_virtual_methods = it.NumVirtualMethods();
411 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700412
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800413 const char* descriptor = dex_file.GetClassDescriptor(class_def);
Mathieu Chartier9865bde2015-12-21 09:58:16 -0800414 mirror::Class* klass = class_linker->FindClass(soa.Self(),
415 descriptor,
416 ScopedNullHandle<mirror::ClassLoader>());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700417
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100418 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(i);
419 CHECK_EQ(mirror::Class::Status::kStatusNotReady, oat_class.GetStatus()) << descriptor;
Brian Carlstromba150c32013-08-27 17:31:03 -0700420 CHECK_EQ(kCompile ? OatClassType::kOatClassAllCompiled : OatClassType::kOatClassNoneCompiled,
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100421 oat_class.GetType()) << descriptor;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700422
423 size_t method_index = 0;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700424 for (auto& m : klass->GetDirectMethods(pointer_size)) {
425 CheckMethod(&m, oat_class.GetOatMethod(method_index), dex_file);
426 ++method_index;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700427 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700428 size_t visited_virtuals = 0;
Alex Lighte64300b2015-12-15 15:02:47 -0800429 // TODO We should also check copied methods in this test.
430 for (auto& m : klass->GetDeclaredVirtualMethods(pointer_size)) {
Alex Lightfcea56f2016-02-17 11:59:05 -0800431 if (!klass->IsInterface()) {
Alex Light36121492016-02-22 13:43:29 -0800432 EXPECT_FALSE(m.IsCopied());
Alex Lightfcea56f2016-02-17 11:59:05 -0800433 }
Alex Lighte64300b2015-12-15 15:02:47 -0800434 CheckMethod(&m, oat_class.GetOatMethod(method_index), dex_file);
435 ++method_index;
436 ++visited_virtuals;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700437 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700438 EXPECT_EQ(visited_virtuals, num_virtual_methods);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700439 }
440}
441
Brian Carlstrom341df942012-06-27 12:29:22 -0700442TEST_F(OatTest, OatHeaderSizeCheck) {
443 // If this test is failing and you have to update these constants,
444 // it is time to update OatHeader::kOatVersion
Elliott Hughes956af0f2014-12-11 14:34:28 -0800445 EXPECT_EQ(72U, sizeof(OatHeader));
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800446 EXPECT_EQ(4U, sizeof(OatMethodOffsets));
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100447 EXPECT_EQ(20U, sizeof(OatQuickMethodHeader));
Andreas Gampe542451c2016-07-26 09:02:02 -0700448 EXPECT_EQ(162 * static_cast<size_t>(GetInstructionSetPointerSize(kRuntimeISA)),
449 sizeof(QuickEntryPoints));
Brian Carlstrom341df942012-06-27 12:29:22 -0700450}
451
Brian Carlstromf852fb22012-10-19 11:01:58 -0700452TEST_F(OatTest, OatHeaderIsValid) {
Jeff Hao5d3baf62016-05-23 19:17:04 -0700453 InstructionSet insn_set = kX86;
454 std::string error_msg;
455 std::unique_ptr<const InstructionSetFeatures> insn_features(
456 InstructionSetFeatures::FromVariant(insn_set, "default", &error_msg));
457 ASSERT_TRUE(insn_features.get() != nullptr) << error_msg;
458 std::unique_ptr<OatHeader> oat_header(OatHeader::Create(insn_set,
459 insn_features.get(),
460 0u,
461 nullptr));
462 ASSERT_NE(oat_header.get(), nullptr);
463 ASSERT_TRUE(oat_header->IsValid());
Brian Carlstromf852fb22012-10-19 11:01:58 -0700464
Jeff Hao5d3baf62016-05-23 19:17:04 -0700465 char* magic = const_cast<char*>(oat_header->GetMagic());
466 strcpy(magic, ""); // bad magic
467 ASSERT_FALSE(oat_header->IsValid());
468 strcpy(magic, "oat\n000"); // bad version
469 ASSERT_FALSE(oat_header->IsValid());
Brian Carlstromf852fb22012-10-19 11:01:58 -0700470}
471
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800472TEST_F(OatTest, EmptyTextSection) {
473 TimingLogger timings("OatTest::EmptyTextSection", false, false);
474
475 // TODO: make selectable.
476 Compiler::Kind compiler_kind = Compiler::kQuick;
477 InstructionSet insn_set = kRuntimeISA;
478 if (insn_set == kArm) insn_set = kThumb2;
479 std::string error_msg;
480 std::vector<std::string> compiler_options;
481 compiler_options.push_back("--compiler-filter=verify-at-runtime");
482 SetupCompiler(compiler_kind, insn_set, compiler_options, /*out*/ &error_msg);
483
484 jobject class_loader;
485 {
486 ScopedObjectAccess soa(Thread::Current());
487 class_loader = LoadDex("Main");
488 }
489 ASSERT_TRUE(class_loader != nullptr);
490 std::vector<const DexFile*> dex_files = GetDexFiles(class_loader);
491 ASSERT_TRUE(!dex_files.empty());
492
493 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
494 for (const DexFile* dex_file : dex_files) {
495 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierf284d442016-06-02 11:48:30 -0700496 class_linker->RegisterDexFile(*dex_file, soa.Decode<mirror::ClassLoader*>(class_loader));
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800497 }
498 compiler_driver_->SetDexFilesForOatFile(dex_files);
499 compiler_driver_->CompileAll(class_loader, dex_files, &timings);
500
501 ScratchFile tmp;
502 SafeMap<std::string, std::string> key_value_store;
503 key_value_store.Put(OatHeader::kImageLocationKey, "test.art");
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800504 bool success = WriteElf(tmp.GetFile(), dex_files, key_value_store, false);
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800505 ASSERT_TRUE(success);
506
507 std::unique_ptr<OatFile> oat_file(OatFile::Open(tmp.GetFilename(),
508 tmp.GetFilename(),
509 nullptr,
510 nullptr,
511 false,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800512 /*low_4gb*/false,
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800513 nullptr,
514 &error_msg));
515 ASSERT_TRUE(oat_file != nullptr);
516 EXPECT_LT(static_cast<size_t>(oat_file->Size()), static_cast<size_t>(tmp.GetFile()->GetLength()));
517}
518
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800519static void MaybeModifyDexFileToFail(bool verify, std::unique_ptr<const DexFile>& data) {
520 // If in verify mode (= fail the verifier mode), make sure we fail early. We'll fail already
521 // because of the missing map, but that may lead to out of bounds reads.
522 if (verify) {
523 const_cast<DexFile::Header*>(&data->GetHeader())->checksum_++;
524 }
525}
526
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800527void OatTest::TestDexFileInput(bool verify, bool low_4gb) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000528 TimingLogger timings("OatTest::DexFileInput", false, false);
529
530 std::vector<const char*> input_filenames;
531
532 ScratchFile dex_file1;
533 TestDexFileBuilder builder1;
534 builder1.AddField("Lsome.TestClass;", "int", "someField");
535 builder1.AddMethod("Lsome.TestClass;", "()I", "foo");
536 std::unique_ptr<const DexFile> dex_file1_data = builder1.Build(dex_file1.GetFilename());
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800537
538 MaybeModifyDexFileToFail(verify, dex_file1_data);
539
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000540 bool success = dex_file1.GetFile()->WriteFully(&dex_file1_data->GetHeader(),
541 dex_file1_data->GetHeader().file_size_);
542 ASSERT_TRUE(success);
543 success = dex_file1.GetFile()->Flush() == 0;
544 ASSERT_TRUE(success);
545 input_filenames.push_back(dex_file1.GetFilename().c_str());
546
547 ScratchFile dex_file2;
548 TestDexFileBuilder builder2;
549 builder2.AddField("Land.AnotherTestClass;", "boolean", "someOtherField");
550 builder2.AddMethod("Land.AnotherTestClass;", "()J", "bar");
551 std::unique_ptr<const DexFile> dex_file2_data = builder2.Build(dex_file2.GetFilename());
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800552
553 MaybeModifyDexFileToFail(verify, dex_file2_data);
554
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000555 success = dex_file2.GetFile()->WriteFully(&dex_file2_data->GetHeader(),
556 dex_file2_data->GetHeader().file_size_);
557 ASSERT_TRUE(success);
558 success = dex_file2.GetFile()->Flush() == 0;
559 ASSERT_TRUE(success);
560 input_filenames.push_back(dex_file2.GetFilename().c_str());
561
562 ScratchFile oat_file;
563 SafeMap<std::string, std::string> key_value_store;
564 key_value_store.Put(OatHeader::kImageLocationKey, "test.art");
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800565 success = WriteElf(oat_file.GetFile(), input_filenames, key_value_store, verify);
566
567 // In verify mode, we expect failure.
568 if (verify) {
569 ASSERT_FALSE(success);
570 return;
571 }
572
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000573 ASSERT_TRUE(success);
574
575 std::string error_msg;
576 std::unique_ptr<OatFile> opened_oat_file(OatFile::Open(oat_file.GetFilename(),
577 oat_file.GetFilename(),
578 nullptr,
579 nullptr,
580 false,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800581 low_4gb,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000582 nullptr,
583 &error_msg));
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800584 if (low_4gb) {
585 uintptr_t begin = reinterpret_cast<uintptr_t>(opened_oat_file->Begin());
586 EXPECT_EQ(begin, static_cast<uint32_t>(begin));
587 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000588 ASSERT_TRUE(opened_oat_file != nullptr);
589 ASSERT_EQ(2u, opened_oat_file->GetOatDexFiles().size());
590 std::unique_ptr<const DexFile> opened_dex_file1 =
591 opened_oat_file->GetOatDexFiles()[0]->OpenDexFile(&error_msg);
592 std::unique_ptr<const DexFile> opened_dex_file2 =
593 opened_oat_file->GetOatDexFiles()[1]->OpenDexFile(&error_msg);
594
595 ASSERT_EQ(dex_file1_data->GetHeader().file_size_, opened_dex_file1->GetHeader().file_size_);
596 ASSERT_EQ(0, memcmp(&dex_file1_data->GetHeader(),
597 &opened_dex_file1->GetHeader(),
598 dex_file1_data->GetHeader().file_size_));
599 ASSERT_EQ(dex_file1_data->GetLocation(), opened_dex_file1->GetLocation());
600
601 ASSERT_EQ(dex_file2_data->GetHeader().file_size_, opened_dex_file2->GetHeader().file_size_);
602 ASSERT_EQ(0, memcmp(&dex_file2_data->GetHeader(),
603 &opened_dex_file2->GetHeader(),
604 dex_file2_data->GetHeader().file_size_));
605 ASSERT_EQ(dex_file2_data->GetLocation(), opened_dex_file2->GetLocation());
606}
607
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800608TEST_F(OatTest, DexFileInputCheckOutput) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800609 TestDexFileInput(false, /*low_4gb*/false);
610}
611
612TEST_F(OatTest, DexFileInputCheckOutputLow4GB) {
613 TestDexFileInput(false, /*low_4gb*/true);
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800614}
615
616TEST_F(OatTest, DexFileInputCheckVerifier) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800617 TestDexFileInput(true, /*low_4gb*/false);
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800618}
619
620void OatTest::TestZipFileInput(bool verify) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000621 TimingLogger timings("OatTest::DexFileInput", false, false);
622
623 ScratchFile zip_file;
624 ZipBuilder zip_builder(zip_file.GetFile());
625
626 ScratchFile dex_file1;
627 TestDexFileBuilder builder1;
628 builder1.AddField("Lsome.TestClass;", "long", "someField");
629 builder1.AddMethod("Lsome.TestClass;", "()D", "foo");
630 std::unique_ptr<const DexFile> dex_file1_data = builder1.Build(dex_file1.GetFilename());
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800631
632 MaybeModifyDexFileToFail(verify, dex_file1_data);
633
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000634 bool success = dex_file1.GetFile()->WriteFully(&dex_file1_data->GetHeader(),
635 dex_file1_data->GetHeader().file_size_);
636 ASSERT_TRUE(success);
637 success = dex_file1.GetFile()->Flush() == 0;
638 ASSERT_TRUE(success);
639 success = zip_builder.AddFile("classes.dex",
640 &dex_file1_data->GetHeader(),
641 dex_file1_data->GetHeader().file_size_);
642 ASSERT_TRUE(success);
643
644 ScratchFile dex_file2;
645 TestDexFileBuilder builder2;
646 builder2.AddField("Land.AnotherTestClass;", "boolean", "someOtherField");
647 builder2.AddMethod("Land.AnotherTestClass;", "()J", "bar");
648 std::unique_ptr<const DexFile> dex_file2_data = builder2.Build(dex_file2.GetFilename());
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800649
650 MaybeModifyDexFileToFail(verify, dex_file2_data);
651
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000652 success = dex_file2.GetFile()->WriteFully(&dex_file2_data->GetHeader(),
653 dex_file2_data->GetHeader().file_size_);
654 ASSERT_TRUE(success);
655 success = dex_file2.GetFile()->Flush() == 0;
656 ASSERT_TRUE(success);
657 success = zip_builder.AddFile("classes2.dex",
658 &dex_file2_data->GetHeader(),
659 dex_file2_data->GetHeader().file_size_);
660 ASSERT_TRUE(success);
661
662 success = zip_builder.Finish();
663 ASSERT_TRUE(success) << strerror(errno);
664
665 SafeMap<std::string, std::string> key_value_store;
666 key_value_store.Put(OatHeader::kImageLocationKey, "test.art");
667 {
668 // Test using the AddDexFileSource() interface with the zip file.
669 std::vector<const char*> input_filenames { zip_file.GetFilename().c_str() }; // NOLINT [readability/braces] [4]
670
671 ScratchFile oat_file;
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800672 success = WriteElf(oat_file.GetFile(), input_filenames, key_value_store, verify);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000673
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800674 if (verify) {
675 ASSERT_FALSE(success);
676 } else {
677 ASSERT_TRUE(success);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000678
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800679 std::string error_msg;
680 std::unique_ptr<OatFile> opened_oat_file(OatFile::Open(oat_file.GetFilename(),
681 oat_file.GetFilename(),
682 nullptr,
683 nullptr,
684 false,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800685 /*low_4gb*/false,
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800686 nullptr,
687 &error_msg));
688 ASSERT_TRUE(opened_oat_file != nullptr);
689 ASSERT_EQ(2u, opened_oat_file->GetOatDexFiles().size());
690 std::unique_ptr<const DexFile> opened_dex_file1 =
691 opened_oat_file->GetOatDexFiles()[0]->OpenDexFile(&error_msg);
692 std::unique_ptr<const DexFile> opened_dex_file2 =
693 opened_oat_file->GetOatDexFiles()[1]->OpenDexFile(&error_msg);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000694
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800695 ASSERT_EQ(dex_file1_data->GetHeader().file_size_, opened_dex_file1->GetHeader().file_size_);
696 ASSERT_EQ(0, memcmp(&dex_file1_data->GetHeader(),
697 &opened_dex_file1->GetHeader(),
698 dex_file1_data->GetHeader().file_size_));
699 ASSERT_EQ(DexFile::GetMultiDexLocation(0, zip_file.GetFilename().c_str()),
700 opened_dex_file1->GetLocation());
701
702 ASSERT_EQ(dex_file2_data->GetHeader().file_size_, opened_dex_file2->GetHeader().file_size_);
703 ASSERT_EQ(0, memcmp(&dex_file2_data->GetHeader(),
704 &opened_dex_file2->GetHeader(),
705 dex_file2_data->GetHeader().file_size_));
706 ASSERT_EQ(DexFile::GetMultiDexLocation(1, zip_file.GetFilename().c_str()),
707 opened_dex_file2->GetLocation());
708 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000709 }
710
711 {
712 // Test using the AddZipDexFileSource() interface with the zip file handle.
Andreas Gampe43e10b02016-07-15 17:17:34 -0700713 File zip_fd(dup(zip_file.GetFd()), /* check_usage */ false);
714 ASSERT_NE(-1, zip_fd.Fd());
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000715
716 ScratchFile oat_file;
717 success = WriteElf(oat_file.GetFile(),
718 std::move(zip_fd),
719 zip_file.GetFilename().c_str(),
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800720 key_value_store,
721 verify);
722 if (verify) {
723 ASSERT_FALSE(success);
724 } else {
725 ASSERT_TRUE(success);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000726
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800727 std::string error_msg;
728 std::unique_ptr<OatFile> opened_oat_file(OatFile::Open(oat_file.GetFilename(),
729 oat_file.GetFilename(),
730 nullptr,
731 nullptr,
732 false,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800733 /*low_4gb*/false,
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800734 nullptr,
735 &error_msg));
736 ASSERT_TRUE(opened_oat_file != nullptr);
737 ASSERT_EQ(2u, opened_oat_file->GetOatDexFiles().size());
738 std::unique_ptr<const DexFile> opened_dex_file1 =
739 opened_oat_file->GetOatDexFiles()[0]->OpenDexFile(&error_msg);
740 std::unique_ptr<const DexFile> opened_dex_file2 =
741 opened_oat_file->GetOatDexFiles()[1]->OpenDexFile(&error_msg);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000742
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800743 ASSERT_EQ(dex_file1_data->GetHeader().file_size_, opened_dex_file1->GetHeader().file_size_);
744 ASSERT_EQ(0, memcmp(&dex_file1_data->GetHeader(),
745 &opened_dex_file1->GetHeader(),
746 dex_file1_data->GetHeader().file_size_));
747 ASSERT_EQ(DexFile::GetMultiDexLocation(0, zip_file.GetFilename().c_str()),
748 opened_dex_file1->GetLocation());
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000749
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800750 ASSERT_EQ(dex_file2_data->GetHeader().file_size_, opened_dex_file2->GetHeader().file_size_);
751 ASSERT_EQ(0, memcmp(&dex_file2_data->GetHeader(),
752 &opened_dex_file2->GetHeader(),
753 dex_file2_data->GetHeader().file_size_));
754 ASSERT_EQ(DexFile::GetMultiDexLocation(1, zip_file.GetFilename().c_str()),
755 opened_dex_file2->GetLocation());
756 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000757 }
758}
759
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800760TEST_F(OatTest, ZipFileInputCheckOutput) {
761 TestZipFileInput(false);
762}
763
764TEST_F(OatTest, ZipFileInputCheckVerifier) {
765 TestZipFileInput(true);
766}
767
Jeff Hao5d3baf62016-05-23 19:17:04 -0700768TEST_F(OatTest, UpdateChecksum) {
769 InstructionSet insn_set = kX86;
770 std::string error_msg;
771 std::unique_ptr<const InstructionSetFeatures> insn_features(
772 InstructionSetFeatures::FromVariant(insn_set, "default", &error_msg));
773 ASSERT_TRUE(insn_features.get() != nullptr) << error_msg;
774 std::unique_ptr<OatHeader> oat_header(OatHeader::Create(insn_set,
775 insn_features.get(),
776 0u,
777 nullptr));
778 // The starting adler32 value is 1.
779 EXPECT_EQ(1U, oat_header->GetChecksum());
780
781 oat_header->UpdateChecksum(OatHeader::kOatMagic, sizeof(OatHeader::kOatMagic));
782 EXPECT_EQ(64291151U, oat_header->GetChecksum());
783
784 // Make sure that null data does not reset the checksum.
785 oat_header->UpdateChecksum(nullptr, 0);
786 EXPECT_EQ(64291151U, oat_header->GetChecksum());
787
788 oat_header->UpdateChecksum(OatHeader::kOatMagic, sizeof(OatHeader::kOatMagic));
789 EXPECT_EQ(216138397U, oat_header->GetChecksum());
790}
791
Brian Carlstrome24fa612011-09-29 00:53:55 -0700792} // namespace art