Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 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 | |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 20 | #include "arch/instruction_set.h" |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 21 | #include "base/macros.h" |
| 22 | #include "base/mutex.h" |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 23 | #include "elf_file.h" |
| 24 | #include "elf_utils.h" |
| 25 | #include "gc/accounting/space_bitmap.h" |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 26 | #include "gc/space/image_space.h" |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 27 | #include "gc/heap.h" |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 28 | #include "os.h" |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 29 | #include "runtime.h" |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 30 | |
| 31 | namespace art { |
| 32 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 33 | class ArtMethod; |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 34 | class ImageHeader; |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 35 | class OatHeader; |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 36 | |
| 37 | namespace mirror { |
| 38 | class Object; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 39 | class PointerArray; |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 40 | class Reference; |
| 41 | class Class; |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 42 | } // namespace mirror |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 43 | |
| 44 | class PatchOat { |
| 45 | public: |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 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? |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 50 | |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 51 | // Patch only the image (art file) |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 52 | static bool Patch(const std::string& art_location, off_t delta, File* art_out, InstructionSet isa, |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 53 | TimingLogger* timings); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 54 | |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 55 | // Patch both the image and the oat file |
| 56 | static bool Patch(File* oat_in, const std::string& art_location, |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 57 | off_t delta, File* oat_out, File* art_out, InstructionSet isa, |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 58 | TimingLogger* timings, |
| 59 | bool output_oat_opened_from_fd, // Was this using --oatput-oat-fd ? |
| 60 | bool new_oat_out); // Output oat was a new file created by us? |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 61 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 62 | ~PatchOat() {} |
| 63 | PatchOat(PatchOat&&) = default; |
| 64 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 65 | private: |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 66 | // Takes ownership only of the ElfFile. All other pointers are only borrowed. |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 67 | PatchOat(ElfFile* oat_file, off_t delta, TimingLogger* timings) |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 68 | : oat_file_(oat_file), image_(nullptr), bitmap_(nullptr), heap_(nullptr), delta_(delta), |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 69 | isa_(kNone), space_map_(nullptr), timings_(timings) {} |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 70 | PatchOat(InstructionSet isa, MemMap* image, gc::accounting::ContinuousSpaceBitmap* bitmap, |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 71 | MemMap* heap, off_t delta, TimingLogger* timings) |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 72 | : image_(image), bitmap_(bitmap), heap_(heap), |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 73 | delta_(delta), isa_(isa), space_map_(nullptr), timings_(timings) {} |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 74 | PatchOat(InstructionSet isa, ElfFile* oat_file, MemMap* image, |
| 75 | gc::accounting::ContinuousSpaceBitmap* bitmap, MemMap* heap, off_t delta, |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 76 | std::map<gc::space::ImageSpace*, std::unique_ptr<MemMap>>* map, TimingLogger* timings) |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 77 | : oat_file_(oat_file), image_(image), bitmap_(bitmap), heap_(heap), |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 78 | delta_(delta), isa_(isa), space_map_(map), timings_(timings) {} |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 79 | |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 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 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 100 | static void BitmapCallback(mirror::Object* obj, void* arg) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 101 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 102 | reinterpret_cast<PatchOat*>(arg)->VisitObject(obj); |
| 103 | } |
| 104 | |
| 105 | void VisitObject(mirror::Object* obj) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 106 | SHARED_REQUIRES(Locks::mutator_lock_); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 107 | void FixupMethod(ArtMethod* object, ArtMethod* copy) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 108 | SHARED_REQUIRES(Locks::mutator_lock_); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 109 | bool InHeap(mirror::Object*); |
| 110 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 111 | // Patches oat in place, modifying the oat_file given to the constructor. |
| 112 | bool PatchElf(); |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 113 | template <typename ElfFileImpl> |
| 114 | bool PatchElf(ElfFileImpl* oat_file); |
| 115 | template <typename ElfFileImpl> |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 116 | bool PatchOatHeader(ElfFileImpl* oat_file); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 117 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 118 | bool PatchImage(bool primary_image) SHARED_REQUIRES(Locks::mutator_lock_); |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 119 | void PatchArtFields(const ImageHeader* image_header) SHARED_REQUIRES(Locks::mutator_lock_); |
| 120 | void PatchArtMethods(const ImageHeader* image_header) SHARED_REQUIRES(Locks::mutator_lock_); |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 121 | void PatchInternedStrings(const ImageHeader* image_header) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 122 | SHARED_REQUIRES(Locks::mutator_lock_); |
Mathieu Chartier | 208a5cb | 2015-12-02 15:44:07 -0800 | [diff] [blame] | 123 | void PatchClassTable(const ImageHeader* image_header) |
| 124 | SHARED_REQUIRES(Locks::mutator_lock_); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 125 | void PatchDexFileArrays(mirror::ObjectArray<mirror::Object>* img_roots) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 126 | SHARED_REQUIRES(Locks::mutator_lock_); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 127 | |
| 128 | bool WriteElf(File* out); |
| 129 | bool WriteImage(File* out); |
| 130 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 131 | template <typename T> |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 132 | T* RelocatedCopyOf(T* obj) const { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 133 | if (obj == nullptr) { |
| 134 | return nullptr; |
| 135 | } |
Jeff Hao | e271fe1 | 2016-01-04 17:38:06 -0800 | [diff] [blame] | 136 | DCHECK_GT(reinterpret_cast<uintptr_t>(obj), reinterpret_cast<uintptr_t>(heap_->Begin())); |
| 137 | DCHECK_LT(reinterpret_cast<uintptr_t>(obj), reinterpret_cast<uintptr_t>(heap_->End())); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 138 | uintptr_t heap_off = |
| 139 | reinterpret_cast<uintptr_t>(obj) - reinterpret_cast<uintptr_t>(heap_->Begin()); |
Jeff Hao | e271fe1 | 2016-01-04 17:38:06 -0800 | [diff] [blame] | 140 | DCHECK_LT(heap_off, image_->Size()); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 141 | return reinterpret_cast<T*>(image_->Begin() + heap_off); |
| 142 | } |
| 143 | |
| 144 | template <typename T> |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 145 | T* RelocatedCopyOfFollowImages(T* obj) const { |
| 146 | if (obj == nullptr) { |
| 147 | return nullptr; |
| 148 | } |
| 149 | // Find ImageSpace this belongs to. |
| 150 | auto image_spaces = Runtime::Current()->GetHeap()->GetBootImageSpaces(); |
| 151 | for (gc::space::ImageSpace* image_space : image_spaces) { |
| 152 | if (image_space->Contains(obj)) { |
| 153 | uintptr_t heap_off = reinterpret_cast<uintptr_t>(obj) - |
| 154 | reinterpret_cast<uintptr_t>(image_space->GetMemMap()->Begin()); |
| 155 | return reinterpret_cast<T*>(space_map_->find(image_space)->second->Begin() + heap_off); |
| 156 | } |
| 157 | } |
| 158 | LOG(FATAL) << "Did not find object in boot image space " << obj; |
| 159 | UNREACHABLE(); |
| 160 | } |
| 161 | |
| 162 | template <typename T> |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 163 | T* RelocatedAddressOfPointer(T* obj) const { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 164 | if (obj == nullptr) { |
| 165 | return obj; |
| 166 | } |
| 167 | auto ret = reinterpret_cast<uintptr_t>(obj) + delta_; |
| 168 | // Trim off high bits in case negative relocation with 64 bit patchoat. |
| 169 | if (InstructionSetPointerSize(isa_) == sizeof(uint32_t)) { |
| 170 | ret = static_cast<uintptr_t>(static_cast<uint32_t>(ret)); |
| 171 | } |
| 172 | return reinterpret_cast<T*>(ret); |
| 173 | } |
| 174 | |
| 175 | template <typename T> |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 176 | T RelocatedAddressOfIntPointer(T obj) const { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 177 | if (obj == 0) { |
| 178 | return obj; |
| 179 | } |
| 180 | T ret = obj + delta_; |
| 181 | // Trim off high bits in case negative relocation with 64 bit patchoat. |
| 182 | if (InstructionSetPointerSize(isa_) == 4) { |
| 183 | ret = static_cast<T>(static_cast<uint32_t>(ret)); |
| 184 | } |
| 185 | return ret; |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 186 | } |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 187 | |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 188 | // Walks through the old image and patches the mmap'd copy of it to the new offset. It does not |
| 189 | // change the heap. |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 190 | class PatchVisitor { |
| 191 | public: |
| 192 | PatchVisitor(PatchOat* patcher, mirror::Object* copy) : patcher_(patcher), copy_(copy) {} |
| 193 | ~PatchVisitor() {} |
| 194 | void operator() (mirror::Object* obj, MemberOffset off, bool b) const |
Mathieu Chartier | da7c650 | 2015-07-23 16:01:26 -0700 | [diff] [blame] | 195 | REQUIRES(Locks::mutator_lock_, Locks::heap_bitmap_lock_); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 196 | // For reference classes. |
| 197 | void operator() (mirror::Class* cls, mirror::Reference* ref) const |
Mathieu Chartier | da7c650 | 2015-07-23 16:01:26 -0700 | [diff] [blame] | 198 | REQUIRES(Locks::mutator_lock_, Locks::heap_bitmap_lock_); |
| 199 | // TODO: Consider using these for updating native class roots? |
| 200 | void VisitRootIfNonNull(mirror::CompressedReference<mirror::Object>* root ATTRIBUTE_UNUSED) |
| 201 | const {} |
| 202 | void VisitRoot(mirror::CompressedReference<mirror::Object>* root ATTRIBUTE_UNUSED) const {} |
| 203 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 204 | private: |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 205 | PatchOat* const patcher_; |
| 206 | mirror::Object* const copy_; |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 207 | }; |
| 208 | |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 209 | // The elf file we are patching. |
| 210 | std::unique_ptr<ElfFile> oat_file_; |
| 211 | // A mmap of the image we are patching. This is modified. |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 212 | const MemMap* const image_; |
| 213 | // The bitmap over the image within the heap we are patching. This is not modified. |
| 214 | gc::accounting::ContinuousSpaceBitmap* const bitmap_; |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 215 | // The heap we are patching. This is not modified. |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 216 | const MemMap* const heap_; |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 217 | // The amount we are changing the offset by. |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 218 | const off_t delta_; |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 219 | // Active instruction set, used to know the entrypoint size. |
| 220 | const InstructionSet isa_; |
| 221 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 222 | const std::map<gc::space::ImageSpace*, std::unique_ptr<MemMap>>* space_map_; |
| 223 | |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 224 | TimingLogger* timings_; |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 225 | |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 226 | friend class FixupRootVisitor; |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 227 | friend class RelocatedPointerVisitor; |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 228 | friend class PatchOatArtFieldVisitor; |
| 229 | friend class PatchOatArtMethodVisitor; |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 230 | DISALLOW_IMPLICIT_CONSTRUCTORS(PatchOat); |
| 231 | }; |
| 232 | |
| 233 | } // namespace art |
| 234 | #endif // ART_PATCHOAT_PATCHOAT_H_ |