1 /*
2  * Copyright (C) 2017 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_COMPILER_LINKER_LINKER_PATCH_H_
18 #define ART_COMPILER_LINKER_LINKER_PATCH_H_
19 
20 #include <iosfwd>
21 #include <stdint.h>
22 
23 #include <android-base/logging.h>
24 
25 #include "base/bit_utils.h"
26 #include "dex/method_reference.h"
27 
28 namespace art {
29 
30 class DexFile;
31 
32 namespace linker {
33 
34 class LinkerPatch {
35  public:
36   // Note: We explicitly specify the underlying type of the enum because GCC
37   // would otherwise select a bigger underlying type and then complain that
38   //     'art::LinkerPatch::patch_type_' is too small to hold all
39   //     values of 'enum class art::LinkerPatch::Type'
40   // which is ridiculous given we have only a handful of values here. If we
41   // choose to squeeze the Type into fewer than 8 bits, we'll have to declare
42   // patch_type_ as an uintN_t and do explicit static_cast<>s.
43   //
44   // Note: Actual patching is instruction_set-dependent.
45   enum class Type : uint8_t {
46     kIntrinsicReference,      // Boot image reference for an intrinsic, see IntrinsicObjects.
47     kDataBimgRelRo,
48     kMethodRelative,
49     kMethodBssEntry,
50     kCallRelative,
51     kTypeRelative,
52     kTypeBssEntry,
53     kStringRelative,
54     kStringBssEntry,
55     kBakerReadBarrierBranch,
56   };
57 
IntrinsicReferencePatch(size_t literal_offset,uint32_t pc_insn_offset,uint32_t intrinsic_data)58   static LinkerPatch IntrinsicReferencePatch(size_t literal_offset,
59                                              uint32_t pc_insn_offset,
60                                              uint32_t intrinsic_data) {
61     LinkerPatch patch(literal_offset, Type::kIntrinsicReference, /* target_dex_file= */ nullptr);
62     patch.intrinsic_data_ = intrinsic_data;
63     patch.pc_insn_offset_ = pc_insn_offset;
64     return patch;
65   }
66 
DataBimgRelRoPatch(size_t literal_offset,uint32_t pc_insn_offset,uint32_t boot_image_offset)67   static LinkerPatch DataBimgRelRoPatch(size_t literal_offset,
68                                         uint32_t pc_insn_offset,
69                                         uint32_t boot_image_offset) {
70     LinkerPatch patch(literal_offset, Type::kDataBimgRelRo, /* target_dex_file= */ nullptr);
71     patch.boot_image_offset_ = boot_image_offset;
72     patch.pc_insn_offset_ = pc_insn_offset;
73     return patch;
74   }
75 
RelativeMethodPatch(size_t literal_offset,const DexFile * target_dex_file,uint32_t pc_insn_offset,uint32_t target_method_idx)76   static LinkerPatch RelativeMethodPatch(size_t literal_offset,
77                                          const DexFile* target_dex_file,
78                                          uint32_t pc_insn_offset,
79                                          uint32_t target_method_idx) {
80     LinkerPatch patch(literal_offset, Type::kMethodRelative, target_dex_file);
81     patch.method_idx_ = target_method_idx;
82     patch.pc_insn_offset_ = pc_insn_offset;
83     return patch;
84   }
85 
MethodBssEntryPatch(size_t literal_offset,const DexFile * target_dex_file,uint32_t pc_insn_offset,uint32_t target_method_idx)86   static LinkerPatch MethodBssEntryPatch(size_t literal_offset,
87                                          const DexFile* target_dex_file,
88                                          uint32_t pc_insn_offset,
89                                          uint32_t target_method_idx) {
90     LinkerPatch patch(literal_offset, Type::kMethodBssEntry, target_dex_file);
91     patch.method_idx_ = target_method_idx;
92     patch.pc_insn_offset_ = pc_insn_offset;
93     return patch;
94   }
95 
RelativeCodePatch(size_t literal_offset,const DexFile * target_dex_file,uint32_t target_method_idx)96   static LinkerPatch RelativeCodePatch(size_t literal_offset,
97                                        const DexFile* target_dex_file,
98                                        uint32_t target_method_idx) {
99     LinkerPatch patch(literal_offset, Type::kCallRelative, target_dex_file);
100     patch.method_idx_ = target_method_idx;
101     return patch;
102   }
103 
RelativeTypePatch(size_t literal_offset,const DexFile * target_dex_file,uint32_t pc_insn_offset,uint32_t target_type_idx)104   static LinkerPatch RelativeTypePatch(size_t literal_offset,
105                                        const DexFile* target_dex_file,
106                                        uint32_t pc_insn_offset,
107                                        uint32_t target_type_idx) {
108     LinkerPatch patch(literal_offset, Type::kTypeRelative, target_dex_file);
109     patch.type_idx_ = target_type_idx;
110     patch.pc_insn_offset_ = pc_insn_offset;
111     return patch;
112   }
113 
TypeBssEntryPatch(size_t literal_offset,const DexFile * target_dex_file,uint32_t pc_insn_offset,uint32_t target_type_idx)114   static LinkerPatch TypeBssEntryPatch(size_t literal_offset,
115                                        const DexFile* target_dex_file,
116                                        uint32_t pc_insn_offset,
117                                        uint32_t target_type_idx) {
118     LinkerPatch patch(literal_offset, Type::kTypeBssEntry, target_dex_file);
119     patch.type_idx_ = target_type_idx;
120     patch.pc_insn_offset_ = pc_insn_offset;
121     return patch;
122   }
123 
RelativeStringPatch(size_t literal_offset,const DexFile * target_dex_file,uint32_t pc_insn_offset,uint32_t target_string_idx)124   static LinkerPatch RelativeStringPatch(size_t literal_offset,
125                                          const DexFile* target_dex_file,
126                                          uint32_t pc_insn_offset,
127                                          uint32_t target_string_idx) {
128     LinkerPatch patch(literal_offset, Type::kStringRelative, target_dex_file);
129     patch.string_idx_ = target_string_idx;
130     patch.pc_insn_offset_ = pc_insn_offset;
131     return patch;
132   }
133 
StringBssEntryPatch(size_t literal_offset,const DexFile * target_dex_file,uint32_t pc_insn_offset,uint32_t target_string_idx)134   static LinkerPatch StringBssEntryPatch(size_t literal_offset,
135                                          const DexFile* target_dex_file,
136                                          uint32_t pc_insn_offset,
137                                          uint32_t target_string_idx) {
138     LinkerPatch patch(literal_offset, Type::kStringBssEntry, target_dex_file);
139     patch.string_idx_ = target_string_idx;
140     patch.pc_insn_offset_ = pc_insn_offset;
141     return patch;
142   }
143 
144   static LinkerPatch BakerReadBarrierBranchPatch(size_t literal_offset,
145                                                  uint32_t custom_value1 = 0u,
146                                                  uint32_t custom_value2 = 0u) {
147     LinkerPatch patch(literal_offset,
148                       Type::kBakerReadBarrierBranch,
149                       /* target_dex_file= */ nullptr);
150     patch.baker_custom_value1_ = custom_value1;
151     patch.baker_custom_value2_ = custom_value2;
152     return patch;
153   }
154 
155   LinkerPatch(const LinkerPatch& other) = default;
156   LinkerPatch& operator=(const LinkerPatch& other) = default;
157 
LiteralOffset()158   size_t LiteralOffset() const {
159     return literal_offset_;
160   }
161 
GetType()162   Type GetType() const {
163     return patch_type_;
164   }
165 
IntrinsicData()166   uint32_t IntrinsicData() const {
167     DCHECK(patch_type_ == Type::kIntrinsicReference);
168     return intrinsic_data_;
169   }
170 
BootImageOffset()171   uint32_t BootImageOffset() const {
172     DCHECK(patch_type_ == Type::kDataBimgRelRo);
173     return boot_image_offset_;
174   }
175 
TargetMethod()176   MethodReference TargetMethod() const {
177     DCHECK(patch_type_ == Type::kMethodRelative ||
178            patch_type_ == Type::kMethodBssEntry ||
179            patch_type_ == Type::kCallRelative);
180     return MethodReference(target_dex_file_, method_idx_);
181   }
182 
TargetTypeDexFile()183   const DexFile* TargetTypeDexFile() const {
184     DCHECK(patch_type_ == Type::kTypeRelative ||
185            patch_type_ == Type::kTypeBssEntry);
186     return target_dex_file_;
187   }
188 
TargetTypeIndex()189   dex::TypeIndex TargetTypeIndex() const {
190     DCHECK(patch_type_ == Type::kTypeRelative ||
191            patch_type_ == Type::kTypeBssEntry);
192     return dex::TypeIndex(type_idx_);
193   }
194 
TargetStringDexFile()195   const DexFile* TargetStringDexFile() const {
196     DCHECK(patch_type_ == Type::kStringRelative ||
197            patch_type_ == Type::kStringBssEntry);
198     return target_dex_file_;
199   }
200 
TargetStringIndex()201   dex::StringIndex TargetStringIndex() const {
202     DCHECK(patch_type_ == Type::kStringRelative ||
203            patch_type_ == Type::kStringBssEntry);
204     return dex::StringIndex(string_idx_);
205   }
206 
PcInsnOffset()207   uint32_t PcInsnOffset() const {
208     DCHECK(patch_type_ == Type::kIntrinsicReference ||
209            patch_type_ == Type::kDataBimgRelRo ||
210            patch_type_ == Type::kMethodRelative ||
211            patch_type_ == Type::kMethodBssEntry ||
212            patch_type_ == Type::kTypeRelative ||
213            patch_type_ == Type::kTypeBssEntry ||
214            patch_type_ == Type::kStringRelative ||
215            patch_type_ == Type::kStringBssEntry);
216     return pc_insn_offset_;
217   }
218 
GetBakerCustomValue1()219   uint32_t GetBakerCustomValue1() const {
220     DCHECK(patch_type_ == Type::kBakerReadBarrierBranch);
221     return baker_custom_value1_;
222   }
223 
GetBakerCustomValue2()224   uint32_t GetBakerCustomValue2() const {
225     DCHECK(patch_type_ == Type::kBakerReadBarrierBranch);
226     return baker_custom_value2_;
227   }
228 
229  private:
LinkerPatch(size_t literal_offset,Type patch_type,const DexFile * target_dex_file)230   LinkerPatch(size_t literal_offset, Type patch_type, const DexFile* target_dex_file)
231       : target_dex_file_(target_dex_file),
232         literal_offset_(literal_offset),
233         patch_type_(patch_type) {
234     cmp1_ = 0u;
235     cmp2_ = 0u;
236     // The compiler rejects methods that are too big, so the compiled code
237     // of a single method really shouln't be anywhere close to 16MiB.
238     DCHECK(IsUint<24>(literal_offset));
239   }
240 
241   const DexFile* target_dex_file_;
242   // TODO: Clean up naming. Some patched locations are literals but others are not.
243   uint32_t literal_offset_ : 24;  // Method code size up to 16MiB.
244   Type patch_type_ : 8;
245   union {
246     uint32_t cmp1_;               // Used for relational operators.
247     uint32_t boot_image_offset_;  // Data to write to the .data.bimg.rel.ro entry.
248     uint32_t method_idx_;         // Method index for Call/Method patches.
249     uint32_t type_idx_;           // Type index for Type patches.
250     uint32_t string_idx_;         // String index for String patches.
251     uint32_t intrinsic_data_;     // Data for IntrinsicObjects.
252     uint32_t baker_custom_value1_;
253     static_assert(sizeof(method_idx_) == sizeof(cmp1_), "needed by relational operators");
254     static_assert(sizeof(type_idx_) == sizeof(cmp1_), "needed by relational operators");
255     static_assert(sizeof(string_idx_) == sizeof(cmp1_), "needed by relational operators");
256     static_assert(sizeof(intrinsic_data_) == sizeof(cmp1_), "needed by relational operators");
257     static_assert(sizeof(baker_custom_value1_) == sizeof(cmp1_), "needed by relational operators");
258   };
259   union {
260     // Note: To avoid uninitialized padding on 64-bit systems, we use `size_t` for `cmp2_`.
261     // This allows a hashing function to treat an array of linker patches as raw memory.
262     size_t cmp2_;             // Used for relational operators.
263     // Literal offset of the insn loading PC (same as literal_offset if it's the same insn,
264     // may be different if the PC-relative addressing needs multiple insns).
265     uint32_t pc_insn_offset_;
266     uint32_t baker_custom_value2_;
267     static_assert(sizeof(pc_insn_offset_) <= sizeof(cmp2_), "needed by relational operators");
268     static_assert(sizeof(baker_custom_value2_) <= sizeof(cmp2_), "needed by relational operators");
269   };
270 
271   friend bool operator==(const LinkerPatch& lhs, const LinkerPatch& rhs);
272   friend bool operator<(const LinkerPatch& lhs, const LinkerPatch& rhs);
273 };
274 std::ostream& operator<<(std::ostream& os, const LinkerPatch::Type& type);
275 
276 inline bool operator==(const LinkerPatch& lhs, const LinkerPatch& rhs) {
277   return lhs.literal_offset_ == rhs.literal_offset_ &&
278       lhs.patch_type_ == rhs.patch_type_ &&
279       lhs.target_dex_file_ == rhs.target_dex_file_ &&
280       lhs.cmp1_ == rhs.cmp1_ &&
281       lhs.cmp2_ == rhs.cmp2_;
282 }
283 
284 inline bool operator<(const LinkerPatch& lhs, const LinkerPatch& rhs) {
285   return (lhs.literal_offset_ != rhs.literal_offset_) ? lhs.literal_offset_ < rhs.literal_offset_
286       : (lhs.patch_type_ != rhs.patch_type_) ? lhs.patch_type_ < rhs.patch_type_
287       : (lhs.target_dex_file_ != rhs.target_dex_file_) ? lhs.target_dex_file_ < rhs.target_dex_file_
288       : (lhs.cmp1_ != rhs.cmp1_) ? lhs.cmp1_ < rhs.cmp1_
289       : lhs.cmp2_ < rhs.cmp2_;
290 }
291 
292 }  // namespace linker
293 }  // namespace art
294 
295 #endif  // ART_COMPILER_LINKER_LINKER_PATCH_H_
296