x64 asm how to set a function pointer to a _cdecl C function and call it?

paulm

I'm trying to do something pretty basic in x64 asm:

  1. Have an asm function which take a function pointer and sets this in a variable. This function is called from C code.

  2. 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.

Yirkha

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

x64 asm how to set a function pointer to a _cdecl C function and call it?

From Dev

Calling ASM x64 function from C (double), GAS

From Dev

How to call a C function from ASM in C?

From Dev

How to call a function by a pointer?

From Dev

C: Pointer after function call

From Dev

How to call function pointer in STL

From Dev

Set C Function Pointer to C# Function

From Dev

Call C/C++ function from assembly (OSX Mavericks x64)

From Dev

c++ pointer to pointer and call function that receive pointer to pointer

From Dev

C/C++ How to hand over a const function pointer or a simple pointer in one function call

From Dev

C++: How to call a member function pointer that is a member of the same class?

From Dev

c++ how to call function with subclass, having superclass pointer

From Dev

How to set a function attribute to a function pointer?

From Dev

How to set a function attribute to a function pointer?

From Dev

How do I call a C++ Class member function with the correct 'this' pointer like a normal C function? (pointer to class member function)

From Dev

how to get the address of an asm function in c

From Dev

Array asm function in C

From Dev

Allocating memory for a pointer inside a function call in C

From Dev

Incompatible pointer type in function call - C

From Dev

Call C++ function pointer from Javascript

From Dev

C++ Pointer to function call not in scope

From Dev

c++ template call of function pointer type

From Dev

C++ pointer, reference and function call

From Dev

Odd C syntax in pointer declaration and function call

From Dev

function pointer assignment and call in c++?

From Dev

c++ template call of function pointer type

From Dev

Allocating memory for a pointer inside a function call in C

From Dev

C++ pointer, reference and function call

From Dev

Call C standard library function from asm in Visual Studio

Related Related

  1. 1

    x64 asm how to set a function pointer to a _cdecl C function and call it?

  2. 2

    Calling ASM x64 function from C (double), GAS

  3. 3

    How to call a C function from ASM in C?

  4. 4

    How to call a function by a pointer?

  5. 5

    C: Pointer after function call

  6. 6

    How to call function pointer in STL

  7. 7

    Set C Function Pointer to C# Function

  8. 8

    Call C/C++ function from assembly (OSX Mavericks x64)

  9. 9

    c++ pointer to pointer and call function that receive pointer to pointer

  10. 10

    C/C++ How to hand over a const function pointer or a simple pointer in one function call

  11. 11

    C++: How to call a member function pointer that is a member of the same class?

  12. 12

    c++ how to call function with subclass, having superclass pointer

  13. 13

    How to set a function attribute to a function pointer?

  14. 14

    How to set a function attribute to a function pointer?

  15. 15

    How do I call a C++ Class member function with the correct 'this' pointer like a normal C function? (pointer to class member function)

  16. 16

    how to get the address of an asm function in c

  17. 17

    Array asm function in C

  18. 18

    Allocating memory for a pointer inside a function call in C

  19. 19

    Incompatible pointer type in function call - C

  20. 20

    Call C++ function pointer from Javascript

  21. 21

    C++ Pointer to function call not in scope

  22. 22

    c++ template call of function pointer type

  23. 23

    C++ pointer, reference and function call

  24. 24

    Odd C syntax in pointer declaration and function call

  25. 25

    function pointer assignment and call in c++?

  26. 26

    c++ template call of function pointer type

  27. 27

    Allocating memory for a pointer inside a function call in C

  28. 28

    C++ pointer, reference and function call

  29. 29

    Call C standard library function from asm in Visual Studio

HotTag

Archive