patchoat.cc revision c9f76628ce1dc54f852b689ed1722b7e154b2a9d
1/*
2 * Copyright (C) 2014 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#include "patchoat.h"
17
18#include <stdio.h>
19#include <stdlib.h>
20#include <sys/file.h>
21#include <sys/stat.h>
22#include <unistd.h>
23
24#include <string>
25#include <vector>
26
27#include "android-base/stringprintf.h"
28#include "android-base/strings.h"
29
30#include "art_field-inl.h"
31#include "art_method-inl.h"
32#include "base/dumpable.h"
33#include "base/scoped_flock.h"
34#include "base/stringpiece.h"
35#include "base/unix_file/fd_file.h"
36#include "base/unix_file/random_access_file_utils.h"
37#include "elf_utils.h"
38#include "elf_file.h"
39#include "elf_file_impl.h"
40#include "gc/space/image_space.h"
41#include "image-inl.h"
42#include "mirror/dex_cache.h"
43#include "mirror/executable.h"
44#include "mirror/object-inl.h"
45#include "mirror/method.h"
46#include "mirror/reference.h"
47#include "noop_compiler_callbacks.h"
48#include "offsets.h"
49#include "os.h"
50#include "runtime.h"
51#include "scoped_thread_state_change-inl.h"
52#include "thread.h"
53#include "utils.h"
54
55namespace art {
56
57static const OatHeader* GetOatHeader(const ElfFile* elf_file) {
58  uint64_t off = 0;
59  if (!elf_file->GetSectionOffsetAndSize(".rodata", &off, nullptr)) {
60    return nullptr;
61  }
62
63  OatHeader* oat_header = reinterpret_cast<OatHeader*>(elf_file->Begin() + off);
64  return oat_header;
65}
66
67static File* CreateOrOpen(const char* name) {
68  if (OS::FileExists(name)) {
69    return OS::OpenFileReadWrite(name);
70  } else {
71    std::unique_ptr<File> f(OS::CreateEmptyFile(name));
72    if (f.get() != nullptr) {
73      if (fchmod(f->Fd(), 0644) != 0) {
74        PLOG(ERROR) << "Unable to make " << name << " world readable";
75        unlink(name);
76        return nullptr;
77      }
78    }
79    return f.release();
80  }
81}
82
83// Either try to close the file (close=true), or erase it.
84static bool FinishFile(File* file, bool close) {
85  if (close) {
86    if (file->FlushCloseOrErase() != 0) {
87      PLOG(ERROR) << "Failed to flush and close file.";
88      return false;
89    }
90    return true;
91  } else {
92    file->Erase();
93    return false;
94  }
95}
96
97static bool SymlinkFile(const std::string& input_filename, const std::string& output_filename) {
98  if (input_filename == output_filename) {
99    // Input and output are the same, nothing to do.
100    return true;
101  }
102
103  // Unlink the original filename, since we are overwriting it.
104  unlink(output_filename.c_str());
105
106  // Create a symlink from the source file to the target path.
107  if (symlink(input_filename.c_str(), output_filename.c_str()) < 0) {
108    PLOG(ERROR) << "Failed to create symlink " << output_filename << " -> " << input_filename;
109    return false;
110  }
111
112  if (kIsDebugBuild) {
113    LOG(INFO) << "Created symlink " << output_filename << " -> " << input_filename;
114  }
115
116  return true;
117}
118
119bool PatchOat::Patch(const std::string& image_location,
120                     off_t delta,
121                     const std::string& output_directory,
122                     InstructionSet isa,
123                     TimingLogger* timings) {
124  CHECK(Runtime::Current() == nullptr);
125  CHECK(!image_location.empty()) << "image file must have a filename.";
126
127  TimingLogger::ScopedTiming t("Runtime Setup", timings);
128
129  CHECK_NE(isa, kNone);
130  const char* isa_name = GetInstructionSetString(isa);
131
132  // Set up the runtime
133  RuntimeOptions options;
134  NoopCompilerCallbacks callbacks;
135  options.push_back(std::make_pair("compilercallbacks", &callbacks));
136  std::string img = "-Ximage:" + image_location;
137  options.push_back(std::make_pair(img.c_str(), nullptr));
138  options.push_back(std::make_pair("imageinstructionset", reinterpret_cast<const void*>(isa_name)));
139  options.push_back(std::make_pair("-Xno-sig-chain", nullptr));
140  if (!Runtime::Create(options, false)) {
141    LOG(ERROR) << "Unable to initialize runtime";
142    return false;
143  }
144  // Runtime::Create acquired the mutator_lock_ that is normally given away when we Runtime::Start,
145  // give it away now and then switch to a more manageable ScopedObjectAccess.
146  Thread::Current()->TransitionFromRunnableToSuspended(kNative);
147  ScopedObjectAccess soa(Thread::Current());
148
149  t.NewTiming("Image Patching setup");
150  std::vector<gc::space::ImageSpace*> spaces = Runtime::Current()->GetHeap()->GetBootImageSpaces();
151  std::map<gc::space::ImageSpace*, std::unique_ptr<File>> space_to_file_map;
152  std::map<gc::space::ImageSpace*, std::unique_ptr<MemMap>> space_to_memmap_map;
153  std::map<gc::space::ImageSpace*, PatchOat> space_to_patchoat_map;
154
155  for (size_t i = 0; i < spaces.size(); ++i) {
156    gc::space::ImageSpace* space = spaces[i];
157    std::string input_image_filename = space->GetImageFilename();
158    std::unique_ptr<File> input_image(OS::OpenFileForReading(input_image_filename.c_str()));
159    if (input_image.get() == nullptr) {
160      LOG(ERROR) << "Unable to open input image file at " << input_image_filename;
161      return false;
162    }
163
164    int64_t image_len = input_image->GetLength();
165    if (image_len < 0) {
166      LOG(ERROR) << "Error while getting image length";
167      return false;
168    }
169    ImageHeader image_header;
170    if (sizeof(image_header) != input_image->Read(reinterpret_cast<char*>(&image_header),
171                                                  sizeof(image_header), 0)) {
172      LOG(ERROR) << "Unable to read image header from image file " << input_image->GetPath();
173    }
174
175    /*bool is_image_pic = */IsImagePic(image_header, input_image->GetPath());
176    // Nothing special to do right now since the image always needs to get patched.
177    // Perhaps in some far-off future we may have images with relative addresses that are true-PIC.
178
179    // Create the map where we will write the image patches to.
180    std::string error_msg;
181    std::unique_ptr<MemMap> image(MemMap::MapFile(image_len,
182                                                  PROT_READ | PROT_WRITE,
183                                                  MAP_PRIVATE,
184                                                  input_image->Fd(),
185                                                  0,
186                                                  /*low_4gb*/false,
187                                                  input_image->GetPath().c_str(),
188                                                  &error_msg));
189    if (image.get() == nullptr) {
190      LOG(ERROR) << "Unable to map image file " << input_image->GetPath() << " : " << error_msg;
191      return false;
192    }
193    space_to_file_map.emplace(space, std::move(input_image));
194    space_to_memmap_map.emplace(space, std::move(image));
195  }
196
197  // Symlink PIC oat and vdex files and patch the image spaces in memory.
198  for (size_t i = 0; i < spaces.size(); ++i) {
199    gc::space::ImageSpace* space = spaces[i];
200    std::string input_image_filename = space->GetImageFilename();
201    std::string input_vdex_filename =
202        ImageHeader::GetVdexLocationFromImageLocation(input_image_filename);
203    std::string input_oat_filename =
204        ImageHeader::GetOatLocationFromImageLocation(input_image_filename);
205    std::unique_ptr<File> input_oat_file(OS::OpenFileForReading(input_oat_filename.c_str()));
206    if (input_oat_file.get() == nullptr) {
207      LOG(ERROR) << "Unable to open input oat file at " << input_oat_filename;
208      return false;
209    }
210    std::string error_msg;
211    std::unique_ptr<ElfFile> elf(ElfFile::Open(input_oat_file.get(),
212                                               PROT_READ | PROT_WRITE, MAP_PRIVATE, &error_msg));
213    if (elf.get() == nullptr) {
214      LOG(ERROR) << "Unable to open oat file " << input_oat_file->GetPath() << " : " << error_msg;
215      return false;
216    }
217
218    MaybePic is_oat_pic = IsOatPic(elf.get());
219    if (is_oat_pic >= ERROR_FIRST) {
220      // Error logged by IsOatPic
221      return false;
222    } else if (is_oat_pic == NOT_PIC) {
223      LOG(ERROR) << input_oat_file->GetPath() << " is not PIC";
224      return false;
225    } else {
226      CHECK(is_oat_pic == PIC);
227
228      // Create a symlink.
229      std::string converted_image_filename = space->GetImageLocation();
230      std::replace(converted_image_filename.begin() + 1, converted_image_filename.end(), '/', '@');
231      std::string output_image_filename = output_directory +
232          (android::base::StartsWith(converted_image_filename, "/") ? "" : "/") +
233          converted_image_filename;
234      std::string output_vdex_filename =
235          ImageHeader::GetVdexLocationFromImageLocation(output_image_filename);
236      std::string output_oat_filename =
237          ImageHeader::GetOatLocationFromImageLocation(output_image_filename);
238
239      if (!ReplaceOatFileWithSymlink(input_oat_file->GetPath(),
240                                     output_oat_filename) ||
241          !SymlinkFile(input_vdex_filename, output_vdex_filename)) {
242        // Errors already logged by above call.
243        return false;
244      }
245    }
246
247    PatchOat& p = space_to_patchoat_map.emplace(space,
248                                                PatchOat(
249                                                    isa,
250                                                    space_to_memmap_map.find(space)->second.get(),
251                                                    space->GetLiveBitmap(),
252                                                    space->GetMemMap(),
253                                                    delta,
254                                                    &space_to_memmap_map,
255                                                    timings)).first->second;
256
257    t.NewTiming("Patching image");
258    if (!p.PatchImage(i == 0)) {
259      LOG(ERROR) << "Failed to patch image file " << input_image_filename;
260      return false;
261    }
262  }
263
264  // Write the patched image spaces.
265  for (size_t i = 0; i < spaces.size(); ++i) {
266    gc::space::ImageSpace* space = spaces[i];
267
268    t.NewTiming("Writing image");
269    std::string converted_image_filename = space->GetImageLocation();
270    std::replace(converted_image_filename.begin() + 1, converted_image_filename.end(), '/', '@');
271    std::string output_image_filename = output_directory +
272        (android::base::StartsWith(converted_image_filename, "/") ? "" : "/") +
273        converted_image_filename;
274    std::unique_ptr<File> output_image_file(CreateOrOpen(output_image_filename.c_str()));
275    if (output_image_file.get() == nullptr) {
276      LOG(ERROR) << "Failed to open output image file at " << output_image_filename;
277      return false;
278    }
279
280    PatchOat& p = space_to_patchoat_map.find(space)->second;
281
282    bool success = p.WriteImage(output_image_file.get());
283    success = FinishFile(output_image_file.get(), success);
284    if (!success) {
285      return false;
286    }
287  }
288  return true;
289}
290
291bool PatchOat::WriteImage(File* out) {
292  TimingLogger::ScopedTiming t("Writing image File", timings_);
293  std::string error_msg;
294
295  ScopedFlock img_flock;
296  img_flock.Init(out, &error_msg);
297
298  CHECK(image_ != nullptr);
299  CHECK(out != nullptr);
300  size_t expect = image_->Size();
301  if (out->WriteFully(reinterpret_cast<char*>(image_->Begin()), expect) &&
302      out->SetLength(expect) == 0) {
303    return true;
304  } else {
305    LOG(ERROR) << "Writing to image file " << out->GetPath() << " failed.";
306    return false;
307  }
308}
309
310bool PatchOat::IsImagePic(const ImageHeader& image_header, const std::string& image_path) {
311  if (!image_header.CompilePic()) {
312    if (kIsDebugBuild) {
313      LOG(INFO) << "image at location " << image_path << " was *not* compiled pic";
314    }
315    return false;
316  }
317
318  if (kIsDebugBuild) {
319    LOG(INFO) << "image at location " << image_path << " was compiled PIC";
320  }
321
322  return true;
323}
324
325PatchOat::MaybePic PatchOat::IsOatPic(const ElfFile* oat_in) {
326  if (oat_in == nullptr) {
327    LOG(ERROR) << "No ELF input oat fie available";
328    return ERROR_OAT_FILE;
329  }
330
331  const std::string& file_path = oat_in->GetFilePath();
332
333  const OatHeader* oat_header = GetOatHeader(oat_in);
334  if (oat_header == nullptr) {
335    LOG(ERROR) << "Failed to find oat header in oat file " << file_path;
336    return ERROR_OAT_FILE;
337  }
338
339  if (!oat_header->IsValid()) {
340    LOG(ERROR) << "Elf file " << file_path << " has an invalid oat header";
341    return ERROR_OAT_FILE;
342  }
343
344  bool is_pic = oat_header->IsPic();
345  if (kIsDebugBuild) {
346    LOG(INFO) << "Oat file at " << file_path << " is " << (is_pic ? "PIC" : "not pic");
347  }
348
349  return is_pic ? PIC : NOT_PIC;
350}
351
352bool PatchOat::ReplaceOatFileWithSymlink(const std::string& input_oat_filename,
353                                         const std::string& output_oat_filename) {
354  // Delete the original file, since we won't need it.
355  unlink(output_oat_filename.c_str());
356
357  // Create a symlink from the old oat to the new oat
358  if (symlink(input_oat_filename.c_str(), output_oat_filename.c_str()) < 0) {
359    int err = errno;
360    LOG(ERROR) << "Failed to create symlink at " << output_oat_filename
361               << " error(" << err << "): " << strerror(err);
362    return false;
363  }
364
365  if (kIsDebugBuild) {
366    LOG(INFO) << "Created symlink " << output_oat_filename << " -> " << input_oat_filename;
367  }
368
369  return true;
370}
371
372class PatchOat::PatchOatArtFieldVisitor : public ArtFieldVisitor {
373 public:
374  explicit PatchOatArtFieldVisitor(PatchOat* patch_oat) : patch_oat_(patch_oat) {}
375
376  void Visit(ArtField* field) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
377    ArtField* const dest = patch_oat_->RelocatedCopyOf(field);
378    dest->SetDeclaringClass(
379        patch_oat_->RelocatedAddressOfPointer(field->GetDeclaringClass().Ptr()));
380  }
381
382 private:
383  PatchOat* const patch_oat_;
384};
385
386void PatchOat::PatchArtFields(const ImageHeader* image_header) {
387  PatchOatArtFieldVisitor visitor(this);
388  image_header->VisitPackedArtFields(&visitor, heap_->Begin());
389}
390
391class PatchOat::PatchOatArtMethodVisitor : public ArtMethodVisitor {
392 public:
393  explicit PatchOatArtMethodVisitor(PatchOat* patch_oat) : patch_oat_(patch_oat) {}
394
395  void Visit(ArtMethod* method) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
396    ArtMethod* const dest = patch_oat_->RelocatedCopyOf(method);
397    patch_oat_->FixupMethod(method, dest);
398  }
399
400 private:
401  PatchOat* const patch_oat_;
402};
403
404void PatchOat::PatchArtMethods(const ImageHeader* image_header) {
405  const PointerSize pointer_size = InstructionSetPointerSize(isa_);
406  PatchOatArtMethodVisitor visitor(this);
407  image_header->VisitPackedArtMethods(&visitor, heap_->Begin(), pointer_size);
408}
409
410void PatchOat::PatchImTables(const ImageHeader* image_header) {
411  const PointerSize pointer_size = InstructionSetPointerSize(isa_);
412  // We can safely walk target image since the conflict tables are independent.
413  image_header->VisitPackedImTables(
414      [this](ArtMethod* method) {
415        return RelocatedAddressOfPointer(method);
416      },
417      image_->Begin(),
418      pointer_size);
419}
420
421void PatchOat::PatchImtConflictTables(const ImageHeader* image_header) {
422  const PointerSize pointer_size = InstructionSetPointerSize(isa_);
423  // We can safely walk target image since the conflict tables are independent.
424  image_header->VisitPackedImtConflictTables(
425      [this](ArtMethod* method) {
426        return RelocatedAddressOfPointer(method);
427      },
428      image_->Begin(),
429      pointer_size);
430}
431
432class PatchOat::FixupRootVisitor : public RootVisitor {
433 public:
434  explicit FixupRootVisitor(const PatchOat* patch_oat) : patch_oat_(patch_oat) {
435  }
436
437  void VisitRoots(mirror::Object*** roots, size_t count, const RootInfo& info ATTRIBUTE_UNUSED)
438      OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
439    for (size_t i = 0; i < count; ++i) {
440      *roots[i] = patch_oat_->RelocatedAddressOfPointer(*roots[i]);
441    }
442  }
443
444  void VisitRoots(mirror::CompressedReference<mirror::Object>** roots, size_t count,
445                  const RootInfo& info ATTRIBUTE_UNUSED)
446      OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
447    for (size_t i = 0; i < count; ++i) {
448      roots[i]->Assign(patch_oat_->RelocatedAddressOfPointer(roots[i]->AsMirrorPtr()));
449    }
450  }
451
452 private:
453  const PatchOat* const patch_oat_;
454};
455
456void PatchOat::PatchInternedStrings(const ImageHeader* image_header) {
457  const auto& section = image_header->GetImageSection(ImageHeader::kSectionInternedStrings);
458  InternTable temp_table;
459  // Note that we require that ReadFromMemory does not make an internal copy of the elements.
460  // This also relies on visit roots not doing any verification which could fail after we update
461  // the roots to be the image addresses.
462  temp_table.AddTableFromMemory(image_->Begin() + section.Offset());
463  FixupRootVisitor visitor(this);
464  temp_table.VisitRoots(&visitor, kVisitRootFlagAllRoots);
465}
466
467void PatchOat::PatchClassTable(const ImageHeader* image_header) {
468  const auto& section = image_header->GetImageSection(ImageHeader::kSectionClassTable);
469  if (section.Size() == 0) {
470    return;
471  }
472  // Note that we require that ReadFromMemory does not make an internal copy of the elements.
473  // This also relies on visit roots not doing any verification which could fail after we update
474  // the roots to be the image addresses.
475  WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_);
476  ClassTable temp_table;
477  temp_table.ReadFromMemory(image_->Begin() + section.Offset());
478  FixupRootVisitor visitor(this);
479  temp_table.VisitRoots(UnbufferedRootVisitor(&visitor, RootInfo(kRootUnknown)));
480}
481
482
483class PatchOat::RelocatedPointerVisitor {
484 public:
485  explicit RelocatedPointerVisitor(PatchOat* patch_oat) : patch_oat_(patch_oat) {}
486
487  template <typename T>
488  T* operator()(T* ptr) const {
489    return patch_oat_->RelocatedAddressOfPointer(ptr);
490  }
491
492 private:
493  PatchOat* const patch_oat_;
494};
495
496void PatchOat::PatchDexFileArrays(mirror::ObjectArray<mirror::Object>* img_roots) {
497  auto* dex_caches = down_cast<mirror::ObjectArray<mirror::DexCache>*>(
498      img_roots->Get(ImageHeader::kDexCaches));
499  const PointerSize pointer_size = InstructionSetPointerSize(isa_);
500  for (size_t i = 0, count = dex_caches->GetLength(); i < count; ++i) {
501    auto* orig_dex_cache = dex_caches->GetWithoutChecks(i);
502    auto* copy_dex_cache = RelocatedCopyOf(orig_dex_cache);
503    // Though the DexCache array fields are usually treated as native pointers, we set the full
504    // 64-bit values here, clearing the top 32 bits for 32-bit targets. The zero-extension is
505    // done by casting to the unsigned type uintptr_t before casting to int64_t, i.e.
506    //     static_cast<int64_t>(reinterpret_cast<uintptr_t>(image_begin_ + offset))).
507    mirror::StringDexCacheType* orig_strings = orig_dex_cache->GetStrings();
508    mirror::StringDexCacheType* relocated_strings = RelocatedAddressOfPointer(orig_strings);
509    copy_dex_cache->SetField64<false>(
510        mirror::DexCache::StringsOffset(),
511        static_cast<int64_t>(reinterpret_cast<uintptr_t>(relocated_strings)));
512    if (orig_strings != nullptr) {
513      orig_dex_cache->FixupStrings(RelocatedCopyOf(orig_strings), RelocatedPointerVisitor(this));
514    }
515    GcRoot<mirror::Class>* orig_types = orig_dex_cache->GetResolvedTypes();
516    GcRoot<mirror::Class>* relocated_types = RelocatedAddressOfPointer(orig_types);
517    copy_dex_cache->SetField64<false>(
518        mirror::DexCache::ResolvedTypesOffset(),
519        static_cast<int64_t>(reinterpret_cast<uintptr_t>(relocated_types)));
520    if (orig_types != nullptr) {
521      orig_dex_cache->FixupResolvedTypes(RelocatedCopyOf(orig_types),
522                                         RelocatedPointerVisitor(this));
523    }
524    ArtMethod** orig_methods = orig_dex_cache->GetResolvedMethods();
525    ArtMethod** relocated_methods = RelocatedAddressOfPointer(orig_methods);
526    copy_dex_cache->SetField64<false>(
527        mirror::DexCache::ResolvedMethodsOffset(),
528        static_cast<int64_t>(reinterpret_cast<uintptr_t>(relocated_methods)));
529    if (orig_methods != nullptr) {
530      ArtMethod** copy_methods = RelocatedCopyOf(orig_methods);
531      for (size_t j = 0, num = orig_dex_cache->NumResolvedMethods(); j != num; ++j) {
532        ArtMethod* orig = mirror::DexCache::GetElementPtrSize(orig_methods, j, pointer_size);
533        ArtMethod* copy = RelocatedAddressOfPointer(orig);
534        mirror::DexCache::SetElementPtrSize(copy_methods, j, copy, pointer_size);
535      }
536    }
537    ArtField** orig_fields = orig_dex_cache->GetResolvedFields();
538    ArtField** relocated_fields = RelocatedAddressOfPointer(orig_fields);
539    copy_dex_cache->SetField64<false>(
540        mirror::DexCache::ResolvedFieldsOffset(),
541        static_cast<int64_t>(reinterpret_cast<uintptr_t>(relocated_fields)));
542    if (orig_fields != nullptr) {
543      ArtField** copy_fields = RelocatedCopyOf(orig_fields);
544      for (size_t j = 0, num = orig_dex_cache->NumResolvedFields(); j != num; ++j) {
545        ArtField* orig = mirror::DexCache::GetElementPtrSize(orig_fields, j, pointer_size);
546        ArtField* copy = RelocatedAddressOfPointer(orig);
547        mirror::DexCache::SetElementPtrSize(copy_fields, j, copy, pointer_size);
548      }
549    }
550    mirror::MethodTypeDexCacheType* orig_method_types = orig_dex_cache->GetResolvedMethodTypes();
551    mirror::MethodTypeDexCacheType* relocated_method_types =
552        RelocatedAddressOfPointer(orig_method_types);
553    copy_dex_cache->SetField64<false>(
554        mirror::DexCache::ResolvedMethodTypesOffset(),
555        static_cast<int64_t>(reinterpret_cast<uintptr_t>(relocated_method_types)));
556    if (orig_method_types != nullptr) {
557      orig_dex_cache->FixupResolvedMethodTypes(RelocatedCopyOf(orig_method_types),
558                                               RelocatedPointerVisitor(this));
559    }
560  }
561}
562
563bool PatchOat::PatchImage(bool primary_image) {
564  ImageHeader* image_header = reinterpret_cast<ImageHeader*>(image_->Begin());
565  CHECK_GT(image_->Size(), sizeof(ImageHeader));
566  // These are the roots from the original file.
567  auto* img_roots = image_header->GetImageRoots();
568  image_header->RelocateImage(delta_);
569
570  PatchArtFields(image_header);
571  PatchArtMethods(image_header);
572  PatchImTables(image_header);
573  PatchImtConflictTables(image_header);
574  PatchInternedStrings(image_header);
575  PatchClassTable(image_header);
576  // Patch dex file int/long arrays which point to ArtFields.
577  PatchDexFileArrays(img_roots);
578
579  if (primary_image) {
580    VisitObject(img_roots);
581  }
582
583  if (!image_header->IsValid()) {
584    LOG(ERROR) << "relocation renders image header invalid";
585    return false;
586  }
587
588  {
589    TimingLogger::ScopedTiming t("Walk Bitmap", timings_);
590    // Walk the bitmap.
591    WriterMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_);
592    bitmap_->Walk(PatchOat::BitmapCallback, this);
593  }
594  return true;
595}
596
597
598void PatchOat::PatchVisitor::operator() (ObjPtr<mirror::Object> obj,
599                                         MemberOffset off,
600                                         bool is_static_unused ATTRIBUTE_UNUSED) const {
601  mirror::Object* referent = obj->GetFieldObject<mirror::Object, kVerifyNone>(off);
602  mirror::Object* moved_object = patcher_->RelocatedAddressOfPointer(referent);
603  copy_->SetFieldObjectWithoutWriteBarrier<false, true, kVerifyNone>(off, moved_object);
604}
605
606void PatchOat::PatchVisitor::operator() (ObjPtr<mirror::Class> cls ATTRIBUTE_UNUSED,
607                                         ObjPtr<mirror::Reference> ref) const {
608  MemberOffset off = mirror::Reference::ReferentOffset();
609  mirror::Object* referent = ref->GetReferent();
610  DCHECK(referent == nullptr ||
611         Runtime::Current()->GetHeap()->ObjectIsInBootImageSpace(referent)) << referent;
612  mirror::Object* moved_object = patcher_->RelocatedAddressOfPointer(referent);
613  copy_->SetFieldObjectWithoutWriteBarrier<false, true, kVerifyNone>(off, moved_object);
614}
615
616// Called by BitmapCallback
617void PatchOat::VisitObject(mirror::Object* object) {
618  mirror::Object* copy = RelocatedCopyOf(object);
619  CHECK(copy != nullptr);
620  if (kUseBakerReadBarrier) {
621    object->AssertReadBarrierState();
622  }
623  PatchOat::PatchVisitor visitor(this, copy);
624  object->VisitReferences<kVerifyNone>(visitor, visitor);
625  if (object->IsClass<kVerifyNone>()) {
626    const PointerSize pointer_size = InstructionSetPointerSize(isa_);
627    mirror::Class* klass = object->AsClass();
628    mirror::Class* copy_klass = down_cast<mirror::Class*>(copy);
629    RelocatedPointerVisitor native_visitor(this);
630    klass->FixupNativePointers(copy_klass, pointer_size, native_visitor);
631    auto* vtable = klass->GetVTable();
632    if (vtable != nullptr) {
633      vtable->Fixup(RelocatedCopyOfFollowImages(vtable), pointer_size, native_visitor);
634    }
635    mirror::IfTable* iftable = klass->GetIfTable();
636    for (int32_t i = 0; i < klass->GetIfTableCount(); ++i) {
637      if (iftable->GetMethodArrayCount(i) > 0) {
638        auto* method_array = iftable->GetMethodArray(i);
639        CHECK(method_array != nullptr);
640        method_array->Fixup(RelocatedCopyOfFollowImages(method_array),
641                            pointer_size,
642                            native_visitor);
643      }
644    }
645  } else if (object->GetClass() == mirror::Method::StaticClass() ||
646             object->GetClass() == mirror::Constructor::StaticClass()) {
647    // Need to go update the ArtMethod.
648    auto* dest = down_cast<mirror::Executable*>(copy);
649    auto* src = down_cast<mirror::Executable*>(object);
650    dest->SetArtMethod(RelocatedAddressOfPointer(src->GetArtMethod()));
651  }
652}
653
654void PatchOat::FixupMethod(ArtMethod* object, ArtMethod* copy) {
655  const PointerSize pointer_size = InstructionSetPointerSize(isa_);
656  copy->CopyFrom(object, pointer_size);
657  // Just update the entry points if it looks like we should.
658  // TODO: sanity check all the pointers' values
659  copy->SetDeclaringClass(RelocatedAddressOfPointer(object->GetDeclaringClass()));
660  copy->SetDexCacheResolvedMethods(
661      RelocatedAddressOfPointer(object->GetDexCacheResolvedMethods(pointer_size)), pointer_size);
662  copy->SetDexCacheResolvedTypes(
663      RelocatedAddressOfPointer(object->GetDexCacheResolvedTypes(pointer_size)), pointer_size);
664  copy->SetEntryPointFromQuickCompiledCodePtrSize(RelocatedAddressOfPointer(
665      object->GetEntryPointFromQuickCompiledCodePtrSize(pointer_size)), pointer_size);
666  // No special handling for IMT conflict table since all pointers are moved by the same offset.
667  copy->SetDataPtrSize(RelocatedAddressOfPointer(
668      object->GetDataPtrSize(pointer_size)), pointer_size);
669}
670
671static int orig_argc;
672static char** orig_argv;
673
674static std::string CommandLine() {
675  std::vector<std::string> command;
676  for (int i = 0; i < orig_argc; ++i) {
677    command.push_back(orig_argv[i]);
678  }
679  return android::base::Join(command, ' ');
680}
681
682static void UsageErrorV(const char* fmt, va_list ap) {
683  std::string error;
684  android::base::StringAppendV(&error, fmt, ap);
685  LOG(ERROR) << error;
686}
687
688static void UsageError(const char* fmt, ...) {
689  va_list ap;
690  va_start(ap, fmt);
691  UsageErrorV(fmt, ap);
692  va_end(ap);
693}
694
695NO_RETURN static void Usage(const char *fmt, ...) {
696  va_list ap;
697  va_start(ap, fmt);
698  UsageErrorV(fmt, ap);
699  va_end(ap);
700
701  UsageError("Command: %s", CommandLine().c_str());
702  UsageError("Usage: patchoat [options]...");
703  UsageError("");
704  UsageError("  --instruction-set=<isa>: Specifies the instruction set the patched code is");
705  UsageError("      compiled for (required).");
706  UsageError("");
707  UsageError("  --input-image-location=<file.art>: Specifies the 'location' of the image file to");
708  UsageError("      be patched.");
709  UsageError("");
710  UsageError("  --output-image-file=<file.art>: Specifies the exact file to write the patched");
711  UsageError("      image file to.");
712  UsageError("");
713  UsageError("  --base-offset-delta=<delta>: Specify the amount to change the old base-offset by.");
714  UsageError("      This value may be negative.");
715  UsageError("");
716  UsageError("  --dump-timings: dump out patch timing information");
717  UsageError("");
718  UsageError("  --no-dump-timings: do not dump out patch timing information");
719  UsageError("");
720
721  exit(EXIT_FAILURE);
722}
723
724static int patchoat_image(TimingLogger& timings,
725                          InstructionSet isa,
726                          const std::string& input_image_location,
727                          const std::string& output_image_filename,
728                          off_t base_delta,
729                          bool base_delta_set,
730                          bool debug) {
731  CHECK(!input_image_location.empty());
732  if (output_image_filename.empty()) {
733    Usage("Image patching requires --output-image-file");
734  }
735
736  if (!base_delta_set) {
737    Usage("Must supply a desired new offset or delta.");
738  }
739
740  if (!IsAligned<kPageSize>(base_delta)) {
741    Usage("Base offset/delta must be aligned to a pagesize (0x%08x) boundary.", kPageSize);
742  }
743
744  if (debug) {
745    LOG(INFO) << "moving offset by " << base_delta
746        << " (0x" << std::hex << base_delta << ") bytes or "
747        << std::dec << (base_delta/kPageSize) << " pages.";
748  }
749
750  TimingLogger::ScopedTiming pt("patch image and oat", &timings);
751
752  std::string output_directory =
753      output_image_filename.substr(0, output_image_filename.find_last_of('/'));
754  bool ret = PatchOat::Patch(input_image_location, base_delta, output_directory, isa, &timings);
755
756  if (kIsDebugBuild) {
757    LOG(INFO) << "Exiting with return ... " << ret;
758  }
759  return ret ? EXIT_SUCCESS : EXIT_FAILURE;
760}
761
762static int patchoat(int argc, char **argv) {
763  InitLogging(argv, Runtime::Aborter);
764  MemMap::Init();
765  const bool debug = kIsDebugBuild;
766  orig_argc = argc;
767  orig_argv = argv;
768  TimingLogger timings("patcher", false, false);
769
770  // Skip over the command name.
771  argv++;
772  argc--;
773
774  if (argc == 0) {
775    Usage("No arguments specified");
776  }
777
778  timings.StartTiming("Patchoat");
779
780  // cmd line args
781  bool isa_set = false;
782  InstructionSet isa = kNone;
783  std::string input_image_location;
784  std::string output_image_filename;
785  off_t base_delta = 0;
786  bool base_delta_set = false;
787  bool dump_timings = kIsDebugBuild;
788
789  for (int i = 0; i < argc; ++i) {
790    const StringPiece option(argv[i]);
791    const bool log_options = false;
792    if (log_options) {
793      LOG(INFO) << "patchoat: option[" << i << "]=" << argv[i];
794    }
795    if (option.starts_with("--instruction-set=")) {
796      isa_set = true;
797      const char* isa_str = option.substr(strlen("--instruction-set=")).data();
798      isa = GetInstructionSetFromString(isa_str);
799      if (isa == kNone) {
800        Usage("Unknown or invalid instruction set %s", isa_str);
801      }
802    } else if (option.starts_with("--input-image-location=")) {
803      input_image_location = option.substr(strlen("--input-image-location=")).data();
804    } else if (option.starts_with("--output-image-file=")) {
805      output_image_filename = option.substr(strlen("--output-image-file=")).data();
806    } else if (option.starts_with("--base-offset-delta=")) {
807      const char* base_delta_str = option.substr(strlen("--base-offset-delta=")).data();
808      base_delta_set = true;
809      if (!ParseInt(base_delta_str, &base_delta)) {
810        Usage("Failed to parse --base-offset-delta argument '%s' as an off_t", base_delta_str);
811      }
812    } else if (option == "--dump-timings") {
813      dump_timings = true;
814    } else if (option == "--no-dump-timings") {
815      dump_timings = false;
816    } else {
817      Usage("Unknown argument %s", option.data());
818    }
819  }
820
821  // The instruction set is mandatory. This simplifies things...
822  if (!isa_set) {
823    Usage("Instruction set must be set.");
824  }
825
826  int ret = patchoat_image(timings,
827                           isa,
828                           input_image_location,
829                           output_image_filename,
830                           base_delta,
831                           base_delta_set,
832                           debug);
833
834  timings.EndTiming();
835  if (dump_timings) {
836    LOG(INFO) << Dumpable<TimingLogger>(timings);
837  }
838
839  return ret;
840}
841
842}  // namespace art
843
844int main(int argc, char **argv) {
845  return art::patchoat(argc, argv);
846}
847