45
Presenter’s Name: Keyur Malaviya Survey of program slicing techniques

Presenter’s Name: Keyur Malaviya Survey of program slicing techniques

  • View
    215

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Presenter’s Name: Keyur Malaviya Survey of program slicing techniques

Presenter’s Name: Keyur Malaviya

Survey of program slicing techniques

Page 2: Presenter’s Name: Keyur Malaviya Survey of program slicing techniques

Purpose of this paper

It’s a survey that presents an overview of program slicing

Various general approaches used to compute slices Specific techniques used to address procedures,

unstructured control flow, composite data types and pointers, and concurrency.

Static and dynamic slicing methods for each of these features

Comparison and classification in terms of their accuracy and efficiency

Page 3: Presenter’s Name: Keyur Malaviya Survey of program slicing techniques

Topics Covered

Definitions Static slicing vs Dynamic slicing Basic slicing algorithm for single procedure and

multiprocedureWeiser AlgorithmHauslerBergeretti and Carr´eHorwitz, Reps, and Binkley Algo

Applications

Page 4: Presenter’s Name: Keyur Malaviya Survey of program slicing techniques

Definitions (Basics)

(1) read(n); (2) i := 1; (3) sum := 0; (4) product := 1; (5) while i <= n do begin (6) sum := sum + i; (7) product := product * i; (8) i := i + 1 end; (9) write(sum); (10) write(product)

Slicing? Slicing Criteria? Static and Dynamic slicing? Program slicing? Program dependence graph (PDG) or Control flow graph (CFG) or

System dependency grapy (SDG)

Page 5: Presenter’s Name: Keyur Malaviya Survey of program slicing techniques

Definitions (CFG \ PDG)

PDG: Directed graph; Vertices = statements and control predicates Edges = data and control dependences

CFG

Page 6: Presenter’s Name: Keyur Malaviya Survey of program slicing techniques

Definitions

Program slice: consists of the parts of a program that affect the values computed at some point of interest.

Slicing criterion: is this point of interest specified by a pair (program point, set of variables)

Original concept by Weiser: Its a mental abstractions that people make when they are debugging a program

Static slicing: Computed without making assumptions regarding a program’s input

Dynamic slicing: Relies on some specific test case

Page 7: Presenter’s Name: Keyur Malaviya Survey of program slicing techniques

Definitions (criteria and slicing )

(1) read(n);(2) i := 1;(3) sum := 0;(4) product := 1;(5) while i <= n do begin(6) sum := sum + i;(7) product := product * i;(8) i := i + 1 end;(9) write(sum);(10) write(product)

Slice of this program w.r.t criterion (10, product)

(1) read(n);(2) i := 1;(3) sum := 0;(4) product := 1;(5) while i <= n do begin(6) sum := sum + i;(7) product := product * i;(8) i := i + 1 end;(9) write(sum);(10) write(product)

(1) read(n);(2) i := 1;(3)(4) product := 1;(5) while i <= n do begin(6) (7) product := product * i;(8) i := i + 1 end;(9) (10) write(product)

Single-procedure programs (PDG); Shading in the PDG shown before vertices in the slice w.r.t. write(product)

Page 8: Presenter’s Name: Keyur Malaviya Survey of program slicing techniques

Static slicing vs Dynamic slicing

Dynamic Slicing: First introduced by Korel and Laski

Non-interactive variation of Balzer’s flowback analysis

Flowback analysis: Interactively traverse a graph (data and

control dependences between statements in the program); For

e.g.: S(V) depends on T(V), S and T are statements; T S is

in CFG, then trace back from vertex for S to vertex for T

Only the dependences that occur in a specific execution of the program are taken into account

Dynamic slicing criterion is a triple (input, occurrence of a statement, variable) – it specifies the input, and distinguishes between different occurrences of a statement in the execution history

Dynamic slicing assumes fixed input for a program Static slicing does not make assumptions regarding the input.

Page 9: Presenter’s Name: Keyur Malaviya Survey of program slicing techniques

Static slicing vs Dynamic slicingcriterion SS: (8, x) and DS: (n=2, 81, x)

read(n);i := 1;while (i <= n) dobeginif (i mod 2 = 0) thenx := 17else;i := i + 1end;write(x)

read(n);i := 1;while (i <= n) dobeginif (i mod 2 = 0) thenx := 17elsex := 18;i := i + 1end;write(x)

read(n);i := 1;while (i <= n) dobeginif (i mod 2 = 0) thenx := 17elsex := 18;i := i + 1end;write(x)

123

45

67

8

Example program: Dynamic slice w.r.t. criterion (n=2, 81, x)

Static slice w.r.t. criterion (8, x)

Page 10: Presenter’s Name: Keyur Malaviya Survey of program slicing techniques

Slicing Algorithm Approaches

Achieved through one of three algorithmic approaches:1) data-flow equations 2) system dependency graph 3) parallel algorithm

All based on control and data dependencies and defined in terms of a graph representation of a program (as seen before)

Page 11: Presenter’s Name: Keyur Malaviya Survey of program slicing techniques

Approaches:

Statements and control predicates are gathered by way of a backward traversal of the program’s control flow graph (CFG) or PDG, starting at the slicing criterion

Weiser’s approach: compute slices from consecutive sets of transitively relevant statements ( data flow and control flow dependences )

Ottenstein approach: in terms of a reachability problem in a PDG.

Slicing criterion A vertex in the PDG; A Slice corresponds to all PDG vertices from

which the vertex under consideration can be reached

Other approaches: Based on modified and extended versions of PDGs

Page 12: Presenter’s Name: Keyur Malaviya Survey of program slicing techniques

Weiser Algorithm (single procedure)

Two levels of iteration:

1. Transitive data dependences in the presence of loops in the program

2. Control dependences, initiating the inclusion of control predicates for which each, step 1 is repeated to include the statements it is dependent upon

Determine directly relevant variables and then indirectly relevant variables; From these compute the sets of relevant statements

Page 13: Presenter’s Name: Keyur Malaviya Survey of program slicing techniques

Parameters and equations

Defined and Referenced Variables DEF(i) and REF(i) Say at node ‘i’ consider a statement a = b + c Then DEF(i) = {a} and REF(i) = {b, c}

Directly Relevant Variable : set of directly relevant variables,

where slice criterion = (V, n) Set DRV (i) Set DRV (all nodes j) that have a

direct edge to i,

Page 14: Presenter’s Name: Keyur Malaviya Survey of program slicing techniques

Parameters and equations

Directly Relevant Statements

: set of all nodes i that define a variable v that is relevant at the successor node of I

Indirectly Relevant Variables referenced variables in control predicate are indirectly

relevant when at least one of the statements in its body is relevant, denoted:

b is known as a range of influence INFL (b),

Page 15: Presenter’s Name: Keyur Malaviya Survey of program slicing techniques

Example program

Page 16: Presenter’s Name: Keyur Malaviya Survey of program slicing techniques

Applying the Weiser algo

NODE DEF

1 {n}

2 {i}

3 {sum}

4 {product}

5 0

6 {sum}

7 {product}

8 {i}

9 0

10 0

REF

0

0

0

0

{i, n}

{sum, i}

{product, i}

{i}

{sum}

{product}

INFL

0

0

0

0

{6, 7, 8}

0

0

0

0

0

Slicing criterion (10, product) & our example program

{product}

R0

Page 17: Presenter’s Name: Keyur Malaviya Survey of program slicing techniques

Applying the Weiser algo

NODE DEF

1 {n}

2 {i}

3 {sum}

4 {product}

5 0

6 {sum}

7 {product}

8 {i}

9 0

10 0

REF

0

0

0

0

{i, n}

{sum, i}

{product, i}

{i}

{sum}

{product}

Slicing criterion (10, product) & our example program

{product}

{product}

R0

{product}

Page 18: Presenter’s Name: Keyur Malaviya Survey of program slicing techniques

Applying the Weiser algo

NODE DEF

1 {n}

2 {i}

3 {sum}

4 {product}

5 0

6 {sum}

7 {product}

8 {i}

9 0

10 0

REF

0

0

0

0

{i, n}

{sum, i}

{product, i}

{i}

{sum}

{product}

Slicing criterion (10, product) & our example program

{product}

{product}

R0

{product}

{product, i}

Page 19: Presenter’s Name: Keyur Malaviya Survey of program slicing techniques

Applying the Weiser algo

NODE DEF

1 {n}

2 {i}

3 {sum}

4 {product}

5 0

6 {sum}

7 {product}

8 {i}

9 0

10 0

REF

0

0

0

0

{i, n}

{sum, i}

{product, i}

{i}

{sum}

{product}

Slicing criterion (10, product) & our example program

R0

0

0

{i}

{i}

{product, i}

{product, i}

{product, i}

{product, i}

{product}

{product}

Slicing criterion (5, {i, n}) & repeat the same procedure

{product, i, n}

{i, n}

{i, n}

{n}0

{product, i, n}{product, i, n}{product, i, n}

{product}{product}

Page 20: Presenter’s Name: Keyur Malaviya Survey of program slicing techniques

NODE DEF

1 {n}

2 {i}

3 {sum}

4 {product}

5 0

6 {sum}

7 {product}

8 {i}

9 0

10 0

REF

0

0

0

0

{i, n}

{sum, i}

{product, i}

{i}

{sum}

{product}

INFL

0

0

0

0

{6, 7, 8}

0

0

0

0

0

R0

0

0

{i}

{i}

{product, i}

{product, i}

{product, i}

{product, i}

{product}

{product}

R1

0

{n}

{i, n}

{i, n}

{product, i, n}

{product, i, n}

{product, i, n}

{product, i, n}

{product}

{product}

Slicing criterion (10, product) & our example program

Applying the Weiser algo

? ? ?

Page 21: Presenter’s Name: Keyur Malaviya Survey of program slicing techniques

Equations for related statements:

Page 22: Presenter’s Name: Keyur Malaviya Survey of program slicing techniques

Hausler (functional style)

For each type of statement, have a function

and & express how a statement transforms

the set of relevant variables & relevant statements reply.

Functions for a while statement are obtained by transforming it into an infinite sequence of if statements

Page 23: Presenter’s Name: Keyur Malaviya Survey of program slicing techniques

Information-flow relations(Bergeretti and Carr´e)

( e can be control predicate or right-hand side of assignment)

Statement S: variable v and an expression e

We define relations:

They possess following properties:

iff the value of v on entry to S potentially affects the value computed for e

iff the value computed for e potentially affects the value of v on exit from S,

iff the value of v on entry to S may affect the value of v on exit from S.

Page 24: Presenter’s Name: Keyur Malaviya Survey of program slicing techniques

Information-flow relations(Bergeretti and Carr´e)

How to get the slice with respect to the final value of v ? The set of all expressions e for which can

be used to construct “partial statements” replace all statements in S that do not contain expressions in by empty statements.

Relations are computed in a syntax-directed, bottom-up

For S, v := e

Page 25: Presenter’s Name: Keyur Malaviya Survey of program slicing techniques

Information-flow relations(Bergeretti and Carr´e)

Set of expressions that potentially affect the value of product at the end of the program are {1, 2, 4, 5, 7, 8}

Partial statement is obtainedby omitting all statements from the program that do not contain expressions in this set, i.e., both assignments to sum and both write statements

The slice is same as Weiser’s algorithm

Page 26: Presenter’s Name: Keyur Malaviya Survey of program slicing techniques

Dependence graph based approaches (PDG) and Procedures

PDG variant of Ottenstein shows considerably more detail than that by Horwitz, Reps, and Binkley

Procedures Call-return structure of interprocedural execution paths Single pass considers infeasible execution paths – a

problem called “calling-context”

Will see two approaches: Weiser’s approach (CFG) Horwitz, Reps, and Binkley (SDG)

Page 27: Presenter’s Name: Keyur Malaviya Survey of program slicing techniques

Dependence graph based approaches (PDG) and Procedures

Weiser’s approach for interprocedural static slicing: Interprocedural summary information is computed,

using previously developed techniques

P, set MOD(P) of variables = modified by P, and set USE(P) of variables = used by P

Intraprocedural slicing algorithm: Treat ‘P()’ as a conditional assignment statement ‘if SomePredicate then MOD(P) := USE(P)’ (external procedures, source-code is unavailable?)

Page 28: Presenter’s Name: Keyur Malaviya Survey of program slicing techniques

Weiser’s approach

Actual inter-procedural slicing algo that generates new slicing criteria iteratively w.r.t slices computed in step (2):

(i) procedures Q called by P: consist of all pairs

(i) procedures Q called by P (ii) procedures R that call P

(i) procedures Q called by P:

(ii) procedures R that call P:(ii) procedures R that call P: consist of all pairs

Page 29: Presenter’s Name: Keyur Malaviya Survey of program slicing techniques

To formalize the generation of new criteria: UP(S) : Map (a set S of slicing criteria in a P) to

(a set of criteria in procedures that call P) DOWN(S): Map (a set S of slicing criteria in a P) to

(a set of criteria in procedures called by P)

Set of all criteria: transitive and reflexive closure of the UP and DOWN relations (UP U DOWN)*

UP and DOWN sets: Requires sets of relevant variables to be known at all call sites computation of these sets is done by slicing these procedures

When iteration stops? When no new criteria are generated

Weiser’s Algo

Page 30: Presenter’s Name: Keyur Malaviya Survey of program slicing techniques

Main issue:

program Main; … while ( ) do P(x1, x2, , xn); z := x1; x1 := x2; x2 := x3; xn1 := xn end; (L) write(z) end

procedure P(y1, y2, … , yn);beginwrite(y1);write(y2);…(M) write(yn)end

Procedure P is sliced ‘n’ times by Weiser’s algorithm for criterion (L, {z}).

Page 31: Presenter’s Name: Keyur Malaviya Survey of program slicing techniques

Lprogram point at S = write(z) M program point at last statement in P Slice w.r.t. criterion (L, { z })?

‘n’ iterations of the body of the while loop During the ith iteration, variables x1, …, xi will be

relevant at call site DOWN(Main): criterion (M, { y1, …, yi }) gets

included Issue is: ???

Weiser’s Algo

Procedure P will be sliced n times

Page 32: Presenter’s Name: Keyur Malaviya Survey of program slicing techniques

What was the problem?

Weiser’s algorithm does not take into account which output parameters are dependent on which input parameters is a source of imprecision

Lets see another examples that shows this problem:

Page 33: Presenter’s Name: Keyur Malaviya Survey of program slicing techniques

What was the problem?

program Example;begin(1) a := 17;(2) b := 18;(3) P(a, b, c, d);(4) write(d)endprocedure P(v, w, x, y);(5) x := v;(6) y := wend

program Example;begin

a := 17;

b := 18;

P(a, b, c, d);

endprocedure P(v, w, x, y);

;y := wend

program Example;begin;b := 18;P(a, b, c, d);write(d)endprocedure P(v, w, x, y);;y := wend

a := 17;

Slice with Weiser’s algoActual Slice

Page 34: Presenter’s Name: Keyur Malaviya Survey of program slicing techniques

Horwitz, Reps, and Binkley Algo

Computes precise inter-procedural static slices:

1. SDG, a graph representation for multi-procedure programs

2. Computation of inter-procedural summary information precise dependence relations between i/p & o/t

parameters explicitly present in SDG as summary edges

3. Two-pass algorithm for extracting interprocedural slices from an SDG

Page 35: Presenter’s Name: Keyur Malaviya Survey of program slicing techniques

Multi-procedure program

Page 36: Presenter’s Name: Keyur Malaviya Survey of program slicing techniques

Horwitz, Reps, and Binkley Algo 1) Structure of SDG

SDG = PDG for main program, & a procedure dependence graph for each procedure

SDG <> PDG (Vertices and edges are different) For each call statement, there is a call-site vertex in the

SDG as well as actual-in and actual-out vertices

Page 37: Presenter’s Name: Keyur Malaviya Survey of program slicing techniques

1) Structure of SDG

interprocedural dependence edges: (i) control dependence edge (call-site vertex & entry vertex) (ii) parameter-in edge between corresponding actual-in and formal-in vertices, (iii) a parameter out edge between corresponding formal-out and actual-out vertices, and (iv) summary edges that represent transitive interprocedural data dependences

Each procedure dependence graph has an entry vertex, and formal-in and formal-out vertices

Page 38: Presenter’s Name: Keyur Malaviya Survey of program slicing techniques

1) Structure of SDG

Page 39: Presenter’s Name: Keyur Malaviya Survey of program slicing techniques

Horwitz, Reps, and Binkley Algo 2) and 3)

Second part: Models the calling relationships between the procedures (as in a call

graph) Compute subordinate characteristic graph For each procedure in the program, this graph contains edges that

correspond to precise transitive flow dependences between its input and output parameters.

Third part: summary edges of an SDG serve to circumvent the calling

context problem First phase: all vertices from which ‘s’ can be reached without

descending into procedure calls (slicing starts at vertex s) Second phase: remaining vertices in the slice by descending

into all previously side-stepped calls

Page 40: Presenter’s Name: Keyur Malaviya Survey of program slicing techniques

COMPLETE SDG

NEXT: Complete SDG for the example program shown above

Page 41: Presenter’s Name: Keyur Malaviya Survey of program slicing techniques
Page 42: Presenter’s Name: Keyur Malaviya Survey of program slicing techniques

SDG style interpretation

Thin solid arrows represent flow dependences, Thick solid arrows correspond to control

dependences, Thin dashed arrows Used for call, parameter-in, and

parameter-out dependences, Thick dashed arrows Transitive inter-procedural

flow dependences. Shaded vertices Vertices in the slice w.r.t. statement

write(product) Light shading Vertices identified in the first phase Dark shading Vertices identified in the second phase

Page 43: Presenter’s Name: Keyur Malaviya Survey of program slicing techniques

The slice with criteria (10, product)

program Example;begin(1) read(n);(2) i := 1;(3) sum := 0;(4) product := 1;(5) while i <= n dobegin(6) Add(sum, i);(7) Multiply(product, i);(8) Add(i, 1)end;(9) write(sum);(10) write(product)end

procedure Add(a; b);begin11) a := a + bEnd

procedure Multiply(c; d);begin12) j := 1;13) k := 0;14) while j <= d dobegin15) Add(k, c);16) Add(j, 1);end;17) c := kend

Page 44: Presenter’s Name: Keyur Malaviya Survey of program slicing techniques

Application of slicing

Debugging and program analysis Program differencing and program

integration analyzing an old and a new version of a program partitioning the components compares slices in order to detect equivalent

behaviors Software maintenance

change at some place in a program behavior of other parts of the program

Page 45: Presenter’s Name: Keyur Malaviya Survey of program slicing techniques

QUESTIONS