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.
Choose the criterion: a program point and a set of variables , written .
Find the data dependences. Statement is data dependent on statement when writes a variable , reads , and some path in the control-flow graph §3.13 (the diagram of which statement can run after which) leads from to with no other write to . The reaching definitions §3.16 analysis computes these pairs, which are also called def-use chains §3.17.
Find the control dependences. Statement postdominates statement when every path from to the end of the program passes through . Statement is control dependent on a branch when, after one outgoing edge of , every path to the end of the program passes through , while after another outgoing edge some path avoids . In other words, decides whether runs.
Build the program dependence graph (PDG): one node per statement, and an edge from to for each data or control dependence of on . Ferrante, Ottenstein and Warren described this graph in 1987 [261].
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 : the value of s returned on line 8.
Walk backwards from line 8.
Line 8 reads
s. Two writes ofscan 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.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.
Line 4 reads
a, which line 1 wrote, so line 4 is data dependent on line 1.Line 1 depends on nothing earlier. Lines 2, 6 and 7 read
b, computetand print it; no statement found so far readsbort.
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 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 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 for the criterion is a program obtained from by deleting zero or more statements, such that for every input on which halts, also halts on that input, and the sequence of values that the variables in hold at the successive visits to is the same in as in .
In the dependence graph, write when is data dependent or control dependent on . The backward slice for the criterion node is , where means that a path of zero or more edges leads from to . Control dependence is defined with postdominance: is control dependent on exactly when there is a path from to on which postdominates every node after , and does not strictly postdominate , where strictly means that postdominates and is a different node. For a branch with two outgoing edges, this says that 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.
Termination. If a loop that can run forever is left out of the slice, the slice halts on inputs where the original never reaches .
Faults. If
x = 1 / yis left out of the slice, then withyequal to 0 the original program fails at the division while the slice runs on.Outputs and timing. Prints, writes to devices and the time the program takes are not preserved.
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].