I'm trying to do something pretty basic in x64 asm:
Have an asm function which take a function pointer and sets this in a variable. This function is called from C code.
Have another asm function which calls the function pointer if not null, this function pointer is also a C function (as as set by the function in 1).
Here is what I have so far for the C side of things:
extern "C" void _asm_set_func_ptr(void* ptr);
void _cdecl c_call_back()
{
}
void init()
{
_asm_set_func_ptr(c_call_back);
}
And the asm side:
.DATA
g_pFuncPtr QWORD 0
.CODE ;Indicates the start of a code segment.
_asm_set_func_ptr PROC fPtr:QWORD
mov [rsp+qword ptr 8], rcx
mov rax, [rsp+qword ptr 8]
mov g_pFuncPtr, rax
ret
_asm_set_func_ptr ENDP
_asm_func PROC
push RBX
push RBP
push RDI
push RSI
push RSP
push R12
push R13
push R14
push R15
CMP g_pFuncPtr, 0
JE SkipCall
MOV RAX, [ g_pFuncPtr ];
CALL RAX;
SkipCall:
pop RBX
pop RBP
pop RDI
pop RSI
pop RSP
pop R12
pop R13
pop R14
pop R15
ret
_asm_func ENDP
But it seems I damage the stack after calling _asm_set_func_ptr(), also I'm not sure if how I call g_pFuncPtr in _asm_func is correct? What is wrong with my code? I'm building this with VS2013 MASM64.
First, you generally need to pop the registers in the reverse order in which you push them, i.e.:
push RBX
, push RBP
... push R15
--> pop R15
... pop RSI
, pop RBX
, ret
. This will definitely break the caller of _asm_func
.
Next you should look at the Windows x64 calling convention what all is necessary to make proper function calls. It is very important to get all the requirements right, otherwise things can break and even very late in some else's code, which is not the greatest thing to debug.
For example, you don't need to save all registers. If the callback function destroys them, it will save and restore them itself. So no pushing and popping is necessary there, RAX
can be invalidated anyway, no argument is being passed in it.
But then note this part:
In the Microsoft x64 calling convention, it's the caller's responsibility to allocate 32 bytes of "shadow space" on the stack right before calling the function (regardless of the actual number of parameters used), and to pop the stack after the call.
So you should do SUB ESP, 32
before your code, then ADD ESP, 32
before the RET
.
There is also the requirement for "stack aligned on 16 bytes", but you don't currently need to address that, because "8 bytes of return address + 32 bytes of shadow space + 8 bytes of next return address" is aligned on 16 bytes.
Additionally, the Windows x64 ABI has also strict requirements on exception handling and correct unwinding. As Raymond pointed out in the comment, because your function is not a leaf one (calls other functions), you need to provide a proper prologue and epilogue instead -- see here.
The temporary saving of RCX
at the beginning of _asm_set_func_ptr
is unnecessary.
Otherwise I don't see any problems there, though.
Finally, semicolons ;
are not needed at end of lines in assembler files.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments