test calling through trampoline

This commit is contained in:
Lysann Tranvouez 2025-10-02 22:51:15 +02:00
parent 7257abce71
commit bf83e14d26

View file

@ -13,6 +13,11 @@ static int libFunctionDetour()
libFunctionDetourCounter++;
return 94;
}
static int libFunctionDetourCallingReal()
{
libFunctionDetourCounter++;
return realLibFunction() + 94;
}
TEST_CASE( "Attaching custom function in dylib installs detour", "[attach][dylib]" )
{
@ -48,6 +53,54 @@ TEST_CASE( "Attaching custom function in dylib installs detour", "[attach][dylib
CHECK( detour_detach_and_commit(reinterpret_cast<detour_func_t*>(&realLibFunction), reinterpret_cast<detour_func_t>(libFunctionDetour)) == err_none );
}
TEST_CASE( "We can call real function using the reassigned trampoline pointer", "[attach][dylib]" )
{
libFunctionCounter = 0;
libFunctionDetourCounter = 0;
REQUIRE( realLibFunction == libFunction );
REQUIRE( libFunction() == 42 );
REQUIRE( libFunctionCounter == 1 );
REQUIRE( libFunctionDetourCounter == 0 );
REQUIRE( detour_transaction_begin() == err_none );
CHECK( detour_attach_and_commit(reinterpret_cast<detour_func_t*>(&realLibFunction), reinterpret_cast<detour_func_t>(libFunctionDetour)) == err_none );
CHECK( realLibFunction != libFunction );
CHECK( realLibFunction() == 42 );
CHECK( libFunctionCounter == 2 );
CHECK( libFunctionDetourCounter == 0 );
// clean up
REQUIRE( detour_transaction_begin() == err_none );
CHECK( detour_detach_and_commit(reinterpret_cast<detour_func_t*>(&realLibFunction), reinterpret_cast<detour_func_t>(libFunctionDetour)) == err_none );
}
TEST_CASE( "Detour function can call real function", "[attach][dylib]" )
{
libFunctionCounter = 0;
libFunctionDetourCounter = 0;
REQUIRE( realLibFunction == libFunction );
REQUIRE( libFunction() == 42 );
REQUIRE( libFunctionCounter == 1 );
REQUIRE( libFunctionDetourCounter == 0 );
REQUIRE( detour_transaction_begin() == err_none );
CHECK( detour_attach_and_commit(reinterpret_cast<detour_func_t*>(&realLibFunction), reinterpret_cast<detour_func_t>(libFunctionDetourCallingReal)) == err_none );
CHECK( realLibFunction != libFunction );
CHECK( libFunctionCounter == 1 );
CHECK( libFunctionDetourCounter == 0 );
CHECK( libFunction() == 94 + 42 );
CHECK( libFunctionCounter == 2 );
CHECK( libFunctionDetourCounter == 1 );
// clean up
REQUIRE( detour_transaction_begin() == err_none );
CHECK( detour_detach_and_commit(reinterpret_cast<detour_func_t*>(&realLibFunction), reinterpret_cast<detour_func_t>(libFunctionDetourCallingReal)) == err_none );
}
TEST_CASE( "Detaching custom function in dylib removes detour", "[detach][dylib]" )
{
libFunctionCounter = 0;