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_WRITER_H_
18 #define ART_DEX2OAT_LINKER_IMAGE_WRITER_H_
19 
20 #include <stdint.h>
21 #include "base/memory_tool.h"
22 
23 #include <cstddef>
24 #include <memory>
25 #include <ostream>
26 #include <set>
27 #include <stack>
28 #include <string>
29 #include <unordered_map>
30 #include <unordered_set>
31 
32 #include "art_method.h"
33 #include "base/bit_utils.h"
34 #include "base/dchecked_vector.h"
35 #include "base/enums.h"
36 #include "base/hash_set.h"
37 #include "base/length_prefixed_array.h"
38 #include "base/macros.h"
39 #include "base/mem_map.h"
40 #include "base/os.h"
41 #include "base/safe_map.h"
42 #include "base/utils.h"
43 #include "class_table.h"
44 #include "gc/accounting/space_bitmap.h"
45 #include "image.h"
46 #include "intern_table.h"
47 #include "lock_word.h"
48 #include "mirror/dex_cache.h"
49 #include "oat_file.h"
50 #include "obj_ptr.h"
51 
52 namespace art {
53 namespace gc {
54 namespace accounting {
55 template <size_t kAlignment> class SpaceBitmap;
56 typedef SpaceBitmap<kObjectAlignment> ContinuousSpaceBitmap;
57 }  // namespace accounting
58 namespace space {
59 class ImageSpace;
60 }  // namespace space
61 }  // namespace gc
62 
63 namespace mirror {
64 class ClassLoader;
65 }  // namespace mirror
66 
67 class ClassLoaderVisitor;
68 class CompilerOptions;
69 template<class T> class Handle;
70 class ImTable;
71 class ImtConflictTable;
72 class TimingLogger;
73 
74 static constexpr int kInvalidFd = -1;
75 
76 namespace linker {
77 
78 // Write a Space built during compilation for use during execution.
79 class ImageWriter final {
80  public:
81   ImageWriter(const CompilerOptions& compiler_options,
82               uintptr_t image_begin,
83               ImageHeader::StorageMode image_storage_mode,
84               const std::vector<std::string>& oat_filenames,
85               const std::unordered_map<const DexFile*, size_t>& dex_file_oat_index_map,
86               jobject class_loader,
87               const HashSet<std::string>* dirty_image_objects);
88 
89   /*
90    * Modifies the heap and collects information about objects and code so that
91    * they can be written to the boot or app image later.
92    *
93    * First, unneeded classes are removed from the managed heap.  Next, we
94    * remove cached values and calculate necessary metadata for later in the
95    * process. Optionally some debugging information is collected and used to
96    * verify the state of the heap at this point.  Next, metadata from earlier
97    * is used to calculate offsets of references to strings to speed up string
98    * interning when the image is loaded.  Lastly, we allocate enough memory to
99    * fit all image data minus the bitmap and relocation sections.
100    *
101    * This function should only be called when all objects to be included in the
102    * image have been initialized and all native methods have been generated.  In
103    * addition, no other thread should be modifying the heap.
104    */
105   bool PrepareImageAddressSpace(bool preload_dex_caches, TimingLogger* timings);
106 
IsImageAddressSpaceReady()107   bool IsImageAddressSpaceReady() const {
108     DCHECK(!image_infos_.empty());
109     for (const ImageInfo& image_info : image_infos_) {
110       if (image_info.image_roots_address_ == 0u) {
111         return false;
112       }
113     }
114     return true;
115   }
116 
117   ObjPtr<mirror::ClassLoader> GetAppClassLoader() const REQUIRES_SHARED(Locks::mutator_lock_);
118 
119   template <typename T>
GetImageAddress(T * object)120   T* GetImageAddress(T* object) const REQUIRES_SHARED(Locks::mutator_lock_) {
121     if (object == nullptr || IsInBootImage(object)) {
122       return object;
123     } else {
124       size_t oat_index = GetOatIndex(object);
125       const ImageInfo& image_info = GetImageInfo(oat_index);
126       return reinterpret_cast<T*>(image_info.image_begin_ + GetImageOffset(object, oat_index));
127     }
128   }
129 
130   ArtMethod* GetImageMethodAddress(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_);
131   const void* GetIntrinsicReferenceAddress(uint32_t intrinsic_data)
132       REQUIRES_SHARED(Locks::mutator_lock_);
133 
GetOatFileOffset(size_t oat_index)134   size_t GetOatFileOffset(size_t oat_index) const {
135     return GetImageInfo(oat_index).oat_offset_;
136   }
137 
GetOatFileBegin(size_t oat_index)138   const uint8_t* GetOatFileBegin(size_t oat_index) const {
139     return GetImageInfo(oat_index).oat_file_begin_;
140   }
141 
142   // If image_fd is not kInvalidFd, then we use that for the image file. Otherwise we open
143   // the names in image_filenames.
144   // If oat_fd is not kInvalidFd, then we use that for the oat file. Otherwise we open
145   // the names in oat_filenames.
146   bool Write(int image_fd,
147              const std::vector<std::string>& image_filenames,
148              size_t component_count)
149       REQUIRES(!Locks::mutator_lock_);
150 
GetOatDataBegin(size_t oat_index)151   uintptr_t GetOatDataBegin(size_t oat_index) {
152     return reinterpret_cast<uintptr_t>(GetImageInfo(oat_index).oat_data_begin_);
153   }
154 
155   // Get the index of the oat file containing the dex file.
156   //
157   // This "oat_index" is used to retrieve information about the the memory layout
158   // of the oat file and its associated image file, needed for link-time patching
159   // of references to the image or across oat files.
160   size_t GetOatIndexForDexFile(const DexFile* dex_file) const;
161 
162   // Get the index of the oat file containing the definition of the class.
163   size_t GetOatIndexForClass(ObjPtr<mirror::Class> klass) const
164       REQUIRES_SHARED(Locks::mutator_lock_);
165 
166   // Update the oat layout for the given oat file.
167   // This will make the oat_offset for the next oat file valid.
168   void UpdateOatFileLayout(size_t oat_index,
169                            size_t oat_loaded_size,
170                            size_t oat_data_offset,
171                            size_t oat_data_size);
172   // Update information about the oat header, i.e. checksum and trampoline offsets.
173   void UpdateOatFileHeader(size_t oat_index, const OatHeader& oat_header);
174 
175  private:
176   bool AllocMemory();
177 
178   // Mark the objects defined in this space in the given live bitmap.
179   void RecordImageAllocations() REQUIRES_SHARED(Locks::mutator_lock_);
180 
181   // Classify different kinds of bins that objects end up getting packed into during image writing.
182   // Ordered from dirtiest to cleanest (until ArtMethods).
183   enum class Bin {
184     kKnownDirty,                  // Known dirty objects from --dirty-image-objects list
185     kMiscDirty,                   // Dex caches, object locks, etc...
186     kClassVerified,               // Class verified, but initializers haven't been run
187     // Unknown mix of clean/dirty:
188     kRegular,
189     kClassInitialized,            // Class initializers have been run
190     // All classes get their own bins since their fields often dirty
191     kClassInitializedFinalStatics,  // Class initializers have been run, no non-final statics
192     // Likely-clean:
193     kString,                      // [String] Almost always immutable (except for obj header).
194     // Add more bins here if we add more segregation code.
195     // Non mirror fields must be below.
196     // ArtFields should be always clean.
197     kArtField,
198     // If the class is initialized, then the ArtMethods are probably clean.
199     kArtMethodClean,
200     // ArtMethods may be dirty if the class has native methods or a declaring class that isn't
201     // initialized.
202     kArtMethodDirty,
203     // IMT (clean)
204     kImTable,
205     // Conflict tables (clean).
206     kIMTConflictTable,
207     // Runtime methods (always clean, do not have a length prefix array).
208     kRuntimeMethod,
209     // Metadata bin for data that is temporary during image lifetime.
210     kMetadata,
211     // Dex cache arrays have a special slot for PC-relative addressing. Since they are
212     // huge, and as such their dirtiness is not important for the clean/dirty separation,
213     // we arbitrarily keep them at the end of the native data.
214     kDexCacheArray,               // Arrays belonging to dex cache.
215     kLast = kDexCacheArray,
216     // Number of bins which are for mirror objects.
217     kMirrorCount = kArtField,
218   };
219   friend std::ostream& operator<<(std::ostream& stream, Bin bin);
220 
221   enum class NativeObjectRelocationType {
222     kArtField,
223     kArtFieldArray,
224     kArtMethodClean,
225     kArtMethodArrayClean,
226     kArtMethodDirty,
227     kArtMethodArrayDirty,
228     kGcRootPointer,
229     kRuntimeMethod,
230     kIMTable,
231     kIMTConflictTable,
232     kDexCacheArray,
233   };
234   friend std::ostream& operator<<(std::ostream& stream, NativeObjectRelocationType type);
235 
236   enum class StubType {
237     kJNIDlsymLookupTrampoline,
238     kJNIDlsymLookupCriticalTrampoline,
239     kQuickGenericJNITrampoline,
240     kQuickIMTConflictTrampoline,
241     kQuickResolutionTrampoline,
242     kQuickToInterpreterBridge,
243     kLast = kQuickToInterpreterBridge,
244   };
245   friend std::ostream& operator<<(std::ostream& stream, StubType stub_type);
246 
247   static constexpr size_t kBinBits =
248       MinimumBitsToStore<uint32_t>(static_cast<size_t>(Bin::kMirrorCount) - 1);
249   // uint32 = typeof(lockword_)
250   // Subtract read barrier bits since we want these to remain 0, or else it may result in DCHECK
251   // failures due to invalid read barrier bits during object field reads.
252   static const size_t kBinShift = BitSizeOf<uint32_t>() - kBinBits - LockWord::kGCStateSize;
253   // 111000.....0
254   static const size_t kBinMask = ((static_cast<size_t>(1) << kBinBits) - 1) << kBinShift;
255 
256   // Number of bins, including non-mirror bins.
257   static constexpr size_t kNumberOfBins = static_cast<size_t>(Bin::kLast) + 1u;
258 
259   // Number of stub types.
260   static constexpr size_t kNumberOfStubTypes = static_cast<size_t>(StubType::kLast) + 1u;
261 
262   // We use the lock word to store the bin # and bin index of the object in the image.
263   //
264   // The struct size must be exactly sizeof(LockWord), currently 32-bits, since this will end up
265   // stored in the lock word bit-for-bit when object forwarding addresses are being calculated.
266   struct BinSlot {
267     explicit BinSlot(uint32_t lockword);
268     BinSlot(Bin bin, uint32_t index);
269 
270     // The bin an object belongs to, i.e. regular, class/verified, class/initialized, etc.
271     Bin GetBin() const;
272     // The offset in bytes from the beginning of the bin. Aligned to object size.
273     uint32_t GetOffset() const;
274     // Pack into a single uint32_t, for storing into a lock word.
Uint32ValueBinSlot275     uint32_t Uint32Value() const { return lockword_; }
276     // Comparison operator for map support
277     bool operator<(const BinSlot& other) const  { return lockword_ < other.lockword_; }
278 
279    private:
280     // Must be the same size as LockWord, any larger and we would truncate the data.
281     uint32_t lockword_;
282   };
283 
284   struct ImageInfo {
285     ImageInfo();
286     ImageInfo(ImageInfo&&) = default;
287 
288     /*
289      * Creates ImageSection objects that describe most of the sections of a
290      * boot or AppImage. The following sections are not included:
291      *   - ImageHeader::kSectionImageBitmap
292      *
293      * In addition, the ImageHeader is not covered here.
294      *
295      * This function will return the total size of the covered sections as well
296      * as a vector containing the individual ImageSection objects.
297      */
298     std::pair<size_t, std::vector<ImageSection>> CreateImageSections() const;
299 
GetStubOffsetImageInfo300     size_t GetStubOffset(StubType stub_type) const {
301       DCHECK_LT(static_cast<size_t>(stub_type), kNumberOfStubTypes);
302       return stub_offsets_[static_cast<size_t>(stub_type)];
303     }
304 
SetStubOffsetImageInfo305     void SetStubOffset(StubType stub_type, size_t offset) {
306       DCHECK_LT(static_cast<size_t>(stub_type), kNumberOfStubTypes);
307       stub_offsets_[static_cast<size_t>(stub_type)] = offset;
308     }
309 
GetBinSlotOffsetImageInfo310     size_t GetBinSlotOffset(Bin bin) const {
311       DCHECK_LT(static_cast<size_t>(bin), kNumberOfBins);
312       return bin_slot_offsets_[static_cast<size_t>(bin)];
313     }
314 
IncrementBinSlotSizeImageInfo315     void IncrementBinSlotSize(Bin bin, size_t size_to_add) {
316       DCHECK_LT(static_cast<size_t>(bin), kNumberOfBins);
317       bin_slot_sizes_[static_cast<size_t>(bin)] += size_to_add;
318     }
319 
GetBinSlotSizeImageInfo320     size_t GetBinSlotSize(Bin bin) const {
321       DCHECK_LT(static_cast<size_t>(bin), kNumberOfBins);
322       return bin_slot_sizes_[static_cast<size_t>(bin)];
323     }
324 
IncrementBinSlotCountImageInfo325     void IncrementBinSlotCount(Bin bin, size_t count_to_add) {
326       DCHECK_LT(static_cast<size_t>(bin), kNumberOfBins);
327       bin_slot_count_[static_cast<size_t>(bin)] += count_to_add;
328     }
329 
330     // Calculate the sum total of the bin slot sizes in [0, up_to). Defaults to all bins.
331     size_t GetBinSizeSum(Bin up_to) const;
332 
333     MemMap image_;  // Memory mapped for generating the image.
334 
335     // Target begin of this image. Notes: It is not valid to write here, this is the address
336     // of the target image, not necessarily where image_ is mapped. The address is only valid
337     // after layouting (otherwise null).
338     uint8_t* image_begin_ = nullptr;
339 
340     // Offset to the free space in image_, initially size of image header.
341     size_t image_end_ = RoundUp(sizeof(ImageHeader), kObjectAlignment);
342     uint32_t image_roots_address_ = 0;  // The image roots address in the image.
343     size_t image_offset_ = 0;  // Offset of this image from the start of the first image.
344 
345     // Image size is the *address space* covered by this image. As the live bitmap is aligned
346     // to the page size, the live bitmap will cover more address space than necessary. But live
347     // bitmaps may not overlap, so an image has a "shadow," which is accounted for in the size.
348     // The next image may only start at image_begin_ + image_size_ (which is guaranteed to be
349     // page-aligned).
350     size_t image_size_ = 0;
351 
352     // Oat data.
353     // Offset of the oat file for this image from start of oat files. This is
354     // valid when the previous oat file has been written.
355     size_t oat_offset_ = 0;
356     // Layout of the loaded ELF file containing the oat file, valid after UpdateOatFileLayout().
357     const uint8_t* oat_file_begin_ = nullptr;
358     size_t oat_loaded_size_ = 0;
359     const uint8_t* oat_data_begin_ = nullptr;
360     size_t oat_size_ = 0;  // Size of the corresponding oat data.
361     // The oat header checksum, valid after UpdateOatFileHeader().
362     uint32_t oat_checksum_ = 0u;
363 
364     // Image bitmap which lets us know where the objects inside of the image reside.
365     gc::accounting::ContinuousSpaceBitmap image_bitmap_;
366 
367     // The start offsets of the dex cache arrays.
368     SafeMap<const DexFile*, size_t> dex_cache_array_starts_;
369 
370     // Offset from oat_data_begin_ to the stubs.
371     uint32_t stub_offsets_[kNumberOfStubTypes] = {};
372 
373     // Bin slot tracking for dirty object packing.
374     size_t bin_slot_sizes_[kNumberOfBins] = {};  // Number of bytes in a bin.
375     size_t bin_slot_offsets_[kNumberOfBins] = {};  // Number of bytes in previous bins.
376     size_t bin_slot_count_[kNumberOfBins] = {};  // Number of objects in a bin.
377 
378     // Cached size of the intern table for when we allocate memory.
379     size_t intern_table_bytes_ = 0;
380 
381     // Number of image class table bytes.
382     size_t class_table_bytes_ = 0;
383 
384     // Number of object fixup bytes.
385     size_t object_fixup_bytes_ = 0;
386 
387     // Number of pointer fixup bytes.
388     size_t pointer_fixup_bytes_ = 0;
389 
390     // Number of offsets to string references that will be written to the
391     // StringFieldOffsets section.
392     size_t num_string_references_ = 0;
393 
394     // Offsets into the image that indicate where string references are recorded.
395     std::vector<AppImageReferenceOffsetInfo> string_reference_offsets_;
396 
397     // Intern table associated with this image for serialization.
398     std::unique_ptr<InternTable> intern_table_;
399 
400     // Class table associated with this image for serialization.
401     std::unique_ptr<ClassTable> class_table_;
402 
403     // Padding offsets to ensure region alignment (if required).
404     // Objects need to be added from the recorded offset until the end of the region.
405     std::vector<size_t> padding_offsets_;
406   };
407 
408   // We use the lock word to store the offset of the object in the image.
409   size_t GetImageOffset(mirror::Object* object, size_t oat_index) const
410       REQUIRES_SHARED(Locks::mutator_lock_);
411 
412   void PrepareDexCacheArraySlots() REQUIRES_SHARED(Locks::mutator_lock_);
413   Bin AssignImageBinSlot(mirror::Object* object, size_t oat_index)
414       REQUIRES_SHARED(Locks::mutator_lock_);
415   void RecordNativeRelocations(ObjPtr<mirror::Object> obj, size_t oat_index)
416       REQUIRES_SHARED(Locks::mutator_lock_);
417   void SetImageBinSlot(mirror::Object* object, BinSlot bin_slot)
418       REQUIRES_SHARED(Locks::mutator_lock_);
419   bool IsImageBinSlotAssigned(mirror::Object* object) const
420       REQUIRES_SHARED(Locks::mutator_lock_);
421   BinSlot GetImageBinSlot(mirror::Object* object, size_t oat_index) const
422       REQUIRES_SHARED(Locks::mutator_lock_);
423   void UpdateImageBinSlotOffset(mirror::Object* object, size_t oat_index, size_t new_offset)
424       REQUIRES_SHARED(Locks::mutator_lock_);
425 
426   void AddDexCacheArrayRelocation(void* array, size_t offset, size_t oat_index)
427       REQUIRES_SHARED(Locks::mutator_lock_);
428   void AddMethodPointerArray(ObjPtr<mirror::PointerArray> arr)
429       REQUIRES_SHARED(Locks::mutator_lock_);
430 
431   // Returns the address in the boot image if we are compiling the app image.
432   const uint8_t* GetOatAddress(StubType type) const;
433 
GetOatAddressForOffset(uint32_t offset,const ImageInfo & image_info)434   const uint8_t* GetOatAddressForOffset(uint32_t offset, const ImageInfo& image_info) const {
435     // With Quick, code is within the OatFile, as there are all in one
436     // .o ELF object. But interpret it as signed.
437     DCHECK_LE(static_cast<int32_t>(offset), static_cast<int32_t>(image_info.oat_size_));
438     DCHECK(image_info.oat_data_begin_ != nullptr);
439     return offset == 0u ? nullptr : image_info.oat_data_begin_ + static_cast<int32_t>(offset);
440   }
441 
442   // Returns true if the class was in the original requested image classes list.
443   bool KeepClass(ObjPtr<mirror::Class> klass) REQUIRES_SHARED(Locks::mutator_lock_);
444 
445   // Debug aid that list of requested image classes.
446   void DumpImageClasses();
447 
448   // Visit all class loaders.
449   void VisitClassLoaders(ClassLoaderVisitor* visitor) REQUIRES_SHARED(Locks::mutator_lock_);
450 
451   // Remove unwanted classes from various roots.
452   void PruneNonImageClasses() REQUIRES_SHARED(Locks::mutator_lock_);
453 
454   // Remove everything from the DexCache.
455   void ClearDexCache(ObjPtr<mirror::DexCache> dex_cache)
456       REQUIRES_SHARED(Locks::mutator_lock_);
457 
458   // Preload deterministic DexCache contents.
459   void PreloadDexCache(ObjPtr<mirror::DexCache> dex_cache, ObjPtr<mirror::ClassLoader> class_loader)
460       REQUIRES_SHARED(Locks::mutator_lock_)
461       REQUIRES(!Locks::classlinker_classes_lock_);
462 
463   // Find dex caches for pruning or preloading.
464   std::vector<ObjPtr<mirror::DexCache>> FindDexCaches(Thread* self)
465       REQUIRES_SHARED(Locks::mutator_lock_)
466       REQUIRES(!Locks::classlinker_classes_lock_);
467 
468   // Verify unwanted classes removed.
469   void CheckNonImageClassesRemoved() REQUIRES_SHARED(Locks::mutator_lock_);
470 
471   // Lays out where the image objects will be at runtime.
472   void CalculateNewObjectOffsets()
473       REQUIRES_SHARED(Locks::mutator_lock_);
474   void CreateHeader(size_t oat_index, size_t component_count)
475       REQUIRES_SHARED(Locks::mutator_lock_);
476   ObjPtr<mirror::ObjectArray<mirror::Object>> CollectDexCaches(Thread* self, size_t oat_index) const
477       REQUIRES_SHARED(Locks::mutator_lock_);
478   ObjPtr<mirror::ObjectArray<mirror::Object>> CreateImageRoots(
479       size_t oat_index,
480       Handle<mirror::ObjectArray<mirror::Object>> boot_image_live_objects) const
481       REQUIRES_SHARED(Locks::mutator_lock_);
482   void CalculateObjectBinSlots(mirror::Object* obj)
483       REQUIRES_SHARED(Locks::mutator_lock_);
484 
485   // Creates the contiguous image in memory and adjusts pointers.
486   void CopyAndFixupNativeData(size_t oat_index) REQUIRES_SHARED(Locks::mutator_lock_);
487   void CopyAndFixupObjects() REQUIRES_SHARED(Locks::mutator_lock_);
488   void CopyAndFixupObject(mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_);
489   void CopyAndFixupMethod(ArtMethod* orig, ArtMethod* copy, size_t oat_index)
490       REQUIRES_SHARED(Locks::mutator_lock_);
491   void CopyAndFixupImTable(ImTable* orig, ImTable* copy)
492       REQUIRES_SHARED(Locks::mutator_lock_);
493   void CopyAndFixupImtConflictTable(ImtConflictTable* orig, ImtConflictTable* copy)
494       REQUIRES_SHARED(Locks::mutator_lock_);
495 
496   /*
497    * Copies metadata from the heap into a buffer that will be compressed and
498    * written to the image.
499    *
500    * This function copies the string offset metadata from a local vector to an
501    * offset inside the image_ field of an ImageInfo struct.  The offset into the
502    * memory pointed to by the image_ field is obtained from the ImageSection
503    * object for the String Offsets section.
504    *
505    * All data for the image, besides the object bitmap and the relocation data,
506    * will also be copied into the memory region pointed to by image_.
507    */
508   void CopyMetadata();
509 
510   void FixupClass(mirror::Class* orig, mirror::Class* copy)
511       REQUIRES_SHARED(Locks::mutator_lock_);
512   void FixupObject(mirror::Object* orig, mirror::Object* copy)
513       REQUIRES_SHARED(Locks::mutator_lock_);
514   template <typename T>
515   void FixupDexCacheArrayEntry(std::atomic<mirror::DexCachePair<T>>* orig_array,
516                                std::atomic<mirror::DexCachePair<T>>* new_array,
517                                uint32_t array_index)
518       REQUIRES_SHARED(Locks::mutator_lock_);
519   template <typename T>
520   void FixupDexCacheArrayEntry(std::atomic<mirror::NativeDexCachePair<T>>* orig_array,
521                                std::atomic<mirror::NativeDexCachePair<T>>* new_array,
522                                uint32_t array_index)
523       REQUIRES_SHARED(Locks::mutator_lock_);
524   void FixupDexCacheArrayEntry(GcRoot<mirror::CallSite>* orig_array,
525                                GcRoot<mirror::CallSite>* new_array,
526                                uint32_t array_index)
527       REQUIRES_SHARED(Locks::mutator_lock_);
528   template <typename EntryType>
529   void FixupDexCacheArray(mirror::DexCache* orig_dex_cache,
530                           mirror::DexCache* copy_dex_cache,
531                           MemberOffset array_offset,
532                           uint32_t size)
533       REQUIRES_SHARED(Locks::mutator_lock_);
534   void FixupDexCache(mirror::DexCache* orig_dex_cache,
535                      mirror::DexCache* copy_dex_cache)
536       REQUIRES_SHARED(Locks::mutator_lock_);
537   void FixupPointerArray(mirror::Object* dst,
538                          mirror::PointerArray* arr,
539                          Bin array_type)
540       REQUIRES_SHARED(Locks::mutator_lock_);
541 
542   // Get quick code for non-resolution/imt_conflict/abstract method.
543   const uint8_t* GetQuickCode(ArtMethod* method, const ImageInfo& image_info)
544       REQUIRES_SHARED(Locks::mutator_lock_);
545 
546   // Return true if a method is likely to be dirtied at runtime.
547   bool WillMethodBeDirty(ArtMethod* m) const REQUIRES_SHARED(Locks::mutator_lock_);
548 
549   // Assign the offset for an ArtMethod.
550   void AssignMethodOffset(ArtMethod* method,
551                           NativeObjectRelocationType type,
552                           size_t oat_index)
553       REQUIRES_SHARED(Locks::mutator_lock_);
554 
555   // Return true if imt was newly inserted.
556   bool TryAssignImTableOffset(ImTable* imt, size_t oat_index) REQUIRES_SHARED(Locks::mutator_lock_);
557 
558   // Assign the offset for an IMT conflict table. Does nothing if the table already has a native
559   // relocation.
560   void TryAssignConflictTableOffset(ImtConflictTable* table, size_t oat_index)
561       REQUIRES_SHARED(Locks::mutator_lock_);
562 
563   // Return true if klass is loaded by the boot class loader but not in the boot image.
564   bool IsBootClassLoaderNonImageClass(mirror::Class* klass) REQUIRES_SHARED(Locks::mutator_lock_);
565 
566   // Return true if `klass` depends on a class defined by the boot class path
567   // we're compiling against but not present in the boot image spaces. We want
568   // to prune these classes since we cannot guarantee that they will not be
569   // already loaded at run time when loading this image. This means that we
570   // also cannot have any classes which refer to these non image classes.
571   bool PruneImageClass(ObjPtr<mirror::Class> klass) REQUIRES_SHARED(Locks::mutator_lock_);
572 
573   // early_exit is true if we had a cyclic dependency anywhere down the chain.
574   bool PruneImageClassInternal(ObjPtr<mirror::Class> klass,
575                                bool* early_exit,
576                                std::unordered_set<mirror::Object*>* visited)
577       REQUIRES_SHARED(Locks::mutator_lock_);
578 
IsMultiImage()579   bool IsMultiImage() const {
580     return image_infos_.size() > 1;
581   }
582 
583   static Bin BinTypeForNativeRelocationType(NativeObjectRelocationType type);
584 
585   struct NativeObjectRelocation {
586     size_t oat_index;
587     uintptr_t offset;
588     NativeObjectRelocationType type;
589 
IsArtMethodRelocationNativeObjectRelocation590     bool IsArtMethodRelocation() const {
591       return type == NativeObjectRelocationType::kArtMethodClean ||
592           type == NativeObjectRelocationType::kArtMethodDirty ||
593           type == NativeObjectRelocationType::kRuntimeMethod;
594     }
595   };
596 
597   NativeObjectRelocation GetNativeRelocation(void* obj) REQUIRES_SHARED(Locks::mutator_lock_);
598 
599   // Location of where the object will be when the image is loaded at runtime.
600   template <typename T>
601   T* NativeLocationInImage(T* obj) REQUIRES_SHARED(Locks::mutator_lock_);
602 
603   // Location of where the temporary copy of the object currently is.
604   template <typename T>
605   T* NativeCopyLocation(T* obj) REQUIRES_SHARED(Locks::mutator_lock_);
606 
607   // Return true if `dex_cache` belongs to the image we're writing.
608   // For a boot image, this is true for all dex caches.
609   // For an app image, boot class path dex caches are excluded.
610   bool IsImageDexCache(ObjPtr<mirror::DexCache> dex_cache) const
611       REQUIRES_SHARED(Locks::mutator_lock_);
612 
613   // Return true if `obj` is inside of a boot image space that we're compiling against.
614   // (Always false when compiling the boot image.)
IsInBootImage(const void * obj)615   ALWAYS_INLINE bool IsInBootImage(const void* obj) const {
616     return reinterpret_cast<uintptr_t>(obj) - boot_image_begin_ < boot_image_size_;
617   }
618 
619   // Get the index of the oat file associated with the object.
620   size_t GetOatIndex(mirror::Object* object) const REQUIRES_SHARED(Locks::mutator_lock_);
621 
622   // The oat index for shared data in multi-image and all data in single-image compilation.
GetDefaultOatIndex()623   static constexpr size_t GetDefaultOatIndex() {
624     return 0u;
625   }
626 
GetImageInfo(size_t oat_index)627   ImageInfo& GetImageInfo(size_t oat_index) {
628     return image_infos_[oat_index];
629   }
630 
GetImageInfo(size_t oat_index)631   const ImageInfo& GetImageInfo(size_t oat_index) const {
632     return image_infos_[oat_index];
633   }
634 
635   // Return true if there already exists a native allocation for an object.
636   bool NativeRelocationAssigned(void* ptr) const;
637 
638   // Copy a reference and record image relocation.
639   template <typename DestType>
640   void CopyAndFixupReference(DestType* dest, ObjPtr<mirror::Object> src)
641       REQUIRES_SHARED(Locks::mutator_lock_);
642 
643   // Copy a native pointer and record image relocation.
644   void CopyAndFixupPointer(void** target, void* value, PointerSize pointer_size)
645       REQUIRES_SHARED(Locks::mutator_lock_);
646   void CopyAndFixupPointer(void** target, void* value)
647       REQUIRES_SHARED(Locks::mutator_lock_);
648   void CopyAndFixupPointer(
649       void* object, MemberOffset offset, void* value, PointerSize pointer_size)
650       REQUIRES_SHARED(Locks::mutator_lock_);
651   void CopyAndFixupPointer(void* object, MemberOffset offset, void* value)
652       REQUIRES_SHARED(Locks::mutator_lock_);
653 
654   /*
655    * Tests an object to see if it will be contained in an AppImage.
656    *
657    * An object reference is considered to be a AppImage String reference iff:
658    *   - It isn't null
659    *   - The referred-object isn't in the boot image
660    *   - The referred-object is a Java String
661    */
662   ALWAYS_INLINE
663   bool IsInternedAppImageStringReference(ObjPtr<mirror::Object> referred_obj) const
664       REQUIRES_SHARED(Locks::mutator_lock_);
665 
666   const CompilerOptions& compiler_options_;
667 
668   // Cached boot image begin and size. This includes heap, native objects and oat files.
669   const uint32_t boot_image_begin_;
670   const uint32_t boot_image_size_;
671 
672   // Beginning target image address for the first image.
673   uint8_t* global_image_begin_;
674 
675   // Offset from image_begin_ to where the first object is in image_.
676   size_t image_objects_offset_begin_;
677 
678   // Pointer arrays that need to be updated. Since these are only some int and long arrays, we need
679   // to keep track. These include vtable arrays, iftable arrays, and dex caches.
680   std::unordered_map<mirror::PointerArray*, Bin> pointer_arrays_;
681 
682   // Saved hash codes. We use these to restore lockwords which were temporarily used to have
683   // forwarding addresses as well as copying over hash codes.
684   std::unordered_map<mirror::Object*, uint32_t> saved_hashcode_map_;
685 
686   // Oat index map for objects.
687   std::unordered_map<mirror::Object*, uint32_t> oat_index_map_;
688 
689   // Size of pointers on the target architecture.
690   PointerSize target_ptr_size_;
691 
692   // Image data indexed by the oat file index.
693   dchecked_vector<ImageInfo> image_infos_;
694 
695   // ArtField, ArtMethod relocating map. These are allocated as array of structs but we want to
696   // have one entry per art field for convenience. ArtFields are placed right after the end of the
697   // image objects (aka sum of bin_slot_sizes_). ArtMethods are placed right after the ArtFields.
698   std::unordered_map<void*, NativeObjectRelocation> native_object_relocations_;
699 
700   // Runtime ArtMethods which aren't reachable from any Class but need to be copied into the image.
701   ArtMethod* image_methods_[ImageHeader::kImageMethodsCount];
702 
703   // Counters for measurements, used for logging only.
704   uint64_t dirty_methods_;
705   uint64_t clean_methods_;
706 
707   // Prune class memoization table to speed up ContainsBootClassLoaderNonImageClass.
708   std::unordered_map<mirror::Class*, bool> prune_class_memo_;
709 
710   // The application class loader. Null for boot image.
711   jobject app_class_loader_;
712 
713   // Boot image live objects, null for app image.
714   mirror::ObjectArray<mirror::Object>* boot_image_live_objects_;
715 
716   // Which mode the image is stored as, see image.h
717   const ImageHeader::StorageMode image_storage_mode_;
718 
719   // The file names of oat files.
720   const std::vector<std::string>& oat_filenames_;
721 
722   // Map of dex files to the indexes of oat files that they were compiled into.
723   const std::unordered_map<const DexFile*, size_t>& dex_file_oat_index_map_;
724 
725   // Set of objects known to be dirty in the image. Can be nullptr if there are none.
726   const HashSet<std::string>* dirty_image_objects_;
727 
728   // Objects are guaranteed to not cross the region size boundary.
729   size_t region_size_ = 0u;
730 
731   // Region alignment bytes wasted.
732   size_t region_alignment_wasted_ = 0u;
733 
734   class FixupClassVisitor;
735   class FixupRootVisitor;
736   class FixupVisitor;
737   class ImageFileGuard;
738   class LayoutHelper;
739   class NativeLocationVisitor;
740   class PruneClassesVisitor;
741   class PruneClassLoaderClassesVisitor;
742   class PruneObjectReferenceVisitor;
743 
744   // A visitor used by the VerifyNativeGCRootInvariants() function.
745   class NativeGCRootInvariantVisitor;
746 
747   DISALLOW_COPY_AND_ASSIGN(ImageWriter);
748 };
749 
750 std::ostream& operator<<(std::ostream& stream, ImageWriter::Bin bin);
751 std::ostream& operator<<(std::ostream& stream, ImageWriter::NativeObjectRelocationType type);
752 std::ostream& operator<<(std::ostream& stream, ImageWriter::StubType stub_type);
753 
754 }  // namespace linker
755 }  // namespace art
756 
757 #endif  // ART_DEX2OAT_LINKER_IMAGE_WRITER_H_
758