xref: /aosp_15_r20/art/dex2oat/linker/image_test.h (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
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 #ifndef ART_DEX2OAT_LINKER_IMAGE_TEST_H_
18*795d594fSAndroid Build Coastguard Worker #define ART_DEX2OAT_LINKER_IMAGE_TEST_H_
19*795d594fSAndroid Build Coastguard Worker 
20*795d594fSAndroid Build Coastguard Worker #include "oat/image.h"
21*795d594fSAndroid Build Coastguard Worker 
22*795d594fSAndroid Build Coastguard Worker #include <memory>
23*795d594fSAndroid Build Coastguard Worker #include <string>
24*795d594fSAndroid Build Coastguard Worker #include <string_view>
25*795d594fSAndroid Build Coastguard Worker #include <vector>
26*795d594fSAndroid Build Coastguard Worker 
27*795d594fSAndroid Build Coastguard Worker #include "android-base/stringprintf.h"
28*795d594fSAndroid Build Coastguard Worker #include "android-base/strings.h"
29*795d594fSAndroid Build Coastguard Worker 
30*795d594fSAndroid Build Coastguard Worker #include "art_method-inl.h"
31*795d594fSAndroid Build Coastguard Worker #include "base/file_utils.h"
32*795d594fSAndroid Build Coastguard Worker #include "base/hash_set.h"
33*795d594fSAndroid Build Coastguard Worker #include "base/stl_util.h"
34*795d594fSAndroid Build Coastguard Worker #include "base/unix_file/fd_file.h"
35*795d594fSAndroid Build Coastguard Worker #include "base/utils.h"
36*795d594fSAndroid Build Coastguard Worker #include "class_linker-inl.h"
37*795d594fSAndroid Build Coastguard Worker #include "common_compiler_driver_test.h"
38*795d594fSAndroid Build Coastguard Worker #include "compiler_callbacks.h"
39*795d594fSAndroid Build Coastguard Worker #include "debug/method_debug_info.h"
40*795d594fSAndroid Build Coastguard Worker #include "dex/quick_compiler_callbacks.h"
41*795d594fSAndroid Build Coastguard Worker #include "dex/signature-inl.h"
42*795d594fSAndroid Build Coastguard Worker #include "driver/compiler_driver.h"
43*795d594fSAndroid Build Coastguard Worker #include "driver/compiler_options.h"
44*795d594fSAndroid Build Coastguard Worker #include "gc/space/image_space.h"
45*795d594fSAndroid Build Coastguard Worker #include "image_writer.h"
46*795d594fSAndroid Build Coastguard Worker #include "linker/elf_writer.h"
47*795d594fSAndroid Build Coastguard Worker #include "linker/elf_writer_quick.h"
48*795d594fSAndroid Build Coastguard Worker #include "linker/multi_oat_relative_patcher.h"
49*795d594fSAndroid Build Coastguard Worker #include "lock_word.h"
50*795d594fSAndroid Build Coastguard Worker #include "mirror/object-inl.h"
51*795d594fSAndroid Build Coastguard Worker #include "oat/oat.h"
52*795d594fSAndroid Build Coastguard Worker #include "oat_writer.h"
53*795d594fSAndroid Build Coastguard Worker #include "read_barrier_config.h"
54*795d594fSAndroid Build Coastguard Worker #include "scoped_thread_state_change-inl.h"
55*795d594fSAndroid Build Coastguard Worker #include "signal_catcher.h"
56*795d594fSAndroid Build Coastguard Worker #include "stream/buffered_output_stream.h"
57*795d594fSAndroid Build Coastguard Worker #include "stream/file_output_stream.h"
58*795d594fSAndroid Build Coastguard Worker 
59*795d594fSAndroid Build Coastguard Worker namespace art {
60*795d594fSAndroid Build Coastguard Worker namespace linker {
61*795d594fSAndroid Build Coastguard Worker 
62*795d594fSAndroid Build Coastguard Worker static const uintptr_t kRequestedImageBase = ART_BASE_ADDRESS;
63*795d594fSAndroid Build Coastguard Worker 
64*795d594fSAndroid Build Coastguard Worker struct CompilationHelper {
65*795d594fSAndroid Build Coastguard Worker   std::vector<std::string> dex_file_locations;
66*795d594fSAndroid Build Coastguard Worker   std::vector<ScratchFile> image_locations;
67*795d594fSAndroid Build Coastguard Worker   std::string extra_dex;
68*795d594fSAndroid Build Coastguard Worker   std::vector<std::unique_ptr<const DexFile>> extra_dex_files;
69*795d594fSAndroid Build Coastguard Worker   std::vector<ScratchFile> image_files;
70*795d594fSAndroid Build Coastguard Worker   std::vector<ScratchFile> oat_files;
71*795d594fSAndroid Build Coastguard Worker   std::vector<ScratchFile> vdex_files;
72*795d594fSAndroid Build Coastguard Worker   std::string image_dir;
73*795d594fSAndroid Build Coastguard Worker 
74*795d594fSAndroid Build Coastguard Worker   std::vector<size_t> GetImageObjectSectionSizes();
75*795d594fSAndroid Build Coastguard Worker 
76*795d594fSAndroid Build Coastguard Worker   ~CompilationHelper();
77*795d594fSAndroid Build Coastguard Worker };
78*795d594fSAndroid Build Coastguard Worker 
79*795d594fSAndroid Build Coastguard Worker class ImageTest : public CommonCompilerDriverTest {
80*795d594fSAndroid Build Coastguard Worker  protected:
SetUp()81*795d594fSAndroid Build Coastguard Worker   void SetUp() override {
82*795d594fSAndroid Build Coastguard Worker     ReserveImageSpace();
83*795d594fSAndroid Build Coastguard Worker     CommonCompilerDriverTest::SetUp();
84*795d594fSAndroid Build Coastguard Worker   }
85*795d594fSAndroid Build Coastguard Worker 
GetCompilerFilter()86*795d594fSAndroid Build Coastguard Worker   CompilerFilter::Filter GetCompilerFilter() const override {
87*795d594fSAndroid Build Coastguard Worker     return compiler_filter_;
88*795d594fSAndroid Build Coastguard Worker   }
89*795d594fSAndroid Build Coastguard Worker 
SetCompilerFilter(CompilerFilter::Filter compiler_filter)90*795d594fSAndroid Build Coastguard Worker   void SetCompilerFilter(CompilerFilter::Filter compiler_filter) {
91*795d594fSAndroid Build Coastguard Worker     compiler_filter_ = compiler_filter;
92*795d594fSAndroid Build Coastguard Worker   }
93*795d594fSAndroid Build Coastguard Worker 
94*795d594fSAndroid Build Coastguard Worker   void Compile(ImageHeader::StorageMode storage_mode,
95*795d594fSAndroid Build Coastguard Worker                uint32_t max_image_block_size,
96*795d594fSAndroid Build Coastguard Worker                /*out*/ CompilationHelper& out_helper,
97*795d594fSAndroid Build Coastguard Worker                const std::string& extra_dex = "",
98*795d594fSAndroid Build Coastguard Worker                const std::initializer_list<std::string>& image_classes = {},
99*795d594fSAndroid Build Coastguard Worker                const std::initializer_list<std::string>& image_classes_failing_aot_clinit = {},
100*795d594fSAndroid Build Coastguard Worker                const std::initializer_list<std::string>& image_classes_failing_resolution = {});
101*795d594fSAndroid Build Coastguard Worker 
SetUpRuntimeOptions(RuntimeOptions * options)102*795d594fSAndroid Build Coastguard Worker   void SetUpRuntimeOptions(RuntimeOptions* options) override {
103*795d594fSAndroid Build Coastguard Worker     CommonCompilerDriverTest::SetUpRuntimeOptions(options);
104*795d594fSAndroid Build Coastguard Worker     QuickCompilerCallbacks* new_callbacks =
105*795d594fSAndroid Build Coastguard Worker         new QuickCompilerCallbacks(CompilerCallbacks::CallbackMode::kCompileBootImage);
106*795d594fSAndroid Build Coastguard Worker     new_callbacks->SetVerificationResults(verification_results_.get());
107*795d594fSAndroid Build Coastguard Worker     callbacks_.reset(new_callbacks);
108*795d594fSAndroid Build Coastguard Worker     options->push_back(std::make_pair("compilercallbacks", callbacks_.get()));
109*795d594fSAndroid Build Coastguard Worker   }
110*795d594fSAndroid Build Coastguard Worker 
GetImageClasses()111*795d594fSAndroid Build Coastguard Worker   std::unique_ptr<HashSet<std::string>> GetImageClasses() override {
112*795d594fSAndroid Build Coastguard Worker     return std::make_unique<HashSet<std::string>>(image_classes_);
113*795d594fSAndroid Build Coastguard Worker   }
114*795d594fSAndroid Build Coastguard Worker 
FindCopiedMethod(ArtMethod * origin,ObjPtr<mirror::Class> klass)115*795d594fSAndroid Build Coastguard Worker   ArtMethod* FindCopiedMethod(ArtMethod* origin, ObjPtr<mirror::Class> klass)
116*795d594fSAndroid Build Coastguard Worker       REQUIRES_SHARED(Locks::mutator_lock_) {
117*795d594fSAndroid Build Coastguard Worker     PointerSize pointer_size = class_linker_->GetImagePointerSize();
118*795d594fSAndroid Build Coastguard Worker     for (ArtMethod& m : klass->GetCopiedMethods(pointer_size)) {
119*795d594fSAndroid Build Coastguard Worker       if (strcmp(origin->GetName(), m.GetName()) == 0 &&
120*795d594fSAndroid Build Coastguard Worker           origin->GetSignature() == m.GetSignature()) {
121*795d594fSAndroid Build Coastguard Worker         return &m;
122*795d594fSAndroid Build Coastguard Worker       }
123*795d594fSAndroid Build Coastguard Worker     }
124*795d594fSAndroid Build Coastguard Worker     return nullptr;
125*795d594fSAndroid Build Coastguard Worker   }
126*795d594fSAndroid Build Coastguard Worker 
127*795d594fSAndroid Build Coastguard Worker  private:
128*795d594fSAndroid Build Coastguard Worker   void DoCompile(ImageHeader::StorageMode storage_mode, /*out*/ CompilationHelper& out_helper);
129*795d594fSAndroid Build Coastguard Worker 
130*795d594fSAndroid Build Coastguard Worker   HashSet<std::string> image_classes_;
131*795d594fSAndroid Build Coastguard Worker 
132*795d594fSAndroid Build Coastguard Worker   // By default we compile with "speed-profile" and an empty profile. This compiles only JNI stubs.
133*795d594fSAndroid Build Coastguard Worker   CompilerFilter::Filter compiler_filter_ = CompilerFilter::kSpeedProfile;
134*795d594fSAndroid Build Coastguard Worker };
135*795d594fSAndroid Build Coastguard Worker 
~CompilationHelper()136*795d594fSAndroid Build Coastguard Worker inline CompilationHelper::~CompilationHelper() {
137*795d594fSAndroid Build Coastguard Worker   for (ScratchFile& image_file : image_files) {
138*795d594fSAndroid Build Coastguard Worker     image_file.Unlink();
139*795d594fSAndroid Build Coastguard Worker   }
140*795d594fSAndroid Build Coastguard Worker   for (ScratchFile& oat_file : oat_files) {
141*795d594fSAndroid Build Coastguard Worker     oat_file.Unlink();
142*795d594fSAndroid Build Coastguard Worker   }
143*795d594fSAndroid Build Coastguard Worker   for (ScratchFile& vdex_file : vdex_files) {
144*795d594fSAndroid Build Coastguard Worker     vdex_file.Unlink();
145*795d594fSAndroid Build Coastguard Worker   }
146*795d594fSAndroid Build Coastguard Worker   const int rmdir_result = rmdir(image_dir.c_str());
147*795d594fSAndroid Build Coastguard Worker   CHECK_EQ(0, rmdir_result);
148*795d594fSAndroid Build Coastguard Worker }
149*795d594fSAndroid Build Coastguard Worker 
GetImageObjectSectionSizes()150*795d594fSAndroid Build Coastguard Worker inline std::vector<size_t> CompilationHelper::GetImageObjectSectionSizes() {
151*795d594fSAndroid Build Coastguard Worker   std::vector<size_t> ret;
152*795d594fSAndroid Build Coastguard Worker   for (ScratchFile& image_file : image_files) {
153*795d594fSAndroid Build Coastguard Worker     std::unique_ptr<File> file(OS::OpenFileForReading(image_file.GetFilename().c_str()));
154*795d594fSAndroid Build Coastguard Worker     CHECK(file.get() != nullptr);
155*795d594fSAndroid Build Coastguard Worker     ImageHeader image_header;
156*795d594fSAndroid Build Coastguard Worker     CHECK_EQ(file->ReadFully(&image_header, sizeof(image_header)), true);
157*795d594fSAndroid Build Coastguard Worker     CHECK(image_header.IsValid());
158*795d594fSAndroid Build Coastguard Worker     ret.push_back(image_header.GetObjectsSection().Size());
159*795d594fSAndroid Build Coastguard Worker   }
160*795d594fSAndroid Build Coastguard Worker   return ret;
161*795d594fSAndroid Build Coastguard Worker }
162*795d594fSAndroid Build Coastguard Worker 
DoCompile(ImageHeader::StorageMode storage_mode,CompilationHelper & out_helper)163*795d594fSAndroid Build Coastguard Worker inline void ImageTest::DoCompile(ImageHeader::StorageMode storage_mode,
164*795d594fSAndroid Build Coastguard Worker                                  /*out*/ CompilationHelper& out_helper) {
165*795d594fSAndroid Build Coastguard Worker   CompilerDriver* driver = compiler_driver_.get();
166*795d594fSAndroid Build Coastguard Worker   Runtime::Current()->AppendToBootClassPath(
167*795d594fSAndroid Build Coastguard Worker       out_helper.extra_dex, out_helper.extra_dex, out_helper.extra_dex_files);
168*795d594fSAndroid Build Coastguard Worker   ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
169*795d594fSAndroid Build Coastguard Worker   std::vector<const DexFile*> class_path = class_linker->GetBootClassPath();
170*795d594fSAndroid Build Coastguard Worker 
171*795d594fSAndroid Build Coastguard Worker   // Enable write for dex2dex.
172*795d594fSAndroid Build Coastguard Worker   for (const DexFile* dex_file : class_path) {
173*795d594fSAndroid Build Coastguard Worker     out_helper.dex_file_locations.push_back(dex_file->GetLocation());
174*795d594fSAndroid Build Coastguard Worker     if (dex_file->IsReadOnly()) {
175*795d594fSAndroid Build Coastguard Worker       dex_file->EnableWrite();
176*795d594fSAndroid Build Coastguard Worker     }
177*795d594fSAndroid Build Coastguard Worker   }
178*795d594fSAndroid Build Coastguard Worker   {
179*795d594fSAndroid Build Coastguard Worker     // Create a generic tmp file, to be the base of the .art and .oat temporary files.
180*795d594fSAndroid Build Coastguard Worker     ScratchFile location;
181*795d594fSAndroid Build Coastguard Worker     std::vector<std::string> image_locations =
182*795d594fSAndroid Build Coastguard Worker         gc::space::ImageSpace::ExpandMultiImageLocations(
183*795d594fSAndroid Build Coastguard Worker             ArrayRef<const std::string>(out_helper.dex_file_locations),
184*795d594fSAndroid Build Coastguard Worker             location.GetFilename() + ".art");
185*795d594fSAndroid Build Coastguard Worker     for (size_t i = 0u; i != class_path.size(); ++i) {
186*795d594fSAndroid Build Coastguard Worker       out_helper.image_locations.push_back(ScratchFile(image_locations[i]));
187*795d594fSAndroid Build Coastguard Worker     }
188*795d594fSAndroid Build Coastguard Worker   }
189*795d594fSAndroid Build Coastguard Worker   std::vector<std::string> image_filenames;
190*795d594fSAndroid Build Coastguard Worker   for (ScratchFile& file : out_helper.image_locations) {
191*795d594fSAndroid Build Coastguard Worker     std::string image_filename(GetSystemImageFilename(file.GetFilename().c_str(), kRuntimeISA));
192*795d594fSAndroid Build Coastguard Worker     image_filenames.push_back(image_filename);
193*795d594fSAndroid Build Coastguard Worker     size_t pos = image_filename.rfind('/');
194*795d594fSAndroid Build Coastguard Worker     CHECK_NE(pos, std::string::npos) << image_filename;
195*795d594fSAndroid Build Coastguard Worker     if (out_helper.image_dir.empty()) {
196*795d594fSAndroid Build Coastguard Worker       out_helper.image_dir = image_filename.substr(0, pos);
197*795d594fSAndroid Build Coastguard Worker       int mkdir_result = mkdir(out_helper.image_dir.c_str(), 0700);
198*795d594fSAndroid Build Coastguard Worker       CHECK_EQ(0, mkdir_result) << out_helper.image_dir;
199*795d594fSAndroid Build Coastguard Worker     }
200*795d594fSAndroid Build Coastguard Worker     out_helper.image_files.push_back(ScratchFile(OS::CreateEmptyFile(image_filename.c_str())));
201*795d594fSAndroid Build Coastguard Worker   }
202*795d594fSAndroid Build Coastguard Worker 
203*795d594fSAndroid Build Coastguard Worker   std::vector<std::string> oat_filenames;
204*795d594fSAndroid Build Coastguard Worker   std::vector<std::string> vdex_filenames;
205*795d594fSAndroid Build Coastguard Worker   for (const std::string& image_filename : image_filenames) {
206*795d594fSAndroid Build Coastguard Worker     std::string oat_filename = ReplaceFileExtension(image_filename, "oat");
207*795d594fSAndroid Build Coastguard Worker     out_helper.oat_files.push_back(ScratchFile(OS::CreateEmptyFile(oat_filename.c_str())));
208*795d594fSAndroid Build Coastguard Worker     oat_filenames.push_back(oat_filename);
209*795d594fSAndroid Build Coastguard Worker     std::string vdex_filename = ReplaceFileExtension(image_filename, "vdex");
210*795d594fSAndroid Build Coastguard Worker     out_helper.vdex_files.push_back(ScratchFile(OS::CreateEmptyFile(vdex_filename.c_str())));
211*795d594fSAndroid Build Coastguard Worker     vdex_filenames.push_back(vdex_filename);
212*795d594fSAndroid Build Coastguard Worker   }
213*795d594fSAndroid Build Coastguard Worker 
214*795d594fSAndroid Build Coastguard Worker   HashMap<const DexFile*, size_t> dex_file_to_oat_index_map;
215*795d594fSAndroid Build Coastguard Worker   size_t image_idx = 0;
216*795d594fSAndroid Build Coastguard Worker   for (const DexFile* dex_file : class_path) {
217*795d594fSAndroid Build Coastguard Worker     dex_file_to_oat_index_map.insert(std::make_pair(dex_file, image_idx));
218*795d594fSAndroid Build Coastguard Worker     ++image_idx;
219*795d594fSAndroid Build Coastguard Worker   }
220*795d594fSAndroid Build Coastguard Worker   std::unique_ptr<ImageWriter> writer(new ImageWriter(*compiler_options_,
221*795d594fSAndroid Build Coastguard Worker                                                       kRequestedImageBase,
222*795d594fSAndroid Build Coastguard Worker                                                       storage_mode,
223*795d594fSAndroid Build Coastguard Worker                                                       oat_filenames,
224*795d594fSAndroid Build Coastguard Worker                                                       dex_file_to_oat_index_map,
225*795d594fSAndroid Build Coastguard Worker                                                       /*class_loader=*/ nullptr,
226*795d594fSAndroid Build Coastguard Worker                                                       /*dirty_image_objects=*/ nullptr));
227*795d594fSAndroid Build Coastguard Worker   {
228*795d594fSAndroid Build Coastguard Worker     {
229*795d594fSAndroid Build Coastguard Worker       jobject class_loader = nullptr;
230*795d594fSAndroid Build Coastguard Worker       TimingLogger timings("ImageTest::WriteRead", false, false);
231*795d594fSAndroid Build Coastguard Worker       CompileAll(class_loader, class_path, &timings);
232*795d594fSAndroid Build Coastguard Worker 
233*795d594fSAndroid Build Coastguard Worker       TimingLogger::ScopedTiming t("WriteElf", &timings);
234*795d594fSAndroid Build Coastguard Worker       SafeMap<std::string, std::string> key_value_store;
235*795d594fSAndroid Build Coastguard Worker       key_value_store.Put(OatHeader::kBootClassPathKey,
236*795d594fSAndroid Build Coastguard Worker                           android::base::Join(out_helper.dex_file_locations, ':'));
237*795d594fSAndroid Build Coastguard Worker       key_value_store.Put(OatHeader::kApexVersionsKey, Runtime::Current()->GetApexVersions());
238*795d594fSAndroid Build Coastguard Worker       key_value_store.Put(
239*795d594fSAndroid Build Coastguard Worker           OatHeader::kConcurrentCopying,
240*795d594fSAndroid Build Coastguard Worker           compiler_options_->EmitReadBarrier() ? OatHeader::kTrueValue : OatHeader::kFalseValue);
241*795d594fSAndroid Build Coastguard Worker 
242*795d594fSAndroid Build Coastguard Worker       std::vector<std::unique_ptr<ElfWriter>> elf_writers;
243*795d594fSAndroid Build Coastguard Worker       std::vector<std::unique_ptr<OatWriter>> oat_writers;
244*795d594fSAndroid Build Coastguard Worker       for (ScratchFile& oat_file : out_helper.oat_files) {
245*795d594fSAndroid Build Coastguard Worker         elf_writers.emplace_back(CreateElfWriterQuick(*compiler_options_, oat_file.GetFile()));
246*795d594fSAndroid Build Coastguard Worker         elf_writers.back()->Start();
247*795d594fSAndroid Build Coastguard Worker         oat_writers.emplace_back(new OatWriter(*compiler_options_,
248*795d594fSAndroid Build Coastguard Worker                                                &timings,
249*795d594fSAndroid Build Coastguard Worker                                                /*profile_compilation_info*/nullptr));
250*795d594fSAndroid Build Coastguard Worker       }
251*795d594fSAndroid Build Coastguard Worker 
252*795d594fSAndroid Build Coastguard Worker       std::vector<OutputStream*> rodata;
253*795d594fSAndroid Build Coastguard Worker       std::vector<MemMap> opened_dex_files_maps;
254*795d594fSAndroid Build Coastguard Worker       std::vector<std::unique_ptr<const DexFile>> opened_dex_files;
255*795d594fSAndroid Build Coastguard Worker       // Now that we have finalized key_value_store_, start writing the oat file.
256*795d594fSAndroid Build Coastguard Worker       for (size_t i = 0, size = oat_writers.size(); i != size; ++i) {
257*795d594fSAndroid Build Coastguard Worker         const DexFile* dex_file = class_path[i];
258*795d594fSAndroid Build Coastguard Worker         rodata.push_back(elf_writers[i]->StartRoData());
259*795d594fSAndroid Build Coastguard Worker         oat_writers[i]->AddRawDexFileSource(dex_file->GetContainer(),
260*795d594fSAndroid Build Coastguard Worker                                             dex_file->Begin(),
261*795d594fSAndroid Build Coastguard Worker                                             dex_file->GetLocation().c_str(),
262*795d594fSAndroid Build Coastguard Worker                                             dex_file->GetLocationChecksum());
263*795d594fSAndroid Build Coastguard Worker 
264*795d594fSAndroid Build Coastguard Worker         std::vector<MemMap> cur_opened_dex_files_maps;
265*795d594fSAndroid Build Coastguard Worker         std::vector<std::unique_ptr<const DexFile>> cur_opened_dex_files;
266*795d594fSAndroid Build Coastguard Worker         bool dex_files_ok = oat_writers[i]->WriteAndOpenDexFiles(
267*795d594fSAndroid Build Coastguard Worker             out_helper.vdex_files[i].GetFile(),
268*795d594fSAndroid Build Coastguard Worker             /* verify */ false,           // Dex files may be dex-to-dex-ed, don't verify.
269*795d594fSAndroid Build Coastguard Worker             /* update_input_vdex */ false,
270*795d594fSAndroid Build Coastguard Worker             /* copy_dex_files */ CopyOption::kOnlyIfCompressed,
271*795d594fSAndroid Build Coastguard Worker             &cur_opened_dex_files_maps,
272*795d594fSAndroid Build Coastguard Worker             &cur_opened_dex_files);
273*795d594fSAndroid Build Coastguard Worker         ASSERT_TRUE(dex_files_ok);
274*795d594fSAndroid Build Coastguard Worker 
275*795d594fSAndroid Build Coastguard Worker         if (!cur_opened_dex_files_maps.empty()) {
276*795d594fSAndroid Build Coastguard Worker           for (MemMap& cur_map : cur_opened_dex_files_maps) {
277*795d594fSAndroid Build Coastguard Worker             opened_dex_files_maps.push_back(std::move(cur_map));
278*795d594fSAndroid Build Coastguard Worker           }
279*795d594fSAndroid Build Coastguard Worker           for (std::unique_ptr<const DexFile>& cur_dex_file : cur_opened_dex_files) {
280*795d594fSAndroid Build Coastguard Worker             // dex_file_oat_index_map_.emplace(dex_file.get(), i);
281*795d594fSAndroid Build Coastguard Worker             opened_dex_files.push_back(std::move(cur_dex_file));
282*795d594fSAndroid Build Coastguard Worker           }
283*795d594fSAndroid Build Coastguard Worker         } else {
284*795d594fSAndroid Build Coastguard Worker           ASSERT_TRUE(cur_opened_dex_files.empty());
285*795d594fSAndroid Build Coastguard Worker         }
286*795d594fSAndroid Build Coastguard Worker       }
287*795d594fSAndroid Build Coastguard Worker       bool image_space_ok = writer->PrepareImageAddressSpace(&timings);
288*795d594fSAndroid Build Coastguard Worker       ASSERT_TRUE(image_space_ok);
289*795d594fSAndroid Build Coastguard Worker 
290*795d594fSAndroid Build Coastguard Worker       DCHECK_EQ(out_helper.vdex_files.size(), out_helper.oat_files.size());
291*795d594fSAndroid Build Coastguard Worker       for (size_t i = 0, size = out_helper.oat_files.size(); i != size; ++i) {
292*795d594fSAndroid Build Coastguard Worker         MultiOatRelativePatcher patcher(compiler_options_->GetInstructionSet(),
293*795d594fSAndroid Build Coastguard Worker                                         compiler_options_->GetInstructionSetFeatures(),
294*795d594fSAndroid Build Coastguard Worker                                         driver->GetCompiledMethodStorage());
295*795d594fSAndroid Build Coastguard Worker         OatWriter* const oat_writer = oat_writers[i].get();
296*795d594fSAndroid Build Coastguard Worker         ElfWriter* const elf_writer = elf_writers[i].get();
297*795d594fSAndroid Build Coastguard Worker         std::vector<const DexFile*> cur_dex_files(1u, class_path[i]);
298*795d594fSAndroid Build Coastguard Worker         bool start_rodata_ok = oat_writer->StartRoData(cur_dex_files,
299*795d594fSAndroid Build Coastguard Worker                                                        rodata[i],
300*795d594fSAndroid Build Coastguard Worker                                                        (i == 0u) ? &key_value_store : nullptr);
301*795d594fSAndroid Build Coastguard Worker         ASSERT_TRUE(start_rodata_ok);
302*795d594fSAndroid Build Coastguard Worker         oat_writer->Initialize(driver, verification_results_.get(), writer.get(), cur_dex_files);
303*795d594fSAndroid Build Coastguard Worker 
304*795d594fSAndroid Build Coastguard Worker         oat_writer->FinishVdexFile(out_helper.vdex_files[i].GetFile(), /*verifier_deps=*/ nullptr);
305*795d594fSAndroid Build Coastguard Worker 
306*795d594fSAndroid Build Coastguard Worker         oat_writer->PrepareLayout(&patcher);
307*795d594fSAndroid Build Coastguard Worker         elf_writer->PrepareDynamicSection(oat_writer->GetOatHeader().GetExecutableOffset(),
308*795d594fSAndroid Build Coastguard Worker                                           oat_writer->GetCodeSize(),
309*795d594fSAndroid Build Coastguard Worker                                           oat_writer->GetDataImgRelRoSize(),
310*795d594fSAndroid Build Coastguard Worker                                           oat_writer->GetDataImgRelRoAppImageOffset(),
311*795d594fSAndroid Build Coastguard Worker                                           oat_writer->GetBssSize(),
312*795d594fSAndroid Build Coastguard Worker                                           oat_writer->GetBssMethodsOffset(),
313*795d594fSAndroid Build Coastguard Worker                                           oat_writer->GetBssRootsOffset(),
314*795d594fSAndroid Build Coastguard Worker                                           oat_writer->GetVdexSize());
315*795d594fSAndroid Build Coastguard Worker 
316*795d594fSAndroid Build Coastguard Worker         writer->UpdateOatFileLayout(i,
317*795d594fSAndroid Build Coastguard Worker                                     elf_writer->GetLoadedSize(),
318*795d594fSAndroid Build Coastguard Worker                                     oat_writer->GetOatDataOffset(),
319*795d594fSAndroid Build Coastguard Worker                                     oat_writer->GetOatSize());
320*795d594fSAndroid Build Coastguard Worker 
321*795d594fSAndroid Build Coastguard Worker         bool rodata_ok = oat_writer->WriteRodata(rodata[i]);
322*795d594fSAndroid Build Coastguard Worker         ASSERT_TRUE(rodata_ok);
323*795d594fSAndroid Build Coastguard Worker         elf_writer->EndRoData(rodata[i]);
324*795d594fSAndroid Build Coastguard Worker 
325*795d594fSAndroid Build Coastguard Worker         OutputStream* text = elf_writer->StartText();
326*795d594fSAndroid Build Coastguard Worker         bool text_ok = oat_writer->WriteCode(text);
327*795d594fSAndroid Build Coastguard Worker         ASSERT_TRUE(text_ok);
328*795d594fSAndroid Build Coastguard Worker         elf_writer->EndText(text);
329*795d594fSAndroid Build Coastguard Worker 
330*795d594fSAndroid Build Coastguard Worker         if (oat_writer->GetDataImgRelRoSize() != 0u) {
331*795d594fSAndroid Build Coastguard Worker           OutputStream* data_img_rel_ro = elf_writer->StartDataImgRelRo();
332*795d594fSAndroid Build Coastguard Worker           bool data_img_rel_ro_ok = oat_writer->WriteDataImgRelRo(data_img_rel_ro);
333*795d594fSAndroid Build Coastguard Worker           ASSERT_TRUE(data_img_rel_ro_ok);
334*795d594fSAndroid Build Coastguard Worker           elf_writer->EndDataImgRelRo(data_img_rel_ro);
335*795d594fSAndroid Build Coastguard Worker         }
336*795d594fSAndroid Build Coastguard Worker 
337*795d594fSAndroid Build Coastguard Worker         bool header_ok = oat_writer->WriteHeader(elf_writer->GetStream());
338*795d594fSAndroid Build Coastguard Worker         ASSERT_TRUE(header_ok);
339*795d594fSAndroid Build Coastguard Worker 
340*795d594fSAndroid Build Coastguard Worker         writer->UpdateOatFileHeader(i, oat_writer->GetOatHeader());
341*795d594fSAndroid Build Coastguard Worker 
342*795d594fSAndroid Build Coastguard Worker         elf_writer->WriteDynamicSection();
343*795d594fSAndroid Build Coastguard Worker         elf_writer->WriteDebugInfo(oat_writer->GetDebugInfo());
344*795d594fSAndroid Build Coastguard Worker 
345*795d594fSAndroid Build Coastguard Worker         bool success = elf_writer->End();
346*795d594fSAndroid Build Coastguard Worker         ASSERT_TRUE(success);
347*795d594fSAndroid Build Coastguard Worker       }
348*795d594fSAndroid Build Coastguard Worker     }
349*795d594fSAndroid Build Coastguard Worker 
350*795d594fSAndroid Build Coastguard Worker     bool success_image = writer->Write(File::kInvalidFd,
351*795d594fSAndroid Build Coastguard Worker                                        image_filenames,
352*795d594fSAndroid Build Coastguard Worker                                        image_filenames.size());
353*795d594fSAndroid Build Coastguard Worker     ASSERT_TRUE(success_image);
354*795d594fSAndroid Build Coastguard Worker   }
355*795d594fSAndroid Build Coastguard Worker }
356*795d594fSAndroid Build Coastguard Worker 
Compile(ImageHeader::StorageMode storage_mode,uint32_t max_image_block_size,CompilationHelper & helper,const std::string & extra_dex,const std::initializer_list<std::string> & image_classes,const std::initializer_list<std::string> & image_classes_failing_aot_clinit,const std::initializer_list<std::string> & image_classes_failing_resolution)357*795d594fSAndroid Build Coastguard Worker inline void ImageTest::Compile(
358*795d594fSAndroid Build Coastguard Worker     ImageHeader::StorageMode storage_mode,
359*795d594fSAndroid Build Coastguard Worker     uint32_t max_image_block_size,
360*795d594fSAndroid Build Coastguard Worker     CompilationHelper& helper,
361*795d594fSAndroid Build Coastguard Worker     const std::string& extra_dex,
362*795d594fSAndroid Build Coastguard Worker     const std::initializer_list<std::string>& image_classes,
363*795d594fSAndroid Build Coastguard Worker     const std::initializer_list<std::string>& image_classes_failing_aot_clinit,
364*795d594fSAndroid Build Coastguard Worker     const std::initializer_list<std::string>& image_classes_failing_resolution) {
365*795d594fSAndroid Build Coastguard Worker   for (const std::string& image_class : image_classes_failing_aot_clinit) {
366*795d594fSAndroid Build Coastguard Worker     ASSERT_TRUE(ContainsElement(image_classes, image_class));
367*795d594fSAndroid Build Coastguard Worker   }
368*795d594fSAndroid Build Coastguard Worker   for (const std::string& image_class : image_classes) {
369*795d594fSAndroid Build Coastguard Worker     image_classes_.insert(image_class);
370*795d594fSAndroid Build Coastguard Worker   }
371*795d594fSAndroid Build Coastguard Worker   number_of_threads_ = kIsTargetBuild ? 2U : 16U;
372*795d594fSAndroid Build Coastguard Worker   CreateCompilerDriver();
373*795d594fSAndroid Build Coastguard Worker   // Set inline filter values.
374*795d594fSAndroid Build Coastguard Worker   compiler_options_->SetInlineMaxCodeUnits(CompilerOptions::kDefaultInlineMaxCodeUnits);
375*795d594fSAndroid Build Coastguard Worker   compiler_options_->SetMaxImageBlockSize(max_image_block_size);
376*795d594fSAndroid Build Coastguard Worker   image_classes_.clear();
377*795d594fSAndroid Build Coastguard Worker   if (!extra_dex.empty()) {
378*795d594fSAndroid Build Coastguard Worker     helper.extra_dex = extra_dex;
379*795d594fSAndroid Build Coastguard Worker     helper.extra_dex_files = OpenTestDexFiles(extra_dex.c_str());
380*795d594fSAndroid Build Coastguard Worker   }
381*795d594fSAndroid Build Coastguard Worker   DoCompile(storage_mode, helper);
382*795d594fSAndroid Build Coastguard Worker   if (image_classes.begin() != image_classes.end()) {
383*795d594fSAndroid Build Coastguard Worker     // Make sure all explicitly specified classes got initialized.
384*795d594fSAndroid Build Coastguard Worker     ScopedObjectAccess soa(Thread::Current());
385*795d594fSAndroid Build Coastguard Worker     ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
386*795d594fSAndroid Build Coastguard Worker     for (const std::string& image_class : image_classes) {
387*795d594fSAndroid Build Coastguard Worker       ObjPtr<mirror::Class> klass =
388*795d594fSAndroid Build Coastguard Worker           class_linker->LookupClass(Thread::Current(), image_class.c_str(), nullptr);
389*795d594fSAndroid Build Coastguard Worker       if (ContainsElement(image_classes_failing_resolution, image_class)) {
390*795d594fSAndroid Build Coastguard Worker         EXPECT_TRUE(klass == nullptr || klass->IsErroneousUnresolved());
391*795d594fSAndroid Build Coastguard Worker       } else  if (ContainsElement(image_classes_failing_aot_clinit, image_class)) {
392*795d594fSAndroid Build Coastguard Worker         ASSERT_TRUE(klass != nullptr) << image_class;
393*795d594fSAndroid Build Coastguard Worker         EXPECT_FALSE(klass->IsInitialized());
394*795d594fSAndroid Build Coastguard Worker       } else {
395*795d594fSAndroid Build Coastguard Worker         ASSERT_TRUE(klass != nullptr) << image_class;
396*795d594fSAndroid Build Coastguard Worker         EXPECT_TRUE(klass->IsInitialized());
397*795d594fSAndroid Build Coastguard Worker       }
398*795d594fSAndroid Build Coastguard Worker     }
399*795d594fSAndroid Build Coastguard Worker   }
400*795d594fSAndroid Build Coastguard Worker }
401*795d594fSAndroid Build Coastguard Worker 
402*795d594fSAndroid Build Coastguard Worker }  // namespace linker
403*795d594fSAndroid Build Coastguard Worker }  // namespace art
404*795d594fSAndroid Build Coastguard Worker 
405*795d594fSAndroid Build Coastguard Worker #endif  // ART_DEX2OAT_LINKER_IMAGE_TEST_H_
406