blob: 859da864bb19ed3e4fe0417df5323a9e682b6eea [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 Carlstroma663ea52011-08-19 23:33:41 -070060namespace {
61
62struct InternTableVisitorState {
63 int index;
Elliott Hughescf4c6c42011-09-01 15:16:42 -070064 ObjectArray<const Object>* interned_array;
Brian Carlstroma663ea52011-08-19 23:33:41 -070065};
66
Elliott Hughescf4c6c42011-09-01 15:16:42 -070067void InternTableVisitor(const Object* obj, void* arg) {
Brian Carlstroma663ea52011-08-19 23:33:41 -070068 InternTableVisitorState* state = reinterpret_cast<InternTableVisitorState*>(arg);
69 state->interned_array->Set(state->index++, obj);
70}
71
Elliott Hughescf4c6c42011-09-01 15:16:42 -070072ObjectArray<const Object>* CreateInternedArray() {
Brian Carlstroma663ea52011-08-19 23:33:41 -070073 // build a Object[] of the interned strings for reinit
74 // TODO: avoid creating this future garbage
Elliott Hughescf4c6c42011-09-01 15:16:42 -070075 Runtime* runtime = Runtime::Current();
76 ClassLinker* class_linker = runtime->GetClassLinker();
77 const InternTable& intern_table = *runtime->GetInternTable();
Brian Carlstroma663ea52011-08-19 23:33:41 -070078 size_t size = intern_table.Size();
79 CHECK_NE(0U, size);
80
81 Class* object_array_class = class_linker->FindSystemClass("[Ljava/lang/Object;");
Elliott Hughescf4c6c42011-09-01 15:16:42 -070082 ObjectArray<const Object>* interned_array = ObjectArray<const Object>::Alloc(object_array_class, size);
Brian Carlstroma663ea52011-08-19 23:33:41 -070083
84 InternTableVisitorState state;
85 state.index = 0;
86 state.interned_array = interned_array;
87
88 intern_table.VisitRoots(InternTableVisitor, &state);
89
90 return interned_array;
91}
92
93} // namespace
94
Brian Carlstrom4873d462011-08-21 15:23:39 -070095void ImageWriter::CalculateNewObjectOffsetsCallback(Object* obj, void *arg) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -070096 DCHECK(obj != NULL);
97 DCHECK(arg != NULL);
98 ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070099 if (!image_writer->InSourceSpace(obj)) {
100 return;
101 }
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700102 image_writer->SetImageOffset(obj, image_writer->image_top_);
Elliott Hughes04b63fd2011-08-16 09:40:10 -0700103 image_writer->image_top_ += RoundUp(obj->SizeOf(), 8); // 64-bit alignment
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700104 DCHECK_LT(image_writer->image_top_, image_writer->image_->GetLength());
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700105
106 // sniff out the DexCaches on this pass for use on the next pass
107 if (obj->IsClass()) {
108 Class* klass = obj->AsClass();
109 DexCache* dex_cache = klass->GetDexCache();
110 if (dex_cache != NULL) {
111 image_writer->dex_caches_.insert(dex_cache);
112 } else {
113 DCHECK(klass->IsArrayClass() || klass->IsPrimitive());
114 }
115 }
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700116}
117
118void ImageWriter::CalculateNewObjectOffsets() {
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700119 ObjectArray<const Object>* interned_array = CreateInternedArray();
Brian Carlstroma663ea52011-08-19 23:33:41 -0700120
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700121 HeapBitmap* heap_bitmap = Heap::GetLiveBits();
122 DCHECK(heap_bitmap != NULL);
123 DCHECK_EQ(0U, image_top_);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700124
125 // leave space for the header, but do not write it yet, we need to
126 // know where interned_array is going to end up
127 image_top_ += RoundUp(sizeof(ImageHeader), 8); // 64-bit-alignment
128
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700129 heap_bitmap->Walk(CalculateNewObjectOffsetsCallback, this); // TODO: add Space-limited Walk
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700130 DCHECK_LT(image_top_, image_->GetLength());
Brian Carlstroma663ea52011-08-19 23:33:41 -0700131
132 // return to write header at start of image with future location of interned_array
133 ImageHeader image_header(reinterpret_cast<uint32_t>(image_base_),
134 reinterpret_cast<uint32_t>(GetImageAddress(interned_array)));
135 memcpy(image_->GetAddress(), &image_header, sizeof(image_header));
136
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700137 // Note that top_ is left at end of used space
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700138}
139
140void ImageWriter::CopyAndFixupObjects() {
141 HeapBitmap* heap_bitmap = Heap::GetLiveBits();
142 DCHECK(heap_bitmap != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700143 // TODO: heap validation can't handle this fix up pass
144 Heap::DisableObjectValidation();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700145 heap_bitmap->Walk(CopyAndFixupObjectsCallback, this); // TODO: add Space-limited Walk
146 FixupDexCaches();
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700147}
148
Brian Carlstrom4873d462011-08-21 15:23:39 -0700149void ImageWriter::CopyAndFixupObjectsCallback(Object* object, void *arg) {
150 DCHECK(object != NULL);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700151 DCHECK(arg != NULL);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700152 const Object* obj = object;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700153 ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700154 if (!image_writer->InSourceSpace(object)) {
155 return;
156 }
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700157
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700158 // see GetLocalAddress for similar computation
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700159 size_t offset = image_writer->GetImageOffset(obj);
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700160 byte* dst = image_writer->image_->GetAddress() + offset;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700161 const byte* src = reinterpret_cast<const byte*>(obj);
Elliott Hughes04b63fd2011-08-16 09:40:10 -0700162 size_t n = obj->SizeOf();
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700163 DCHECK_LT(offset + n, image_writer->image_->GetLength());
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700164 memcpy(dst, src, n);
165 Object* copy = reinterpret_cast<Object*>(dst);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700166 ResetImageOffset(copy);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700167 image_writer->FixupObject(obj, copy);
168}
169
Brian Carlstrom4873d462011-08-21 15:23:39 -0700170void ImageWriter::FixupObject(const Object* orig, Object* copy) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700171 DCHECK(orig != NULL);
172 DCHECK(copy != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700173 copy->SetClass(down_cast<Class*>(GetImageAddress(orig->GetClass())));
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700174 // TODO: special case init of pointers to malloc data (or removal of these pointers)
175 if (orig->IsClass()) {
176 FixupClass(orig->AsClass(), down_cast<Class*>(copy));
177 } else if (orig->IsObjectArray()) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700178 FixupObjectArray(orig->AsObjectArray<Object>(), down_cast<ObjectArray<Object>*>(copy));
179 } else {
180 FixupInstanceFields(orig, copy);
181 }
182}
183
Brian Carlstrom4873d462011-08-21 15:23:39 -0700184void ImageWriter::FixupClass(const Class* orig, Class* copy) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700185 FixupInstanceFields(orig, copy);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700186 FixupStaticFields(orig, copy);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700187}
188
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700189const void* FixupCode(const ByteArray* copy_code_array, const void* orig_code) {
190 // TODO: change to DCHECK when all code compiling
191 if (copy_code_array == NULL) {
192 return NULL;
193 }
194 const void* copy_code = copy_code_array->GetData();
195 // TODO: remember InstructionSet with each code array so we know if we need to do thumb fixup?
196 if ((reinterpret_cast<uintptr_t>(orig_code) % 2) == 1) {
197 return reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(copy_code) + 1);
198 }
199 return copy_code;
200}
201
Brian Carlstrom4873d462011-08-21 15:23:39 -0700202void ImageWriter::FixupMethod(const Method* orig, Method* copy) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700203 FixupInstanceFields(orig, copy);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700204 // TODO: convert shorty_ to heap allocated storage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700205 copy->code_ = FixupCode(copy->code_array_, orig->code_);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700206 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 -0700207}
208
Brian Carlstrom4873d462011-08-21 15:23:39 -0700209void ImageWriter::FixupObjectArray(const ObjectArray<Object>* orig, ObjectArray<Object>* copy) {
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700210 for (int32_t i = 0; i < orig->GetLength(); ++i) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700211 const Object* element = orig->Get(i);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700212 copy->SetWithoutChecks(i, GetImageAddress(element));
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700213 }
214}
215
Brian Carlstrom4873d462011-08-21 15:23:39 -0700216void ImageWriter::FixupInstanceFields(const Object* orig, Object* copy) {
217 DCHECK(orig != NULL);
218 DCHECK(copy != NULL);
219 Class* klass = orig->GetClass();
220 DCHECK(klass != NULL);
221 FixupFields(orig,
222 copy,
223 klass->GetReferenceInstanceOffsets(),
224 false);
225}
226
227void ImageWriter::FixupStaticFields(const Class* orig, Class* copy) {
228 DCHECK(orig != NULL);
229 DCHECK(copy != NULL);
230 FixupFields(orig,
231 copy,
232 orig->GetReferenceStaticOffsets(),
233 true);
234}
235
236void ImageWriter::FixupFields(const Object* orig,
237 Object* copy,
238 uint32_t ref_offsets,
239 bool is_static) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700240 if (ref_offsets != CLASS_WALK_SUPER) {
241 // Found a reference offset bitmap. Fixup the specified offsets.
242 while (ref_offsets != 0) {
243 size_t right_shift = CLZ(ref_offsets);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700244 MemberOffset byte_offset = CLASS_OFFSET_FROM_CLZ(right_shift);
245 const Object* ref = orig->GetFieldObject<const Object*>(byte_offset, false);
246 copy->SetFieldObject(byte_offset, GetImageAddress(ref), false);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700247 ref_offsets &= ~(CLASS_HIGH_BIT >> right_shift);
248 }
249 } else {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700250 // There is no reference offset bitmap. In the non-static case,
251 // walk up the class inheritance hierarchy and find reference
252 // offsets the hard way. In the static case, just consider this
253 // class.
254 for (const Class *klass = is_static ? orig->AsClass() : orig->GetClass();
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700255 klass != NULL;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700256 klass = is_static ? NULL : klass->GetSuperClass()) {
257 size_t num_reference_fields = (is_static
258 ? klass->NumReferenceStaticFields()
259 : klass->NumReferenceInstanceFields());
260 for (size_t i = 0; i < num_reference_fields; ++i) {
261 Field* field = (is_static
262 ? klass->GetStaticField(i)
263 : klass->GetInstanceField(i));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700264 MemberOffset field_offset = field->GetOffset();
265 const Object* ref = orig->GetFieldObject<const Object*>(field_offset, false);
266 copy->SetFieldObject(field_offset, GetImageAddress(ref), false);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700267 }
268 }
269 }
270}
271
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700272void ImageWriter::FixupDexCaches() {
273 typedef Set::const_iterator It; // TODO: C++0x auto
274 for (It it = dex_caches_.begin(), end = dex_caches_.end(); it != end; ++it) {
275 DexCache* orig = *it;
276 DexCache* copy = down_cast<DexCache*>(GetLocalAddress(orig));
277 FixupDexCache(orig, copy);
278 }
279}
280
281void ImageWriter::FixupDexCache(const DexCache* orig, DexCache* copy) {
282 CHECK(orig != NULL);
283 CHECK(copy != NULL);
284
285 CodeAndDirectMethods* orig_cadms = orig->GetCodeAndDirectMethods();
286 CodeAndDirectMethods* copy_cadms = down_cast<CodeAndDirectMethods*>(GetLocalAddress(orig_cadms));
287 for (size_t i = 0; i < orig->NumResolvedMethods(); i++) {
288 Method* orig_method = orig->GetResolvedMethod(i);
289 // if it was resolved in the original, resolve it in the copy
290 if (orig_method != NULL
291 && InSourceSpace(orig_method)
292 && orig_method == orig_cadms->GetResolvedMethod(i)) {
293 Method* copy_method = down_cast<Method*>(GetLocalAddress(orig_method));
294 copy_cadms->Set(CodeAndDirectMethods::CodeIndex(i),
295 reinterpret_cast<int32_t>(copy_method->code_));
296 copy_cadms->Set(CodeAndDirectMethods::MethodIndex(i),
297 reinterpret_cast<int32_t>(GetImageAddress(orig_method)));
298 }
299 }
300}
301
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700302} // namespace art