Skip to content

Commit

Permalink
fix GetOatQuickMethodHeader
Browse files Browse the repository at this point in the history
  • Loading branch information
LoveSy authored and kotori2 committed Dec 20, 2020
1 parent 4726fab commit f93af7c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 26 deletions.
15 changes: 5 additions & 10 deletions edxp-core/src/main/cpp/external/yahfa/src/HookMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,24 +126,20 @@ static int replaceMethod(void *fromMethod, void *toMethod, int isBackup) {
// replace entry point
void *newEntrypoint = nullptr;
void* fromEntrypoint = (char *) fromMethod + OFFSET_entry_point_from_quick_compiled_code_in_ArtMethod;
if(isBackup) {
if (isBackup) {
void *originEntrypoint = readAddr((char *) toMethod + OFFSET_entry_point_from_quick_compiled_code_in_ArtMethod);
// entry point hardcoded
newEntrypoint = genTrampoline(toMethod, originEntrypoint);
replaced_entrypoint[toMethod] = originEntrypoint;
}
else {
// entry point from ArtMethod struct
newEntrypoint = genTrampoline(toMethod, nullptr);
}
replaced_entrypoint[fromMethod] = readAddr(fromEntrypoint);

LOGI("replace entry point from %p to %p",
readAddr(fromEntrypoint),
newEntrypoint
);
LOGI("replace entry point from %p to %p", readAddr(fromEntrypoint), newEntrypoint);
if (newEntrypoint) {
writeAddr(fromEntrypoint,
newEntrypoint);
writeAddr(fromEntrypoint, newEntrypoint);
} else {
LOGE("failed to allocate space for trampoline of target method");
return 1;
Expand All @@ -152,8 +148,7 @@ static int replaceMethod(void *fromMethod, void *toMethod, int isBackup) {
// For pre Android M devices, should be not used by EdXposed.
if (OFFSET_entry_point_from_interpreter_in_ArtMethod != 0) {
void *interpEntrypoint = readAddr((char *) toMethod + OFFSET_entry_point_from_interpreter_in_ArtMethod);
writeAddr(fromEntrypoint,
interpEntrypoint);
writeAddr(fromEntrypoint, interpEntrypoint);
}

return 0;
Expand Down
22 changes: 6 additions & 16 deletions edxp-core/src/main/cpp/main/include/art/runtime/art_method.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,35 +26,26 @@ namespace art {
return PrettyMethod(thiz, true);
}

CREATE_HOOK_STUB_ENTRIES(uint32_t, ToDexPc, void** frame, const uintptr_t pc, bool abort_on_failure) {
void* method = *frame;
if (UNLIKELY(edxp::isHooked(method))) {
LOGD("art_method::ToDexPc: Method %p is hooked, return kDexNoIndex", method);
return 0xFFFFFFFF; // kDexNoIndex
}
return ToDexPcBackup(frame, pc, abort_on_failure);
}

CREATE_HOOK_STUB_ENTRIES(void *, GetOatQuickMethodHeader, void *thiz, uintptr_t pc) {
// This is a partial copy from AOSP. We only touch them if they are hooked.
if (UNLIKELY(edxp::isHooked(thiz))) {
uintptr_t original_ep = reinterpret_cast<uintptr_t>(
getOriginalEntryPointFromTargetMethod(thiz));
uintptr_t original_ep = reinterpret_cast<uintptr_t>(getOriginalEntryPointFromTargetMethod(
thiz)) & ~0x1;
if (original_ep) {
char *code_length_loc =
reinterpret_cast<char *>(original_ep) + oat_header_code_length_offset;
uint32_t code_length =
*reinterpret_cast<uint32_t *>(code_length_loc) & ~0x80000000;
LOGD("art_method::GetOatQuickMethodHeader: ArtMethod=%p (%s), isHooked=true, original_ep=0x%x, code_length=0x%x, pc=0x%x",
*reinterpret_cast<uint32_t *>(code_length_loc) & ~0x80000000u;
LOGD("art_method::GetOatQuickMethodHeader: ArtMethod=%p (%s), isHooked=true, original_ep=0x%zux, code_length=0x%x, pc=0x%zux",
thiz, PrettyMethod(thiz).c_str(), original_ep, code_length, pc);
if (original_ep <= pc && pc <= original_ep + code_length)
return reinterpret_cast<void *>(original_ep - oat_header_length);
// If PC is not in range, we mark it as not found.
LOGD("art_method::GetOatQuickMethodHeader: PC not found in current method.");
return nullptr;
} else {
LOGD("art_method::GetOatQuickMethodHeader: ArtMethod=%p (%s), isHooked=true, pc=0x%x, isHooked but not backup, fallback to system",
thiz, PrettyMethod(thiz).c_str(), pc);
LOGD("art_method::GetOatQuickMethodHeader: ArtMethod=%p (%s) isHooked but not backup, fallback to system",
thiz, PrettyMethod(thiz).c_str());
}
}
return GetOatQuickMethodHeaderBackup(thiz, pc);
Expand Down Expand Up @@ -90,7 +81,6 @@ namespace art {
}

RETRIEVE_FUNC_SYMBOL(PrettyMethod, "_ZN3art9ArtMethod12PrettyMethodEb");
HOOK_FUNC(ToDexPc, "_ZNK3art20OatQuickMethodHeader7ToDexPcEPPNS_9ArtMethodEjb");
}
}
}
Expand Down

0 comments on commit f93af7c

Please sign in to comment.