Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use cases for self-modifying code?

On a Von Neumann architecture, program and data are both stored in memory, so a program can modify itself. Is this useful for a programmer? Could you give some examples?

like image 593
gylns Avatar asked Jan 03 '12 14:01

gylns


People also ask

How does self-modifying code work?

In computer science, self-modifying code (SMC) is code that alters its own instructions while it is executing – usually to reduce the instruction path length and improve performance or simply to reduce otherwise repetitively similar code, thus simplifying maintenance.

Can a program edit its own code?

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?

Related Definitions Modified Code means any modification, addition and/or development of code scripts deviating from the predefined product code tree(s)/modules developed by VMware for production deployment or use.


2 Answers

Metamorphism

One (questionable) use case that comes to my mind is metamorphic computer viruses. These are malicious pieces of software that conceal themselves from signature based detection by rewriting their own machine code to an semantically equivalent representation that looks different.

Trampolining

Another (more complex, but also more common) use case is trampolining, a technique based on dynamic code generation to solve certain problems with nested function calls.

JIT compilation

The most common usage of dynamic code generation that I can think of is JIT (just-in-time) compilation. Modern languages like .NET or Java are not compiled into native machine code, but into some kind of intermediate language (called bytecode). This bytecode is then interpreted when the program is executed (by a virtual machine written for the target architecture). At the same time, a background process checks which parts of the code are executed very often. These parts then have a good chance of being dynamically compiled into native machine language for maximum performance. All this happens during the run time of the program!

Security implications

One thing to keep in mind is that the possibility to interpret data as code is useful for exploiting security holes in computer software, which is why the trend in modern hardware and operating systems is to enable and, if possible, even enforce the separation of code and data (also see NX bit and DEP).

like image 92
Niklas B. Avatar answered Jan 01 '23 13:01

Niklas B.


I can best answer this by referring you to an answer to a similar (exceptionally well written and answered) question, also on StackOverflow - Homoiconic and "unrestricted" self modifying code + Is lisp really self modifying?. The answer focuses on Lisp, a family languages known for taking "code is data" to the next level, and explores the uses of that in AI.

like image 40
Lee Hambley Avatar answered Jan 01 '23 12:01

Lee Hambley