This is known as 'self modifying code' (see wikipedia) and it used to be quite trendy in the 80s and early 90s. Particularly in machine code and ASM, however, it pretty much died out as an approach with modern languages because it's pretty fragile. Managed languages attempted to provide a more secure model as it was also the basis for a buffer-overrun attack.
Bearing in mind your code pages may be marked as read-only or copy-on-write and you might get access violation on many modern OS's, but if memory serves me, the basic principle you need to get hold of a memory address of a variable, or function, and you need to have quite specific knowledge about the code generated and/or stack layout at that location.
Here's some links to get you started;
- How to write self-modifying code in x86 assembly
- Self-modifying code for debug tracing in quasi-C
Specifically, in your case, I wouldn't modify foo
by inserting operations and then trying to adjust all the code, all you need to do is change the jump
address to bar
to go through an intermediary. This is known as a Thunk
. The advantage of doing it this way is that it's much less fragile to modify a jump address from one to another because it doesn't change the structure of the original function, just a number. In fact it's trivial by comparison.
In your thunk
you can do whatever operations you like before and after you call the real function. If you're already in the same address space and your thunk code is loaded you're home.
If you're on Windows, you might also want to take a look at Detours.