How to intercept a call to a free feature like hippomocks?

I am wondering what hippomocks do to intercept the call function exit, for example, as shown in the code below:

   MockRepository mocks;
   mocks.ExpectCallFunc(exit).With(2).Throw(std::exception());
+4
source share
2 answers

The code that performs the interception is in hippomocks.h. It changes the memory protection flags to allow writing to the address of the provided function pointer, and then writes the jump instruction instead of the initial bytes of the function. When the hook is no longer needed, the original bytes are restored. This is the same approach used, for example, in the Microsoft Detours library .

+5
source

, ( , ) char *, , ( mprotect Unices VirtualProtect Windows), 5 14 - . ( ) , .

, HippoMocks, Replace . ( hippomocks.h GitHub 32/64 x86, ARM ). 200, . horrible_cast Unprotect; - ( reinterpret_cast), ( ).

++ 11 , detail/replace.h, , . https://github.com/dascandy/hippomocks/blob/cpp11/HippoMocks/detail/replace.h.

+5

All Articles