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 #include "inline_method_analyser.h"
18
19 #include "art_field-inl.h"
20 #include "art_method-inl.h"
21 #include "base/pointer_size.h"
22 #include "class_linker-inl.h"
23 #include "dex/code_item_accessors-inl.h"
24 #include "dex/dex_file-inl.h"
25 #include "dex/dex_instruction-inl.h"
26 #include "dex/dex_instruction.h"
27 #include "dex/dex_instruction_utils.h"
28 #include "mirror/class-inl.h"
29 #include "mirror/dex_cache-inl.h"
30
31 /*
32 * NOTE: This code is part of the quick compiler. It lives in the runtime
33 * only to allow the debugger to check whether a method has been inlined.
34 */
35
36 namespace art HIDDEN {
37
38 namespace { // anonymous namespace
39
40 // Helper class for matching a pattern.
41 class Matcher {
42 public:
43 // Match function type.
44 using MatchFn = bool(Matcher*);
45
46 template <size_t size>
47 static bool Match(const CodeItemDataAccessor* code_item, MatchFn* const (&pattern)[size]);
48
49 // Match and advance.
50
51 static bool Mark(Matcher* matcher);
52
53 template <bool (Matcher::*Fn)()>
54 static bool Required(Matcher* matcher);
55
56 template <bool (Matcher::*Fn)()>
57 static bool Repeated(Matcher* matcher); // On match, returns to the mark.
58
59 // Match an individual instruction.
60
61 template <Instruction::Code opcode> bool Opcode();
62 bool Const0();
63 bool IPutOnThis();
64
65 // Match Fn1 or Fn2. This should be used in combination of e.g. Required.
66 template <bool (Matcher::*Fn1)(), bool (Matcher::*Fn2)()>
67 bool Or();
68
69 private:
Matcher(const CodeItemDataAccessor * code_item)70 explicit Matcher(const CodeItemDataAccessor* code_item)
71 : code_item_(code_item),
72 instruction_(code_item->begin()) {}
73
74 static bool DoMatch(const CodeItemDataAccessor* code_item, MatchFn* const* pattern, size_t size);
75
76 const CodeItemDataAccessor* const code_item_;
77 DexInstructionIterator instruction_;
78 size_t pos_ = 0u;
79 size_t mark_ = 0u;
80 };
81
82 template <size_t size>
Match(const CodeItemDataAccessor * code_item,MatchFn * const (& pattern)[size])83 bool Matcher::Match(const CodeItemDataAccessor* code_item, MatchFn* const (&pattern)[size]) {
84 return DoMatch(code_item, pattern, size);
85 }
86
Mark(Matcher * matcher)87 bool Matcher::Mark(Matcher* matcher) {
88 matcher->pos_ += 1u; // Advance to the next match function before marking.
89 matcher->mark_ = matcher->pos_;
90 return true;
91 }
92
93 template <bool (Matcher::*Fn)()>
Required(Matcher * matcher)94 bool Matcher::Required(Matcher* matcher) {
95 if (!(matcher->*Fn)()) {
96 return false;
97 }
98 matcher->pos_ += 1u;
99 ++matcher->instruction_;
100 return true;
101 }
102
103 template <bool (Matcher::*Fn)()>
Repeated(Matcher * matcher)104 bool Matcher::Repeated(Matcher* matcher) {
105 if (!(matcher->*Fn)()) {
106 // Didn't match optional instruction, try the next match function.
107 matcher->pos_ += 1u;
108 return true;
109 }
110 matcher->pos_ = matcher->mark_;
111 ++matcher->instruction_;
112 return true;
113 }
114
115 template <Instruction::Code opcode>
Opcode()116 bool Matcher::Opcode() {
117 return instruction_->Opcode() == opcode;
118 }
119
120 // Match const 0.
Const0()121 bool Matcher::Const0() {
122 return IsInstructionDirectConst(instruction_->Opcode()) &&
123 (instruction_->Opcode() == Instruction::CONST_WIDE ? instruction_->VRegB_51l() == 0
124 : instruction_->VRegB() == 0);
125 }
126
IPutOnThis()127 bool Matcher::IPutOnThis() {
128 DCHECK_NE(code_item_->InsSize(), 0u);
129 return IsInstructionIPut(instruction_->Opcode()) &&
130 instruction_->VRegB_22c() == code_item_->RegistersSize() - code_item_->InsSize();
131 }
132
133 template <bool (Matcher::*Fn1)(), bool (Matcher::*Fn2)()>
Or()134 bool Matcher::Or() {
135 return (this->*Fn1)() || (this->*Fn2)();
136 }
137
DoMatch(const CodeItemDataAccessor * code_item,MatchFn * const * pattern,size_t size)138 bool Matcher::DoMatch(const CodeItemDataAccessor* code_item, MatchFn* const* pattern, size_t size) {
139 Matcher matcher(code_item);
140 while (matcher.pos_ != size) {
141 if (!pattern[matcher.pos_](&matcher)) {
142 return false;
143 }
144 }
145 return true;
146 }
147
148 // Used for a single invoke in a constructor. In that situation, the method verifier makes
149 // sure we invoke a constructor either in the same class or superclass with at least "this".
GetTargetConstructor(ArtMethod * method,const Instruction * invoke_direct)150 ArtMethod* GetTargetConstructor(ArtMethod* method, const Instruction* invoke_direct)
151 REQUIRES_SHARED(Locks::mutator_lock_) {
152 DCHECK(invoke_direct->Opcode() == Instruction::INVOKE_DIRECT ||
153 invoke_direct->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
154 if (kIsDebugBuild) {
155 uint16_t vregc = invoke_direct->Opcode() == Instruction::INVOKE_DIRECT
156 ? invoke_direct->VRegC_35c()
157 : invoke_direct->VRegC_3rc();
158 CodeItemDataAccessor accessor(method->DexInstructionData());
159 DCHECK_EQ(vregc, accessor.RegistersSize() - accessor.InsSize());
160 }
161 uint32_t method_index = invoke_direct->Opcode() == Instruction::INVOKE_DIRECT
162 ? invoke_direct->VRegB_35c()
163 : invoke_direct->VRegB_3rc();
164 ArtMethod* target_method = Runtime::Current()->GetClassLinker()->LookupResolvedMethod(
165 method_index, method->GetDexCache(), method->GetClassLoader());
166 if (kIsDebugBuild && target_method != nullptr) {
167 CHECK(!target_method->IsStatic());
168 CHECK(target_method->IsConstructor());
169 CHECK(method->GetDeclaringClass()->IsSubClass(target_method->GetDeclaringClass()));
170 }
171 return target_method;
172 }
173
174 // Return the forwarded arguments and check that all remaining arguments are zero.
175 // If the check fails, return static_cast<size_t>(-1).
CountForwardedConstructorArguments(const CodeItemDataAccessor * code_item,const Instruction * invoke_direct,uint16_t zero_vreg_mask)176 size_t CountForwardedConstructorArguments(const CodeItemDataAccessor* code_item,
177 const Instruction* invoke_direct,
178 uint16_t zero_vreg_mask) {
179 DCHECK(invoke_direct->Opcode() == Instruction::INVOKE_DIRECT ||
180 invoke_direct->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
181 size_t number_of_args = invoke_direct->Opcode() == Instruction::INVOKE_DIRECT
182 ? invoke_direct->VRegA_35c()
183 : invoke_direct->VRegA_3rc();
184 DCHECK_NE(number_of_args, 0u);
185
186 if (invoke_direct->Opcode() == Instruction::INVOKE_DIRECT) {
187 uint32_t args[Instruction::kMaxVarArgRegs];
188 invoke_direct->GetVarArgs(args);
189 uint16_t this_vreg = args[0];
190 DCHECK_EQ(this_vreg,
191 code_item->RegistersSize() - code_item->InsSize()); // Checked by verifier.
192 size_t forwarded = 1u;
193 while (forwarded < number_of_args &&
194 args[forwarded] == this_vreg + forwarded &&
195 (zero_vreg_mask & (1u << args[forwarded])) == 0) {
196 ++forwarded;
197 }
198 for (size_t i = forwarded; i != number_of_args; ++i) {
199 if ((zero_vreg_mask & (1u << args[i])) == 0) {
200 return static_cast<size_t>(-1);
201 }
202 }
203 return forwarded;
204 } else {
205 uint16_t this_vreg = invoke_direct->VRegC_3rc();
206 DCHECK_EQ(this_vreg,
207 code_item->RegistersSize() - code_item->InsSize()); // Checked by verifier.
208 size_t forwarded = 1u;
209 while (forwarded < number_of_args &&
210 (zero_vreg_mask & (1u << (this_vreg + forwarded))) == 0) {
211 ++forwarded;
212 }
213 for (size_t i = forwarded; i != number_of_args; ++i) {
214 if ((zero_vreg_mask & (1u << (this_vreg + i))) == 0) {
215 return static_cast<size_t>(-1);
216 }
217 }
218 return forwarded;
219 }
220 }
221
GetZeroVRegMask(const Instruction * const0)222 uint16_t GetZeroVRegMask(const Instruction* const0) {
223 DCHECK(IsInstructionDirectConst(const0->Opcode()));
224 DCHECK((const0->Opcode() == Instruction::CONST_WIDE) ? const0->VRegB_51l() == 0u
225 : const0->VRegB() == 0);
226 uint16_t base_mask = IsInstructionConstWide(const0->Opcode()) ? 3u : 1u;
227 return base_mask << const0->VRegA();
228 }
229
230 // We limit the number of IPUTs storing parameters. There can be any number
231 // of IPUTs that store the value 0 as they are useless in a constructor as
232 // the object always starts zero-initialized. We also eliminate all but the
233 // last store to any field as they are not observable; not even if the field
234 // is volatile as no reference to the object can escape from a constructor
235 // with this pattern.
236 static constexpr size_t kMaxConstructorIPuts = 3u;
237
238 struct ConstructorIPutData {
ConstructorIPutDataart::__anon3e471f560111::ConstructorIPutData239 ConstructorIPutData() : field_index(DexFile::kDexNoIndex16), arg(0u) { }
240
241 uint16_t field_index;
242 uint16_t arg;
243 };
244
RecordConstructorIPut(ArtMethod * method,const Instruction * new_iput,uint16_t this_vreg,uint16_t zero_vreg_mask,ConstructorIPutData (& iputs)[kMaxConstructorIPuts],size_t & iput_count)245 bool RecordConstructorIPut(ArtMethod* method,
246 const Instruction* new_iput,
247 uint16_t this_vreg,
248 uint16_t zero_vreg_mask,
249 /*inout*/ ConstructorIPutData (&iputs)[kMaxConstructorIPuts],
250 /*inout*/ size_t& iput_count)
251 REQUIRES_SHARED(Locks::mutator_lock_) {
252 DCHECK(IsInstructionIPut(new_iput->Opcode()));
253 uint32_t field_index = new_iput->VRegC_22c();
254 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
255 ArtField* field = class_linker->LookupResolvedField(field_index, method, /* is_static= */ false);
256 if (UNLIKELY(field == nullptr)) {
257 return false;
258 }
259 // Remove previous IPUT to the same field, if any. Different field indexes may refer
260 // to the same field, so we need to compare resolved fields from the dex cache.
261 for (size_t old_pos = 0, end = iput_count; old_pos < end; ++old_pos) {
262 ArtField* f = class_linker->LookupResolvedField(iputs[old_pos].field_index,
263 method,
264 /* is_static= */ false);
265 DCHECK(f != nullptr);
266 if (f == field) {
267 auto back_it = std::copy(iputs + old_pos + 1, iputs + iput_count, iputs + old_pos);
268 *back_it = ConstructorIPutData();
269 --iput_count;
270 break;
271 }
272 }
273 // If the stored value isn't zero, record the IPUT.
274 if ((zero_vreg_mask & (1u << new_iput->VRegA_22c())) == 0u) {
275 size_t new_pos = iput_count;
276 if (new_pos == arraysize(iputs)) {
277 return false; // Exceeded capacity of the output array.
278 }
279 iputs[new_pos].field_index = field_index;
280 iputs[new_pos].arg = new_iput->VRegA_22c() - this_vreg;
281 ++iput_count;
282 }
283 return true;
284 }
285
DoAnalyseConstructor(const CodeItemDataAccessor * code_item,ArtMethod * method,ConstructorIPutData (& iputs)[kMaxConstructorIPuts],size_t & iput_count)286 bool DoAnalyseConstructor(const CodeItemDataAccessor* code_item,
287 ArtMethod* method,
288 /*inout*/ ConstructorIPutData (&iputs)[kMaxConstructorIPuts],
289 /*inout*/ size_t &iput_count)
290 REQUIRES_SHARED(Locks::mutator_lock_) {
291 // On entry we should not have any IPUTs yet.
292 DCHECK_EQ(iput_count, 0u);
293
294 // Limit the maximum number of code units we're willing to match.
295 static constexpr size_t kMaxCodeUnits = 16u;
296
297 // Limit the number of registers that the constructor may use to 16.
298 // Given that IPUTs must use low 16 registers and we do not match MOVEs,
299 // this is a reasonable limitation.
300 static constexpr size_t kMaxVRegs = 16u;
301
302 // We try to match a constructor that calls another constructor (either in
303 // superclass or in the same class) with the same parameters, or with some
304 // parameters truncated (allowed only for calls to superclass constructor)
305 // or with extra parameters with value 0 (with any type, including null).
306 // This call can be followed by optional IPUTs on "this" storing either one
307 // of the parameters or 0 and the code must then finish with RETURN_VOID.
308 // The called constructor must be either java.lang.Object.<init>() or it
309 // must also match the same pattern.
310 static constexpr Matcher::MatchFn* const kConstructorPattern[] = {
311 &Matcher::Mark,
312 &Matcher::Repeated<&Matcher::Const0>,
313 // Either invoke-direct or invoke-direct/range works
314 &Matcher::Required<&Matcher::Or<&Matcher::Opcode<Instruction::INVOKE_DIRECT>,
315 &Matcher::Opcode<Instruction::INVOKE_DIRECT_RANGE>>>,
316 &Matcher::Mark,
317 &Matcher::Repeated<&Matcher::Const0>,
318 &Matcher::Repeated<&Matcher::IPutOnThis>,
319 &Matcher::Required<&Matcher::Opcode<Instruction::RETURN_VOID>>,
320 };
321
322 DCHECK(method != nullptr);
323 DCHECK(!method->IsStatic());
324 DCHECK(method->IsConstructor());
325 DCHECK(code_item != nullptr);
326 if (!method->GetDeclaringClass()->IsVerified() ||
327 code_item->InsnsSizeInCodeUnits() > kMaxCodeUnits ||
328 code_item->RegistersSize() > kMaxVRegs ||
329 !Matcher::Match(code_item, kConstructorPattern)) {
330 return false;
331 }
332
333 // Verify the invoke, prevent a few odd cases and collect IPUTs.
334 uint16_t this_vreg = code_item->RegistersSize() - code_item->InsSize();
335 uint16_t zero_vreg_mask = 0u;
336
337 for (const DexInstructionPcPair& pair : *code_item) {
338 const Instruction& instruction = pair.Inst();
339 if (instruction.Opcode() == Instruction::RETURN_VOID) {
340 break;
341 } else if (instruction.Opcode() == Instruction::INVOKE_DIRECT ||
342 instruction.Opcode() == Instruction::INVOKE_DIRECT_RANGE) {
343 ArtMethod* target_method = GetTargetConstructor(method, &instruction);
344 if (target_method == nullptr) {
345 return false;
346 }
347 // We allow forwarding constructors only if they pass more arguments
348 // to prevent infinite recursion.
349 size_t number_of_args = instruction.Opcode() == Instruction::INVOKE_DIRECT
350 ? instruction.VRegA_35c()
351 : instruction.VRegA_3rc();
352 if (target_method->GetDeclaringClass() == method->GetDeclaringClass() &&
353 number_of_args <= code_item->InsSize()) {
354 return false;
355 }
356 size_t forwarded = CountForwardedConstructorArguments(code_item, &instruction, zero_vreg_mask);
357 if (forwarded == static_cast<size_t>(-1)) {
358 return false;
359 }
360 if (target_method->GetDeclaringClass()->IsObjectClass()) {
361 DCHECK_EQ(target_method->DexInstructionData().begin()->Opcode(), Instruction::RETURN_VOID);
362 } else {
363 CodeItemDataAccessor target_code_item(target_method->DexInstructionData());
364 if (!target_code_item.HasCodeItem()) {
365 return false; // Native constructor?
366 }
367 if (!DoAnalyseConstructor(&target_code_item, target_method, iputs, iput_count)) {
368 return false;
369 }
370 // Prune IPUTs with zero input.
371 auto kept_end = std::remove_if(
372 iputs,
373 iputs + iput_count,
374 [forwarded](const ConstructorIPutData& iput_data) {
375 return iput_data.arg >= forwarded;
376 });
377 iput_count = std::distance(iputs, kept_end);
378 std::fill(kept_end, iputs + arraysize(iputs), ConstructorIPutData());
379 // If we have any IPUTs from the call, check that the target method is in the same
380 // dex file (compare DexCache references), otherwise field_indexes would be bogus.
381 if (iput_count > 0u && target_method->GetDexCache() != method->GetDexCache()) {
382 return false;
383 }
384 }
385 } else if (IsInstructionDirectConst(instruction.Opcode())) {
386 zero_vreg_mask |= GetZeroVRegMask(&instruction);
387 if ((zero_vreg_mask & (1u << this_vreg)) != 0u) {
388 return false; // Overwriting `this` is unsupported.
389 }
390 } else {
391 DCHECK(IsInstructionIPut(instruction.Opcode()));
392 DCHECK_EQ(instruction.VRegB_22c(), this_vreg);
393 if (!RecordConstructorIPut(method,
394 &instruction,
395 this_vreg,
396 zero_vreg_mask,
397 iputs,
398 iput_count)) {
399 return false;
400 }
401 }
402 }
403 return true;
404 }
405
406 } // anonymous namespace
407
AnalyseConstructor(const CodeItemDataAccessor * code_item,ArtMethod * method,InlineMethod * result)408 bool AnalyseConstructor(const CodeItemDataAccessor* code_item,
409 ArtMethod* method,
410 InlineMethod* result)
411 REQUIRES_SHARED(Locks::mutator_lock_) {
412 size_t iput_count(0u);
413 ConstructorIPutData iputs[kMaxConstructorIPuts];
414 if (!DoAnalyseConstructor(code_item, method, iputs, iput_count)) {
415 return false;
416 }
417 static_assert(kMaxConstructorIPuts == 3, "Unexpected limit"); // Code below depends on this.
418 DCHECK_LE(iput_count, kMaxConstructorIPuts);
419
420 #define STORE_IPUT(n) \
421 do { \
422 result->d.constructor_data.iput##n##_field_index = iputs[n].field_index; \
423 result->d.constructor_data.iput##n##_arg = iputs[n].arg; \
424 } while (false)
425
426 STORE_IPUT(0);
427 STORE_IPUT(1);
428 STORE_IPUT(2);
429 #undef STORE_IPUT
430
431 result->opcode = kInlineOpConstructor;
432 result->d.constructor_data.iput_count = static_cast<uint16_t>(iput_count);
433 return true;
434 }
435
436 static_assert(IsInstructionIGet(Instruction::IGET));
437 static_assert(IsInstructionIGet(Instruction::IGET_WIDE));
438 static_assert(IsInstructionIGet(Instruction::IGET_OBJECT));
439 static_assert(IsInstructionIGet(Instruction::IGET_BOOLEAN));
440 static_assert(IsInstructionIGet(Instruction::IGET_BYTE));
441 static_assert(IsInstructionIGet(Instruction::IGET_CHAR));
442 static_assert(IsInstructionIGet(Instruction::IGET_SHORT));
443 static_assert(IsInstructionIPut(Instruction::IPUT));
444 static_assert(IsInstructionIPut(Instruction::IPUT_WIDE));
445 static_assert(IsInstructionIPut(Instruction::IPUT_OBJECT));
446 static_assert(IsInstructionIPut(Instruction::IPUT_BOOLEAN));
447 static_assert(IsInstructionIPut(Instruction::IPUT_BYTE));
448 static_assert(IsInstructionIPut(Instruction::IPUT_CHAR));
449 static_assert(IsInstructionIPut(Instruction::IPUT_SHORT));
450 static_assert(IGetMemAccessType(Instruction::IGET) == IPutMemAccessType(Instruction::IPUT));
451 static_assert(
452 IGetMemAccessType(Instruction::IGET_WIDE) == IPutMemAccessType(Instruction::IPUT_WIDE));
453 static_assert(
454 IGetMemAccessType(Instruction::IGET_OBJECT) == IPutMemAccessType(Instruction::IPUT_OBJECT));
455 static_assert(
456 IGetMemAccessType(Instruction::IGET_BOOLEAN) == IPutMemAccessType(Instruction::IPUT_BOOLEAN));
457 static_assert(
458 IGetMemAccessType(Instruction::IGET_BYTE) == IPutMemAccessType(Instruction::IPUT_BYTE));
459 static_assert(
460 IGetMemAccessType(Instruction::IGET_CHAR) == IPutMemAccessType(Instruction::IPUT_CHAR));
461 static_assert(
462 IGetMemAccessType(Instruction::IGET_SHORT) == IPutMemAccessType(Instruction::IPUT_SHORT));
463
AnalyseMethodCode(ArtMethod * method,const CodeItemDataAccessor * code_item,InlineMethod * result)464 bool InlineMethodAnalyser::AnalyseMethodCode(ArtMethod* method,
465 const CodeItemDataAccessor* code_item,
466 InlineMethod* result) {
467 // We currently support only plain return or 2-instruction methods.
468
469 DCHECK_NE(code_item->InsnsSizeInCodeUnits(), 0u);
470 Instruction::Code opcode = code_item->begin()->Opcode();
471
472 switch (opcode) {
473 case Instruction::RETURN_VOID:
474 if (result != nullptr) {
475 result->opcode = kInlineOpNop;
476 result->d.data = 0u;
477 }
478 return true;
479 case Instruction::RETURN:
480 case Instruction::RETURN_OBJECT:
481 case Instruction::RETURN_WIDE:
482 return AnalyseReturnMethod(code_item, result);
483 case Instruction::CONST:
484 case Instruction::CONST_4:
485 case Instruction::CONST_16:
486 case Instruction::CONST_HIGH16:
487 // TODO: Support wide constants (RETURN_WIDE).
488 if (AnalyseConstMethod(code_item, result)) {
489 return true;
490 }
491 FALLTHROUGH_INTENDED;
492 case Instruction::CONST_WIDE:
493 case Instruction::CONST_WIDE_16:
494 case Instruction::CONST_WIDE_32:
495 case Instruction::CONST_WIDE_HIGH16:
496 case Instruction::INVOKE_DIRECT:
497 case Instruction::INVOKE_DIRECT_RANGE:
498 if (method != nullptr && !method->IsStatic() && method->IsConstructor()) {
499 return AnalyseConstructor(code_item, method, result);
500 }
501 return false;
502 case Instruction::IGET:
503 case Instruction::IGET_OBJECT:
504 case Instruction::IGET_BOOLEAN:
505 case Instruction::IGET_BYTE:
506 case Instruction::IGET_CHAR:
507 case Instruction::IGET_SHORT:
508 case Instruction::IGET_WIDE:
509 return AnalyseIGetMethod(method, code_item, result);
510 case Instruction::IPUT:
511 case Instruction::IPUT_OBJECT:
512 case Instruction::IPUT_BOOLEAN:
513 case Instruction::IPUT_BYTE:
514 case Instruction::IPUT_CHAR:
515 case Instruction::IPUT_SHORT:
516 case Instruction::IPUT_WIDE:
517 return AnalyseIPutMethod(method, code_item, result);
518 default:
519 return false;
520 }
521 }
522
IsSyntheticAccessor(ArtMethod * method)523 bool InlineMethodAnalyser::IsSyntheticAccessor(ArtMethod* method) {
524 const DexFile* dex_file = method->GetDexFile();
525 const dex::MethodId& method_id = dex_file->GetMethodId(method->GetDexMethodIndex());
526 const char* method_name = dex_file->GetMethodName(method_id);
527 // javac names synthetic accessors "access$nnn",
528 // jack names them "-getN", "-putN", "-wrapN".
529 return strncmp(method_name, "access$", strlen("access$")) == 0 ||
530 strncmp(method_name, "-", strlen("-")) == 0;
531 }
532
AnalyseReturnMethod(const CodeItemDataAccessor * code_item,InlineMethod * result)533 bool InlineMethodAnalyser::AnalyseReturnMethod(const CodeItemDataAccessor* code_item,
534 InlineMethod* result) {
535 DexInstructionIterator return_instruction = code_item->begin();
536 Instruction::Code return_opcode = return_instruction->Opcode();
537 uint32_t reg = return_instruction->VRegA_11x();
538 uint32_t arg_start = code_item->RegistersSize() - code_item->InsSize();
539 DCHECK_GE(reg, arg_start);
540 DCHECK_LT((return_opcode == Instruction::RETURN_WIDE) ? reg + 1 : reg,
541 code_item->RegistersSize());
542
543 if (result != nullptr) {
544 result->opcode = kInlineOpReturnArg;
545 InlineReturnArgData* data = &result->d.return_data;
546 data->arg = reg - arg_start;
547 data->is_wide = (return_opcode == Instruction::RETURN_WIDE) ? 1u : 0u;
548 data->is_object = (return_opcode == Instruction::RETURN_OBJECT) ? 1u : 0u;
549 data->reserved = 0u;
550 data->reserved2 = 0u;
551 }
552 return true;
553 }
554
AnalyseConstMethod(const CodeItemDataAccessor * code_item,InlineMethod * result)555 bool InlineMethodAnalyser::AnalyseConstMethod(const CodeItemDataAccessor* code_item,
556 InlineMethod* result) {
557 DexInstructionIterator instruction = code_item->begin();
558 const Instruction* return_instruction = instruction->Next();
559 Instruction::Code return_opcode = return_instruction->Opcode();
560 if (return_opcode != Instruction::RETURN &&
561 return_opcode != Instruction::RETURN_OBJECT) {
562 return false;
563 }
564
565 int32_t return_reg = return_instruction->VRegA_11x();
566 DCHECK_LT(return_reg, code_item->RegistersSize());
567
568 int32_t const_value = instruction->VRegB();
569 if (instruction->Opcode() == Instruction::CONST_HIGH16) {
570 const_value <<= 16;
571 }
572 DCHECK_LT(instruction->VRegA(), code_item->RegistersSize());
573 if (instruction->VRegA() != return_reg) {
574 return false; // Not returning the value set by const?
575 }
576 if (return_opcode == Instruction::RETURN_OBJECT && const_value != 0) {
577 return false; // Returning non-null reference constant?
578 }
579 if (result != nullptr) {
580 result->opcode = kInlineOpNonWideConst;
581 result->d.data = static_cast<uint64_t>(const_value);
582 }
583 return true;
584 }
585
AnalyseIGetMethod(ArtMethod * method,const CodeItemDataAccessor * code_item,InlineMethod * result)586 bool InlineMethodAnalyser::AnalyseIGetMethod(ArtMethod* method,
587 const CodeItemDataAccessor* code_item,
588 InlineMethod* result) {
589 DexInstructionIterator instruction = code_item->begin();
590 Instruction::Code opcode = instruction->Opcode();
591 DCHECK(IsInstructionIGet(opcode));
592
593 const Instruction* return_instruction = instruction->Next();
594 Instruction::Code return_opcode = return_instruction->Opcode();
595 if (!(return_opcode == Instruction::RETURN_WIDE && opcode == Instruction::IGET_WIDE) &&
596 !(return_opcode == Instruction::RETURN_OBJECT && opcode == Instruction::IGET_OBJECT) &&
597 !(return_opcode == Instruction::RETURN && opcode != Instruction::IGET_WIDE &&
598 opcode != Instruction::IGET_OBJECT)) {
599 return false;
600 }
601
602 uint32_t return_reg = return_instruction->VRegA_11x();
603 DCHECK_LT(return_opcode == Instruction::RETURN_WIDE ? return_reg + 1 : return_reg,
604 code_item->RegistersSize());
605
606 uint32_t dst_reg = instruction->VRegA_22c();
607 uint32_t object_reg = instruction->VRegB_22c();
608 uint32_t field_idx = instruction->VRegC_22c();
609 uint32_t arg_start = code_item->RegistersSize() - code_item->InsSize();
610 DCHECK_GE(object_reg, arg_start);
611 DCHECK_LT(object_reg, code_item->RegistersSize());
612 uint32_t object_arg = object_reg - arg_start;
613
614 DCHECK_LT(opcode == Instruction::IGET_WIDE ? dst_reg + 1 : dst_reg, code_item->RegistersSize());
615 if (dst_reg != return_reg) {
616 return false; // Not returning the value retrieved by IGET?
617 }
618
619 // InlineIGetIPutData::object_arg is only 4 bits wide.
620 static constexpr uint16_t kMaxObjectArg = 15u;
621 if (object_arg > kMaxObjectArg) {
622 return false;
623 }
624
625 bool is_static = method->IsStatic();
626 if (is_static || object_arg != 0u) {
627 // TODO: Implement inlining of IGET on non-"this" registers (needs correct stack trace for NPE).
628 // Allow synthetic accessors. We don't care about losing their stack frame in NPE.
629 if (!IsSyntheticAccessor(method)) {
630 return false;
631 }
632 }
633
634 DCHECK(result != nullptr);
635 InlineIGetIPutData* data = &result->d.ifield_data;
636 if (!ComputeSpecialAccessorInfo(method, field_idx, false, data)) {
637 return false;
638 }
639 result->opcode = kInlineOpIGet;
640 data->op_variant = enum_cast<uint16_t>(IGetMemAccessType(opcode));
641 data->method_is_static = is_static ? 1u : 0u;
642 data->object_arg = object_arg; // Allow IGET on any register, not just "this".
643 data->src_arg = 0u;
644 data->return_arg_plus1 = 0u;
645 return true;
646 }
647
AnalyseIPutMethod(ArtMethod * method,const CodeItemDataAccessor * code_item,InlineMethod * result)648 bool InlineMethodAnalyser::AnalyseIPutMethod(ArtMethod* method,
649 const CodeItemDataAccessor* code_item,
650 InlineMethod* result) {
651 DexInstructionIterator instruction = code_item->begin();
652 Instruction::Code opcode = instruction->Opcode();
653 DCHECK(IsInstructionIPut(opcode));
654
655 const Instruction* return_instruction = instruction->Next();
656 Instruction::Code return_opcode = return_instruction->Opcode();
657 uint32_t arg_start = code_item->RegistersSize() - code_item->InsSize();
658 uint16_t return_arg_plus1 = 0u;
659 if (return_opcode != Instruction::RETURN_VOID) {
660 if (return_opcode != Instruction::RETURN &&
661 return_opcode != Instruction::RETURN_OBJECT &&
662 return_opcode != Instruction::RETURN_WIDE) {
663 return false;
664 }
665 // Returning an argument.
666 uint32_t return_reg = return_instruction->VRegA_11x();
667 DCHECK_GE(return_reg, arg_start);
668 DCHECK_LT(return_opcode == Instruction::RETURN_WIDE ? return_reg + 1u : return_reg,
669 code_item->RegistersSize());
670 return_arg_plus1 = return_reg - arg_start + 1u;
671 }
672
673 uint32_t src_reg = instruction->VRegA_22c();
674 uint32_t object_reg = instruction->VRegB_22c();
675 uint32_t field_idx = instruction->VRegC_22c();
676 DCHECK_GE(object_reg, arg_start);
677 DCHECK_LT(object_reg, code_item->RegistersSize());
678 DCHECK_GE(src_reg, arg_start);
679 DCHECK_LT(opcode == Instruction::IPUT_WIDE ? src_reg + 1 : src_reg, code_item->RegistersSize());
680 uint32_t object_arg = object_reg - arg_start;
681 uint32_t src_arg = src_reg - arg_start;
682
683 // InlineIGetIPutData::object_arg/src_arg/return_arg_plus1 are each only 4 bits wide.
684 static constexpr uint16_t kMaxObjectArg = 15u;
685 static constexpr uint16_t kMaxSrcArg = 15u;
686 static constexpr uint16_t kMaxReturnArgPlus1 = 15u;
687 if (object_arg > kMaxObjectArg || src_arg > kMaxSrcArg || return_arg_plus1 > kMaxReturnArgPlus1) {
688 return false;
689 }
690
691 bool is_static = method->IsStatic();
692 if (is_static || object_arg != 0u) {
693 // TODO: Implement inlining of IPUT on non-"this" registers (needs correct stack trace for NPE).
694 // Allow synthetic accessors. We don't care about losing their stack frame in NPE.
695 if (!IsSyntheticAccessor(method)) {
696 return false;
697 }
698 }
699
700 DCHECK(result != nullptr);
701 InlineIGetIPutData* data = &result->d.ifield_data;
702 if (!ComputeSpecialAccessorInfo(method, field_idx, true, data)) {
703 return false;
704 }
705 result->opcode = kInlineOpIPut;
706 data->op_variant = enum_cast<uint16_t>(IPutMemAccessType(opcode));
707 data->method_is_static = is_static ? 1u : 0u;
708 data->object_arg = object_arg; // Allow IPUT on any register, not just "this".
709 data->src_arg = src_arg;
710 data->return_arg_plus1 = return_arg_plus1;
711 return true;
712 }
713
ComputeSpecialAccessorInfo(ArtMethod * method,uint32_t field_idx,bool is_put,InlineIGetIPutData * result)714 bool InlineMethodAnalyser::ComputeSpecialAccessorInfo(ArtMethod* method,
715 uint32_t field_idx,
716 bool is_put,
717 InlineIGetIPutData* result) {
718 if (method == nullptr) {
719 return false;
720 }
721 ObjPtr<mirror::DexCache> dex_cache = method->GetDexCache();
722 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
723 ArtField* field = class_linker->LookupResolvedField(field_idx, method, /* is_static= */ false);
724 if (field == nullptr || field->IsStatic()) {
725 return false;
726 }
727 ObjPtr<mirror::Class> method_class = method->GetDeclaringClass();
728 ObjPtr<mirror::Class> field_class = field->GetDeclaringClass();
729 if (!method_class->CanAccessResolvedField(field_class, field, dex_cache, field_idx) ||
730 (is_put && field->IsFinal() && method_class != field_class)) {
731 return false;
732 }
733 DCHECK_GE(field->GetOffset().Int32Value(), 0);
734 // Historical note: We made sure not to interleave function calls with bit field writes to
735 // placate Valgrind. Bug: 27552451.
736 uint32_t field_offset = field->GetOffset().Uint32Value();
737 bool is_volatile = field->IsVolatile();
738 result->field_idx = field_idx;
739 result->field_offset = field_offset;
740 result->is_volatile = is_volatile ? 1u : 0u;
741 return true;
742 }
743
744 } // namespace art
745