image_writer.cc revision 9433ec60b325b708b9fa87e699ab4a6565741494
1/*
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 <memory>
22#include <vector>
23
24#include "base/logging.h"
25#include "base/unix_file/fd_file.h"
26#include "class_linker.h"
27#include "compiled_method.h"
28#include "dex_file-inl.h"
29#include "driver/compiler_driver.h"
30#include "elf_file.h"
31#include "elf_utils.h"
32#include "elf_patcher.h"
33#include "elf_writer.h"
34#include "gc/accounting/card_table-inl.h"
35#include "gc/accounting/heap_bitmap.h"
36#include "gc/accounting/space_bitmap-inl.h"
37#include "gc/heap.h"
38#include "gc/space/large_object_space.h"
39#include "gc/space/space-inl.h"
40#include "globals.h"
41#include "image.h"
42#include "intern_table.h"
43#include "lock_word.h"
44#include "mirror/art_field-inl.h"
45#include "mirror/art_method-inl.h"
46#include "mirror/array-inl.h"
47#include "mirror/class-inl.h"
48#include "mirror/class_loader.h"
49#include "mirror/dex_cache-inl.h"
50#include "mirror/object-inl.h"
51#include "mirror/object_array-inl.h"
52#include "mirror/string-inl.h"
53#include "oat.h"
54#include "oat_file.h"
55#include "runtime.h"
56#include "scoped_thread_state_change.h"
57#include "handle_scope-inl.h"
58#include "utils.h"
59
60using ::art::mirror::ArtField;
61using ::art::mirror::ArtMethod;
62using ::art::mirror::Class;
63using ::art::mirror::DexCache;
64using ::art::mirror::EntryPointFromInterpreter;
65using ::art::mirror::Object;
66using ::art::mirror::ObjectArray;
67using ::art::mirror::String;
68
69namespace art {
70
71bool ImageWriter::Write(const std::string& image_filename,
72                        uintptr_t image_begin,
73                        const std::string& oat_filename,
74                        const std::string& oat_location,
75                        bool compile_pic) {
76  CHECK(!image_filename.empty());
77
78  CHECK_NE(image_begin, 0U);
79  image_begin_ = reinterpret_cast<byte*>(image_begin);
80  compile_pic_ = compile_pic;
81
82  ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
83
84  std::unique_ptr<File> oat_file(OS::OpenFileReadWrite(oat_filename.c_str()));
85  if (oat_file.get() == NULL) {
86    LOG(ERROR) << "Failed to open oat file " << oat_filename << " for " << oat_location;
87    return false;
88  }
89  std::string error_msg;
90  oat_file_ = OatFile::OpenReadable(oat_file.get(), oat_location, &error_msg);
91  if (oat_file_ == nullptr) {
92    LOG(ERROR) << "Failed to open writable oat file " << oat_filename << " for " << oat_location
93        << ": " << error_msg;
94    return false;
95  }
96  CHECK_EQ(class_linker->RegisterOatFile(oat_file_), oat_file_);
97
98  interpreter_to_interpreter_bridge_offset_ =
99      oat_file_->GetOatHeader().GetInterpreterToInterpreterBridgeOffset();
100  interpreter_to_compiled_code_bridge_offset_ =
101      oat_file_->GetOatHeader().GetInterpreterToCompiledCodeBridgeOffset();
102
103  jni_dlsym_lookup_offset_ = oat_file_->GetOatHeader().GetJniDlsymLookupOffset();
104
105  portable_imt_conflict_trampoline_offset_ =
106      oat_file_->GetOatHeader().GetPortableImtConflictTrampolineOffset();
107  portable_resolution_trampoline_offset_ =
108      oat_file_->GetOatHeader().GetPortableResolutionTrampolineOffset();
109  portable_to_interpreter_bridge_offset_ =
110      oat_file_->GetOatHeader().GetPortableToInterpreterBridgeOffset();
111
112  quick_generic_jni_trampoline_offset_ =
113      oat_file_->GetOatHeader().GetQuickGenericJniTrampolineOffset();
114  quick_imt_conflict_trampoline_offset_ =
115      oat_file_->GetOatHeader().GetQuickImtConflictTrampolineOffset();
116  quick_resolution_trampoline_offset_ =
117      oat_file_->GetOatHeader().GetQuickResolutionTrampolineOffset();
118  quick_to_interpreter_bridge_offset_ =
119      oat_file_->GetOatHeader().GetQuickToInterpreterBridgeOffset();
120  {
121    Thread::Current()->TransitionFromSuspendedToRunnable();
122    PruneNonImageClasses();  // Remove junk
123    ComputeLazyFieldsForImageClasses();  // Add useful information
124    ComputeEagerResolvedStrings();
125    Thread::Current()->TransitionFromRunnableToSuspended(kNative);
126  }
127  gc::Heap* heap = Runtime::Current()->GetHeap();
128  heap->CollectGarbage(false);  // Remove garbage.
129
130  if (!AllocMemory()) {
131    return false;
132  }
133
134  if (kIsDebugBuild) {
135    ScopedObjectAccess soa(Thread::Current());
136    CheckNonImageClassesRemoved();
137  }
138
139  Thread::Current()->TransitionFromSuspendedToRunnable();
140  size_t oat_loaded_size = 0;
141  size_t oat_data_offset = 0;
142  ElfWriter::GetOatElfInformation(oat_file.get(), oat_loaded_size, oat_data_offset);
143  CalculateNewObjectOffsets(oat_loaded_size, oat_data_offset);
144  CopyAndFixupObjects();
145
146  PatchOatCodeAndMethods(oat_file.get());
147
148  // Before flushing, which might fail, release the mutator lock.
149  Thread::Current()->TransitionFromRunnableToSuspended(kNative);
150
151  if (oat_file->FlushCloseOrErase() != 0) {
152    LOG(ERROR) << "Failed to flush and close oat file " << oat_filename << " for " << oat_location;
153    return false;
154  }
155
156  std::unique_ptr<File> image_file(OS::CreateEmptyFile(image_filename.c_str()));
157  ImageHeader* image_header = reinterpret_cast<ImageHeader*>(image_->Begin());
158  if (image_file.get() == NULL) {
159    LOG(ERROR) << "Failed to open image file " << image_filename;
160    return false;
161  }
162  if (fchmod(image_file->Fd(), 0644) != 0) {
163    PLOG(ERROR) << "Failed to make image file world readable: " << image_filename;
164    image_file->Erase();
165    return EXIT_FAILURE;
166  }
167
168  // Write out the image.
169  CHECK_EQ(image_end_, image_header->GetImageSize());
170  if (!image_file->WriteFully(image_->Begin(), image_end_)) {
171    PLOG(ERROR) << "Failed to write image file " << image_filename;
172    image_file->Erase();
173    return false;
174  }
175
176  // Write out the image bitmap at the page aligned start of the image end.
177  CHECK_ALIGNED(image_header->GetImageBitmapOffset(), kPageSize);
178  if (!image_file->Write(reinterpret_cast<char*>(image_bitmap_->Begin()),
179                         image_header->GetImageBitmapSize(),
180                         image_header->GetImageBitmapOffset())) {
181    PLOG(ERROR) << "Failed to write image file " << image_filename;
182    image_file->Erase();
183    return false;
184  }
185
186  if (image_file->FlushCloseOrErase() != 0) {
187    PLOG(ERROR) << "Failed to flush and close image file " << image_filename;
188    return false;
189  }
190  return true;
191}
192
193void ImageWriter::SetImageOffset(mirror::Object* object, size_t offset) {
194  DCHECK(object != nullptr);
195  DCHECK_NE(offset, 0U);
196  DCHECK(!IsImageOffsetAssigned(object));
197  mirror::Object* obj = reinterpret_cast<mirror::Object*>(image_->Begin() + offset);
198  DCHECK_ALIGNED(obj, kObjectAlignment);
199  image_bitmap_->Set(obj);
200  // Before we stomp over the lock word, save the hash code for later.
201  Monitor::Deflate(Thread::Current(), object);;
202  LockWord lw(object->GetLockWord(false));
203  switch (lw.GetState()) {
204    case LockWord::kFatLocked: {
205      LOG(FATAL) << "Fat locked object " << obj << " found during object copy";
206      break;
207    }
208    case LockWord::kThinLocked: {
209      LOG(FATAL) << "Thin locked object " << obj << " found during object copy";
210      break;
211    }
212    case LockWord::kUnlocked:
213      // No hash, don't need to save it.
214      break;
215    case LockWord::kHashCode:
216      saved_hashes_.push_back(std::make_pair(obj, lw.GetHashCode()));
217      break;
218    default:
219      LOG(FATAL) << "Unreachable.";
220      break;
221  }
222  object->SetLockWord(LockWord::FromForwardingAddress(offset), false);
223  DCHECK(IsImageOffsetAssigned(object));
224}
225
226void ImageWriter::AssignImageOffset(mirror::Object* object) {
227  DCHECK(object != nullptr);
228  SetImageOffset(object, image_end_);
229  image_end_ += RoundUp(object->SizeOf(), 8);  // 64-bit alignment
230  DCHECK_LT(image_end_, image_->Size());
231}
232
233bool ImageWriter::IsImageOffsetAssigned(mirror::Object* object) const {
234  DCHECK(object != nullptr);
235  return object->GetLockWord(false).GetState() == LockWord::kForwardingAddress;
236}
237
238size_t ImageWriter::GetImageOffset(mirror::Object* object) const {
239  DCHECK(object != nullptr);
240  DCHECK(IsImageOffsetAssigned(object));
241  LockWord lock_word = object->GetLockWord(false);
242  size_t offset = lock_word.ForwardingAddress();
243  DCHECK_LT(offset, image_end_);
244  return offset;
245}
246
247bool ImageWriter::AllocMemory() {
248  size_t length = RoundUp(Runtime::Current()->GetHeap()->GetTotalMemory(), kPageSize);
249  std::string error_msg;
250  image_.reset(MemMap::MapAnonymous("image writer image", NULL, length, PROT_READ | PROT_WRITE,
251                                    true, &error_msg));
252  if (UNLIKELY(image_.get() == nullptr)) {
253    LOG(ERROR) << "Failed to allocate memory for image file generation: " << error_msg;
254    return false;
255  }
256
257  // Create the image bitmap.
258  image_bitmap_.reset(gc::accounting::ContinuousSpaceBitmap::Create("image bitmap", image_->Begin(),
259                                                                    length));
260  if (image_bitmap_.get() == nullptr) {
261    LOG(ERROR) << "Failed to allocate memory for image bitmap";
262    return false;
263  }
264  return true;
265}
266
267void ImageWriter::ComputeLazyFieldsForImageClasses() {
268  ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
269  class_linker->VisitClassesWithoutClassesLock(ComputeLazyFieldsForClassesVisitor, NULL);
270}
271
272bool ImageWriter::ComputeLazyFieldsForClassesVisitor(Class* c, void* /*arg*/) {
273  Thread* self = Thread::Current();
274  StackHandleScope<1> hs(self);
275  mirror::Class::ComputeName(hs.NewHandle(c));
276  return true;
277}
278
279void ImageWriter::ComputeEagerResolvedStringsCallback(Object* obj, void* arg) {
280  if (!obj->GetClass()->IsStringClass()) {
281    return;
282  }
283  mirror::String* string = obj->AsString();
284  const uint16_t* utf16_string = string->GetCharArray()->GetData() + string->GetOffset();
285  ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
286  ReaderMutexLock mu(Thread::Current(), *class_linker->DexLock());
287  size_t dex_cache_count = class_linker->GetDexCacheCount();
288  for (size_t i = 0; i < dex_cache_count; ++i) {
289    DexCache* dex_cache = class_linker->GetDexCache(i);
290    const DexFile& dex_file = *dex_cache->GetDexFile();
291    const DexFile::StringId* string_id;
292    if (UNLIKELY(string->GetLength() == 0)) {
293      string_id = dex_file.FindStringId("");
294    } else {
295      string_id = dex_file.FindStringId(utf16_string);
296    }
297    if (string_id != nullptr) {
298      // This string occurs in this dex file, assign the dex cache entry.
299      uint32_t string_idx = dex_file.GetIndexForStringId(*string_id);
300      if (dex_cache->GetResolvedString(string_idx) == NULL) {
301        dex_cache->SetResolvedString(string_idx, string);
302      }
303    }
304  }
305}
306
307void ImageWriter::ComputeEagerResolvedStrings() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
308  ReaderMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_);
309  Runtime::Current()->GetHeap()->VisitObjects(ComputeEagerResolvedStringsCallback, this);
310}
311
312bool ImageWriter::IsImageClass(Class* klass) {
313  std::string temp;
314  return compiler_driver_.IsImageClass(klass->GetDescriptor(&temp));
315}
316
317struct NonImageClasses {
318  ImageWriter* image_writer;
319  std::set<std::string>* non_image_classes;
320};
321
322void ImageWriter::PruneNonImageClasses() {
323  if (compiler_driver_.GetImageClasses() == NULL) {
324    return;
325  }
326  Runtime* runtime = Runtime::Current();
327  ClassLinker* class_linker = runtime->GetClassLinker();
328
329  // Make a list of classes we would like to prune.
330  std::set<std::string> non_image_classes;
331  NonImageClasses context;
332  context.image_writer = this;
333  context.non_image_classes = &non_image_classes;
334  class_linker->VisitClasses(NonImageClassesVisitor, &context);
335
336  // Remove the undesired classes from the class roots.
337  for (const std::string& it : non_image_classes) {
338    bool result = class_linker->RemoveClass(it.c_str(), NULL);
339    DCHECK(result);
340  }
341
342  // Clear references to removed classes from the DexCaches.
343  ArtMethod* resolution_method = runtime->GetResolutionMethod();
344  ReaderMutexLock mu(Thread::Current(), *class_linker->DexLock());
345  size_t dex_cache_count = class_linker->GetDexCacheCount();
346  for (size_t idx = 0; idx < dex_cache_count; ++idx) {
347    DexCache* dex_cache = class_linker->GetDexCache(idx);
348    for (size_t i = 0; i < dex_cache->NumResolvedTypes(); i++) {
349      Class* klass = dex_cache->GetResolvedType(i);
350      if (klass != NULL && !IsImageClass(klass)) {
351        dex_cache->SetResolvedType(i, NULL);
352      }
353    }
354    for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) {
355      ArtMethod* method = dex_cache->GetResolvedMethod(i);
356      if (method != NULL && !IsImageClass(method->GetDeclaringClass())) {
357        dex_cache->SetResolvedMethod(i, resolution_method);
358      }
359    }
360    for (size_t i = 0; i < dex_cache->NumResolvedFields(); i++) {
361      ArtField* field = dex_cache->GetResolvedField(i);
362      if (field != NULL && !IsImageClass(field->GetDeclaringClass())) {
363        dex_cache->SetResolvedField(i, NULL);
364      }
365    }
366  }
367}
368
369bool ImageWriter::NonImageClassesVisitor(Class* klass, void* arg) {
370  NonImageClasses* context = reinterpret_cast<NonImageClasses*>(arg);
371  if (!context->image_writer->IsImageClass(klass)) {
372    std::string temp;
373    context->non_image_classes->insert(klass->GetDescriptor(&temp));
374  }
375  return true;
376}
377
378void ImageWriter::CheckNonImageClassesRemoved()
379    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
380  if (compiler_driver_.GetImageClasses() != nullptr) {
381    gc::Heap* heap = Runtime::Current()->GetHeap();
382    ReaderMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_);
383    heap->VisitObjects(CheckNonImageClassesRemovedCallback, this);
384  }
385}
386
387void ImageWriter::CheckNonImageClassesRemovedCallback(Object* obj, void* arg) {
388  ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg);
389  if (obj->IsClass()) {
390    Class* klass = obj->AsClass();
391    if (!image_writer->IsImageClass(klass)) {
392      image_writer->DumpImageClasses();
393      std::string temp;
394      CHECK(image_writer->IsImageClass(klass)) << klass->GetDescriptor(&temp)
395                                               << " " << PrettyDescriptor(klass);
396    }
397  }
398}
399
400void ImageWriter::DumpImageClasses() {
401  const std::set<std::string>* image_classes = compiler_driver_.GetImageClasses();
402  CHECK(image_classes != NULL);
403  for (const std::string& image_class : *image_classes) {
404    LOG(INFO) << " " << image_class;
405  }
406}
407
408void ImageWriter::CalculateObjectOffsets(Object* obj) {
409  DCHECK(obj != NULL);
410  // if it is a string, we want to intern it if its not interned.
411  if (obj->GetClass()->IsStringClass()) {
412    // we must be an interned string that was forward referenced and already assigned
413    if (IsImageOffsetAssigned(obj)) {
414      DCHECK_EQ(obj, obj->AsString()->Intern());
415      return;
416    }
417    mirror::String* const interned = obj->AsString()->Intern();
418    if (obj != interned) {
419      if (!IsImageOffsetAssigned(interned)) {
420        // interned obj is after us, allocate its location early
421        AssignImageOffset(interned);
422      }
423      // point those looking for this object to the interned version.
424      SetImageOffset(obj, GetImageOffset(interned));
425      return;
426    }
427    // else (obj == interned), nothing to do but fall through to the normal case
428  }
429
430  AssignImageOffset(obj);
431}
432
433ObjectArray<Object>* ImageWriter::CreateImageRoots() const {
434  Runtime* runtime = Runtime::Current();
435  ClassLinker* class_linker = runtime->GetClassLinker();
436  Thread* self = Thread::Current();
437  StackHandleScope<3> hs(self);
438  Handle<Class> object_array_class(hs.NewHandle(
439      class_linker->FindSystemClass(self, "[Ljava/lang/Object;")));
440
441  // build an Object[] of all the DexCaches used in the source_space_.
442  // Since we can't hold the dex lock when allocating the dex_caches
443  // ObjectArray, we lock the dex lock twice, first to get the number
444  // of dex caches first and then lock it again to copy the dex
445  // caches. We check that the number of dex caches does not change.
446  size_t dex_cache_count;
447  {
448    ReaderMutexLock mu(Thread::Current(), *class_linker->DexLock());
449    dex_cache_count = class_linker->GetDexCacheCount();
450  }
451  Handle<ObjectArray<Object>> dex_caches(
452      hs.NewHandle(ObjectArray<Object>::Alloc(self, object_array_class.Get(),
453                                              dex_cache_count)));
454  CHECK(dex_caches.Get() != nullptr) << "Failed to allocate a dex cache array.";
455  {
456    ReaderMutexLock mu(Thread::Current(), *class_linker->DexLock());
457    CHECK_EQ(dex_cache_count, class_linker->GetDexCacheCount())
458        << "The number of dex caches changed.";
459    for (size_t i = 0; i < dex_cache_count; ++i) {
460      dex_caches->Set<false>(i, class_linker->GetDexCache(i));
461    }
462  }
463
464  // build an Object[] of the roots needed to restore the runtime
465  Handle<ObjectArray<Object>> image_roots(hs.NewHandle(
466      ObjectArray<Object>::Alloc(self, object_array_class.Get(), ImageHeader::kImageRootsMax)));
467  image_roots->Set<false>(ImageHeader::kResolutionMethod, runtime->GetResolutionMethod());
468  image_roots->Set<false>(ImageHeader::kImtConflictMethod, runtime->GetImtConflictMethod());
469  image_roots->Set<false>(ImageHeader::kImtUnimplementedMethod,
470                          runtime->GetImtUnimplementedMethod());
471  image_roots->Set<false>(ImageHeader::kDefaultImt, runtime->GetDefaultImt());
472  image_roots->Set<false>(ImageHeader::kCalleeSaveMethod,
473                          runtime->GetCalleeSaveMethod(Runtime::kSaveAll));
474  image_roots->Set<false>(ImageHeader::kRefsOnlySaveMethod,
475                          runtime->GetCalleeSaveMethod(Runtime::kRefsOnly));
476  image_roots->Set<false>(ImageHeader::kRefsAndArgsSaveMethod,
477                          runtime->GetCalleeSaveMethod(Runtime::kRefsAndArgs));
478  image_roots->Set<false>(ImageHeader::kDexCaches, dex_caches.Get());
479  image_roots->Set<false>(ImageHeader::kClassRoots, class_linker->GetClassRoots());
480  for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
481    CHECK(image_roots->Get(i) != NULL);
482  }
483  return image_roots.Get();
484}
485
486// Walk instance fields of the given Class. Separate function to allow recursion on the super
487// class.
488void ImageWriter::WalkInstanceFields(mirror::Object* obj, mirror::Class* klass) {
489  // Visit fields of parent classes first.
490  StackHandleScope<1> hs(Thread::Current());
491  Handle<mirror::Class> h_class(hs.NewHandle(klass));
492  mirror::Class* super = h_class->GetSuperClass();
493  if (super != nullptr) {
494    WalkInstanceFields(obj, super);
495  }
496  //
497  size_t num_reference_fields = h_class->NumReferenceInstanceFields();
498  MemberOffset field_offset = h_class->GetFirstReferenceInstanceFieldOffset();
499  for (size_t i = 0; i < num_reference_fields; ++i) {
500    mirror::Object* value = obj->GetFieldObject<mirror::Object>(field_offset);
501    if (value != nullptr) {
502      WalkFieldsInOrder(value);
503    }
504    field_offset = MemberOffset(field_offset.Uint32Value() +
505                                sizeof(mirror::HeapReference<mirror::Object>));
506  }
507}
508
509// For an unvisited object, visit it then all its children found via fields.
510void ImageWriter::WalkFieldsInOrder(mirror::Object* obj) {
511  if (!IsImageOffsetAssigned(obj)) {
512    // Walk instance fields of all objects
513    StackHandleScope<2> hs(Thread::Current());
514    Handle<mirror::Object> h_obj(hs.NewHandle(obj));
515    Handle<mirror::Class> klass(hs.NewHandle(obj->GetClass()));
516    // visit the object itself.
517    CalculateObjectOffsets(h_obj.Get());
518    WalkInstanceFields(h_obj.Get(), klass.Get());
519    // Walk static fields of a Class.
520    if (h_obj->IsClass()) {
521      size_t num_static_fields = klass->NumReferenceStaticFields();
522      MemberOffset field_offset = klass->GetFirstReferenceStaticFieldOffset();
523      for (size_t i = 0; i < num_static_fields; ++i) {
524        mirror::Object* value = h_obj->GetFieldObject<mirror::Object>(field_offset);
525        if (value != nullptr) {
526          WalkFieldsInOrder(value);
527        }
528        field_offset = MemberOffset(field_offset.Uint32Value() +
529                                    sizeof(mirror::HeapReference<mirror::Object>));
530      }
531    } else if (h_obj->IsObjectArray()) {
532      // Walk elements of an object array.
533      int32_t length = h_obj->AsObjectArray<mirror::Object>()->GetLength();
534      for (int32_t i = 0; i < length; i++) {
535        mirror::ObjectArray<mirror::Object>* obj_array = h_obj->AsObjectArray<mirror::Object>();
536        mirror::Object* value = obj_array->Get(i);
537        if (value != nullptr) {
538          WalkFieldsInOrder(value);
539        }
540      }
541    }
542  }
543}
544
545void ImageWriter::WalkFieldsCallback(mirror::Object* obj, void* arg) {
546  ImageWriter* writer = reinterpret_cast<ImageWriter*>(arg);
547  DCHECK(writer != nullptr);
548  writer->WalkFieldsInOrder(obj);
549}
550
551void ImageWriter::CalculateNewObjectOffsets(size_t oat_loaded_size, size_t oat_data_offset) {
552  CHECK_NE(0U, oat_loaded_size);
553  Thread* self = Thread::Current();
554  StackHandleScope<1> hs(self);
555  Handle<ObjectArray<Object>> image_roots(hs.NewHandle(CreateImageRoots()));
556
557  gc::Heap* heap = Runtime::Current()->GetHeap();
558  DCHECK_EQ(0U, image_end_);
559
560  // Leave space for the header, but do not write it yet, we need to
561  // know where image_roots is going to end up
562  image_end_ += RoundUp(sizeof(ImageHeader), 8);  // 64-bit-alignment
563
564  {
565    WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
566    // TODO: Image spaces only?
567    const char* old = self->StartAssertNoThreadSuspension("ImageWriter");
568    DCHECK_LT(image_end_, image_->Size());
569    // Clear any pre-existing monitors which may have been in the monitor words.
570    heap->VisitObjects(WalkFieldsCallback, this);
571    self->EndAssertNoThreadSuspension(old);
572  }
573
574  const byte* oat_file_begin = image_begin_ + RoundUp(image_end_, kPageSize);
575  const byte* oat_file_end = oat_file_begin + oat_loaded_size;
576  oat_data_begin_ = oat_file_begin + oat_data_offset;
577  const byte* oat_data_end = oat_data_begin_ + oat_file_->Size();
578
579  // Return to write header at start of image with future location of image_roots. At this point,
580  // image_end_ is the size of the image (excluding bitmaps).
581  const size_t heap_bytes_per_bitmap_byte = kBitsPerByte * kObjectAlignment;
582  const size_t bitmap_bytes = RoundUp(image_end_, heap_bytes_per_bitmap_byte) /
583      heap_bytes_per_bitmap_byte;
584  ImageHeader image_header(PointerToLowMemUInt32(image_begin_),
585                           static_cast<uint32_t>(image_end_),
586                           RoundUp(image_end_, kPageSize),
587                           RoundUp(bitmap_bytes, kPageSize),
588                           PointerToLowMemUInt32(GetImageAddress(image_roots.Get())),
589                           oat_file_->GetOatHeader().GetChecksum(),
590                           PointerToLowMemUInt32(oat_file_begin),
591                           PointerToLowMemUInt32(oat_data_begin_),
592                           PointerToLowMemUInt32(oat_data_end),
593                           PointerToLowMemUInt32(oat_file_end),
594                           compile_pic_);
595  memcpy(image_->Begin(), &image_header, sizeof(image_header));
596
597  // Note that image_end_ is left at end of used space
598}
599
600void ImageWriter::CopyAndFixupObjects()
601    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
602  Thread* self = Thread::Current();
603  const char* old_cause = self->StartAssertNoThreadSuspension("ImageWriter");
604  gc::Heap* heap = Runtime::Current()->GetHeap();
605  // TODO: heap validation can't handle this fix up pass
606  heap->DisableObjectValidation();
607  // TODO: Image spaces only?
608  WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
609  heap->VisitObjects(CopyAndFixupObjectsCallback, this);
610  // Fix up the object previously had hash codes.
611  for (const std::pair<mirror::Object*, uint32_t>& hash_pair : saved_hashes_) {
612    hash_pair.first->SetLockWord(LockWord::FromHashCode(hash_pair.second), false);
613  }
614  saved_hashes_.clear();
615  self->EndAssertNoThreadSuspension(old_cause);
616}
617
618void ImageWriter::CopyAndFixupObjectsCallback(Object* obj, void* arg) {
619  DCHECK(obj != nullptr);
620  DCHECK(arg != nullptr);
621  ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg);
622  // see GetLocalAddress for similar computation
623  size_t offset = image_writer->GetImageOffset(obj);
624  byte* dst = image_writer->image_->Begin() + offset;
625  const byte* src = reinterpret_cast<const byte*>(obj);
626  size_t n = obj->SizeOf();
627  DCHECK_LT(offset + n, image_writer->image_->Size());
628  memcpy(dst, src, n);
629  Object* copy = reinterpret_cast<Object*>(dst);
630  // Write in a hash code of objects which have inflated monitors or a hash code in their monitor
631  // word.
632  copy->SetLockWord(LockWord(), false);
633  image_writer->FixupObject(obj, copy);
634}
635
636class FixupVisitor {
637 public:
638  FixupVisitor(ImageWriter* image_writer, Object* copy) : image_writer_(image_writer), copy_(copy) {
639  }
640
641  void operator()(Object* obj, MemberOffset offset, bool /*is_static*/) const
642      EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_) {
643    Object* ref = obj->GetFieldObject<Object, kVerifyNone>(offset);
644    // Use SetFieldObjectWithoutWriteBarrier to avoid card marking since we are writing to the
645    // image.
646    copy_->SetFieldObjectWithoutWriteBarrier<false, true, kVerifyNone>(
647        offset, image_writer_->GetImageAddress(ref));
648  }
649
650  // java.lang.ref.Reference visitor.
651  void operator()(mirror::Class* /*klass*/, mirror::Reference* ref) const
652      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
653      EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
654    copy_->SetFieldObjectWithoutWriteBarrier<false, true, kVerifyNone>(
655        mirror::Reference::ReferentOffset(), image_writer_->GetImageAddress(ref->GetReferent()));
656  }
657
658 protected:
659  ImageWriter* const image_writer_;
660  mirror::Object* const copy_;
661};
662
663class FixupClassVisitor FINAL : public FixupVisitor {
664 public:
665  FixupClassVisitor(ImageWriter* image_writer, Object* copy) : FixupVisitor(image_writer, copy) {
666  }
667
668  void operator()(Object* obj, MemberOffset offset, bool /*is_static*/) const
669      EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_) {
670    DCHECK(obj->IsClass());
671    FixupVisitor::operator()(obj, offset, false);
672
673    if (offset.Uint32Value() < mirror::Class::EmbeddedVTableOffset().Uint32Value()) {
674      return;
675    }
676  }
677
678  void operator()(mirror::Class* /*klass*/, mirror::Reference* ref) const
679      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
680      EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
681    LOG(FATAL) << "Reference not expected here.";
682  }
683};
684
685void ImageWriter::FixupObject(Object* orig, Object* copy) {
686  DCHECK(orig != nullptr);
687  DCHECK(copy != nullptr);
688  if (kUseBakerOrBrooksReadBarrier) {
689    orig->AssertReadBarrierPointer();
690    if (kUseBrooksReadBarrier) {
691      // Note the address 'copy' isn't the same as the image address of 'orig'.
692      copy->SetReadBarrierPointer(GetImageAddress(orig));
693      DCHECK_EQ(copy->GetReadBarrierPointer(), GetImageAddress(orig));
694    }
695  }
696  if (orig->IsClass() && orig->AsClass()->ShouldHaveEmbeddedImtAndVTable()) {
697    FixupClassVisitor visitor(this, copy);
698    orig->VisitReferences<true /*visit class*/>(visitor, visitor);
699  } else {
700    FixupVisitor visitor(this, copy);
701    orig->VisitReferences<true /*visit class*/>(visitor, visitor);
702  }
703  if (orig->IsArtMethod<kVerifyNone>()) {
704    FixupMethod(orig->AsArtMethod<kVerifyNone>(), down_cast<ArtMethod*>(copy));
705  }
706}
707
708const byte* ImageWriter::GetQuickCode(mirror::ArtMethod* method, bool* quick_is_interpreted) {
709  DCHECK(!method->IsResolutionMethod() && !method->IsImtConflictMethod() &&
710         !method->IsImtUnimplementedMethod() && !method->IsAbstract()) << PrettyMethod(method);
711
712  // Use original code if it exists. Otherwise, set the code pointer to the resolution
713  // trampoline.
714
715  // Quick entrypoint:
716  const byte* quick_code = GetOatAddress(method->GetQuickOatCodeOffset());
717  *quick_is_interpreted = false;
718  if (quick_code != nullptr &&
719      (!method->IsStatic() || method->IsConstructor() || method->GetDeclaringClass()->IsInitialized())) {
720    // We have code for a non-static or initialized method, just use the code.
721  } else if (quick_code == nullptr && method->IsNative() &&
722      (!method->IsStatic() || method->GetDeclaringClass()->IsInitialized())) {
723    // Non-static or initialized native method missing compiled code, use generic JNI version.
724    quick_code = GetOatAddress(quick_generic_jni_trampoline_offset_);
725  } else if (quick_code == nullptr && !method->IsNative()) {
726    // We don't have code at all for a non-native method, use the interpreter.
727    quick_code = GetOatAddress(quick_to_interpreter_bridge_offset_);
728    *quick_is_interpreted = true;
729  } else {
730    CHECK(!method->GetDeclaringClass()->IsInitialized());
731    // We have code for a static method, but need to go through the resolution stub for class
732    // initialization.
733    quick_code = GetOatAddress(quick_resolution_trampoline_offset_);
734  }
735  return quick_code;
736}
737
738const byte* ImageWriter::GetQuickEntryPoint(mirror::ArtMethod* method) {
739  // Calculate the quick entry point following the same logic as FixupMethod() below.
740  // The resolution method has a special trampoline to call.
741  Runtime* runtime = Runtime::Current();
742  if (UNLIKELY(method == runtime->GetResolutionMethod())) {
743    return GetOatAddress(quick_resolution_trampoline_offset_);
744  } else if (UNLIKELY(method == runtime->GetImtConflictMethod() ||
745                      method == runtime->GetImtUnimplementedMethod())) {
746    return GetOatAddress(quick_imt_conflict_trampoline_offset_);
747  } else {
748    // We assume all methods have code. If they don't currently then we set them to the use the
749    // resolution trampoline. Abstract methods never have code and so we need to make sure their
750    // use results in an AbstractMethodError. We use the interpreter to achieve this.
751    if (UNLIKELY(method->IsAbstract())) {
752      return GetOatAddress(quick_to_interpreter_bridge_offset_);
753    } else {
754      bool quick_is_interpreted;
755      return GetQuickCode(method, &quick_is_interpreted);
756    }
757  }
758}
759
760void ImageWriter::FixupMethod(ArtMethod* orig, ArtMethod* copy) {
761  // OatWriter replaces the code_ with an offset value. Here we re-adjust to a pointer relative to
762  // oat_begin_
763
764  // The resolution method has a special trampoline to call.
765  Runtime* runtime = Runtime::Current();
766  if (UNLIKELY(orig == runtime->GetResolutionMethod())) {
767#if defined(ART_USE_PORTABLE_COMPILER)
768    copy->SetEntryPointFromPortableCompiledCode<kVerifyNone>(GetOatAddress(portable_resolution_trampoline_offset_));
769#endif
770    copy->SetEntryPointFromQuickCompiledCode<kVerifyNone>(GetOatAddress(quick_resolution_trampoline_offset_));
771  } else if (UNLIKELY(orig == runtime->GetImtConflictMethod() ||
772                      orig == runtime->GetImtUnimplementedMethod())) {
773#if defined(ART_USE_PORTABLE_COMPILER)
774    copy->SetEntryPointFromPortableCompiledCode<kVerifyNone>(GetOatAddress(portable_imt_conflict_trampoline_offset_));
775#endif
776    copy->SetEntryPointFromQuickCompiledCode<kVerifyNone>(GetOatAddress(quick_imt_conflict_trampoline_offset_));
777  } else {
778    // We assume all methods have code. If they don't currently then we set them to the use the
779    // resolution trampoline. Abstract methods never have code and so we need to make sure their
780    // use results in an AbstractMethodError. We use the interpreter to achieve this.
781    if (UNLIKELY(orig->IsAbstract())) {
782#if defined(ART_USE_PORTABLE_COMPILER)
783      copy->SetEntryPointFromPortableCompiledCode<kVerifyNone>(GetOatAddress(portable_to_interpreter_bridge_offset_));
784#endif
785      copy->SetEntryPointFromQuickCompiledCode<kVerifyNone>(GetOatAddress(quick_to_interpreter_bridge_offset_));
786      copy->SetEntryPointFromInterpreter<kVerifyNone>(reinterpret_cast<EntryPointFromInterpreter*>
787          (const_cast<byte*>(GetOatAddress(interpreter_to_interpreter_bridge_offset_))));
788    } else {
789      bool quick_is_interpreted;
790      const byte* quick_code = GetQuickCode(orig, &quick_is_interpreted);
791      copy->SetEntryPointFromQuickCompiledCode<kVerifyNone>(quick_code);
792
793      // Portable entrypoint:
794      bool portable_is_interpreted = false;
795#if defined(ART_USE_PORTABLE_COMPILER)
796      const byte* portable_code = GetOatAddress(orig->GetPortableOatCodeOffset());
797      if (portable_code != nullptr &&
798          (!orig->IsStatic() || orig->IsConstructor() || orig->GetDeclaringClass()->IsInitialized())) {
799        // We have code for a non-static or initialized method, just use the code.
800      } else if (portable_code == nullptr && orig->IsNative() &&
801          (!orig->IsStatic() || orig->GetDeclaringClass()->IsInitialized())) {
802        // Non-static or initialized native method missing compiled code, use generic JNI version.
803        // TODO: generic JNI support for LLVM.
804        portable_code = GetOatAddress(portable_resolution_trampoline_offset_);
805      } else if (portable_code == nullptr && !orig->IsNative()) {
806        // We don't have code at all for a non-native method, use the interpreter.
807        portable_code = GetOatAddress(portable_to_interpreter_bridge_offset_);
808        portable_is_interpreted = true;
809      } else {
810        CHECK(!orig->GetDeclaringClass()->IsInitialized());
811        // We have code for a static method, but need to go through the resolution stub for class
812        // initialization.
813        portable_code = GetOatAddress(portable_resolution_trampoline_offset_);
814      }
815      copy->SetEntryPointFromPortableCompiledCode<kVerifyNone>(portable_code);
816#endif
817      // JNI entrypoint:
818      if (orig->IsNative()) {
819        // The native method's pointer is set to a stub to lookup via dlsym.
820        // Note this is not the code_ pointer, that is handled above.
821        copy->SetNativeMethod<kVerifyNone>(GetOatAddress(jni_dlsym_lookup_offset_));
822      } else {
823        // Normal (non-abstract non-native) methods have various tables to relocate.
824        uint32_t native_gc_map_offset = orig->GetOatNativeGcMapOffset();
825        const byte* native_gc_map = GetOatAddress(native_gc_map_offset);
826        copy->SetNativeGcMap<kVerifyNone>(reinterpret_cast<const uint8_t*>(native_gc_map));
827      }
828
829      // Interpreter entrypoint:
830      // Set the interpreter entrypoint depending on whether there is compiled code or not.
831      uint32_t interpreter_code = (quick_is_interpreted && portable_is_interpreted)
832          ? interpreter_to_interpreter_bridge_offset_
833          : interpreter_to_compiled_code_bridge_offset_;
834      copy->SetEntryPointFromInterpreter<kVerifyNone>(
835          reinterpret_cast<EntryPointFromInterpreter*>(
836              const_cast<byte*>(GetOatAddress(interpreter_code))));
837    }
838  }
839}
840
841static OatHeader* GetOatHeaderFromElf(ElfFile* elf) {
842  Elf32_Shdr* data_sec = elf->FindSectionByName(".rodata");
843  if (data_sec == nullptr) {
844    return nullptr;
845  }
846  return reinterpret_cast<OatHeader*>(elf->Begin() + data_sec->sh_offset);
847}
848
849void ImageWriter::PatchOatCodeAndMethods(File* elf_file) {
850  std::string error_msg;
851  std::unique_ptr<ElfFile> elf(ElfFile::Open(elf_file, PROT_READ|PROT_WRITE,
852                                             MAP_SHARED, &error_msg));
853  if (elf.get() == nullptr) {
854    LOG(FATAL) << "Unable patch oat file: " << error_msg;
855    return;
856  }
857  if (!ElfPatcher::Patch(&compiler_driver_, elf.get(), oat_file_,
858                         reinterpret_cast<uintptr_t>(oat_data_begin_),
859                         GetImageAddressCallback, reinterpret_cast<void*>(this),
860                         &error_msg)) {
861    LOG(FATAL) << "unable to patch oat file: " << error_msg;
862    return;
863  }
864  OatHeader* oat_header = GetOatHeaderFromElf(elf.get());
865  CHECK(oat_header != nullptr);
866  CHECK(oat_header->IsValid());
867
868  ImageHeader* image_header = reinterpret_cast<ImageHeader*>(image_->Begin());
869  image_header->SetOatChecksum(oat_header->GetChecksum());
870}
871
872}  // namespace art
873