a survey of techniques for precise program slicing

51
A survey of techniques for precise program slicing Komondoor V. Raghavan Indian Institute of Science, Bangalore

Upload: brina

Post on 25-Feb-2016

47 views

Category:

Documents


5 download

DESCRIPTION

A survey of techniques for precise program slicing. Komondoor V. Raghavan Indian Institute of Science, Bangalore. The problem of program slicing. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: A survey of techniques for precise program slicing

A survey of techniques for precise program slicing

Komondoor V. RaghavanIndian Institute of Science, Bangalore

Page 2: A survey of techniques for precise program slicing

Raghavan Komondoor, Precise slicing 2/33

The problem of program slicing

• Given a program P, and a statement c (the criterion), identify statements and conditionals in the program that are relevant to the variables that occur in c– A conditional is relevant if modifying the conditional could

disturb the values of the variables in c from what’s expected (on any input)

– A statement is relevant if modifying its rhs could disturb the values of the variables at c

• Intuitively, a slice is a projection of P that’s behaviorally equivalent to P wrt what’s observable at c

Page 3: A survey of techniques for precise program slicing

Raghavan Komondoor, Precise slicing 3

An example

Page 4: A survey of techniques for precise program slicing

Raghavan Komondoor, Precise slicing 4/33

Applications of slicing• Software understanding tools• Software maintenance tools– Clone detection– Merging back different variants of a program– Decomposition of monolithic programs into coherent

functionalities (e.g., sum-product example)– Recovering independent threads from sequential program

• Compilers and verification tools– Improves scalability, by identifying portion of program

that’s relevant to a property that needs to be checked

Page 5: A survey of techniques for precise program slicing

Raghavan Komondoor, Precise slicing 5

Control flow graph

Page 6: A survey of techniques for precise program slicing

Raghavan Komondoor, Precise slicing 6

Flow dependence relation

s1 s2 if• s1 defines a variable v• s2 uses v• there is a control-flow path from s1 to s2 along which no other statement defines v

Page 7: A survey of techniques for precise program slicing

Raghavan Komondoor, Precise slicing 7

Flow dependences

Page 8: A survey of techniques for precise program slicing

Raghavan Komondoor, Precise slicing 8/33

Control dependence relation

s1 s2 if• s1 is a conditional• s2 is definitely reachable along one branch out of s1• there is a path along the other branch along which s2

is not reached

Page 9: A survey of techniques for precise program slicing

Raghavan Komondoor, Precise slicing 9

Flow + control dependences

Page 10: A survey of techniques for precise program slicing

Raghavan Komondoor, Precise slicing 10/33

Basic slicing technique

1. From P, construct flow dependence relation F and control dependence relation C

2. Obtain reflexive-transitive closure R of (F ⋃ C)3. Slice = { s | <s, c> in R }, where c is given criterion

Page 11: A survey of techniques for precise program slicing

Raghavan Komondoor, Precise slicing 11

Illustration of slicing

Page 12: A survey of techniques for precise program slicing

Raghavan Komondoor, Precise slicing 12

Illustration of slicing

Page 13: A survey of techniques for precise program slicing

Raghavan Komondoor, Precise slicing 13

A more complex example

Page 14: A survey of techniques for precise program slicing

Raghavan Komondoor, Precise slicing 14

Basic technique yields imprecise slice

Page 15: A survey of techniques for precise program slicing

Raghavan Komondoor, Precise slicing 15/33

Need to rule out infeasible paths[Hong et al., ‘95] achieve this by code duplication

• Take a set of predicates Q (on program variables) as input

• Make up to 2|Q| copies of each statement, one for each combination of predicate evaluations

• Identify feasible paths in this “exploded” flow graph• Then, apply usual slicing technique on this exploded

graph

Page 16: A survey of techniques for precise program slicing

Raghavan Komondoor, Precise slicing 16

Exploded flow graph

Page 17: A survey of techniques for precise program slicing

Raghavan Komondoor, Precise slicing 17

Adding edges in exploded flow graph

• Edge (1) not present because in state ¬p1 x < y cannot True• Edge (2) not present for similar reason• Edge (3) not present because: Program in state p1 remains in same state after executing “x = x – 1”

Page 18: A survey of techniques for precise program slicing

Raghavan Komondoor, Precise slicing 18

Loops

Page 19: A survey of techniques for precise program slicing

Raghavan Komondoor, Precise slicing 19

Loops

Page 20: A survey of techniques for precise program slicing

Raghavan Komondoor, Precise slicing 20

Precision is closely linked to given partitioning

Page 21: A survey of techniques for precise program slicing

Raghavan Komondoor, Precise slicing 21

Precision is closely linked to given partitioning

Page 22: A survey of techniques for precise program slicing

Raghavan Komondoor, Precise slicing 22/33

Summary of Hong et al.• Obtains more precise slices than standard slicing, by

excluding certain infeasible paths• Handles loops cleanly• Precision is linked to given partitioning Q– Partitioning needs to be selected carefully, based on

statements in program– In general, a bigger Q gives better precision (at the

expense of slicing time)– Other work exists to infer suitable Q automatically from

program by iterative refinement• However, in the context of verification, not slicing

Page 23: A survey of techniques for precise program slicing

Raghavan Komondoor, Precise slicing 23/33

An approach based on symbolic execution [Jaffar et al., ‘12]

• Explodes control-flow graph by symbolically executing all possible paths in the program

• Does not require Q as input• Basic idea– During execution, at each point

• Have a symbolic store, which tracks current values of variables as expressions on program’s initial parameters

• Have path constraint, which is a predicate on the initial parameters that needs to hold for path p to be feasible

– If p is s1 sn, and sn sp and sn sq, split execution into two paths s1 sp and s1 sq.

Page 24: A survey of techniques for precise program slicing

Raghavan Komondoor, Precise slicing 24

Illustration of symbolic execution

Page 25: A survey of techniques for precise program slicing

Raghavan Komondoor, Precise slicing 25

Illustration of symbolic execution

Page 26: A survey of techniques for precise program slicing

Raghavan Komondoor, Precise slicing 26

Symbolic paths ⟶ exploded flow graph

Page 27: A survey of techniques for precise program slicing

Raghavan Komondoor, Precise slicing 27

Now, perform standard slicing

Page 28: A survey of techniques for precise program slicing

Raghavan Komondoor, Precise slicing 28

Now, perform standard slicing

Page 29: A survey of techniques for precise program slicing

Raghavan Komondoor, Precise slicing 29/33

So what do we have …• Fully automated. Does not need partitioning Q.• Precise even on examples like the complex one seen

earlier (involving x = x + w; y = y + w;) • However, problem with loops

Page 30: A survey of techniques for precise program slicing

Raghavan Komondoor, Precise slicing 30

The problem with loops

Page 31: A survey of techniques for precise program slicing

Raghavan Komondoor, Precise slicing 31

The problem with loops

Page 32: A survey of techniques for precise program slicing

Raghavan Komondoor, Precise slicing 32

The exploded flow graph

Page 33: A survey of techniques for precise program slicing

Raghavan Komondoor, Precise slicing 33

Slicing

Page 34: A survey of techniques for precise program slicing

Raghavan Komondoor, Precise slicing 34

Imprecise slicing

Page 35: A survey of techniques for precise program slicing

Raghavan Komondoor, Precise slicing 35/33

Our approach [Komondoor ‘13]• Objectives– Fully precise in loop-free fragments, without relying on

user-provided partitioning– Use user-provided partitioning only when “crossing” loop

iterations– Handle programs that access and manipulate linked data

structures

Page 36: A survey of techniques for precise program slicing

Raghavan Komondoor, Precise slicing 36/33

We use PIM• What is PIM? – A graph/term representation for C programs– An equational logic and rewrite system on terms

• Embodies the full concrete operational semantics of C

• Applications– Precise constrained slicing– Partial evaluation

Page 37: A survey of techniques for precise program slicing

Raghavan Komondoor, Precise slicing 37

Example PIM termx = 1;

y = x + 2;

if (x == 2)z = y;

Store cell

sequential composition

fragment addr

@

Page 38: A survey of techniques for precise program slicing

Raghavan Komondoor, Precise slicing 38

Our notation

x = 1

y = x + 2

x@

Page 39: A survey of techniques for precise program slicing

Raghavan Komondoor, Precise slicing 39

Slicing via term simplification in PIM

y = (x < 50) y + 2;

y = (x > 150) y + 1;

x = x – 1;

(x 100) y Criterion

@ @

y = (false) y + 2;

y = (x > 150) y + 1;

x = x - 1;

@ @

(x 100) y

Page 40: A survey of techniques for precise program slicing

Raghavan Komondoor, Precise slicing 40/33

Summary of PIM’s approach1. Convert the (program + criterion) into a store

lookup2. Rewrite/simplify the store lookup term3. Identify subterms in the program on which

simplified term is dependent4. These terms constitute the slice

Fully precise in loop-free fragments. No partitioning required as input.

Page 41: A survey of techniques for precise program slicing

Raghavan Komondoor, Precise slicing 41

Slicing a loop

while (x > n) { y = (x < 50)

y + 2; y = (x > 150)

y + 1; x = x – 1;}

(x = 100) y Criterion

PIM does not terminate while

computing precise slice

Page 42: A survey of techniques for precise program slicing

Raghavan Komondoor, Precise slicing 42

Abstract lattice for given example

T

(≤ 100) (≥ 100)

(= 100)

(≠ 100)

(Tracks only value of x)

Page 43: A survey of techniques for precise program slicing

Raghavan Komondoor, Precise slicing 43

Iteration 1

y = (x < 50) y + 2;

y = (x > 150) y + 1;

x = x – 1;

(x = 100) Criterion

⊨ @

y = (false) y + 2;

y = (false) ;

;

⊨ @

(= 100) y y (= 100) (1)

(2)

(≥ 100) y (≥ 100) x

(1)

(2)

abstract weakest pre-condition

Page 44: A survey of techniques for precise program slicing

Raghavan Komondoor, Precise slicing 44

Iteration 2

y = (x < 50) y + 2;

y = (x > 150) y + 1;

x = x – 1;

(≥ 100) y

@

(1)

(2)

(≥ 100) y (≥ 100) x

(1)

(2)

Page 45: A survey of techniques for precise program slicing

Raghavan Komondoor, Precise slicing 45

Iteration 3

y = (x < 50) y + 2;

y = (x > 150) y + 1;

x = x – 1;

(≥ 100) x

@

(2)

(≥ 100) x(2)

Page 46: A survey of techniques for precise program slicing

Raghavan Komondoor, Precise slicing 46

Final slice

while (x > n) { y = (x < 50)

y + 2; y = (x > 150)

y + 1; x = x – 1;}

Page 47: A survey of techniques for precise program slicing

Raghavan Komondoor, Precise slicing 47/33

Our approach, at each iteration• Use abstract predicates, of the form `s ⊨ l’, where s is

a fragment and l is an element of a user-provided abstract lattice L

• Convert concrete guards in criteria to abstract guards at the beginning of each iteration

• Rewrite term using extended PIM rewrite rules • Then, use dependences to obtain the slice

Page 48: A survey of techniques for precise program slicing

Raghavan Komondoor, Precise slicing 48/33

Ensuring termination• If given lattice is finite– Assuming no heap, finite number of addresses.– Therefore, there is a bound on total number of possible

abstract-guarded criteria.

• If lattice is finite-height– Whenever we generate a new criterion c ≡ l v, If we had previously generated a criterion l' v then modify c to (l ⊔ l') v.– This also bounds the total number of possible criteria.

Page 49: A survey of techniques for precise program slicing

Raghavan Komondoor, Precise slicing 49

Example

// x points to a singly-linked // listy = null;while (x.d != k) { t = y; y = x; x = x.next; y.next = t;}

c b ad e f

yx

c b ade f

yx

after iteration

x@

Page 50: A survey of techniques for precise program slicing

Raghavan Komondoor, Precise slicing 50

Another example

if (x % 2 == 1) z = z + 1;while (x < n) x = x + 2;if (x % 2 == 0) y = z + 2;

y@

Page 51: A survey of techniques for precise program slicing

Raghavan Komondoor, Precise slicing 51/33

Summary of our approach• Fully precise slicing in loop-free fragments• Slicing of loops: Precision linked to user-provided

lattice• We address loops that traverse heap structures• Support partial evaluation also• Technical contribution– Integrate abstract interpretation with term rewriting– May be useful in other applications where term rewriting

is used