blob: e0e77c70d366fcad4863849773fdbe632f63e395 [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
26bool ImageWriter::Write(Space* space, const char* filename, byte* image_base) {
27 image_base_ = image_base;
28 if (!Init(space)) {
29 return false;
30 }
31 CalculateNewObjectOffsets();
32 CopyAndFixupObjects();
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070033
Elliott Hughes90a33692011-08-30 13:27:07 -070034 UniquePtr<File> file(OS::OpenFile(filename, true));
35 if (file.get() == NULL) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -070036 return false;
37 }
Brian Carlstrom4e777d42011-08-15 13:53:52 -070038 return file->WriteFully(image_->GetAddress(), image_top_);
Brian Carlstromdb4d5402011-08-09 12:18:28 -070039}
40
41bool ImageWriter::Init(Space* space) {
42 size_t size = space->Size();
43 int prot = PROT_READ | PROT_WRITE;
Brian Carlstromdb4d5402011-08-09 12:18:28 -070044 size_t length = RoundUp(size, kPageSize);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070045 image_.reset(MemMap::Map(length, prot));
Elliott Hughes90a33692011-08-30 13:27:07 -070046 if (image_.get() == NULL) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -070047 return false;
48 }
49 return true;
50}
51
Brian Carlstroma663ea52011-08-19 23:33:41 -070052namespace {
53
54struct InternTableVisitorState {
55 int index;
Elliott Hughescf4c6c42011-09-01 15:16:42 -070056 ObjectArray<const Object>* interned_array;
Brian Carlstroma663ea52011-08-19 23:33:41 -070057};
58
Elliott Hughescf4c6c42011-09-01 15:16:42 -070059void InternTableVisitor(const Object* obj, void* arg) {
Brian Carlstroma663ea52011-08-19 23:33:41 -070060 InternTableVisitorState* state = reinterpret_cast<InternTableVisitorState*>(arg);
61 state->interned_array->Set(state->index++, obj);
62}
63
Elliott Hughescf4c6c42011-09-01 15:16:42 -070064ObjectArray<const Object>* CreateInternedArray() {
Brian Carlstroma663ea52011-08-19 23:33:41 -070065 // build a Object[] of the interned strings for reinit
66 // TODO: avoid creating this future garbage
Elliott Hughescf4c6c42011-09-01 15:16:42 -070067 Runtime* runtime = Runtime::Current();
68 ClassLinker* class_linker = runtime->GetClassLinker();
69 const InternTable& intern_table = *runtime->GetInternTable();
Brian Carlstroma663ea52011-08-19 23:33:41 -070070 size_t size = intern_table.Size();
71 CHECK_NE(0U, size);
72
73 Class* object_array_class = class_linker->FindSystemClass("[Ljava/lang/Object;");
Elliott Hughescf4c6c42011-09-01 15:16:42 -070074 ObjectArray<const Object>* interned_array = ObjectArray<const Object>::Alloc(object_array_class, size);
Brian Carlstroma663ea52011-08-19 23:33:41 -070075
76 InternTableVisitorState state;
77 state.index = 0;
78 state.interned_array = interned_array;
79
80 intern_table.VisitRoots(InternTableVisitor, &state);
81
82 return interned_array;
83}
84
85} // namespace
86
Brian Carlstrom4873d462011-08-21 15:23:39 -070087void ImageWriter::CalculateNewObjectOffsetsCallback(Object* obj, void *arg) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -070088 DCHECK(obj != NULL);
89 DCHECK(arg != NULL);
90 ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg);
Brian Carlstrom4e777d42011-08-15 13:53:52 -070091 image_writer->SetImageOffset(obj, image_writer->image_top_);
Elliott Hughes04b63fd2011-08-16 09:40:10 -070092 image_writer->image_top_ += RoundUp(obj->SizeOf(), 8); // 64-bit alignment
Brian Carlstrom4e777d42011-08-15 13:53:52 -070093 DCHECK_LT(image_writer->image_top_, image_writer->image_->GetLength());
94}
95
96void ImageWriter::CalculateNewObjectOffsets() {
Elliott Hughescf4c6c42011-09-01 15:16:42 -070097 ObjectArray<const Object>* interned_array = CreateInternedArray();
Brian Carlstroma663ea52011-08-19 23:33:41 -070098
Brian Carlstrom4e777d42011-08-15 13:53:52 -070099 HeapBitmap* heap_bitmap = Heap::GetLiveBits();
100 DCHECK(heap_bitmap != NULL);
101 DCHECK_EQ(0U, image_top_);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700102
103 // leave space for the header, but do not write it yet, we need to
104 // know where interned_array is going to end up
105 image_top_ += RoundUp(sizeof(ImageHeader), 8); // 64-bit-alignment
106
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700107 heap_bitmap->Walk(CalculateNewObjectOffsetsCallback, this);
108 DCHECK_LT(image_top_, image_->GetLength());
Brian Carlstroma663ea52011-08-19 23:33:41 -0700109
110 // return to write header at start of image with future location of interned_array
111 ImageHeader image_header(reinterpret_cast<uint32_t>(image_base_),
112 reinterpret_cast<uint32_t>(GetImageAddress(interned_array)));
113 memcpy(image_->GetAddress(), &image_header, sizeof(image_header));
114
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700115 // Note that top_ is left at end of used space
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700116}
117
118void ImageWriter::CopyAndFixupObjects() {
119 HeapBitmap* heap_bitmap = Heap::GetLiveBits();
120 DCHECK(heap_bitmap != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700121 // TODO: heap validation can't handle this fix up pass
122 Heap::DisableObjectValidation();
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700123 heap_bitmap->Walk(CopyAndFixupObjectsCallback, this);
124}
125
Brian Carlstrom4873d462011-08-21 15:23:39 -0700126void ImageWriter::CopyAndFixupObjectsCallback(Object* object, void *arg) {
127 DCHECK(object != NULL);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700128 DCHECK(arg != NULL);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700129 const Object* obj = object;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700130 ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700131
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700132 size_t offset = image_writer->GetImageOffset(obj);
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700133 byte* dst = image_writer->image_->GetAddress() + offset;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700134 const byte* src = reinterpret_cast<const byte*>(obj);
Elliott Hughes04b63fd2011-08-16 09:40:10 -0700135 size_t n = obj->SizeOf();
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700136 DCHECK_LT(offset + n, image_writer->image_->GetLength());
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700137 memcpy(dst, src, n);
138 Object* copy = reinterpret_cast<Object*>(dst);
139 image_writer->FixupObject(obj, copy);
140}
141
Brian Carlstrom4873d462011-08-21 15:23:39 -0700142void ImageWriter::FixupObject(const Object* orig, Object* copy) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700143 DCHECK(orig != NULL);
144 DCHECK(copy != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700145 copy->SetClass(down_cast<Class*>(GetImageAddress(orig->GetClass())));
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700146 // TODO: special case init of pointers to malloc data (or removal of these pointers)
147 if (orig->IsClass()) {
148 FixupClass(orig->AsClass(), down_cast<Class*>(copy));
Brian Carlstroma663ea52011-08-19 23:33:41 -0700149 } else if (orig->IsMethod()) {
150 FixupMethod(orig->AsMethod(), down_cast<Method*>(copy));
151 } else if (orig->IsField()) {
152 FixupField(orig->AsField(), down_cast<Field*>(copy));
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700153 } else if (orig->IsObjectArray()) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700154 FixupObjectArray(orig->AsObjectArray<Object>(), down_cast<ObjectArray<Object>*>(copy));
155 } else {
156 FixupInstanceFields(orig, copy);
157 }
158}
159
Brian Carlstrom4873d462011-08-21 15:23:39 -0700160void ImageWriter::FixupClass(const Class* orig, Class* copy) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700161 FixupInstanceFields(orig, copy);
162 copy->descriptor_ = down_cast<String*>(GetImageAddress(orig->descriptor_));
163 copy->dex_cache_ = down_cast<DexCache*>(GetImageAddress(orig->dex_cache_));
164 copy->verify_error_class_ = down_cast<Class*>(GetImageAddress(orig->verify_error_class_));
165 copy->component_type_ = down_cast<Class*>(GetImageAddress(orig->component_type_));
166 copy->super_class_ = down_cast<Class*>(GetImageAddress(orig->super_class_));
167 copy->class_loader_ = down_cast<ClassLoader*>(GetImageAddress(orig->class_loader_));
168 copy->interfaces_ = down_cast<ObjectArray<Class>*>(GetImageAddress(orig->interfaces_));
169 copy->direct_methods_ = down_cast<ObjectArray<Method>*>(GetImageAddress(orig->direct_methods_));
170 copy->virtual_methods_ = down_cast<ObjectArray<Method>*>(GetImageAddress(orig->virtual_methods_));
171 copy->vtable_ = down_cast<ObjectArray<Method>*>(GetImageAddress(orig->vtable_));
172 // TODO: convert iftable_ to heap allocated storage
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700173 // TODO: convert ifvi_pool_ to heap allocated storage
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700174 copy->ifields_ = down_cast<ObjectArray<Field>*>(GetImageAddress(orig->ifields_));
Brian Carlstrom4873d462011-08-21 15:23:39 -0700175 // TODO: convert source_file_ to heap allocated storage
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700176 copy->sfields_ = down_cast<ObjectArray<Field>*>(GetImageAddress(orig->sfields_));
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700177 copy->interfaces_type_idx_ = down_cast<IntArray*>(GetImageAddress(orig->interfaces_type_idx_));
Brian Carlstrom4873d462011-08-21 15:23:39 -0700178 FixupStaticFields(orig, copy);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700179}
180
Brian Carlstroma663ea52011-08-19 23:33:41 -0700181// TODO: remove this slow path
Brian Carlstrom4873d462011-08-21 15:23:39 -0700182void ImageWriter::FixupMethod(const Method* orig, Method* copy) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700183 FixupInstanceFields(orig, copy);
184 // TODO: remove need for this by adding "signature" to java.lang.reflect.Method
185 copy->signature_ = down_cast<String*>(GetImageAddress(orig->signature_));
186 DCHECK(copy->signature_ != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700187 copy->dex_cache_strings_ = down_cast<ObjectArray<String>*>(GetImageAddress(orig->dex_cache_strings_));
188 copy->dex_cache_resolved_types_ = down_cast<ObjectArray<Class>*>(GetImageAddress(orig->dex_cache_resolved_types_));
189 copy->dex_cache_resolved_methods_ = down_cast<ObjectArray<Method>*>(GetImageAddress(orig->dex_cache_resolved_methods_));
190 copy->dex_cache_resolved_fields_ = down_cast<ObjectArray<Field>*>(GetImageAddress(orig->dex_cache_resolved_fields_));
191 copy->dex_cache_code_and_direct_methods_ = down_cast<CodeAndDirectMethods*>(GetImageAddress(orig->dex_cache_code_and_direct_methods_));
192 copy->dex_cache_initialized_static_storage_ = down_cast<ObjectArray<StaticStorageBase>*>(GetImageAddress(orig->dex_cache_initialized_static_storage_));
193
Brian Carlstroma663ea52011-08-19 23:33:41 -0700194 // TODO: convert shorty_ to heap allocated storage
195}
196
Brian Carlstrom4873d462011-08-21 15:23:39 -0700197void ImageWriter::FixupField(const Field* orig, Field* copy) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700198 FixupInstanceFields(orig, copy);
199 // TODO: convert descriptor_ to heap allocated storage
200}
201
Brian Carlstrom4873d462011-08-21 15:23:39 -0700202void ImageWriter::FixupObjectArray(const ObjectArray<Object>* orig, ObjectArray<Object>* copy) {
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700203 for (int32_t i = 0; i < orig->GetLength(); ++i) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700204 const Object* element = orig->Get(i);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700205 copy->SetWithoutChecks(i, GetImageAddress(element));
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700206 }
207}
208
Brian Carlstrom4873d462011-08-21 15:23:39 -0700209void ImageWriter::FixupInstanceFields(const Object* orig, Object* copy) {
210 DCHECK(orig != NULL);
211 DCHECK(copy != NULL);
212 Class* klass = orig->GetClass();
213 DCHECK(klass != NULL);
214 FixupFields(orig,
215 copy,
216 klass->GetReferenceInstanceOffsets(),
217 false);
218}
219
220void ImageWriter::FixupStaticFields(const Class* orig, Class* copy) {
221 DCHECK(orig != NULL);
222 DCHECK(copy != NULL);
223 FixupFields(orig,
224 copy,
225 orig->GetReferenceStaticOffsets(),
226 true);
227}
228
229void ImageWriter::FixupFields(const Object* orig,
230 Object* copy,
231 uint32_t ref_offsets,
232 bool is_static) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700233 if (ref_offsets != CLASS_WALK_SUPER) {
234 // Found a reference offset bitmap. Fixup the specified offsets.
235 while (ref_offsets != 0) {
236 size_t right_shift = CLZ(ref_offsets);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700237 MemberOffset byte_offset = CLASS_OFFSET_FROM_CLZ(right_shift);
238 const Object* ref = orig->GetFieldObject<const Object*>(byte_offset, false);
239 copy->SetFieldObject(byte_offset, GetImageAddress(ref), false);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700240 ref_offsets &= ~(CLASS_HIGH_BIT >> right_shift);
241 }
242 } else {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700243 // There is no reference offset bitmap. In the non-static case,
244 // walk up the class inheritance hierarchy and find reference
245 // offsets the hard way. In the static case, just consider this
246 // class.
247 for (const Class *klass = is_static ? orig->AsClass() : orig->GetClass();
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700248 klass != NULL;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700249 klass = is_static ? NULL : klass->GetSuperClass()) {
250 size_t num_reference_fields = (is_static
251 ? klass->NumReferenceStaticFields()
252 : klass->NumReferenceInstanceFields());
253 for (size_t i = 0; i < num_reference_fields; ++i) {
254 Field* field = (is_static
255 ? klass->GetStaticField(i)
256 : klass->GetInstanceField(i));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700257 MemberOffset field_offset = field->GetOffset();
258 const Object* ref = orig->GetFieldObject<const Object*>(field_offset, false);
259 copy->SetFieldObject(field_offset, GetImageAddress(ref), false);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700260 }
261 }
262 }
263}
264
265} // namespace art