Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 | |
| 3 | #include "image_writer.h" |
| 4 | |
| 5 | #include <sys/mman.h> |
| 6 | #include <vector> |
| 7 | |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 8 | #include "dex_cache.h" |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 9 | #include "class_linker.h" |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 10 | #include "file.h" |
| 11 | #include "globals.h" |
| 12 | #include "heap.h" |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 13 | #include "image.h" |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 14 | #include "intern_table.h" |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 15 | #include "logging.h" |
| 16 | #include "object.h" |
| 17 | #include "space.h" |
| 18 | #include "utils.h" |
| 19 | |
| 20 | namespace art { |
| 21 | |
| 22 | bool 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 Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 29 | |
Brian Carlstrom | 4e777d4 | 2011-08-15 13:53:52 -0700 | [diff] [blame] | 30 | scoped_ptr<File> file(OS::OpenFile(filename, true)); |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 31 | if (file == NULL) { |
| 32 | return false; |
| 33 | } |
Brian Carlstrom | 4e777d4 | 2011-08-15 13:53:52 -0700 | [diff] [blame] | 34 | return file->WriteFully(image_->GetAddress(), image_top_); |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | bool ImageWriter::Init(Space* space) { |
| 38 | size_t size = space->Size(); |
| 39 | int prot = PROT_READ | PROT_WRITE; |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 40 | size_t length = RoundUp(size, kPageSize); |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 41 | image_.reset(MemMap::Map(length, prot)); |
Brian Carlstrom | 4e777d4 | 2011-08-15 13:53:52 -0700 | [diff] [blame] | 42 | if (image_ == NULL) { |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 43 | return false; |
| 44 | } |
| 45 | return true; |
| 46 | } |
| 47 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 48 | namespace { |
| 49 | |
| 50 | struct InternTableVisitorState { |
| 51 | int index; |
| 52 | ObjectArray<Object>* interned_array; |
| 53 | }; |
| 54 | |
| 55 | void InternTableVisitor(Object* obj, void* arg) { |
| 56 | InternTableVisitorState* state = reinterpret_cast<InternTableVisitorState*>(arg); |
| 57 | state->interned_array->Set(state->index++, obj); |
| 58 | } |
| 59 | |
| 60 | ObjectArray<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 Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 82 | void ImageWriter::CalculateNewObjectOffsetsCallback(Object* obj, void *arg) { |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 83 | DCHECK(obj != NULL); |
| 84 | DCHECK(arg != NULL); |
| 85 | ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg); |
Brian Carlstrom | 4e777d4 | 2011-08-15 13:53:52 -0700 | [diff] [blame] | 86 | image_writer->SetImageOffset(obj, image_writer->image_top_); |
Elliott Hughes | 04b63fd | 2011-08-16 09:40:10 -0700 | [diff] [blame] | 87 | image_writer->image_top_ += RoundUp(obj->SizeOf(), 8); // 64-bit alignment |
Brian Carlstrom | 4e777d4 | 2011-08-15 13:53:52 -0700 | [diff] [blame] | 88 | DCHECK_LT(image_writer->image_top_, image_writer->image_->GetLength()); |
| 89 | } |
| 90 | |
| 91 | void ImageWriter::CalculateNewObjectOffsets() { |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 92 | ObjectArray<Object>* interned_array = CreateInternedArray(); |
| 93 | |
Brian Carlstrom | 4e777d4 | 2011-08-15 13:53:52 -0700 | [diff] [blame] | 94 | HeapBitmap* heap_bitmap = Heap::GetLiveBits(); |
| 95 | DCHECK(heap_bitmap != NULL); |
| 96 | DCHECK_EQ(0U, image_top_); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 97 | |
| 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 Carlstrom | 4e777d4 | 2011-08-15 13:53:52 -0700 | [diff] [blame] | 102 | heap_bitmap->Walk(CalculateNewObjectOffsetsCallback, this); |
| 103 | DCHECK_LT(image_top_, image_->GetLength()); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 104 | |
| 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 Carlstrom | 4e777d4 | 2011-08-15 13:53:52 -0700 | [diff] [blame] | 110 | // Note that top_ is left at end of used space |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | void ImageWriter::CopyAndFixupObjects() { |
| 114 | HeapBitmap* heap_bitmap = Heap::GetLiveBits(); |
| 115 | DCHECK(heap_bitmap != NULL); |
| 116 | heap_bitmap->Walk(CopyAndFixupObjectsCallback, this); |
| 117 | } |
| 118 | |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 119 | void ImageWriter::CopyAndFixupObjectsCallback(Object* object, void *arg) { |
| 120 | DCHECK(object != NULL); |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 121 | DCHECK(arg != NULL); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 122 | const Object* obj = object; |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 123 | ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg); |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 124 | |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 125 | size_t offset = image_writer->GetImageOffset(obj); |
Brian Carlstrom | 4e777d4 | 2011-08-15 13:53:52 -0700 | [diff] [blame] | 126 | byte* dst = image_writer->image_->GetAddress() + offset; |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 127 | const byte* src = reinterpret_cast<const byte*>(obj); |
Elliott Hughes | 04b63fd | 2011-08-16 09:40:10 -0700 | [diff] [blame] | 128 | size_t n = obj->SizeOf(); |
Brian Carlstrom | 4e777d4 | 2011-08-15 13:53:52 -0700 | [diff] [blame] | 129 | DCHECK_LT(offset + n, image_writer->image_->GetLength()); |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 130 | memcpy(dst, src, n); |
| 131 | Object* copy = reinterpret_cast<Object*>(dst); |
| 132 | image_writer->FixupObject(obj, copy); |
| 133 | } |
| 134 | |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 135 | void ImageWriter::FixupObject(const Object* orig, Object* copy) { |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 136 | DCHECK(orig != NULL); |
| 137 | DCHECK(copy != NULL); |
| 138 | copy->klass_ = down_cast<Class*>(GetImageAddress(orig->klass_)); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 139 | // 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 Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 142 | } 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 Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 146 | } else if (orig->IsObjectArray()) { |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 147 | FixupObjectArray(orig->AsObjectArray<Object>(), down_cast<ObjectArray<Object>*>(copy)); |
| 148 | } else { |
| 149 | FixupInstanceFields(orig, copy); |
| 150 | } |
| 151 | } |
| 152 | |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 153 | void ImageWriter::FixupClass(const Class* orig, Class* copy) { |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 154 | 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 Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 166 | // TODO: convert ifvi_pool_ to heap allocated storage |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 167 | copy->ifields_ = down_cast<ObjectArray<Field>*>(GetImageAddress(orig->ifields_)); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 168 | // TODO: convert source_file_ to heap allocated storage |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 169 | copy->sfields_ = down_cast<ObjectArray<Field>*>(GetImageAddress(orig->sfields_)); |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 170 | copy->interfaces_type_idx_ = down_cast<IntArray*>(GetImageAddress(orig->interfaces_type_idx_)); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 171 | FixupStaticFields(orig, copy); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 174 | // TODO: remove this slow path |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 175 | void ImageWriter::FixupMethod(const Method* orig, Method* copy) { |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 176 | 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 Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 183 | void ImageWriter::FixupField(const Field* orig, Field* copy) { |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 184 | FixupInstanceFields(orig, copy); |
| 185 | // TODO: convert descriptor_ to heap allocated storage |
| 186 | } |
| 187 | |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 188 | void ImageWriter::FixupObjectArray(const ObjectArray<Object>* orig, ObjectArray<Object>* copy) { |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 189 | for (int32_t i = 0; i < orig->GetLength(); ++i) { |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 190 | const Object* element = orig->Get(i); |
| 191 | copy->Set(i, GetImageAddress(element)); |
| 192 | } |
| 193 | } |
| 194 | |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 195 | void 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 | |
| 206 | void 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 | |
| 215 | void ImageWriter::FixupFields(const Object* orig, |
| 216 | Object* copy, |
| 217 | uint32_t ref_offsets, |
| 218 | bool is_static) { |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 219 | 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 Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 229 | // 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 Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 234 | klass != NULL; |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 235 | 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 Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 244 | const Object* ref = orig->GetFieldObject(field_offset); |
| 245 | copy->SetFieldObject(field_offset, GetImageAddress(ref)); |
| 246 | } |
| 247 | } |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | } // namespace art |