2025-10-02 22:18:10 +02:00
// Copyright (c) Lysann Tranvouez. All rights reserved.
# include <catch2/catch_test_macros.hpp>
# include <mach_detours.h>
static int localFunctionCounter = 0 ;
int localFunction ( )
{
localFunctionCounter + + ;
return 67 ;
}
int ( * realLocalFunction ) ( ) = localFunction ;
static int localFunctionDetourCounter = 0 ;
int localFunctionDetour ( )
{
localFunctionDetourCounter + + ;
return 12 ;
}
2025-10-02 22:41:33 +02:00
TEST_CASE ( " Overriding local function " , " [attach][local] " )
2025-10-02 22:18:10 +02:00
{
localFunctionCounter = 0 ;
localFunctionDetourCounter = 0 ;
CHECK ( realLocalFunction = = localFunction ) ;
CHECK ( localFunction ( ) = = 67 ) ;
CHECK ( localFunctionCounter = = 1 ) ;
CHECK ( 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 * > ( & realLocalFunction ) , reinterpret_cast < detour_func_t > ( localFunctionDetour ) ) = = err_none ) ;
CHECK ( realLocalFunction ! = localFunction ) ;
CHECK ( localFunctionCounter = = 1 ) ;
CHECK ( localFunctionDetourCounter = = 0 ) ;
CHECK ( localFunction ( ) = = 12 ) ;
CHECK ( localFunctionCounter = = 1 ) ;
CHECK ( localFunctionDetourCounter = = 1 ) ;
// clean up
CHECK ( detour_transaction_begin ( ) = = err_none ) ;
CHECK ( detour_detach_and_commit ( reinterpret_cast < detour_func_t * > ( & realLocalFunction ) , reinterpret_cast < detour_func_t > ( localFunctionDetour ) ) = = err_none ) ;
}