Skip to content

Commit

Permalink
fix x64generator, 64bit wintest.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
matcool committed Jun 2, 2024
1 parent 377f063 commit 2ec2506
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 24 deletions.
2 changes: 2 additions & 0 deletions src/generator/X64Generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ std::vector<uint8_t> X64HandlerGenerator::handlerBytes(uint64_t address) {
a.nop();
}

m_metadata.m_convention->generateIntoDefault(a, m_metadata.m_abstract);

// preserve registers
#ifdef TULIP_HOOK_WINDOWS
constexpr auto PRESERVE_SIZE = 0x78;
Expand Down
10 changes: 6 additions & 4 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ add_executable(${PROJECT_NAME} Assembler86.cpp Assembler64.cpp Hook.cpp)
target_link_libraries(${PROJECT_NAME} PUBLIC GTest::gtest_main TulipHook)

if(WIN32)
set_target_properties(${PROJECT_NAME} PROPERTIES LINK_FLAGS "/INCREMENTAL:NO")
# set_target_properties(${PROJECT_NAME} PROPERTIES LINK_FLAGS "/INCREMENTAL:NO")

# add_executable(WinTest misc/WinTest.cpp)
# target_link_libraries(WinTest PUBLIC TulipHook)
# set_target_properties(WinTest PROPERTIES LINK_FLAGS "/INCREMENTAL:NO")
if (CMAKE_SIZEOF_VOID_P EQUAL 8)
add_executable(WinTest misc/WinTest.cpp)
target_link_libraries(WinTest PUBLIC TulipHook)
# set_target_properties(WinTest PROPERTIES LINK_FLAGS "/INCREMENTAL:NO")
endif()

if (CMAKE_SIZEOF_VOID_P EQUAL 4)
add_library(TestMod SHARED misc/Mod.cpp)
Expand Down
36 changes: 16 additions & 20 deletions test/misc/WinTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,29 @@

#define NO_INLINE __declspec(noinline)

struct Dummy {
int x, y, z, d;
~Dummy() {}
};

struct FooBar {
int number;

NO_INLINE bool __thiscall targetFunc(int stack, int stack2) {
NO_INLINE Dummy targetFunc(int stack, int stack2) {
std::cout << "I am FooBar::targetFunc\n";
std::cout << " I live at " << this << " avenue\n";
std::cout << " My number is " << this->number << "\n";
std::cout << " this: " << this << "\n";
std::cout << " this->number: " << this->number << "\n";
std::cout << " My stack is " << stack << " and " << stack2 << "\n";
return true;
return {.x = 3};
}
};

NO_INLINE bool __cdecl targetFuncHook(FooBar* self, int stack, int stack2) {
NO_INLINE Dummy targetFuncHook(FooBar* self, int stack, int stack2) {
std::cout << "I am targetFuncHook" << std::endl;
std::cout << " I robbed " << self << " avenue" << std::endl;
std::cout << " I stole the stack of " << stack << " and " << stack2 << std::endl;
self->targetFunc(stack, stack2);
std::cout << "Setting number to 69" << std::endl;
self->number = 69;
auto ret = self->targetFunc(420, stack2);
std::cout << "The burglared one returned: " << ret << std::endl;
return false;
std::cout << " this: " << self << std::endl;
std::cout << " this->number: " << self->number << "\n";
std::cout << " My stack " << stack << " and " << stack2 << std::endl;
return self->targetFunc(stack, stack2);
}

int main() {
Expand All @@ -39,12 +40,6 @@ int main() {
.m_abstract = tulip::hook::AbstractFunction::from(&targetFuncHook)
};

std::cout << "## __thiscall -> __cdecl ##\n";
std::cout << metadata.m_convention->generateIntoDefault(metadata.m_abstract) << "\n";

std::cout << "## __cdecl stack fix ##\n";
std::cout << metadata.m_convention->generateDefaultCleanup(metadata.m_abstract) << "\n";

auto handleResult = tulip::hook::createHandler(address, metadata);
if (!handleResult) {
std::cout << "creating the handler failed" << std::endl;
Expand All @@ -61,10 +56,11 @@ int main() {
std::cout << "hook created!" << std::endl;

auto bar = new FooBar;
std::cout << "bar is " << bar << std::endl;
bar->number = 23;
auto value = bar->targetFunc(5, 7);

std::cout << "targetFunc returned " << value << std::endl;
std::cout << "targetFunc returned " << value.x << std::endl;

return 0;
}

0 comments on commit 2ec2506

Please sign in to comment.