Android运行时ART执行类方法的过程分析

       在前面一篇文章中,我们分析了ART运行时加载类以及查找其方法的过程。一旦找到了目标类方法,我们就可以获得它的DEX字节码或者本地机器指令,这样就可以对它进行执行了。在ART运行时中,类方法的执行方式有两种。一种是像Dalvik虚拟机一样,将其DEX字节码交给解释器执行;另一种则是直接将其本地机器指令交给CPU执行。在本文中,我们就将通过分析ART运行时执行类方法的过程来理解ART运行时的运行原理。

        我们先来看看图1,它描述了ART运行时执行一个类方法的流程,如下所示:

图1 ART运行时执行类方法的过程

       图1综合了我们在前面Android运行时ART加载OAT文件的过程分析Android运行时ART加载类和方法的过程分析这两篇文章中提到的两个知识点,我们先来回顾一下。

       第一个知识点是ART运行时将DEX字节码翻译成本地机器指令时,使用的后端(Backend)是Quick类型还是Portable类型。ART运行时在编译的时候,默认使用的后端是Quick类型的,不过可以通过将环境变量ART_USE_PORTABLE_COMPILER的值设置为true来指定使用Portable类型的后端,如下所示:

[plain]   view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. ......  
  2. LIBART_CFLAGS :=  
  3. ifeq ($(ART_USE_PORTABLE_COMPILER),true)  
  4.   LIBART_CFLAGS += -DART_USE_PORTABLE_COMPILER=1  
  5. endif  
  6. ......  
       上述编译脚本定义在文件art/runtime/Android.mk中。

       一旦我们将环境变量ART_USE_PORTABLE_COMPILER的值设置为true,那么就会定义一个宏ART_USE_PORTABLE_COMPILER。参考前面Android运行时ART加载OAT文件的过程分析这篇文章,宏ART_USE_PORTABLE_COMPILER的定义与否会影响到加载OAT文件所使用的方法,如下所示:

[cpp]   view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. OatFile* OatFile::Open(const std::string& filename,  
  2.                        const std::string& location,  
  3.                        byte* requested_base,  
  4.                        bool executable) {  
  5.   CHECK(!filename.empty()) << location;  
  6.   CheckLocation(filename);  
  7. #ifdef ART_USE_PORTABLE_COMPILER  
  8.   // If we are using PORTABLE, use dlopen to deal with relocations.  
  9.   //  
  10.   // We use our own ELF loader for Quick to deal with legacy apps that  
  11.   // open a generated dex file by name, remove the file, then open  
  12.   // another generated dex file with the same name. http://b/10614658  
  13.   if (executable) {  
  14.     return OpenDlopen(filename, location, requested_base);  
  15.   }  
  16. #endif  
  17.   // If we aren't trying to execute, we just use our own ElfFile loader for a couple reasons:  
  18.   //  
  19.   // On target, dlopen may fail when compiling due to selinux restrictions on installd.  
  20.   //  
  21.   // On host, dlopen is expected to fail when cross compiling, so fall back to OpenElfFile.  
  22.   // This won't work for portable runtime execution because it doesn't process relocations.  
  23.   UniquePtr<File> file(OS::OpenFileForReading(filename.c_str()));  
  24.   if (file.get() == NULL) {  
  25.     return NULL;  
  26.   }  
  27.   return OpenElfFile(file.get(), location, requested_base, false, executable);  
  28. }  
        这个函数定义在文件art/runtime/oat_file.cc中。

        这个函数的详细解释可以参考前面Android运行时ART加载OAT文件的过程分析一文,这里只对结论进行进一步的解释。从注释可以知道,通过Portable后端和Quick后端生成的OAT文件的本质区别在于,前者使用标准的动态链接器加载,而后者使用自定义的加载器加载。

        标准动态链接器在加载SO文件(这里是OAT文件)的时候,会自动处理重定位问题。也就是说,在生成的本地机器指令中,如果有依赖其它的SO导出的函数,那么标准动态链接器就会将被依赖的SO也加载进来,并且从里面找到被引用的函数的地址,用来重定位引用了该函数的符号。生成的本地机器指令引用的一般都是些SO呢?其实就是ART运行时库(libart.so)。例如,如果在生成的本地机器指令需要分配一个对象,那么就需要调用ART运行时的堆管理器提供的AllocObject接口来分配。

       自定义加载器的做法就不一样了。它在加载OAT文件时,并不需要做上述的重定位操作。因为Quick后端生成的本地机器指令需要调用一些外部库提供的函数时,是通过一个函数跳转表来实现的。由于在加载过程中不需要执行重定位,因此加载过程就会更快,Quick的名字就是这样得来的。Portable后端生成的本地机器指令在调用外部库提供的函数时,使用了标准的方法,因此它不但可以在ART运行时加载,也可以在其它运行时加载,因此就得名于Portable。

       接下来我们的重点是分析Quick后端生成的本地机器指令在调用外部库函数时所使用的函数跳转表是如何定义的。

       在前面分析Dalvik虚拟机的文章Dalvik虚拟机进程和线程的创建过程分析中,我们提到每一个Dalvik虚拟机线程在内部都通过一个Thread对象描述。这个Thread对象包含了一些与虚拟机相关的信息。例如,JNI函数调用函数表。在ART运行时中创建的线程,和Davik虚拟机线程一样,在内部也会通过一个Thread对象来描述。这个新的Thread对象内部除了定义JNI调用函数表之外,还定义了我们在上面提到的外部函数调用跳转表。

       在前面Android运行时ART加载OAT文件的过程分析一文,我们提到了ART运行时的启动和初始化过程。其中的一个初始化过程便是将主线程关联到ART运行时去,如下所示:

[cpp]   view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. bool Runtime::Init(const Options& raw_options, bool ignore_unrecognized) {    
  2.   ......    
  3.   
  4.   java_vm_ = new JavaVMExt(this, options.get());    
  5.   ......    
  6.         
  7.   Thread* self = Thread::Attach("main"false, NULL, false);    
  8.   ......    
  9.         
  10.   return true;    
  11. }    
       这个函数定义在文件art/runtime/runtime.cc中。

       在Runtime类的成员函数Init中,通过调用Thread类的静态成员函数Attach将当前线程,也就是主线程,关联到ART运行时去。在关联的过程中,就会初始化一个外部库函数调用跳转表。

       Thread类的静态成员函数Attach的实现如下所示:

[cpp]   view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. Thread* Thread::Attach(const char* thread_name, bool as_daemon, jobject thread_group,  
  2.                        bool create_peer) {  
  3.   Thread* self;  
  4.   Runtime* runtime = Runtime::Current();  
  5.   ......  
  6.   
  7.   {  
  8.     MutexLock mu(NULL, *Locks::runtime_shutdown_lock_);  
  9.     if (runtime->IsShuttingDown()) {  
  10.       LOG(ERROR) << "Thread attaching while runtime is shutting down: " << thread_name;  
  11.       return NULL;  
  12.     } else {  
  13.       Runtime::Current()->StartThreadBirth();  
  14.       self = new Thread(as_daemon);  
  15.       self->Init(runtime->GetThreadList(), runtime->GetJavaVM());  
  16.       Runtime::Current()->EndThreadBirth();  
  17.     }  
  18.   }  
  19.   
  20.   ......  
  21.   
  22.   return self;  
  23. }  
        这个函数定义在文件art/runtime/thread.cc中。

        在Thread类的静态成员函数Attach中,最重要的就是创建了一个Thread对象来描述当前被关联到ART运行时的线程。创建了这个Thread对象之后,马上就调用它的成员函数Init来对它进行初始化。

        Thread类的成员函数Init的实现如下所示:

[cpp]   view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. void Thread::Init(ThreadList* thread_list, JavaVMExt* java_vm) {  
  2.   ......  
  3.   
  4.   InitTlsEntryPoints();  
  5.   ......  
  6.   
  7.   jni_env_ = new JNIEnvExt(this, java_vm);  
  8.   ......  
  9. }  
       这个函数定义在文件art/runtime/thread.cc中。

       Thread类的成员函数Init除了给当前的线程创建一个JNIEnvExt对象来描述它的JNI调用接口之外,还通过调用另外一个成员函数InitTlsEntryPoints来初始化一个外部库函数调用跳转表。

       Thread类的成员函数InitTlsEntryPoints的实现如下所示:

[cpp]   view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. void InitEntryPoints(InterpreterEntryPoints* ipoints, JniEntryPoints* jpoints,  
  2.                      PortableEntryPoints* ppoints, QuickEntryPoints* qpoints);  
  3.   
  4. void Thread::InitTlsEntryPoints() {  
  5.   ......  
  6.   InitEntryPoints(&interpreter_entrypoints_, &jni_entrypoints_, &portable_entrypoints_,  
  7.                   &quick_entrypoints_);  
  8. }  
       这个函数定义在文件art/runtime/thread.cc中。

       Thread类定义了四个成员变量interpreter_entrypoints_、jni_entrypoints_、portable_entrypoints_和quick_entrypoints_,如下所示:

[cpp]   view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. class PACKED(4) Thread {  
  2.   ......  
  3.   
  4.  public:  
  5.   // Entrypoint function pointers  
  6.   // TODO: move this near the top, since changing its offset requires all oats to be recompiled!  
  7.   InterpreterEntryPoints interpreter_entrypoints_;  
  8.   JniEntryPoints jni_entrypoints_;  
  9.   PortableEntryPoints portable_entrypoints_;  
  10.   QuickEntryPoints quick_entrypoints_;  
  11.   
  12.  ......  
  13. };  
       Thread类的声明定义在文件art/runtime/thread.h中。

       Thread类将外部库函数调用跳转表划分为4个,其中,interpreter_entrypoints_描述的是解释器要用到的跳转表,jni_entrypoints_描述的是JNI调用相关的跳转表,portable_entrypoints_描述的是Portable后端生成的本地机器指令要用到的跳转表,而quick_entrypoints_描述的是Quick后端生成的本地机器指令要用到的跳转表。从这里可以看出,Portable后端生成的本地机器指令也会使用到ART运行时内部的函数跳转表。不过与Quick后端生成的本地机器指令使用到的ART运行时内部的函数跳转表相比,它里面包含的函数项会少很多很多。接下来我们将会看到这一点。

        回到Thread类的成员函数InitTlsEntryPoints中,它通过调用一个全局函数InitEntryPoints来初始化上述的4个跳转表。全局函数InitEntryPoints的实现是和CPU体系结构相关的,因为跳转表里面的函数调用入口是用汇编语言来实现的。

        我们以ARM体系架构为例,来看全局函数InitEntryPoints的实现,如下所示:

[cpp]   view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. void InitEntryPoints(InterpreterEntryPoints* ipoints, JniEntryPoints* jpoints,  
  2.                      PortableEntryPoints* ppoints, QuickEntryPoints* qpoints) {  
  3.   // Interpreter  
  4.   ipoints->pInterpreterToInterpreterBridge = artInterpreterToInterpreterBridge;  
  5.   ipoints->pInterpreterToCompiledCodeBridge = artInterpreterToCompiledCodeBridge;  
  6.   
  7.   // JNI  
  8.   jpoints->pDlsymLookup = art_jni_dlsym_lookup_stub;  
  9.   
  10.   // Portable  
  11.   ppoints->pPortableResolutionTrampoline = art_portable_resolution_trampoline;  
  12.   ppoints->pPortableToInterpreterBridge = art_portable_to_interpreter_bridge;  
  13.   
  14.   // Alloc  
  15.   qpoints->pAllocArray = art_quick_alloc_array;  
  16.   qpoints->pAllocArrayWithAccessCheck = art_quick_alloc_array_with_access_check;  
  17.   qpoints->pAllocObject = art_quick_alloc_object;  
  18.   qpoints->pAllocObjectWithAccessCheck = art_quick_alloc_object_with_access_check;  
  19.   qpoints->pCheckAndAllocArray = art_quick_check_and_alloc_array;  
  20.   qpoints->pCheckAndAllocArrayWithAccessCheck = art_quick_check_and_alloc_array_with_access_check;  
  21.   
  22.   // Cast  
  23.   qpoints->pInstanceofNonTrivial = artIsAssignableFromCode;  
  24.   qpoints->pCanPutArrayElement = art_quick_can_put_array_element;  
  25.   qpoints->pCheckCast = art_quick_check_cast;  
  26.   
  27.   // DexCache  
  28.   qpoints->pInitializeStaticStorage = art_quick_initialize_static_storage;  
  29.   qpoints->pInitializeTypeAndVerifyAccess = art_quick_initialize_type_and_verify_access;  
  30.   qpoints->pInitializeType = art_quick_initialize_type;  
  31.   qpoints->pResolveString = art_quick_resolve_string;  
  32.   
  33.   // Field  
  34.   qpoints->pSet32Instance = art_quick_set32_instance;  
  35.   qpoints->pSet32Static = art_quick_set32_static;  
  36.   qpoints->pSet64Instance = art_quick_set64_instance;  
  37.   qpoints->pSet64Static = art_quick_set64_static;  
  38.   qpoints->pSetObjInstance = art_quick_set_obj_instance;  
  39.   qpoints->pSetObjStatic = art_quick_set_obj_static;  
  40.   qpoints->pGet32Instance = art_quick_get32_instance;  
  41.   qpoints->pGet64Instance = art_quick_get64_instance;  
  42.   qpoints->pGetObjInstance = art_quick_get_obj_instance;  
  43.   qpoints->pGet32Static = art_quick_get32_static;  
  44.   qpoints->pGet64Static = art_quick_get64_static;  
  45.   qpoints->pGetObjStatic = art_quick_get_obj_static;  
  46.   
  47.   // FillArray  
  48.   qpoints->pHandleFillArrayData = art_quick_handle_fill_data;  
  49.   
  50.   // JNI  
  51.   qpoints->pJniMethodStart = JniMethodStart;  
  52.   qpoints->pJniMethodStartSynchronized = JniMethodStartSynchronized;  
  53.   qpoints->pJniMethodEnd = JniMethodEnd;  
  54.   qpoints->pJniMethodEndSynchronized = JniMethodEndSynchronized;  
  55.   qpoints->pJniMethodEndWithReference = JniMethodEndWithReference;  
  56.   qpoints->pJniMethodEndWithReferenceSynchronized = JniMethodEndWithReferenceSynchronized;  
  57.   
  58.   // Locks  
  59.   qpoints->pLockObject = art_quick_lock_object;  
  60.   qpoints->pUnlockObject = art_quick_unlock_object;  
  61.   
  62.   // Math  
  63.   qpoints->pCmpgDouble = CmpgDouble;  
  64.   qpoints->pCmpgFloat = CmpgFloat;  
  65.   qpoints->pCmplDouble = CmplDouble;  
  66.   qpoints->pCmplFloat = CmplFloat;  
  67.   qpoints->pFmod = fmod;  
  68.   qpoints->pSqrt = sqrt;  
  69.   qpoints->pL2d = __aeabi_l2d;  
  70.   qpoints->pFmodf = fmodf;  
  71.   qpoints->pL2f = __aeabi_l2f;  
  72.   qpoints->pD2iz = __aeabi_d2iz;  
  73.   qpoints->pF2iz = __aeabi_f2iz;  
  74.   qpoints->pIdivmod = __aeabi_idivmod;  
  75.   qpoints->pD2l = art_d2l;  
  76.   qpoints->pF2l = art_f2l;  
  77.   qpoints->pLdiv = __aeabi_ldivmod;  
  78.   qpoints->pLdivmod = __aeabi_ldivmod;  // result returned in r2:r3  
  79.   qpoints->pLmul = art_quick_mul_long;  
  80.   qpoints->pShlLong = art_quick_shl_long;  
  81.   qpoints->pShrLong = art_quick_shr_long;  
  82.   qpoints->pUshrLong = art_quick_ushr_long;  
  83.   
  84.   // Intrinsics  
  85.   qpoints->pIndexOf = art_quick_indexof;  
  86.   qpoints->pMemcmp16 = __memcmp16;  
  87.   qpoints->pStringCompareTo = art_quick_string_compareto;  
  88.   qpoints->pMemcpy = memcpy;  
  89.   
  90.   // Invocation  
  91.   qpoints->pQuickResolutionTrampoline = art_quick_resolution_trampoline;  
  92.   qpoints->pQuickToInterpreterBridge = art_quick_to_interpreter_bridge;  
  93.   qpoints->pInvokeDirectTrampolineWithAccessCheck = art_quick_invoke_direct_trampoline_with_access_check;  
  94.   qpoints->pInvokeInterfaceTrampoline = art_quick_invoke_interface_trampoline;  
  95.   qpoints->pInvokeInterfaceTrampolineWithAccessCheck = art_quick_invoke_interface_trampoline_with_access_check;  
  96.   qpoints->pInvokeStaticTrampolineWithAccessCheck = art_quick_invoke_static_trampoline_with_access_check;  
  97.   qpoints->pInvokeSuperTrampolineWithAccessCheck = art_quick_invoke_super_trampoline_with_access_check;  
  98.   qpoints->pInvokeVirtualTrampolineWithAccessCheck = art_quick_invoke_virtual_trampoline_with_access_check;  
  99.   
  100.   // Thread  
  101.   qpoints->pCheckSuspend = CheckSuspendFromCode;  
  102.   qpoints->pTestSuspend = art_quick_test_suspend;  
  103.   
  104.   // Throws  
  105.   qpoints->pDeliverException = art_quick_deliver_exception;  
  106.   qpoints->pThrowArrayBounds = art_quick_throw_array_bounds;  
  107.   qpoints->pThrowDivZero = art_quick_throw_div_zero;  
  108.   qpoints->pThrowNoSuchMethod = art_quick_throw_no_such_method;  
  109.   qpoints->pThrowNullPointer = art_quick_throw_null_pointer_exception;  
  110.   qpoints->pThrowStackOverflow = art_quick_throw_stack_overflow;  
  111. };  
        这个函数定义在文件art/runtime/arch/arm/entrypoints_init_arm.cc中。

        从函数InitEntryPoints的实现就可以看到Quick后端和Portable后端生成的本地机器指令要使用到的外部库函数调用跳转表的初始化过程了。例如,如果在生成的本地机器指令中,需要调用一个JNI函数,那么就需要通过art_jni_dlsym_lookup_stub函数来间接地调用,以便可以找到正确的JNI函数来调用。

       此外,我们还可以看到,解释器要用到的跳转表只包含了两项,分别是artInterpreterToInterpreterBridge和artInterpreterToCompiledCodeBridge。前者用来从一个解释执行的类方法跳到另外一个也是解释执行的类方法去执行,后者用来从一个解释执行的类方法跳到另外一个以本地机器指令执行的类方法去执行。

       Portable后端生成的本地机器指令要用到的跳转表也只包含了两项,分别是art_portable_resolution_trampoline和art_portable_to_interpreter_bridge。前者用作一个还未链接好的类方法的调用入口点,后者用来从一个以本地机器指令执行的类方法跳到另外一个解释执行的类方法去执行。

       剩下的其它代码均是用来初始化Quick后端生成的本地机器指令要用到的跳转表,它包含的项非常多,但是可以划分为Alloc(对象分配)、Cast(类型转换)、DexCache(Dex缓访问)、Field(成员变量访问)、FillArray(数组填充)、JNI(JNI函数调用)、Locks(锁)、Math(数学计算)、Intrinsics(内建函数调用)、Invocation(类方法调用)、Thread(线程操作)和Throws(异常处理)等12类。

       有了这些跳转表之后,当我们需要在生成的本地机器指令中调用一个外部库提供的函数时,只要找到用来描述当前线程的Thread对象,然后再根据上述的四个跳转表在该Thread对象内的偏移位置,那么就很容易找到所需要的跳转项了。

       以上就是我们需要了解的第一个知识点,也就是Portable后端和Quick后端生成的本地机器指令的区别。接下来我们现来看另外一个知识点,它们是涉及到类方法的执行方式,也就是是通过解释器来执行,还是直接以本地机器指令来执行,以及它们之间是如何穿插执行的。

       在前面Android运行时ART加载类和方法的过程分析这篇文章中,我们提到,在类的加载过程中,需要对类的各个方法进行链接,实际上就是确定它们是通过解释器来执行,还是以本地机器指令来直接执行,如下所示:

[cpp]   view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. static void LinkCode(SirtRef<mirror::ArtMethod>& method, const OatFile::OatClass* oat_class,  
  2.                      uint32_t method_index)  
  3.     SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {  
  4.   // Method shouldn't have already been linked.  
  5.   DCHECK(method->GetEntryPointFromCompiledCode() == NULL);  
  6.   // Every kind of method should at least get an invoke stub from the oat_method.  
  7.   // non-abstract methods also get their code pointers.  
  8.   const OatFile::OatMethod oat_method = oat_class->GetOatMethod(method_index);  
  9.   oat_method.LinkMethod(method.get());  
  10.   
  11.   // Install entry point from interpreter.  
  12.   Runtime* runtime = Runtime::Current();  
  13.   bool enter_interpreter = NeedsInterpreter(method.get(), method->GetEntryPointFromCompiledCode());  
  14.   if (enter_interpreter) {  
  15.     method->SetEntryPointFromInterpreter(interpreter::artInterpreterToInterpreterBridge);  
  16.   } else {  
  17.     method->SetEntryPointFromInterpreter(artInterpreterToCompiledCodeBridge);  
  18.   }  
  19.   
  20.   if (method->IsAbstract()) {  
  21.     method->SetEntryPointFromCompiledCode(GetCompiledCodeToInterpreterBridge());  
  22.     return;  
  23.   }  
  24.   
  25.   if (method->IsStatic() && !method->IsConstructor()) {  
  26.     // For static methods excluding the class initializer, install the trampoline.  
  27.     // It will be replaced by the proper entry point by ClassLinker::FixupStaticTrampolines  
  28.     // after initializing class (see ClassLinker::InitializeClass method).  
  29.     method->SetEntryPointFromCompiledCode(GetResolutionTrampoline(runtime->GetClassLinker()));  
  30.   } else if (enter_interpreter) {  
  31.     // Set entry point from compiled code if there's no code or in interpreter only mode.  
  32.     method->SetEntryPointFromCompiledCode(GetCompiledCodeToInterpreterBridge());  
  33.   }  
  34.   
  35.   if (method->IsNative()) {  
  36.     // Unregistering restores the dlsym lookup stub.  
  37.     method->UnregisterNative(Thread::Current());  
  38.   }  
  39.   
  40.   // Allow instrumentation its chance to hijack code.  
  41.   runtime->GetInstrumentation()->UpdateMethodsCode(method.get(),  
  42.                                                    method->GetEntryPointFromCompiledCode());  
  43. }  
        这个函数定义在文件art/runtime/class_linker.cc中。

        函数LinkCode的详细解释可以参考前面Android运行时ART加载类和方法的过程分析一文,这里我们只对结论进行总结,以及对结论进行进一步的分析:

        1. ART运行时有两种执行方法:解释执行模式和本地机器指令执行模式。默认是本地机器指令执行模式,但是在启动ART运行时时可以通过-Xint选项指定为解释执行模式。

        2. 即使是在本地机器指令模式中,也有类方法可能需要以解释模式执行。反之亦然。解释执行的类方法通过函数artInterpreterToCompiledCodeBridge的返回值调用本地机器指令执行的类方法;本地机器指令执行的类方法通过函数GetCompiledCodeToInterpreterBridge的返回值调用解释执行的类方法;解释执行的类方法通过函数artInterpreterToInterpreterBridge的返回值解释执行的类方法。

        3. 在解释执行模式下,除了JNI方法和动态Proxy方法,其余所有的方法均通过解释器执行,它们的入口点设置为函数GetCompiledCodeToInterpreterBridge的返回值。

        4. 抽象方法不能执行,它必须要由子类实现,因此会将抽象方法的入口点设置为函数GetCompiledCodeToInterpreterBridge的返回值,目的检测是否在本地机器指令中调用了抽象方法。如果调用了,上述入口点就会抛出一个异常。

        5. 静态类方法的执行模式延迟至类初始化确定。在类初始化之前,它们的入口点由函数GetResolutionTrampoline的返回值代理。

        接下来,我们就着重分析artInterpreterToCompiledCodeBridge、GetCompiledCodeToInterpreterBridge、artInterpreterToInterpreterBridge和GetResolutionTrampoline这4个函数以及它们所返回的函数的实现,以便可以更好地理解上述5个结论。

        函数artInterpreterToCompiledCodeBridge用来在解释器中调用以本地机器指令执行的函数,它的实现如下所示:

[cpp]   view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. extern "C" void artInterpreterToCompiledCodeBridge(Thread* self, MethodHelper& mh,  
  2.                                                    const DexFile::CodeItem* code_item,  
  3.                                                    ShadowFrame* shadow_frame, JValue* result) {  
  4.   mirror::ArtMethod* method = shadow_frame->GetMethod();  
  5.   // Ensure static methods are initialized.  
  6.   if (method->IsStatic()) {  
  7.     Runtime::Current()->GetClassLinker()->EnsureInitialized(method->GetDeclaringClass(), truetrue);  
  8.   }  
  9.   uint16_t arg_offset = (code_item == NULL) ? 0 : code_item->registers_size_ - code_item->ins_size_;  
  10. #if defined(ART_USE_PORTABLE_COMPILER)  
  11.   ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());  
  12.   arg_array.BuildArgArrayFromFrame(shadow_frame, arg_offset);  
  13.   method->Invoke(self, arg_array.GetArray(), arg_array.GetNumBytes(), result, mh.GetShorty()[0]);  
  14. #else  
  15.   method->Invoke(self, shadow_frame->GetVRegArgs(arg_offset),  
  16.                  (shadow_frame->NumberOfVRegs() - arg_offset) * 4,  
  17.                  result, mh.GetShorty()[0]);  
  18. #endif  
  19. }  
        这个函数定义在文件art/runtime/entrypoints/interpreter/interpreter_entrypoints.cc中。

        被调用的类方法通过一个ArtMethod对象来描述,并且可以在调用栈帧shadow_frame中获得。获得了用来描述被调用方法的ArtMehtod对象之后,就可以调用它的成员函数Invoke来对它进行执行。后面我们就会看到,ArtMethod类的成员函数Invoke会找到类方法的本地机器指令来执行。

        在调用类方法的本地机器指令的时候,从解释器调用栈获取的传入参数根据ART运行时使用的是Quick后端还是Portable后端来生成本地机器指令有所不同。不过最终都会ArtMethod类的成员函数Invoke来执行被调用类方法的本地机器指令。

        函数GetCompiledCodeToInterpreterBridge用来返回一个函数指针,这个函数指针指向的函数用来从以本地机器指令执行的类方法中调用以解释执行的类方法,它的实现如下所示:

[cpp]   view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. static inline const void* GetCompiledCodeToInterpreterBridge() {  
  2. #if defined(ART_USE_PORTABLE_COMPILER)  
  3.   return GetPortableToInterpreterBridge();  
  4. #else  
  5.   return GetQuickToInterpreterBridge();  
  6. #endif  
  7. }  
        这个函数定义在文件art/runtime/entrypoints/entrypoint_utils.h中。

        根据ART运行时使用的是Quick后端还是Portable后端,函数GetCompiledCodeToInterpreterBridge的返回值有所不同,不过它们的作用是一样的。我们假设ART运行时使用的是Quick后端,那么函数GetCompiledCodeToInterpreterBridge的返回值通过调用函数GetQuickToInterpreterBridge来获得。

        函数GetQuickToInterpreterBridge的实现如下所示:

[cpp]   view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. extern "C" void art_quick_to_interpreter_bridge(mirror::ArtMethod*);  
  2. static inline const void* GetQuickToInterpreterBridge() {  
  3.   return reinterpret_cast<void*>(art_quick_to_interpreter_bridge);  
  4. }  
        这个函数定义在文件art/runtime/entrypoints/entrypoint_utils.h中。

        函数GetQuickToInterpreterBridge的返回值实际上指向的是函数art_quick_to_interpreter_bridge。函数art_quick_to_interpreter_bridge是使用汇编代码来实现的,用来从本地机器指令进入到解释器的。

        以ARM体系结构为例,函数art_quick_to_interpreter_bridge的实现如下所示:

[plain]   view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1.     .extern artQuickToInterpreterBridge  
  2. ENTRY art_quick_to_interpreter_bridge  
  3.     SETUP_REF_AND_ARGS_CALLEE_SAVE_FRAME  
  4.     mov     r1, r9                 @ pass Thread::Current  
  5.     mov     r2, sp                 @ pass SP  
  6.     blx     artQuickToInterpreterBridge    @ (Method* method, Thread*, SP)  
  7.     ldr     r2, [r9, #THREAD_EXCEPTION_OFFSET]  @ load Thread::Current()->exception_  
  8.     ldr     lr,  [sp, #44]         @ restore lr  
  9.     add     sp,  #48               @ pop frame  
  10.     .cfi_adjust_cfa_offset -48  
  11.     cbnz    r2, 1f                 @ success if no exception is pending  
  12.     bx    lr                       @ return on success  
  13. 1:  
  14.     DELIVER_PENDING_EXCEPTION  
  15. END art_quick_to_interpreter_bridge  
        这个函数定义在文件art/runtime/arch/arm/quick_entrypoints_arm.S中。

        很明显,函数art_quick_to_interpreter_bridge通过调用另外一个函数artQuickToInterpreterBridge从本地机器指令进入到解释器中去。

        函数artQuickToInterpreterBridge的实现如下所示:

[cpp]   view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. extern "C" uint64_t artQuickToInterpreterBridge(mirror::ArtMethod* method, Thread* self,  
  2.                                                 mirror::ArtMethod** sp)  
  3.     SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {  
  4.   // Ensure we don't get thread suspension until the object arguments are safely in the shadow  
  5.   // frame.  
  6.   FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs);  
  7.   
  8.   if (method->IsAbstract()) {  
  9.     ThrowAbstractMethodError(method);  
  10.     return 0;  
  11.   } else {  
  12.     const char* old_cause = self->StartAssertNoThreadSuspension("Building interpreter shadow frame");  
  13.     MethodHelper mh(method);  
  14.     const DexFile::CodeItem* code_item = mh.GetCodeItem();  
  15.     uint16_t num_regs = code_item->registers_size_;  
  16.     void* memory = alloca(ShadowFrame::ComputeSize(num_regs));  
  17.     ShadowFrame* shadow_frame(ShadowFrame::Create(num_regs, NULL,  // No last shadow coming from quick.  
  18.                                                   method, 0, memory));  
  19.     size_t first_arg_reg = code_item->registers_size_ - code_item->ins_size_;  
  20.     BuildQuickShadowFrameVisitor shadow_frame_builder(sp, mh.IsStatic(), mh.GetShorty(),  
  21.                                                  mh.GetShortyLength(),  
  22.                                                  *shadow_frame, first_arg_reg);  
  23.     shadow_frame_builder.VisitArguments();  
  24.     // Push a transition back into managed code onto the linked list in thread.  
  25.     ManagedStack fragment;  
  26.     self->PushManagedStackFragment(&fragment);  
  27.     self->PushShadowFrame(shadow_frame);  
  28.     self->EndAssertNoThreadSuspension(old_cause);  
  29.   
  30.     if (method->IsStatic() && !method->GetDeclaringClass()->IsInitializing()) {  
  31.       // Ensure static method's class is initialized.  
  32.       if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(method->GetDeclaringClass(),  
  33.                                                                    truetrue)) {  
  34.         DCHECK(Thread::Current()->IsExceptionPending());  
  35.         self->PopManagedStackFragment(fragment);  
  36.         return 0;  
  37.       }  
  38.     }  
  39.   
  40.     JValue result = interpreter::EnterInterpreterFromStub(self, mh, code_item, *shadow_frame);  
  41.     // Pop transition.  
  42.     self->PopManagedStackFragment(fragment);  
  43.     return result.GetJ();  
  44.   }  
  45. }  
        这个函数定义在文件art/runtime/entrypoints/quick/quick_trampoline_entrypoints.cc中。

        函数artQuickToInterpreterBridge的作用实际上就是找到被调用类方法method的DEX字节码code_item,然后根据调用传入的参数构造一个解释器调用栈帧shadow_frame,最后就可以通过函数interpreter::EnterInterpreterFromStub进入到解释器去执行了。

        既然已经知道了要执行的类方法的DEX字节码,以及已经构造好了要执行的类方法的调用栈帧,我们就不难理解解释器是如何执行该类方法了,具体可以参考一下Dalvik虚拟机的运行过程分析这篇文章描述的Dalvik虚拟机解释器的实现。

        如果要执行的类方法method是一个静态方法,那么我们就需要确保它的声明类是已经初始化过了的。如果还没有初始化过,那么就需要调用ClassLinker类的成员函数EnsureInitialized来对它进行初始化。

        函数artInterpreterToInterpreterBridge用来从解释执行的函数调用到另外一个也是解释执行的函数,它的实现如下所示:

[cpp]   view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. extern "C" void artInterpreterToInterpreterBridge(Thread* self, MethodHelper& mh,  
  2.                                                   const DexFile::CodeItem* code_item,  
  3.                                                   ShadowFrame* shadow_frame, JValue* result) {  
  4.   if (UNLIKELY(__builtin_frame_address(0) < self->GetStackEnd())) {  
  5.     ThrowStackOverflowError(self);  
  6.     return;  
  7.   }  
  8.   
  9.   ArtMethod* method = shadow_frame->GetMethod();  
  10.   if (method->IsStatic() && !method->GetDeclaringClass()->IsInitializing()) {  
  11.     if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(method->GetDeclaringClass(),  
  12.                                                                  truetrue)) {  
  13.       DCHECK(Thread::Current()->IsExceptionPending());  
  14.       return;  
  15.     }  
  16.     CHECK(method->GetDeclaringClass()->IsInitializing());  
  17.   }  
  18.   
  19.   self->PushShadowFrame(shadow_frame);  
  20.   
  21.   if (LIKELY(!method->IsNative())) {  
  22.     result->SetJ(Execute(self, mh, code_item, *shadow_frame, JValue()).GetJ());  
  23.   } else {  
  24.     // We don't expect to be asked to interpret native code (which is entered via a JNI compiler  
  25.     // generated stub) except during testing and image writing.  
  26.     CHECK(!Runtime::Current()->IsStarted());  
  27.     Object* receiver = method->IsStatic() ? NULL : shadow_frame->GetVRegReference(0);  
  28.     uint32_t* args = shadow_frame->GetVRegArgs(method->IsStatic() ? 0 : 1);  
  29.     UnstartedRuntimeJni(self, method, receiver, args, result);  
  30.   }  
  31.   
  32.   self->PopShadowFrame();  
  33.   return;  
  34. }  
       这个函数定义在文件art/runtime/interpreter/interpreter.cc中。

       对比函数artInterpreterToInterpreterBridge和artQuickToInterpreterBridge的实现就可以看出,虽然都是要跳入到解释器去执行一个被调用类方法,但是两者的实现是不一样的。前者由于调用方法本来就是在解释器中执行的,因此,调用被调用类方法所需要的解释器栈帧实际上已经准备就绪,并且被调用方法的DEX字节码也已经知晓,因此这时候就可以直接调用另外一个函数Execute来继续在解释器中执行。

       同样,如果被调用的类方法是一个静态方法,并且它的声明类还没有被初始化,那么就需要调用ClassLinker类的成员函数EnsureInitialized来确保它的声明类是已经初始化好了的。

       如果被调用的类方法是一个JNI方法,那么此种情况在ART运行时已经启动之后不允许的(ART运行时启动之前允许,但是只是测试ART运行时时才会用到),因为JNI方法在解释器中有自己的调用方式,而函数函数artInterpreterToInterpreterBridge仅仅是用于调用非JNI方法,因此这时候就会调用另外一个函数UnstartedRuntimeJni记录和抛出错误。

        函数GetResolutionTrampoline用来获得一个延迟链接类方法的函数。这个延迟链接类方法的函数用作那些在类加载时还没有链接好的方法的调用入口点,也就是还没有确定调用入口的类方法。对于已经链接好的类方法来说,无论它是解释执行,还是本地机器指令执行,相应的调用入口都是已经通过ArtMehtod类的成员函数SetEntryPointFromCompiledCode和SetEntryPointFromInterpreter设置好了的。如上所述,这类典型的类方法就是静态方法,它们需要等到类初始化的时候才会进行链接。

        函数GetResolutionTrampoline的实现如下所示:

[cpp]   view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. static inline const void* GetResolutionTrampoline(ClassLinker* class_linker) {  
  2. #if defined(ART_USE_PORTABLE_COMPILER)  
  3.   return GetPortableResolutionTrampoline(class_linker);  
  4. #else  
  5.   return GetQuickResolutionTrampoline(class_linker);  
  6. #endif  
  7. }  

        这个函数定义在文件art/runtime/entrypoints/entrypoint_utils.h中。 

        我们假设没有定义宏ART_USE_PORTABLE_COMPILER,那么接下来就会调用GetQuickResolutionTrampoline来获得一个函数指针。

        函数GetQuickResolutionTrampoline的实现如下所示:

[cpp]   view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. static inline const void* GetQuickResolutionTrampoline(ClassLinker* class_linker) {  
  2.   return class_linker->GetQuickResolutionTrampoline();  
  3. }  
        这个函数定义在文件art/runtime/entrypoints/entrypoint_utils.h中。

        函数GetQuickResolutionTrampoline又是通过调用参数class_linker指向的ClassLinker对象的成员函数GetQuickResolutionTrampoline来获得一个函数指针的。

        ClassLinker类的成员函数GetQuickResolutionTrampoline的实现如下所示:

[cpp]   view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. class ClassLinker {  
  2.  public:  
  3.   ......  
  4.   
  5.   const void* GetQuickResolutionTrampoline() const {  
  6.     return quick_resolution_trampoline_;  
  7.   }  
  8.   
  9.   ......  
  10.   
  11.  private:  
  12.   ......  
  13.   
  14.   const void* quick_resolution_trampoline_;  
  15.     
  16.   ......  
  17. };  
        这个函数定义在文件art/runtime/class_linker.h中。

        ClassLinker类的成员函数GetQuickResolutionTrampoline返回的是成员变量quick_resolution_trampoline_的值。那么ClassLinker类的成员变量quick_resolution_trampoline_的值是什么时候初始化的呢?是在ClassLinker类的成员函数InitFromImage中初始化。如下所示:

[cpp]   view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. void ClassLinker::InitFromImage() {  
  2.   ......  
  3.   
  4.   gc::Heap* heap = Runtime::Current()->GetHeap();  
  5.   gc::space::ImageSpace* space = heap->GetImageSpace();  
  6.   ......  
  7.   
  8.   OatFile& oat_file = GetImageOatFile(space);  
  9.   ......  
  10.   
  11.   quick_resolution_trampoline_ = oat_file.GetOatHeader().GetQuickResolutionTrampoline();  
  12.    
  13.   ......  
  14. }  
        这个函数定义在文件art/runtime/class_linker.h中。

        从前面Android运行时ART加载OAT文件的过程分析这篇文章可以知道,ART运行时在启动的时候,会加载一个Image文件,并且根据这个Image文件创建一个Image空间。这个Image空间属于ART运行时堆的一部分。后面我们分析ART运行时的垃圾收集机制再详细分析。

        Image文件是ART运行时第一次启动时翻译系统启动类的DEX字节码创建的OAT文件的过程中创建的。我们将这个OAT文件称为启动OAT文件。这个启动OAT文件的OAT头部包含有一个quick_resolution_trampoline_offset_字段。这个quick_resolution_trampoline_offset_字段指向一小段Trampoline代码。这一小段Trampoline代码的作用是找到当前线程类型为Quick的函数跳转表中的pQuickResolutionTrampoline项,并且跳到这个pQuickResolutionTrampoline项指向的函数去执行。

       从上面的分析可以知道,类型为Quick的函数跳转表中的pQuickResolutionTrampoline项指向的函数为art_quick_resolution_trampoline,它是一个用汇编语言实现的函数,如下所示:

[plain]   view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1.     .extern artQuickResolutionTrampoline  
  2. ENTRY art_quick_resolution_trampoline  
  3.     SETUP_REF_AND_ARGS_CALLEE_SAVE_FRAME  
  4.     mov     r2, r9                 @ pass Thread::Current  
  5.     mov     r3, sp                 @ pass SP  
  6.     blx     artQuickResolutionTrampoline  @ (Method* called, receiver, Thread*, SP)  
  7.     cbz     r0, 1f                 @ is code pointer null? goto exception  
  8.     mov     r12, r0  
  9.     ldr  r0, [sp, #0]              @ load resolved method in r0  
  10.     ldr  r1, [sp, #8]              @ restore non-callee save r1  
  11.     ldrd r2, [sp, #12]             @ restore non-callee saves r2-r3  
  12.     ldr  lr, [sp, #44]             @ restore lr  
  13.     add  sp, #48                   @ rewind sp  
  14.     .cfi_adjust_cfa_offset -48  
  15.     bx      r12                    @ tail-call into actual code  
  16. 1:  
  17.     RESTORE_REF_AND_ARGS_CALLEE_SAVE_FRAME  
  18.     DELIVER_PENDING_EXCEPTION  
  19. END art_quick_resolution_trampoline  
       这个函数定义在文件art/runtime/arch/arm/quick_entrypoints_arm.S中。

       函数art_quick_resolution_trampoline首先是调用另外一个函数artQuickResolutionTrampoline来获得真正要调用的函数的地址,并且通过bx指令跳到该地址去执行。函数artQuickResolutionTrampoline的作用就是用来延迟链接类方法的,也就是等到该类方法被调用时才会对它进行解析链接,确定真正要调用的函数。

       函数artQuickResolutionTrampoline的实现如下所示:

[cpp]   view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. // Lazily resolve a method for quick. Called by stub code.  
  2. extern "C" const void* artQuickResolutionTrampoline(mirror::ArtMethod* called,  
  3.                                                     mirror::Object* receiver,  
  4.                                                     Thread* thread, mirror::ArtMethod** sp)  
  5.     SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {  
  6.   FinishCalleeSaveFrameSetup(thread, sp, Runtime::kRefsAndArgs);    
  7.   ......  
  8.   
  9.   // Start new JNI local reference state  
  10.   JNIEnvExt* env = thread->GetJniEnv();  
  11.   ScopedObjectAccessUnchecked soa(env);  
  12.   ......  
  13.   
  14.   // Compute details about the called method (avoid GCs)  
  15.   ClassLinker* linker = Runtime::Current()->GetClassLinker();  
  16.   mirror::ArtMethod* caller = QuickArgumentVisitor::GetCallingMethod(sp);  
  17.   InvokeType invoke_type;  
  18.   const DexFile* dex_file;  
  19.   uint32_t dex_method_idx;  
  20.   if (called->IsRuntimeMethod()) {  
  21.     uint32_t dex_pc = caller->ToDexPc(QuickArgumentVisitor::GetCallingPc(sp));  
  22.     const DexFile::CodeItem* code;  
  23.     {  
  24.       MethodHelper mh(caller);  
  25.       dex_file = &mh.GetDexFile();  
  26.       code = mh.GetCodeItem();  
  27.     }  
  28.     const Instruction* instr = Instruction::At(&code->insns_[dex_pc]);  
  29.     Instruction::Code instr_code = instr->Opcode();  
  30.     bool is_range;  
  31.     switch (instr_code) {  
  32.       case Instruction::INVOKE_DIRECT:  
  33.         invoke_type = kDirect;  
  34.         is_range = false;  
  35.         break;  
  36.       case Instruction::INVOKE_DIRECT_RANGE:  
  37.         invoke_type = kDirect;  
  38.         is_range = true;  
  39.         break;  
  40.       case Instruction::INVOKE_STATIC:  
  41.         invoke_type = kStatic;  
  42.         is_range = false;  
  43.         break;  
  44.       case Instruction::INVOKE_STATIC_RANGE:  
  45.         invoke_type = kStatic;  
  46.         is_range = true;  
  47.         break;  
  48.       case Instruction::INVOKE_SUPER:  
  49.         invoke_type = kSuper;  
  50.         is_range = false;  
  51.         break;  
  52.       case Instruction::INVOKE_SUPER_RANGE:  
  53.         invoke_type = kSuper;  
  54.         is_range = true;  
  55.         break;  
  56.       case Instruction::INVOKE_VIRTUAL:  
  57.         invoke_type = kVirtual;  
  58.         is_range = false;  
  59.         break;  
  60.       case Instruction::INVOKE_VIRTUAL_RANGE:  
  61.         invoke_type = kVirtual;  
  62.         is_range = true;  
  63.         break;  
  64.       case Instruction::INVOKE_INTERFACE:  
  65.         invoke_type = kInterface;  
  66.         is_range = false;  
  67.         break;  
  68.       case Instruction::INVOKE_INTERFACE_RANGE:  
  69.         invoke_type = kInterface;  
  70.         is_range = true;  
  71.         break;  
  72.       default:  
  73.         LOG(FATAL) << "Unexpected call into trampoline: " << instr->DumpString(NULL);  
  74.         // Avoid used uninitialized warnings.  
  75.         invoke_type = kDirect;  
  76.         is_range = false;  
  77.     }  
  78.     dex_method_idx = (is_range) ? instr->VRegB_3rc() : instr->VRegB_35c();  
  79.   
  80.   } else {  
  81.     invoke_type = kStatic;  
  82.     dex_file = &MethodHelper(called).GetDexFile();  
  83.     dex_method_idx = called->GetDexMethodIndex();  
  84.   }  
  85.   ......  
  86.   
  87.   // Resolve method filling in dex cache.  
  88.   if (called->IsRuntimeMethod()) {  
  89.     called = linker->ResolveMethod(dex_method_idx, caller, invoke_type);  
  90.   }  
  91.   
  92.   const void* code = NULL;  
  93.   if (LIKELY(!thread->IsExceptionPending())) {  
  94.     ......  
  95.     // Refine called method based on receiver.  
  96.     if (invoke_type == kVirtual) {  
  97.       called = receiver->GetClass()->FindVirtualMethodForVirtual(called);  
  98.     } else if (invoke_type == kInterface) {  
  99.       called = receiver->GetClass()->FindVirtualMethodForInterface(called);  
  100.     }  
  101.     // Ensure that the called method's class is initialized.  
  102.     mirror::Class* called_class = called->GetDeclaringClass();  
  103.     linker->EnsureInitialized(called_class, truetrue);  
  104.     if (LIKELY(called_class->IsInitialized())) {  
  105.       code = called->GetEntryPointFromCompiledCode();  
  106.     } else if (called_class->IsInitializing()) {  
  107.       if (invoke_type == kStatic) {  
  108.         // Class is still initializing, go to oat and grab code (trampoline must be left in place  
  109.         // until class is initialized to stop races between threads).  
  110.         code = linker->GetOatCodeFor(called);  
  111.       } else {  
  112.         // No trampoline for non-static methods.  
  113.         code = called->GetEntryPointFromCompiledCode();  
  114.       }  
  115.     }   
  116.     ......  
  117.   }  
  118.    
  119.   ......  
  120.   
  121.   // Place called method in callee-save frame to be placed as first argument to quick method.  
  122.   *sp = called;  
  123.   return code;  
  124. }  

       这个函数定义在文件art/runtime/entrypoints/quick/quick_trampoline_entrypoints.cc中。

       第一个参数called表示被调用的类方法,第二个参数receiver表示被调用的对象,也就是接收消息的对象,第三个参数thread表示当前线程,第四个参数sp指向调用栈顶。通过调用QuickArgumentVisitor类的静态成员函数GetCallingMethod可以在调用栈找到类方法called的调用者,保存在变量caller中。

       被调用类方法called有可能是一个运行时方法(Runtime Method)。运行时方法相当是一个替身,它是用来找到被替换的类方法。当调用类方法called是一个运行时方法时,调用它的成员函数IsRuntimeMethod得到的返回值为true,这时候我们就需要找到被替换的类方法。那么问题就来了,怎么找到此时被替换的类方法呢?运行时方法只是一个空壳,没有任何线索可以提供给我们,不过我们却可以在DEX字节码的调用指令中找到一些蜘丝马迹。在DEX字节码中,我们在一个类方法中通过invoke-static/invoke-direct/invoke-interface/invoke-super/invoke-virtual等指令来调用另外一个类方法。在这些调用指令中,有一个寄存器记录了被调用的类方法在DEX文件中的方法索引dex_method_index。有了这个DEX文件方法索引之后,我们就可以在相应的DEX文件找到被替换的类方法了。现在第二个问题又来了,我们要在哪一个DEX文件查找被替换的类方法呢?函数artQuickResolutionTrampoline适用的是调用方法caller和被调用方法called均是位于同一个DEX文件的情况。因此,我们可以通过调用方法caller来得到要查找的DEX文件dex_file。有了上述两个重要的信息之后,函数artQuickResolutionTrampoline接下来就可以调用ClassLinker类的成员函数ResolveMethod来查找被替换的类方法了,并且继续保存在参数called中。另一方面,如果被调用类方法called不是运行时方法,那么情况就简单多了,因为此时called描述的便是要调用的类方法。

       经过上面的处理之后,参数called指向的ArtMethod对象还不一定是最终要调用的类方法。这是因为当前发生的可能是一个虚函数调用或者接口调用。在上述两种情况下,我们需要通过接收消息的对象receiver来确定真正被调用的类方法。为了完成这个任务,我们首先通过调用Object类的成员函数GetClass获得接收消息的对象receiver的类对象,接着再通过调用过Class类的成员函数FindVirtualMethodForVirtual或者FindVirtualMethodForInterface来获得真正要被调用的类方法。前者针对的是虚函数调用,而后者针对的是接口调用。

       最终我们得到的真正被调用的类方法仍然是保存在参数called中。这时候事情还没完,因为此时被调用的类方法所属的类可能还没有初始化好。因此,在继续下一步操作之前,我们需要调用ClassLinker类的成员函数EnsureInitialized来确保存被调用类方法called所属的类已经初始好了。在调用ClassLinker类的成员函数EnsureInitialized的时候,如果被调用类方法called所属的类还没有初始化,那么就会对它进行初始化,不过不等它初始化完成就返回了。因此,这时候就可能会出现两种情况。

        第一种情况是被调用类方法called所属的类已经初始好了。这时候我们就可以直接调用它的成员函数GetEntryPointFromCompiledCode来获得它的本地机器指令或者DEX字节码,取决于它是以本地机器指令方式执行还是通过解释器来执行。

        第二种情况是被调用方法called所属的类正在初始化中。这时候需要区分静态和非静态调用两种情况。在进一步解释之前,我们需要明确,类加载和类初始化是两个不同的操作。类加载的过程并不一定会伴随着类的初始化。此时我们唯一确定的是被调用方法called所属的类已经被加载(否则它的类方法无法被调用)。又从前面Android运行时ART加载类和方法的过程分析这篇文章可以知道,当一个类被加载时,除了它的静态成员函数,其余所有的成员函数均已加载完毕。这意味着我们可以直接调用ArtMethod类的成员函数GetEntryPointFromCompiledCode来获得被调用方法called的本地机器指令或者DEX字节码。对于静态成员函数的情况,我们就唯有到DEX文件去查找到被调用方法called的本地机器指令了。这是通过调用ClassLinker类的成员函数GetOatCodeFor来实现的。当然,如果该静态成员函数不存在本地机器指令,那么ClassLinker类的成员函数GetOatCodeFor返回的是进入解释器的入口函数地址。这样我们就可以通过解释器来执行该静态成员函数了。

       最后,函数artQuickResolutionTrampoline将获得的真正被调用的类方法的执行入口地址code返回给前一个函数,即art_quick_resolution_trampoline,以便后者可以通过bx跳过去执行。函数artQuickResolutionTrampoline在返回之前,同时还会将此时栈顶的内容设置为真正被调用的类方法对象,以便真正被调用的类方法在运行时,可以获得正确的调用栈帧。

       到这里,函数artQuickResolutionTrampoline的实现就分析完成了。不过对于上面提到的运行时方法,我们还需要继续解释。只有了理解了运行时方法的作用之后,我们才能真正理解函数artQuickResolutionTrampoline的作用。

       运行时方法与另一个称为Dex Cache的机制有关。在ART运行时中,每一个DEX文件都有一个关联的Dex Cache,用来缓存对应的DEX文件中已经被解析过的信息,例如类方法和类属性等。这个Dex Cache使用类DexCache来描述,它的定义如下所示:

[cpp]   view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. class MANAGED DexCache : public Object {  
  2.  public:  
  3.   ......  
  4.   
  5.  private:  
  6.   ......  
  7.   ObjectArray<ArtMethod>* resolved_methods_;  
  8.   .....  
  9.   uint32_t dex_file_;  
  10.   ......  
  11. };  
       这个类定义在文件rt/runtime/mirror/dex_cache.h中。

       这里我们只关注Dex Cache中的类方法,它通过成员变量resolved_methods_来描述。

       在ART运行时中,每当一个类被加载时,ART运行时都会检查该类所属的DEX文件是否已经关联有一个Dex Cache。如果还没有关联,那么就会创建一个Dex Cache,并且建立好关联关系。以Java层的DexFile类为例,当我们通过调用它的成员函数loadClass来加载一个类的时候,最终会调用到C++层的JNI函数DexFile_defineClassNative来执行真正的加载操作。

       函数DexFile_defineClassNative的实现如下所示:

[cpp]   view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. static jclass DexFile_defineClassNative(JNIEnv* env, jclass, jstring javaName, jobject javaLoader,  
  2.                                         jint cookie) {  
  3.   ScopedObjectAccess soa(env);  
  4.   const DexFile* dex_file = toDexFile(cookie);  
  5.   ......  
  6.   
  7.   ScopedUtfChars class_name(env, javaName);  
  8.   ......  
  9.   
  10.   const std::string descriptor(DotToDescriptor(class_name.c_str()));  
  11.   const DexFile::ClassDef* dex_class_def = dex_file->FindClassDef(descriptor.c_str());  
  12.   ......  
  13.   
  14.   ClassLinker* class_linker = Runtime::Current()->GetClassLinker();  
  15.   class_linker->RegisterDexFile(*dex_file);  
  16.   mirror::ClassLoader* class_loader = soa.Decode<mirror::ClassLoader*>(javaLoader);  
  17.   mirror::Class* result = class_linker->DefineClass(descriptor.c_str(), class_loader, *dex_file,  
  18.                                                     *dex_class_def);  
  19.   ......  
  20.   return soa.AddLocalReference<jclass>(result);  
  21. }  

       这个函数定义在文件art/runtime/native/dalvik_system_DexFile.cc中。

       参数cookie指向之前已经打开了的DEX文件,因此这里首先将它转换为一个DexFile指针dex_file。这个DEX文件是包含在OAT文件里面的,它们的打开过程可以参考Android运行时ART加载OAT文件的过程分析一文。得到了之前打开的DEX文件之后,接下来就调用ClassLinker类的成员函数RegisterDexFile将它注册到ART运行时中去,以便以后可以查询使用。最后再通过ClassLinker类的成员函数DefineClass来加载参数javaName指定的类。

        类的加载过程可以参考前面Android运行时ART加载类和方法的过程分析一文,接下来我们主要关注ClassLinker类的成员函数RegisterDexFile的实现,如下所示:

[cpp]   view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. void ClassLinker::RegisterDexFileLocked(const DexFile& dex_file, SirtRef<mirror::DexCache>& dex_cache) {  
  2.   dex_lock_.AssertExclusiveHeld(Thread::Current());  
  3.   CHECK(dex_cache.get() != NULL) << dex_file.GetLocation();  
  4.   CHECK(dex_cache->GetLocation()->Equals(dex_file.GetLocation()))  
  5.       << dex_cache->GetLocation()->ToModifiedUtf8() << " " << dex_file.GetLocation();  
  6.   dex_caches_.push_back(dex_cache.get());  
  7.   dex_cache->SetDexFile(&dex_file);  
  8.   dex_caches_dirty_ = true;  
  9. }  
  10.   
  11. void ClassLinker::RegisterDexFile(const DexFile& dex_file) {  
  12.   Thread* self = Thread::Current();  
  13.   {  
  14.     ReaderMutexLock mu(self, dex_lock_);  
  15.     if (IsDexFileRegisteredLocked(dex_file)) {  
  16.       return;  
  17.     }  
  18.   }  
  19.   // Don't alloc while holding the lock, since allocation may need to  
  20.   // suspend all threads and another thread may need the dex_lock_ to  
  21.   // get to a suspend point.  
  22.   SirtRef<mirror::DexCache> dex_cache(self, AllocDexCache(self, dex_file));  
  23.   CHECK(dex_cache.get() != NULL) << "Failed to allocate dex cache for " << dex_file.GetLocation();  
  24.   {  
  25.     WriterMutexLock mu(self, dex_lock_);  
  26.     if (IsDexFileRegisteredLocked(dex_file)) {  
  27.       return;  
  28.     }  
  29.     RegisterDexFileLocked(dex_file, dex_cache);  
  30.   }  
  31. }  
       这个函数定义在文件art/runtime/class_linker.cc中。

       ClassLinker类的成员函数RegisterDexFile首先将调用另外一个成员函数IsDexFileRegisteredLocked检查参数dex_file指定的DEX文件是否已经注册过了。如果已经注册过了,那么就什么也不用做。否则的话,就调用ClassLinker类的成员函数AllocDexCache为其分配一个Dex Cache,并且调用ClassLinker类的成员函数RegisterDexFileLocked执行真正的注册工作。

       从上面的分析就可以看出,注册DEX文件实际上就是创建一个与之关联的Dex Cache,并且将该Dex Cache保存在ClassLinker类的成员变量dex_caches_所描述的一个向量中。不过,这里我们关注的是注册过程中所创建的Dex Cache。因此,接下来我们继续分析ClassLinker类的成员函数AllocDexCache的实现,如下所示:

[cpp]   view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. mirror::DexCache* ClassLinker::AllocDexCache(Thread* self, const DexFile& dex_file) {  
  2.   gc::Heap* heap = Runtime::Current()->GetHeap();  
  3.   mirror::Class* dex_cache_class = GetClassRoot(kJavaLangDexCache);  
  4.   SirtRef<mirror::DexCache> dex_cache(self,  
  5.                               down_cast<mirror::DexCache*>(heap->AllocObject(self, dex_cache_class,  
  6.                                                                 dex_cache_class->GetObjectSize())));  
  7.   ......  
  8.   
  9.   SirtRef<mirror::String>  
  10.       location(self, intern_table_->InternStrong(dex_file.GetLocation().c_str()));  
  11.   ......  
  12.   
  13.   SirtRef<mirror::ObjectArray<mirror::String> >  
  14.       strings(self, AllocStringArray(self, dex_file.NumStringIds()));  
  15.   ......  
  16.   
  17.   SirtRef<mirror::ObjectArray<mirror::Class> >  
  18.       types(self, AllocClassArray(self, dex_file.NumTypeIds()));  
  19.   ......  
  20.   
  21.   SirtRef<mirror::ObjectArray<mirror::ArtMethod> >  
  22.       methods(self, AllocArtMethodArray(self, dex_file.NumMethodIds()));  
  23.   ......  
  24.   
  25.   SirtRef<mirror::ObjectArray<mirror::ArtField> >  
  26.       fields(self, AllocArtFieldArray(self, dex_file.NumFieldIds()));  
  27.   ......  
  28.   
  29.   SirtRef<mirror::ObjectArray<mirror::StaticStorageBase> >  
  30.       initialized_static_storage(self,  
  31.                           AllocObjectArray<mirror::StaticStorageBase>(self, dex_file.NumTypeIds()));  
  32.   ......  
  33.   
  34.   dex_cache->Init(&dex_file,  
  35.                   location.get(),  
  36.                   strings.get(),  
  37.                   types.get(),  
  38.                   methods.get(),  
  39.                   fields.get(),  
  40.                   initialized_static_storage.get());  
  41.   return dex_cache.get();  
  42. }  
       这个函数定义在文件art/runtime/class_linker.cc中。

       我们要创建的Dex Cache使用java.lang.DexCache类来描述。java.lang.DexCache类对象保存在ART运行时的Image空间中,我们可以通过ClassLinker类的成员函数GetClassRoot来获得的。获得了用来描述java.lang.DexCache类的类对象之后,我们就可以调用Heap类的成员函数AllocObject在ART运行堆上分配一个DexCache对象了。关于ART运行时的Image空间和堆,我们接下来的文章中将会详细分析。

       Dex Cache的作用是用来缓存包含在一个DEX文件里面的类型(Type)、方法(Method)、域(Field)、字符串(String)和静态储存区(Static Storage)等信息。因此,我们需要为上述提到的信息分配储存空间。例如,对于类方法来说,我们需要创建一个ArtMethod对象指针数组,其中每一个ArtMethod对象都用来描述DEX文件里面的一个类方法。有了这些储存空间之后,最后就可以调用DexCache类的成员函数Init对刚才创建的Dex Cache进行初始化了。

       DexCache类的成员函数Init的实现如下所示:

[cpp]   view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. void DexCache::Init(const DexFile* dex_file,  
  2.                     String* location,  
  3.                     ObjectArray<String>* strings,  
  4.                     ObjectArray<Class>* resolved_types,  
  5.                     ObjectArray<ArtMethod>* resolved_methods,  
  6.                     ObjectArray<ArtField>* resolved_fields,  
  7.                     ObjectArray<StaticStorageBase>* initialized_static_storage) {  
  8.   ......  
  9.   
  10.   SetFieldObject(ResolvedMethodsOffset(), resolved_methods, false);  
  11.   ......  
  12.   
  13.   Runtime* runtime = Runtime::Current();  
  14.   if (runtime->HasResolutionMethod()) {  
  15.     // Initialize the resolve methods array to contain trampolines for resolution.  
  16.     ArtMethod* trampoline = runtime->GetResolutionMethod();  
  17.     size_t length = resolved_methods->GetLength();  
  18.     for (size_t i = 0; i < length; i++) {  
  19.       resolved_methods->SetWithoutChecks(i, trampoline);  
  20.     }  
  21.   }  
  22. }  
       这个函数定义在文件art/runtime/mirror/dex_cache.cc中。

       我们重点关注Dex Cache中的类方法对象数组的初始化。前面提到,DexCache类有一个类型为ObjectArray<ArtMethod>的resolved_methods_指针数组。我们通过DexCache类的成员函数ResolvedMethodsOffset可以知道它在DexCache类中的偏移值。有了这个偏移值之后,就可以调用父类Object的成员函数SetFieldObject来将参数resolved_methods描述的ArtMethod对象指针数组设置到当前正在初始化的DexCache对象的成员变量resolved_methods_去了。

       接下来重点就来了,DexCache类的成员变量resolved_methods_指向的ArtMethod对象指针数组中的每一个ArtMethod指针都会指向同一个Resolution Method。这个Resolution Method可以通过Runtime类的成员函数GetResolutionMethod获得,它的实现如下所示:

[cpp]   view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. class Runtime {  
  2.  public:  
  3.   ......  
  4.   
  5.   // Returns a special method that calls into a trampoline for runtime method resolution  
  6.   mirror::ArtMethod* GetResolutionMethod() const {  
  7.     CHECK(HasResolutionMethod());  
  8.     return resolution_method_;  
  9.   }  
  10.     
  11.   ......  
  12.    
  13.  private:  
  14.   ......  
  15.   
  16.   mirror::ArtMethod* resolution_method_;  
  17.   
  18.   ......  
  19. };  
        这个函数定义在文件rt/runtime/runtime.h中。

        Runtime类的成员函数GetResolutionMethod返回的是成员变量resolution_method_指向的一个ArtMethod对象。

        那么Runtime类的成员变量resolution_method_是什么时候初始化的呢?是在ART运行时的Image空间初始化过程中初始化的。在前面Android运行时ART加载OAT文件的过程分析一篇文章中,我们提到,ART运行时的Image空间创建完成之后,会调用ImageSpace类的成员函数Init来对它进行初始化。

        ImageSpace类的成员函数Init的实现如下所示:

[cpp]   view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. ImageSpace* ImageSpace::Init(const std::string& image_file_name, bool validate_oat_file) {  
  2.   ......  
  3.   
  4.   UniquePtr<File> file(OS::OpenFileForReading(image_file_name.c_str()));  
  5.   ......  
  6.   
  7.   ImageHeader image_header;  
  8.   bool success = file->ReadFully(&image_header, sizeof(image_header));  
  9.   ......  
  10.   
  11.   UniquePtr<MemMap> image_map(MemMap::MapFileAtAddress(nullptr, image_header.GetImageBitmapSize(),  
  12.                                                        PROT_READ, MAP_PRIVATE,  
  13.                                                        file->Fd(), image_header.GetBitmapOffset(),  
  14.   ......  
  15.   
  16.   Runtime* runtime = Runtime::Current();  
  17.   mirror::Object* resolution_method = image_header.GetImageRoot(ImageHeader::kResolutionMethod);  
  18.   runtime->SetResolutionMethod(down_cast<mirror::ArtMethod*>(resolution_method));  
  19.    
  20.   ......  
  21. }  
       这个函数定义在文件art/runtime/gc/space/image_space.cc中。

       ImageSpace类的成员函数Init首先是将Image文件的头部读取出来,并且根据得到的Image头部信息将Image文件映射到指定的内存位置。Image头部指定了ART运行时使用的Resolution Method在内存中的位置,可以通过ImageHeader类的成员函数GetImageRoot来获得。获得了这个Resolution Method之后,就可以调用Runtime类的成员函数SetResolutionMethod将它设置到ART运行时去了。

       前面说了那么多,好像还是没有发现为什么要给ART运行时设置一个Resolution Method。迷局就要准备解开了。在解开之前,我们首先要知道ART运行时使用的Resolution Method是长什么样的,也就是它是如何创建的。

       Resolution Method本质上就一个ArtMethod对象。当我们调用dex2oat工具将系统启动类翻译成本地机器指令时,会创建这个Resolution Method,并且将它保存在Image文件中。这样以后要使用这个Resolution Method时,就可以将对应的Image文件加载到内存获得。

       Resolution Method是通过调用Runtime类的成员函数CreateResolutionMethod来创建的,如下所示:

[cpp]   view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. mirror::ArtMethod* Runtime::CreateResolutionMethod() {  
  2.   mirror::Class* method_class = mirror::ArtMethod::GetJavaLangReflectArtMethod();  
  3.   Thread* self = Thread::Current();  
  4.   SirtRef<mirror::ArtMethod>  
  5.       method(self, down_cast<mirror::ArtMethod*>(method_class->AllocObject(self)));  
  6.   method->SetDeclaringClass(method_class);  
  7.   // TODO: use a special method for resolution method saves  
  8.   method->SetDexMethodIndex(DexFile::kDexNoIndex);  
  9.   // When compiling, the code pointer will get set later when the image is loaded.  
  10.   Runtime* r = Runtime::Current();  
  11.   ClassLinker* cl = r->GetClassLinker();  
  12.   method->SetEntryPointFromCompiledCode(r->IsCompiler() ? NULL : GetResolutionTrampoline(cl));  
  13.   return method.get();  
  14. }  
       这个函数定义在文件art/runtime/runtime.cc中。

       从Runtime类的成员函数CreateResolutionMethod的实现就可以看出,ART运行时的Resolution Method有以下两个特点:

       1. 它的Dex Method Index为DexFile::kDexNoIndex,这是因为它不代表任何的类方法。

       2. 由于上述原因,它没有相应的本地机器指令,因此它不能执行。

       回想我们前面分析的函数artQuickResolutionTrampoline,它通过ArtMethod类的成员函数IsRuntimeMethod来判断一个ArtMethod对象是否是一个运行时方法。ArtMethod类的成员函数IsRuntimeMethod的实现如下所示:

[cpp]   view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. inline bool ArtMethod::IsRuntimeMethod() const {  
  2.   return GetDexMethodIndex() == DexFile::kDexNoIndex;  
  3. }  
       这个函数定义在文件art/runtime/mirror/art_method-inl.h文件中。

       如果一个ArtMethod的Dex Method Index等于DexFile::kDexNoIndex,那么它就被认为是运行时方法。当然,Resoultion Method只是运行方法的其中一种。其中类型的运行时方法我们后面分析ART运行时的Image空间的文章时再讲解。

       如前面所述,函数artQuickResolutionTrampoline一旦发现一个接着要调用的类方法是一个运行时方法时,就会调用ClassLinker类的成员函数ResolveMethod来对其进行解析,也就是找到真正要被调用的类方法。

       ClassLinker类的成员函数ResolveMethod的实现如下所示:

[cpp]   view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. inline mirror::ArtMethod* ClassLinker::ResolveMethod(uint32_t method_idx,  
  2.                                                      const mirror::ArtMethod* referrer,  
  3.                                                      InvokeType type) {  
  4.   mirror::ArtMethod* resolved_method =  
  5.       referrer->GetDexCacheResolvedMethods()->Get(method_idx);  
  6.   if (UNLIKELY(resolved_method == NULL || resolved_method->IsRuntimeMethod())) {  
  7.     mirror::Class* declaring_class = referrer->GetDeclaringClass();  
  8.     mirror::DexCache* dex_cache = declaring_class->GetDexCache();  
  9.     mirror::ClassLoader* class_loader = declaring_class->GetClassLoader();  
  10.     const DexFile& dex_file = *dex_cache->GetDexFile();  
  11.     resolved_method = ResolveMethod(dex_file, method_idx, dex_cache, class_loader, referrer, type);  
  12.   }  
  13.   return resolved_method;  
  14. }   
       这个函数定义在文件art/runtime/class_linker-inl.h中。

       参数method_idx描述的是接下来将要被调用类方法在DEX文件的索引。注意,每一个类方法在宿主类中有一个索引,在对应的DEX文件中也有一个索引。这两个索引是不一样的,根据前面Android运行时ART加载类和方法的过程分析一文,前一个索引用来查找一个类方法的本地机器指令。而后面一个索引,自然的就是用来DEX文件中找到对应的类方法描述信息了。这意味着一旦知道一个类方法在DEX文件的索引,那么就可以在对应的DEX文件中对该类方法进行解析了。一旦解析完成,自然就可以知道接下来要被调用的类方法是什么了。

        参数referrer指向的ArtMethod对象描述的是调用者(类方法)。每一个类方法都关联有一个ArtMethod对象指针数组,这个ArtMethod对象指针数组实际上就是我们在前面提到的Dex Cache中的ArtMethod对象指针数组。同时,每一个类对象(Class)也关联有一个Dex Cache。这个Dex Cache实际上就是与包含该类的DEX文件相关联的Dex Cache。为了搞清楚上述关系,我们回顾一下前面Android运行时ART加载类和方法的过程分析一文提到的ClassLinker类的两个成员函数DefineClass和LoadMethod。

        在ClassLinker类的成员函数DefineClass中,会给每一个加载的类关联一个Dex Cache,如下所示:

[cpp]   view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. mirror::Class* ClassLinker::DefineClass(const char* descriptor,  
  2.                                         mirror::ClassLoader* class_loader,  
  3.                                         const DexFile& dex_file,  
  4.                                         const DexFile::ClassDef& dex_class_def) {  
  5.   Thread* self = Thread::Current();  
  6.   SirtRef<mirror::Class> klass(self, NULL);  
  7.   ......  
  8.   
  9.   klass->SetDexCache(FindDexCache(dex_file));  
  10.   LoadClass(dex_file, dex_class_def, klass, class_loader);  
  11.   ......  
  12.   
  13. }  
       这个函数定义在文件art/runtime/class_linker.cc中。

       变量klass描述的就是正在加载的类,在对其进行加载之前,首先会调用ClassLinker类的成员函数FindDexCache找到与参数dex_file描述的DEX文件相关联的Dex Cache。有了这个Dex Cache,就可以将它设置到kclass指向的Class对象中去了。注意,参数dex_file描述的DEX文件就是包含正在加载的类的文件。

       在ClassLinker类的成员函数LoadMethod中,会给每一个加载的类方法设置一个DEX文件类方法索引,以及关联一个ArtMethod对象指针数组,如下所示:

[cpp]   view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. mirror::ArtMethod* ClassLinker::LoadMethod(Thread* self, const DexFile& dex_file,  
  2.                                            const ClassDataItemIterator& it,  
  3.                                            SirtRef<mirror::Class>& klass) {  
  4.   uint32_t dex_method_idx = it.GetMemberIndex();  
  5.   ......  
  6.   
  7.   mirror::ArtMethod* dst = AllocArtMethod(self);  
  8.   ......  
  9.   
  10.   dst->SetDexMethodIndex(dex_method_idx);  
  11.   ......  
  12.   
  13.   dst->SetDexCacheResolvedMethods(klass->GetDexCache()->GetResolvedMethods());  
  14.   ......  
  15.   
  16. }  
       这个函数定义在文件art/runtime/class_linker.cc中。

       变量dst描述的便是正在加载的类方法,我们可以通过参数it获得它在DEX文件中的类方法索引,并且将该索引设置到变量dst指向的ArtMethod对象中去。

       参数klass描述是正在加载的类方法所属的类,前面我们已经给这个类关联过一个Dex Cache了,因此,只要将重新获得该Dex Cache,并且获得该Dex Cache里面的ArtMethod对象指针数组,那么就可以将ArtMethod对象指针数组设置到正在加载的类方法去了。

        从ClassLinker类的两个成员函数DefineClass和LoadMethod的实现就可以看出,同一个DEX文件的所有类关联的Dex Cache都是同一个Dex Cache,并且属于这些类的所有类方法关联的ArtMethod对象指针数组都是该Dex Cache内部的ArtMethod对象指针数组。这个结论对我们理解ClassLinker类的成员函数ResolveMethod的实现很重要。

       在ClassLinker类的成员函数ResolveMethod中,我们知道的是调用者以及被调用者在DEX文件中的类方法索引,因此,我们就可以从与调用者关联的ArtMethod对象指针数组中找到接下来真正要被调用的类方法了。

       Dex Cache内部的ArtMethod对象指针数组的每一个ArtMethod指针一开始都是指向ART运行时的Resolution Method。但是每当一个类方法第一次被调用的时候,函数artQuickResolutionTrampoline能够根据捕捉到这种情况,并且根据调用者和调用指令的信息,通过ClassLinker类的成员函数ResolveMethod找到接下来真正要被调用的类方法。查找的过程就是解析类方法的过程,这是一个漫长的过程,因为要解析DEX文件。不过一旦接下来要被调用的类方法解析完成,就会创建另外一个ArtMethod对象来描述解析得到的信息,并且将该ArtMethod对象保存在对应的Dex Cache内部的ArtMethod对象指针数组的相应位置去。这样下次该类方法再被调用时,就不用再次解析了。

       从上面的分析我们还可以进一步得到以下的结论:

       1. 在生成的本地机器指令中,一个类方法调用另外一个类方法并不是直接进行的,而是通过Dex Cache来间接进行的。

       2. 通过Dex Cache间接调用类方法,可以做到延时解析类方法,也就是等到类方法第一次被调用时才解析,这样可以避免解析那些永远不会被调用的类方法。

       3. 一个类方法只会被解析一次,解析的结果保存在Dex Cache中,因此当该类方法再次被调用时,就可以直接从Dex Cache中获得所需要的信息。

       以上就是Dex Cache在ART运行时所起到的作用了,理解这一点对阅读ART运行时的源代码非常重要。

       有了以上的知识点之后,接下来我们就可以真正地分析类方法的调用过程了。在Android运行时ART加载类和方法的过程分析一文中,我们通过AndroidRuntime类的成员函数start来分析类和类方法的加载过程。本文同样是从这个函数开始分析类方法的执行过程,如下所示:

[cpp]   view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. void AndroidRuntime::start(const char* className, const char* options)  
  2. {  
  3.     ......  
  4.   
  5.     char* slashClassName = toSlashClassName(className);  
  6.     jclass startClass = env->FindClass(slashClassName);  
  7.     if (startClass == NULL) {  
  8.         ALOGE("JavaVM unable to locate class '%s'\n", slashClassName);  
  9.         /* keep going */  
  10.     } else {  
  11.         jmethodID startMeth = env->GetStaticMethodID(startClass, "main",  
  12.             "([Ljava/lang/String;)V");  
  13.         if (startMeth == NULL) {  
  14.             ALOGE("JavaVM unable to find main() in '%s'\n", className);  
  15.             /* keep going */  
  16.         } else {  
  17.             env->CallStaticVoidMethod(startClass, startMeth, strArray);   
  18.             ......  
  19.         }  
  20.     }  
  21.   
  22.     ......  
  23. }  
       这个函数定义在文件frameworks/base/core/jni/AndroidRuntime.cpp。

       找到要调用类方法之后,就可以调用JNI接口CallStaticVoidMethod来执行它了。

       根据我们在Android运行时ART加载类和方法的过程分析一文的分析可以知道,JNI接口CallStaticVoidMethod由JNI类的成员函数CallStaticVoidMethod实现,如下所示:

[cpp]   view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. class JNI {  
  2.  public:  
  3.   ......  
  4.   
  5.   static void CallStaticVoidMethod(JNIEnv* env, jclass, jmethodID mid, ...) {  
  6.     va_list ap;  
  7.     va_start(ap, mid);  
  8.     CHECK_NON_NULL_ARGUMENT(CallStaticVoidMethod, mid);  
  9.     ScopedObjectAccess soa(env);  
  10.     InvokeWithVarArgs(soa, NULL, mid, ap);  
  11.     va_end(ap);  
  12.   }  
  13.   
  14.   ......  
  15. };  
        这个函数定义在文件art/runtime/jni_internal.cc中。

        JNI类的成员函数CallStaticVoidMethod实际上又是通过全局函数InvokeWithVarArgs来调用参数mid指定的方法的,如下所示:

[cpp]   view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. static JValue InvokeWithVarArgs(const ScopedObjectAccess& soa, jobject obj,  
  2.                                 jmethodID mid, va_list args)  
  3.     SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {  
  4.   ArtMethod* method = soa.DecodeMethod(mid);  
  5.   Object* receiver = method->IsStatic() ? NULL : soa.Decode<Object*>(obj);  
  6.   MethodHelper mh(method);  
  7.   JValue result;  
  8.   ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());  
  9.   arg_array.BuildArgArray(soa, receiver, args);  
  10.   InvokeWithArgArray(soa, method, &arg_array, &result, mh.GetShorty()[0]);  
  11.   return result;  
  12. }  
        这个函数定义在文件art/runtime/jni_internal.cc中。

        函数InvokeWithVarArgs将调用参数封装在一个数组中,然后再调用另外一个函数InvokeWithArgArray来参数mid指定的方法。参数mid实际上是一个ArtMethod对象指针,因此,我们可以将它转换为一个ArtMethod指针,于是就可以得到被调用类方法的相关信息了。

        函数InvokeWithArgArray的实现如下所示:

[cpp]   view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. void InvokeWithArgArray(const ScopedObjectAccess& soa, ArtMethod* method,  
  2.                         ArgArray* arg_array, JValue* result, char result_type)  
  3.     SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {  
  4.   uint32_t* args = arg_array->GetArray();  
  5.   if (UNLIKELY(soa.Env()->check_jni)) {  
  6.     CheckMethodArguments(method, args);  
  7.   }  
  8.   method->Invoke(soa.Self(), args, arg_array->GetNumBytes(), result, result_type);  
  9. }  
       这个函数定义在文件art/runtime/jni_internal.cc中。

       函数InvokeWithArgArray通过ArtMethod类的成员函数Invoke来调用参数method指定的类方法。

       ArtMethod类的成员函数Invoke的实现如下所示:

[cpp]   view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. void ArtMethod::Invoke(Thread* self, uint32_t* args, uint32_t args_size, JValue* result,  
  2.                        char result_type) {  
  3.   ......  
  4.   
  5.   // Push a transition back into managed code onto the linked list in thread.  
  6.   ManagedStack fragment;  
  7.   self->PushManagedStackFragment(&fragment);  
  8.   
  9.   Runtime* runtime = Runtime::Current();  
  10.   // Call the invoke stub, passing everything as arguments.  
  11.   if (UNLIKELY(!runtime->IsStarted())) {  
  12.     ......  
  13.     if (result != NULL) {  
  14.       result->SetJ(0);  
  15.     }  
  16.   } else {  
  17.     const bool kLogInvocationStartAndReturn = false;  
  18.     if (GetEntryPointFromCompiledCode() != NULL) {  
  19.       ......  
  20. #ifdef ART_USE_PORTABLE_COMPILER  
  21.       (*art_portable_invoke_stub)(this, args, args_size, self, result, result_type);  
  22. #else  
  23.       (*art_quick_invoke_stub)(this, args, args_size, self, result, result_type);  
  24. #endif  
  25.       if (UNLIKELY(reinterpret_cast<int32_t>(self->GetException(NULL)) == -1)) {  
  26.         // Unusual case where we were running LLVM generated code and an  
  27.         // exception was thrown to force the activations to be removed from the  
  28.         // stack. Continue execution in the interpreter.  
  29.         self->ClearException();  
  30.         ShadowFrame* shadow_frame = self->GetAndClearDeoptimizationShadowFrame(result);  
  31.         self->SetTopOfStack(NULL, 0);  
  32.         self->SetTopOfShadowStack(shadow_frame);  
  33.         interpreter::EnterInterpreterFromDeoptimize(self, shadow_frame, result);  
  34.       }  
  35.       ......  
  36.     } else {  
  37.       ......  
  38.       if (result != NULL) {  
  39.         result->SetJ(0);  
  40.       }  
  41.     }  
  42.   }  
  43.   
  44.   // Pop transition.  
  45.   self->PopManagedStackFragment(fragment);  
  46. }  

       这个函数定义在文件rt/runtime/mirror/art_method.cc中。

       ArtMethod类的成员函数Invoke的执行逻辑如下所示:

       1. 构造一个类型为ManagedStack的调用栈帧。这些调用栈帧会保存在当前线程对象的一个链表中,在进行垃圾收集会使用到。

       2. 如果ART运行时还没有启动,那么这时候是不能够调用任何类方法的,因此就直接返回。否则,继续往下执行。

       3. 从前面的函数LinkCode可以知道,无论一个类方法是通过解释器执行,还是直接以本地机器指令执行,均可以通过ArtMethod类的成员函数GetEntryPointFromCompiledCode获得其入口点,并且该入口不为NULL。不过,这里并没有直接调用该入口点,而是通过Stub来间接调用。这是因为我们需要设置一些特殊的寄存器。如果ART运行时使用的是Quick类型的后端,那么使用的Stub就为art_quick_invoke_stub。否则的话,使用的Stub就为art_portable_invoke_stub。

       4. 如果在执行类方法的过程中,出现了一个值为-1的异常,那么就在运行生成的本地机器指令出现了问题,这时候就通过解释器来继续执行。每次通过解释器执行一个类方法的时候,都需要构造一个类型为ShadowFrame的调用栈帧。这些调用栈帧同样是在垃圾回收时使用到。

       接下来我们主要是分析第3步,并且假设目标CPU体系架构为ARM,以及ART运行时使用的是Quick类型的后端,这样第3步使用的Stub就为函数art_quick_invoke_stub,它的实现如下所示:

[plain]   view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1.     /*  
  2.      * Quick invocation stub.  
  3.      * On entry:  
  4.      *   r0 = method pointer  
  5.      *   r1 = argument array or NULL for no argument methods  
  6.      *   r2 = size of argument array in bytes  
  7.      *   r3 = (managed) thread pointer  
  8.      *   [sp] = JValue* result  
  9.      *   [sp + 4] = result type char  
  10.      */  
  11. ENTRY art_quick_invoke_stub  
  12.     push   {r0, r4, r5, r9, r11, lr}       @ spill regs  
  13.     .save  {r0, r4, r5, r9, r11, lr}  
  14.     .pad #24  
  15.     .cfi_adjust_cfa_offset 24  
  16.     .cfi_rel_offset r0, 0  
  17.     .cfi_rel_offset r4, 4  
  18.     .cfi_rel_offset r5, 8  
  19.     .cfi_rel_offset r9, 12  
  20.     .cfi_rel_offset r11, 16  
  21.     .cfi_rel_offset lr, 20  
  22.     mov    r11, sp                         @ save the stack pointer  
  23.     .cfi_def_cfa_register r11  
  24.     mov    r9, r3                          @ move managed thread pointer into r9  
  25.     mov    r4, #SUSPEND_CHECK_INTERVAL     @ reset r4 to suspend check interval  
  26.     add    r5, r2, #16                     @ create space for method pointer in frame  
  27.     and    r5, #0xFFFFFFF0                 @ align frame size to 16 bytes  
  28.     sub    sp, r5                          @ reserve stack space for argument array  
  29.     add    r0, sp, #4                      @ pass stack pointer + method ptr as dest for memcpy  
  30.     bl     memcpy                          @ memcpy (dest, src, bytes)  
  31.     ldr    r0, [r11]                       @ restore method*  
  32.     ldr    r1, [sp, #4]                    @ copy arg value for r1  
  33.     ldr    r2, [sp, #8]                    @ copy arg value for r2  
  34.     ldr    r3, [sp, #12]                   @ copy arg value for r3  
  35.     mov    ip, #0                          @ set ip to 0  
  36.     str    ip, [sp]                        @ store NULL for method* at bottom of frame  
  37.     ldr    ip, [r0, #METHOD_CODE_OFFSET]   @ get pointer to the code  
  38.     blx    ip                              @ call the method  
  39.     mov    sp, r11                         @ restore the stack pointer  
  40.     ldr    ip, [sp, #24]                   @ load the result pointer  
  41.     strd   r0, [ip]                        @ store r0/r1 into result pointer  
  42.     pop    {r0, r4, r5, r9, r11, lr}       @ restore spill regs  
  43.     .cfi_adjust_cfa_offset -24  
  44.     bx     lr  
  45. END art_quick_invoke_stub  
        这个函数定义在文件art/runtime/arch/arm/quick_entrypoints_arm.S中。

        函数art_quick_invoke_stub前面的注释列出了 函数art_quick_invoke_stub被调用的时候,寄存器r0-r3的值,以及调用栈顶端的两个值。其中,r0指向当前被调用的类方法,r1指向一个参数数组地址,r2记录参数数组的大小,r3指向当前线程。调用栈顶端的两个元素分别用来保存调用结果及其类型。

       无论一个类方法是通过解释器执行,还是直接以本地机器指令执行,当它被调用时,都有着特殊的调用约定。其中,寄存器r9指向用来描述当前调用线程的一个Thread对象地址,这样本地机器指令在执行的过程中,就可以通过它来定位线程的相关信息,例如我们在前面描述的各种函数跳转表;寄存器r4初始化为一个计数值,当计数值递减至0时,就需要检查当前线程是否已经被挂起;寄存器r0指向用来描述被调用类方法的一个ArtMethod对象地址。

        所有传递给被调用方法的参数都会保存在调用栈中,因此,在进入类方法的入口点之前,需要在栈中预留足够的位置,并且通过调用memcpy函数将参数都拷贝到预留的栈位置去。同时,前面三个参数还会额外地保存在寄存器r1、r2和r3中。这样对于小于等于3个参数的类方法,就可以通过访问寄存器来快速地获得参数。

       注意,传递给被调用类方法的参数并不是从栈顶第一个位置(一个位置等于一个字长,即4个字节)开始保存的,而是从第二个位置开始的,即sp + 4。这是因为栈顶的第一个位置是预留用来保存用来描述当调用类方法(Caller)的ArtMethod对象地址的。由于函数art_quick_invoke_stub是用来从外部进入到ART运行时的,即不存在调用类方法,因此这时候栈顶第一个位置会被设置为NULL。

       准备好调用栈帧之后,就找到从用来描述当前调用类方法的ArtMethod对象地址偏移METHOD_CODE_OFFSET处的值,并且以该值作为类方法的执行入口点,最后通过blx指令跳过去执行。

        METHOD_CODE_OFFSET的值定义在文件art/runtime/asm_support.h中,值为40,如下所示:

[cpp]   view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. // Offset of field Method::entry_point_from_compiled_code_  
  2. #define METHOD_CODE_OFFSET 40  
        参照注释,可以知道,在ArtMethod对象中,偏移值为40的成员变量为entry_point_from_compiled_code_,而该成员变量就是调用函数LinkCode链接类方法时调用ArtMethod类的成员函数SetEntryPointFromCompiledCode设置的。相应的,可以通过ArtMethod类的成员函数GetEntryPointFromCompiledCode获得该成员变量的值,如前面ArtMethod类的成员函数Invoke所示。

        在ARM体系架构中,当调用blx指令跳转到指定的地址执行,那么位于blx下面的一条指令会保存在寄存器lr中,而被调用类方法在执行前,一般又会通过SETUP_REF_AND_ARGS_CALLEE_SAVE_FRAME宏保存指定的寄存器。

        宏SETUP_REF_AND_ARGS_CALLEE_SAVE_FRAME的定义如下所示:

[cpp]   view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1.     /* 
  2.      * Macro that sets up the callee save frame to conform with 
  3.      * Runtime::CreateCalleeSaveMethod(kRefsAndArgs). Restoration assumes non-moving GC. 
  4.      */  
  5. .macro SETUP_REF_AND_ARGS_CALLEE_SAVE_FRAME  
  6.     push {r1-r3, r5-r8, r10-r11, lr}  @ 10 words of callee saves  
  7.     .save {r1-r3, r5-r8, r10-r11, lr}  
  8.     .cfi_adjust_cfa_offset 40  
  9.     .cfi_rel_offset r1, 0  
  10.     .cfi_rel_offset r2, 4  
  11.     .cfi_rel_offset r3, 8  
  12.     .cfi_rel_offset r5, 12  
  13.     .cfi_rel_offset r6, 16  
  14.     .cfi_rel_offset r7, 20  
  15.     .cfi_rel_offset r8, 24  
  16.     .cfi_rel_offset r10, 28  
  17.     .cfi_rel_offset r11, 32  
  18.     .cfi_rel_offset lr, 36  
  19.     sub sp, #8                        @ 2 words of space, bottom word will hold Method*  
  20.     .pad #8  
  21.     .cfi_adjust_cfa_offset 8  
  22. .endm  
       这个宏定义在文件art/runtime/arch/arm/quick_entrypoints_arm.S中。

       寄存器r1-r3、r5-r8、r10-r11和lr在栈上依次从低地址往高地址保存,即从栈顶开始保存。除了保存上述寄存器之外,还会在栈上预留两个位置(每个位置等于一个字长,即4个字节),其中,栈顶第一个位置用来保存用来描述当前被调用的类方法的ArtMethod对象地址,第二个位置可能是设计用来保存寄存器r0的,但是目前还没有发现这样使用的地方。宏SETUP_REF_AND_ARGS_CALLEE_SAVE_FRAME的使用情景之一可能参考我们前面分析的函数art_quick_resolution_trampoline。

       现在还有一个问题是,上面提到的栈顶第一位置是什么时候会被设置为用来描述当前被调用的类方法的ArtMethod对象地址的呢?因为宏SETUP_REF_AND_ARGS_CALLEE_SAVE_FRAME只是在栈上预留这个位置,但是没有设置这个位置的值。以我们前面分析的函数artQuickResolutionTrampoline为例,一开始这个位置被函数FinishCalleeSaveFrameSetup设置为一个类型为Runtime::kRefsAndArgs的运行时方法,这是因为这时候我们还不知道真正被调用的类方法是什么。类型为Runtime::kRefsAndArgs的运行时方法与我们前面提到的Resolution Method的作用有点类似,虽然它们本身是一个ArtMethod对象,但是它们的真正作用是用来描述其它的ArtMethod。例如,类型为Runtime::kRefsAndArgs的运行时方法要描述的就是真正要被调用类的类方法会在栈上保存r1-r3、r5-r8、r10-r11和lr这10个寄器,并且会在栈上预留两个额外的位置,其中的一个位置用来保存真正被调用的类方法。果然,等到函数artQuickResolutionTrampoline找到真正被调用的类方法之后,就会将相应的ArtMethod对象地址保存在栈顶上。这正是到函数artQuickResolutionTrampoline返回前所做的事情。

       综合上面的分析,我们就得到ART运行时类方法的调用约定,如下所示:

[plain]   view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. | argN       |  |                       -------------                    
  2. | ...        |  |                        |  
  3. | arg4       |  |                        |    
  4. | arg3 spill |  |  Caller's frame        |  art_quick_invoke_stub  
  5. | arg2 spill |  |                        |  
  6. | arg1 spill |  |                        |  
  7. | Method*    | ---                       |  
  8. | LR         |                          ------------  
  9. | ...        |    callee saves              |    |  
  10. | R3         |    arg3                      |    |SETUP_REF_AND_ARGS_CALLEE_SAVE_FRAME  
  11. | R2         |    arg2                      |    |  
  12. | R1         |    arg1                      | -------  
  13. | R0         |                              | art_quick_resolution_trampoline  
  14. | Method*    |  <- sp                   ------------- artQuickResolutionTrampoline  
       左边显示了一个类方法(Caller)调用另外一个类方法(Callee)时栈的内存布局情况,右边显示了负责安排这些内存布局每一部分的一个情景。

       回到前面的函数art_quick_invoke_stub中,通过blx指令调用指定的类方法结束后,结果就保存在r0和r1两个寄存器中,其中一个表示返回值,另外一个表示返回值类型,最后通过strd指令将这两个寄存器的值拷贝到栈上指定的内存地址去,实际上就将调用结果返回给调用者指定的两个变量去。

       这样,我们就分析完成类方法的执行过程了,也基本上解释上前面分析的函数LinkCode所涉及到关键函数,包括artInterpreterToCompiledCodeBridge、GetCompiledCodeToInterpreterBridge/GetQuickToInterpreterBridge/art_quick_to_interpreter_bridge/artQuickToInterpreterBridge、artInterpreterToInterpreterBridge、GetResolutionTrampoline/GetQuickResolutionTrampoline/GetQuickResolutionTrampoline/art_quick_resolution_trampoline/artQuickResolutionTrampoline、ArtMethod::SetEntryPointFromCompiledCode/ArtMethod::GetEntryPointFromCompiledCode和ArtMethod::SetEntryPointFromInterpreter等。

       为了完整性,接下来我们继续分析一下与函数ArtMethod::SetEntryPointFromInterpreter相对应的函数ArtMethod::GetEntryPointFromInterpreter,以便可以看到由前者设置的函数入口点是在什么情况下被使用的。

      前面提到,ArtMethod类的成员函数SetEntryPointFromInterpreter设置的入口点是用来给解释器调用另外一个类方法时使用的。ART运行时的解释器主要由art/runtime/interpreter/interpreter.cc文件中,当它需要调用另外一个类方法时,就会通过函数DoInvoke来实现,如下所示:

[plain]   view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. template<InvokeType type, bool is_range, bool do_access_check>  
  2. static bool DoInvoke(Thread* self, ShadowFrame& shadow_frame,  
  3.                      const Instruction* inst, JValue* result) {  
  4.   ......  
  5.   uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();  
  6.   uint32_t vregC = (is_range) ? inst->VRegC_3rc() : inst->VRegC_35c();  
  7.   Object* receiver = (type == kStatic) ? NULL : shadow_frame.GetVRegReference(vregC);  
  8.   ArtMethod* method = FindMethodFromCode(method_idx, receiver, shadow_frame.GetMethod(), self,  
  9.                                          do_access_check, type);  
  10.   ......  
  11.   
  12.   if (LIKELY(Runtime::Current()->IsStarted())) {  
  13.     (method->GetEntryPointFromInterpreter())(self, mh, code_item, new_shadow_frame, result);  
  14.   } else {  
  15.     UnstartedRuntimeInvoke(self, mh, code_item, new_shadow_frame, result, num_regs - num_ins);  
  16.   }  
  17.   return !self->IsExceptionPending();  
  18. }  
       这个函数定义在art/runtime/interpreter/interpreter.cc文件中。

       通过调用指令中的指定的Dex Method Index,我们可以通过另外一个函数FindMethodFromCode找到被调用的类方法,通过ArtMethod对象method来描述。有了这个ArtMethod对象后,我们就可以调用它的成员函数GetEntryPointFromInterpreter来获得接下来要被调用类方法的执行入口点。从函数LinkCode的实现可以知道,通过ArtMethod类的成员函数GetEntryPointFromInterpreter获得的类方法执行入口点有可能是用来进入解释器的,也有可能是用来进入到类方法的本地机器指令去的。

        至此,我们就分析完成ART运行时执行一个类方法的过程以及在执行过程中涉及到的各种关键函数了。本文与其说是分析类方法的执行过程,不如说是分析ART运行时的实现原理。理解了本文分析到的各个关键函数之后,相信对ART运行时就会有一个清晰的认识了。

 

原文地址:http://blog.csdn.net/luoshengyang/article/details/40289405

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值