image_writer.cc revision 4e6a31eb97f22f4480827474b30b9e64f396eace
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 <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"
32#include "gc/accounting/space_bitmap-inl.h"
33#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"
39#include "lock_word.h"
40#include "mirror/art_field-inl.h"
41#include "mirror/art_method-inl.h"
42#include "mirror/array-inl.h"
43#include "mirror/class-inl.h"
44#include "mirror/class_loader.h"
45#include "mirror/dex_cache-inl.h"
46#include "mirror/object-inl.h"
47#include "mirror/object_array-inl.h"
48#include "oat.h"
49#include "oat_file.h"
50#include "object_utils.h"
51#include "runtime.h"
52#include "scoped_thread_state_change.h"
53#include "sirt_ref.h"
54#include "UniquePtr.h"
55#include "utils.h"
56
57using ::art::mirror::ArtField;
58using ::art::mirror::ArtMethod;
59using ::art::mirror::Class;
60using ::art::mirror::DexCache;
61using ::art::mirror::EntryPointFromInterpreter;
62using ::art::mirror::Object;
63using ::art::mirror::ObjectArray;
64using ::art::mirror::String;
65
66namespace art {
67
68bool ImageWriter::Write(const std::string& image_filename,
69                        uintptr_t image_begin,
70                        const std::string& oat_filename,
71                        const std::string& oat_location) {
72  CHECK(!image_filename.empty());
73
74  CHECK_NE(image_begin, 0U);
75  image_begin_ = reinterpret_cast<byte*>(image_begin);
76
77  ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
78  const std::vector<DexCache*>& all_dex_caches = class_linker->GetDexCaches();
79  dex_caches_.insert(all_dex_caches.begin(), all_dex_caches.end());
80
81  UniquePtr<File> oat_file(OS::OpenFileReadWrite(oat_filename.c_str()));
82  if (oat_file.get() == NULL) {
83    LOG(ERROR) << "Failed to open oat file " << oat_filename << " for " << oat_location;
84    return false;
85  }
86  std::string error_msg;
87  oat_file_ = OatFile::OpenWritable(oat_file.get(), oat_location, &error_msg);
88  if (oat_file_ == nullptr) {
89    LOG(ERROR) << "Failed to open writable oat file " << oat_filename << " for " << oat_location
90        << ": " << error_msg;
91    return false;
92  }
93  CHECK_EQ(class_linker->RegisterOatFile(oat_file_), oat_file_);
94
95  interpreter_to_interpreter_bridge_offset_ =
96      oat_file_->GetOatHeader().GetInterpreterToInterpreterBridgeOffset();
97  interpreter_to_compiled_code_bridge_offset_ =
98      oat_file_->GetOatHeader().GetInterpreterToCompiledCodeBridgeOffset();
99
100  jni_dlsym_lookup_offset_ = oat_file_->GetOatHeader().GetJniDlsymLookupOffset();
101
102  portable_imt_conflict_trampoline_offset_ =
103      oat_file_->GetOatHeader().GetPortableImtConflictTrampolineOffset();
104  portable_resolution_trampoline_offset_ =
105      oat_file_->GetOatHeader().GetPortableResolutionTrampolineOffset();
106  portable_to_interpreter_bridge_offset_ =
107      oat_file_->GetOatHeader().GetPortableToInterpreterBridgeOffset();
108
109  quick_imt_conflict_trampoline_offset_ =
110      oat_file_->GetOatHeader().GetQuickImtConflictTrampolineOffset();
111  quick_resolution_trampoline_offset_ =
112      oat_file_->GetOatHeader().GetQuickResolutionTrampolineOffset();
113  quick_to_interpreter_bridge_offset_ =
114      oat_file_->GetOatHeader().GetQuickToInterpreterBridgeOffset();
115  {
116    Thread::Current()->TransitionFromSuspendedToRunnable();
117    PruneNonImageClasses();  // Remove junk
118    ComputeLazyFieldsForImageClasses();  // Add useful information
119    ComputeEagerResolvedStrings();
120    Thread::Current()->TransitionFromRunnableToSuspended(kNative);
121  }
122  gc::Heap* heap = Runtime::Current()->GetHeap();
123  heap->CollectGarbage(false);  // Remove garbage.
124  // Trim size of alloc spaces.
125  for (const auto& space : heap->GetContinuousSpaces()) {
126    if (space->IsDlMallocSpace()) {
127      space->AsDlMallocSpace()->Trim();
128    }
129  }
130
131  if (!AllocMemory()) {
132    return false;
133  }
134#ifndef NDEBUG
135  {  // NOLINT(whitespace/braces)
136    ScopedObjectAccess soa(Thread::Current());
137    CheckNonImageClassesRemoved();
138  }
139#endif
140  Thread::Current()->TransitionFromSuspendedToRunnable();
141  size_t oat_loaded_size = 0;
142  size_t oat_data_offset = 0;
143  ElfWriter::GetOatElfInformation(oat_file.get(), oat_loaded_size, oat_data_offset);
144  CalculateNewObjectOffsets(oat_loaded_size, oat_data_offset);
145  CopyAndFixupObjects();
146  PatchOatCodeAndMethods();
147  // Record allocations into the image bitmap.
148  RecordImageAllocations();
149  Thread::Current()->TransitionFromRunnableToSuspended(kNative);
150
151  UniquePtr<File> image_file(OS::CreateEmptyFile(image_filename.c_str()));
152  ImageHeader* image_header = reinterpret_cast<ImageHeader*>(image_->Begin());
153  if (image_file.get() == NULL) {
154    LOG(ERROR) << "Failed to open image file " << image_filename;
155    return false;
156  }
157  if (fchmod(image_file->Fd(), 0644) != 0) {
158    PLOG(ERROR) << "Failed to make image file world readable: " << image_filename;
159    return EXIT_FAILURE;
160  }
161
162  // Write out the image.
163  CHECK_EQ(image_end_, image_header->GetImageSize());
164  if (!image_file->WriteFully(image_->Begin(), image_end_)) {
165    PLOG(ERROR) << "Failed to write image file " << image_filename;
166    return false;
167  }
168
169  // Write out the image bitmap at the page aligned start of the image end.
170  CHECK_ALIGNED(image_header->GetImageBitmapOffset(), kPageSize);
171  if (!image_file->Write(reinterpret_cast<char*>(image_bitmap_->Begin()),
172                         image_header->GetImageBitmapSize(),
173                         image_header->GetImageBitmapOffset())) {
174    PLOG(ERROR) << "Failed to write image file " << image_filename;
175    return false;
176  }
177
178  return true;
179}
180
181void ImageWriter::RecordImageAllocations() {
182  uint64_t start_time = NanoTime();
183  CHECK(image_bitmap_.get() != nullptr);
184  for (const auto& it : offsets_) {
185    mirror::Object* obj = reinterpret_cast<mirror::Object*>(image_->Begin() + it.second);
186    DCHECK_ALIGNED(obj, kObjectAlignment);
187    image_bitmap_->Set(obj);
188  }
189  LOG(INFO) << "RecordImageAllocations took " << PrettyDuration(NanoTime() - start_time);
190}
191
192bool ImageWriter::AllocMemory() {
193  size_t size = 0;
194  for (const auto& space : Runtime::Current()->GetHeap()->GetContinuousSpaces()) {
195    if (space->IsDlMallocSpace()) {
196      size += space->Size();
197    }
198  }
199
200  int prot = PROT_READ | PROT_WRITE;
201  size_t length = RoundUp(size, kPageSize);
202  std::string error_msg;
203  image_.reset(MemMap::MapAnonymous("image writer image", NULL, length, prot, &error_msg));
204  if (UNLIKELY(image_.get() == nullptr)) {
205    LOG(ERROR) << "Failed to allocate memory for image file generation: " << error_msg;
206    return false;
207  }
208  return true;
209}
210
211void ImageWriter::ComputeLazyFieldsForImageClasses() {
212  Runtime* runtime = Runtime::Current();
213  ClassLinker* class_linker = runtime->GetClassLinker();
214  class_linker->VisitClassesWithoutClassesLock(ComputeLazyFieldsForClassesVisitor, NULL);
215}
216
217bool ImageWriter::ComputeLazyFieldsForClassesVisitor(Class* c, void* /*arg*/) {
218  c->ComputeName();
219  return true;
220}
221
222void ImageWriter::ComputeEagerResolvedStringsCallback(Object* obj, void* arg) {
223  if (!obj->GetClass()->IsStringClass()) {
224    return;
225  }
226  String* string = obj->AsString();
227  const uint16_t* utf16_string = string->GetCharArray()->GetData() + string->GetOffset();
228  ImageWriter* writer = reinterpret_cast<ImageWriter*>(arg);
229  for (DexCache* dex_cache : writer->dex_caches_) {
230    const DexFile& dex_file = *dex_cache->GetDexFile();
231    const DexFile::StringId* string_id = dex_file.FindStringId(utf16_string);
232    if (string_id != NULL) {
233      // This string occurs in this dex file, assign the dex cache entry.
234      uint32_t string_idx = dex_file.GetIndexForStringId(*string_id);
235      if (dex_cache->GetResolvedString(string_idx) == NULL) {
236        dex_cache->SetResolvedString(string_idx, string);
237      }
238    }
239  }
240}
241
242void ImageWriter::ComputeEagerResolvedStrings()
243    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
244  // TODO: Check image spaces only?
245  gc::Heap* heap = Runtime::Current()->GetHeap();
246  WriterMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_);
247  heap->FlushAllocStack();
248  heap->GetLiveBitmap()->Walk(ComputeEagerResolvedStringsCallback, this);
249}
250
251bool ImageWriter::IsImageClass(const Class* klass) {
252  return compiler_driver_.IsImageClass(ClassHelper(klass).GetDescriptor());
253}
254
255struct NonImageClasses {
256  ImageWriter* image_writer;
257  std::set<std::string>* non_image_classes;
258};
259
260void ImageWriter::PruneNonImageClasses() {
261  if (compiler_driver_.GetImageClasses() == NULL) {
262    return;
263  }
264  Runtime* runtime = Runtime::Current();
265  ClassLinker* class_linker = runtime->GetClassLinker();
266
267  // Make a list of classes we would like to prune.
268  std::set<std::string> non_image_classes;
269  NonImageClasses context;
270  context.image_writer = this;
271  context.non_image_classes = &non_image_classes;
272  class_linker->VisitClasses(NonImageClassesVisitor, &context);
273
274  // Remove the undesired classes from the class roots.
275  for (const std::string& it : non_image_classes) {
276    class_linker->RemoveClass(it.c_str(), NULL);
277  }
278
279  // Clear references to removed classes from the DexCaches.
280  ArtMethod* resolution_method = runtime->GetResolutionMethod();
281  for (DexCache* dex_cache : dex_caches_) {
282    for (size_t i = 0; i < dex_cache->NumResolvedTypes(); i++) {
283      Class* klass = dex_cache->GetResolvedType(i);
284      if (klass != NULL && !IsImageClass(klass)) {
285        dex_cache->SetResolvedType(i, NULL);
286        dex_cache->GetInitializedStaticStorage()->Set(i, NULL);
287      }
288    }
289    for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) {
290      ArtMethod* method = dex_cache->GetResolvedMethod(i);
291      if (method != NULL && !IsImageClass(method->GetDeclaringClass())) {
292        dex_cache->SetResolvedMethod(i, resolution_method);
293      }
294    }
295    for (size_t i = 0; i < dex_cache->NumResolvedFields(); i++) {
296      ArtField* field = dex_cache->GetResolvedField(i);
297      if (field != NULL && !IsImageClass(field->GetDeclaringClass())) {
298        dex_cache->SetResolvedField(i, NULL);
299      }
300    }
301  }
302}
303
304bool ImageWriter::NonImageClassesVisitor(Class* klass, void* arg) {
305  NonImageClasses* context = reinterpret_cast<NonImageClasses*>(arg);
306  if (!context->image_writer->IsImageClass(klass)) {
307    context->non_image_classes->insert(ClassHelper(klass).GetDescriptor());
308  }
309  return true;
310}
311
312void ImageWriter::CheckNonImageClassesRemoved()
313    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
314  if (compiler_driver_.GetImageClasses() == NULL) {
315    return;
316  }
317
318  gc::Heap* heap = Runtime::Current()->GetHeap();
319  Thread* self = Thread::Current();
320  {
321    WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
322    heap->FlushAllocStack();
323  }
324
325  ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
326  heap->GetLiveBitmap()->Walk(CheckNonImageClassesRemovedCallback, this);
327}
328
329void ImageWriter::CheckNonImageClassesRemovedCallback(Object* obj, void* arg) {
330  ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg);
331  if (!obj->IsClass()) {
332    return;
333  }
334  Class* klass = obj->AsClass();
335  if (!image_writer->IsImageClass(klass)) {
336    image_writer->DumpImageClasses();
337    CHECK(image_writer->IsImageClass(klass)) << ClassHelper(klass).GetDescriptor()
338                                             << " " << PrettyDescriptor(klass);
339  }
340}
341
342void ImageWriter::DumpImageClasses() {
343  CompilerDriver::DescriptorSet* image_classes = compiler_driver_.GetImageClasses();
344  CHECK(image_classes != NULL);
345  for (const std::string& image_class : *image_classes) {
346    LOG(INFO) << " " << image_class;
347  }
348}
349
350void ImageWriter::CalculateNewObjectOffsetsCallback(Object* obj, void* arg) {
351  DCHECK(obj != NULL);
352  DCHECK(arg != NULL);
353  ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg);
354
355  // if it is a string, we want to intern it if its not interned.
356  if (obj->GetClass()->IsStringClass()) {
357    // we must be an interned string that was forward referenced and already assigned
358    if (image_writer->IsImageOffsetAssigned(obj)) {
359      DCHECK_EQ(obj, obj->AsString()->Intern());
360      return;
361    }
362    SirtRef<String> interned(Thread::Current(), obj->AsString()->Intern());
363    if (obj != interned.get()) {
364      if (!image_writer->IsImageOffsetAssigned(interned.get())) {
365        // interned obj is after us, allocate its location early
366        image_writer->AssignImageOffset(interned.get());
367      }
368      // point those looking for this object to the interned version.
369      image_writer->SetImageOffset(obj, image_writer->GetImageOffset(interned.get()));
370      return;
371    }
372    // else (obj == interned), nothing to do but fall through to the normal case
373  }
374
375  image_writer->AssignImageOffset(obj);
376}
377
378ObjectArray<Object>* ImageWriter::CreateImageRoots() const {
379  Runtime* runtime = Runtime::Current();
380  ClassLinker* class_linker = runtime->GetClassLinker();
381  Class* object_array_class = class_linker->FindSystemClass("[Ljava/lang/Object;");
382  Thread* self = Thread::Current();
383
384  // build an Object[] of all the DexCaches used in the source_space_
385  ObjectArray<Object>* dex_caches = ObjectArray<Object>::Alloc(self, object_array_class,
386                                                               dex_caches_.size());
387  int i = 0;
388  for (DexCache* dex_cache : dex_caches_) {
389    dex_caches->Set(i++, dex_cache);
390  }
391
392  // build an Object[] of the roots needed to restore the runtime
393  SirtRef<ObjectArray<Object> >
394      image_roots(self,
395                  ObjectArray<Object>::Alloc(self, object_array_class,
396                                             ImageHeader::kImageRootsMax));
397  image_roots->Set(ImageHeader::kResolutionMethod, runtime->GetResolutionMethod());
398  image_roots->Set(ImageHeader::kImtConflictMethod, runtime->GetImtConflictMethod());
399  image_roots->Set(ImageHeader::kDefaultImt, runtime->GetDefaultImt());
400  image_roots->Set(ImageHeader::kCalleeSaveMethod,
401                   runtime->GetCalleeSaveMethod(Runtime::kSaveAll));
402  image_roots->Set(ImageHeader::kRefsOnlySaveMethod,
403                   runtime->GetCalleeSaveMethod(Runtime::kRefsOnly));
404  image_roots->Set(ImageHeader::kRefsAndArgsSaveMethod,
405                   runtime->GetCalleeSaveMethod(Runtime::kRefsAndArgs));
406  image_roots->Set(ImageHeader::kOatLocation,
407                   String::AllocFromModifiedUtf8(self, oat_file_->GetLocation().c_str()));
408  image_roots->Set(ImageHeader::kDexCaches,
409                   dex_caches);
410  image_roots->Set(ImageHeader::kClassRoots,
411                   class_linker->GetClassRoots());
412  for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
413    CHECK(image_roots->Get(i) != NULL);
414  }
415  return image_roots.get();
416}
417
418void ImageWriter::CalculateNewObjectOffsets(size_t oat_loaded_size, size_t oat_data_offset) {
419  CHECK_NE(0U, oat_loaded_size);
420  Thread* self = Thread::Current();
421  SirtRef<ObjectArray<Object> > image_roots(self, CreateImageRoots());
422
423  gc::Heap* heap = Runtime::Current()->GetHeap();
424  const auto& spaces = heap->GetContinuousSpaces();
425  DCHECK(!spaces.empty());
426  DCHECK_EQ(0U, image_end_);
427
428  // Leave space for the header, but do not write it yet, we need to
429  // know where image_roots is going to end up
430  image_end_ += RoundUp(sizeof(ImageHeader), 8);  // 64-bit-alignment
431
432  {
433    WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
434    heap->FlushAllocStack();
435    // TODO: Image spaces only?
436    // TODO: Add InOrderWalk to heap bitmap.
437    const char* old = self->StartAssertNoThreadSuspension("ImageWriter");
438    DCHECK(heap->GetLargeObjectsSpace()->GetLiveObjects()->IsEmpty());
439    for (const auto& space : spaces) {
440      space->GetLiveBitmap()->InOrderWalk(CalculateNewObjectOffsetsCallback, this);
441      DCHECK_LT(image_end_, image_->Size());
442    }
443    self->EndAssertNoThreadSuspension(old);
444  }
445
446  // Create the image bitmap.
447  image_bitmap_.reset(gc::accounting::SpaceBitmap::Create("image bitmap", image_->Begin(),
448                                                          image_end_));
449  const byte* oat_file_begin = image_begin_ + RoundUp(image_end_, kPageSize);
450  const byte* oat_file_end = oat_file_begin + oat_loaded_size;
451  oat_data_begin_ = oat_file_begin + oat_data_offset;
452  const byte* oat_data_end = oat_data_begin_ + oat_file_->Size();
453
454  // Return to write header at start of image with future location of image_roots. At this point,
455  // image_end_ is the size of the image (excluding bitmaps).
456  ImageHeader image_header(reinterpret_cast<uint32_t>(image_begin_),
457                           static_cast<uint32_t>(image_end_),
458                           RoundUp(image_end_, kPageSize),
459                           image_bitmap_->Size(),
460                           reinterpret_cast<uint32_t>(GetImageAddress(image_roots.get())),
461                           oat_file_->GetOatHeader().GetChecksum(),
462                           reinterpret_cast<uint32_t>(oat_file_begin),
463                           reinterpret_cast<uint32_t>(oat_data_begin_),
464                           reinterpret_cast<uint32_t>(oat_data_end),
465                           reinterpret_cast<uint32_t>(oat_file_end));
466  memcpy(image_->Begin(), &image_header, sizeof(image_header));
467
468  // Note that image_end_ is left at end of used space
469}
470
471void ImageWriter::CopyAndFixupObjects()
472    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
473  Thread* self = Thread::Current();
474  const char* old_cause = self->StartAssertNoThreadSuspension("ImageWriter");
475  gc::Heap* heap = Runtime::Current()->GetHeap();
476  // TODO: heap validation can't handle this fix up pass
477  heap->DisableObjectValidation();
478  // TODO: Image spaces only?
479  WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
480  heap->FlushAllocStack();
481  heap->GetLiveBitmap()->Walk(CopyAndFixupObjectsCallback, this);
482  self->EndAssertNoThreadSuspension(old_cause);
483}
484
485void ImageWriter::CopyAndFixupObjectsCallback(Object* object, void* arg) {
486  DCHECK(object != NULL);
487  DCHECK(arg != NULL);
488  const Object* obj = object;
489  ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg);
490
491  // see GetLocalAddress for similar computation
492  size_t offset = image_writer->GetImageOffset(obj);
493  byte* dst = image_writer->image_->Begin() + offset;
494  const byte* src = reinterpret_cast<const byte*>(obj);
495  size_t n = obj->SizeOf();
496  DCHECK_LT(offset + n, image_writer->image_->Size());
497  memcpy(dst, src, n);
498  Object* copy = reinterpret_cast<Object*>(dst);
499  // Write in a hash code of objects which have inflated monitors or a hash code in their monitor
500  // word.
501  LockWord lw(copy->GetLockWord());
502  switch (lw.GetState()) {
503    case LockWord::kFatLocked: {
504      Monitor* monitor = lw.FatLockMonitor();
505      CHECK(monitor != nullptr);
506      CHECK(!monitor->IsLocked());
507      if (monitor->HasHashCode()) {
508        copy->SetLockWord(LockWord::FromHashCode(monitor->GetHashCode()));
509      } else {
510        copy->SetLockWord(LockWord());
511      }
512      break;
513    }
514    case LockWord::kThinLocked: {
515      LOG(FATAL) << "Thin locked object " << obj << " found during object copy";
516      break;
517    }
518    case LockWord::kUnlocked:
519      break;
520    case LockWord::kHashCode:
521      // Do nothing since we can just keep the same hash code.
522      CHECK_NE(lw.GetHashCode(), 0);
523      break;
524    default:
525      LOG(FATAL) << "Unreachable.";
526      break;
527  }
528  image_writer->FixupObject(obj, copy);
529}
530
531void ImageWriter::FixupObject(const Object* orig, Object* copy) {
532  DCHECK(orig != NULL);
533  DCHECK(copy != NULL);
534  copy->SetClass(down_cast<Class*>(GetImageAddress(orig->GetClass())));
535  // TODO: special case init of pointers to malloc data (or removal of these pointers)
536  if (orig->IsClass()) {
537    FixupClass(orig->AsClass(), down_cast<Class*>(copy));
538  } else if (orig->IsObjectArray()) {
539    FixupObjectArray(orig->AsObjectArray<Object>(), down_cast<ObjectArray<Object>*>(copy));
540  } else if (orig->IsArtMethod()) {
541    FixupMethod(orig->AsArtMethod(), down_cast<ArtMethod*>(copy));
542  } else {
543    FixupInstanceFields(orig, copy);
544  }
545}
546
547void ImageWriter::FixupClass(const Class* orig, Class* copy) {
548  FixupInstanceFields(orig, copy);
549  FixupStaticFields(orig, copy);
550}
551
552void ImageWriter::FixupMethod(const ArtMethod* orig, ArtMethod* copy) {
553  FixupInstanceFields(orig, copy);
554
555  // OatWriter replaces the code_ with an offset value. Here we re-adjust to a pointer relative to
556  // oat_begin_
557
558  // The resolution method has a special trampoline to call.
559  if (UNLIKELY(orig == Runtime::Current()->GetResolutionMethod())) {
560#if defined(ART_USE_PORTABLE_COMPILER)
561    copy->SetEntryPointFromCompiledCode(GetOatAddress(portable_resolution_trampoline_offset_));
562#else
563    copy->SetEntryPointFromCompiledCode(GetOatAddress(quick_resolution_trampoline_offset_));
564#endif
565  } else if (UNLIKELY(orig == Runtime::Current()->GetImtConflictMethod())) {
566#if defined(ART_USE_PORTABLE_COMPILER)
567    copy->SetEntryPointFromCompiledCode(GetOatAddress(portable_imt_conflict_trampoline_offset_));
568#else
569    copy->SetEntryPointFromCompiledCode(GetOatAddress(quick_imt_conflict_trampoline_offset_));
570#endif
571  } else {
572    // We assume all methods have code. If they don't currently then we set them to the use the
573    // resolution trampoline. Abstract methods never have code and so we need to make sure their
574    // use results in an AbstractMethodError. We use the interpreter to achieve this.
575    if (UNLIKELY(orig->IsAbstract())) {
576#if defined(ART_USE_PORTABLE_COMPILER)
577      copy->SetEntryPointFromCompiledCode(GetOatAddress(portable_to_interpreter_bridge_offset_));
578#else
579      copy->SetEntryPointFromCompiledCode(GetOatAddress(quick_to_interpreter_bridge_offset_));
580#endif
581      copy->SetEntryPointFromInterpreter(reinterpret_cast<EntryPointFromInterpreter*>
582      (GetOatAddress(interpreter_to_interpreter_bridge_offset_)));
583    } else {
584      copy->SetEntryPointFromInterpreter(reinterpret_cast<EntryPointFromInterpreter*>
585      (GetOatAddress(interpreter_to_compiled_code_bridge_offset_)));
586      // Use original code if it exists. Otherwise, set the code pointer to the resolution
587      // trampoline.
588      const byte* code = GetOatAddress(orig->GetOatCodeOffset());
589      if (code != NULL) {
590        copy->SetEntryPointFromCompiledCode(code);
591      } else {
592#if defined(ART_USE_PORTABLE_COMPILER)
593        copy->SetEntryPointFromCompiledCode(GetOatAddress(portable_resolution_trampoline_offset_));
594#else
595        copy->SetEntryPointFromCompiledCode(GetOatAddress(quick_resolution_trampoline_offset_));
596#endif
597      }
598      if (orig->IsNative()) {
599        // The native method's pointer is set to a stub to lookup via dlsym.
600        // Note this is not the code_ pointer, that is handled above.
601        copy->SetNativeMethod(GetOatAddress(jni_dlsym_lookup_offset_));
602      } else {
603        // Normal (non-abstract non-native) methods have various tables to relocate.
604        uint32_t mapping_table_off = orig->GetOatMappingTableOffset();
605        const byte* mapping_table = GetOatAddress(mapping_table_off);
606        copy->SetMappingTable(mapping_table);
607
608        uint32_t vmap_table_offset = orig->GetOatVmapTableOffset();
609        const byte* vmap_table = GetOatAddress(vmap_table_offset);
610        copy->SetVmapTable(vmap_table);
611
612        uint32_t native_gc_map_offset = orig->GetOatNativeGcMapOffset();
613        const byte* native_gc_map = GetOatAddress(native_gc_map_offset);
614        copy->SetNativeGcMap(reinterpret_cast<const uint8_t*>(native_gc_map));
615      }
616    }
617  }
618}
619
620void ImageWriter::FixupObjectArray(const ObjectArray<Object>* orig, ObjectArray<Object>* copy) {
621  for (int32_t i = 0; i < orig->GetLength(); ++i) {
622    const Object* element = orig->Get(i);
623    copy->SetPtrWithoutChecks(i, GetImageAddress(element));
624  }
625}
626
627void ImageWriter::FixupInstanceFields(const Object* orig, Object* copy) {
628  DCHECK(orig != NULL);
629  DCHECK(copy != NULL);
630  Class* klass = orig->GetClass();
631  DCHECK(klass != NULL);
632  FixupFields(orig,
633              copy,
634              klass->GetReferenceInstanceOffsets(),
635              false);
636}
637
638void ImageWriter::FixupStaticFields(const Class* orig, Class* copy) {
639  DCHECK(orig != NULL);
640  DCHECK(copy != NULL);
641  FixupFields(orig,
642              copy,
643              orig->GetReferenceStaticOffsets(),
644              true);
645}
646
647void ImageWriter::FixupFields(const Object* orig,
648                              Object* copy,
649                              uint32_t ref_offsets,
650                              bool is_static) {
651  if (ref_offsets != CLASS_WALK_SUPER) {
652    // Found a reference offset bitmap.  Fixup the specified offsets.
653    while (ref_offsets != 0) {
654      size_t right_shift = CLZ(ref_offsets);
655      MemberOffset byte_offset = CLASS_OFFSET_FROM_CLZ(right_shift);
656      const Object* ref = orig->GetFieldObject<const Object*>(byte_offset, false);
657      // Use SetFieldPtr to avoid card marking since we are writing to the image.
658      copy->SetFieldPtr(byte_offset, GetImageAddress(ref), false);
659      ref_offsets &= ~(CLASS_HIGH_BIT >> right_shift);
660    }
661  } else {
662    // There is no reference offset bitmap.  In the non-static case,
663    // walk up the class inheritance hierarchy and find reference
664    // offsets the hard way. In the static case, just consider this
665    // class.
666    for (const Class *klass = is_static ? orig->AsClass() : orig->GetClass();
667         klass != NULL;
668         klass = is_static ? NULL : klass->GetSuperClass()) {
669      size_t num_reference_fields = (is_static
670                                     ? klass->NumReferenceStaticFields()
671                                     : klass->NumReferenceInstanceFields());
672      for (size_t i = 0; i < num_reference_fields; ++i) {
673        ArtField* field = (is_static
674                           ? klass->GetStaticField(i)
675                           : klass->GetInstanceField(i));
676        MemberOffset field_offset = field->GetOffset();
677        const Object* ref = orig->GetFieldObject<const Object*>(field_offset, false);
678        // Use SetFieldPtr to avoid card marking since we are writing to the image.
679        copy->SetFieldPtr(field_offset, GetImageAddress(ref), false);
680      }
681    }
682  }
683  if (!is_static && orig->IsReferenceInstance()) {
684    // Fix-up referent, that isn't marked as an object field, for References.
685    ArtField* field = orig->GetClass()->FindInstanceField("referent", "Ljava/lang/Object;");
686    MemberOffset field_offset = field->GetOffset();
687    const Object* ref = orig->GetFieldObject<const Object*>(field_offset, false);
688    // Use SetFieldPtr to avoid card marking since we are writing to the image.
689    copy->SetFieldPtr(field_offset, GetImageAddress(ref), false);
690  }
691}
692
693static ArtMethod* GetTargetMethod(const CompilerDriver::PatchInformation* patch)
694    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
695  ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
696  DexCache* dex_cache = class_linker->FindDexCache(patch->GetDexFile());
697  ArtMethod* method = class_linker->ResolveMethod(patch->GetDexFile(),
698                                                  patch->GetTargetMethodIdx(),
699                                                  dex_cache,
700                                                  NULL,
701                                                  NULL,
702                                                  patch->GetTargetInvokeType());
703  CHECK(method != NULL)
704    << patch->GetDexFile().GetLocation() << " " << patch->GetTargetMethodIdx();
705  CHECK(!method->IsRuntimeMethod())
706    << patch->GetDexFile().GetLocation() << " " << patch->GetTargetMethodIdx();
707  CHECK(dex_cache->GetResolvedMethods()->Get(patch->GetTargetMethodIdx()) == method)
708    << patch->GetDexFile().GetLocation() << " " << patch->GetReferrerMethodIdx() << " "
709    << PrettyMethod(dex_cache->GetResolvedMethods()->Get(patch->GetTargetMethodIdx())) << " "
710    << PrettyMethod(method);
711  return method;
712}
713
714void ImageWriter::PatchOatCodeAndMethods() {
715  Thread* self = Thread::Current();
716  ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
717  const char* old_cause = self->StartAssertNoThreadSuspension("ImageWriter");
718
719  typedef std::vector<const CompilerDriver::PatchInformation*> Patches;
720  const Patches& code_to_patch = compiler_driver_.GetCodeToPatch();
721  for (size_t i = 0; i < code_to_patch.size(); i++) {
722    const CompilerDriver::PatchInformation* patch = code_to_patch[i];
723    ArtMethod* target = GetTargetMethod(patch);
724    uint32_t code = reinterpret_cast<uint32_t>(class_linker->GetOatCodeFor(target));
725    uint32_t code_base = reinterpret_cast<uint32_t>(&oat_file_->GetOatHeader());
726    uint32_t code_offset = code - code_base;
727    SetPatchLocation(patch, reinterpret_cast<uint32_t>(GetOatAddress(code_offset)));
728  }
729
730  const Patches& methods_to_patch = compiler_driver_.GetMethodsToPatch();
731  for (size_t i = 0; i < methods_to_patch.size(); i++) {
732    const CompilerDriver::PatchInformation* patch = methods_to_patch[i];
733    ArtMethod* target = GetTargetMethod(patch);
734    SetPatchLocation(patch, reinterpret_cast<uint32_t>(GetImageAddress(target)));
735  }
736
737  // Update the image header with the new checksum after patching
738  ImageHeader* image_header = reinterpret_cast<ImageHeader*>(image_->Begin());
739  image_header->SetOatChecksum(oat_file_->GetOatHeader().GetChecksum());
740  self->EndAssertNoThreadSuspension(old_cause);
741}
742
743void ImageWriter::SetPatchLocation(const CompilerDriver::PatchInformation* patch, uint32_t value) {
744  ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
745  const void* oat_code = class_linker->GetOatCodeFor(patch->GetDexFile(),
746                                                     patch->GetReferrerClassDefIdx(),
747                                                     patch->GetReferrerMethodIdx());
748  OatHeader& oat_header = const_cast<OatHeader&>(oat_file_->GetOatHeader());
749  // TODO: make this Thumb2 specific
750  uint8_t* base = reinterpret_cast<uint8_t*>(reinterpret_cast<uint32_t>(oat_code) & ~0x1);
751  uint32_t* patch_location = reinterpret_cast<uint32_t*>(base + patch->GetLiteralOffset());
752#ifndef NDEBUG
753  const DexFile::MethodId& id = patch->GetDexFile().GetMethodId(patch->GetTargetMethodIdx());
754  uint32_t expected = reinterpret_cast<uint32_t>(&id);
755  uint32_t actual = *patch_location;
756  CHECK(actual == expected || actual == value) << std::hex
757    << "actual=" << actual
758    << "expected=" << expected
759    << "value=" << value;
760#endif
761  *patch_location = value;
762  oat_header.UpdateChecksum(patch_location, sizeof(value));
763}
764
765}  // namespace art
766