MCPcopy Create free account
hub / github.com/clementgallet/libTAS / overwrite_orig_function

Function overwrite_orig_function

src/library/hookpatch.cpp:690–714  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

688}
689
690void 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
716void hook_patch(const char* name, const char* library, void** tramp_function, void* my_function)
717{

Callers 1

hook_patch_addrFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected