interpreter_common.cc revision 848574ca50bb7e2d109608359d1086b3ca6bb4b3
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 "dex_file_types.h"
24#include "entrypoints/runtime_asm_entrypoints.h"
25#include "jit/jit.h"
26#include "jvalue.h"
27#include "method_handles-inl.h"
28#include "method_handles.h"
29#include "mirror/array-inl.h"
30#include "mirror/class.h"
31#include "mirror/emulated_stack_frame.h"
32#include "mirror/method_handle_impl-inl.h"
33#include "reflection-inl.h"
34#include "reflection.h"
35#include "stack.h"
36#include "thread-inl.h"
37#include "transaction.h"
38#include "well_known_classes.h"
39
40namespace art {
41namespace interpreter {
42
43void ThrowNullPointerExceptionFromInterpreter() {
44  ThrowNullPointerExceptionFromDexPC();
45}
46
47template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check,
48         bool transaction_active>
49bool DoFieldGet(Thread* self, ShadowFrame& shadow_frame, const Instruction* inst,
50                uint16_t inst_data) {
51  const bool is_static = (find_type == StaticObjectRead) || (find_type == StaticPrimitiveRead);
52  const uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
53  ArtField* f =
54      FindFieldFromCode<find_type, do_access_check>(field_idx, shadow_frame.GetMethod(), self,
55                                                    Primitive::ComponentSize(field_type));
56  if (UNLIKELY(f == nullptr)) {
57    CHECK(self->IsExceptionPending());
58    return false;
59  }
60  ObjPtr<mirror::Object> obj;
61  if (is_static) {
62    obj = f->GetDeclaringClass();
63    if (transaction_active) {
64      if (Runtime::Current()->GetTransaction()->ReadConstraint(obj.Ptr(), f)) {
65        Runtime::Current()->AbortTransactionAndThrowAbortError(self, "Can't read static fields of "
66            + obj->PrettyTypeOf() + " since it does not belong to clinit's class.");
67        return false;
68      }
69    }
70  } else {
71    obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
72    if (UNLIKELY(obj == nullptr)) {
73      ThrowNullPointerExceptionForFieldAccess(f, true);
74      return false;
75    }
76  }
77
78  JValue result;
79  if (UNLIKELY(!DoFieldGetCommon<field_type>(self, shadow_frame, obj, f, &result))) {
80    // Instrumentation threw an error!
81    CHECK(self->IsExceptionPending());
82    return false;
83  }
84  uint32_t vregA = is_static ? inst->VRegA_21c(inst_data) : inst->VRegA_22c(inst_data);
85  switch (field_type) {
86    case Primitive::kPrimBoolean:
87      shadow_frame.SetVReg(vregA, result.GetZ());
88      break;
89    case Primitive::kPrimByte:
90      shadow_frame.SetVReg(vregA, result.GetB());
91      break;
92    case Primitive::kPrimChar:
93      shadow_frame.SetVReg(vregA, result.GetC());
94      break;
95    case Primitive::kPrimShort:
96      shadow_frame.SetVReg(vregA, result.GetS());
97      break;
98    case Primitive::kPrimInt:
99      shadow_frame.SetVReg(vregA, result.GetI());
100      break;
101    case Primitive::kPrimLong:
102      shadow_frame.SetVRegLong(vregA, result.GetJ());
103      break;
104    case Primitive::kPrimNot:
105      shadow_frame.SetVRegReference(vregA, result.GetL());
106      break;
107    default:
108      LOG(FATAL) << "Unreachable: " << field_type;
109      UNREACHABLE();
110  }
111  return true;
112}
113
114// Explicitly instantiate all DoFieldGet functions.
115#define EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, _do_check, _transaction_active) \
116  template bool DoFieldGet<_find_type, _field_type, _do_check, _transaction_active>(Thread* self, \
117                                                               ShadowFrame& shadow_frame, \
118                                                               const Instruction* inst, \
119                                                               uint16_t inst_data)
120
121#define EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(_find_type, _field_type)  \
122    EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, false, true);  \
123    EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, false, false);  \
124    EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, true, true);  \
125    EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, true, false);
126
127// iget-XXX
128EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimBoolean)
129EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimByte)
130EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimChar)
131EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimShort)
132EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimInt)
133EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimLong)
134EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstanceObjectRead, Primitive::kPrimNot)
135
136// sget-XXX
137EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimBoolean)
138EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimByte)
139EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimChar)
140EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimShort)
141EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimInt)
142EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimLong)
143EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticObjectRead, Primitive::kPrimNot)
144
145#undef EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL
146#undef EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL
147
148// Handles iget-quick, iget-wide-quick and iget-object-quick instructions.
149// Returns true on success, otherwise throws an exception and returns false.
150template<Primitive::Type field_type>
151bool DoIGetQuick(ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) {
152  ObjPtr<mirror::Object> obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
153  if (UNLIKELY(obj == nullptr)) {
154    // We lost the reference to the field index so we cannot get a more
155    // precised exception message.
156    ThrowNullPointerExceptionFromDexPC();
157    return false;
158  }
159  MemberOffset field_offset(inst->VRegC_22c());
160  // Report this field access to instrumentation if needed. Since we only have the offset of
161  // the field from the base of the object, we need to look for it first.
162  instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
163  if (UNLIKELY(instrumentation->HasFieldReadListeners())) {
164    ArtField* f = ArtField::FindInstanceFieldWithOffset(obj->GetClass(),
165                                                        field_offset.Uint32Value());
166    DCHECK(f != nullptr);
167    DCHECK(!f->IsStatic());
168    Thread* self = Thread::Current();
169    StackHandleScope<1> hs(self);
170    // Save obj in case the instrumentation event has thread suspension.
171    HandleWrapperObjPtr<mirror::Object> h = hs.NewHandleWrapper(&obj);
172    instrumentation->FieldReadEvent(self,
173                                    obj.Ptr(),
174                                    shadow_frame.GetMethod(),
175                                    shadow_frame.GetDexPC(),
176                                    f);
177    if (UNLIKELY(self->IsExceptionPending())) {
178      return false;
179    }
180  }
181  // Note: iget-x-quick instructions are only for non-volatile fields.
182  const uint32_t vregA = inst->VRegA_22c(inst_data);
183  switch (field_type) {
184    case Primitive::kPrimInt:
185      shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetField32(field_offset)));
186      break;
187    case Primitive::kPrimBoolean:
188      shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldBoolean(field_offset)));
189      break;
190    case Primitive::kPrimByte:
191      shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldByte(field_offset)));
192      break;
193    case Primitive::kPrimChar:
194      shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldChar(field_offset)));
195      break;
196    case Primitive::kPrimShort:
197      shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldShort(field_offset)));
198      break;
199    case Primitive::kPrimLong:
200      shadow_frame.SetVRegLong(vregA, static_cast<int64_t>(obj->GetField64(field_offset)));
201      break;
202    case Primitive::kPrimNot:
203      shadow_frame.SetVRegReference(vregA, obj->GetFieldObject<mirror::Object>(field_offset));
204      break;
205    default:
206      LOG(FATAL) << "Unreachable: " << field_type;
207      UNREACHABLE();
208  }
209  return true;
210}
211
212// Explicitly instantiate all DoIGetQuick functions.
213#define EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(_field_type) \
214  template bool DoIGetQuick<_field_type>(ShadowFrame& shadow_frame, const Instruction* inst, \
215                                         uint16_t inst_data)
216
217EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimInt);      // iget-quick.
218EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimBoolean);  // iget-boolean-quick.
219EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimByte);     // iget-byte-quick.
220EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimChar);     // iget-char-quick.
221EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimShort);    // iget-short-quick.
222EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimLong);     // iget-wide-quick.
223EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimNot);      // iget-object-quick.
224#undef EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL
225
226template<Primitive::Type field_type>
227static JValue GetFieldValue(const ShadowFrame& shadow_frame, uint32_t vreg)
228    REQUIRES_SHARED(Locks::mutator_lock_) {
229  JValue field_value;
230  switch (field_type) {
231    case Primitive::kPrimBoolean:
232      field_value.SetZ(static_cast<uint8_t>(shadow_frame.GetVReg(vreg)));
233      break;
234    case Primitive::kPrimByte:
235      field_value.SetB(static_cast<int8_t>(shadow_frame.GetVReg(vreg)));
236      break;
237    case Primitive::kPrimChar:
238      field_value.SetC(static_cast<uint16_t>(shadow_frame.GetVReg(vreg)));
239      break;
240    case Primitive::kPrimShort:
241      field_value.SetS(static_cast<int16_t>(shadow_frame.GetVReg(vreg)));
242      break;
243    case Primitive::kPrimInt:
244      field_value.SetI(shadow_frame.GetVReg(vreg));
245      break;
246    case Primitive::kPrimLong:
247      field_value.SetJ(shadow_frame.GetVRegLong(vreg));
248      break;
249    case Primitive::kPrimNot:
250      field_value.SetL(shadow_frame.GetVRegReference(vreg));
251      break;
252    default:
253      LOG(FATAL) << "Unreachable: " << field_type;
254      UNREACHABLE();
255  }
256  return field_value;
257}
258
259template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check,
260         bool transaction_active>
261bool DoFieldPut(Thread* self, const ShadowFrame& shadow_frame, const Instruction* inst,
262                uint16_t inst_data) {
263  const bool do_assignability_check = do_access_check;
264  bool is_static = (find_type == StaticObjectWrite) || (find_type == StaticPrimitiveWrite);
265  uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
266  ArtField* f =
267      FindFieldFromCode<find_type, do_access_check>(field_idx, shadow_frame.GetMethod(), self,
268                                                    Primitive::ComponentSize(field_type));
269  if (UNLIKELY(f == nullptr)) {
270    CHECK(self->IsExceptionPending());
271    return false;
272  }
273  ObjPtr<mirror::Object> obj;
274  if (is_static) {
275    obj = f->GetDeclaringClass();
276    if (transaction_active) {
277      if (Runtime::Current()->GetTransaction()->WriteConstraint(obj.Ptr(), f)) {
278        Runtime::Current()->AbortTransactionAndThrowAbortError(
279            self, "Can't set fields of " + obj->PrettyTypeOf());
280        return false;
281      }
282    }
283
284  } else {
285    obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
286    if (UNLIKELY(obj == nullptr)) {
287      ThrowNullPointerExceptionForFieldAccess(f, false);
288      return false;
289    }
290  }
291
292  uint32_t vregA = is_static ? inst->VRegA_21c(inst_data) : inst->VRegA_22c(inst_data);
293  JValue value = GetFieldValue<field_type>(shadow_frame, vregA);
294  return DoFieldPutCommon<field_type, do_assignability_check, transaction_active>(self,
295                                                                                  shadow_frame,
296                                                                                  obj,
297                                                                                  f,
298                                                                                  value);
299}
300
301// Explicitly instantiate all DoFieldPut functions.
302#define EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, _do_check, _transaction_active) \
303  template bool DoFieldPut<_find_type, _field_type, _do_check, _transaction_active>(Thread* self, \
304      const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data)
305
306#define EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(_find_type, _field_type)  \
307    EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, false, false);  \
308    EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, true, false);  \
309    EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, false, true);  \
310    EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, true, true);
311
312// iput-XXX
313EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimBoolean)
314EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimByte)
315EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimChar)
316EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimShort)
317EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimInt)
318EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimLong)
319EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstanceObjectWrite, Primitive::kPrimNot)
320
321// sput-XXX
322EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimBoolean)
323EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimByte)
324EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimChar)
325EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimShort)
326EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimInt)
327EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimLong)
328EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticObjectWrite, Primitive::kPrimNot)
329
330#undef EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL
331#undef EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL
332
333template<Primitive::Type field_type, bool transaction_active>
334bool DoIPutQuick(const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) {
335  ObjPtr<mirror::Object> obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
336  if (UNLIKELY(obj == nullptr)) {
337    // We lost the reference to the field index so we cannot get a more
338    // precised exception message.
339    ThrowNullPointerExceptionFromDexPC();
340    return false;
341  }
342  MemberOffset field_offset(inst->VRegC_22c());
343  const uint32_t vregA = inst->VRegA_22c(inst_data);
344  // Report this field modification to instrumentation if needed. Since we only have the offset of
345  // the field from the base of the object, we need to look for it first.
346  instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
347  if (UNLIKELY(instrumentation->HasFieldWriteListeners())) {
348    ArtField* f = ArtField::FindInstanceFieldWithOffset(obj->GetClass(),
349                                                        field_offset.Uint32Value());
350    DCHECK(f != nullptr);
351    DCHECK(!f->IsStatic());
352    JValue field_value = GetFieldValue<field_type>(shadow_frame, vregA);
353    Thread* self = Thread::Current();
354    StackHandleScope<2> hs(self);
355    // Save obj in case the instrumentation event has thread suspension.
356    HandleWrapperObjPtr<mirror::Object> h = hs.NewHandleWrapper(&obj);
357    mirror::Object* fake_root = nullptr;
358    HandleWrapper<mirror::Object> ret(hs.NewHandleWrapper<mirror::Object>(
359        field_type == Primitive::kPrimNot ? field_value.GetGCRoot() : &fake_root));
360    instrumentation->FieldWriteEvent(self,
361                                     obj.Ptr(),
362                                     shadow_frame.GetMethod(),
363                                     shadow_frame.GetDexPC(),
364                                     f,
365                                     field_value);
366    if (UNLIKELY(self->IsExceptionPending())) {
367      return false;
368    }
369  }
370  // Note: iput-x-quick instructions are only for non-volatile fields.
371  switch (field_type) {
372    case Primitive::kPrimBoolean:
373      obj->SetFieldBoolean<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
374      break;
375    case Primitive::kPrimByte:
376      obj->SetFieldByte<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
377      break;
378    case Primitive::kPrimChar:
379      obj->SetFieldChar<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
380      break;
381    case Primitive::kPrimShort:
382      obj->SetFieldShort<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
383      break;
384    case Primitive::kPrimInt:
385      obj->SetField32<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
386      break;
387    case Primitive::kPrimLong:
388      obj->SetField64<transaction_active>(field_offset, shadow_frame.GetVRegLong(vregA));
389      break;
390    case Primitive::kPrimNot:
391      obj->SetFieldObject<transaction_active>(field_offset, shadow_frame.GetVRegReference(vregA));
392      break;
393    default:
394      LOG(FATAL) << "Unreachable: " << field_type;
395      UNREACHABLE();
396  }
397  return true;
398}
399
400// Explicitly instantiate all DoIPutQuick functions.
401#define EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, _transaction_active) \
402  template bool DoIPutQuick<_field_type, _transaction_active>(const ShadowFrame& shadow_frame, \
403                                                              const Instruction* inst, \
404                                                              uint16_t inst_data)
405
406#define EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(_field_type)   \
407  EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, false);     \
408  EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, true);
409
410EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimInt)      // iput-quick.
411EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimBoolean)  // iput-boolean-quick.
412EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimByte)     // iput-byte-quick.
413EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimChar)     // iput-char-quick.
414EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimShort)    // iput-short-quick.
415EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimLong)     // iput-wide-quick.
416EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimNot)      // iput-object-quick.
417#undef EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL
418#undef EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL
419
420// We execute any instrumentation events that are triggered by this exception and change the
421// shadow_frame's dex_pc to that of the exception handler if there is one in the current method.
422// Return true if we should continue executing in the current method and false if we need to go up
423// the stack to find an exception handler.
424// We accept a null Instrumentation* meaning we must not report anything to the instrumentation.
425// TODO We should have a better way to skip instrumentation reporting or possibly rethink that
426// behavior.
427bool MoveToExceptionHandler(Thread* self,
428                            ShadowFrame& shadow_frame,
429                            const instrumentation::Instrumentation* instrumentation) {
430  self->VerifyStack();
431  StackHandleScope<2> hs(self);
432  Handle<mirror::Throwable> exception(hs.NewHandle(self->GetException()));
433  if (instrumentation != nullptr &&
434      instrumentation->HasExceptionThrownListeners() &&
435      self->IsExceptionThrownByCurrentMethod(exception.Get())) {
436    // See b/65049545 for why we don't need to check to see if the exception has changed.
437    instrumentation->ExceptionThrownEvent(self, exception.Get());
438  }
439  bool clear_exception = false;
440  uint32_t found_dex_pc = shadow_frame.GetMethod()->FindCatchBlock(
441      hs.NewHandle(exception->GetClass()), shadow_frame.GetDexPC(), &clear_exception);
442  if (found_dex_pc == dex::kDexNoIndex) {
443    if (instrumentation != nullptr) {
444      if (shadow_frame.NeedsNotifyPop()) {
445        instrumentation->WatchedFramePopped(self, shadow_frame);
446      }
447      // Exception is not caught by the current method. We will unwind to the
448      // caller. Notify any instrumentation listener.
449      instrumentation->MethodUnwindEvent(self,
450                                         shadow_frame.GetThisObject(),
451                                         shadow_frame.GetMethod(),
452                                         shadow_frame.GetDexPC());
453    }
454    return false;
455  } else {
456    shadow_frame.SetDexPC(found_dex_pc);
457    if (instrumentation != nullptr && instrumentation->HasExceptionHandledListeners()) {
458      self->ClearException();
459      instrumentation->ExceptionHandledEvent(self, exception.Get());
460      if (UNLIKELY(self->IsExceptionPending())) {
461        // Exception handled event threw an exception. Try to find the handler for this one.
462        return MoveToExceptionHandler(self, shadow_frame, instrumentation);
463      } else if (!clear_exception) {
464        self->SetException(exception.Get());
465      }
466    } else if (clear_exception) {
467      self->ClearException();
468    }
469    return true;
470  }
471}
472
473void UnexpectedOpcode(const Instruction* inst, const ShadowFrame& shadow_frame) {
474  LOG(FATAL) << "Unexpected instruction: "
475             << inst->DumpString(shadow_frame.GetMethod()->GetDexFile());
476  UNREACHABLE();
477}
478
479void AbortTransactionF(Thread* self, const char* fmt, ...) {
480  va_list args;
481  va_start(args, fmt);
482  AbortTransactionV(self, fmt, args);
483  va_end(args);
484}
485
486void AbortTransactionV(Thread* self, const char* fmt, va_list args) {
487  CHECK(Runtime::Current()->IsActiveTransaction());
488  // Constructs abort message.
489  std::string abort_msg;
490  android::base::StringAppendV(&abort_msg, fmt, args);
491  // Throws an exception so we can abort the transaction and rollback every change.
492  Runtime::Current()->AbortTransactionAndThrowAbortError(self, abort_msg);
493}
494
495// START DECLARATIONS :
496//
497// These additional declarations are required because clang complains
498// about ALWAYS_INLINE (-Werror, -Wgcc-compat) in definitions.
499//
500
501template <bool is_range, bool do_assignability_check>
502static ALWAYS_INLINE bool DoCallCommon(ArtMethod* called_method,
503                                       Thread* self,
504                                       ShadowFrame& shadow_frame,
505                                       JValue* result,
506                                       uint16_t number_of_inputs,
507                                       uint32_t (&arg)[Instruction::kMaxVarArgRegs],
508                                       uint32_t vregC) REQUIRES_SHARED(Locks::mutator_lock_);
509
510template <bool is_range>
511ALWAYS_INLINE void CopyRegisters(ShadowFrame& caller_frame,
512                                 ShadowFrame* callee_frame,
513                                 const uint32_t (&arg)[Instruction::kMaxVarArgRegs],
514                                 const size_t first_src_reg,
515                                 const size_t first_dest_reg,
516                                 const size_t num_regs) REQUIRES_SHARED(Locks::mutator_lock_);
517
518// END DECLARATIONS.
519
520void ArtInterpreterToCompiledCodeBridge(Thread* self,
521                                        ArtMethod* caller,
522                                        ShadowFrame* shadow_frame,
523                                        uint16_t arg_offset,
524                                        JValue* result)
525    REQUIRES_SHARED(Locks::mutator_lock_) {
526  ArtMethod* method = shadow_frame->GetMethod();
527  // Ensure static methods are initialized.
528  if (method->IsStatic()) {
529    ObjPtr<mirror::Class> declaringClass = method->GetDeclaringClass();
530    if (UNLIKELY(!declaringClass->IsInitialized())) {
531      self->PushShadowFrame(shadow_frame);
532      StackHandleScope<1> hs(self);
533      Handle<mirror::Class> h_class(hs.NewHandle(declaringClass));
534      if (UNLIKELY(!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true,
535                                                                            true))) {
536        self->PopShadowFrame();
537        DCHECK(self->IsExceptionPending());
538        return;
539      }
540      self->PopShadowFrame();
541      CHECK(h_class->IsInitializing());
542      // Reload from shadow frame in case the method moved, this is faster than adding a handle.
543      method = shadow_frame->GetMethod();
544    }
545  }
546  // Basic checks for the arg_offset. If there's no code item, the arg_offset must be 0. Otherwise,
547  // check that the arg_offset isn't greater than the number of registers. A stronger check is
548  // difficult since the frame may contain space for all the registers in the method, or only enough
549  // space for the arguments.
550  if (kIsDebugBuild) {
551    if (method->GetCodeItem() == nullptr) {
552      DCHECK_EQ(0u, arg_offset) << method->PrettyMethod();
553    } else {
554      DCHECK_LE(arg_offset, shadow_frame->NumberOfVRegs());
555    }
556  }
557  jit::Jit* jit = Runtime::Current()->GetJit();
558  if (jit != nullptr && caller != nullptr) {
559    jit->NotifyInterpreterToCompiledCodeTransition(self, caller);
560  }
561  method->Invoke(self, shadow_frame->GetVRegArgs(arg_offset),
562                 (shadow_frame->NumberOfVRegs() - arg_offset) * sizeof(uint32_t),
563                 result, method->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty());
564}
565
566void SetStringInitValueToAllAliases(ShadowFrame* shadow_frame,
567                                    uint16_t this_obj_vreg,
568                                    JValue result)
569    REQUIRES_SHARED(Locks::mutator_lock_) {
570  ObjPtr<mirror::Object> existing = shadow_frame->GetVRegReference(this_obj_vreg);
571  if (existing == nullptr) {
572    // If it's null, we come from compiled code that was deoptimized. Nothing to do,
573    // as the compiler verified there was no alias.
574    // Set the new string result of the StringFactory.
575    shadow_frame->SetVRegReference(this_obj_vreg, result.GetL());
576    return;
577  }
578  // Set the string init result into all aliases.
579  for (uint32_t i = 0, e = shadow_frame->NumberOfVRegs(); i < e; ++i) {
580    if (shadow_frame->GetVRegReference(i) == existing) {
581      DCHECK_EQ(shadow_frame->GetVRegReference(i),
582                reinterpret_cast<mirror::Object*>(shadow_frame->GetVReg(i)));
583      shadow_frame->SetVRegReference(i, result.GetL());
584      DCHECK_EQ(shadow_frame->GetVRegReference(i),
585                reinterpret_cast<mirror::Object*>(shadow_frame->GetVReg(i)));
586    }
587  }
588}
589
590template<bool is_range>
591bool DoInvokePolymorphic(Thread* self,
592                         ShadowFrame& shadow_frame,
593                         const Instruction* inst,
594                         uint16_t inst_data,
595                         JValue* result)
596    REQUIRES_SHARED(Locks::mutator_lock_) {
597  // Make sure to check for async exceptions
598  if (UNLIKELY(self->ObserveAsyncException())) {
599    return false;
600  }
601  // Invoke-polymorphic instructions always take a receiver. i.e, they are never static.
602  const uint32_t vRegC = (is_range) ? inst->VRegC_4rcc() : inst->VRegC_45cc();
603  const int invoke_method_idx = (is_range) ? inst->VRegB_4rcc() : inst->VRegB_45cc();
604
605  // Initialize |result| to 0 as this is the default return value for
606  // polymorphic invocations of method handle types with void return
607  // and provides sane return result in error cases.
608  result->SetJ(0);
609
610  // The invoke_method_idx here is the name of the signature polymorphic method that
611  // was symbolically invoked in bytecode (say MethodHandle.invoke or MethodHandle.invokeExact)
612  // and not the method that we'll dispatch to in the end.
613  StackHandleScope<5> hs(self);
614  Handle<mirror::MethodHandle> method_handle(hs.NewHandle(
615      ObjPtr<mirror::MethodHandle>::DownCast(
616          MakeObjPtr(shadow_frame.GetVRegReference(vRegC)))));
617  if (UNLIKELY(method_handle == nullptr)) {
618    // Note that the invoke type is kVirtual here because a call to a signature
619    // polymorphic method is shaped like a virtual call at the bytecode level.
620    ThrowNullPointerExceptionForMethodAccess(invoke_method_idx, InvokeType::kVirtual);
621    return false;
622  }
623
624  // The vRegH value gives the index of the proto_id associated with this
625  // signature polymorphic call site.
626  const uint32_t callsite_proto_id = (is_range) ? inst->VRegH_4rcc() : inst->VRegH_45cc();
627
628  // Call through to the classlinker and ask it to resolve the static type associated
629  // with the callsite. This information is stored in the dex cache so it's
630  // guaranteed to be fast after the first resolution.
631  ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
632  Handle<mirror::Class> caller_class(hs.NewHandle(shadow_frame.GetMethod()->GetDeclaringClass()));
633  Handle<mirror::MethodType> callsite_type(hs.NewHandle(class_linker->ResolveMethodType(
634      caller_class->GetDexFile(), callsite_proto_id,
635      hs.NewHandle<mirror::DexCache>(caller_class->GetDexCache()),
636      hs.NewHandle<mirror::ClassLoader>(caller_class->GetClassLoader()))));
637
638  // This implies we couldn't resolve one or more types in this method handle.
639  if (UNLIKELY(callsite_type == nullptr)) {
640    CHECK(self->IsExceptionPending());
641    return false;
642  }
643
644  ArtMethod* invoke_method =
645      class_linker->ResolveMethod<ClassLinker::ResolveMode::kCheckICCEAndIAE>(
646          self, invoke_method_idx, shadow_frame.GetMethod(), kVirtual);
647
648  // There is a common dispatch method for method handles that takes
649  // arguments either from a range or an array of arguments depending
650  // on whether the DEX instruction is invoke-polymorphic/range or
651  // invoke-polymorphic. The array here is for the latter.
652  uint32_t args[Instruction::kMaxVarArgRegs] = {};
653  if (is_range) {
654    // VRegC is the register holding the method handle. Arguments passed
655    // to the method handle's target do not include the method handle.
656    uint32_t first_arg = inst->VRegC_4rcc() + 1;
657    return DoInvokePolymorphic<is_range>(self,
658                                         invoke_method,
659                                         shadow_frame,
660                                         method_handle,
661                                         callsite_type,
662                                         args /* unused */,
663                                         first_arg,
664                                         result);
665  } else {
666    // Get the register arguments for the invoke.
667    inst->GetVarArgs(args, inst_data);
668    // Drop the first register which is the method handle performing the invoke.
669    memmove(args, args + 1, sizeof(args[0]) * (Instruction::kMaxVarArgRegs - 1));
670    args[Instruction::kMaxVarArgRegs - 1] = 0;
671    return DoInvokePolymorphic<is_range>(self,
672                                         invoke_method,
673                                         shadow_frame,
674                                         method_handle,
675                                         callsite_type,
676                                         args,
677                                         args[0],
678                                         result);
679  }
680}
681
682static ObjPtr<mirror::CallSite> InvokeBootstrapMethod(Thread* self,
683                                                      ShadowFrame& shadow_frame,
684                                                      uint32_t call_site_idx)
685    REQUIRES_SHARED(Locks::mutator_lock_) {
686  ArtMethod* referrer = shadow_frame.GetMethod();
687  const DexFile* dex_file = referrer->GetDexFile();
688  const DexFile::CallSiteIdItem& csi = dex_file->GetCallSiteId(call_site_idx);
689
690  StackHandleScope<10> hs(self);
691  Handle<mirror::ClassLoader> class_loader(hs.NewHandle(referrer->GetClassLoader()));
692  Handle<mirror::DexCache> dex_cache(hs.NewHandle(referrer->GetDexCache()));
693
694  CallSiteArrayValueIterator it(*dex_file, csi);
695  uint32_t method_handle_idx = static_cast<uint32_t>(it.GetJavaValue().i);
696  ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
697  Handle<mirror::MethodHandle>
698      bootstrap(hs.NewHandle(class_linker->ResolveMethodHandle(method_handle_idx, referrer)));
699  if (bootstrap.IsNull()) {
700    DCHECK(self->IsExceptionPending());
701    return nullptr;
702  }
703  Handle<mirror::MethodType> bootstrap_method_type = hs.NewHandle(bootstrap->GetMethodType());
704  it.Next();
705
706  DCHECK_EQ(static_cast<size_t>(bootstrap->GetMethodType()->GetPTypes()->GetLength()), it.Size());
707  const size_t num_bootstrap_vregs = bootstrap->GetMethodType()->NumberOfVRegs();
708
709  // Set-up a shadow frame for invoking the bootstrap method handle.
710  ShadowFrameAllocaUniquePtr bootstrap_frame =
711      CREATE_SHADOW_FRAME(num_bootstrap_vregs, nullptr, referrer, shadow_frame.GetDexPC());
712  ScopedStackedShadowFramePusher pusher(
713      self, bootstrap_frame.get(), StackedShadowFrameType::kShadowFrameUnderConstruction);
714  size_t vreg = 0;
715
716  // The first parameter is a MethodHandles lookup instance.
717  {
718    Handle<mirror::Class> lookup_class(hs.NewHandle(bootstrap->GetTargetClass()));
719    ObjPtr<mirror::MethodHandlesLookup> lookup =
720        mirror::MethodHandlesLookup::Create(self, lookup_class);
721    if (lookup.IsNull()) {
722      DCHECK(self->IsExceptionPending());
723      return nullptr;
724    }
725    bootstrap_frame->SetVRegReference(vreg++, lookup.Ptr());
726  }
727
728  // The second parameter is the name to lookup.
729  {
730    dex::StringIndex name_idx(static_cast<uint32_t>(it.GetJavaValue().i));
731    ObjPtr<mirror::String> name = class_linker->ResolveString(*dex_file, name_idx, dex_cache);
732    if (name.IsNull()) {
733      DCHECK(self->IsExceptionPending());
734      return nullptr;
735    }
736    bootstrap_frame->SetVRegReference(vreg++, name.Ptr());
737  }
738  it.Next();
739
740  // The third parameter is the method type associated with the name.
741  uint32_t method_type_idx = static_cast<uint32_t>(it.GetJavaValue().i);
742  Handle<mirror::MethodType>
743      method_type(hs.NewHandle(class_linker->ResolveMethodType(*dex_file,
744                                                               method_type_idx,
745                                                               dex_cache,
746                                                               class_loader)));
747  if (method_type.IsNull()) {
748    DCHECK(self->IsExceptionPending());
749    return nullptr;
750  }
751  bootstrap_frame->SetVRegReference(vreg++, method_type.Get());
752  it.Next();
753
754  // Append remaining arguments (if any).
755  while (it.HasNext()) {
756    const jvalue& jvalue = it.GetJavaValue();
757    switch (it.GetValueType()) {
758      case EncodedArrayValueIterator::ValueType::kBoolean:
759      case EncodedArrayValueIterator::ValueType::kByte:
760      case EncodedArrayValueIterator::ValueType::kChar:
761      case EncodedArrayValueIterator::ValueType::kShort:
762      case EncodedArrayValueIterator::ValueType::kInt:
763        bootstrap_frame->SetVReg(vreg, jvalue.i);
764        vreg += 1;
765        break;
766      case EncodedArrayValueIterator::ValueType::kLong:
767        bootstrap_frame->SetVRegLong(vreg, jvalue.j);
768        vreg += 2;
769        break;
770      case EncodedArrayValueIterator::ValueType::kFloat:
771        bootstrap_frame->SetVRegFloat(vreg, jvalue.f);
772        vreg += 1;
773        break;
774      case EncodedArrayValueIterator::ValueType::kDouble:
775        bootstrap_frame->SetVRegDouble(vreg, jvalue.d);
776        vreg += 2;
777        break;
778      case EncodedArrayValueIterator::ValueType::kMethodType: {
779        uint32_t idx = static_cast<uint32_t>(jvalue.i);
780        ObjPtr<mirror::MethodType> ref =
781            class_linker->ResolveMethodType(*dex_file, idx, dex_cache, class_loader);
782        if (ref.IsNull()) {
783          DCHECK(self->IsExceptionPending());
784          return nullptr;
785        }
786        bootstrap_frame->SetVRegReference(vreg, ref.Ptr());
787        vreg += 1;
788        break;
789      }
790      case EncodedArrayValueIterator::ValueType::kMethodHandle: {
791        uint32_t idx = static_cast<uint32_t>(jvalue.i);
792        ObjPtr<mirror::MethodHandle> ref =
793            class_linker->ResolveMethodHandle(idx, referrer);
794        if (ref.IsNull()) {
795          DCHECK(self->IsExceptionPending());
796          return nullptr;
797        }
798        bootstrap_frame->SetVRegReference(vreg, ref.Ptr());
799        vreg += 1;
800        break;
801      }
802      case EncodedArrayValueIterator::ValueType::kString: {
803        dex::StringIndex idx(static_cast<uint32_t>(jvalue.i));
804        ObjPtr<mirror::String> ref = class_linker->ResolveString(*dex_file, idx, dex_cache);
805        if (ref.IsNull()) {
806          DCHECK(self->IsExceptionPending());
807          return nullptr;
808        }
809        bootstrap_frame->SetVRegReference(vreg, ref.Ptr());
810        vreg += 1;
811        break;
812      }
813      case EncodedArrayValueIterator::ValueType::kType: {
814        dex::TypeIndex idx(static_cast<uint32_t>(jvalue.i));
815        ObjPtr<mirror::Class> ref =
816            class_linker->ResolveType(*dex_file, idx, dex_cache, class_loader);
817        if (ref.IsNull()) {
818          DCHECK(self->IsExceptionPending());
819          return nullptr;
820        }
821        bootstrap_frame->SetVRegReference(vreg, ref.Ptr());
822        vreg += 1;
823        break;
824      }
825      case EncodedArrayValueIterator::ValueType::kNull:
826        bootstrap_frame->SetVRegReference(vreg, nullptr);
827        vreg += 1;
828        break;
829      case EncodedArrayValueIterator::ValueType::kField:
830      case EncodedArrayValueIterator::ValueType::kMethod:
831      case EncodedArrayValueIterator::ValueType::kEnum:
832      case EncodedArrayValueIterator::ValueType::kArray:
833      case EncodedArrayValueIterator::ValueType::kAnnotation:
834        // Unreachable based on current EncodedArrayValueIterator::Next().
835        UNREACHABLE();
836    }
837
838    it.Next();
839  }
840
841  // Invoke the bootstrap method handle.
842  JValue result;
843
844  // This array of arguments is unused. DoInvokePolymorphic() operates on either a
845  // an argument array or a range, but always takes an array argument.
846  uint32_t args_unused[Instruction::kMaxVarArgRegs];
847  ArtMethod* invoke_exact =
848      jni::DecodeArtMethod(WellKnownClasses::java_lang_invoke_MethodHandle_invokeExact);
849  bool invoke_success = DoInvokePolymorphic<true /* is_range */>(self,
850                                                                 invoke_exact,
851                                                                 *bootstrap_frame,
852                                                                 bootstrap,
853                                                                 bootstrap_method_type,
854                                                                 args_unused,
855                                                                 0,
856                                                                 &result);
857  if (!invoke_success) {
858    DCHECK(self->IsExceptionPending());
859    return nullptr;
860  }
861
862  Handle<mirror::Object> object(hs.NewHandle(result.GetL()));
863
864  // Check the result is not null.
865  if (UNLIKELY(object.IsNull())) {
866    ThrowNullPointerException("CallSite == null");
867    return nullptr;
868  }
869
870  // Check the result type is a subclass of CallSite.
871  if (UNLIKELY(!object->InstanceOf(mirror::CallSite::StaticClass()))) {
872    ThrowClassCastException(object->GetClass(), mirror::CallSite::StaticClass());
873    return nullptr;
874  }
875
876  Handle<mirror::CallSite> call_site =
877      hs.NewHandle(ObjPtr<mirror::CallSite>::DownCast(ObjPtr<mirror::Object>(result.GetL())));
878
879  // Check the call site target is not null as we're going to invoke it.
880  Handle<mirror::MethodHandle> target = hs.NewHandle(call_site->GetTarget());
881  if (UNLIKELY(target.IsNull())) {
882    ThrowNullPointerException("CallSite target == null");
883    return nullptr;
884  }
885
886  // Check the target method type matches the method type requested modulo the receiver
887  // needs to be compatible rather than exact.
888  Handle<mirror::MethodType> target_method_type = hs.NewHandle(target->GetMethodType());
889  if (UNLIKELY(!target_method_type->IsExactMatch(method_type.Get()) &&
890               !IsParameterTypeConvertible(target_method_type->GetPTypes()->GetWithoutChecks(0),
891                                           method_type->GetPTypes()->GetWithoutChecks(0)))) {
892    ThrowWrongMethodTypeException(target_method_type.Get(), method_type.Get());
893    return nullptr;
894  }
895
896  return call_site.Get();
897}
898
899template<bool is_range>
900bool DoInvokeCustom(Thread* self,
901                    ShadowFrame& shadow_frame,
902                    const Instruction* inst,
903                    uint16_t inst_data,
904                    JValue* result)
905    REQUIRES_SHARED(Locks::mutator_lock_) {
906  // Make sure to check for async exceptions
907  if (UNLIKELY(self->ObserveAsyncException())) {
908    return false;
909  }
910  // invoke-custom is not supported in transactions. In transactions
911  // there is a limited set of types supported. invoke-custom allows
912  // running arbitrary code and instantiating arbitrary types.
913  CHECK(!Runtime::Current()->IsActiveTransaction());
914  StackHandleScope<4> hs(self);
915  Handle<mirror::DexCache> dex_cache(hs.NewHandle(shadow_frame.GetMethod()->GetDexCache()));
916  const uint32_t call_site_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
917  MutableHandle<mirror::CallSite>
918      call_site(hs.NewHandle(dex_cache->GetResolvedCallSite(call_site_idx)));
919  if (call_site.IsNull()) {
920    call_site.Assign(InvokeBootstrapMethod(self, shadow_frame, call_site_idx));
921    if (UNLIKELY(call_site.IsNull())) {
922      CHECK(self->IsExceptionPending());
923      ThrowWrappedBootstrapMethodError("Exception from call site #%u bootstrap method",
924                                       call_site_idx);
925      result->SetJ(0);
926      return false;
927    }
928    mirror::CallSite* winning_call_site =
929        dex_cache->SetResolvedCallSite(call_site_idx, call_site.Get());
930    call_site.Assign(winning_call_site);
931  }
932
933  // CallSite.java checks the re-assignment of the call site target
934  // when mutating call site targets. We only check the target is
935  // non-null and has the right type during bootstrap method execution.
936  Handle<mirror::MethodHandle> target = hs.NewHandle(call_site->GetTarget());
937  Handle<mirror::MethodType> target_method_type = hs.NewHandle(target->GetMethodType());
938  DCHECK_EQ(static_cast<size_t>(inst->VRegA()), target_method_type->NumberOfVRegs());
939
940  uint32_t args[Instruction::kMaxVarArgRegs];
941  if (is_range) {
942    args[0] = inst->VRegC_3rc();
943  } else {
944    inst->GetVarArgs(args, inst_data);
945  }
946
947  ArtMethod* invoke_exact =
948      jni::DecodeArtMethod(WellKnownClasses::java_lang_invoke_MethodHandle_invokeExact);
949  return DoInvokePolymorphic<is_range>(self,
950                                       invoke_exact,
951                                       shadow_frame,
952                                       target,
953                                       target_method_type,
954                                       args,
955                                       args[0],
956                                       result);
957}
958
959template <bool is_range>
960inline void CopyRegisters(ShadowFrame& caller_frame,
961                          ShadowFrame* callee_frame,
962                          const uint32_t (&arg)[Instruction::kMaxVarArgRegs],
963                          const size_t first_src_reg,
964                          const size_t first_dest_reg,
965                          const size_t num_regs) {
966  if (is_range) {
967    const size_t dest_reg_bound = first_dest_reg + num_regs;
968    for (size_t src_reg = first_src_reg, dest_reg = first_dest_reg; dest_reg < dest_reg_bound;
969        ++dest_reg, ++src_reg) {
970      AssignRegister(callee_frame, caller_frame, dest_reg, src_reg);
971    }
972  } else {
973    DCHECK_LE(num_regs, arraysize(arg));
974
975    for (size_t arg_index = 0; arg_index < num_regs; ++arg_index) {
976      AssignRegister(callee_frame, caller_frame, first_dest_reg + arg_index, arg[arg_index]);
977    }
978  }
979}
980
981template <bool is_range,
982          bool do_assignability_check>
983static inline bool DoCallCommon(ArtMethod* called_method,
984                                Thread* self,
985                                ShadowFrame& shadow_frame,
986                                JValue* result,
987                                uint16_t number_of_inputs,
988                                uint32_t (&arg)[Instruction::kMaxVarArgRegs],
989                                uint32_t vregC) {
990  bool string_init = false;
991  // Replace calls to String.<init> with equivalent StringFactory call.
992  if (UNLIKELY(called_method->GetDeclaringClass()->IsStringClass()
993               && called_method->IsConstructor())) {
994    called_method = WellKnownClasses::StringInitToStringFactory(called_method);
995    string_init = true;
996  }
997
998  // Compute method information.
999  const DexFile::CodeItem* code_item = called_method->GetCodeItem();
1000  // Number of registers for the callee's call frame.
1001  uint16_t num_regs;
1002  // Test whether to use the interpreter or compiler entrypoint, and save that result to pass to
1003  // PerformCall. A deoptimization could occur at any time, and we shouldn't change which
1004  // entrypoint to use once we start building the shadow frame.
1005
1006  // For unstarted runtimes, always use the interpreter entrypoint. This fixes the case where we are
1007  // doing cross compilation. Note that GetEntryPointFromQuickCompiledCode doesn't use the image
1008  // pointer size here and this may case an overflow if it is called from the compiler. b/62402160
1009  const bool use_interpreter_entrypoint = !Runtime::Current()->IsStarted() ||
1010      ClassLinker::ShouldUseInterpreterEntrypoint(
1011          called_method,
1012          called_method->GetEntryPointFromQuickCompiledCode());
1013  if (LIKELY(code_item != nullptr)) {
1014    // When transitioning to compiled code, space only needs to be reserved for the input registers.
1015    // The rest of the frame gets discarded. This also prevents accessing the called method's code
1016    // item, saving memory by keeping code items of compiled code untouched.
1017    if (!use_interpreter_entrypoint) {
1018      DCHECK(!Runtime::Current()->IsAotCompiler()) << "Compiler should use interpreter entrypoint";
1019      num_regs = number_of_inputs;
1020    } else {
1021      num_regs = code_item->registers_size_;
1022      DCHECK_EQ(string_init ? number_of_inputs - 1 : number_of_inputs, code_item->ins_size_);
1023    }
1024  } else {
1025    DCHECK(called_method->IsNative() || called_method->IsProxyMethod());
1026    num_regs = number_of_inputs;
1027  }
1028
1029  // Hack for String init:
1030  //
1031  // Rewrite invoke-x java.lang.String.<init>(this, a, b, c, ...) into:
1032  //         invoke-x StringFactory(a, b, c, ...)
1033  // by effectively dropping the first virtual register from the invoke.
1034  //
1035  // (at this point the ArtMethod has already been replaced,
1036  // so we just need to fix-up the arguments)
1037  //
1038  // Note that FindMethodFromCode in entrypoint_utils-inl.h was also special-cased
1039  // to handle the compiler optimization of replacing `this` with null without
1040  // throwing NullPointerException.
1041  uint32_t string_init_vreg_this = is_range ? vregC : arg[0];
1042  if (UNLIKELY(string_init)) {
1043    DCHECK_GT(num_regs, 0u);  // As the method is an instance method, there should be at least 1.
1044
1045    // The new StringFactory call is static and has one fewer argument.
1046    if (code_item == nullptr) {
1047      DCHECK(called_method->IsNative() || called_method->IsProxyMethod());
1048      num_regs--;
1049    }  // else ... don't need to change num_regs since it comes up from the string_init's code item
1050    number_of_inputs--;
1051
1052    // Rewrite the var-args, dropping the 0th argument ("this")
1053    for (uint32_t i = 1; i < arraysize(arg); ++i) {
1054      arg[i - 1] = arg[i];
1055    }
1056    arg[arraysize(arg) - 1] = 0;
1057
1058    // Rewrite the non-var-arg case
1059    vregC++;  // Skips the 0th vreg in the range ("this").
1060  }
1061
1062  // Parameter registers go at the end of the shadow frame.
1063  DCHECK_GE(num_regs, number_of_inputs);
1064  size_t first_dest_reg = num_regs - number_of_inputs;
1065  DCHECK_NE(first_dest_reg, (size_t)-1);
1066
1067  // Allocate shadow frame on the stack.
1068  const char* old_cause = self->StartAssertNoThreadSuspension("DoCallCommon");
1069  ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
1070      CREATE_SHADOW_FRAME(num_regs, &shadow_frame, called_method, /* dex pc */ 0);
1071  ShadowFrame* new_shadow_frame = shadow_frame_unique_ptr.get();
1072
1073  // Initialize new shadow frame by copying the registers from the callee shadow frame.
1074  if (do_assignability_check) {
1075    // Slow path.
1076    // We might need to do class loading, which incurs a thread state change to kNative. So
1077    // register the shadow frame as under construction and allow suspension again.
1078    ScopedStackedShadowFramePusher pusher(
1079        self, new_shadow_frame, StackedShadowFrameType::kShadowFrameUnderConstruction);
1080    self->EndAssertNoThreadSuspension(old_cause);
1081
1082    // ArtMethod here is needed to check type information of the call site against the callee.
1083    // Type information is retrieved from a DexFile/DexCache for that respective declared method.
1084    //
1085    // As a special case for proxy methods, which are not dex-backed,
1086    // we have to retrieve type information from the proxy's method
1087    // interface method instead (which is dex backed since proxies are never interfaces).
1088    ArtMethod* method =
1089        new_shadow_frame->GetMethod()->GetInterfaceMethodIfProxy(kRuntimePointerSize);
1090
1091    // We need to do runtime check on reference assignment. We need to load the shorty
1092    // to get the exact type of each reference argument.
1093    const DexFile::TypeList* params = method->GetParameterTypeList();
1094    uint32_t shorty_len = 0;
1095    const char* shorty = method->GetShorty(&shorty_len);
1096
1097    // Handle receiver apart since it's not part of the shorty.
1098    size_t dest_reg = first_dest_reg;
1099    size_t arg_offset = 0;
1100
1101    if (!method->IsStatic()) {
1102      size_t receiver_reg = is_range ? vregC : arg[0];
1103      new_shadow_frame->SetVRegReference(dest_reg, shadow_frame.GetVRegReference(receiver_reg));
1104      ++dest_reg;
1105      ++arg_offset;
1106      DCHECK(!string_init);  // All StringFactory methods are static.
1107    }
1108
1109    // Copy the caller's invoke-* arguments into the callee's parameter registers.
1110    for (uint32_t shorty_pos = 0; dest_reg < num_regs; ++shorty_pos, ++dest_reg, ++arg_offset) {
1111      // Skip the 0th 'shorty' type since it represents the return type.
1112      DCHECK_LT(shorty_pos + 1, shorty_len) << "for shorty '" << shorty << "'";
1113      const size_t src_reg = (is_range) ? vregC + arg_offset : arg[arg_offset];
1114      switch (shorty[shorty_pos + 1]) {
1115        // Handle Object references. 1 virtual register slot.
1116        case 'L': {
1117          ObjPtr<mirror::Object> o = shadow_frame.GetVRegReference(src_reg);
1118          if (do_assignability_check && o != nullptr) {
1119            const dex::TypeIndex type_idx = params->GetTypeItem(shorty_pos).type_idx_;
1120            ObjPtr<mirror::Class> arg_type = method->GetDexCache()->GetResolvedType(type_idx);
1121            if (arg_type == nullptr) {
1122              StackHandleScope<1> hs(self);
1123              // Preserve o since it is used below and GetClassFromTypeIndex may cause thread
1124              // suspension.
1125              HandleWrapperObjPtr<mirror::Object> h = hs.NewHandleWrapper(&o);
1126              arg_type = method->ResolveClassFromTypeIndex(type_idx);
1127              if (arg_type == nullptr) {
1128                CHECK(self->IsExceptionPending());
1129                return false;
1130              }
1131            }
1132            if (!o->VerifierInstanceOf(arg_type)) {
1133              // This should never happen.
1134              std::string temp1, temp2;
1135              self->ThrowNewExceptionF("Ljava/lang/InternalError;",
1136                                       "Invoking %s with bad arg %d, type '%s' not instance of '%s'",
1137                                       new_shadow_frame->GetMethod()->GetName(), shorty_pos,
1138                                       o->GetClass()->GetDescriptor(&temp1),
1139                                       arg_type->GetDescriptor(&temp2));
1140              return false;
1141            }
1142          }
1143          new_shadow_frame->SetVRegReference(dest_reg, o.Ptr());
1144          break;
1145        }
1146        // Handle doubles and longs. 2 consecutive virtual register slots.
1147        case 'J': case 'D': {
1148          uint64_t wide_value =
1149              (static_cast<uint64_t>(shadow_frame.GetVReg(src_reg + 1)) << BitSizeOf<uint32_t>()) |
1150               static_cast<uint32_t>(shadow_frame.GetVReg(src_reg));
1151          new_shadow_frame->SetVRegLong(dest_reg, wide_value);
1152          // Skip the next virtual register slot since we already used it.
1153          ++dest_reg;
1154          ++arg_offset;
1155          break;
1156        }
1157        // Handle all other primitives that are always 1 virtual register slot.
1158        default:
1159          new_shadow_frame->SetVReg(dest_reg, shadow_frame.GetVReg(src_reg));
1160          break;
1161      }
1162    }
1163  } else {
1164    if (is_range) {
1165      DCHECK_EQ(num_regs, first_dest_reg + number_of_inputs);
1166    }
1167
1168    CopyRegisters<is_range>(shadow_frame,
1169                            new_shadow_frame,
1170                            arg,
1171                            vregC,
1172                            first_dest_reg,
1173                            number_of_inputs);
1174    self->EndAssertNoThreadSuspension(old_cause);
1175  }
1176
1177  PerformCall(self,
1178              code_item,
1179              shadow_frame.GetMethod(),
1180              first_dest_reg,
1181              new_shadow_frame,
1182              result,
1183              use_interpreter_entrypoint);
1184
1185  if (string_init && !self->IsExceptionPending()) {
1186    SetStringInitValueToAllAliases(&shadow_frame, string_init_vreg_this, *result);
1187  }
1188
1189  return !self->IsExceptionPending();
1190}
1191
1192template<bool is_range, bool do_assignability_check>
1193bool DoCall(ArtMethod* called_method, Thread* self, ShadowFrame& shadow_frame,
1194            const Instruction* inst, uint16_t inst_data, JValue* result) {
1195  // Argument word count.
1196  const uint16_t number_of_inputs =
1197      (is_range) ? inst->VRegA_3rc(inst_data) : inst->VRegA_35c(inst_data);
1198
1199  // TODO: find a cleaner way to separate non-range and range information without duplicating
1200  //       code.
1201  uint32_t arg[Instruction::kMaxVarArgRegs] = {};  // only used in invoke-XXX.
1202  uint32_t vregC = 0;
1203  if (is_range) {
1204    vregC = inst->VRegC_3rc();
1205  } else {
1206    vregC = inst->VRegC_35c();
1207    inst->GetVarArgs(arg, inst_data);
1208  }
1209
1210  return DoCallCommon<is_range, do_assignability_check>(
1211      called_method, self, shadow_frame,
1212      result, number_of_inputs, arg, vregC);
1213}
1214
1215template <bool is_range, bool do_access_check, bool transaction_active>
1216bool DoFilledNewArray(const Instruction* inst,
1217                      const ShadowFrame& shadow_frame,
1218                      Thread* self,
1219                      JValue* result) {
1220  DCHECK(inst->Opcode() == Instruction::FILLED_NEW_ARRAY ||
1221         inst->Opcode() == Instruction::FILLED_NEW_ARRAY_RANGE);
1222  const int32_t length = is_range ? inst->VRegA_3rc() : inst->VRegA_35c();
1223  if (!is_range) {
1224    // Checks FILLED_NEW_ARRAY's length does not exceed 5 arguments.
1225    CHECK_LE(length, 5);
1226  }
1227  if (UNLIKELY(length < 0)) {
1228    ThrowNegativeArraySizeException(length);
1229    return false;
1230  }
1231  uint16_t type_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
1232  ObjPtr<mirror::Class> array_class = ResolveVerifyAndClinit(dex::TypeIndex(type_idx),
1233                                                             shadow_frame.GetMethod(),
1234                                                             self,
1235                                                             false,
1236                                                             do_access_check);
1237  if (UNLIKELY(array_class == nullptr)) {
1238    DCHECK(self->IsExceptionPending());
1239    return false;
1240  }
1241  CHECK(array_class->IsArrayClass());
1242  ObjPtr<mirror::Class> component_class = array_class->GetComponentType();
1243  const bool is_primitive_int_component = component_class->IsPrimitiveInt();
1244  if (UNLIKELY(component_class->IsPrimitive() && !is_primitive_int_component)) {
1245    if (component_class->IsPrimitiveLong() || component_class->IsPrimitiveDouble()) {
1246      ThrowRuntimeException("Bad filled array request for type %s",
1247                            component_class->PrettyDescriptor().c_str());
1248    } else {
1249      self->ThrowNewExceptionF("Ljava/lang/InternalError;",
1250                               "Found type %s; filled-new-array not implemented for anything but 'int'",
1251                               component_class->PrettyDescriptor().c_str());
1252    }
1253    return false;
1254  }
1255  ObjPtr<mirror::Object> new_array = mirror::Array::Alloc<true>(
1256      self,
1257      array_class,
1258      length,
1259      array_class->GetComponentSizeShift(),
1260      Runtime::Current()->GetHeap()->GetCurrentAllocator());
1261  if (UNLIKELY(new_array == nullptr)) {
1262    self->AssertPendingOOMException();
1263    return false;
1264  }
1265  uint32_t arg[Instruction::kMaxVarArgRegs];  // only used in filled-new-array.
1266  uint32_t vregC = 0;   // only used in filled-new-array-range.
1267  if (is_range) {
1268    vregC = inst->VRegC_3rc();
1269  } else {
1270    inst->GetVarArgs(arg);
1271  }
1272  for (int32_t i = 0; i < length; ++i) {
1273    size_t src_reg = is_range ? vregC + i : arg[i];
1274    if (is_primitive_int_component) {
1275      new_array->AsIntArray()->SetWithoutChecks<transaction_active>(
1276          i, shadow_frame.GetVReg(src_reg));
1277    } else {
1278      new_array->AsObjectArray<mirror::Object>()->SetWithoutChecks<transaction_active>(
1279          i, shadow_frame.GetVRegReference(src_reg));
1280    }
1281  }
1282
1283  result->SetL(new_array);
1284  return true;
1285}
1286
1287// TODO: Use ObjPtr here.
1288template<typename T>
1289static void RecordArrayElementsInTransactionImpl(mirror::PrimitiveArray<T>* array,
1290                                                 int32_t count)
1291    REQUIRES_SHARED(Locks::mutator_lock_) {
1292  Runtime* runtime = Runtime::Current();
1293  for (int32_t i = 0; i < count; ++i) {
1294    runtime->RecordWriteArray(array, i, array->GetWithoutChecks(i));
1295  }
1296}
1297
1298void RecordArrayElementsInTransaction(ObjPtr<mirror::Array> array, int32_t count)
1299    REQUIRES_SHARED(Locks::mutator_lock_) {
1300  DCHECK(Runtime::Current()->IsActiveTransaction());
1301  DCHECK(array != nullptr);
1302  DCHECK_LE(count, array->GetLength());
1303  Primitive::Type primitive_component_type = array->GetClass()->GetComponentType()->GetPrimitiveType();
1304  switch (primitive_component_type) {
1305    case Primitive::kPrimBoolean:
1306      RecordArrayElementsInTransactionImpl(array->AsBooleanArray(), count);
1307      break;
1308    case Primitive::kPrimByte:
1309      RecordArrayElementsInTransactionImpl(array->AsByteArray(), count);
1310      break;
1311    case Primitive::kPrimChar:
1312      RecordArrayElementsInTransactionImpl(array->AsCharArray(), count);
1313      break;
1314    case Primitive::kPrimShort:
1315      RecordArrayElementsInTransactionImpl(array->AsShortArray(), count);
1316      break;
1317    case Primitive::kPrimInt:
1318      RecordArrayElementsInTransactionImpl(array->AsIntArray(), count);
1319      break;
1320    case Primitive::kPrimFloat:
1321      RecordArrayElementsInTransactionImpl(array->AsFloatArray(), count);
1322      break;
1323    case Primitive::kPrimLong:
1324      RecordArrayElementsInTransactionImpl(array->AsLongArray(), count);
1325      break;
1326    case Primitive::kPrimDouble:
1327      RecordArrayElementsInTransactionImpl(array->AsDoubleArray(), count);
1328      break;
1329    default:
1330      LOG(FATAL) << "Unsupported primitive type " << primitive_component_type
1331                 << " in fill-array-data";
1332      break;
1333  }
1334}
1335
1336// Explicit DoCall template function declarations.
1337#define EXPLICIT_DO_CALL_TEMPLATE_DECL(_is_range, _do_assignability_check)                      \
1338  template REQUIRES_SHARED(Locks::mutator_lock_)                                                \
1339  bool DoCall<_is_range, _do_assignability_check>(ArtMethod* method, Thread* self,              \
1340                                                  ShadowFrame& shadow_frame,                    \
1341                                                  const Instruction* inst, uint16_t inst_data,  \
1342                                                  JValue* result)
1343EXPLICIT_DO_CALL_TEMPLATE_DECL(false, false);
1344EXPLICIT_DO_CALL_TEMPLATE_DECL(false, true);
1345EXPLICIT_DO_CALL_TEMPLATE_DECL(true, false);
1346EXPLICIT_DO_CALL_TEMPLATE_DECL(true, true);
1347#undef EXPLICIT_DO_CALL_TEMPLATE_DECL
1348
1349// Explicit DoInvokeCustom template function declarations.
1350#define EXPLICIT_DO_INVOKE_CUSTOM_TEMPLATE_DECL(_is_range)               \
1351  template REQUIRES_SHARED(Locks::mutator_lock_)                         \
1352  bool DoInvokeCustom<_is_range>(                                        \
1353      Thread* self, ShadowFrame& shadow_frame, const Instruction* inst,  \
1354      uint16_t inst_data, JValue* result)
1355EXPLICIT_DO_INVOKE_CUSTOM_TEMPLATE_DECL(false);
1356EXPLICIT_DO_INVOKE_CUSTOM_TEMPLATE_DECL(true);
1357#undef EXPLICIT_DO_INVOKE_CUSTOM_TEMPLATE_DECL
1358
1359// Explicit DoInvokePolymorphic template function declarations.
1360#define EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(_is_range)          \
1361  template REQUIRES_SHARED(Locks::mutator_lock_)                         \
1362  bool DoInvokePolymorphic<_is_range>(                                   \
1363      Thread* self, ShadowFrame& shadow_frame, const Instruction* inst,  \
1364      uint16_t inst_data, JValue* result)
1365EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(false);
1366EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(true);
1367#undef EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL
1368
1369// Explicit DoFilledNewArray template function declarations.
1370#define EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(_is_range_, _check, _transaction_active)       \
1371  template REQUIRES_SHARED(Locks::mutator_lock_)                                                  \
1372  bool DoFilledNewArray<_is_range_, _check, _transaction_active>(const Instruction* inst,         \
1373                                                                 const ShadowFrame& shadow_frame, \
1374                                                                 Thread* self, JValue* result)
1375#define EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(_transaction_active)       \
1376  EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, false, _transaction_active);  \
1377  EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, true, _transaction_active);   \
1378  EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, false, _transaction_active);   \
1379  EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, true, _transaction_active)
1380EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(false);
1381EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(true);
1382#undef EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL
1383#undef EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL
1384
1385}  // namespace interpreter
1386}  // namespace art
1387