Use phmap instead of absl (#2781)
This commit is contained in:
parent
35dfd650fe
commit
7fef809d01
|
|
@ -22,7 +22,8 @@
|
||||||
#include "slicer/reader.h"
|
#include "slicer/reader.h"
|
||||||
|
|
||||||
#include <list>
|
#include <list>
|
||||||
#include <absl/container/flat_hash_map.h>
|
#include <set>
|
||||||
|
#include <parallel_hashmap/phmap.h>
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
using Value = std::tuple<jint /*type*/, std::vector<jbyte> /*data*/>;
|
using Value = std::tuple<jint /*type*/, std::vector<jbyte> /*data*/>;
|
||||||
|
|
@ -62,11 +63,11 @@ namespace {
|
||||||
};
|
};
|
||||||
|
|
||||||
std::vector<ClassData> class_data;
|
std::vector<ClassData> class_data;
|
||||||
absl::flat_hash_map<jint, std::vector<jint>> field_annotations;
|
phmap::flat_hash_map<jint, std::vector<jint>> field_annotations;
|
||||||
absl::flat_hash_map<jint, std::vector<jint>> method_annotations;
|
phmap::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>> parameter_annotations;
|
||||||
|
|
||||||
absl::flat_hash_map<jint, MethodBody> method_bodies;
|
phmap::flat_hash_map<jint, MethodBody> method_bodies;
|
||||||
};
|
};
|
||||||
|
|
||||||
template<class T>
|
template<class T>
|
||||||
|
|
|
||||||
|
|
@ -20,7 +20,7 @@
|
||||||
#include "hook_bridge.h"
|
#include "hook_bridge.h"
|
||||||
#include "native_util.h"
|
#include "native_util.h"
|
||||||
#include "lsplant.hpp"
|
#include "lsplant.hpp"
|
||||||
#include <absl/container/flat_hash_map.h>
|
#include <parallel_hashmap/phmap.h>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <shared_mutex>
|
#include <shared_mutex>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
|
|
@ -58,8 +58,12 @@ public:
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
std::shared_mutex hooked_lock;
|
template <class K, class V, class Hash = phmap::priv::hash_default_hash<K>,
|
||||||
absl::flat_hash_map<jmethodID, std::unique_ptr<HookItem>> hooked_methods;
|
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 invoke = nullptr;
|
||||||
jmethodID callback_ctor = nullptr;
|
jmethodID callback_ctor = nullptr;
|
||||||
|
|
@ -88,22 +92,14 @@ LSP_DEF_NATIVE_METHOD(jboolean, HookBridge, hookMethod, jboolean useModernApi, j
|
||||||
#endif
|
#endif
|
||||||
auto target = env->FromReflectedMethod(hookMethod);
|
auto target = env->FromReflectedMethod(hookMethod);
|
||||||
HookItem * hook_item = nullptr;
|
HookItem * hook_item = nullptr;
|
||||||
{
|
hooked_methods.lazy_emplace_l(target, [&hook_item](auto &it) {
|
||||||
std::shared_lock lk(hooked_lock);
|
hook_item = it.second.get();
|
||||||
if (auto found = hooked_methods.find(target); found != hooked_methods.end()) {
|
}, [&hook_item, &target, &newHook](const auto &ctor) {
|
||||||
hook_item = found->second.get();
|
auto ptr = std::make_unique<HookItem>();
|
||||||
}
|
|
||||||
}
|
|
||||||
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();
|
hook_item = ptr.get();
|
||||||
|
ctor(target, std::move(ptr));
|
||||||
newHook = true;
|
newHook = true;
|
||||||
} else {
|
});
|
||||||
hook_item = ptr.get();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (newHook) {
|
if (newHook) {
|
||||||
auto init = env->GetMethodID(hooker, "<init>", "(Ljava/lang/reflect/Executable;)V");
|
auto init = env->GetMethodID(hooker, "<init>", "(Ljava/lang/reflect/Executable;)V");
|
||||||
auto callback_method = env->ToReflectedMethod(hooker, env->GetMethodID(hooker, "callback",
|
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) {
|
LSP_DEF_NATIVE_METHOD(jboolean, HookBridge, unhookMethod, jboolean useModernApi, jobject hookMethod, jobject callback) {
|
||||||
auto target = env->FromReflectedMethod(hookMethod);
|
auto target = env->FromReflectedMethod(hookMethod);
|
||||||
HookItem * hook_item = nullptr;
|
HookItem * hook_item = nullptr;
|
||||||
{
|
hooked_methods.if_contains(target, [&hook_item](const auto &it) {
|
||||||
std::shared_lock lk(hooked_lock);
|
hook_item = it.second.get();
|
||||||
if (auto found = hooked_methods.find(target); found != hooked_methods.end()) {
|
});
|
||||||
hook_item = found->second.get();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!hook_item) return JNI_FALSE;
|
if (!hook_item) return JNI_FALSE;
|
||||||
jobject backup = hook_item->GetBackup();
|
jobject backup = hook_item->GetBackup();
|
||||||
if (!backup) return JNI_FALSE;
|
if (!backup) return JNI_FALSE;
|
||||||
|
|
@ -178,12 +171,9 @@ LSP_DEF_NATIVE_METHOD(jobject, HookBridge, invokeOriginalMethod, jobject hookMet
|
||||||
jobject thiz, jobjectArray args) {
|
jobject thiz, jobjectArray args) {
|
||||||
auto target = env->FromReflectedMethod(hookMethod);
|
auto target = env->FromReflectedMethod(hookMethod);
|
||||||
HookItem * hook_item = nullptr;
|
HookItem * hook_item = nullptr;
|
||||||
{
|
hooked_methods.if_contains(target, [&hook_item](const auto &it) {
|
||||||
std::shared_lock lk(hooked_lock);
|
hook_item = it.second.get();
|
||||||
if (auto found = hooked_methods.find(target); found != hooked_methods.end()) {
|
});
|
||||||
hook_item = found->second.get();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return env->CallObjectMethod(hook_item ? hook_item->GetBackup() : hookMethod, invoke, thiz, args);
|
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) {
|
LSP_DEF_NATIVE_METHOD(jobjectArray, HookBridge, callbackSnapshot, jclass callback_class, jobject method) {
|
||||||
auto target = env->FromReflectedMethod(method);
|
auto target = env->FromReflectedMethod(method);
|
||||||
HookItem *hook_item = nullptr;
|
HookItem *hook_item = nullptr;
|
||||||
{
|
hooked_methods.if_contains(target, [&hook_item](const auto &it) {
|
||||||
std::shared_lock lk(hooked_lock);
|
hook_item = it.second.get();
|
||||||
if (auto found = hooked_methods.find(target); found != hooked_methods.end()) {
|
});
|
||||||
hook_item = found->second.get();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!hook_item) return nullptr;
|
if (!hook_item) return nullptr;
|
||||||
jobject backup = hook_item->GetBackup();
|
jobject backup = hook_item->GetBackup();
|
||||||
if (!backup) return nullptr;
|
if (!backup) return nullptr;
|
||||||
|
|
|
||||||
|
|
@ -19,12 +19,12 @@
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <absl/container/flat_hash_map.h>
|
#include <parallel_hashmap/phmap.h>
|
||||||
#include "utils/jni_helper.hpp"
|
#include "utils/jni_helper.hpp"
|
||||||
|
|
||||||
class WA: public dex::Writer::Allocator {
|
class WA: public dex::Writer::Allocator {
|
||||||
// addr: {size, fd}
|
// 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:
|
public:
|
||||||
inline void* Allocate(size_t size) override {
|
inline void* Allocate(size_t size) override {
|
||||||
auto fd = ASharedMemory_create("", size);
|
auto fd = ASharedMemory_create("", size);
|
||||||
|
|
|
||||||
|
|
@ -24,9 +24,9 @@ set(LIBCXX_SOURCES
|
||||||
# filesystem/operations.cpp
|
# filesystem/operations.cpp
|
||||||
functional.cpp
|
functional.cpp
|
||||||
future.cpp
|
future.cpp
|
||||||
# use absl instead
|
hash.cpp
|
||||||
# hash.cpp
|
|
||||||
# ios.cpp
|
# ios.cpp
|
||||||
|
# ios.instantiations.cpp
|
||||||
# iostream.cpp
|
# iostream.cpp
|
||||||
# locale.cpp
|
# locale.cpp
|
||||||
memory.cpp
|
memory.cpp
|
||||||
|
|
@ -61,6 +61,7 @@ set(LIBCXX_EXPORT_FLAGS
|
||||||
-D_LIBCPP_BUILDING_LIBRARY
|
-D_LIBCPP_BUILDING_LIBRARY
|
||||||
-D_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS
|
-D_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS
|
||||||
-D_LIBCXXABI_NO_EXCEPTIONS
|
-D_LIBCXXABI_NO_EXCEPTIONS
|
||||||
|
-D_LIBCPP_HAS_NO_LOCALIZATION
|
||||||
)
|
)
|
||||||
set(LIBCXX_FLAGS
|
set(LIBCXX_FLAGS
|
||||||
-fvisibility-global-new-delete-hidden
|
-fvisibility-global-new-delete-hidden
|
||||||
|
|
|
||||||
|
|
@ -1 +1 @@
|
||||||
Subproject commit bbabd25043f4fd8056cf5df55e2839de824467a1
|
Subproject commit c18f27617727f04b0785709ff63a63457bc5dd6c
|
||||||
Loading…
Reference in New Issue