blob: a49b2618343f7dc281588984f0e63d45c114812e [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 }
38 CalculateNewObjectOffsets();
39 CopyAndFixupObjects();
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070040
Elliott Hughes90a33692011-08-30 13:27:07 -070041 UniquePtr<File> file(OS::OpenFile(filename, true));
42 if (file.get() == NULL) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -070043 return false;
44 }
Brian Carlstrom4e777d42011-08-15 13:53:52 -070045 return file->WriteFully(image_->GetAddress(), image_top_);
Brian Carlstromdb4d5402011-08-09 12:18:28 -070046}
47
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070048bool ImageWriter::Init() {
49 size_t size = source_space_->Size();
Brian Carlstromdb4d5402011-08-09 12:18:28 -070050 int prot = PROT_READ | PROT_WRITE;
Brian Carlstromdb4d5402011-08-09 12:18:28 -070051 size_t length = RoundUp(size, kPageSize);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070052 image_.reset(MemMap::Map(length, prot));
Elliott Hughes90a33692011-08-30 13:27:07 -070053 if (image_.get() == NULL) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -070054 return false;
55 }
56 return true;
57}
58
Brian Carlstroma663ea52011-08-19 23:33:41 -070059namespace {
60
61struct InternTableVisitorState {
62 int index;
Elliott Hughescf4c6c42011-09-01 15:16:42 -070063 ObjectArray<const Object>* interned_array;
Brian Carlstroma663ea52011-08-19 23:33:41 -070064};
65
Elliott Hughescf4c6c42011-09-01 15:16:42 -070066void InternTableVisitor(const Object* obj, void* arg) {
Brian Carlstroma663ea52011-08-19 23:33:41 -070067 InternTableVisitorState* state = reinterpret_cast<InternTableVisitorState*>(arg);
68 state->interned_array->Set(state->index++, obj);
69}
70
Elliott Hughescf4c6c42011-09-01 15:16:42 -070071ObjectArray<const Object>* CreateInternedArray() {
Brian Carlstroma663ea52011-08-19 23:33:41 -070072 // build a Object[] of the interned strings for reinit
73 // TODO: avoid creating this future garbage
Elliott Hughescf4c6c42011-09-01 15:16:42 -070074 Runtime* runtime = Runtime::Current();
75 ClassLinker* class_linker = runtime->GetClassLinker();
76 const InternTable& intern_table = *runtime->GetInternTable();
Brian Carlstroma663ea52011-08-19 23:33:41 -070077 size_t size = intern_table.Size();
78 CHECK_NE(0U, size);
79
80 Class* object_array_class = class_linker->FindSystemClass("[Ljava/lang/Object;");
Elliott Hughescf4c6c42011-09-01 15:16:42 -070081 ObjectArray<const Object>* interned_array = ObjectArray<const Object>::Alloc(object_array_class, size);
Brian Carlstroma663ea52011-08-19 23:33:41 -070082
83 InternTableVisitorState state;
84 state.index = 0;
85 state.interned_array = interned_array;
86
87 intern_table.VisitRoots(InternTableVisitor, &state);
88
89 return interned_array;
90}
91
92} // namespace
93
Brian Carlstrom4873d462011-08-21 15:23:39 -070094void ImageWriter::CalculateNewObjectOffsetsCallback(Object* obj, void *arg) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -070095 DCHECK(obj != NULL);
96 DCHECK(arg != NULL);
97 ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070098 if (!image_writer->InSourceSpace(obj)) {
99 return;
100 }
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700101 image_writer->SetImageOffset(obj, image_writer->image_top_);
Elliott Hughes04b63fd2011-08-16 09:40:10 -0700102 image_writer->image_top_ += RoundUp(obj->SizeOf(), 8); // 64-bit alignment
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700103 DCHECK_LT(image_writer->image_top_, image_writer->image_->GetLength());
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700104
105 // sniff out the DexCaches on this pass for use on the next pass
106 if (obj->IsClass()) {
107 Class* klass = obj->AsClass();
108 DexCache* dex_cache = klass->GetDexCache();
109 if (dex_cache != NULL) {
110 image_writer->dex_caches_.insert(dex_cache);
111 } else {
112 DCHECK(klass->IsArrayClass() || klass->IsPrimitive());
113 }
114 }
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700115}
116
117void ImageWriter::CalculateNewObjectOffsets() {
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700118 ObjectArray<const Object>* interned_array = CreateInternedArray();
Brian Carlstroma663ea52011-08-19 23:33:41 -0700119
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700120 HeapBitmap* heap_bitmap = Heap::GetLiveBits();
121 DCHECK(heap_bitmap != NULL);
122 DCHECK_EQ(0U, image_top_);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700123
124 // leave space for the header, but do not write it yet, we need to
125 // know where interned_array is going to end up
126 image_top_ += RoundUp(sizeof(ImageHeader), 8); // 64-bit-alignment
127
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700128 heap_bitmap->Walk(CalculateNewObjectOffsetsCallback, this); // TODO: add Space-limited Walk
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700129 DCHECK_LT(image_top_, image_->GetLength());
Brian Carlstroma663ea52011-08-19 23:33:41 -0700130
131 // return to write header at start of image with future location of interned_array
132 ImageHeader image_header(reinterpret_cast<uint32_t>(image_base_),
133 reinterpret_cast<uint32_t>(GetImageAddress(interned_array)));
134 memcpy(image_->GetAddress(), &image_header, sizeof(image_header));
135
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700136 // Note that top_ is left at end of used space
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700137}
138
139void ImageWriter::CopyAndFixupObjects() {
140 HeapBitmap* heap_bitmap = Heap::GetLiveBits();
141 DCHECK(heap_bitmap != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700142 // TODO: heap validation can't handle this fix up pass
143 Heap::DisableObjectValidation();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700144 heap_bitmap->Walk(CopyAndFixupObjectsCallback, this); // TODO: add Space-limited Walk
145 FixupDexCaches();
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700146}
147
Brian Carlstrom4873d462011-08-21 15:23:39 -0700148void ImageWriter::CopyAndFixupObjectsCallback(Object* object, void *arg) {
149 DCHECK(object != NULL);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700150 DCHECK(arg != NULL);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700151 const Object* obj = object;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700152 ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700153 if (!image_writer->InSourceSpace(object)) {
154 return;
155 }
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700156
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700157 // see GetLocalAddress for similar computation
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700158 size_t offset = image_writer->GetImageOffset(obj);
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700159 byte* dst = image_writer->image_->GetAddress() + offset;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700160 const byte* src = reinterpret_cast<const byte*>(obj);
Elliott Hughes04b63fd2011-08-16 09:40:10 -0700161 size_t n = obj->SizeOf();
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700162 DCHECK_LT(offset + n, image_writer->image_->GetLength());
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700163 memcpy(dst, src, n);
164 Object* copy = reinterpret_cast<Object*>(dst);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700165 ResetImageOffset(copy);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700166 image_writer->FixupObject(obj, copy);
167}
168
Brian Carlstrom4873d462011-08-21 15:23:39 -0700169void ImageWriter::FixupObject(const Object* orig, Object* copy) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700170 DCHECK(orig != NULL);
171 DCHECK(copy != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700172 copy->SetClass(down_cast<Class*>(GetImageAddress(orig->GetClass())));
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700173 // TODO: special case init of pointers to malloc data (or removal of these pointers)
174 if (orig->IsClass()) {
175 FixupClass(orig->AsClass(), down_cast<Class*>(copy));
Brian Carlstroma663ea52011-08-19 23:33:41 -0700176 } else if (orig->IsMethod()) {
177 FixupMethod(orig->AsMethod(), down_cast<Method*>(copy));
178 } else if (orig->IsField()) {
179 FixupField(orig->AsField(), down_cast<Field*>(copy));
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700180 } else if (orig->IsObjectArray()) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700181 FixupObjectArray(orig->AsObjectArray<Object>(), down_cast<ObjectArray<Object>*>(copy));
182 } else {
183 FixupInstanceFields(orig, copy);
184 }
185}
186
Brian Carlstrom4873d462011-08-21 15:23:39 -0700187void ImageWriter::FixupClass(const Class* orig, Class* copy) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700188 FixupInstanceFields(orig, copy);
189 copy->descriptor_ = down_cast<String*>(GetImageAddress(orig->descriptor_));
190 copy->dex_cache_ = down_cast<DexCache*>(GetImageAddress(orig->dex_cache_));
191 copy->verify_error_class_ = down_cast<Class*>(GetImageAddress(orig->verify_error_class_));
192 copy->component_type_ = down_cast<Class*>(GetImageAddress(orig->component_type_));
193 copy->super_class_ = down_cast<Class*>(GetImageAddress(orig->super_class_));
194 copy->class_loader_ = down_cast<ClassLoader*>(GetImageAddress(orig->class_loader_));
195 copy->interfaces_ = down_cast<ObjectArray<Class>*>(GetImageAddress(orig->interfaces_));
196 copy->direct_methods_ = down_cast<ObjectArray<Method>*>(GetImageAddress(orig->direct_methods_));
197 copy->virtual_methods_ = down_cast<ObjectArray<Method>*>(GetImageAddress(orig->virtual_methods_));
198 copy->vtable_ = down_cast<ObjectArray<Method>*>(GetImageAddress(orig->vtable_));
199 // TODO: convert iftable_ to heap allocated storage
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700200 // TODO: convert ifvi_pool_ to heap allocated storage
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700201 copy->ifields_ = down_cast<ObjectArray<Field>*>(GetImageAddress(orig->ifields_));
Brian Carlstrom4873d462011-08-21 15:23:39 -0700202 // TODO: convert source_file_ to heap allocated storage
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700203 copy->sfields_ = down_cast<ObjectArray<Field>*>(GetImageAddress(orig->sfields_));
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700204 copy->interfaces_type_idx_ = down_cast<IntArray*>(GetImageAddress(orig->interfaces_type_idx_));
Brian Carlstrom4873d462011-08-21 15:23:39 -0700205 FixupStaticFields(orig, copy);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700206}
207
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700208const void* FixupCode(const ByteArray* copy_code_array, const void* orig_code) {
209 // TODO: change to DCHECK when all code compiling
210 if (copy_code_array == NULL) {
211 return NULL;
212 }
213 const void* copy_code = copy_code_array->GetData();
214 // TODO: remember InstructionSet with each code array so we know if we need to do thumb fixup?
215 if ((reinterpret_cast<uintptr_t>(orig_code) % 2) == 1) {
216 return reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(copy_code) + 1);
217 }
218 return copy_code;
219}
220
Brian Carlstroma663ea52011-08-19 23:33:41 -0700221// TODO: remove this slow path
Brian Carlstrom4873d462011-08-21 15:23:39 -0700222void ImageWriter::FixupMethod(const Method* orig, Method* copy) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700223 FixupInstanceFields(orig, copy);
224 // TODO: remove need for this by adding "signature" to java.lang.reflect.Method
225 copy->signature_ = down_cast<String*>(GetImageAddress(orig->signature_));
226 DCHECK(copy->signature_ != NULL);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700227 // TODO: convert shorty_ to heap allocated storage
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700228 copy->dex_cache_strings_ = down_cast<ObjectArray<String>*>(GetImageAddress(orig->dex_cache_strings_));
229 copy->dex_cache_resolved_types_ = down_cast<ObjectArray<Class>*>(GetImageAddress(orig->dex_cache_resolved_types_));
230 copy->dex_cache_resolved_methods_ = down_cast<ObjectArray<Method>*>(GetImageAddress(orig->dex_cache_resolved_methods_));
231 copy->dex_cache_resolved_fields_ = down_cast<ObjectArray<Field>*>(GetImageAddress(orig->dex_cache_resolved_fields_));
232 copy->dex_cache_code_and_direct_methods_ = down_cast<CodeAndDirectMethods*>(GetImageAddress(orig->dex_cache_code_and_direct_methods_));
233 copy->dex_cache_initialized_static_storage_ = down_cast<ObjectArray<StaticStorageBase>*>(GetImageAddress(orig->dex_cache_initialized_static_storage_));
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700234 copy->code_array_ = down_cast<ByteArray*>(GetImageAddress(orig->code_array_));
235 copy->code_ = FixupCode(copy->code_array_, orig->code_);
236 copy->invoke_stub_array_ = down_cast<ByteArray*>(GetImageAddress(orig->invoke_stub_array_));
237 copy->invoke_stub_ = reinterpret_cast<Method::InvokeStub*>(FixupCode(copy->invoke_stub_array_, reinterpret_cast<void*>(orig->invoke_stub_)));
Brian Carlstroma663ea52011-08-19 23:33:41 -0700238}
239
Brian Carlstrom4873d462011-08-21 15:23:39 -0700240void ImageWriter::FixupField(const Field* orig, Field* copy) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700241 FixupInstanceFields(orig, copy);
242 // TODO: convert descriptor_ to heap allocated storage
243}
244
Brian Carlstrom4873d462011-08-21 15:23:39 -0700245void ImageWriter::FixupObjectArray(const ObjectArray<Object>* orig, ObjectArray<Object>* copy) {
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700246 for (int32_t i = 0; i < orig->GetLength(); ++i) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700247 const Object* element = orig->Get(i);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700248 copy->SetWithoutChecks(i, GetImageAddress(element));
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700249 }
250}
251
Brian Carlstrom4873d462011-08-21 15:23:39 -0700252void ImageWriter::FixupInstanceFields(const Object* orig, Object* copy) {
253 DCHECK(orig != NULL);
254 DCHECK(copy != NULL);
255 Class* klass = orig->GetClass();
256 DCHECK(klass != NULL);
257 FixupFields(orig,
258 copy,
259 klass->GetReferenceInstanceOffsets(),
260 false);
261}
262
263void ImageWriter::FixupStaticFields(const Class* orig, Class* copy) {
264 DCHECK(orig != NULL);
265 DCHECK(copy != NULL);
266 FixupFields(orig,
267 copy,
268 orig->GetReferenceStaticOffsets(),
269 true);
270}
271
272void ImageWriter::FixupFields(const Object* orig,
273 Object* copy,
274 uint32_t ref_offsets,
275 bool is_static) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700276 if (ref_offsets != CLASS_WALK_SUPER) {
277 // Found a reference offset bitmap. Fixup the specified offsets.
278 while (ref_offsets != 0) {
279 size_t right_shift = CLZ(ref_offsets);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700280 MemberOffset byte_offset = CLASS_OFFSET_FROM_CLZ(right_shift);
281 const Object* ref = orig->GetFieldObject<const Object*>(byte_offset, false);
282 copy->SetFieldObject(byte_offset, GetImageAddress(ref), false);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700283 ref_offsets &= ~(CLASS_HIGH_BIT >> right_shift);
284 }
285 } else {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700286 // There is no reference offset bitmap. In the non-static case,
287 // walk up the class inheritance hierarchy and find reference
288 // offsets the hard way. In the static case, just consider this
289 // class.
290 for (const Class *klass = is_static ? orig->AsClass() : orig->GetClass();
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700291 klass != NULL;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700292 klass = is_static ? NULL : klass->GetSuperClass()) {
293 size_t num_reference_fields = (is_static
294 ? klass->NumReferenceStaticFields()
295 : klass->NumReferenceInstanceFields());
296 for (size_t i = 0; i < num_reference_fields; ++i) {
297 Field* field = (is_static
298 ? klass->GetStaticField(i)
299 : klass->GetInstanceField(i));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700300 MemberOffset field_offset = field->GetOffset();
301 const Object* ref = orig->GetFieldObject<const Object*>(field_offset, false);
302 copy->SetFieldObject(field_offset, GetImageAddress(ref), false);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700303 }
304 }
305 }
306}
307
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700308void ImageWriter::FixupDexCaches() {
309 typedef Set::const_iterator It; // TODO: C++0x auto
310 for (It it = dex_caches_.begin(), end = dex_caches_.end(); it != end; ++it) {
311 DexCache* orig = *it;
312 DexCache* copy = down_cast<DexCache*>(GetLocalAddress(orig));
313 FixupDexCache(orig, copy);
314 }
315}
316
317void ImageWriter::FixupDexCache(const DexCache* orig, DexCache* copy) {
318 CHECK(orig != NULL);
319 CHECK(copy != NULL);
320
321 CodeAndDirectMethods* orig_cadms = orig->GetCodeAndDirectMethods();
322 CodeAndDirectMethods* copy_cadms = down_cast<CodeAndDirectMethods*>(GetLocalAddress(orig_cadms));
323 for (size_t i = 0; i < orig->NumResolvedMethods(); i++) {
324 Method* orig_method = orig->GetResolvedMethod(i);
325 // if it was resolved in the original, resolve it in the copy
326 if (orig_method != NULL
327 && InSourceSpace(orig_method)
328 && orig_method == orig_cadms->GetResolvedMethod(i)) {
329 Method* copy_method = down_cast<Method*>(GetLocalAddress(orig_method));
330 copy_cadms->Set(CodeAndDirectMethods::CodeIndex(i),
331 reinterpret_cast<int32_t>(copy_method->code_));
332 copy_cadms->Set(CodeAndDirectMethods::MethodIndex(i),
333 reinterpret_cast<int32_t>(GetImageAddress(orig_method)));
334 }
335 }
336}
337
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700338} // namespace art