blob: 53f5ce4545bfd575475a6a88dc8c22835594d390 [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),
Elliott Hughes956af0f2014-12-11 14:34:28 -080051 quick_generic_jni_trampoline_offset_(0),
Vladimir Markof4da6752014-08-01 19:04:18 +010052 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
Andreas Gampe245ee002014-12-04 21:25:04 -080059 ~ImageWriter() {
60 // For interned strings a large array is allocated to hold all the character data and avoid
61 // overhead. However, no GC is run anymore at this point. As the array is likely large, it
62 // will be allocated in the large object space, where valgrind can track every single
63 // allocation. Not explicitly freeing that array will be recognized as a leak.
64 if (RUNNING_ON_VALGRIND != 0) {
65 FreeStringDataArray();
66 }
67 }
Brian Carlstrom7940e442013-07-12 13:46:57 -070068
Vladimir Markof4da6752014-08-01 19:04:18 +010069 bool PrepareImageAddressSpace();
70
71 bool IsImageAddressSpaceReady() const {
72 return image_roots_address_ != 0u;
73 }
74
75 mirror::Object* GetImageAddress(mirror::Object* object) const
76 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Igor Murashkin46774762014-10-22 11:37:02 -070077 if (object == nullptr) {
78 return nullptr;
Vladimir Markof4da6752014-08-01 19:04:18 +010079 }
80 return reinterpret_cast<mirror::Object*>(image_begin_ + GetImageOffset(object));
81 }
82
Ian Rogers13735952014-10-08 12:43:28 -070083 uint8_t* GetOatFileBegin() const {
Vladimir Markof4da6752014-08-01 19:04:18 +010084 return image_begin_ + RoundUp(image_end_, kPageSize);
85 }
86
Brian Carlstrom7940e442013-07-12 13:46:57 -070087 bool Write(const std::string& image_filename,
Brian Carlstrom7940e442013-07-12 13:46:57 -070088 const std::string& oat_filename,
89 const std::string& oat_location)
90 LOCKS_EXCLUDED(Locks::mutator_lock_);
91
92 uintptr_t GetOatDataBegin() {
93 return reinterpret_cast<uintptr_t>(oat_data_begin_);
94 }
95
96 private:
97 bool AllocMemory();
98
Mathieu Chartier31e89252013-08-28 11:29:12 -070099 // Mark the objects defined in this space in the given live bitmap.
100 void RecordImageAllocations() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
101
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800102 // Classify different kinds of bins that objects end up getting packed into during image writing.
103 enum Bin {
104 // Likely-clean:
105 kBinString, // [String] Almost always immutable (except for obj header).
106 kBinArtMethodsManagedInitialized, // [ArtMethod] Not-native, and initialized. Unlikely to dirty
107 // Unknown mix of clean/dirty:
108 kBinRegular,
109 // Likely-dirty:
110 // All classes get their own bins since their fields often dirty
111 kBinClassInitializedFinalStatics, // Class initializers have been run, no non-final statics
112 kBinClassInitialized, // Class initializers have been run
113 kBinClassVerified, // Class verified, but initializers haven't been run
114 kBinArtMethodNative, // Art method that is actually native
115 kBinArtMethodNotInitialized, // Art method with a declaring class that wasn't initialized
116 // Don't care about other art methods since they don't dirty
117 // Add more bins here if we add more segregation code.
118 kBinSize,
119 };
120
121 friend std::ostream& operator<<(std::ostream& stream, const Bin& bin);
122
123 static constexpr size_t kBinBits = MinimumBitsToStore(kBinSize - 1);
124 // uint32 = typeof(lockword_)
125 static constexpr size_t kBinShift = BitSizeOf<uint32_t>() - kBinBits;
126 // 111000.....0
127 static constexpr size_t kBinMask = ((static_cast<size_t>(1) << kBinBits) - 1) << kBinShift;
128
129 // We use the lock word to store the bin # and bin index of the object in the image.
130 //
131 // The struct size must be exactly sizeof(LockWord), currently 32-bits, since this will end up
132 // stored in the lock word bit-for-bit when object forwarding addresses are being calculated.
133 struct BinSlot {
134 explicit BinSlot(uint32_t lockword);
135 BinSlot(Bin bin, uint32_t index);
136
137 // The bin an object belongs to, i.e. regular, class/verified, class/initialized, etc.
138 Bin GetBin() const;
139 // The offset in bytes from the beginning of the bin. Aligned to object size.
140 uint32_t GetIndex() const;
141 // Pack into a single uint32_t, for storing into a lock word.
142 explicit operator uint32_t() const { return lockword_; }
143 // Comparison operator for map support
144 bool operator<(const BinSlot& other) const { return lockword_ < other.lockword_; }
145
146 private:
147 // Must be the same size as LockWord, any larger and we would truncate the data.
148 const uint32_t lockword_;
149 };
150
Mathieu Chartier31e89252013-08-28 11:29:12 -0700151 // We use the lock word to store the offset of the object in the image.
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800152 void AssignImageOffset(mirror::Object* object, BinSlot bin_slot)
153 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
154 void SetImageOffset(mirror::Object* object, BinSlot bin_slot, size_t offset)
Mathieu Chartier590fee92013-09-13 13:46:47 -0700155 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700156 bool IsImageOffsetAssigned(mirror::Object* object) const
157 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
158 size_t GetImageOffset(mirror::Object* object) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700159
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800160 void AssignImageBinSlot(mirror::Object* object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
161 void SetImageBinSlot(mirror::Object* object, BinSlot bin_slot)
162 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
163 bool IsImageBinSlotAssigned(mirror::Object* object) const
164 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
165 BinSlot GetImageBinSlot(mirror::Object* object) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
166
Alex Lighta59dd802014-07-02 16:28:08 -0700167 static void* GetImageAddressCallback(void* writer, mirror::Object* obj)
168 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
169 return reinterpret_cast<ImageWriter*>(writer)->GetImageAddress(obj);
170 }
171
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700172 mirror::Object* GetLocalAddress(mirror::Object* object) const
173 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700174 size_t offset = GetImageOffset(object);
Ian Rogers13735952014-10-08 12:43:28 -0700175 uint8_t* dst = image_->Begin() + offset;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700176 return reinterpret_cast<mirror::Object*>(dst);
177 }
178
Ian Rogers13735952014-10-08 12:43:28 -0700179 const uint8_t* GetOatAddress(uint32_t offset) const {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700180 // With Quick, code is within the OatFile, as there are all in one
Elliott Hughes956af0f2014-12-11 14:34:28 -0800181 // .o ELF object.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700182 DCHECK_LT(offset, oat_file_->Size());
Igor Murashkin46774762014-10-22 11:37:02 -0700183 if (offset == 0u) {
184 return nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700185 }
186 return oat_data_begin_ + offset;
187 }
188
189 // Returns true if the class was in the original requested image classes list.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800190 bool IsImageClass(mirror::Class* klass) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700191
192 // Debug aid that list of requested image classes.
193 void DumpImageClasses();
194
195 // Preinitializes some otherwise lazy fields (such as Class name) to avoid runtime image dirtying.
196 void ComputeLazyFieldsForImageClasses()
197 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
198 static bool ComputeLazyFieldsForClassesVisitor(mirror::Class* klass, void* arg)
199 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
200
201 // Wire dex cache resolved strings to strings in the image to avoid runtime resolution.
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700202 void ComputeEagerResolvedStrings() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700203 static void ComputeEagerResolvedStringsCallback(mirror::Object* obj, void* arg)
204 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
205
Mathieu Chartierfd04b6f2014-11-14 19:34:18 -0800206 // Combine string char arrays.
207 void ProcessStrings() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
208
Brian Carlstrom7940e442013-07-12 13:46:57 -0700209 // Remove unwanted classes from various roots.
210 void PruneNonImageClasses() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
211 static bool NonImageClassesVisitor(mirror::Class* c, void* arg)
212 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
213
214 // Verify unwanted classes removed.
Mathieu Chartierfd04b6f2014-11-14 19:34:18 -0800215 void CheckNonImageClassesRemoved() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700216 static void CheckNonImageClassesRemovedCallback(mirror::Object* obj, void* arg)
217 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
218
219 // Lays out where the image objects will be at runtime.
Vladimir Markof4da6752014-08-01 19:04:18 +0100220 void CalculateNewObjectOffsets()
221 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
222 void CreateHeader(size_t oat_loaded_size, size_t oat_data_offset)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700223 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
224 mirror::ObjectArray<mirror::Object>* CreateImageRoots() const
225 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800226 void CalculateObjectBinSlots(mirror::Object* obj)
227 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
228 void UnbinObjectsIntoOffset(mirror::Object* obj)
Mathieu Chartier590fee92013-09-13 13:46:47 -0700229 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
230
231 void WalkInstanceFields(mirror::Object* obj, mirror::Class* klass)
232 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
233 void WalkFieldsInOrder(mirror::Object* obj)
234 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
235 static void WalkFieldsCallback(mirror::Object* obj, void* arg)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700236 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800237 static void UnbinObjectsIntoOffsetCallback(mirror::Object* obj, void* arg)
238 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700239
240 // Creates the contiguous image in memory and adjusts pointers.
Mathieu Chartierfd04b6f2014-11-14 19:34:18 -0800241 void CopyAndFixupObjects() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700242 static void CopyAndFixupObjectsCallback(mirror::Object* obj, void* arg)
243 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800244 void FixupMethod(mirror::ArtMethod* orig, mirror::ArtMethod* copy)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700245 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800246 void FixupObject(mirror::Object* orig, mirror::Object* copy)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700247 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700248
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700249 // Get quick code for non-resolution/imt_conflict/abstract method.
Ian Rogers13735952014-10-08 12:43:28 -0700250 const uint8_t* GetQuickCode(mirror::ArtMethod* method, bool* quick_is_interpreted)
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700251 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
252
Ian Rogers13735952014-10-08 12:43:28 -0700253 const uint8_t* GetQuickEntryPoint(mirror::ArtMethod* method)
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700254 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
255
Brian Carlstrom7940e442013-07-12 13:46:57 -0700256 // Patches references in OatFile to expect runtime addresses.
Vladimir Markof4da6752014-08-01 19:04:18 +0100257 void SetOatChecksumFromElfFile(File* elf_file);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700258
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800259 // Calculate the sum total of the bin slot sizes in [0, up_to). Defaults to all bins.
260 size_t GetBinSizeSum(Bin up_to = kBinSize) const;
261
Andreas Gampe245ee002014-12-04 21:25:04 -0800262 // Release the string_data_array_.
263 void FreeStringDataArray();
264
Brian Carlstrom7940e442013-07-12 13:46:57 -0700265 const CompilerDriver& compiler_driver_;
266
Vladimir Markof4da6752014-08-01 19:04:18 +0100267 // Beginning target image address for the output image.
Ian Rogers13735952014-10-08 12:43:28 -0700268 uint8_t* image_begin_;
Vladimir Markof4da6752014-08-01 19:04:18 +0100269
270 // Offset to the free space in image_.
271 size_t image_end_;
272
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800273 // Offset from image_begin_ to where the first object is in image_.
274 size_t image_objects_offset_begin_;
275
Vladimir Markof4da6752014-08-01 19:04:18 +0100276 // The image roots address in the image.
277 uint32_t image_roots_address_;
278
Brian Carlstrom7940e442013-07-12 13:46:57 -0700279 // oat file with code for this image
280 OatFile* oat_file_;
281
282 // Memory mapped for generating the image.
Ian Rogers700a4022014-05-19 16:49:03 -0700283 std::unique_ptr<MemMap> image_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700284
Mathieu Chartier590fee92013-09-13 13:46:47 -0700285 // Saved hashes (objects are inside of the image so that they don't move).
Ian Rogers700a4022014-05-19 16:49:03 -0700286 std::vector<std::pair<mirror::Object*, uint32_t>> saved_hashes_;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700287
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800288 // Saved hashes (objects are bin slots to inside of the image, not yet allocated an address).
289 std::map<BinSlot, uint32_t> saved_hashes_map_;
290
Brian Carlstrom7940e442013-07-12 13:46:57 -0700291 // Beginning target oat address for the pointers from the output image to its oat file.
Ian Rogers13735952014-10-08 12:43:28 -0700292 const uint8_t* oat_data_begin_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700293
Mathieu Chartier31e89252013-08-28 11:29:12 -0700294 // Image bitmap which lets us know where the objects inside of the image reside.
Ian Rogers700a4022014-05-19 16:49:03 -0700295 std::unique_ptr<gc::accounting::ContinuousSpaceBitmap> image_bitmap_;
Mathieu Chartier31e89252013-08-28 11:29:12 -0700296
Brian Carlstrom7940e442013-07-12 13:46:57 -0700297 // Offset from oat_data_begin_ to the stubs.
Ian Rogers848871b2013-08-05 10:56:33 -0700298 uint32_t interpreter_to_interpreter_bridge_offset_;
299 uint32_t interpreter_to_compiled_code_bridge_offset_;
300 uint32_t jni_dlsym_lookup_offset_;
Andreas Gampe2da88232014-02-27 12:26:20 -0800301 uint32_t quick_generic_jni_trampoline_offset_;
Jeff Hao88474b42013-10-23 16:24:40 -0700302 uint32_t quick_imt_conflict_trampoline_offset_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700303 uint32_t quick_resolution_trampoline_offset_;
Ian Rogers848871b2013-08-05 10:56:33 -0700304 uint32_t quick_to_interpreter_bridge_offset_;
Igor Murashkin46774762014-10-22 11:37:02 -0700305 const bool compile_pic_;
Mathieu Chartierb7ea3ac2014-03-24 16:54:46 -0700306
Mathieu Chartier2d721012014-11-10 11:08:06 -0800307 // Size of pointers on the target architecture.
308 size_t target_ptr_size_;
309
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800310 // Bin slot tracking for dirty object packing
311 size_t bin_slot_sizes_[kBinSize]; // Number of bytes in a bin
312 size_t bin_slot_count_[kBinSize]; // Number of objects in a bin
313
Andreas Gampe245ee002014-12-04 21:25:04 -0800314 void* string_data_array_; // The backing for the interned strings.
315
Mathieu Chartierb7ea3ac2014-03-24 16:54:46 -0700316 friend class FixupVisitor;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700317 friend class FixupClassVisitor;
Mathieu Chartierb7ea3ac2014-03-24 16:54:46 -0700318 DISALLOW_COPY_AND_ASSIGN(ImageWriter);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700319};
320
321} // namespace art
322
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700323#endif // ART_COMPILER_IMAGE_WRITER_H_