patchoat.h revision 3856af0d6e09525a4e774bec729dd781a72d5549
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/heap.h"
28#include "gc/space/image_space.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  // Relocates the provided image by the specified offset. If output_image_directory is non-empty,
48  // outputs the relocated image into that directory. If output_image_relocation_directory is
49  // non-empty, outputs image relocation files (see GeneratePatch) into that directory.
50  static bool Patch(const std::string& image_location,
51                    off_t delta,
52                    const std::string& output_image_directory,
53                    const std::string& output_image_relocation_directory,
54                    InstructionSet isa,
55                    TimingLogger* timings);
56
57  // Generates a patch which can be used to efficiently relocate the original file or to check that
58  // a relocated file matches the original. The patch is generated from the difference of the
59  // |original| and the already |relocated| image, and written to |output| in the form of unsigned
60  // LEB128 for each relocation position.
61  static bool GeneratePatch(const MemMap& original,
62                            const MemMap& relocated,
63                            std::vector<uint8_t>* output,
64                            std::string* error_msg);
65
66  ~PatchOat() {}
67  PatchOat(PatchOat&&) = default;
68
69 private:
70  // All pointers are only borrowed.
71  PatchOat(InstructionSet isa, MemMap* image,
72           gc::accounting::ContinuousSpaceBitmap* bitmap, MemMap* heap, off_t delta,
73           std::map<gc::space::ImageSpace*, std::unique_ptr<MemMap>>* map, TimingLogger* timings)
74      : image_(image), bitmap_(bitmap), heap_(heap),
75        delta_(delta), isa_(isa), space_map_(map), timings_(timings) {}
76
77  // Was the .art image at image_path made with --compile-pic ?
78  static bool IsImagePic(const ImageHeader& image_header, const std::string& image_path);
79
80  enum MaybePic {
81      NOT_PIC,            // Code not pic. Patch as usual.
82      PIC,                // Code was pic. Create symlink; skip OAT patching.
83      ERROR_OAT_FILE,     // Failed to symlink oat file
84      ERROR_FIRST = ERROR_OAT_FILE,
85  };
86
87  // Was the .oat image at oat_in made with --compile-pic ?
88  static MaybePic IsOatPic(const ElfFile* oat_in);
89
90  // Attempt to replace the file with a symlink
91  // Returns false if it fails
92  static bool ReplaceOatFileWithSymlink(const std::string& input_oat_filename,
93                                        const std::string& output_oat_filename);
94
95  void VisitObject(mirror::Object* obj)
96      REQUIRES_SHARED(Locks::mutator_lock_);
97  void FixupMethod(ArtMethod* object, ArtMethod* copy)
98      REQUIRES_SHARED(Locks::mutator_lock_);
99
100  bool PatchImage(bool primary_image) REQUIRES_SHARED(Locks::mutator_lock_);
101  void PatchArtFields(const ImageHeader* image_header) REQUIRES_SHARED(Locks::mutator_lock_);
102  void PatchArtMethods(const ImageHeader* image_header) REQUIRES_SHARED(Locks::mutator_lock_);
103  void PatchImTables(const ImageHeader* image_header) REQUIRES_SHARED(Locks::mutator_lock_);
104  void PatchImtConflictTables(const ImageHeader* image_header)
105      REQUIRES_SHARED(Locks::mutator_lock_);
106  void PatchInternedStrings(const ImageHeader* image_header)
107      REQUIRES_SHARED(Locks::mutator_lock_);
108  void PatchClassTable(const ImageHeader* image_header)
109      REQUIRES_SHARED(Locks::mutator_lock_);
110  void PatchDexFileArrays(mirror::ObjectArray<mirror::Object>* img_roots)
111      REQUIRES_SHARED(Locks::mutator_lock_);
112
113  bool WriteImage(File* out);
114
115  template <typename T>
116  T* RelocatedCopyOf(T* obj) const {
117    if (obj == nullptr) {
118      return nullptr;
119    }
120    DCHECK_GT(reinterpret_cast<uintptr_t>(obj), reinterpret_cast<uintptr_t>(heap_->Begin()));
121    DCHECK_LT(reinterpret_cast<uintptr_t>(obj), reinterpret_cast<uintptr_t>(heap_->End()));
122    uintptr_t heap_off =
123        reinterpret_cast<uintptr_t>(obj) - reinterpret_cast<uintptr_t>(heap_->Begin());
124    DCHECK_LT(heap_off, image_->Size());
125    return reinterpret_cast<T*>(image_->Begin() + heap_off);
126  }
127
128  template <typename T>
129  T* RelocatedCopyOfFollowImages(T* obj) const {
130    if (obj == nullptr) {
131      return nullptr;
132    }
133    // Find ImageSpace this belongs to.
134    auto image_spaces = Runtime::Current()->GetHeap()->GetBootImageSpaces();
135    for (gc::space::ImageSpace* image_space : image_spaces) {
136      if (image_space->Contains(obj)) {
137        uintptr_t heap_off = reinterpret_cast<uintptr_t>(obj) -
138                             reinterpret_cast<uintptr_t>(image_space->GetMemMap()->Begin());
139        return reinterpret_cast<T*>(space_map_->find(image_space)->second->Begin() + heap_off);
140      }
141    }
142    LOG(FATAL) << "Did not find object in boot image space " << obj;
143    UNREACHABLE();
144  }
145
146  template <typename T>
147  T* RelocatedAddressOfPointer(T* obj) const {
148    if (obj == nullptr) {
149      return obj;
150    }
151    auto ret = reinterpret_cast<uintptr_t>(obj) + delta_;
152    // Trim off high bits in case negative relocation with 64 bit patchoat.
153    if (Is32BitISA()) {
154      ret = static_cast<uintptr_t>(static_cast<uint32_t>(ret));
155    }
156    return reinterpret_cast<T*>(ret);
157  }
158
159  bool Is32BitISA() const {
160    return InstructionSetPointerSize(isa_) == PointerSize::k32;
161  }
162
163  // Walks through the old image and patches the mmap'd copy of it to the new offset. It does not
164  // change the heap.
165  class PatchVisitor {
166   public:
167    PatchVisitor(PatchOat* patcher, mirror::Object* copy) : patcher_(patcher), copy_(copy) {}
168    ~PatchVisitor() {}
169    void operator() (ObjPtr<mirror::Object> obj, MemberOffset off, bool b) const
170        REQUIRES(Locks::mutator_lock_, Locks::heap_bitmap_lock_);
171    // For reference classes.
172    void operator() (ObjPtr<mirror::Class> cls, ObjPtr<mirror::Reference>  ref) const
173        REQUIRES(Locks::mutator_lock_, Locks::heap_bitmap_lock_);
174    // TODO: Consider using these for updating native class roots?
175    void VisitRootIfNonNull(mirror::CompressedReference<mirror::Object>* root ATTRIBUTE_UNUSED)
176        const {}
177    void VisitRoot(mirror::CompressedReference<mirror::Object>* root ATTRIBUTE_UNUSED) const {}
178
179   private:
180    PatchOat* const patcher_;
181    mirror::Object* const copy_;
182  };
183
184  // A mmap of the image we are patching. This is modified.
185  const MemMap* const image_;
186  // The bitmap over the image within the heap we are patching. This is not modified.
187  gc::accounting::ContinuousSpaceBitmap* const bitmap_;
188  // The heap we are patching. This is not modified.
189  const MemMap* const heap_;
190  // The amount we are changing the offset by.
191  const off_t delta_;
192  // Active instruction set, used to know the entrypoint size.
193  const InstructionSet isa_;
194
195  const std::map<gc::space::ImageSpace*, std::unique_ptr<MemMap>>* space_map_;
196
197  TimingLogger* timings_;
198
199  class FixupRootVisitor;
200  class RelocatedPointerVisitor;
201  class PatchOatArtFieldVisitor;
202  class PatchOatArtMethodVisitor;
203
204  DISALLOW_IMPLICIT_CONSTRUCTORS(PatchOat);
205};
206
207}  // namespace art
208#endif  // ART_PATCHOAT_PATCHOAT_H_
209