more tests and tweaks/fixes as a result
This commit is contained in:
parent
327dfb0cb7
commit
058069d1f3
6 changed files with 194 additions and 101 deletions
47
tests/test_local_function.cpp
Normal file
47
tests/test_local_function.cpp
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
// 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;
|
||||
}
|
||||
|
||||
TEST_CASE( "Overriding local function", "[local][attach]" )
|
||||
{
|
||||
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 );
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue