question

sneakyevil-4798 avatar image
0 Votes"
sneakyevil-4798 asked sneakyevil-4798 commented

MSVC C++17 compile self-crashing call

Hello,

I've tried multiple things that I use for my vtable calling function but it seems like the compiler can't simply do "call eax".

Code I use for calling:

 M::VFunc<void(__stdcall*)(IMaterial*, OverrideType_t)>(this, 1)(newMaterial, 0);

VFunc:

 template <class T>
 static __forceinline T VFunc(void* pThis, int iIndex)
 {
     return reinterpret_cast<T>(GetPointerFromVTable(pThis, iIndex));
 }

How it looks like in ASM after compilation:
qFRfFMR.png


How it should actually looks like:
9CN4JCS.png

Since the error is obvious with the storing variable in [ebp - xx] because after push it moves all stuff but the compiler still thinks the call will be still there. And I don't see the point why the compiler doesn't generate straight up "call eax" when it will never touch eax...

I tried changing optimization settings, C++ versions, etc... Nothing change.
Any help will be appreciated.





c++
· 6
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.


Maybe GetPointerFromVTable is not so good?

Do you have a simple project that illustrates the problem (on OneDrive, GitHub, etc.)?

0 Votes 0 ·

GetPointerFromVTable is literally just ASM code that reads ECX pointer to EAX and then reads [EAX + EDX * 0x4] there is nothing wrong with that function. I also noticed the compiler straight up ignores typedef functions and doesnt call them directly.

Example with typedef:
J2rdOcE.png

I don't share the project anywhere but the issue is just straight up with calling any function. If its variable/hardcoded value. It always push it to [ebp +/- xx] and then calls it via [ebp +/- xx] instead of calling directly it by ex. call [variable] or call value


0 Votes 0 ·

It seems to work well in Visual Studio 2019. Maybe try implementing the GetPointerFromVTable function in C++, not in ASM.

0 Votes 0 ·

As I said this thing happens even for typedef functions. It doesnt call them directly as you would define it example:

tTypedefFunction tFunction;
tFunction();

and the result in asm is not (call tFunction) it just does useless mov to the ebp and then call dword ptr of that ebp.

EDIT:

After editing my project and changing it to C++14 just for testing it seems like is just C++17 issue?
ASM generated using C++14
GzirOa9.png



0 Votes 0 ·

Is this observed using default Release configuration?

0 Votes 0 ·
Show more comments

0 Answers