Reputation: 374
I started using vmprotect sdk to protect my app, but I've faced few issues, biggest one is compiler optimisations - it's optimising out my markers, take a look at example:
void *CaptureInterface( char *pszInterfaceName )
{
VMProtectBeginMutation( "capture_interface" );
void* ptr = 12345;
...
...
...
VMProtectEnd( );
return ptr;
}
I tried various combinations, VMProtectEnd before return, after, but compiler keeps optimising out VMProtectEnd function so call isn't visible in disassembly so markers begin being unpaired and I can't protect my app. Is there any way to turn optimisation related things for 1 or 2 functions? Or is there any other way to tell compiler to not touch that marker but keep optimising others?
Upvotes: 1
Views: 424
Reputation: 30206
These things never help much anyhow and personally I'd be worried that if they can't get such a basic thing right, what else they will do to your code which will be really fun to debug.
But anyhow, if you want to avoid them optimizing the code away, I'd probably do some inline assembly to make the call:
static void foo() {
}
void test() {
std::cout << "Hello World\n";
foo();
std::cout << "Goodbye world\n";
}
void test2() {
std::cout << "Hello World\n";
__asm {
call foo;
}
std::cout << "Goodbye world\n";
}
The call to foo in test2 is not optimized away in VS2015 with /O2. That's limited to x86 code though, because as far as I know Microsoft does not support inline assembly any longer for x64..
If the call to the function is not just a hint to the runtime, but the call itself is what's important, you could use a function pointer to achieve this in a portable manner. I don't know enough about the innards of the used library to know whether that's the case though.
Upvotes: 2
Reputation: 979
You are looking for the #pragma optimize
preprocessor directive.
See, e.g., https://msdn.microsoft.com/en-us/library/chh3fb0k.aspx
Upvotes: 2