From 362ab24859fef350a52d59c5430451ae8a2236c6 Mon Sep 17 00:00:00 2001 From: LoveSy Date: Tue, 20 Apr 2021 00:44:10 +0800 Subject: [PATCH] Install native hook iff needed (#496) * iff: https://en.wikipedia.org/wiki/If_and_only_if --- core/src/main/cpp/main/src/context.cpp | 5 ++--- core/src/main/cpp/main/src/context.h | 2 +- core/src/main/cpp/main/src/jni/native_api.h | 2 +- core/src/main/cpp/main/src/native_api.cpp | 5 +++++ core/src/main/cpp/main/src/native_hook.cpp | 2 +- 5 files changed, 10 insertions(+), 6 deletions(-) diff --git a/core/src/main/cpp/main/src/context.cpp b/core/src/main/cpp/main/src/context.cpp index bff086d6..fac3b428 100644 --- a/core/src/main/cpp/main/src/context.cpp +++ b/core/src/main/cpp/main/src/context.cpp @@ -68,7 +68,7 @@ namespace lspd { fseek(f, 0, SEEK_END); dex.resize(ftell(f)); rewind(f); - if (dex.size() != fread(dex.data(), 1, dex.size(), f)) { + if (dex.size() != fread(dex.data(), sizeof(decltype(dex)::value_type), dex.size(), f)) { LOGE("Read dex failed"); dex.resize(0); } @@ -90,8 +90,7 @@ namespace lspd { auto initMid = JNI_GetMethodID(env, in_memory_classloader, "", "(Ljava/nio/ByteBuffer;Ljava/lang/ClassLoader;)V"); auto byte_buffer_class = JNI_FindClass(env, "java/nio/ByteBuffer"); - auto dex_buffer = env->NewDirectByteBuffer(reinterpret_cast(dex.data()), - dex.size()); + auto dex_buffer = env->NewDirectByteBuffer(dex.data(), dex.size()); if (auto my_cl = JNI_NewObject(env, in_memory_classloader, initMid, dex_buffer, sys_classloader)) { inject_class_loader_ = JNI_NewGlobalRef(env, my_cl); diff --git a/core/src/main/cpp/main/src/context.h b/core/src/main/cpp/main/src/context.h index dc14de94..500fd8e1 100644 --- a/core/src/main/cpp/main/src/context.h +++ b/core/src/main/cpp/main/src/context.h @@ -69,7 +69,7 @@ namespace lspd { jclass class_linker_class_ = nullptr; jmethodID post_fixup_static_mid_ = nullptr; bool skip_ = false; - std::vector dex{}; + std::vector dex{}; Context() {} diff --git a/core/src/main/cpp/main/src/jni/native_api.h b/core/src/main/cpp/main/src/jni/native_api.h index 258ad012..6776c823 100644 --- a/core/src/main/cpp/main/src/jni/native_api.h +++ b/core/src/main/cpp/main/src/jni/native_api.h @@ -28,7 +28,7 @@ #include namespace lspd { - void RegisterNativeAPI(JNIEnv*); + void RegisterNativeAPI(JNIEnv *); } diff --git a/core/src/main/cpp/main/src/native_api.cpp b/core/src/main/cpp/main/src/native_api.cpp index 5a6e8414..d66988bb 100644 --- a/core/src/main/cpp/main/src/native_api.cpp +++ b/core/src/main/cpp/main/src/native_api.cpp @@ -58,6 +58,11 @@ namespace lspd { } void RegisterNativeLib(const std::string &library_name) { + static bool initialized = []() { + InstallNativeAPI(); + return true; + }(); + if (UNLIKELY(!initialized)) return; LOGD("native_api: Registered %s", library_name.c_str()); moduleNativeLibs.push_back(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 6e3229f1..0508f712 100644 --- a/core/src/main/cpp/main/src/native_hook.cpp +++ b/core/src/main/cpp/main/src/native_hook.cpp @@ -64,7 +64,7 @@ namespace lspd { InstallRiruHooks(); // install ART hooks InstallArtHooks(handle_libart); - InstallNativeAPI(); +// InstallNativeAPI(); } void InstallArtHooks(void *art_handle) {