Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can compilers generate self modifying code?

It is commonly said that a static variable initialization is wrapped in an if to prevent it from being initialized multiple times.

For this, and other one-off conditions, it would be more efficient to have the code remove the conditional after the first pass via self modification.

Are C++ compilers allowed to generate such code and if not, why? I heard that it might have a negative impact on the cache, but I don’t know the details.

like image 449
janekb04 Avatar asked Aug 24 '20 21:08

janekb04


People also ask

Can a program modify itself?

Regardless, at a meta-level, programs can still modify their own behavior by changing data stored elsewhere (see metaprogramming) or via use of polymorphism.

What is a self-modifying program?

Self-modifying programs are programs which are able to modify their own code at runtime. Nowadays, self- modifying programs are commonly used. For example, a packer transforms any program into a program with equiva- lent behavior, but which decompresses and/or decrypts some instructions.

Is self-modifying code bad?

Self-modifying code is generally frowned upon. Not only is it hard to read and debug, it also creates problems with the cache (``cache coherence'').


1 Answers

There's nothing preventing a compiler from implementing what you suggest but it's a rather heavyweight solution to a very minor performance problem.

To implement the self-modifying code the compiler, for a typical C++ implementation running on Windows or Linux, would have to insert code that would change the permissions on the code page(s), modify the code, and then restore the permissions. These operations could easily cost far more cycles than then the implied "if" operation would take over the lifetime of the program.

This would also have the consequence of preventing the modified code pages from being shared between processes. That may seem inconsequential, but compilers often pessimize their code (pretty badly in the case of i386) in order to implement position independent code that can be loaded a different addresses at runtime without modifying the code and preventing sharing of code pages.

As Remy Lebeau and Nathan Oliver mention in comments there are also thread safety issues to consider, but they can probably be dealt with as there various solutions for hot patching executables like this.

like image 98
Ross Ridge Avatar answered Sep 30 '22 01:09

Ross Ridge