blob: f31586d86ec3546d6b293114f2194e8eca26ad16 [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
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700166 // TODO: convert ifvi_pool_ to heap allocated storage
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700167 copy->ifields_ = down_cast<ObjectArray<Field>*>(GetImageAddress(orig->ifields_));
Brian Carlstrom4873d462011-08-21 15:23:39 -0700168 // TODO: convert source_file_ to heap allocated storage
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700169 copy->sfields_ = down_cast<ObjectArray<Field>*>(GetImageAddress(orig->sfields_));
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700170 copy->interfaces_type_idx_ = down_cast<IntArray*>(GetImageAddress(orig->interfaces_type_idx_));
Brian Carlstrom4873d462011-08-21 15:23:39 -0700171 FixupStaticFields(orig, copy);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700172}
173
Brian Carlstroma663ea52011-08-19 23:33:41 -0700174// TODO: remove this slow path
Brian Carlstrom4873d462011-08-21 15:23:39 -0700175void ImageWriter::FixupMethod(const Method* orig, Method* copy) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700176 FixupInstanceFields(orig, copy);
177 // TODO: remove need for this by adding "signature" to java.lang.reflect.Method
178 copy->signature_ = down_cast<String*>(GetImageAddress(orig->signature_));
179 DCHECK(copy->signature_ != NULL);
180 // TODO: convert shorty_ to heap allocated storage
181}
182
Brian Carlstrom4873d462011-08-21 15:23:39 -0700183void ImageWriter::FixupField(const Field* orig, Field* copy) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700184 FixupInstanceFields(orig, copy);
185 // TODO: convert descriptor_ to heap allocated storage
186}
187
Brian Carlstrom4873d462011-08-21 15:23:39 -0700188void ImageWriter::FixupObjectArray(const ObjectArray<Object>* orig, ObjectArray<Object>* copy) {
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700189 for (int32_t i = 0; i < orig->GetLength(); ++i) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700190 const Object* element = orig->Get(i);
191 copy->Set(i, GetImageAddress(element));
192 }
193}
194
Brian Carlstrom4873d462011-08-21 15:23:39 -0700195void ImageWriter::FixupInstanceFields(const Object* orig, Object* copy) {
196 DCHECK(orig != NULL);
197 DCHECK(copy != NULL);
198 Class* klass = orig->GetClass();
199 DCHECK(klass != NULL);
200 FixupFields(orig,
201 copy,
202 klass->GetReferenceInstanceOffsets(),
203 false);
204}
205
206void ImageWriter::FixupStaticFields(const Class* orig, Class* copy) {
207 DCHECK(orig != NULL);
208 DCHECK(copy != NULL);
209 FixupFields(orig,
210 copy,
211 orig->GetReferenceStaticOffsets(),
212 true);
213}
214
215void ImageWriter::FixupFields(const Object* orig,
216 Object* copy,
217 uint32_t ref_offsets,
218 bool is_static) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700219 if (ref_offsets != CLASS_WALK_SUPER) {
220 // Found a reference offset bitmap. Fixup the specified offsets.
221 while (ref_offsets != 0) {
222 size_t right_shift = CLZ(ref_offsets);
223 size_t byte_offset = CLASS_OFFSET_FROM_CLZ(right_shift);
224 const Object* ref = orig->GetFieldObject(byte_offset);
225 copy->SetFieldObject(byte_offset, GetImageAddress(ref));
226 ref_offsets &= ~(CLASS_HIGH_BIT >> right_shift);
227 }
228 } else {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700229 // There is no reference offset bitmap. In the non-static case,
230 // walk up the class inheritance hierarchy and find reference
231 // offsets the hard way. In the static case, just consider this
232 // class.
233 for (const Class *klass = is_static ? orig->AsClass() : orig->GetClass();
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700234 klass != NULL;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700235 klass = is_static ? NULL : klass->GetSuperClass()) {
236 size_t num_reference_fields = (is_static
237 ? klass->NumReferenceStaticFields()
238 : klass->NumReferenceInstanceFields());
239 for (size_t i = 0; i < num_reference_fields; ++i) {
240 Field* field = (is_static
241 ? klass->GetStaticField(i)
242 : klass->GetInstanceField(i));
243 size_t field_offset = field->GetOffset();
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700244 const Object* ref = orig->GetFieldObject(field_offset);
245 copy->SetFieldObject(field_offset, GetImageAddress(ref));
246 }
247 }
248 }
249}
250
251} // namespace art