blob: 6d1f94491e59f92f7ea3026619278dbcf0b6adfa [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"
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -080019#include "base/unix_file/fd_file.h"
Andreas Gampe53c913b2014-08-12 23:19:23 -070020#include "class_linker.h"
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080021#include "common_compiler_test.h"
Vladimir Marko20f85592015-03-19 10:07:02 +000022#include "compiled_method.h"
Ian Rogerse63db272014-07-15 15:36:11 -070023#include "compiler.h"
David Srbecky4fda4eb2016-02-05 13:34:46 +000024#include "debug/method_debug_info.h"
Ian Rogerse63db272014-07-15 15:36:11 -070025#include "dex/quick/dex_file_to_method_inliner_map.h"
26#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"
40#include "scoped_thread_state_change.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)
Mathieu Chartier90443472015-07-16 20:32:27 -070062 SHARED_REQUIRES(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()));
102 method_inliner_map_.reset(new DexFileToMethodInlinerMap);
103 callbacks_.reset(new QuickCompilerCallbacks(verification_results_.get(),
104 method_inliner_map_.get(),
105 CompilerCallbacks::CallbackMode::kCompileApp));
106 Runtime::Current()->SetCompilerCallbacks(callbacks_.get());
107 timer_.reset(new CumulativeLogger("Compilation times"));
108 compiler_driver_.reset(new CompilerDriver(compiler_options_.get(),
109 verification_results_.get(),
110 method_inliner_map_.get(),
111 compiler_kind,
112 insn_set,
113 insn_features_.get(),
Vladimir Marko944da602016-02-19 12:27:55 +0000114 /* boot_image */ false,
Mathieu Chartier91288d82016-04-28 09:44:54 -0700115 /* app_image */ false,
Vladimir Marko944da602016-02-19 12:27:55 +0000116 /* image_classes */ nullptr,
117 /* compiled_classes */ nullptr,
118 /* compiled_methods */ nullptr,
119 /* thread_count */ 2,
120 /* dump_stats */ true,
121 /* dump_passes */ true,
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800122 timer_.get(),
Vladimir Marko944da602016-02-19 12:27:55 +0000123 /* swap_fd */ -1,
124 /* profile_compilation_info */ nullptr));
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800125 }
126
127 bool WriteElf(File* file,
128 const std::vector<const DexFile*>& dex_files,
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800129 SafeMap<std::string, std::string>& key_value_store,
130 bool verify) {
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800131 TimingLogger timings("WriteElf", false, false);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000132 OatWriter oat_writer(/*compiling_boot_image*/false, &timings);
133 for (const DexFile* dex_file : dex_files) {
134 ArrayRef<const uint8_t> raw_dex_file(
135 reinterpret_cast<const uint8_t*>(&dex_file->GetHeader()),
136 dex_file->GetHeader().file_size_);
137 if (!oat_writer.AddRawDexFileSource(raw_dex_file,
138 dex_file->GetLocation().c_str(),
139 dex_file->GetLocationChecksum())) {
140 return false;
141 }
142 }
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800143 return DoWriteElf(file, oat_writer, key_value_store, verify);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000144 }
145
146 bool WriteElf(File* file,
147 const std::vector<const char*>& dex_filenames,
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800148 SafeMap<std::string, std::string>& key_value_store,
149 bool verify) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000150 TimingLogger timings("WriteElf", false, false);
151 OatWriter oat_writer(/*compiling_boot_image*/false, &timings);
152 for (const char* dex_filename : dex_filenames) {
153 if (!oat_writer.AddDexFileSource(dex_filename, dex_filename)) {
154 return false;
155 }
156 }
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800157 return DoWriteElf(file, oat_writer, key_value_store, verify);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000158 }
159
160 bool WriteElf(File* file,
161 ScopedFd&& zip_fd,
162 const char* location,
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800163 SafeMap<std::string, std::string>& key_value_store,
164 bool verify) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000165 TimingLogger timings("WriteElf", false, false);
166 OatWriter oat_writer(/*compiling_boot_image*/false, &timings);
167 if (!oat_writer.AddZippedDexFilesSource(std::move(zip_fd), location)) {
168 return false;
169 }
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800170 return DoWriteElf(file, oat_writer, key_value_store, verify);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000171 }
172
173 bool DoWriteElf(File* file,
174 OatWriter& oat_writer,
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800175 SafeMap<std::string, std::string>& key_value_store,
176 bool verify) {
Vladimir Marko10c13562015-11-25 14:33:36 +0000177 std::unique_ptr<ElfWriter> elf_writer = CreateElfWriterQuick(
178 compiler_driver_->GetInstructionSet(),
David Srbecky5d811202016-03-08 13:21:22 +0000179 compiler_driver_->GetInstructionSetFeatures(),
Vladimir Marko10c13562015-11-25 14:33:36 +0000180 &compiler_driver_->GetCompilerOptions(),
181 file);
Vladimir Marko10c13562015-11-25 14:33:36 +0000182 elf_writer->Start();
Vladimir Marko10c13562015-11-25 14:33:36 +0000183 OutputStream* rodata = elf_writer->StartRoData();
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000184 std::unique_ptr<MemMap> opened_dex_files_map;
185 std::vector<std::unique_ptr<const DexFile>> opened_dex_files;
186 if (!oat_writer.WriteAndOpenDexFiles(rodata,
187 file,
188 compiler_driver_->GetInstructionSet(),
189 compiler_driver_->GetInstructionSetFeatures(),
190 &key_value_store,
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800191 verify,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000192 &opened_dex_files_map,
193 &opened_dex_files)) {
194 return false;
195 }
196 Runtime* runtime = Runtime::Current();
197 ClassLinker* const class_linker = runtime->GetClassLinker();
198 std::vector<const DexFile*> dex_files;
199 for (const std::unique_ptr<const DexFile>& dex_file : opened_dex_files) {
200 dex_files.push_back(dex_file.get());
201 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierf284d442016-06-02 11:48:30 -0700202 class_linker->RegisterDexFile(*dex_file, nullptr);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000203 }
Vladimir Marko944da602016-02-19 12:27:55 +0000204 linker::MultiOatRelativePatcher patcher(compiler_driver_->GetInstructionSet(),
205 instruction_set_features_.get());
206 oat_writer.PrepareLayout(compiler_driver_.get(), nullptr, dex_files, &patcher);
207 size_t rodata_size = oat_writer.GetOatHeader().GetExecutableOffset();
208 size_t text_size = oat_writer.GetSize() - rodata_size;
209 elf_writer->SetLoadedSectionSizes(rodata_size, text_size, oat_writer.GetBssSize());
210
Vladimir Marko10c13562015-11-25 14:33:36 +0000211 if (!oat_writer.WriteRodata(rodata)) {
212 return false;
213 }
214 elf_writer->EndRoData(rodata);
215
216 OutputStream* text = elf_writer->StartText();
217 if (!oat_writer.WriteCode(text)) {
218 return false;
219 }
220 elf_writer->EndText(text);
221
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000222 if (!oat_writer.WriteHeader(elf_writer->GetStream(), 42U, 4096U, 0)) {
223 return false;
224 }
225
Vladimir Marko10c13562015-11-25 14:33:36 +0000226 elf_writer->WriteDynamicSection();
Vladimir Marko49b0f452015-12-10 13:49:19 +0000227 elf_writer->WriteDebugInfo(oat_writer.GetMethodDebugInfo());
228 elf_writer->WritePatchLocations(oat_writer.GetAbsolutePatchLocations());
Vladimir Marko10c13562015-11-25 14:33:36 +0000229
230 return elf_writer->End();
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800231 }
232
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800233 void TestDexFileInput(bool verify, bool low_4gb);
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800234 void TestZipFileInput(bool verify);
235
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800236 std::unique_ptr<const InstructionSetFeatures> insn_features_;
237 std::unique_ptr<QuickCompilerCallbacks> callbacks_;
Logan Chieneeb7edf2012-03-09 20:38:39 +0800238};
Brian Carlstrome24fa612011-09-29 00:53:55 -0700239
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000240class ZipBuilder {
241 public:
242 explicit ZipBuilder(File* zip_file) : zip_file_(zip_file) { }
243
244 bool AddFile(const char* location, const void* data, size_t size) {
245 off_t offset = lseek(zip_file_->Fd(), 0, SEEK_CUR);
246 if (offset == static_cast<off_t>(-1)) {
247 return false;
248 }
249
250 ZipFileHeader file_header;
251 file_header.crc32 = crc32(0u, reinterpret_cast<const Bytef*>(data), size);
252 file_header.compressed_size = size;
253 file_header.uncompressed_size = size;
254 file_header.filename_length = strlen(location);
255
256 if (!zip_file_->WriteFully(&file_header, sizeof(file_header)) ||
257 !zip_file_->WriteFully(location, file_header.filename_length) ||
258 !zip_file_->WriteFully(data, size)) {
259 return false;
260 }
261
262 CentralDirectoryFileHeader cdfh;
263 cdfh.crc32 = file_header.crc32;
264 cdfh.compressed_size = size;
265 cdfh.uncompressed_size = size;
266 cdfh.filename_length = file_header.filename_length;
267 cdfh.relative_offset_of_local_file_header = offset;
268 file_data_.push_back(FileData { cdfh, location });
269 return true;
270 }
271
272 bool Finish() {
273 off_t offset = lseek(zip_file_->Fd(), 0, SEEK_CUR);
274 if (offset == static_cast<off_t>(-1)) {
275 return false;
276 }
277
278 size_t central_directory_size = 0u;
279 for (const FileData& file_data : file_data_) {
280 if (!zip_file_->WriteFully(&file_data.cdfh, sizeof(file_data.cdfh)) ||
281 !zip_file_->WriteFully(file_data.location, file_data.cdfh.filename_length)) {
282 return false;
283 }
284 central_directory_size += sizeof(file_data.cdfh) + file_data.cdfh.filename_length;
285 }
286 EndOfCentralDirectoryRecord eocd_record;
287 eocd_record.number_of_central_directory_records_on_this_disk = file_data_.size();
288 eocd_record.total_number_of_central_directory_records = file_data_.size();
289 eocd_record.size_of_central_directory = central_directory_size;
290 eocd_record.offset_of_start_of_central_directory = offset;
291 return
292 zip_file_->WriteFully(&eocd_record, sizeof(eocd_record)) &&
293 zip_file_->Flush() == 0;
294 }
295
296 private:
297 struct PACKED(1) ZipFileHeader {
298 uint32_t signature = 0x04034b50;
299 uint16_t version_needed_to_extract = 10;
300 uint16_t general_purpose_bit_flag = 0;
301 uint16_t compression_method = 0; // 0 = store only.
302 uint16_t file_last_modification_time = 0u;
303 uint16_t file_last_modification_date = 0u;
304 uint32_t crc32;
305 uint32_t compressed_size;
306 uint32_t uncompressed_size;
307 uint16_t filename_length;
308 uint16_t extra_field_length = 0u; // No extra fields.
309 };
310
311 struct PACKED(1) CentralDirectoryFileHeader {
312 uint32_t signature = 0x02014b50;
313 uint16_t version_made_by = 10;
314 uint16_t version_needed_to_extract = 10;
315 uint16_t general_purpose_bit_flag = 0;
316 uint16_t compression_method = 0; // 0 = store only.
317 uint16_t file_last_modification_time = 0u;
318 uint16_t file_last_modification_date = 0u;
319 uint32_t crc32;
320 uint32_t compressed_size;
321 uint32_t uncompressed_size;
322 uint16_t filename_length;
323 uint16_t extra_field_length = 0u; // No extra fields.
324 uint16_t file_comment_length = 0u; // No file comment.
325 uint16_t disk_number_where_file_starts = 0u;
326 uint16_t internal_file_attributes = 0u;
327 uint32_t external_file_attributes = 0u;
328 uint32_t relative_offset_of_local_file_header;
329 };
330
331 struct PACKED(1) EndOfCentralDirectoryRecord {
332 uint32_t signature = 0x06054b50;
333 uint16_t number_of_this_disk = 0u;
334 uint16_t disk_where_central_directory_starts = 0u;
335 uint16_t number_of_central_directory_records_on_this_disk;
336 uint16_t total_number_of_central_directory_records;
337 uint32_t size_of_central_directory;
338 uint32_t offset_of_start_of_central_directory;
339 uint16_t comment_length = 0u; // No file comment.
340 };
341
342 struct FileData {
343 CentralDirectoryFileHeader cdfh;
344 const char* location;
345 };
346
347 File* zip_file_;
348 std::vector<FileData> file_data_;
349};
350
Brian Carlstrome24fa612011-09-29 00:53:55 -0700351TEST_F(OatTest, WriteRead) {
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800352 TimingLogger timings("OatTest::WriteRead", false, false);
Jesse Wilson254db0f2011-11-16 16:44:11 -0500353 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700354
Ian Rogersef7d42f2014-01-06 12:55:46 -0800355 // TODO: make selectable.
Elliott Hughes956af0f2014-12-11 14:34:28 -0800356 Compiler::Kind compiler_kind = Compiler::kQuick;
Ian Rogersa073c672013-07-30 17:43:55 -0700357 InstructionSet insn_set = kIsTargetBuild ? kThumb2 : kX86;
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700358 std::string error_msg;
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800359 SetupCompiler(compiler_kind, insn_set, std::vector<std::string>(), /*out*/ &error_msg);
360
Ian Rogersd4c4d952014-10-16 20:31:53 -0700361 jobject class_loader = nullptr;
Brian Carlstromba150c32013-08-27 17:31:03 -0700362 if (kCompile) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800363 TimingLogger timings2("OatTest::WriteRead", false, false);
Vladimir Markod1eaf0d2015-10-29 12:18:29 +0000364 compiler_driver_->SetDexFilesForOatFile(class_linker->GetBootClassPath());
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800365 compiler_driver_->CompileAll(class_loader, class_linker->GetBootClassPath(), &timings2);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700366 }
367
368 ScratchFile tmp;
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700369 SafeMap<std::string, std::string> key_value_store;
370 key_value_store.Put(OatHeader::kImageLocationKey, "lue.art");
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800371 bool success = WriteElf(tmp.GetFile(), class_linker->GetBootClassPath(), key_value_store, false);
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700372 ASSERT_TRUE(success);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700373
Brian Carlstromba150c32013-08-27 17:31:03 -0700374 if (kCompile) { // OatWriter strips the code, regenerate to compare
Ian Rogers3d504072014-03-01 09:16:49 -0800375 compiler_driver_->CompileAll(class_loader, class_linker->GetBootClassPath(), &timings);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700376 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800377 std::unique_ptr<OatFile> oat_file(OatFile::Open(tmp.GetFilename(),
378 tmp.GetFilename(),
379 nullptr,
380 nullptr,
381 false,
382 /*low_4gb*/true,
383 nullptr,
384 &error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700385 ASSERT_TRUE(oat_file.get() != nullptr) << error_msg;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700386 const OatHeader& oat_header = oat_file->GetOatHeader();
Brian Carlstromf852fb22012-10-19 11:01:58 -0700387 ASSERT_TRUE(oat_header.IsValid());
Yi Kong9515b512015-11-11 14:36:07 +0000388 ASSERT_EQ(class_linker->GetBootClassPath().size(), oat_header.GetDexFileCount()); // core
Brian Carlstrom28db0122012-10-18 16:20:41 -0700389 ASSERT_EQ(42U, oat_header.GetImageFileLocationOatChecksum());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800390 ASSERT_EQ(4096U, oat_header.GetImageFileLocationOatDataBegin());
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700391 ASSERT_EQ("lue.art", std::string(oat_header.GetStoreValueByKey(OatHeader::kImageLocationKey)));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700392
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800393 ASSERT_TRUE(java_lang_dex_file_ != nullptr);
394 const DexFile& dex_file = *java_lang_dex_file_;
395 uint32_t dex_file_checksum = dex_file.GetLocationChecksum();
396 const OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(dex_file.GetLocation().c_str(),
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700397 &dex_file_checksum);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700398 ASSERT_TRUE(oat_dex_file != nullptr);
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800399 CHECK_EQ(dex_file.GetLocationChecksum(), oat_dex_file->GetDexFileLocationChecksum());
Vladimir Markof4da6752014-08-01 19:04:18 +0100400 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700401 auto pointer_size = class_linker->GetImagePointerSize();
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800402 for (size_t i = 0; i < dex_file.NumClassDefs(); i++) {
403 const DexFile::ClassDef& class_def = dex_file.GetClassDef(i);
404 const uint8_t* class_data = dex_file.GetClassData(class_def);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700405
Brian Carlstromba150c32013-08-27 17:31:03 -0700406 size_t num_virtual_methods = 0;
Ian Rogersd4c4d952014-10-16 20:31:53 -0700407 if (class_data != nullptr) {
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800408 ClassDataItemIterator it(dex_file, class_data);
Ian Rogers0571d352011-11-03 19:51:38 -0700409 num_virtual_methods = it.NumVirtualMethods();
410 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700411
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800412 const char* descriptor = dex_file.GetClassDescriptor(class_def);
Mathieu Chartier9865bde2015-12-21 09:58:16 -0800413 mirror::Class* klass = class_linker->FindClass(soa.Self(),
414 descriptor,
415 ScopedNullHandle<mirror::ClassLoader>());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700416
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100417 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(i);
418 CHECK_EQ(mirror::Class::Status::kStatusNotReady, oat_class.GetStatus()) << descriptor;
Brian Carlstromba150c32013-08-27 17:31:03 -0700419 CHECK_EQ(kCompile ? OatClassType::kOatClassAllCompiled : OatClassType::kOatClassNoneCompiled,
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100420 oat_class.GetType()) << descriptor;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700421
422 size_t method_index = 0;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700423 for (auto& m : klass->GetDirectMethods(pointer_size)) {
424 CheckMethod(&m, oat_class.GetOatMethod(method_index), dex_file);
425 ++method_index;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700426 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700427 size_t visited_virtuals = 0;
Alex Lighte64300b2015-12-15 15:02:47 -0800428 // TODO We should also check copied methods in this test.
429 for (auto& m : klass->GetDeclaredVirtualMethods(pointer_size)) {
Alex Lightfcea56f2016-02-17 11:59:05 -0800430 if (!klass->IsInterface()) {
Alex Light36121492016-02-22 13:43:29 -0800431 EXPECT_FALSE(m.IsCopied());
Alex Lightfcea56f2016-02-17 11:59:05 -0800432 }
Alex Lighte64300b2015-12-15 15:02:47 -0800433 CheckMethod(&m, oat_class.GetOatMethod(method_index), dex_file);
434 ++method_index;
435 ++visited_virtuals;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700436 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700437 EXPECT_EQ(visited_virtuals, num_virtual_methods);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700438 }
439}
440
Brian Carlstrom341df942012-06-27 12:29:22 -0700441TEST_F(OatTest, OatHeaderSizeCheck) {
442 // If this test is failing and you have to update these constants,
443 // it is time to update OatHeader::kOatVersion
Elliott Hughes956af0f2014-12-11 14:34:28 -0800444 EXPECT_EQ(72U, sizeof(OatHeader));
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800445 EXPECT_EQ(4U, sizeof(OatMethodOffsets));
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100446 EXPECT_EQ(20U, sizeof(OatQuickMethodHeader));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000447 EXPECT_EQ(132 * GetInstructionSetPointerSize(kRuntimeISA), sizeof(QuickEntryPoints));
Brian Carlstrom341df942012-06-27 12:29:22 -0700448}
449
Brian Carlstromf852fb22012-10-19 11:01:58 -0700450TEST_F(OatTest, OatHeaderIsValid) {
Jeff Hao5d3baf62016-05-23 19:17:04 -0700451 InstructionSet insn_set = kX86;
452 std::string error_msg;
453 std::unique_ptr<const InstructionSetFeatures> insn_features(
454 InstructionSetFeatures::FromVariant(insn_set, "default", &error_msg));
455 ASSERT_TRUE(insn_features.get() != nullptr) << error_msg;
456 std::unique_ptr<OatHeader> oat_header(OatHeader::Create(insn_set,
457 insn_features.get(),
458 0u,
459 nullptr));
460 ASSERT_NE(oat_header.get(), nullptr);
461 ASSERT_TRUE(oat_header->IsValid());
Brian Carlstromf852fb22012-10-19 11:01:58 -0700462
Jeff Hao5d3baf62016-05-23 19:17:04 -0700463 char* magic = const_cast<char*>(oat_header->GetMagic());
464 strcpy(magic, ""); // bad magic
465 ASSERT_FALSE(oat_header->IsValid());
466 strcpy(magic, "oat\n000"); // bad version
467 ASSERT_FALSE(oat_header->IsValid());
Brian Carlstromf852fb22012-10-19 11:01:58 -0700468}
469
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800470TEST_F(OatTest, EmptyTextSection) {
471 TimingLogger timings("OatTest::EmptyTextSection", false, false);
472
473 // TODO: make selectable.
474 Compiler::Kind compiler_kind = Compiler::kQuick;
475 InstructionSet insn_set = kRuntimeISA;
476 if (insn_set == kArm) insn_set = kThumb2;
477 std::string error_msg;
478 std::vector<std::string> compiler_options;
479 compiler_options.push_back("--compiler-filter=verify-at-runtime");
480 SetupCompiler(compiler_kind, insn_set, compiler_options, /*out*/ &error_msg);
481
482 jobject class_loader;
483 {
484 ScopedObjectAccess soa(Thread::Current());
485 class_loader = LoadDex("Main");
486 }
487 ASSERT_TRUE(class_loader != nullptr);
488 std::vector<const DexFile*> dex_files = GetDexFiles(class_loader);
489 ASSERT_TRUE(!dex_files.empty());
490
491 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
492 for (const DexFile* dex_file : dex_files) {
493 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierf284d442016-06-02 11:48:30 -0700494 class_linker->RegisterDexFile(*dex_file, soa.Decode<mirror::ClassLoader*>(class_loader));
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800495 }
496 compiler_driver_->SetDexFilesForOatFile(dex_files);
497 compiler_driver_->CompileAll(class_loader, dex_files, &timings);
498
499 ScratchFile tmp;
500 SafeMap<std::string, std::string> key_value_store;
501 key_value_store.Put(OatHeader::kImageLocationKey, "test.art");
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800502 bool success = WriteElf(tmp.GetFile(), dex_files, key_value_store, false);
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800503 ASSERT_TRUE(success);
504
505 std::unique_ptr<OatFile> oat_file(OatFile::Open(tmp.GetFilename(),
506 tmp.GetFilename(),
507 nullptr,
508 nullptr,
509 false,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800510 /*low_4gb*/false,
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800511 nullptr,
512 &error_msg));
513 ASSERT_TRUE(oat_file != nullptr);
514 EXPECT_LT(static_cast<size_t>(oat_file->Size()), static_cast<size_t>(tmp.GetFile()->GetLength()));
515}
516
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800517static void MaybeModifyDexFileToFail(bool verify, std::unique_ptr<const DexFile>& data) {
518 // If in verify mode (= fail the verifier mode), make sure we fail early. We'll fail already
519 // because of the missing map, but that may lead to out of bounds reads.
520 if (verify) {
521 const_cast<DexFile::Header*>(&data->GetHeader())->checksum_++;
522 }
523}
524
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800525void OatTest::TestDexFileInput(bool verify, bool low_4gb) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000526 TimingLogger timings("OatTest::DexFileInput", false, false);
527
528 std::vector<const char*> input_filenames;
529
530 ScratchFile dex_file1;
531 TestDexFileBuilder builder1;
532 builder1.AddField("Lsome.TestClass;", "int", "someField");
533 builder1.AddMethod("Lsome.TestClass;", "()I", "foo");
534 std::unique_ptr<const DexFile> dex_file1_data = builder1.Build(dex_file1.GetFilename());
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800535
536 MaybeModifyDexFileToFail(verify, dex_file1_data);
537
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000538 bool success = dex_file1.GetFile()->WriteFully(&dex_file1_data->GetHeader(),
539 dex_file1_data->GetHeader().file_size_);
540 ASSERT_TRUE(success);
541 success = dex_file1.GetFile()->Flush() == 0;
542 ASSERT_TRUE(success);
543 input_filenames.push_back(dex_file1.GetFilename().c_str());
544
545 ScratchFile dex_file2;
546 TestDexFileBuilder builder2;
547 builder2.AddField("Land.AnotherTestClass;", "boolean", "someOtherField");
548 builder2.AddMethod("Land.AnotherTestClass;", "()J", "bar");
549 std::unique_ptr<const DexFile> dex_file2_data = builder2.Build(dex_file2.GetFilename());
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800550
551 MaybeModifyDexFileToFail(verify, dex_file2_data);
552
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000553 success = dex_file2.GetFile()->WriteFully(&dex_file2_data->GetHeader(),
554 dex_file2_data->GetHeader().file_size_);
555 ASSERT_TRUE(success);
556 success = dex_file2.GetFile()->Flush() == 0;
557 ASSERT_TRUE(success);
558 input_filenames.push_back(dex_file2.GetFilename().c_str());
559
560 ScratchFile oat_file;
561 SafeMap<std::string, std::string> key_value_store;
562 key_value_store.Put(OatHeader::kImageLocationKey, "test.art");
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800563 success = WriteElf(oat_file.GetFile(), input_filenames, key_value_store, verify);
564
565 // In verify mode, we expect failure.
566 if (verify) {
567 ASSERT_FALSE(success);
568 return;
569 }
570
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000571 ASSERT_TRUE(success);
572
573 std::string error_msg;
574 std::unique_ptr<OatFile> opened_oat_file(OatFile::Open(oat_file.GetFilename(),
575 oat_file.GetFilename(),
576 nullptr,
577 nullptr,
578 false,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800579 low_4gb,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000580 nullptr,
581 &error_msg));
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800582 if (low_4gb) {
583 uintptr_t begin = reinterpret_cast<uintptr_t>(opened_oat_file->Begin());
584 EXPECT_EQ(begin, static_cast<uint32_t>(begin));
585 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000586 ASSERT_TRUE(opened_oat_file != nullptr);
587 ASSERT_EQ(2u, opened_oat_file->GetOatDexFiles().size());
588 std::unique_ptr<const DexFile> opened_dex_file1 =
589 opened_oat_file->GetOatDexFiles()[0]->OpenDexFile(&error_msg);
590 std::unique_ptr<const DexFile> opened_dex_file2 =
591 opened_oat_file->GetOatDexFiles()[1]->OpenDexFile(&error_msg);
592
593 ASSERT_EQ(dex_file1_data->GetHeader().file_size_, opened_dex_file1->GetHeader().file_size_);
594 ASSERT_EQ(0, memcmp(&dex_file1_data->GetHeader(),
595 &opened_dex_file1->GetHeader(),
596 dex_file1_data->GetHeader().file_size_));
597 ASSERT_EQ(dex_file1_data->GetLocation(), opened_dex_file1->GetLocation());
598
599 ASSERT_EQ(dex_file2_data->GetHeader().file_size_, opened_dex_file2->GetHeader().file_size_);
600 ASSERT_EQ(0, memcmp(&dex_file2_data->GetHeader(),
601 &opened_dex_file2->GetHeader(),
602 dex_file2_data->GetHeader().file_size_));
603 ASSERT_EQ(dex_file2_data->GetLocation(), opened_dex_file2->GetLocation());
604}
605
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800606TEST_F(OatTest, DexFileInputCheckOutput) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800607 TestDexFileInput(false, /*low_4gb*/false);
608}
609
610TEST_F(OatTest, DexFileInputCheckOutputLow4GB) {
611 TestDexFileInput(false, /*low_4gb*/true);
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800612}
613
614TEST_F(OatTest, DexFileInputCheckVerifier) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800615 TestDexFileInput(true, /*low_4gb*/false);
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800616}
617
618void OatTest::TestZipFileInput(bool verify) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000619 TimingLogger timings("OatTest::DexFileInput", false, false);
620
621 ScratchFile zip_file;
622 ZipBuilder zip_builder(zip_file.GetFile());
623
624 ScratchFile dex_file1;
625 TestDexFileBuilder builder1;
626 builder1.AddField("Lsome.TestClass;", "long", "someField");
627 builder1.AddMethod("Lsome.TestClass;", "()D", "foo");
628 std::unique_ptr<const DexFile> dex_file1_data = builder1.Build(dex_file1.GetFilename());
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800629
630 MaybeModifyDexFileToFail(verify, dex_file1_data);
631
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000632 bool success = dex_file1.GetFile()->WriteFully(&dex_file1_data->GetHeader(),
633 dex_file1_data->GetHeader().file_size_);
634 ASSERT_TRUE(success);
635 success = dex_file1.GetFile()->Flush() == 0;
636 ASSERT_TRUE(success);
637 success = zip_builder.AddFile("classes.dex",
638 &dex_file1_data->GetHeader(),
639 dex_file1_data->GetHeader().file_size_);
640 ASSERT_TRUE(success);
641
642 ScratchFile dex_file2;
643 TestDexFileBuilder builder2;
644 builder2.AddField("Land.AnotherTestClass;", "boolean", "someOtherField");
645 builder2.AddMethod("Land.AnotherTestClass;", "()J", "bar");
646 std::unique_ptr<const DexFile> dex_file2_data = builder2.Build(dex_file2.GetFilename());
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800647
648 MaybeModifyDexFileToFail(verify, dex_file2_data);
649
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000650 success = dex_file2.GetFile()->WriteFully(&dex_file2_data->GetHeader(),
651 dex_file2_data->GetHeader().file_size_);
652 ASSERT_TRUE(success);
653 success = dex_file2.GetFile()->Flush() == 0;
654 ASSERT_TRUE(success);
655 success = zip_builder.AddFile("classes2.dex",
656 &dex_file2_data->GetHeader(),
657 dex_file2_data->GetHeader().file_size_);
658 ASSERT_TRUE(success);
659
660 success = zip_builder.Finish();
661 ASSERT_TRUE(success) << strerror(errno);
662
663 SafeMap<std::string, std::string> key_value_store;
664 key_value_store.Put(OatHeader::kImageLocationKey, "test.art");
665 {
666 // Test using the AddDexFileSource() interface with the zip file.
667 std::vector<const char*> input_filenames { zip_file.GetFilename().c_str() }; // NOLINT [readability/braces] [4]
668
669 ScratchFile oat_file;
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800670 success = WriteElf(oat_file.GetFile(), input_filenames, key_value_store, verify);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000671
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800672 if (verify) {
673 ASSERT_FALSE(success);
674 } else {
675 ASSERT_TRUE(success);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000676
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800677 std::string error_msg;
678 std::unique_ptr<OatFile> opened_oat_file(OatFile::Open(oat_file.GetFilename(),
679 oat_file.GetFilename(),
680 nullptr,
681 nullptr,
682 false,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800683 /*low_4gb*/false,
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800684 nullptr,
685 &error_msg));
686 ASSERT_TRUE(opened_oat_file != nullptr);
687 ASSERT_EQ(2u, opened_oat_file->GetOatDexFiles().size());
688 std::unique_ptr<const DexFile> opened_dex_file1 =
689 opened_oat_file->GetOatDexFiles()[0]->OpenDexFile(&error_msg);
690 std::unique_ptr<const DexFile> opened_dex_file2 =
691 opened_oat_file->GetOatDexFiles()[1]->OpenDexFile(&error_msg);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000692
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800693 ASSERT_EQ(dex_file1_data->GetHeader().file_size_, opened_dex_file1->GetHeader().file_size_);
694 ASSERT_EQ(0, memcmp(&dex_file1_data->GetHeader(),
695 &opened_dex_file1->GetHeader(),
696 dex_file1_data->GetHeader().file_size_));
697 ASSERT_EQ(DexFile::GetMultiDexLocation(0, zip_file.GetFilename().c_str()),
698 opened_dex_file1->GetLocation());
699
700 ASSERT_EQ(dex_file2_data->GetHeader().file_size_, opened_dex_file2->GetHeader().file_size_);
701 ASSERT_EQ(0, memcmp(&dex_file2_data->GetHeader(),
702 &opened_dex_file2->GetHeader(),
703 dex_file2_data->GetHeader().file_size_));
704 ASSERT_EQ(DexFile::GetMultiDexLocation(1, zip_file.GetFilename().c_str()),
705 opened_dex_file2->GetLocation());
706 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000707 }
708
709 {
710 // Test using the AddZipDexFileSource() interface with the zip file handle.
711 ScopedFd zip_fd(dup(zip_file.GetFd()));
712 ASSERT_NE(-1, zip_fd.get());
713
714 ScratchFile oat_file;
715 success = WriteElf(oat_file.GetFile(),
716 std::move(zip_fd),
717 zip_file.GetFilename().c_str(),
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800718 key_value_store,
719 verify);
720 if (verify) {
721 ASSERT_FALSE(success);
722 } else {
723 ASSERT_TRUE(success);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000724
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800725 std::string error_msg;
726 std::unique_ptr<OatFile> opened_oat_file(OatFile::Open(oat_file.GetFilename(),
727 oat_file.GetFilename(),
728 nullptr,
729 nullptr,
730 false,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800731 /*low_4gb*/false,
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800732 nullptr,
733 &error_msg));
734 ASSERT_TRUE(opened_oat_file != nullptr);
735 ASSERT_EQ(2u, opened_oat_file->GetOatDexFiles().size());
736 std::unique_ptr<const DexFile> opened_dex_file1 =
737 opened_oat_file->GetOatDexFiles()[0]->OpenDexFile(&error_msg);
738 std::unique_ptr<const DexFile> opened_dex_file2 =
739 opened_oat_file->GetOatDexFiles()[1]->OpenDexFile(&error_msg);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000740
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800741 ASSERT_EQ(dex_file1_data->GetHeader().file_size_, opened_dex_file1->GetHeader().file_size_);
742 ASSERT_EQ(0, memcmp(&dex_file1_data->GetHeader(),
743 &opened_dex_file1->GetHeader(),
744 dex_file1_data->GetHeader().file_size_));
745 ASSERT_EQ(DexFile::GetMultiDexLocation(0, zip_file.GetFilename().c_str()),
746 opened_dex_file1->GetLocation());
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000747
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800748 ASSERT_EQ(dex_file2_data->GetHeader().file_size_, opened_dex_file2->GetHeader().file_size_);
749 ASSERT_EQ(0, memcmp(&dex_file2_data->GetHeader(),
750 &opened_dex_file2->GetHeader(),
751 dex_file2_data->GetHeader().file_size_));
752 ASSERT_EQ(DexFile::GetMultiDexLocation(1, zip_file.GetFilename().c_str()),
753 opened_dex_file2->GetLocation());
754 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000755 }
756}
757
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800758TEST_F(OatTest, ZipFileInputCheckOutput) {
759 TestZipFileInput(false);
760}
761
762TEST_F(OatTest, ZipFileInputCheckVerifier) {
763 TestZipFileInput(true);
764}
765
Jeff Hao5d3baf62016-05-23 19:17:04 -0700766TEST_F(OatTest, UpdateChecksum) {
767 InstructionSet insn_set = kX86;
768 std::string error_msg;
769 std::unique_ptr<const InstructionSetFeatures> insn_features(
770 InstructionSetFeatures::FromVariant(insn_set, "default", &error_msg));
771 ASSERT_TRUE(insn_features.get() != nullptr) << error_msg;
772 std::unique_ptr<OatHeader> oat_header(OatHeader::Create(insn_set,
773 insn_features.get(),
774 0u,
775 nullptr));
776 // The starting adler32 value is 1.
777 EXPECT_EQ(1U, oat_header->GetChecksum());
778
779 oat_header->UpdateChecksum(OatHeader::kOatMagic, sizeof(OatHeader::kOatMagic));
780 EXPECT_EQ(64291151U, oat_header->GetChecksum());
781
782 // Make sure that null data does not reset the checksum.
783 oat_header->UpdateChecksum(nullptr, 0);
784 EXPECT_EQ(64291151U, oat_header->GetChecksum());
785
786 oat_header->UpdateChecksum(OatHeader::kOatMagic, sizeof(OatHeader::kOatMagic));
787 EXPECT_EQ(216138397U, oat_header->GetChecksum());
788}
789
Brian Carlstrome24fa612011-09-29 00:53:55 -0700790} // namespace art