In the Intel manuals, it details various steps which should be taken if code is self-modifying. These can be easily implemented in assembly. However, do compilers make any attempts to recognise that you're writing self-modifying code and account for this? Obviously there are cases where it can't tell, but if you do:
Code: Select all
void f(void)
{
unsigned short *smc = (unsigned short*) (void*) &f2;
// Write something to that address (in this case, UD2 instruction)
*smc = 0x0B0F;
// ^-- This will (I imagine?) probably segfault except in some circumstances (like kernel code?)
f2();
// ^-- Should generate invalid-opcode exception
I'm guessing the answer is 'No' and that it will conceivably throw a warning (at least), but I'm just curious (and don't have a compiler to hand here).