Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiler ignore __stdcall

It seems to me, that MSVS ignores __stdcall directive on my functions. I'm cleaning up the stack manually, but the compiler still append ADD ESP instructions after each CALL.

This is how I declare the function:

extern "C" void * __stdcall core_call(int addr, ...);
#define function(...) (DWORD WINAPI) core_call(12345, __VA_ARGS__)
return function("Hello", 789);

And this is how the output looks like: enter image description here
(source: server4u.cz)

I've marked with arrows redundant ADD instructions, which MSVS automatically append after each call, despite the fact, that cleaining the stack is a callee responsibility (reference: http://en.wikipedia.org/wiki/X86_calling_conventions#List_of_x86_calling_conventions) and this causes the crash of my progrm. If I manually replace the ADD instructions with NOPs, program works as supposed. So, my question is... Is there a way how to force the compiler to stop addaing these instructions?

Thanks.

like image 933
Peter Avatar asked Apr 18 '26 00:04

Peter


1 Answers

The problem is here: , ...).

Functions with variable number of arguments cannot be __stdcall.

__stdcall functions must remove all their stack arguments from the stack at the end, but they can't know in advance how much stuff they will receive as parameters.

The same holds for __fastcall functions.

The only applicable calling convention for functions with variable number of arguments is __cdecl, where the caller has to remove the stack parameters after the call. And that's what the compiler uses despite your request to use __stdcall.

like image 60
Alexey Frunze Avatar answered Apr 20 '26 12:04

Alexey Frunze