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" |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 21 | #include "base/enums.h" |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 22 | #include "base/macros.h" |
| 23 | #include "base/mutex.h" |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 24 | #include "elf_file.h" |
| 25 | #include "elf_utils.h" |
| 26 | #include "gc/accounting/space_bitmap.h" |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 27 | #include "gc/space/image_space.h" |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 28 | #include "gc/heap.h" |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 29 | #include "os.h" |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 30 | #include "runtime.h" |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 31 | |
| 32 | namespace art { |
| 33 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 34 | class ArtMethod; |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 35 | class ImageHeader; |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 36 | class OatHeader; |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 37 | |
| 38 | namespace mirror { |
| 39 | class Object; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 40 | class PointerArray; |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 41 | class Reference; |
| 42 | class Class; |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 43 | } // namespace mirror |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 44 | |
| 45 | class PatchOat { |
| 46 | public: |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 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? |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 51 | |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 52 | // Patch only the image (art file) |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 53 | 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] | 54 | TimingLogger* timings); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 55 | |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 56 | // Patch both the image and the oat file |
Andreas Gampe | 6eb6a39 | 2016-02-10 20:18:37 -0800 | [diff] [blame] | 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); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 62 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 63 | ~PatchOat() {} |
| 64 | PatchOat(PatchOat&&) = default; |
| 65 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 66 | private: |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 67 | // Takes ownership only of the ElfFile. All other pointers are only borrowed. |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 68 | PatchOat(ElfFile* oat_file, off_t delta, TimingLogger* timings) |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 69 | : oat_file_(oat_file), image_(nullptr), bitmap_(nullptr), heap_(nullptr), delta_(delta), |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 70 | isa_(kNone), space_map_(nullptr), timings_(timings) {} |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 71 | PatchOat(InstructionSet isa, MemMap* image, gc::accounting::ContinuousSpaceBitmap* bitmap, |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 72 | MemMap* heap, off_t delta, TimingLogger* timings) |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 73 | : image_(image), bitmap_(bitmap), heap_(heap), |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 74 | delta_(delta), isa_(isa), space_map_(nullptr), timings_(timings) {} |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 75 | PatchOat(InstructionSet isa, ElfFile* oat_file, MemMap* image, |
| 76 | gc::accounting::ContinuousSpaceBitmap* bitmap, MemMap* heap, off_t delta, |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 77 | std::map<gc::space::ImageSpace*, std::unique_ptr<MemMap>>* map, TimingLogger* timings) |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 78 | : oat_file_(oat_file), image_(image), bitmap_(bitmap), heap_(heap), |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 79 | delta_(delta), isa_(isa), space_map_(map), timings_(timings) {} |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 80 | |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 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 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 101 | static void BitmapCallback(mirror::Object* obj, void* arg) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 102 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 103 | reinterpret_cast<PatchOat*>(arg)->VisitObject(obj); |
| 104 | } |
| 105 | |
| 106 | void VisitObject(mirror::Object* obj) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 107 | REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 108 | void FixupMethod(ArtMethod* object, ArtMethod* copy) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 109 | REQUIRES_SHARED(Locks::mutator_lock_); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 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 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 118 | bool PatchImage(bool primary_image) REQUIRES_SHARED(Locks::mutator_lock_); |
| 119 | void PatchArtFields(const ImageHeader* image_header) REQUIRES_SHARED(Locks::mutator_lock_); |
| 120 | void PatchArtMethods(const ImageHeader* image_header) REQUIRES_SHARED(Locks::mutator_lock_); |
| 121 | void PatchImTables(const ImageHeader* image_header) REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 122 | void PatchImtConflictTables(const ImageHeader* image_header) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 123 | REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 124 | void PatchInternedStrings(const ImageHeader* image_header) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 125 | REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | 208a5cb | 2015-12-02 15:44:07 -0800 | [diff] [blame] | 126 | void PatchClassTable(const ImageHeader* image_header) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 127 | REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 128 | void PatchDexFileArrays(mirror::ObjectArray<mirror::Object>* img_roots) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 129 | REQUIRES_SHARED(Locks::mutator_lock_); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 130 | |
| 131 | bool WriteElf(File* out); |
| 132 | bool WriteImage(File* out); |
| 133 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 134 | template <typename T> |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 135 | T* RelocatedCopyOf(T* obj) const { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 136 | if (obj == nullptr) { |
| 137 | return nullptr; |
| 138 | } |
Jeff Hao | 0d2af30 | 2016-01-04 17:38:06 -0800 | [diff] [blame] | 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())); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 141 | uintptr_t heap_off = |
| 142 | reinterpret_cast<uintptr_t>(obj) - reinterpret_cast<uintptr_t>(heap_->Begin()); |
Jeff Hao | 0d2af30 | 2016-01-04 17:38:06 -0800 | [diff] [blame] | 143 | DCHECK_LT(heap_off, image_->Size()); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 144 | return reinterpret_cast<T*>(image_->Begin() + heap_off); |
| 145 | } |
| 146 | |
| 147 | template <typename T> |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 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> |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 166 | T* RelocatedAddressOfPointer(T* obj) const { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 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. |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 172 | if (Is32BitISA()) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 173 | ret = static_cast<uintptr_t>(static_cast<uint32_t>(ret)); |
| 174 | } |
| 175 | return reinterpret_cast<T*>(ret); |
| 176 | } |
| 177 | |
| 178 | template <typename T> |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 179 | T RelocatedAddressOfIntPointer(T obj) const { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 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. |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 185 | if (Is32BitISA()) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 186 | ret = static_cast<T>(static_cast<uint32_t>(ret)); |
| 187 | } |
| 188 | return ret; |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 189 | } |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 190 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 191 | bool Is32BitISA() const { |
| 192 | return InstructionSetPointerSize(isa_) == PointerSize::k32; |
| 193 | } |
| 194 | |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 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. |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 197 | class PatchVisitor { |
| 198 | public: |
| 199 | PatchVisitor(PatchOat* patcher, mirror::Object* copy) : patcher_(patcher), copy_(copy) {} |
| 200 | ~PatchVisitor() {} |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 201 | void operator() (ObjPtr<mirror::Object> obj, MemberOffset off, bool b) const |
Mathieu Chartier | da7c650 | 2015-07-23 16:01:26 -0700 | [diff] [blame] | 202 | REQUIRES(Locks::mutator_lock_, Locks::heap_bitmap_lock_); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 203 | // For reference classes. |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 204 | void operator() (ObjPtr<mirror::Class> cls, ObjPtr<mirror::Reference> ref) const |
Mathieu Chartier | da7c650 | 2015-07-23 16:01:26 -0700 | [diff] [blame] | 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 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 211 | private: |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 212 | PatchOat* const patcher_; |
| 213 | mirror::Object* const copy_; |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 214 | }; |
| 215 | |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 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. |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 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_; |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 222 | // The heap we are patching. This is not modified. |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 223 | const MemMap* const heap_; |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 224 | // The amount we are changing the offset by. |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 225 | const off_t delta_; |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 226 | // Active instruction set, used to know the entrypoint size. |
| 227 | const InstructionSet isa_; |
| 228 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 229 | const std::map<gc::space::ImageSpace*, std::unique_ptr<MemMap>>* space_map_; |
| 230 | |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 231 | TimingLogger* timings_; |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 232 | |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 233 | friend class FixupRootVisitor; |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 234 | friend class RelocatedPointerVisitor; |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 235 | friend class PatchOatArtFieldVisitor; |
| 236 | friend class PatchOatArtMethodVisitor; |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 237 | DISALLOW_IMPLICIT_CONSTRUCTORS(PatchOat); |
| 238 | }; |
| 239 | |
| 240 | } // namespace art |
| 241 | #endif // ART_PATCHOAT_PATCHOAT_H_ |