Use phmap instead of absl (#2781)

This commit is contained in:
LoveSy 2023-10-07 23:11:26 +08:00 committed by GitHub
parent 35dfd650fe
commit 7fef809d01
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 36 additions and 47 deletions

View File

@ -22,7 +22,8 @@
#include "slicer/reader.h"
#include <list>
#include <absl/container/flat_hash_map.h>
#include <set>
#include <parallel_hashmap/phmap.h>
namespace {
using Value = std::tuple<jint /*type*/, std::vector<jbyte> /*data*/>;
@ -62,11 +63,11 @@ namespace {
};
std::vector<ClassData> class_data;
absl::flat_hash_map<jint, std::vector<jint>> field_annotations;
absl::flat_hash_map<jint, std::vector<jint>> method_annotations;
absl::flat_hash_map<jint, std::vector<jint>> parameter_annotations;
phmap::flat_hash_map<jint, std::vector<jint>> field_annotations;
phmap::flat_hash_map<jint, std::vector<jint>> method_annotations;
phmap::flat_hash_map<jint, std::vector<jint>> parameter_annotations;
absl::flat_hash_map<jint, MethodBody> method_bodies;
phmap::flat_hash_map<jint, MethodBody> method_bodies;
};
template<class T>

View File

@ -20,7 +20,7 @@
#include "hook_bridge.h"
#include "native_util.h"
#include "lsplant.hpp"
#include <absl/container/flat_hash_map.h>
#include <parallel_hashmap/phmap.h>
#include <memory>
#include <shared_mutex>
#include <mutex>
@ -58,8 +58,12 @@ public:
}
};
std::shared_mutex hooked_lock;
absl::flat_hash_map<jmethodID, std::unique_ptr<HookItem>> hooked_methods;
template <class K, class V, class Hash = phmap::priv::hash_default_hash<K>,
class Eq = phmap::priv::hash_default_eq<K>,
class Alloc = phmap::priv::Allocator<phmap::priv::Pair<const K, V>>, size_t N = 4>
using SharedHashMap = phmap::parallel_flat_hash_map<K, V, Hash, Eq, Alloc, N, std::shared_mutex>;
SharedHashMap<jmethodID, std::unique_ptr<HookItem>> 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<HookItem>();
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<HookItem>();
hook_item = ptr.get();
ctor(target, std::move(ptr));
newHook = true;
});
if (newHook) {
auto init = env->GetMethodID(hooker, "<init>", "(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;

View File

@ -19,12 +19,12 @@
#pragma once
#include <string>
#include <absl/container/flat_hash_map.h>
#include <parallel_hashmap/phmap.h>
#include "utils/jni_helper.hpp"
class WA: public dex::Writer::Allocator {
// addr: {size, fd}
absl::flat_hash_map<void*, std::pair<size_t, int>> allocated_;
phmap::flat_hash_map<void*, std::pair<size_t, int>> allocated_;
public:
inline void* Allocate(size_t size) override {
auto fd = ASharedMemory_create("", size);

View File

@ -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

2
external/lsplant vendored

@ -1 +1 @@
Subproject commit bbabd25043f4fd8056cf5df55e2839de824467a1
Subproject commit c18f27617727f04b0785709ff63a63457bc5dd6c