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