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