Function pointer to data?

Posts 1–9 of 9 · Page 1 of 1
Function pointer to data?
Is it possible to have a function pointer that points to say... an array of bytes, and actually execute/call it? I'm a little confused about the limitations on function pointers lol.
umm functions are like arrays of bytes
Pointers are all the same lower level, it's only in high level languages that there are things like 'function pointers' and 'char pointers'. They are there so that the compiler knows how to handle them and what kinds of instructions to generate for them. I'm guessing you're asking this because you wish to execute a piece of code located on the heap or else in the .data section. Here's how you'd do that in C++

Code:
BYTE ToExecute[] = { 0xEB, 0xFE }; // infinite loop

__asm{
lea eax, ToExecute
jmp eax ;; call eax
}
Is there any way to do that in 64-bit C++ code? As far as I'm aware, you can't do inline in 64-bit. :/
Function pointers have nothing to do with a function being inline. And in 64 it's the same really, the only difference will be this:


Code:
lea rax, Some_64bit_offset
call rax ; jmp rax
Using rAX instead of eAX, obviously the former is only 32bit while rax is 64 bit.
I thought inline wasn't supported in x64. 0_o
I don't see why it wouldn't, inline simply means that the compiler doesn't generate a call to a function. But installs the function in the main function.
For example:
Code:
int return1(){ return 1;}
Is:

Code:
Return1 Proc
mov eax, 1
ret
Return1 endp

call Return1  ; not-inline
or:

Code:
mov eax, 1    ;; inline
Alternatively you can use a typedef to define the function's signature and then just cast the pointer to the bytes into the new type pointer and call it same way as you would call any function. It just saves on some manual asm and the compiler can handle it for you, plus it makes things a bit easier to follow.

Code:
typedef int (__stdcall *fMyFunction)(void); //similar to int __stdcall function(void)

BYTE *pData = (BYTE*)"\xDE\xAD\xBE\xEF"; //functions data

fMyFunction MyFunction = (fMyFunction)pData; //cast the data pointer into a function pointer.

MyFunction(); //call the function.
So if I'm using Visual Studio 2010 Ultimate, 64-bit inline assembly will work? If not, what compiler(s) support it? Also which assembler is better in your opinion, MASM or NASM? Or should I use a different one?
Posts 1–9 of 9 · Page 1 of 1

Post a Reply

Similar Threads

Talk with us