interpreter_common.cc revision 848f70a3d73833fc1bf3032a9ff6812e429661d9
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 "debugger.h"
22#include "mirror/array-inl.h"
23#include "unstarted_runtime.h"
24#include "verifier/method_verifier.h"
25
26namespace art {
27namespace interpreter {
28
29void ThrowNullPointerExceptionFromInterpreter() {
30  ThrowNullPointerExceptionFromDexPC();
31}
32
33template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check>
34bool DoFieldGet(Thread* self, ShadowFrame& shadow_frame, const Instruction* inst,
35                uint16_t inst_data) {
36  const bool is_static = (find_type == StaticObjectRead) || (find_type == StaticPrimitiveRead);
37  const uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
38  ArtField* f = FindFieldFromCode<find_type, do_access_check>(field_idx, shadow_frame.GetMethod(), self,
39                                                              Primitive::ComponentSize(field_type));
40  if (UNLIKELY(f == nullptr)) {
41    CHECK(self->IsExceptionPending());
42    return false;
43  }
44  Object* obj;
45  if (is_static) {
46    obj = f->GetDeclaringClass();
47  } else {
48    obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
49    if (UNLIKELY(obj == nullptr)) {
50      ThrowNullPointerExceptionForFieldAccess(f, true);
51      return false;
52    }
53  }
54  f->GetDeclaringClass()->AssertInitializedOrInitializingInThread(self);
55  // Report this field access to instrumentation if needed.
56  instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
57  if (UNLIKELY(instrumentation->HasFieldReadListeners())) {
58    Object* this_object = f->IsStatic() ? nullptr : obj;
59    instrumentation->FieldReadEvent(self, this_object, shadow_frame.GetMethod(),
60                                    shadow_frame.GetDexPC(), f);
61  }
62  uint32_t vregA = is_static ? inst->VRegA_21c(inst_data) : inst->VRegA_22c(inst_data);
63  switch (field_type) {
64    case Primitive::kPrimBoolean:
65      shadow_frame.SetVReg(vregA, f->GetBoolean(obj));
66      break;
67    case Primitive::kPrimByte:
68      shadow_frame.SetVReg(vregA, f->GetByte(obj));
69      break;
70    case Primitive::kPrimChar:
71      shadow_frame.SetVReg(vregA, f->GetChar(obj));
72      break;
73    case Primitive::kPrimShort:
74      shadow_frame.SetVReg(vregA, f->GetShort(obj));
75      break;
76    case Primitive::kPrimInt:
77      shadow_frame.SetVReg(vregA, f->GetInt(obj));
78      break;
79    case Primitive::kPrimLong:
80      shadow_frame.SetVRegLong(vregA, f->GetLong(obj));
81      break;
82    case Primitive::kPrimNot:
83      shadow_frame.SetVRegReference(vregA, f->GetObject(obj));
84      break;
85    default:
86      LOG(FATAL) << "Unreachable: " << field_type;
87      UNREACHABLE();
88  }
89  return true;
90}
91
92// Explicitly instantiate all DoFieldGet functions.
93#define EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, _do_check) \
94  template bool DoFieldGet<_find_type, _field_type, _do_check>(Thread* self, \
95                                                               ShadowFrame& shadow_frame, \
96                                                               const Instruction* inst, \
97                                                               uint16_t inst_data)
98
99#define EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(_find_type, _field_type)  \
100    EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, false);  \
101    EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, true);
102
103// iget-XXX
104EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimBoolean)
105EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimByte)
106EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimChar)
107EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimShort)
108EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimInt)
109EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimLong)
110EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstanceObjectRead, Primitive::kPrimNot)
111
112// sget-XXX
113EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimBoolean)
114EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimByte)
115EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimChar)
116EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimShort)
117EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimInt)
118EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimLong)
119EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticObjectRead, Primitive::kPrimNot)
120
121#undef EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL
122#undef EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL
123
124// Handles iget-quick, iget-wide-quick and iget-object-quick instructions.
125// Returns true on success, otherwise throws an exception and returns false.
126template<Primitive::Type field_type>
127bool DoIGetQuick(ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) {
128  Object* obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
129  if (UNLIKELY(obj == nullptr)) {
130    // We lost the reference to the field index so we cannot get a more
131    // precised exception message.
132    ThrowNullPointerExceptionFromDexPC();
133    return false;
134  }
135  MemberOffset field_offset(inst->VRegC_22c());
136  // Report this field access to instrumentation if needed. Since we only have the offset of
137  // the field from the base of the object, we need to look for it first.
138  instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
139  if (UNLIKELY(instrumentation->HasFieldReadListeners())) {
140    ArtField* f = ArtField::FindInstanceFieldWithOffset(obj->GetClass(),
141                                                        field_offset.Uint32Value());
142    DCHECK(f != nullptr);
143    DCHECK(!f->IsStatic());
144    instrumentation->FieldReadEvent(Thread::Current(), obj, shadow_frame.GetMethod(),
145                                    shadow_frame.GetDexPC(), f);
146  }
147  // Note: iget-x-quick instructions are only for non-volatile fields.
148  const uint32_t vregA = inst->VRegA_22c(inst_data);
149  switch (field_type) {
150    case Primitive::kPrimInt:
151      shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetField32(field_offset)));
152      break;
153    case Primitive::kPrimBoolean:
154      shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldBoolean(field_offset)));
155      break;
156    case Primitive::kPrimByte:
157      shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldByte(field_offset)));
158      break;
159    case Primitive::kPrimChar:
160      shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldChar(field_offset)));
161      break;
162    case Primitive::kPrimShort:
163      shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldShort(field_offset)));
164      break;
165    case Primitive::kPrimLong:
166      shadow_frame.SetVRegLong(vregA, static_cast<int64_t>(obj->GetField64(field_offset)));
167      break;
168    case Primitive::kPrimNot:
169      shadow_frame.SetVRegReference(vregA, obj->GetFieldObject<mirror::Object>(field_offset));
170      break;
171    default:
172      LOG(FATAL) << "Unreachable: " << field_type;
173      UNREACHABLE();
174  }
175  return true;
176}
177
178// Explicitly instantiate all DoIGetQuick functions.
179#define EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(_field_type) \
180  template bool DoIGetQuick<_field_type>(ShadowFrame& shadow_frame, const Instruction* inst, \
181                                         uint16_t inst_data)
182
183EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimInt);      // iget-quick.
184EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimBoolean);  // iget-boolean-quick.
185EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimByte);     // iget-byte-quick.
186EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimChar);     // iget-char-quick.
187EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimShort);    // iget-short-quick.
188EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimLong);     // iget-wide-quick.
189EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimNot);      // iget-object-quick.
190#undef EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL
191
192template<Primitive::Type field_type>
193static JValue GetFieldValue(const ShadowFrame& shadow_frame, uint32_t vreg)
194    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
195  JValue field_value;
196  switch (field_type) {
197    case Primitive::kPrimBoolean:
198      field_value.SetZ(static_cast<uint8_t>(shadow_frame.GetVReg(vreg)));
199      break;
200    case Primitive::kPrimByte:
201      field_value.SetB(static_cast<int8_t>(shadow_frame.GetVReg(vreg)));
202      break;
203    case Primitive::kPrimChar:
204      field_value.SetC(static_cast<uint16_t>(shadow_frame.GetVReg(vreg)));
205      break;
206    case Primitive::kPrimShort:
207      field_value.SetS(static_cast<int16_t>(shadow_frame.GetVReg(vreg)));
208      break;
209    case Primitive::kPrimInt:
210      field_value.SetI(shadow_frame.GetVReg(vreg));
211      break;
212    case Primitive::kPrimLong:
213      field_value.SetJ(shadow_frame.GetVRegLong(vreg));
214      break;
215    case Primitive::kPrimNot:
216      field_value.SetL(shadow_frame.GetVRegReference(vreg));
217      break;
218    default:
219      LOG(FATAL) << "Unreachable: " << field_type;
220      UNREACHABLE();
221  }
222  return field_value;
223}
224
225template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check,
226         bool transaction_active>
227bool DoFieldPut(Thread* self, const ShadowFrame& shadow_frame, const Instruction* inst,
228                uint16_t inst_data) {
229  bool do_assignability_check = do_access_check;
230  bool is_static = (find_type == StaticObjectWrite) || (find_type == StaticPrimitiveWrite);
231  uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
232  ArtField* f = FindFieldFromCode<find_type, do_access_check>(field_idx, shadow_frame.GetMethod(), self,
233                                                              Primitive::ComponentSize(field_type));
234  if (UNLIKELY(f == nullptr)) {
235    CHECK(self->IsExceptionPending());
236    return false;
237  }
238  Object* obj;
239  if (is_static) {
240    obj = f->GetDeclaringClass();
241  } else {
242    obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
243    if (UNLIKELY(obj == nullptr)) {
244      ThrowNullPointerExceptionForFieldAccess(f, false);
245      return false;
246    }
247  }
248  f->GetDeclaringClass()->AssertInitializedOrInitializingInThread(self);
249  uint32_t vregA = is_static ? inst->VRegA_21c(inst_data) : inst->VRegA_22c(inst_data);
250  // Report this field access to instrumentation if needed. Since we only have the offset of
251  // the field from the base of the object, we need to look for it first.
252  instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
253  if (UNLIKELY(instrumentation->HasFieldWriteListeners())) {
254    JValue field_value = GetFieldValue<field_type>(shadow_frame, vregA);
255    Object* this_object = f->IsStatic() ? nullptr : obj;
256    instrumentation->FieldWriteEvent(self, this_object, shadow_frame.GetMethod(),
257                                     shadow_frame.GetDexPC(), f, field_value);
258  }
259  switch (field_type) {
260    case Primitive::kPrimBoolean:
261      f->SetBoolean<transaction_active>(obj, shadow_frame.GetVReg(vregA));
262      break;
263    case Primitive::kPrimByte:
264      f->SetByte<transaction_active>(obj, shadow_frame.GetVReg(vregA));
265      break;
266    case Primitive::kPrimChar:
267      f->SetChar<transaction_active>(obj, shadow_frame.GetVReg(vregA));
268      break;
269    case Primitive::kPrimShort:
270      f->SetShort<transaction_active>(obj, shadow_frame.GetVReg(vregA));
271      break;
272    case Primitive::kPrimInt:
273      f->SetInt<transaction_active>(obj, shadow_frame.GetVReg(vregA));
274      break;
275    case Primitive::kPrimLong:
276      f->SetLong<transaction_active>(obj, shadow_frame.GetVRegLong(vregA));
277      break;
278    case Primitive::kPrimNot: {
279      Object* reg = shadow_frame.GetVRegReference(vregA);
280      if (do_assignability_check && reg != nullptr) {
281        // FieldHelper::GetType can resolve classes, use a handle wrapper which will restore the
282        // object in the destructor.
283        Class* field_class;
284        {
285          StackHandleScope<2> hs(self);
286          HandleWrapper<mirror::Object> h_reg(hs.NewHandleWrapper(&reg));
287          HandleWrapper<mirror::Object> h_obj(hs.NewHandleWrapper(&obj));
288          field_class = f->GetType<true>();
289        }
290        if (!reg->VerifierInstanceOf(field_class)) {
291          // This should never happen.
292          std::string temp1, temp2, temp3;
293          self->ThrowNewExceptionF("Ljava/lang/VirtualMachineError;",
294                                   "Put '%s' that is not instance of field '%s' in '%s'",
295                                   reg->GetClass()->GetDescriptor(&temp1),
296                                   field_class->GetDescriptor(&temp2),
297                                   f->GetDeclaringClass()->GetDescriptor(&temp3));
298          return false;
299        }
300      }
301      f->SetObj<transaction_active>(obj, reg);
302      break;
303    }
304    default:
305      LOG(FATAL) << "Unreachable: " << field_type;
306      UNREACHABLE();
307  }
308  return true;
309}
310
311// Explicitly instantiate all DoFieldPut functions.
312#define EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, _do_check, _transaction_active) \
313  template bool DoFieldPut<_find_type, _field_type, _do_check, _transaction_active>(Thread* self, \
314      const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data)
315
316#define EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(_find_type, _field_type)  \
317    EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, false, false);  \
318    EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, true, false);  \
319    EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, false, true);  \
320    EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, true, true);
321
322// iput-XXX
323EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimBoolean)
324EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimByte)
325EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimChar)
326EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimShort)
327EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimInt)
328EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimLong)
329EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstanceObjectWrite, Primitive::kPrimNot)
330
331// sput-XXX
332EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimBoolean)
333EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimByte)
334EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimChar)
335EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimShort)
336EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimInt)
337EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimLong)
338EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticObjectWrite, Primitive::kPrimNot)
339
340#undef EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL
341#undef EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL
342
343template<Primitive::Type field_type, bool transaction_active>
344bool DoIPutQuick(const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) {
345  Object* obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
346  if (UNLIKELY(obj == nullptr)) {
347    // We lost the reference to the field index so we cannot get a more
348    // precised exception message.
349    ThrowNullPointerExceptionFromDexPC();
350    return false;
351  }
352  MemberOffset field_offset(inst->VRegC_22c());
353  const uint32_t vregA = inst->VRegA_22c(inst_data);
354  // Report this field modification to instrumentation if needed. Since we only have the offset of
355  // the field from the base of the object, we need to look for it first.
356  instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
357  if (UNLIKELY(instrumentation->HasFieldWriteListeners())) {
358    ArtField* f = ArtField::FindInstanceFieldWithOffset(obj->GetClass(),
359                                                        field_offset.Uint32Value());
360    DCHECK(f != nullptr);
361    DCHECK(!f->IsStatic());
362    JValue field_value = GetFieldValue<field_type>(shadow_frame, vregA);
363    instrumentation->FieldWriteEvent(Thread::Current(), obj, shadow_frame.GetMethod(),
364                                     shadow_frame.GetDexPC(), f, field_value);
365  }
366  // Note: iput-x-quick instructions are only for non-volatile fields.
367  switch (field_type) {
368    case Primitive::kPrimBoolean:
369      obj->SetFieldBoolean<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
370      break;
371    case Primitive::kPrimByte:
372      obj->SetFieldByte<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
373      break;
374    case Primitive::kPrimChar:
375      obj->SetFieldChar<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
376      break;
377    case Primitive::kPrimShort:
378      obj->SetFieldShort<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
379      break;
380    case Primitive::kPrimInt:
381      obj->SetField32<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
382      break;
383    case Primitive::kPrimLong:
384      obj->SetField64<transaction_active>(field_offset, shadow_frame.GetVRegLong(vregA));
385      break;
386    case Primitive::kPrimNot:
387      obj->SetFieldObject<transaction_active>(field_offset, shadow_frame.GetVRegReference(vregA));
388      break;
389    default:
390      LOG(FATAL) << "Unreachable: " << field_type;
391      UNREACHABLE();
392  }
393  return true;
394}
395
396// Explicitly instantiate all DoIPutQuick functions.
397#define EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, _transaction_active) \
398  template bool DoIPutQuick<_field_type, _transaction_active>(const ShadowFrame& shadow_frame, \
399                                                              const Instruction* inst, \
400                                                              uint16_t inst_data)
401
402#define EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(_field_type)   \
403  EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, false);     \
404  EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, true);
405
406EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimInt)      // iput-quick.
407EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimBoolean)  // iput-boolean-quick.
408EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimByte)     // iput-byte-quick.
409EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimChar)     // iput-char-quick.
410EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimShort)    // iput-short-quick.
411EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimLong)     // iput-wide-quick.
412EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimNot)      // iput-object-quick.
413#undef EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL
414#undef EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL
415
416uint32_t FindNextInstructionFollowingException(Thread* self,
417                                               ShadowFrame& shadow_frame,
418                                               uint32_t dex_pc,
419                                               const instrumentation::Instrumentation* instrumentation) {
420  self->VerifyStack();
421  StackHandleScope<3> hs(self);
422  Handle<mirror::Throwable> exception(hs.NewHandle(self->GetException()));
423  if (instrumentation->HasExceptionCaughtListeners()
424      && self->IsExceptionThrownByCurrentMethod(exception.Get())) {
425    instrumentation->ExceptionCaughtEvent(self, exception.Get());
426  }
427  bool clear_exception = false;
428  uint32_t found_dex_pc;
429  {
430    Handle<mirror::Class> exception_class(hs.NewHandle(exception->GetClass()));
431    Handle<mirror::ArtMethod> h_method(hs.NewHandle(shadow_frame.GetMethod()));
432    found_dex_pc = mirror::ArtMethod::FindCatchBlock(h_method, exception_class, dex_pc,
433                                                     &clear_exception);
434  }
435  if (found_dex_pc == DexFile::kDexNoIndex) {
436    // Exception is not caught by the current method. We will unwind to the
437    // caller. Notify any instrumentation listener.
438    instrumentation->MethodUnwindEvent(self, shadow_frame.GetThisObject(),
439                                       shadow_frame.GetMethod(), dex_pc);
440  } else {
441    // Exception is caught in the current method. We will jump to the found_dex_pc.
442    if (clear_exception) {
443      self->ClearException();
444    }
445  }
446  return found_dex_pc;
447}
448
449void UnexpectedOpcode(const Instruction* inst, const ShadowFrame& shadow_frame) {
450  LOG(FATAL) << "Unexpected instruction: "
451             << inst->DumpString(shadow_frame.GetMethod()->GetDexFile());
452  UNREACHABLE();
453}
454
455// Assign register 'src_reg' from shadow_frame to register 'dest_reg' into new_shadow_frame.
456static inline void AssignRegister(ShadowFrame* new_shadow_frame, const ShadowFrame& shadow_frame,
457                                  size_t dest_reg, size_t src_reg)
458    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
459  // If both register locations contains the same value, the register probably holds a reference.
460  // Uint required, so that sign extension does not make this wrong on 64b systems
461  uint32_t src_value = shadow_frame.GetVReg(src_reg);
462  mirror::Object* o = shadow_frame.GetVRegReference<kVerifyNone>(src_reg);
463  if (src_value == reinterpret_cast<uintptr_t>(o)) {
464    new_shadow_frame->SetVRegReference(dest_reg, o);
465  } else {
466    new_shadow_frame->SetVReg(dest_reg, src_value);
467  }
468}
469
470void AbortTransactionF(Thread* self, const char* fmt, ...) {
471  va_list args;
472  va_start(args, fmt);
473  AbortTransactionV(self, fmt, args);
474  va_end(args);
475}
476
477void AbortTransactionV(Thread* self, const char* fmt, va_list args) {
478  CHECK(Runtime::Current()->IsActiveTransaction());
479  // Constructs abort message.
480  std::string abort_msg;
481  StringAppendV(&abort_msg, fmt, args);
482  // Throws an exception so we can abort the transaction and rollback every change.
483  Runtime::Current()->AbortTransactionAndThrowAbortError(self, abort_msg);
484}
485
486template<bool is_range, bool do_assignability_check>
487bool DoCall(ArtMethod* called_method, Thread* self, ShadowFrame& shadow_frame,
488            const Instruction* inst, uint16_t inst_data, JValue* result) {
489  bool string_init = false;
490  // Replace calls to String.<init> with equivalent StringFactory call.
491  if (called_method->GetDeclaringClass()->IsStringClass() && called_method->IsConstructor()) {
492    ScopedObjectAccessUnchecked soa(self);
493    jmethodID mid = soa.EncodeMethod(called_method);
494    called_method = soa.DecodeMethod(WellKnownClasses::StringInitToStringFactoryMethodID(mid));
495    string_init = true;
496  }
497
498  // Compute method information.
499  const DexFile::CodeItem* code_item = called_method->GetCodeItem();
500  const uint16_t num_ins = (is_range) ? inst->VRegA_3rc(inst_data) : inst->VRegA_35c(inst_data);
501  uint16_t num_regs;
502  if (LIKELY(code_item != nullptr)) {
503    num_regs = code_item->registers_size_;
504  } else {
505    DCHECK(called_method->IsNative() || called_method->IsProxyMethod());
506    num_regs = num_ins;
507    if (string_init) {
508      // The new StringFactory call is static and has one fewer argument.
509      num_regs--;
510    }
511  }
512
513  // Allocate shadow frame on the stack.
514  const char* old_cause = self->StartAssertNoThreadSuspension("DoCall");
515  void* memory = alloca(ShadowFrame::ComputeSize(num_regs));
516  ShadowFrame* new_shadow_frame(ShadowFrame::Create(num_regs, &shadow_frame, called_method, 0,
517                                                    memory));
518
519  // Initialize new shadow frame.
520  size_t first_dest_reg = num_regs - num_ins;
521  if (do_assignability_check) {
522    // Slow path.
523    // We might need to do class loading, which incurs a thread state change to kNative. So
524    // register the shadow frame as under construction and allow suspension again.
525    self->SetShadowFrameUnderConstruction(new_shadow_frame);
526    self->EndAssertNoThreadSuspension(old_cause);
527
528    // We need to do runtime check on reference assignment. We need to load the shorty
529    // to get the exact type of each reference argument.
530    const DexFile::TypeList* params = new_shadow_frame->GetMethod()->GetParameterTypeList();
531    uint32_t shorty_len = 0;
532    const char* shorty = new_shadow_frame->GetMethod()->GetShorty(&shorty_len);
533
534    // TODO: find a cleaner way to separate non-range and range information without duplicating
535    //       code.
536    uint32_t arg[5];  // only used in invoke-XXX.
537    uint32_t vregC;   // only used in invoke-XXX-range.
538    if (is_range) {
539      vregC = inst->VRegC_3rc();
540    } else {
541      inst->GetVarArgs(arg, inst_data);
542    }
543
544    // Handle receiver apart since it's not part of the shorty.
545    size_t dest_reg = first_dest_reg;
546    size_t arg_offset = 0;
547    if (!new_shadow_frame->GetMethod()->IsStatic()) {
548      size_t receiver_reg = is_range ? vregC : arg[0];
549      new_shadow_frame->SetVRegReference(dest_reg, shadow_frame.GetVRegReference(receiver_reg));
550      ++dest_reg;
551      ++arg_offset;
552    } else if (string_init) {
553      // Skip the referrer for the new static StringFactory call.
554      ++dest_reg;
555      ++arg_offset;
556    }
557    for (uint32_t shorty_pos = 0; dest_reg < num_regs; ++shorty_pos, ++dest_reg, ++arg_offset) {
558      DCHECK_LT(shorty_pos + 1, shorty_len);
559      const size_t src_reg = (is_range) ? vregC + arg_offset : arg[arg_offset];
560      switch (shorty[shorty_pos + 1]) {
561        case 'L': {
562          Object* o = shadow_frame.GetVRegReference(src_reg);
563          if (do_assignability_check && o != nullptr) {
564            Class* arg_type =
565                new_shadow_frame->GetMethod()->GetClassFromTypeIndex(
566                    params->GetTypeItem(shorty_pos).type_idx_, true);
567            if (arg_type == nullptr) {
568              CHECK(self->IsExceptionPending());
569              return false;
570            }
571            if (!o->VerifierInstanceOf(arg_type)) {
572              // This should never happen.
573              std::string temp1, temp2;
574              self->ThrowNewExceptionF("Ljava/lang/VirtualMachineError;",
575                                       "Invoking %s with bad arg %d, type '%s' not instance of '%s'",
576                                       new_shadow_frame->GetMethod()->GetName(), shorty_pos,
577                                       o->GetClass()->GetDescriptor(&temp1),
578                                       arg_type->GetDescriptor(&temp2));
579              return false;
580            }
581          }
582          new_shadow_frame->SetVRegReference(dest_reg, o);
583          break;
584        }
585        case 'J': case 'D': {
586          uint64_t wide_value = (static_cast<uint64_t>(shadow_frame.GetVReg(src_reg + 1)) << 32) |
587                                static_cast<uint32_t>(shadow_frame.GetVReg(src_reg));
588          new_shadow_frame->SetVRegLong(dest_reg, wide_value);
589          ++dest_reg;
590          ++arg_offset;
591          break;
592        }
593        default:
594          new_shadow_frame->SetVReg(dest_reg, shadow_frame.GetVReg(src_reg));
595          break;
596      }
597    }
598    // We're done with the construction.
599    self->ClearShadowFrameUnderConstruction();
600  } else {
601    // Fast path: no extra checks.
602    if (is_range) {
603      uint16_t first_src_reg = inst->VRegC_3rc();
604      if (string_init) {
605        // Skip the referrer for the new static StringFactory call.
606        ++first_src_reg;
607        ++first_dest_reg;
608      }
609      for (size_t src_reg = first_src_reg, dest_reg = first_dest_reg; dest_reg < num_regs;
610          ++dest_reg, ++src_reg) {
611        AssignRegister(new_shadow_frame, shadow_frame, dest_reg, src_reg);
612      }
613    } else {
614      DCHECK_LE(num_ins, 5U);
615      uint16_t regList = inst->Fetch16(2);
616      uint16_t count = num_ins;
617      size_t arg_index = 0;
618      if (string_init) {
619        // Skip the referrer for the new static StringFactory call.
620        regList >>= 4;
621        ++arg_index;
622      }
623      if (count == 5) {
624        AssignRegister(new_shadow_frame, shadow_frame, first_dest_reg + 4U,
625                       (inst_data >> 8) & 0x0f);
626        --count;
627       }
628      for (; arg_index < count; ++arg_index, regList >>= 4) {
629        AssignRegister(new_shadow_frame, shadow_frame, first_dest_reg + arg_index, regList & 0x0f);
630      }
631    }
632    self->EndAssertNoThreadSuspension(old_cause);
633  }
634
635  // Do the call now.
636  if (LIKELY(Runtime::Current()->IsStarted())) {
637    if (kIsDebugBuild && new_shadow_frame->GetMethod()->GetEntryPointFromInterpreter() == nullptr) {
638      LOG(FATAL) << "Attempt to invoke non-executable method: "
639          << PrettyMethod(new_shadow_frame->GetMethod());
640      UNREACHABLE();
641    }
642    if (kIsDebugBuild && Runtime::Current()->GetInstrumentation()->IsForcedInterpretOnly() &&
643        !new_shadow_frame->GetMethod()->IsNative() &&
644        !new_shadow_frame->GetMethod()->IsProxyMethod() &&
645        new_shadow_frame->GetMethod()->GetEntryPointFromInterpreter()
646            == artInterpreterToCompiledCodeBridge) {
647      LOG(FATAL) << "Attempt to call compiled code when -Xint: "
648          << PrettyMethod(new_shadow_frame->GetMethod());
649      UNREACHABLE();
650    }
651    // Force the use of interpreter when it is required by the debugger.
652    mirror::EntryPointFromInterpreter* entry;
653    if (UNLIKELY(Dbg::IsForcedInterpreterNeededForCalling(self, new_shadow_frame->GetMethod()))) {
654      entry = &art::artInterpreterToInterpreterBridge;
655    } else {
656      entry = new_shadow_frame->GetMethod()->GetEntryPointFromInterpreter();
657    }
658    entry(self, code_item, new_shadow_frame, result);
659  } else {
660    UnstartedRuntimeInvoke(self, code_item, new_shadow_frame, result, first_dest_reg);
661  }
662
663  if (string_init && !self->IsExceptionPending()) {
664    // Set the new string result of the StringFactory.
665    uint32_t vregC = (is_range) ? inst->VRegC_3rc() : inst->VRegC_35c();
666    shadow_frame.SetVRegReference(vregC, result->GetL());
667    // Overwrite all potential copies of the original result of the new-instance of string with the
668    // new result of the StringFactory. Use the verifier to find this set of registers.
669    mirror::ArtMethod* method = shadow_frame.GetMethod();
670    MethodReference method_ref = method->ToMethodReference();
671    SafeMap<uint32_t, std::set<uint32_t>> string_init_map;
672    SafeMap<uint32_t, std::set<uint32_t>>* string_init_map_ptr;
673    MethodRefToStringInitRegMap& method_to_string_init_map = Runtime::Current()->GetStringInitMap();
674    auto it = method_to_string_init_map.find(method_ref);
675    if (it == method_to_string_init_map.end()) {
676      string_init_map = std::move(verifier::MethodVerifier::FindStringInitMap(method));
677      method_to_string_init_map.Overwrite(method_ref, string_init_map);
678      string_init_map_ptr = &string_init_map;
679    } else {
680      string_init_map_ptr = &it->second;
681    }
682    if (string_init_map_ptr->size() != 0) {
683      uint32_t dex_pc = shadow_frame.GetDexPC();
684      auto map_it = string_init_map_ptr->find(dex_pc);
685      if (map_it != string_init_map_ptr->end()) {
686        const std::set<uint32_t>& reg_set = map_it->second;
687        for (auto set_it = reg_set.begin(); set_it != reg_set.end(); ++set_it) {
688          shadow_frame.SetVRegReference(*set_it, result->GetL());
689        }
690      }
691    }
692  }
693
694  return !self->IsExceptionPending();
695}
696
697template <bool is_range, bool do_access_check, bool transaction_active>
698bool DoFilledNewArray(const Instruction* inst, const ShadowFrame& shadow_frame,
699                      Thread* self, JValue* result) {
700  DCHECK(inst->Opcode() == Instruction::FILLED_NEW_ARRAY ||
701         inst->Opcode() == Instruction::FILLED_NEW_ARRAY_RANGE);
702  const int32_t length = is_range ? inst->VRegA_3rc() : inst->VRegA_35c();
703  if (!is_range) {
704    // Checks FILLED_NEW_ARRAY's length does not exceed 5 arguments.
705    CHECK_LE(length, 5);
706  }
707  if (UNLIKELY(length < 0)) {
708    ThrowNegativeArraySizeException(length);
709    return false;
710  }
711  uint16_t type_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
712  Class* arrayClass = ResolveVerifyAndClinit(type_idx, shadow_frame.GetMethod(),
713                                             self, false, do_access_check);
714  if (UNLIKELY(arrayClass == nullptr)) {
715    DCHECK(self->IsExceptionPending());
716    return false;
717  }
718  CHECK(arrayClass->IsArrayClass());
719  Class* componentClass = arrayClass->GetComponentType();
720  if (UNLIKELY(componentClass->IsPrimitive() && !componentClass->IsPrimitiveInt())) {
721    if (componentClass->IsPrimitiveLong() || componentClass->IsPrimitiveDouble()) {
722      ThrowRuntimeException("Bad filled array request for type %s",
723                            PrettyDescriptor(componentClass).c_str());
724    } else {
725      self->ThrowNewExceptionF("Ljava/lang/InternalError;",
726                               "Found type %s; filled-new-array not implemented for anything but 'int'",
727                               PrettyDescriptor(componentClass).c_str());
728    }
729    return false;
730  }
731  Object* newArray = Array::Alloc<true>(self, arrayClass, length,
732                                        arrayClass->GetComponentSizeShift(),
733                                        Runtime::Current()->GetHeap()->GetCurrentAllocator());
734  if (UNLIKELY(newArray == nullptr)) {
735    DCHECK(self->IsExceptionPending());
736    return false;
737  }
738  uint32_t arg[5];  // only used in filled-new-array.
739  uint32_t vregC;   // only used in filled-new-array-range.
740  if (is_range) {
741    vregC = inst->VRegC_3rc();
742  } else {
743    inst->GetVarArgs(arg);
744  }
745  const bool is_primitive_int_component = componentClass->IsPrimitiveInt();
746  for (int32_t i = 0; i < length; ++i) {
747    size_t src_reg = is_range ? vregC + i : arg[i];
748    if (is_primitive_int_component) {
749      newArray->AsIntArray()->SetWithoutChecks<transaction_active>(i, shadow_frame.GetVReg(src_reg));
750    } else {
751      newArray->AsObjectArray<Object>()->SetWithoutChecks<transaction_active>(i, shadow_frame.GetVRegReference(src_reg));
752    }
753  }
754
755  result->SetL(newArray);
756  return true;
757}
758
759// TODO fix thread analysis: should be SHARED_LOCKS_REQUIRED(Locks::mutator_lock_).
760template<typename T>
761static void RecordArrayElementsInTransactionImpl(mirror::PrimitiveArray<T>* array, int32_t count)
762    NO_THREAD_SAFETY_ANALYSIS {
763  Runtime* runtime = Runtime::Current();
764  for (int32_t i = 0; i < count; ++i) {
765    runtime->RecordWriteArray(array, i, array->GetWithoutChecks(i));
766  }
767}
768
769void RecordArrayElementsInTransaction(mirror::Array* array, int32_t count)
770    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
771  DCHECK(Runtime::Current()->IsActiveTransaction());
772  DCHECK(array != nullptr);
773  DCHECK_LE(count, array->GetLength());
774  Primitive::Type primitive_component_type = array->GetClass()->GetComponentType()->GetPrimitiveType();
775  switch (primitive_component_type) {
776    case Primitive::kPrimBoolean:
777      RecordArrayElementsInTransactionImpl(array->AsBooleanArray(), count);
778      break;
779    case Primitive::kPrimByte:
780      RecordArrayElementsInTransactionImpl(array->AsByteArray(), count);
781      break;
782    case Primitive::kPrimChar:
783      RecordArrayElementsInTransactionImpl(array->AsCharArray(), count);
784      break;
785    case Primitive::kPrimShort:
786      RecordArrayElementsInTransactionImpl(array->AsShortArray(), count);
787      break;
788    case Primitive::kPrimInt:
789    case Primitive::kPrimFloat:
790      RecordArrayElementsInTransactionImpl(array->AsIntArray(), count);
791      break;
792    case Primitive::kPrimLong:
793    case Primitive::kPrimDouble:
794      RecordArrayElementsInTransactionImpl(array->AsLongArray(), count);
795      break;
796    default:
797      LOG(FATAL) << "Unsupported primitive type " << primitive_component_type
798                 << " in fill-array-data";
799      break;
800  }
801}
802
803// Explicit DoCall template function declarations.
804#define EXPLICIT_DO_CALL_TEMPLATE_DECL(_is_range, _do_assignability_check)                      \
805  template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)                                          \
806  bool DoCall<_is_range, _do_assignability_check>(ArtMethod* method, Thread* self,              \
807                                                  ShadowFrame& shadow_frame,                    \
808                                                  const Instruction* inst, uint16_t inst_data,  \
809                                                  JValue* result)
810EXPLICIT_DO_CALL_TEMPLATE_DECL(false, false);
811EXPLICIT_DO_CALL_TEMPLATE_DECL(false, true);
812EXPLICIT_DO_CALL_TEMPLATE_DECL(true, false);
813EXPLICIT_DO_CALL_TEMPLATE_DECL(true, true);
814#undef EXPLICIT_DO_CALL_TEMPLATE_DECL
815
816// Explicit DoFilledNewArray template function declarations.
817#define EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(_is_range_, _check, _transaction_active)       \
818  template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)                                            \
819  bool DoFilledNewArray<_is_range_, _check, _transaction_active>(const Instruction* inst,         \
820                                                                 const ShadowFrame& shadow_frame, \
821                                                                 Thread* self, JValue* result)
822#define EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(_transaction_active)       \
823  EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, false, _transaction_active);  \
824  EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, true, _transaction_active);   \
825  EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, false, _transaction_active);   \
826  EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, true, _transaction_active)
827EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(false);
828EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(true);
829#undef EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL
830#undef EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL
831
832}  // namespace interpreter
833}  // namespace art
834