blob: 78e9ca91b75a310d76946708255fca0e49090288 [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)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070063 REQUIRES_SHARED(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
David Brazdil7b49e6c2016-09-01 11:06:18 +0100128 bool WriteElf(File* vdex_file,
129 File* oat_file,
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800130 const std::vector<const DexFile*>& dex_files,
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800131 SafeMap<std::string, std::string>& key_value_store,
132 bool verify) {
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800133 TimingLogger timings("WriteElf", false, false);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000134 OatWriter oat_writer(/*compiling_boot_image*/false, &timings);
135 for (const DexFile* dex_file : dex_files) {
136 ArrayRef<const uint8_t> raw_dex_file(
137 reinterpret_cast<const uint8_t*>(&dex_file->GetHeader()),
138 dex_file->GetHeader().file_size_);
139 if (!oat_writer.AddRawDexFileSource(raw_dex_file,
140 dex_file->GetLocation().c_str(),
141 dex_file->GetLocationChecksum())) {
142 return false;
143 }
144 }
David Brazdil7b49e6c2016-09-01 11:06:18 +0100145 return DoWriteElf(vdex_file, oat_file, oat_writer, key_value_store, verify);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000146 }
147
David Brazdil7b49e6c2016-09-01 11:06:18 +0100148 bool WriteElf(File* vdex_file,
149 File* oat_file,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000150 const std::vector<const char*>& dex_filenames,
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800151 SafeMap<std::string, std::string>& key_value_store,
152 bool verify) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000153 TimingLogger timings("WriteElf", false, false);
154 OatWriter oat_writer(/*compiling_boot_image*/false, &timings);
155 for (const char* dex_filename : dex_filenames) {
156 if (!oat_writer.AddDexFileSource(dex_filename, dex_filename)) {
157 return false;
158 }
159 }
David Brazdil7b49e6c2016-09-01 11:06:18 +0100160 return DoWriteElf(vdex_file, oat_file, oat_writer, key_value_store, verify);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000161 }
162
David Brazdil7b49e6c2016-09-01 11:06:18 +0100163 bool WriteElf(File* vdex_file,
164 File* oat_file,
Andreas Gampe43e10b02016-07-15 17:17:34 -0700165 File&& zip_fd,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000166 const char* location,
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800167 SafeMap<std::string, std::string>& key_value_store,
168 bool verify) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000169 TimingLogger timings("WriteElf", false, false);
170 OatWriter oat_writer(/*compiling_boot_image*/false, &timings);
171 if (!oat_writer.AddZippedDexFilesSource(std::move(zip_fd), location)) {
172 return false;
173 }
David Brazdil7b49e6c2016-09-01 11:06:18 +0100174 return DoWriteElf(vdex_file, oat_file, oat_writer, key_value_store, verify);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000175 }
176
David Brazdil7b49e6c2016-09-01 11:06:18 +0100177 bool DoWriteElf(File* vdex_file,
178 File* oat_file,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000179 OatWriter& oat_writer,
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800180 SafeMap<std::string, std::string>& key_value_store,
181 bool verify) {
Vladimir Marko10c13562015-11-25 14:33:36 +0000182 std::unique_ptr<ElfWriter> elf_writer = CreateElfWriterQuick(
183 compiler_driver_->GetInstructionSet(),
David Srbecky5d811202016-03-08 13:21:22 +0000184 compiler_driver_->GetInstructionSetFeatures(),
Vladimir Marko10c13562015-11-25 14:33:36 +0000185 &compiler_driver_->GetCompilerOptions(),
David Brazdil7b49e6c2016-09-01 11:06:18 +0100186 oat_file);
Vladimir Marko10c13562015-11-25 14:33:36 +0000187 elf_writer->Start();
David Brazdil7b49e6c2016-09-01 11:06:18 +0100188 OutputStream* oat_rodata = elf_writer->StartRoData();
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000189 std::unique_ptr<MemMap> opened_dex_files_map;
190 std::vector<std::unique_ptr<const DexFile>> opened_dex_files;
David Brazdil7b49e6c2016-09-01 11:06:18 +0100191 if (!oat_writer.WriteAndOpenDexFiles(kIsVdexEnabled ? vdex_file : oat_file,
192 oat_rodata,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000193 compiler_driver_->GetInstructionSet(),
194 compiler_driver_->GetInstructionSetFeatures(),
195 &key_value_store,
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800196 verify,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000197 &opened_dex_files_map,
198 &opened_dex_files)) {
199 return false;
200 }
201 Runtime* runtime = Runtime::Current();
202 ClassLinker* const class_linker = runtime->GetClassLinker();
203 std::vector<const DexFile*> dex_files;
204 for (const std::unique_ptr<const DexFile>& dex_file : opened_dex_files) {
205 dex_files.push_back(dex_file.get());
206 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierf284d442016-06-02 11:48:30 -0700207 class_linker->RegisterDexFile(*dex_file, nullptr);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000208 }
Vladimir Marko944da602016-02-19 12:27:55 +0000209 linker::MultiOatRelativePatcher patcher(compiler_driver_->GetInstructionSet(),
210 instruction_set_features_.get());
211 oat_writer.PrepareLayout(compiler_driver_.get(), nullptr, dex_files, &patcher);
212 size_t rodata_size = oat_writer.GetOatHeader().GetExecutableOffset();
David Brazdil7b49e6c2016-09-01 11:06:18 +0100213 size_t text_size = oat_writer.GetOatSize() - rodata_size;
Vladimir Marko944da602016-02-19 12:27:55 +0000214 elf_writer->SetLoadedSectionSizes(rodata_size, text_size, oat_writer.GetBssSize());
215
David Brazdil7b49e6c2016-09-01 11:06:18 +0100216 if (!oat_writer.WriteRodata(oat_rodata)) {
Vladimir Marko10c13562015-11-25 14:33:36 +0000217 return false;
218 }
David Brazdil7b49e6c2016-09-01 11:06:18 +0100219 elf_writer->EndRoData(oat_rodata);
Vladimir Marko10c13562015-11-25 14:33:36 +0000220
221 OutputStream* text = elf_writer->StartText();
222 if (!oat_writer.WriteCode(text)) {
223 return false;
224 }
225 elf_writer->EndText(text);
226
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000227 if (!oat_writer.WriteHeader(elf_writer->GetStream(), 42U, 4096U, 0)) {
228 return false;
229 }
230
Vladimir Marko10c13562015-11-25 14:33:36 +0000231 elf_writer->WriteDynamicSection();
Vladimir Marko49b0f452015-12-10 13:49:19 +0000232 elf_writer->WriteDebugInfo(oat_writer.GetMethodDebugInfo());
233 elf_writer->WritePatchLocations(oat_writer.GetAbsolutePatchLocations());
Vladimir Marko10c13562015-11-25 14:33:36 +0000234
235 return elf_writer->End();
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800236 }
237
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800238 void TestDexFileInput(bool verify, bool low_4gb);
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800239 void TestZipFileInput(bool verify);
240
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800241 std::unique_ptr<const InstructionSetFeatures> insn_features_;
242 std::unique_ptr<QuickCompilerCallbacks> callbacks_;
Logan Chieneeb7edf2012-03-09 20:38:39 +0800243};
Brian Carlstrome24fa612011-09-29 00:53:55 -0700244
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000245class ZipBuilder {
246 public:
247 explicit ZipBuilder(File* zip_file) : zip_file_(zip_file) { }
248
249 bool AddFile(const char* location, const void* data, size_t size) {
250 off_t offset = lseek(zip_file_->Fd(), 0, SEEK_CUR);
251 if (offset == static_cast<off_t>(-1)) {
252 return false;
253 }
254
255 ZipFileHeader file_header;
256 file_header.crc32 = crc32(0u, reinterpret_cast<const Bytef*>(data), size);
257 file_header.compressed_size = size;
258 file_header.uncompressed_size = size;
259 file_header.filename_length = strlen(location);
260
261 if (!zip_file_->WriteFully(&file_header, sizeof(file_header)) ||
262 !zip_file_->WriteFully(location, file_header.filename_length) ||
263 !zip_file_->WriteFully(data, size)) {
264 return false;
265 }
266
267 CentralDirectoryFileHeader cdfh;
268 cdfh.crc32 = file_header.crc32;
269 cdfh.compressed_size = size;
270 cdfh.uncompressed_size = size;
271 cdfh.filename_length = file_header.filename_length;
272 cdfh.relative_offset_of_local_file_header = offset;
273 file_data_.push_back(FileData { cdfh, location });
274 return true;
275 }
276
277 bool Finish() {
278 off_t offset = lseek(zip_file_->Fd(), 0, SEEK_CUR);
279 if (offset == static_cast<off_t>(-1)) {
280 return false;
281 }
282
283 size_t central_directory_size = 0u;
284 for (const FileData& file_data : file_data_) {
285 if (!zip_file_->WriteFully(&file_data.cdfh, sizeof(file_data.cdfh)) ||
286 !zip_file_->WriteFully(file_data.location, file_data.cdfh.filename_length)) {
287 return false;
288 }
289 central_directory_size += sizeof(file_data.cdfh) + file_data.cdfh.filename_length;
290 }
291 EndOfCentralDirectoryRecord eocd_record;
292 eocd_record.number_of_central_directory_records_on_this_disk = file_data_.size();
293 eocd_record.total_number_of_central_directory_records = file_data_.size();
294 eocd_record.size_of_central_directory = central_directory_size;
295 eocd_record.offset_of_start_of_central_directory = offset;
296 return
297 zip_file_->WriteFully(&eocd_record, sizeof(eocd_record)) &&
298 zip_file_->Flush() == 0;
299 }
300
301 private:
302 struct PACKED(1) ZipFileHeader {
303 uint32_t signature = 0x04034b50;
304 uint16_t version_needed_to_extract = 10;
305 uint16_t general_purpose_bit_flag = 0;
306 uint16_t compression_method = 0; // 0 = store only.
307 uint16_t file_last_modification_time = 0u;
308 uint16_t file_last_modification_date = 0u;
309 uint32_t crc32;
310 uint32_t compressed_size;
311 uint32_t uncompressed_size;
312 uint16_t filename_length;
313 uint16_t extra_field_length = 0u; // No extra fields.
314 };
315
316 struct PACKED(1) CentralDirectoryFileHeader {
317 uint32_t signature = 0x02014b50;
318 uint16_t version_made_by = 10;
319 uint16_t version_needed_to_extract = 10;
320 uint16_t general_purpose_bit_flag = 0;
321 uint16_t compression_method = 0; // 0 = store only.
322 uint16_t file_last_modification_time = 0u;
323 uint16_t file_last_modification_date = 0u;
324 uint32_t crc32;
325 uint32_t compressed_size;
326 uint32_t uncompressed_size;
327 uint16_t filename_length;
328 uint16_t extra_field_length = 0u; // No extra fields.
329 uint16_t file_comment_length = 0u; // No file comment.
330 uint16_t disk_number_where_file_starts = 0u;
331 uint16_t internal_file_attributes = 0u;
332 uint32_t external_file_attributes = 0u;
333 uint32_t relative_offset_of_local_file_header;
334 };
335
336 struct PACKED(1) EndOfCentralDirectoryRecord {
337 uint32_t signature = 0x06054b50;
338 uint16_t number_of_this_disk = 0u;
339 uint16_t disk_where_central_directory_starts = 0u;
340 uint16_t number_of_central_directory_records_on_this_disk;
341 uint16_t total_number_of_central_directory_records;
342 uint32_t size_of_central_directory;
343 uint32_t offset_of_start_of_central_directory;
344 uint16_t comment_length = 0u; // No file comment.
345 };
346
347 struct FileData {
348 CentralDirectoryFileHeader cdfh;
349 const char* location;
350 };
351
352 File* zip_file_;
353 std::vector<FileData> file_data_;
354};
355
Brian Carlstrome24fa612011-09-29 00:53:55 -0700356TEST_F(OatTest, WriteRead) {
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800357 TimingLogger timings("OatTest::WriteRead", false, false);
Jesse Wilson254db0f2011-11-16 16:44:11 -0500358 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700359
Ian Rogersef7d42f2014-01-06 12:55:46 -0800360 // TODO: make selectable.
Elliott Hughes956af0f2014-12-11 14:34:28 -0800361 Compiler::Kind compiler_kind = Compiler::kQuick;
Ian Rogersa073c672013-07-30 17:43:55 -0700362 InstructionSet insn_set = kIsTargetBuild ? kThumb2 : kX86;
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700363 std::string error_msg;
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800364 SetupCompiler(compiler_kind, insn_set, std::vector<std::string>(), /*out*/ &error_msg);
365
Ian Rogersd4c4d952014-10-16 20:31:53 -0700366 jobject class_loader = nullptr;
Brian Carlstromba150c32013-08-27 17:31:03 -0700367 if (kCompile) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800368 TimingLogger timings2("OatTest::WriteRead", false, false);
Vladimir Markod1eaf0d2015-10-29 12:18:29 +0000369 compiler_driver_->SetDexFilesForOatFile(class_linker->GetBootClassPath());
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800370 compiler_driver_->CompileAll(class_loader, class_linker->GetBootClassPath(), &timings2);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700371 }
372
David Brazdil7b49e6c2016-09-01 11:06:18 +0100373 ScratchFile tmp_oat, tmp_vdex(tmp_oat, ".vdex");
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700374 SafeMap<std::string, std::string> key_value_store;
375 key_value_store.Put(OatHeader::kImageLocationKey, "lue.art");
David Brazdil7b49e6c2016-09-01 11:06:18 +0100376 bool success = WriteElf(tmp_vdex.GetFile(),
377 tmp_oat.GetFile(),
378 class_linker->GetBootClassPath(),
379 key_value_store,
380 false);
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700381 ASSERT_TRUE(success);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700382
Brian Carlstromba150c32013-08-27 17:31:03 -0700383 if (kCompile) { // OatWriter strips the code, regenerate to compare
Ian Rogers3d504072014-03-01 09:16:49 -0800384 compiler_driver_->CompileAll(class_loader, class_linker->GetBootClassPath(), &timings);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700385 }
David Brazdil7b49e6c2016-09-01 11:06:18 +0100386 std::unique_ptr<OatFile> oat_file(OatFile::Open(tmp_oat.GetFilename(),
387 tmp_oat.GetFilename(),
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800388 nullptr,
389 nullptr,
390 false,
391 /*low_4gb*/true,
392 nullptr,
393 &error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700394 ASSERT_TRUE(oat_file.get() != nullptr) << error_msg;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700395 const OatHeader& oat_header = oat_file->GetOatHeader();
Brian Carlstromf852fb22012-10-19 11:01:58 -0700396 ASSERT_TRUE(oat_header.IsValid());
Yi Kong9515b512015-11-11 14:36:07 +0000397 ASSERT_EQ(class_linker->GetBootClassPath().size(), oat_header.GetDexFileCount()); // core
Brian Carlstrom28db0122012-10-18 16:20:41 -0700398 ASSERT_EQ(42U, oat_header.GetImageFileLocationOatChecksum());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800399 ASSERT_EQ(4096U, oat_header.GetImageFileLocationOatDataBegin());
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700400 ASSERT_EQ("lue.art", std::string(oat_header.GetStoreValueByKey(OatHeader::kImageLocationKey)));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700401
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800402 ASSERT_TRUE(java_lang_dex_file_ != nullptr);
403 const DexFile& dex_file = *java_lang_dex_file_;
404 uint32_t dex_file_checksum = dex_file.GetLocationChecksum();
405 const OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(dex_file.GetLocation().c_str(),
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700406 &dex_file_checksum);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700407 ASSERT_TRUE(oat_dex_file != nullptr);
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800408 CHECK_EQ(dex_file.GetLocationChecksum(), oat_dex_file->GetDexFileLocationChecksum());
Vladimir Markof4da6752014-08-01 19:04:18 +0100409 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700410 auto pointer_size = class_linker->GetImagePointerSize();
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800411 for (size_t i = 0; i < dex_file.NumClassDefs(); i++) {
412 const DexFile::ClassDef& class_def = dex_file.GetClassDef(i);
413 const uint8_t* class_data = dex_file.GetClassData(class_def);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700414
Brian Carlstromba150c32013-08-27 17:31:03 -0700415 size_t num_virtual_methods = 0;
Ian Rogersd4c4d952014-10-16 20:31:53 -0700416 if (class_data != nullptr) {
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800417 ClassDataItemIterator it(dex_file, class_data);
Ian Rogers0571d352011-11-03 19:51:38 -0700418 num_virtual_methods = it.NumVirtualMethods();
419 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700420
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800421 const char* descriptor = dex_file.GetClassDescriptor(class_def);
Mathieu Chartier9865bde2015-12-21 09:58:16 -0800422 mirror::Class* klass = class_linker->FindClass(soa.Self(),
423 descriptor,
424 ScopedNullHandle<mirror::ClassLoader>());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700425
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100426 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(i);
427 CHECK_EQ(mirror::Class::Status::kStatusNotReady, oat_class.GetStatus()) << descriptor;
Brian Carlstromba150c32013-08-27 17:31:03 -0700428 CHECK_EQ(kCompile ? OatClassType::kOatClassAllCompiled : OatClassType::kOatClassNoneCompiled,
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100429 oat_class.GetType()) << descriptor;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700430
431 size_t method_index = 0;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700432 for (auto& m : klass->GetDirectMethods(pointer_size)) {
433 CheckMethod(&m, oat_class.GetOatMethod(method_index), dex_file);
434 ++method_index;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700435 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700436 size_t visited_virtuals = 0;
Alex Lighte64300b2015-12-15 15:02:47 -0800437 // TODO We should also check copied methods in this test.
438 for (auto& m : klass->GetDeclaredVirtualMethods(pointer_size)) {
Alex Lightfcea56f2016-02-17 11:59:05 -0800439 if (!klass->IsInterface()) {
Alex Light36121492016-02-22 13:43:29 -0800440 EXPECT_FALSE(m.IsCopied());
Alex Lightfcea56f2016-02-17 11:59:05 -0800441 }
Alex Lighte64300b2015-12-15 15:02:47 -0800442 CheckMethod(&m, oat_class.GetOatMethod(method_index), dex_file);
443 ++method_index;
444 ++visited_virtuals;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700445 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700446 EXPECT_EQ(visited_virtuals, num_virtual_methods);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700447 }
448}
449
Brian Carlstrom341df942012-06-27 12:29:22 -0700450TEST_F(OatTest, OatHeaderSizeCheck) {
451 // If this test is failing and you have to update these constants,
452 // it is time to update OatHeader::kOatVersion
Elliott Hughes956af0f2014-12-11 14:34:28 -0800453 EXPECT_EQ(72U, sizeof(OatHeader));
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800454 EXPECT_EQ(4U, sizeof(OatMethodOffsets));
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100455 EXPECT_EQ(20U, sizeof(OatQuickMethodHeader));
Igor Murashkin9d4b6da2016-07-29 09:51:58 -0700456 EXPECT_EQ(164 * static_cast<size_t>(GetInstructionSetPointerSize(kRuntimeISA)),
Andreas Gampe542451c2016-07-26 09:02:02 -0700457 sizeof(QuickEntryPoints));
Brian Carlstrom341df942012-06-27 12:29:22 -0700458}
459
Brian Carlstromf852fb22012-10-19 11:01:58 -0700460TEST_F(OatTest, OatHeaderIsValid) {
Jeff Hao5d3baf62016-05-23 19:17:04 -0700461 InstructionSet insn_set = kX86;
462 std::string error_msg;
463 std::unique_ptr<const InstructionSetFeatures> insn_features(
464 InstructionSetFeatures::FromVariant(insn_set, "default", &error_msg));
465 ASSERT_TRUE(insn_features.get() != nullptr) << error_msg;
466 std::unique_ptr<OatHeader> oat_header(OatHeader::Create(insn_set,
467 insn_features.get(),
468 0u,
469 nullptr));
470 ASSERT_NE(oat_header.get(), nullptr);
471 ASSERT_TRUE(oat_header->IsValid());
Brian Carlstromf852fb22012-10-19 11:01:58 -0700472
Jeff Hao5d3baf62016-05-23 19:17:04 -0700473 char* magic = const_cast<char*>(oat_header->GetMagic());
474 strcpy(magic, ""); // bad magic
475 ASSERT_FALSE(oat_header->IsValid());
476 strcpy(magic, "oat\n000"); // bad version
477 ASSERT_FALSE(oat_header->IsValid());
Brian Carlstromf852fb22012-10-19 11:01:58 -0700478}
479
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800480TEST_F(OatTest, EmptyTextSection) {
481 TimingLogger timings("OatTest::EmptyTextSection", false, false);
482
483 // TODO: make selectable.
484 Compiler::Kind compiler_kind = Compiler::kQuick;
485 InstructionSet insn_set = kRuntimeISA;
486 if (insn_set == kArm) insn_set = kThumb2;
487 std::string error_msg;
488 std::vector<std::string> compiler_options;
489 compiler_options.push_back("--compiler-filter=verify-at-runtime");
490 SetupCompiler(compiler_kind, insn_set, compiler_options, /*out*/ &error_msg);
491
492 jobject class_loader;
493 {
494 ScopedObjectAccess soa(Thread::Current());
495 class_loader = LoadDex("Main");
496 }
497 ASSERT_TRUE(class_loader != nullptr);
498 std::vector<const DexFile*> dex_files = GetDexFiles(class_loader);
499 ASSERT_TRUE(!dex_files.empty());
500
501 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
502 for (const DexFile* dex_file : dex_files) {
503 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierf284d442016-06-02 11:48:30 -0700504 class_linker->RegisterDexFile(*dex_file, soa.Decode<mirror::ClassLoader*>(class_loader));
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800505 }
506 compiler_driver_->SetDexFilesForOatFile(dex_files);
507 compiler_driver_->CompileAll(class_loader, dex_files, &timings);
508
David Brazdil7b49e6c2016-09-01 11:06:18 +0100509 ScratchFile tmp_oat, tmp_vdex(tmp_oat, ".vdex");
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800510 SafeMap<std::string, std::string> key_value_store;
511 key_value_store.Put(OatHeader::kImageLocationKey, "test.art");
David Brazdil7b49e6c2016-09-01 11:06:18 +0100512 bool success = WriteElf(tmp_vdex.GetFile(), tmp_oat.GetFile(), dex_files, key_value_store, false);
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800513 ASSERT_TRUE(success);
514
David Brazdil7b49e6c2016-09-01 11:06:18 +0100515 std::unique_ptr<OatFile> oat_file(OatFile::Open(tmp_oat.GetFilename(),
516 tmp_oat.GetFilename(),
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800517 nullptr,
518 nullptr,
519 false,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800520 /*low_4gb*/false,
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800521 nullptr,
522 &error_msg));
523 ASSERT_TRUE(oat_file != nullptr);
David Brazdil7b49e6c2016-09-01 11:06:18 +0100524 EXPECT_LT(static_cast<size_t>(oat_file->Size()),
525 static_cast<size_t>(tmp_oat.GetFile()->GetLength()));
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800526}
527
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800528static void MaybeModifyDexFileToFail(bool verify, std::unique_ptr<const DexFile>& data) {
529 // If in verify mode (= fail the verifier mode), make sure we fail early. We'll fail already
530 // because of the missing map, but that may lead to out of bounds reads.
531 if (verify) {
532 const_cast<DexFile::Header*>(&data->GetHeader())->checksum_++;
533 }
534}
535
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800536void OatTest::TestDexFileInput(bool verify, bool low_4gb) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000537 TimingLogger timings("OatTest::DexFileInput", false, false);
538
539 std::vector<const char*> input_filenames;
540
541 ScratchFile dex_file1;
542 TestDexFileBuilder builder1;
543 builder1.AddField("Lsome.TestClass;", "int", "someField");
544 builder1.AddMethod("Lsome.TestClass;", "()I", "foo");
545 std::unique_ptr<const DexFile> dex_file1_data = builder1.Build(dex_file1.GetFilename());
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800546
547 MaybeModifyDexFileToFail(verify, dex_file1_data);
548
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000549 bool success = dex_file1.GetFile()->WriteFully(&dex_file1_data->GetHeader(),
550 dex_file1_data->GetHeader().file_size_);
551 ASSERT_TRUE(success);
552 success = dex_file1.GetFile()->Flush() == 0;
553 ASSERT_TRUE(success);
554 input_filenames.push_back(dex_file1.GetFilename().c_str());
555
556 ScratchFile dex_file2;
557 TestDexFileBuilder builder2;
558 builder2.AddField("Land.AnotherTestClass;", "boolean", "someOtherField");
559 builder2.AddMethod("Land.AnotherTestClass;", "()J", "bar");
560 std::unique_ptr<const DexFile> dex_file2_data = builder2.Build(dex_file2.GetFilename());
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800561
562 MaybeModifyDexFileToFail(verify, dex_file2_data);
563
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000564 success = dex_file2.GetFile()->WriteFully(&dex_file2_data->GetHeader(),
565 dex_file2_data->GetHeader().file_size_);
566 ASSERT_TRUE(success);
567 success = dex_file2.GetFile()->Flush() == 0;
568 ASSERT_TRUE(success);
569 input_filenames.push_back(dex_file2.GetFilename().c_str());
570
David Brazdil7b49e6c2016-09-01 11:06:18 +0100571 ScratchFile oat_file, vdex_file(oat_file, ".vdex");
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000572 SafeMap<std::string, std::string> key_value_store;
573 key_value_store.Put(OatHeader::kImageLocationKey, "test.art");
David Brazdil7b49e6c2016-09-01 11:06:18 +0100574 success = WriteElf(vdex_file.GetFile(),
575 oat_file.GetFile(),
576 input_filenames,
577 key_value_store,
578 verify);
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800579
580 // In verify mode, we expect failure.
581 if (verify) {
582 ASSERT_FALSE(success);
583 return;
584 }
585
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000586 ASSERT_TRUE(success);
587
588 std::string error_msg;
589 std::unique_ptr<OatFile> opened_oat_file(OatFile::Open(oat_file.GetFilename(),
590 oat_file.GetFilename(),
591 nullptr,
592 nullptr,
593 false,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800594 low_4gb,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000595 nullptr,
596 &error_msg));
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800597 if (low_4gb) {
598 uintptr_t begin = reinterpret_cast<uintptr_t>(opened_oat_file->Begin());
599 EXPECT_EQ(begin, static_cast<uint32_t>(begin));
600 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000601 ASSERT_TRUE(opened_oat_file != nullptr);
602 ASSERT_EQ(2u, opened_oat_file->GetOatDexFiles().size());
603 std::unique_ptr<const DexFile> opened_dex_file1 =
604 opened_oat_file->GetOatDexFiles()[0]->OpenDexFile(&error_msg);
605 std::unique_ptr<const DexFile> opened_dex_file2 =
606 opened_oat_file->GetOatDexFiles()[1]->OpenDexFile(&error_msg);
607
608 ASSERT_EQ(dex_file1_data->GetHeader().file_size_, opened_dex_file1->GetHeader().file_size_);
609 ASSERT_EQ(0, memcmp(&dex_file1_data->GetHeader(),
610 &opened_dex_file1->GetHeader(),
611 dex_file1_data->GetHeader().file_size_));
612 ASSERT_EQ(dex_file1_data->GetLocation(), opened_dex_file1->GetLocation());
613
614 ASSERT_EQ(dex_file2_data->GetHeader().file_size_, opened_dex_file2->GetHeader().file_size_);
615 ASSERT_EQ(0, memcmp(&dex_file2_data->GetHeader(),
616 &opened_dex_file2->GetHeader(),
617 dex_file2_data->GetHeader().file_size_));
618 ASSERT_EQ(dex_file2_data->GetLocation(), opened_dex_file2->GetLocation());
619}
620
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800621TEST_F(OatTest, DexFileInputCheckOutput) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800622 TestDexFileInput(false, /*low_4gb*/false);
623}
624
625TEST_F(OatTest, DexFileInputCheckOutputLow4GB) {
626 TestDexFileInput(false, /*low_4gb*/true);
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800627}
628
629TEST_F(OatTest, DexFileInputCheckVerifier) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800630 TestDexFileInput(true, /*low_4gb*/false);
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800631}
632
633void OatTest::TestZipFileInput(bool verify) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000634 TimingLogger timings("OatTest::DexFileInput", false, false);
635
636 ScratchFile zip_file;
637 ZipBuilder zip_builder(zip_file.GetFile());
638
639 ScratchFile dex_file1;
640 TestDexFileBuilder builder1;
641 builder1.AddField("Lsome.TestClass;", "long", "someField");
642 builder1.AddMethod("Lsome.TestClass;", "()D", "foo");
643 std::unique_ptr<const DexFile> dex_file1_data = builder1.Build(dex_file1.GetFilename());
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800644
645 MaybeModifyDexFileToFail(verify, dex_file1_data);
646
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000647 bool success = dex_file1.GetFile()->WriteFully(&dex_file1_data->GetHeader(),
648 dex_file1_data->GetHeader().file_size_);
649 ASSERT_TRUE(success);
650 success = dex_file1.GetFile()->Flush() == 0;
651 ASSERT_TRUE(success);
652 success = zip_builder.AddFile("classes.dex",
653 &dex_file1_data->GetHeader(),
654 dex_file1_data->GetHeader().file_size_);
655 ASSERT_TRUE(success);
656
657 ScratchFile dex_file2;
658 TestDexFileBuilder builder2;
659 builder2.AddField("Land.AnotherTestClass;", "boolean", "someOtherField");
660 builder2.AddMethod("Land.AnotherTestClass;", "()J", "bar");
661 std::unique_ptr<const DexFile> dex_file2_data = builder2.Build(dex_file2.GetFilename());
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800662
663 MaybeModifyDexFileToFail(verify, dex_file2_data);
664
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000665 success = dex_file2.GetFile()->WriteFully(&dex_file2_data->GetHeader(),
666 dex_file2_data->GetHeader().file_size_);
667 ASSERT_TRUE(success);
668 success = dex_file2.GetFile()->Flush() == 0;
669 ASSERT_TRUE(success);
670 success = zip_builder.AddFile("classes2.dex",
671 &dex_file2_data->GetHeader(),
672 dex_file2_data->GetHeader().file_size_);
673 ASSERT_TRUE(success);
674
675 success = zip_builder.Finish();
676 ASSERT_TRUE(success) << strerror(errno);
677
678 SafeMap<std::string, std::string> key_value_store;
679 key_value_store.Put(OatHeader::kImageLocationKey, "test.art");
680 {
681 // Test using the AddDexFileSource() interface with the zip file.
682 std::vector<const char*> input_filenames { zip_file.GetFilename().c_str() }; // NOLINT [readability/braces] [4]
683
David Brazdil7b49e6c2016-09-01 11:06:18 +0100684 ScratchFile oat_file, vdex_file(oat_file, ".vdex");
685 success = WriteElf(vdex_file.GetFile(), oat_file.GetFile(),
686 input_filenames, key_value_store, verify);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000687
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800688 if (verify) {
689 ASSERT_FALSE(success);
690 } else {
691 ASSERT_TRUE(success);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000692
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800693 std::string error_msg;
694 std::unique_ptr<OatFile> opened_oat_file(OatFile::Open(oat_file.GetFilename(),
695 oat_file.GetFilename(),
696 nullptr,
697 nullptr,
698 false,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800699 /*low_4gb*/false,
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800700 nullptr,
701 &error_msg));
702 ASSERT_TRUE(opened_oat_file != nullptr);
703 ASSERT_EQ(2u, opened_oat_file->GetOatDexFiles().size());
704 std::unique_ptr<const DexFile> opened_dex_file1 =
705 opened_oat_file->GetOatDexFiles()[0]->OpenDexFile(&error_msg);
706 std::unique_ptr<const DexFile> opened_dex_file2 =
707 opened_oat_file->GetOatDexFiles()[1]->OpenDexFile(&error_msg);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000708
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800709 ASSERT_EQ(dex_file1_data->GetHeader().file_size_, opened_dex_file1->GetHeader().file_size_);
710 ASSERT_EQ(0, memcmp(&dex_file1_data->GetHeader(),
711 &opened_dex_file1->GetHeader(),
712 dex_file1_data->GetHeader().file_size_));
713 ASSERT_EQ(DexFile::GetMultiDexLocation(0, zip_file.GetFilename().c_str()),
714 opened_dex_file1->GetLocation());
715
716 ASSERT_EQ(dex_file2_data->GetHeader().file_size_, opened_dex_file2->GetHeader().file_size_);
717 ASSERT_EQ(0, memcmp(&dex_file2_data->GetHeader(),
718 &opened_dex_file2->GetHeader(),
719 dex_file2_data->GetHeader().file_size_));
720 ASSERT_EQ(DexFile::GetMultiDexLocation(1, zip_file.GetFilename().c_str()),
721 opened_dex_file2->GetLocation());
722 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000723 }
724
725 {
726 // Test using the AddZipDexFileSource() interface with the zip file handle.
Andreas Gampe43e10b02016-07-15 17:17:34 -0700727 File zip_fd(dup(zip_file.GetFd()), /* check_usage */ false);
728 ASSERT_NE(-1, zip_fd.Fd());
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000729
David Brazdil7b49e6c2016-09-01 11:06:18 +0100730 ScratchFile oat_file, vdex_file(oat_file, ".vdex");
731 success = WriteElf(vdex_file.GetFile(),
732 oat_file.GetFile(),
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000733 std::move(zip_fd),
734 zip_file.GetFilename().c_str(),
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800735 key_value_store,
736 verify);
737 if (verify) {
738 ASSERT_FALSE(success);
739 } else {
740 ASSERT_TRUE(success);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000741
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800742 std::string error_msg;
743 std::unique_ptr<OatFile> opened_oat_file(OatFile::Open(oat_file.GetFilename(),
744 oat_file.GetFilename(),
745 nullptr,
746 nullptr,
747 false,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800748 /*low_4gb*/false,
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800749 nullptr,
750 &error_msg));
751 ASSERT_TRUE(opened_oat_file != nullptr);
752 ASSERT_EQ(2u, opened_oat_file->GetOatDexFiles().size());
753 std::unique_ptr<const DexFile> opened_dex_file1 =
754 opened_oat_file->GetOatDexFiles()[0]->OpenDexFile(&error_msg);
755 std::unique_ptr<const DexFile> opened_dex_file2 =
756 opened_oat_file->GetOatDexFiles()[1]->OpenDexFile(&error_msg);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000757
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800758 ASSERT_EQ(dex_file1_data->GetHeader().file_size_, opened_dex_file1->GetHeader().file_size_);
759 ASSERT_EQ(0, memcmp(&dex_file1_data->GetHeader(),
760 &opened_dex_file1->GetHeader(),
761 dex_file1_data->GetHeader().file_size_));
762 ASSERT_EQ(DexFile::GetMultiDexLocation(0, zip_file.GetFilename().c_str()),
763 opened_dex_file1->GetLocation());
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000764
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800765 ASSERT_EQ(dex_file2_data->GetHeader().file_size_, opened_dex_file2->GetHeader().file_size_);
766 ASSERT_EQ(0, memcmp(&dex_file2_data->GetHeader(),
767 &opened_dex_file2->GetHeader(),
768 dex_file2_data->GetHeader().file_size_));
769 ASSERT_EQ(DexFile::GetMultiDexLocation(1, zip_file.GetFilename().c_str()),
770 opened_dex_file2->GetLocation());
771 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000772 }
773}
774
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800775TEST_F(OatTest, ZipFileInputCheckOutput) {
776 TestZipFileInput(false);
777}
778
779TEST_F(OatTest, ZipFileInputCheckVerifier) {
780 TestZipFileInput(true);
781}
782
Jeff Hao5d3baf62016-05-23 19:17:04 -0700783TEST_F(OatTest, UpdateChecksum) {
784 InstructionSet insn_set = kX86;
785 std::string error_msg;
786 std::unique_ptr<const InstructionSetFeatures> insn_features(
787 InstructionSetFeatures::FromVariant(insn_set, "default", &error_msg));
788 ASSERT_TRUE(insn_features.get() != nullptr) << error_msg;
789 std::unique_ptr<OatHeader> oat_header(OatHeader::Create(insn_set,
790 insn_features.get(),
791 0u,
792 nullptr));
793 // The starting adler32 value is 1.
794 EXPECT_EQ(1U, oat_header->GetChecksum());
795
796 oat_header->UpdateChecksum(OatHeader::kOatMagic, sizeof(OatHeader::kOatMagic));
797 EXPECT_EQ(64291151U, oat_header->GetChecksum());
798
799 // Make sure that null data does not reset the checksum.
800 oat_header->UpdateChecksum(nullptr, 0);
801 EXPECT_EQ(64291151U, oat_header->GetChecksum());
802
803 oat_header->UpdateChecksum(OatHeader::kOatMagic, sizeof(OatHeader::kOatMagic));
804 EXPECT_EQ(216138397U, oat_header->GetChecksum());
805}
806
Brian Carlstrome24fa612011-09-29 00:53:55 -0700807} // namespace art