blob: ae6c1c84abf2cd119d19a4f603f2c8f1d9a0e1a1 [file] [log] [blame]
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
3#include "image_writer.h"
4
5#include <sys/mman.h>
Elliott Hughes90a33692011-08-30 13:27:07 -07006
Brian Carlstromdb4d5402011-08-09 12:18:28 -07007#include <vector>
8
Elliott Hughes90a33692011-08-30 13:27:07 -07009#include "UniquePtr.h"
Brian Carlstroma663ea52011-08-19 23:33:41 -070010#include "class_linker.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070011#include "class_loader.h"
Elliott Hughes90a33692011-08-30 13:27:07 -070012#include "dex_cache.h"
Brian Carlstromdb4d5402011-08-09 12:18:28 -070013#include "file.h"
14#include "globals.h"
15#include "heap.h"
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070016#include "image.h"
Brian Carlstroma663ea52011-08-19 23:33:41 -070017#include "intern_table.h"
Brian Carlstromdb4d5402011-08-09 12:18:28 -070018#include "logging.h"
19#include "object.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070020#include "runtime.h"
Brian Carlstromdb4d5402011-08-09 12:18:28 -070021#include "space.h"
22#include "utils.h"
23
24namespace art {
25
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070026bool ImageWriter::Write(const char* filename, uintptr_t image_base) {
27 CHECK_NE(image_base, 0U);
28 image_base_ = reinterpret_cast<byte*>(image_base);
29
30 const std::vector<Space*>& spaces = Heap::GetSpaces();
31 // currently just write the last space, assuming it is the space that was being used for allocation
32 CHECK_GE(spaces.size(), 1U);
33 source_space_ = spaces[spaces.size()-1];
34
35 if (!Init()) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -070036 return false;
37 }
Brian Carlstrom693267a2011-09-06 09:25:34 -070038 Heap::CollectGarbage();
Brian Carlstromdb4d5402011-08-09 12:18:28 -070039 CalculateNewObjectOffsets();
40 CopyAndFixupObjects();
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070041
Elliott Hughes90a33692011-08-30 13:27:07 -070042 UniquePtr<File> file(OS::OpenFile(filename, true));
43 if (file.get() == NULL) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -070044 return false;
45 }
Brian Carlstrom4e777d42011-08-15 13:53:52 -070046 return file->WriteFully(image_->GetAddress(), image_top_);
Brian Carlstromdb4d5402011-08-09 12:18:28 -070047}
48
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070049bool ImageWriter::Init() {
50 size_t size = source_space_->Size();
Brian Carlstromdb4d5402011-08-09 12:18:28 -070051 int prot = PROT_READ | PROT_WRITE;
Brian Carlstromdb4d5402011-08-09 12:18:28 -070052 size_t length = RoundUp(size, kPageSize);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070053 image_.reset(MemMap::Map(length, prot));
Elliott Hughes90a33692011-08-30 13:27:07 -070054 if (image_.get() == NULL) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -070055 return false;
56 }
57 return true;
58}
59
Brian Carlstrom78128a62011-09-15 17:21:19 -070060void ImageWriter::CalculateNewObjectOffsetsCallback(Object* obj, void* arg) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -070061 DCHECK(obj != NULL);
62 DCHECK(arg != NULL);
63 ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070064 if (!image_writer->InSourceSpace(obj)) {
65 return;
66 }
Brian Carlstromc74255f2011-09-11 22:47:39 -070067
68 // if it is a string, we want to intern it if its not interned.
69 if (obj->IsString()) {
70 // we must be an interned string that was forward referenced and already assigned
71 if (IsImageOffsetAssigned(obj)) {
72 DCHECK_EQ(obj, obj->AsString()->Intern());
73 return;
74 }
75 String* interned = obj->AsString()->Intern();
76 if (obj != interned) {
77 if (!IsImageOffsetAssigned(interned)) {
78 // interned obj is after us, allocate its location early
79 image_writer->AssignImageOffset(interned);
80 }
81 // point those looking for this object to the interned version.
82 SetImageOffset(obj, GetImageOffset(interned));
83 return;
84 }
85 // else (obj == interned), nothing to do but fall through to the normal case
86 }
87
88 image_writer->AssignImageOffset(obj);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070089
90 // sniff out the DexCaches on this pass for use on the next pass
91 if (obj->IsClass()) {
92 Class* klass = obj->AsClass();
93 DexCache* dex_cache = klass->GetDexCache();
94 if (dex_cache != NULL) {
95 image_writer->dex_caches_.insert(dex_cache);
96 } else {
97 DCHECK(klass->IsArrayClass() || klass->IsPrimitive());
98 }
99 }
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700100}
101
Brian Carlstrom16192862011-09-12 17:50:06 -0700102ObjectArray<Object>* CreateImageRoots() {
103 // build a Object[] of the roots needed to restore the runtime
104 Runtime* runtime = Runtime::Current();
105 ClassLinker* class_linker = runtime->GetClassLinker();
106 Class* object_array_class = class_linker->FindSystemClass("[Ljava/lang/Object;");
107 ObjectArray<Object>* image_roots = ObjectArray<Object>::Alloc(object_array_class,
108 ImageHeader::kImageRootsMax);
109 image_roots->Set(ImageHeader::kJniStubArray, runtime->GetJniStubArray());
110 return image_roots;
111}
112
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700113void ImageWriter::CalculateNewObjectOffsets() {
Brian Carlstrom16192862011-09-12 17:50:06 -0700114 ObjectArray<Object>* image_roots = CreateImageRoots();
115
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700116 HeapBitmap* heap_bitmap = Heap::GetLiveBits();
117 DCHECK(heap_bitmap != NULL);
118 DCHECK_EQ(0U, image_top_);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700119
Brian Carlstrom16192862011-09-12 17:50:06 -0700120 // leave space for the header, but do not write it yet, we need to
121 // know where image_roots is going to end up
Brian Carlstroma663ea52011-08-19 23:33:41 -0700122 image_top_ += RoundUp(sizeof(ImageHeader), 8); // 64-bit-alignment
123
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700124 heap_bitmap->Walk(CalculateNewObjectOffsetsCallback, this); // TODO: add Space-limited Walk
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700125 DCHECK_LT(image_top_, image_->GetLength());
Brian Carlstroma663ea52011-08-19 23:33:41 -0700126
Brian Carlstrom16192862011-09-12 17:50:06 -0700127 // return to write header at start of image with future location of image_roots
128 ImageHeader image_header(reinterpret_cast<uint32_t>(image_base_),
129 reinterpret_cast<uint32_t>(GetImageAddress(image_roots)));
Brian Carlstroma663ea52011-08-19 23:33:41 -0700130 memcpy(image_->GetAddress(), &image_header, sizeof(image_header));
131
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700132 // Note that top_ is left at end of used space
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700133}
134
135void ImageWriter::CopyAndFixupObjects() {
136 HeapBitmap* heap_bitmap = Heap::GetLiveBits();
137 DCHECK(heap_bitmap != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700138 // TODO: heap validation can't handle this fix up pass
139 Heap::DisableObjectValidation();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700140 heap_bitmap->Walk(CopyAndFixupObjectsCallback, this); // TODO: add Space-limited Walk
141 FixupDexCaches();
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700142}
143
Brian Carlstrom78128a62011-09-15 17:21:19 -0700144void ImageWriter::CopyAndFixupObjectsCallback(Object* object, void* arg) {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700145 DCHECK(object != NULL);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700146 DCHECK(arg != NULL);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700147 const Object* obj = object;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700148 ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700149 if (!image_writer->InSourceSpace(object)) {
150 return;
151 }
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700152
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700153 // see GetLocalAddress for similar computation
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700154 size_t offset = image_writer->GetImageOffset(obj);
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700155 byte* dst = image_writer->image_->GetAddress() + offset;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700156 const byte* src = reinterpret_cast<const byte*>(obj);
Elliott Hughes04b63fd2011-08-16 09:40:10 -0700157 size_t n = obj->SizeOf();
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700158 DCHECK_LT(offset + n, image_writer->image_->GetLength());
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700159 memcpy(dst, src, n);
160 Object* copy = reinterpret_cast<Object*>(dst);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700161 ResetImageOffset(copy);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700162 image_writer->FixupObject(obj, copy);
163}
164
Brian Carlstrom4873d462011-08-21 15:23:39 -0700165void ImageWriter::FixupObject(const Object* orig, Object* copy) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700166 DCHECK(orig != NULL);
167 DCHECK(copy != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700168 copy->SetClass(down_cast<Class*>(GetImageAddress(orig->GetClass())));
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700169 // TODO: special case init of pointers to malloc data (or removal of these pointers)
170 if (orig->IsClass()) {
171 FixupClass(orig->AsClass(), down_cast<Class*>(copy));
172 } else if (orig->IsObjectArray()) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700173 FixupObjectArray(orig->AsObjectArray<Object>(), down_cast<ObjectArray<Object>*>(copy));
Brian Carlstrom16192862011-09-12 17:50:06 -0700174 } else if (orig->IsMethod()) {
175 FixupMethod(orig->AsMethod(), down_cast<Method*>(copy));
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700176 } else {
177 FixupInstanceFields(orig, copy);
178 }
179}
180
Brian Carlstrom4873d462011-08-21 15:23:39 -0700181void ImageWriter::FixupClass(const Class* orig, Class* copy) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700182 FixupInstanceFields(orig, copy);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700183 FixupStaticFields(orig, copy);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700184}
185
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700186const void* FixupCode(const ByteArray* copy_code_array, const void* orig_code) {
187 // TODO: change to DCHECK when all code compiling
188 if (copy_code_array == NULL) {
189 return NULL;
190 }
191 const void* copy_code = copy_code_array->GetData();
192 // TODO: remember InstructionSet with each code array so we know if we need to do thumb fixup?
193 if ((reinterpret_cast<uintptr_t>(orig_code) % 2) == 1) {
Brian Carlstrom16192862011-09-12 17:50:06 -0700194 return reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(copy_code) + 1);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700195 }
196 return copy_code;
197}
198
Brian Carlstrom4873d462011-08-21 15:23:39 -0700199void ImageWriter::FixupMethod(const Method* orig, Method* copy) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700200 FixupInstanceFields(orig, copy);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700201 copy->code_ = FixupCode(copy->code_array_, orig->code_);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700202 copy->invoke_stub_ = reinterpret_cast<Method::InvokeStub*>(FixupCode(copy->invoke_stub_array_, reinterpret_cast<void*>(orig->invoke_stub_)));
Brian Carlstrom16192862011-09-12 17:50:06 -0700203 if (orig->IsNative()) {
204 ByteArray* orig_jni_stub_array_ = Runtime::Current()->GetJniStubArray();
205 ByteArray* copy_jni_stub_array_ = down_cast<ByteArray*>(GetImageAddress(orig_jni_stub_array_));
206 copy->native_method_ = copy_jni_stub_array_->GetData();
207 } else {
208 DCHECK(copy->native_method_ == NULL);
209 }
Brian Carlstroma663ea52011-08-19 23:33:41 -0700210}
211
Brian Carlstrom4873d462011-08-21 15:23:39 -0700212void ImageWriter::FixupObjectArray(const ObjectArray<Object>* orig, ObjectArray<Object>* copy) {
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700213 for (int32_t i = 0; i < orig->GetLength(); ++i) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700214 const Object* element = orig->Get(i);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700215 copy->SetWithoutChecks(i, GetImageAddress(element));
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700216 }
217}
218
Brian Carlstrom4873d462011-08-21 15:23:39 -0700219void ImageWriter::FixupInstanceFields(const Object* orig, Object* copy) {
220 DCHECK(orig != NULL);
221 DCHECK(copy != NULL);
222 Class* klass = orig->GetClass();
223 DCHECK(klass != NULL);
224 FixupFields(orig,
225 copy,
226 klass->GetReferenceInstanceOffsets(),
227 false);
228}
229
230void ImageWriter::FixupStaticFields(const Class* orig, Class* copy) {
231 DCHECK(orig != NULL);
232 DCHECK(copy != NULL);
233 FixupFields(orig,
234 copy,
235 orig->GetReferenceStaticOffsets(),
236 true);
237}
238
239void ImageWriter::FixupFields(const Object* orig,
240 Object* copy,
241 uint32_t ref_offsets,
242 bool is_static) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700243 if (ref_offsets != CLASS_WALK_SUPER) {
244 // Found a reference offset bitmap. Fixup the specified offsets.
245 while (ref_offsets != 0) {
246 size_t right_shift = CLZ(ref_offsets);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700247 MemberOffset byte_offset = CLASS_OFFSET_FROM_CLZ(right_shift);
248 const Object* ref = orig->GetFieldObject<const Object*>(byte_offset, false);
249 copy->SetFieldObject(byte_offset, GetImageAddress(ref), false);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700250 ref_offsets &= ~(CLASS_HIGH_BIT >> right_shift);
251 }
252 } else {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700253 // There is no reference offset bitmap. In the non-static case,
254 // walk up the class inheritance hierarchy and find reference
255 // offsets the hard way. In the static case, just consider this
256 // class.
257 for (const Class *klass = is_static ? orig->AsClass() : orig->GetClass();
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700258 klass != NULL;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700259 klass = is_static ? NULL : klass->GetSuperClass()) {
260 size_t num_reference_fields = (is_static
261 ? klass->NumReferenceStaticFields()
262 : klass->NumReferenceInstanceFields());
263 for (size_t i = 0; i < num_reference_fields; ++i) {
264 Field* field = (is_static
265 ? klass->GetStaticField(i)
266 : klass->GetInstanceField(i));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700267 MemberOffset field_offset = field->GetOffset();
268 const Object* ref = orig->GetFieldObject<const Object*>(field_offset, false);
269 copy->SetFieldObject(field_offset, GetImageAddress(ref), false);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700270 }
271 }
272 }
273}
274
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700275void ImageWriter::FixupDexCaches() {
276 typedef Set::const_iterator It; // TODO: C++0x auto
277 for (It it = dex_caches_.begin(), end = dex_caches_.end(); it != end; ++it) {
278 DexCache* orig = *it;
279 DexCache* copy = down_cast<DexCache*>(GetLocalAddress(orig));
280 FixupDexCache(orig, copy);
281 }
282}
283
284void ImageWriter::FixupDexCache(const DexCache* orig, DexCache* copy) {
285 CHECK(orig != NULL);
286 CHECK(copy != NULL);
287
288 CodeAndDirectMethods* orig_cadms = orig->GetCodeAndDirectMethods();
289 CodeAndDirectMethods* copy_cadms = down_cast<CodeAndDirectMethods*>(GetLocalAddress(orig_cadms));
290 for (size_t i = 0; i < orig->NumResolvedMethods(); i++) {
291 Method* orig_method = orig->GetResolvedMethod(i);
292 // if it was resolved in the original, resolve it in the copy
293 if (orig_method != NULL
294 && InSourceSpace(orig_method)
295 && orig_method == orig_cadms->GetResolvedMethod(i)) {
296 Method* copy_method = down_cast<Method*>(GetLocalAddress(orig_method));
297 copy_cadms->Set(CodeAndDirectMethods::CodeIndex(i),
298 reinterpret_cast<int32_t>(copy_method->code_));
299 copy_cadms->Set(CodeAndDirectMethods::MethodIndex(i),
300 reinterpret_cast<int32_t>(GetImageAddress(orig_method)));
301 }
302 }
303}
304
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700305} // namespace art