blob: 8c84b686fa3a843b04027f5806bc35f9aac94523 [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>
21
22#include <cstddef>
Ian Rogers700a4022014-05-19 16:49:03 -070023#include <memory>
Brian Carlstrom7940e442013-07-12 13:46:57 -070024#include <set>
25#include <string>
Igor Murashkinf5b4c502014-11-14 15:01:59 -080026#include <ostream>
Brian Carlstrom7940e442013-07-12 13:46:57 -070027
Igor Murashkin46774762014-10-22 11:37:02 -070028#include "base/macros.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070029#include "driver/compiler_driver.h"
Mathieu Chartierfd04b6f2014-11-14 19:34:18 -080030#include "gc/space/space.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070031#include "mem_map.h"
32#include "oat_file.h"
33#include "mirror/dex_cache.h"
34#include "os.h"
35#include "safe_map.h"
Igor Murashkinf5b4c502014-11-14 15:01:59 -080036#include "gc/space/space.h"
37#include "utils.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070038
39namespace art {
40
41// Write a Space built during compilation for use during execution.
Igor Murashkin46774762014-10-22 11:37:02 -070042class ImageWriter FINAL {
Brian Carlstrom7940e442013-07-12 13:46:57 -070043 public:
Igor Murashkin46774762014-10-22 11:37:02 -070044 ImageWriter(const CompilerDriver& compiler_driver, uintptr_t image_begin,
45 bool compile_pic)
Ian Rogers13735952014-10-08 12:43:28 -070046 : compiler_driver_(compiler_driver), image_begin_(reinterpret_cast<uint8_t*>(image_begin)),
Igor Murashkinf5b4c502014-11-14 15:01:59 -080047 image_end_(0), image_objects_offset_begin_(0), image_roots_address_(0), oat_file_(nullptr),
Igor Murashkin46774762014-10-22 11:37:02 -070048 oat_data_begin_(nullptr), interpreter_to_interpreter_bridge_offset_(0),
Vladimir Markof4da6752014-08-01 19:04:18 +010049 interpreter_to_compiled_code_bridge_offset_(0), jni_dlsym_lookup_offset_(0),
50 portable_imt_conflict_trampoline_offset_(0), portable_resolution_trampoline_offset_(0),
51 portable_to_interpreter_bridge_offset_(0), quick_generic_jni_trampoline_offset_(0),
52 quick_imt_conflict_trampoline_offset_(0), quick_resolution_trampoline_offset_(0),
Mathieu Chartier2d721012014-11-10 11:08:06 -080053 quick_to_interpreter_bridge_offset_(0), compile_pic_(compile_pic),
Igor Murashkinf5b4c502014-11-14 15:01:59 -080054 target_ptr_size_(InstructionSetPointerSize(compiler_driver_.GetInstructionSet())),
55 bin_slot_sizes_(), bin_slot_count_() {
Vladimir Markof4da6752014-08-01 19:04:18 +010056 CHECK_NE(image_begin, 0U);
57 }
Brian Carlstrom7940e442013-07-12 13:46:57 -070058
59 ~ImageWriter() {}
60
Vladimir Markof4da6752014-08-01 19:04:18 +010061 bool PrepareImageAddressSpace();
62
63 bool IsImageAddressSpaceReady() const {
64 return image_roots_address_ != 0u;
65 }
66
67 mirror::Object* GetImageAddress(mirror::Object* object) const
68 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Igor Murashkin46774762014-10-22 11:37:02 -070069 if (object == nullptr) {
70 return nullptr;
Vladimir Markof4da6752014-08-01 19:04:18 +010071 }
72 return reinterpret_cast<mirror::Object*>(image_begin_ + GetImageOffset(object));
73 }
74
Ian Rogers13735952014-10-08 12:43:28 -070075 uint8_t* GetOatFileBegin() const {
Vladimir Markof4da6752014-08-01 19:04:18 +010076 return image_begin_ + RoundUp(image_end_, kPageSize);
77 }
78
Brian Carlstrom7940e442013-07-12 13:46:57 -070079 bool Write(const std::string& image_filename,
Brian Carlstrom7940e442013-07-12 13:46:57 -070080 const std::string& oat_filename,
81 const std::string& oat_location)
82 LOCKS_EXCLUDED(Locks::mutator_lock_);
83
84 uintptr_t GetOatDataBegin() {
85 return reinterpret_cast<uintptr_t>(oat_data_begin_);
86 }
87
88 private:
89 bool AllocMemory();
90
Mathieu Chartier31e89252013-08-28 11:29:12 -070091 // Mark the objects defined in this space in the given live bitmap.
92 void RecordImageAllocations() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
93
Igor Murashkinf5b4c502014-11-14 15:01:59 -080094 // Classify different kinds of bins that objects end up getting packed into during image writing.
95 enum Bin {
96 // Likely-clean:
97 kBinString, // [String] Almost always immutable (except for obj header).
98 kBinArtMethodsManagedInitialized, // [ArtMethod] Not-native, and initialized. Unlikely to dirty
99 // Unknown mix of clean/dirty:
100 kBinRegular,
101 // Likely-dirty:
102 // All classes get their own bins since their fields often dirty
103 kBinClassInitializedFinalStatics, // Class initializers have been run, no non-final statics
104 kBinClassInitialized, // Class initializers have been run
105 kBinClassVerified, // Class verified, but initializers haven't been run
106 kBinArtMethodNative, // Art method that is actually native
107 kBinArtMethodNotInitialized, // Art method with a declaring class that wasn't initialized
108 // Don't care about other art methods since they don't dirty
109 // Add more bins here if we add more segregation code.
110 kBinSize,
111 };
112
113 friend std::ostream& operator<<(std::ostream& stream, const Bin& bin);
114
115 static constexpr size_t kBinBits = MinimumBitsToStore(kBinSize - 1);
116 // uint32 = typeof(lockword_)
117 static constexpr size_t kBinShift = BitSizeOf<uint32_t>() - kBinBits;
118 // 111000.....0
119 static constexpr size_t kBinMask = ((static_cast<size_t>(1) << kBinBits) - 1) << kBinShift;
120
121 // We use the lock word to store the bin # and bin index of the object in the image.
122 //
123 // The struct size must be exactly sizeof(LockWord), currently 32-bits, since this will end up
124 // stored in the lock word bit-for-bit when object forwarding addresses are being calculated.
125 struct BinSlot {
126 explicit BinSlot(uint32_t lockword);
127 BinSlot(Bin bin, uint32_t index);
128
129 // The bin an object belongs to, i.e. regular, class/verified, class/initialized, etc.
130 Bin GetBin() const;
131 // The offset in bytes from the beginning of the bin. Aligned to object size.
132 uint32_t GetIndex() const;
133 // Pack into a single uint32_t, for storing into a lock word.
134 explicit operator uint32_t() const { return lockword_; }
135 // Comparison operator for map support
136 bool operator<(const BinSlot& other) const { return lockword_ < other.lockword_; }
137
138 private:
139 // Must be the same size as LockWord, any larger and we would truncate the data.
140 const uint32_t lockword_;
141 };
142
Mathieu Chartier31e89252013-08-28 11:29:12 -0700143 // We use the lock word to store the offset of the object in the image.
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800144 void AssignImageOffset(mirror::Object* object, BinSlot bin_slot)
145 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
146 void SetImageOffset(mirror::Object* object, BinSlot bin_slot, size_t offset)
Mathieu Chartier590fee92013-09-13 13:46:47 -0700147 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700148 bool IsImageOffsetAssigned(mirror::Object* object) const
149 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
150 size_t GetImageOffset(mirror::Object* object) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700151
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800152 void AssignImageBinSlot(mirror::Object* object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
153 void SetImageBinSlot(mirror::Object* object, BinSlot bin_slot)
154 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
155 bool IsImageBinSlotAssigned(mirror::Object* object) const
156 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
157 BinSlot GetImageBinSlot(mirror::Object* object) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
158
Alex Lighta59dd802014-07-02 16:28:08 -0700159 static void* GetImageAddressCallback(void* writer, mirror::Object* obj)
160 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
161 return reinterpret_cast<ImageWriter*>(writer)->GetImageAddress(obj);
162 }
163
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700164 mirror::Object* GetLocalAddress(mirror::Object* object) const
165 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700166 size_t offset = GetImageOffset(object);
Ian Rogers13735952014-10-08 12:43:28 -0700167 uint8_t* dst = image_->Begin() + offset;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700168 return reinterpret_cast<mirror::Object*>(dst);
169 }
170
Ian Rogers13735952014-10-08 12:43:28 -0700171 const uint8_t* GetOatAddress(uint32_t offset) const {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700172#if !defined(ART_USE_PORTABLE_COMPILER)
173 // With Quick, code is within the OatFile, as there are all in one
174 // .o ELF object. However with Portable, the code is always in
175 // different .o ELF objects.
176 DCHECK_LT(offset, oat_file_->Size());
177#endif
Igor Murashkin46774762014-10-22 11:37:02 -0700178 if (offset == 0u) {
179 return nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700180 }
181 return oat_data_begin_ + offset;
182 }
183
184 // Returns true if the class was in the original requested image classes list.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800185 bool IsImageClass(mirror::Class* klass) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700186
187 // Debug aid that list of requested image classes.
188 void DumpImageClasses();
189
190 // Preinitializes some otherwise lazy fields (such as Class name) to avoid runtime image dirtying.
191 void ComputeLazyFieldsForImageClasses()
192 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
193 static bool ComputeLazyFieldsForClassesVisitor(mirror::Class* klass, void* arg)
194 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
195
196 // Wire dex cache resolved strings to strings in the image to avoid runtime resolution.
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700197 void ComputeEagerResolvedStrings() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700198 static void ComputeEagerResolvedStringsCallback(mirror::Object* obj, void* arg)
199 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
200
Mathieu Chartierfd04b6f2014-11-14 19:34:18 -0800201 // Combine string char arrays.
202 void ProcessStrings() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
203
Brian Carlstrom7940e442013-07-12 13:46:57 -0700204 // Remove unwanted classes from various roots.
205 void PruneNonImageClasses() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
206 static bool NonImageClassesVisitor(mirror::Class* c, void* arg)
207 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
208
209 // Verify unwanted classes removed.
Mathieu Chartierfd04b6f2014-11-14 19:34:18 -0800210 void CheckNonImageClassesRemoved() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700211 static void CheckNonImageClassesRemovedCallback(mirror::Object* obj, void* arg)
212 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
213
214 // Lays out where the image objects will be at runtime.
Vladimir Markof4da6752014-08-01 19:04:18 +0100215 void CalculateNewObjectOffsets()
216 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
217 void CreateHeader(size_t oat_loaded_size, size_t oat_data_offset)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700218 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
219 mirror::ObjectArray<mirror::Object>* CreateImageRoots() const
220 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800221 void CalculateObjectBinSlots(mirror::Object* obj)
222 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
223 void UnbinObjectsIntoOffset(mirror::Object* obj)
Mathieu Chartier590fee92013-09-13 13:46:47 -0700224 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
225
226 void WalkInstanceFields(mirror::Object* obj, mirror::Class* klass)
227 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
228 void WalkFieldsInOrder(mirror::Object* obj)
229 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
230 static void WalkFieldsCallback(mirror::Object* obj, void* arg)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700231 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800232 static void UnbinObjectsIntoOffsetCallback(mirror::Object* obj, void* arg)
233 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700234
235 // Creates the contiguous image in memory and adjusts pointers.
Mathieu Chartierfd04b6f2014-11-14 19:34:18 -0800236 void CopyAndFixupObjects() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700237 static void CopyAndFixupObjectsCallback(mirror::Object* obj, void* arg)
238 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800239 void FixupMethod(mirror::ArtMethod* orig, mirror::ArtMethod* copy)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700240 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800241 void FixupObject(mirror::Object* orig, mirror::Object* copy)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700242 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700243
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700244 // Get quick code for non-resolution/imt_conflict/abstract method.
Ian Rogers13735952014-10-08 12:43:28 -0700245 const uint8_t* GetQuickCode(mirror::ArtMethod* method, bool* quick_is_interpreted)
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700246 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
247
Ian Rogers13735952014-10-08 12:43:28 -0700248 const uint8_t* GetQuickEntryPoint(mirror::ArtMethod* method)
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700249 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
250
Brian Carlstrom7940e442013-07-12 13:46:57 -0700251 // Patches references in OatFile to expect runtime addresses.
Vladimir Markof4da6752014-08-01 19:04:18 +0100252 void SetOatChecksumFromElfFile(File* elf_file);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700253
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800254 // Calculate the sum total of the bin slot sizes in [0, up_to). Defaults to all bins.
255 size_t GetBinSizeSum(Bin up_to = kBinSize) const;
256
Brian Carlstrom7940e442013-07-12 13:46:57 -0700257 const CompilerDriver& compiler_driver_;
258
Vladimir Markof4da6752014-08-01 19:04:18 +0100259 // Beginning target image address for the output image.
Ian Rogers13735952014-10-08 12:43:28 -0700260 uint8_t* image_begin_;
Vladimir Markof4da6752014-08-01 19:04:18 +0100261
262 // Offset to the free space in image_.
263 size_t image_end_;
264
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800265 // Offset from image_begin_ to where the first object is in image_.
266 size_t image_objects_offset_begin_;
267
Vladimir Markof4da6752014-08-01 19:04:18 +0100268 // The image roots address in the image.
269 uint32_t image_roots_address_;
270
Brian Carlstrom7940e442013-07-12 13:46:57 -0700271 // oat file with code for this image
272 OatFile* oat_file_;
273
274 // Memory mapped for generating the image.
Ian Rogers700a4022014-05-19 16:49:03 -0700275 std::unique_ptr<MemMap> image_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700276
Mathieu Chartier590fee92013-09-13 13:46:47 -0700277 // Saved hashes (objects are inside of the image so that they don't move).
Ian Rogers700a4022014-05-19 16:49:03 -0700278 std::vector<std::pair<mirror::Object*, uint32_t>> saved_hashes_;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700279
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800280 // Saved hashes (objects are bin slots to inside of the image, not yet allocated an address).
281 std::map<BinSlot, uint32_t> saved_hashes_map_;
282
Brian Carlstrom7940e442013-07-12 13:46:57 -0700283 // Beginning target oat address for the pointers from the output image to its oat file.
Ian Rogers13735952014-10-08 12:43:28 -0700284 const uint8_t* oat_data_begin_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700285
Mathieu Chartier31e89252013-08-28 11:29:12 -0700286 // Image bitmap which lets us know where the objects inside of the image reside.
Ian Rogers700a4022014-05-19 16:49:03 -0700287 std::unique_ptr<gc::accounting::ContinuousSpaceBitmap> image_bitmap_;
Mathieu Chartier31e89252013-08-28 11:29:12 -0700288
Brian Carlstrom7940e442013-07-12 13:46:57 -0700289 // Offset from oat_data_begin_ to the stubs.
Ian Rogers848871b2013-08-05 10:56:33 -0700290 uint32_t interpreter_to_interpreter_bridge_offset_;
291 uint32_t interpreter_to_compiled_code_bridge_offset_;
292 uint32_t jni_dlsym_lookup_offset_;
Jeff Hao88474b42013-10-23 16:24:40 -0700293 uint32_t portable_imt_conflict_trampoline_offset_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700294 uint32_t portable_resolution_trampoline_offset_;
Ian Rogers848871b2013-08-05 10:56:33 -0700295 uint32_t portable_to_interpreter_bridge_offset_;
Andreas Gampe2da88232014-02-27 12:26:20 -0800296 uint32_t quick_generic_jni_trampoline_offset_;
Jeff Hao88474b42013-10-23 16:24:40 -0700297 uint32_t quick_imt_conflict_trampoline_offset_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700298 uint32_t quick_resolution_trampoline_offset_;
Ian Rogers848871b2013-08-05 10:56:33 -0700299 uint32_t quick_to_interpreter_bridge_offset_;
Igor Murashkin46774762014-10-22 11:37:02 -0700300 const bool compile_pic_;
Mathieu Chartierb7ea3ac2014-03-24 16:54:46 -0700301
Mathieu Chartier2d721012014-11-10 11:08:06 -0800302 // Size of pointers on the target architecture.
303 size_t target_ptr_size_;
304
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800305 // Bin slot tracking for dirty object packing
306 size_t bin_slot_sizes_[kBinSize]; // Number of bytes in a bin
307 size_t bin_slot_count_[kBinSize]; // Number of objects in a bin
308
Mathieu Chartierb7ea3ac2014-03-24 16:54:46 -0700309 friend class FixupVisitor;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700310 friend class FixupClassVisitor;
Mathieu Chartierb7ea3ac2014-03-24 16:54:46 -0700311 DISALLOW_COPY_AND_ASSIGN(ImageWriter);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700312};
313
314} // namespace art
315
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700316#endif // ART_COMPILER_IMAGE_WRITER_H_