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