Use cases for self-modifying code?

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

+4
source share
2 answers

Metamorphism

One (dubious) use case that comes to my mind is metamorphic computer viruses . These are malicious software tools that hide themselves from signing based on a signature by rewriting their own machine code into a semantically equivalent representation that looks different.

trampoline

Another (more complicated, but more common) use case of trampolining is a method based on the generation of dynamic code to solve certain problems with nested function calls.

JIT Compilation

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

Safety implications

It should be borne in mind that the ability to interpret data as code is useful for using security holes in computer software, so the trend in modern hardware and operating systems is to allow and, if possible, even enforce separation of code and data (see also NX bit and DEP ).

+4
source

I can better answer this by referring to the answer to a similar (exceptionally well-written and answer) question, also to StackOverflow - Homoiconic and "unlimited" self-modifying code + Is lisp really self-modifying? . The answer focuses on Lisp, a family language known for taking code-data to the next level, and explores the use of this in AI.

+1
source

Source: https://habr.com/ru/post/1389085/


All Articles