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 = class_linker->ResolveMethodId(method_idx, method);
181 if (target_method == nullptr) {
182 self->ClearException();
183 return false;
184 }
185 if (!target_method->GetDeclaringClass()->IsObjectClass()) {
186 return false;
187 }
188 DCHECK(target_method->GetDeclaringClass()->IsVerified());
189 CodeItemDataAccessor accessor(*target_method->GetDexFile(), target_method->GetCodeItem());
190 DCHECK_EQ(accessor.InsnsSizeInCodeUnits(), 1u);
191 DCHECK_EQ(accessor.begin().Inst().Opcode(), Instruction::RETURN_VOID);
192 return true;
193 };
194
195 // Recognize a constructor of the form:
196 // invoke-direct v0, j.l.Object.<init>
197 // return-void
198 if (insns_size == 4u) {
199 DCHECK(is_recognizable_constructor);
200 const Instruction& instruction = accessor.begin().Inst();
201 if (instruction.Opcode() == Instruction::INVOKE_DIRECT &&
202 is_object_init_invoke(instruction)) {
203 return reinterpret_cast<void*>(&EmptyMethod);
204 }
205 return nullptr;
206 }
207
208 // Recognize:
209 // return-void
210 // Or:
211 // return-object v0
212 if (insns_size == 1u) {
213 const Instruction& instruction = accessor.begin().Inst();
214 if (instruction.Opcode() == Instruction::RETURN_VOID) {
215 return reinterpret_cast<void*>(&EmptyMethod);
216 }
217
218 if (instruction.Opcode() == Instruction::RETURN_OBJECT) {
219 uint16_t number_of_vregs = accessor.RegistersSize();
220 uint16_t number_of_parameters = accessor.InsSize();
221 uint16_t obj_reg = number_of_vregs - number_of_parameters;
222 if (obj_reg == instruction.VRegA_11x()) {
223 return reinterpret_cast<void*>(&ReturnFirstArgMethod);
224 }
225 }
226 return nullptr;
227 }
228
229 // Recognize:
230 // const vX, 0/1
231 // return{-object} vX
232 if (insns_size == 2u) {
233 if (method->GetReturnTypePrimitive() == Primitive::kPrimFloat) {
234 // Too rare to bother.
235 return nullptr;
236 }
237 int32_t register_index = -1;
238 int32_t constant = -1;
239 for (DexInstructionPcPair pair : accessor) {
240 const Instruction& instruction = pair.Inst();
241 switch (pair->Opcode()) {
242 case Instruction::CONST_4: {
243 register_index = instruction.VRegA_11n();
244 constant = instruction.VRegB_11n();
245 if (constant != 0 && constant != 1) {
246 return nullptr;
247 }
248 break;
249 }
250 case Instruction::CONST_16: {
251 register_index = instruction.VRegA_21s();
252 constant = instruction.VRegB_21s();
253 if (constant != 0 && constant != 1) {
254 return nullptr;
255 }
256 break;
257 }
258 case Instruction::RETURN:
259 case Instruction::RETURN_OBJECT: {
260 if (register_index == instruction.VRegA_11x()) {
261 if (constant == 0) {
262 return reinterpret_cast<void*>(&ReturnZero);
263 } else if (constant == 1) {
264 return reinterpret_cast<void*>(&ReturnOne);
265 }
266 }
267 return nullptr;
268 }
269 default:
270 return nullptr;
271 }
272 }
273 return nullptr;
274 }
275
276 // Recognize:
277 // iget-{object,wide,boolean} vX, v0, field
278 // return-{object} vX
279 // Or:
280 // iput-{object,wide,boolean} v1, v0, field
281 // return-void
282 // Or:
283 // sget-object vX, field
284 // return-object vX
285 // Or:
286 // iput-{object,wide,boolean} v1, v0, field
287 // invoke-direct v0, j.l.Object.<init>
288 // return-void
289 // Or:
290 // invoke-direct v0, j.l.Object.<init>
291 // iput-{object,wide,boolean} v1, v0, field
292 // return-void
293 if (insns_size == 3u || insns_size == 6u) {
294 DCHECK_IMPLIES(insns_size == 6u, is_recognizable_constructor);
295 uint16_t number_of_vregs = accessor.RegistersSize();
296 uint16_t number_of_parameters = accessor.InsSize();
297 uint16_t obj_reg = number_of_vregs - number_of_parameters;
298 uint16_t first_param_reg = number_of_vregs - number_of_parameters + 1;
299 uint16_t dest_reg = -1;
300 uint32_t offset = -1;
301 bool is_object = false;
302 bool is_put = false;
303 bool is_static = false;
304 bool is_final = false;
305 Primitive::Type field_type;
306 for (DexInstructionPcPair pair : accessor) {
307 const Instruction& instruction = pair.Inst();
308 switch (pair->Opcode()) {
309 case Instruction::INVOKE_DIRECT:
310 if (!is_recognizable_constructor || !is_object_init_invoke(instruction)) {
311 return nullptr;
312 }
313 break;
314 case Instruction::SGET_OBJECT:
315 is_static = true;
316 FALLTHROUGH_INTENDED;
317 case Instruction::IPUT_OBJECT:
318 case Instruction::IGET_OBJECT:
319 is_object = true;
320 FALLTHROUGH_INTENDED;
321 case Instruction::IPUT:
322 case Instruction::IGET:
323 case Instruction::IGET_BOOLEAN:
324 case Instruction::IPUT_BOOLEAN:
325 case Instruction::IGET_WIDE:
326 case Instruction::IPUT_WIDE: {
327 is_put = (pair->Opcode() == Instruction::IPUT ||
328 pair->Opcode() == Instruction::IPUT_OBJECT ||
329 pair->Opcode() == Instruction::IPUT_BOOLEAN ||
330 pair->Opcode() == Instruction::IPUT_WIDE);
331 if (!is_static && obj_reg != instruction.VRegB_22c()) {
332 // The field access is not on the first parameter.
333 return nullptr;
334 }
335 if (!is_static && method->IsStatic()) {
336 // Getting/setting an instance field on an object that can be null.
337 // Our stubs cannot handle implicit null checks.
338 return nullptr;
339 }
340 if (is_put) {
341 if (first_param_reg != instruction.VRegA_22c()) {
342 // The value being stored is not the first parameter after 'this'.
343 return nullptr;
344 }
345 } else {
346 dest_reg = is_static ? instruction.VRegA_21c() : instruction.VRegA_22c();
347 }
348 uint16_t field_index = is_static ? instruction.VRegB_21c() : instruction.VRegC_22c();
349 Thread* self = Thread::Current();
350 ArtField* field =
351 ResolveFieldWithAccessChecks(Thread::Current(),
352 class_linker,
353 field_index,
354 method,
355 is_static,
356 is_put,
357 /* resolve_field_type= */ is_put && is_object);
358 if (field == nullptr) {
359 self->ClearException();
360 return nullptr;
361 }
362 if (field->IsVolatile()) {
363 return nullptr;
364 }
365 if (is_static && field->GetDeclaringClass() != method->GetDeclaringClass()) {
366 return nullptr;
367 }
368 offset = field->GetOffset().Int32Value();
369 if (is_static) {
370 // We subtract the start of reference fields to share more stubs.
371 MemberOffset first_field_offset =
372 field->GetDeclaringClass()->GetFirstReferenceStaticFieldOffset(kRuntimePointerSize);
373 offset = offset - first_field_offset.Int32Value();
374 } else {
375 offset = offset - sizeof(mirror::Object);
376 }
377 if (offset > 64) {
378 return nullptr;
379 }
380 field_type = field->GetTypeAsPrimitiveType();
381 is_final = field->IsFinal();
382 break;
383 }
384 case Instruction::RETURN_OBJECT:
385 case Instruction::RETURN_WIDE:
386 case Instruction::RETURN: {
387 if (is_put || dest_reg != instruction.VRegA_11x()) {
388 // The returned value is not the fetched field.
389 return nullptr;
390 }
391 if (is_static) {
392 DO_SWITCH(offset, ReturnStaticFieldObjectAt, ReturnStaticFieldAt, field_type);
393 } else {
394 DO_SWITCH(offset, ReturnFieldObjectAt, ReturnFieldAt, field_type);
395 }
396 }
397 case Instruction::RETURN_VOID: {
398 if (!is_put) {
399 return nullptr;
400 }
401 if (is_final) {
402 DCHECK(is_recognizable_constructor);
403 DO_SWITCH(offset, ConstructorSetFieldObjectAt, ConstructorSetFieldAt, field_type);
404 } else {
405 DO_SWITCH(offset, SetFieldObjectAt, SetFieldAt, field_type);
406 }
407 }
408 default:
409 return nullptr;
410 }
411 }
412 }
413
414 return nullptr;
415 }
416
417 } // namespace jit
418 } // namespace art
419