else localFunction in test_local_function is likely inlined and the tests fail
50 lines
2 KiB
C++
50 lines
2 KiB
C++
// Copyright (c) Lysann Tranvouez. All rights reserved.
|
|
|
|
#include <catch2/catch_test_macros.hpp>
|
|
|
|
#include <mach_detours.h>
|
|
|
|
// Note that this file must be compiled without optimizations (or at least without inlining)
|
|
// else localFunction() calls below are likely going to be inlined at the call site, and detours will not work.
|
|
|
|
static int localFunctionCounter = 0;
|
|
int localFunction()
|
|
{
|
|
localFunctionCounter++;
|
|
return 67;
|
|
}
|
|
int (*realLocalFunction)() = localFunction;
|
|
static int localFunctionDetourCounter = 0;
|
|
int localFunctionDetour()
|
|
{
|
|
localFunctionDetourCounter++;
|
|
return 12;
|
|
}
|
|
|
|
TEST_CASE( "Overriding local function", "[attach][local]" )
|
|
{
|
|
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 );
|
|
}
|