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