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" |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 24 | #include "base/os.h" |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 25 | #include "elf_file.h" |
| 26 | #include "elf_utils.h" |
| 27 | #include "gc/accounting/space_bitmap.h" |
| 28 | #include "gc/heap.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 29 | #include "gc/space/image_space.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: |
Alex Klyubin | 3856af0 | 2017-10-23 13:53:13 -0700 | [diff] [blame] | 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. |
Richard Uhler | 4bc11d0 | 2017-02-01 09:53:54 +0000 | [diff] [blame] | 50 | static bool Patch(const std::string& image_location, |
Andreas Gampe | 6eb6a39 | 2016-02-10 20:18:37 -0800 | [diff] [blame] | 51 | off_t delta, |
Alex Klyubin | 3856af0 | 2017-10-23 13:53:13 -0700 | [diff] [blame] | 52 | const std::string& output_image_directory, |
| 53 | const std::string& output_image_relocation_directory, |
Andreas Gampe | 6eb6a39 | 2016-02-10 20:18:37 -0800 | [diff] [blame] | 54 | InstructionSet isa, |
| 55 | TimingLogger* timings); |
Chris Morin | 754b757 | 2018-01-19 18:04:46 -0800 | [diff] [blame] | 56 | static bool Verify(const std::string& image_location, |
| 57 | const std::string& output_image_filename, |
| 58 | InstructionSet isa, |
| 59 | TimingLogger* timings); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 60 | |
Alex Klyubin | 3856af0 | 2017-10-23 13:53:13 -0700 | [diff] [blame] | 61 | // Generates a patch which can be used to efficiently relocate the original file or to check that |
| 62 | // a relocated file matches the original. The patch is generated from the difference of the |
| 63 | // |original| and the already |relocated| image, and written to |output| in the form of unsigned |
| 64 | // LEB128 for each relocation position. |
| 65 | static bool GeneratePatch(const MemMap& original, |
| 66 | const MemMap& relocated, |
| 67 | std::vector<uint8_t>* output, |
| 68 | std::string* error_msg); |
| 69 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 70 | ~PatchOat() {} |
| 71 | PatchOat(PatchOat&&) = default; |
| 72 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 73 | private: |
Richard Uhler | 4bc11d0 | 2017-02-01 09:53:54 +0000 | [diff] [blame] | 74 | // All pointers are only borrowed. |
| 75 | PatchOat(InstructionSet isa, MemMap* image, |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 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) |
Richard Uhler | 4bc11d0 | 2017-02-01 09:53:54 +0000 | [diff] [blame] | 78 | : 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 | |
Chris Morin | ae6832f | 2018-02-09 19:12:35 -0800 | [diff] [blame] | 94 | static bool CreateVdexAndOatSymlinks(const std::string& input_image_filename, |
| 95 | const std::string& output_image_filename); |
| 96 | |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 97 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 98 | void VisitObject(mirror::Object* obj) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 99 | REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 100 | void FixupMethod(ArtMethod* object, ArtMethod* copy) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 101 | REQUIRES_SHARED(Locks::mutator_lock_); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 102 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 103 | bool PatchImage(bool primary_image) REQUIRES_SHARED(Locks::mutator_lock_); |
| 104 | void PatchArtFields(const ImageHeader* image_header) REQUIRES_SHARED(Locks::mutator_lock_); |
| 105 | void PatchArtMethods(const ImageHeader* image_header) REQUIRES_SHARED(Locks::mutator_lock_); |
| 106 | void PatchImTables(const ImageHeader* image_header) REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 107 | void PatchImtConflictTables(const ImageHeader* image_header) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 108 | REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 109 | void PatchInternedStrings(const ImageHeader* image_header) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 110 | REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | 208a5cb | 2015-12-02 15:44:07 -0800 | [diff] [blame] | 111 | void PatchClassTable(const ImageHeader* image_header) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 112 | REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 113 | void PatchDexFileArrays(mirror::ObjectArray<mirror::Object>* img_roots) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 114 | REQUIRES_SHARED(Locks::mutator_lock_); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 115 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 116 | bool WriteImage(File* out); |
| 117 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 118 | template <typename T> |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 119 | T* RelocatedCopyOf(T* obj) const { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 120 | if (obj == nullptr) { |
| 121 | return nullptr; |
| 122 | } |
Jeff Hao | 0d2af30 | 2016-01-04 17:38:06 -0800 | [diff] [blame] | 123 | DCHECK_GT(reinterpret_cast<uintptr_t>(obj), reinterpret_cast<uintptr_t>(heap_->Begin())); |
| 124 | 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] | 125 | uintptr_t heap_off = |
| 126 | reinterpret_cast<uintptr_t>(obj) - reinterpret_cast<uintptr_t>(heap_->Begin()); |
Jeff Hao | 0d2af30 | 2016-01-04 17:38:06 -0800 | [diff] [blame] | 127 | DCHECK_LT(heap_off, image_->Size()); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 128 | return reinterpret_cast<T*>(image_->Begin() + heap_off); |
| 129 | } |
| 130 | |
| 131 | template <typename T> |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 132 | T* RelocatedCopyOfFollowImages(T* obj) const { |
| 133 | if (obj == nullptr) { |
| 134 | return nullptr; |
| 135 | } |
| 136 | // Find ImageSpace this belongs to. |
| 137 | auto image_spaces = Runtime::Current()->GetHeap()->GetBootImageSpaces(); |
| 138 | for (gc::space::ImageSpace* image_space : image_spaces) { |
| 139 | if (image_space->Contains(obj)) { |
| 140 | uintptr_t heap_off = reinterpret_cast<uintptr_t>(obj) - |
| 141 | reinterpret_cast<uintptr_t>(image_space->GetMemMap()->Begin()); |
| 142 | return reinterpret_cast<T*>(space_map_->find(image_space)->second->Begin() + heap_off); |
| 143 | } |
| 144 | } |
| 145 | LOG(FATAL) << "Did not find object in boot image space " << obj; |
| 146 | UNREACHABLE(); |
| 147 | } |
| 148 | |
| 149 | template <typename T> |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 150 | T* RelocatedAddressOfPointer(T* obj) const { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 151 | if (obj == nullptr) { |
| 152 | return obj; |
| 153 | } |
| 154 | auto ret = reinterpret_cast<uintptr_t>(obj) + delta_; |
| 155 | // Trim off high bits in case negative relocation with 64 bit patchoat. |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 156 | if (Is32BitISA()) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 157 | ret = static_cast<uintptr_t>(static_cast<uint32_t>(ret)); |
| 158 | } |
| 159 | return reinterpret_cast<T*>(ret); |
| 160 | } |
| 161 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 162 | bool Is32BitISA() const { |
| 163 | return InstructionSetPointerSize(isa_) == PointerSize::k32; |
| 164 | } |
| 165 | |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 166 | // Walks through the old image and patches the mmap'd copy of it to the new offset. It does not |
| 167 | // change the heap. |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 168 | class PatchVisitor { |
Igor Murashkin | 2ffb703 | 2017-11-08 13:35:21 -0800 | [diff] [blame] | 169 | public: |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 170 | PatchVisitor(PatchOat* patcher, mirror::Object* copy) : patcher_(patcher), copy_(copy) {} |
| 171 | ~PatchVisitor() {} |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 172 | void operator() (ObjPtr<mirror::Object> obj, MemberOffset off, bool b) const |
Mathieu Chartier | da7c650 | 2015-07-23 16:01:26 -0700 | [diff] [blame] | 173 | REQUIRES(Locks::mutator_lock_, Locks::heap_bitmap_lock_); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 174 | // For reference classes. |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 175 | void operator() (ObjPtr<mirror::Class> cls, ObjPtr<mirror::Reference> ref) const |
Mathieu Chartier | da7c650 | 2015-07-23 16:01:26 -0700 | [diff] [blame] | 176 | REQUIRES(Locks::mutator_lock_, Locks::heap_bitmap_lock_); |
| 177 | // TODO: Consider using these for updating native class roots? |
| 178 | void VisitRootIfNonNull(mirror::CompressedReference<mirror::Object>* root ATTRIBUTE_UNUSED) |
| 179 | const {} |
| 180 | void VisitRoot(mirror::CompressedReference<mirror::Object>* root ATTRIBUTE_UNUSED) const {} |
| 181 | |
Igor Murashkin | 2ffb703 | 2017-11-08 13:35:21 -0800 | [diff] [blame] | 182 | private: |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 183 | PatchOat* const patcher_; |
| 184 | mirror::Object* const copy_; |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 185 | }; |
| 186 | |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 187 | // A mmap of the image we are patching. This is modified. |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 188 | const MemMap* const image_; |
| 189 | // The bitmap over the image within the heap we are patching. This is not modified. |
| 190 | gc::accounting::ContinuousSpaceBitmap* const bitmap_; |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 191 | // The heap we are patching. This is not modified. |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 192 | const MemMap* const heap_; |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 193 | // The amount we are changing the offset by. |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 194 | const off_t delta_; |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 195 | // Active instruction set, used to know the entrypoint size. |
| 196 | const InstructionSet isa_; |
| 197 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 198 | const std::map<gc::space::ImageSpace*, std::unique_ptr<MemMap>>* space_map_; |
| 199 | |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 200 | TimingLogger* timings_; |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 201 | |
Vladimir Marko | ad06b98 | 2016-11-17 16:38:59 +0000 | [diff] [blame] | 202 | class FixupRootVisitor; |
| 203 | class RelocatedPointerVisitor; |
| 204 | class PatchOatArtFieldVisitor; |
| 205 | class PatchOatArtMethodVisitor; |
| 206 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 207 | DISALLOW_IMPLICIT_CONSTRUCTORS(PatchOat); |
| 208 | }; |
| 209 | |
| 210 | } // namespace art |
| 211 | #endif // ART_PATCHOAT_PATCHOAT_H_ |