blob: 0a778b0954c268f442d51bdfd6908edd29a47b91 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Brian Carlstrome24fa612011-09-29 00:53:55 -070016
Ian Rogersd582fa42014-11-05 23:46:43 -080017#include "arch/instruction_set_features.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070018#include "art_method-inl.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070019#include "base/enums.h"
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -080020#include "base/unix_file/fd_file.h"
Andreas Gampe53c913b2014-08-12 23:19:23 -070021#include "class_linker.h"
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080022#include "common_compiler_test.h"
Vladimir Marko20f85592015-03-19 10:07:02 +000023#include "compiled_method.h"
Ian Rogerse63db272014-07-15 15:36:11 -070024#include "compiler.h"
David Srbecky4fda4eb2016-02-05 13:34:46 +000025#include "debug/method_debug_info.h"
Ian Rogerse63db272014-07-15 15:36:11 -070026#include "dex/quick_compiler_callbacks.h"
Mathieu Chartier5bdab122015-01-26 18:30:19 -080027#include "dex/verification_results.h"
Vladimir Marko20f85592015-03-19 10:07:02 +000028#include "driver/compiler_driver.h"
29#include "driver/compiler_options.h"
Vladimir Marko10c13562015-11-25 14:33:36 +000030#include "elf_writer.h"
31#include "elf_writer_quick.h"
Vladimir Marko7624d252014-05-02 14:40:15 +010032#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffrayf54e5df2016-12-01 10:45:08 +000033#include "linker/buffered_output_stream.h"
34#include "linker/file_output_stream.h"
Vladimir Marko944da602016-02-19 12:27:55 +000035#include "linker/multi_oat_relative_patcher.h"
Vladimir Marko131980f2015-12-03 18:29:23 +000036#include "linker/vector_output_stream.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080037#include "mirror/class-inl.h"
Ian Rogerse63db272014-07-15 15:36:11 -070038#include "mirror/object-inl.h"
David Srbecky4fda4eb2016-02-05 13:34:46 +000039#include "mirror/object_array-inl.h"
Vladimir Marko7624d252014-05-02 14:40:15 +010040#include "oat_file-inl.h"
Ian Rogerse63db272014-07-15 15:36:11 -070041#include "oat_writer.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070042#include "scoped_thread_state_change-inl.h"
Vladimir Marko9bdf1082016-01-21 12:15:52 +000043#include "utils/test_dex_file_builder.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070044
Brian Carlstrome24fa612011-09-29 00:53:55 -070045namespace art {
46
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -080047NO_RETURN static void Usage(const char* fmt, ...) {
48 va_list ap;
49 va_start(ap, fmt);
50 std::string error;
51 StringAppendV(&error, fmt, ap);
52 LOG(FATAL) << error;
53 va_end(ap);
54 UNREACHABLE();
55}
56
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080057class OatTest : public CommonCompilerTest {
Logan Chieneeb7edf2012-03-09 20:38:39 +080058 protected:
Brian Carlstromba150c32013-08-27 17:31:03 -070059 static const bool kCompile = false; // DISABLED_ due to the time to compile libcore
60
Mathieu Chartiere401d142015-04-22 13:56:20 -070061 void CheckMethod(ArtMethod* method,
Logan Chieneeb7edf2012-03-09 20:38:39 +080062 const OatFile::OatMethod& oat_method,
Richard Uhlerfbef44d2014-12-23 09:48:51 -080063 const DexFile& dex_file)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070064 REQUIRES_SHARED(Locks::mutator_lock_) {
Logan Chieneeb7edf2012-03-09 20:38:39 +080065 const CompiledMethod* compiled_method =
Richard Uhlerfbef44d2014-12-23 09:48:51 -080066 compiler_driver_->GetCompiledMethod(MethodReference(&dex_file,
Brian Carlstrom51c24672013-07-11 16:00:56 -070067 method->GetDexMethodIndex()));
Logan Chieneeb7edf2012-03-09 20:38:39 +080068
Ian Rogersd4c4d952014-10-16 20:31:53 -070069 if (compiled_method == nullptr) {
David Sehr709b0702016-10-13 09:12:37 -070070 EXPECT_TRUE(oat_method.GetQuickCode() == nullptr) << method->PrettyMethod() << " "
Ian Rogersd4c4d952014-10-16 20:31:53 -070071 << oat_method.GetQuickCode();
Ian Rogersef7d42f2014-01-06 12:55:46 -080072 EXPECT_EQ(oat_method.GetFrameSizeInBytes(), 0U);
Logan Chieneeb7edf2012-03-09 20:38:39 +080073 EXPECT_EQ(oat_method.GetCoreSpillMask(), 0U);
74 EXPECT_EQ(oat_method.GetFpSpillMask(), 0U);
75 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -080076 const void* quick_oat_code = oat_method.GetQuickCode();
David Sehr709b0702016-10-13 09:12:37 -070077 EXPECT_TRUE(quick_oat_code != nullptr) << method->PrettyMethod();
Elliott Hughes956af0f2014-12-11 14:34:28 -080078 EXPECT_EQ(oat_method.GetFrameSizeInBytes(), compiled_method->GetFrameSizeInBytes());
79 EXPECT_EQ(oat_method.GetCoreSpillMask(), compiled_method->GetCoreSpillMask());
80 EXPECT_EQ(oat_method.GetFpSpillMask(), compiled_method->GetFpSpillMask());
81 uintptr_t oat_code_aligned = RoundDown(reinterpret_cast<uintptr_t>(quick_oat_code), 2);
82 quick_oat_code = reinterpret_cast<const void*>(oat_code_aligned);
Vladimir Marko35831e82015-09-11 11:59:18 +010083 ArrayRef<const uint8_t> quick_code = compiled_method->GetQuickCode();
84 EXPECT_FALSE(quick_code.empty());
85 size_t code_size = quick_code.size() * sizeof(quick_code[0]);
Elliott Hughes956af0f2014-12-11 14:34:28 -080086 EXPECT_EQ(0, memcmp(quick_oat_code, &quick_code[0], code_size))
David Sehr709b0702016-10-13 09:12:37 -070087 << method->PrettyMethod() << " " << code_size;
Elliott Hughes956af0f2014-12-11 14:34:28 -080088 CHECK_EQ(0, memcmp(quick_oat_code, &quick_code[0], code_size));
Logan Chieneeb7edf2012-03-09 20:38:39 +080089 }
90 }
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -080091
92 void SetupCompiler(Compiler::Kind compiler_kind,
93 InstructionSet insn_set,
94 const std::vector<std::string>& compiler_options,
95 /*out*/std::string* error_msg) {
96 ASSERT_TRUE(error_msg != nullptr);
Andreas Gampe0415b4e2015-01-06 15:17:07 -080097 insn_features_ = InstructionSetFeatures::FromVariant(insn_set, "default", error_msg);
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -080098 ASSERT_TRUE(insn_features_ != nullptr) << error_msg;
99 compiler_options_.reset(new CompilerOptions);
100 for (const std::string& option : compiler_options) {
101 compiler_options_->ParseCompilerOption(option, Usage);
102 }
103 verification_results_.reset(new VerificationResults(compiler_options_.get()));
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800104 callbacks_.reset(new QuickCompilerCallbacks(verification_results_.get(),
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800105 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(),
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800110 compiler_kind,
111 insn_set,
112 insn_features_.get(),
Vladimir Marko944da602016-02-19 12:27:55 +0000113 /* image_classes */ nullptr,
114 /* compiled_classes */ nullptr,
115 /* compiled_methods */ nullptr,
116 /* thread_count */ 2,
117 /* dump_stats */ true,
118 /* dump_passes */ true,
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800119 timer_.get(),
Vladimir Marko944da602016-02-19 12:27:55 +0000120 /* swap_fd */ -1,
121 /* profile_compilation_info */ nullptr));
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800122 }
123
David Brazdil7b49e6c2016-09-01 11:06:18 +0100124 bool WriteElf(File* vdex_file,
125 File* oat_file,
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800126 const std::vector<const DexFile*>& dex_files,
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800127 SafeMap<std::string, std::string>& key_value_store,
128 bool verify) {
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800129 TimingLogger timings("WriteElf", false, false);
Jeff Hao608f2ce2016-10-19 11:17:11 -0700130 OatWriter oat_writer(/*compiling_boot_image*/false,
131 &timings,
132 /*profile_compilation_info*/nullptr);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000133 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 }
David Brazdil7b49e6c2016-09-01 11:06:18 +0100143 return DoWriteElf(vdex_file, oat_file, oat_writer, key_value_store, verify);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000144 }
145
David Brazdil7b49e6c2016-09-01 11:06:18 +0100146 bool WriteElf(File* vdex_file,
147 File* oat_file,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000148 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);
Jeff Hao608f2ce2016-10-19 11:17:11 -0700152 OatWriter oat_writer(/*compiling_boot_image*/false,
153 &timings,
154 /*profile_compilation_info*/nullptr);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000155 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);
Jeff Hao608f2ce2016-10-19 11:17:11 -0700170 OatWriter oat_writer(/*compiling_boot_image*/false,
171 &timings,
172 /*profile_compilation_info*/nullptr);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000173 if (!oat_writer.AddZippedDexFilesSource(std::move(zip_fd), location)) {
174 return false;
175 }
David Brazdil7b49e6c2016-09-01 11:06:18 +0100176 return DoWriteElf(vdex_file, oat_file, oat_writer, key_value_store, verify);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000177 }
178
David Brazdil7b49e6c2016-09-01 11:06:18 +0100179 bool DoWriteElf(File* vdex_file,
180 File* oat_file,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000181 OatWriter& oat_writer,
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800182 SafeMap<std::string, std::string>& key_value_store,
183 bool verify) {
Vladimir Marko10c13562015-11-25 14:33:36 +0000184 std::unique_ptr<ElfWriter> elf_writer = CreateElfWriterQuick(
185 compiler_driver_->GetInstructionSet(),
David Srbecky5d811202016-03-08 13:21:22 +0000186 compiler_driver_->GetInstructionSetFeatures(),
Vladimir Marko10c13562015-11-25 14:33:36 +0000187 &compiler_driver_->GetCompilerOptions(),
David Brazdil7b49e6c2016-09-01 11:06:18 +0100188 oat_file);
Vladimir Marko10c13562015-11-25 14:33:36 +0000189 elf_writer->Start();
David Brazdil7b49e6c2016-09-01 11:06:18 +0100190 OutputStream* oat_rodata = elf_writer->StartRoData();
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000191 std::unique_ptr<MemMap> opened_dex_files_map;
192 std::vector<std::unique_ptr<const DexFile>> opened_dex_files;
David Brazdil7b49e6c2016-09-01 11:06:18 +0100193 if (!oat_writer.WriteAndOpenDexFiles(kIsVdexEnabled ? vdex_file : oat_file,
194 oat_rodata,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000195 compiler_driver_->GetInstructionSet(),
196 compiler_driver_->GetInstructionSetFeatures(),
197 &key_value_store,
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800198 verify,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000199 &opened_dex_files_map,
200 &opened_dex_files)) {
201 return false;
202 }
Vladimir Markoaad75c62016-10-03 08:46:48 +0000203
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000204 Runtime* runtime = Runtime::Current();
205 ClassLinker* const class_linker = runtime->GetClassLinker();
206 std::vector<const DexFile*> dex_files;
207 for (const std::unique_ptr<const DexFile>& dex_file : opened_dex_files) {
208 dex_files.push_back(dex_file.get());
209 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierf284d442016-06-02 11:48:30 -0700210 class_linker->RegisterDexFile(*dex_file, nullptr);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000211 }
Vladimir Marko944da602016-02-19 12:27:55 +0000212 linker::MultiOatRelativePatcher patcher(compiler_driver_->GetInstructionSet(),
213 instruction_set_features_.get());
Nicolas Geoffray4acefd32016-10-24 13:14:58 +0100214 oat_writer.Initialize(compiler_driver_.get(), nullptr, dex_files);
215 oat_writer.PrepareLayout(&patcher);
Vladimir Marko944da602016-02-19 12:27:55 +0000216 size_t rodata_size = oat_writer.GetOatHeader().GetExecutableOffset();
David Brazdil7b49e6c2016-09-01 11:06:18 +0100217 size_t text_size = oat_writer.GetOatSize() - rodata_size;
Vladimir Markoaad75c62016-10-03 08:46:48 +0000218 elf_writer->PrepareDynamicSection(rodata_size,
219 text_size,
220 oat_writer.GetBssSize(),
221 oat_writer.GetBssRootsOffset());
Vladimir Marko944da602016-02-19 12:27:55 +0000222
Nicolas Geoffrayf54e5df2016-12-01 10:45:08 +0000223 if (kIsVdexEnabled) {
224 std::unique_ptr<BufferedOutputStream> vdex_out(
225 MakeUnique<BufferedOutputStream>(MakeUnique<FileOutputStream>(vdex_file)));
226 if (!oat_writer.WriteVerifierDeps(vdex_out.get(), nullptr)) {
227 return false;
228 }
229 if (!oat_writer.WriteChecksumsAndVdexHeader(vdex_out.get())) {
230 return false;
231 }
232 }
233
David Brazdil7b49e6c2016-09-01 11:06:18 +0100234 if (!oat_writer.WriteRodata(oat_rodata)) {
Vladimir Marko10c13562015-11-25 14:33:36 +0000235 return false;
236 }
David Brazdil7b49e6c2016-09-01 11:06:18 +0100237 elf_writer->EndRoData(oat_rodata);
Vladimir Marko10c13562015-11-25 14:33:36 +0000238
239 OutputStream* text = elf_writer->StartText();
240 if (!oat_writer.WriteCode(text)) {
241 return false;
242 }
243 elf_writer->EndText(text);
244
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000245 if (!oat_writer.WriteHeader(elf_writer->GetStream(), 42U, 4096U, 0)) {
246 return false;
247 }
248
Vladimir Marko10c13562015-11-25 14:33:36 +0000249 elf_writer->WriteDynamicSection();
Vladimir Marko49b0f452015-12-10 13:49:19 +0000250 elf_writer->WriteDebugInfo(oat_writer.GetMethodDebugInfo());
251 elf_writer->WritePatchLocations(oat_writer.GetAbsolutePatchLocations());
Vladimir Marko10c13562015-11-25 14:33:36 +0000252
Vladimir Markoaad75c62016-10-03 08:46:48 +0000253 if (!elf_writer->End()) {
254 return false;
255 }
256
257 opened_dex_files_maps_.emplace_back(std::move(opened_dex_files_map));
258 for (std::unique_ptr<const DexFile>& dex_file : opened_dex_files) {
259 opened_dex_files_.emplace_back(dex_file.release());
260 }
261 return true;
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800262 }
263
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800264 void TestDexFileInput(bool verify, bool low_4gb);
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800265 void TestZipFileInput(bool verify);
266
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800267 std::unique_ptr<const InstructionSetFeatures> insn_features_;
268 std::unique_ptr<QuickCompilerCallbacks> callbacks_;
Vladimir Markoaad75c62016-10-03 08:46:48 +0000269
270 std::vector<std::unique_ptr<MemMap>> opened_dex_files_maps_;
271 std::vector<std::unique_ptr<const DexFile>> opened_dex_files_;
Logan Chieneeb7edf2012-03-09 20:38:39 +0800272};
Brian Carlstrome24fa612011-09-29 00:53:55 -0700273
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000274class ZipBuilder {
275 public:
276 explicit ZipBuilder(File* zip_file) : zip_file_(zip_file) { }
277
278 bool AddFile(const char* location, const void* data, size_t size) {
279 off_t offset = lseek(zip_file_->Fd(), 0, SEEK_CUR);
280 if (offset == static_cast<off_t>(-1)) {
281 return false;
282 }
283
284 ZipFileHeader file_header;
285 file_header.crc32 = crc32(0u, reinterpret_cast<const Bytef*>(data), size);
286 file_header.compressed_size = size;
287 file_header.uncompressed_size = size;
288 file_header.filename_length = strlen(location);
289
290 if (!zip_file_->WriteFully(&file_header, sizeof(file_header)) ||
291 !zip_file_->WriteFully(location, file_header.filename_length) ||
292 !zip_file_->WriteFully(data, size)) {
293 return false;
294 }
295
296 CentralDirectoryFileHeader cdfh;
297 cdfh.crc32 = file_header.crc32;
298 cdfh.compressed_size = size;
299 cdfh.uncompressed_size = size;
300 cdfh.filename_length = file_header.filename_length;
301 cdfh.relative_offset_of_local_file_header = offset;
302 file_data_.push_back(FileData { cdfh, location });
303 return true;
304 }
305
306 bool Finish() {
307 off_t offset = lseek(zip_file_->Fd(), 0, SEEK_CUR);
308 if (offset == static_cast<off_t>(-1)) {
309 return false;
310 }
311
312 size_t central_directory_size = 0u;
313 for (const FileData& file_data : file_data_) {
314 if (!zip_file_->WriteFully(&file_data.cdfh, sizeof(file_data.cdfh)) ||
315 !zip_file_->WriteFully(file_data.location, file_data.cdfh.filename_length)) {
316 return false;
317 }
318 central_directory_size += sizeof(file_data.cdfh) + file_data.cdfh.filename_length;
319 }
320 EndOfCentralDirectoryRecord eocd_record;
321 eocd_record.number_of_central_directory_records_on_this_disk = file_data_.size();
322 eocd_record.total_number_of_central_directory_records = file_data_.size();
323 eocd_record.size_of_central_directory = central_directory_size;
324 eocd_record.offset_of_start_of_central_directory = offset;
325 return
326 zip_file_->WriteFully(&eocd_record, sizeof(eocd_record)) &&
327 zip_file_->Flush() == 0;
328 }
329
330 private:
331 struct PACKED(1) ZipFileHeader {
332 uint32_t signature = 0x04034b50;
333 uint16_t version_needed_to_extract = 10;
334 uint16_t general_purpose_bit_flag = 0;
335 uint16_t compression_method = 0; // 0 = store only.
336 uint16_t file_last_modification_time = 0u;
337 uint16_t file_last_modification_date = 0u;
338 uint32_t crc32;
339 uint32_t compressed_size;
340 uint32_t uncompressed_size;
341 uint16_t filename_length;
342 uint16_t extra_field_length = 0u; // No extra fields.
343 };
344
345 struct PACKED(1) CentralDirectoryFileHeader {
346 uint32_t signature = 0x02014b50;
347 uint16_t version_made_by = 10;
348 uint16_t version_needed_to_extract = 10;
349 uint16_t general_purpose_bit_flag = 0;
350 uint16_t compression_method = 0; // 0 = store only.
351 uint16_t file_last_modification_time = 0u;
352 uint16_t file_last_modification_date = 0u;
353 uint32_t crc32;
354 uint32_t compressed_size;
355 uint32_t uncompressed_size;
356 uint16_t filename_length;
357 uint16_t extra_field_length = 0u; // No extra fields.
358 uint16_t file_comment_length = 0u; // No file comment.
359 uint16_t disk_number_where_file_starts = 0u;
360 uint16_t internal_file_attributes = 0u;
361 uint32_t external_file_attributes = 0u;
362 uint32_t relative_offset_of_local_file_header;
363 };
364
365 struct PACKED(1) EndOfCentralDirectoryRecord {
366 uint32_t signature = 0x06054b50;
367 uint16_t number_of_this_disk = 0u;
368 uint16_t disk_where_central_directory_starts = 0u;
369 uint16_t number_of_central_directory_records_on_this_disk;
370 uint16_t total_number_of_central_directory_records;
371 uint32_t size_of_central_directory;
372 uint32_t offset_of_start_of_central_directory;
373 uint16_t comment_length = 0u; // No file comment.
374 };
375
376 struct FileData {
377 CentralDirectoryFileHeader cdfh;
378 const char* location;
379 };
380
381 File* zip_file_;
382 std::vector<FileData> file_data_;
383};
384
Brian Carlstrome24fa612011-09-29 00:53:55 -0700385TEST_F(OatTest, WriteRead) {
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800386 TimingLogger timings("OatTest::WriteRead", false, false);
Jesse Wilson254db0f2011-11-16 16:44:11 -0500387 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700388
Ian Rogersef7d42f2014-01-06 12:55:46 -0800389 // TODO: make selectable.
Elliott Hughes956af0f2014-12-11 14:34:28 -0800390 Compiler::Kind compiler_kind = Compiler::kQuick;
Ian Rogersa073c672013-07-30 17:43:55 -0700391 InstructionSet insn_set = kIsTargetBuild ? kThumb2 : kX86;
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700392 std::string error_msg;
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800393 SetupCompiler(compiler_kind, insn_set, std::vector<std::string>(), /*out*/ &error_msg);
394
Ian Rogersd4c4d952014-10-16 20:31:53 -0700395 jobject class_loader = nullptr;
Brian Carlstromba150c32013-08-27 17:31:03 -0700396 if (kCompile) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800397 TimingLogger timings2("OatTest::WriteRead", false, false);
Vladimir Markod1eaf0d2015-10-29 12:18:29 +0000398 compiler_driver_->SetDexFilesForOatFile(class_linker->GetBootClassPath());
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +0000399 compiler_driver_->CompileAll(
400 class_loader, class_linker->GetBootClassPath(), /* verifier_deps */ nullptr, &timings2);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700401 }
402
David Brazdil7b49e6c2016-09-01 11:06:18 +0100403 ScratchFile tmp_oat, tmp_vdex(tmp_oat, ".vdex");
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700404 SafeMap<std::string, std::string> key_value_store;
405 key_value_store.Put(OatHeader::kImageLocationKey, "lue.art");
David Brazdil7b49e6c2016-09-01 11:06:18 +0100406 bool success = WriteElf(tmp_vdex.GetFile(),
407 tmp_oat.GetFile(),
408 class_linker->GetBootClassPath(),
409 key_value_store,
410 false);
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700411 ASSERT_TRUE(success);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700412
Brian Carlstromba150c32013-08-27 17:31:03 -0700413 if (kCompile) { // OatWriter strips the code, regenerate to compare
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +0000414 compiler_driver_->CompileAll(
415 class_loader, class_linker->GetBootClassPath(), /* verifier_deps */ nullptr, &timings);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700416 }
David Brazdil7b49e6c2016-09-01 11:06:18 +0100417 std::unique_ptr<OatFile> oat_file(OatFile::Open(tmp_oat.GetFilename(),
418 tmp_oat.GetFilename(),
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800419 nullptr,
420 nullptr,
421 false,
422 /*low_4gb*/true,
423 nullptr,
424 &error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700425 ASSERT_TRUE(oat_file.get() != nullptr) << error_msg;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700426 const OatHeader& oat_header = oat_file->GetOatHeader();
Brian Carlstromf852fb22012-10-19 11:01:58 -0700427 ASSERT_TRUE(oat_header.IsValid());
Yi Kong9515b512015-11-11 14:36:07 +0000428 ASSERT_EQ(class_linker->GetBootClassPath().size(), oat_header.GetDexFileCount()); // core
Brian Carlstrom28db0122012-10-18 16:20:41 -0700429 ASSERT_EQ(42U, oat_header.GetImageFileLocationOatChecksum());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800430 ASSERT_EQ(4096U, oat_header.GetImageFileLocationOatDataBegin());
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700431 ASSERT_EQ("lue.art", std::string(oat_header.GetStoreValueByKey(OatHeader::kImageLocationKey)));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700432
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800433 ASSERT_TRUE(java_lang_dex_file_ != nullptr);
434 const DexFile& dex_file = *java_lang_dex_file_;
435 uint32_t dex_file_checksum = dex_file.GetLocationChecksum();
436 const OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(dex_file.GetLocation().c_str(),
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700437 &dex_file_checksum);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700438 ASSERT_TRUE(oat_dex_file != nullptr);
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800439 CHECK_EQ(dex_file.GetLocationChecksum(), oat_dex_file->GetDexFileLocationChecksum());
Vladimir Markof4da6752014-08-01 19:04:18 +0100440 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700441 auto pointer_size = class_linker->GetImagePointerSize();
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800442 for (size_t i = 0; i < dex_file.NumClassDefs(); i++) {
443 const DexFile::ClassDef& class_def = dex_file.GetClassDef(i);
444 const uint8_t* class_data = dex_file.GetClassData(class_def);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700445
Brian Carlstromba150c32013-08-27 17:31:03 -0700446 size_t num_virtual_methods = 0;
Ian Rogersd4c4d952014-10-16 20:31:53 -0700447 if (class_data != nullptr) {
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800448 ClassDataItemIterator it(dex_file, class_data);
Ian Rogers0571d352011-11-03 19:51:38 -0700449 num_virtual_methods = it.NumVirtualMethods();
450 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700451
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800452 const char* descriptor = dex_file.GetClassDescriptor(class_def);
Mathieu Chartier9865bde2015-12-21 09:58:16 -0800453 mirror::Class* klass = class_linker->FindClass(soa.Self(),
454 descriptor,
455 ScopedNullHandle<mirror::ClassLoader>());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700456
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100457 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(i);
458 CHECK_EQ(mirror::Class::Status::kStatusNotReady, oat_class.GetStatus()) << descriptor;
Brian Carlstromba150c32013-08-27 17:31:03 -0700459 CHECK_EQ(kCompile ? OatClassType::kOatClassAllCompiled : OatClassType::kOatClassNoneCompiled,
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100460 oat_class.GetType()) << descriptor;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700461
462 size_t method_index = 0;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700463 for (auto& m : klass->GetDirectMethods(pointer_size)) {
464 CheckMethod(&m, oat_class.GetOatMethod(method_index), dex_file);
465 ++method_index;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700466 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700467 size_t visited_virtuals = 0;
Alex Lighte64300b2015-12-15 15:02:47 -0800468 // TODO We should also check copied methods in this test.
469 for (auto& m : klass->GetDeclaredVirtualMethods(pointer_size)) {
Alex Lightfcea56f2016-02-17 11:59:05 -0800470 if (!klass->IsInterface()) {
Alex Light36121492016-02-22 13:43:29 -0800471 EXPECT_FALSE(m.IsCopied());
Alex Lightfcea56f2016-02-17 11:59:05 -0800472 }
Alex Lighte64300b2015-12-15 15:02:47 -0800473 CheckMethod(&m, oat_class.GetOatMethod(method_index), dex_file);
474 ++method_index;
475 ++visited_virtuals;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700476 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700477 EXPECT_EQ(visited_virtuals, num_virtual_methods);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700478 }
479}
480
Brian Carlstrom341df942012-06-27 12:29:22 -0700481TEST_F(OatTest, OatHeaderSizeCheck) {
482 // If this test is failing and you have to update these constants,
483 // it is time to update OatHeader::kOatVersion
Elliott Hughes956af0f2014-12-11 14:34:28 -0800484 EXPECT_EQ(72U, sizeof(OatHeader));
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800485 EXPECT_EQ(4U, sizeof(OatMethodOffsets));
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100486 EXPECT_EQ(20U, sizeof(OatQuickMethodHeader));
Igor Murashkinaf1e2992016-10-12 17:44:50 -0700487 EXPECT_EQ(164 * static_cast<size_t>(GetInstructionSetPointerSize(kRuntimeISA)),
Andreas Gampe542451c2016-07-26 09:02:02 -0700488 sizeof(QuickEntryPoints));
Brian Carlstrom341df942012-06-27 12:29:22 -0700489}
490
Brian Carlstromf852fb22012-10-19 11:01:58 -0700491TEST_F(OatTest, OatHeaderIsValid) {
Jeff Hao5d3baf62016-05-23 19:17:04 -0700492 InstructionSet insn_set = kX86;
493 std::string error_msg;
494 std::unique_ptr<const InstructionSetFeatures> insn_features(
495 InstructionSetFeatures::FromVariant(insn_set, "default", &error_msg));
496 ASSERT_TRUE(insn_features.get() != nullptr) << error_msg;
497 std::unique_ptr<OatHeader> oat_header(OatHeader::Create(insn_set,
498 insn_features.get(),
499 0u,
500 nullptr));
501 ASSERT_NE(oat_header.get(), nullptr);
502 ASSERT_TRUE(oat_header->IsValid());
Brian Carlstromf852fb22012-10-19 11:01:58 -0700503
Jeff Hao5d3baf62016-05-23 19:17:04 -0700504 char* magic = const_cast<char*>(oat_header->GetMagic());
505 strcpy(magic, ""); // bad magic
506 ASSERT_FALSE(oat_header->IsValid());
507 strcpy(magic, "oat\n000"); // bad version
508 ASSERT_FALSE(oat_header->IsValid());
Brian Carlstromf852fb22012-10-19 11:01:58 -0700509}
510
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800511TEST_F(OatTest, EmptyTextSection) {
512 TimingLogger timings("OatTest::EmptyTextSection", false, false);
513
514 // TODO: make selectable.
515 Compiler::Kind compiler_kind = Compiler::kQuick;
516 InstructionSet insn_set = kRuntimeISA;
517 if (insn_set == kArm) insn_set = kThumb2;
518 std::string error_msg;
519 std::vector<std::string> compiler_options;
520 compiler_options.push_back("--compiler-filter=verify-at-runtime");
521 SetupCompiler(compiler_kind, insn_set, compiler_options, /*out*/ &error_msg);
522
523 jobject class_loader;
524 {
525 ScopedObjectAccess soa(Thread::Current());
526 class_loader = LoadDex("Main");
527 }
528 ASSERT_TRUE(class_loader != nullptr);
529 std::vector<const DexFile*> dex_files = GetDexFiles(class_loader);
530 ASSERT_TRUE(!dex_files.empty());
531
532 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
533 for (const DexFile* dex_file : dex_files) {
534 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartier0795f232016-09-27 18:43:30 -0700535 class_linker->RegisterDexFile(*dex_file,
Mathieu Chartier1cc62e42016-10-03 18:01:28 -0700536 soa.Decode<mirror::ClassLoader>(class_loader).Ptr());
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800537 }
538 compiler_driver_->SetDexFilesForOatFile(dex_files);
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +0000539 compiler_driver_->CompileAll(class_loader, dex_files, /* verifier_deps */ nullptr, &timings);
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800540
David Brazdil7b49e6c2016-09-01 11:06:18 +0100541 ScratchFile tmp_oat, tmp_vdex(tmp_oat, ".vdex");
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800542 SafeMap<std::string, std::string> key_value_store;
543 key_value_store.Put(OatHeader::kImageLocationKey, "test.art");
David Brazdil7b49e6c2016-09-01 11:06:18 +0100544 bool success = WriteElf(tmp_vdex.GetFile(), tmp_oat.GetFile(), dex_files, key_value_store, false);
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800545 ASSERT_TRUE(success);
546
David Brazdil7b49e6c2016-09-01 11:06:18 +0100547 std::unique_ptr<OatFile> oat_file(OatFile::Open(tmp_oat.GetFilename(),
548 tmp_oat.GetFilename(),
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800549 nullptr,
550 nullptr,
551 false,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800552 /*low_4gb*/false,
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800553 nullptr,
554 &error_msg));
555 ASSERT_TRUE(oat_file != nullptr);
David Brazdil7b49e6c2016-09-01 11:06:18 +0100556 EXPECT_LT(static_cast<size_t>(oat_file->Size()),
557 static_cast<size_t>(tmp_oat.GetFile()->GetLength()));
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800558}
559
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800560static void MaybeModifyDexFileToFail(bool verify, std::unique_ptr<const DexFile>& data) {
561 // If in verify mode (= fail the verifier mode), make sure we fail early. We'll fail already
562 // because of the missing map, but that may lead to out of bounds reads.
563 if (verify) {
564 const_cast<DexFile::Header*>(&data->GetHeader())->checksum_++;
565 }
566}
567
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800568void OatTest::TestDexFileInput(bool verify, bool low_4gb) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000569 TimingLogger timings("OatTest::DexFileInput", false, false);
570
571 std::vector<const char*> input_filenames;
572
573 ScratchFile dex_file1;
574 TestDexFileBuilder builder1;
575 builder1.AddField("Lsome.TestClass;", "int", "someField");
576 builder1.AddMethod("Lsome.TestClass;", "()I", "foo");
577 std::unique_ptr<const DexFile> dex_file1_data = builder1.Build(dex_file1.GetFilename());
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800578
579 MaybeModifyDexFileToFail(verify, dex_file1_data);
580
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000581 bool success = dex_file1.GetFile()->WriteFully(&dex_file1_data->GetHeader(),
582 dex_file1_data->GetHeader().file_size_);
583 ASSERT_TRUE(success);
584 success = dex_file1.GetFile()->Flush() == 0;
585 ASSERT_TRUE(success);
586 input_filenames.push_back(dex_file1.GetFilename().c_str());
587
588 ScratchFile dex_file2;
589 TestDexFileBuilder builder2;
590 builder2.AddField("Land.AnotherTestClass;", "boolean", "someOtherField");
591 builder2.AddMethod("Land.AnotherTestClass;", "()J", "bar");
592 std::unique_ptr<const DexFile> dex_file2_data = builder2.Build(dex_file2.GetFilename());
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800593
594 MaybeModifyDexFileToFail(verify, dex_file2_data);
595
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000596 success = dex_file2.GetFile()->WriteFully(&dex_file2_data->GetHeader(),
597 dex_file2_data->GetHeader().file_size_);
598 ASSERT_TRUE(success);
599 success = dex_file2.GetFile()->Flush() == 0;
600 ASSERT_TRUE(success);
601 input_filenames.push_back(dex_file2.GetFilename().c_str());
602
David Brazdil7b49e6c2016-09-01 11:06:18 +0100603 ScratchFile oat_file, vdex_file(oat_file, ".vdex");
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000604 SafeMap<std::string, std::string> key_value_store;
605 key_value_store.Put(OatHeader::kImageLocationKey, "test.art");
David Brazdil7b49e6c2016-09-01 11:06:18 +0100606 success = WriteElf(vdex_file.GetFile(),
607 oat_file.GetFile(),
608 input_filenames,
609 key_value_store,
610 verify);
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800611
612 // In verify mode, we expect failure.
613 if (verify) {
614 ASSERT_FALSE(success);
615 return;
616 }
617
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000618 ASSERT_TRUE(success);
619
620 std::string error_msg;
621 std::unique_ptr<OatFile> opened_oat_file(OatFile::Open(oat_file.GetFilename(),
622 oat_file.GetFilename(),
623 nullptr,
624 nullptr,
625 false,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800626 low_4gb,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000627 nullptr,
628 &error_msg));
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800629 if (low_4gb) {
630 uintptr_t begin = reinterpret_cast<uintptr_t>(opened_oat_file->Begin());
631 EXPECT_EQ(begin, static_cast<uint32_t>(begin));
632 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000633 ASSERT_TRUE(opened_oat_file != nullptr);
634 ASSERT_EQ(2u, opened_oat_file->GetOatDexFiles().size());
635 std::unique_ptr<const DexFile> opened_dex_file1 =
636 opened_oat_file->GetOatDexFiles()[0]->OpenDexFile(&error_msg);
637 std::unique_ptr<const DexFile> opened_dex_file2 =
638 opened_oat_file->GetOatDexFiles()[1]->OpenDexFile(&error_msg);
639
640 ASSERT_EQ(dex_file1_data->GetHeader().file_size_, opened_dex_file1->GetHeader().file_size_);
641 ASSERT_EQ(0, memcmp(&dex_file1_data->GetHeader(),
642 &opened_dex_file1->GetHeader(),
643 dex_file1_data->GetHeader().file_size_));
644 ASSERT_EQ(dex_file1_data->GetLocation(), opened_dex_file1->GetLocation());
645
646 ASSERT_EQ(dex_file2_data->GetHeader().file_size_, opened_dex_file2->GetHeader().file_size_);
647 ASSERT_EQ(0, memcmp(&dex_file2_data->GetHeader(),
648 &opened_dex_file2->GetHeader(),
649 dex_file2_data->GetHeader().file_size_));
650 ASSERT_EQ(dex_file2_data->GetLocation(), opened_dex_file2->GetLocation());
651}
652
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800653TEST_F(OatTest, DexFileInputCheckOutput) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800654 TestDexFileInput(false, /*low_4gb*/false);
655}
656
657TEST_F(OatTest, DexFileInputCheckOutputLow4GB) {
658 TestDexFileInput(false, /*low_4gb*/true);
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800659}
660
661TEST_F(OatTest, DexFileInputCheckVerifier) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800662 TestDexFileInput(true, /*low_4gb*/false);
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800663}
664
665void OatTest::TestZipFileInput(bool verify) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000666 TimingLogger timings("OatTest::DexFileInput", false, false);
667
668 ScratchFile zip_file;
669 ZipBuilder zip_builder(zip_file.GetFile());
670
671 ScratchFile dex_file1;
672 TestDexFileBuilder builder1;
673 builder1.AddField("Lsome.TestClass;", "long", "someField");
674 builder1.AddMethod("Lsome.TestClass;", "()D", "foo");
675 std::unique_ptr<const DexFile> dex_file1_data = builder1.Build(dex_file1.GetFilename());
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800676
677 MaybeModifyDexFileToFail(verify, dex_file1_data);
678
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000679 bool success = dex_file1.GetFile()->WriteFully(&dex_file1_data->GetHeader(),
680 dex_file1_data->GetHeader().file_size_);
681 ASSERT_TRUE(success);
682 success = dex_file1.GetFile()->Flush() == 0;
683 ASSERT_TRUE(success);
684 success = zip_builder.AddFile("classes.dex",
685 &dex_file1_data->GetHeader(),
686 dex_file1_data->GetHeader().file_size_);
687 ASSERT_TRUE(success);
688
689 ScratchFile dex_file2;
690 TestDexFileBuilder builder2;
691 builder2.AddField("Land.AnotherTestClass;", "boolean", "someOtherField");
692 builder2.AddMethod("Land.AnotherTestClass;", "()J", "bar");
693 std::unique_ptr<const DexFile> dex_file2_data = builder2.Build(dex_file2.GetFilename());
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800694
695 MaybeModifyDexFileToFail(verify, dex_file2_data);
696
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000697 success = dex_file2.GetFile()->WriteFully(&dex_file2_data->GetHeader(),
698 dex_file2_data->GetHeader().file_size_);
699 ASSERT_TRUE(success);
700 success = dex_file2.GetFile()->Flush() == 0;
701 ASSERT_TRUE(success);
702 success = zip_builder.AddFile("classes2.dex",
703 &dex_file2_data->GetHeader(),
704 dex_file2_data->GetHeader().file_size_);
705 ASSERT_TRUE(success);
706
707 success = zip_builder.Finish();
708 ASSERT_TRUE(success) << strerror(errno);
709
710 SafeMap<std::string, std::string> key_value_store;
711 key_value_store.Put(OatHeader::kImageLocationKey, "test.art");
712 {
713 // Test using the AddDexFileSource() interface with the zip file.
714 std::vector<const char*> input_filenames { zip_file.GetFilename().c_str() }; // NOLINT [readability/braces] [4]
715
David Brazdil7b49e6c2016-09-01 11:06:18 +0100716 ScratchFile oat_file, vdex_file(oat_file, ".vdex");
717 success = WriteElf(vdex_file.GetFile(), oat_file.GetFile(),
718 input_filenames, key_value_store, verify);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000719
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800720 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());
747
748 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 {
758 // Test using the AddZipDexFileSource() interface with the zip file handle.
Andreas Gampe43e10b02016-07-15 17:17:34 -0700759 File zip_fd(dup(zip_file.GetFd()), /* check_usage */ false);
760 ASSERT_NE(-1, zip_fd.Fd());
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000761
David Brazdil7b49e6c2016-09-01 11:06:18 +0100762 ScratchFile oat_file, vdex_file(oat_file, ".vdex");
763 success = WriteElf(vdex_file.GetFile(),
764 oat_file.GetFile(),
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000765 std::move(zip_fd),
766 zip_file.GetFilename().c_str(),
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800767 key_value_store,
768 verify);
769 if (verify) {
770 ASSERT_FALSE(success);
771 } else {
772 ASSERT_TRUE(success);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000773
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800774 std::string error_msg;
775 std::unique_ptr<OatFile> opened_oat_file(OatFile::Open(oat_file.GetFilename(),
776 oat_file.GetFilename(),
777 nullptr,
778 nullptr,
779 false,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800780 /*low_4gb*/false,
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800781 nullptr,
782 &error_msg));
783 ASSERT_TRUE(opened_oat_file != nullptr);
784 ASSERT_EQ(2u, opened_oat_file->GetOatDexFiles().size());
785 std::unique_ptr<const DexFile> opened_dex_file1 =
786 opened_oat_file->GetOatDexFiles()[0]->OpenDexFile(&error_msg);
787 std::unique_ptr<const DexFile> opened_dex_file2 =
788 opened_oat_file->GetOatDexFiles()[1]->OpenDexFile(&error_msg);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000789
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800790 ASSERT_EQ(dex_file1_data->GetHeader().file_size_, opened_dex_file1->GetHeader().file_size_);
791 ASSERT_EQ(0, memcmp(&dex_file1_data->GetHeader(),
792 &opened_dex_file1->GetHeader(),
793 dex_file1_data->GetHeader().file_size_));
794 ASSERT_EQ(DexFile::GetMultiDexLocation(0, zip_file.GetFilename().c_str()),
795 opened_dex_file1->GetLocation());
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000796
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800797 ASSERT_EQ(dex_file2_data->GetHeader().file_size_, opened_dex_file2->GetHeader().file_size_);
798 ASSERT_EQ(0, memcmp(&dex_file2_data->GetHeader(),
799 &opened_dex_file2->GetHeader(),
800 dex_file2_data->GetHeader().file_size_));
801 ASSERT_EQ(DexFile::GetMultiDexLocation(1, zip_file.GetFilename().c_str()),
802 opened_dex_file2->GetLocation());
803 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000804 }
805}
806
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800807TEST_F(OatTest, ZipFileInputCheckOutput) {
808 TestZipFileInput(false);
809}
810
811TEST_F(OatTest, ZipFileInputCheckVerifier) {
812 TestZipFileInput(true);
813}
814
Jeff Hao5d3baf62016-05-23 19:17:04 -0700815TEST_F(OatTest, UpdateChecksum) {
816 InstructionSet insn_set = kX86;
817 std::string error_msg;
818 std::unique_ptr<const InstructionSetFeatures> insn_features(
819 InstructionSetFeatures::FromVariant(insn_set, "default", &error_msg));
820 ASSERT_TRUE(insn_features.get() != nullptr) << error_msg;
821 std::unique_ptr<OatHeader> oat_header(OatHeader::Create(insn_set,
822 insn_features.get(),
823 0u,
824 nullptr));
825 // The starting adler32 value is 1.
826 EXPECT_EQ(1U, oat_header->GetChecksum());
827
828 oat_header->UpdateChecksum(OatHeader::kOatMagic, sizeof(OatHeader::kOatMagic));
829 EXPECT_EQ(64291151U, oat_header->GetChecksum());
830
831 // Make sure that null data does not reset the checksum.
832 oat_header->UpdateChecksum(nullptr, 0);
833 EXPECT_EQ(64291151U, oat_header->GetChecksum());
834
835 oat_header->UpdateChecksum(OatHeader::kOatMagic, sizeof(OatHeader::kOatMagic));
836 EXPECT_EQ(216138397U, oat_header->GetChecksum());
837}
838
Brian Carlstrome24fa612011-09-29 00:53:55 -0700839} // namespace art