// Copyright 2011-2020 Molecular Matters GmbH, all rights reserved. #if LC_VERSION == 1 // BEGIN EPIC MOD //#include PCH_INCLUDE // END EPIC MOD #include "LC_Patch.h" #include "LC_Assembler.h" #include "LC_PointerUtil.h" #include "LC_Process.h" // BEGIN EPIC MOD #include "LC_Assert.h" #include "LC_Logging.h" // END EPIC MOD // BEGIN EPIC MODS #pragma warning(push) #pragma warning(disable:4686) // possible change in behavior, change in UDT return calling convention // END EPIC MODS namespace { void Install(Process::Handle processHandle, void* address, const Assembler::Instruction& instruction) { Process::WriteProcessMemory(processHandle, address, instruction.machineCode, instruction.size); Process::FlushInstructionCache(processHandle, address, instruction.size); } template void Install(Process::Handle processHandle, void* address, const Assembler::SizedInstruction& instruction) { Process::WriteProcessMemory(processHandle, address, instruction.machineCode, N); Process::FlushInstructionCache(processHandle, address, N); } } void patch::InstallNOPs(Process::Handle processHandle, void* address, uint8_t size) { const Assembler::Instruction& nop = Assembler::MakeNOP(size); Install(processHandle, address, nop); } void patch::InstallJumpToSelf(Process::Handle processHandle, void* address) { InstallRelativeShortJump(processHandle, address, address); } void patch::InstallRelativeShortJump(Process::Handle processHandle, void* address, void* destination) { char* oldFuncAddr = static_cast(address); char* newFuncAddr = static_cast(destination); const ptrdiff_t displacement = newFuncAddr - oldFuncAddr; LC_ASSERT(displacement >= std::numeric_limits::min(), "Displacement is out-of-range."); LC_ASSERT(displacement <= std::numeric_limits::max(), "Displacement is out-of-range."); Install(processHandle, address, Assembler::MakeJMP8Relative(static_cast(displacement))); } void patch::InstallRelativeNearJump(Process::Handle processHandle, void* address, void* destination) { char* oldFuncAddr = static_cast(address); char* newFuncAddr = static_cast(destination); const ptrdiff_t displacement = newFuncAddr - oldFuncAddr; LC_ASSERT(displacement >= std::numeric_limits::min(), "Displacement is out-of-range."); LC_ASSERT(displacement <= std::numeric_limits::max(), "Displacement is out-of-range."); Install(processHandle, address, Assembler::MakeJMP32Relative(static_cast(displacement))); } // BEGIN EPIC MODS #pragma warning(pop) // END EPIC MODS #endif