1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
|
// Copyright 2011 Google Inc. All Rights Reserved.
#include "image_writer.h"
#include <sys/mman.h>
#include <vector>
#include "UniquePtr.h"
#include "class_linker.h"
#include "class_loader.h"
#include "dex_cache.h"
#include "file.h"
#include "globals.h"
#include "heap.h"
#include "image.h"
#include "intern_table.h"
#include "logging.h"
#include "object.h"
#include "runtime.h"
#include "space.h"
#include "utils.h"
namespace art {
bool ImageWriter::Write(const char* image_filename, uintptr_t image_base,
const std::string& oat_filename, const std::string& strip_location_prefix) {
CHECK_NE(image_base, 0U);
image_base_ = reinterpret_cast<byte*>(image_base);
const std::vector<Space*>& spaces = Heap::GetSpaces();
// currently just write the last space, assuming it is the space that was being used for allocation
CHECK_GE(spaces.size(), 1U);
source_space_ = spaces[spaces.size()-1];
CHECK(!source_space_->IsImageSpace());
oat_file_.reset(OatFile::Open(oat_filename, strip_location_prefix, NULL));
if (oat_file_.get() == NULL) {
LOG(ERROR) << "Failed to open oat file " << oat_filename;
return false;
}
if (!Init()) {
return false;
}
Heap::CollectGarbage();
CalculateNewObjectOffsets();
CopyAndFixupObjects();
UniquePtr<File> file(OS::OpenFile(image_filename, true));
if (file.get() == NULL) {
LOG(ERROR) << "Failed to open image file " << image_filename;
return false;
}
bool success = file->WriteFully(image_->GetAddress(), image_top_);
if (!success) {
PLOG(ERROR) << "Failed to write image file " << image_filename;
return false;
}
return true;
}
bool ImageWriter::Init() {
size_t size = source_space_->Size();
int prot = PROT_READ | PROT_WRITE;
size_t length = RoundUp(size, kPageSize);
image_.reset(MemMap::Map(length, prot));
if (image_.get() == NULL) {
LOG(ERROR) << "Failed to allocate memory for image file generation";
return false;
}
return true;
}
void ImageWriter::CalculateNewObjectOffsetsCallback(Object* obj, void* arg) {
DCHECK(obj != NULL);
DCHECK(arg != NULL);
ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg);
if (!image_writer->InSourceSpace(obj)) {
return;
}
// if it is a string, we want to intern it if its not interned.
if (obj->IsString()) {
// we must be an interned string that was forward referenced and already assigned
if (IsImageOffsetAssigned(obj)) {
DCHECK_EQ(obj, obj->AsString()->Intern());
return;
}
String* interned = obj->AsString()->Intern();
if (obj != interned) {
if (!IsImageOffsetAssigned(interned)) {
// interned obj is after us, allocate its location early
image_writer->AssignImageOffset(interned);
}
// point those looking for this object to the interned version.
SetImageOffset(obj, GetImageOffset(interned));
return;
}
// else (obj == interned), nothing to do but fall through to the normal case
}
image_writer->AssignImageOffset(obj);
// sniff out the DexCaches on this pass for use on the next pass
if (obj->IsClass()) {
Class* klass = obj->AsClass();
DexCache* dex_cache = klass->GetDexCache();
if (dex_cache != NULL) {
image_writer->dex_caches_.insert(dex_cache);
} else {
DCHECK(klass->IsArrayClass() || klass->IsPrimitive()) << PrettyClass(klass);
}
}
}
ObjectArray<Object>* ImageWriter::CreateImageRoots() const {
Runtime* runtime = Runtime::Current();
ClassLinker* class_linker = runtime->GetClassLinker();
Class* object_array_class = class_linker->FindSystemClass("[Ljava/lang/Object;");
// build an Object[] of all the DexCaches used in the source_space_
const std::vector<DexCache*>& all_dex_caches = class_linker->GetDexCaches();
std::vector<DexCache*> source_space_dex_caches;
for (size_t i = 0; i < all_dex_caches.size(); i++) {
DexCache* dex_cache = all_dex_caches[i];
if (InSourceSpace(dex_cache)) {
source_space_dex_caches.push_back(dex_cache);
}
}
ObjectArray<Object>* dex_caches = ObjectArray<Object>::Alloc(object_array_class,
source_space_dex_caches.size());
for (size_t i = 0; i < source_space_dex_caches.size(); i++) {
dex_caches->Set(i, source_space_dex_caches[i]);
}
// build an Object[] of the roots needed to restore the runtime
ObjectArray<Object>* image_roots = ObjectArray<Object>::Alloc(object_array_class,
ImageHeader::kImageRootsMax);
image_roots->Set(ImageHeader::kJniStubArray,
runtime->GetJniStubArray());
image_roots->Set(ImageHeader::kAbstractMethodErrorStubArray,
runtime->GetAbstractMethodErrorStubArray());
image_roots->Set(ImageHeader::kCalleeSaveMethod,
runtime->GetCalleeSaveMethod());
image_roots->Set(ImageHeader::kOatLocation,
String::AllocFromModifiedUtf8(oat_file_->GetLocation().c_str()));
image_roots->Set(ImageHeader::kDexCaches,
dex_caches);
image_roots->Set(ImageHeader::kClassRoots,
class_linker->GetClassRoots());
for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
CHECK(image_roots->Get(i) != NULL);
}
return image_roots;
}
void ImageWriter::CalculateNewObjectOffsets() {
ObjectArray<Object>* image_roots = CreateImageRoots();
HeapBitmap* heap_bitmap = Heap::GetLiveBits();
DCHECK(heap_bitmap != NULL);
DCHECK_EQ(0U, image_top_);
// leave space for the header, but do not write it yet, we need to
// know where image_roots is going to end up
image_top_ += RoundUp(sizeof(ImageHeader), 8); // 64-bit-alignment
heap_bitmap->Walk(CalculateNewObjectOffsetsCallback, this); // TODO: add Space-limited Walk
DCHECK_LT(image_top_, image_->GetLength());
// Note that image_top_ is left at end of used space
oat_base_ = image_base_ + RoundUp(image_top_, kPageSize);
byte* oat_limit = oat_base_ + oat_file_->GetSize();
// return to write header at start of image with future location of image_roots
ImageHeader image_header(reinterpret_cast<uint32_t>(image_base_),
reinterpret_cast<uint32_t>(GetImageAddress(image_roots)),
oat_file_->GetOatHeader().GetChecksum(),
reinterpret_cast<uint32_t>(oat_base_),
reinterpret_cast<uint32_t>(oat_limit));
memcpy(image_->GetAddress(), &image_header, sizeof(image_header));
}
void ImageWriter::CopyAndFixupObjects() {
HeapBitmap* heap_bitmap = Heap::GetLiveBits();
DCHECK(heap_bitmap != NULL);
// TODO: heap validation can't handle this fix up pass
Heap::DisableObjectValidation();
heap_bitmap->Walk(CopyAndFixupObjectsCallback, this); // TODO: add Space-limited Walk
FixupDexCaches();
}
void ImageWriter::CopyAndFixupObjectsCallback(Object* object, void* arg) {
DCHECK(object != NULL);
DCHECK(arg != NULL);
const Object* obj = object;
ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg);
if (!image_writer->InSourceSpace(object)) {
return;
}
// see GetLocalAddress for similar computation
size_t offset = image_writer->GetImageOffset(obj);
byte* dst = image_writer->image_->GetAddress() + offset;
const byte* src = reinterpret_cast<const byte*>(obj);
size_t n = obj->SizeOf();
DCHECK_LT(offset + n, image_writer->image_->GetLength());
memcpy(dst, src, n);
Object* copy = reinterpret_cast<Object*>(dst);
ResetImageOffset(copy);
image_writer->FixupObject(obj, copy);
}
void ImageWriter::FixupObject(const Object* orig, Object* copy) {
DCHECK(orig != NULL);
DCHECK(copy != NULL);
copy->SetClass(down_cast<Class*>(GetImageAddress(orig->GetClass())));
// TODO: special case init of pointers to malloc data (or removal of these pointers)
if (orig->IsClass()) {
FixupClass(orig->AsClass(), down_cast<Class*>(copy));
} else if (orig->IsObjectArray()) {
FixupObjectArray(orig->AsObjectArray<Object>(), down_cast<ObjectArray<Object>*>(copy));
} else if (orig->IsMethod()) {
FixupMethod(orig->AsMethod(), down_cast<Method*>(copy));
} else {
FixupInstanceFields(orig, copy);
}
}
void ImageWriter::FixupClass(const Class* orig, Class* copy) {
FixupInstanceFields(orig, copy);
FixupStaticFields(orig, copy);
}
const void* FixupCode(const ByteArray* copy_code_array, const void* orig_code) {
// TODO: change to DCHECK when all code compiling
if (copy_code_array == NULL) {
return NULL;
}
const void* copy_code = copy_code_array->GetData();
// TODO: remember InstructionSet with each code array so we know if we need to do thumb fixup?
if ((reinterpret_cast<uintptr_t>(orig_code) % 2) == 1) {
return reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(copy_code) + 1);
}
return copy_code;
}
void ImageWriter::FixupMethod(const Method* orig, Method* copy) {
FixupInstanceFields(orig, copy);
// OatWriter clears the code_array_ after writing the code.
// It replaces the code_ with an offset value we now adjust to be a pointer.
DCHECK(copy->code_array_ == NULL)
<< PrettyMethod(orig)
<< " orig_code_array_=" << orig->GetCodeArray() << " orig_code_=" << orig->GetCode()
<< " copy_code_array_=" << copy->code_array_ << " orig_code_=" << copy->code_
<< " jni_stub=" << Runtime::Current()->GetJniStubArray()
<< " ame_stub=" << Runtime::Current()->GetAbstractMethodErrorStubArray();
copy->invoke_stub_ = reinterpret_cast<Method::InvokeStub*>(FixupCode(copy->invoke_stub_array_, reinterpret_cast<void*>(orig->invoke_stub_)));
if (orig->IsNative()) {
ByteArray* orig_jni_stub_array_ = Runtime::Current()->GetJniStubArray();
ByteArray* copy_jni_stub_array_ = down_cast<ByteArray*>(GetImageAddress(orig_jni_stub_array_));
copy->native_method_ = copy_jni_stub_array_->GetData();
copy->code_ = oat_base_ + orig->GetOatCodeOffset();
} else {
DCHECK(copy->native_method_ == NULL) << copy->native_method_;
if (orig->IsAbstract()) {
ByteArray* orig_ame_stub_array_ = Runtime::Current()->GetAbstractMethodErrorStubArray();
ByteArray* copy_ame_stub_array_ = down_cast<ByteArray*>(GetImageAddress(orig_ame_stub_array_));
copy->code_ = copy_ame_stub_array_->GetData();
} else {
copy->code_ = oat_base_ + orig->GetOatCodeOffset();
}
}
}
void ImageWriter::FixupObjectArray(const ObjectArray<Object>* orig, ObjectArray<Object>* copy) {
for (int32_t i = 0; i < orig->GetLength(); ++i) {
const Object* element = orig->Get(i);
copy->SetWithoutChecks(i, GetImageAddress(element));
}
}
void ImageWriter::FixupInstanceFields(const Object* orig, Object* copy) {
DCHECK(orig != NULL);
DCHECK(copy != NULL);
Class* klass = orig->GetClass();
DCHECK(klass != NULL);
FixupFields(orig,
copy,
klass->GetReferenceInstanceOffsets(),
false);
}
void ImageWriter::FixupStaticFields(const Class* orig, Class* copy) {
DCHECK(orig != NULL);
DCHECK(copy != NULL);
FixupFields(orig,
copy,
orig->GetReferenceStaticOffsets(),
true);
}
void ImageWriter::FixupFields(const Object* orig,
Object* copy,
uint32_t ref_offsets,
bool is_static) {
if (ref_offsets != CLASS_WALK_SUPER) {
// Found a reference offset bitmap. Fixup the specified offsets.
while (ref_offsets != 0) {
size_t right_shift = CLZ(ref_offsets);
MemberOffset byte_offset = CLASS_OFFSET_FROM_CLZ(right_shift);
const Object* ref = orig->GetFieldObject<const Object*>(byte_offset, false);
copy->SetFieldObject(byte_offset, GetImageAddress(ref), false);
ref_offsets &= ~(CLASS_HIGH_BIT >> right_shift);
}
} else {
// There is no reference offset bitmap. In the non-static case,
// walk up the class inheritance hierarchy and find reference
// offsets the hard way. In the static case, just consider this
// class.
for (const Class *klass = is_static ? orig->AsClass() : orig->GetClass();
klass != NULL;
klass = is_static ? NULL : klass->GetSuperClass()) {
size_t num_reference_fields = (is_static
? klass->NumReferenceStaticFields()
: klass->NumReferenceInstanceFields());
for (size_t i = 0; i < num_reference_fields; ++i) {
Field* field = (is_static
? klass->GetStaticField(i)
: klass->GetInstanceField(i));
MemberOffset field_offset = field->GetOffset();
const Object* ref = orig->GetFieldObject<const Object*>(field_offset, false);
copy->SetFieldObject(field_offset, GetImageAddress(ref), false);
}
}
}
}
void ImageWriter::FixupDexCaches() {
typedef Set::const_iterator It; // TODO: C++0x auto
for (It it = dex_caches_.begin(), end = dex_caches_.end(); it != end; ++it) {
DexCache* orig = *it;
DexCache* copy = down_cast<DexCache*>(GetLocalAddress(orig));
FixupDexCache(orig, copy);
}
}
void ImageWriter::FixupDexCache(const DexCache* orig, DexCache* copy) {
CHECK(orig != NULL);
CHECK(copy != NULL);
CodeAndDirectMethods* orig_cadms = orig->GetCodeAndDirectMethods();
CodeAndDirectMethods* copy_cadms = down_cast<CodeAndDirectMethods*>(GetLocalAddress(orig_cadms));
for (size_t i = 0; i < orig->NumResolvedMethods(); i++) {
Method* orig_method = orig->GetResolvedMethod(i);
// if it was resolved in the original, resolve it in the copy
if (orig_method != NULL
&& InSourceSpace(orig_method)
&& orig_method == orig_cadms->GetResolvedMethod(i)) {
Method* copy_method = down_cast<Method*>(GetLocalAddress(orig_method));
copy_cadms->Set(CodeAndDirectMethods::CodeIndex(i),
reinterpret_cast<int32_t>(copy_method->code_));
copy_cadms->Set(CodeAndDirectMethods::MethodIndex(i),
reinterpret_cast<int32_t>(GetImageAddress(orig_method)));
}
}
}
} // namespace art
|