c++ - What does void(*)() mean in code -
i saw code today in fb profile, , not able understand , how working:-
(*(void(*)()) shellcode)()
can please explain me, above code mean ?
full code snippet below :-
#include <stdio.h> #include <string.h> char *shellcode = "\x31\xc0\x50\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69" "\x6e\x89\xe3\x50\x53\x89\xe1\xb0\x0b\xcd\x80"; int main(void) { fprintf(stdout,"length: %d\n",strlen(shellcode)); (*(void(*)()) shellcode)(); return 0; }
it cast function pointer (with no returned result , no arguments). prefer using typedef
define signature of such functions:
typedef void plainsig_t(void);
then code
(*(plainsig_t*)shellcode) ();
for function pointers, don't need dereference them, shorter code:
((plainsig_t*) shellcode) ();
which calls function machine code located inside shellcode
memory zone.
btw, not strictly portable c. in principle, there no guarantee can cast data pointer function pointer. (on weird processors -e.g. embedded microcontrollers, dsp, 1970s era computers-, code , data sit in different address spaces, or have different pointer sizes, etc....). common processors , abi (x86-64/linux, arm/android, ....) have same address space code , data , accept casting function pointers data pointers , vice versa.
Comments
Post a Comment