Skip to content

Commit

Permalink
Use Dobby for native_api
Browse files Browse the repository at this point in the history
LSPlt might not be capable for general native_api hooks

Fix Dobby module repo Url

chiteroman has deleted his repo, so I should maintain my own fork.
  • Loading branch information
JingMatrix committed Sep 12, 2024
1 parent 4c4a3f4 commit 75e3005
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 9 deletions.
5 changes: 4 additions & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
[submodule "external/lsplant"]
path = external/lsplant
url = https://github.com/JingMatrix/LSPlant.git
[submodule "external/dobby"]
path = external/dobby
url = https://github.com/JingMatrix/Dobby.git
[submodule "external/lsplt"]
path = external/lsplt
url = https://github.com/LSPosed/LSPlt
url = https://github.com/LSPosed/LSPlt.git
[submodule "external/fmt"]
path = external/fmt
url = https://github.com/fmtlib/fmt.git
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ You can contribute translation [here](https://crowdin.com/project/lsposed_jingma
- [Magisk](https://github.com/topjohnwu/Magisk/): makes all these possible
- [Riru](https://github.com/RikkaApps/Riru): provides a way to inject code into zygote process
- [XposedBridge](https://github.com/rovo89/XposedBridge): the OG Xposed framework APIs
- [LSPlt](https://github.com/LSPosed/LSPlt): used for inline hooking
- [LSPlt](https://github.com/LSPosed/LSPlt): used for (Android 15) `libart` inline hooking
- [Dobby](https://github.com/JingMatrix/Dobby): used for fallback and `native_api` inline hooking
- [LSPlant](https://github.com/JingMatrix/LSPlant): the core ART hooking framework
- [EdXposed](https://github.com/ElderDrivers/EdXposed): fork source
- ~[Dobby](https://github.com/chiteroman/Dobby): used for inline hooking~
- ~[SandHook](https://github.com/ganyao114/SandHook/): ART hooking framework for SandHook variant~
- ~[YAHFA](https://github.com/rk700/YAHFA): previous ART hooking framework~
- ~[dexmaker](https://github.com/linkedin/dexmaker) and [dalvikdx](https://github.com/JakeWharton/dalvik-dx): to dynamically generate YAHFA hooker classes~
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/jni/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ add_library(${PROJECT_NAME} STATIC ${SRC_LIST})
target_include_directories(${PROJECT_NAME} PUBLIC include)
target_include_directories(${PROJECT_NAME} PRIVATE src)

target_link_libraries(${PROJECT_NAME} PUBLIC lsplt_static lsplant_static log fmt-header-only)
target_link_libraries(${PROJECT_NAME} PUBLIC dobby_static lsplt_static lsplant_static log fmt-header-only)
target_link_libraries(${PROJECT_NAME} PRIVATE dex_builder_static)
7 changes: 3 additions & 4 deletions core/src/main/jni/src/native_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
#include <sys/mman.h>
#include <list>
#include <dlfcn.h>
#include "native_util.h"
#include "elf_util.h"


Expand Down Expand Up @@ -59,8 +58,8 @@ namespace lspd {
const auto[entries] = []() {
auto *entries = new(protected_page.get()) NativeAPIEntries{
.version = 2,
.hookFunc = &HookFunction,
.unhookFunc = &UnhookFunction,
.hookFunc = &DobbyHookFunction,
.unhookFunc = &DobbyUnhookFunction,
};

mprotect(protected_page.get(), 4096, PROT_READ);
Expand All @@ -72,7 +71,7 @@ namespace lspd {
return InstallNativeAPI({
.inline_hooker = [](auto t, auto r) {
void* bk = nullptr;
return HookFunction(t, r, &bk) == 0 ? bk : nullptr;
return DobbyHookFunction(t, r, &bk) == 0 ? bk : nullptr;
},
});
}();
Expand Down
25 changes: 25 additions & 0 deletions core/src/main/jni/src/native_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@
#define LSPOSED_NATIVE_API_H

#include <cstdint>
#include <dlfcn.h>
#include <string>
#include <dobby.h>

#include "config.h"
#include "utils/hook_helper.hpp"

typedef int (*HookFunType)(void *func, void *replace, void **backup);
Expand All @@ -48,6 +51,28 @@ namespace lspd {
bool InstallNativeAPI(const lsplant::HookHandler& handler);

void RegisterNativeLib(const std::string &library_name);

inline int DobbyHookFunction(void *original, void *replace, void **backup) {
if constexpr (isDebug) {
Dl_info info;
if (dladdr(original, &info))
LOGD("Dobby hooking {} ({}) from {} ({})",
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, reinterpret_cast<dobby_dummy_func_t>(replace), reinterpret_cast<dobby_dummy_func_t *>(backup));
}

inline int DobbyUnhookFunction(void *original) {
if constexpr (isDebug) {
Dl_info info;
if (dladdr(original, &info))
LOGD("Dobby unhooking {} ({}) from {} ({})",
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);
}
}

#endif //LSPOSED_NATIVE_API_H
3 changes: 2 additions & 1 deletion external/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ link_libraries(cxx)

OPTION(LSPLANT_BUILD_SHARED OFF)
OPTION(LSPLT_BUILD_SHARED OFF)
add_subdirectory(dobby)
add_subdirectory(fmt)
add_subdirectory(lsplant/lsplant/src/main/jni)
add_subdirectory(lsplt/lsplt/src/main/jni)
add_subdirectory(fmt)
target_compile_definitions(fmt-header-only INTERFACE FMT_STATIC_THOUSANDS_SEPARATOR=1 FMT_USE_FLOAT=0 FMT_USE_DOUBLE=0 FMT_USE_LONG_DOUBLE=0)
1 change: 1 addition & 0 deletions external/dobby
Submodule dobby added at 389938

0 comments on commit 75e3005

Please sign in to comment.