patchoat.cc revision 8c52a3f534c49580688239549fca937b36fc0aed
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 "base/scoped_flock.h"
28#include "base/stringpiece.h"
29#include "base/stringprintf.h"
30#include "elf_utils.h"
31#include "elf_file.h"
32#include "elf_file_impl.h"
33#include "gc/space/image_space.h"
34#include "image.h"
35#include "instruction_set.h"
36#include "mirror/art_field.h"
37#include "mirror/art_field-inl.h"
38#include "mirror/art_method.h"
39#include "mirror/art_method-inl.h"
40#include "mirror/object.h"
41#include "mirror/object-inl.h"
42#include "mirror/reference.h"
43#include "noop_compiler_callbacks.h"
44#include "offsets.h"
45#include "os.h"
46#include "runtime.h"
47#include "scoped_thread_state_change.h"
48#include "thread.h"
49#include "utils.h"
50
51namespace art {
52
53static InstructionSet ElfISAToInstructionSet(Elf32_Word isa) {
54  switch (isa) {
55    case EM_ARM:
56      return kArm;
57    case EM_AARCH64:
58      return kArm64;
59    case EM_386:
60      return kX86;
61    case EM_X86_64:
62      return kX86_64;
63    case EM_MIPS:
64      return kMips;
65    default:
66      return kNone;
67  }
68}
69
70static bool LocationToFilename(const std::string& location, InstructionSet isa,
71                               std::string* filename) {
72  bool has_system = false;
73  bool has_cache = false;
74  // image_location = /system/framework/boot.art
75  // system_image_location = /system/framework/<image_isa>/boot.art
76  std::string system_filename(GetSystemImageFilename(location.c_str(), isa));
77  if (OS::FileExists(system_filename.c_str())) {
78    has_system = true;
79  }
80
81  bool have_android_data = false;
82  bool dalvik_cache_exists = false;
83  bool is_global_cache = false;
84  std::string dalvik_cache;
85  GetDalvikCache(GetInstructionSetString(isa), false, &dalvik_cache,
86                 &have_android_data, &dalvik_cache_exists, &is_global_cache);
87
88  std::string cache_filename;
89  if (have_android_data && dalvik_cache_exists) {
90    // Always set output location even if it does not exist,
91    // so that the caller knows where to create the image.
92    //
93    // image_location = /system/framework/boot.art
94    // *image_filename = /data/dalvik-cache/<image_isa>/boot.art
95    std::string error_msg;
96    if (GetDalvikCacheFilename(location.c_str(), dalvik_cache.c_str(),
97                               &cache_filename, &error_msg)) {
98      has_cache = true;
99    }
100  }
101  if (has_system) {
102    *filename = system_filename;
103    return true;
104  } else if (has_cache) {
105    *filename = cache_filename;
106    return true;
107  } else {
108    return false;
109  }
110}
111
112bool PatchOat::Patch(const std::string& image_location, off_t delta,
113                     File* output_image, InstructionSet isa,
114                     TimingLogger* timings) {
115  CHECK(Runtime::Current() == nullptr);
116  CHECK(output_image != nullptr);
117  CHECK_GE(output_image->Fd(), 0);
118  CHECK(!image_location.empty()) << "image file must have a filename.";
119  CHECK_NE(isa, kNone);
120
121  TimingLogger::ScopedTiming t("Runtime Setup", timings);
122  const char *isa_name = GetInstructionSetString(isa);
123  std::string image_filename;
124  if (!LocationToFilename(image_location, isa, &image_filename)) {
125    LOG(ERROR) << "Unable to find image at location " << image_location;
126    return false;
127  }
128  std::unique_ptr<File> input_image(OS::OpenFileForReading(image_filename.c_str()));
129  if (input_image.get() == nullptr) {
130    LOG(ERROR) << "unable to open input image file at " << image_filename
131               << " for location " << image_location;
132    return false;
133  }
134  int64_t image_len = input_image->GetLength();
135  if (image_len < 0) {
136    LOG(ERROR) << "Error while getting image length";
137    return false;
138  }
139  ImageHeader image_header;
140  if (sizeof(image_header) != input_image->Read(reinterpret_cast<char*>(&image_header),
141                                              sizeof(image_header), 0)) {
142    LOG(ERROR) << "Unable to read image header from image file " << input_image->GetPath();
143    return false;
144  }
145
146  // Set up the runtime
147  RuntimeOptions options;
148  NoopCompilerCallbacks callbacks;
149  options.push_back(std::make_pair("compilercallbacks", &callbacks));
150  std::string img = "-Ximage:" + image_location;
151  options.push_back(std::make_pair(img.c_str(), nullptr));
152  options.push_back(std::make_pair("imageinstructionset", reinterpret_cast<const void*>(isa_name)));
153  if (!Runtime::Create(options, false)) {
154    LOG(ERROR) << "Unable to initialize runtime";
155    return false;
156  }
157  // Runtime::Create acquired the mutator_lock_ that is normally given away when we Runtime::Start,
158  // give it away now and then switch to a more manageable ScopedObjectAccess.
159  Thread::Current()->TransitionFromRunnableToSuspended(kNative);
160  ScopedObjectAccess soa(Thread::Current());
161
162  t.NewTiming("Image and oat Patching setup");
163  // Create the map where we will write the image patches to.
164  std::string error_msg;
165  std::unique_ptr<MemMap> image(MemMap::MapFile(image_len, PROT_READ | PROT_WRITE, MAP_PRIVATE,
166                                                input_image->Fd(), 0,
167                                                input_image->GetPath().c_str(),
168                                                &error_msg));
169  if (image.get() == nullptr) {
170    LOG(ERROR) << "unable to map image file " << input_image->GetPath() << " : " << error_msg;
171    return false;
172  }
173  gc::space::ImageSpace* ispc = Runtime::Current()->GetHeap()->GetImageSpace();
174
175  PatchOat p(image.release(), ispc->GetLiveBitmap(), ispc->GetMemMap(),
176             delta, timings);
177  t.NewTiming("Patching files");
178  if (!p.PatchImage()) {
179    LOG(ERROR) << "Failed to patch image file " << input_image->GetPath();
180    return false;
181  }
182
183  t.NewTiming("Writing files");
184  if (!p.WriteImage(output_image)) {
185    return false;
186  }
187  return true;
188}
189
190bool PatchOat::Patch(const File* input_oat, const std::string& image_location, off_t delta,
191                     File* output_oat, File* output_image, InstructionSet isa,
192                     TimingLogger* timings) {
193  CHECK(Runtime::Current() == nullptr);
194  CHECK(output_image != nullptr);
195  CHECK_GE(output_image->Fd(), 0);
196  CHECK(input_oat != nullptr);
197  CHECK(output_oat != nullptr);
198  CHECK_GE(input_oat->Fd(), 0);
199  CHECK_GE(output_oat->Fd(), 0);
200  CHECK(!image_location.empty()) << "image file must have a filename.";
201
202  TimingLogger::ScopedTiming t("Runtime Setup", timings);
203
204  if (isa == kNone) {
205    Elf32_Ehdr elf_hdr;
206    if (sizeof(elf_hdr) != input_oat->Read(reinterpret_cast<char*>(&elf_hdr), sizeof(elf_hdr), 0)) {
207      LOG(ERROR) << "unable to read elf header";
208      return false;
209    }
210    isa = ElfISAToInstructionSet(elf_hdr.e_machine);
211  }
212  const char* isa_name = GetInstructionSetString(isa);
213  std::string image_filename;
214  if (!LocationToFilename(image_location, isa, &image_filename)) {
215    LOG(ERROR) << "Unable to find image at location " << image_location;
216    return false;
217  }
218  std::unique_ptr<File> input_image(OS::OpenFileForReading(image_filename.c_str()));
219  if (input_image.get() == nullptr) {
220    LOG(ERROR) << "unable to open input image file at " << image_filename
221               << " for location " << image_location;
222    return false;
223  }
224  int64_t image_len = input_image->GetLength();
225  if (image_len < 0) {
226    LOG(ERROR) << "Error while getting image length";
227    return false;
228  }
229  ImageHeader image_header;
230  if (sizeof(image_header) != input_image->Read(reinterpret_cast<char*>(&image_header),
231                                              sizeof(image_header), 0)) {
232    LOG(ERROR) << "Unable to read image header from image file " << input_image->GetPath();
233  }
234
235  // Set up the runtime
236  RuntimeOptions options;
237  NoopCompilerCallbacks callbacks;
238  options.push_back(std::make_pair("compilercallbacks", &callbacks));
239  std::string img = "-Ximage:" + image_location;
240  options.push_back(std::make_pair(img.c_str(), nullptr));
241  options.push_back(std::make_pair("imageinstructionset", reinterpret_cast<const void*>(isa_name)));
242  if (!Runtime::Create(options, false)) {
243    LOG(ERROR) << "Unable to initialize runtime";
244    return false;
245  }
246  // Runtime::Create acquired the mutator_lock_ that is normally given away when we Runtime::Start,
247  // give it away now and then switch to a more manageable ScopedObjectAccess.
248  Thread::Current()->TransitionFromRunnableToSuspended(kNative);
249  ScopedObjectAccess soa(Thread::Current());
250
251  t.NewTiming("Image and oat Patching setup");
252  // Create the map where we will write the image patches to.
253  std::string error_msg;
254  std::unique_ptr<MemMap> image(MemMap::MapFile(image_len, PROT_READ | PROT_WRITE, MAP_PRIVATE,
255                                                input_image->Fd(), 0,
256                                                input_image->GetPath().c_str(),
257                                                &error_msg));
258  if (image.get() == nullptr) {
259    LOG(ERROR) << "unable to map image file " << input_image->GetPath() << " : " << error_msg;
260    return false;
261  }
262  gc::space::ImageSpace* ispc = Runtime::Current()->GetHeap()->GetImageSpace();
263
264  std::unique_ptr<ElfFile> elf(ElfFile::Open(const_cast<File*>(input_oat),
265                                             PROT_READ | PROT_WRITE, MAP_PRIVATE, &error_msg));
266  if (elf.get() == nullptr) {
267    LOG(ERROR) << "unable to open oat file " << input_oat->GetPath() << " : " << error_msg;
268    return false;
269  }
270
271  PatchOat p(elf.release(), image.release(), ispc->GetLiveBitmap(), ispc->GetMemMap(),
272             delta, timings);
273  t.NewTiming("Patching files");
274  if (!p.PatchElf()) {
275    LOG(ERROR) << "Failed to patch oat file " << input_oat->GetPath();
276    return false;
277  }
278  if (!p.PatchImage()) {
279    LOG(ERROR) << "Failed to patch image file " << input_image->GetPath();
280    return false;
281  }
282
283  t.NewTiming("Writing files");
284  if (!p.WriteElf(output_oat)) {
285    return false;
286  }
287  if (!p.WriteImage(output_image)) {
288    return false;
289  }
290  return true;
291}
292
293bool PatchOat::WriteElf(File* out) {
294  TimingLogger::ScopedTiming t("Writing Elf File", timings_);
295
296  CHECK(oat_file_.get() != nullptr);
297  CHECK(out != nullptr);
298  size_t expect = oat_file_->Size();
299  if (out->WriteFully(reinterpret_cast<char*>(oat_file_->Begin()), expect) &&
300      out->SetLength(expect) == 0) {
301    return true;
302  } else {
303    LOG(ERROR) << "Writing to oat file " << out->GetPath() << " failed.";
304    return false;
305  }
306}
307
308bool PatchOat::WriteImage(File* out) {
309  TimingLogger::ScopedTiming t("Writing image File", timings_);
310  std::string error_msg;
311
312  ScopedFlock img_flock;
313  img_flock.Init(out, &error_msg);
314
315  CHECK(image_ != nullptr);
316  CHECK(out != nullptr);
317  size_t expect = image_->Size();
318  if (out->WriteFully(reinterpret_cast<char*>(image_->Begin()), expect) &&
319      out->SetLength(expect) == 0) {
320    return true;
321  } else {
322    LOG(ERROR) << "Writing to image file " << out->GetPath() << " failed.";
323    return false;
324  }
325}
326
327bool PatchOat::PatchImage() {
328  ImageHeader* image_header = reinterpret_cast<ImageHeader*>(image_->Begin());
329  CHECK_GT(image_->Size(), sizeof(ImageHeader));
330  // These are the roots from the original file.
331  mirror::Object* img_roots = image_header->GetImageRoots();
332  image_header->RelocateImage(delta_);
333
334  VisitObject(img_roots);
335  if (!image_header->IsValid()) {
336    LOG(ERROR) << "reloction renders image header invalid";
337    return false;
338  }
339
340  {
341    TimingLogger::ScopedTiming t("Walk Bitmap", timings_);
342    // Walk the bitmap.
343    WriterMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_);
344    bitmap_->Walk(PatchOat::BitmapCallback, this);
345  }
346  return true;
347}
348
349bool PatchOat::InHeap(mirror::Object* o) {
350  uintptr_t begin = reinterpret_cast<uintptr_t>(heap_->Begin());
351  uintptr_t end = reinterpret_cast<uintptr_t>(heap_->End());
352  uintptr_t obj = reinterpret_cast<uintptr_t>(o);
353  return o == nullptr || (begin <= obj && obj < end);
354}
355
356void PatchOat::PatchVisitor::operator() (mirror::Object* obj, MemberOffset off,
357                                         bool is_static_unused) const {
358  mirror::Object* referent = obj->GetFieldObject<mirror::Object, kVerifyNone>(off);
359  DCHECK(patcher_->InHeap(referent)) << "Referent is not in the heap.";
360  mirror::Object* moved_object = patcher_->RelocatedAddressOf(referent);
361  copy_->SetFieldObjectWithoutWriteBarrier<false, true, kVerifyNone>(off, moved_object);
362}
363
364void PatchOat::PatchVisitor::operator() (mirror::Class* cls, mirror::Reference* ref) const {
365  MemberOffset off = mirror::Reference::ReferentOffset();
366  mirror::Object* referent = ref->GetReferent();
367  DCHECK(patcher_->InHeap(referent)) << "Referent is not in the heap.";
368  mirror::Object* moved_object = patcher_->RelocatedAddressOf(referent);
369  copy_->SetFieldObjectWithoutWriteBarrier<false, true, kVerifyNone>(off, moved_object);
370}
371
372mirror::Object* PatchOat::RelocatedCopyOf(mirror::Object* obj) {
373  if (obj == nullptr) {
374    return nullptr;
375  }
376  DCHECK_GT(reinterpret_cast<uintptr_t>(obj), reinterpret_cast<uintptr_t>(heap_->Begin()));
377  DCHECK_LT(reinterpret_cast<uintptr_t>(obj), reinterpret_cast<uintptr_t>(heap_->End()));
378  uintptr_t heap_off =
379      reinterpret_cast<uintptr_t>(obj) - reinterpret_cast<uintptr_t>(heap_->Begin());
380  DCHECK_LT(heap_off, image_->Size());
381  return reinterpret_cast<mirror::Object*>(image_->Begin() + heap_off);
382}
383
384mirror::Object* PatchOat::RelocatedAddressOf(mirror::Object* obj) {
385  if (obj == nullptr) {
386    return nullptr;
387  } else {
388    return reinterpret_cast<mirror::Object*>(reinterpret_cast<byte*>(obj) + delta_);
389  }
390}
391
392// Called by BitmapCallback
393void PatchOat::VisitObject(mirror::Object* object) {
394  mirror::Object* copy = RelocatedCopyOf(object);
395  CHECK(copy != nullptr);
396  if (kUseBakerOrBrooksReadBarrier) {
397    object->AssertReadBarrierPointer();
398    if (kUseBrooksReadBarrier) {
399      mirror::Object* moved_to = RelocatedAddressOf(object);
400      copy->SetReadBarrierPointer(moved_to);
401      DCHECK_EQ(copy->GetReadBarrierPointer(), moved_to);
402    }
403  }
404  PatchOat::PatchVisitor visitor(this, copy);
405  object->VisitReferences<true, kVerifyNone>(visitor, visitor);
406  if (object->IsArtMethod<kVerifyNone>()) {
407    FixupMethod(static_cast<mirror::ArtMethod*>(object),
408                static_cast<mirror::ArtMethod*>(copy));
409  }
410}
411
412void PatchOat::FixupMethod(mirror::ArtMethod* object, mirror::ArtMethod* copy) {
413  // Just update the entry points if it looks like we should.
414  // TODO: sanity check all the pointers' values
415  uintptr_t portable = reinterpret_cast<uintptr_t>(
416      object->GetEntryPointFromPortableCompiledCode<kVerifyNone>());
417  if (portable != 0) {
418    copy->SetEntryPointFromPortableCompiledCode(reinterpret_cast<void*>(portable + delta_));
419  }
420  uintptr_t quick= reinterpret_cast<uintptr_t>(
421      object->GetEntryPointFromQuickCompiledCode<kVerifyNone>());
422  if (quick != 0) {
423    copy->SetEntryPointFromQuickCompiledCode(reinterpret_cast<void*>(quick + delta_));
424  }
425  uintptr_t interpreter = reinterpret_cast<uintptr_t>(
426      object->GetEntryPointFromInterpreter<kVerifyNone>());
427  if (interpreter != 0) {
428    copy->SetEntryPointFromInterpreter(
429        reinterpret_cast<mirror::EntryPointFromInterpreter*>(interpreter + delta_));
430  }
431
432  uintptr_t native_method = reinterpret_cast<uintptr_t>(object->GetNativeMethod());
433  if (native_method != 0) {
434    copy->SetNativeMethod(reinterpret_cast<void*>(native_method + delta_));
435  }
436
437  uintptr_t native_gc_map = reinterpret_cast<uintptr_t>(object->GetNativeGcMap());
438  if (native_gc_map != 0) {
439    copy->SetNativeGcMap(reinterpret_cast<uint8_t*>(native_gc_map + delta_));
440  }
441}
442
443bool PatchOat::Patch(File* input_oat, off_t delta, File* output_oat, TimingLogger* timings) {
444  CHECK(input_oat != nullptr);
445  CHECK(output_oat != nullptr);
446  CHECK_GE(input_oat->Fd(), 0);
447  CHECK_GE(output_oat->Fd(), 0);
448  TimingLogger::ScopedTiming t("Setup Oat File Patching", timings);
449
450  std::string error_msg;
451  std::unique_ptr<ElfFile> elf(ElfFile::Open(const_cast<File*>(input_oat),
452                                             PROT_READ | PROT_WRITE, MAP_PRIVATE, &error_msg));
453  if (elf.get() == nullptr) {
454    LOG(ERROR) << "unable to open oat file " << input_oat->GetPath() << " : " << error_msg;
455    return false;
456  }
457
458  PatchOat p(elf.release(), delta, timings);
459  t.NewTiming("Patch Oat file");
460  if (!p.PatchElf()) {
461    return false;
462  }
463
464  t.NewTiming("Writing oat file");
465  if (!p.WriteElf(output_oat)) {
466    return false;
467  }
468  return true;
469}
470
471template <typename ElfFileImpl, typename ptr_t>
472bool PatchOat::CheckOatFile(ElfFileImpl* oat_file) {
473  auto patches_sec = oat_file->FindSectionByName(".oat_patches");
474  if (patches_sec->sh_type != SHT_OAT_PATCH) {
475    return false;
476  }
477  ptr_t* patches = reinterpret_cast<ptr_t*>(oat_file->Begin() + patches_sec->sh_offset);
478  ptr_t* patches_end = patches + (patches_sec->sh_size / sizeof(ptr_t));
479  auto oat_data_sec = oat_file->FindSectionByName(".rodata");
480  auto oat_text_sec = oat_file->FindSectionByName(".text");
481  if (oat_data_sec == nullptr) {
482    return false;
483  }
484  if (oat_text_sec == nullptr) {
485    return false;
486  }
487  if (oat_text_sec->sh_offset <= oat_data_sec->sh_offset) {
488    return false;
489  }
490
491  for (; patches < patches_end; patches++) {
492    if (oat_text_sec->sh_size <= *patches) {
493      return false;
494    }
495  }
496
497  return true;
498}
499
500template <typename ElfFileImpl>
501bool PatchOat::PatchOatHeader(ElfFileImpl* oat_file) {
502  auto rodata_sec = oat_file->FindSectionByName(".rodata");
503  if (rodata_sec == nullptr) {
504    return false;
505  }
506  OatHeader* oat_header = reinterpret_cast<OatHeader*>(oat_file->Begin() + rodata_sec->sh_offset);
507  if (!oat_header->IsValid()) {
508    LOG(ERROR) << "Elf file " << oat_file->GetFile().GetPath() << " has an invalid oat header";
509    return false;
510  }
511  oat_header->RelocateOat(delta_);
512  return true;
513}
514
515bool PatchOat::PatchElf() {
516  if (oat_file_->is_elf64_)
517    return PatchElf<ElfFileImpl64>(oat_file_->GetImpl64());
518  else
519    return PatchElf<ElfFileImpl32>(oat_file_->GetImpl32());
520}
521
522template <typename ElfFileImpl>
523bool PatchOat::PatchElf(ElfFileImpl* oat_file) {
524  TimingLogger::ScopedTiming t("Fixup Elf Text Section", timings_);
525  if (!PatchTextSection<ElfFileImpl>(oat_file)) {
526    return false;
527  }
528
529  if (!PatchOatHeader<ElfFileImpl>(oat_file)) {
530    return false;
531  }
532
533  bool need_fixup = false;
534  for (unsigned int i = 0; i < oat_file->GetProgramHeaderNum(); i++) {
535    auto hdr = oat_file->GetProgramHeader(i);
536    if (hdr->p_vaddr != 0 && hdr->p_vaddr != hdr->p_offset) {
537      need_fixup = true;
538    }
539    if (hdr->p_paddr != 0 && hdr->p_paddr != hdr->p_offset) {
540      need_fixup = true;
541    }
542  }
543  if (!need_fixup) {
544    // This was never passed through ElfFixup so all headers/symbols just have their offset as
545    // their addr. Therefore we do not need to update these parts.
546    return true;
547  }
548
549  t.NewTiming("Fixup Elf Headers");
550  // Fixup Phdr's
551  oat_file->FixupProgramHeaders(delta_);
552
553  t.NewTiming("Fixup Section Headers");
554  // Fixup Shdr's
555  oat_file->FixupSectionHeaders(delta_);
556
557  t.NewTiming("Fixup Dynamics");
558  oat_file->FixupDynamic(delta_);
559
560  t.NewTiming("Fixup Elf Symbols");
561  // Fixup dynsym
562  if (!oat_file->FixupSymbols(delta_, true)) {
563    return false;
564  }
565  // Fixup symtab
566  if (!oat_file->FixupSymbols(delta_, false)) {
567    return false;
568  }
569
570  t.NewTiming("Fixup Debug Sections");
571  if (!oat_file->FixupDebugSections(delta_)) {
572    return false;
573  }
574
575  return true;
576}
577
578template <typename ElfFileImpl>
579bool PatchOat::PatchTextSection(ElfFileImpl* oat_file) {
580  auto patches_sec = oat_file->FindSectionByName(".oat_patches");
581  if (patches_sec == nullptr) {
582    LOG(ERROR) << ".oat_patches section not found. Aborting patch";
583    return false;
584  }
585  if (patches_sec->sh_type != SHT_OAT_PATCH) {
586    LOG(ERROR) << "Unexpected type of .oat_patches";
587    return false;
588  }
589
590  switch (patches_sec->sh_entsize) {
591    case sizeof(uint32_t):
592      return PatchTextSection<ElfFileImpl, uint32_t>(oat_file);
593    case sizeof(uint64_t):
594      return PatchTextSection<ElfFileImpl, uint64_t>(oat_file);
595    default:
596      LOG(ERROR) << ".oat_patches Entsize of " << patches_sec->sh_entsize << "bits "
597                 << "is not valid";
598      return false;
599  }
600}
601
602template <typename ElfFileImpl, typename patch_loc_t>
603bool PatchOat::PatchTextSection(ElfFileImpl* oat_file) {
604  bool oat_file_valid = CheckOatFile<ElfFileImpl, patch_loc_t>(oat_file);
605  CHECK(oat_file_valid) << "Oat file invalid";
606  auto patches_sec = oat_file->FindSectionByName(".oat_patches");
607  patch_loc_t* patches = reinterpret_cast<patch_loc_t*>(oat_file->Begin() + patches_sec->sh_offset);
608  patch_loc_t* patches_end = patches + (patches_sec->sh_size / sizeof(patch_loc_t));
609  auto oat_text_sec = oat_file->FindSectionByName(".text");
610  CHECK(oat_text_sec != nullptr);
611  byte* to_patch = oat_file->Begin() + oat_text_sec->sh_offset;
612  uintptr_t to_patch_end = reinterpret_cast<uintptr_t>(to_patch) + oat_text_sec->sh_size;
613
614  for (; patches < patches_end; patches++) {
615    CHECK_LT(*patches, oat_text_sec->sh_size) << "Bad Patch";
616    uint32_t* patch_loc = reinterpret_cast<uint32_t*>(to_patch + *patches);
617    CHECK_LT(reinterpret_cast<uintptr_t>(patch_loc), to_patch_end);
618    *patch_loc += delta_;
619  }
620  return true;
621}
622
623static int orig_argc;
624static char** orig_argv;
625
626static std::string CommandLine() {
627  std::vector<std::string> command;
628  for (int i = 0; i < orig_argc; ++i) {
629    command.push_back(orig_argv[i]);
630  }
631  return Join(command, ' ');
632}
633
634static void UsageErrorV(const char* fmt, va_list ap) {
635  std::string error;
636  StringAppendV(&error, fmt, ap);
637  LOG(ERROR) << error;
638}
639
640static void UsageError(const char* fmt, ...) {
641  va_list ap;
642  va_start(ap, fmt);
643  UsageErrorV(fmt, ap);
644  va_end(ap);
645}
646
647static void Usage(const char *fmt, ...) {
648  va_list ap;
649  va_start(ap, fmt);
650  UsageErrorV(fmt, ap);
651  va_end(ap);
652
653  UsageError("Command: %s", CommandLine().c_str());
654  UsageError("Usage: patchoat [options]...");
655  UsageError("");
656  UsageError("  --instruction-set=<isa>: Specifies the instruction set the patched code is");
657  UsageError("      compiled for. Required if you use --input-oat-location");
658  UsageError("");
659  UsageError("  --input-oat-file=<file.oat>: Specifies the exact filename of the oat file to be");
660  UsageError("      patched.");
661  UsageError("");
662  UsageError("  --input-oat-fd=<file-descriptor>: Specifies the file-descriptor of the oat file");
663  UsageError("      to be patched.");
664  UsageError("");
665  UsageError("  --input-oat-location=<file.oat>: Specifies the 'location' to read the patched");
666  UsageError("      oat file from. If used one must also supply the --instruction-set");
667  UsageError("");
668  UsageError("  --input-image-location=<file.art>: Specifies the 'location' of the image file to");
669  UsageError("      be patched. If --instruction-set is not given it will use the instruction set");
670  UsageError("      extracted from the --input-oat-file.");
671  UsageError("");
672  UsageError("  --output-oat-file=<file.oat>: Specifies the exact file to write the patched oat");
673  UsageError("      file to.");
674  UsageError("");
675  UsageError("  --output-oat-fd=<file-descriptor>: Specifies the file-descriptor to write the");
676  UsageError("      the patched oat file to.");
677  UsageError("");
678  UsageError("  --output-image-file=<file.art>: Specifies the exact file to write the patched");
679  UsageError("      image file to.");
680  UsageError("");
681  UsageError("  --output-image-fd=<file-descriptor>: Specifies the file-descriptor to write the");
682  UsageError("      the patched image file to.");
683  UsageError("");
684  UsageError("  --orig-base-offset=<original-base-offset>: Specify the base offset the input file");
685  UsageError("      was compiled with. This is needed if one is specifying a --base-offset");
686  UsageError("");
687  UsageError("  --base-offset=<new-base-offset>: Specify the base offset we will repatch the");
688  UsageError("      given files to use. This requires that --orig-base-offset is also given.");
689  UsageError("");
690  UsageError("  --base-offset-delta=<delta>: Specify the amount to change the old base-offset by.");
691  UsageError("      This value may be negative.");
692  UsageError("");
693  UsageError("  --patched-image-file=<file.art>: Use the same patch delta as was used to patch");
694  UsageError("      the given image file.");
695  UsageError("");
696  UsageError("  --patched-image-location=<file.art>: Use the same patch delta as was used to");
697  UsageError("      patch the given image location. If used one must also specify the");
698  UsageError("      --instruction-set flag. It will search for this image in the same way that");
699  UsageError("      is done when loading one.");
700  UsageError("");
701  UsageError("  --lock-output: Obtain a flock on output oat file before starting.");
702  UsageError("");
703  UsageError("  --no-lock-output: Do not attempt to obtain a flock on output oat file.");
704  UsageError("");
705  UsageError("  --dump-timings: dump out patch timing information");
706  UsageError("");
707  UsageError("  --no-dump-timings: do not dump out patch timing information");
708  UsageError("");
709
710  exit(EXIT_FAILURE);
711}
712
713static bool ReadBaseDelta(const char* name, off_t* delta, std::string* error_msg) {
714  CHECK(name != nullptr);
715  CHECK(delta != nullptr);
716  std::unique_ptr<File> file;
717  if (OS::FileExists(name)) {
718    file.reset(OS::OpenFileForReading(name));
719    if (file.get() == nullptr) {
720      *error_msg = "Failed to open file %s for reading";
721      return false;
722    }
723  } else {
724    *error_msg = "File %s does not exist";
725    return false;
726  }
727  CHECK(file.get() != nullptr);
728  ImageHeader hdr;
729  if (sizeof(hdr) != file->Read(reinterpret_cast<char*>(&hdr), sizeof(hdr), 0)) {
730    *error_msg = "Failed to read file %s";
731    return false;
732  }
733  if (!hdr.IsValid()) {
734    *error_msg = "%s does not contain a valid image header.";
735    return false;
736  }
737  *delta = hdr.GetPatchDelta();
738  return true;
739}
740
741static File* CreateOrOpen(const char* name, bool* created) {
742  if (OS::FileExists(name)) {
743    *created = false;
744    return OS::OpenFileReadWrite(name);
745  } else {
746    *created = true;
747    std::unique_ptr<File> f(OS::CreateEmptyFile(name));
748    if (f.get() != nullptr) {
749      if (fchmod(f->Fd(), 0644) != 0) {
750        PLOG(ERROR) << "Unable to make " << name << " world readable";
751        TEMP_FAILURE_RETRY(unlink(name));
752        return nullptr;
753      }
754    }
755    return f.release();
756  }
757}
758
759static int patchoat(int argc, char **argv) {
760  InitLogging(argv);
761  const bool debug = kIsDebugBuild;
762  orig_argc = argc;
763  orig_argv = argv;
764  TimingLogger timings("patcher", false, false);
765
766  InitLogging(argv);
767
768  // Skip over the command name.
769  argv++;
770  argc--;
771
772  if (argc == 0) {
773    Usage("No arguments specified");
774  }
775
776  timings.StartTiming("Patchoat");
777
778  // cmd line args
779  bool isa_set = false;
780  InstructionSet isa = kNone;
781  std::string input_oat_filename;
782  std::string input_oat_location;
783  int input_oat_fd = -1;
784  bool have_input_oat = false;
785  std::string input_image_location;
786  std::string output_oat_filename;
787  int output_oat_fd = -1;
788  bool have_output_oat = false;
789  std::string output_image_filename;
790  int output_image_fd = -1;
791  bool have_output_image = false;
792  uintptr_t base_offset = 0;
793  bool base_offset_set = false;
794  uintptr_t orig_base_offset = 0;
795  bool orig_base_offset_set = false;
796  off_t base_delta = 0;
797  bool base_delta_set = false;
798  std::string patched_image_filename;
799  std::string patched_image_location;
800  bool dump_timings = kIsDebugBuild;
801  bool lock_output = true;
802
803  for (int i = 0; i < argc; i++) {
804    const StringPiece option(argv[i]);
805    const bool log_options = false;
806    if (log_options) {
807      LOG(INFO) << "patchoat: option[" << i << "]=" << argv[i];
808    }
809    if (option.starts_with("--instruction-set=")) {
810      isa_set = true;
811      const char* isa_str = option.substr(strlen("--instruction-set=")).data();
812      isa = GetInstructionSetFromString(isa_str);
813      if (isa == kNone) {
814        Usage("Unknown or invalid instruction set %s", isa_str);
815      }
816    } else if (option.starts_with("--input-oat-location=")) {
817      if (have_input_oat) {
818        Usage("Only one of --input-oat-file, --input-oat-location and --input-oat-fd may be used.");
819      }
820      have_input_oat = true;
821      input_oat_location = option.substr(strlen("--input-oat-location=")).data();
822    } else if (option.starts_with("--input-oat-file=")) {
823      if (have_input_oat) {
824        Usage("Only one of --input-oat-file, --input-oat-location and --input-oat-fd may be used.");
825      }
826      have_input_oat = true;
827      input_oat_filename = option.substr(strlen("--input-oat-file=")).data();
828    } else if (option.starts_with("--input-oat-fd=")) {
829      if (have_input_oat) {
830        Usage("Only one of --input-oat-file, --input-oat-location and --input-oat-fd may be used.");
831      }
832      have_input_oat = true;
833      const char* oat_fd_str = option.substr(strlen("--input-oat-fd=")).data();
834      if (!ParseInt(oat_fd_str, &input_oat_fd)) {
835        Usage("Failed to parse --input-oat-fd argument '%s' as an integer", oat_fd_str);
836      }
837      if (input_oat_fd < 0) {
838        Usage("--input-oat-fd pass a negative value %d", input_oat_fd);
839      }
840    } else if (option.starts_with("--input-image-location=")) {
841      input_image_location = option.substr(strlen("--input-image-location=")).data();
842    } else if (option.starts_with("--output-oat-file=")) {
843      if (have_output_oat) {
844        Usage("Only one of --output-oat-file, and --output-oat-fd may be used.");
845      }
846      have_output_oat = true;
847      output_oat_filename = option.substr(strlen("--output-oat-file=")).data();
848    } else if (option.starts_with("--output-oat-fd=")) {
849      if (have_output_oat) {
850        Usage("Only one of --output-oat-file, --output-oat-fd may be used.");
851      }
852      have_output_oat = true;
853      const char* oat_fd_str = option.substr(strlen("--output-oat-fd=")).data();
854      if (!ParseInt(oat_fd_str, &output_oat_fd)) {
855        Usage("Failed to parse --output-oat-fd argument '%s' as an integer", oat_fd_str);
856      }
857      if (output_oat_fd < 0) {
858        Usage("--output-oat-fd pass a negative value %d", output_oat_fd);
859      }
860    } else if (option.starts_with("--output-image-file=")) {
861      if (have_output_image) {
862        Usage("Only one of --output-image-file, and --output-image-fd may be used.");
863      }
864      have_output_image = true;
865      output_image_filename = option.substr(strlen("--output-image-file=")).data();
866    } else if (option.starts_with("--output-image-fd=")) {
867      if (have_output_image) {
868        Usage("Only one of --output-image-file, and --output-image-fd may be used.");
869      }
870      have_output_image = true;
871      const char* image_fd_str = option.substr(strlen("--output-image-fd=")).data();
872      if (!ParseInt(image_fd_str, &output_image_fd)) {
873        Usage("Failed to parse --output-image-fd argument '%s' as an integer", image_fd_str);
874      }
875      if (output_image_fd < 0) {
876        Usage("--output-image-fd pass a negative value %d", output_image_fd);
877      }
878    } else if (option.starts_with("--orig-base-offset=")) {
879      const char* orig_base_offset_str = option.substr(strlen("--orig-base-offset=")).data();
880      orig_base_offset_set = true;
881      if (!ParseUint(orig_base_offset_str, &orig_base_offset)) {
882        Usage("Failed to parse --orig-base-offset argument '%s' as an uintptr_t",
883              orig_base_offset_str);
884      }
885    } else if (option.starts_with("--base-offset=")) {
886      const char* base_offset_str = option.substr(strlen("--base-offset=")).data();
887      base_offset_set = true;
888      if (!ParseUint(base_offset_str, &base_offset)) {
889        Usage("Failed to parse --base-offset argument '%s' as an uintptr_t", base_offset_str);
890      }
891    } else if (option.starts_with("--base-offset-delta=")) {
892      const char* base_delta_str = option.substr(strlen("--base-offset-delta=")).data();
893      base_delta_set = true;
894      if (!ParseInt(base_delta_str, &base_delta)) {
895        Usage("Failed to parse --base-offset-delta argument '%s' as an off_t", base_delta_str);
896      }
897    } else if (option.starts_with("--patched-image-location=")) {
898      patched_image_location = option.substr(strlen("--patched-image-location=")).data();
899    } else if (option.starts_with("--patched-image-file=")) {
900      patched_image_filename = option.substr(strlen("--patched-image-file=")).data();
901    } else if (option == "--lock-output") {
902      lock_output = true;
903    } else if (option == "--no-lock-output") {
904      lock_output = false;
905    } else if (option == "--dump-timings") {
906      dump_timings = true;
907    } else if (option == "--no-dump-timings") {
908      dump_timings = false;
909    } else {
910      Usage("Unknown argument %s", option.data());
911    }
912  }
913
914  {
915    // Only 1 of these may be set.
916    uint32_t cnt = 0;
917    cnt += (base_delta_set) ? 1 : 0;
918    cnt += (base_offset_set && orig_base_offset_set) ? 1 : 0;
919    cnt += (!patched_image_filename.empty()) ? 1 : 0;
920    cnt += (!patched_image_location.empty()) ? 1 : 0;
921    if (cnt > 1) {
922      Usage("Only one of --base-offset/--orig-base-offset, --base-offset-delta, "
923            "--patched-image-filename or --patched-image-location may be used.");
924    } else if (cnt == 0) {
925      Usage("Must specify --base-offset-delta, --base-offset and --orig-base-offset, "
926            "--patched-image-location or --patched-image-file");
927    }
928  }
929
930  if (have_input_oat != have_output_oat) {
931    Usage("Either both input and output oat must be supplied or niether must be.");
932  }
933
934  if ((!input_image_location.empty()) != have_output_image) {
935    Usage("Either both input and output image must be supplied or niether must be.");
936  }
937
938  // We know we have both the input and output so rename for clarity.
939  bool have_image_files = have_output_image;
940  bool have_oat_files = have_output_oat;
941
942  if (!have_oat_files && !have_image_files) {
943    Usage("Must be patching either an oat or an image file or both.");
944  }
945
946  if (!have_oat_files && !isa_set) {
947    Usage("Must include ISA if patching an image file without an oat file.");
948  }
949
950  if (!input_oat_location.empty()) {
951    if (!isa_set) {
952      Usage("specifying a location requires specifying an instruction set");
953    }
954    if (!LocationToFilename(input_oat_location, isa, &input_oat_filename)) {
955      Usage("Unable to find filename for input oat location %s", input_oat_location.c_str());
956    }
957    if (debug) {
958      LOG(INFO) << "Using input-oat-file " << input_oat_filename;
959    }
960  }
961  if (!patched_image_location.empty()) {
962    if (!isa_set) {
963      Usage("specifying a location requires specifying an instruction set");
964    }
965    std::string system_filename;
966    bool has_system = false;
967    std::string cache_filename;
968    bool has_cache = false;
969    bool has_android_data_unused = false;
970    bool is_global_cache = false;
971    if (!gc::space::ImageSpace::FindImageFilename(patched_image_location.c_str(), isa,
972                                                  &system_filename, &has_system, &cache_filename,
973                                                  &has_android_data_unused, &has_cache,
974                                                  &is_global_cache)) {
975      Usage("Unable to determine image file for location %s", patched_image_location.c_str());
976    }
977    if (has_cache) {
978      patched_image_filename = cache_filename;
979    } else if (has_system) {
980      LOG(WARNING) << "Only image file found was in /system for image location "
981                   << patched_image_location;
982      patched_image_filename = system_filename;
983    } else {
984      Usage("Unable to determine image file for location %s", patched_image_location.c_str());
985    }
986    if (debug) {
987      LOG(INFO) << "Using patched-image-file " << patched_image_filename;
988    }
989  }
990
991  if (!base_delta_set) {
992    if (orig_base_offset_set && base_offset_set) {
993      base_delta_set = true;
994      base_delta = base_offset - orig_base_offset;
995    } else if (!patched_image_filename.empty()) {
996      base_delta_set = true;
997      std::string error_msg;
998      if (!ReadBaseDelta(patched_image_filename.c_str(), &base_delta, &error_msg)) {
999        Usage(error_msg.c_str(), patched_image_filename.c_str());
1000      }
1001    } else {
1002      if (base_offset_set) {
1003        Usage("Unable to determine original base offset.");
1004      } else {
1005        Usage("Must supply a desired new offset or delta.");
1006      }
1007    }
1008  }
1009
1010  if (!IsAligned<kPageSize>(base_delta)) {
1011    Usage("Base offset/delta must be alligned to a pagesize (0x%08x) boundary.", kPageSize);
1012  }
1013
1014  // Do we need to cleanup output files if we fail?
1015  bool new_image_out = false;
1016  bool new_oat_out = false;
1017
1018  std::unique_ptr<File> input_oat;
1019  std::unique_ptr<File> output_oat;
1020  std::unique_ptr<File> output_image;
1021
1022  if (have_image_files) {
1023    CHECK(!input_image_location.empty());
1024
1025    if (output_image_fd != -1) {
1026      if (output_image_filename.empty()) {
1027        output_image_filename = "output-image-file";
1028      }
1029      output_image.reset(new File(output_image_fd, output_image_filename));
1030    } else {
1031      CHECK(!output_image_filename.empty());
1032      output_image.reset(CreateOrOpen(output_image_filename.c_str(), &new_image_out));
1033    }
1034  } else {
1035    CHECK(output_image_filename.empty() && output_image_fd == -1 && input_image_location.empty());
1036  }
1037
1038  if (have_oat_files) {
1039    if (input_oat_fd != -1) {
1040      if (input_oat_filename.empty()) {
1041        input_oat_filename = "input-oat-file";
1042      }
1043      input_oat.reset(new File(input_oat_fd, input_oat_filename));
1044    } else {
1045      CHECK(!input_oat_filename.empty());
1046      input_oat.reset(OS::OpenFileForReading(input_oat_filename.c_str()));
1047      if (input_oat.get() == nullptr) {
1048        LOG(ERROR) << "Could not open input oat file: " << strerror(errno);
1049      }
1050    }
1051
1052    if (output_oat_fd != -1) {
1053      if (output_oat_filename.empty()) {
1054        output_oat_filename = "output-oat-file";
1055      }
1056      output_oat.reset(new File(output_oat_fd, output_oat_filename));
1057    } else {
1058      CHECK(!output_oat_filename.empty());
1059      output_oat.reset(CreateOrOpen(output_oat_filename.c_str(), &new_oat_out));
1060    }
1061  }
1062
1063  auto cleanup = [&output_image_filename, &output_oat_filename,
1064                  &new_oat_out, &new_image_out, &timings, &dump_timings](bool success) {
1065    timings.EndTiming();
1066    if (!success) {
1067      if (new_oat_out) {
1068        CHECK(!output_oat_filename.empty());
1069        TEMP_FAILURE_RETRY(unlink(output_oat_filename.c_str()));
1070      }
1071      if (new_image_out) {
1072        CHECK(!output_image_filename.empty());
1073        TEMP_FAILURE_RETRY(unlink(output_image_filename.c_str()));
1074      }
1075    }
1076    if (dump_timings) {
1077      LOG(INFO) << Dumpable<TimingLogger>(timings);
1078    }
1079  };
1080
1081  if ((have_oat_files && (input_oat.get() == nullptr || output_oat.get() == nullptr)) ||
1082      (have_image_files && output_image.get() == nullptr)) {
1083    cleanup(false);
1084    return EXIT_FAILURE;
1085  }
1086
1087  ScopedFlock output_oat_lock;
1088  if (lock_output) {
1089    std::string error_msg;
1090    if (have_oat_files && !output_oat_lock.Init(output_oat.get(), &error_msg)) {
1091      LOG(ERROR) << "Unable to lock output oat " << output_image->GetPath() << ": " << error_msg;
1092      cleanup(false);
1093      return EXIT_FAILURE;
1094    }
1095  }
1096
1097  if (debug) {
1098    LOG(INFO) << "moving offset by " << base_delta
1099              << " (0x" << std::hex << base_delta << ") bytes or "
1100              << std::dec << (base_delta/kPageSize) << " pages.";
1101  }
1102
1103  bool ret;
1104  if (have_image_files && have_oat_files) {
1105    TimingLogger::ScopedTiming pt("patch image and oat", &timings);
1106    ret = PatchOat::Patch(input_oat.get(), input_image_location, base_delta,
1107                          output_oat.get(), output_image.get(), isa, &timings);
1108  } else if (have_oat_files) {
1109    TimingLogger::ScopedTiming pt("patch oat", &timings);
1110    ret = PatchOat::Patch(input_oat.get(), base_delta, output_oat.get(), &timings);
1111  } else {
1112    TimingLogger::ScopedTiming pt("patch image", &timings);
1113    CHECK(have_image_files);
1114    ret = PatchOat::Patch(input_image_location, base_delta, output_image.get(), isa, &timings);
1115  }
1116  cleanup(ret);
1117  return (ret) ? EXIT_SUCCESS : EXIT_FAILURE;
1118}
1119
1120}  // namespace art
1121
1122int main(int argc, char **argv) {
1123  return art::patchoat(argc, argv);
1124}
1125