diff --git a/core/src/main/jni/src/jni/dex_parser.cpp b/core/src/main/jni/src/jni/dex_parser.cpp index 6a1bc700..6d73545f 100644 --- a/core/src/main/jni/src/jni/dex_parser.cpp +++ b/core/src/main/jni/src/jni/dex_parser.cpp @@ -22,7 +22,8 @@ #include "slicer/reader.h" #include -#include +#include +#include namespace { using Value = std::tuple /*data*/>; @@ -62,11 +63,11 @@ namespace { }; std::vector class_data; - absl::flat_hash_map> field_annotations; - absl::flat_hash_map> method_annotations; - absl::flat_hash_map> parameter_annotations; + phmap::flat_hash_map> field_annotations; + phmap::flat_hash_map> method_annotations; + phmap::flat_hash_map> parameter_annotations; - absl::flat_hash_map method_bodies; + phmap::flat_hash_map method_bodies; }; template diff --git a/core/src/main/jni/src/jni/hook_bridge.cpp b/core/src/main/jni/src/jni/hook_bridge.cpp index 467a6811..724013b4 100644 --- a/core/src/main/jni/src/jni/hook_bridge.cpp +++ b/core/src/main/jni/src/jni/hook_bridge.cpp @@ -20,7 +20,7 @@ #include "hook_bridge.h" #include "native_util.h" #include "lsplant.hpp" -#include +#include #include #include #include @@ -58,8 +58,12 @@ public: } }; -std::shared_mutex hooked_lock; -absl::flat_hash_map> hooked_methods; +template , + class Eq = phmap::priv::hash_default_eq, + class Alloc = phmap::priv::Allocator>, size_t N = 4> +using SharedHashMap = phmap::parallel_flat_hash_map; + +SharedHashMap> hooked_methods; jmethodID invoke = nullptr; jmethodID callback_ctor = nullptr; @@ -88,22 +92,14 @@ LSP_DEF_NATIVE_METHOD(jboolean, HookBridge, hookMethod, jboolean useModernApi, j #endif auto target = env->FromReflectedMethod(hookMethod); HookItem * hook_item = nullptr; - { - std::shared_lock lk(hooked_lock); - if (auto found = hooked_methods.find(target); found != hooked_methods.end()) { - hook_item = found->second.get(); - } - } - if (!hook_item) { - std::unique_lock lk(hooked_lock); - if (auto &ptr = hooked_methods[target]; !ptr) { - ptr = std::make_unique(); - hook_item = ptr.get(); - newHook = true; - } else { - hook_item = ptr.get(); - } - } + hooked_methods.lazy_emplace_l(target, [&hook_item](auto &it) { + hook_item = it.second.get(); + }, [&hook_item, &target, &newHook](const auto &ctor) { + auto ptr = std::make_unique(); + hook_item = ptr.get(); + ctor(target, std::move(ptr)); + newHook = true; + }); if (newHook) { auto init = env->GetMethodID(hooker, "", "(Ljava/lang/reflect/Executable;)V"); auto callback_method = env->ToReflectedMethod(hooker, env->GetMethodID(hooker, "callback", @@ -139,12 +135,9 @@ LSP_DEF_NATIVE_METHOD(jboolean, HookBridge, hookMethod, jboolean useModernApi, j LSP_DEF_NATIVE_METHOD(jboolean, HookBridge, unhookMethod, jboolean useModernApi, jobject hookMethod, jobject callback) { auto target = env->FromReflectedMethod(hookMethod); HookItem * hook_item = nullptr; - { - std::shared_lock lk(hooked_lock); - if (auto found = hooked_methods.find(target); found != hooked_methods.end()) { - hook_item = found->second.get(); - } - } + hooked_methods.if_contains(target, [&hook_item](const auto &it) { + hook_item = it.second.get(); + }); if (!hook_item) return JNI_FALSE; jobject backup = hook_item->GetBackup(); if (!backup) return JNI_FALSE; @@ -178,12 +171,9 @@ LSP_DEF_NATIVE_METHOD(jobject, HookBridge, invokeOriginalMethod, jobject hookMet jobject thiz, jobjectArray args) { auto target = env->FromReflectedMethod(hookMethod); HookItem * hook_item = nullptr; - { - std::shared_lock lk(hooked_lock); - if (auto found = hooked_methods.find(target); found != hooked_methods.end()) { - hook_item = found->second.get(); - } - } + hooked_methods.if_contains(target, [&hook_item](const auto &it) { + hook_item = it.second.get(); + }); return env->CallObjectMethod(hook_item ? hook_item->GetBackup() : hookMethod, invoke, thiz, args); } @@ -307,12 +297,9 @@ LSP_DEF_NATIVE_METHOD(jboolean, HookBridge, setTrusted, jobject cookie) { LSP_DEF_NATIVE_METHOD(jobjectArray, HookBridge, callbackSnapshot, jclass callback_class, jobject method) { auto target = env->FromReflectedMethod(method); HookItem *hook_item = nullptr; - { - std::shared_lock lk(hooked_lock); - if (auto found = hooked_methods.find(target); found != hooked_methods.end()) { - hook_item = found->second.get(); - } - } + hooked_methods.if_contains(target, [&hook_item](const auto &it) { + hook_item = it.second.get(); + }); if (!hook_item) return nullptr; jobject backup = hook_item->GetBackup(); if (!backup) return nullptr; diff --git a/daemon/src/main/jni/obfuscation.h b/daemon/src/main/jni/obfuscation.h index f721d03e..ec9dd151 100644 --- a/daemon/src/main/jni/obfuscation.h +++ b/daemon/src/main/jni/obfuscation.h @@ -19,12 +19,12 @@ #pragma once #include -#include +#include #include "utils/jni_helper.hpp" class WA: public dex::Writer::Allocator { // addr: {size, fd} - absl::flat_hash_map> allocated_; + phmap::flat_hash_map> allocated_; public: inline void* Allocate(size_t size) override { auto fd = ASharedMemory_create("", size); diff --git a/external/CMakeLists.txt b/external/CMakeLists.txt index 1c6af93b..84e99017 100644 --- a/external/CMakeLists.txt +++ b/external/CMakeLists.txt @@ -24,9 +24,9 @@ set(LIBCXX_SOURCES # filesystem/operations.cpp functional.cpp future.cpp -# use absl instead -# hash.cpp + hash.cpp # ios.cpp +# ios.instantiations.cpp # iostream.cpp # locale.cpp memory.cpp @@ -61,6 +61,7 @@ set(LIBCXX_EXPORT_FLAGS -D_LIBCPP_BUILDING_LIBRARY -D_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS -D_LIBCXXABI_NO_EXCEPTIONS + -D_LIBCPP_HAS_NO_LOCALIZATION ) set(LIBCXX_FLAGS -fvisibility-global-new-delete-hidden diff --git a/external/lsplant b/external/lsplant index bbabd250..c18f2761 160000 --- a/external/lsplant +++ b/external/lsplant @@ -1 +1 @@ -Subproject commit bbabd25043f4fd8056cf5df55e2839de824467a1 +Subproject commit c18f27617727f04b0785709ff63a63457bc5dd6c