Home | History | Annotate | Download | only in compiler
      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_COMPILER_IMAGE_WRITER_H_
     18 #define ART_COMPILER_IMAGE_WRITER_H_
     19 
     20 #include <stdint.h>
     21 
     22 #include <cstddef>
     23 #include <memory>
     24 #include <set>
     25 #include <string>
     26 
     27 #include "base/macros.h"
     28 #include "driver/compiler_driver.h"
     29 #include "gc/space/space.h"
     30 #include "mem_map.h"
     31 #include "oat_file.h"
     32 #include "mirror/dex_cache.h"
     33 #include "os.h"
     34 #include "safe_map.h"
     35 #include "gc/space/space.h"
     36 #include "utils.h"
     37 
     38 namespace art {
     39 
     40 // Write a Space built during compilation for use during execution.
     41 class ImageWriter FINAL {
     42  public:
     43   explicit ImageWriter(const CompilerDriver& compiler_driver)
     44       : compiler_driver_(compiler_driver), oat_file_(NULL), image_end_(0),
     45         image_objects_offset_begin_(0), image_begin_(NULL),
     46         oat_data_begin_(NULL), interpreter_to_interpreter_bridge_offset_(0),
     47         interpreter_to_compiled_code_bridge_offset_(0), portable_imt_conflict_trampoline_offset_(0),
     48         portable_resolution_trampoline_offset_(0), quick_generic_jni_trampoline_offset_(0),
     49         quick_imt_conflict_trampoline_offset_(0), quick_resolution_trampoline_offset_(0),
     50         compile_pic_(false), target_ptr_size_(0), bin_slot_sizes_(), bin_slot_count_() {}
     51 
     52   ~ImageWriter() {}
     53 
     54   bool Write(const std::string& image_filename,
     55              uintptr_t image_begin,
     56              const std::string& oat_filename,
     57              const std::string& oat_location,
     58              bool compile_pic)
     59       LOCKS_EXCLUDED(Locks::mutator_lock_);
     60 
     61   uintptr_t GetOatDataBegin() {
     62     return reinterpret_cast<uintptr_t>(oat_data_begin_);
     63   }
     64 
     65  private:
     66   bool AllocMemory();
     67 
     68   // Mark the objects defined in this space in the given live bitmap.
     69   void RecordImageAllocations() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
     70 
     71   // Classify different kinds of bins that objects end up getting packed into during image writing.
     72   enum Bin {
     73     // Likely-clean:
     74     kBinString,                        // [String] Almost always immutable (except for obj header).
     75     kBinArtMethodsManagedInitialized,  // [ArtMethod] Not-native, and initialized. Unlikely to dirty
     76     // Unknown mix of clean/dirty:
     77     kBinRegular,
     78     // Likely-dirty:
     79     // All classes get their own bins since their fields often dirty
     80     kBinClassInitializedFinalStatics,  // Class initializers have been run, no non-final statics
     81     kBinClassInitialized,         // Class initializers have been run
     82     kBinClassVerified,            // Class verified, but initializers haven't been run
     83     kBinArtMethodNative,          // Art method that is actually native
     84     kBinArtMethodNotInitialized,  // Art method with a declaring class that wasn't initialized
     85     // Don't care about other art methods since they don't dirty
     86     // Add more bins here if we add more segregation code.
     87     kBinSize,
     88   };
     89 
     90   static constexpr size_t kBinBits = MinimumBitsToStore(kBinSize - 1);
     91   // uint32 = typeof(lockword_)
     92   static constexpr size_t kBinShift = BitSizeOf<uint32_t>() - kBinBits;
     93   // 111000.....0
     94   static constexpr size_t kBinMask = ((static_cast<size_t>(1) << kBinBits) - 1) << kBinShift;
     95 
     96   // We use the lock word to store the bin # and bin index of the object in the image.
     97   //
     98   // The struct size must be exactly sizeof(LockWord), currently 32-bits, since this will end up
     99   // stored in the lock word bit-for-bit when object forwarding addresses are being calculated.
    100   struct BinSlot {
    101     explicit BinSlot(uint32_t lockword);
    102     BinSlot(Bin bin, uint32_t index);
    103 
    104     // The bin an object belongs to, i.e. regular, class/verified, class/initialized, etc.
    105     Bin GetBin() const;
    106     // The offset in bytes from the beginning of the bin. Aligned to object size.
    107     uint32_t GetIndex() const;
    108     // Pack into a single uint32_t, for storing into a lock word.
    109     explicit operator uint32_t() const { return lockword_; }
    110     // Comparison operator for map support
    111     bool operator<(const BinSlot& other) const  { return lockword_ < other.lockword_; }
    112 
    113   private:
    114     // Must be the same size as LockWord, any larger and we would truncate the data.
    115     const uint32_t lockword_;
    116   };
    117 
    118   // We use the lock word to store the offset of the object in the image.
    119   void AssignImageOffset(mirror::Object* object, BinSlot bin_slot)
    120       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
    121   void SetImageOffset(mirror::Object* object, BinSlot bin_slot, size_t offset)
    122       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
    123   bool IsImageOffsetAssigned(mirror::Object* object) const
    124       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
    125   size_t GetImageOffset(mirror::Object* object) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
    126 
    127   void AssignImageBinSlot(mirror::Object* object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
    128   void SetImageBinSlot(mirror::Object* object, BinSlot bin_slot)
    129       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
    130   bool IsImageBinSlotAssigned(mirror::Object* object) const
    131       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
    132   BinSlot GetImageBinSlot(mirror::Object* object) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
    133 
    134   static void* GetImageAddressCallback(void* writer, mirror::Object* obj)
    135       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
    136     return reinterpret_cast<ImageWriter*>(writer)->GetImageAddress(obj);
    137   }
    138 
    139   mirror::Object* GetImageAddress(mirror::Object* object) const
    140       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
    141     if (object == NULL) {
    142       return NULL;
    143     }
    144     return reinterpret_cast<mirror::Object*>(image_begin_ + GetImageOffset(object));
    145   }
    146 
    147   mirror::Object* GetLocalAddress(mirror::Object* object) const
    148       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
    149     size_t offset = GetImageOffset(object);
    150     byte* dst = image_->Begin() + offset;
    151     return reinterpret_cast<mirror::Object*>(dst);
    152   }
    153 
    154   const byte* GetOatAddress(uint32_t offset) const {
    155 #if !defined(ART_USE_PORTABLE_COMPILER)
    156     // With Quick, code is within the OatFile, as there are all in one
    157     // .o ELF object. However with Portable, the code is always in
    158     // different .o ELF objects.
    159     DCHECK_LT(offset, oat_file_->Size());
    160 #endif
    161     if (offset == 0u) {
    162       return nullptr;
    163     }
    164     return oat_data_begin_ + offset;
    165   }
    166 
    167   // Returns true if the class was in the original requested image classes list.
    168   bool IsImageClass(mirror::Class* klass) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
    169 
    170   // Debug aid that list of requested image classes.
    171   void DumpImageClasses();
    172 
    173   // Preinitializes some otherwise lazy fields (such as Class name) to avoid runtime image dirtying.
    174   void ComputeLazyFieldsForImageClasses()
    175       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
    176   static bool ComputeLazyFieldsForClassesVisitor(mirror::Class* klass, void* arg)
    177       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
    178 
    179   // Wire dex cache resolved strings to strings in the image to avoid runtime resolution.
    180   void ComputeEagerResolvedStrings() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
    181   static void ComputeEagerResolvedStringsCallback(mirror::Object* obj, void* arg)
    182       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
    183 
    184   // Combine string char arrays.
    185   void ProcessStrings() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
    186 
    187   // Remove unwanted classes from various roots.
    188   void PruneNonImageClasses() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
    189   static bool NonImageClassesVisitor(mirror::Class* c, void* arg)
    190       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
    191 
    192   // Verify unwanted classes removed.
    193   void CheckNonImageClassesRemoved() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
    194   static void CheckNonImageClassesRemovedCallback(mirror::Object* obj, void* arg)
    195       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
    196 
    197   // Lays out where the image objects will be at runtime.
    198   void CalculateNewObjectOffsets(size_t oat_loaded_size, size_t oat_data_offset)
    199       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
    200   mirror::ObjectArray<mirror::Object>* CreateImageRoots() const
    201       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
    202   void CalculateObjectBinSlots(mirror::Object* obj)
    203       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
    204   void UnbinObjectsIntoOffset(mirror::Object* obj)
    205       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
    206 
    207   void WalkInstanceFields(mirror::Object* obj, mirror::Class* klass)
    208       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
    209   void WalkFieldsInOrder(mirror::Object* obj)
    210       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
    211   static void WalkFieldsCallback(mirror::Object* obj, void* arg)
    212       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
    213   static void UnbinObjectsIntoOffsetCallback(mirror::Object* obj, void* arg)
    214       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
    215 
    216   // Creates the contiguous image in memory and adjusts pointers.
    217   void CopyAndFixupObjects() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
    218   static void CopyAndFixupObjectsCallback(mirror::Object* obj, void* arg)
    219       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
    220   void FixupMethod(mirror::ArtMethod* orig, mirror::ArtMethod* copy)
    221       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
    222   void FixupObject(mirror::Object* orig, mirror::Object* copy)
    223       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
    224 
    225   // Get quick code for non-resolution/imt_conflict/abstract method.
    226   const byte* GetQuickCode(mirror::ArtMethod* method, bool* quick_is_interpreted)
    227       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
    228 
    229   const byte* GetQuickEntryPoint(mirror::ArtMethod* method)
    230       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
    231 
    232   // Patches references in OatFile to expect runtime addresses.
    233   void PatchOatCodeAndMethods(File* elf_file)
    234       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
    235 
    236   // Calculate the sum total of the bin slot sizes in [0, up_to). Defaults to all bins.
    237   size_t GetBinSizeSum(Bin up_to = kBinSize) const;
    238 
    239   const CompilerDriver& compiler_driver_;
    240 
    241   // oat file with code for this image
    242   OatFile* oat_file_;
    243 
    244   // Memory mapped for generating the image.
    245   std::unique_ptr<MemMap> image_;
    246 
    247   // Offset to the free space in image_.
    248   size_t image_end_;
    249 
    250   // Offset from image_begin_ to where the first object is in image_.
    251   size_t image_objects_offset_begin_;
    252 
    253   // Beginning target image address for the output image.
    254   byte* image_begin_;
    255 
    256   // Saved hashes (objects are inside of the image so that they don't move).
    257   std::vector<std::pair<mirror::Object*, uint32_t>> saved_hashes_;
    258 
    259   // Saved hashes (objects are bin slots to inside of the image, not yet allocated an address).
    260   std::map<BinSlot, uint32_t> saved_hashes_map_;
    261 
    262   // Beginning target oat address for the pointers from the output image to its oat file.
    263   const byte* oat_data_begin_;
    264 
    265   // Image bitmap which lets us know where the objects inside of the image reside.
    266   std::unique_ptr<gc::accounting::ContinuousSpaceBitmap> image_bitmap_;
    267 
    268   // Offset from oat_data_begin_ to the stubs.
    269   uint32_t interpreter_to_interpreter_bridge_offset_;
    270   uint32_t interpreter_to_compiled_code_bridge_offset_;
    271   uint32_t jni_dlsym_lookup_offset_;
    272   uint32_t portable_imt_conflict_trampoline_offset_;
    273   uint32_t portable_resolution_trampoline_offset_;
    274   uint32_t portable_to_interpreter_bridge_offset_;
    275   uint32_t quick_generic_jni_trampoline_offset_;
    276   uint32_t quick_imt_conflict_trampoline_offset_;
    277   uint32_t quick_resolution_trampoline_offset_;
    278   uint32_t quick_to_interpreter_bridge_offset_;
    279   bool compile_pic_;
    280 
    281   // Size of pointers on the target architecture.
    282   size_t target_ptr_size_;
    283 
    284   // Bin slot tracking for dirty object packing
    285   size_t bin_slot_sizes_[kBinSize];  // Number of bytes in a bin
    286   size_t bin_slot_count_[kBinSize];  // Number of objects in a bin
    287 
    288   friend class FixupVisitor;
    289   friend class FixupClassVisitor;
    290   DISALLOW_COPY_AND_ASSIGN(ImageWriter);
    291 };
    292 
    293 }  // namespace art
    294 
    295 #endif  // ART_COMPILER_IMAGE_WRITER_H_
    296