Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the C Standard Allow for Self-Modifying Code?

Is self-modifying code possible in a portable manner in C?

The reason I ask is that, in a way, OOP relies on self-modifying code (because the code that executes at run-time is actually generated as data, e.g. in a v-table), and yet, it seems that, if this is taken too far, it would prevent most optimizations in a compiler.

For example:

void add(char *restrict p, char *restrict pAddend, int len)
{
    for (int i = 0; i < len; i++)
        p[i] += *pAddend;
}

An optimizing compiler could hoist the *pAddend out of the loop, because it wouldn't interfere with p. However, this is no longer a valid optimization in self-modifying code.

In this way, it seems that C doesn't allow for self-modifying code, but at the same time, wouldn't that imply that you can't do some things like OOP in C? Does C really support self-modifying code?

like image 889
user541686 Avatar asked Jun 18 '11 21:06

user541686


People also ask

What allows self-modifying codes?

The Push programming language is a genetic programming system that is explicitly designed for creating self-modifying programs. While not a high level language, it is not as low level as assembly language.

Can a program change itself?

Self-modifying code is a programming philosophy in which the developer makes a program that is able to alter its own coding when executed. While the developer can enter parameters for the self-modifying code, it usually changes and optimizes itself without interaction.

What does modifying code mean?

Modified Code means any modification, addition and/or development of code scripts deviating from the original product code as delivered by Droplet Computing for use in a production environment.


1 Answers

Self-modifying code is not possible in C for many reasons, the most important of which are:

  1. The code generated by the compiler is completely up to the compiler, and might not look anything like what the programmer trying to write code that modifies itself expects. This is a fundamental problem with doing SMC at all, not just a portability problem.
  2. Function and data pointers are completely separate in C; the language provides no way to convert back and forth between them. This issue is not fundamental, since some implementations or higher-level standards (POSIX) guarantee that code and data pointers share a representation.

Aside from that, self-modifying code is just a really really bad idea. 20 years ago it might have had some uses, but nowadays it will result in nothing but bugs, atrocious performance, and portability failures. Note that on some ISAs, whether the instruction cache even sees changes that were made to cached code might be unspecified/unpredictable!

Finally, vtables have nothing to do with self-modifying code. It's purely a matter of modifying function pointers, which are data, not code.

like image 126
R.. GitHub STOP HELPING ICE Avatar answered Nov 12 '22 06:11

R.. GitHub STOP HELPING ICE