blob: 871cfd5c41b59c941fc76476c39fbec635ae0f4d [file] [log] [blame]
Brian Carlstrom7940e442013-07-12 13:46:57 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "image_writer.h"
18
19#include <sys/stat.h>
20
21#include <vector>
22
23#include "base/logging.h"
24#include "base/unix_file/fd_file.h"
25#include "class_linker.h"
26#include "compiled_method.h"
27#include "dex_file-inl.h"
28#include "driver/compiler_driver.h"
29#include "elf_writer.h"
30#include "gc/accounting/card_table-inl.h"
31#include "gc/accounting/heap_bitmap.h"
Mathieu Chartier31e89252013-08-28 11:29:12 -070032#include "gc/accounting/space_bitmap-inl.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070033#include "gc/heap.h"
34#include "gc/space/large_object_space.h"
35#include "gc/space/space-inl.h"
36#include "globals.h"
37#include "image.h"
38#include "intern_table.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070039#include "mirror/art_field-inl.h"
40#include "mirror/art_method-inl.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070041#include "mirror/array-inl.h"
42#include "mirror/class-inl.h"
43#include "mirror/class_loader.h"
44#include "mirror/dex_cache-inl.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070045#include "mirror/object-inl.h"
46#include "mirror/object_array-inl.h"
47#include "oat.h"
48#include "oat_file.h"
49#include "object_utils.h"
50#include "runtime.h"
51#include "scoped_thread_state_change.h"
52#include "sirt_ref.h"
53#include "UniquePtr.h"
54#include "utils.h"
55
Brian Carlstromea46f952013-07-30 01:26:50 -070056using ::art::mirror::ArtField;
57using ::art::mirror::ArtMethod;
Brian Carlstrom3e3d5912013-07-18 00:19:45 -070058using ::art::mirror::Class;
59using ::art::mirror::DexCache;
60using ::art::mirror::EntryPointFromInterpreter;
Brian Carlstrom3e3d5912013-07-18 00:19:45 -070061using ::art::mirror::Object;
62using ::art::mirror::ObjectArray;
63using ::art::mirror::String;
Brian Carlstrom7940e442013-07-12 13:46:57 -070064
65namespace art {
66
67bool ImageWriter::Write(const std::string& image_filename,
68 uintptr_t image_begin,
69 const std::string& oat_filename,
70 const std::string& oat_location) {
71 CHECK(!image_filename.empty());
72
73 CHECK_NE(image_begin, 0U);
74 image_begin_ = reinterpret_cast<byte*>(image_begin);
75
76 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
77 const std::vector<DexCache*>& all_dex_caches = class_linker->GetDexCaches();
Mathieu Chartier02e25112013-08-14 16:14:24 -070078 dex_caches_.insert(all_dex_caches.begin(), all_dex_caches.end());
Brian Carlstrom7940e442013-07-12 13:46:57 -070079
Brian Carlstrom7571e8b2013-08-12 17:04:14 -070080 UniquePtr<File> oat_file(OS::OpenFileReadWrite(oat_filename.c_str()));
Brian Carlstrom7940e442013-07-12 13:46:57 -070081 if (oat_file.get() == NULL) {
82 LOG(ERROR) << "Failed to open oat file " << oat_filename << " for " << oat_location;
83 return false;
84 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -070085 std::string error_msg;
86 oat_file_ = OatFile::OpenWritable(oat_file.get(), oat_location, &error_msg);
87 if (oat_file_ == nullptr) {
88 LOG(ERROR) << "Failed to open writable oat file " << oat_filename << " for " << oat_location
89 << ": " << error_msg;
Brian Carlstromc50d8e12013-07-23 22:35:16 -070090 return false;
91 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -070092 CHECK_EQ(class_linker->RegisterOatFile(oat_file_), oat_file_);
Brian Carlstrom7940e442013-07-12 13:46:57 -070093
Ian Rogers848871b2013-08-05 10:56:33 -070094 interpreter_to_interpreter_bridge_offset_ =
95 oat_file_->GetOatHeader().GetInterpreterToInterpreterBridgeOffset();
96 interpreter_to_compiled_code_bridge_offset_ =
97 oat_file_->GetOatHeader().GetInterpreterToCompiledCodeBridgeOffset();
98
99 jni_dlsym_lookup_offset_ = oat_file_->GetOatHeader().GetJniDlsymLookupOffset();
100
101 portable_resolution_trampoline_offset_ =
102 oat_file_->GetOatHeader().GetPortableResolutionTrampolineOffset();
103 portable_to_interpreter_bridge_offset_ =
104 oat_file_->GetOatHeader().GetPortableToInterpreterBridgeOffset();
105
106 quick_resolution_trampoline_offset_ =
107 oat_file_->GetOatHeader().GetQuickResolutionTrampolineOffset();
108 quick_to_interpreter_bridge_offset_ =
109 oat_file_->GetOatHeader().GetQuickToInterpreterBridgeOffset();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700110 {
111 Thread::Current()->TransitionFromSuspendedToRunnable();
112 PruneNonImageClasses(); // Remove junk
113 ComputeLazyFieldsForImageClasses(); // Add useful information
114 ComputeEagerResolvedStrings();
115 Thread::Current()->TransitionFromRunnableToSuspended(kNative);
116 }
117 gc::Heap* heap = Runtime::Current()->GetHeap();
118 heap->CollectGarbage(false); // Remove garbage.
119 // Trim size of alloc spaces.
Mathieu Chartier02e25112013-08-14 16:14:24 -0700120 for (const auto& space : heap->GetContinuousSpaces()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700121 if (space->IsDlMallocSpace()) {
122 space->AsDlMallocSpace()->Trim();
123 }
124 }
125
126 if (!AllocMemory()) {
127 return false;
128 }
129#ifndef NDEBUG
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700130 { // NOLINT(whitespace/braces)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700131 ScopedObjectAccess soa(Thread::Current());
132 CheckNonImageClassesRemoved();
133 }
134#endif
135 Thread::Current()->TransitionFromSuspendedToRunnable();
136 size_t oat_loaded_size = 0;
137 size_t oat_data_offset = 0;
138 ElfWriter::GetOatElfInformation(oat_file.get(), oat_loaded_size, oat_data_offset);
139 CalculateNewObjectOffsets(oat_loaded_size, oat_data_offset);
140 CopyAndFixupObjects();
141 PatchOatCodeAndMethods();
Mathieu Chartier31e89252013-08-28 11:29:12 -0700142 // Record allocations into the image bitmap.
143 RecordImageAllocations();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700144 Thread::Current()->TransitionFromRunnableToSuspended(kNative);
145
Brian Carlstrom7571e8b2013-08-12 17:04:14 -0700146 UniquePtr<File> image_file(OS::CreateEmptyFile(image_filename.c_str()));
Mathieu Chartier31e89252013-08-28 11:29:12 -0700147 ImageHeader* image_header = reinterpret_cast<ImageHeader*>(image_->Begin());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700148 if (image_file.get() == NULL) {
149 LOG(ERROR) << "Failed to open image file " << image_filename;
150 return false;
151 }
152 if (fchmod(image_file->Fd(), 0644) != 0) {
153 PLOG(ERROR) << "Failed to make image file world readable: " << image_filename;
154 return EXIT_FAILURE;
155 }
Mathieu Chartier31e89252013-08-28 11:29:12 -0700156
157 // Write out the image.
158 CHECK_EQ(image_end_, image_header->GetImageSize());
159 if (!image_file->WriteFully(image_->Begin(), image_end_)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700160 PLOG(ERROR) << "Failed to write image file " << image_filename;
161 return false;
162 }
Mathieu Chartier31e89252013-08-28 11:29:12 -0700163
164 // Write out the image bitmap at the page aligned start of the image end.
165 CHECK_ALIGNED(image_header->GetImageBitmapOffset(), kPageSize);
166 if (!image_file->Write(reinterpret_cast<char*>(image_bitmap_->Begin()),
167 image_header->GetImageBitmapSize(),
168 image_header->GetImageBitmapOffset())) {
169 PLOG(ERROR) << "Failed to write image file " << image_filename;
170 return false;
171 }
172
Brian Carlstrom7940e442013-07-12 13:46:57 -0700173 return true;
174}
175
Mathieu Chartier31e89252013-08-28 11:29:12 -0700176void ImageWriter::RecordImageAllocations() {
177 uint64_t start_time = NanoTime();
178 CHECK(image_bitmap_.get() != nullptr);
179 for (const auto& it : offsets_) {
180 mirror::Object* obj = reinterpret_cast<mirror::Object*>(image_->Begin() + it.second);
181 DCHECK_ALIGNED(obj, kObjectAlignment);
182 image_bitmap_->Set(obj);
183 }
184 LOG(INFO) << "RecordImageAllocations took " << PrettyDuration(NanoTime() - start_time);
185}
186
Brian Carlstrom7940e442013-07-12 13:46:57 -0700187bool ImageWriter::AllocMemory() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700188 size_t size = 0;
Mathieu Chartier02e25112013-08-14 16:14:24 -0700189 for (const auto& space : Runtime::Current()->GetHeap()->GetContinuousSpaces()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700190 if (space->IsDlMallocSpace()) {
191 size += space->Size();
192 }
193 }
194
195 int prot = PROT_READ | PROT_WRITE;
196 size_t length = RoundUp(size, kPageSize);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700197 std::string error_msg;
198 image_.reset(MemMap::MapAnonymous("image writer image", NULL, length, prot, &error_msg));
199 if (UNLIKELY(image_.get() == nullptr)) {
200 LOG(ERROR) << "Failed to allocate memory for image file generation: " << error_msg;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700201 return false;
202 }
203 return true;
204}
205
206void ImageWriter::ComputeLazyFieldsForImageClasses() {
207 Runtime* runtime = Runtime::Current();
208 ClassLinker* class_linker = runtime->GetClassLinker();
209 class_linker->VisitClassesWithoutClassesLock(ComputeLazyFieldsForClassesVisitor, NULL);
210}
211
212bool ImageWriter::ComputeLazyFieldsForClassesVisitor(Class* c, void* /*arg*/) {
213 c->ComputeName();
214 return true;
215}
216
217void ImageWriter::ComputeEagerResolvedStringsCallback(Object* obj, void* arg) {
218 if (!obj->GetClass()->IsStringClass()) {
219 return;
220 }
221 String* string = obj->AsString();
222 const uint16_t* utf16_string = string->GetCharArray()->GetData() + string->GetOffset();
223 ImageWriter* writer = reinterpret_cast<ImageWriter*>(arg);
Mathieu Chartier02e25112013-08-14 16:14:24 -0700224 for (DexCache* dex_cache : writer->dex_caches_) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700225 const DexFile& dex_file = *dex_cache->GetDexFile();
226 const DexFile::StringId* string_id = dex_file.FindStringId(utf16_string);
227 if (string_id != NULL) {
228 // This string occurs in this dex file, assign the dex cache entry.
229 uint32_t string_idx = dex_file.GetIndexForStringId(*string_id);
230 if (dex_cache->GetResolvedString(string_idx) == NULL) {
231 dex_cache->SetResolvedString(string_idx, string);
232 }
233 }
234 }
235}
236
237void ImageWriter::ComputeEagerResolvedStrings()
238 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
239 // TODO: Check image spaces only?
240 gc::Heap* heap = Runtime::Current()->GetHeap();
241 WriterMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_);
242 heap->FlushAllocStack();
243 heap->GetLiveBitmap()->Walk(ComputeEagerResolvedStringsCallback, this);
244}
245
246bool ImageWriter::IsImageClass(const Class* klass) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700247 return compiler_driver_.IsImageClass(ClassHelper(klass).GetDescriptorAsStringPiece());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700248}
249
250struct NonImageClasses {
251 ImageWriter* image_writer;
252 std::set<std::string>* non_image_classes;
253};
254
255void ImageWriter::PruneNonImageClasses() {
256 if (compiler_driver_.GetImageClasses() == NULL) {
257 return;
258 }
259 Runtime* runtime = Runtime::Current();
260 ClassLinker* class_linker = runtime->GetClassLinker();
261
262 // Make a list of classes we would like to prune.
263 std::set<std::string> non_image_classes;
264 NonImageClasses context;
265 context.image_writer = this;
266 context.non_image_classes = &non_image_classes;
267 class_linker->VisitClasses(NonImageClassesVisitor, &context);
268
269 // Remove the undesired classes from the class roots.
Mathieu Chartier02e25112013-08-14 16:14:24 -0700270 for (const std::string& it : non_image_classes) {
271 class_linker->RemoveClass(it.c_str(), NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700272 }
273
274 // Clear references to removed classes from the DexCaches.
Brian Carlstromea46f952013-07-30 01:26:50 -0700275 ArtMethod* resolution_method = runtime->GetResolutionMethod();
Mathieu Chartier02e25112013-08-14 16:14:24 -0700276 for (DexCache* dex_cache : dex_caches_) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700277 for (size_t i = 0; i < dex_cache->NumResolvedTypes(); i++) {
278 Class* klass = dex_cache->GetResolvedType(i);
279 if (klass != NULL && !IsImageClass(klass)) {
280 dex_cache->SetResolvedType(i, NULL);
281 dex_cache->GetInitializedStaticStorage()->Set(i, NULL);
282 }
283 }
284 for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700285 ArtMethod* method = dex_cache->GetResolvedMethod(i);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700286 if (method != NULL && !IsImageClass(method->GetDeclaringClass())) {
287 dex_cache->SetResolvedMethod(i, resolution_method);
288 }
289 }
290 for (size_t i = 0; i < dex_cache->NumResolvedFields(); i++) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700291 ArtField* field = dex_cache->GetResolvedField(i);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700292 if (field != NULL && !IsImageClass(field->GetDeclaringClass())) {
293 dex_cache->SetResolvedField(i, NULL);
294 }
295 }
296 }
297}
298
299bool ImageWriter::NonImageClassesVisitor(Class* klass, void* arg) {
300 NonImageClasses* context = reinterpret_cast<NonImageClasses*>(arg);
301 if (!context->image_writer->IsImageClass(klass)) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700302 context->non_image_classes->insert(ClassHelper(klass).GetDescriptorAsStringPiece().as_string());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700303 }
304 return true;
305}
306
307void ImageWriter::CheckNonImageClassesRemoved()
308 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
309 if (compiler_driver_.GetImageClasses() == NULL) {
310 return;
311 }
312
313 gc::Heap* heap = Runtime::Current()->GetHeap();
314 Thread* self = Thread::Current();
315 {
316 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
317 heap->FlushAllocStack();
318 }
319
320 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
321 heap->GetLiveBitmap()->Walk(CheckNonImageClassesRemovedCallback, this);
322}
323
324void ImageWriter::CheckNonImageClassesRemovedCallback(Object* obj, void* arg) {
325 ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg);
326 if (!obj->IsClass()) {
327 return;
328 }
329 Class* klass = obj->AsClass();
330 if (!image_writer->IsImageClass(klass)) {
331 image_writer->DumpImageClasses();
332 CHECK(image_writer->IsImageClass(klass)) << ClassHelper(klass).GetDescriptor()
333 << " " << PrettyDescriptor(klass);
334 }
335}
336
337void ImageWriter::DumpImageClasses() {
338 CompilerDriver::DescriptorSet* image_classes = compiler_driver_.GetImageClasses();
339 CHECK(image_classes != NULL);
Mathieu Chartier02e25112013-08-14 16:14:24 -0700340 for (const std::string& image_class : *image_classes) {
341 LOG(INFO) << " " << image_class;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700342 }
343}
344
345void ImageWriter::CalculateNewObjectOffsetsCallback(Object* obj, void* arg) {
346 DCHECK(obj != NULL);
347 DCHECK(arg != NULL);
348 ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg);
349
350 // if it is a string, we want to intern it if its not interned.
351 if (obj->GetClass()->IsStringClass()) {
352 // we must be an interned string that was forward referenced and already assigned
353 if (image_writer->IsImageOffsetAssigned(obj)) {
354 DCHECK_EQ(obj, obj->AsString()->Intern());
355 return;
356 }
357 SirtRef<String> interned(Thread::Current(), obj->AsString()->Intern());
358 if (obj != interned.get()) {
359 if (!image_writer->IsImageOffsetAssigned(interned.get())) {
360 // interned obj is after us, allocate its location early
361 image_writer->AssignImageOffset(interned.get());
362 }
363 // point those looking for this object to the interned version.
364 image_writer->SetImageOffset(obj, image_writer->GetImageOffset(interned.get()));
365 return;
366 }
367 // else (obj == interned), nothing to do but fall through to the normal case
368 }
369
370 image_writer->AssignImageOffset(obj);
371}
372
373ObjectArray<Object>* ImageWriter::CreateImageRoots() const {
374 Runtime* runtime = Runtime::Current();
375 ClassLinker* class_linker = runtime->GetClassLinker();
376 Class* object_array_class = class_linker->FindSystemClass("[Ljava/lang/Object;");
377 Thread* self = Thread::Current();
378
379 // build an Object[] of all the DexCaches used in the source_space_
380 ObjectArray<Object>* dex_caches = ObjectArray<Object>::Alloc(self, object_array_class,
381 dex_caches_.size());
382 int i = 0;
Mathieu Chartier02e25112013-08-14 16:14:24 -0700383 for (DexCache* dex_cache : dex_caches_) {
384 dex_caches->Set(i++, dex_cache);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700385 }
386
387 // build an Object[] of the roots needed to restore the runtime
388 SirtRef<ObjectArray<Object> >
389 image_roots(self,
390 ObjectArray<Object>::Alloc(self, object_array_class,
391 ImageHeader::kImageRootsMax));
392 image_roots->Set(ImageHeader::kResolutionMethod, runtime->GetResolutionMethod());
393 image_roots->Set(ImageHeader::kCalleeSaveMethod,
394 runtime->GetCalleeSaveMethod(Runtime::kSaveAll));
395 image_roots->Set(ImageHeader::kRefsOnlySaveMethod,
396 runtime->GetCalleeSaveMethod(Runtime::kRefsOnly));
397 image_roots->Set(ImageHeader::kRefsAndArgsSaveMethod,
398 runtime->GetCalleeSaveMethod(Runtime::kRefsAndArgs));
399 image_roots->Set(ImageHeader::kOatLocation,
400 String::AllocFromModifiedUtf8(self, oat_file_->GetLocation().c_str()));
401 image_roots->Set(ImageHeader::kDexCaches,
402 dex_caches);
403 image_roots->Set(ImageHeader::kClassRoots,
404 class_linker->GetClassRoots());
405 for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
406 CHECK(image_roots->Get(i) != NULL);
407 }
408 return image_roots.get();
409}
410
411void ImageWriter::CalculateNewObjectOffsets(size_t oat_loaded_size, size_t oat_data_offset) {
412 CHECK_NE(0U, oat_loaded_size);
413 Thread* self = Thread::Current();
414 SirtRef<ObjectArray<Object> > image_roots(self, CreateImageRoots());
415
416 gc::Heap* heap = Runtime::Current()->GetHeap();
Mathieu Chartier02e25112013-08-14 16:14:24 -0700417 const auto& spaces = heap->GetContinuousSpaces();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700418 DCHECK(!spaces.empty());
419 DCHECK_EQ(0U, image_end_);
420
Mathieu Chartier31e89252013-08-28 11:29:12 -0700421 // Leave space for the header, but do not write it yet, we need to
Brian Carlstrom7940e442013-07-12 13:46:57 -0700422 // know where image_roots is going to end up
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700423 image_end_ += RoundUp(sizeof(ImageHeader), 8); // 64-bit-alignment
Brian Carlstrom7940e442013-07-12 13:46:57 -0700424
425 {
426 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
427 heap->FlushAllocStack();
428 // TODO: Image spaces only?
429 // TODO: Add InOrderWalk to heap bitmap.
430 const char* old = self->StartAssertNoThreadSuspension("ImageWriter");
431 DCHECK(heap->GetLargeObjectsSpace()->GetLiveObjects()->IsEmpty());
Mathieu Chartier02e25112013-08-14 16:14:24 -0700432 for (const auto& space : spaces) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700433 space->GetLiveBitmap()->InOrderWalk(CalculateNewObjectOffsetsCallback, this);
434 DCHECK_LT(image_end_, image_->Size());
435 }
436 self->EndAssertNoThreadSuspension(old);
437 }
438
Mathieu Chartier31e89252013-08-28 11:29:12 -0700439 // Create the image bitmap.
440 image_bitmap_.reset(gc::accounting::SpaceBitmap::Create("image bitmap", image_->Begin(),
441 image_end_));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700442 const byte* oat_file_begin = image_begin_ + RoundUp(image_end_, kPageSize);
443 const byte* oat_file_end = oat_file_begin + oat_loaded_size;
444 oat_data_begin_ = oat_file_begin + oat_data_offset;
445 const byte* oat_data_end = oat_data_begin_ + oat_file_->Size();
446
Mathieu Chartier31e89252013-08-28 11:29:12 -0700447 // Return to write header at start of image with future location of image_roots. At this point,
448 // image_end_ is the size of the image (excluding bitmaps).
Brian Carlstrom7940e442013-07-12 13:46:57 -0700449 ImageHeader image_header(reinterpret_cast<uint32_t>(image_begin_),
Mathieu Chartier31e89252013-08-28 11:29:12 -0700450 static_cast<uint32_t>(image_end_),
451 RoundUp(image_end_, kPageSize),
452 image_bitmap_->Size(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700453 reinterpret_cast<uint32_t>(GetImageAddress(image_roots.get())),
454 oat_file_->GetOatHeader().GetChecksum(),
455 reinterpret_cast<uint32_t>(oat_file_begin),
456 reinterpret_cast<uint32_t>(oat_data_begin_),
457 reinterpret_cast<uint32_t>(oat_data_end),
458 reinterpret_cast<uint32_t>(oat_file_end));
459 memcpy(image_->Begin(), &image_header, sizeof(image_header));
460
461 // Note that image_end_ is left at end of used space
462}
463
464void ImageWriter::CopyAndFixupObjects()
465 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
466 Thread* self = Thread::Current();
467 const char* old_cause = self->StartAssertNoThreadSuspension("ImageWriter");
468 gc::Heap* heap = Runtime::Current()->GetHeap();
469 // TODO: heap validation can't handle this fix up pass
470 heap->DisableObjectValidation();
471 // TODO: Image spaces only?
472 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
473 heap->FlushAllocStack();
474 heap->GetLiveBitmap()->Walk(CopyAndFixupObjectsCallback, this);
475 self->EndAssertNoThreadSuspension(old_cause);
476}
477
478void ImageWriter::CopyAndFixupObjectsCallback(Object* object, void* arg) {
479 DCHECK(object != NULL);
480 DCHECK(arg != NULL);
481 const Object* obj = object;
482 ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg);
483
484 // see GetLocalAddress for similar computation
485 size_t offset = image_writer->GetImageOffset(obj);
486 byte* dst = image_writer->image_->Begin() + offset;
487 const byte* src = reinterpret_cast<const byte*>(obj);
488 size_t n = obj->SizeOf();
489 DCHECK_LT(offset + n, image_writer->image_->Size());
490 memcpy(dst, src, n);
491 Object* copy = reinterpret_cast<Object*>(dst);
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700492 copy->SetField32(Object::MonitorOffset(), 0, false); // We may have inflated the lock during compilation.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700493 image_writer->FixupObject(obj, copy);
494}
495
496void ImageWriter::FixupObject(const Object* orig, Object* copy) {
497 DCHECK(orig != NULL);
498 DCHECK(copy != NULL);
499 copy->SetClass(down_cast<Class*>(GetImageAddress(orig->GetClass())));
500 // TODO: special case init of pointers to malloc data (or removal of these pointers)
501 if (orig->IsClass()) {
502 FixupClass(orig->AsClass(), down_cast<Class*>(copy));
503 } else if (orig->IsObjectArray()) {
504 FixupObjectArray(orig->AsObjectArray<Object>(), down_cast<ObjectArray<Object>*>(copy));
Brian Carlstromea46f952013-07-30 01:26:50 -0700505 } else if (orig->IsArtMethod()) {
506 FixupMethod(orig->AsArtMethod(), down_cast<ArtMethod*>(copy));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700507 } else {
508 FixupInstanceFields(orig, copy);
509 }
510}
511
512void ImageWriter::FixupClass(const Class* orig, Class* copy) {
513 FixupInstanceFields(orig, copy);
514 FixupStaticFields(orig, copy);
515}
516
Brian Carlstromea46f952013-07-30 01:26:50 -0700517void ImageWriter::FixupMethod(const ArtMethod* orig, ArtMethod* copy) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700518 FixupInstanceFields(orig, copy);
519
Ian Rogers848871b2013-08-05 10:56:33 -0700520 // OatWriter replaces the code_ with an offset value. Here we re-adjust to a pointer relative to
521 // oat_begin_
Brian Carlstrom7940e442013-07-12 13:46:57 -0700522
Ian Rogers848871b2013-08-05 10:56:33 -0700523 // The resolution method has a special trampoline to call.
524 if (UNLIKELY(orig == Runtime::Current()->GetResolutionMethod())) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700525#if defined(ART_USE_PORTABLE_COMPILER)
526 copy->SetEntryPointFromCompiledCode(GetOatAddress(portable_resolution_trampoline_offset_));
527#else
528 copy->SetEntryPointFromCompiledCode(GetOatAddress(quick_resolution_trampoline_offset_));
529#endif
Brian Carlstrom7940e442013-07-12 13:46:57 -0700530 } else {
Ian Rogers848871b2013-08-05 10:56:33 -0700531 // We assume all methods have code. If they don't currently then we set them to the use the
532 // resolution trampoline. Abstract methods never have code and so we need to make sure their
533 // use results in an AbstractMethodError. We use the interpreter to achieve this.
534 if (UNLIKELY(orig->IsAbstract())) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700535#if defined(ART_USE_PORTABLE_COMPILER)
Ian Rogers848871b2013-08-05 10:56:33 -0700536 copy->SetEntryPointFromCompiledCode(GetOatAddress(portable_to_interpreter_bridge_offset_));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700537#else
Ian Rogers848871b2013-08-05 10:56:33 -0700538 copy->SetEntryPointFromCompiledCode(GetOatAddress(quick_to_interpreter_bridge_offset_));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700539#endif
Ian Rogers848871b2013-08-05 10:56:33 -0700540 copy->SetEntryPointFromInterpreter(reinterpret_cast<EntryPointFromInterpreter*>
541 (GetOatAddress(interpreter_to_interpreter_bridge_offset_)));
542 } else {
543 copy->SetEntryPointFromInterpreter(reinterpret_cast<EntryPointFromInterpreter*>
544 (GetOatAddress(interpreter_to_compiled_code_bridge_offset_)));
545 // Use original code if it exists. Otherwise, set the code pointer to the resolution
546 // trampoline.
547 const byte* code = GetOatAddress(orig->GetOatCodeOffset());
548 if (code != NULL) {
549 copy->SetEntryPointFromCompiledCode(code);
550 } else {
551#if defined(ART_USE_PORTABLE_COMPILER)
552 copy->SetEntryPointFromCompiledCode(GetOatAddress(portable_resolution_trampoline_offset_));
553#else
554 copy->SetEntryPointFromCompiledCode(GetOatAddress(quick_resolution_trampoline_offset_));
555#endif
556 }
557 if (orig->IsNative()) {
558 // The native method's pointer is set to a stub to lookup via dlsym.
559 // Note this is not the code_ pointer, that is handled above.
560 copy->SetNativeMethod(GetOatAddress(jni_dlsym_lookup_offset_));
561 } else {
562 // Normal (non-abstract non-native) methods have various tables to relocate.
563 uint32_t mapping_table_off = orig->GetOatMappingTableOffset();
564 const byte* mapping_table = GetOatAddress(mapping_table_off);
Ian Rogers1809a722013-08-09 22:05:32 -0700565 copy->SetMappingTable(mapping_table);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700566
Ian Rogers848871b2013-08-05 10:56:33 -0700567 uint32_t vmap_table_offset = orig->GetOatVmapTableOffset();
568 const byte* vmap_table = GetOatAddress(vmap_table_offset);
Ian Rogers1809a722013-08-09 22:05:32 -0700569 copy->SetVmapTable(vmap_table);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700570
Ian Rogers848871b2013-08-05 10:56:33 -0700571 uint32_t native_gc_map_offset = orig->GetOatNativeGcMapOffset();
572 const byte* native_gc_map = GetOatAddress(native_gc_map_offset);
573 copy->SetNativeGcMap(reinterpret_cast<const uint8_t*>(native_gc_map));
574 }
575 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700576 }
577}
578
579void ImageWriter::FixupObjectArray(const ObjectArray<Object>* orig, ObjectArray<Object>* copy) {
580 for (int32_t i = 0; i < orig->GetLength(); ++i) {
581 const Object* element = orig->Get(i);
582 copy->SetPtrWithoutChecks(i, GetImageAddress(element));
583 }
584}
585
586void ImageWriter::FixupInstanceFields(const Object* orig, Object* copy) {
587 DCHECK(orig != NULL);
588 DCHECK(copy != NULL);
589 Class* klass = orig->GetClass();
590 DCHECK(klass != NULL);
591 FixupFields(orig,
592 copy,
593 klass->GetReferenceInstanceOffsets(),
594 false);
595}
596
597void ImageWriter::FixupStaticFields(const Class* orig, Class* copy) {
598 DCHECK(orig != NULL);
599 DCHECK(copy != NULL);
600 FixupFields(orig,
601 copy,
602 orig->GetReferenceStaticOffsets(),
603 true);
604}
605
606void ImageWriter::FixupFields(const Object* orig,
607 Object* copy,
608 uint32_t ref_offsets,
609 bool is_static) {
610 if (ref_offsets != CLASS_WALK_SUPER) {
611 // Found a reference offset bitmap. Fixup the specified offsets.
612 while (ref_offsets != 0) {
613 size_t right_shift = CLZ(ref_offsets);
614 MemberOffset byte_offset = CLASS_OFFSET_FROM_CLZ(right_shift);
615 const Object* ref = orig->GetFieldObject<const Object*>(byte_offset, false);
616 // Use SetFieldPtr to avoid card marking since we are writing to the image.
617 copy->SetFieldPtr(byte_offset, GetImageAddress(ref), false);
618 ref_offsets &= ~(CLASS_HIGH_BIT >> right_shift);
619 }
620 } else {
621 // There is no reference offset bitmap. In the non-static case,
622 // walk up the class inheritance hierarchy and find reference
623 // offsets the hard way. In the static case, just consider this
624 // class.
625 for (const Class *klass = is_static ? orig->AsClass() : orig->GetClass();
626 klass != NULL;
627 klass = is_static ? NULL : klass->GetSuperClass()) {
628 size_t num_reference_fields = (is_static
629 ? klass->NumReferenceStaticFields()
630 : klass->NumReferenceInstanceFields());
631 for (size_t i = 0; i < num_reference_fields; ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700632 ArtField* field = (is_static
633 ? klass->GetStaticField(i)
634 : klass->GetInstanceField(i));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700635 MemberOffset field_offset = field->GetOffset();
636 const Object* ref = orig->GetFieldObject<const Object*>(field_offset, false);
637 // Use SetFieldPtr to avoid card marking since we are writing to the image.
638 copy->SetFieldPtr(field_offset, GetImageAddress(ref), false);
639 }
640 }
641 }
642 if (!is_static && orig->IsReferenceInstance()) {
643 // Fix-up referent, that isn't marked as an object field, for References.
Brian Carlstromea46f952013-07-30 01:26:50 -0700644 ArtField* field = orig->GetClass()->FindInstanceField("referent", "Ljava/lang/Object;");
Brian Carlstrom7940e442013-07-12 13:46:57 -0700645 MemberOffset field_offset = field->GetOffset();
646 const Object* ref = orig->GetFieldObject<const Object*>(field_offset, false);
647 // Use SetFieldPtr to avoid card marking since we are writing to the image.
648 copy->SetFieldPtr(field_offset, GetImageAddress(ref), false);
649 }
650}
651
Brian Carlstromea46f952013-07-30 01:26:50 -0700652static ArtMethod* GetTargetMethod(const CompilerDriver::PatchInformation* patch)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700653 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
654 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
655 DexCache* dex_cache = class_linker->FindDexCache(patch->GetDexFile());
Brian Carlstromea46f952013-07-30 01:26:50 -0700656 ArtMethod* method = class_linker->ResolveMethod(patch->GetDexFile(),
657 patch->GetTargetMethodIdx(),
658 dex_cache,
659 NULL,
660 NULL,
661 patch->GetTargetInvokeType());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700662 CHECK(method != NULL)
663 << patch->GetDexFile().GetLocation() << " " << patch->GetTargetMethodIdx();
664 CHECK(!method->IsRuntimeMethod())
665 << patch->GetDexFile().GetLocation() << " " << patch->GetTargetMethodIdx();
666 CHECK(dex_cache->GetResolvedMethods()->Get(patch->GetTargetMethodIdx()) == method)
667 << patch->GetDexFile().GetLocation() << " " << patch->GetReferrerMethodIdx() << " "
668 << PrettyMethod(dex_cache->GetResolvedMethods()->Get(patch->GetTargetMethodIdx())) << " "
669 << PrettyMethod(method);
670 return method;
671}
672
673void ImageWriter::PatchOatCodeAndMethods() {
674 Thread* self = Thread::Current();
675 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
676 const char* old_cause = self->StartAssertNoThreadSuspension("ImageWriter");
677
678 typedef std::vector<const CompilerDriver::PatchInformation*> Patches;
679 const Patches& code_to_patch = compiler_driver_.GetCodeToPatch();
680 for (size_t i = 0; i < code_to_patch.size(); i++) {
681 const CompilerDriver::PatchInformation* patch = code_to_patch[i];
Brian Carlstromea46f952013-07-30 01:26:50 -0700682 ArtMethod* target = GetTargetMethod(patch);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700683 uint32_t code = reinterpret_cast<uint32_t>(class_linker->GetOatCodeFor(target));
684 uint32_t code_base = reinterpret_cast<uint32_t>(&oat_file_->GetOatHeader());
685 uint32_t code_offset = code - code_base;
686 SetPatchLocation(patch, reinterpret_cast<uint32_t>(GetOatAddress(code_offset)));
687 }
688
689 const Patches& methods_to_patch = compiler_driver_.GetMethodsToPatch();
690 for (size_t i = 0; i < methods_to_patch.size(); i++) {
691 const CompilerDriver::PatchInformation* patch = methods_to_patch[i];
Brian Carlstromea46f952013-07-30 01:26:50 -0700692 ArtMethod* target = GetTargetMethod(patch);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700693 SetPatchLocation(patch, reinterpret_cast<uint32_t>(GetImageAddress(target)));
694 }
695
696 // Update the image header with the new checksum after patching
697 ImageHeader* image_header = reinterpret_cast<ImageHeader*>(image_->Begin());
698 image_header->SetOatChecksum(oat_file_->GetOatHeader().GetChecksum());
699 self->EndAssertNoThreadSuspension(old_cause);
700}
701
702void ImageWriter::SetPatchLocation(const CompilerDriver::PatchInformation* patch, uint32_t value) {
703 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
704 const void* oat_code = class_linker->GetOatCodeFor(patch->GetDexFile(),
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700705 patch->GetReferrerClassDefIdx(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700706 patch->GetReferrerMethodIdx());
707 OatHeader& oat_header = const_cast<OatHeader&>(oat_file_->GetOatHeader());
708 // TODO: make this Thumb2 specific
709 uint8_t* base = reinterpret_cast<uint8_t*>(reinterpret_cast<uint32_t>(oat_code) & ~0x1);
710 uint32_t* patch_location = reinterpret_cast<uint32_t*>(base + patch->GetLiteralOffset());
711#ifndef NDEBUG
712 const DexFile::MethodId& id = patch->GetDexFile().GetMethodId(patch->GetTargetMethodIdx());
713 uint32_t expected = reinterpret_cast<uint32_t>(&id);
714 uint32_t actual = *patch_location;
715 CHECK(actual == expected || actual == value) << std::hex
716 << "actual=" << actual
717 << "expected=" << expected
718 << "value=" << value;
719#endif
720 *patch_location = value;
721 oat_header.UpdateChecksum(patch_location, sizeof(value));
722}
723
724} // namespace art