| 688 | } |
| 689 | |
| 690 | void overwrite_orig_function(void *orig_fun, void* my_function) |
| 691 | { |
| 692 | /* Overwrite the original function */ |
| 693 | LOG(LL_DEBUG, LCF_HOOK, " Overwriting the native function in %p", orig_fun); |
| 694 | |
| 695 | char *pTarget = reinterpret_cast<char *>(orig_fun); |
| 696 | uintptr_t addrTarget = reinterpret_cast<uintptr_t>(pTarget); |
| 697 | uintptr_t alignedBeg = (addrTarget / 4096) * 4096; |
| 698 | uintptr_t alignedEnd = ((addrTarget+sizeof(JMP_INSTR)+sizeof(uintptr_t)) / 4096) * 4096; |
| 699 | size_t alignedSize = alignedEnd - alignedBeg + 4096; |
| 700 | |
| 701 | MYASSERT(mprotect(reinterpret_cast<void*>(alignedBeg), alignedSize, PROT_EXEC | PROT_READ | PROT_WRITE) == 0) |
| 702 | |
| 703 | memcpy(pTarget, JMP_INSTR, sizeof(JMP_INSTR)); |
| 704 | pTarget += sizeof(JMP_INSTR); |
| 705 | #ifdef __i386__ |
| 706 | uintptr_t indirAddr = reinterpret_cast<uintptr_t>(pTarget+4); |
| 707 | memcpy(pTarget, &indirAddr, sizeof(uintptr_t)); |
| 708 | pTarget += sizeof(uintptr_t); |
| 709 | #endif |
| 710 | uintptr_t targetAddr = reinterpret_cast<uintptr_t>(my_function); |
| 711 | memcpy(pTarget, &targetAddr, sizeof(uintptr_t)); |
| 712 | |
| 713 | MYASSERT(mprotect(reinterpret_cast<void*>(alignedBeg), alignedSize, PROT_EXEC | PROT_READ) == 0) |
| 714 | } |
| 715 | |
| 716 | void hook_patch(const char* name, const char* library, void** tramp_function, void* my_function) |
| 717 | { |