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/macros.h"
22 #include "base/mutex.h"
23 #include "elf_file.h"
24 #include "elf_utils.h"
25 #include "gc/accounting/space_bitmap.h"
26 #include "gc/heap.h"
27 #include "os.h"
28 
29 namespace art {
30 
31 class ArtMethod;
32 class ImageHeader;
33 class OatHeader;
34 
35 namespace mirror {
36 class Object;
37 class PointerArray;
38 class Reference;
39 class Class;
40 }  // namespace mirror
41 
42 class PatchOat {
43  public:
44   // Patch only the oat file
45   static bool Patch(File* oat_in, off_t delta, File* oat_out, TimingLogger* timings,
46                     bool output_oat_opened_from_fd,  // Was this using --oatput-oat-fd ?
47                     bool new_oat_out);               // Output oat was a new file created by us?
48 
49   // Patch only the image (art file)
50   static bool Patch(const std::string& art_location, off_t delta, File* art_out, InstructionSet isa,
51                     TimingLogger* timings);
52 
53   // Patch both the image and the oat file
54   static bool Patch(File* oat_in, const std::string& art_location,
55                     off_t delta, File* oat_out, File* art_out, InstructionSet isa,
56                     TimingLogger* timings,
57                     bool output_oat_opened_from_fd,  // Was this using --oatput-oat-fd ?
58                     bool new_oat_out);               // Output oat was a new file created by us?
59 
60  private:
61   // Takes ownership only of the ElfFile. All other pointers are only borrowed.
PatchOat(ElfFile * oat_file,off_t delta,TimingLogger * timings)62   PatchOat(ElfFile* oat_file, off_t delta, TimingLogger* timings)
63       : oat_file_(oat_file), image_(nullptr), bitmap_(nullptr), heap_(nullptr), delta_(delta),
64         isa_(kNone), timings_(timings) {}
PatchOat(InstructionSet isa,MemMap * image,gc::accounting::ContinuousSpaceBitmap * bitmap,MemMap * heap,off_t delta,TimingLogger * timings)65   PatchOat(InstructionSet isa, MemMap* image, gc::accounting::ContinuousSpaceBitmap* bitmap,
66            MemMap* heap, off_t delta, TimingLogger* timings)
67       : image_(image), bitmap_(bitmap), heap_(heap),
68         delta_(delta), isa_(isa), timings_(timings) {}
PatchOat(InstructionSet isa,ElfFile * oat_file,MemMap * image,gc::accounting::ContinuousSpaceBitmap * bitmap,MemMap * heap,off_t delta,TimingLogger * timings)69   PatchOat(InstructionSet isa, ElfFile* oat_file, MemMap* image,
70            gc::accounting::ContinuousSpaceBitmap* bitmap, MemMap* heap, off_t delta,
71            TimingLogger* timings)
72       : oat_file_(oat_file), image_(image), bitmap_(bitmap), heap_(heap),
73         delta_(delta), isa_(isa), timings_(timings) {}
~PatchOat()74   ~PatchOat() {}
75 
76   // Was the .art image at image_path made with --compile-pic ?
77   static bool IsImagePic(const ImageHeader& image_header, const std::string& image_path);
78 
79   enum MaybePic {
80       NOT_PIC,            // Code not pic. Patch as usual.
81       PIC,                // Code was pic. Create symlink; skip OAT patching.
82       ERROR_OAT_FILE,     // Failed to symlink oat file
83       ERROR_FIRST = ERROR_OAT_FILE,
84   };
85 
86   // Was the .oat image at oat_in made with --compile-pic ?
87   static MaybePic IsOatPic(const ElfFile* oat_in);
88 
89   // Attempt to replace the file with a symlink
90   // Returns false if it fails
91   static bool ReplaceOatFileWithSymlink(const std::string& input_oat_filename,
92                                         const std::string& output_oat_filename,
93                                         bool output_oat_opened_from_fd,
94                                         bool new_oat_out);  // Output oat was newly created?
95 
BitmapCallback(mirror::Object * obj,void * arg)96   static void BitmapCallback(mirror::Object* obj, void* arg)
97       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
98     reinterpret_cast<PatchOat*>(arg)->VisitObject(obj);
99   }
100 
101   void VisitObject(mirror::Object* obj)
102       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
103   void FixupMethod(ArtMethod* object, ArtMethod* copy)
104       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
105   void FixupNativePointerArray(mirror::PointerArray* object)
106       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
107   bool InHeap(mirror::Object*);
108 
109   // Patches oat in place, modifying the oat_file given to the constructor.
110   bool PatchElf();
111   template <typename ElfFileImpl>
112   bool PatchElf(ElfFileImpl* oat_file);
113   template <typename ElfFileImpl>
114   bool PatchOatHeader(ElfFileImpl* oat_file);
115 
116   bool PatchImage() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
117   void PatchArtFields(const ImageHeader* image_header) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
118   void PatchArtMethods(const ImageHeader* image_header) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
119   void PatchInternedStrings(const ImageHeader* image_header)
120       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
121   void PatchDexFileArrays(mirror::ObjectArray<mirror::Object>* img_roots)
122       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
123 
124   bool WriteElf(File* out);
125   bool WriteImage(File* out);
126 
127   template <typename T>
RelocatedCopyOf(T * obj)128   T* RelocatedCopyOf(T* obj) const {
129     if (obj == nullptr) {
130       return nullptr;
131     }
132     DCHECK_GT(reinterpret_cast<uintptr_t>(obj), reinterpret_cast<uintptr_t>(heap_->Begin()));
133     DCHECK_LT(reinterpret_cast<uintptr_t>(obj), reinterpret_cast<uintptr_t>(heap_->End()));
134     uintptr_t heap_off =
135         reinterpret_cast<uintptr_t>(obj) - reinterpret_cast<uintptr_t>(heap_->Begin());
136     DCHECK_LT(heap_off, image_->Size());
137     return reinterpret_cast<T*>(image_->Begin() + heap_off);
138   }
139 
140   template <typename T>
RelocatedAddressOfPointer(T * obj)141   T* RelocatedAddressOfPointer(T* obj) const {
142     if (obj == nullptr) {
143       return obj;
144     }
145     auto ret = reinterpret_cast<uintptr_t>(obj) + delta_;
146     // Trim off high bits in case negative relocation with 64 bit patchoat.
147     if (InstructionSetPointerSize(isa_) == sizeof(uint32_t)) {
148       ret = static_cast<uintptr_t>(static_cast<uint32_t>(ret));
149     }
150     return reinterpret_cast<T*>(ret);
151   }
152 
153   template <typename T>
RelocatedAddressOfIntPointer(T obj)154   T RelocatedAddressOfIntPointer(T obj) const {
155     if (obj == 0) {
156       return obj;
157     }
158     T ret = obj + delta_;
159     // Trim off high bits in case negative relocation with 64 bit patchoat.
160     if (InstructionSetPointerSize(isa_) == 4) {
161       ret = static_cast<T>(static_cast<uint32_t>(ret));
162     }
163     return ret;
164   }
165 
166   // Look up the oat header from any elf file.
167   static const OatHeader* GetOatHeader(const ElfFile* elf_file);
168 
169   // Templatized version to actually look up the oat header
170   template <typename ElfFileImpl>
171   static const OatHeader* GetOatHeader(const ElfFileImpl* elf_file);
172 
173   // Walks through the old image and patches the mmap'd copy of it to the new offset. It does not
174   // change the heap.
175   class PatchVisitor {
176   public:
PatchVisitor(PatchOat * patcher,mirror::Object * copy)177     PatchVisitor(PatchOat* patcher, mirror::Object* copy) : patcher_(patcher), copy_(copy) {}
~PatchVisitor()178     ~PatchVisitor() {}
179     void operator() (mirror::Object* obj, MemberOffset off, bool b) const
180       EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_);
181     // For reference classes.
182     void operator() (mirror::Class* cls, mirror::Reference* ref) const
183       EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_);
184   private:
185     PatchOat* const patcher_;
186     mirror::Object* const copy_;
187   };
188 
189   // The elf file we are patching.
190   std::unique_ptr<ElfFile> oat_file_;
191   // A mmap of the image we are patching. This is modified.
192   const MemMap* const image_;
193   // The bitmap over the image within the heap we are patching. This is not modified.
194   gc::accounting::ContinuousSpaceBitmap* const bitmap_;
195   // The heap we are patching. This is not modified.
196   const MemMap* const heap_;
197   // The amount we are changing the offset by.
198   const off_t delta_;
199   // Active instruction set, used to know the entrypoint size.
200   const InstructionSet isa_;
201 
202   TimingLogger* timings_;
203 
204   friend class FixupRootVisitor;
205   DISALLOW_IMPLICIT_CONSTRUCTORS(PatchOat);
206 };
207 
208 }  // namespace art
209 #endif  // ART_PATCHOAT_PATCHOAT_H_
210