mirror of
https://github.com/LSPosed/LSPlt.git
synced 2025-05-06 21:48:48 +08:00
Fallback to hook without backup when remap fails
Close #1 Co-authored-by: canye <31466456+canyie@users.noreply.github.com>
This commit is contained in:
parent
d935c30d14
commit
1afe99bb16
@ -109,14 +109,15 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool DoHook(uintptr_t addr, uintptr_t callback, uintptr_t *backup) {
|
bool DoHook(uintptr_t addr, uintptr_t callback, uintptr_t *backup) {
|
||||||
|
static bool kSkipRemap = false;
|
||||||
LOGV("Hooking %p", reinterpret_cast<void *>(addr));
|
LOGV("Hooking %p", reinterpret_cast<void *>(addr));
|
||||||
auto iter = lower_bound(addr);
|
auto iter = lower_bound(addr);
|
||||||
if (iter == end()) return false;
|
if (iter == end()) return false;
|
||||||
// iter.first < addr
|
// iter.first < addr
|
||||||
auto &info = iter->second;
|
auto &info = iter->second;
|
||||||
if (info.end <= addr) return false;
|
if (info.end <= addr) return false;
|
||||||
if (!iter->second.backup && !info.self) {
|
const auto len = info.end - info.start;
|
||||||
auto len = info.end - info.start;
|
while (!info.backup && !info.self && !kSkipRemap) {
|
||||||
// let os find a suitable address
|
// let os find a suitable address
|
||||||
auto *backup_addr = mmap(nullptr, len, PROT_NONE, MAP_PRIVATE | MAP_ANON, -1, 0);
|
auto *backup_addr = mmap(nullptr, len, PROT_NONE, MAP_PRIVATE | MAP_ANON, -1, 0);
|
||||||
LOGD("Backup %p to %p", reinterpret_cast<void *>(addr), backup_addr);
|
LOGD("Backup %p to %p", reinterpret_cast<void *>(addr), backup_addr);
|
||||||
@ -124,7 +125,8 @@ public:
|
|||||||
if (auto *new_addr = mremap(reinterpret_cast<void *>(info.start), len, len,
|
if (auto *new_addr = mremap(reinterpret_cast<void *>(info.start), len, len,
|
||||||
MREMAP_FIXED | MREMAP_MAYMOVE, backup_addr);
|
MREMAP_FIXED | MREMAP_MAYMOVE, backup_addr);
|
||||||
new_addr == MAP_FAILED || new_addr != backup_addr) {
|
new_addr == MAP_FAILED || new_addr != backup_addr) {
|
||||||
return false;
|
kSkipRemap = true;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
if (auto *new_addr = mmap(reinterpret_cast<void *>(info.start), len,
|
if (auto *new_addr = mmap(reinterpret_cast<void *>(info.start), len,
|
||||||
PROT_READ | PROT_WRITE | info.perms,
|
PROT_READ | PROT_WRITE | info.perms,
|
||||||
@ -134,10 +136,10 @@ public:
|
|||||||
}
|
}
|
||||||
memcpy(reinterpret_cast<void *>(info.start), backup_addr, len);
|
memcpy(reinterpret_cast<void *>(info.start), backup_addr, len);
|
||||||
info.backup = reinterpret_cast<uintptr_t>(backup_addr);
|
info.backup = reinterpret_cast<uintptr_t>(backup_addr);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
if (info.self) {
|
if (info.self || !info.backup) {
|
||||||
// self hooking, no need backup since we are always dirty
|
// self hooking, no need backup since we are always dirty
|
||||||
auto len = info.end - info.start;
|
|
||||||
if (!(info.perms & PROT_WRITE)) {
|
if (!(info.perms & PROT_WRITE)) {
|
||||||
info.perms |= PROT_WRITE;
|
info.perms |= PROT_WRITE;
|
||||||
mprotect(reinterpret_cast<void *>(info.start), len, info.perms);
|
mprotect(reinterpret_cast<void *>(info.start), len, info.perms);
|
||||||
@ -155,8 +157,7 @@ public:
|
|||||||
} else {
|
} else {
|
||||||
info.hooks.emplace(addr, the_backup);
|
info.hooks.emplace(addr, the_backup);
|
||||||
}
|
}
|
||||||
if (info.hooks.empty() && !info.self) {
|
if (info.hooks.empty() && !info.self && info.backup) {
|
||||||
auto len = info.end - info.start;
|
|
||||||
LOGD("Restore %p from %p", reinterpret_cast<void *>(info.start),
|
LOGD("Restore %p from %p", reinterpret_cast<void *>(info.start),
|
||||||
reinterpret_cast<void *>(info.backup));
|
reinterpret_cast<void *>(info.backup));
|
||||||
if (auto *new_addr =
|
if (auto *new_addr =
|
||||||
|
Loading…
x
Reference in New Issue
Block a user