patchoat.h revision a13abbac97d38d726f9e7d5d786b571a5d37c227
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
17#ifndef ART_PATCHOAT_PATCHOAT_H_
18#define ART_PATCHOAT_PATCHOAT_H_
19
20#include "arch/instruction_set.h"
21#include "base/macros.h"
22#include "base/mutex.h"
23#include "elf_file.h"
24#include "elf_utils.h"
25#include "gc/accounting/space_bitmap.h"
26#include "gc/space/image_space.h"
27#include "gc/heap.h"
28#include "os.h"
29#include "runtime.h"
30
31namespace art {
32
33class ArtMethod;
34class ImageHeader;
35class OatHeader;
36
37namespace mirror {
38class Object;
39class PointerArray;
40class Reference;
41class Class;
42}  // namespace mirror
43
44class PatchOat {
45 public:
46  // Patch only the oat file
47  static bool Patch(File* oat_in, off_t delta, File* oat_out, TimingLogger* timings,
48                    bool output_oat_opened_from_fd,  // Was this using --oatput-oat-fd ?
49                    bool new_oat_out);               // Output oat was a new file created by us?
50
51  // Patch only the image (art file)
52  static bool Patch(const std::string& art_location, off_t delta, File* art_out, InstructionSet isa,
53                    TimingLogger* timings);
54
55  // Patch both the image and the oat file
56  static bool Patch(const std::string& art_location,
57                    off_t delta,
58                    const std::string& output_directory,
59                    InstructionSet isa,
60                    TimingLogger* timings);
61
62  ~PatchOat() {}
63  PatchOat(PatchOat&&) = default;
64
65 private:
66  // Takes ownership only of the ElfFile. All other pointers are only borrowed.
67  PatchOat(ElfFile* oat_file, off_t delta, TimingLogger* timings)
68      : oat_file_(oat_file), image_(nullptr), bitmap_(nullptr), heap_(nullptr), delta_(delta),
69        isa_(kNone), space_map_(nullptr), timings_(timings) {}
70  PatchOat(InstructionSet isa, MemMap* image, gc::accounting::ContinuousSpaceBitmap* bitmap,
71           MemMap* heap, off_t delta, TimingLogger* timings)
72      : image_(image), bitmap_(bitmap), heap_(heap),
73        delta_(delta), isa_(isa), space_map_(nullptr), timings_(timings) {}
74  PatchOat(InstructionSet isa, ElfFile* oat_file, MemMap* image,
75           gc::accounting::ContinuousSpaceBitmap* bitmap, MemMap* heap, off_t delta,
76           std::map<gc::space::ImageSpace*, std::unique_ptr<MemMap>>* map, TimingLogger* timings)
77      : oat_file_(oat_file), image_(image), bitmap_(bitmap), heap_(heap),
78        delta_(delta), isa_(isa), space_map_(map), timings_(timings) {}
79
80  // Was the .art image at image_path made with --compile-pic ?
81  static bool IsImagePic(const ImageHeader& image_header, const std::string& image_path);
82
83  enum MaybePic {
84      NOT_PIC,            // Code not pic. Patch as usual.
85      PIC,                // Code was pic. Create symlink; skip OAT patching.
86      ERROR_OAT_FILE,     // Failed to symlink oat file
87      ERROR_FIRST = ERROR_OAT_FILE,
88  };
89
90  // Was the .oat image at oat_in made with --compile-pic ?
91  static MaybePic IsOatPic(const ElfFile* oat_in);
92
93  // Attempt to replace the file with a symlink
94  // Returns false if it fails
95  static bool ReplaceOatFileWithSymlink(const std::string& input_oat_filename,
96                                        const std::string& output_oat_filename,
97                                        bool output_oat_opened_from_fd,
98                                        bool new_oat_out);  // Output oat was newly created?
99
100  static void BitmapCallback(mirror::Object* obj, void* arg)
101      SHARED_REQUIRES(Locks::mutator_lock_) {
102    reinterpret_cast<PatchOat*>(arg)->VisitObject(obj);
103  }
104
105  void VisitObject(mirror::Object* obj)
106      SHARED_REQUIRES(Locks::mutator_lock_);
107  void FixupMethod(ArtMethod* object, ArtMethod* copy)
108      SHARED_REQUIRES(Locks::mutator_lock_);
109
110  // Patches oat in place, modifying the oat_file given to the constructor.
111  bool PatchElf();
112  template <typename ElfFileImpl>
113  bool PatchElf(ElfFileImpl* oat_file);
114  template <typename ElfFileImpl>
115  bool PatchOatHeader(ElfFileImpl* oat_file);
116
117  bool PatchImage(bool primary_image) SHARED_REQUIRES(Locks::mutator_lock_);
118  void PatchArtFields(const ImageHeader* image_header) SHARED_REQUIRES(Locks::mutator_lock_);
119  void PatchArtMethods(const ImageHeader* image_header) SHARED_REQUIRES(Locks::mutator_lock_);
120  void PatchInternedStrings(const ImageHeader* image_header)
121      SHARED_REQUIRES(Locks::mutator_lock_);
122  void PatchClassTable(const ImageHeader* image_header)
123      SHARED_REQUIRES(Locks::mutator_lock_);
124  void PatchDexFileArrays(mirror::ObjectArray<mirror::Object>* img_roots)
125      SHARED_REQUIRES(Locks::mutator_lock_);
126
127  bool WriteElf(File* out);
128  bool WriteImage(File* out);
129
130  template <typename T>
131  T* RelocatedCopyOf(T* obj) const {
132    if (obj == nullptr) {
133      return nullptr;
134    }
135    DCHECK_GT(reinterpret_cast<uintptr_t>(obj), reinterpret_cast<uintptr_t>(heap_->Begin()));
136    DCHECK_LT(reinterpret_cast<uintptr_t>(obj), reinterpret_cast<uintptr_t>(heap_->End()));
137    uintptr_t heap_off =
138        reinterpret_cast<uintptr_t>(obj) - reinterpret_cast<uintptr_t>(heap_->Begin());
139    DCHECK_LT(heap_off, image_->Size());
140    return reinterpret_cast<T*>(image_->Begin() + heap_off);
141  }
142
143  template <typename T>
144  T* RelocatedCopyOfFollowImages(T* obj) const {
145    if (obj == nullptr) {
146      return nullptr;
147    }
148    // Find ImageSpace this belongs to.
149    auto image_spaces = Runtime::Current()->GetHeap()->GetBootImageSpaces();
150    for (gc::space::ImageSpace* image_space : image_spaces) {
151      if (image_space->Contains(obj)) {
152        uintptr_t heap_off = reinterpret_cast<uintptr_t>(obj) -
153                             reinterpret_cast<uintptr_t>(image_space->GetMemMap()->Begin());
154        return reinterpret_cast<T*>(space_map_->find(image_space)->second->Begin() + heap_off);
155      }
156    }
157    LOG(FATAL) << "Did not find object in boot image space " << obj;
158    UNREACHABLE();
159  }
160
161  template <typename T>
162  T* RelocatedAddressOfPointer(T* obj) const {
163    if (obj == nullptr) {
164      return obj;
165    }
166    auto ret = reinterpret_cast<uintptr_t>(obj) + delta_;
167    // Trim off high bits in case negative relocation with 64 bit patchoat.
168    if (InstructionSetPointerSize(isa_) == sizeof(uint32_t)) {
169      ret = static_cast<uintptr_t>(static_cast<uint32_t>(ret));
170    }
171    return reinterpret_cast<T*>(ret);
172  }
173
174  template <typename T>
175  T RelocatedAddressOfIntPointer(T obj) const {
176    if (obj == 0) {
177      return obj;
178    }
179    T ret = obj + delta_;
180    // Trim off high bits in case negative relocation with 64 bit patchoat.
181    if (InstructionSetPointerSize(isa_) == 4) {
182      ret = static_cast<T>(static_cast<uint32_t>(ret));
183    }
184    return ret;
185  }
186
187  // Walks through the old image and patches the mmap'd copy of it to the new offset. It does not
188  // change the heap.
189  class PatchVisitor {
190  public:
191    PatchVisitor(PatchOat* patcher, mirror::Object* copy) : patcher_(patcher), copy_(copy) {}
192    ~PatchVisitor() {}
193    void operator() (mirror::Object* obj, MemberOffset off, bool b) const
194        REQUIRES(Locks::mutator_lock_, Locks::heap_bitmap_lock_);
195    // For reference classes.
196    void operator() (mirror::Class* cls, mirror::Reference* ref) const
197        REQUIRES(Locks::mutator_lock_, Locks::heap_bitmap_lock_);
198    // TODO: Consider using these for updating native class roots?
199    void VisitRootIfNonNull(mirror::CompressedReference<mirror::Object>* root ATTRIBUTE_UNUSED)
200        const {}
201    void VisitRoot(mirror::CompressedReference<mirror::Object>* root ATTRIBUTE_UNUSED) const {}
202
203  private:
204    PatchOat* const patcher_;
205    mirror::Object* const copy_;
206  };
207
208  // The elf file we are patching.
209  std::unique_ptr<ElfFile> oat_file_;
210  // A mmap of the image we are patching. This is modified.
211  const MemMap* const image_;
212  // The bitmap over the image within the heap we are patching. This is not modified.
213  gc::accounting::ContinuousSpaceBitmap* const bitmap_;
214  // The heap we are patching. This is not modified.
215  const MemMap* const heap_;
216  // The amount we are changing the offset by.
217  const off_t delta_;
218  // Active instruction set, used to know the entrypoint size.
219  const InstructionSet isa_;
220
221  const std::map<gc::space::ImageSpace*, std::unique_ptr<MemMap>>* space_map_;
222
223  TimingLogger* timings_;
224
225  friend class FixupRootVisitor;
226  friend class RelocatedPointerVisitor;
227  friend class PatchOatArtFieldVisitor;
228  friend class PatchOatArtMethodVisitor;
229  DISALLOW_IMPLICIT_CONSTRUCTORS(PatchOat);
230};
231
232}  // namespace art
233#endif  // ART_PATCHOAT_PATCHOAT_H_
234