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 ).
source share