Skip to content

Commit

Permalink
Indicate plt hook explicitly
Browse files Browse the repository at this point in the history
The file magisk_loader.cpp is reformatted using clangd
  • Loading branch information
JingMatrix committed Sep 12, 2024
1 parent c2c4e52 commit e28e194
Show file tree
Hide file tree
Showing 5 changed files with 188 additions and 200 deletions.
10 changes: 5 additions & 5 deletions core/src/main/jni/include/native_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ static dev_t dev = 0;
static ino_t inode = 0;
static std::vector<std::pair<const char *, void **>> plt_hook_saved = {};

inline int HookArtFunction(void *art_symbol, void *callback, void **backup, bool save = true) {
inline int HookPLT(void *art_symbol, void *callback, void **backup, bool save = true) {
auto symbol = reinterpret_cast<const char *>(art_symbol);

if (GetArt()->isStripped()) {
Expand All @@ -106,25 +106,25 @@ inline int HookArtFunction(void *art_symbol, void *callback, void **backup, bool
if (auto addr = GetArt()->getSymbAddress(symbol); addr) {
Dl_info info;
if (dladdr(addr, &info) && info.dli_sname != nullptr && strcmp(info.dli_sname, symbol) == 0)
HookFunction(addr, callback, backup);
HookInline(addr, callback, backup);
} else if (*backup == nullptr && isDebug) {
LOGW("Failed to {} Art symbol {}", save ? "hook" : "unhook", symbol);
}
return *backup == nullptr;
}

inline int UnhookArtFunction(void *original) {
inline int UnhookPLT(void *original) {
Dl_info info;

if (!dladdr(original, &info) || info.dli_sname != nullptr) return 1;
if (!GetArt()->isStripped()) return UnhookFunction(original);
if (!GetArt()->isStripped()) return UnhookInline(original);

auto hook_iter =
std::find_if(plt_hook_saved.begin(), plt_hook_saved.end(),
[info](auto record) { return strcmp(record.first, info.dli_sname) == 0; });
void *stub = nullptr;
if (hook_iter != plt_hook_saved.end() &&
HookArtFunction(original, *(hook_iter->second), &stub, false)) {
HookPLT(original, *(hook_iter->second), &stub, false)) {
plt_hook_saved.erase(hook_iter);
return 0;
}
Expand Down
6 changes: 3 additions & 3 deletions core/src/main/jni/src/native_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ namespace lspd {
const auto[entries] = []() {
auto *entries = new(protected_page.get()) NativeAPIEntries{
.version = 2,
.hookFunc = &HookFunction,
.unhookFunc = &UnhookFunction,
.hookFunc = &HookInline,
.unhookFunc = &UnhookInline,
};

mprotect(protected_page.get(), 4096, PROT_READ);
Expand All @@ -71,7 +71,7 @@ namespace lspd {
return InstallNativeAPI(lsplant::InitInfo {
.inline_hooker = [](auto t, auto r) {
void* bk = nullptr;
return HookFunction(t, r, &bk) == 0 ? bk : nullptr;
return HookInline(t, r, &bk) == 0 ? bk : nullptr;
},
.art_symbol_resolver = [](auto symbol){
return GetLinker()->getSymbAddress(symbol);
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/jni/src/native_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ namespace lspd {

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

inline int HookFunction(void *original, void *replace, void **backup) {
inline int HookInline(void *original, void *replace, void **backup) {
if constexpr (isDebug) {
Dl_info info;
if (dladdr(original, &info))
Expand All @@ -64,7 +64,7 @@ namespace lspd {
return DobbyHook(original, reinterpret_cast<dobby_dummy_func_t>(replace), reinterpret_cast<dobby_dummy_func_t *>(backup));
}

inline int UnhookFunction(void *original) {
inline int UnhookInline(void *original) {
if constexpr (isDebug) {
Dl_info info;
if (dladdr(original, &info))
Expand Down
2 changes: 1 addition & 1 deletion external/lsplant
Loading

0 comments on commit e28e194

Please sign in to comment.