Compiler optimization merge identical functions implementation meant to be stubs to be detoured during runtime

StackOverflow https://stackoverflow.com/questions/13522531

Pregunta

I have a C++ test project with a bunch of stub functions that have the same implementation. Those stubs are meant to be 'replaced' during runtime using Windows Detours. The issue is that, in release mode, the compiler make all of those stubs to point to the same implementation. To illustrate this, consider this code:

#include <iostream>
using namespace std;

void A() { cout << "stub" << endl; }
void B() { cout << "stub" << endl; }

void main()
{
    cout << &A << ", " << &B << endl;
}

In debug mode, the pointer values will be different. In release mode, they are the same. I tried the pragma optimize directive (I am using the Microsoft compiler) but it didn't fix the issue. As a result, my Windows Detours hook intercept all the calls to the identical stubs.

How can I fix this? Thanks.

¿Fue útil?

Solución

Try using preprocessor macros to make your stub functions unique so the optimizer won't merge them into one.

__FILE__, __LINE__, and __FUNCTION__ usage in C++

Something like this:

void A() { cout << __FUNCTION__ << endl; }
void B() { cout << __FUNCTION__ << endl; }
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top