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