Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is it possible to use function pointers this way?

This is something that recently crossed my mind, quoting from wikipedia: "To initialize a function pointer, you must give it the address of a function in your program."

So, I can't make it point to an arbitrary memory address but what if i overwrite the memory at the address of the function with a piece of data the same size as before and than invoke it via pointer ? If such data corresponds to an actual function and the two functions have matching signatures the latter should be invoked instead of the first.

Is it theoretically possible ?

I apologize if this is impossible due to some very obvious reason that i should be aware of.

like image 514
user1909612 Avatar asked Dec 03 '13 16:12

user1909612


People also ask

Can we use pointers with functions?

You can use pointers to call functions and to pass functions as arguments to other functions. You cannot perform pointer arithmetic on pointers to functions.

Why would you use function pointers?

Function pointers can be useful when you want to create callback mechanism, and need to pass address of a function to another function. They can also be useful when you want to store an array of functions, to call dynamically for example.

How does a function pointer work?

func is a function that takes an int and a char * and returns an int. pFunc is a function pointer to which is assigned the address of a function that takes an int and a char * and returns an int.

Should you use function pointers in C?

Function pointers in C can be used to create function calls to which they point. This allows programmers to pass them to functions as arguments. Such functions passed as an argument to other functions are also called callback functions.


3 Answers

If you're writing something like a JIT, which generates native code on the fly, then yes you could do all of those things.

However, in order to generate native code you obviously need to know some implementation details of the system you're on, including how its function pointers work and what special measures need to be taken for executable code. For one example, on some systems after modifying memory containing code you need to flush the instruction cache before you can safely execute the new code. You can't do any of this portably using standard C or C++.

You might find when you come to overwrite the function, that you can only do it for functions that your program generated at runtime. Functions that are part of the running executable are liable to be marked write-protected by the OS.

like image 157
Steve Jessop Avatar answered Oct 24 '22 05:10

Steve Jessop


The issue you may run into is the Data Execution Prevention. It tries to keep you from executing data as code or allowing code to be written to like data. You can turn it off on Windows. Some compilers/oses may also place code into const-like sections of memory that the OS/hardware protect. The standard says nothing about what should or should not work when you write an array of bytes to a memory location and then call a function that includes jmping to that location. It's all dependent on your hardware and your OS.

like image 42
KitsuneYMG Avatar answered Oct 24 '22 04:10

KitsuneYMG


While the standard does not provide any guarantees as of what would happen if you make a function pointer that does not refer to a function, in real life and in your particular implementation and knowing the platform you may be able to do that with raw data.

I have seen example programs that created a char array with the appropriate binary code and have it execute by doing careful casting of pointers. So in practice, and in a non-portable way you can achieve that behavior.

like image 1
David Rodríguez - dribeas Avatar answered Oct 24 '22 03:10

David Rodríguez - dribeas