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: |
Richard Uhler | 4bc11d0 | 2017-02-01 09:53:54 +0000 | [diff] [blame] | 47 | static bool Patch(const std::string& image_location, |
Andreas Gampe | 6eb6a39 | 2016-02-10 20:18:37 -0800 | [diff] [blame] | 48 | off_t delta, |
| 49 | const std::string& output_directory, |
| 50 | InstructionSet isa, |
| 51 | TimingLogger* timings); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 52 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 53 | ~PatchOat() {} |
| 54 | PatchOat(PatchOat&&) = default; |
| 55 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 56 | private: |
Richard Uhler | 4bc11d0 | 2017-02-01 09:53:54 +0000 | [diff] [blame] | 57 | // All pointers are only borrowed. |
| 58 | PatchOat(InstructionSet isa, MemMap* image, |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 59 | gc::accounting::ContinuousSpaceBitmap* bitmap, MemMap* heap, off_t delta, |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 60 | std::map<gc::space::ImageSpace*, std::unique_ptr<MemMap>>* map, TimingLogger* timings) |
Richard Uhler | 4bc11d0 | 2017-02-01 09:53:54 +0000 | [diff] [blame] | 61 | : image_(image), bitmap_(bitmap), heap_(heap), |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 62 | delta_(delta), isa_(isa), space_map_(map), timings_(timings) {} |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 63 | |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 64 | // Was the .art image at image_path made with --compile-pic ? |
| 65 | static bool IsImagePic(const ImageHeader& image_header, const std::string& image_path); |
| 66 | |
| 67 | enum MaybePic { |
| 68 | NOT_PIC, // Code not pic. Patch as usual. |
| 69 | PIC, // Code was pic. Create symlink; skip OAT patching. |
| 70 | ERROR_OAT_FILE, // Failed to symlink oat file |
| 71 | ERROR_FIRST = ERROR_OAT_FILE, |
| 72 | }; |
| 73 | |
| 74 | // Was the .oat image at oat_in made with --compile-pic ? |
| 75 | static MaybePic IsOatPic(const ElfFile* oat_in); |
| 76 | |
| 77 | // Attempt to replace the file with a symlink |
| 78 | // Returns false if it fails |
| 79 | static bool ReplaceOatFileWithSymlink(const std::string& input_oat_filename, |
Richard Uhler | 4bc11d0 | 2017-02-01 09:53:54 +0000 | [diff] [blame] | 80 | const std::string& output_oat_filename); |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 81 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 82 | static void BitmapCallback(mirror::Object* obj, void* arg) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 83 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 84 | reinterpret_cast<PatchOat*>(arg)->VisitObject(obj); |
| 85 | } |
| 86 | |
| 87 | void VisitObject(mirror::Object* obj) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 88 | REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 89 | void FixupMethod(ArtMethod* object, ArtMethod* copy) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 90 | REQUIRES_SHARED(Locks::mutator_lock_); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 91 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 92 | bool PatchImage(bool primary_image) REQUIRES_SHARED(Locks::mutator_lock_); |
| 93 | void PatchArtFields(const ImageHeader* image_header) REQUIRES_SHARED(Locks::mutator_lock_); |
| 94 | void PatchArtMethods(const ImageHeader* image_header) REQUIRES_SHARED(Locks::mutator_lock_); |
| 95 | void PatchImTables(const ImageHeader* image_header) REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 96 | void PatchImtConflictTables(const ImageHeader* image_header) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 97 | REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 98 | void PatchInternedStrings(const ImageHeader* image_header) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 99 | REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | 208a5cb | 2015-12-02 15:44:07 -0800 | [diff] [blame] | 100 | void PatchClassTable(const ImageHeader* image_header) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 101 | REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 102 | void PatchDexFileArrays(mirror::ObjectArray<mirror::Object>* img_roots) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 103 | REQUIRES_SHARED(Locks::mutator_lock_); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 104 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 105 | bool WriteImage(File* out); |
| 106 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 107 | template <typename T> |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 108 | T* RelocatedCopyOf(T* obj) const { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 109 | if (obj == nullptr) { |
| 110 | return nullptr; |
| 111 | } |
Jeff Hao | 0d2af30 | 2016-01-04 17:38:06 -0800 | [diff] [blame] | 112 | DCHECK_GT(reinterpret_cast<uintptr_t>(obj), reinterpret_cast<uintptr_t>(heap_->Begin())); |
| 113 | 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] | 114 | uintptr_t heap_off = |
| 115 | reinterpret_cast<uintptr_t>(obj) - reinterpret_cast<uintptr_t>(heap_->Begin()); |
Jeff Hao | 0d2af30 | 2016-01-04 17:38:06 -0800 | [diff] [blame] | 116 | DCHECK_LT(heap_off, image_->Size()); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 117 | return reinterpret_cast<T*>(image_->Begin() + heap_off); |
| 118 | } |
| 119 | |
| 120 | template <typename T> |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 121 | T* RelocatedCopyOfFollowImages(T* obj) const { |
| 122 | if (obj == nullptr) { |
| 123 | return nullptr; |
| 124 | } |
| 125 | // Find ImageSpace this belongs to. |
| 126 | auto image_spaces = Runtime::Current()->GetHeap()->GetBootImageSpaces(); |
| 127 | for (gc::space::ImageSpace* image_space : image_spaces) { |
| 128 | if (image_space->Contains(obj)) { |
| 129 | uintptr_t heap_off = reinterpret_cast<uintptr_t>(obj) - |
| 130 | reinterpret_cast<uintptr_t>(image_space->GetMemMap()->Begin()); |
| 131 | return reinterpret_cast<T*>(space_map_->find(image_space)->second->Begin() + heap_off); |
| 132 | } |
| 133 | } |
| 134 | LOG(FATAL) << "Did not find object in boot image space " << obj; |
| 135 | UNREACHABLE(); |
| 136 | } |
| 137 | |
| 138 | template <typename T> |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 139 | T* RelocatedAddressOfPointer(T* obj) const { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 140 | if (obj == nullptr) { |
| 141 | return obj; |
| 142 | } |
| 143 | auto ret = reinterpret_cast<uintptr_t>(obj) + delta_; |
| 144 | // Trim off high bits in case negative relocation with 64 bit patchoat. |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 145 | if (Is32BitISA()) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 146 | ret = static_cast<uintptr_t>(static_cast<uint32_t>(ret)); |
| 147 | } |
| 148 | return reinterpret_cast<T*>(ret); |
| 149 | } |
| 150 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 151 | bool Is32BitISA() const { |
| 152 | return InstructionSetPointerSize(isa_) == PointerSize::k32; |
| 153 | } |
| 154 | |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 155 | // Walks through the old image and patches the mmap'd copy of it to the new offset. It does not |
| 156 | // change the heap. |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 157 | class PatchVisitor { |
| 158 | public: |
| 159 | PatchVisitor(PatchOat* patcher, mirror::Object* copy) : patcher_(patcher), copy_(copy) {} |
| 160 | ~PatchVisitor() {} |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 161 | void operator() (ObjPtr<mirror::Object> obj, MemberOffset off, bool b) const |
Mathieu Chartier | da7c650 | 2015-07-23 16:01:26 -0700 | [diff] [blame] | 162 | REQUIRES(Locks::mutator_lock_, Locks::heap_bitmap_lock_); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 163 | // For reference classes. |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 164 | void operator() (ObjPtr<mirror::Class> cls, ObjPtr<mirror::Reference> ref) const |
Mathieu Chartier | da7c650 | 2015-07-23 16:01:26 -0700 | [diff] [blame] | 165 | REQUIRES(Locks::mutator_lock_, Locks::heap_bitmap_lock_); |
| 166 | // TODO: Consider using these for updating native class roots? |
| 167 | void VisitRootIfNonNull(mirror::CompressedReference<mirror::Object>* root ATTRIBUTE_UNUSED) |
| 168 | const {} |
| 169 | void VisitRoot(mirror::CompressedReference<mirror::Object>* root ATTRIBUTE_UNUSED) const {} |
| 170 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 171 | private: |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 172 | PatchOat* const patcher_; |
| 173 | mirror::Object* const copy_; |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 174 | }; |
| 175 | |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 176 | // A mmap of the image we are patching. This is modified. |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 177 | const MemMap* const image_; |
| 178 | // The bitmap over the image within the heap we are patching. This is not modified. |
| 179 | gc::accounting::ContinuousSpaceBitmap* const bitmap_; |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 180 | // The heap we are patching. This is not modified. |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 181 | const MemMap* const heap_; |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 182 | // The amount we are changing the offset by. |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 183 | const off_t delta_; |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 184 | // Active instruction set, used to know the entrypoint size. |
| 185 | const InstructionSet isa_; |
| 186 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 187 | const std::map<gc::space::ImageSpace*, std::unique_ptr<MemMap>>* space_map_; |
| 188 | |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 189 | TimingLogger* timings_; |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 190 | |
Vladimir Marko | ad06b98 | 2016-11-17 16:38:59 +0000 | [diff] [blame] | 191 | class FixupRootVisitor; |
| 192 | class RelocatedPointerVisitor; |
| 193 | class PatchOatArtFieldVisitor; |
| 194 | class PatchOatArtMethodVisitor; |
| 195 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 196 | DISALLOW_IMPLICIT_CONSTRUCTORS(PatchOat); |
| 197 | }; |
| 198 | |
| 199 | } // namespace art |
| 200 | #endif // ART_PATCHOAT_PATCHOAT_H_ |