• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2023 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 #include "small_pattern_matcher.h"
18 
19 #include "art_method-inl.h"
20 #include "dex/dex_instruction-inl.h"
21 #include "entrypoints/entrypoint_utils-inl.h"
22 
23 namespace art HIDDEN {
24 namespace jit {
25 
26 // The following methods will be directly invoked by our own JIT/AOT compiled
27 // code.
28 
EmptyMethod()29 static void EmptyMethod() {}
ReturnZero()30 static int32_t ReturnZero() { return 0; }
ReturnOne()31 static int32_t ReturnOne() { return 1; }
ReturnFirstArgMethod(ArtMethod * method,int32_t first_arg)32 static int32_t ReturnFirstArgMethod([[maybe_unused]] ArtMethod* method, int32_t first_arg) {
33   return first_arg;
34 }
35 
36 template <int offset, typename T>
ReturnFieldAt(ArtMethod * method,mirror::Object * obj)37 static std::conditional_t<(sizeof(T) < sizeof(int32_t)), int32_t, T> ReturnFieldAt(
38     [[maybe_unused]] ArtMethod* method, mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_) {
39   return obj->GetFieldPrimitive<T, /* kIsVolatile= */ false>(
40       MemberOffset(offset + sizeof(mirror::Object)));
41 }
42 
43 template <int offset, typename unused>
ReturnFieldObjectAt(ArtMethod * method,mirror::Object * obj)44 static mirror::Object* ReturnFieldObjectAt([[maybe_unused]] ArtMethod* method, mirror::Object* obj)
45     REQUIRES_SHARED(Locks::mutator_lock_) {
46   return obj->GetFieldObject<mirror::Object>(MemberOffset(offset + sizeof(mirror::Object)));
47 }
48 
49 template <int offset, typename T>
ReturnStaticFieldAt(ArtMethod * method)50 static std::conditional_t<(sizeof(T) < sizeof(int32_t)), int32_t, T> ReturnStaticFieldAt(
51     ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_) {
52   ObjPtr<mirror::Class> cls = method->GetDeclaringClass();
53   MemberOffset first_field_offset = cls->GetFirstReferenceStaticFieldOffset(kRuntimePointerSize);
54   return cls->GetFieldPrimitive<T, /* kIsVolatile= */ false>(
55       MemberOffset(offset + first_field_offset.Int32Value()));
56 }
57 
58 template <int offset, typename unused>
ReturnStaticFieldObjectAt(ArtMethod * method)59 static mirror::Object* ReturnStaticFieldObjectAt(ArtMethod* method)
60     REQUIRES_SHARED(Locks::mutator_lock_) {
61   ObjPtr<mirror::Class> cls = method->GetDeclaringClass();
62   MemberOffset first_field_offset = cls->GetFirstReferenceStaticFieldOffset(kRuntimePointerSize);
63   return cls->GetFieldObject<mirror::Object>(
64       MemberOffset(offset + first_field_offset.Int32Value()));
65 }
66 
67 template <int offset, typename T>
SetFieldAt(ArtMethod * method,mirror::Object * obj,T value)68 static void SetFieldAt([[maybe_unused]] ArtMethod* method, mirror::Object* obj, T value)
69     REQUIRES_SHARED(Locks::mutator_lock_) {
70   obj->SetFieldPrimitive<T, /* kIsVolatile= */ false>(
71       MemberOffset(offset + sizeof(mirror::Object)), value);
72 }
73 
74 template <int offset, typename unused>
SetFieldObjectAt(ArtMethod * method,mirror::Object * obj,mirror::Object * value)75 static void SetFieldObjectAt([[maybe_unused]] ArtMethod* method,
76                              mirror::Object* obj,
77                              mirror::Object* value)
78     REQUIRES_SHARED(Locks::mutator_lock_) {
79   obj->SetFieldObject</* kTransactionActive */ false>(
80       MemberOffset(offset + sizeof(mirror::Object)), value);
81 }
82 
83 template <int offset, typename T>
ConstructorSetFieldAt(ArtMethod * method,mirror::Object * obj,T value)84 static void ConstructorSetFieldAt([[maybe_unused]] ArtMethod* method, mirror::Object* obj, T value)
85     REQUIRES_SHARED(Locks::mutator_lock_) {
86   obj->SetFieldPrimitive<T, /* kIsVolatile= */ false>(
87       MemberOffset(offset + sizeof(mirror::Object)), value);
88   QuasiAtomic::ThreadFenceForConstructor();
89 }
90 
91 template <int offset, typename unused>
ConstructorSetFieldObjectAt(ArtMethod * method,mirror::Object * obj,mirror::Object * value)92 static void ConstructorSetFieldObjectAt([[maybe_unused]] ArtMethod* method,
93                                         mirror::Object* obj,
94                                         mirror::Object* value)
95     REQUIRES_SHARED(Locks::mutator_lock_) {
96   obj->SetFieldObject</* kTransactionActive */ false>(
97       MemberOffset(offset + sizeof(mirror::Object)), value);
98   QuasiAtomic::ThreadFenceForConstructor();
99 }
100 
101 #define SWITCH_CASE(offset, func, type) \
102   case offset:                          \
103     return reinterpret_cast<void*>(&func<offset, type>);  // NOLINT [bugprone-macro-parentheses]
104 
105 #define DO_SWITCH_OFFSET(offset, F, T) \
106   switch (offset) { \
107     SWITCH_CASE(0, F, T) \
108     SWITCH_CASE(4, F, T) \
109     SWITCH_CASE(8, F, T) \
110     SWITCH_CASE(12, F, T) \
111     SWITCH_CASE(16, F, T) \
112     SWITCH_CASE(20, F, T) \
113     SWITCH_CASE(24, F, T) \
114     SWITCH_CASE(28, F, T) \
115     SWITCH_CASE(32, F, T) \
116     SWITCH_CASE(36, F, T) \
117     SWITCH_CASE(40, F, T) \
118     SWITCH_CASE(44, F, T) \
119     SWITCH_CASE(48, F, T) \
120     SWITCH_CASE(52, F, T) \
121     SWITCH_CASE(56, F, T) \
122     SWITCH_CASE(60, F, T) \
123     SWITCH_CASE(64, F, T) \
124     default: return nullptr; \
125   }
126 
127 #define DO_SWITCH(offset, O, P, K)                  \
128   DCHECK_EQ(is_object, (K) == Primitive::kPrimNot); \
129   switch (K) {                                      \
130     case Primitive::kPrimBoolean:                   \
131       DO_SWITCH_OFFSET(offset, P, uint8_t);         \
132     case Primitive::kPrimInt:                       \
133       DO_SWITCH_OFFSET(offset, P, int32_t);         \
134     case Primitive::kPrimLong:                      \
135       DO_SWITCH_OFFSET(offset, P, int64_t);         \
136     case Primitive::kPrimNot:                       \
137       DO_SWITCH_OFFSET(offset, O, mirror::Object*); \
138     case Primitive::kPrimFloat:                     \
139       if (kRuntimeISA == InstructionSet::kArm64) {  \
140         DO_SWITCH_OFFSET(offset, P, float);         \
141       } else {                                      \
142         return nullptr;                             \
143       }                                             \
144     case Primitive::kPrimDouble:                    \
145       if (kRuntimeISA == InstructionSet::kArm64) {  \
146         DO_SWITCH_OFFSET(offset, P, double);        \
147       } else {                                      \
148         return nullptr;                             \
149       }                                             \
150     default:                                        \
151       return nullptr;                               \
152   }
153 
TryMatch(ArtMethod * method)154 const void* SmallPatternMatcher::TryMatch(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_) {
155   CodeItemDataAccessor accessor(*method->GetDexFile(), method->GetCodeItem());
156 
157   ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
158 
159   bool is_recognizable_constructor =
160       method->IsConstructor() &&
161       !method->IsStatic() &&
162       method->GetDeclaringClass()->GetSuperClass() != nullptr &&
163       method->GetDeclaringClass()->GetSuperClass()->IsObjectClass();
164 
165   size_t insns_size = accessor.InsnsSizeInCodeUnits();
166   if (insns_size >= 4u) {
167     if (!is_recognizable_constructor) {
168       return nullptr;
169     }
170     // We can recognize a constructor with 6 or 4 code units.
171     if (insns_size != 4u && insns_size != 6u) {
172       return nullptr;
173     }
174   }
175 
176   auto is_object_init_invoke = [&](const Instruction& instruction)
177       REQUIRES_SHARED(Locks::mutator_lock_) {
178     uint16_t method_idx = instruction.VRegB_35c();
179     Thread* self = Thread::Current();
180     ArtMethod* target_method =
181         class_linker->ResolveMethod<ClassLinker::ResolveMode::kNoChecks>(self,
182                                                                          method_idx,
183                                                                          method,
184                                                                          kDirect);
185     if (target_method == nullptr) {
186       self->ClearException();
187       return false;
188     }
189     if (!target_method->GetDeclaringClass()->IsObjectClass()) {
190       return false;
191     }
192     DCHECK(target_method->GetDeclaringClass()->IsVerified());
193     CodeItemDataAccessor accessor(*target_method->GetDexFile(), target_method->GetCodeItem());
194     DCHECK_EQ(accessor.InsnsSizeInCodeUnits(), 1u);
195     DCHECK_EQ(accessor.begin().Inst().Opcode(), Instruction::RETURN_VOID);
196     return true;
197   };
198 
199   // Recognize a constructor of the form:
200   //   invoke-direct v0, j.l.Object.<init>
201   //   return-void
202   if (insns_size == 4u) {
203     DCHECK(is_recognizable_constructor);
204     const Instruction& instruction = accessor.begin().Inst();
205     if (instruction.Opcode() == Instruction::INVOKE_DIRECT &&
206         is_object_init_invoke(instruction)) {
207       return reinterpret_cast<void*>(&EmptyMethod);
208     }
209     return nullptr;
210   }
211 
212   // Recognize:
213   //   return-void
214   // Or:
215   //   return-object v0
216   if (insns_size == 1u) {
217     const Instruction& instruction = accessor.begin().Inst();
218     if (instruction.Opcode() == Instruction::RETURN_VOID) {
219       return reinterpret_cast<void*>(&EmptyMethod);
220     }
221 
222     if (instruction.Opcode() == Instruction::RETURN_OBJECT) {
223       uint16_t number_of_vregs = accessor.RegistersSize();
224       uint16_t number_of_parameters = accessor.InsSize();
225       uint16_t obj_reg = number_of_vregs - number_of_parameters;
226       if (obj_reg == instruction.VRegA_11x()) {
227         return reinterpret_cast<void*>(&ReturnFirstArgMethod);
228       }
229     }
230     return nullptr;
231   }
232 
233   // Recognize:
234   //   const vX, 0/1
235   //   return{-object} vX
236   if (insns_size == 2u) {
237     if (method->GetReturnTypePrimitive() == Primitive::kPrimFloat) {
238       // Too rare to bother.
239       return nullptr;
240     }
241     int32_t register_index = -1;
242     int32_t constant = -1;
243     for (DexInstructionPcPair pair : accessor) {
244       const Instruction& instruction = pair.Inst();
245       switch (pair->Opcode()) {
246         case Instruction::CONST_4: {
247           register_index = instruction.VRegA_11n();
248           constant = instruction.VRegB_11n();
249           if (constant != 0 && constant != 1) {
250             return nullptr;
251           }
252           break;
253         }
254         case Instruction::CONST_16: {
255           register_index = instruction.VRegA_21s();
256           constant = instruction.VRegB_21s();
257           if (constant != 0 && constant != 1) {
258             return nullptr;
259           }
260           break;
261         }
262         case Instruction::RETURN:
263         case Instruction::RETURN_OBJECT: {
264           if (register_index == instruction.VRegA_11x()) {
265             if (constant == 0) {
266               return reinterpret_cast<void*>(&ReturnZero);
267             } else if (constant == 1) {
268               return reinterpret_cast<void*>(&ReturnOne);
269             }
270           }
271           return nullptr;
272         }
273         default:
274           return nullptr;
275       }
276     }
277     return nullptr;
278   }
279 
280   // Recognize:
281   //   iget-{object,wide,boolean} vX, v0, field
282   //   return-{object} vX
283   // Or:
284   //   iput-{object,wide,boolean} v1, v0, field
285   //   return-void
286   // Or:
287   //   sget-object vX, field
288   //   return-object vX
289   // Or:
290   //   iput-{object,wide,boolean} v1, v0, field
291   //   invoke-direct v0, j.l.Object.<init>
292   //   return-void
293   // Or:
294   //   invoke-direct v0, j.l.Object.<init>
295   //   iput-{object,wide,boolean} v1, v0, field
296   //   return-void
297   if (insns_size == 3u || insns_size == 6u) {
298     DCHECK_IMPLIES(insns_size == 6u, is_recognizable_constructor);
299     uint16_t number_of_vregs = accessor.RegistersSize();
300     uint16_t number_of_parameters = accessor.InsSize();
301     uint16_t obj_reg = number_of_vregs - number_of_parameters;
302     uint16_t first_param_reg = number_of_vregs - number_of_parameters + 1;
303     uint16_t dest_reg = -1;
304     uint32_t offset = -1;
305     bool is_object = false;
306     bool is_put = false;
307     bool is_static = false;
308     bool is_final = false;
309     Primitive::Type field_type;
310     for (DexInstructionPcPair pair : accessor) {
311       const Instruction& instruction = pair.Inst();
312       switch (pair->Opcode()) {
313         case Instruction::INVOKE_DIRECT:
314           if (!is_recognizable_constructor || !is_object_init_invoke(instruction)) {
315             return nullptr;
316           }
317           break;
318         case Instruction::SGET_OBJECT:
319           is_static = true;
320           FALLTHROUGH_INTENDED;
321         case Instruction::IPUT_OBJECT:
322         case Instruction::IGET_OBJECT:
323           is_object = true;
324           FALLTHROUGH_INTENDED;
325         case Instruction::IPUT:
326         case Instruction::IGET:
327         case Instruction::IGET_BOOLEAN:
328         case Instruction::IPUT_BOOLEAN:
329         case Instruction::IGET_WIDE:
330         case Instruction::IPUT_WIDE: {
331           is_put = (pair->Opcode() == Instruction::IPUT ||
332                     pair->Opcode() == Instruction::IPUT_OBJECT ||
333                     pair->Opcode() == Instruction::IPUT_BOOLEAN ||
334                     pair->Opcode() == Instruction::IPUT_WIDE);
335           if (!is_static && obj_reg != instruction.VRegB_22c()) {
336             // The field access is not on the first parameter.
337             return nullptr;
338           }
339           if (!is_static && method->IsStatic()) {
340             // Getting/setting an instance field on an object that can be null.
341             // Our stubs cannot handle implicit null checks.
342             return nullptr;
343           }
344           if (is_put) {
345             if (first_param_reg != instruction.VRegA_22c()) {
346               // The value being stored is not the first parameter after 'this'.
347               return nullptr;
348             }
349           } else {
350             dest_reg = is_static ? instruction.VRegA_21c() : instruction.VRegA_22c();
351           }
352           uint16_t field_index = is_static ? instruction.VRegB_21c() : instruction.VRegC_22c();
353           Thread* self = Thread::Current();
354           ArtField* field =
355               ResolveFieldWithAccessChecks(Thread::Current(),
356                                            class_linker,
357                                            field_index,
358                                            method,
359                                            is_static,
360                                            is_put,
361                                            /* resolve_field_type= */ is_put && is_object);
362           if (field == nullptr) {
363             self->ClearException();
364             return nullptr;
365           }
366           if (field->IsVolatile()) {
367             return nullptr;
368           }
369           if (is_static && field->GetDeclaringClass() != method->GetDeclaringClass()) {
370             return nullptr;
371           }
372           offset = field->GetOffset().Int32Value();
373           if (is_static) {
374             // We subtract the start of reference fields to share more stubs.
375             MemberOffset first_field_offset =
376                 field->GetDeclaringClass()->GetFirstReferenceStaticFieldOffset(kRuntimePointerSize);
377             offset = offset - first_field_offset.Int32Value();
378           } else {
379             offset = offset - sizeof(mirror::Object);
380           }
381           if (offset > 64) {
382             return nullptr;
383           }
384           field_type = field->GetTypeAsPrimitiveType();
385           is_final = field->IsFinal();
386           break;
387         }
388         case Instruction::RETURN_OBJECT:
389         case Instruction::RETURN_WIDE:
390         case Instruction::RETURN: {
391           if (is_put || dest_reg != instruction.VRegA_11x()) {
392             // The returned value is not the fetched field.
393             return nullptr;
394           }
395           if (is_static) {
396             DO_SWITCH(offset, ReturnStaticFieldObjectAt, ReturnStaticFieldAt, field_type);
397           } else {
398             DO_SWITCH(offset, ReturnFieldObjectAt, ReturnFieldAt, field_type);
399           }
400         }
401         case Instruction::RETURN_VOID: {
402           if (!is_put) {
403             return nullptr;
404           }
405           if (is_final) {
406             DCHECK(is_recognizable_constructor);
407             DO_SWITCH(offset,  ConstructorSetFieldObjectAt, ConstructorSetFieldAt, field_type);
408           } else {
409             DO_SWITCH(offset, SetFieldObjectAt, SetFieldAt, field_type);
410           }
411         }
412         default:
413           return nullptr;
414       }
415     }
416   }
417 
418   return nullptr;
419 }
420 
421 }  // namespace jit
422 }  // namespace art
423