From 307b88f1d609f6f8eb9f083342e1c3812f90476d Mon Sep 17 00:00:00 2001 From: LoveSy Date: Wed, 29 Jun 2022 02:21:39 +0800 Subject: [PATCH] Use `absl::flat_hash_map` instead (#2001) --- .github/workflows/core.yml | 1 + core/src/main/jni/src/jni/hook_bridge.cpp | 21 ++++++++++++--------- daemon/src/main/jni/obfuscation.h | 3 ++- external/CMakeLists.txt | 6 ++++-- external/lsplant | 2 +- 5 files changed, 20 insertions(+), 13 deletions(-) diff --git a/.github/workflows/core.yml b/.github/workflows/core.yml index 11831b7b..b1749dbf 100644 --- a/.github/workflows/core.yml +++ b/.github/workflows/core.yml @@ -62,6 +62,7 @@ jobs: ccache -o max_size=1G ccache -o hash_dir=false ccache -o compiler_check='%compiler% -dumpmachine; %compiler% -dumpversion' + ccache -o hard_link=true ccache -zp - name: Build with Gradle run: | diff --git a/core/src/main/jni/src/jni/hook_bridge.cpp b/core/src/main/jni/src/jni/hook_bridge.cpp index bc3ba130..60d23205 100644 --- a/core/src/main/jni/src/jni/hook_bridge.cpp +++ b/core/src/main/jni/src/jni/hook_bridge.cpp @@ -20,7 +20,8 @@ #include "hook_bridge.h" #include "native_util.h" #include "lsplant.hpp" -#include "unordered_map" +#include +#include #include #include @@ -34,8 +35,7 @@ struct HookItem { }; std::shared_mutex hooked_lock; -// Rehashing invalidates iterators, changes ordering between elements, and changes which buckets elements appear in, but does not invalidate pointers or references to elements. -std::unordered_map hooked_methods; +absl::flat_hash_map> hooked_methods; jmethodID invoke = nullptr; } @@ -64,13 +64,16 @@ LSP_DEF_NATIVE_METHOD(jboolean, HookBridge, hookMethod, jobject hookMethod, { std::shared_lock lk(hooked_lock); if (auto found = hooked_methods.find(target); found != hooked_methods.end()) { - hook_item = &found->second; + hook_item = found->second.get(); } } if (!hook_item) { std::unique_lock lk(hooked_lock); - hook_item = &hooked_methods[target]; - newHook = true; + if (auto &ptr = hooked_methods[target]; !ptr) { + ptr = std::make_unique(); + hook_item = ptr.get(); + newHook = true; + } } if (newHook) { auto init = env->GetMethodID(hooker, "", "(Ljava/lang/reflect/Executable;)V"); @@ -92,7 +95,7 @@ LSP_DEF_NATIVE_METHOD(jboolean, HookBridge, unhookMethod, jobject hookMethod, jo { std::shared_lock lk(hooked_lock); if (auto found = hooked_methods.find(target); found != hooked_methods.end()) { - hook_item = &found->second; + hook_item = found->second.get(); } } if (!hook_item) return JNI_FALSE; @@ -118,7 +121,7 @@ LSP_DEF_NATIVE_METHOD(jobject, HookBridge, invokeOriginalMethod, jobject hookMet { std::shared_lock lk(hooked_lock); if (auto found = hooked_methods.find(target); found != hooked_methods.end()) { - hook_item = &found->second; + hook_item = found->second.get(); } } jobject to_call = hookMethod; @@ -142,7 +145,7 @@ LSP_DEF_NATIVE_METHOD(jobjectArray, HookBridge, callbackSnapshot, jobject method { std::shared_lock lk(hooked_lock); if (auto found = hooked_methods.find(target); found != hooked_methods.end()) { - hook_item = &found->second; + hook_item = found->second.get(); } } if (!hook_item) return nullptr; diff --git a/daemon/src/main/jni/obfuscation.h b/daemon/src/main/jni/obfuscation.h index 4d8b16ad..f721d03e 100644 --- a/daemon/src/main/jni/obfuscation.h +++ b/daemon/src/main/jni/obfuscation.h @@ -19,11 +19,12 @@ #pragma once #include +#include #include "utils/jni_helper.hpp" class WA: public dex::Writer::Allocator { // addr: {size, fd} - std::unordered_map> allocated_; + absl::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 84011bf3..17b41029 100644 --- a/external/CMakeLists.txt +++ b/external/CMakeLists.txt @@ -27,7 +27,8 @@ set(LIBCXX_SOURCES filesystem/operations.cpp functional.cpp future.cpp - hash.cpp +# use absl instead +# hash.cpp ios.cpp iostream.cpp locale.cpp @@ -44,7 +45,7 @@ set(LIBCXX_SOURCES strstream.cpp system_error.cpp thread.cpp - typeinfo.cpp +# typeinfo.cpp utility.cpp valarray.cpp variant.cpp @@ -101,6 +102,7 @@ target_include_directories(cxx PRIVATE ${LIBCXX_INCLUDES} ${LIBCXXABI_INCLUDES}) link_libraries(cxx) +OPTION(LSPLANT_BUILD_SHARED OFF) add_subdirectory(lsplant/lsplant/src/main/jni) add_subdirectory(dobby) add_subdirectory(fmt) diff --git a/external/lsplant b/external/lsplant index 5867b673..ca19d619 160000 --- a/external/lsplant +++ b/external/lsplant @@ -1 +1 @@ -Subproject commit 5867b673803e255c4f0c75b9055fe7da3209e2ae +Subproject commit ca19d61942994a72b8bf875ea7ed97c0e0513b74