Home | History | Annotate | Download | only in interpreter
      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.h"
     18 
     19 #include <limits>
     20 #include <string_view>
     21 
     22 #include "common_dex_operations.h"
     23 #include "common_throws.h"
     24 #include "dex/dex_file_types.h"
     25 #include "interpreter_common.h"
     26 #include "interpreter_mterp_impl.h"
     27 #include "interpreter_switch_impl.h"
     28 #include "jit/jit.h"
     29 #include "jit/jit_code_cache.h"
     30 #include "jvalue-inl.h"
     31 #include "mirror/string-inl.h"
     32 #include "mterp/mterp.h"
     33 #include "nativehelper/scoped_local_ref.h"
     34 #include "scoped_thread_state_change-inl.h"
     35 #include "shadow_frame-inl.h"
     36 #include "stack.h"
     37 #include "thread-inl.h"
     38 #include "unstarted_runtime.h"
     39 
     40 namespace art {
     41 namespace interpreter {
     42 
     43 ALWAYS_INLINE static ObjPtr<mirror::Object> ObjArg(uint32_t arg)
     44     REQUIRES_SHARED(Locks::mutator_lock_) {
     45   return reinterpret_cast<mirror::Object*>(arg);
     46 }
     47 
     48 static void InterpreterJni(Thread* self,
     49                            ArtMethod* method,
     50                            std::string_view shorty,
     51                            ObjPtr<mirror::Object> receiver,
     52                            uint32_t* args,
     53                            JValue* result)
     54     REQUIRES_SHARED(Locks::mutator_lock_) {
     55   // TODO: The following enters JNI code using a typedef-ed function rather than the JNI compiler,
     56   //       it should be removed and JNI compiled stubs used instead.
     57   ScopedObjectAccessUnchecked soa(self);
     58   if (method->IsStatic()) {
     59     if (shorty == "L") {
     60       using fntype = jobject(JNIEnv*, jclass);
     61       fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
     62       ScopedLocalRef<jclass> klass(soa.Env(),
     63                                    soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
     64       jobject jresult;
     65       {
     66         ScopedThreadStateChange tsc(self, kNative);
     67         jresult = fn(soa.Env(), klass.get());
     68       }
     69       result->SetL(soa.Decode<mirror::Object>(jresult));
     70     } else if (shorty == "V") {
     71       using fntype = void(JNIEnv*, jclass);
     72       fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
     73       ScopedLocalRef<jclass> klass(soa.Env(),
     74                                    soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
     75       ScopedThreadStateChange tsc(self, kNative);
     76       fn(soa.Env(), klass.get());
     77     } else if (shorty == "Z") {
     78       using fntype = jboolean(JNIEnv*, jclass);
     79       fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
     80       ScopedLocalRef<jclass> klass(soa.Env(),
     81                                    soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
     82       ScopedThreadStateChange tsc(self, kNative);
     83       result->SetZ(fn(soa.Env(), klass.get()));
     84     } else if (shorty == "BI") {
     85       using fntype = jbyte(JNIEnv*, jclass, jint);
     86       fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
     87       ScopedLocalRef<jclass> klass(soa.Env(),
     88                                    soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
     89       ScopedThreadStateChange tsc(self, kNative);
     90       result->SetB(fn(soa.Env(), klass.get(), args[0]));
     91     } else if (shorty == "II") {
     92       using fntype = jint(JNIEnv*, jclass, jint);
     93       fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
     94       ScopedLocalRef<jclass> klass(soa.Env(),
     95                                    soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
     96       ScopedThreadStateChange tsc(self, kNative);
     97       result->SetI(fn(soa.Env(), klass.get(), args[0]));
     98     } else if (shorty == "LL") {
     99       using fntype = jobject(JNIEnv*, jclass, jobject);
    100       fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
    101       ScopedLocalRef<jclass> klass(soa.Env(),
    102                                    soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
    103       ScopedLocalRef<jobject> arg0(soa.Env(),
    104                                    soa.AddLocalReference<jobject>(ObjArg(args[0])));
    105       jobject jresult;
    106       {
    107         ScopedThreadStateChange tsc(self, kNative);
    108         jresult = fn(soa.Env(), klass.get(), arg0.get());
    109       }
    110       result->SetL(soa.Decode<mirror::Object>(jresult));
    111     } else if (shorty == "IIZ") {
    112       using fntype = jint(JNIEnv*, jclass, jint, jboolean);
    113       fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
    114       ScopedLocalRef<jclass> klass(soa.Env(),
    115                                    soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
    116       ScopedThreadStateChange tsc(self, kNative);
    117       result->SetI(fn(soa.Env(), klass.get(), args[0], args[1]));
    118     } else if (shorty == "ILI") {
    119       using fntype = jint(JNIEnv*, jclass, jobject, jint);
    120       fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(
    121           method->GetEntryPointFromJni()));
    122       ScopedLocalRef<jclass> klass(soa.Env(),
    123                                    soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
    124       ScopedLocalRef<jobject> arg0(soa.Env(),
    125                                    soa.AddLocalReference<jobject>(ObjArg(args[0])));
    126       ScopedThreadStateChange tsc(self, kNative);
    127       result->SetI(fn(soa.Env(), klass.get(), arg0.get(), args[1]));
    128     } else if (shorty == "SIZ") {
    129       using fntype = jshort(JNIEnv*, jclass, jint, jboolean);
    130       fntype* const fn =
    131           reinterpret_cast<fntype*>(const_cast<void*>(method->GetEntryPointFromJni()));
    132       ScopedLocalRef<jclass> klass(soa.Env(),
    133                                    soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
    134       ScopedThreadStateChange tsc(self, kNative);
    135       result->SetS(fn(soa.Env(), klass.get(), args[0], args[1]));
    136     } else if (shorty == "VIZ") {
    137       using fntype = void(JNIEnv*, jclass, jint, jboolean);
    138       fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
    139       ScopedLocalRef<jclass> klass(soa.Env(),
    140                                    soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
    141       ScopedThreadStateChange tsc(self, kNative);
    142       fn(soa.Env(), klass.get(), args[0], args[1]);
    143     } else if (shorty == "ZLL") {
    144       using fntype = jboolean(JNIEnv*, jclass, jobject, jobject);
    145       fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
    146       ScopedLocalRef<jclass> klass(soa.Env(),
    147                                    soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
    148       ScopedLocalRef<jobject> arg0(soa.Env(),
    149                                    soa.AddLocalReference<jobject>(ObjArg(args[0])));
    150       ScopedLocalRef<jobject> arg1(soa.Env(),
    151                                    soa.AddLocalReference<jobject>(ObjArg(args[1])));
    152       ScopedThreadStateChange tsc(self, kNative);
    153       result->SetZ(fn(soa.Env(), klass.get(), arg0.get(), arg1.get()));
    154     } else if (shorty == "ZILL") {
    155       using fntype = jboolean(JNIEnv*, jclass, jint, jobject, jobject);
    156       fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
    157       ScopedLocalRef<jclass> klass(soa.Env(),
    158                                    soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
    159       ScopedLocalRef<jobject> arg1(soa.Env(),
    160                                    soa.AddLocalReference<jobject>(ObjArg(args[1])));
    161       ScopedLocalRef<jobject> arg2(soa.Env(),
    162                                    soa.AddLocalReference<jobject>(ObjArg(args[2])));
    163       ScopedThreadStateChange tsc(self, kNative);
    164       result->SetZ(fn(soa.Env(), klass.get(), args[0], arg1.get(), arg2.get()));
    165     } else if (shorty == "VILII") {
    166       using fntype = void(JNIEnv*, jclass, jint, jobject, jint, jint);
    167       fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
    168       ScopedLocalRef<jclass> klass(soa.Env(),
    169                                    soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
    170       ScopedLocalRef<jobject> arg1(soa.Env(),
    171                                    soa.AddLocalReference<jobject>(ObjArg(args[1])));
    172       ScopedThreadStateChange tsc(self, kNative);
    173       fn(soa.Env(), klass.get(), args[0], arg1.get(), args[2], args[3]);
    174     } else if (shorty == "VLILII") {
    175       using fntype = void(JNIEnv*, jclass, jobject, jint, jobject, jint, jint);
    176       fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
    177       ScopedLocalRef<jclass> klass(soa.Env(),
    178                                    soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
    179       ScopedLocalRef<jobject> arg0(soa.Env(),
    180                                    soa.AddLocalReference<jobject>(ObjArg(args[0])));
    181       ScopedLocalRef<jobject> arg2(soa.Env(),
    182                                    soa.AddLocalReference<jobject>(ObjArg(args[2])));
    183       ScopedThreadStateChange tsc(self, kNative);
    184       fn(soa.Env(), klass.get(), arg0.get(), args[1], arg2.get(), args[3], args[4]);
    185     } else {
    186       LOG(FATAL) << "Do something with static native method: " << method->PrettyMethod()
    187           << " shorty: " << shorty;
    188     }
    189   } else {
    190     if (shorty == "L") {
    191       using fntype = jobject(JNIEnv*, jobject);
    192       fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
    193       ScopedLocalRef<jobject> rcvr(soa.Env(),
    194                                    soa.AddLocalReference<jobject>(receiver));
    195       jobject jresult;
    196       {
    197         ScopedThreadStateChange tsc(self, kNative);
    198         jresult = fn(soa.Env(), rcvr.get());
    199       }
    200       result->SetL(soa.Decode<mirror::Object>(jresult));
    201     } else if (shorty == "V") {
    202       using fntype = void(JNIEnv*, jobject);
    203       fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
    204       ScopedLocalRef<jobject> rcvr(soa.Env(),
    205                                    soa.AddLocalReference<jobject>(receiver));
    206       ScopedThreadStateChange tsc(self, kNative);
    207       fn(soa.Env(), rcvr.get());
    208     } else if (shorty == "LL") {
    209       using fntype = jobject(JNIEnv*, jobject, jobject);
    210       fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
    211       ScopedLocalRef<jobject> rcvr(soa.Env(),
    212                                    soa.AddLocalReference<jobject>(receiver));
    213       ScopedLocalRef<jobject> arg0(soa.Env(),
    214                                    soa.AddLocalReference<jobject>(ObjArg(args[0])));
    215       jobject jresult;
    216       {
    217         ScopedThreadStateChange tsc(self, kNative);
    218         jresult = fn(soa.Env(), rcvr.get(), arg0.get());
    219       }
    220       result->SetL(soa.Decode<mirror::Object>(jresult));
    221       ScopedThreadStateChange tsc(self, kNative);
    222     } else if (shorty == "III") {
    223       using fntype = jint(JNIEnv*, jobject, jint, jint);
    224       fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
    225       ScopedLocalRef<jobject> rcvr(soa.Env(),
    226                                    soa.AddLocalReference<jobject>(receiver));
    227       ScopedThreadStateChange tsc(self, kNative);
    228       result->SetI(fn(soa.Env(), rcvr.get(), args[0], args[1]));
    229     } else {
    230       LOG(FATAL) << "Do something with native method: " << method->PrettyMethod()
    231           << " shorty: " << shorty;
    232     }
    233   }
    234 }
    235 
    236 enum InterpreterImplKind {
    237   kSwitchImplKind,        // Switch-based interpreter implementation.
    238   kMterpImplKind          // Assembly interpreter
    239 };
    240 
    241 #if ART_USE_CXX_INTERPRETER
    242 static constexpr InterpreterImplKind kInterpreterImplKind = kSwitchImplKind;
    243 #else
    244 static constexpr InterpreterImplKind kInterpreterImplKind = kMterpImplKind;
    245 #endif
    246 
    247 static inline JValue Execute(
    248     Thread* self,
    249     const CodeItemDataAccessor& accessor,
    250     ShadowFrame& shadow_frame,
    251     JValue result_register,
    252     bool stay_in_interpreter = false,
    253     bool from_deoptimize = false) REQUIRES_SHARED(Locks::mutator_lock_) {
    254   DCHECK(!shadow_frame.GetMethod()->IsAbstract());
    255   DCHECK(!shadow_frame.GetMethod()->IsNative());
    256 
    257   // Check that we are using the right interpreter.
    258   if (kIsDebugBuild && self->UseMterp() != CanUseMterp()) {
    259     // The flag might be currently being updated on all threads. Retry with lock.
    260     MutexLock tll_mu(self, *Locks::thread_list_lock_);
    261     DCHECK_EQ(self->UseMterp(), CanUseMterp());
    262   }
    263 
    264   if (LIKELY(!from_deoptimize)) {  // Entering the method, but not via deoptimization.
    265     if (kIsDebugBuild) {
    266       CHECK_EQ(shadow_frame.GetDexPC(), 0u);
    267       self->AssertNoPendingException();
    268     }
    269     instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
    270     ArtMethod *method = shadow_frame.GetMethod();
    271 
    272     if (UNLIKELY(instrumentation->HasMethodEntryListeners())) {
    273       instrumentation->MethodEnterEvent(self,
    274                                         shadow_frame.GetThisObject(accessor.InsSize()),
    275                                         method,
    276                                         0);
    277       if (UNLIKELY(shadow_frame.GetForcePopFrame())) {
    278         // The caller will retry this invoke. Just return immediately without any value.
    279         DCHECK(Runtime::Current()->AreNonStandardExitsEnabled());
    280         DCHECK(PrevFrameWillRetry(self, shadow_frame));
    281         return JValue();
    282       }
    283       if (UNLIKELY(self->IsExceptionPending())) {
    284         instrumentation->MethodUnwindEvent(self,
    285                                            shadow_frame.GetThisObject(accessor.InsSize()),
    286                                            method,
    287                                            0);
    288         return JValue();
    289       }
    290     }
    291 
    292     if (!stay_in_interpreter && !self->IsForceInterpreter()) {
    293       jit::Jit* jit = Runtime::Current()->GetJit();
    294       if (jit != nullptr) {
    295         jit->MethodEntered(self, shadow_frame.GetMethod());
    296         if (jit->CanInvokeCompiledCode(method)) {
    297           JValue result;
    298 
    299           // Pop the shadow frame before calling into compiled code.
    300           self->PopShadowFrame();
    301           // Calculate the offset of the first input reg. The input registers are in the high regs.
    302           // It's ok to access the code item here since JIT code will have been touched by the
    303           // interpreter and compiler already.
    304           uint16_t arg_offset = accessor.RegistersSize() - accessor.InsSize();
    305           ArtInterpreterToCompiledCodeBridge(self, nullptr, &shadow_frame, arg_offset, &result);
    306           // Push the shadow frame back as the caller will expect it.
    307           self->PushShadowFrame(&shadow_frame);
    308 
    309           return result;
    310         }
    311       }
    312     }
    313   }
    314 
    315   ArtMethod* method = shadow_frame.GetMethod();
    316 
    317   DCheckStaticState(self, method);
    318 
    319   // Lock counting is a special version of accessibility checks, and for simplicity and
    320   // reduction of template parameters, we gate it behind access-checks mode.
    321   DCHECK(!method->SkipAccessChecks() || !method->MustCountLocks());
    322 
    323   bool transaction_active = Runtime::Current()->IsActiveTransaction();
    324   if (LIKELY(method->SkipAccessChecks())) {
    325     // Enter the "without access check" interpreter.
    326     if (kInterpreterImplKind == kMterpImplKind) {
    327       if (transaction_active) {
    328         // No Mterp variant - just use the switch interpreter.
    329         return ExecuteSwitchImpl<false, true>(self, accessor, shadow_frame, result_register,
    330                                               false);
    331       } else if (UNLIKELY(!Runtime::Current()->IsStarted())) {
    332         return ExecuteSwitchImpl<false, false>(self, accessor, shadow_frame, result_register,
    333                                                false);
    334       } else {
    335         while (true) {
    336           // Mterp does not support all instrumentation/debugging.
    337           if (!self->UseMterp()) {
    338             return ExecuteSwitchImpl<false, false>(self, accessor, shadow_frame, result_register,
    339                                                    false);
    340           }
    341           bool returned = ExecuteMterpImpl(self,
    342                                            accessor.Insns(),
    343                                            &shadow_frame,
    344                                            &result_register);
    345           if (returned) {
    346             return result_register;
    347           } else {
    348             // Mterp didn't like that instruction.  Single-step it with the reference interpreter.
    349             result_register = ExecuteSwitchImpl<false, false>(self, accessor, shadow_frame,
    350                                                               result_register, true);
    351             if (shadow_frame.GetDexPC() == dex::kDexNoIndex) {
    352               // Single-stepped a return or an exception not handled locally.  Return to caller.
    353               return result_register;
    354             }
    355           }
    356         }
    357       }
    358     } else {
    359       DCHECK_EQ(kInterpreterImplKind, kSwitchImplKind);
    360       if (transaction_active) {
    361         return ExecuteSwitchImpl<false, true>(self, accessor, shadow_frame, result_register,
    362                                               false);
    363       } else {
    364         return ExecuteSwitchImpl<false, false>(self, accessor, shadow_frame, result_register,
    365                                                false);
    366       }
    367     }
    368   } else {
    369     // Enter the "with access check" interpreter.
    370 
    371     // The boot classpath should really not have to run access checks.
    372     DCHECK(method->GetDeclaringClass()->GetClassLoader() != nullptr
    373            || Runtime::Current()->IsVerificationSoftFail()
    374            || Runtime::Current()->IsAotCompiler())
    375         << method->PrettyMethod();
    376 
    377     if (kInterpreterImplKind == kMterpImplKind) {
    378       // No access check variants for Mterp.  Just use the switch version.
    379       if (transaction_active) {
    380         return ExecuteSwitchImpl<true, true>(self, accessor, shadow_frame, result_register,
    381                                              false);
    382       } else {
    383         return ExecuteSwitchImpl<true, false>(self, accessor, shadow_frame, result_register,
    384                                               false);
    385       }
    386     } else {
    387       DCHECK_EQ(kInterpreterImplKind, kSwitchImplKind);
    388       if (transaction_active) {
    389         return ExecuteSwitchImpl<true, true>(self, accessor, shadow_frame, result_register,
    390                                              false);
    391       } else {
    392         return ExecuteSwitchImpl<true, false>(self, accessor, shadow_frame, result_register,
    393                                               false);
    394       }
    395     }
    396   }
    397 }
    398 
    399 void EnterInterpreterFromInvoke(Thread* self,
    400                                 ArtMethod* method,
    401                                 ObjPtr<mirror::Object> receiver,
    402                                 uint32_t* args,
    403                                 JValue* result,
    404                                 bool stay_in_interpreter) {
    405   DCHECK_EQ(self, Thread::Current());
    406   bool implicit_check = !Runtime::Current()->ExplicitStackOverflowChecks();
    407   if (UNLIKELY(__builtin_frame_address(0) < self->GetStackEndForInterpreter(implicit_check))) {
    408     ThrowStackOverflowError(self);
    409     return;
    410   }
    411 
    412   // This can happen if we are in forced interpreter mode and an obsolete method is called using
    413   // reflection.
    414   if (UNLIKELY(method->IsObsolete())) {
    415     ThrowInternalError("Attempting to invoke obsolete version of '%s'.",
    416                        method->PrettyMethod().c_str());
    417     return;
    418   }
    419 
    420   const char* old_cause = self->StartAssertNoThreadSuspension("EnterInterpreterFromInvoke");
    421   CodeItemDataAccessor accessor(method->DexInstructionData());
    422   uint16_t num_regs;
    423   uint16_t num_ins;
    424   if (accessor.HasCodeItem()) {
    425     num_regs =  accessor.RegistersSize();
    426     num_ins = accessor.InsSize();
    427   } else if (!method->IsInvokable()) {
    428     self->EndAssertNoThreadSuspension(old_cause);
    429     method->ThrowInvocationTimeError();
    430     return;
    431   } else {
    432     DCHECK(method->IsNative());
    433     num_regs = num_ins = ArtMethod::NumArgRegisters(method->GetShorty());
    434     if (!method->IsStatic()) {
    435       num_regs++;
    436       num_ins++;
    437     }
    438   }
    439   // Set up shadow frame with matching number of reference slots to vregs.
    440   ShadowFrame* last_shadow_frame = self->GetManagedStack()->GetTopShadowFrame();
    441   ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
    442       CREATE_SHADOW_FRAME(num_regs, last_shadow_frame, method, /* dex pc */ 0);
    443   ShadowFrame* shadow_frame = shadow_frame_unique_ptr.get();
    444   self->PushShadowFrame(shadow_frame);
    445 
    446   size_t cur_reg = num_regs - num_ins;
    447   if (!method->IsStatic()) {
    448     CHECK(receiver != nullptr);
    449     shadow_frame->SetVRegReference(cur_reg, receiver);
    450     ++cur_reg;
    451   }
    452   uint32_t shorty_len = 0;
    453   const char* shorty = method->GetShorty(&shorty_len);
    454   for (size_t shorty_pos = 0, arg_pos = 0; cur_reg < num_regs; ++shorty_pos, ++arg_pos, cur_reg++) {
    455     DCHECK_LT(shorty_pos + 1, shorty_len);
    456     switch (shorty[shorty_pos + 1]) {
    457       case 'L': {
    458         ObjPtr<mirror::Object> o =
    459             reinterpret_cast<StackReference<mirror::Object>*>(&args[arg_pos])->AsMirrorPtr();
    460         shadow_frame->SetVRegReference(cur_reg, o);
    461         break;
    462       }
    463       case 'J': case 'D': {
    464         uint64_t wide_value = (static_cast<uint64_t>(args[arg_pos + 1]) << 32) | args[arg_pos];
    465         shadow_frame->SetVRegLong(cur_reg, wide_value);
    466         cur_reg++;
    467         arg_pos++;
    468         break;
    469       }
    470       default:
    471         shadow_frame->SetVReg(cur_reg, args[arg_pos]);
    472         break;
    473     }
    474   }
    475   self->EndAssertNoThreadSuspension(old_cause);
    476   // Do this after populating the shadow frame in case EnsureInitialized causes a GC.
    477   if (method->IsStatic() && UNLIKELY(!method->GetDeclaringClass()->IsInitialized())) {
    478     ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
    479     StackHandleScope<1> hs(self);
    480     Handle<mirror::Class> h_class(hs.NewHandle(method->GetDeclaringClass()));
    481     if (UNLIKELY(!class_linker->EnsureInitialized(self, h_class, true, true))) {
    482       CHECK(self->IsExceptionPending());
    483       self->PopShadowFrame();
    484       return;
    485     }
    486   }
    487   if (LIKELY(!method->IsNative())) {
    488     JValue r = Execute(self, accessor, *shadow_frame, JValue(), stay_in_interpreter);
    489     if (result != nullptr) {
    490       *result = r;
    491     }
    492   } else {
    493     // We don't expect to be asked to interpret native code (which is entered via a JNI compiler
    494     // generated stub) except during testing and image writing.
    495     // Update args to be the args in the shadow frame since the input ones could hold stale
    496     // references pointers due to moving GC.
    497     args = shadow_frame->GetVRegArgs(method->IsStatic() ? 0 : 1);
    498     if (!Runtime::Current()->IsStarted()) {
    499       UnstartedRuntime::Jni(self, method, receiver.Ptr(), args, result);
    500     } else {
    501       InterpreterJni(self, method, shorty, receiver, args, result);
    502     }
    503   }
    504   self->PopShadowFrame();
    505 }
    506 
    507 static int16_t GetReceiverRegisterForStringInit(const Instruction* instr) {
    508   DCHECK(instr->Opcode() == Instruction::INVOKE_DIRECT_RANGE ||
    509          instr->Opcode() == Instruction::INVOKE_DIRECT);
    510   return (instr->Opcode() == Instruction::INVOKE_DIRECT_RANGE) ?
    511       instr->VRegC_3rc() : instr->VRegC_35c();
    512 }
    513 
    514 void EnterInterpreterFromDeoptimize(Thread* self,
    515                                     ShadowFrame* shadow_frame,
    516                                     JValue* ret_val,
    517                                     bool from_code,
    518                                     DeoptimizationMethodType deopt_method_type)
    519     REQUIRES_SHARED(Locks::mutator_lock_) {
    520   JValue value;
    521   // Set value to last known result in case the shadow frame chain is empty.
    522   value.SetJ(ret_val->GetJ());
    523   // How many frames we have executed.
    524   size_t frame_cnt = 0;
    525   while (shadow_frame != nullptr) {
    526     // We do not want to recover lock state for lock counting when deoptimizing. Currently,
    527     // the compiler should not have compiled a method that failed structured-locking checks.
    528     DCHECK(!shadow_frame->GetMethod()->MustCountLocks());
    529 
    530     self->SetTopOfShadowStack(shadow_frame);
    531     CodeItemDataAccessor accessor(shadow_frame->GetMethod()->DexInstructionData());
    532     const uint32_t dex_pc = shadow_frame->GetDexPC();
    533     uint32_t new_dex_pc = dex_pc;
    534     if (UNLIKELY(self->IsExceptionPending())) {
    535       // If we deoptimize from the QuickExceptionHandler, we already reported the exception to
    536       // the instrumentation. To prevent from reporting it a second time, we simply pass a
    537       // null Instrumentation*.
    538       const instrumentation::Instrumentation* const instrumentation =
    539           frame_cnt == 0 ? nullptr : Runtime::Current()->GetInstrumentation();
    540       new_dex_pc = MoveToExceptionHandler(
    541           self, *shadow_frame, instrumentation) ? shadow_frame->GetDexPC() : dex::kDexNoIndex;
    542     } else if (!from_code) {
    543       // Deoptimization is not called from code directly.
    544       const Instruction* instr = &accessor.InstructionAt(dex_pc);
    545       if (deopt_method_type == DeoptimizationMethodType::kKeepDexPc ||
    546           shadow_frame->GetForceRetryInstruction()) {
    547         DCHECK(frame_cnt == 0 || (frame_cnt == 1 && shadow_frame->GetForceRetryInstruction()))
    548             << "frame_cnt: " << frame_cnt
    549             << " force-retry: " << shadow_frame->GetForceRetryInstruction();
    550         // Need to re-execute the dex instruction.
    551         // (1) An invocation might be split into class initialization and invoke.
    552         //     In this case, the invoke should not be skipped.
    553         // (2) A suspend check should also execute the dex instruction at the
    554         //     corresponding dex pc.
    555         // If the ForceRetryInstruction bit is set this must be the second frame (the first being
    556         // the one that is being popped).
    557         DCHECK_EQ(new_dex_pc, dex_pc);
    558         shadow_frame->SetForceRetryInstruction(false);
    559       } else if (instr->Opcode() == Instruction::MONITOR_ENTER ||
    560                  instr->Opcode() == Instruction::MONITOR_EXIT) {
    561         DCHECK(deopt_method_type == DeoptimizationMethodType::kDefault);
    562         DCHECK_EQ(frame_cnt, 0u);
    563         // Non-idempotent dex instruction should not be re-executed.
    564         // On the other hand, if a MONITOR_ENTER is at the dex_pc of a suspend
    565         // check, that MONITOR_ENTER should be executed. That case is handled
    566         // above.
    567         new_dex_pc = dex_pc + instr->SizeInCodeUnits();
    568       } else if (instr->IsInvoke()) {
    569         DCHECK(deopt_method_type == DeoptimizationMethodType::kDefault);
    570         if (IsStringInit(instr, shadow_frame->GetMethod())) {
    571           uint16_t this_obj_vreg = GetReceiverRegisterForStringInit(instr);
    572           // Move the StringFactory.newStringFromChars() result into the register representing
    573           // "this object" when invoking the string constructor in the original dex instruction.
    574           // Also move the result into all aliases.
    575           DCHECK(value.GetL()->IsString());
    576           SetStringInitValueToAllAliases(shadow_frame, this_obj_vreg, value);
    577           // Calling string constructor in the original dex code doesn't generate a result value.
    578           value.SetJ(0);
    579         }
    580         new_dex_pc = dex_pc + instr->SizeInCodeUnits();
    581       } else if (instr->Opcode() == Instruction::NEW_INSTANCE) {
    582         // A NEW_INSTANCE is simply re-executed, including
    583         // "new-instance String" which is compiled into a call into
    584         // StringFactory.newEmptyString().
    585         DCHECK_EQ(new_dex_pc, dex_pc);
    586       } else {
    587         DCHECK(deopt_method_type == DeoptimizationMethodType::kDefault);
    588         DCHECK_EQ(frame_cnt, 0u);
    589         // By default, we re-execute the dex instruction since if they are not
    590         // an invoke, so that we don't have to decode the dex instruction to move
    591         // result into the right vreg. All slow paths have been audited to be
    592         // idempotent except monitor-enter/exit and invocation stubs.
    593         // TODO: move result and advance dex pc. That also requires that we
    594         // can tell the return type of a runtime method, possibly by decoding
    595         // the dex instruction at the caller.
    596         DCHECK_EQ(new_dex_pc, dex_pc);
    597       }
    598     } else {
    599       // Nothing to do, the dex_pc is the one at which the code requested
    600       // the deoptimization.
    601       DCHECK_EQ(frame_cnt, 0u);
    602       DCHECK_EQ(new_dex_pc, dex_pc);
    603     }
    604     if (new_dex_pc != dex::kDexNoIndex) {
    605       shadow_frame->SetDexPC(new_dex_pc);
    606       value = Execute(self,
    607                       accessor,
    608                       *shadow_frame,
    609                       value,
    610                       /* stay_in_interpreter= */ true,
    611                       /* from_deoptimize= */ true);
    612     }
    613     ShadowFrame* old_frame = shadow_frame;
    614     shadow_frame = shadow_frame->GetLink();
    615     ShadowFrame::DeleteDeoptimizedFrame(old_frame);
    616     // Following deoptimizations of shadow frames must be at invocation point
    617     // and should advance dex pc past the invoke instruction.
    618     from_code = false;
    619     deopt_method_type = DeoptimizationMethodType::kDefault;
    620     frame_cnt++;
    621   }
    622   ret_val->SetJ(value.GetJ());
    623 }
    624 
    625 JValue EnterInterpreterFromEntryPoint(Thread* self, const CodeItemDataAccessor& accessor,
    626                                       ShadowFrame* shadow_frame) {
    627   DCHECK_EQ(self, Thread::Current());
    628   bool implicit_check = !Runtime::Current()->ExplicitStackOverflowChecks();
    629   if (UNLIKELY(__builtin_frame_address(0) < self->GetStackEndForInterpreter(implicit_check))) {
    630     ThrowStackOverflowError(self);
    631     return JValue();
    632   }
    633 
    634   jit::Jit* jit = Runtime::Current()->GetJit();
    635   if (jit != nullptr) {
    636     jit->NotifyCompiledCodeToInterpreterTransition(self, shadow_frame->GetMethod());
    637   }
    638   return Execute(self, accessor, *shadow_frame, JValue());
    639 }
    640 
    641 void ArtInterpreterToInterpreterBridge(Thread* self,
    642                                        const CodeItemDataAccessor& accessor,
    643                                        ShadowFrame* shadow_frame,
    644                                        JValue* result) {
    645   bool implicit_check = !Runtime::Current()->ExplicitStackOverflowChecks();
    646   if (UNLIKELY(__builtin_frame_address(0) < self->GetStackEndForInterpreter(implicit_check))) {
    647     ThrowStackOverflowError(self);
    648     return;
    649   }
    650 
    651   self->PushShadowFrame(shadow_frame);
    652   ArtMethod* method = shadow_frame->GetMethod();
    653   // Ensure static methods are initialized.
    654   const bool is_static = method->IsStatic();
    655   if (is_static) {
    656     ObjPtr<mirror::Class> declaring_class = method->GetDeclaringClass();
    657     if (UNLIKELY(!declaring_class->IsInitialized())) {
    658       StackHandleScope<1> hs(self);
    659       HandleWrapperObjPtr<mirror::Class> h_declaring_class(hs.NewHandleWrapper(&declaring_class));
    660       if (UNLIKELY(!Runtime::Current()->GetClassLinker()->EnsureInitialized(
    661           self, h_declaring_class, true, true))) {
    662         DCHECK(self->IsExceptionPending());
    663         self->PopShadowFrame();
    664         return;
    665       }
    666       CHECK(h_declaring_class->IsInitializing());
    667     }
    668   }
    669 
    670   if (LIKELY(!shadow_frame->GetMethod()->IsNative())) {
    671     result->SetJ(Execute(self, accessor, *shadow_frame, JValue()).GetJ());
    672   } else {
    673     // We don't expect to be asked to interpret native code (which is entered via a JNI compiler
    674     // generated stub) except during testing and image writing.
    675     CHECK(!Runtime::Current()->IsStarted());
    676     ObjPtr<mirror::Object> receiver = is_static ? nullptr : shadow_frame->GetVRegReference(0);
    677     uint32_t* args = shadow_frame->GetVRegArgs(is_static ? 0 : 1);
    678     UnstartedRuntime::Jni(self, shadow_frame->GetMethod(), receiver.Ptr(), args, result);
    679   }
    680 
    681   self->PopShadowFrame();
    682 }
    683 
    684 void CheckInterpreterAsmConstants() {
    685   CheckMterpAsmConstants();
    686 }
    687 
    688 void InitInterpreterTls(Thread* self) {
    689   InitMterpTls(self);
    690 }
    691 
    692 bool PrevFrameWillRetry(Thread* self, const ShadowFrame& frame) {
    693   ShadowFrame* prev_frame = frame.GetLink();
    694   if (prev_frame == nullptr) {
    695     NthCallerVisitor vis(self, 1, false);
    696     vis.WalkStack();
    697     prev_frame = vis.GetCurrentShadowFrame();
    698     if (prev_frame == nullptr) {
    699       prev_frame = self->FindDebuggerShadowFrame(vis.GetFrameId());
    700     }
    701   }
    702   return prev_frame != nullptr && prev_frame->GetForceRetryInstruction();
    703 }
    704 
    705 }  // namespace interpreter
    706 }  // namespace art
    707