• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/enums.h"
22 #include "base/macros.h"
23 #include "base/mutex.h"
24 #include "elf_file.h"
25 #include "elf_utils.h"
26 #include "gc/accounting/space_bitmap.h"
27 #include "gc/space/image_space.h"
28 #include "gc/heap.h"
29 #include "os.h"
30 #include "runtime.h"
31 
32 namespace art {
33 
34 class ArtMethod;
35 class ImageHeader;
36 class OatHeader;
37 
38 namespace mirror {
39 class Object;
40 class PointerArray;
41 class Reference;
42 class Class;
43 }  // namespace mirror
44 
45 class PatchOat {
46  public:
47   static bool Patch(const std::string& image_location,
48                     off_t delta,
49                     const std::string& output_directory,
50                     InstructionSet isa,
51                     TimingLogger* timings);
52 
~PatchOat()53   ~PatchOat() {}
54   PatchOat(PatchOat&&) = default;
55 
56  private:
57   // All pointers are only borrowed.
PatchOat(InstructionSet isa,MemMap * image,gc::accounting::ContinuousSpaceBitmap * bitmap,MemMap * heap,off_t delta,std::map<gc::space::ImageSpace *,std::unique_ptr<MemMap>> * map,TimingLogger * timings)58   PatchOat(InstructionSet isa, MemMap* image,
59            gc::accounting::ContinuousSpaceBitmap* bitmap, MemMap* heap, off_t delta,
60            std::map<gc::space::ImageSpace*, std::unique_ptr<MemMap>>* map, TimingLogger* timings)
61       : image_(image), bitmap_(bitmap), heap_(heap),
62         delta_(delta), isa_(isa), space_map_(map), timings_(timings) {}
63 
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,
80                                         const std::string& output_oat_filename);
81 
82   void VisitObject(mirror::Object* obj)
83       REQUIRES_SHARED(Locks::mutator_lock_);
84   void FixupMethod(ArtMethod* object, ArtMethod* copy)
85       REQUIRES_SHARED(Locks::mutator_lock_);
86 
87   bool PatchImage(bool primary_image) REQUIRES_SHARED(Locks::mutator_lock_);
88   void PatchArtFields(const ImageHeader* image_header) REQUIRES_SHARED(Locks::mutator_lock_);
89   void PatchArtMethods(const ImageHeader* image_header) REQUIRES_SHARED(Locks::mutator_lock_);
90   void PatchImTables(const ImageHeader* image_header) REQUIRES_SHARED(Locks::mutator_lock_);
91   void PatchImtConflictTables(const ImageHeader* image_header)
92       REQUIRES_SHARED(Locks::mutator_lock_);
93   void PatchInternedStrings(const ImageHeader* image_header)
94       REQUIRES_SHARED(Locks::mutator_lock_);
95   void PatchClassTable(const ImageHeader* image_header)
96       REQUIRES_SHARED(Locks::mutator_lock_);
97   void PatchDexFileArrays(mirror::ObjectArray<mirror::Object>* img_roots)
98       REQUIRES_SHARED(Locks::mutator_lock_);
99 
100   bool WriteImage(File* out);
101 
102   template <typename T>
RelocatedCopyOf(T * obj)103   T* RelocatedCopyOf(T* obj) const {
104     if (obj == nullptr) {
105       return nullptr;
106     }
107     DCHECK_GT(reinterpret_cast<uintptr_t>(obj), reinterpret_cast<uintptr_t>(heap_->Begin()));
108     DCHECK_LT(reinterpret_cast<uintptr_t>(obj), reinterpret_cast<uintptr_t>(heap_->End()));
109     uintptr_t heap_off =
110         reinterpret_cast<uintptr_t>(obj) - reinterpret_cast<uintptr_t>(heap_->Begin());
111     DCHECK_LT(heap_off, image_->Size());
112     return reinterpret_cast<T*>(image_->Begin() + heap_off);
113   }
114 
115   template <typename T>
RelocatedCopyOfFollowImages(T * obj)116   T* RelocatedCopyOfFollowImages(T* obj) const {
117     if (obj == nullptr) {
118       return nullptr;
119     }
120     // Find ImageSpace this belongs to.
121     auto image_spaces = Runtime::Current()->GetHeap()->GetBootImageSpaces();
122     for (gc::space::ImageSpace* image_space : image_spaces) {
123       if (image_space->Contains(obj)) {
124         uintptr_t heap_off = reinterpret_cast<uintptr_t>(obj) -
125                              reinterpret_cast<uintptr_t>(image_space->GetMemMap()->Begin());
126         return reinterpret_cast<T*>(space_map_->find(image_space)->second->Begin() + heap_off);
127       }
128     }
129     LOG(FATAL) << "Did not find object in boot image space " << obj;
130     UNREACHABLE();
131   }
132 
133   template <typename T>
RelocatedAddressOfPointer(T * obj)134   T* RelocatedAddressOfPointer(T* obj) const {
135     if (obj == nullptr) {
136       return obj;
137     }
138     auto ret = reinterpret_cast<uintptr_t>(obj) + delta_;
139     // Trim off high bits in case negative relocation with 64 bit patchoat.
140     if (Is32BitISA()) {
141       ret = static_cast<uintptr_t>(static_cast<uint32_t>(ret));
142     }
143     return reinterpret_cast<T*>(ret);
144   }
145 
Is32BitISA()146   bool Is32BitISA() const {
147     return InstructionSetPointerSize(isa_) == PointerSize::k32;
148   }
149 
150   // Walks through the old image and patches the mmap'd copy of it to the new offset. It does not
151   // change the heap.
152   class PatchVisitor {
153   public:
PatchVisitor(PatchOat * patcher,mirror::Object * copy)154     PatchVisitor(PatchOat* patcher, mirror::Object* copy) : patcher_(patcher), copy_(copy) {}
~PatchVisitor()155     ~PatchVisitor() {}
156     void operator() (ObjPtr<mirror::Object> obj, MemberOffset off, bool b) const
157         REQUIRES(Locks::mutator_lock_, Locks::heap_bitmap_lock_);
158     // For reference classes.
159     void operator() (ObjPtr<mirror::Class> cls, ObjPtr<mirror::Reference>  ref) const
160         REQUIRES(Locks::mutator_lock_, Locks::heap_bitmap_lock_);
161     // TODO: Consider using these for updating native class roots?
VisitRootIfNonNull(mirror::CompressedReference<mirror::Object> * root ATTRIBUTE_UNUSED)162     void VisitRootIfNonNull(mirror::CompressedReference<mirror::Object>* root ATTRIBUTE_UNUSED)
163         const {}
VisitRoot(mirror::CompressedReference<mirror::Object> * root ATTRIBUTE_UNUSED)164     void VisitRoot(mirror::CompressedReference<mirror::Object>* root ATTRIBUTE_UNUSED) const {}
165 
166   private:
167     PatchOat* const patcher_;
168     mirror::Object* const copy_;
169   };
170 
171   // A mmap of the image we are patching. This is modified.
172   const MemMap* const image_;
173   // The bitmap over the image within the heap we are patching. This is not modified.
174   gc::accounting::ContinuousSpaceBitmap* const bitmap_;
175   // The heap we are patching. This is not modified.
176   const MemMap* const heap_;
177   // The amount we are changing the offset by.
178   const off_t delta_;
179   // Active instruction set, used to know the entrypoint size.
180   const InstructionSet isa_;
181 
182   const std::map<gc::space::ImageSpace*, std::unique_ptr<MemMap>>* space_map_;
183 
184   TimingLogger* timings_;
185 
186   class FixupRootVisitor;
187   class RelocatedPointerVisitor;
188   class PatchOatArtFieldVisitor;
189   class PatchOatArtMethodVisitor;
190 
191   DISALLOW_IMPLICIT_CONSTRUCTORS(PatchOat);
192 };
193 
194 }  // namespace art
195 #endif  // ART_PATCHOAT_PATCHOAT_H_
196