MCPcopy Create free account
hub / github.com/R-Fuzz/symsan / OverrideFunctionWithHotPatch

Function OverrideFunctionWithHotPatch

runtime/interception/interception_win.cpp:769–817  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

767}
768
769bool OverrideFunctionWithHotPatch(
770 uptr old_func, uptr new_func, uptr *orig_old_func) {
771 const int kHotPatchHeaderLen = kBranchLength;
772
773 uptr header = (uptr)old_func - kHotPatchHeaderLen;
774 uptr patch_length = kHotPatchHeaderLen + kShortJumpInstructionLength;
775
776 // Validate that the function is hot patchable.
777 size_t instruction_size = GetInstructionSize(old_func);
778 if (instruction_size < kShortJumpInstructionLength ||
779 !FunctionHasPadding(old_func, kHotPatchHeaderLen))
780 return false;
781
782 if (orig_old_func) {
783 // Put the needed instructions into the trampoline bytes.
784 uptr trampoline_length = instruction_size + kDirectBranchLength;
785 uptr trampoline = AllocateMemoryForTrampoline(old_func, trampoline_length);
786 if (!trampoline)
787 return false;
788 if (!CopyInstructions(trampoline, old_func, instruction_size))
789 return false;
790 WriteDirectBranch(trampoline + instruction_size,
791 old_func + instruction_size);
792 *orig_old_func = trampoline;
793 }
794
795 // If needed, get memory space for indirect address.
796 uptr indirect_address = 0;
797#if SANITIZER_WINDOWS64
798 indirect_address = AllocateMemoryForTrampoline(old_func, kAddressLength);
799 if (!indirect_address)
800 return false;
801#endif
802
803 // Change memory protection to writable.
804 DWORD protection = 0;
805 if (!ChangeMemoryProtection(header, patch_length, &protection))
806 return false;
807
808 // Write jumps to the redirected function.
809 WriteBranch(header, indirect_address, new_func);
810 WriteShortJumpInstruction(old_func, header);
811
812 // Restore previous memory protection.
813 if (!RestoreMemoryProtection(header, patch_length, protection))
814 return false;
815
816 return true;
817}
818
819bool OverrideFunctionWithTrampoline(
820 uptr old_func, uptr new_func, uptr *orig_old_func) {

Callers 1

OverrideFunctionFunction · 0.85

Calls 9

GetInstructionSizeFunction · 0.85
FunctionHasPaddingFunction · 0.85
CopyInstructionsFunction · 0.85
WriteDirectBranchFunction · 0.85
ChangeMemoryProtectionFunction · 0.85
WriteBranchFunction · 0.85
RestoreMemoryProtectionFunction · 0.85

Tested by

no test coverage detected