1 /*
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 */
16
17 #include "android-base/stringprintf.h"
18
19 #include "arch/instruction_set_features.h"
20 #include "art_method-inl.h"
21 #include "base/file_utils.h"
22 #include "base/pointer_size.h"
23 #include "base/stl_util.h"
24 #include "base/unix_file/fd_file.h"
25 #include "class_linker.h"
26 #include "common_compiler_driver_test.h"
27 #include "compiler.h"
28 #include "debug/method_debug_info.h"
29 #include "dex/class_accessor-inl.h"
30 #include "dex/dex_file_loader.h"
31 #include "dex/quick_compiler_callbacks.h"
32 #include "dex/test_dex_file_builder.h"
33 #include "dex/verification_results.h"
34 #include "driver/compiled_method-inl.h"
35 #include "driver/compiler_driver.h"
36 #include "driver/compiler_options.h"
37 #include "entrypoints/quick/quick_entrypoints.h"
38 #include "linker/elf_writer.h"
39 #include "linker/elf_writer_quick.h"
40 #include "linker/multi_oat_relative_patcher.h"
41 #include "mirror/class-inl.h"
42 #include "mirror/object-inl.h"
43 #include "mirror/object_array-inl.h"
44 #include "oat/oat.h"
45 #include "oat/oat_file-inl.h"
46 #include "oat_writer.h"
47 #include "profile/profile_compilation_info.h"
48 #include "scoped_thread_state_change-inl.h"
49 #include "stream/buffered_output_stream.h"
50 #include "stream/file_output_stream.h"
51 #include "stream/vector_output_stream.h"
52 #include "vdex_file.h"
53
54 namespace art {
55 namespace linker {
56
57 class OatTest : public CommonCompilerDriverTest {
58 protected:
59 static const bool kCompile = false; // DISABLED_ due to the time to compile libcore
60
CheckMethod(ArtMethod * method,const OatFile::OatMethod & oat_method,const DexFile & dex_file)61 void CheckMethod(ArtMethod* method,
62 const OatFile::OatMethod& oat_method,
63 const DexFile& dex_file)
64 REQUIRES_SHARED(Locks::mutator_lock_) {
65 const CompiledMethod* compiled_method =
66 compiler_driver_->GetCompiledMethod(MethodReference(&dex_file,
67 method->GetDexMethodIndex()));
68
69 if (compiled_method == nullptr) {
70 EXPECT_TRUE(oat_method.GetQuickCode() == nullptr) << method->PrettyMethod() << " "
71 << oat_method.GetQuickCode();
72 EXPECT_EQ(oat_method.GetFrameSizeInBytes(), 0U);
73 EXPECT_EQ(oat_method.GetCoreSpillMask(), 0U);
74 EXPECT_EQ(oat_method.GetFpSpillMask(), 0U);
75 } else {
76 const void* quick_oat_code = oat_method.GetQuickCode();
77 EXPECT_TRUE(quick_oat_code != nullptr) << method->PrettyMethod();
78 uintptr_t oat_code_aligned = RoundDown(reinterpret_cast<uintptr_t>(quick_oat_code), 2);
79 quick_oat_code = reinterpret_cast<const void*>(oat_code_aligned);
80 ArrayRef<const uint8_t> quick_code = compiled_method->GetQuickCode();
81 EXPECT_FALSE(quick_code.empty());
82 size_t code_size = quick_code.size() * sizeof(quick_code[0]);
83 EXPECT_EQ(0, memcmp(quick_oat_code, &quick_code[0], code_size))
84 << method->PrettyMethod() << " " << code_size;
85 CHECK_EQ(0, memcmp(quick_oat_code, &quick_code[0], code_size));
86 }
87 }
88
SetupCompiler(const std::vector<std::string> & compiler_options)89 void SetupCompiler(const std::vector<std::string>& compiler_options) {
90 std::string error_msg;
91 if (!compiler_options_->ParseCompilerOptions(compiler_options,
92 /*ignore_unrecognized=*/ false,
93 &error_msg)) {
94 LOG(FATAL) << error_msg;
95 UNREACHABLE();
96 }
97 callbacks_.reset(new QuickCompilerCallbacks(CompilerCallbacks::CallbackMode::kCompileApp));
98 callbacks_->SetVerificationResults(verification_results_.get());
99 Runtime::Current()->SetCompilerCallbacks(callbacks_.get());
100 }
101
WriteElf(File * vdex_file,File * oat_file,const std::vector<const DexFile * > & dex_files,SafeMap<std::string,std::string> & key_value_store,bool verify)102 bool WriteElf(File* vdex_file,
103 File* oat_file,
104 const std::vector<const DexFile*>& dex_files,
105 SafeMap<std::string, std::string>& key_value_store,
106 bool verify) {
107 TimingLogger timings("WriteElf", false, false);
108 ClearBootImageOption();
109 OatWriter oat_writer(*compiler_options_,
110 verification_results_.get(),
111 &timings,
112 /*profile_compilation_info*/nullptr);
113 for (const DexFile* dex_file : dex_files) {
114 if (!oat_writer.AddRawDexFileSource(dex_file->GetContainer(),
115 dex_file->Begin(),
116 dex_file->GetLocation().c_str(),
117 dex_file->GetLocationChecksum())) {
118 return false;
119 }
120 }
121 return DoWriteElf(
122 vdex_file, oat_file, oat_writer, key_value_store, verify, CopyOption::kOnlyIfCompressed);
123 }
124
WriteElf(File * vdex_file,File * oat_file,const std::vector<const char * > & dex_filenames,SafeMap<std::string,std::string> & key_value_store,bool verify,CopyOption copy,ProfileCompilationInfo * profile_compilation_info)125 bool WriteElf(File* vdex_file,
126 File* oat_file,
127 const std::vector<const char*>& dex_filenames,
128 SafeMap<std::string, std::string>& key_value_store,
129 bool verify,
130 CopyOption copy,
131 ProfileCompilationInfo* profile_compilation_info) {
132 TimingLogger timings("WriteElf", false, false);
133 ClearBootImageOption();
134 OatWriter oat_writer(*compiler_options_,
135 verification_results_.get(),
136 &timings,
137 profile_compilation_info);
138 for (const char* dex_filename : dex_filenames) {
139 if (!oat_writer.AddDexFileSource(dex_filename, dex_filename)) {
140 return false;
141 }
142 }
143 return DoWriteElf(vdex_file, oat_file, oat_writer, key_value_store, verify, copy);
144 }
145
WriteElf(File * vdex_file,File * oat_file,File && dex_file_fd,const char * location,SafeMap<std::string,std::string> & key_value_store,bool verify,CopyOption copy,ProfileCompilationInfo * profile_compilation_info=nullptr)146 bool WriteElf(File* vdex_file,
147 File* oat_file,
148 File&& dex_file_fd,
149 const char* location,
150 SafeMap<std::string, std::string>& key_value_store,
151 bool verify,
152 CopyOption copy,
153 ProfileCompilationInfo* profile_compilation_info = nullptr) {
154 TimingLogger timings("WriteElf", false, false);
155 ClearBootImageOption();
156 OatWriter oat_writer(*compiler_options_,
157 verification_results_.get(),
158 &timings,
159 profile_compilation_info);
160 if (!oat_writer.AddDexFileSource(std::move(dex_file_fd), location)) {
161 return false;
162 }
163 return DoWriteElf(vdex_file, oat_file, oat_writer, key_value_store, verify, copy);
164 }
165
DoWriteElf(File * vdex_file,File * oat_file,OatWriter & oat_writer,SafeMap<std::string,std::string> & key_value_store,bool verify,CopyOption copy)166 bool DoWriteElf(File* vdex_file,
167 File* oat_file,
168 OatWriter& oat_writer,
169 SafeMap<std::string, std::string>& key_value_store,
170 bool verify,
171 CopyOption copy) {
172 std::unique_ptr<ElfWriter> elf_writer = CreateElfWriterQuick(
173 compiler_driver_->GetCompilerOptions(),
174 oat_file);
175 elf_writer->Start();
176 OutputStream* oat_rodata = elf_writer->StartRoData();
177 std::vector<MemMap> opened_dex_files_maps;
178 std::vector<std::unique_ptr<const DexFile>> opened_dex_files;
179 if (!oat_writer.WriteAndOpenDexFiles(
180 vdex_file,
181 verify,
182 /*use_existing_vdex=*/ false,
183 copy,
184 &opened_dex_files_maps,
185 &opened_dex_files)) {
186 return false;
187 }
188
189 Runtime* runtime = Runtime::Current();
190 ClassLinker* const class_linker = runtime->GetClassLinker();
191 std::vector<const DexFile*> dex_files;
192 for (const std::unique_ptr<const DexFile>& dex_file : opened_dex_files) {
193 dex_files.push_back(dex_file.get());
194 ScopedObjectAccess soa(Thread::Current());
195 class_linker->RegisterDexFile(*dex_file, nullptr);
196 }
197 MultiOatRelativePatcher patcher(compiler_options_->GetInstructionSet(),
198 compiler_options_->GetInstructionSetFeatures(),
199 compiler_driver_->GetCompiledMethodStorage());
200 if (!oat_writer.StartRoData(dex_files, oat_rodata, &key_value_store)) {
201 return false;
202 }
203 oat_writer.Initialize(compiler_driver_.get(), /*image_writer=*/ nullptr, dex_files);
204 if (!oat_writer.FinishVdexFile(vdex_file, /*verifier_deps=*/ nullptr)) {
205 return false;
206 }
207 oat_writer.PrepareLayout(&patcher);
208 elf_writer->PrepareDynamicSection(oat_writer.GetOatHeader().GetExecutableOffset(),
209 oat_writer.GetCodeSize(),
210 oat_writer.GetDataImgRelRoSize(),
211 oat_writer.GetDataImgRelRoAppImageOffset(),
212 oat_writer.GetBssSize(),
213 oat_writer.GetBssMethodsOffset(),
214 oat_writer.GetBssRootsOffset(),
215 oat_writer.GetVdexSize());
216
217
218 if (!oat_writer.WriteRodata(oat_rodata)) {
219 return false;
220 }
221 elf_writer->EndRoData(oat_rodata);
222
223 OutputStream* text = elf_writer->StartText();
224 if (!oat_writer.WriteCode(text)) {
225 return false;
226 }
227 elf_writer->EndText(text);
228
229 if (oat_writer.GetDataImgRelRoSize() != 0u) {
230 OutputStream* data_img_rel_ro = elf_writer->StartDataImgRelRo();
231 if (!oat_writer.WriteDataImgRelRo(data_img_rel_ro)) {
232 return false;
233 }
234 elf_writer->EndDataImgRelRo(data_img_rel_ro);
235 }
236
237 if (!oat_writer.WriteHeader(elf_writer->GetStream())) {
238 return false;
239 }
240
241 elf_writer->WriteDynamicSection();
242 elf_writer->WriteDebugInfo(oat_writer.GetDebugInfo());
243
244 if (!elf_writer->End()) {
245 return false;
246 }
247
248 for (MemMap& map : opened_dex_files_maps) {
249 opened_dex_files_maps_.emplace_back(std::move(map));
250 }
251 for (std::unique_ptr<const DexFile>& dex_file : opened_dex_files) {
252 opened_dex_files_.emplace_back(dex_file.release());
253 }
254 return true;
255 }
256
CheckOatWriteResult(ScratchFile & oat_file,ScratchFile & vdex_file,std::vector<std::unique_ptr<const DexFile>> & input_dexfiles,const unsigned int expected_oat_dexfile_count,bool low_4gb)257 void CheckOatWriteResult(ScratchFile& oat_file,
258 ScratchFile& vdex_file,
259 std::vector<std::unique_ptr<const DexFile>>& input_dexfiles,
260 const unsigned int expected_oat_dexfile_count,
261 bool low_4gb) {
262 ASSERT_EQ(expected_oat_dexfile_count, input_dexfiles.size());
263
264 std::string error_msg;
265 std::unique_ptr<OatFile> opened_oat_file(OatFile::Open(/*zip_fd=*/ -1,
266 oat_file.GetFilename(),
267 oat_file.GetFilename(),
268 /*executable=*/ false,
269 low_4gb,
270 &error_msg));
271 ASSERT_TRUE(opened_oat_file != nullptr) << error_msg;
272 ASSERT_EQ(expected_oat_dexfile_count, opened_oat_file->GetOatDexFiles().size());
273
274 if (low_4gb) {
275 uintptr_t begin = reinterpret_cast<uintptr_t>(opened_oat_file->Begin());
276 EXPECT_EQ(begin, static_cast<uint32_t>(begin));
277 }
278
279 for (uint32_t i = 0; i < input_dexfiles.size(); i++) {
280 const std::unique_ptr<const DexFile>& dex_file_data = input_dexfiles[i];
281 std::unique_ptr<const DexFile> opened_dex_file =
282 opened_oat_file->GetOatDexFiles()[i]->OpenDexFile(&error_msg);
283
284 ASSERT_EQ(opened_oat_file->GetOatDexFiles()[i]->GetDexFileLocationChecksum(),
285 dex_file_data->GetHeader().checksum_);
286
287 ASSERT_EQ(dex_file_data->GetHeader().file_size_, opened_dex_file->GetHeader().file_size_);
288 ASSERT_EQ(0, memcmp(&dex_file_data->GetHeader(),
289 &opened_dex_file->GetHeader(),
290 dex_file_data->GetHeader().file_size_));
291 ASSERT_EQ(dex_file_data->GetLocation(), opened_dex_file->GetLocation());
292 }
293
294 int64_t actual_vdex_size = vdex_file.GetFile()->GetLength();
295 ASSERT_GE(actual_vdex_size, 0);
296 ASSERT_EQ(dchecked_integral_cast<uint64_t>(actual_vdex_size),
297 opened_oat_file->GetVdexFile()->GetComputedFileSize());
298 }
299
300 void TestDexFileInput(bool verify, bool low_4gb, bool use_profile);
301 void TestZipFileInput(bool verify, CopyOption copy);
302 void TestZipFileInputWithEmptyDex();
303
304 std::unique_ptr<QuickCompilerCallbacks> callbacks_;
305
306 std::vector<MemMap> opened_dex_files_maps_;
307 std::vector<std::unique_ptr<const DexFile>> opened_dex_files_;
308 };
309
310 class ZipBuilder {
311 public:
ZipBuilder(File * zip_file)312 explicit ZipBuilder(File* zip_file) : zip_file_(zip_file) { }
313
AddFile(const char * location,const void * data,size_t size)314 bool AddFile(const char* location, const void* data, size_t size) {
315 off_t offset = lseek(zip_file_->Fd(), 0, SEEK_CUR);
316 if (offset == static_cast<off_t>(-1)) {
317 return false;
318 }
319
320 ZipFileHeader file_header;
321 file_header.crc32 = crc32(0u, reinterpret_cast<const Bytef*>(data), size);
322 file_header.compressed_size = size;
323 file_header.uncompressed_size = size;
324 file_header.filename_length = strlen(location);
325
326 if (!zip_file_->WriteFully(&file_header, sizeof(file_header)) ||
327 !zip_file_->WriteFully(location, file_header.filename_length) ||
328 !zip_file_->WriteFully(data, size)) {
329 return false;
330 }
331
332 CentralDirectoryFileHeader cdfh;
333 cdfh.crc32 = file_header.crc32;
334 cdfh.compressed_size = size;
335 cdfh.uncompressed_size = size;
336 cdfh.filename_length = file_header.filename_length;
337 cdfh.relative_offset_of_local_file_header = offset;
338 file_data_.push_back(FileData { cdfh, location });
339 return true;
340 }
341
Finish()342 bool Finish() {
343 off_t offset = lseek(zip_file_->Fd(), 0, SEEK_CUR);
344 if (offset == static_cast<off_t>(-1)) {
345 return false;
346 }
347
348 size_t central_directory_size = 0u;
349 for (const FileData& file_data : file_data_) {
350 if (!zip_file_->WriteFully(&file_data.cdfh, sizeof(file_data.cdfh)) ||
351 !zip_file_->WriteFully(file_data.location, file_data.cdfh.filename_length)) {
352 return false;
353 }
354 central_directory_size += sizeof(file_data.cdfh) + file_data.cdfh.filename_length;
355 }
356 EndOfCentralDirectoryRecord eocd_record;
357 eocd_record.number_of_central_directory_records_on_this_disk = file_data_.size();
358 eocd_record.total_number_of_central_directory_records = file_data_.size();
359 eocd_record.size_of_central_directory = central_directory_size;
360 eocd_record.offset_of_start_of_central_directory = offset;
361 return
362 zip_file_->WriteFully(&eocd_record, sizeof(eocd_record)) &&
363 zip_file_->Flush() == 0;
364 }
365
366 private:
367 struct PACKED(1) ZipFileHeader {
368 uint32_t signature = 0x04034b50;
369 uint16_t version_needed_to_extract = 10;
370 uint16_t general_purpose_bit_flag = 0;
371 uint16_t compression_method = 0; // 0 = store only.
372 uint16_t file_last_modification_time = 0u;
373 uint16_t file_last_modification_date = 0u;
374 uint32_t crc32;
375 uint32_t compressed_size;
376 uint32_t uncompressed_size;
377 uint16_t filename_length;
378 uint16_t extra_field_length = 0u; // No extra fields.
379 };
380
381 struct PACKED(1) CentralDirectoryFileHeader {
382 uint32_t signature = 0x02014b50;
383 uint16_t version_made_by = 10;
384 uint16_t version_needed_to_extract = 10;
385 uint16_t general_purpose_bit_flag = 0;
386 uint16_t compression_method = 0; // 0 = store only.
387 uint16_t file_last_modification_time = 0u;
388 uint16_t file_last_modification_date = 0u;
389 uint32_t crc32;
390 uint32_t compressed_size;
391 uint32_t uncompressed_size;
392 uint16_t filename_length;
393 uint16_t extra_field_length = 0u; // No extra fields.
394 uint16_t file_comment_length = 0u; // No file comment.
395 uint16_t disk_number_where_file_starts = 0u;
396 uint16_t internal_file_attributes = 0u;
397 uint32_t external_file_attributes = 0u;
398 uint32_t relative_offset_of_local_file_header;
399 };
400
401 struct PACKED(1) EndOfCentralDirectoryRecord {
402 uint32_t signature = 0x06054b50;
403 uint16_t number_of_this_disk = 0u;
404 uint16_t disk_where_central_directory_starts = 0u;
405 uint16_t number_of_central_directory_records_on_this_disk;
406 uint16_t total_number_of_central_directory_records;
407 uint32_t size_of_central_directory;
408 uint32_t offset_of_start_of_central_directory;
409 uint16_t comment_length = 0u; // No file comment.
410 };
411
412 struct FileData {
413 CentralDirectoryFileHeader cdfh;
414 const char* location;
415 };
416
417 File* zip_file_;
418 std::vector<FileData> file_data_;
419 };
420
TEST_F(OatTest,WriteRead)421 TEST_F(OatTest, WriteRead) {
422 TimingLogger timings("OatTest::WriteRead", false, false);
423 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
424
425 std::string error_msg;
426 SetupCompiler(std::vector<std::string>());
427
428 jobject class_loader = nullptr;
429 if (kCompile) {
430 TimingLogger timings2("OatTest::WriteRead", false, false);
431 CompileAll(class_loader, class_linker->GetBootClassPath(), &timings2);
432 }
433
434 ScratchFile tmp_base, tmp_oat(tmp_base, ".oat"), tmp_vdex(tmp_base, ".vdex");
435 SafeMap<std::string, std::string> key_value_store;
436 key_value_store.Put(OatHeader::kBootClassPathChecksumsKey, "testkey");
437 bool success = WriteElf(tmp_vdex.GetFile(),
438 tmp_oat.GetFile(),
439 class_linker->GetBootClassPath(),
440 key_value_store,
441 false);
442 ASSERT_TRUE(success);
443
444 if (kCompile) { // OatWriter strips the code, regenerate to compare
445 CompileAll(class_loader, class_linker->GetBootClassPath(), &timings);
446 }
447 std::unique_ptr<OatFile> oat_file(OatFile::Open(/*zip_fd=*/ -1,
448 tmp_oat.GetFilename(),
449 tmp_oat.GetFilename(),
450 /*executable=*/ false,
451 /*low_4gb=*/ true,
452 &error_msg));
453 ASSERT_TRUE(oat_file.get() != nullptr) << error_msg;
454 const OatHeader& oat_header = oat_file->GetOatHeader();
455 ASSERT_TRUE(oat_header.IsValid());
456 ASSERT_EQ(class_linker->GetBootClassPath().size(), oat_header.GetDexFileCount()); // core
457 ASSERT_TRUE(oat_header.GetStoreValueByKey(OatHeader::kBootClassPathChecksumsKey) != nullptr);
458 ASSERT_STREQ("testkey", oat_header.GetStoreValueByKey(OatHeader::kBootClassPathChecksumsKey));
459
460 ASSERT_TRUE(java_lang_dex_file_ != nullptr);
461 const DexFile& dex_file = *java_lang_dex_file_;
462 const OatDexFile* oat_dex_file = oat_file->GetOatDexFile(dex_file.GetLocation().c_str());
463 ASSERT_TRUE(oat_dex_file != nullptr);
464 CHECK_EQ(dex_file.GetLocationChecksum(), oat_dex_file->GetDexFileLocationChecksum());
465 ScopedObjectAccess soa(Thread::Current());
466 auto pointer_size = class_linker->GetImagePointerSize();
467 for (ClassAccessor accessor : dex_file.GetClasses()) {
468 size_t num_virtual_methods = accessor.NumVirtualMethods();
469
470 const char* descriptor = accessor.GetDescriptor();
471 ObjPtr<mirror::Class> klass = class_linker->FindClass(soa.Self(),
472 descriptor,
473 ScopedNullHandle<mirror::ClassLoader>());
474
475 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(accessor.GetClassDefIndex());
476 CHECK_EQ(ClassStatus::kNotReady, oat_class.GetStatus()) << descriptor;
477 CHECK_EQ(kCompile ? OatClassType::kAllCompiled : OatClassType::kNoneCompiled,
478 oat_class.GetType()) << descriptor;
479
480 size_t method_index = 0;
481 for (auto& m : klass->GetDirectMethods(pointer_size)) {
482 CheckMethod(&m, oat_class.GetOatMethod(method_index), dex_file);
483 ++method_index;
484 }
485 size_t visited_virtuals = 0;
486 // TODO We should also check copied methods in this test.
487 for (auto& m : klass->GetDeclaredVirtualMethods(pointer_size)) {
488 if (!klass->IsInterface()) {
489 EXPECT_FALSE(m.IsCopied());
490 }
491 CheckMethod(&m, oat_class.GetOatMethod(method_index), dex_file);
492 ++method_index;
493 ++visited_virtuals;
494 }
495 EXPECT_EQ(visited_virtuals, num_virtual_methods);
496 }
497 }
498
TEST_F(OatTest,OatHeaderSizeCheck)499 TEST_F(OatTest, OatHeaderSizeCheck) {
500 // If this test is failing and you have to update these constants,
501 // it is time to update OatHeader::kOatVersion
502 EXPECT_EQ(68U, sizeof(OatHeader));
503 EXPECT_EQ(4U, sizeof(OatMethodOffsets));
504 EXPECT_EQ(4U, sizeof(OatQuickMethodHeader));
505 EXPECT_EQ(170 * static_cast<size_t>(GetInstructionSetPointerSize(kRuntimeISA)),
506 sizeof(QuickEntryPoints));
507 }
508
TEST_F(OatTest,OatHeaderIsValid)509 TEST_F(OatTest, OatHeaderIsValid) {
510 InstructionSet insn_set = InstructionSet::kX86;
511 std::string error_msg;
512 std::unique_ptr<const InstructionSetFeatures> insn_features(
513 InstructionSetFeatures::FromVariant(insn_set, "default", &error_msg));
514 ASSERT_TRUE(insn_features.get() != nullptr) << error_msg;
515 std::unique_ptr<OatHeader> oat_header(OatHeader::Create(insn_set,
516 insn_features.get(),
517 0u,
518 nullptr));
519 ASSERT_NE(oat_header.get(), nullptr);
520 ASSERT_TRUE(oat_header->IsValid());
521
522 char* magic = const_cast<char*>(oat_header->GetMagic());
523 strcpy(magic, ""); // bad magic
524 ASSERT_FALSE(oat_header->IsValid());
525 strcpy(magic, "oat\n000"); // bad version
526 ASSERT_FALSE(oat_header->IsValid());
527 }
528
TEST_F(OatTest,EmptyTextSection)529 TEST_F(OatTest, EmptyTextSection) {
530 TimingLogger timings("OatTest::EmptyTextSection", false, false);
531
532 std::vector<std::string> compiler_options;
533 compiler_options.push_back("--compiler-filter=extract");
534 SetupCompiler(compiler_options);
535
536 jobject class_loader;
537 {
538 ScopedObjectAccess soa(Thread::Current());
539 class_loader = LoadDex("Main");
540 }
541 ASSERT_TRUE(class_loader != nullptr);
542 std::vector<const DexFile*> dex_files = GetDexFiles(class_loader);
543 ASSERT_TRUE(!dex_files.empty());
544
545 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
546 for (const DexFile* dex_file : dex_files) {
547 ScopedObjectAccess soa(Thread::Current());
548 class_linker->RegisterDexFile(*dex_file, soa.Decode<mirror::ClassLoader>(class_loader));
549 }
550 CompileAll(class_loader, dex_files, &timings);
551
552 ScratchFile tmp_base, tmp_oat(tmp_base, ".oat"), tmp_vdex(tmp_base, ".vdex");
553 SafeMap<std::string, std::string> key_value_store;
554 bool success = WriteElf(tmp_vdex.GetFile(),
555 tmp_oat.GetFile(),
556 dex_files,
557 key_value_store,
558 /*verify=*/ false);
559 ASSERT_TRUE(success);
560
561 std::string error_msg;
562 std::unique_ptr<OatFile> oat_file(OatFile::Open(/*zip_fd=*/ -1,
563 tmp_oat.GetFilename(),
564 tmp_oat.GetFilename(),
565 /*executable=*/ false,
566 /*low_4gb=*/ false,
567 &error_msg));
568 ASSERT_TRUE(oat_file != nullptr);
569 EXPECT_LT(static_cast<size_t>(oat_file->Size()),
570 static_cast<size_t>(tmp_oat.GetFile()->GetLength()));
571 }
572
MaybeModifyDexFileToFail(bool verify,std::unique_ptr<const DexFile> & data)573 static void MaybeModifyDexFileToFail(bool verify, std::unique_ptr<const DexFile>& data) {
574 // If in verify mode (= fail the verifier mode), make sure we fail early. We'll fail already
575 // because of the missing map, but that may lead to out of bounds reads.
576 if (verify) {
577 const_cast<DexFile::Header*>(&data->GetHeader())->checksum_++;
578 }
579 }
580
TestDexFileInput(bool verify,bool low_4gb,bool use_profile)581 void OatTest::TestDexFileInput(bool verify, bool low_4gb, bool use_profile) {
582 TimingLogger timings("OatTest::DexFileInput", false, false);
583
584 std::vector<const char*> input_filenames;
585 std::vector<std::unique_ptr<const DexFile>> input_dexfiles;
586 std::vector<const ScratchFile*> scratch_files;
587
588 ScratchFile dex_file1;
589 TestDexFileBuilder builder1;
590 builder1.AddField("Lsome/TestClass;", "int", "someField");
591 builder1.AddMethod("Lsome/TestClass;", "()I", "foo");
592 std::unique_ptr<const DexFile> dex_file1_data = builder1.Build(dex_file1.GetFilename());
593
594 MaybeModifyDexFileToFail(verify, dex_file1_data);
595
596 bool success = dex_file1.GetFile()->WriteFully(&dex_file1_data->GetHeader(),
597 dex_file1_data->GetHeader().file_size_);
598 ASSERT_TRUE(success);
599 success = dex_file1.GetFile()->Flush() == 0;
600 ASSERT_TRUE(success);
601 input_filenames.push_back(dex_file1.GetFilename().c_str());
602 input_dexfiles.push_back(std::move(dex_file1_data));
603 scratch_files.push_back(&dex_file1);
604
605 ScratchFile dex_file2;
606 TestDexFileBuilder builder2;
607 builder2.AddField("Land/AnotherTestClass;", "boolean", "someOtherField");
608 builder2.AddMethod("Land/AnotherTestClass;", "()J", "bar");
609 std::unique_ptr<const DexFile> dex_file2_data = builder2.Build(dex_file2.GetFilename());
610
611 MaybeModifyDexFileToFail(verify, dex_file2_data);
612
613 success = dex_file2.GetFile()->WriteFully(&dex_file2_data->GetHeader(),
614 dex_file2_data->GetHeader().file_size_);
615 ASSERT_TRUE(success);
616 success = dex_file2.GetFile()->Flush() == 0;
617 ASSERT_TRUE(success);
618 input_filenames.push_back(dex_file2.GetFilename().c_str());
619 input_dexfiles.push_back(std::move(dex_file2_data));
620 scratch_files.push_back(&dex_file2);
621
622 SafeMap<std::string, std::string> key_value_store;
623 {
624 // Test using the AddDexFileSource() interface with the dex files.
625 ScratchFile tmp_base, tmp_oat(tmp_base, ".oat"), tmp_vdex(tmp_base, ".vdex");
626 std::unique_ptr<ProfileCompilationInfo>
627 profile_compilation_info(use_profile ? new ProfileCompilationInfo() : nullptr);
628 success = WriteElf(tmp_vdex.GetFile(),
629 tmp_oat.GetFile(),
630 input_filenames,
631 key_value_store,
632 verify,
633 CopyOption::kOnlyIfCompressed,
634 profile_compilation_info.get());
635
636 // In verify mode, we expect failure.
637 if (verify) {
638 ASSERT_FALSE(success);
639 return;
640 }
641
642 ASSERT_TRUE(success);
643
644 CheckOatWriteResult(tmp_oat,
645 tmp_vdex,
646 input_dexfiles,
647 /* oat_dexfile_count */ 2,
648 low_4gb);
649 }
650
651 {
652 // Test using the AddDexFileSource() interface with the dexfile1's fd.
653 // Only need one input dexfile.
654 std::vector<std::unique_ptr<const DexFile>> input_dexfiles2;
655 input_dexfiles2.push_back(std::move(input_dexfiles[0]));
656 const ScratchFile* dex_file = scratch_files[0];
657 File dex_file_fd(DupCloexec(dex_file->GetFd()), /*check_usage=*/ false);
658
659 ASSERT_NE(-1, dex_file_fd.Fd());
660 ASSERT_EQ(0, lseek(dex_file_fd.Fd(), 0, SEEK_SET));
661
662 ScratchFile tmp_base, tmp_oat(tmp_base, ".oat"), tmp_vdex(tmp_base, ".vdex");
663 std::unique_ptr<ProfileCompilationInfo>
664 profile_compilation_info(use_profile ? new ProfileCompilationInfo() : nullptr);
665 success = WriteElf(tmp_vdex.GetFile(),
666 tmp_oat.GetFile(),
667 std::move(dex_file_fd),
668 dex_file->GetFilename().c_str(),
669 key_value_store,
670 verify,
671 CopyOption::kOnlyIfCompressed,
672 profile_compilation_info.get());
673
674 // In verify mode, we expect failure.
675 if (verify) {
676 ASSERT_FALSE(success);
677 return;
678 }
679
680 ASSERT_TRUE(success);
681
682 CheckOatWriteResult(tmp_oat,
683 tmp_vdex,
684 input_dexfiles2,
685 /* oat_dexfile_count */ 1,
686 low_4gb);
687 }
688 }
689
TEST_F(OatTest,DexFileInputCheckOutput)690 TEST_F(OatTest, DexFileInputCheckOutput) {
691 TestDexFileInput(/*verify*/false, /*low_4gb*/false, /*use_profile*/false);
692 }
693
TEST_F(OatTest,DexFileInputCheckOutputLow4GB)694 TEST_F(OatTest, DexFileInputCheckOutputLow4GB) {
695 TestDexFileInput(/*verify*/false, /*low_4gb*/true, /*use_profile*/false);
696 }
697
TEST_F(OatTest,DexFileInputCheckVerifier)698 TEST_F(OatTest, DexFileInputCheckVerifier) {
699 TestDexFileInput(/*verify*/true, /*low_4gb*/false, /*use_profile*/false);
700 }
701
TEST_F(OatTest,DexFileFailsVerifierWithLayout)702 TEST_F(OatTest, DexFileFailsVerifierWithLayout) {
703 TestDexFileInput(/*verify*/true, /*low_4gb*/false, /*use_profile*/true);
704 }
705
TestZipFileInput(bool verify,CopyOption copy)706 void OatTest::TestZipFileInput(bool verify, CopyOption copy) {
707 TimingLogger timings("OatTest::DexFileInput", false, false);
708
709 ScratchFile zip_file;
710 ZipBuilder zip_builder(zip_file.GetFile());
711
712 ScratchFile dex_file1;
713 TestDexFileBuilder builder1;
714 builder1.AddField("Lsome/TestClass;", "long", "someField");
715 builder1.AddMethod("Lsome/TestClass;", "()D", "foo");
716 std::unique_ptr<const DexFile> dex_file1_data = builder1.Build(dex_file1.GetFilename());
717
718 MaybeModifyDexFileToFail(verify, dex_file1_data);
719
720 bool success = dex_file1.GetFile()->WriteFully(&dex_file1_data->GetHeader(),
721 dex_file1_data->GetHeader().file_size_);
722 ASSERT_TRUE(success);
723 success = dex_file1.GetFile()->Flush() == 0;
724 ASSERT_TRUE(success);
725 success = zip_builder.AddFile("classes.dex",
726 &dex_file1_data->GetHeader(),
727 dex_file1_data->GetHeader().file_size_);
728 ASSERT_TRUE(success);
729
730 ScratchFile dex_file2;
731 TestDexFileBuilder builder2;
732 builder2.AddField("Land/AnotherTestClass;", "boolean", "someOtherField");
733 builder2.AddMethod("Land/AnotherTestClass;", "()J", "bar");
734 std::unique_ptr<const DexFile> dex_file2_data = builder2.Build(dex_file2.GetFilename());
735
736 MaybeModifyDexFileToFail(verify, dex_file2_data);
737
738 success = dex_file2.GetFile()->WriteFully(&dex_file2_data->GetHeader(),
739 dex_file2_data->GetHeader().file_size_);
740 ASSERT_TRUE(success);
741 success = dex_file2.GetFile()->Flush() == 0;
742 ASSERT_TRUE(success);
743 success = zip_builder.AddFile("classes2.dex",
744 &dex_file2_data->GetHeader(),
745 dex_file2_data->GetHeader().file_size_);
746 ASSERT_TRUE(success);
747
748 success = zip_builder.Finish();
749 ASSERT_TRUE(success) << strerror(errno);
750
751 SafeMap<std::string, std::string> key_value_store;
752 {
753 // Test using the AddDexFileSource() interface with the zip file.
754 std::vector<const char*> input_filenames = { zip_file.GetFilename().c_str() };
755
756 ScratchFile tmp_base, tmp_oat(tmp_base, ".oat"), tmp_vdex(tmp_base, ".vdex");
757 success = WriteElf(tmp_vdex.GetFile(),
758 tmp_oat.GetFile(),
759 input_filenames,
760 key_value_store,
761 verify,
762 copy,
763 /*profile_compilation_info=*/ nullptr);
764
765 if (verify) {
766 ASSERT_FALSE(success);
767 } else {
768 ASSERT_TRUE(success);
769
770 std::string error_msg;
771 std::unique_ptr<OatFile> opened_oat_file(OatFile::Open(/*zip_fd=*/ -1,
772 tmp_oat.GetFilename(),
773 tmp_oat.GetFilename(),
774 /*executable=*/ false,
775 /*low_4gb=*/ false,
776 &error_msg));
777 ASSERT_TRUE(opened_oat_file != nullptr) << error_msg;
778 ASSERT_EQ(2u, opened_oat_file->GetOatDexFiles().size());
779 std::unique_ptr<const DexFile> opened_dex_file1 =
780 opened_oat_file->GetOatDexFiles()[0]->OpenDexFile(&error_msg);
781 std::unique_ptr<const DexFile> opened_dex_file2 =
782 opened_oat_file->GetOatDexFiles()[1]->OpenDexFile(&error_msg);
783
784 ASSERT_EQ(dex_file1_data->GetHeader().file_size_, opened_dex_file1->GetHeader().file_size_);
785 ASSERT_EQ(0, memcmp(&dex_file1_data->GetHeader(),
786 &opened_dex_file1->GetHeader(),
787 dex_file1_data->GetHeader().file_size_));
788 ASSERT_EQ(DexFileLoader::GetMultiDexLocation(0, zip_file.GetFilename().c_str()),
789 opened_dex_file1->GetLocation());
790
791 ASSERT_EQ(dex_file2_data->GetHeader().file_size_, opened_dex_file2->GetHeader().file_size_);
792 ASSERT_EQ(0, memcmp(&dex_file2_data->GetHeader(),
793 &opened_dex_file2->GetHeader(),
794 dex_file2_data->GetHeader().file_size_));
795 ASSERT_EQ(DexFileLoader::GetMultiDexLocation(1, zip_file.GetFilename().c_str()),
796 opened_dex_file2->GetLocation());
797 }
798 }
799
800 {
801 // Test using the AddDexFileSource() interface with the zip file handle.
802 File zip_fd(DupCloexec(zip_file.GetFd()), /*check_usage=*/ false);
803 ASSERT_NE(-1, zip_fd.Fd());
804 ASSERT_EQ(0, lseek(zip_fd.Fd(), 0, SEEK_SET));
805
806 ScratchFile tmp_base, tmp_oat(tmp_base, ".oat"), tmp_vdex(tmp_base, ".vdex");
807 success = WriteElf(tmp_vdex.GetFile(),
808 tmp_oat.GetFile(),
809 std::move(zip_fd),
810 zip_file.GetFilename().c_str(),
811 key_value_store,
812 verify,
813 copy);
814 if (verify) {
815 ASSERT_FALSE(success);
816 } else {
817 ASSERT_TRUE(success);
818
819 std::string error_msg;
820 std::unique_ptr<OatFile> opened_oat_file(OatFile::Open(/*zip_fd=*/ -1,
821 tmp_oat.GetFilename(),
822 tmp_oat.GetFilename(),
823 /*executable=*/ false,
824 /*low_4gb=*/ false,
825 &error_msg));
826 ASSERT_TRUE(opened_oat_file != nullptr) << error_msg;
827 ASSERT_EQ(2u, opened_oat_file->GetOatDexFiles().size());
828 std::unique_ptr<const DexFile> opened_dex_file1 =
829 opened_oat_file->GetOatDexFiles()[0]->OpenDexFile(&error_msg);
830 std::unique_ptr<const DexFile> opened_dex_file2 =
831 opened_oat_file->GetOatDexFiles()[1]->OpenDexFile(&error_msg);
832
833 ASSERT_EQ(dex_file1_data->GetHeader().file_size_, opened_dex_file1->GetHeader().file_size_);
834 ASSERT_EQ(0, memcmp(&dex_file1_data->GetHeader(),
835 &opened_dex_file1->GetHeader(),
836 dex_file1_data->GetHeader().file_size_));
837 ASSERT_EQ(DexFileLoader::GetMultiDexLocation(0, zip_file.GetFilename().c_str()),
838 opened_dex_file1->GetLocation());
839
840 ASSERT_EQ(dex_file2_data->GetHeader().file_size_, opened_dex_file2->GetHeader().file_size_);
841 ASSERT_EQ(0, memcmp(&dex_file2_data->GetHeader(),
842 &opened_dex_file2->GetHeader(),
843 dex_file2_data->GetHeader().file_size_));
844 ASSERT_EQ(DexFileLoader::GetMultiDexLocation(1, zip_file.GetFilename().c_str()),
845 opened_dex_file2->GetLocation());
846 }
847 }
848 }
849
TEST_F(OatTest,ZipFileInputCheckOutput)850 TEST_F(OatTest, ZipFileInputCheckOutput) {
851 TestZipFileInput(false, CopyOption::kOnlyIfCompressed);
852 }
853
TEST_F(OatTest,ZipFileInputCheckOutputWithoutCopy)854 TEST_F(OatTest, ZipFileInputCheckOutputWithoutCopy) {
855 TestZipFileInput(false, CopyOption::kNever);
856 }
857
TEST_F(OatTest,ZipFileInputCheckVerifier)858 TEST_F(OatTest, ZipFileInputCheckVerifier) {
859 TestZipFileInput(true, CopyOption::kOnlyIfCompressed);
860 }
861
TestZipFileInputWithEmptyDex()862 void OatTest::TestZipFileInputWithEmptyDex() {
863 ScratchFile zip_file;
864 ZipBuilder zip_builder(zip_file.GetFile());
865 bool success = zip_builder.AddFile("classes.dex", nullptr, 0);
866 ASSERT_TRUE(success);
867 success = zip_builder.Finish();
868 ASSERT_TRUE(success) << strerror(errno);
869
870 SafeMap<std::string, std::string> key_value_store;
871 std::vector<const char*> input_filenames = { zip_file.GetFilename().c_str() };
872 ScratchFile oat_file, vdex_file(oat_file, ".vdex");
873 std::unique_ptr<ProfileCompilationInfo> profile_compilation_info(new ProfileCompilationInfo());
874 success = WriteElf(vdex_file.GetFile(),
875 oat_file.GetFile(),
876 input_filenames,
877 key_value_store,
878 /*verify=*/ false,
879 CopyOption::kOnlyIfCompressed,
880 profile_compilation_info.get());
881 ASSERT_FALSE(success);
882 }
883
TEST_F(OatTest,ZipFileInputWithEmptyDex)884 TEST_F(OatTest, ZipFileInputWithEmptyDex) {
885 TestZipFileInputWithEmptyDex();
886 }
887
888 } // namespace linker
889 } // namespace art
890