Section 3.20

Program slicing

Definition

Program slicing is a technique that extracts from a program the statements that can affect, or be affected by, the values of chosen variables at a chosen point, together called the slicing criterion; the extracted statements form a slice. A backward slice holds every statement that can influence the criterion; a forward slice holds every statement that the criterion can influence. Slicing follows data dependence, where one statement reads a value that another wrote, and control dependence, where one statement decides whether another runs.

The idea

A program of 2,000 lines returns a wrong value for a variable fee at line 1,850. Most of the lines cannot affect fee: they print reports, update other records or handle other requests. The investigator’s question is which statements could have produced this value. A backward slice answers it mechanically and keeps only those statements. Security review asks the same question about a decision: which code can influence the variable that decides whether a request is allowed? A reviewer who reads the backward slice of that decision reads everything that can change it, and nothing else.

The intuition is to walk backwards from the value and ask two questions at each statement found: who wrote this value? and who decided whether that write happened? The first question follows data dependence, and the second follows control dependence. Ask both questions again about every statement found, until nothing new appears. A text search for the name of the variable answers neither question fully: it misses writes made through pointers, and it misses the branch conditions that chose between writes.

How it works

Mark Weiser introduced slicing in 1981. The usual way to compute a slice today goes through a graph of dependences.

  1. Choose the criterion: a program point nn and a set of variables VV, written ⟨n,V⟩\langle n, V \rangle.

  2. Find the data dependences. Statement tt is data dependent on statement ss when ss writes a variable vv, tt reads vv, and some path in the control-flow graph §3.13 (the diagram of which statement can run after which) leads from ss to tt with no other write to vv. The reaching definitions §3.16 analysis computes these pairs, which are also called def-use chains §3.17.

  3. Find the control dependences. Statement pp postdominates statement ss when every path from ss to the end of the program passes through pp. Statement tt is control dependent on a branch bb when, after one outgoing edge of bb, every path to the end of the program passes through tt, while after another outgoing edge some path avoids tt. In other words, bb decides whether tt runs.

  4. Build the program dependence graph (PDG): one node per statement, and an edge from ss to tt for each data or control dependence of tt on ss. Ferrante, Ottenstein and Warren described this graph in 1987 [261].

  5. Collect the slice. The backward slice is every node from which a path of edges leads to the criterion node; the analysis finds these nodes by walking edges backwards from the criterion. The forward slice is every node reached by walking edges forwards from the criterion.

Memory and function calls need more. A write through one pointer and a read through another form a data dependence when the two may refer to the same memory, so the graph needs alias analysis §3.18. A call needs either the statements of the called function copied into the graph or a summary of what the function reads and writes. The system dependence graph of Horwitz, Reps and Binkley (1990) [262] extends the PDG across calls.

A static slice covers every execution that the analysis models. A dynamic slice, introduced by Korel and Laski (1988) [263], covers one recorded execution with one input. It keeps only the statements that actually influenced the criterion in that run, so it is usually much smaller, and it says nothing about other inputs.

Worked example

Take this eight-line program, which reads two inputs and returns s:

1 read(a);
2 read(b);
3 s = 0;
4 if (a > 10)
5 s = 1;
6 t = b * 2;
7 print(t);
8 return s;

The criterion is ⟨8,{𝚜}⟩\langle 8, \{\texttt{s}\} \rangle: the value of s returned on line 8.

Walk backwards from line 8.

  1. Line 8 reads s. Two writes of s can reach it: line 3, along the path where the test on line 4 fails, and line 5. So line 8 is data dependent on lines 3 and 5.

  2. Line 5 runs only when the test on line 4 is true, so line 5 is control dependent on line 4. Line 3 runs on every path, so it is not.

  3. Line 4 reads a, which line 1 wrote, so line 4 is data dependent on line 1.

  4. Line 1 depends on nothing earlier. Lines 2, 6 and 7 read b, compute t and print it; no statement found so far reads b or t.

The backward slice is lines 1, 3, 4, 5 and 8, five of the eight lines.

Check the slice on two inputs. With a equal to 3, the original program sets s to 0, finds 3>103 > 10 false, and returns 0; the slice runs the same statements and also returns 0. With a equal to 20, both set s to 1 and return 1. Now drop the control dependence and keep only lines 3, 5 and 8, which is what following data dependence alone gives. That program is s = 0; s = 1; return s;, which returns 1 for every input. With a equal to 3 it returns 1 where the original returns 0. Line 5 copies no data from a; the only link from a to the result is the decision on line 4.

The forward slice from ⟨1,{𝚊}⟩\langle 1, \{\texttt{a}\} \rangle is lines 1, 4, 5 and 8: line 4 reads a, line 5 is control dependent on line 4, and line 8 reads the s that line 5 writes. Line 3 is not in it, because its value 0 does not depend on a.

Stated precisely

Weiser’s definition (1981, 1984) [264] states what a slice must preserve. A slice of a program PP for the criterion ⟨n,V⟩\langle n, V \rangle is a program SS obtained from PP by deleting zero or more statements, such that for every input on which PP halts, SS also halts on that input, and the sequence of values that the variables in VV hold at the successive visits to nn is the same in SS as in PP.

In the dependence graph, write s→ts \to t when tt is data dependent or control dependent on ss. The backward slice for the criterion node nn is {s∣s→*n}\{\, s \mid s \to^{*} n \,\}, where s→*ns \to^{*} n means that a path of zero or more edges leads from ss to nn. Control dependence is defined with postdominance: tt is control dependent on bb exactly when there is a path from bb to tt on which tt postdominates every node after bb, and tt does not strictly postdominate bb, where strictly means that tt postdominates bb and is a different node. For a branch with two outgoing edges, this says that tt is certain to run after one edge and may be skipped after the other.

The graph slice is correct in Weiser’s sense under three assumptions: every data dependence is in the graph, which needs a sound alias analysis and correct summaries of called code; every control dependence is in the graph, including those created by jumps such as break, return and exceptions; and every input, output and device access that the criterion can depend on is a statement in the graph. Weiser also showed that finding a smallest possible slice is undecidable §19.2.1 in general, so practical slices can contain statements that a perfect analysis would drop.

Limits and common mistakes

A smaller program is not automatically a correct slice, and even a correct slice preserves only what the definition names: the values of the criterion variables, on inputs where the original halts. It can change everything else.

A use of a slice that relies on any of these needs its own argument.

A dependence missing from the graph shrinks the slice silently. A wrong no-alias answer removes a real data dependence through memory, and an ignored control dependence removes the branch that chose a value, as the example shows.

A static slice can be large. In a program with many pointers, or one in which all work passes through one central dispatch loop, the backward slice of almost any value contains most of the program.

A slice follows dependence through the state the analysis models. It does not show influence through anything else, such as a file written in one run and read in the next, or a secret that changes how long a computation takes, which is a side channel §1.29.

Questions answered

Why must a backward slice include control dependencies?

A backward slice must include control dependencies because a branch can decide which write reaches the criterion, or whether a write happens at all, without the tested value ever being copied into the result. In the program s = 0; if (a > 10) s = 1; return s; the returned value is either the constant 0 or the constant 1, so following data dependence alone from return s finds only the two assignments. Those two assignments alone, s = 0; s = 1; return s;, return 1 for every input. With a equal to 3 the original returns 0, so a slice without the test gives the wrong value. The test a > 10 is what links the input to the result.

Once the branch is in the slice, the statements that compute its condition must be included too, here the statement that reads a; otherwise the slice cannot evaluate the test. For security review the point is sharper. When a backward slice is used to find all code that can influence a decision such as allow or deny, a slice without control dependencies hides every influence that acts by choosing a branch, and checks act exactly that way.

Related entries: Def-use chain (§3.17), Alias analysis (§3.18), Model checking (§3.30), Control-flow graph (CFG) (§3.13), Reaching definitions (§3.16), Taint analysis (§3.19). Further reading: [261, 262, 263, 264].