blob: 23bc6cf8db6839d0cab30e06c16b3abb44262972 [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());
Ian Rogersff1ed472011-09-20 13:46:24 -0700110 image_roots->Set(ImageHeader::kCalleeSaveMethod, runtime->GetCalleeSaveMethod());
Brian Carlstrom16192862011-09-12 17:50:06 -0700111 return image_roots;
112}
113
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700114void ImageWriter::CalculateNewObjectOffsets() {
Brian Carlstrom16192862011-09-12 17:50:06 -0700115 ObjectArray<Object>* image_roots = CreateImageRoots();
116
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700117 HeapBitmap* heap_bitmap = Heap::GetLiveBits();
118 DCHECK(heap_bitmap != NULL);
119 DCHECK_EQ(0U, image_top_);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700120
Brian Carlstrom16192862011-09-12 17:50:06 -0700121 // leave space for the header, but do not write it yet, we need to
122 // know where image_roots is going to end up
Brian Carlstroma663ea52011-08-19 23:33:41 -0700123 image_top_ += RoundUp(sizeof(ImageHeader), 8); // 64-bit-alignment
124
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700125 heap_bitmap->Walk(CalculateNewObjectOffsetsCallback, this); // TODO: add Space-limited Walk
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700126 DCHECK_LT(image_top_, image_->GetLength());
Brian Carlstroma663ea52011-08-19 23:33:41 -0700127
Brian Carlstrom16192862011-09-12 17:50:06 -0700128 // return to write header at start of image with future location of image_roots
129 ImageHeader image_header(reinterpret_cast<uint32_t>(image_base_),
130 reinterpret_cast<uint32_t>(GetImageAddress(image_roots)));
Brian Carlstroma663ea52011-08-19 23:33:41 -0700131 memcpy(image_->GetAddress(), &image_header, sizeof(image_header));
132
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700133 // Note that top_ is left at end of used space
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700134}
135
136void ImageWriter::CopyAndFixupObjects() {
137 HeapBitmap* heap_bitmap = Heap::GetLiveBits();
138 DCHECK(heap_bitmap != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700139 // TODO: heap validation can't handle this fix up pass
140 Heap::DisableObjectValidation();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700141 heap_bitmap->Walk(CopyAndFixupObjectsCallback, this); // TODO: add Space-limited Walk
142 FixupDexCaches();
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700143}
144
Brian Carlstrom78128a62011-09-15 17:21:19 -0700145void ImageWriter::CopyAndFixupObjectsCallback(Object* object, void* arg) {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700146 DCHECK(object != NULL);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700147 DCHECK(arg != NULL);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700148 const Object* obj = object;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700149 ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700150 if (!image_writer->InSourceSpace(object)) {
151 return;
152 }
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700153
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700154 // see GetLocalAddress for similar computation
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700155 size_t offset = image_writer->GetImageOffset(obj);
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700156 byte* dst = image_writer->image_->GetAddress() + offset;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700157 const byte* src = reinterpret_cast<const byte*>(obj);
Elliott Hughes04b63fd2011-08-16 09:40:10 -0700158 size_t n = obj->SizeOf();
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700159 DCHECK_LT(offset + n, image_writer->image_->GetLength());
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700160 memcpy(dst, src, n);
161 Object* copy = reinterpret_cast<Object*>(dst);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700162 ResetImageOffset(copy);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700163 image_writer->FixupObject(obj, copy);
164}
165
Brian Carlstrom4873d462011-08-21 15:23:39 -0700166void ImageWriter::FixupObject(const Object* orig, Object* copy) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700167 DCHECK(orig != NULL);
168 DCHECK(copy != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700169 copy->SetClass(down_cast<Class*>(GetImageAddress(orig->GetClass())));
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700170 // TODO: special case init of pointers to malloc data (or removal of these pointers)
171 if (orig->IsClass()) {
172 FixupClass(orig->AsClass(), down_cast<Class*>(copy));
173 } else if (orig->IsObjectArray()) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700174 FixupObjectArray(orig->AsObjectArray<Object>(), down_cast<ObjectArray<Object>*>(copy));
Brian Carlstrom16192862011-09-12 17:50:06 -0700175 } else if (orig->IsMethod()) {
176 FixupMethod(orig->AsMethod(), down_cast<Method*>(copy));
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700177 } else {
178 FixupInstanceFields(orig, copy);
179 }
180}
181
Brian Carlstrom4873d462011-08-21 15:23:39 -0700182void ImageWriter::FixupClass(const Class* orig, Class* copy) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700183 FixupInstanceFields(orig, copy);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700184 FixupStaticFields(orig, copy);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700185}
186
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700187const void* FixupCode(const ByteArray* copy_code_array, const void* orig_code) {
188 // TODO: change to DCHECK when all code compiling
189 if (copy_code_array == NULL) {
190 return NULL;
191 }
192 const void* copy_code = copy_code_array->GetData();
193 // TODO: remember InstructionSet with each code array so we know if we need to do thumb fixup?
194 if ((reinterpret_cast<uintptr_t>(orig_code) % 2) == 1) {
Brian Carlstrom16192862011-09-12 17:50:06 -0700195 return reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(copy_code) + 1);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700196 }
197 return copy_code;
198}
199
Brian Carlstrom4873d462011-08-21 15:23:39 -0700200void ImageWriter::FixupMethod(const Method* orig, Method* copy) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700201 FixupInstanceFields(orig, copy);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700202 copy->code_ = FixupCode(copy->code_array_, orig->code_);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700203 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 -0700204 if (orig->IsNative()) {
205 ByteArray* orig_jni_stub_array_ = Runtime::Current()->GetJniStubArray();
206 ByteArray* copy_jni_stub_array_ = down_cast<ByteArray*>(GetImageAddress(orig_jni_stub_array_));
207 copy->native_method_ = copy_jni_stub_array_->GetData();
208 } else {
209 DCHECK(copy->native_method_ == NULL);
210 }
Brian Carlstroma663ea52011-08-19 23:33:41 -0700211}
212
Brian Carlstrom4873d462011-08-21 15:23:39 -0700213void ImageWriter::FixupObjectArray(const ObjectArray<Object>* orig, ObjectArray<Object>* copy) {
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700214 for (int32_t i = 0; i < orig->GetLength(); ++i) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700215 const Object* element = orig->Get(i);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700216 copy->SetWithoutChecks(i, GetImageAddress(element));
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700217 }
218}
219
Brian Carlstrom4873d462011-08-21 15:23:39 -0700220void ImageWriter::FixupInstanceFields(const Object* orig, Object* copy) {
221 DCHECK(orig != NULL);
222 DCHECK(copy != NULL);
223 Class* klass = orig->GetClass();
224 DCHECK(klass != NULL);
225 FixupFields(orig,
226 copy,
227 klass->GetReferenceInstanceOffsets(),
228 false);
229}
230
231void ImageWriter::FixupStaticFields(const Class* orig, Class* copy) {
232 DCHECK(orig != NULL);
233 DCHECK(copy != NULL);
234 FixupFields(orig,
235 copy,
236 orig->GetReferenceStaticOffsets(),
237 true);
238}
239
240void ImageWriter::FixupFields(const Object* orig,
241 Object* copy,
242 uint32_t ref_offsets,
243 bool is_static) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700244 if (ref_offsets != CLASS_WALK_SUPER) {
245 // Found a reference offset bitmap. Fixup the specified offsets.
246 while (ref_offsets != 0) {
247 size_t right_shift = CLZ(ref_offsets);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700248 MemberOffset byte_offset = CLASS_OFFSET_FROM_CLZ(right_shift);
249 const Object* ref = orig->GetFieldObject<const Object*>(byte_offset, false);
250 copy->SetFieldObject(byte_offset, GetImageAddress(ref), false);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700251 ref_offsets &= ~(CLASS_HIGH_BIT >> right_shift);
252 }
253 } else {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700254 // There is no reference offset bitmap. In the non-static case,
255 // walk up the class inheritance hierarchy and find reference
256 // offsets the hard way. In the static case, just consider this
257 // class.
258 for (const Class *klass = is_static ? orig->AsClass() : orig->GetClass();
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700259 klass != NULL;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700260 klass = is_static ? NULL : klass->GetSuperClass()) {
261 size_t num_reference_fields = (is_static
262 ? klass->NumReferenceStaticFields()
263 : klass->NumReferenceInstanceFields());
264 for (size_t i = 0; i < num_reference_fields; ++i) {
265 Field* field = (is_static
266 ? klass->GetStaticField(i)
267 : klass->GetInstanceField(i));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700268 MemberOffset field_offset = field->GetOffset();
269 const Object* ref = orig->GetFieldObject<const Object*>(field_offset, false);
270 copy->SetFieldObject(field_offset, GetImageAddress(ref), false);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700271 }
272 }
273 }
274}
275
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700276void ImageWriter::FixupDexCaches() {
277 typedef Set::const_iterator It; // TODO: C++0x auto
278 for (It it = dex_caches_.begin(), end = dex_caches_.end(); it != end; ++it) {
279 DexCache* orig = *it;
280 DexCache* copy = down_cast<DexCache*>(GetLocalAddress(orig));
281 FixupDexCache(orig, copy);
282 }
283}
284
285void ImageWriter::FixupDexCache(const DexCache* orig, DexCache* copy) {
286 CHECK(orig != NULL);
287 CHECK(copy != NULL);
288
289 CodeAndDirectMethods* orig_cadms = orig->GetCodeAndDirectMethods();
290 CodeAndDirectMethods* copy_cadms = down_cast<CodeAndDirectMethods*>(GetLocalAddress(orig_cadms));
291 for (size_t i = 0; i < orig->NumResolvedMethods(); i++) {
292 Method* orig_method = orig->GetResolvedMethod(i);
293 // if it was resolved in the original, resolve it in the copy
294 if (orig_method != NULL
295 && InSourceSpace(orig_method)
296 && orig_method == orig_cadms->GetResolvedMethod(i)) {
297 Method* copy_method = down_cast<Method*>(GetLocalAddress(orig_method));
298 copy_cadms->Set(CodeAndDirectMethods::CodeIndex(i),
299 reinterpret_cast<int32_t>(copy_method->code_));
300 copy_cadms->Set(CodeAndDirectMethods::MethodIndex(i),
301 reinterpret_cast<int32_t>(GetImageAddress(orig_method)));
302 }
303 }
304}
305
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700306} // namespace art