blob: 39981404e154d0e8fdd65cb01bf8ebf5405c8a7b [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>
6#include <vector>
7
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07008#include "dex_cache.h"
Brian Carlstroma663ea52011-08-19 23:33:41 -07009#include "class_linker.h"
Brian Carlstromdb4d5402011-08-09 12:18:28 -070010#include "file.h"
11#include "globals.h"
12#include "heap.h"
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070013#include "image.h"
Brian Carlstroma663ea52011-08-19 23:33:41 -070014#include "intern_table.h"
Brian Carlstromdb4d5402011-08-09 12:18:28 -070015#include "logging.h"
16#include "object.h"
17#include "space.h"
18#include "utils.h"
19
20namespace art {
21
22bool ImageWriter::Write(Space* space, const char* filename, byte* image_base) {
23 image_base_ = image_base;
24 if (!Init(space)) {
25 return false;
26 }
27 CalculateNewObjectOffsets();
28 CopyAndFixupObjects();
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070029
Brian Carlstrom4e777d42011-08-15 13:53:52 -070030 scoped_ptr<File> file(OS::OpenFile(filename, true));
Brian Carlstromdb4d5402011-08-09 12:18:28 -070031 if (file == NULL) {
32 return false;
33 }
Brian Carlstrom4e777d42011-08-15 13:53:52 -070034 return file->WriteFully(image_->GetAddress(), image_top_);
Brian Carlstromdb4d5402011-08-09 12:18:28 -070035}
36
37bool ImageWriter::Init(Space* space) {
38 size_t size = space->Size();
39 int prot = PROT_READ | PROT_WRITE;
Brian Carlstromdb4d5402011-08-09 12:18:28 -070040 size_t length = RoundUp(size, kPageSize);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070041 image_.reset(MemMap::Map(length, prot));
Brian Carlstrom4e777d42011-08-15 13:53:52 -070042 if (image_ == NULL) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -070043 return false;
44 }
45 return true;
46}
47
Brian Carlstroma663ea52011-08-19 23:33:41 -070048namespace {
49
50struct InternTableVisitorState {
51 int index;
52 ObjectArray<Object>* interned_array;
53};
54
55void InternTableVisitor(Object* obj, void* arg) {
56 InternTableVisitorState* state = reinterpret_cast<InternTableVisitorState*>(arg);
57 state->interned_array->Set(state->index++, obj);
58}
59
60ObjectArray<Object>* CreateInternedArray() {
61 // build a Object[] of the interned strings for reinit
62 // TODO: avoid creating this future garbage
63 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
64 const InternTable& intern_table = class_linker->GetInternTable();
65 size_t size = intern_table.Size();
66 CHECK_NE(0U, size);
67
68 Class* object_array_class = class_linker->FindSystemClass("[Ljava/lang/Object;");
69 ObjectArray<Object>* interned_array = ObjectArray<Object>::Alloc(object_array_class, size);
70
71 InternTableVisitorState state;
72 state.index = 0;
73 state.interned_array = interned_array;
74
75 intern_table.VisitRoots(InternTableVisitor, &state);
76
77 return interned_array;
78}
79
80} // namespace
81
Brian Carlstrom4873d462011-08-21 15:23:39 -070082void ImageWriter::CalculateNewObjectOffsetsCallback(Object* obj, void *arg) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -070083 DCHECK(obj != NULL);
84 DCHECK(arg != NULL);
85 ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg);
Brian Carlstrom4e777d42011-08-15 13:53:52 -070086 image_writer->SetImageOffset(obj, image_writer->image_top_);
Elliott Hughes04b63fd2011-08-16 09:40:10 -070087 image_writer->image_top_ += RoundUp(obj->SizeOf(), 8); // 64-bit alignment
Brian Carlstrom4e777d42011-08-15 13:53:52 -070088 DCHECK_LT(image_writer->image_top_, image_writer->image_->GetLength());
89}
90
91void ImageWriter::CalculateNewObjectOffsets() {
Brian Carlstroma663ea52011-08-19 23:33:41 -070092 ObjectArray<Object>* interned_array = CreateInternedArray();
93
Brian Carlstrom4e777d42011-08-15 13:53:52 -070094 HeapBitmap* heap_bitmap = Heap::GetLiveBits();
95 DCHECK(heap_bitmap != NULL);
96 DCHECK_EQ(0U, image_top_);
Brian Carlstroma663ea52011-08-19 23:33:41 -070097
98 // leave space for the header, but do not write it yet, we need to
99 // know where interned_array is going to end up
100 image_top_ += RoundUp(sizeof(ImageHeader), 8); // 64-bit-alignment
101
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700102 heap_bitmap->Walk(CalculateNewObjectOffsetsCallback, this);
103 DCHECK_LT(image_top_, image_->GetLength());
Brian Carlstroma663ea52011-08-19 23:33:41 -0700104
105 // return to write header at start of image with future location of interned_array
106 ImageHeader image_header(reinterpret_cast<uint32_t>(image_base_),
107 reinterpret_cast<uint32_t>(GetImageAddress(interned_array)));
108 memcpy(image_->GetAddress(), &image_header, sizeof(image_header));
109
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700110 // Note that top_ is left at end of used space
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700111}
112
113void ImageWriter::CopyAndFixupObjects() {
114 HeapBitmap* heap_bitmap = Heap::GetLiveBits();
115 DCHECK(heap_bitmap != NULL);
116 heap_bitmap->Walk(CopyAndFixupObjectsCallback, this);
117}
118
Brian Carlstrom4873d462011-08-21 15:23:39 -0700119void ImageWriter::CopyAndFixupObjectsCallback(Object* object, void *arg) {
120 DCHECK(object != NULL);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700121 DCHECK(arg != NULL);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700122 const Object* obj = object;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700123 ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700124
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700125 size_t offset = image_writer->GetImageOffset(obj);
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700126 byte* dst = image_writer->image_->GetAddress() + offset;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700127 const byte* src = reinterpret_cast<const byte*>(obj);
Elliott Hughes04b63fd2011-08-16 09:40:10 -0700128 size_t n = obj->SizeOf();
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700129 DCHECK_LT(offset + n, image_writer->image_->GetLength());
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700130 memcpy(dst, src, n);
131 Object* copy = reinterpret_cast<Object*>(dst);
132 image_writer->FixupObject(obj, copy);
133}
134
Brian Carlstrom4873d462011-08-21 15:23:39 -0700135void ImageWriter::FixupObject(const Object* orig, Object* copy) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700136 DCHECK(orig != NULL);
137 DCHECK(copy != NULL);
138 copy->klass_ = down_cast<Class*>(GetImageAddress(orig->klass_));
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700139 // TODO: special case init of pointers to malloc data (or removal of these pointers)
140 if (orig->IsClass()) {
141 FixupClass(orig->AsClass(), down_cast<Class*>(copy));
Brian Carlstroma663ea52011-08-19 23:33:41 -0700142 } else if (orig->IsMethod()) {
143 FixupMethod(orig->AsMethod(), down_cast<Method*>(copy));
144 } else if (orig->IsField()) {
145 FixupField(orig->AsField(), down_cast<Field*>(copy));
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700146 } else if (orig->IsObjectArray()) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700147 FixupObjectArray(orig->AsObjectArray<Object>(), down_cast<ObjectArray<Object>*>(copy));
148 } else {
149 FixupInstanceFields(orig, copy);
150 }
151}
152
Brian Carlstrom4873d462011-08-21 15:23:39 -0700153void ImageWriter::FixupClass(const Class* orig, Class* copy) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700154 FixupInstanceFields(orig, copy);
155 copy->descriptor_ = down_cast<String*>(GetImageAddress(orig->descriptor_));
156 copy->dex_cache_ = down_cast<DexCache*>(GetImageAddress(orig->dex_cache_));
157 copy->verify_error_class_ = down_cast<Class*>(GetImageAddress(orig->verify_error_class_));
158 copy->component_type_ = down_cast<Class*>(GetImageAddress(orig->component_type_));
159 copy->super_class_ = down_cast<Class*>(GetImageAddress(orig->super_class_));
160 copy->class_loader_ = down_cast<ClassLoader*>(GetImageAddress(orig->class_loader_));
161 copy->interfaces_ = down_cast<ObjectArray<Class>*>(GetImageAddress(orig->interfaces_));
162 copy->direct_methods_ = down_cast<ObjectArray<Method>*>(GetImageAddress(orig->direct_methods_));
163 copy->virtual_methods_ = down_cast<ObjectArray<Method>*>(GetImageAddress(orig->virtual_methods_));
164 copy->vtable_ = down_cast<ObjectArray<Method>*>(GetImageAddress(orig->vtable_));
165 // TODO: convert iftable_ to heap allocated storage
166 copy->ifields_ = down_cast<ObjectArray<Field>*>(GetImageAddress(orig->ifields_));
Brian Carlstrom4873d462011-08-21 15:23:39 -0700167 // TODO: convert source_file_ to heap allocated storage
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700168 copy->sfields_ = down_cast<ObjectArray<Field>*>(GetImageAddress(orig->sfields_));
Brian Carlstrom4873d462011-08-21 15:23:39 -0700169 FixupStaticFields(orig, copy);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700170}
171
Brian Carlstroma663ea52011-08-19 23:33:41 -0700172// TODO: remove this slow path
Brian Carlstrom4873d462011-08-21 15:23:39 -0700173void ImageWriter::FixupMethod(const Method* orig, Method* copy) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700174 FixupInstanceFields(orig, copy);
175 // TODO: remove need for this by adding "signature" to java.lang.reflect.Method
176 copy->signature_ = down_cast<String*>(GetImageAddress(orig->signature_));
177 DCHECK(copy->signature_ != NULL);
178 // TODO: convert shorty_ to heap allocated storage
179}
180
Brian Carlstrom4873d462011-08-21 15:23:39 -0700181void ImageWriter::FixupField(const Field* orig, Field* copy) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700182 FixupInstanceFields(orig, copy);
183 // TODO: convert descriptor_ to heap allocated storage
184}
185
Brian Carlstrom4873d462011-08-21 15:23:39 -0700186void ImageWriter::FixupObjectArray(const ObjectArray<Object>* orig, ObjectArray<Object>* copy) {
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700187 for (int32_t i = 0; i < orig->GetLength(); ++i) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700188 const Object* element = orig->Get(i);
189 copy->Set(i, GetImageAddress(element));
190 }
191}
192
Brian Carlstrom4873d462011-08-21 15:23:39 -0700193void ImageWriter::FixupInstanceFields(const Object* orig, Object* copy) {
194 DCHECK(orig != NULL);
195 DCHECK(copy != NULL);
196 Class* klass = orig->GetClass();
197 DCHECK(klass != NULL);
198 FixupFields(orig,
199 copy,
200 klass->GetReferenceInstanceOffsets(),
201 false);
202}
203
204void ImageWriter::FixupStaticFields(const Class* orig, Class* copy) {
205 DCHECK(orig != NULL);
206 DCHECK(copy != NULL);
207 FixupFields(orig,
208 copy,
209 orig->GetReferenceStaticOffsets(),
210 true);
211}
212
213void ImageWriter::FixupFields(const Object* orig,
214 Object* copy,
215 uint32_t ref_offsets,
216 bool is_static) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700217 if (ref_offsets != CLASS_WALK_SUPER) {
218 // Found a reference offset bitmap. Fixup the specified offsets.
219 while (ref_offsets != 0) {
220 size_t right_shift = CLZ(ref_offsets);
221 size_t byte_offset = CLASS_OFFSET_FROM_CLZ(right_shift);
222 const Object* ref = orig->GetFieldObject(byte_offset);
223 copy->SetFieldObject(byte_offset, GetImageAddress(ref));
224 ref_offsets &= ~(CLASS_HIGH_BIT >> right_shift);
225 }
226 } else {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700227 // There is no reference offset bitmap. In the non-static case,
228 // walk up the class inheritance hierarchy and find reference
229 // offsets the hard way. In the static case, just consider this
230 // class.
231 for (const Class *klass = is_static ? orig->AsClass() : orig->GetClass();
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700232 klass != NULL;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700233 klass = is_static ? NULL : klass->GetSuperClass()) {
234 size_t num_reference_fields = (is_static
235 ? klass->NumReferenceStaticFields()
236 : klass->NumReferenceInstanceFields());
237 for (size_t i = 0; i < num_reference_fields; ++i) {
238 Field* field = (is_static
239 ? klass->GetStaticField(i)
240 : klass->GetInstanceField(i));
241 size_t field_offset = field->GetOffset();
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700242 const Object* ref = orig->GetFieldObject(field_offset);
243 copy->SetFieldObject(field_offset, GetImageAddress(ref));
244 }
245 }
246 }
247}
248
249} // namespace art