diff --git a/core/src/main/cpp/main/include/base/object.h b/core/src/main/cpp/main/include/base/object.h index 4c1e1228..e566a517 100644 --- a/core/src/main/cpp/main/include/base/object.h +++ b/core/src/main/cpp/main/include/base/object.h @@ -110,12 +110,12 @@ namespace lspd { }; [[gnu::always_inline]] - static void *Dlsym(void *handle, const char *name) { + inline void *Dlsym(void *handle, const char *name) { return dlsym(handle, name); } template - static void *Dlsym(void *handle, T first, Args... last) { + inline void *Dlsym(void *handle, T first, Args... last) { auto ret = Dlsym(handle, first); if (ret) { return ret; @@ -123,9 +123,27 @@ namespace lspd { return Dlsym(handle, last...); } - static void HookFunction(void *original, void *replace, void **backup) { + inline int HookFunction(void *original, void *replace, void **backup) { _make_rwx(original, _page_size); - hook_func(original, replace, backup); + if constexpr (isDebug) { + Dl_info info; + dladdr(original, &info); + LOGD("Hooking %s (%p) from %s (%p)", + info.dli_sname ? info.dli_sname : "(unknown symbol)", info.dli_saddr, + info.dli_fname ? info.dli_fname : "(unknown file)", info.dli_fbase); + } + return DobbyHook(original, replace, backup); + } + + inline int UnhookFunction(void *original) { + if constexpr (isDebug) { + Dl_info info; + dladdr(original, &info); + LOGD("Unhooking %s (%p) from %s (%p)", + info.dli_sname ? info.dli_sname : "(unknown symbol)", info.dli_saddr, + info.dli_fname ? info.dli_fname : "(unknown file)", info.dli_fbase); + } + return DobbyDestroy(original); } template class> diff --git a/core/src/main/cpp/main/src/native_api.cpp b/core/src/main/cpp/main/src/native_api.cpp index d66988bb..a8843361 100644 --- a/core/src/main/cpp/main/src/native_api.cpp +++ b/core/src/main/cpp/main/src/native_api.cpp @@ -44,18 +44,22 @@ */ namespace lspd { - std::vector moduleLoadedCallbacks; + std::vector moduleLoadedCallbacks; std::vector moduleNativeLibs; + std::unique_ptr> protected_page( + mmap(nullptr, _page_size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_SHARED, -1, 0), + [](void *ptr) { munmap(ptr, _page_size); }); - LsposedNativeAPIEntriesV1 init(LsposedNativeOnModuleLoaded onModuleLoaded) { - if (onModuleLoaded != nullptr) moduleLoadedCallbacks.push_back(onModuleLoaded); - - LsposedNativeAPIEntriesV1 ret{ - .version = 1, - .inlineHookFunc = HookFunction + const auto[entries] = []() { + auto *entries = new(protected_page.get()) NativeAPIEntries{ + .version = 2, + .hookFunc = HookFunction, + .unhookFunc = UnhookFunction }; - return ret; - } + + mprotect(protected_page.get(), _page_size, PROT_READ); + return std::make_tuple(entries); + }(); void RegisterNativeLib(const std::string &library_name) { static bool initialized = []() { @@ -102,12 +106,17 @@ namespace lspd { break; } auto native_init = reinterpret_cast(native_init_sym); - native_init(reinterpret_cast(init)); + auto *callback = native_init(entries); + if (callback) { + moduleLoadedCallbacks.push_back(callback); + // return directly to avoid module interaction + return handle; + } } } // Callbacks - for (LsposedNativeOnModuleLoaded callback: moduleLoadedCallbacks) { + for (auto &callback: moduleLoadedCallbacks) { callback(name, handle); } return handle; diff --git a/core/src/main/cpp/main/src/native_api.h b/core/src/main/cpp/main/src/native_api.h index b512a97b..cbe5cc88 100644 --- a/core/src/main/cpp/main/src/native_api.h +++ b/core/src/main/cpp/main/src/native_api.h @@ -29,17 +29,24 @@ #include #include -// typedef int (*HookFunType)(void *, void *, void **); // For portability -typedef void (*LsposedNativeOnModuleLoaded) (const char* name, void* handle); -typedef void (*NativeInit)(void * init_func); -struct LsposedNativeAPIEntriesV1 { +typedef int (*HookFunType)(void *func, void *replace, void **backup); + +typedef int (*UnhookFunType)(void *func); + +typedef void (*NativeOnModuleLoaded)(const char *name, void *handle); + +typedef struct { uint32_t version; - lspd::HookFunType inlineHookFunc; -}; + HookFunType hookFunc; + UnhookFunType unhookFunc; +} NativeAPIEntries; + +typedef NativeOnModuleLoaded (*NativeInit)(const NativeAPIEntries *entries); namespace lspd { void InstallNativeAPI(); - void RegisterNativeLib(const std::string& library_name); + + void RegisterNativeLib(const std::string &library_name); } #endif //LSPOSED_NATIVE_API_H diff --git a/core/src/main/cpp/main/src/native_hook.cpp b/core/src/main/cpp/main/src/native_hook.cpp index 0508f712..74cbf88e 100644 --- a/core/src/main/cpp/main/src/native_hook.cpp +++ b/core/src/main/cpp/main/src/native_hook.cpp @@ -42,7 +42,6 @@ #include "art/runtime/gc/scoped_gc_critical_section.h" namespace lspd { - static volatile bool installed = false; static volatile bool art_hooks_installed = false; diff --git a/core/src/main/cpp/main/src/native_hook.h b/core/src/main/cpp/main/src/native_hook.h index f1bc9c22..2e6df53e 100644 --- a/core/src/main/cpp/main/src/native_hook.h +++ b/core/src/main/cpp/main/src/native_hook.h @@ -19,11 +19,10 @@ */ #pragma once + #include namespace lspd { - typedef void (*HookFunType)(void *, void *, void **); - static HookFunType hook_func = reinterpret_cast(DobbyHook); void InstallInlineHooks(); }