patchoat.cc revision 647b1a86f518d8db0331b3d52a96392b7a62504b
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<uint8_t*>(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  uint8_t* 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, ...) NO_RETURN;
648static void Usage(const char *fmt, ...) {
649  va_list ap;
650  va_start(ap, fmt);
651  UsageErrorV(fmt, ap);
652  va_end(ap);
653
654  UsageError("Command: %s", CommandLine().c_str());
655  UsageError("Usage: patchoat [options]...");
656  UsageError("");
657  UsageError("  --instruction-set=<isa>: Specifies the instruction set the patched code is");
658  UsageError("      compiled for. Required if you use --input-oat-location");
659  UsageError("");
660  UsageError("  --input-oat-file=<file.oat>: Specifies the exact filename of the oat file to be");
661  UsageError("      patched.");
662  UsageError("");
663  UsageError("  --input-oat-fd=<file-descriptor>: Specifies the file-descriptor of the oat file");
664  UsageError("      to be patched.");
665  UsageError("");
666  UsageError("  --input-oat-location=<file.oat>: Specifies the 'location' to read the patched");
667  UsageError("      oat file from. If used one must also supply the --instruction-set");
668  UsageError("");
669  UsageError("  --input-image-location=<file.art>: Specifies the 'location' of the image file to");
670  UsageError("      be patched. If --instruction-set is not given it will use the instruction set");
671  UsageError("      extracted from the --input-oat-file.");
672  UsageError("");
673  UsageError("  --output-oat-file=<file.oat>: Specifies the exact file to write the patched oat");
674  UsageError("      file to.");
675  UsageError("");
676  UsageError("  --output-oat-fd=<file-descriptor>: Specifies the file-descriptor to write the");
677  UsageError("      the patched oat file to.");
678  UsageError("");
679  UsageError("  --output-image-file=<file.art>: Specifies the exact file to write the patched");
680  UsageError("      image file to.");
681  UsageError("");
682  UsageError("  --output-image-fd=<file-descriptor>: Specifies the file-descriptor to write the");
683  UsageError("      the patched image file to.");
684  UsageError("");
685  UsageError("  --orig-base-offset=<original-base-offset>: Specify the base offset the input file");
686  UsageError("      was compiled with. This is needed if one is specifying a --base-offset");
687  UsageError("");
688  UsageError("  --base-offset=<new-base-offset>: Specify the base offset we will repatch the");
689  UsageError("      given files to use. This requires that --orig-base-offset is also given.");
690  UsageError("");
691  UsageError("  --base-offset-delta=<delta>: Specify the amount to change the old base-offset by.");
692  UsageError("      This value may be negative.");
693  UsageError("");
694  UsageError("  --patched-image-file=<file.art>: Use the same patch delta as was used to patch");
695  UsageError("      the given image file.");
696  UsageError("");
697  UsageError("  --patched-image-location=<file.art>: Use the same patch delta as was used to");
698  UsageError("      patch the given image location. If used one must also specify the");
699  UsageError("      --instruction-set flag. It will search for this image in the same way that");
700  UsageError("      is done when loading one.");
701  UsageError("");
702  UsageError("  --lock-output: Obtain a flock on output oat file before starting.");
703  UsageError("");
704  UsageError("  --no-lock-output: Do not attempt to obtain a flock on output oat file.");
705  UsageError("");
706  UsageError("  --dump-timings: dump out patch timing information");
707  UsageError("");
708  UsageError("  --no-dump-timings: do not dump out patch timing information");
709  UsageError("");
710
711  exit(EXIT_FAILURE);
712}
713
714static bool ReadBaseDelta(const char* name, off_t* delta, std::string* error_msg) {
715  CHECK(name != nullptr);
716  CHECK(delta != nullptr);
717  std::unique_ptr<File> file;
718  if (OS::FileExists(name)) {
719    file.reset(OS::OpenFileForReading(name));
720    if (file.get() == nullptr) {
721      *error_msg = "Failed to open file %s for reading";
722      return false;
723    }
724  } else {
725    *error_msg = "File %s does not exist";
726    return false;
727  }
728  CHECK(file.get() != nullptr);
729  ImageHeader hdr;
730  if (sizeof(hdr) != file->Read(reinterpret_cast<char*>(&hdr), sizeof(hdr), 0)) {
731    *error_msg = "Failed to read file %s";
732    return false;
733  }
734  if (!hdr.IsValid()) {
735    *error_msg = "%s does not contain a valid image header.";
736    return false;
737  }
738  *delta = hdr.GetPatchDelta();
739  return true;
740}
741
742static File* CreateOrOpen(const char* name, bool* created) {
743  if (OS::FileExists(name)) {
744    *created = false;
745    return OS::OpenFileReadWrite(name);
746  } else {
747    *created = true;
748    std::unique_ptr<File> f(OS::CreateEmptyFile(name));
749    if (f.get() != nullptr) {
750      if (fchmod(f->Fd(), 0644) != 0) {
751        PLOG(ERROR) << "Unable to make " << name << " world readable";
752        TEMP_FAILURE_RETRY(unlink(name));
753        return nullptr;
754      }
755    }
756    return f.release();
757  }
758}
759
760static int patchoat(int argc, char **argv) {
761  InitLogging(argv);
762  const bool debug = kIsDebugBuild;
763  orig_argc = argc;
764  orig_argv = argv;
765  TimingLogger timings("patcher", false, false);
766
767  InitLogging(argv);
768
769  // Skip over the command name.
770  argv++;
771  argc--;
772
773  if (argc == 0) {
774    Usage("No arguments specified");
775  }
776
777  timings.StartTiming("Patchoat");
778
779  // cmd line args
780  bool isa_set = false;
781  InstructionSet isa = kNone;
782  std::string input_oat_filename;
783  std::string input_oat_location;
784  int input_oat_fd = -1;
785  bool have_input_oat = false;
786  std::string input_image_location;
787  std::string output_oat_filename;
788  int output_oat_fd = -1;
789  bool have_output_oat = false;
790  std::string output_image_filename;
791  int output_image_fd = -1;
792  bool have_output_image = false;
793  uintptr_t base_offset = 0;
794  bool base_offset_set = false;
795  uintptr_t orig_base_offset = 0;
796  bool orig_base_offset_set = false;
797  off_t base_delta = 0;
798  bool base_delta_set = false;
799  std::string patched_image_filename;
800  std::string patched_image_location;
801  bool dump_timings = kIsDebugBuild;
802  bool lock_output = true;
803
804  for (int i = 0; i < argc; i++) {
805    const StringPiece option(argv[i]);
806    const bool log_options = false;
807    if (log_options) {
808      LOG(INFO) << "patchoat: option[" << i << "]=" << argv[i];
809    }
810    if (option.starts_with("--instruction-set=")) {
811      isa_set = true;
812      const char* isa_str = option.substr(strlen("--instruction-set=")).data();
813      isa = GetInstructionSetFromString(isa_str);
814      if (isa == kNone) {
815        Usage("Unknown or invalid instruction set %s", isa_str);
816      }
817    } else if (option.starts_with("--input-oat-location=")) {
818      if (have_input_oat) {
819        Usage("Only one of --input-oat-file, --input-oat-location and --input-oat-fd may be used.");
820      }
821      have_input_oat = true;
822      input_oat_location = option.substr(strlen("--input-oat-location=")).data();
823    } else if (option.starts_with("--input-oat-file=")) {
824      if (have_input_oat) {
825        Usage("Only one of --input-oat-file, --input-oat-location and --input-oat-fd may be used.");
826      }
827      have_input_oat = true;
828      input_oat_filename = option.substr(strlen("--input-oat-file=")).data();
829    } else if (option.starts_with("--input-oat-fd=")) {
830      if (have_input_oat) {
831        Usage("Only one of --input-oat-file, --input-oat-location and --input-oat-fd may be used.");
832      }
833      have_input_oat = true;
834      const char* oat_fd_str = option.substr(strlen("--input-oat-fd=")).data();
835      if (!ParseInt(oat_fd_str, &input_oat_fd)) {
836        Usage("Failed to parse --input-oat-fd argument '%s' as an integer", oat_fd_str);
837      }
838      if (input_oat_fd < 0) {
839        Usage("--input-oat-fd pass a negative value %d", input_oat_fd);
840      }
841    } else if (option.starts_with("--input-image-location=")) {
842      input_image_location = option.substr(strlen("--input-image-location=")).data();
843    } else if (option.starts_with("--output-oat-file=")) {
844      if (have_output_oat) {
845        Usage("Only one of --output-oat-file, and --output-oat-fd may be used.");
846      }
847      have_output_oat = true;
848      output_oat_filename = option.substr(strlen("--output-oat-file=")).data();
849    } else if (option.starts_with("--output-oat-fd=")) {
850      if (have_output_oat) {
851        Usage("Only one of --output-oat-file, --output-oat-fd may be used.");
852      }
853      have_output_oat = true;
854      const char* oat_fd_str = option.substr(strlen("--output-oat-fd=")).data();
855      if (!ParseInt(oat_fd_str, &output_oat_fd)) {
856        Usage("Failed to parse --output-oat-fd argument '%s' as an integer", oat_fd_str);
857      }
858      if (output_oat_fd < 0) {
859        Usage("--output-oat-fd pass a negative value %d", output_oat_fd);
860      }
861    } else if (option.starts_with("--output-image-file=")) {
862      if (have_output_image) {
863        Usage("Only one of --output-image-file, and --output-image-fd may be used.");
864      }
865      have_output_image = true;
866      output_image_filename = option.substr(strlen("--output-image-file=")).data();
867    } else if (option.starts_with("--output-image-fd=")) {
868      if (have_output_image) {
869        Usage("Only one of --output-image-file, and --output-image-fd may be used.");
870      }
871      have_output_image = true;
872      const char* image_fd_str = option.substr(strlen("--output-image-fd=")).data();
873      if (!ParseInt(image_fd_str, &output_image_fd)) {
874        Usage("Failed to parse --output-image-fd argument '%s' as an integer", image_fd_str);
875      }
876      if (output_image_fd < 0) {
877        Usage("--output-image-fd pass a negative value %d", output_image_fd);
878      }
879    } else if (option.starts_with("--orig-base-offset=")) {
880      const char* orig_base_offset_str = option.substr(strlen("--orig-base-offset=")).data();
881      orig_base_offset_set = true;
882      if (!ParseUint(orig_base_offset_str, &orig_base_offset)) {
883        Usage("Failed to parse --orig-base-offset argument '%s' as an uintptr_t",
884              orig_base_offset_str);
885      }
886    } else if (option.starts_with("--base-offset=")) {
887      const char* base_offset_str = option.substr(strlen("--base-offset=")).data();
888      base_offset_set = true;
889      if (!ParseUint(base_offset_str, &base_offset)) {
890        Usage("Failed to parse --base-offset argument '%s' as an uintptr_t", base_offset_str);
891      }
892    } else if (option.starts_with("--base-offset-delta=")) {
893      const char* base_delta_str = option.substr(strlen("--base-offset-delta=")).data();
894      base_delta_set = true;
895      if (!ParseInt(base_delta_str, &base_delta)) {
896        Usage("Failed to parse --base-offset-delta argument '%s' as an off_t", base_delta_str);
897      }
898    } else if (option.starts_with("--patched-image-location=")) {
899      patched_image_location = option.substr(strlen("--patched-image-location=")).data();
900    } else if (option.starts_with("--patched-image-file=")) {
901      patched_image_filename = option.substr(strlen("--patched-image-file=")).data();
902    } else if (option == "--lock-output") {
903      lock_output = true;
904    } else if (option == "--no-lock-output") {
905      lock_output = false;
906    } else if (option == "--dump-timings") {
907      dump_timings = true;
908    } else if (option == "--no-dump-timings") {
909      dump_timings = false;
910    } else {
911      Usage("Unknown argument %s", option.data());
912    }
913  }
914
915  {
916    // Only 1 of these may be set.
917    uint32_t cnt = 0;
918    cnt += (base_delta_set) ? 1 : 0;
919    cnt += (base_offset_set && orig_base_offset_set) ? 1 : 0;
920    cnt += (!patched_image_filename.empty()) ? 1 : 0;
921    cnt += (!patched_image_location.empty()) ? 1 : 0;
922    if (cnt > 1) {
923      Usage("Only one of --base-offset/--orig-base-offset, --base-offset-delta, "
924            "--patched-image-filename or --patched-image-location may be used.");
925    } else if (cnt == 0) {
926      Usage("Must specify --base-offset-delta, --base-offset and --orig-base-offset, "
927            "--patched-image-location or --patched-image-file");
928    }
929  }
930
931  if (have_input_oat != have_output_oat) {
932    Usage("Either both input and output oat must be supplied or niether must be.");
933  }
934
935  if ((!input_image_location.empty()) != have_output_image) {
936    Usage("Either both input and output image must be supplied or niether must be.");
937  }
938
939  // We know we have both the input and output so rename for clarity.
940  bool have_image_files = have_output_image;
941  bool have_oat_files = have_output_oat;
942
943  if (!have_oat_files && !have_image_files) {
944    Usage("Must be patching either an oat or an image file or both.");
945  }
946
947  if (!have_oat_files && !isa_set) {
948    Usage("Must include ISA if patching an image file without an oat file.");
949  }
950
951  if (!input_oat_location.empty()) {
952    if (!isa_set) {
953      Usage("specifying a location requires specifying an instruction set");
954    }
955    if (!LocationToFilename(input_oat_location, isa, &input_oat_filename)) {
956      Usage("Unable to find filename for input oat location %s", input_oat_location.c_str());
957    }
958    if (debug) {
959      LOG(INFO) << "Using input-oat-file " << input_oat_filename;
960    }
961  }
962  if (!patched_image_location.empty()) {
963    if (!isa_set) {
964      Usage("specifying a location requires specifying an instruction set");
965    }
966    std::string system_filename;
967    bool has_system = false;
968    std::string cache_filename;
969    bool has_cache = false;
970    bool has_android_data_unused = false;
971    bool is_global_cache = false;
972    if (!gc::space::ImageSpace::FindImageFilename(patched_image_location.c_str(), isa,
973                                                  &system_filename, &has_system, &cache_filename,
974                                                  &has_android_data_unused, &has_cache,
975                                                  &is_global_cache)) {
976      Usage("Unable to determine image file for location %s", patched_image_location.c_str());
977    }
978    if (has_cache) {
979      patched_image_filename = cache_filename;
980    } else if (has_system) {
981      LOG(WARNING) << "Only image file found was in /system for image location "
982                   << patched_image_location;
983      patched_image_filename = system_filename;
984    } else {
985      Usage("Unable to determine image file for location %s", patched_image_location.c_str());
986    }
987    if (debug) {
988      LOG(INFO) << "Using patched-image-file " << patched_image_filename;
989    }
990  }
991
992  if (!base_delta_set) {
993    if (orig_base_offset_set && base_offset_set) {
994      base_delta_set = true;
995      base_delta = base_offset - orig_base_offset;
996    } else if (!patched_image_filename.empty()) {
997      base_delta_set = true;
998      std::string error_msg;
999      if (!ReadBaseDelta(patched_image_filename.c_str(), &base_delta, &error_msg)) {
1000        Usage(error_msg.c_str(), patched_image_filename.c_str());
1001      }
1002    } else {
1003      if (base_offset_set) {
1004        Usage("Unable to determine original base offset.");
1005      } else {
1006        Usage("Must supply a desired new offset or delta.");
1007      }
1008    }
1009  }
1010
1011  if (!IsAligned<kPageSize>(base_delta)) {
1012    Usage("Base offset/delta must be alligned to a pagesize (0x%08x) boundary.", kPageSize);
1013  }
1014
1015  // Do we need to cleanup output files if we fail?
1016  bool new_image_out = false;
1017  bool new_oat_out = false;
1018
1019  std::unique_ptr<File> input_oat;
1020  std::unique_ptr<File> output_oat;
1021  std::unique_ptr<File> output_image;
1022
1023  if (have_image_files) {
1024    CHECK(!input_image_location.empty());
1025
1026    if (output_image_fd != -1) {
1027      if (output_image_filename.empty()) {
1028        output_image_filename = "output-image-file";
1029      }
1030      output_image.reset(new File(output_image_fd, output_image_filename));
1031    } else {
1032      CHECK(!output_image_filename.empty());
1033      output_image.reset(CreateOrOpen(output_image_filename.c_str(), &new_image_out));
1034    }
1035  } else {
1036    CHECK(output_image_filename.empty() && output_image_fd == -1 && input_image_location.empty());
1037  }
1038
1039  if (have_oat_files) {
1040    if (input_oat_fd != -1) {
1041      if (input_oat_filename.empty()) {
1042        input_oat_filename = "input-oat-file";
1043      }
1044      input_oat.reset(new File(input_oat_fd, input_oat_filename));
1045    } else {
1046      CHECK(!input_oat_filename.empty());
1047      input_oat.reset(OS::OpenFileForReading(input_oat_filename.c_str()));
1048      if (input_oat.get() == nullptr) {
1049        LOG(ERROR) << "Could not open input oat file: " << strerror(errno);
1050      }
1051    }
1052
1053    if (output_oat_fd != -1) {
1054      if (output_oat_filename.empty()) {
1055        output_oat_filename = "output-oat-file";
1056      }
1057      output_oat.reset(new File(output_oat_fd, output_oat_filename));
1058    } else {
1059      CHECK(!output_oat_filename.empty());
1060      output_oat.reset(CreateOrOpen(output_oat_filename.c_str(), &new_oat_out));
1061    }
1062  }
1063
1064  auto cleanup = [&output_image_filename, &output_oat_filename,
1065                  &new_oat_out, &new_image_out, &timings, &dump_timings](bool success) {
1066    timings.EndTiming();
1067    if (!success) {
1068      if (new_oat_out) {
1069        CHECK(!output_oat_filename.empty());
1070        TEMP_FAILURE_RETRY(unlink(output_oat_filename.c_str()));
1071      }
1072      if (new_image_out) {
1073        CHECK(!output_image_filename.empty());
1074        TEMP_FAILURE_RETRY(unlink(output_image_filename.c_str()));
1075      }
1076    }
1077    if (dump_timings) {
1078      LOG(INFO) << Dumpable<TimingLogger>(timings);
1079    }
1080  };
1081
1082  if ((have_oat_files && (input_oat.get() == nullptr || output_oat.get() == nullptr)) ||
1083      (have_image_files && output_image.get() == nullptr)) {
1084    cleanup(false);
1085    return EXIT_FAILURE;
1086  }
1087
1088  ScopedFlock output_oat_lock;
1089  if (lock_output) {
1090    std::string error_msg;
1091    if (have_oat_files && !output_oat_lock.Init(output_oat.get(), &error_msg)) {
1092      LOG(ERROR) << "Unable to lock output oat " << output_image->GetPath() << ": " << error_msg;
1093      cleanup(false);
1094      return EXIT_FAILURE;
1095    }
1096  }
1097
1098  if (debug) {
1099    LOG(INFO) << "moving offset by " << base_delta
1100              << " (0x" << std::hex << base_delta << ") bytes or "
1101              << std::dec << (base_delta/kPageSize) << " pages.";
1102  }
1103
1104  bool ret;
1105  if (have_image_files && have_oat_files) {
1106    TimingLogger::ScopedTiming pt("patch image and oat", &timings);
1107    ret = PatchOat::Patch(input_oat.get(), input_image_location, base_delta,
1108                          output_oat.get(), output_image.get(), isa, &timings);
1109  } else if (have_oat_files) {
1110    TimingLogger::ScopedTiming pt("patch oat", &timings);
1111    ret = PatchOat::Patch(input_oat.get(), base_delta, output_oat.get(), &timings);
1112  } else {
1113    TimingLogger::ScopedTiming pt("patch image", &timings);
1114    CHECK(have_image_files);
1115    ret = PatchOat::Patch(input_image_location, base_delta, output_image.get(), isa, &timings);
1116  }
1117  cleanup(ret);
1118  return (ret) ? EXIT_SUCCESS : EXIT_FAILURE;
1119}
1120
1121}  // namespace art
1122
1123int main(int argc, char **argv) {
1124  return art::patchoat(argc, argv);
1125}
1126