executablecpu-architectureinstructionsself-modifyingplatform-agnostic

Are there any smart cases of runtime code modification?


Can you think of any legitimate (smart) uses for runtime code modification (program modifying it's own code at runtime)?

Modern operating systems seem to frown upon programs that do this since this technique has been used by viruses to avoid detection.

All I can think of is some kind of runtime optimization that would remove or add some code by knowing something at runtime which cannot be known at compile time.


Solution

  • There are many valid cases for code modification. Generating code at run time can be useful for:

    Sometimes code is translated into code at runtime (this is called dynamic binary translation):

    Code modification can be used to work around limitations of the instruction set:

    More cases of code modification: