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
|
|
@ -1,7 +1,9 @@
|
|||
# Copyright (c) Lysann Tranvouez. All rights reserved.
|
||||
|
||||
add_executable(mach_detours_tests
|
||||
test.cpp)
|
||||
test_dylib_function.cpp
|
||||
test_local_function.cpp
|
||||
)
|
||||
|
||||
# The target function must be in a shared library because otherwise it might be in the same code page as the test.cpp functions.
|
||||
# Between attach and commit the target function's code page is not executable, which can mean our test driver code would not
|
||||
|
|
|
|||
|
|
@ -1,79 +0,0 @@
|
|||
// Copyright (c) Lysann Tranvouez. All rights reserved.
|
||||
|
||||
#include <catch2/catch_test_macros.hpp>
|
||||
|
||||
#include <mach_detours.h>
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include "lib_function.h"
|
||||
int (*realLibFunction)() = libFunction;
|
||||
|
||||
static int libFunctionDetourCounter = 0;
|
||||
int libFunctionDetour()
|
||||
{
|
||||
libFunctionDetourCounter++;
|
||||
return 94;
|
||||
}
|
||||
|
||||
TEST_CASE( "Overriding custom function in dylib" )
|
||||
{
|
||||
libFunctionCounter = 0;
|
||||
libFunctionDetourCounter = 0;
|
||||
|
||||
REQUIRE( libFunction() == 42 );
|
||||
REQUIRE( libFunctionCounter == 1 );
|
||||
REQUIRE( libFunctionDetourCounter == 0 );
|
||||
|
||||
CHECK( detour_transaction_begin() == err_none );
|
||||
CHECK( detour_attach(reinterpret_cast<detour_func_t*>(&realLibFunction), reinterpret_cast<detour_func_t>(libFunctionDetour)) == err_none );
|
||||
CHECK( detour_transaction_commit() == err_none );
|
||||
|
||||
REQUIRE( realLibFunction != libFunction );
|
||||
|
||||
REQUIRE( libFunctionCounter == 1 );
|
||||
REQUIRE( libFunctionDetourCounter == 0 );
|
||||
REQUIRE( libFunction() == 94 );
|
||||
REQUIRE( libFunctionCounter == 1 );
|
||||
REQUIRE( libFunctionDetourCounter == 1 );
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
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 );
|
||||
}
|
||||
108
tests/test_dylib_function.cpp
Normal file
108
tests/test_dylib_function.cpp
Normal file
|
|
@ -0,0 +1,108 @@
|
|||
// Copyright (c) Lysann Tranvouez. All rights reserved.
|
||||
|
||||
#include <catch2/catch_test_macros.hpp>
|
||||
|
||||
#include <mach_detours.h>
|
||||
|
||||
#include "lib_function.h"
|
||||
int (*realLibFunction)() = libFunction;
|
||||
|
||||
static int libFunctionDetourCounter = 0;
|
||||
int libFunctionDetour()
|
||||
{
|
||||
libFunctionDetourCounter++;
|
||||
return 94;
|
||||
}
|
||||
|
||||
TEST_CASE( "Overriding custom function in dylib", "[dylib]" )
|
||||
{
|
||||
libFunctionCounter = 0;
|
||||
libFunctionDetourCounter = 0;
|
||||
|
||||
SECTION( "attaching installs a detour" )
|
||||
{
|
||||
REQUIRE( realLibFunction == libFunction );
|
||||
REQUIRE( libFunction() == 42 );
|
||||
REQUIRE( libFunctionCounter == 1 );
|
||||
REQUIRE( libFunctionDetourCounter == 0 );
|
||||
|
||||
REQUIRE( detour_transaction_begin() == err_none );
|
||||
|
||||
SECTION( "attach + transaction_commit" )
|
||||
{
|
||||
REQUIRE( detour_attach(reinterpret_cast<detour_func_t*>(&realLibFunction), reinterpret_cast<detour_func_t>(libFunctionDetour)) == err_none );
|
||||
CHECK( detour_transaction_commit() == err_none );
|
||||
}
|
||||
SECTION( "attach_and_commit" )
|
||||
{
|
||||
CHECK( detour_attach_and_commit(reinterpret_cast<detour_func_t*>(&realLibFunction), reinterpret_cast<detour_func_t>(libFunctionDetour)) == err_none );
|
||||
}
|
||||
|
||||
CHECK( realLibFunction != libFunction );
|
||||
CHECK( libFunctionCounter == 1 );
|
||||
CHECK( libFunctionDetourCounter == 0 );
|
||||
CHECK( libFunction() == 94 );
|
||||
CHECK( libFunctionCounter == 1 );
|
||||
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>(libFunctionDetour)) == err_none );
|
||||
}
|
||||
|
||||
SECTION( "detaching in separate transaction removes detour" )
|
||||
{
|
||||
REQUIRE( detour_transaction_begin() == err_none );
|
||||
REQUIRE( detour_attach_and_commit(reinterpret_cast<detour_func_t*>(&realLibFunction), reinterpret_cast<detour_func_t>(libFunctionDetour)) == err_none );
|
||||
REQUIRE( realLibFunction != libFunction );
|
||||
|
||||
REQUIRE( detour_transaction_begin() == err_none );
|
||||
|
||||
SECTION( "detach + transaction_commit" )
|
||||
{
|
||||
REQUIRE( detour_detach(reinterpret_cast<detour_func_t*>(&realLibFunction), reinterpret_cast<detour_func_t>(libFunctionDetour)) == err_none );
|
||||
CHECK( detour_transaction_commit() == err_none );
|
||||
}
|
||||
SECTION( "detach_and_commit" )
|
||||
{
|
||||
CHECK( detour_detach_and_commit(reinterpret_cast<detour_func_t*>(&realLibFunction), reinterpret_cast<detour_func_t>(libFunctionDetour)) == err_none );
|
||||
}
|
||||
|
||||
CHECK( realLibFunction == libFunction );
|
||||
CHECK( libFunctionCounter == 0 );
|
||||
CHECK( libFunctionDetourCounter == 0 );
|
||||
CHECK( libFunction() == 42 );
|
||||
CHECK( libFunctionCounter == 1 );
|
||||
CHECK( libFunctionDetourCounter == 0 );
|
||||
}
|
||||
|
||||
SECTION( "aborting transaction means no detour" )
|
||||
{
|
||||
REQUIRE( detour_transaction_begin() == err_none );
|
||||
REQUIRE( detour_attach(reinterpret_cast<detour_func_t*>(&realLibFunction), reinterpret_cast<detour_func_t>(libFunctionDetour)) == err_none );
|
||||
CHECK( detour_transaction_abort() == err_none );
|
||||
|
||||
CHECK( realLibFunction == libFunction );
|
||||
CHECK( libFunctionCounter == 0 );
|
||||
CHECK( libFunctionDetourCounter == 0 );
|
||||
CHECK( libFunction() == 42 );
|
||||
CHECK( libFunctionCounter == 1 );
|
||||
CHECK( libFunctionDetourCounter == 0 );
|
||||
}
|
||||
|
||||
SECTION( "an error in a transaction means no detour" )
|
||||
{
|
||||
REQUIRE( detour_transaction_begin() == err_none );
|
||||
REQUIRE( detour_attach(reinterpret_cast<detour_func_t*>(&realLibFunction), reinterpret_cast<detour_func_t>(libFunctionDetour)) == err_none );
|
||||
// cannot detach because trampoline is not yet in place
|
||||
CHECK( detour_detach(reinterpret_cast<detour_func_t*>(&realLibFunction), reinterpret_cast<detour_func_t>(libFunctionDetour)) == KERN_FAILURE );
|
||||
CHECK( detour_transaction_commit() == KERN_FAILURE );
|
||||
|
||||
CHECK( realLibFunction == libFunction );
|
||||
CHECK( libFunctionCounter == 0 );
|
||||
CHECK( libFunctionDetourCounter == 0 );
|
||||
CHECK( libFunction() == 42 );
|
||||
CHECK( libFunctionCounter == 1 );
|
||||
CHECK( libFunctionDetourCounter == 0 );
|
||||
}
|
||||
}
|
||||
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