blob: 4418879e73ec0096362e47bd13e3e24e5bb2745b [file] [log] [blame]
Brian Carlstrom7940e442013-07-12 13:46:57 -07001/*
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
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_COMPILER_IMAGE_WRITER_H_
18#define ART_COMPILER_IMAGE_WRITER_H_
Brian Carlstrom7940e442013-07-12 13:46:57 -070019
20#include <stdint.h>
Andreas Gampe245ee002014-12-04 21:25:04 -080021#include <valgrind.h>
Brian Carlstrom7940e442013-07-12 13:46:57 -070022
23#include <cstddef>
Ian Rogers700a4022014-05-19 16:49:03 -070024#include <memory>
Brian Carlstrom7940e442013-07-12 13:46:57 -070025#include <set>
26#include <string>
Igor Murashkinf5b4c502014-11-14 15:01:59 -080027#include <ostream>
Brian Carlstrom7940e442013-07-12 13:46:57 -070028
Igor Murashkin46774762014-10-22 11:37:02 -070029#include "base/macros.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070030#include "driver/compiler_driver.h"
Mathieu Chartierfd04b6f2014-11-14 19:34:18 -080031#include "gc/space/space.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070032#include "mem_map.h"
33#include "oat_file.h"
34#include "mirror/dex_cache.h"
35#include "os.h"
36#include "safe_map.h"
Igor Murashkinf5b4c502014-11-14 15:01:59 -080037#include "gc/space/space.h"
38#include "utils.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070039
40namespace art {
41
42// Write a Space built during compilation for use during execution.
Igor Murashkin46774762014-10-22 11:37:02 -070043class ImageWriter FINAL {
Brian Carlstrom7940e442013-07-12 13:46:57 -070044 public:
Igor Murashkin46774762014-10-22 11:37:02 -070045 ImageWriter(const CompilerDriver& compiler_driver, uintptr_t image_begin,
46 bool compile_pic)
Ian Rogers13735952014-10-08 12:43:28 -070047 : compiler_driver_(compiler_driver), image_begin_(reinterpret_cast<uint8_t*>(image_begin)),
Igor Murashkinf5b4c502014-11-14 15:01:59 -080048 image_end_(0), image_objects_offset_begin_(0), image_roots_address_(0), oat_file_(nullptr),
Igor Murashkin46774762014-10-22 11:37:02 -070049 oat_data_begin_(nullptr), interpreter_to_interpreter_bridge_offset_(0),
Vladimir Markof4da6752014-08-01 19:04:18 +010050 interpreter_to_compiled_code_bridge_offset_(0), jni_dlsym_lookup_offset_(0),
51 portable_imt_conflict_trampoline_offset_(0), portable_resolution_trampoline_offset_(0),
52 portable_to_interpreter_bridge_offset_(0), quick_generic_jni_trampoline_offset_(0),
53 quick_imt_conflict_trampoline_offset_(0), quick_resolution_trampoline_offset_(0),
Mathieu Chartier2d721012014-11-10 11:08:06 -080054 quick_to_interpreter_bridge_offset_(0), compile_pic_(compile_pic),
Igor Murashkinf5b4c502014-11-14 15:01:59 -080055 target_ptr_size_(InstructionSetPointerSize(compiler_driver_.GetInstructionSet())),
56 bin_slot_sizes_(), bin_slot_count_() {
Vladimir Markof4da6752014-08-01 19:04:18 +010057 CHECK_NE(image_begin, 0U);
58 }
Brian Carlstrom7940e442013-07-12 13:46:57 -070059
Andreas Gampe245ee002014-12-04 21:25:04 -080060 ~ImageWriter() {
61 // For interned strings a large array is allocated to hold all the character data and avoid
62 // overhead. However, no GC is run anymore at this point. As the array is likely large, it
63 // will be allocated in the large object space, where valgrind can track every single
64 // allocation. Not explicitly freeing that array will be recognized as a leak.
65 if (RUNNING_ON_VALGRIND != 0) {
66 FreeStringDataArray();
67 }
68 }
Brian Carlstrom7940e442013-07-12 13:46:57 -070069
Vladimir Markof4da6752014-08-01 19:04:18 +010070 bool PrepareImageAddressSpace();
71
72 bool IsImageAddressSpaceReady() const {
73 return image_roots_address_ != 0u;
74 }
75
76 mirror::Object* GetImageAddress(mirror::Object* object) const
77 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Igor Murashkin46774762014-10-22 11:37:02 -070078 if (object == nullptr) {
79 return nullptr;
Vladimir Markof4da6752014-08-01 19:04:18 +010080 }
81 return reinterpret_cast<mirror::Object*>(image_begin_ + GetImageOffset(object));
82 }
83
Ian Rogers13735952014-10-08 12:43:28 -070084 uint8_t* GetOatFileBegin() const {
Vladimir Markof4da6752014-08-01 19:04:18 +010085 return image_begin_ + RoundUp(image_end_, kPageSize);
86 }
87
Brian Carlstrom7940e442013-07-12 13:46:57 -070088 bool Write(const std::string& image_filename,
Brian Carlstrom7940e442013-07-12 13:46:57 -070089 const std::string& oat_filename,
90 const std::string& oat_location)
91 LOCKS_EXCLUDED(Locks::mutator_lock_);
92
93 uintptr_t GetOatDataBegin() {
94 return reinterpret_cast<uintptr_t>(oat_data_begin_);
95 }
96
97 private:
98 bool AllocMemory();
99
Mathieu Chartier31e89252013-08-28 11:29:12 -0700100 // Mark the objects defined in this space in the given live bitmap.
101 void RecordImageAllocations() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
102
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800103 // Classify different kinds of bins that objects end up getting packed into during image writing.
104 enum Bin {
105 // Likely-clean:
106 kBinString, // [String] Almost always immutable (except for obj header).
107 kBinArtMethodsManagedInitialized, // [ArtMethod] Not-native, and initialized. Unlikely to dirty
108 // Unknown mix of clean/dirty:
109 kBinRegular,
110 // Likely-dirty:
111 // All classes get their own bins since their fields often dirty
112 kBinClassInitializedFinalStatics, // Class initializers have been run, no non-final statics
113 kBinClassInitialized, // Class initializers have been run
114 kBinClassVerified, // Class verified, but initializers haven't been run
115 kBinArtMethodNative, // Art method that is actually native
116 kBinArtMethodNotInitialized, // Art method with a declaring class that wasn't initialized
117 // Don't care about other art methods since they don't dirty
118 // Add more bins here if we add more segregation code.
119 kBinSize,
120 };
121
122 friend std::ostream& operator<<(std::ostream& stream, const Bin& bin);
123
124 static constexpr size_t kBinBits = MinimumBitsToStore(kBinSize - 1);
125 // uint32 = typeof(lockword_)
126 static constexpr size_t kBinShift = BitSizeOf<uint32_t>() - kBinBits;
127 // 111000.....0
128 static constexpr size_t kBinMask = ((static_cast<size_t>(1) << kBinBits) - 1) << kBinShift;
129
130 // We use the lock word to store the bin # and bin index of the object in the image.
131 //
132 // The struct size must be exactly sizeof(LockWord), currently 32-bits, since this will end up
133 // stored in the lock word bit-for-bit when object forwarding addresses are being calculated.
134 struct BinSlot {
135 explicit BinSlot(uint32_t lockword);
136 BinSlot(Bin bin, uint32_t index);
137
138 // The bin an object belongs to, i.e. regular, class/verified, class/initialized, etc.
139 Bin GetBin() const;
140 // The offset in bytes from the beginning of the bin. Aligned to object size.
141 uint32_t GetIndex() const;
142 // Pack into a single uint32_t, for storing into a lock word.
143 explicit operator uint32_t() const { return lockword_; }
144 // Comparison operator for map support
145 bool operator<(const BinSlot& other) const { return lockword_ < other.lockword_; }
146
147 private:
148 // Must be the same size as LockWord, any larger and we would truncate the data.
149 const uint32_t lockword_;
150 };
151
Mathieu Chartier31e89252013-08-28 11:29:12 -0700152 // We use the lock word to store the offset of the object in the image.
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800153 void AssignImageOffset(mirror::Object* object, BinSlot bin_slot)
154 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
155 void SetImageOffset(mirror::Object* object, BinSlot bin_slot, size_t offset)
Mathieu Chartier590fee92013-09-13 13:46:47 -0700156 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700157 bool IsImageOffsetAssigned(mirror::Object* object) const
158 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
159 size_t GetImageOffset(mirror::Object* object) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700160
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800161 void AssignImageBinSlot(mirror::Object* object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
162 void SetImageBinSlot(mirror::Object* object, BinSlot bin_slot)
163 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
164 bool IsImageBinSlotAssigned(mirror::Object* object) const
165 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
166 BinSlot GetImageBinSlot(mirror::Object* object) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
167
Alex Lighta59dd802014-07-02 16:28:08 -0700168 static void* GetImageAddressCallback(void* writer, mirror::Object* obj)
169 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
170 return reinterpret_cast<ImageWriter*>(writer)->GetImageAddress(obj);
171 }
172
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700173 mirror::Object* GetLocalAddress(mirror::Object* object) const
174 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700175 size_t offset = GetImageOffset(object);
Ian Rogers13735952014-10-08 12:43:28 -0700176 uint8_t* dst = image_->Begin() + offset;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700177 return reinterpret_cast<mirror::Object*>(dst);
178 }
179
Ian Rogers13735952014-10-08 12:43:28 -0700180 const uint8_t* GetOatAddress(uint32_t offset) const {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700181#if !defined(ART_USE_PORTABLE_COMPILER)
182 // With Quick, code is within the OatFile, as there are all in one
183 // .o ELF object. However with Portable, the code is always in
184 // different .o ELF objects.
185 DCHECK_LT(offset, oat_file_->Size());
186#endif
Igor Murashkin46774762014-10-22 11:37:02 -0700187 if (offset == 0u) {
188 return nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700189 }
190 return oat_data_begin_ + offset;
191 }
192
193 // Returns true if the class was in the original requested image classes list.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800194 bool IsImageClass(mirror::Class* klass) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700195
196 // Debug aid that list of requested image classes.
197 void DumpImageClasses();
198
199 // Preinitializes some otherwise lazy fields (such as Class name) to avoid runtime image dirtying.
200 void ComputeLazyFieldsForImageClasses()
201 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
202 static bool ComputeLazyFieldsForClassesVisitor(mirror::Class* klass, void* arg)
203 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
204
205 // Wire dex cache resolved strings to strings in the image to avoid runtime resolution.
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700206 void ComputeEagerResolvedStrings() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700207 static void ComputeEagerResolvedStringsCallback(mirror::Object* obj, void* arg)
208 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
209
Mathieu Chartierfd04b6f2014-11-14 19:34:18 -0800210 // Combine string char arrays.
211 void ProcessStrings() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
212
Brian Carlstrom7940e442013-07-12 13:46:57 -0700213 // Remove unwanted classes from various roots.
214 void PruneNonImageClasses() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
215 static bool NonImageClassesVisitor(mirror::Class* c, void* arg)
216 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
217
218 // Verify unwanted classes removed.
Mathieu Chartierfd04b6f2014-11-14 19:34:18 -0800219 void CheckNonImageClassesRemoved() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700220 static void CheckNonImageClassesRemovedCallback(mirror::Object* obj, void* arg)
221 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
222
223 // Lays out where the image objects will be at runtime.
Vladimir Markof4da6752014-08-01 19:04:18 +0100224 void CalculateNewObjectOffsets()
225 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
226 void CreateHeader(size_t oat_loaded_size, size_t oat_data_offset)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700227 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
228 mirror::ObjectArray<mirror::Object>* CreateImageRoots() const
229 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800230 void CalculateObjectBinSlots(mirror::Object* obj)
231 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
232 void UnbinObjectsIntoOffset(mirror::Object* obj)
Mathieu Chartier590fee92013-09-13 13:46:47 -0700233 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
234
235 void WalkInstanceFields(mirror::Object* obj, mirror::Class* klass)
236 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
237 void WalkFieldsInOrder(mirror::Object* obj)
238 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
239 static void WalkFieldsCallback(mirror::Object* obj, void* arg)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700240 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800241 static void UnbinObjectsIntoOffsetCallback(mirror::Object* obj, void* arg)
242 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700243
244 // Creates the contiguous image in memory and adjusts pointers.
Mathieu Chartierfd04b6f2014-11-14 19:34:18 -0800245 void CopyAndFixupObjects() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700246 static void CopyAndFixupObjectsCallback(mirror::Object* obj, void* arg)
247 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800248 void FixupMethod(mirror::ArtMethod* orig, mirror::ArtMethod* copy)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700249 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800250 void FixupObject(mirror::Object* orig, mirror::Object* copy)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700251 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700252
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700253 // Get quick code for non-resolution/imt_conflict/abstract method.
Ian Rogers13735952014-10-08 12:43:28 -0700254 const uint8_t* GetQuickCode(mirror::ArtMethod* method, bool* quick_is_interpreted)
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700255 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
256
Ian Rogers13735952014-10-08 12:43:28 -0700257 const uint8_t* GetQuickEntryPoint(mirror::ArtMethod* method)
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700258 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
259
Brian Carlstrom7940e442013-07-12 13:46:57 -0700260 // Patches references in OatFile to expect runtime addresses.
Vladimir Markof4da6752014-08-01 19:04:18 +0100261 void SetOatChecksumFromElfFile(File* elf_file);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700262
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800263 // Calculate the sum total of the bin slot sizes in [0, up_to). Defaults to all bins.
264 size_t GetBinSizeSum(Bin up_to = kBinSize) const;
265
Andreas Gampe245ee002014-12-04 21:25:04 -0800266 // Release the string_data_array_.
267 void FreeStringDataArray();
268
Brian Carlstrom7940e442013-07-12 13:46:57 -0700269 const CompilerDriver& compiler_driver_;
270
Vladimir Markof4da6752014-08-01 19:04:18 +0100271 // Beginning target image address for the output image.
Ian Rogers13735952014-10-08 12:43:28 -0700272 uint8_t* image_begin_;
Vladimir Markof4da6752014-08-01 19:04:18 +0100273
274 // Offset to the free space in image_.
275 size_t image_end_;
276
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800277 // Offset from image_begin_ to where the first object is in image_.
278 size_t image_objects_offset_begin_;
279
Vladimir Markof4da6752014-08-01 19:04:18 +0100280 // The image roots address in the image.
281 uint32_t image_roots_address_;
282
Brian Carlstrom7940e442013-07-12 13:46:57 -0700283 // oat file with code for this image
284 OatFile* oat_file_;
285
286 // Memory mapped for generating the image.
Ian Rogers700a4022014-05-19 16:49:03 -0700287 std::unique_ptr<MemMap> image_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700288
Mathieu Chartier590fee92013-09-13 13:46:47 -0700289 // Saved hashes (objects are inside of the image so that they don't move).
Ian Rogers700a4022014-05-19 16:49:03 -0700290 std::vector<std::pair<mirror::Object*, uint32_t>> saved_hashes_;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700291
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800292 // Saved hashes (objects are bin slots to inside of the image, not yet allocated an address).
293 std::map<BinSlot, uint32_t> saved_hashes_map_;
294
Brian Carlstrom7940e442013-07-12 13:46:57 -0700295 // Beginning target oat address for the pointers from the output image to its oat file.
Ian Rogers13735952014-10-08 12:43:28 -0700296 const uint8_t* oat_data_begin_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700297
Mathieu Chartier31e89252013-08-28 11:29:12 -0700298 // Image bitmap which lets us know where the objects inside of the image reside.
Ian Rogers700a4022014-05-19 16:49:03 -0700299 std::unique_ptr<gc::accounting::ContinuousSpaceBitmap> image_bitmap_;
Mathieu Chartier31e89252013-08-28 11:29:12 -0700300
Brian Carlstrom7940e442013-07-12 13:46:57 -0700301 // Offset from oat_data_begin_ to the stubs.
Ian Rogers848871b2013-08-05 10:56:33 -0700302 uint32_t interpreter_to_interpreter_bridge_offset_;
303 uint32_t interpreter_to_compiled_code_bridge_offset_;
304 uint32_t jni_dlsym_lookup_offset_;
Jeff Hao88474b42013-10-23 16:24:40 -0700305 uint32_t portable_imt_conflict_trampoline_offset_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700306 uint32_t portable_resolution_trampoline_offset_;
Ian Rogers848871b2013-08-05 10:56:33 -0700307 uint32_t portable_to_interpreter_bridge_offset_;
Andreas Gampe2da88232014-02-27 12:26:20 -0800308 uint32_t quick_generic_jni_trampoline_offset_;
Jeff Hao88474b42013-10-23 16:24:40 -0700309 uint32_t quick_imt_conflict_trampoline_offset_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700310 uint32_t quick_resolution_trampoline_offset_;
Ian Rogers848871b2013-08-05 10:56:33 -0700311 uint32_t quick_to_interpreter_bridge_offset_;
Igor Murashkin46774762014-10-22 11:37:02 -0700312 const bool compile_pic_;
Mathieu Chartierb7ea3ac2014-03-24 16:54:46 -0700313
Mathieu Chartier2d721012014-11-10 11:08:06 -0800314 // Size of pointers on the target architecture.
315 size_t target_ptr_size_;
316
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800317 // Bin slot tracking for dirty object packing
318 size_t bin_slot_sizes_[kBinSize]; // Number of bytes in a bin
319 size_t bin_slot_count_[kBinSize]; // Number of objects in a bin
320
Andreas Gampe245ee002014-12-04 21:25:04 -0800321 void* string_data_array_; // The backing for the interned strings.
322
Mathieu Chartierb7ea3ac2014-03-24 16:54:46 -0700323 friend class FixupVisitor;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700324 friend class FixupClassVisitor;
Mathieu Chartierb7ea3ac2014-03-24 16:54:46 -0700325 DISALLOW_COPY_AND_ASSIGN(ImageWriter);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700326};
327
328} // namespace art
329
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700330#endif // ART_COMPILER_IMAGE_WRITER_H_