• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <vector>
25 
26 #include "android-base/stringprintf.h"
27 
28 #include "art_method-inl.h"
29 #include "base/file_utils.h"
30 #include "base/unix_file/fd_file.h"
31 #include "base/utils.h"
32 #include "class_linker-inl.h"
33 #include "common_compiler_test.h"
34 #include "compiler_callbacks.h"
35 #include "debug/method_debug_info.h"
36 #include "dex/quick_compiler_callbacks.h"
37 #include "driver/compiler_options.h"
38 #include "gc/space/image_space.h"
39 #include "image_writer.h"
40 #include "linker/buffered_output_stream.h"
41 #include "linker/elf_writer.h"
42 #include "linker/elf_writer_quick.h"
43 #include "linker/file_output_stream.h"
44 #include "linker/multi_oat_relative_patcher.h"
45 #include "lock_word.h"
46 #include "mirror/object-inl.h"
47 #include "oat_writer.h"
48 #include "scoped_thread_state_change-inl.h"
49 #include "signal_catcher.h"
50 
51 namespace art {
52 namespace linker {
53 
54 static const uintptr_t kRequestedImageBase = ART_BASE_ADDRESS;
55 
56 struct CompilationHelper {
57   std::vector<std::string> dex_file_locations;
58   std::vector<ScratchFile> image_locations;
59   std::vector<std::unique_ptr<const DexFile>> extra_dex_files;
60   std::vector<ScratchFile> image_files;
61   std::vector<ScratchFile> oat_files;
62   std::vector<ScratchFile> vdex_files;
63   std::string image_dir;
64 
65   void Compile(CompilerDriver* driver,
66                ImageHeader::StorageMode storage_mode);
67 
68   std::vector<size_t> GetImageObjectSectionSizes();
69 
70   ~CompilationHelper();
71 };
72 
73 class ImageTest : public CommonCompilerTest {
74  protected:
SetUp()75   virtual void SetUp() {
76     ReserveImageSpace();
77     CommonCompilerTest::SetUp();
78   }
79 
80   void TestWriteRead(ImageHeader::StorageMode storage_mode);
81 
82   void Compile(ImageHeader::StorageMode storage_mode,
83                CompilationHelper& out_helper,
84                const std::string& extra_dex = "",
85                const std::initializer_list<std::string>& image_classes = {});
86 
SetUpRuntimeOptions(RuntimeOptions * options)87   void SetUpRuntimeOptions(RuntimeOptions* options) OVERRIDE {
88     CommonCompilerTest::SetUpRuntimeOptions(options);
89     QuickCompilerCallbacks* new_callbacks =
90         new QuickCompilerCallbacks(CompilerCallbacks::CallbackMode::kCompileBootImage);
91     new_callbacks->SetVerificationResults(verification_results_.get());
92     callbacks_.reset(new_callbacks);
93     options->push_back(std::make_pair("compilercallbacks", callbacks_.get()));
94   }
95 
GetImageClasses()96   std::unordered_set<std::string>* GetImageClasses() OVERRIDE {
97     return new std::unordered_set<std::string>(image_classes_);
98   }
99 
FindCopiedMethod(ArtMethod * origin,mirror::Class * klass)100   ArtMethod* FindCopiedMethod(ArtMethod* origin, mirror::Class* klass)
101       REQUIRES_SHARED(Locks::mutator_lock_) {
102     PointerSize pointer_size = class_linker_->GetImagePointerSize();
103     for (ArtMethod& m : klass->GetCopiedMethods(pointer_size)) {
104       if (strcmp(origin->GetName(), m.GetName()) == 0 &&
105           origin->GetSignature() == m.GetSignature()) {
106         return &m;
107       }
108     }
109     return nullptr;
110   }
111 
112  private:
113   std::unordered_set<std::string> image_classes_;
114 };
115 
~CompilationHelper()116 inline CompilationHelper::~CompilationHelper() {
117   for (ScratchFile& image_file : image_files) {
118     image_file.Unlink();
119   }
120   for (ScratchFile& oat_file : oat_files) {
121     oat_file.Unlink();
122   }
123   for (ScratchFile& vdex_file : vdex_files) {
124     vdex_file.Unlink();
125   }
126   const int rmdir_result = rmdir(image_dir.c_str());
127   CHECK_EQ(0, rmdir_result);
128 }
129 
GetImageObjectSectionSizes()130 inline std::vector<size_t> CompilationHelper::GetImageObjectSectionSizes() {
131   std::vector<size_t> ret;
132   for (ScratchFile& image_file : image_files) {
133     std::unique_ptr<File> file(OS::OpenFileForReading(image_file.GetFilename().c_str()));
134     CHECK(file.get() != nullptr);
135     ImageHeader image_header;
136     CHECK_EQ(file->ReadFully(&image_header, sizeof(image_header)), true);
137     CHECK(image_header.IsValid());
138     ret.push_back(image_header.GetObjectsSection().Size());
139   }
140   return ret;
141 }
142 
Compile(CompilerDriver * driver,ImageHeader::StorageMode storage_mode)143 inline void CompilationHelper::Compile(CompilerDriver* driver,
144                                        ImageHeader::StorageMode storage_mode) {
145   ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
146   std::vector<const DexFile*> class_path = class_linker->GetBootClassPath();
147 
148   for (const std::unique_ptr<const DexFile>& dex_file : extra_dex_files) {
149     {
150       ScopedObjectAccess soa(Thread::Current());
151       // Inject in boot class path so that the compiler driver can see it.
152       class_linker->AppendToBootClassPath(soa.Self(), *dex_file.get());
153     }
154     class_path.push_back(dex_file.get());
155   }
156 
157   // Enable write for dex2dex.
158   for (const DexFile* dex_file : class_path) {
159     dex_file_locations.push_back(dex_file->GetLocation());
160     if (dex_file->IsReadOnly()) {
161       dex_file->EnableWrite();
162     }
163   }
164   {
165     // Create a generic tmp file, to be the base of the .art and .oat temporary files.
166     ScratchFile location;
167     for (int i = 0; i < static_cast<int>(class_path.size()); ++i) {
168       std::string cur_location =
169           android::base::StringPrintf("%s-%d.art", location.GetFilename().c_str(), i);
170       image_locations.push_back(ScratchFile(cur_location));
171     }
172   }
173   std::vector<std::string> image_filenames;
174   for (ScratchFile& file : image_locations) {
175     std::string image_filename(GetSystemImageFilename(file.GetFilename().c_str(), kRuntimeISA));
176     image_filenames.push_back(image_filename);
177     size_t pos = image_filename.rfind('/');
178     CHECK_NE(pos, std::string::npos) << image_filename;
179     if (image_dir.empty()) {
180       image_dir = image_filename.substr(0, pos);
181       int mkdir_result = mkdir(image_dir.c_str(), 0700);
182       CHECK_EQ(0, mkdir_result) << image_dir;
183     }
184     image_files.push_back(ScratchFile(OS::CreateEmptyFile(image_filename.c_str())));
185   }
186 
187   std::vector<std::string> oat_filenames;
188   std::vector<std::string> vdex_filenames;
189   for (const std::string& image_filename : image_filenames) {
190     std::string oat_filename = ReplaceFileExtension(image_filename, "oat");
191     oat_files.push_back(ScratchFile(OS::CreateEmptyFile(oat_filename.c_str())));
192     oat_filenames.push_back(oat_filename);
193     std::string vdex_filename = ReplaceFileExtension(image_filename, "vdex");
194     vdex_files.push_back(ScratchFile(OS::CreateEmptyFile(vdex_filename.c_str())));
195     vdex_filenames.push_back(vdex_filename);
196   }
197 
198   std::unordered_map<const DexFile*, size_t> dex_file_to_oat_index_map;
199   std::vector<const char*> oat_filename_vector;
200   for (const std::string& file : oat_filenames) {
201     oat_filename_vector.push_back(file.c_str());
202   }
203   std::vector<const char*> image_filename_vector;
204   for (const std::string& file : image_filenames) {
205     image_filename_vector.push_back(file.c_str());
206   }
207   size_t image_idx = 0;
208   for (const DexFile* dex_file : class_path) {
209     dex_file_to_oat_index_map.emplace(dex_file, image_idx);
210     ++image_idx;
211   }
212   // TODO: compile_pic should be a test argument.
213   std::unique_ptr<ImageWriter> writer(new ImageWriter(*driver,
214                                                       kRequestedImageBase,
215                                                       /*compile_pic*/false,
216                                                       /*compile_app_image*/false,
217                                                       storage_mode,
218                                                       oat_filename_vector,
219                                                       dex_file_to_oat_index_map,
220                                                       /*dirty_image_objects*/nullptr));
221   {
222     {
223       jobject class_loader = nullptr;
224       TimingLogger timings("ImageTest::WriteRead", false, false);
225       TimingLogger::ScopedTiming t("CompileAll", &timings);
226       driver->SetDexFilesForOatFile(class_path);
227       driver->CompileAll(class_loader, class_path, &timings);
228 
229       t.NewTiming("WriteElf");
230       SafeMap<std::string, std::string> key_value_store;
231       std::vector<const char*> dex_filename_vector;
232       for (size_t i = 0; i < class_path.size(); ++i) {
233         dex_filename_vector.push_back("");
234       }
235       key_value_store.Put(OatHeader::kBootClassPathKey,
236                           gc::space::ImageSpace::GetMultiImageBootClassPath(
237                               dex_filename_vector,
238                               oat_filename_vector,
239                               image_filename_vector));
240 
241       std::vector<std::unique_ptr<ElfWriter>> elf_writers;
242       std::vector<std::unique_ptr<OatWriter>> oat_writers;
243       for (ScratchFile& oat_file : oat_files) {
244         elf_writers.emplace_back(CreateElfWriterQuick(driver->GetInstructionSet(),
245                                                       driver->GetInstructionSetFeatures(),
246                                                       &driver->GetCompilerOptions(),
247                                                       oat_file.GetFile()));
248         elf_writers.back()->Start();
249         oat_writers.emplace_back(new OatWriter(/*compiling_boot_image*/true,
250                                                &timings,
251                                                /*profile_compilation_info*/nullptr,
252                                                CompactDexLevel::kCompactDexLevelNone));
253       }
254 
255       std::vector<OutputStream*> rodata;
256       std::vector<std::unique_ptr<MemMap>> opened_dex_files_maps;
257       std::vector<std::unique_ptr<const DexFile>> opened_dex_files;
258       // Now that we have finalized key_value_store_, start writing the oat file.
259       for (size_t i = 0, size = oat_writers.size(); i != size; ++i) {
260         const DexFile* dex_file = class_path[i];
261         rodata.push_back(elf_writers[i]->StartRoData());
262         ArrayRef<const uint8_t> raw_dex_file(
263             reinterpret_cast<const uint8_t*>(&dex_file->GetHeader()),
264             dex_file->GetHeader().file_size_);
265         oat_writers[i]->AddRawDexFileSource(raw_dex_file,
266                                             dex_file->GetLocation().c_str(),
267                                             dex_file->GetLocationChecksum());
268 
269         std::vector<std::unique_ptr<MemMap>> cur_opened_dex_files_maps;
270         std::vector<std::unique_ptr<const DexFile>> cur_opened_dex_files;
271         bool dex_files_ok = oat_writers[i]->WriteAndOpenDexFiles(
272             vdex_files[i].GetFile(),
273             rodata.back(),
274             driver->GetInstructionSet(),
275             driver->GetInstructionSetFeatures(),
276             &key_value_store,
277             /* verify */ false,           // Dex files may be dex-to-dex-ed, don't verify.
278             /* update_input_vdex */ false,
279             /* copy_dex_files */ CopyOption::kOnlyIfCompressed,
280             &cur_opened_dex_files_maps,
281             &cur_opened_dex_files);
282         ASSERT_TRUE(dex_files_ok);
283 
284         if (!cur_opened_dex_files_maps.empty()) {
285           for (std::unique_ptr<MemMap>& cur_map : cur_opened_dex_files_maps) {
286             opened_dex_files_maps.push_back(std::move(cur_map));
287           }
288           for (std::unique_ptr<const DexFile>& cur_dex_file : cur_opened_dex_files) {
289             // dex_file_oat_index_map_.emplace(dex_file.get(), i);
290             opened_dex_files.push_back(std::move(cur_dex_file));
291           }
292         } else {
293           ASSERT_TRUE(cur_opened_dex_files.empty());
294         }
295       }
296       bool image_space_ok = writer->PrepareImageAddressSpace();
297       ASSERT_TRUE(image_space_ok);
298 
299       DCHECK_EQ(vdex_files.size(), oat_files.size());
300       for (size_t i = 0, size = oat_files.size(); i != size; ++i) {
301         MultiOatRelativePatcher patcher(driver->GetInstructionSet(),
302                                         driver->GetInstructionSetFeatures());
303         OatWriter* const oat_writer = oat_writers[i].get();
304         ElfWriter* const elf_writer = elf_writers[i].get();
305         std::vector<const DexFile*> cur_dex_files(1u, class_path[i]);
306         oat_writer->Initialize(driver, writer.get(), cur_dex_files);
307 
308         std::unique_ptr<BufferedOutputStream> vdex_out =
309             std::make_unique<BufferedOutputStream>(
310                 std::make_unique<FileOutputStream>(vdex_files[i].GetFile()));
311         oat_writer->WriteVerifierDeps(vdex_out.get(), nullptr);
312         oat_writer->WriteQuickeningInfo(vdex_out.get());
313         oat_writer->WriteChecksumsAndVdexHeader(vdex_out.get());
314 
315         oat_writer->PrepareLayout(&patcher);
316         size_t rodata_size = oat_writer->GetOatHeader().GetExecutableOffset();
317         size_t text_size = oat_writer->GetOatSize() - rodata_size;
318         elf_writer->PrepareDynamicSection(rodata_size,
319                                           text_size,
320                                           oat_writer->GetBssSize(),
321                                           oat_writer->GetBssMethodsOffset(),
322                                           oat_writer->GetBssRootsOffset(),
323                                           oat_writer->GetVdexSize());
324 
325         writer->UpdateOatFileLayout(i,
326                                     elf_writer->GetLoadedSize(),
327                                     oat_writer->GetOatDataOffset(),
328                                     oat_writer->GetOatSize());
329 
330         bool rodata_ok = oat_writer->WriteRodata(rodata[i]);
331         ASSERT_TRUE(rodata_ok);
332         elf_writer->EndRoData(rodata[i]);
333 
334         OutputStream* text = elf_writer->StartText();
335         bool text_ok = oat_writer->WriteCode(text);
336         ASSERT_TRUE(text_ok);
337         elf_writer->EndText(text);
338 
339         bool header_ok = oat_writer->WriteHeader(elf_writer->GetStream(), 0u, 0u, 0u);
340         ASSERT_TRUE(header_ok);
341 
342         writer->UpdateOatFileHeader(i, oat_writer->GetOatHeader());
343 
344         elf_writer->WriteDynamicSection();
345         elf_writer->WriteDebugInfo(oat_writer->GetDebugInfo());
346 
347         bool success = elf_writer->End();
348         ASSERT_TRUE(success);
349       }
350     }
351 
352     bool success_image = writer->Write(kInvalidFd,
353                                        image_filename_vector,
354                                        oat_filename_vector);
355     ASSERT_TRUE(success_image);
356 
357     for (size_t i = 0, size = oat_filenames.size(); i != size; ++i) {
358       const char* oat_filename = oat_filenames[i].c_str();
359       std::unique_ptr<File> oat_file(OS::OpenFileReadWrite(oat_filename));
360       ASSERT_TRUE(oat_file != nullptr);
361       bool success_fixup = ElfWriter::Fixup(oat_file.get(),
362                                             writer->GetOatDataBegin(i));
363       ASSERT_TRUE(success_fixup);
364       ASSERT_EQ(oat_file->FlushCloseOrErase(), 0) << "Could not flush and close oat file "
365                                                   << oat_filename;
366     }
367   }
368 }
369 
Compile(ImageHeader::StorageMode storage_mode,CompilationHelper & helper,const std::string & extra_dex,const std::initializer_list<std::string> & image_classes)370 inline void ImageTest::Compile(ImageHeader::StorageMode storage_mode,
371                         CompilationHelper& helper,
372                         const std::string& extra_dex,
373                         const std::initializer_list<std::string>& image_classes) {
374   for (const std::string& image_class : image_classes) {
375     image_classes_.insert(image_class);
376   }
377   CreateCompilerDriver(Compiler::kOptimizing, kRuntimeISA, kIsTargetBuild ? 2U : 16U);
378   // Set inline filter values.
379   compiler_options_->SetInlineMaxCodeUnits(CompilerOptions::kDefaultInlineMaxCodeUnits);
380   image_classes_.clear();
381   if (!extra_dex.empty()) {
382     helper.extra_dex_files = OpenTestDexFiles(extra_dex.c_str());
383   }
384   helper.Compile(compiler_driver_.get(), storage_mode);
385   if (image_classes.begin() != image_classes.end()) {
386     // Make sure the class got initialized.
387     ScopedObjectAccess soa(Thread::Current());
388     ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
389     for (const std::string& image_class : image_classes) {
390       mirror::Class* klass = class_linker->FindSystemClass(Thread::Current(), image_class.c_str());
391       EXPECT_TRUE(klass != nullptr);
392       EXPECT_TRUE(klass->IsInitialized());
393     }
394   }
395 }
396 
TestWriteRead(ImageHeader::StorageMode storage_mode)397 inline void ImageTest::TestWriteRead(ImageHeader::StorageMode storage_mode) {
398   CompilationHelper helper;
399   Compile(storage_mode, /*out*/ helper);
400   std::vector<uint64_t> image_file_sizes;
401   for (ScratchFile& image_file : helper.image_files) {
402     std::unique_ptr<File> file(OS::OpenFileForReading(image_file.GetFilename().c_str()));
403     ASSERT_TRUE(file.get() != nullptr);
404     ImageHeader image_header;
405     ASSERT_EQ(file->ReadFully(&image_header, sizeof(image_header)), true);
406     ASSERT_TRUE(image_header.IsValid());
407     const auto& bitmap_section = image_header.GetImageBitmapSection();
408     ASSERT_GE(bitmap_section.Offset(), sizeof(image_header));
409     ASSERT_NE(0U, bitmap_section.Size());
410 
411     gc::Heap* heap = Runtime::Current()->GetHeap();
412     ASSERT_TRUE(heap->HaveContinuousSpaces());
413     gc::space::ContinuousSpace* space = heap->GetNonMovingSpace();
414     ASSERT_FALSE(space->IsImageSpace());
415     ASSERT_TRUE(space != nullptr);
416     ASSERT_TRUE(space->IsMallocSpace());
417     image_file_sizes.push_back(file->GetLength());
418   }
419 
420   ASSERT_TRUE(compiler_driver_->GetImageClasses() != nullptr);
421   std::unordered_set<std::string> image_classes(*compiler_driver_->GetImageClasses());
422 
423   // Need to delete the compiler since it has worker threads which are attached to runtime.
424   compiler_driver_.reset();
425 
426   // Tear down old runtime before making a new one, clearing out misc state.
427 
428   // Remove the reservation of the memory for use to load the image.
429   // Need to do this before we reset the runtime.
430   UnreserveImageSpace();
431 
432   helper.extra_dex_files.clear();
433   runtime_.reset();
434   java_lang_dex_file_ = nullptr;
435 
436   MemMap::Init();
437 
438   RuntimeOptions options;
439   std::string image("-Ximage:");
440   image.append(helper.image_locations[0].GetFilename());
441   options.push_back(std::make_pair(image.c_str(), static_cast<void*>(nullptr)));
442   // By default the compiler this creates will not include patch information.
443   options.push_back(std::make_pair("-Xnorelocate", nullptr));
444 
445   if (!Runtime::Create(options, false)) {
446     LOG(FATAL) << "Failed to create runtime";
447     return;
448   }
449   runtime_.reset(Runtime::Current());
450   // Runtime::Create acquired the mutator_lock_ that is normally given away when we Runtime::Start,
451   // give it away now and then switch to a more managable ScopedObjectAccess.
452   Thread::Current()->TransitionFromRunnableToSuspended(kNative);
453   ScopedObjectAccess soa(Thread::Current());
454   ASSERT_TRUE(runtime_.get() != nullptr);
455   class_linker_ = runtime_->GetClassLinker();
456 
457   gc::Heap* heap = Runtime::Current()->GetHeap();
458   ASSERT_TRUE(heap->HasBootImageSpace());
459   ASSERT_TRUE(heap->GetNonMovingSpace()->IsMallocSpace());
460 
461   // We loaded the runtime with an explicit image, so it must exist.
462   ASSERT_EQ(heap->GetBootImageSpaces().size(), image_file_sizes.size());
463   for (size_t i = 0; i < helper.dex_file_locations.size(); ++i) {
464     std::unique_ptr<const DexFile> dex(
465         LoadExpectSingleDexFile(helper.dex_file_locations[i].c_str()));
466     ASSERT_TRUE(dex != nullptr);
467     uint64_t image_file_size = image_file_sizes[i];
468     gc::space::ImageSpace* image_space = heap->GetBootImageSpaces()[i];
469     ASSERT_TRUE(image_space != nullptr);
470     if (storage_mode == ImageHeader::kStorageModeUncompressed) {
471       // Uncompressed, image should be smaller than file.
472       ASSERT_LE(image_space->GetImageHeader().GetImageSize(), image_file_size);
473     } else if (image_file_size > 16 * KB) {
474       // Compressed, file should be smaller than image. Not really valid for small images.
475       ASSERT_LE(image_file_size, image_space->GetImageHeader().GetImageSize());
476     }
477 
478     image_space->VerifyImageAllocations();
479     uint8_t* image_begin = image_space->Begin();
480     uint8_t* image_end = image_space->End();
481     if (i == 0) {
482       // This check is only valid for image 0.
483       CHECK_EQ(kRequestedImageBase, reinterpret_cast<uintptr_t>(image_begin));
484     }
485     for (size_t j = 0; j < dex->NumClassDefs(); ++j) {
486       const DexFile::ClassDef& class_def = dex->GetClassDef(j);
487       const char* descriptor = dex->GetClassDescriptor(class_def);
488       mirror::Class* klass = class_linker_->FindSystemClass(soa.Self(), descriptor);
489       EXPECT_TRUE(klass != nullptr) << descriptor;
490       if (image_classes.find(descriptor) == image_classes.end()) {
491         EXPECT_TRUE(reinterpret_cast<uint8_t*>(klass) >= image_end ||
492                     reinterpret_cast<uint8_t*>(klass) < image_begin) << descriptor;
493       } else {
494         // Image classes should be located inside the image.
495         EXPECT_LT(image_begin, reinterpret_cast<uint8_t*>(klass)) << descriptor;
496         EXPECT_LT(reinterpret_cast<uint8_t*>(klass), image_end) << descriptor;
497       }
498       EXPECT_TRUE(Monitor::IsValidLockWord(klass->GetLockWord(false)));
499     }
500   }
501 }
502 
503 }  // namespace linker
504 }  // namespace art
505 
506 #endif  // ART_DEX2OAT_LINKER_IMAGE_TEST_H_
507