From a5febe8f861a813af93b223d2e77d4305666ca47 Mon Sep 17 00:00:00 2001 From: LoveSy Date: Thu, 12 Aug 2021 03:56:15 +0800 Subject: [PATCH] LLVM12 (#510) --- .github/workflows/core.yml | 1 + build.gradle.kts | 2 +- .../cpp/main/include/art/runtime/art_method.h | 4 ++-- .../cpp/main/include/art/runtime/class_linker.h | 12 ++++++------ .../art/runtime/gc/scoped_gc_critical_section.h | 8 ++++---- .../cpp/main/include/art/runtime/hidden_api.h | 2 +- .../main/include/art/runtime/instrumentation.h | 2 +- .../cpp/main/include/art/runtime/mirror/class.h | 2 +- .../main/cpp/main/include/art/runtime/runtime.h | 2 +- .../main/cpp/main/include/art/runtime/thread.h | 4 ++-- .../cpp/main/include/art/runtime/thread_list.h | 8 ++++---- core/src/main/cpp/main/include/macros.h | 7 +------ core/src/main/cpp/main/src/context.cpp | 12 ++++++------ core/src/main/cpp/main/src/jni/pending_hooks.cpp | 2 +- core/src/main/cpp/main/src/native_api.cpp | 16 +++++++++------- core/src/main/cpp/main/src/native_api.h | 2 +- core/src/main/cpp/main/src/native_hook.cpp | 2 +- core/src/main/cpp/main/src/service.cpp | 16 ++++++++-------- core/src/main/cpp/main/src/symbol_cache.cpp | 5 ++--- 19 files changed, 53 insertions(+), 56 deletions(-) diff --git a/.github/workflows/core.yml b/.github/workflows/core.yml index e110b0f4..4736115f 100644 --- a/.github/workflows/core.yml +++ b/.github/workflows/core.yml @@ -63,6 +63,7 @@ jobs: sudo apt-get install -y ccache ccache -o max_size=2G ccache -o hash_dir=false + ccache -o compiler_check=content - name: Build with Gradle env: NDK_CCACHE: ccache diff --git a/build.gradle.kts b/build.gradle.kts index a2a10949..b8b94fd6 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -42,7 +42,7 @@ val androidTargetSdkVersion by extra(31) val androidMinSdkVersion by extra(27) val androidBuildToolsVersion by extra("31.0.0") val androidCompileSdkVersion by extra(31) -val androidCompileNdkVersion by extra("22.1.7171670") +val androidCompileNdkVersion by extra("23.0.7599858") val androidSourceCompatibility by extra(JavaVersion.VERSION_11) val androidTargetCompatibility by extra(JavaVersion.VERSION_11) val apiCode by extra(93) diff --git a/core/src/main/cpp/main/include/art/runtime/art_method.h b/core/src/main/cpp/main/include/art/runtime/art_method.h index 55d8c2ba..dce2abbe 100644 --- a/core/src/main/cpp/main/include/art/runtime/art_method.h +++ b/core/src/main/cpp/main/include/art/runtime/art_method.h @@ -31,9 +31,9 @@ namespace art { namespace art_method { CREATE_MEM_FUNC_SYMBOL_ENTRY(std::string, PrettyMethod, void *thiz, bool with_signature) { - if (UNLIKELY(thiz == nullptr)) + if (thiz == nullptr) [[unlikely]] return "null"; - if (LIKELY(PrettyMethodSym)) + if (PrettyMethodSym) [[likely]] return PrettyMethodSym(thiz, with_signature); else return "null sym"; } diff --git a/core/src/main/cpp/main/include/art/runtime/class_linker.h b/core/src/main/cpp/main/include/art/runtime/class_linker.h index ea3fec25..75e68726 100644 --- a/core/src/main/cpp/main/include/art/runtime/class_linker.h +++ b/core/src/main/cpp/main/include/art/runtime/class_linker.h @@ -40,7 +40,7 @@ namespace art { CREATE_MEM_FUNC_SYMBOL_ENTRY(void, SetEntryPointsToInterpreter, void *thiz, void *art_method) { - if (LIKELY(SetEntryPointsToInterpreterSym)) + if (SetEntryPointsToInterpreterSym) [[likely]] SetEntryPointsToInterpreterSym(thiz, art_method); } @@ -49,7 +49,7 @@ namespace art { art::mirror::Class mirror_class(clazz_ptr); auto class_def = mirror_class.GetClassDef(); bool should_intercept = class_def && lspd::IsClassPending(class_def); - if (UNLIKELY(should_intercept)) { + if (should_intercept) [[unlikely]] { LOGD("Pending hook for %p (%s)", clazz_ptr, art::mirror::Class(clazz_ptr).GetDescriptor().c_str()); lspd::Context::GetInstance()->CallOnPostFixupStaticTrampolines(clazz_ptr); @@ -83,7 +83,7 @@ namespace art { CREATE_MEM_FUNC_SYMBOL_ENTRY(void, MakeInitializedClassesVisiblyInitialized, void *thiz, void *self, bool wait) { - if (LIKELY(MakeInitializedClassesVisiblyInitializedSym)) + if (MakeInitializedClassesVisiblyInitializedSym) [[likely]] MakeInitializedClassesVisiblyInitializedSym(thiz, self, wait); } @@ -93,7 +93,7 @@ namespace art { bool, ShouldUseInterpreterEntrypoint, (void * art_method, const void *quick_code), { if (quick_code != nullptr && - UNLIKELY(lspd::isHooked(art_method) || lspd::IsMethodPending(art_method))) { + lspd::isHooked(art_method) || lspd::IsMethodPending(art_method)) [[unlikely]] { return false; } return backup(art_method, quick_code); @@ -183,14 +183,14 @@ namespace art { [[gnu::always_inline]] void MakeInitializedClassesVisiblyInitialized(void *self, bool wait) const { LOGD("MakeInitializedClassesVisiblyInitialized start, thiz=%p, self=%p", thiz_, self); - if (LIKELY(thiz_)) + if (thiz_) [[likely]] MakeInitializedClassesVisiblyInitialized(thiz_, self, wait); } [[gnu::always_inline]] void SetEntryPointsToInterpreter(void *art_method) const { LOGD("SetEntryPointsToInterpreter start, thiz=%p, art_method=%p", thiz_, art_method); - if (LIKELY(thiz_)) + if (thiz_) [[likely]] SetEntryPointsToInterpreter(thiz_, art_method); } diff --git a/core/src/main/cpp/main/include/art/runtime/gc/scoped_gc_critical_section.h b/core/src/main/cpp/main/include/art/runtime/gc/scoped_gc_critical_section.h index 47417678..fab430c2 100644 --- a/core/src/main/cpp/main/include/art/runtime/gc/scoped_gc_critical_section.h +++ b/core/src/main/cpp/main/include/art/runtime/gc/scoped_gc_critical_section.h @@ -34,13 +34,13 @@ namespace art { class ScopedGCCriticalSection { CREATE_MEM_FUNC_SYMBOL_ENTRY(void, constructor, void *thiz, void* self, GcCause cause, CollectorType collector_type) { - if (UNLIKELY(thiz == nullptr)) return; - if (LIKELY(constructorSym)) + if (thiz == nullptr) [[unlikely]] return; + if (constructorSym) [[likely]] return constructorSym(thiz, self, cause, collector_type); } CREATE_MEM_FUNC_SYMBOL_ENTRY(void, destructor, void *thiz) { - if (UNLIKELY(thiz == nullptr)) return; - if (LIKELY(destructorSym)) + if (thiz == nullptr) [[unlikely]] return; + if (destructorSym) [[likely]] return destructorSym(thiz); } public: diff --git a/core/src/main/cpp/main/include/art/runtime/hidden_api.h b/core/src/main/cpp/main/include/art/runtime/hidden_api.h index aeb6dbbb..fa3749f9 100644 --- a/core/src/main/cpp/main/include/art/runtime/hidden_api.h +++ b/core/src/main/cpp/main/include/art/runtime/hidden_api.h @@ -31,7 +31,7 @@ namespace art { CREATE_FUNC_SYMBOL_ENTRY(void, DexFile_setTrusted, JNIEnv *env, jclass clazz, jobject j_cookie) { - if (LIKELY(DexFile_setTrustedSym != nullptr)) { + if (DexFile_setTrustedSym != nullptr) [[likely]] { Runtime::Current()->SetJavaDebuggable(true); DexFile_setTrustedSym(env, clazz, j_cookie); Runtime::Current()->SetJavaDebuggable(false); diff --git a/core/src/main/cpp/main/include/art/runtime/instrumentation.h b/core/src/main/cpp/main/include/art/runtime/instrumentation.h index 44451ac2..47a8a726 100644 --- a/core/src/main/cpp/main/include/art/runtime/instrumentation.h +++ b/core/src/main/cpp/main/include/art/runtime/instrumentation.h @@ -29,7 +29,7 @@ namespace art { CREATE_MEM_HOOK_STUB_ENTRIES( "_ZN3art15instrumentation15Instrumentation21UpdateMethodsCodeImplEPNS_9ArtMethodEPKv", void, UpdateMethodsCode, (void * thiz, void * art_method, const void *quick_code), { - if (UNLIKELY(lspd::isHooked(art_method))) { + if (lspd::isHooked(art_method)) [[unlikely]] { LOGD("Skip update method code for hooked method %s", art_method::PrettyMethod(art_method).c_str()); return; diff --git a/core/src/main/cpp/main/include/art/runtime/mirror/class.h b/core/src/main/cpp/main/include/art/runtime/mirror/class.h index 5644b4fe..da58a3c7 100644 --- a/core/src/main/cpp/main/include/art/runtime/mirror/class.h +++ b/core/src/main/cpp/main/include/art/runtime/mirror/class.h @@ -42,7 +42,7 @@ namespace art { } CREATE_MEM_FUNC_SYMBOL_ENTRY(void*, GetClassDef, void* thiz) { - if (LIKELY(GetClassDefSym)) + if (GetClassDefSym) [[likely]] return GetClassDefSym(thiz); return nullptr; } diff --git a/core/src/main/cpp/main/include/art/runtime/runtime.h b/core/src/main/cpp/main/include/art/runtime/runtime.h index 85a34705..8e5a12ff 100644 --- a/core/src/main/cpp/main/include/art/runtime/runtime.h +++ b/core/src/main/cpp/main/include/art/runtime/runtime.h @@ -28,7 +28,7 @@ namespace art { private: inline static Runtime *instance_; CREATE_MEM_FUNC_SYMBOL_ENTRY(void, SetJavaDebuggable, void *thiz, bool value) { - if (LIKELY(SetJavaDebuggableSym)) { + if (SetJavaDebuggableSym) [[likely]] { SetJavaDebuggableSym(thiz, value); } } diff --git a/core/src/main/cpp/main/include/art/runtime/thread.h b/core/src/main/cpp/main/include/art/runtime/thread.h index b18423fd..6c8690a1 100644 --- a/core/src/main/cpp/main/include/art/runtime/thread.h +++ b/core/src/main/cpp/main/include/art/runtime/thread.h @@ -34,7 +34,7 @@ namespace art { } CREATE_FUNC_SYMBOL_ENTRY(void *, CurrentFromGdb) { - if (LIKELY(CurrentFromGdbSym)) + if (CurrentFromGdbSym) [[likely]] return CurrentFromGdbSym(); else return nullptr; @@ -55,7 +55,7 @@ namespace art { } void *DecodeJObject(jobject obj) { - if (LIKELY(thiz_ && DecodeJObjectSym)) { + if (thiz_ && DecodeJObjectSym) [[likely]] { return DecodeJObject(thiz_, obj).data; } return nullptr; diff --git a/core/src/main/cpp/main/include/art/runtime/thread_list.h b/core/src/main/cpp/main/include/art/runtime/thread_list.h index 90e90902..c21bf800 100644 --- a/core/src/main/cpp/main/include/art/runtime/thread_list.h +++ b/core/src/main/cpp/main/include/art/runtime/thread_list.h @@ -25,13 +25,13 @@ namespace art { class ScopedSuspendAll { CREATE_MEM_FUNC_SYMBOL_ENTRY(void, constructor, void *thiz, const char * cause, bool long_suspend) { - if (UNLIKELY(thiz == nullptr)) return; - if (LIKELY(constructorSym)) + if (thiz == nullptr) [[unlikely]] return; + if (constructorSym) [[likely]] return constructorSym(thiz, cause, long_suspend); } CREATE_MEM_FUNC_SYMBOL_ENTRY(void, destructor, void *thiz) { - if (UNLIKELY(thiz == nullptr)) return; - if (LIKELY(destructorSym)) + if (thiz == nullptr) [[unlikely]] return; + if (destructorSym) [[likely]] return destructorSym(thiz); } public: diff --git a/core/src/main/cpp/main/include/macros.h b/core/src/main/cpp/main/include/macros.h index 646730e1..5e47256a 100644 --- a/core/src/main/cpp/main/include/macros.h +++ b/core/src/main/cpp/main/include/macros.h @@ -20,14 +20,9 @@ // used in defining new arrays, for example. If you use arraysize on // a pointer by mistake, you will get a compile-time error. template -[[gnu::always_inline]] constexpr size_t arraysize(T(&)[N]) { +[[gnu::always_inline]] constexpr inline size_t arraysize(T(&)[N]) { return N; } -// Changing this definition will cause you a lot of pain. A majority of -// vendor code defines LIKELY and UNLIKELY this way, and includes -// this header through an indirect path. -#define LIKELY( exp ) (__builtin_expect( (exp) != 0, true )) -#define UNLIKELY( exp ) (__builtin_expect( (exp) != 0, false )) // Current ABI string #if defined(__arm__) #define ABI_STRING "arm" diff --git a/core/src/main/cpp/main/src/context.cpp b/core/src/main/cpp/main/src/context.cpp index 8392cd7d..8ea260c3 100644 --- a/core/src/main/cpp/main/src/context.cpp +++ b/core/src/main/cpp/main/src/context.cpp @@ -42,7 +42,7 @@ namespace lspd { constexpr int PER_USER_RANGE = 100000; void Context::CallOnPostFixupStaticTrampolines(void *class_ptr) { - if (UNLIKELY(!class_ptr || !class_linker_class_ || !post_fixup_static_mid_)) { + if (!class_ptr || !class_linker_class_ || !post_fixup_static_mid_) [[unlikely]] { return; } @@ -56,7 +56,7 @@ namespace lspd { } void Context::PreLoadDex(std::string_view dex_path) { - if (LIKELY(!dex.empty())) return; + if (!dex.empty()) [[unlikely]] return; FILE *f = fopen(dex_path.data(), "rb"); if (!f) { @@ -80,7 +80,7 @@ namespace lspd { auto getsyscl_mid = JNI_GetStaticMethodID( env, classloader, "getSystemClassLoader", "()Ljava/lang/ClassLoader;"); auto sys_classloader = JNI_CallStaticObjectMethod(env, classloader, getsyscl_mid); - if (UNLIKELY(!sys_classloader)) { + if (!sys_classloader) [[unlikely]] { LOGE("getSystemClassLoader failed!!!"); return; } @@ -130,7 +130,7 @@ namespace lspd { if (!mid) { mid = JNI_GetMethodID(env, clz, "findClass", "(Ljava/lang/String;)Ljava/lang/Class;"); } - if (LIKELY(mid)) { + if (mid) [[likely]] { auto target = JNI_CallObjectMethod(env, class_loader, mid, env->NewStringUTF(class_name.data())); if (target) { @@ -147,12 +147,12 @@ namespace lspd { void Context::FindAndCall(JNIEnv *env, std::string_view method_name, std::string_view method_sig, Args &&... args) const { - if (UNLIKELY(!entry_class_)) { + if (!entry_class_) [[unlikely]] { LOGE("cannot call method %s, entry class is null", method_name.data()); return; } jmethodID mid = JNI_GetStaticMethodID(env, entry_class_, method_name, method_sig); - if (LIKELY(mid)) { + if (mid) [[likely]] { JNI_CallStaticVoidMethod(env, entry_class_, mid, std::forward(args)...); } else { LOGE("method %s id is null", method_name.data()); diff --git a/core/src/main/cpp/main/src/jni/pending_hooks.cpp b/core/src/main/cpp/main/src/jni/pending_hooks.cpp index 3802e339..8f1a4a47 100644 --- a/core/src/main/cpp/main/src/jni/pending_hooks.cpp +++ b/core/src/main/cpp/main/src/jni/pending_hooks.cpp @@ -67,7 +67,7 @@ namespace lspd { auto *class_ptr = art::Thread::Current().DecodeJObject(class_ref); auto *method = yahfa::getArtMethod(env, method_ref); art::mirror::Class mirror_class(class_ptr); - if (auto def = mirror_class.GetClassDef(); LIKELY(def)) { + if (auto def = mirror_class.GetClassDef(); def) [[likely]] { LOGD("record pending: %p (%s) with %p", class_ptr, mirror_class.GetDescriptor().c_str(), method); // Add it for ShouldUseInterpreterEntrypoint diff --git a/core/src/main/cpp/main/src/native_api.cpp b/core/src/main/cpp/main/src/native_api.cpp index cdd6a85f..168680c5 100644 --- a/core/src/main/cpp/main/src/native_api.cpp +++ b/core/src/main/cpp/main/src/native_api.cpp @@ -63,10 +63,9 @@ namespace lspd { void RegisterNativeLib(const std::string &library_name) { static bool initialized = []() { - InstallNativeAPI(); - return true; + return InstallNativeAPI(); }(); - if (UNLIKELY(!initialized)) return; + if (!initialized) [[unlikely]] return; LOGD("native_api: Registered %s", library_name.c_str()); moduleNativeLibs.push_back(library_name); } @@ -97,10 +96,10 @@ namespace lspd { } for (std::string_view module_lib: moduleNativeLibs) { // the so is a module so - if (UNLIKELY(hasEnding(ns, module_lib))) { + if (hasEnding(ns, module_lib)) [[unlikely]] { LOGD("Loading module native library %s", module_lib.data()); void *native_init_sym = dlsym(handle, "native_init"); - if (UNLIKELY(native_init_sym == nullptr)) { + if (native_init_sym == nullptr) [[unlikely]] { LOGD("Failed to get symbol \"native_init\" from library %s", module_lib.data()); break; @@ -122,9 +121,12 @@ namespace lspd { return handle; }); - void InstallNativeAPI() { + bool InstallNativeAPI() { LOGD("InstallNativeAPI: %p", sym_do_dlopen); - if (sym_do_dlopen) + if (sym_do_dlopen) [[likely]] { HookSymNoHandle(sym_do_dlopen, do_dlopen); + return true; + } + return false; } } diff --git a/core/src/main/cpp/main/src/native_api.h b/core/src/main/cpp/main/src/native_api.h index cbe5cc88..88ea0a95 100644 --- a/core/src/main/cpp/main/src/native_api.h +++ b/core/src/main/cpp/main/src/native_api.h @@ -44,7 +44,7 @@ typedef struct { typedef NativeOnModuleLoaded (*NativeInit)(const NativeAPIEntries *entries); namespace lspd { - void InstallNativeAPI(); + bool InstallNativeAPI(); void RegisterNativeLib(const std::string &library_name); } diff --git a/core/src/main/cpp/main/src/native_hook.cpp b/core/src/main/cpp/main/src/native_hook.cpp index f9dd1524..8b46ec29 100644 --- a/core/src/main/cpp/main/src/native_hook.cpp +++ b/core/src/main/cpp/main/src/native_hook.cpp @@ -41,7 +41,7 @@ namespace lspd { static std::atomic_bool installed = false; void InstallInlineHooks() { - if (installed.exchange(true)) { + if (installed.exchange(true)) [[unlikely]] { LOGD("Inline hooks have been installed, skip"); return; } diff --git a/core/src/main/cpp/main/src/service.cpp b/core/src/main/cpp/main/src/service.cpp index 5113242f..df12c264 100644 --- a/core/src/main/cpp/main/src/service.cpp +++ b/core/src/main/cpp/main/src/service.cpp @@ -39,7 +39,7 @@ namespace lspd { code = va_arg(copy, jint); va_end(copy); - if (UNLIKELY(code == BRIDGE_TRANSACTION_CODE)) { + if (code == BRIDGE_TRANSACTION_CODE) [[unlikely]] { *res = env->CallStaticBooleanMethodV(instance()->bridge_service_class_, instance()->exec_transact_replace_methodID_, args); @@ -52,16 +52,16 @@ namespace lspd { jboolean Service::call_boolean_method_va_replace(JNIEnv *env, jobject obj, jmethodID methodId, va_list args) { - if (UNLIKELY(methodId == instance()->exec_transact_backup_methodID_)) { + if (methodId == instance()->exec_transact_backup_methodID_) [[unlikely]] { jboolean res = false; - if (LIKELY(exec_transact_replace(&res, env, obj, args))) return res; + if (exec_transact_replace(&res, env, obj, args)) [[unlikely]] return res; // else fallback to backup } return instance()->call_boolean_method_va_backup_(env, obj, methodId, args); } void Service::InitService(JNIEnv *env) { - if (LIKELY(initialized_)) return; + if (initialized_) [[unlikely]] return; // ServiceManager if (auto service_manager_class = JNI_FindClass(env, "android/os/ServiceManager")) @@ -110,8 +110,8 @@ namespace lspd { void Service::HookBridge(const Context &context, JNIEnv *env) { static bool hooked = false; // This should only be ran once, so unlikely - if (UNLIKELY(hooked)) return; - if (UNLIKELY(!initialized_)) return; + if (hooked) [[unlikely]] return; + if (!initialized_) [[unlikely]] return; hooked = true; if (auto bridge_service_class = context.FindClassFromCurrentLoader(env, kBridgeServiceClassName)) @@ -148,7 +148,7 @@ namespace lspd { } ScopedLocalRef Service::RequestBinder(JNIEnv *env, jstring nice_name) { - if (UNLIKELY(!initialized_)) { + if (!initialized_) [[unlikely]] { LOGE("Service not initialized"); return {env, nullptr}; } @@ -192,7 +192,7 @@ namespace lspd { } ScopedLocalRef Service::RequestBinderForSystemServer(JNIEnv *env) { - if (UNLIKELY(!initialized_ || !bridge_service_class_)) { + if (!initialized_ || !bridge_service_class_) [[unlikely]] { LOGE("Service not initialized"); return {env, nullptr}; } diff --git a/core/src/main/cpp/main/src/symbol_cache.cpp b/core/src/main/cpp/main/src/symbol_cache.cpp index da456c6a..8517163b 100644 --- a/core/src/main/cpp/main/src/symbol_cache.cpp +++ b/core/src/main/cpp/main/src/symbol_cache.cpp @@ -65,12 +65,11 @@ namespace lspd { } void InitSymbolCache() { - if (UNLIKELY(sym_initialized)) return; LOGD("InitSymbolCache"); sym_initialized = FindLibArt(); sym_do_dlopen = SandHook::ElfImg("linker").getSymbAddress( - "__dl__Z9do_dlopenPKciPK17android_dlextinfoPKv"); - if (UNLIKELY(!sym_initialized)) { + "__dl__Z9do_dlopenPKciPK17android_dlextinfoPKv"); + if (!sym_initialized) [[unlikely]] { sym_initialized = false; art_img.reset(); LOGE("Init symbol cache failed");