日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android5.1.1源码 - 让某个APP以解释执行模式运行

發布時間:2025/3/15 Android 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android5.1.1源码 - 让某个APP以解释执行模式运行 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

[實踐] Android5.1.1源碼 - 讓某個APP以解釋執行模式運行

@(Android研究)[Android5.1.1|APP解釋執行]

前言

本文的實踐修改了Android5.1.1的源碼。

本文只簡單的講了一下原理。在“實踐”一節講了具體做法。

本文的內容涉及Art模式下dex加載的知識,想要詳細了解這部分知識可以去看老羅的文章:?Android運行時ART簡要介紹和學習計劃?Android運行時ART加載OAT文件的過程分析?Android運行時ART加載類和方法的過程分析Android運行時ART執行類方法的過程分析

本文的內容涉及zygote,如果不知道zygote是什么,或者好奇zygote如何啟動,可以去看老羅的文章:?Android系統進程Zygote啟動過程的源代碼分析

老羅的文章分析的是Android2.3的源碼,所以下面提到的與zygote有關的函數在老羅的文章里面可能沒有,如果想要對下面提到的與zygote有關的函數有一個簡單的了解可以看我的文章:Android5.1.1源碼 - zygote fork出的子進程如何權限降級

原理簡介

怎么才能讓方法解釋執行

在函數ClassLinker::LinkCode中會鏈接dex中的方法代碼,這個函數的定義在文件"art/runtime/class_linker.cc"中,下面是它的源碼(這里只列出了與本文有關的部分):

void ClassLinker::LinkCode(Handle<mirror::ArtMethod> method, const OatFile::OatClass* oat_class,const DexFile& dex_file, uint32_t dex_method_index,uint32_t method_index) {......bool enter_interpreter = NeedsInterpreter(...);......if (method->IsStatic() && !method->IsConstructor()) {......} else if (enter_interpreter) {if (!method->IsNative()) {// Set entry point from compiled code if there's no code or in interpreter only mode.method->SetEntryPointFromQuickCompiledCode(GetQuickToInterpreterBridge());......} else {......}} else {......}......}

在這個函數中調用了NeedsInterpreter函數判斷當前方法是否要解釋執行,如果返回值為true,即局部變量enter_interpreter被賦值為true,那么調用ArtMethod類中的SetEntryPointFromQuickCompiledCode函數并將GetQuickToInterpreterBridge()的返回值傳入,GetQuickToInterpreterBridge()函數返回用于解釋執行的函數的入口地址,這個入口函數解釋執行dex中的方法。

那么現在來看看NeedsInterpreter函數,這個函數的定義在文件"art/runtime/class_linker.cc"中,下面是它的源碼:

// Returns true if the method must run with interpreter, false otherwise. static bool NeedsInterpreter(......// If interpreter mode is enabled, every method (except native and proxy) must// be run with interpreter.return Runtime::Current()->GetInstrumentation()->InterpretOnly() &&!method->IsNative() && !method->IsProxyMethod(); }

當"Runtime::Current()->GetInstrumentation()->InterpretOnly()"返回true且不是本地方法和代理方法,那么這個函數就會返回true,否則返回false。

InterpretOnly函數是Instrumentation類的成員函數,它的函數定義在文件"art/runtime/instrumentation.h"中,下面是它的源碼:

// Called by ArtMethod::Invoke to determine dispatch mechanism. bool InterpretOnly() const {return interpret_only_; }

interpret_only_是類Instrumentation的成員變量,是布爾類型。可以發現InterpretOnly函數僅僅是將"interpret_only_"返回,如果將interpret_only_設置為true,那么根據上文分析,所有“非本地且非代理”方法都將被解釋執行。

那么如何將interpret_only_設置為true哪,在Instrumentation類中有一個ForceInterpretOnly函數,下面是這個函數的源碼:

void ForceInterpretOnly() {interpret_only_ = true;forced_interpret_only_ = true; }

這個函數是Instrumentation類的公有成員函數,所以直接調用這個函數即可將interpret_only_設置為true。

這里有一個問題,將interpret_only_設置為true,那么“非本地且非代理”方法在鏈接代碼時都將被設置成解釋執行,那么會不會影響到其他的APP進程?不會,因為ClassLinker::LinkCode函數對方法的鏈接是在APP進程的內存中進行的,所以這個操作并不會影響到其他進程。

這里進行一個小節,當執行"Runtime::Current()->GetInstrumentation()->ForceInterpretOnly()"語句時,會把Instrumentation對象的interpret_only_成員變量設置為true。那么當方法是“非本地且非代理”方法時,NeedsInterpreter函數將返回true,那么在ClassLinker::LinkCode函數中會將這個方法設置為解釋執行。

如果要將APP中所有方法都設置為解釋執行,那么就需要在鏈接APP的dex中的方法代碼之前執行"Runtime::Current()->GetInstrumentation()->ForceInterpretOnly()"語句。

調用ForceInterpretOnly函數的時機

我的辦法是在EnableDebugFeatures函數中調用ForceInterpretOnly函數,在這一節中會先說明Android如何執行到EnableDebugFeatures函數,然后會說明在EnableDebugFeatures函數中調用ForceInterpretOnly函數的好處。

所有的Android應用進程都是zygote fork出來的,fork APP進程時的函數調用路徑:

|- forkAndSpecialize - java方法|- com_android_internal_os_Zygote_nativeForkAndSpecialize - native函數|- ForkAndSpecializeCommon - native函數

調用完ForkAndSpecializeCommon函數后APP進程就被fork出來了。

ForkAndSpecializeCommon函數定義在文件"frameworks/base/core/jni/com_android_internal_os_Zygote.cpp"中,下面它的源碼:

static pid_t ForkAndSpecializeCommon(JNIEnv* env, uid_t uid, gid_t gid, jintArray javaGids, jint debug_flags, jobjectArray javaRlimits, jlong permittedCapabilities, jlong effectiveCapabilities, jint mount_external, jstring java_se_info, jstring java_se_name, bool is_system_server, jintArray fdsToClose, jstring instructionSet, jstring dataDir) {......pid_t pid = fork();if (pid == 0) {......env->CallStaticVoidMethod(gZygoteClass, gCallPostForkChildHooks, debug_flags,is_system_server ? NULL : instructionSet);......} else if (pid > 0) {// the parent process}return pid; }

gCallPostForkChildHooks是一個全局變量,它在com_android_internal_os_Zygote.cpp文件的register_com_android_internal_os_Zygote函數中被初始化。

env->CallStaticVoidMethod(...)語句調用了Java方法"Zygote.callPostForkChildHooks"。

下面是Zygote.callPostForkChildHooks方法的源碼,這個方法在文件"frameworks/base/core/java/com/android/internal/os/Zygote.java"中:

private static void callPostForkChildHooks(int debugFlags, String instructionSet) {long startTime = SystemClock.elapsedRealtime();VM_HOOKS.postForkChild(debugFlags, instructionSet);checkTime(startTime, "Zygote.callPostForkChildHooks"); }

VM_HOOKS是Zygote類的成員變量,下面是它的定義:

private static final ZygoteHooks VM_HOOKS = new ZygoteHooks();

VM_HOOKS.postForkChild調用的就是ZygoteHooks類中的成員方法postForkChild,這個方法在文件"libcore/dalvik/src/main/java/dalvik/system/ZygoteHooks.java"中,下面是它的源碼:

/** * Called by the zygote in the child process after every fork. The debug * flags from {@code debugFlags} are applied to the child process. The string * {@code instructionSet} determines whether to use a native bridge. */ public void postForkChild(int debugFlags, String instructionSet) {nativePostForkChild(token, debugFlags, instructionSet); }

這個方法中調用了native函數nativePostForkChild,nativePostForkChild函數的C層代碼在文件"/home/sowuy/android/system/art/runtime/native/dalvik_system_ZygoteHooks.cc"中,下面是它的源碼:

static void ZygoteHooks_nativePostForkChild(JNIEnv* env, jclass, jlong token, jint debug_flags,jstring instruction_set) {......EnableDebugFeatures(debug_flags);...... }

我將在EnableDebugFeatures函數中調用ForceInterpretOnly函數,原因有三點:

  • EnableDebugFeatures函數參數接收的是一個標志,我可以設置一個新的標志位用來表示是否需要調用ForceInterpretOnly函數。
  • 每次APP啟動的時候都會執行EnableDebugFeatures函數。
  • EnableDebugFeatures函數被調用的時機好,它運行在fork出的APP進程中,并且在鏈接APP的dex中的方法前被調用。
  • 實踐

    修改Zygote.java中的代碼

    Zygote.java文件的位置是:frameworks/base/core/java/com/android/internal/os/Zygote.java,在Zygote類中添加一個成員變量:

    public static final int DEBUG_ENABLE_INTERPRET = 1 << 31;

    在Zygote類forkAndSpecialize方法的開始部分添加下面的代碼:

    if (<APP包名>.equals(niceName)) {debugFlags |= DEBUG_ENABLE_INTERPRET; }

    修改dalvik_system_ZygoteHooks.cc中的代碼

    dalvik_system_ZygoteHooks.cc文件的位置是:art/runtime/native/dalvik_system_ZygoteHooks.cc,修改這個文件中的EnableDebugFeatures函數的代碼。

    向這個函數中添加下面的代碼:

    DEBUG_ENABLE_INTERPRET = 1 << 31, if ((debug_flags & DEBUG_ENABLE_INTERPRET) != 0) {Runtime::Current()->GetInstrumentation()->ForceInterpretOnly();debug_flags &= ~DEBUG_ENABLE_INTERPRET; }

    下面是對這個函數修改后的完整代碼:

    static void EnableDebugFeatures(uint32_t debug_flags) {// Must match values in dalvik.system.Zygote.enum {DEBUG_ENABLE_DEBUGGER = 1,DEBUG_ENABLE_CHECKJNI = 1 << 1,DEBUG_ENABLE_ASSERT = 1 << 2,DEBUG_ENABLE_SAFEMODE = 1 << 3,DEBUG_ENABLE_JNI_LOGGING = 1 << 4,DEBUG_ENABLE_INTERPRET = 1 << 31,};if ((debug_flags & DEBUG_ENABLE_CHECKJNI) != 0) {Runtime* runtime = Runtime::Current();JavaVMExt* vm = runtime->GetJavaVM();if (!vm->check_jni) {LOG(INFO) << "Late-enabling -Xcheck:jni";vm->SetCheckJniEnabled(true);// There's only one thread running at this point, so only one JNIEnv to fix up.Thread::Current()->GetJniEnv()->SetCheckJniEnabled(true);} else {LOG(INFO) << "Not late-enabling -Xcheck:jni (already on)";}debug_flags &= ~DEBUG_ENABLE_CHECKJNI;}if ((debug_flags & DEBUG_ENABLE_JNI_LOGGING) != 0) {gLogVerbosity.third_party_jni = true;debug_flags &= ~DEBUG_ENABLE_JNI_LOGGING;}Dbg::SetJdwpAllowed((debug_flags & DEBUG_ENABLE_DEBUGGER) != 0);if ((debug_flags & DEBUG_ENABLE_DEBUGGER) != 0) {EnableDebugger();}debug_flags &= ~DEBUG_ENABLE_DEBUGGER;if ((debug_flags & DEBUG_ENABLE_SAFEMODE) != 0) {// Ensure that any (secondary) oat files will be interpreted.Runtime* runtime = Runtime::Current();runtime->AddCompilerOption("--compiler-filter=interpret-only");debug_flags &= ~DEBUG_ENABLE_SAFEMODE;}// This is for backwards compatibility with Dalvik.debug_flags &= ~DEBUG_ENABLE_ASSERT;if ((debug_flags & DEBUG_ENABLE_INTERPRET) != 0) {Runtime::Current()->GetInstrumentation()->ForceInterpretOnly();debug_flags &= ~DEBUG_ENABLE_INTERPRET;}if (debug_flags != 0) {LOG(ERROR) << StringPrintf("Unknown bits set in debug_flags: %#x", debug_flags);} } 原文地址:?https://my.oschina.net/ibuwai/blog/528023 與50位技術專家面對面20年技術見證,附贈技術全景圖

    總結

    以上是生活随笔為你收集整理的Android5.1.1源码 - 让某个APP以解释执行模式运行的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。