Program slicing explained

In computer programming, program slicing is the computation of the set of program statements, the program slice, that may affect the values at some point of interest, referred to as a slicing criterion. Program slicing can be used in debugging to locate source of errors more easily. Other applications of slicing include software maintenance, optimization, program analysis, and information flow control.

Slicing techniques have been seeing a rapid development since the original definition by Mark Weiser. At first, slicing was only static, i.e., applied on the source code with no other information than the source code. Bogdan Korel and Janusz Laski introduced dynamic slicing, which works on a specific execution of the program (for a given execution trace).[1] Other forms of slicing exist, for instance path slicing.[2]

Static slicing

Based on the original definition of Weiser,[3] informally, a static program slice S consists of all statements in program P that may affect the value of variable v in a statement x. The slice is defined for a slicing criterion C=(x,v) where x is a statement in program P and v is variable in x. A static slice includes all the statements that can affect the value of variable v at statement x for any possible input. Static slices are computed by backtracking dependencies between statements. More specifically, to compute the static slice for (x,v), we first find all statements that can directly affect the value of v before statement x is encountered. Recursively, for each statement y which can affect the value of v in statement x, we compute the slices for all variables z in y that affect the value of v. The union of all those slices is the static slice for (x,v).

Example

For example, consider the C program below. Let's compute the slice for (write(sum), sum). The value of sum is directly affected by the statements "sum = sum + i + w" if N>1 and "int sum = 0" if N <= 1. So, slice(write(sum), sum) is the union of three slices and the "int sum = 0" statement which has no dependencies:

  1. slice(sum = sum + i + w, sum),
  2. slice(sum = sum + i + w, i),
  3. slice(sum = sum + i + w, w), and
  4. .
It is fairly easy to see that slice(sum = sum + i + w, sum) consists of "sum = sum + i + w" and "int sum = 0" because those are the only two prior statements that can affect the value of sum at "sum = sum + i + w". Similarly, slice(sum = sum + i + w, i) only contains "for(i = 1; i < N; ++i)

Notes and References

  1. Korel . Bogdan . Laski . Janusz . Dynamic Program Slicing . Information Processing Letters . 1988 . 29 . 3 . 155–163 . 10.1016/0020-0190(88)90054-3 . 10.1.1.158.9078 .
  2. Book: Jhala. Ranjit. Majumdar. Rupak. Proceedings of the 2005 ACM SIGPLAN conference on Programming language design and implementation . Path slicing . 2005. PLDI '05. New York, NY, USA. ACM. 38–47. 10.1145/1065010.1065016. 9781595930569. 5065847 .
  3. Weiser. Mark David. Program Slices: Formal, Psychological, and Practical Investigations of an Automatic Program Abstraction Method. 1979. PhD Thesis. University of Michigan. Ann Arbor, MI, USA.