MCPcopy Create free account
hub / github.com/GJDuck/e9patch / tactic_T3

Function tactic_T3

src/e9patch/e9tactics.cpp:654–796  ·  view source on GitHub ↗

* Tactic T3: evict a neighbour instruction. */

Source from the content-addressed store, hash-verified

652 * Tactic T3: evict a neighbour instruction.
653 */
654static Patch *tactic_T3(Binary &B, Instr *I, const Trampoline *T)
655{
656 if (I->size == 1)
657 return tactic_T3b(B, I, T);
658 if (I->size >= JMP_SIZE || !option_tactic_T3 || !canPatch(I))
659 return nullptr;
660
661 // Step (1): find nearest instruction at +SHORT_JMP_MAX (or
662 // -SHORT_JMP_MIN) bytes ahead.
663 Instr *J = I;
664 while (true)
665 {
666 Instr *K = J->next();
667 if (K == nullptr)
668 break;
669 if (K->addr - (I->addr + /*sizeof(short jmp)=*/2) > SHORT_JMP_MAX)
670 break;
671 J = K;
672 }
673
674 // Step (2): Iterate through all neighbour instructions:
675 Patch *P = nullptr;
676 const Alloc *A = nullptr;
677 intptr_t addr = 0;
678 for (; P == nullptr; J = J->prev())
679 {
680 if (J == I)
681 continue;
682 if (J == nullptr)
683 break;
684 if (!option_tactic_backward_T3 && J->addr < I->addr)
685 break;
686 if ((I->addr + /*sizeof(short jmp)=*/2) - (J->addr + J->size -1) >
687 -SHORT_JMP_MIN)
688 {
689 // Out-of-range, so give up... :(
690 break;
691 }
692
693 switch (J->STATE[0])
694 {
695 case STATE_INSTRUCTION: case STATE_FREE:
696 break;
697 case STATE_PATCHED:
698 case STATE_PATCHED | STATE_LOCKED:
699 {
700 if (J->STATE[J->size-1] == STATE_FREE)
701 break;
702 continue;
703 }
704 default:
705 continue;
706 }
707
708 for (int i = 0; i < (int)J->size && P == nullptr; i++)
709 {
710 if (i == 0 && J->STATE[0] != STATE_FREE)
711 {

Callers 1

patchFunction · 0.85

Calls 12

tactic_T3bFunction · 0.85
canPatchFunction · 0.85
allocatePunnedJumpFunction · 0.85
patchJumpFunction · 0.85
tactic_B1Function · 0.85
tactic_B2Function · 0.85
tactic_T1Function · 0.85
undoFunction · 0.85
patchShortJumpFunction · 0.85
patchUnusedFunction · 0.85
nextMethod · 0.80
prevMethod · 0.80

Tested by

no test coverage detected