interpreter_common.cc revision e5cd2cd7d0f8e1332f25edfd2798d84fec871f10
1/*
2 * Copyright (C) 2012 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 "interpreter_common.h"
18
19#include <cmath>
20
21#include "base/enums.h"
22#include "debugger.h"
23#include "entrypoints/runtime_asm_entrypoints.h"
24#include "jit/jit.h"
25#include "jvalue.h"
26#include "method_handles.h"
27#include "method_handles-inl.h"
28#include "mirror/array-inl.h"
29#include "mirror/class.h"
30#include "mirror/method_handle_impl.h"
31#include "reflection.h"
32#include "reflection-inl.h"
33#include "stack.h"
34#include "unstarted_runtime.h"
35#include "verifier/method_verifier.h"
36
37namespace art {
38namespace interpreter {
39
40void ThrowNullPointerExceptionFromInterpreter() {
41  ThrowNullPointerExceptionFromDexPC();
42}
43
44template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check>
45bool DoFieldGet(Thread* self, ShadowFrame& shadow_frame, const Instruction* inst,
46                uint16_t inst_data) {
47  const bool is_static = (find_type == StaticObjectRead) || (find_type == StaticPrimitiveRead);
48  const uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
49  ArtField* f =
50      FindFieldFromCode<find_type, do_access_check>(field_idx, shadow_frame.GetMethod(), self,
51                                                    Primitive::ComponentSize(field_type));
52  if (UNLIKELY(f == nullptr)) {
53    CHECK(self->IsExceptionPending());
54    return false;
55  }
56  ObjPtr<Object> obj;
57  if (is_static) {
58    obj = f->GetDeclaringClass();
59  } else {
60    obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
61    if (UNLIKELY(obj == nullptr)) {
62      ThrowNullPointerExceptionForFieldAccess(f, true);
63      return false;
64    }
65  }
66  f->GetDeclaringClass()->AssertInitializedOrInitializingInThread(self);
67  // Report this field access to instrumentation if needed.
68  instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
69  if (UNLIKELY(instrumentation->HasFieldReadListeners())) {
70    StackHandleScope<1> hs(self);
71    // Wrap in handle wrapper in case the listener does thread suspension.
72    HandleWrapperObjPtr<mirror::Object> h(hs.NewHandleWrapper(&obj));
73    ObjPtr<Object> this_object;
74    if (!f->IsStatic()) {
75      this_object = obj;
76    }
77    instrumentation->FieldReadEvent(self,
78                                    this_object.Ptr(),
79                                    shadow_frame.GetMethod(),
80                                    shadow_frame.GetDexPC(),
81                                    f);
82  }
83  uint32_t vregA = is_static ? inst->VRegA_21c(inst_data) : inst->VRegA_22c(inst_data);
84  switch (field_type) {
85    case Primitive::kPrimBoolean:
86      shadow_frame.SetVReg(vregA, f->GetBoolean(obj));
87      break;
88    case Primitive::kPrimByte:
89      shadow_frame.SetVReg(vregA, f->GetByte(obj));
90      break;
91    case Primitive::kPrimChar:
92      shadow_frame.SetVReg(vregA, f->GetChar(obj));
93      break;
94    case Primitive::kPrimShort:
95      shadow_frame.SetVReg(vregA, f->GetShort(obj));
96      break;
97    case Primitive::kPrimInt:
98      shadow_frame.SetVReg(vregA, f->GetInt(obj));
99      break;
100    case Primitive::kPrimLong:
101      shadow_frame.SetVRegLong(vregA, f->GetLong(obj));
102      break;
103    case Primitive::kPrimNot:
104      shadow_frame.SetVRegReference(vregA, f->GetObject(obj).Ptr());
105      break;
106    default:
107      LOG(FATAL) << "Unreachable: " << field_type;
108      UNREACHABLE();
109  }
110  return true;
111}
112
113// Explicitly instantiate all DoFieldGet functions.
114#define EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, _do_check) \
115  template bool DoFieldGet<_find_type, _field_type, _do_check>(Thread* self, \
116                                                               ShadowFrame& shadow_frame, \
117                                                               const Instruction* inst, \
118                                                               uint16_t inst_data)
119
120#define EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(_find_type, _field_type)  \
121    EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, false);  \
122    EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, true);
123
124// iget-XXX
125EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimBoolean)
126EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimByte)
127EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimChar)
128EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimShort)
129EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimInt)
130EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimLong)
131EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstanceObjectRead, Primitive::kPrimNot)
132
133// sget-XXX
134EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimBoolean)
135EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimByte)
136EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimChar)
137EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimShort)
138EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimInt)
139EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimLong)
140EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticObjectRead, Primitive::kPrimNot)
141
142#undef EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL
143#undef EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL
144
145// Handles iget-quick, iget-wide-quick and iget-object-quick instructions.
146// Returns true on success, otherwise throws an exception and returns false.
147template<Primitive::Type field_type>
148bool DoIGetQuick(ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) {
149  Object* obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
150  if (UNLIKELY(obj == nullptr)) {
151    // We lost the reference to the field index so we cannot get a more
152    // precised exception message.
153    ThrowNullPointerExceptionFromDexPC();
154    return false;
155  }
156  MemberOffset field_offset(inst->VRegC_22c());
157  // Report this field access to instrumentation if needed. Since we only have the offset of
158  // the field from the base of the object, we need to look for it first.
159  instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
160  if (UNLIKELY(instrumentation->HasFieldReadListeners())) {
161    ArtField* f = ArtField::FindInstanceFieldWithOffset(obj->GetClass(),
162                                                        field_offset.Uint32Value());
163    DCHECK(f != nullptr);
164    DCHECK(!f->IsStatic());
165    instrumentation->FieldReadEvent(Thread::Current(), obj, shadow_frame.GetMethod(),
166                                    shadow_frame.GetDexPC(), f);
167  }
168  // Note: iget-x-quick instructions are only for non-volatile fields.
169  const uint32_t vregA = inst->VRegA_22c(inst_data);
170  switch (field_type) {
171    case Primitive::kPrimInt:
172      shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetField32(field_offset)));
173      break;
174    case Primitive::kPrimBoolean:
175      shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldBoolean(field_offset)));
176      break;
177    case Primitive::kPrimByte:
178      shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldByte(field_offset)));
179      break;
180    case Primitive::kPrimChar:
181      shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldChar(field_offset)));
182      break;
183    case Primitive::kPrimShort:
184      shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldShort(field_offset)));
185      break;
186    case Primitive::kPrimLong:
187      shadow_frame.SetVRegLong(vregA, static_cast<int64_t>(obj->GetField64(field_offset)));
188      break;
189    case Primitive::kPrimNot:
190      shadow_frame.SetVRegReference(vregA, obj->GetFieldObject<mirror::Object>(field_offset));
191      break;
192    default:
193      LOG(FATAL) << "Unreachable: " << field_type;
194      UNREACHABLE();
195  }
196  return true;
197}
198
199// Explicitly instantiate all DoIGetQuick functions.
200#define EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(_field_type) \
201  template bool DoIGetQuick<_field_type>(ShadowFrame& shadow_frame, const Instruction* inst, \
202                                         uint16_t inst_data)
203
204EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimInt);      // iget-quick.
205EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimBoolean);  // iget-boolean-quick.
206EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimByte);     // iget-byte-quick.
207EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimChar);     // iget-char-quick.
208EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimShort);    // iget-short-quick.
209EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimLong);     // iget-wide-quick.
210EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimNot);      // iget-object-quick.
211#undef EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL
212
213template<Primitive::Type field_type>
214static JValue GetFieldValue(const ShadowFrame& shadow_frame, uint32_t vreg)
215    REQUIRES_SHARED(Locks::mutator_lock_) {
216  JValue field_value;
217  switch (field_type) {
218    case Primitive::kPrimBoolean:
219      field_value.SetZ(static_cast<uint8_t>(shadow_frame.GetVReg(vreg)));
220      break;
221    case Primitive::kPrimByte:
222      field_value.SetB(static_cast<int8_t>(shadow_frame.GetVReg(vreg)));
223      break;
224    case Primitive::kPrimChar:
225      field_value.SetC(static_cast<uint16_t>(shadow_frame.GetVReg(vreg)));
226      break;
227    case Primitive::kPrimShort:
228      field_value.SetS(static_cast<int16_t>(shadow_frame.GetVReg(vreg)));
229      break;
230    case Primitive::kPrimInt:
231      field_value.SetI(shadow_frame.GetVReg(vreg));
232      break;
233    case Primitive::kPrimLong:
234      field_value.SetJ(shadow_frame.GetVRegLong(vreg));
235      break;
236    case Primitive::kPrimNot:
237      field_value.SetL(shadow_frame.GetVRegReference(vreg));
238      break;
239    default:
240      LOG(FATAL) << "Unreachable: " << field_type;
241      UNREACHABLE();
242  }
243  return field_value;
244}
245
246template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check,
247         bool transaction_active>
248bool DoFieldPut(Thread* self, const ShadowFrame& shadow_frame, const Instruction* inst,
249                uint16_t inst_data) {
250  bool do_assignability_check = do_access_check;
251  bool is_static = (find_type == StaticObjectWrite) || (find_type == StaticPrimitiveWrite);
252  uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
253  ArtField* f =
254      FindFieldFromCode<find_type, do_access_check>(field_idx, shadow_frame.GetMethod(), self,
255                                                    Primitive::ComponentSize(field_type));
256  if (UNLIKELY(f == nullptr)) {
257    CHECK(self->IsExceptionPending());
258    return false;
259  }
260  ObjPtr<Object> obj;
261  if (is_static) {
262    obj = f->GetDeclaringClass();
263  } else {
264    obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
265    if (UNLIKELY(obj == nullptr)) {
266      ThrowNullPointerExceptionForFieldAccess(f, false);
267      return false;
268    }
269  }
270  f->GetDeclaringClass()->AssertInitializedOrInitializingInThread(self);
271  uint32_t vregA = is_static ? inst->VRegA_21c(inst_data) : inst->VRegA_22c(inst_data);
272  // Report this field access to instrumentation if needed. Since we only have the offset of
273  // the field from the base of the object, we need to look for it first.
274  instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
275  if (UNLIKELY(instrumentation->HasFieldWriteListeners())) {
276    StackHandleScope<1> hs(self);
277    // Wrap in handle wrapper in case the listener does thread suspension.
278    HandleWrapperObjPtr<mirror::Object> h(hs.NewHandleWrapper(&obj));
279    JValue field_value = GetFieldValue<field_type>(shadow_frame, vregA);
280    ObjPtr<Object> this_object = f->IsStatic() ? nullptr : obj;
281    instrumentation->FieldWriteEvent(self, this_object.Ptr(),
282                                     shadow_frame.GetMethod(),
283                                     shadow_frame.GetDexPC(),
284                                     f,
285                                     field_value);
286  }
287  switch (field_type) {
288    case Primitive::kPrimBoolean:
289      f->SetBoolean<transaction_active>(obj, shadow_frame.GetVReg(vregA));
290      break;
291    case Primitive::kPrimByte:
292      f->SetByte<transaction_active>(obj, shadow_frame.GetVReg(vregA));
293      break;
294    case Primitive::kPrimChar:
295      f->SetChar<transaction_active>(obj, shadow_frame.GetVReg(vregA));
296      break;
297    case Primitive::kPrimShort:
298      f->SetShort<transaction_active>(obj, shadow_frame.GetVReg(vregA));
299      break;
300    case Primitive::kPrimInt:
301      f->SetInt<transaction_active>(obj, shadow_frame.GetVReg(vregA));
302      break;
303    case Primitive::kPrimLong:
304      f->SetLong<transaction_active>(obj, shadow_frame.GetVRegLong(vregA));
305      break;
306    case Primitive::kPrimNot: {
307      Object* reg = shadow_frame.GetVRegReference(vregA);
308      if (do_assignability_check && reg != nullptr) {
309        // FieldHelper::GetType can resolve classes, use a handle wrapper which will restore the
310        // object in the destructor.
311        ObjPtr<Class> field_class;
312        {
313          StackHandleScope<2> hs(self);
314          HandleWrapper<mirror::Object> h_reg(hs.NewHandleWrapper(&reg));
315          HandleWrapperObjPtr<mirror::Object> h_obj(hs.NewHandleWrapper(&obj));
316          field_class = f->GetType<true>();
317        }
318        if (!reg->VerifierInstanceOf(field_class.Ptr())) {
319          // This should never happen.
320          std::string temp1, temp2, temp3;
321          self->ThrowNewExceptionF("Ljava/lang/VirtualMachineError;",
322                                   "Put '%s' that is not instance of field '%s' in '%s'",
323                                   reg->GetClass()->GetDescriptor(&temp1),
324                                   field_class->GetDescriptor(&temp2),
325                                   f->GetDeclaringClass()->GetDescriptor(&temp3));
326          return false;
327        }
328      }
329      f->SetObj<transaction_active>(obj, reg);
330      break;
331    }
332    default:
333      LOG(FATAL) << "Unreachable: " << field_type;
334      UNREACHABLE();
335  }
336  return true;
337}
338
339// Explicitly instantiate all DoFieldPut functions.
340#define EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, _do_check, _transaction_active) \
341  template bool DoFieldPut<_find_type, _field_type, _do_check, _transaction_active>(Thread* self, \
342      const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data)
343
344#define EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(_find_type, _field_type)  \
345    EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, false, false);  \
346    EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, true, false);  \
347    EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, false, true);  \
348    EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, true, true);
349
350// iput-XXX
351EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimBoolean)
352EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimByte)
353EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimChar)
354EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimShort)
355EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimInt)
356EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimLong)
357EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstanceObjectWrite, Primitive::kPrimNot)
358
359// sput-XXX
360EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimBoolean)
361EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimByte)
362EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimChar)
363EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimShort)
364EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimInt)
365EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimLong)
366EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticObjectWrite, Primitive::kPrimNot)
367
368#undef EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL
369#undef EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL
370
371template<Primitive::Type field_type, bool transaction_active>
372bool DoIPutQuick(const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) {
373  Object* obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
374  if (UNLIKELY(obj == nullptr)) {
375    // We lost the reference to the field index so we cannot get a more
376    // precised exception message.
377    ThrowNullPointerExceptionFromDexPC();
378    return false;
379  }
380  MemberOffset field_offset(inst->VRegC_22c());
381  const uint32_t vregA = inst->VRegA_22c(inst_data);
382  // Report this field modification to instrumentation if needed. Since we only have the offset of
383  // the field from the base of the object, we need to look for it first.
384  instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
385  if (UNLIKELY(instrumentation->HasFieldWriteListeners())) {
386    ArtField* f = ArtField::FindInstanceFieldWithOffset(obj->GetClass(),
387                                                        field_offset.Uint32Value());
388    DCHECK(f != nullptr);
389    DCHECK(!f->IsStatic());
390    JValue field_value = GetFieldValue<field_type>(shadow_frame, vregA);
391    instrumentation->FieldWriteEvent(Thread::Current(), obj, shadow_frame.GetMethod(),
392                                     shadow_frame.GetDexPC(), f, field_value);
393  }
394  // Note: iput-x-quick instructions are only for non-volatile fields.
395  switch (field_type) {
396    case Primitive::kPrimBoolean:
397      obj->SetFieldBoolean<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
398      break;
399    case Primitive::kPrimByte:
400      obj->SetFieldByte<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
401      break;
402    case Primitive::kPrimChar:
403      obj->SetFieldChar<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
404      break;
405    case Primitive::kPrimShort:
406      obj->SetFieldShort<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
407      break;
408    case Primitive::kPrimInt:
409      obj->SetField32<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
410      break;
411    case Primitive::kPrimLong:
412      obj->SetField64<transaction_active>(field_offset, shadow_frame.GetVRegLong(vregA));
413      break;
414    case Primitive::kPrimNot:
415      obj->SetFieldObject<transaction_active>(field_offset, shadow_frame.GetVRegReference(vregA));
416      break;
417    default:
418      LOG(FATAL) << "Unreachable: " << field_type;
419      UNREACHABLE();
420  }
421  return true;
422}
423
424// Explicitly instantiate all DoIPutQuick functions.
425#define EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, _transaction_active) \
426  template bool DoIPutQuick<_field_type, _transaction_active>(const ShadowFrame& shadow_frame, \
427                                                              const Instruction* inst, \
428                                                              uint16_t inst_data)
429
430#define EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(_field_type)   \
431  EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, false);     \
432  EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, true);
433
434EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimInt)      // iput-quick.
435EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimBoolean)  // iput-boolean-quick.
436EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimByte)     // iput-byte-quick.
437EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimChar)     // iput-char-quick.
438EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimShort)    // iput-short-quick.
439EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimLong)     // iput-wide-quick.
440EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimNot)      // iput-object-quick.
441#undef EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL
442#undef EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL
443
444// We accept a null Instrumentation* meaning we must not report anything to the instrumentation.
445uint32_t FindNextInstructionFollowingException(
446    Thread* self, ShadowFrame& shadow_frame, uint32_t dex_pc,
447    const instrumentation::Instrumentation* instrumentation) {
448  self->VerifyStack();
449  StackHandleScope<2> hs(self);
450  Handle<mirror::Throwable> exception(hs.NewHandle(self->GetException()));
451  if (instrumentation != nullptr && instrumentation->HasExceptionCaughtListeners()
452      && self->IsExceptionThrownByCurrentMethod(exception.Get())) {
453    instrumentation->ExceptionCaughtEvent(self, exception.Get());
454  }
455  bool clear_exception = false;
456  uint32_t found_dex_pc = shadow_frame.GetMethod()->FindCatchBlock(
457      hs.NewHandle(exception->GetClass()), dex_pc, &clear_exception);
458  if (found_dex_pc == DexFile::kDexNoIndex && instrumentation != nullptr) {
459    // Exception is not caught by the current method. We will unwind to the
460    // caller. Notify any instrumentation listener.
461    instrumentation->MethodUnwindEvent(self, shadow_frame.GetThisObject(),
462                                       shadow_frame.GetMethod(), dex_pc);
463  } else {
464    // Exception is caught in the current method. We will jump to the found_dex_pc.
465    if (clear_exception) {
466      self->ClearException();
467    }
468  }
469  return found_dex_pc;
470}
471
472void UnexpectedOpcode(const Instruction* inst, const ShadowFrame& shadow_frame) {
473  LOG(FATAL) << "Unexpected instruction: "
474             << inst->DumpString(shadow_frame.GetMethod()->GetDexFile());
475  UNREACHABLE();
476}
477
478void AbortTransactionF(Thread* self, const char* fmt, ...) {
479  va_list args;
480  va_start(args, fmt);
481  AbortTransactionV(self, fmt, args);
482  va_end(args);
483}
484
485void AbortTransactionV(Thread* self, const char* fmt, va_list args) {
486  CHECK(Runtime::Current()->IsActiveTransaction());
487  // Constructs abort message.
488  std::string abort_msg;
489  StringAppendV(&abort_msg, fmt, args);
490  // Throws an exception so we can abort the transaction and rollback every change.
491  Runtime::Current()->AbortTransactionAndThrowAbortError(self, abort_msg);
492}
493
494// Separate declaration is required solely for the attributes.
495template <bool is_range, bool do_assignability_check>
496    REQUIRES_SHARED(Locks::mutator_lock_)
497static inline bool DoCallCommon(ArtMethod* called_method,
498                                Thread* self,
499                                ShadowFrame& shadow_frame,
500                                JValue* result,
501                                uint16_t number_of_inputs,
502                                uint32_t (&arg)[Instruction::kMaxVarArgRegs],
503                                uint32_t vregC) ALWAYS_INLINE;
504
505// Separate declaration is required solely for the attributes.
506template <bool is_range> REQUIRES_SHARED(Locks::mutator_lock_)
507static inline bool DoCallPolymorphic(ArtMethod* called_method,
508                                     Handle<mirror::MethodType> callsite_type,
509                                     Handle<mirror::MethodType> target_type,
510                                     Thread* self,
511                                     ShadowFrame& shadow_frame,
512                                     JValue* result,
513                                     uint32_t (&arg)[Instruction::kMaxVarArgRegs],
514                                     uint32_t vregC) ALWAYS_INLINE;
515
516void ArtInterpreterToCompiledCodeBridge(Thread* self,
517                                        ArtMethod* caller,
518                                        const DexFile::CodeItem* code_item,
519                                        ShadowFrame* shadow_frame,
520                                        JValue* result)
521    REQUIRES_SHARED(Locks::mutator_lock_) {
522  ArtMethod* method = shadow_frame->GetMethod();
523  // Ensure static methods are initialized.
524  if (method->IsStatic()) {
525    mirror::Class* declaringClass = method->GetDeclaringClass();
526    if (UNLIKELY(!declaringClass->IsInitialized())) {
527      self->PushShadowFrame(shadow_frame);
528      StackHandleScope<1> hs(self);
529      Handle<mirror::Class> h_class(hs.NewHandle(declaringClass));
530      if (UNLIKELY(!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true,
531                                                                            true))) {
532        self->PopShadowFrame();
533        DCHECK(self->IsExceptionPending());
534        return;
535      }
536      self->PopShadowFrame();
537      CHECK(h_class->IsInitializing());
538      // Reload from shadow frame in case the method moved, this is faster than adding a handle.
539      method = shadow_frame->GetMethod();
540    }
541  }
542  uint16_t arg_offset = (code_item == nullptr)
543                            ? 0
544                            : code_item->registers_size_ - code_item->ins_size_;
545  jit::Jit* jit = Runtime::Current()->GetJit();
546  if (jit != nullptr && caller != nullptr) {
547    jit->NotifyInterpreterToCompiledCodeTransition(self, caller);
548  }
549  method->Invoke(self, shadow_frame->GetVRegArgs(arg_offset),
550                 (shadow_frame->NumberOfVRegs() - arg_offset) * sizeof(uint32_t),
551                 result, method->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty());
552}
553
554void SetStringInitValueToAllAliases(ShadowFrame* shadow_frame,
555                                    uint16_t this_obj_vreg,
556                                    JValue result)
557    REQUIRES_SHARED(Locks::mutator_lock_) {
558  Object* existing = shadow_frame->GetVRegReference(this_obj_vreg);
559  if (existing == nullptr) {
560    // If it's null, we come from compiled code that was deoptimized. Nothing to do,
561    // as the compiler verified there was no alias.
562    // Set the new string result of the StringFactory.
563    shadow_frame->SetVRegReference(this_obj_vreg, result.GetL());
564    return;
565  }
566  // Set the string init result into all aliases.
567  for (uint32_t i = 0, e = shadow_frame->NumberOfVRegs(); i < e; ++i) {
568    if (shadow_frame->GetVRegReference(i) == existing) {
569      DCHECK_EQ(shadow_frame->GetVRegReference(i),
570                reinterpret_cast<mirror::Object*>(shadow_frame->GetVReg(i)));
571      shadow_frame->SetVRegReference(i, result.GetL());
572      DCHECK_EQ(shadow_frame->GetVRegReference(i),
573                reinterpret_cast<mirror::Object*>(shadow_frame->GetVReg(i)));
574    }
575  }
576}
577
578template<bool is_range, bool do_access_check>
579    REQUIRES_SHARED(Locks::mutator_lock_)
580inline bool DoInvokePolymorphic(Thread* self, ShadowFrame& shadow_frame,
581                                const Instruction* inst, uint16_t inst_data,
582                                JValue* result) {
583  // Invoke-polymorphic instructions always take a receiver. i.e, they are never static.
584  const uint32_t vRegC = (is_range) ? inst->VRegC_4rcc() : inst->VRegC_45cc();
585
586  // The method_idx here is the name of the signature polymorphic method that
587  // was symbolically invoked in bytecode (say MethodHandle.invoke or MethodHandle.invokeExact)
588  // and not the method that we'll dispatch to in the end.
589  //
590  // TODO(narayan) We'll have to check in the verifier that this is in fact a
591  // signature polymorphic method so that we disallow calls via invoke-polymorphic
592  // to non sig-poly methods. This would also have the side effect of verifying
593  // that vRegC really is a reference type.
594  StackHandleScope<6> hs(self);
595  Handle<mirror::MethodHandleImpl> method_handle(hs.NewHandle(
596      reinterpret_cast<mirror::MethodHandleImpl*>(shadow_frame.GetVRegReference(vRegC))));
597  if (UNLIKELY(method_handle.Get() == nullptr)) {
598    const int method_idx = (is_range) ? inst->VRegB_4rcc() : inst->VRegB_45cc();
599    // Note that the invoke type is kVirtual here because a call to a signature
600    // polymorphic method is shaped like a virtual call at the bytecode level.
601    ThrowNullPointerExceptionForMethodAccess(method_idx, InvokeType::kVirtual);
602
603    result->SetJ(0);
604    return false;
605  }
606
607  // The vRegH value gives the index of the proto_id associated with this
608  // signature polymorphic callsite.
609  const uint32_t callsite_proto_id = (is_range) ? inst->VRegH_4rcc() : inst->VRegH_45cc();
610
611  // Call through to the classlinker and ask it to resolve the static type associated
612  // with the callsite. This information is stored in the dex cache so it's
613  // guaranteed to be fast after the first resolution.
614  ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
615  Handle<mirror::Class> caller_class(hs.NewHandle(shadow_frame.GetMethod()->GetDeclaringClass()));
616  Handle<mirror::MethodType> callsite_type(hs.NewHandle(class_linker->ResolveMethodType(
617      caller_class->GetDexFile(), callsite_proto_id,
618      hs.NewHandle<mirror::DexCache>(caller_class->GetDexCache()),
619      hs.NewHandle<mirror::ClassLoader>(caller_class->GetClassLoader()))));
620
621  // This implies we couldn't resolve one or more types in this method handle.
622  if (UNLIKELY(callsite_type.Get() == nullptr)) {
623    CHECK(self->IsExceptionPending());
624    result->SetJ(0);
625    return false;
626  }
627
628  // Get the method we're actually invoking along with the kind of
629  // invoke that is desired. We don't need to perform access checks at this
630  // point because they would have been performed on our behalf at the point
631  // of creation of the method handle.
632  ArtMethod* called_method = method_handle->GetTargetMethod();
633  const MethodHandleKind handle_kind = method_handle->GetHandleKind();
634  Handle<mirror::MethodType> handle_type(hs.NewHandle(method_handle->GetMethodType()));
635  CHECK(called_method != nullptr);
636  CHECK(handle_type.Get() != nullptr);
637
638  // We now have to massage the number of inputs to the target function.
639  // It's always one less than the number of inputs to the signature polymorphic
640  // invoke, the first input being a reference to the MethodHandle itself.
641  const uint16_t number_of_inputs =
642      ((is_range) ? inst->VRegA_4rcc(inst_data) : inst->VRegA_45cc(inst_data)) - 1;
643
644  uint32_t arg[Instruction::kMaxVarArgRegs] = {};
645  uint32_t receiver_vregC = 0;
646  if (is_range) {
647    receiver_vregC = (inst->VRegC_4rcc() + 1);
648  } else {
649    inst->GetVarArgs(arg, inst_data);
650    arg[0] = arg[1];
651    arg[1] = arg[2];
652    arg[2] = arg[3];
653    arg[3] = arg[4];
654    arg[4] = 0;
655    receiver_vregC = arg[0];
656  }
657
658  if (IsInvoke(handle_kind)) {
659    if (handle_kind == kInvokeVirtual || handle_kind == kInvokeInterface) {
660      mirror::Object* receiver = shadow_frame.GetVRegReference(receiver_vregC);
661      mirror::Class* declaring_class = called_method->GetDeclaringClass();
662      // Verify that _vRegC is an object reference and of the type expected by
663      // the receiver.
664      called_method = receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(
665          called_method, kRuntimePointerSize);
666      if (!VerifyObjectIsClass(receiver, declaring_class)) {
667        return false;
668      }
669    } else if (handle_kind == kInvokeDirect) {
670      if (called_method->IsConstructor()) {
671        // TODO(narayan) : We need to handle the case where the target method is a
672        // constructor here.
673        UNIMPLEMENTED(FATAL) << "Direct invokes for constructors are not implemented yet.";
674        return false;
675      }
676
677      // Nothing special to do in the case where we're not dealing with a
678      // constructor. It's a private method, and we've already access checked at
679      // the point of creating the handle.
680    } else if (handle_kind == kInvokeSuper) {
681      mirror::Class* declaring_class = called_method->GetDeclaringClass();
682
683      // Note that we're not dynamically dispatching on the type of the receiver
684      // here. We use the static type of the "receiver" object that we've
685      // recorded in the method handle's type, which will be the same as the
686      // special caller that was specified at the point of lookup.
687      mirror::Class* referrer_class = handle_type->GetPTypes()->Get(0);
688      if (!declaring_class->IsInterface()) {
689        mirror::Class* super_class = referrer_class->GetSuperClass();
690        uint16_t vtable_index = called_method->GetMethodIndex();
691        DCHECK(super_class != nullptr);
692        DCHECK(super_class->HasVTable());
693        // Note that super_class is a super of referrer_class and called_method
694        // will always be declared by super_class (or one of its super classes).
695        DCHECK_LT(vtable_index, super_class->GetVTableLength());
696        called_method = super_class->GetVTableEntry(vtable_index, kRuntimePointerSize);
697      } else {
698        called_method = referrer_class->FindVirtualMethodForInterfaceSuper(
699            called_method, kRuntimePointerSize);
700      }
701
702      CHECK(called_method != nullptr);
703    }
704
705    // NOTE: handle_kind == kInvokeStatic needs no special treatment here. We
706    // can directly make the call. handle_kind == kInvokeSuper doesn't have any
707    // particular use and can probably be dropped.
708
709    if (callsite_type->IsExactMatch(handle_type.Get())) {
710      return DoCallCommon<is_range, do_access_check>(
711          called_method, self, shadow_frame, result, number_of_inputs,
712          arg, receiver_vregC);
713    } else {
714      return DoCallPolymorphic<is_range>(
715          called_method, callsite_type, handle_type, self, shadow_frame,
716          result, arg, receiver_vregC);
717    }
718  } else {
719    // TODO(narayan): Implement field getters and setters.
720    UNIMPLEMENTED(FATAL) << "Field references in method handles are not implemented yet.";
721    return false;
722  }
723}
724
725// Calculate the number of ins for a proxy or native method, where we
726// can't just look at the code item.
727static inline size_t GetInsForProxyOrNativeMethod(ArtMethod* method)
728    REQUIRES_SHARED(Locks::mutator_lock_) {
729  DCHECK(method->IsNative() || method->IsProxyMethod());
730
731  method = method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
732  size_t num_ins = 0;
733  // Separate accounting for the receiver, which isn't a part of the
734  // shorty.
735  if (!method->IsStatic()) {
736    ++num_ins;
737  }
738
739  uint32_t shorty_len = 0;
740  const char* shorty = method->GetShorty(&shorty_len);
741  for (size_t i = 1; i < shorty_len; ++i) {
742    const char c = shorty[i];
743    ++num_ins;
744    if (c == 'J' || c == 'D') {
745      ++num_ins;
746    }
747  }
748
749  return num_ins;
750}
751
752template <bool is_range>
753static inline bool DoCallPolymorphic(ArtMethod* called_method,
754                                     Handle<mirror::MethodType> callsite_type,
755                                     Handle<mirror::MethodType> target_type,
756                                     Thread* self,
757                                     ShadowFrame& shadow_frame,
758                                     JValue* result,
759                                     uint32_t (&arg)[Instruction::kMaxVarArgRegs],
760                                     uint32_t vregC) {
761  // TODO(narayan): Wire in the String.init hacks.
762
763  // Compute method information.
764  const DexFile::CodeItem* code_item = called_method->GetCodeItem();
765
766  // Number of registers for the callee's call frame. Note that for non-exact
767  // invokes, we always derive this information from the callee method. We
768  // cannot guarantee during verification that the number of registers encoded
769  // in the invoke is equal to the number of ins for the callee. This is because
770  // some transformations (such as boxing a long -> Long or wideining an
771  // int -> long will change that number.
772  uint16_t num_regs;
773  size_t first_dest_reg;
774  if (LIKELY(code_item != nullptr)) {
775    num_regs = code_item->registers_size_;
776    first_dest_reg = num_regs - code_item->ins_size_;
777    // Parameter registers go at the end of the shadow frame.
778    DCHECK_NE(first_dest_reg, (size_t)-1);
779  } else {
780    // No local regs for proxy and native methods.
781    DCHECK(called_method->IsNative() || called_method->IsProxyMethod());
782    num_regs = GetInsForProxyOrNativeMethod(called_method);
783    first_dest_reg = 0;
784  }
785
786  // Allocate shadow frame on the stack.
787  ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
788      CREATE_SHADOW_FRAME(num_regs, &shadow_frame, called_method, /* dex pc */ 0);
789  ShadowFrame* new_shadow_frame = shadow_frame_unique_ptr.get();
790
791  // Thread might be suspended during PerformArgumentConversions due to the
792  // allocations performed during boxing.
793  {
794    ScopedStackedShadowFramePusher pusher(
795        self, new_shadow_frame, StackedShadowFrameType::kShadowFrameUnderConstruction);
796    if (!PerformArgumentConversions<is_range>(self, callsite_type, target_type,
797                                              shadow_frame, vregC, first_dest_reg,
798                                              arg, new_shadow_frame, result)) {
799      DCHECK(self->IsExceptionPending());
800      result->SetL(0);
801      return false;
802    }
803  }
804
805  // Do the call now.
806  if (LIKELY(Runtime::Current()->IsStarted())) {
807    ArtMethod* target = new_shadow_frame->GetMethod();
808    if (ClassLinker::ShouldUseInterpreterEntrypoint(
809        target,
810        target->GetEntryPointFromQuickCompiledCode())) {
811      ArtInterpreterToInterpreterBridge(self, code_item, new_shadow_frame, result);
812    } else {
813      ArtInterpreterToCompiledCodeBridge(
814          self, shadow_frame.GetMethod(), code_item, new_shadow_frame, result);
815    }
816  } else {
817    UnstartedRuntime::Invoke(self, code_item, new_shadow_frame, result, first_dest_reg);
818  }
819
820  // TODO(narayan): Perform return value conversions.
821
822  return !self->IsExceptionPending();
823}
824
825template <bool is_range,
826          bool do_assignability_check>
827static inline bool DoCallCommon(ArtMethod* called_method,
828                                Thread* self,
829                                ShadowFrame& shadow_frame,
830                                JValue* result,
831                                uint16_t number_of_inputs,
832                                uint32_t (&arg)[Instruction::kMaxVarArgRegs],
833                                uint32_t vregC) {
834  bool string_init = false;
835  // Replace calls to String.<init> with equivalent StringFactory call.
836  if (UNLIKELY(called_method->GetDeclaringClass()->IsStringClass()
837               && called_method->IsConstructor())) {
838    called_method = WellKnownClasses::StringInitToStringFactory(called_method);
839    string_init = true;
840  }
841
842  // Compute method information.
843  const DexFile::CodeItem* code_item = called_method->GetCodeItem();
844
845  // Number of registers for the callee's call frame.
846  uint16_t num_regs;
847  if (LIKELY(code_item != nullptr)) {
848    num_regs = code_item->registers_size_;
849    DCHECK_EQ(string_init ? number_of_inputs - 1 : number_of_inputs, code_item->ins_size_);
850  } else {
851    DCHECK(called_method->IsNative() || called_method->IsProxyMethod());
852    num_regs = number_of_inputs;
853  }
854
855  // Hack for String init:
856  //
857  // Rewrite invoke-x java.lang.String.<init>(this, a, b, c, ...) into:
858  //         invoke-x StringFactory(a, b, c, ...)
859  // by effectively dropping the first virtual register from the invoke.
860  //
861  // (at this point the ArtMethod has already been replaced,
862  // so we just need to fix-up the arguments)
863  //
864  // Note that FindMethodFromCode in entrypoint_utils-inl.h was also special-cased
865  // to handle the compiler optimization of replacing `this` with null without
866  // throwing NullPointerException.
867  uint32_t string_init_vreg_this = is_range ? vregC : arg[0];
868  if (UNLIKELY(string_init)) {
869    DCHECK_GT(num_regs, 0u);  // As the method is an instance method, there should be at least 1.
870
871    // The new StringFactory call is static and has one fewer argument.
872    if (code_item == nullptr) {
873      DCHECK(called_method->IsNative() || called_method->IsProxyMethod());
874      num_regs--;
875    }  // else ... don't need to change num_regs since it comes up from the string_init's code item
876    number_of_inputs--;
877
878    // Rewrite the var-args, dropping the 0th argument ("this")
879    for (uint32_t i = 1; i < arraysize(arg); ++i) {
880      arg[i - 1] = arg[i];
881    }
882    arg[arraysize(arg) - 1] = 0;
883
884    // Rewrite the non-var-arg case
885    vregC++;  // Skips the 0th vreg in the range ("this").
886  }
887
888  // Parameter registers go at the end of the shadow frame.
889  DCHECK_GE(num_regs, number_of_inputs);
890  size_t first_dest_reg = num_regs - number_of_inputs;
891  DCHECK_NE(first_dest_reg, (size_t)-1);
892
893  // Allocate shadow frame on the stack.
894  const char* old_cause = self->StartAssertNoThreadSuspension("DoCallCommon");
895  ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
896      CREATE_SHADOW_FRAME(num_regs, &shadow_frame, called_method, /* dex pc */ 0);
897  ShadowFrame* new_shadow_frame = shadow_frame_unique_ptr.get();
898
899  // Initialize new shadow frame by copying the registers from the callee shadow frame.
900  if (do_assignability_check) {
901    // Slow path.
902    // We might need to do class loading, which incurs a thread state change to kNative. So
903    // register the shadow frame as under construction and allow suspension again.
904    ScopedStackedShadowFramePusher pusher(
905        self, new_shadow_frame, StackedShadowFrameType::kShadowFrameUnderConstruction);
906    self->EndAssertNoThreadSuspension(old_cause);
907
908    // ArtMethod here is needed to check type information of the call site against the callee.
909    // Type information is retrieved from a DexFile/DexCache for that respective declared method.
910    //
911    // As a special case for proxy methods, which are not dex-backed,
912    // we have to retrieve type information from the proxy's method
913    // interface method instead (which is dex backed since proxies are never interfaces).
914    ArtMethod* method =
915        new_shadow_frame->GetMethod()->GetInterfaceMethodIfProxy(kRuntimePointerSize);
916
917    // We need to do runtime check on reference assignment. We need to load the shorty
918    // to get the exact type of each reference argument.
919    const DexFile::TypeList* params = method->GetParameterTypeList();
920    uint32_t shorty_len = 0;
921    const char* shorty = method->GetShorty(&shorty_len);
922
923    // Handle receiver apart since it's not part of the shorty.
924    size_t dest_reg = first_dest_reg;
925    size_t arg_offset = 0;
926
927    if (!method->IsStatic()) {
928      size_t receiver_reg = is_range ? vregC : arg[0];
929      new_shadow_frame->SetVRegReference(dest_reg, shadow_frame.GetVRegReference(receiver_reg));
930      ++dest_reg;
931      ++arg_offset;
932      DCHECK(!string_init);  // All StringFactory methods are static.
933    }
934
935    // Copy the caller's invoke-* arguments into the callee's parameter registers.
936    for (uint32_t shorty_pos = 0; dest_reg < num_regs; ++shorty_pos, ++dest_reg, ++arg_offset) {
937      // Skip the 0th 'shorty' type since it represents the return type.
938      DCHECK_LT(shorty_pos + 1, shorty_len) << "for shorty '" << shorty << "'";
939      const size_t src_reg = (is_range) ? vregC + arg_offset : arg[arg_offset];
940      switch (shorty[shorty_pos + 1]) {
941        // Handle Object references. 1 virtual register slot.
942        case 'L': {
943          Object* o = shadow_frame.GetVRegReference(src_reg);
944          if (do_assignability_check && o != nullptr) {
945            PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
946            Class* arg_type =
947                method->GetClassFromTypeIndex(
948                    params->GetTypeItem(shorty_pos).type_idx_, true /* resolve */, pointer_size);
949            if (arg_type == nullptr) {
950              CHECK(self->IsExceptionPending());
951              return false;
952            }
953            if (!o->VerifierInstanceOf(arg_type)) {
954              // This should never happen.
955              std::string temp1, temp2;
956              self->ThrowNewExceptionF("Ljava/lang/VirtualMachineError;",
957                                       "Invoking %s with bad arg %d, type '%s' not instance of '%s'",
958                                       new_shadow_frame->GetMethod()->GetName(), shorty_pos,
959                                       o->GetClass()->GetDescriptor(&temp1),
960                                       arg_type->GetDescriptor(&temp2));
961              return false;
962            }
963          }
964          new_shadow_frame->SetVRegReference(dest_reg, o);
965          break;
966        }
967        // Handle doubles and longs. 2 consecutive virtual register slots.
968        case 'J': case 'D': {
969          uint64_t wide_value =
970              (static_cast<uint64_t>(shadow_frame.GetVReg(src_reg + 1)) << BitSizeOf<uint32_t>()) |
971               static_cast<uint32_t>(shadow_frame.GetVReg(src_reg));
972          new_shadow_frame->SetVRegLong(dest_reg, wide_value);
973          // Skip the next virtual register slot since we already used it.
974          ++dest_reg;
975          ++arg_offset;
976          break;
977        }
978        // Handle all other primitives that are always 1 virtual register slot.
979        default:
980          new_shadow_frame->SetVReg(dest_reg, shadow_frame.GetVReg(src_reg));
981          break;
982      }
983    }
984  } else {
985    size_t arg_index = 0;
986
987    // Fast path: no extra checks.
988    if (is_range) {
989      uint16_t first_src_reg = vregC;
990
991      for (size_t src_reg = first_src_reg, dest_reg = first_dest_reg; dest_reg < num_regs;
992          ++dest_reg, ++src_reg) {
993        AssignRegister(new_shadow_frame, shadow_frame, dest_reg, src_reg);
994      }
995    } else {
996      DCHECK_LE(number_of_inputs, arraysize(arg));
997
998      for (; arg_index < number_of_inputs; ++arg_index) {
999        AssignRegister(new_shadow_frame, shadow_frame, first_dest_reg + arg_index, arg[arg_index]);
1000      }
1001    }
1002    self->EndAssertNoThreadSuspension(old_cause);
1003  }
1004
1005  // Do the call now.
1006  if (LIKELY(Runtime::Current()->IsStarted())) {
1007    ArtMethod* target = new_shadow_frame->GetMethod();
1008    if (ClassLinker::ShouldUseInterpreterEntrypoint(
1009        target,
1010        target->GetEntryPointFromQuickCompiledCode())) {
1011      ArtInterpreterToInterpreterBridge(self, code_item, new_shadow_frame, result);
1012    } else {
1013      ArtInterpreterToCompiledCodeBridge(
1014          self, shadow_frame.GetMethod(), code_item, new_shadow_frame, result);
1015    }
1016  } else {
1017    UnstartedRuntime::Invoke(self, code_item, new_shadow_frame, result, first_dest_reg);
1018  }
1019
1020  if (string_init && !self->IsExceptionPending()) {
1021    SetStringInitValueToAllAliases(&shadow_frame, string_init_vreg_this, *result);
1022  }
1023
1024  return !self->IsExceptionPending();
1025}
1026
1027template<bool is_range, bool do_assignability_check>
1028bool DoCall(ArtMethod* called_method, Thread* self, ShadowFrame& shadow_frame,
1029            const Instruction* inst, uint16_t inst_data, JValue* result) {
1030  // Argument word count.
1031  const uint16_t number_of_inputs =
1032      (is_range) ? inst->VRegA_3rc(inst_data) : inst->VRegA_35c(inst_data);
1033
1034  // TODO: find a cleaner way to separate non-range and range information without duplicating
1035  //       code.
1036  uint32_t arg[Instruction::kMaxVarArgRegs] = {};  // only used in invoke-XXX.
1037  uint32_t vregC = 0;
1038  if (is_range) {
1039    vregC = inst->VRegC_3rc();
1040  } else {
1041    vregC = inst->VRegC_35c();
1042    inst->GetVarArgs(arg, inst_data);
1043  }
1044
1045  return DoCallCommon<is_range, do_assignability_check>(
1046      called_method, self, shadow_frame,
1047      result, number_of_inputs, arg, vregC);
1048}
1049
1050template <bool is_range, bool do_access_check, bool transaction_active>
1051bool DoFilledNewArray(const Instruction* inst, const ShadowFrame& shadow_frame,
1052                      Thread* self, JValue* result) {
1053  DCHECK(inst->Opcode() == Instruction::FILLED_NEW_ARRAY ||
1054         inst->Opcode() == Instruction::FILLED_NEW_ARRAY_RANGE);
1055  const int32_t length = is_range ? inst->VRegA_3rc() : inst->VRegA_35c();
1056  if (!is_range) {
1057    // Checks FILLED_NEW_ARRAY's length does not exceed 5 arguments.
1058    CHECK_LE(length, 5);
1059  }
1060  if (UNLIKELY(length < 0)) {
1061    ThrowNegativeArraySizeException(length);
1062    return false;
1063  }
1064  uint16_t type_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
1065  Class* array_class = ResolveVerifyAndClinit(type_idx, shadow_frame.GetMethod(),
1066                                              self, false, do_access_check);
1067  if (UNLIKELY(array_class == nullptr)) {
1068    DCHECK(self->IsExceptionPending());
1069    return false;
1070  }
1071  CHECK(array_class->IsArrayClass());
1072  Class* component_class = array_class->GetComponentType();
1073  const bool is_primitive_int_component = component_class->IsPrimitiveInt();
1074  if (UNLIKELY(component_class->IsPrimitive() && !is_primitive_int_component)) {
1075    if (component_class->IsPrimitiveLong() || component_class->IsPrimitiveDouble()) {
1076      ThrowRuntimeException("Bad filled array request for type %s",
1077                            PrettyDescriptor(component_class).c_str());
1078    } else {
1079      self->ThrowNewExceptionF("Ljava/lang/InternalError;",
1080                               "Found type %s; filled-new-array not implemented for anything but 'int'",
1081                               PrettyDescriptor(component_class).c_str());
1082    }
1083    return false;
1084  }
1085  Object* new_array = Array::Alloc<true>(self, array_class, length,
1086                                         array_class->GetComponentSizeShift(),
1087                                         Runtime::Current()->GetHeap()->GetCurrentAllocator());
1088  if (UNLIKELY(new_array == nullptr)) {
1089    self->AssertPendingOOMException();
1090    return false;
1091  }
1092  uint32_t arg[Instruction::kMaxVarArgRegs];  // only used in filled-new-array.
1093  uint32_t vregC = 0;   // only used in filled-new-array-range.
1094  if (is_range) {
1095    vregC = inst->VRegC_3rc();
1096  } else {
1097    inst->GetVarArgs(arg);
1098  }
1099  for (int32_t i = 0; i < length; ++i) {
1100    size_t src_reg = is_range ? vregC + i : arg[i];
1101    if (is_primitive_int_component) {
1102      new_array->AsIntArray()->SetWithoutChecks<transaction_active>(
1103          i, shadow_frame.GetVReg(src_reg));
1104    } else {
1105      new_array->AsObjectArray<Object>()->SetWithoutChecks<transaction_active>(
1106          i, shadow_frame.GetVRegReference(src_reg));
1107    }
1108  }
1109
1110  result->SetL(new_array);
1111  return true;
1112}
1113
1114// TODO fix thread analysis: should be REQUIRES_SHARED(Locks::mutator_lock_).
1115template<typename T>
1116static void RecordArrayElementsInTransactionImpl(mirror::PrimitiveArray<T>* array, int32_t count)
1117    NO_THREAD_SAFETY_ANALYSIS {
1118  Runtime* runtime = Runtime::Current();
1119  for (int32_t i = 0; i < count; ++i) {
1120    runtime->RecordWriteArray(array, i, array->GetWithoutChecks(i));
1121  }
1122}
1123
1124void RecordArrayElementsInTransaction(mirror::Array* array, int32_t count)
1125    REQUIRES_SHARED(Locks::mutator_lock_) {
1126  DCHECK(Runtime::Current()->IsActiveTransaction());
1127  DCHECK(array != nullptr);
1128  DCHECK_LE(count, array->GetLength());
1129  Primitive::Type primitive_component_type = array->GetClass()->GetComponentType()->GetPrimitiveType();
1130  switch (primitive_component_type) {
1131    case Primitive::kPrimBoolean:
1132      RecordArrayElementsInTransactionImpl(array->AsBooleanArray(), count);
1133      break;
1134    case Primitive::kPrimByte:
1135      RecordArrayElementsInTransactionImpl(array->AsByteArray(), count);
1136      break;
1137    case Primitive::kPrimChar:
1138      RecordArrayElementsInTransactionImpl(array->AsCharArray(), count);
1139      break;
1140    case Primitive::kPrimShort:
1141      RecordArrayElementsInTransactionImpl(array->AsShortArray(), count);
1142      break;
1143    case Primitive::kPrimInt:
1144      RecordArrayElementsInTransactionImpl(array->AsIntArray(), count);
1145      break;
1146    case Primitive::kPrimFloat:
1147      RecordArrayElementsInTransactionImpl(array->AsFloatArray(), count);
1148      break;
1149    case Primitive::kPrimLong:
1150      RecordArrayElementsInTransactionImpl(array->AsLongArray(), count);
1151      break;
1152    case Primitive::kPrimDouble:
1153      RecordArrayElementsInTransactionImpl(array->AsDoubleArray(), count);
1154      break;
1155    default:
1156      LOG(FATAL) << "Unsupported primitive type " << primitive_component_type
1157                 << " in fill-array-data";
1158      break;
1159  }
1160}
1161
1162// Explicit DoCall template function declarations.
1163#define EXPLICIT_DO_CALL_TEMPLATE_DECL(_is_range, _do_assignability_check)                      \
1164  template REQUIRES_SHARED(Locks::mutator_lock_)                                                \
1165  bool DoCall<_is_range, _do_assignability_check>(ArtMethod* method, Thread* self,              \
1166                                                  ShadowFrame& shadow_frame,                    \
1167                                                  const Instruction* inst, uint16_t inst_data,  \
1168                                                  JValue* result)
1169EXPLICIT_DO_CALL_TEMPLATE_DECL(false, false);
1170EXPLICIT_DO_CALL_TEMPLATE_DECL(false, true);
1171EXPLICIT_DO_CALL_TEMPLATE_DECL(true, false);
1172EXPLICIT_DO_CALL_TEMPLATE_DECL(true, true);
1173#undef EXPLICIT_DO_CALL_TEMPLATE_DECL
1174
1175// Explicit DoInvokePolymorphic template function declarations.
1176#define EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(_is_range, _do_assignability_check)  \
1177  template REQUIRES_SHARED(Locks::mutator_lock_)                                          \
1178  bool DoInvokePolymorphic<_is_range, _do_assignability_check>(                           \
1179      Thread* self, ShadowFrame& shadow_frame, const Instruction* inst,                   \
1180      uint16_t inst_data, JValue* result)
1181
1182EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(false, false);
1183EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(false, true);
1184EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(true, false);
1185EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(true, true);
1186#undef EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL
1187
1188// Explicit DoFilledNewArray template function declarations.
1189#define EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(_is_range_, _check, _transaction_active)       \
1190  template REQUIRES_SHARED(Locks::mutator_lock_)                                                  \
1191  bool DoFilledNewArray<_is_range_, _check, _transaction_active>(const Instruction* inst,         \
1192                                                                 const ShadowFrame& shadow_frame, \
1193                                                                 Thread* self, JValue* result)
1194#define EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(_transaction_active)       \
1195  EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, false, _transaction_active);  \
1196  EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, true, _transaction_active);   \
1197  EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, false, _transaction_active);   \
1198  EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, true, _transaction_active)
1199EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(false);
1200EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(true);
1201#undef EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL
1202#undef EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL
1203
1204}  // namespace interpreter
1205}  // namespace art
1206