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