2025-10-01 23:04:46 +02:00
// Copyright (c) Lysann Tranvouez. All rights reserved.
2025-10-01 23:46:55 +02:00
# include <catch2/catch_test_macros.hpp>
# include <mach_detours.h>
2025-10-01 23:55:17 +02:00
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2025-10-02 20:51:25 +02:00
# include "lib_function.h"
int ( * realLibFunction ) ( ) = libFunction ;
2025-10-01 23:46:55 +02:00
2025-10-02 20:51:25 +02:00
static int libFunctionDetourCounter = 0 ;
int libFunctionDetour ( )
2025-10-01 23:46:55 +02:00
{
2025-10-02 20:51:25 +02:00
libFunctionDetourCounter + + ;
2025-10-01 23:46:55 +02:00
return 94 ;
}
2025-10-01 23:55:17 +02:00
TEST_CASE ( " Overriding custom function in dylib " )
2025-10-01 23:46:55 +02:00
{
2025-10-02 20:51:25 +02:00
libFunctionCounter = 0 ;
libFunctionDetourCounter = 0 ;
2025-10-01 23:46:55 +02:00
2025-10-02 20:51:25 +02:00
REQUIRE ( libFunction ( ) = = 42 ) ;
REQUIRE ( libFunctionCounter = = 1 ) ;
REQUIRE ( libFunctionDetourCounter = = 0 ) ;
2025-10-01 23:46:55 +02:00
CHECK ( detour_transaction_begin ( ) = = err_none ) ;
2025-10-02 20:51:25 +02:00
CHECK ( detour_attach ( reinterpret_cast < detour_func_t * > ( & realLibFunction ) , reinterpret_cast < detour_func_t > ( libFunctionDetour ) ) = = err_none ) ;
2025-10-01 23:46:55 +02:00
CHECK ( detour_transaction_commit ( ) = = err_none ) ;
2025-10-02 20:51:25 +02:00
REQUIRE ( realLibFunction ! = libFunction ) ;
2025-10-01 23:46:55 +02:00
2025-10-02 20:51:25 +02:00
REQUIRE ( libFunctionCounter = = 1 ) ;
REQUIRE ( libFunctionDetourCounter = = 0 ) ;
REQUIRE ( libFunction ( ) = = 94 ) ;
REQUIRE ( libFunctionCounter = = 1 ) ;
REQUIRE ( libFunctionDetourCounter = = 1 ) ;
2025-10-01 23:46:55 +02:00
}
2025-10-01 23:55:17 +02:00
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static int localFunctionCounter = 0 ;
int localFunction ( )
{
localFunctionCounter + + ;
return 67 ;
}
static int localFunctionDetourCounter = 0 ;
int localFunctionDetour ( )
{
localFunctionDetourCounter + + ;
return 12 ;
}
TEST_CASE ( " Overriding local function " )
{
int ( * realFunction ) ( ) = localFunction ;
localFunctionCounter = 0 ;
localFunctionDetourCounter = 0 ;
REQUIRE ( localFunction ( ) = = 67 ) ;
REQUIRE ( localFunctionCounter = = 1 ) ;
REQUIRE ( localFunctionDetourCounter = = 0 ) ;
CHECK ( detour_transaction_begin ( ) = = err_none ) ;
// Overriding a local function requires using detour_attach_and_commit instead of calling attach and commit individually.
// Otherwise when we return from attach (and before commit), the code page with the local function is marked as read+write (but _not_ executable).
// There is a good chance the code we return to (in this case the test function) is on the same memory page and can therefore not be executed (until we call commit).
CHECK ( detour_attach_and_commit ( reinterpret_cast < detour_func_t * > ( & realFunction ) , reinterpret_cast < detour_func_t > ( localFunctionDetour ) ) = = err_none ) ;
REQUIRE ( realFunction ! = localFunction ) ;
REQUIRE ( localFunctionCounter = = 1 ) ;
REQUIRE ( localFunctionDetourCounter = = 0 ) ;
REQUIRE ( localFunction ( ) = = 12 ) ;
REQUIRE ( localFunctionCounter = = 1 ) ;
REQUIRE ( localFunctionDetourCounter = = 1 ) ;
}