Skip to content

Commit

Permalink
fix the handler params
Browse files Browse the repository at this point in the history
  • Loading branch information
altalk23 committed May 26, 2024
1 parent 2572c0e commit 3978560
Showing 1 changed file with 21 additions and 11 deletions.
32 changes: 21 additions & 11 deletions src/generator/X64Generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ std::vector<uint8_t> X64HandlerGenerator::handlerBytes(uint64_t address) {
RegMem64 m;
using enum X64Register;

#ifdef TULIP_HOOK_WINDOWS
constexpr auto SCRATCH = RAX;
constexpr auto FIRST_PARAM = RCX;
constexpr auto SECOND_PARAM = RDX;
#else
constexpr auto SCRATCH = RAX;
constexpr auto FIRST_PARAM = RDI;
constexpr auto SECOND_PARAM = RSI;
#endif

for (size_t i = 0; i < 8; ++i) {
a.nop();
}
Expand All @@ -53,19 +63,19 @@ std::vector<uint8_t> X64HandlerGenerator::handlerBytes(uint64_t address) {
a.movaps(m[RSP + 0x00], XMM0);

// preserve the original return
a.mov(RAX, m[RSP + 0xb8]);
a.mov(SCRATCH, m[RSP + 0xb8]);

// set the new return
a.lea(RDI, "handlerCont");
a.mov(m[RSP + 0xb8], RDI);
a.lea(FIRST_PARAM, "handlerCont");
a.mov(m[RSP + 0xb8], FIRST_PARAM);

// set the parameters
a.mov(RDI, "content");
a.mov(RSI, RAX);
a.mov(FIRST_PARAM, "content");
a.mov(SECOND_PARAM, SCRATCH);

// call the pre handler, incrementing
a.mov(RAX, "handlerPre");
a.call(RAX);
a.mov(SCRATCH, "handlerPre");
a.call(SCRATCH);

// recover registers
a.movaps(XMM0, m[RSP + 0x00]);
Expand All @@ -86,7 +96,7 @@ std::vector<uint8_t> X64HandlerGenerator::handlerBytes(uint64_t address) {
a.add(RSP, 0xb8);

// call the func
a.jmp(RAX);
a.jmp(SCRATCH);

a.label("handlerCont");

Expand All @@ -101,11 +111,11 @@ std::vector<uint8_t> X64HandlerGenerator::handlerBytes(uint64_t address) {
a.movaps(m[RSP + 0x00], XMM0);

// call the post handler, decrementing
a.mov(RAX, "handlerPost");
a.call(RAX);
a.mov(SCRATCH, "handlerPost");
a.call(SCRATCH);

// recover the original return
a.mov(m[RSP + 0x38], RAX);
a.mov(m[RSP + 0x38], SCRATCH);

// recover the return values
a.movaps(XMM0, m[RSP + 0x00]);
Expand Down

0 comments on commit 3978560

Please sign in to comment.