taocp

This site has no text of the book. Keep your own copy of TAOCP next to it. See what is checked.

Exercise · Volume 1 · 1.4.1-7

Exercise 1.4.1-7

not stated yet in Lean solution written

Rating20
KindInformal: the exercise has no formal statement, so Lean does not state it from the written solution
SectionSection 1.4.1 of Volume 1. Companion chapter: Subroutines

The task

Explain why programmers now avoid code that changes its own instructions.

The exact wording is in your copy of the book.

Hints

Open one hint at a time. Try the exercise again after each.

Hint 1

Think of what a computer does with instruction memory: where it keeps copies, what it assumes.

Hint 2

Think about two programs, or two threads, that use the same code at the same time.

Solution

Show the solution

Several reasons work together.

  • Memory protection. If code is never written, the operating system can mark its pages read-only and share one copy among many processes. It can also drop a page and read it again from the file later, as no one has changed it. A program that changes itself needs a private writable copy.
  • Security. A page that is writable and executable is a target: an error in the program can let data be run as code. Modern systems refuse to give both rights at once.
  • Caches. A processor can keep instructions in a separate cache that it never writes back. If the code could change, the machine would have to look at each store to see if it hits a cached instruction, which costs circuits and time.
  • Pipelines. A processor fetches and decodes the next instructions before it finishes the present one. If an instruction changes one that is already in the pipeline, the pipeline must be flushed. The check for this is complex and slows the machine.
  • Reentrancy. Two processes or threads cannot execute one self-modifying routine at the same time, because each needs its own return address and its own changed fields. Code that keeps its state in registers or on a stack can be shared. In particular recursive calls fail with the linkage of this section.
  • Understanding and tools. A reader cannot tell from the listing which instruction is going to run, because the text and the executed program differ. Debuggers, profilers, jump-trace routines and proofs of correctness work on the unchanged text.

The idea behind the old technique is still sound. A return address must be kept somewhere. Modern machines keep it in a register or on a stack, which are data, so the code stays fixed.

Remarks

The text of Section 1.4.1 says that the next edition of the book will use this view. The MMIX machine keeps the return address in a special register. The matter is discussed in the C1 chapter on Section 1.4.1 and in Exercise 1.4.1-5, where the linkage by self-modification is used on purpose.

This solution is drafted, and a person has not reviewed it yet. The source.

In Lean

not stated yet The exercise is not stated in Lean yet.

The status is computed from the Lean build. The node of the map.

Something is wrong? Report it.