Section 3.17

Def-use chain

Full name: Definition-use chain.

Definition

A def-use chain is the set of links from one definition of a variable, an instruction that writes it, to every use, an instruction that reads it, that the written value can reach without being overwritten first. The reverse link, from one use to every definition that can supply its value, is called a use-def chain. Both are computed from reaching definitions §3.16, the data-flow analysis §3.15 that finds which definitions may reach each program point. Compilers, debuggers and security tools use the chains to follow values through a program.

The idea

A maintainer finds the bounds check if (len > 64) return; and wants to know whether it protects a copy of len bytes further down. The question is whether the copy reads the same len that the check tested. The chains answer it. If the use of len in the check and the use of len in the copy are reached by the same single definition, the copy reads the value that the check tested. If a second definition, such as len = len + 8, lies between the check and the copy, the copy’s use is reached by a value that was never checked.

The chains are wiring laid over the program: each write is wired to the reads it can feed, and each read to the writes that can feed it, skipping the instructions in between. With the wiring, a compiler finds a definition whose value nobody reads (its chain is empty, so the definition can be deleted if computing it has no other effect), and a security tool follows untrusted input from the place it is read to the places it is used, as taint analysis §3.19 does.

The difficulty is memory. Registers and simple local variables are written and read by name, so matching names is enough to wire them. Memory can be written under one name and read under another: a store through a pointer p can change the variable a. A chain builder that only matches names misses that wire.

How it works

Def-use chains for variables accessed only by name are built in four steps.

  1. Number the definitions and uses. For a register, or a local variable whose address is never taken, a definition is an instruction that names it as a destination, and a use is an instruction that names it as an operand.

  2. Compute reaching definitions: for each point, the set of definitions that may reach it.

  3. Build the use-def chains. For each use of variable vv, the use-def chain is the set of definitions of vv that reach that use.

  4. Invert them. Add the use uu to the def-use chain of every definition in the use-def chain of uu.

In static single assignment §3.38 (SSA) form, an intermediate representation §3.37 used by most modern optimizing compilers, each variable has exactly one definition. Where paths meet, a ϕ\phi-function (phi-function) selects the value that arrives on the edge actually taken. The def-use chain of an SSA name is then simply the list of its uses, and compilers keep that list up to date as they transform the program.

Memory needs more.

Worked example

Registers only. Consider these five instructions.

  1. d1d_1: x = 2.

  2. if c goto 4.

  3. d2d_2: x = 7.

  4. d3d_3: y = x + 3.

  5. return y.

If c is true, the run skips instruction 3 and reaches instruction 4 with x=2x = 2. If c is false, instruction 3 overwrites xx with 7. So both d1d_1 and d2d_2 reach the use of x in d3d_3. The chains are:

Following the chains backward from the returned value: y=x+3y = x + 3 with x=2x = 2 gives 2+3=52 + 3 = 5, and with x=7x = 7 gives 7+3=107 + 3 = 10. The function returns 5 or 10, depending on c.

Memory and an alias.

  1. d1d_1: a = 4.

  2. d2d_2: p = &a, so p holds the address of a.

  3. d3d_3: *p = 9, a store through p.

  4. d4d_4: y = a.

The only instruction that names a as a destination is d1d_1. A builder that matches names links the use of a in d4d_4 to d1d_1 and concludes y=4y = 4. The run gives y=9y = 9, because d3d_3 wrote a through the alias *p. With alias information, p certainly points to a, so d3d_3 is a certain definition of a: it kills d1d_1, and the use-def chain of a in d4d_4 is {d3}\{d_3\}.

Now change instruction 2 to p = c ? &a : &b, so that p points to a when c is true and to b otherwise. The store d3d_3 is then only a possible definition of a. It cannot kill d1d_1, because when p points to b, a keeps the value 4. The use-def chain of a in d4d_4 is {d1,d3}\{d_1, d_3\}, and yy is 4 or 9. Dropping d1d_1 would be wrong on runs where c is false; dropping d3d_3 would be wrong on runs where c is true.

Stated precisely

Let UU be the set of uses. For a definition or use tt, let loc(t)\mathrm{loc}(t) be the location it writes or reads, and for a use uu let Reach(u)\mathrm{Reach}(u) be the set of definitions that reach uu, as reaching definitions computes it. Then UseDef(u)={d∈Reach(u):loc(d)=loc(u)},DefUse(d)={u∈U:d∈UseDef(u)}.\mathrm{UseDef}(u) = \{\, d \in \mathrm{Reach}(u) : \mathrm{loc}(d) = \mathrm{loc}(u) \,\}, \qquad \mathrm{DefUse}(d) = \{\, u \in U : d \in \mathrm{UseDef}(u) \,\}. For memory, the equality loc(d)=loc(u)\mathrm{loc}(d) = \mathrm{loc}(u) is replaced by the condition that the location written by dd may be the location read by uu, as alias analysis decides.

The chains are sound when they contain every pair (d,u)(d, u) such that some run reads at uu the value written at dd. That holds under two assumptions. First, the reaching-definitions result is sound: the control-flow graph §3.13 (CFG) contains every possible transfer, every write is modeled as a definition, and a definition kills another only when it certainly overwrites the same location. Second, the may-alias relation contains every pair of locations that are the same in some run.

The chains can be large. For one variable with mm definitions that all reach nn uses, the chains hold m×nm \times n links: with 3 definitions and 4 uses, 3×4=123 \times 4 = 12. In SSA form, one ϕ\phi-function merges the 3 definitions into one name, giving 3+4=73 + 4 = 7 links: 3 into the ϕ\phi-function and 4 out of it. This saving is one reason compilers prefer SSA form.

Limits and common mistakes

A def-use chain records data dependence only: a value flows from a write to a read. It does not record control dependence, the fact that a condition decided whether the write ran at all. In if (secret) x = 1; else x = 0; the value of x reveals secret, although no chain links the two. Program slicing §3.20 and taint analysis must add control dependence.

Chains over-approximate. They include links along paths that no run takes, and weak updates keep older definitions on the chains.

Matching names is unsound for memory. Aliases, called functions that receive addresses, other threads and interrupt handlers can all write a location without naming it.

Coarse locations cost precision. An analysis that treats a whole array §14.5 or structure as one location links every store into it to every load from it. That is sound, but it adds many links that no run uses.

A use-def chain and a def-use chain contain the same pairs, read in opposite directions. Mixing them up answers the wrong question: “where can this value go” is a def-use question, and “where can this value come from” is a use-def question.

Chains describe one version of the program. After a compiler transforms the code, the chains must be updated or computed again.

Questions answered

How do aliases and memory stores complicate a def-use chain?

Aliases and memory stores break the assumption that a variable is written only under its own name, so a def-use chain, the link from an instruction that writes a value to every instruction that can read it, can no longer be built by matching names. An alias is a second name for the same memory location, for example a pointer p that holds the address of variable a. A store through p is then a definition of a, although a does not appear in the instruction. This causes four complications.

  1. Missed links. A builder that matches names does not link the store *p = 9 to a later read of a, so it believes that the read still sees the old value of a. A proof or an optimization based on that chain is wrong.

  2. Uncertain targets. When p may point to several locations, the store is only a possible definition of each. It must be added to the chains of each location, and it must not remove their older definitions, because on some runs it wrote elsewhere; this is a weak update. The chains grow and lose precision.

  3. Hidden writers. A called function that receives an address can store through it, and another thread or an interrupt handler can write shared memory between two instructions. Each is a possible definition that the analyzed instructions do not show.

  4. Dependence on alias analysis. The memory chains are only as good as the alias analysis that decides which stores can reach which loads: an imprecise one adds false links, and an unsound one drops true links.

The usual remedy is to keep exact chains for registers and for variables whose address is never taken, and conservative chains, built from a sound alias analysis, for memory.

Related entries: Reaching definitions (§3.16), Alias analysis (§3.18), Program slicing (§3.20), Static single assignment (SSA) (§3.38), Taint analysis (§3.19).