interpreter_common.cc revision 2cb856c47b884a08485e2f08e6a3ef6a5bbf773a
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/emulated_stack_frame.h"
31#include "mirror/method_handle_impl.h"
32#include "reflection.h"
33#include "reflection-inl.h"
34#include "stack.h"
35#include "unstarted_runtime.h"
36#include "verifier/method_verifier.h"
37#include "well_known_classes.h"
38
39namespace art {
40namespace interpreter {
41
42void ThrowNullPointerExceptionFromInterpreter() {
43  ThrowNullPointerExceptionFromDexPC();
44}
45
46template<Primitive::Type field_type>
47static ALWAYS_INLINE void DoFieldGetCommon(Thread* self,
48                                           const ShadowFrame& shadow_frame,
49                                           ObjPtr<mirror::Object>& obj,
50                                           ArtField* field,
51                                           JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) {
52  field->GetDeclaringClass()->AssertInitializedOrInitializingInThread(self);
53
54  // Report this field access to instrumentation if needed.
55  instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
56  if (UNLIKELY(instrumentation->HasFieldReadListeners())) {
57    StackHandleScope<1> hs(self);
58    // Wrap in handle wrapper in case the listener does thread suspension.
59    HandleWrapperObjPtr<mirror::Object> h(hs.NewHandleWrapper(&obj));
60    ObjPtr<mirror::Object> this_object;
61    if (!field->IsStatic()) {
62      this_object = obj;
63    }
64    instrumentation->FieldReadEvent(self,
65                                    this_object.Ptr(),
66                                    shadow_frame.GetMethod(),
67                                    shadow_frame.GetDexPC(),
68                                    field);
69  }
70
71  switch (field_type) {
72    case Primitive::kPrimBoolean:
73      result->SetZ(field->GetBoolean(obj));
74      break;
75    case Primitive::kPrimByte:
76      result->SetB(field->GetByte(obj));
77      break;
78    case Primitive::kPrimChar:
79      result->SetC(field->GetChar(obj));
80      break;
81    case Primitive::kPrimShort:
82      result->SetS(field->GetShort(obj));
83      break;
84    case Primitive::kPrimInt:
85      result->SetI(field->GetInt(obj));
86      break;
87    case Primitive::kPrimLong:
88      result->SetJ(field->GetLong(obj));
89      break;
90    case Primitive::kPrimNot:
91      result->SetL(field->GetObject(obj));
92      break;
93    default:
94      LOG(FATAL) << "Unreachable: " << field_type;
95      UNREACHABLE();
96  }
97}
98
99template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check>
100bool DoFieldGet(Thread* self, ShadowFrame& shadow_frame, const Instruction* inst,
101                uint16_t inst_data) {
102  const bool is_static = (find_type == StaticObjectRead) || (find_type == StaticPrimitiveRead);
103  const uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
104  ArtField* f =
105      FindFieldFromCode<find_type, do_access_check>(field_idx, shadow_frame.GetMethod(), self,
106                                                    Primitive::ComponentSize(field_type));
107  if (UNLIKELY(f == nullptr)) {
108    CHECK(self->IsExceptionPending());
109    return false;
110  }
111  ObjPtr<mirror::Object> obj;
112  if (is_static) {
113    obj = f->GetDeclaringClass();
114  } else {
115    obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
116    if (UNLIKELY(obj == nullptr)) {
117      ThrowNullPointerExceptionForFieldAccess(f, true);
118      return false;
119    }
120  }
121
122  JValue result;
123  DoFieldGetCommon<field_type>(self, shadow_frame, obj, f, &result);
124  uint32_t vregA = is_static ? inst->VRegA_21c(inst_data) : inst->VRegA_22c(inst_data);
125  switch (field_type) {
126    case Primitive::kPrimBoolean:
127      shadow_frame.SetVReg(vregA, result.GetZ());
128      break;
129    case Primitive::kPrimByte:
130      shadow_frame.SetVReg(vregA, result.GetB());
131      break;
132    case Primitive::kPrimChar:
133      shadow_frame.SetVReg(vregA, result.GetC());
134      break;
135    case Primitive::kPrimShort:
136      shadow_frame.SetVReg(vregA, result.GetS());
137      break;
138    case Primitive::kPrimInt:
139      shadow_frame.SetVReg(vregA, result.GetI());
140      break;
141    case Primitive::kPrimLong:
142      shadow_frame.SetVRegLong(vregA, result.GetJ());
143      break;
144    case Primitive::kPrimNot:
145      shadow_frame.SetVRegReference(vregA, result.GetL());
146      break;
147    default:
148      LOG(FATAL) << "Unreachable: " << field_type;
149      UNREACHABLE();
150  }
151  return true;
152}
153
154// Explicitly instantiate all DoFieldGet functions.
155#define EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, _do_check) \
156  template bool DoFieldGet<_find_type, _field_type, _do_check>(Thread* self, \
157                                                               ShadowFrame& shadow_frame, \
158                                                               const Instruction* inst, \
159                                                               uint16_t inst_data)
160
161#define EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(_find_type, _field_type)  \
162    EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, false);  \
163    EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, true);
164
165// iget-XXX
166EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimBoolean)
167EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimByte)
168EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimChar)
169EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimShort)
170EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimInt)
171EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimLong)
172EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstanceObjectRead, Primitive::kPrimNot)
173
174// sget-XXX
175EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimBoolean)
176EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimByte)
177EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimChar)
178EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimShort)
179EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimInt)
180EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimLong)
181EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticObjectRead, Primitive::kPrimNot)
182
183#undef EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL
184#undef EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL
185
186// Helper for getters in invoke-polymorphic.
187inline static void DoFieldGetForInvokePolymorphic(Thread* self,
188                                                  const ShadowFrame& shadow_frame,
189                                                  ObjPtr<mirror::Object>& obj,
190                                                  ArtField* field,
191                                                  Primitive::Type field_type,
192                                                  JValue* result)
193    REQUIRES_SHARED(Locks::mutator_lock_) {
194  switch (field_type) {
195    case Primitive::kPrimBoolean:
196      DoFieldGetCommon<Primitive::kPrimBoolean>(self, shadow_frame, obj, field, result);
197      break;
198    case Primitive::kPrimByte:
199      DoFieldGetCommon<Primitive::kPrimByte>(self, shadow_frame, obj, field, result);
200      break;
201    case Primitive::kPrimChar:
202      DoFieldGetCommon<Primitive::kPrimChar>(self, shadow_frame, obj, field, result);
203      break;
204    case Primitive::kPrimShort:
205      DoFieldGetCommon<Primitive::kPrimShort>(self, shadow_frame, obj, field, result);
206      break;
207    case Primitive::kPrimInt:
208      DoFieldGetCommon<Primitive::kPrimInt>(self, shadow_frame, obj, field, result);
209      break;
210    case Primitive::kPrimLong:
211      DoFieldGetCommon<Primitive::kPrimLong>(self, shadow_frame, obj, field, result);
212      break;
213    case Primitive::kPrimFloat:
214      DoFieldGetCommon<Primitive::kPrimInt>(self, shadow_frame, obj, field, result);
215      break;
216    case Primitive::kPrimDouble:
217      DoFieldGetCommon<Primitive::kPrimLong>(self, shadow_frame, obj, field, result);
218      break;
219    case Primitive::kPrimNot:
220      DoFieldGetCommon<Primitive::kPrimNot>(self, shadow_frame, obj, field, result);
221      break;
222    case Primitive::kPrimVoid:
223      LOG(FATAL) << "Unreachable: " << field_type;
224      UNREACHABLE();
225  }
226}
227
228// Handles iget-quick, iget-wide-quick and iget-object-quick instructions.
229// Returns true on success, otherwise throws an exception and returns false.
230template<Primitive::Type field_type>
231bool DoIGetQuick(ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) {
232  ObjPtr<mirror::Object> obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
233  if (UNLIKELY(obj == nullptr)) {
234    // We lost the reference to the field index so we cannot get a more
235    // precised exception message.
236    ThrowNullPointerExceptionFromDexPC();
237    return false;
238  }
239  MemberOffset field_offset(inst->VRegC_22c());
240  // Report this field access to instrumentation if needed. Since we only have the offset of
241  // the field from the base of the object, we need to look for it first.
242  instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
243  if (UNLIKELY(instrumentation->HasFieldReadListeners())) {
244    ArtField* f = ArtField::FindInstanceFieldWithOffset(obj->GetClass(),
245                                                        field_offset.Uint32Value());
246    DCHECK(f != nullptr);
247    DCHECK(!f->IsStatic());
248    StackHandleScope<1> hs(Thread::Current());
249    // Save obj in case the instrumentation event has thread suspension.
250    HandleWrapperObjPtr<mirror::Object> h = hs.NewHandleWrapper(&obj);
251    instrumentation->FieldReadEvent(Thread::Current(),
252                                    obj.Ptr(),
253                                    shadow_frame.GetMethod(),
254                                    shadow_frame.GetDexPC(),
255                                    f);
256  }
257  // Note: iget-x-quick instructions are only for non-volatile fields.
258  const uint32_t vregA = inst->VRegA_22c(inst_data);
259  switch (field_type) {
260    case Primitive::kPrimInt:
261      shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetField32(field_offset)));
262      break;
263    case Primitive::kPrimBoolean:
264      shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldBoolean(field_offset)));
265      break;
266    case Primitive::kPrimByte:
267      shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldByte(field_offset)));
268      break;
269    case Primitive::kPrimChar:
270      shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldChar(field_offset)));
271      break;
272    case Primitive::kPrimShort:
273      shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldShort(field_offset)));
274      break;
275    case Primitive::kPrimLong:
276      shadow_frame.SetVRegLong(vregA, static_cast<int64_t>(obj->GetField64(field_offset)));
277      break;
278    case Primitive::kPrimNot:
279      shadow_frame.SetVRegReference(vregA, obj->GetFieldObject<mirror::Object>(field_offset));
280      break;
281    default:
282      LOG(FATAL) << "Unreachable: " << field_type;
283      UNREACHABLE();
284  }
285  return true;
286}
287
288// Explicitly instantiate all DoIGetQuick functions.
289#define EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(_field_type) \
290  template bool DoIGetQuick<_field_type>(ShadowFrame& shadow_frame, const Instruction* inst, \
291                                         uint16_t inst_data)
292
293EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimInt);      // iget-quick.
294EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimBoolean);  // iget-boolean-quick.
295EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimByte);     // iget-byte-quick.
296EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimChar);     // iget-char-quick.
297EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimShort);    // iget-short-quick.
298EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimLong);     // iget-wide-quick.
299EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimNot);      // iget-object-quick.
300#undef EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL
301
302template<Primitive::Type field_type>
303static JValue GetFieldValue(const ShadowFrame& shadow_frame, uint32_t vreg)
304    REQUIRES_SHARED(Locks::mutator_lock_) {
305  JValue field_value;
306  switch (field_type) {
307    case Primitive::kPrimBoolean:
308      field_value.SetZ(static_cast<uint8_t>(shadow_frame.GetVReg(vreg)));
309      break;
310    case Primitive::kPrimByte:
311      field_value.SetB(static_cast<int8_t>(shadow_frame.GetVReg(vreg)));
312      break;
313    case Primitive::kPrimChar:
314      field_value.SetC(static_cast<uint16_t>(shadow_frame.GetVReg(vreg)));
315      break;
316    case Primitive::kPrimShort:
317      field_value.SetS(static_cast<int16_t>(shadow_frame.GetVReg(vreg)));
318      break;
319    case Primitive::kPrimInt:
320      field_value.SetI(shadow_frame.GetVReg(vreg));
321      break;
322    case Primitive::kPrimLong:
323      field_value.SetJ(shadow_frame.GetVRegLong(vreg));
324      break;
325    case Primitive::kPrimNot:
326      field_value.SetL(shadow_frame.GetVRegReference(vreg));
327      break;
328    default:
329      LOG(FATAL) << "Unreachable: " << field_type;
330      UNREACHABLE();
331  }
332  return field_value;
333}
334
335template<Primitive::Type field_type, bool do_assignability_check, bool transaction_active>
336static inline bool DoFieldPutCommon(Thread* self,
337                                    const ShadowFrame& shadow_frame,
338                                    ObjPtr<mirror::Object>& obj,
339                                    ArtField* f,
340                                    size_t vregA) REQUIRES_SHARED(Locks::mutator_lock_) {
341  f->GetDeclaringClass()->AssertInitializedOrInitializingInThread(self);
342
343  // Report this field access to instrumentation if needed. Since we only have the offset of
344  // the field from the base of the object, we need to look for it first.
345  instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
346  if (UNLIKELY(instrumentation->HasFieldWriteListeners())) {
347    StackHandleScope<1> hs(self);
348    // Wrap in handle wrapper in case the listener does thread suspension.
349    HandleWrapperObjPtr<mirror::Object> h(hs.NewHandleWrapper(&obj));
350    JValue field_value = GetFieldValue<field_type>(shadow_frame, vregA);
351    ObjPtr<mirror::Object> this_object = f->IsStatic() ? nullptr : obj;
352    instrumentation->FieldWriteEvent(self, this_object.Ptr(),
353                                     shadow_frame.GetMethod(),
354                                     shadow_frame.GetDexPC(),
355                                     f,
356                                     field_value);
357  }
358
359  switch (field_type) {
360    case Primitive::kPrimBoolean:
361      f->SetBoolean<transaction_active>(obj, shadow_frame.GetVReg(vregA));
362      break;
363    case Primitive::kPrimByte:
364      f->SetByte<transaction_active>(obj, shadow_frame.GetVReg(vregA));
365      break;
366    case Primitive::kPrimChar:
367      f->SetChar<transaction_active>(obj, shadow_frame.GetVReg(vregA));
368      break;
369    case Primitive::kPrimShort:
370      f->SetShort<transaction_active>(obj, shadow_frame.GetVReg(vregA));
371      break;
372    case Primitive::kPrimInt:
373      f->SetInt<transaction_active>(obj, shadow_frame.GetVReg(vregA));
374      break;
375    case Primitive::kPrimLong:
376      f->SetLong<transaction_active>(obj, shadow_frame.GetVRegLong(vregA));
377      break;
378    case Primitive::kPrimNot: {
379      ObjPtr<mirror::Object> reg = shadow_frame.GetVRegReference(vregA);
380      if (do_assignability_check && reg != nullptr) {
381        // FieldHelper::GetType can resolve classes, use a handle wrapper which will restore the
382        // object in the destructor.
383        ObjPtr<mirror::Class> field_class;
384        {
385          StackHandleScope<2> hs(self);
386          HandleWrapperObjPtr<mirror::Object> h_reg(hs.NewHandleWrapper(&reg));
387          HandleWrapperObjPtr<mirror::Object> h_obj(hs.NewHandleWrapper(&obj));
388          field_class = f->GetType<true>();
389        }
390        if (!reg->VerifierInstanceOf(field_class.Ptr())) {
391          // This should never happen.
392          std::string temp1, temp2, temp3;
393          self->ThrowNewExceptionF("Ljava/lang/VirtualMachineError;",
394                                   "Put '%s' that is not instance of field '%s' in '%s'",
395                                   reg->GetClass()->GetDescriptor(&temp1),
396                                   field_class->GetDescriptor(&temp2),
397                                   f->GetDeclaringClass()->GetDescriptor(&temp3));
398          return false;
399        }
400      }
401      f->SetObj<transaction_active>(obj, reg);
402      break;
403    }
404    default:
405      LOG(FATAL) << "Unreachable: " << field_type;
406      UNREACHABLE();
407  }
408  return true;
409}
410
411template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check,
412         bool transaction_active>
413bool DoFieldPut(Thread* self, const ShadowFrame& shadow_frame, const Instruction* inst,
414                uint16_t inst_data) {
415  const bool do_assignability_check = do_access_check;
416  bool is_static = (find_type == StaticObjectWrite) || (find_type == StaticPrimitiveWrite);
417  uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
418  ArtField* f =
419      FindFieldFromCode<find_type, do_access_check>(field_idx, shadow_frame.GetMethod(), self,
420                                                    Primitive::ComponentSize(field_type));
421  if (UNLIKELY(f == nullptr)) {
422    CHECK(self->IsExceptionPending());
423    return false;
424  }
425  ObjPtr<mirror::Object> obj;
426  if (is_static) {
427    obj = f->GetDeclaringClass();
428  } else {
429    obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
430    if (UNLIKELY(obj == nullptr)) {
431      ThrowNullPointerExceptionForFieldAccess(f, false);
432      return false;
433    }
434  }
435
436  uint32_t vregA = is_static ? inst->VRegA_21c(inst_data) : inst->VRegA_22c(inst_data);
437  return DoFieldPutCommon<field_type, do_assignability_check, transaction_active>(self,
438                                                                                  shadow_frame,
439                                                                                  obj,
440                                                                                  f,
441                                                                                  vregA);
442}
443
444// Explicitly instantiate all DoFieldPut functions.
445#define EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, _do_check, _transaction_active) \
446  template bool DoFieldPut<_find_type, _field_type, _do_check, _transaction_active>(Thread* self, \
447      const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data)
448
449#define EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(_find_type, _field_type)  \
450    EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, false, false);  \
451    EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, true, false);  \
452    EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, false, true);  \
453    EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, true, true);
454
455// iput-XXX
456EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimBoolean)
457EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimByte)
458EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimChar)
459EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimShort)
460EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimInt)
461EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimLong)
462EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstanceObjectWrite, Primitive::kPrimNot)
463
464// sput-XXX
465EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimBoolean)
466EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimByte)
467EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimChar)
468EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimShort)
469EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimInt)
470EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimLong)
471EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticObjectWrite, Primitive::kPrimNot)
472
473#undef EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL
474#undef EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL
475
476// Helper for setters in invoke-polymorphic.
477bool DoFieldPutForInvokePolymorphic(Thread* self,
478                                    ShadowFrame& shadow_frame,
479                                    ObjPtr<mirror::Object>& obj,
480                                    ArtField* field,
481                                    Primitive::Type field_type,
482                                    size_t vregA) REQUIRES_SHARED(Locks::mutator_lock_) {
483  static const bool kDoCheckAssignability = false;
484  static const bool kTransaction = false;
485  switch (field_type) {
486    case Primitive::kPrimBoolean:
487      return DoFieldPutCommon<Primitive::kPrimBoolean, kDoCheckAssignability, kTransaction>(
488          self, shadow_frame, obj, field, vregA);
489    case Primitive::kPrimByte:
490      return DoFieldPutCommon<Primitive::kPrimByte, kDoCheckAssignability, kTransaction>(
491          self, shadow_frame, obj, field, vregA);
492    case Primitive::kPrimChar:
493      return DoFieldPutCommon<Primitive::kPrimChar, kDoCheckAssignability, kTransaction>(
494          self, shadow_frame, obj, field, vregA);
495    case Primitive::kPrimShort:
496      return DoFieldPutCommon<Primitive::kPrimShort, kDoCheckAssignability, kTransaction>(
497          self, shadow_frame, obj, field, vregA);
498    case Primitive::kPrimInt:
499      return DoFieldPutCommon<Primitive::kPrimInt, kDoCheckAssignability, kTransaction>(
500          self, shadow_frame, obj, field, vregA);
501    case Primitive::kPrimLong:
502      return DoFieldPutCommon<Primitive::kPrimLong, kDoCheckAssignability, kTransaction>(
503          self, shadow_frame, obj, field, vregA);
504    case Primitive::kPrimFloat:
505      return DoFieldPutCommon<Primitive::kPrimInt, kDoCheckAssignability, kTransaction>(
506          self, shadow_frame, obj, field, vregA);
507    case Primitive::kPrimDouble:
508      return DoFieldPutCommon<Primitive::kPrimLong, kDoCheckAssignability, kTransaction>(
509          self, shadow_frame, obj, field, vregA);
510    case Primitive::kPrimNot:
511      return DoFieldPutCommon<Primitive::kPrimNot, kDoCheckAssignability, kTransaction>(
512          self, shadow_frame, obj, field, vregA);
513    case Primitive::kPrimVoid:
514      LOG(FATAL) << "Unreachable: " << field_type;
515      UNREACHABLE();
516  }
517}
518
519template<Primitive::Type field_type, bool transaction_active>
520bool DoIPutQuick(const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) {
521  ObjPtr<mirror::Object> obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
522  if (UNLIKELY(obj == nullptr)) {
523    // We lost the reference to the field index so we cannot get a more
524    // precised exception message.
525    ThrowNullPointerExceptionFromDexPC();
526    return false;
527  }
528  MemberOffset field_offset(inst->VRegC_22c());
529  const uint32_t vregA = inst->VRegA_22c(inst_data);
530  // Report this field modification to instrumentation if needed. Since we only have the offset of
531  // the field from the base of the object, we need to look for it first.
532  instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
533  if (UNLIKELY(instrumentation->HasFieldWriteListeners())) {
534    ArtField* f = ArtField::FindInstanceFieldWithOffset(obj->GetClass(),
535                                                        field_offset.Uint32Value());
536    DCHECK(f != nullptr);
537    DCHECK(!f->IsStatic());
538    JValue field_value = GetFieldValue<field_type>(shadow_frame, vregA);
539    StackHandleScope<1> hs(Thread::Current());
540    // Save obj in case the instrumentation event has thread suspension.
541    HandleWrapperObjPtr<mirror::Object> h = hs.NewHandleWrapper(&obj);
542    instrumentation->FieldWriteEvent(Thread::Current(),
543                                     obj.Ptr(),
544                                     shadow_frame.GetMethod(),
545                                     shadow_frame.GetDexPC(),
546                                     f,
547                                     field_value);
548  }
549  // Note: iput-x-quick instructions are only for non-volatile fields.
550  switch (field_type) {
551    case Primitive::kPrimBoolean:
552      obj->SetFieldBoolean<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
553      break;
554    case Primitive::kPrimByte:
555      obj->SetFieldByte<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
556      break;
557    case Primitive::kPrimChar:
558      obj->SetFieldChar<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
559      break;
560    case Primitive::kPrimShort:
561      obj->SetFieldShort<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
562      break;
563    case Primitive::kPrimInt:
564      obj->SetField32<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
565      break;
566    case Primitive::kPrimLong:
567      obj->SetField64<transaction_active>(field_offset, shadow_frame.GetVRegLong(vregA));
568      break;
569    case Primitive::kPrimNot:
570      obj->SetFieldObject<transaction_active>(field_offset, shadow_frame.GetVRegReference(vregA));
571      break;
572    default:
573      LOG(FATAL) << "Unreachable: " << field_type;
574      UNREACHABLE();
575  }
576  return true;
577}
578
579// Explicitly instantiate all DoIPutQuick functions.
580#define EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, _transaction_active) \
581  template bool DoIPutQuick<_field_type, _transaction_active>(const ShadowFrame& shadow_frame, \
582                                                              const Instruction* inst, \
583                                                              uint16_t inst_data)
584
585#define EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(_field_type)   \
586  EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, false);     \
587  EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, true);
588
589EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimInt)      // iput-quick.
590EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimBoolean)  // iput-boolean-quick.
591EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimByte)     // iput-byte-quick.
592EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimChar)     // iput-char-quick.
593EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimShort)    // iput-short-quick.
594EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimLong)     // iput-wide-quick.
595EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimNot)      // iput-object-quick.
596#undef EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL
597#undef EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL
598
599// We accept a null Instrumentation* meaning we must not report anything to the instrumentation.
600uint32_t FindNextInstructionFollowingException(
601    Thread* self, ShadowFrame& shadow_frame, uint32_t dex_pc,
602    const instrumentation::Instrumentation* instrumentation) {
603  self->VerifyStack();
604  StackHandleScope<2> hs(self);
605  Handle<mirror::Throwable> exception(hs.NewHandle(self->GetException()));
606  if (instrumentation != nullptr && instrumentation->HasExceptionCaughtListeners()
607      && self->IsExceptionThrownByCurrentMethod(exception.Get())) {
608    instrumentation->ExceptionCaughtEvent(self, exception.Get());
609  }
610  bool clear_exception = false;
611  uint32_t found_dex_pc = shadow_frame.GetMethod()->FindCatchBlock(
612      hs.NewHandle(exception->GetClass()), dex_pc, &clear_exception);
613  if (found_dex_pc == DexFile::kDexNoIndex && instrumentation != nullptr) {
614    // Exception is not caught by the current method. We will unwind to the
615    // caller. Notify any instrumentation listener.
616    instrumentation->MethodUnwindEvent(self, shadow_frame.GetThisObject(),
617                                       shadow_frame.GetMethod(), dex_pc);
618  } else {
619    // Exception is caught in the current method. We will jump to the found_dex_pc.
620    if (clear_exception) {
621      self->ClearException();
622    }
623  }
624  return found_dex_pc;
625}
626
627void UnexpectedOpcode(const Instruction* inst, const ShadowFrame& shadow_frame) {
628  LOG(FATAL) << "Unexpected instruction: "
629             << inst->DumpString(shadow_frame.GetMethod()->GetDexFile());
630  UNREACHABLE();
631}
632
633void AbortTransactionF(Thread* self, const char* fmt, ...) {
634  va_list args;
635  va_start(args, fmt);
636  AbortTransactionV(self, fmt, args);
637  va_end(args);
638}
639
640void AbortTransactionV(Thread* self, const char* fmt, va_list args) {
641  CHECK(Runtime::Current()->IsActiveTransaction());
642  // Constructs abort message.
643  std::string abort_msg;
644  StringAppendV(&abort_msg, fmt, args);
645  // Throws an exception so we can abort the transaction and rollback every change.
646  Runtime::Current()->AbortTransactionAndThrowAbortError(self, abort_msg);
647}
648
649// START DECLARATIONS :
650//
651// These additional declarations are required because clang complains
652// about ALWAYS_INLINE (-Werror, -Wgcc-compat) in definitions.
653//
654
655template <bool is_range, bool do_assignability_check>
656static ALWAYS_INLINE bool DoCallCommon(ArtMethod* called_method,
657                                       Thread* self,
658                                       ShadowFrame& shadow_frame,
659                                       JValue* result,
660                                       uint16_t number_of_inputs,
661                                       uint32_t (&arg)[Instruction::kMaxVarArgRegs],
662                                       uint32_t vregC) REQUIRES_SHARED(Locks::mutator_lock_);
663
664template <bool is_range>
665static ALWAYS_INLINE bool DoCallPolymorphic(ArtMethod* called_method,
666                                            Handle<mirror::MethodType> callsite_type,
667                                            Handle<mirror::MethodType> target_type,
668                                            Thread* self,
669                                            ShadowFrame& shadow_frame,
670                                            JValue* result,
671                                            uint32_t (&arg)[Instruction::kMaxVarArgRegs],
672                                            uint32_t vregC,
673                                            const MethodHandleKind handle_kind)
674  REQUIRES_SHARED(Locks::mutator_lock_);
675
676template <bool is_range>
677static ALWAYS_INLINE bool DoCallTransform(ArtMethod* called_method,
678                                          Handle<mirror::MethodType> callsite_type,
679                                          Handle<mirror::MethodType> callee_type,
680                                          Thread* self,
681                                          ShadowFrame& shadow_frame,
682                                          Handle<mirror::MethodHandleImpl> receiver,
683                                          JValue* result,
684                                          uint32_t (&arg)[Instruction::kMaxVarArgRegs],
685                                          uint32_t vregC) REQUIRES_SHARED(Locks::mutator_lock_);
686
687ALWAYS_INLINE void PerformCall(Thread* self,
688                               const DexFile::CodeItem* code_item,
689                               ArtMethod* caller_method,
690                               const size_t first_dest_reg,
691                               ShadowFrame* callee_frame,
692                               JValue* result) REQUIRES_SHARED(Locks::mutator_lock_);
693
694template <bool is_range>
695ALWAYS_INLINE void CopyRegisters(ShadowFrame& caller_frame,
696                                 ShadowFrame* callee_frame,
697                                 const uint32_t (&arg)[Instruction::kMaxVarArgRegs],
698                                 const size_t first_src_reg,
699                                 const size_t first_dest_reg,
700                                 const size_t num_regs) REQUIRES_SHARED(Locks::mutator_lock_);
701
702// END DECLARATIONS.
703
704void ArtInterpreterToCompiledCodeBridge(Thread* self,
705                                        ArtMethod* caller,
706                                        const DexFile::CodeItem* code_item,
707                                        ShadowFrame* shadow_frame,
708                                        JValue* result)
709    REQUIRES_SHARED(Locks::mutator_lock_) {
710  ArtMethod* method = shadow_frame->GetMethod();
711  // Ensure static methods are initialized.
712  if (method->IsStatic()) {
713    ObjPtr<mirror::Class> declaringClass = method->GetDeclaringClass();
714    if (UNLIKELY(!declaringClass->IsInitialized())) {
715      self->PushShadowFrame(shadow_frame);
716      StackHandleScope<1> hs(self);
717      Handle<mirror::Class> h_class(hs.NewHandle(declaringClass));
718      if (UNLIKELY(!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true,
719                                                                            true))) {
720        self->PopShadowFrame();
721        DCHECK(self->IsExceptionPending());
722        return;
723      }
724      self->PopShadowFrame();
725      CHECK(h_class->IsInitializing());
726      // Reload from shadow frame in case the method moved, this is faster than adding a handle.
727      method = shadow_frame->GetMethod();
728    }
729  }
730  uint16_t arg_offset = (code_item == nullptr)
731                            ? 0
732                            : code_item->registers_size_ - code_item->ins_size_;
733  jit::Jit* jit = Runtime::Current()->GetJit();
734  if (jit != nullptr && caller != nullptr) {
735    jit->NotifyInterpreterToCompiledCodeTransition(self, caller);
736  }
737  method->Invoke(self, shadow_frame->GetVRegArgs(arg_offset),
738                 (shadow_frame->NumberOfVRegs() - arg_offset) * sizeof(uint32_t),
739                 result, method->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty());
740}
741
742void SetStringInitValueToAllAliases(ShadowFrame* shadow_frame,
743                                    uint16_t this_obj_vreg,
744                                    JValue result)
745    REQUIRES_SHARED(Locks::mutator_lock_) {
746  ObjPtr<mirror::Object> existing = shadow_frame->GetVRegReference(this_obj_vreg);
747  if (existing == nullptr) {
748    // If it's null, we come from compiled code that was deoptimized. Nothing to do,
749    // as the compiler verified there was no alias.
750    // Set the new string result of the StringFactory.
751    shadow_frame->SetVRegReference(this_obj_vreg, result.GetL());
752    return;
753  }
754  // Set the string init result into all aliases.
755  for (uint32_t i = 0, e = shadow_frame->NumberOfVRegs(); i < e; ++i) {
756    if (shadow_frame->GetVRegReference(i) == existing) {
757      DCHECK_EQ(shadow_frame->GetVRegReference(i),
758                reinterpret_cast<mirror::Object*>(shadow_frame->GetVReg(i)));
759      shadow_frame->SetVRegReference(i, result.GetL());
760      DCHECK_EQ(shadow_frame->GetVRegReference(i),
761                reinterpret_cast<mirror::Object*>(shadow_frame->GetVReg(i)));
762    }
763  }
764}
765
766inline static bool IsInvokeExact(const DexFile& dex_file, int invoke_method_idx) {
767  // This check uses string comparison as it needs less code and data
768  // to do than fetching the associated ArtMethod from the DexCache
769  // and checking against ArtMethods in the well known classes. The
770  // verifier needs to perform a more rigorous check.
771  const char* method_name = dex_file.GetMethodName(dex_file.GetMethodId(invoke_method_idx));
772  bool is_invoke_exact = (0 == strcmp(method_name, "invokeExact"));
773  DCHECK(is_invoke_exact || (0 == strcmp(method_name, "invoke")));
774  return is_invoke_exact;
775}
776
777template<bool is_range, bool do_access_check>
778inline bool DoInvokePolymorphic(Thread* self,
779                                ShadowFrame& shadow_frame,
780                                const Instruction* inst,
781                                uint16_t inst_data,
782                                JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) {
783  // Invoke-polymorphic instructions always take a receiver. i.e, they are never static.
784  const uint32_t vRegC = (is_range) ? inst->VRegC_4rcc() : inst->VRegC_45cc();
785  const int invoke_method_idx = (is_range) ? inst->VRegB_4rcc() : inst->VRegB_45cc();
786
787  // Determine if this invocation is MethodHandle.invoke() or
788  // MethodHandle.invokeExact().
789  bool is_invoke_exact = IsInvokeExact(shadow_frame.GetMethod()->GetDeclaringClass()->GetDexFile(),
790                                       invoke_method_idx);
791
792  // The invoke_method_idx here is the name of the signature polymorphic method that
793  // was symbolically invoked in bytecode (say MethodHandle.invoke or MethodHandle.invokeExact)
794  // and not the method that we'll dispatch to in the end.
795  //
796  // TODO(narayan) We'll have to check in the verifier that this is in fact a
797  // signature polymorphic method so that we disallow calls via invoke-polymorphic
798  // to non sig-poly methods. This would also have the side effect of verifying
799  // that vRegC really is a reference type.
800  StackHandleScope<6> hs(self);
801  Handle<mirror::MethodHandleImpl> method_handle(hs.NewHandle(
802      ObjPtr<mirror::MethodHandleImpl>::DownCast(
803          MakeObjPtr(shadow_frame.GetVRegReference(vRegC)))));
804  if (UNLIKELY(method_handle.Get() == nullptr)) {
805    // Note that the invoke type is kVirtual here because a call to a signature
806    // polymorphic method is shaped like a virtual call at the bytecode level.
807    ThrowNullPointerExceptionForMethodAccess(invoke_method_idx, InvokeType::kVirtual);
808    result->SetJ(0);
809    return false;
810  }
811
812  // The vRegH value gives the index of the proto_id associated with this
813  // signature polymorphic callsite.
814  const uint32_t callsite_proto_id = (is_range) ? inst->VRegH_4rcc() : inst->VRegH_45cc();
815
816  // Call through to the classlinker and ask it to resolve the static type associated
817  // with the callsite. This information is stored in the dex cache so it's
818  // guaranteed to be fast after the first resolution.
819  ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
820  Handle<mirror::Class> caller_class(hs.NewHandle(shadow_frame.GetMethod()->GetDeclaringClass()));
821  Handle<mirror::MethodType> callsite_type(hs.NewHandle(class_linker->ResolveMethodType(
822      caller_class->GetDexFile(), callsite_proto_id,
823      hs.NewHandle<mirror::DexCache>(caller_class->GetDexCache()),
824      hs.NewHandle<mirror::ClassLoader>(caller_class->GetClassLoader()))));
825
826  // This implies we couldn't resolve one or more types in this method handle.
827  if (UNLIKELY(callsite_type.Get() == nullptr)) {
828    CHECK(self->IsExceptionPending());
829    result->SetJ(0);
830    return false;
831  }
832
833  const MethodHandleKind handle_kind = method_handle->GetHandleKind();
834  Handle<mirror::MethodType> handle_type(hs.NewHandle(method_handle->GetMethodType()));
835  CHECK(handle_type.Get() != nullptr);
836  if (UNLIKELY(is_invoke_exact && !callsite_type->IsExactMatch(handle_type.Get()))) {
837    ThrowWrongMethodTypeException(handle_type.Get(), callsite_type.Get());
838    return false;
839  }
840
841  uint32_t arg[Instruction::kMaxVarArgRegs] = {};
842  uint32_t first_src_reg = 0;
843  if (is_range) {
844    first_src_reg = (inst->VRegC_4rcc() + 1);
845  } else {
846    inst->GetVarArgs(arg, inst_data);
847    arg[0] = arg[1];
848    arg[1] = arg[2];
849    arg[2] = arg[3];
850    arg[3] = arg[4];
851    arg[4] = 0;
852    first_src_reg = arg[0];
853  }
854
855  if (IsInvoke(handle_kind)) {
856    // Get the method we're actually invoking along with the kind of
857    // invoke that is desired. We don't need to perform access checks at this
858    // point because they would have been performed on our behalf at the point
859    // of creation of the method handle.
860    ArtMethod* called_method = method_handle->GetTargetMethod();
861    CHECK(called_method != nullptr);
862
863    if (handle_kind == kInvokeVirtual || handle_kind == kInvokeInterface) {
864      // TODO: Unfortunately, we have to postpone dynamic receiver based checks
865      // because the receiver might be cast or might come from an emulated stack
866      // frame, which means that it is unknown at this point. We perform these
867      // checks inside DoCallPolymorphic right before we do the actualy invoke.
868    } else if (handle_kind == kInvokeDirect) {
869      if (called_method->IsConstructor()) {
870        // TODO(narayan) : We need to handle the case where the target method is a
871        // constructor here.
872        UNIMPLEMENTED(FATAL) << "Direct invokes for constructors are not implemented yet.";
873        return false;
874      }
875
876      // Nothing special to do in the case where we're not dealing with a
877      // constructor. It's a private method, and we've already access checked at
878      // the point of creating the handle.
879    } else if (handle_kind == kInvokeSuper) {
880      ObjPtr<mirror::Class> declaring_class = called_method->GetDeclaringClass();
881
882      // Note that we're not dynamically dispatching on the type of the receiver
883      // here. We use the static type of the "receiver" object that we've
884      // recorded in the method handle's type, which will be the same as the
885      // special caller that was specified at the point of lookup.
886      ObjPtr<mirror::Class> referrer_class = handle_type->GetPTypes()->Get(0);
887      if (!declaring_class->IsInterface()) {
888        ObjPtr<mirror::Class> super_class = referrer_class->GetSuperClass();
889        uint16_t vtable_index = called_method->GetMethodIndex();
890        DCHECK(super_class != nullptr);
891        DCHECK(super_class->HasVTable());
892        // Note that super_class is a super of referrer_class and called_method
893        // will always be declared by super_class (or one of its super classes).
894        DCHECK_LT(vtable_index, super_class->GetVTableLength());
895        called_method = super_class->GetVTableEntry(vtable_index, kRuntimePointerSize);
896      } else {
897        called_method = referrer_class->FindVirtualMethodForInterfaceSuper(
898            called_method, kRuntimePointerSize);
899      }
900
901      CHECK(called_method != nullptr);
902    }
903
904    if (handle_kind == kInvokeTransform) {
905      return DoCallTransform<is_range>(called_method,
906                                       callsite_type,
907                                       handle_type,
908                                       self,
909                                       shadow_frame,
910                                       method_handle /* receiver */,
911                                       result,
912                                       arg,
913                                       first_src_reg);
914    } else {
915      return DoCallPolymorphic<is_range>(called_method,
916                                         callsite_type,
917                                         handle_type,
918                                         self,
919                                         shadow_frame,
920                                         result,
921                                         arg,
922                                         first_src_reg,
923                                         handle_kind);
924    }
925  } else {
926    DCHECK(!is_range);
927    ArtField* field = method_handle->GetTargetField();
928    Primitive::Type field_type = field->GetTypeAsPrimitiveType();;
929
930    if (!is_invoke_exact) {
931      // TODO(oth): conversion plumbing for invoke().
932      UNIMPLEMENTED(FATAL);
933    }
934
935    switch (handle_kind) {
936      case kInstanceGet: {
937        ObjPtr<mirror::Object> obj = shadow_frame.GetVRegReference(first_src_reg);
938        DoFieldGetForInvokePolymorphic(self, shadow_frame, obj, field, field_type, result);
939        return true;
940      }
941      case kInstancePut: {
942        ObjPtr<mirror::Object> obj = shadow_frame.GetVRegReference(first_src_reg);
943        return DoFieldPutForInvokePolymorphic(self, shadow_frame, obj, field, field_type, arg[1]);
944      }
945      case kStaticGet: {
946        ObjPtr<mirror::Object> obj = field->GetDeclaringClass();
947        DoFieldGetForInvokePolymorphic(self, shadow_frame, obj, field, field_type, result);
948        return true;
949      }
950      case kStaticPut: {
951        ObjPtr<mirror::Object> obj = field->GetDeclaringClass();
952        return DoFieldPutForInvokePolymorphic(self, shadow_frame, obj, field, field_type, arg[0]);
953      }
954      default:
955        LOG(FATAL) << "Unreachable: " << handle_kind;
956        UNREACHABLE();
957    }
958  }
959}
960
961// Calculate the number of ins for a proxy or native method, where we
962// can't just look at the code item.
963static inline size_t GetInsForProxyOrNativeMethod(ArtMethod* method)
964    REQUIRES_SHARED(Locks::mutator_lock_) {
965  DCHECK(method->IsNative() || method->IsProxyMethod());
966
967  method = method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
968  size_t num_ins = 0;
969  // Separate accounting for the receiver, which isn't a part of the
970  // shorty.
971  if (!method->IsStatic()) {
972    ++num_ins;
973  }
974
975  uint32_t shorty_len = 0;
976  const char* shorty = method->GetShorty(&shorty_len);
977  for (size_t i = 1; i < shorty_len; ++i) {
978    const char c = shorty[i];
979    ++num_ins;
980    if (c == 'J' || c == 'D') {
981      ++num_ins;
982    }
983  }
984
985  return num_ins;
986}
987
988
989inline void PerformCall(Thread* self,
990                        const DexFile::CodeItem* code_item,
991                        ArtMethod* caller_method,
992                        const size_t first_dest_reg,
993                        ShadowFrame* callee_frame,
994                        JValue* result) {
995  if (LIKELY(Runtime::Current()->IsStarted())) {
996    ArtMethod* target = callee_frame->GetMethod();
997    if (ClassLinker::ShouldUseInterpreterEntrypoint(
998        target,
999        target->GetEntryPointFromQuickCompiledCode())) {
1000      ArtInterpreterToInterpreterBridge(self, code_item, callee_frame, result);
1001    } else {
1002      ArtInterpreterToCompiledCodeBridge(
1003          self, caller_method, code_item, callee_frame, result);
1004    }
1005  } else {
1006    UnstartedRuntime::Invoke(self, code_item, callee_frame, result, first_dest_reg);
1007  }
1008}
1009
1010template <bool is_range>
1011inline void CopyRegisters(ShadowFrame& caller_frame,
1012                          ShadowFrame* callee_frame,
1013                          const uint32_t (&arg)[Instruction::kMaxVarArgRegs],
1014                          const size_t first_src_reg,
1015                          const size_t first_dest_reg,
1016                          const size_t num_regs) {
1017  if (is_range) {
1018    const size_t dest_reg_bound = first_dest_reg + num_regs;
1019    for (size_t src_reg = first_src_reg, dest_reg = first_dest_reg; dest_reg < dest_reg_bound;
1020        ++dest_reg, ++src_reg) {
1021      AssignRegister(callee_frame, caller_frame, dest_reg, src_reg);
1022    }
1023  } else {
1024    DCHECK_LE(num_regs, arraysize(arg));
1025
1026    for (size_t arg_index = 0; arg_index < num_regs; ++arg_index) {
1027      AssignRegister(callee_frame, caller_frame, first_dest_reg + arg_index, arg[arg_index]);
1028    }
1029  }
1030}
1031
1032// Returns true iff. the callsite type for a polymorphic invoke is transformer
1033// like, i.e that it has a single input argument whose type is
1034// dalvik.system.EmulatedStackFrame.
1035static inline bool IsCallerTransformer(Handle<mirror::MethodType> callsite_type)
1036    REQUIRES_SHARED(Locks::mutator_lock_) {
1037  ObjPtr<mirror::ObjectArray<mirror::Class>> param_types(callsite_type->GetPTypes());
1038  if (param_types->GetLength() == 1) {
1039    ObjPtr<mirror::Class> param(param_types->GetWithoutChecks(0));
1040    return param == WellKnownClasses::ToClass(WellKnownClasses::dalvik_system_EmulatedStackFrame);
1041  }
1042
1043  return false;
1044}
1045
1046template <bool is_range>
1047static inline bool DoCallPolymorphic(ArtMethod* called_method,
1048                                     Handle<mirror::MethodType> callsite_type,
1049                                     Handle<mirror::MethodType> target_type,
1050                                     Thread* self,
1051                                     ShadowFrame& shadow_frame,
1052                                     JValue* result,
1053                                     uint32_t (&arg)[Instruction::kMaxVarArgRegs],
1054                                     uint32_t first_src_reg,
1055                                     const MethodHandleKind handle_kind) {
1056  // TODO(narayan): Wire in the String.init hacks.
1057
1058  // Compute method information.
1059  const DexFile::CodeItem* code_item = called_method->GetCodeItem();
1060
1061  // Number of registers for the callee's call frame. Note that for non-exact
1062  // invokes, we always derive this information from the callee method. We
1063  // cannot guarantee during verification that the number of registers encoded
1064  // in the invoke is equal to the number of ins for the callee. This is because
1065  // some transformations (such as boxing a long -> Long or wideining an
1066  // int -> long will change that number.
1067  uint16_t num_regs;
1068  size_t num_input_regs;
1069  size_t first_dest_reg;
1070  if (LIKELY(code_item != nullptr)) {
1071    num_regs = code_item->registers_size_;
1072    first_dest_reg = num_regs - code_item->ins_size_;
1073    num_input_regs = code_item->ins_size_;
1074    // Parameter registers go at the end of the shadow frame.
1075    DCHECK_NE(first_dest_reg, (size_t)-1);
1076  } else {
1077    // No local regs for proxy and native methods.
1078    DCHECK(called_method->IsNative() || called_method->IsProxyMethod());
1079    num_regs = num_input_regs = GetInsForProxyOrNativeMethod(called_method);
1080    first_dest_reg = 0;
1081  }
1082
1083  // Allocate shadow frame on the stack.
1084  ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
1085      CREATE_SHADOW_FRAME(num_regs, &shadow_frame, called_method, /* dex pc */ 0);
1086  ShadowFrame* new_shadow_frame = shadow_frame_unique_ptr.get();
1087
1088  // Whether this polymorphic invoke was issued by a transformer method.
1089  bool is_caller_transformer = false;
1090  // Thread might be suspended during PerformArgumentConversions due to the
1091  // allocations performed during boxing.
1092  {
1093    ScopedStackedShadowFramePusher pusher(
1094        self, new_shadow_frame, StackedShadowFrameType::kShadowFrameUnderConstruction);
1095    if (callsite_type->IsExactMatch(target_type.Get())) {
1096      // This is an exact invoke, we can take the fast path of just copying all
1097      // registers without performing any argument conversions.
1098      CopyRegisters<is_range>(shadow_frame,
1099                              new_shadow_frame,
1100                              arg,
1101                              first_src_reg,
1102                              first_dest_reg,
1103                              num_input_regs);
1104    } else {
1105      // This includes the case where we're entering this invoke-polymorphic
1106      // from a transformer method. In that case, the callsite_type will contain
1107      // a single argument of type dalvik.system.EmulatedStackFrame. In that
1108      // case, we'll have to unmarshal the EmulatedStackFrame into the
1109      // new_shadow_frame and perform argument conversions on it.
1110      if (IsCallerTransformer(callsite_type)) {
1111        is_caller_transformer = true;
1112        // The emulated stack frame is the first and only argument when we're coming
1113        // through from a transformer.
1114        ObjPtr<mirror::EmulatedStackFrame> emulated_stack_frame(
1115            reinterpret_cast<mirror::EmulatedStackFrame*>(
1116                shadow_frame.GetVRegReference(first_src_reg)));
1117        if (!emulated_stack_frame->WriteToShadowFrame(self,
1118                                                      target_type,
1119                                                      first_dest_reg,
1120                                                      new_shadow_frame)) {
1121          DCHECK(self->IsExceptionPending());
1122          result->SetL(0);
1123          return false;
1124        }
1125      } else if (!ConvertAndCopyArgumentsFromCallerFrame<is_range>(self,
1126                                                                   callsite_type,
1127                                                                   target_type,
1128                                                                   shadow_frame,
1129                                                                   first_src_reg,
1130                                                                   first_dest_reg,
1131                                                                   arg,
1132                                                                   new_shadow_frame)) {
1133        DCHECK(self->IsExceptionPending());
1134        result->SetL(0);
1135        return false;
1136      }
1137    }
1138  }
1139
1140  // See TODO in DoInvokePolymorphic : We need to perform this dynamic, receiver
1141  // based dispatch right before we perform the actual call, because the
1142  // receiver isn't known very early.
1143  if (handle_kind == kInvokeVirtual || handle_kind == kInvokeInterface) {
1144    ObjPtr<mirror::Object> receiver(new_shadow_frame->GetVRegReference(first_dest_reg));
1145    ObjPtr<mirror::Class> declaring_class(called_method->GetDeclaringClass());
1146    // Verify that _vRegC is an object reference and of the type expected by
1147    // the receiver.
1148    if (!VerifyObjectIsClass(receiver, declaring_class)) {
1149      DCHECK(self->IsExceptionPending());
1150      return false;
1151    }
1152
1153    called_method = receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(
1154        called_method, kRuntimePointerSize);
1155  }
1156
1157  PerformCall(self, code_item, shadow_frame.GetMethod(), first_dest_reg, new_shadow_frame, result);
1158
1159  // TODO(narayan): Perform return value conversions.
1160
1161  // If the caller of this signature polymorphic method was a transformer,
1162  // we need to copy the result back out to the emulated stack frame.
1163  if (is_caller_transformer && !self->IsExceptionPending()) {
1164    ObjPtr<mirror::EmulatedStackFrame> emulated_stack_frame(
1165        reinterpret_cast<mirror::EmulatedStackFrame*>(
1166            shadow_frame.GetVRegReference(first_src_reg)));
1167
1168    emulated_stack_frame->SetReturnValue(self, *result);
1169  }
1170
1171  return !self->IsExceptionPending();
1172}
1173
1174template <bool is_range>
1175static inline bool DoCallTransform(ArtMethod* called_method,
1176                                   Handle<mirror::MethodType> callsite_type,
1177                                   Handle<mirror::MethodType> callee_type,
1178                                   Thread* self,
1179                                   ShadowFrame& shadow_frame,
1180                                   Handle<mirror::MethodHandleImpl> receiver,
1181                                   JValue* result,
1182                                   uint32_t (&arg)[Instruction::kMaxVarArgRegs],
1183                                   uint32_t first_src_reg) {
1184  // This can be fixed to two, because the method we're calling here
1185  // (MethodHandle.transformInternal) doesn't have any locals and the signature
1186  // is known :
1187  //
1188  // private MethodHandle.transformInternal(EmulatedStackFrame sf);
1189  //
1190  // This means we need only two vregs :
1191  // - One for the receiver object.
1192  // - One for the only method argument (an EmulatedStackFrame).
1193  static constexpr size_t kNumRegsForTransform = 2;
1194
1195  const DexFile::CodeItem* code_item = called_method->GetCodeItem();
1196  DCHECK(code_item != nullptr);
1197  DCHECK_EQ(kNumRegsForTransform, code_item->registers_size_);
1198  DCHECK_EQ(kNumRegsForTransform, code_item->ins_size_);
1199
1200  ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
1201      CREATE_SHADOW_FRAME(kNumRegsForTransform, &shadow_frame, called_method, /* dex pc */ 0);
1202  ShadowFrame* new_shadow_frame = shadow_frame_unique_ptr.get();
1203
1204  StackHandleScope<1> hs(self);
1205  MutableHandle<mirror::EmulatedStackFrame> sf(hs.NewHandle<mirror::EmulatedStackFrame>(nullptr));
1206  if (IsCallerTransformer(callsite_type)) {
1207    // If we're entering this transformer from another transformer, we can pass
1208    // through the handle directly to the callee, instead of having to
1209    // instantiate a new stack frame based on the shadow frame.
1210    sf.Assign(reinterpret_cast<mirror::EmulatedStackFrame*>(
1211        shadow_frame.GetVRegReference(first_src_reg)));
1212  } else {
1213    sf.Assign(mirror::EmulatedStackFrame::CreateFromShadowFrameAndArgs<is_range>(
1214        self,
1215        callsite_type,
1216        callee_type,
1217        shadow_frame,
1218        first_src_reg,
1219        arg));
1220
1221    // Something went wrong while creating the emulated stack frame, we should
1222    // throw the pending exception.
1223    if (sf.Get() == nullptr) {
1224      DCHECK(self->IsExceptionPending());
1225      return false;
1226    }
1227  }
1228
1229  new_shadow_frame->SetVRegReference(0, receiver.Get());
1230  new_shadow_frame->SetVRegReference(1, sf.Get());
1231
1232  PerformCall(self,
1233              code_item,
1234              shadow_frame.GetMethod(),
1235              0 /* first dest reg */,
1236              new_shadow_frame,
1237              result);
1238
1239  // If the called transformer method we called has returned a value, then we
1240  // need to copy it back to |result|.
1241  if (!self->IsExceptionPending()) {
1242    sf->GetReturnValue(self, result);
1243  }
1244
1245  return !self->IsExceptionPending();
1246}
1247
1248template <bool is_range,
1249          bool do_assignability_check>
1250static inline bool DoCallCommon(ArtMethod* called_method,
1251                                Thread* self,
1252                                ShadowFrame& shadow_frame,
1253                                JValue* result,
1254                                uint16_t number_of_inputs,
1255                                uint32_t (&arg)[Instruction::kMaxVarArgRegs],
1256                                uint32_t vregC) {
1257  bool string_init = false;
1258  // Replace calls to String.<init> with equivalent StringFactory call.
1259  if (UNLIKELY(called_method->GetDeclaringClass()->IsStringClass()
1260               && called_method->IsConstructor())) {
1261    called_method = WellKnownClasses::StringInitToStringFactory(called_method);
1262    string_init = true;
1263  }
1264
1265  // Compute method information.
1266  const DexFile::CodeItem* code_item = called_method->GetCodeItem();
1267
1268  // Number of registers for the callee's call frame.
1269  uint16_t num_regs;
1270  if (LIKELY(code_item != nullptr)) {
1271    num_regs = code_item->registers_size_;
1272    DCHECK_EQ(string_init ? number_of_inputs - 1 : number_of_inputs, code_item->ins_size_);
1273  } else {
1274    DCHECK(called_method->IsNative() || called_method->IsProxyMethod());
1275    num_regs = number_of_inputs;
1276  }
1277
1278  // Hack for String init:
1279  //
1280  // Rewrite invoke-x java.lang.String.<init>(this, a, b, c, ...) into:
1281  //         invoke-x StringFactory(a, b, c, ...)
1282  // by effectively dropping the first virtual register from the invoke.
1283  //
1284  // (at this point the ArtMethod has already been replaced,
1285  // so we just need to fix-up the arguments)
1286  //
1287  // Note that FindMethodFromCode in entrypoint_utils-inl.h was also special-cased
1288  // to handle the compiler optimization of replacing `this` with null without
1289  // throwing NullPointerException.
1290  uint32_t string_init_vreg_this = is_range ? vregC : arg[0];
1291  if (UNLIKELY(string_init)) {
1292    DCHECK_GT(num_regs, 0u);  // As the method is an instance method, there should be at least 1.
1293
1294    // The new StringFactory call is static and has one fewer argument.
1295    if (code_item == nullptr) {
1296      DCHECK(called_method->IsNative() || called_method->IsProxyMethod());
1297      num_regs--;
1298    }  // else ... don't need to change num_regs since it comes up from the string_init's code item
1299    number_of_inputs--;
1300
1301    // Rewrite the var-args, dropping the 0th argument ("this")
1302    for (uint32_t i = 1; i < arraysize(arg); ++i) {
1303      arg[i - 1] = arg[i];
1304    }
1305    arg[arraysize(arg) - 1] = 0;
1306
1307    // Rewrite the non-var-arg case
1308    vregC++;  // Skips the 0th vreg in the range ("this").
1309  }
1310
1311  // Parameter registers go at the end of the shadow frame.
1312  DCHECK_GE(num_regs, number_of_inputs);
1313  size_t first_dest_reg = num_regs - number_of_inputs;
1314  DCHECK_NE(first_dest_reg, (size_t)-1);
1315
1316  // Allocate shadow frame on the stack.
1317  const char* old_cause = self->StartAssertNoThreadSuspension("DoCallCommon");
1318  ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
1319      CREATE_SHADOW_FRAME(num_regs, &shadow_frame, called_method, /* dex pc */ 0);
1320  ShadowFrame* new_shadow_frame = shadow_frame_unique_ptr.get();
1321
1322  // Initialize new shadow frame by copying the registers from the callee shadow frame.
1323  if (do_assignability_check) {
1324    // Slow path.
1325    // We might need to do class loading, which incurs a thread state change to kNative. So
1326    // register the shadow frame as under construction and allow suspension again.
1327    ScopedStackedShadowFramePusher pusher(
1328        self, new_shadow_frame, StackedShadowFrameType::kShadowFrameUnderConstruction);
1329    self->EndAssertNoThreadSuspension(old_cause);
1330
1331    // ArtMethod here is needed to check type information of the call site against the callee.
1332    // Type information is retrieved from a DexFile/DexCache for that respective declared method.
1333    //
1334    // As a special case for proxy methods, which are not dex-backed,
1335    // we have to retrieve type information from the proxy's method
1336    // interface method instead (which is dex backed since proxies are never interfaces).
1337    ArtMethod* method =
1338        new_shadow_frame->GetMethod()->GetInterfaceMethodIfProxy(kRuntimePointerSize);
1339
1340    // We need to do runtime check on reference assignment. We need to load the shorty
1341    // to get the exact type of each reference argument.
1342    const DexFile::TypeList* params = method->GetParameterTypeList();
1343    uint32_t shorty_len = 0;
1344    const char* shorty = method->GetShorty(&shorty_len);
1345
1346    // Handle receiver apart since it's not part of the shorty.
1347    size_t dest_reg = first_dest_reg;
1348    size_t arg_offset = 0;
1349
1350    if (!method->IsStatic()) {
1351      size_t receiver_reg = is_range ? vregC : arg[0];
1352      new_shadow_frame->SetVRegReference(dest_reg, shadow_frame.GetVRegReference(receiver_reg));
1353      ++dest_reg;
1354      ++arg_offset;
1355      DCHECK(!string_init);  // All StringFactory methods are static.
1356    }
1357
1358    // Copy the caller's invoke-* arguments into the callee's parameter registers.
1359    for (uint32_t shorty_pos = 0; dest_reg < num_regs; ++shorty_pos, ++dest_reg, ++arg_offset) {
1360      // Skip the 0th 'shorty' type since it represents the return type.
1361      DCHECK_LT(shorty_pos + 1, shorty_len) << "for shorty '" << shorty << "'";
1362      const size_t src_reg = (is_range) ? vregC + arg_offset : arg[arg_offset];
1363      switch (shorty[shorty_pos + 1]) {
1364        // Handle Object references. 1 virtual register slot.
1365        case 'L': {
1366          ObjPtr<mirror::Object> o = shadow_frame.GetVRegReference(src_reg);
1367          if (do_assignability_check && o != nullptr) {
1368            PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
1369            const uint32_t type_idx = params->GetTypeItem(shorty_pos).type_idx_;
1370            ObjPtr<mirror::Class> arg_type = method->GetDexCacheResolvedType(type_idx,
1371                                                                             pointer_size);
1372            if (arg_type == nullptr) {
1373              StackHandleScope<1> hs(self);
1374              // Preserve o since it is used below and GetClassFromTypeIndex may cause thread
1375              // suspension.
1376              HandleWrapperObjPtr<mirror::Object> h = hs.NewHandleWrapper(&o);
1377              arg_type = method->GetClassFromTypeIndex(type_idx, true /* resolve */, pointer_size);
1378              if (arg_type == nullptr) {
1379                CHECK(self->IsExceptionPending());
1380                return false;
1381              }
1382            }
1383            if (!o->VerifierInstanceOf(arg_type)) {
1384              // This should never happen.
1385              std::string temp1, temp2;
1386              self->ThrowNewExceptionF("Ljava/lang/VirtualMachineError;",
1387                                       "Invoking %s with bad arg %d, type '%s' not instance of '%s'",
1388                                       new_shadow_frame->GetMethod()->GetName(), shorty_pos,
1389                                       o->GetClass()->GetDescriptor(&temp1),
1390                                       arg_type->GetDescriptor(&temp2));
1391              return false;
1392            }
1393          }
1394          new_shadow_frame->SetVRegReference(dest_reg, o.Ptr());
1395          break;
1396        }
1397        // Handle doubles and longs. 2 consecutive virtual register slots.
1398        case 'J': case 'D': {
1399          uint64_t wide_value =
1400              (static_cast<uint64_t>(shadow_frame.GetVReg(src_reg + 1)) << BitSizeOf<uint32_t>()) |
1401               static_cast<uint32_t>(shadow_frame.GetVReg(src_reg));
1402          new_shadow_frame->SetVRegLong(dest_reg, wide_value);
1403          // Skip the next virtual register slot since we already used it.
1404          ++dest_reg;
1405          ++arg_offset;
1406          break;
1407        }
1408        // Handle all other primitives that are always 1 virtual register slot.
1409        default:
1410          new_shadow_frame->SetVReg(dest_reg, shadow_frame.GetVReg(src_reg));
1411          break;
1412      }
1413    }
1414  } else {
1415    if (is_range) {
1416      DCHECK_EQ(num_regs, first_dest_reg + number_of_inputs);
1417    }
1418
1419    CopyRegisters<is_range>(shadow_frame,
1420                            new_shadow_frame,
1421                            arg,
1422                            vregC,
1423                            first_dest_reg,
1424                            number_of_inputs);
1425    self->EndAssertNoThreadSuspension(old_cause);
1426  }
1427
1428  PerformCall(self, code_item, shadow_frame.GetMethod(), first_dest_reg, new_shadow_frame, result);
1429
1430  if (string_init && !self->IsExceptionPending()) {
1431    SetStringInitValueToAllAliases(&shadow_frame, string_init_vreg_this, *result);
1432  }
1433
1434  return !self->IsExceptionPending();
1435}
1436
1437template<bool is_range, bool do_assignability_check>
1438bool DoCall(ArtMethod* called_method, Thread* self, ShadowFrame& shadow_frame,
1439            const Instruction* inst, uint16_t inst_data, JValue* result) {
1440  // Argument word count.
1441  const uint16_t number_of_inputs =
1442      (is_range) ? inst->VRegA_3rc(inst_data) : inst->VRegA_35c(inst_data);
1443
1444  // TODO: find a cleaner way to separate non-range and range information without duplicating
1445  //       code.
1446  uint32_t arg[Instruction::kMaxVarArgRegs] = {};  // only used in invoke-XXX.
1447  uint32_t vregC = 0;
1448  if (is_range) {
1449    vregC = inst->VRegC_3rc();
1450  } else {
1451    vregC = inst->VRegC_35c();
1452    inst->GetVarArgs(arg, inst_data);
1453  }
1454
1455  return DoCallCommon<is_range, do_assignability_check>(
1456      called_method, self, shadow_frame,
1457      result, number_of_inputs, arg, vregC);
1458}
1459
1460template <bool is_range, bool do_access_check, bool transaction_active>
1461bool DoFilledNewArray(const Instruction* inst,
1462                      const ShadowFrame& shadow_frame,
1463                      Thread* self,
1464                      JValue* result) {
1465  DCHECK(inst->Opcode() == Instruction::FILLED_NEW_ARRAY ||
1466         inst->Opcode() == Instruction::FILLED_NEW_ARRAY_RANGE);
1467  const int32_t length = is_range ? inst->VRegA_3rc() : inst->VRegA_35c();
1468  if (!is_range) {
1469    // Checks FILLED_NEW_ARRAY's length does not exceed 5 arguments.
1470    CHECK_LE(length, 5);
1471  }
1472  if (UNLIKELY(length < 0)) {
1473    ThrowNegativeArraySizeException(length);
1474    return false;
1475  }
1476  uint16_t type_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
1477  ObjPtr<mirror::Class> array_class = ResolveVerifyAndClinit(type_idx,
1478                                                             shadow_frame.GetMethod(),
1479                                                             self,
1480                                                             false,
1481                                                             do_access_check);
1482  if (UNLIKELY(array_class == nullptr)) {
1483    DCHECK(self->IsExceptionPending());
1484    return false;
1485  }
1486  CHECK(array_class->IsArrayClass());
1487  ObjPtr<mirror::Class> component_class = array_class->GetComponentType();
1488  const bool is_primitive_int_component = component_class->IsPrimitiveInt();
1489  if (UNLIKELY(component_class->IsPrimitive() && !is_primitive_int_component)) {
1490    if (component_class->IsPrimitiveLong() || component_class->IsPrimitiveDouble()) {
1491      ThrowRuntimeException("Bad filled array request for type %s",
1492                            component_class->PrettyDescriptor().c_str());
1493    } else {
1494      self->ThrowNewExceptionF("Ljava/lang/InternalError;",
1495                               "Found type %s; filled-new-array not implemented for anything but 'int'",
1496                               component_class->PrettyDescriptor().c_str());
1497    }
1498    return false;
1499  }
1500  ObjPtr<mirror::Object> new_array = mirror::Array::Alloc<true>(
1501      self,
1502      array_class,
1503      length,
1504      array_class->GetComponentSizeShift(),
1505      Runtime::Current()->GetHeap()->GetCurrentAllocator());
1506  if (UNLIKELY(new_array == nullptr)) {
1507    self->AssertPendingOOMException();
1508    return false;
1509  }
1510  uint32_t arg[Instruction::kMaxVarArgRegs];  // only used in filled-new-array.
1511  uint32_t vregC = 0;   // only used in filled-new-array-range.
1512  if (is_range) {
1513    vregC = inst->VRegC_3rc();
1514  } else {
1515    inst->GetVarArgs(arg);
1516  }
1517  for (int32_t i = 0; i < length; ++i) {
1518    size_t src_reg = is_range ? vregC + i : arg[i];
1519    if (is_primitive_int_component) {
1520      new_array->AsIntArray()->SetWithoutChecks<transaction_active>(
1521          i, shadow_frame.GetVReg(src_reg));
1522    } else {
1523      new_array->AsObjectArray<mirror::Object>()->SetWithoutChecks<transaction_active>(
1524          i, shadow_frame.GetVRegReference(src_reg));
1525    }
1526  }
1527
1528  result->SetL(new_array);
1529  return true;
1530}
1531
1532// TODO: Use ObjPtr here.
1533template<typename T>
1534static void RecordArrayElementsInTransactionImpl(mirror::PrimitiveArray<T>* array,
1535                                                 int32_t count)
1536    REQUIRES_SHARED(Locks::mutator_lock_) {
1537  Runtime* runtime = Runtime::Current();
1538  for (int32_t i = 0; i < count; ++i) {
1539    runtime->RecordWriteArray(array, i, array->GetWithoutChecks(i));
1540  }
1541}
1542
1543void RecordArrayElementsInTransaction(ObjPtr<mirror::Array> array, int32_t count)
1544    REQUIRES_SHARED(Locks::mutator_lock_) {
1545  DCHECK(Runtime::Current()->IsActiveTransaction());
1546  DCHECK(array != nullptr);
1547  DCHECK_LE(count, array->GetLength());
1548  Primitive::Type primitive_component_type = array->GetClass()->GetComponentType()->GetPrimitiveType();
1549  switch (primitive_component_type) {
1550    case Primitive::kPrimBoolean:
1551      RecordArrayElementsInTransactionImpl(array->AsBooleanArray(), count);
1552      break;
1553    case Primitive::kPrimByte:
1554      RecordArrayElementsInTransactionImpl(array->AsByteArray(), count);
1555      break;
1556    case Primitive::kPrimChar:
1557      RecordArrayElementsInTransactionImpl(array->AsCharArray(), count);
1558      break;
1559    case Primitive::kPrimShort:
1560      RecordArrayElementsInTransactionImpl(array->AsShortArray(), count);
1561      break;
1562    case Primitive::kPrimInt:
1563      RecordArrayElementsInTransactionImpl(array->AsIntArray(), count);
1564      break;
1565    case Primitive::kPrimFloat:
1566      RecordArrayElementsInTransactionImpl(array->AsFloatArray(), count);
1567      break;
1568    case Primitive::kPrimLong:
1569      RecordArrayElementsInTransactionImpl(array->AsLongArray(), count);
1570      break;
1571    case Primitive::kPrimDouble:
1572      RecordArrayElementsInTransactionImpl(array->AsDoubleArray(), count);
1573      break;
1574    default:
1575      LOG(FATAL) << "Unsupported primitive type " << primitive_component_type
1576                 << " in fill-array-data";
1577      break;
1578  }
1579}
1580
1581// Explicit DoCall template function declarations.
1582#define EXPLICIT_DO_CALL_TEMPLATE_DECL(_is_range, _do_assignability_check)                      \
1583  template REQUIRES_SHARED(Locks::mutator_lock_)                                                \
1584  bool DoCall<_is_range, _do_assignability_check>(ArtMethod* method, Thread* self,              \
1585                                                  ShadowFrame& shadow_frame,                    \
1586                                                  const Instruction* inst, uint16_t inst_data,  \
1587                                                  JValue* result)
1588EXPLICIT_DO_CALL_TEMPLATE_DECL(false, false);
1589EXPLICIT_DO_CALL_TEMPLATE_DECL(false, true);
1590EXPLICIT_DO_CALL_TEMPLATE_DECL(true, false);
1591EXPLICIT_DO_CALL_TEMPLATE_DECL(true, true);
1592#undef EXPLICIT_DO_CALL_TEMPLATE_DECL
1593
1594// Explicit DoInvokePolymorphic template function declarations.
1595#define EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(_is_range, _do_assignability_check)  \
1596  template REQUIRES_SHARED(Locks::mutator_lock_)                                          \
1597  bool DoInvokePolymorphic<_is_range, _do_assignability_check>(                           \
1598      Thread* self, ShadowFrame& shadow_frame, const Instruction* inst,                   \
1599      uint16_t inst_data, JValue* result)
1600
1601EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(false, false);
1602EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(false, true);
1603EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(true, false);
1604EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(true, true);
1605#undef EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL
1606
1607// Explicit DoFilledNewArray template function declarations.
1608#define EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(_is_range_, _check, _transaction_active)       \
1609  template REQUIRES_SHARED(Locks::mutator_lock_)                                                  \
1610  bool DoFilledNewArray<_is_range_, _check, _transaction_active>(const Instruction* inst,         \
1611                                                                 const ShadowFrame& shadow_frame, \
1612                                                                 Thread* self, JValue* result)
1613#define EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(_transaction_active)       \
1614  EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, false, _transaction_active);  \
1615  EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, true, _transaction_active);   \
1616  EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, false, _transaction_active);   \
1617  EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, true, _transaction_active)
1618EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(false);
1619EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(true);
1620#undef EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL
1621#undef EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL
1622
1623}  // namespace interpreter
1624}  // namespace art
1625