Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static...

200
Stac Analysis and Dataflow Analysis

Transcript of Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static...

Page 1: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

Static Analysis and

Dataflow Analysis

Page 2: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

2

Static Analysis

Static analyses consider all possible behaviors of a program without running it.

Page 3: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

3

Static Analysis

Static analyses consider all possible behaviors of a program without running it.

● Look for a property of interest

Page 4: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

4

Static Analysis

Static analyses consider all possible behaviors of a program without running it.

● Look for a property of interest– Do I dereference NULL pointers?

Page 5: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

5

Static Analysis

Static analyses consider all possible behaviors of a program without running it.

● Look for a property of interest– Do I dereference NULL pointers?– Do I leak memory?

Page 6: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

6

Static Analysis

Static analyses consider all possible behaviors of a program without running it.

● Look for a property of interest– Do I dereference NULL pointers?– Do I leak memory?– Do I violate a protocol specification?

Page 7: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

7

Static Analysis

Static analyses consider all possible behaviors of a program without running it.

● Look for a property of interest– Do I dereference NULL pointers?– Do I leak memory?– Do I violate a protocol specification?– Is this file open?

Page 8: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

8

Static Analysis

Static analyses consider all possible behaviors of a program without running it.

● Look for a property of interest– Do I dereference NULL pointers?– Do I leak memory?– Do I violate a protocol specification?– Is this file open?– Does my program terminate?

Page 9: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

9

Static Analysis

Static analyses consider all possible behaviors of a program without running it.

● Look for a property of interest– Do I dereference NULL pointers?– Do I leak memory?– Do I violate a protocol specification?– Is this file open?– Does my program terminate?

But wait? Isn't that impossible?

Page 10: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

10

Static Analysis

HALT?

Brief Review ofUndecidability

“Does my program terminate?”

Page 11: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

11

Static Analysis

HALT?

P

Brief Review ofUndecidability

Page 12: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

12

Static Analysis

or

HALT?

P

Brief Review ofUndecidability

Page 13: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

13

Static Analysis

or

HALT?

P

Brief Review ofUndecidability

if HALT?( P , P ):while True: { }

elsereturn True

PP

P P

or

=H'

HALT?

Page 14: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

14

Static Analysis

or

HALT?

P

Brief Review ofUndecidability

H'

H'

?

if HALT?( P , P ):while True: { }

elsereturn True

PP

P P

or

=H'

HALT?

Page 15: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

15

Static Analysis

or

HALT?

P

Brief Review ofUndecidability

H'

H'

?

It's a classic paradox!

if HALT?( P , P ):while True: { }

elsereturn True

PP

P P

or

=H'

HALT?

Page 16: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

16

Static Analysis

Static analyses consider all possible behaviors of a program without running it.

● Look for a property of interest– Do I dereference NULL pointers?– Do I leak memory?– Do I violate a protocol specification?– Is this file open?– Does my program terminate?

But wait? Isn't that impossible?

Page 17: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

17

Static Analysis

Static analyses consider all possible behaviors of a program without running it.

● Look for a property of interest– Do I dereference NULL pointers?– Do I leak memory?– Do I violate a protocol specification?– Is this file open?– Does my program terminate?

But wait? Isn't that impossible?

● Only if answers must be perfect.

Page 18: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

18

Static Analysis

Static analyses consider all possible behaviors of a program without running it.

● Look for a property of interest– Do I dereference NULL pointers?– Do I leak memory?– Do I violate a protocol specification?– Is this file open?– Does my program terminate?

But wait? Isn't that impossible?

● Only if answers must be perfect.HALT?

Page 19: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

19

Static Analysis

Static analyses consider all possible behaviors of a program without running it.

● Look for a property of interest– Do I dereference NULL pointers?– Do I leak memory?– Do I violate a protocol specification?– Is this file open?– Does my program terminate?

But wait? Isn't that impossible?

● Only if answers must be perfect.HALT?

?

Page 20: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

20

Static Analysis

Overapproximate or underapproximate the problem, and try to solve this simpler version.

Page 21: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

21

Static Analysis

Overapproximate or underapproximate the problem, and try to solve this simpler version.

● Sound analyses– Overapproximate– Guaranteed to find violations of property– May raise false alarms

Page 22: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

22

Static Analysis

Overapproximate or underapproximate the problem, and try to solve this simpler version.

● Sound analyses– Overapproximate– Guaranteed to find violations of property– May raise false alarms

● Complete analyses– Underapproximate– Reported violations are real– May miss violations

Page 23: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

23

Static Analysis

Overapproximate or underapproximate the problem, and try to solve this simpler version.

● Sound analyses– Overapproximate– Guaranteed to find violations of property– May raise false alarms

● Complete analyses– Underapproximate– Reported violations are real– May miss violations

Striking the right balance is key to a useful analysis

Page 24: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

24

Approximation

Modeled program behaviors

Possible Program Behavior

Page 25: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

25

Approximation

Modeled program behaviors

Overapproximate

Possible Program Behavior

Consider some behaviors possible when they are not.

Page 26: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

26

Approximation

Modeled program behaviors

Overapproximate

Possible Program Behavior

Underapproximate

Ignore some behaviors that are possible.

Page 27: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

27

Approximation

● Dynamic Analysis

– Analyzed ⊆ Feasible– As # tests ↑, Analyzed → Feasible

Analyzed

Feasible

Page 28: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

28

Approximation

● Dynamic Analysis

– Analyzed ⊆ Feasible– As # tests ↑, Analyzed → Feasible

● Static Analysis

– Feasible ⊆ Analyzed

Page 29: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

29

Approximation

● Dynamic Analysis

– Analyzed ⊆ Feasible– As # tests ↑, Analyzed → Feasible

● Static Analysis

– Feasible ⊆ Analyzed

Analyzed

Feasible

Page 30: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

30

Approximation

● Dynamic Analysis

– Analyzed ⊆ Feasible– As # tests ↑, Analyzed → Feasible

● Static Analysis

– Feasible ⊆ Analyzed– As infeasible paths ↓, Analyzed → Feasible

AnalyzedFeasible

Page 31: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

31

Approximation

● Dynamic Analysis

– Analyzed ⊆ Feasible– As # tests ↑, Analyzed → Feasible

● Static Analysis

– Feasible ⊆ Analyzed– As infeasible paths ↓, Analyzed → Feasible

● The two areas complement each other

– Static analysis can help generate useful tests– Dynamic analysis can help identify infeasibility

Page 32: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

32

Abstract Interpretation

Q: Is a particular number ever negative?– Might be an offset into invalid memory!

Approximate the program's behavior

Page 33: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

33

Abstract Interpretation

Q: Is a particular number ever negative?– Might be an offset into invalid memory!

Approximate the program's behavior

● Concrete domain: integers● Abstract domain: {-,0,+} ⋃ {⊤,⊥}

Page 34: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

34

Abstract Interpretation

Q: Is a particular number ever negative?– Might be an offset into invalid memory!

Approximate the program's behavior

● Concrete domain: integers● Abstract domain: {-,0,+} ⋃ {⊤,⊥}

concrete(x) = 5 concrete(y) = -3 concrete(z) = 0

Combines sets of the concrete domain

↦ abstract(x) = + ↦ abstract(y) = - ↦ abstract(z) = 0

Page 35: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

35

Abstract Interpretation

● Transfer Functions show how to evaluate this approximated program:

Page 36: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

36

Abstract Interpretation

● Transfer Functions show how to evaluate this approximated program:– + + + → +– - + - → -– 0 + 0 → 0– 0 + - → -– …– + + - → ⊤(unknown / might vary)– … / 0 → ⊥(undefined)

Page 37: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

37

Abstract Interpretation

● Transfer Functions show how to evaluate this approximated program:– + + + → +– - + - → -– 0 + 0 → 0– 0 + - → -– …– + + - → ⊤(unknown / might vary)– … / 0 → ⊥(undefined)

This type of approximation is calledabstract interpretation.

Page 38: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

38

Abstract Interpretation

1)sum = 02)i = 1

3)if i < N

4)i = i + 15)sum = sum + i

6)print(sum)7)print(i)

Page 39: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

39

Abstract Interpretation

1)sum = 02)i = 1

3)if i < N

4)i = i + 15)sum = sum + i

6)print(sum)7)print(i)

2s 0↦i↦⊥

1s↦⊥i↦⊥

Page 40: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

40

Abstract Interpretation

1)sum = 02)i = 1

3)if i < N

4)i = i + 15)sum = sum + i

6)print(sum)7)print(i)

2s 0↦i↦⊥

3s 0↦i 1↦

3s 0↦i +↦

1s↦⊥i↦⊥

Page 41: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

41

Abstract Interpretation

1)sum = 02)i = 1

3)if i < N

4)i = i + 15)sum = sum + i

6)print(sum)7)print(i)

2s 0↦i↦⊥

3s 0↦i 1↦

3s 0↦i +↦

6s 0↦i +↦

7s 0↦i +↦

1s↦⊥i↦⊥

Page 42: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

42

Abstract Interpretation

1)sum = 02)i = 1

3)if i < N

4)i = i + 15)sum = sum + i

6)print(sum)7)print(i)

2s 0↦i↦⊥

3s 0↦i 1↦

3s 0↦i +↦

6s 0↦i +↦

7s 0↦i +↦

4s 0↦i +↦

5s 0↦i +↦

1s↦⊥i↦⊥

Page 43: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

43

Abstract Interpretation

1)sum = 02)i = 1

3)if i < N

4)i = i + 15)sum = sum + i

6)print(sum)7)print(i)

2s 0↦i↦⊥

3s 0↦i 1↦

3s 0↦i +↦

6s 0↦i +↦

7s 0↦i +↦

4s 0↦i +↦

5s 0↦i +↦

3s +↦i +↦

1s↦⊥i↦⊥

Page 44: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

44

Abstract Interpretation

1)sum = 02)i = 1

3)if i < N

4)i = i + 15)sum = sum + i

6)print(sum)7)print(i)

2s 0↦i↦⊥

3s 0↦i 1↦

3s 0↦i +↦

6s 0↦i +↦

7s 0↦i +↦

4s 0↦i +↦

5s 0↦i +↦

3s +↦i +↦

6s +↦i +↦

7s +↦i +↦

1s↦⊥i↦⊥

Page 45: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

45

Abstract Interpretation

1)sum = 02)i = 1

3)if i < N

4)i = i + 15)sum = sum + i

6)print(sum)7)print(i)

2s 0↦i↦⊥

3s 0↦i 1↦

3s 0↦i +↦

6s 0↦i +↦

7s 0↦i +↦

4s 0↦i +↦

5s 0↦i +↦

3s +↦i +↦

6s +↦i +↦

7s +↦i +↦

4s +↦i +↦

5s +↦i +↦

1s↦⊥i↦⊥

Page 46: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

46

Abstract Interpretation

1)sum = 02)i = 1

3)if i < N

4)i = i + 15)sum = sum + i

6)print(sum)7)print(i)

Does the process ever end?

2s 0↦i↦⊥

3s 0↦i 1↦

3s 0↦i +↦

6s 0↦i +↦

7s 0↦i +↦

4s 0↦i +↦

5s 0↦i +↦

3s +↦i +↦

6s +↦i +↦

7s +↦i +↦

4s +↦i +↦

5s +↦i +↦

1s↦⊥i↦⊥

Page 47: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

47

Abstract Interpretation

1)sum = 02)i = 1

3)if i < N

4)i = i + 15)sum = sum + i

6)print(sum)7)print(i)

2s 0↦i↦⊥

3s 0↦i 1↦

3s 0↦i +↦

6s 0↦i +↦

7s 0↦i +↦

4s 0↦i +↦

5s 0↦i +↦

3s +↦i +↦

6s +↦i +↦

7s +↦i +↦

4s +↦i +↦

5s +↦i +↦

1s↦⊥i↦⊥

Page 48: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

48

Abstract Interpretation

1)sum = 02)i = 1

3)if i < N

4)i = i + 15)sum = sum + i

6)print(sum)7)print(i)

Can the final sum ever be negative?

2s 0↦i↦⊥

3s 0↦i 1↦

3s 0↦i +↦

6s 0↦i +↦

7s 0↦i +↦

4s 0↦i +↦

5s 0↦i +↦

3s +↦i +↦

6s +↦i +↦

7s +↦i +↦

4s +↦i +↦

5s +↦i +↦

1s↦⊥i↦⊥

Page 49: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

49

Abstract Interpretation

● Guarantee termination by carefully choosing

– The abstract domain

– The transfer function

Page 50: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

50

Abstract Interpretation

● Guarantee termination by carefully choosing

– The abstract domain

– The transfer function

● For basic analyses, use a monotone frameworkLoosely: <CFG, Transfer Function, Lattice Abstraction>

Page 51: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

51

Abstract Interpretation

● Guarantee termination by carefully choosing

– The abstract domain

– The transfer function

● For basic analyses, use a monotone framework

– {-,0,+} ⋃ {⊤,⊥}

– They define a partial order

– Abstract state can onlymove up lattice at a statement

0- +

Page 52: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

52

Abstract Interpretation

● Guarantee termination by carefully choosing

– The abstract domain

– The transfer function

● For basic analyses, use a monotone framework

– {-,0,+} ⋃ {⊤,⊥}

– They define a partial order

– Abstract state can onlymove up lattice at a statement

Why does this specificexample terminate?

0- +

Page 53: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

53

Abstract Interpretation

● Guarantee termination by carefully choosing

– The abstract domain

– The transfer function

● For basic analyses, use a monotone framework

● But in theory a lattice need not be finite!(ranges/intervals, linear constraints, ...)

Page 54: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

54

Abstract Interpretation

● Guarantee termination by carefully choosing

– The abstract domain

– The transfer function

● For basic analyses, use a monotone framework

● But in theory a lattice need not be finite!

– Widening operators can still make it feasible

(e.g., heuristically raise to )⊤

Page 55: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

55

Abstract Interpretation

● What properties should a good abstraction have?

Page 56: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

56

Abstract Interpretation

● What properties should a good abstraction have?

{1, 5}

Concrete

{1, 4}

Page 57: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

57

Abstract Interpretation

● What properties should a good abstraction have?

{1, 5}

{1, 4, 5}

Concrete

{1, 4}

Page 58: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

58

Abstract Interpretation

● What properties should a good abstraction have?

{1, 5}

{1, 4, 5}

Concrete

{1, 4}

{1, 2, 3, …}

Page 59: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

59

Abstract Interpretation

● What properties should a good abstraction have?

{1, 5}

0- +{1, 4, 5}

Concrete Abstract

{1, 4}

{1, 2, 3, …}

Page 60: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

60

Abstract Interpretation

● What properties should a good abstraction have?

{1, 5}

0- +{1, 4, 5}

Concrete Abstract

{1, 4}

{1, 2, 3, …}

α

Page 61: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

61

Abstract Interpretation

● What properties should a good abstraction have?

{1, 5}

0- +{1, 4, 5}

Concrete Abstract

{1, 4}

{1, 2, 3, …}

α

γ

Page 62: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

62

Abstract Interpretation

● What properties should a good abstraction have?

{1, 5}

0- +{1, 4, 5}

Concrete Abstract

{1, 4}

{1, 2, 3, …}

α

γ

s ⊆ γ(α(s))

Page 63: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

63

Abstract Interpretation

● What properties should a good abstraction have?

{1, 5}

0- +{1, 4, 5}

Concrete Abstract

{1, 4}

{1, 2, 3, …}

α

γ

s ⊆ γ(α(s))

No concrete values werediscarded by abstraction

Page 64: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

64

Dataflow Analysis

● Dataflow analysis performs model checking of abstract interpretations

Page 65: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

65

Dataflow Analysis

● Dataflow analysis performs model checking of abstract interpretations

● Meet Operator (⨅) combines results across program paths

Page 66: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

66

Dataflow Analysis

● Dataflow analysis performs model checking of abstract interpretations

● Meet Operator (⨅) combines results across program paths

A B

C

xA

+↦ xB

-↦

xC↦?

xA

x⨅B = ?

Page 67: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

67

Dataflow Analysis

● Dataflow analysis performs model checking of abstract interpretations

● Meet Operator (⨅) combines results across program paths

A B

C

xA

+↦ xB

-↦

xC↦?

xA

x⨅B = α(x

A)⨆α(x

B) = ?

Page 68: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

68

Dataflow Analysis

● Dataflow analysis performs model checking of abstract interpretations

● Meet Operator (⨅) combines results across program paths

A B

C

xA

+↦ xB

-↦⊤

0- +

xC↦?

xA

x⨅B = α(x

A)⨆α(x

B) = ?

Page 69: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

69

Dataflow Analysis

● Dataflow analysis performs model checking of abstract interpretations

● Meet Operator (⨅) combines results across program paths

A B

C

xA

+↦ xB

-↦⊤

0- +

xC↦?

xA

x⨅B = α(x

A)⨆α(x

B) = ?

Page 70: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

70

Dataflow Analysis

● Dataflow analysis performs model checking of abstract interpretations

● Meet Operator (⨅) combines results across program paths

A B

C

xA

+↦ xB

-↦⊤

0- +

xC↦⊤

xA

x⨅B = α(x

A)⨆α(x

B) = ⊤

Page 71: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

71

Dataflow Analysis

● Now model the abstract program state and propagate through the CFG.

1)sum = 02)i = 1

3)if i < N

4)i = i + 15)sum = sum + i

6)print(sum)7)print(i)

sum → ⊥i → ⊥

Page 72: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

72

Dataflow Analysis

● Now model the abstract program state and propagate through the CFG.

1)sum = 02)i = 1

3)if i < N

4)i = i + 15)sum = sum + i

6)print(sum)7)print(i)

sum → ⊥i → ⊥

sum → 0i → +

Page 73: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

73

Dataflow Analysis

● Now model the abstract program state and propagate through the CFG.

1)sum = 02)i = 1

3)if i < N

4)i = i + 15)sum = sum + i

6)print(sum)7)print(i)

sum → ⊥i → ⊥

sum → 0i → +

sum → 0i → +

Page 74: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

74

Dataflow Analysis

● Now model the abstract program state and propagate through the CFG.

1)sum = 02)i = 1

3)if i < N

4)i = i + 15)sum = sum + i

6)print(sum)7)print(i)

sum → ⊥i → ⊥

sum → 0i → +

sum → 0i → +

sum → 0i → +

Page 75: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

75

Dataflow Analysis

● Now model the abstract program state and propagate through the CFG.

1)sum = 02)i = 1

3)if i < N

4)i = i + 15)sum = sum + i

6)print(sum)7)print(i)

sum → ⊥i → ⊥

sum → 0i → +

sum → 0i → +

sum → 0i → +

sum → 0i → +

Page 76: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

76

Dataflow Analysis

● Now model the abstract program state and propagate through the CFG.

1)sum = 02)i = 1

3)if i < N

4)i = i + 15)sum = sum + i

6)print(sum)7)print(i)

sum → ⊥i → ⊥

sum → 0i → +

sum → 0i → +

sum → 0i → +

sum → 0i → +

sum → 0i → +

Page 77: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

77

Dataflow Analysis

● Now model the abstract program state and propagate through the CFG.

1)sum = 02)i = 1

3)if i < N

4)i = i + 15)sum = sum + i

6)print(sum)7)print(i)

sum → ⊥i → ⊥

sum → 0i → +

sum → 0i → +

sum → 0i → +

sum → 0i → +

sum → 0i → +

sum → 0i → +

Page 78: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

78

Dataflow Analysis

● Now model the abstract program state and propagate through the CFG.

1)sum = 02)i = 1

3)if i < N

4)i = i + 15)sum = sum + i

6)print(sum)7)print(i)

sum → ⊥i → ⊥

sum → 0i → +

sum → 0i → +

sum → 0i → +

sum → 0i → +

sum → 0i → +

sum → 0i → +

sum → +i → +

Page 79: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

79

Dataflow Analysis

● Now model the abstract program state and propagate through the CFG.

1)sum = 02)i = 1

3)if i < N

4)i = i + 15)sum = sum + i

6)print(sum)7)print(i)

sum → ⊥i → ⊥

sum → i → +

sum → 0i → +

sum → 0i → +

sum → 0i → +

sum → 0i → +

sum → 0i → +

sum → +i → +

Meet Operatorsum was 0, butwhat should it be now?

?

Page 80: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

80

Dataflow Analysis

● Now model the abstract program state and propagate through the CFG.

1)sum = 02)i = 1

3)if i < N

4)i = i + 15)sum = sum + i

6)print(sum)7)print(i)

sum → 0i → +

sum → 0i → +

sum → 0i → +

sum → +i → +

sum → ⊥i → ⊥

sum → i → +

sum → 0i → +

sum → 0i → +

?The value across all executions is not -, 0, or +,so it is simply unknown/anything. (⊤)

Page 81: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

81

Dataflow Analysis

● Now model the abstract program state and propagate through the CFG.

1)sum = 02)i = 1

3)if i < N

4)i = i + 15)sum = sum + i

6)print(sum)7)print(i)

sum → 0i → +

sum → 0i → +

sum → 0i → +

sum → +i → +

sum → ⊥i → ⊥

sum → ⊤ i → +

sum → 0i → +

sum → 0i → +

Page 82: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

82

Dataflow Analysis

● Now model the abstract program state and propagate through the CFG.

1)sum = 02)i = 1

3)if i < N

4)i = i + 15)sum = sum + i

6)print(sum)7)print(i)

sum → 0i → +

sum → ⊤i → +

sum → 0i → +

sum → +i → +

sum → ⊥i → ⊥

sum → ⊤ i → +

sum → 0i → +

sum → 0i → +

Page 83: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

83

Dataflow Analysis

● Now model the abstract program state and propagate through the CFG.

1)sum = 02)i = 1

3)if i < N

4)i = i + 15)sum = sum + i

6)print(sum)7)print(i)

sum → ⊥i → ⊥

sum → ⊤ i → +

sum → ⊤i → +

sum → 0i → +

sum → 0i → +

sum → ⊤i → +

sum → 0i → +

sum → +i → +

Page 84: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

84

Dataflow Analysis

● Now model the abstract program state and propagate through the CFG.

1)sum = 02)i = 1

3)if i < N

4)i = i + 15)sum = sum + i

6)print(sum)7)print(i)

sum → ⊥i → ⊥

sum → ⊤ i → +

sum → ⊤i → +

sum → 0i → +

sum → 0i → +

sum → ⊤i → +

sum → ⊤i → +

sum → +i → +

Page 85: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

85

Dataflow Analysis

● Now model the abstract program state and propagate through the CFG.

– Continue until we reach a fixed point

(No more changes)

Page 86: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

86

Dataflow Analysis

● Now model the abstract program state and propagate through the CFG.

– Continue until we reach a fixed point

(No more changes)

– Proper ordering can improve the efficiency.

(Topological Order, Strongly Connected Components)

Page 87: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

87

Dataflow Analysis

● Now model the abstract program state and propagate through the CFG.

– Continue until we reach a fixed point

(No more changes)

– Proper ordering can improve the efficiency.● (Topological Order, Strongly Connected Components)

Will it always terminate?

Page 88: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

88

Dataflow Analysis

● Note: need to model program state before and after each statement

● Proper ordering & a work list algorithm improves the efficiency

Page 89: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

89

Worklist Algorithms

work = nodes()state(n) = ⊥ ∀ n ∈ nodes()while work ≠ ∅: unit = take(work) old = state(unit) before = ∏state(p) ∀ p preds(unit)∈ new = transfer(before, unit) if old ≠ after: work = work ∪ succs(unit) state(unit) = new

1

2

3

4

Page 90: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

90

Worklist Algorithms

1

2

3

4

work: 1 2 3 4

state:

{ 1( ↦⊥)2( ↦⊥)

3( ↦⊥)4( ↦⊥) }

work = nodes()state(n) = ⊥ ∀ n ∈ nodes()while work ≠ ∅: unit = take(work) old = state(unit) before = ∏state(p) ∀ p preds(unit)∈ new = transfer(before, unit) if old ≠ after: work = work ∪ succs(unit) state(unit) = new

Page 91: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

91

Worklist Algorithms

1

2

3

4

work:

1

2 3 4

state:

{ 2( ↦⊥)

3( ↦⊥)4( ↦⊥)

unit =

}1( ↦⊥)

work = nodes()state(n) = ⊥ ∀ n ∈ nodes()while work ≠ ∅: unit = take(work) old = state(unit) before = ∏state(p) ∀ p preds(unit)∈ new = transfer(before, unit) if old ≠ after: work = work ∪ succs(unit) state(unit) = new

Page 92: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

92

Worklist Algorithms

1

2

3

4

work:

1

2 3 4

state:

{ 2( ↦⊥)

3( ↦⊥)4( ↦⊥)

unit =old = ⊥

}1( ↦⊥)

work = nodes()state(n) = ⊥ ∀ n ∈ nodes()while work ≠ ∅: unit = take(work) old = state(unit) before = ∏state(p) ∀ p preds(unit)∈ new = transfer(before, unit) if old ≠ after: work = work ∪ succs(unit) state(unit) = new

Page 93: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

93

Worklist Algorithms

1

2

3

4

work:

1

2 3 4

state:

{ 2( ↦⊥)

3( ↦⊥)4( ↦⊥)

unit =old = ⊥new =

sum → 0i → +

}1( ↦⊥)

work = nodes()state(n) = ⊥ ∀ n ∈ nodes()while work ≠ ∅: unit = take(work) old = state(unit) before = ∏state(p) ∀ p preds(unit)∈ new = transfer(before, unit) if old ≠ after: work = work ∪ succs(unit) state(unit) = new

Page 94: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

94

Worklist Algorithms

1

2

3

4

work:

1

2 3 4

state:

{ 1( ↦ )2( ↦⊥)

3( ↦⊥)4( ↦⊥)

unit =old = ⊥new =

sum → 0i → +

sum → 0i → + }

work = nodes()state(n) = ⊥ ∀ n ∈ nodes()while work ≠ ∅: unit = take(work) old = state(unit) before = ∏state(p) ∀ p preds(unit)∈ new = transfer(before, unit) if old ≠ after: work = work ∪ succs(unit) state(unit) = new

Page 95: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

95

Worklist Algorithms

1

2

3

4

work:

2

3 4

state:

{ 1( ↦ )2( ↦⊥ )

3( ↦⊥)4( ↦⊥)

unit =old = ⊥new =

sum → 0i → +

sum → 0i → +

sum → 0i → +

}

work = nodes()state(n) = ⊥ ∀ n ∈ nodes()while work ≠ ∅: unit = take(work) old = state(unit) before = ∏state(p) ∀ p preds(unit)∈ new = transfer(before, unit) if old ≠ after: work = work ∪ succs(unit) state(unit) = new

Page 96: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

96

Worklist Algorithms

1

2

3

4

work:

3

4

state:

{ 1( ↦ )2( ↦⊥ )

3( ↦⊥ )

unit =old = ⊥new =

sum → +i → +

sum → 0i → +

sum → 0i → +

2sum → +i → +

2 was added back to the list

4( ↦⊥) }

work = nodes()state(n) = ⊥ ∀ n ∈ nodes()while work ≠ ∅: unit = take(work) old = state(unit) before = ∏state(p) ∀ p preds(unit)∈ new = transfer(before, unit) if old ≠ after: work = work ∪ succs(unit) state(unit) = new

Page 97: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

97

Worklist Algorithms

1

2

3

4

work:

state:

{ 1( ↦ )2( ↦⊥ )

3( ↦⊥ )4( ↦⊥ )}

unit =old = ⊥new =

sum → 0i → +

sum → 0i → +

sum → 0i → +

2sum → +i → +

4

sum → 0i → +

work = nodes()state(n) = ⊥ ∀ n ∈ nodes()while work ≠ ∅: unit = take(work) old = state(unit) before = ∏state(p) ∀ p preds(unit)∈ new = transfer(before, unit) if old ≠ after: work = work ∪ succs(unit) state(unit) = new

Page 98: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

98

work = nodes()state(n) = ⊥ ∀ n ∈ nodes()while work ≠ ∅: unit = take(work) old = state(unit) before = ∏state(p) ∀ p preds(unit)∈ new = transfer(before, unit) if old ≠ after: work = work ∪ succs(unit) state(unit) = new

Worklist Algorithms

1

2

3

4

work:

state:

{ 1( ↦ )2( ↦⊥ )

3( ↦⊥ )4( ↦⊥ )}

unit =old = ⊥new =

sum → 0i → +

sum → 0i → +

sum → ⊤i → +

4sum → +i → +

sum → 0i → +

2

4,3 were added back to the list

sum →⊤i → +

3

Page 99: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

99

Worklist Algorithms

1

2

3

4

work:

state:

{ 1( ↦ )2( ↦⊥ )

3( ↦⊥ )4( ↦⊥ )}

unit =old = ⊥new =

sum → 0i → +

sum → 0i → +

sum → ⊤i → +

sum → +i → +

sum → ⊤i → +

4

sum →⊤i → +

3

work = nodes()state(n) = ⊥ ∀ n ∈ nodes()while work ≠ ∅: unit = take(work) old = state(unit) before = ∏state(p) ∀ p preds(unit)∈ new = transfer(before, unit) if old ≠ after: work = work ∪ succs(unit) state(unit) = new

Page 100: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

100

Worklist Algorithms

1

2

3

4

work:

state:

{ 1( ↦ )2( ↦⊥ )

3( ↦⊥ )4( ↦⊥ )}

unit =old = ⊥new =

sum → +i → +

sum → 0i → +

sum → ⊤i → +

sum → +i → +

sum → ⊤i → +

3

sum → +i → +

No change

work = nodes()state(n) = ⊥ ∀ n ∈ nodes()while work ≠ ∅: unit = take(work) old = state(unit) before = ∏state(p) ∀ p preds(unit)∈ new = transfer(before, unit) if old ≠ after: work = work ∪ succs(unit) state(unit) = new

Page 101: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

101

work = nodes()state(n) = ⊥ ∀ n ∈ nodes()while work ≠ ∅: unit = take(work) old = state(unit) before = ∏state(p) ∀ p preds(unit)∈ new = transfer(before, unit) if old ≠ after: work = work ∪ succs(unit) state(unit) = new

Worklist Algorithms

1

2

3

4

work:

state:

{ 1( ↦ )2( ↦⊥ )

3( ↦⊥ )4( ↦⊥ )}

unit =old = ⊥new =

sum → 0i → +

sum → 0i → +

sum → ⊤i → +

sum → +i → +

sum → ⊤i → +

4

sum →⊤i → +

Done!

Page 102: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

102

Effect of Approximation

● There are several possible sources of imprecision

Page 103: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

103

Effect of Approximation

● There are several possible sources of imprecision

...

1)x = 22)y = 1

3)x = 24)y = 1

5)c = x * y

Page 104: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

104

Effect of Approximation

● There are several possible sources of imprecision

...

1)x = 22)y = 1

3)x = -24)y = -1

5)c = x * y

x→ +, y→ + x→ -, y→ -

c→ ?

Page 105: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

105

Effect of Approximation

● There are several possible sources of imprecision

● 2 Key sources are

– Control flow● Many different paths are summarized together

Page 106: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

106

Effect of Approximation

● There are several possible sources of imprecision

● 2 Key sources are

– Control flow● Many different paths are summarized together

– Abstraction● Deliberately throwing away information● Granularity of program state affects correlations across

variables

Page 107: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

107

Effect of Approximation

● We compute results with maximal fixed points (MFP) in the lattice

Page 108: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

108

Effect of Approximation

● We compute results with maximal fixed points (MFP) in the lattice

● Ideal solution is a Meet Over all Paths (MOP)

Page 109: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

109

Effect of Approximation

● We compute results with maximal fixed points (MFP) in the lattice

● Ideal solution is a Meet Over all Paths (MOP)

For one path p: fp(⊥) = fn(fn-1(...f1(f0(⊥))))

Page 110: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

110

Effect of Approximation

● We compute results with maximal fixed points (MFP) in the lattice

● Ideal solution is a Meet Over all Paths (MOP)

For one path p: fp(⊥) = fn(fn-1(...f1(f0(⊥))))For all paths p: ⨅pfp(⊥)

Page 111: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

111

Effect of Approximation

● We compute results with maximal fixed points (MFP) in the lattice

● Ideal solution is a Meet Over all Paths (MOP)

● Are they different?

Page 112: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

112

Effect of Approximation

● We compute results with maximal fixed points (MFP) in the lattice

● Ideal solution is a Meet Over all Paths (MOP)

● Are they different?

– Sometimes. But sometime solutions are perfect.

Page 113: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

113

Effect of Approximation

● We compute results with maximal fixed points (MFP) in the lattice

● Ideal solution is a Meet Over all Paths (MOP)

● Are they different?

– Sometimes. But sometime solutions are perfect.

– When f() is distributive, MFP=MOP

f(x ⨅ y ⨅ z) = f(x) ⨅ f(y) ⨅ f(z)

Page 114: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

114

Effect of Approximation

● We compute results with maximal fixed points (MFP) in the lattice

● Ideal solution is a Meet Over all Paths (MOP)

● Are they different?

– Sometimes. But sometime solutions are perfect.

– When f() is distributive, MFP=MOP

f(x ⨅ y ⨅ z) = f(x) ⨅ f(y) ⨅ f(z)

– This applies to an important class of problems called bitvector frameworks.

Page 115: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

115

Bitvector Frameworks

● When the property concerns subsets of a finite set, the abstract domain & lattice are easy:

– Concrete: D = {a, b, c, d, … }

– Abstract: ℘(D) = { {}, {a}, {b}, …, {a, b}, {a, c}, …}

– Lattice: Defined by subset relation:

D

{}

{a} {b} {c} ...

{a,b} {a,c} {b,c}... {b,d} ...

...

Page 116: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

116

Bitvector Frameworks

● When the property concerns subsets of a finite set, the abstract domain & lattice are easy:

– Concrete: D = {a, b, c, d, … }

– Abstract: ℘(D) = { {}, {a}, {b}, …, {a, b}, {a, c}, …}

– Lattice: Defined by subset relation:

D

{}

{a} {b} {c} ...

{a,b} {a,c} {b,c}... {b,d} ...

...

What would themeet operator be?

Page 117: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

117

Bitvector Frameworks

● Why is this convenient?

– Hint: bitvector frameworks

Page 118: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

118

Bitvector Frameworks

● Why is this convenient?

– Hint: bitvector frameworks

– X={a,b}, Y={c,d} → X⨆Y = {a,b}{c,d} = {a,b,c,d}

– We can implement the abstract state using efficient bitvectors!

Page 119: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

119

Effect of Approximation

● If approximation yields imprecise results, why do we do it?

Page 120: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

120

Recap: Dataflow Analysis

Analyze complex behavior with approximation:● Abstract domain: e.g. {-,0,+} ⋃ {⊤,⊥}● Transfer functions: - + + → ⊤● Bounded domain lattice height:● Concern for false + & -

0- +

Page 121: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

121

Recap: Dataflow Analysis

Analyze complex behavior with approximation:● Abstract domain: e.g. {-,0,+} ⋃ {⊤,⊥}● Transfer functions: - + + → ⊤● Bounded domain lattice height:● Concern for false + & -

Implementation:● Computing using work lists● Speeding up by sorting CFG nodes

0- +

Page 122: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

122

Recap: Dataflow Analysis

Analyze complex behavior with approximation:● Abstract domain: e.g. {-,0,+} ⋃ {⊤,⊥}● Transfer functions: - + + → ⊤● Bounded domain lattice height:● Concern for false + & -

Implementation:● Computing using work lists● Speeding up by sorting CFG nodes

0- +

Let's see an example

Page 123: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

123

File Policy Analysis

Goal: Identify potential misuses of open/closed files

Page 124: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

124

File Policy Analysis

Goal: Identify potential misuses of open/closed files● Files may be open or closed

Page 125: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

125

File Policy Analysis

Goal: Identify potential misuses of open/closed files● Files may be open or closed● Many operations may only occur on open files

e.g. read, write, print, flush, close, …

Page 126: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

126

File Policy Analysis

Goal: Identify potential misuses of open/closed files● Files may be open or closed● Many operations may only occur on open files

e.g. read, write, print, flush, close, …

What should our design actually be?● Abstract domain?● Transfer functions?● Lattice?

Page 127: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

127

File Policy Analysis

Goal: Identify potential misuses of open/closed files● Files may be open or closed● Many operations may only occur on open files

e.g. read, write, print, flush, close, …

What should our design actually be?● Abstract domain?● Transfer functions?● Lattice?

[DEMO]

Page 128: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

128

Flow Insensitive Analysis

● Saw flow sensitive analysis

– Modeling state at each statement is expensive

– Scales to functions and small components

– Usually not beyond 1000s of lines without care

Page 129: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

129

Flow Insensitive Analysis

● Saw flow sensitive analysis

– Modeling state at each statement is expensive

– Scales to functions and small components

– Usually not beyond 1000s of lines without care

● Flow insensitive analyses aggregate into a global state

– Better scalability

– Less precision

– “Does this function modify global variable X?”

Page 130: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

130

Context Sensitive Analyses

● Program behavior may be dependent on the call stack / calling context.

– “If bar() is called by foo(), then it is exception free.”

– Can enable more precise interprocedural analyses

Page 131: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

131

Context Sensitive Analyses

● Program behavior may be dependent on the call stack / calling context.

– “If bar() is called by foo(), then it is exception free.”

– Can enable more precise interprocedural analyses

Can you imagine how to solve this?What problems might arise?

Page 132: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

132

Context Sensitivity

● Recall that we can extract a call graph

– Just as you are doing in your first project!

a()

b() c()

def a(): b() … b()

def b(): … c()

def c(): …

The behavior of c() could beaffected by each “...”

Modeling them can makeanalysis more precise.

Page 133: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

133

Context Sensitivity

● Simplest Approach

– Add edges between call sites & targets– Perform data flow on this larger graph

Example from Stephen Chong

def main(): x = 7 r = p(x) x = r z = p(x+10)

def p(a): if a < 9: y = 0 else: y = 1

Page 134: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

134

Context Sensitivity

● Simplest Approach

– Add edges between call sites & targets– Perform data flow on this larger graph

Example from Stephen Chong

def main(): x = 7 r = p(x) x = r z = p(x+10)

def p(a): if a < 9: y = 0 else: y = 1

x = 7call p(x)

if a < 9

r = return p(x)x = rcall p(x+10)

z = return p(x+10)

main()

p(a)

y = 1y = 0

return a

Page 135: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

135

Context Sensitivity

● Simplest Approach

– Add edges between call sites & targets– Perform data flow on this larger graph

Example from Stephen Chong

def main(): x = 7 r = p(x) x = r z = p(x+10)

def p(a): if a < 9: y = 0 else: y = 1

x = 7call p(x)

if a < 9

r = return p(x)x = rcall p(x+10)

z = return p(x+10)

main()

p(a)

y = 1y = 0

return a

Page 136: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

136

Context Sensitivity

● Simplest Approach

– Add edges between call sites & targets– Perform data flow on this larger graph

Example from Stephen Chong

def main(): x = 7 r = p(x) x = r z = p(x+10)

def p(a): if a < 9: y = 0 else: y = 1

x = 7call p(x)

if a < 9

r = return p(x)x = rcall p(x+10)

z = return p(x+10)

main()

p(a)

y = 1y = 0

return a

Page 137: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

137

Context Sensitivity

● Simplest Approach

– Add edges between call sites & targets– Perform data flow on this larger graph

Example from Stephen Chong

def main(): x = 7 r = p(x) x = r z = p(x+10)

def p(a): if a < 9: y = 0 else: y = 1

x = 7call p(x)

if a < 9

r = return p(x)x = rcall p(x+10)

z = return p(x+10)

main()

p(a)

y = 1y = 0

return a

a=7

Page 138: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

138

Context Sensitivity

● Simplest Approach

– Add edges between call sites & targets– Perform data flow on this larger graph

Example from Stephen Chong

def main(): x = 7 r = p(x) x = r z = p(x+10)

def p(a): if a < 9: y = 0 else: y = 1

x = 7call p(x)

if a < 9

r = return p(x)x = rcall p(x+10)

z = return p(x+10)

main()

p(a)

y = 1y = 0

return a

a=7 a=7

Page 139: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

139

Context Sensitivity

● Simplest Approach

– Add edges between call sites & targets– Perform data flow on this larger graph

Example from Stephen Chong

def main(): x = 7 r = p(x) x = r z = p(x+10)

def p(a): if a < 9: y = 0 else: y = 1

x = 7call p(x)

if a < 9

r = return p(x)x = rcall p(x+10)

z = return p(x+10)

main()

p(a)

y = 1y = 0

return a

a=7 a=7

a=7

Page 140: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

140

Context Sensitivity

● Simplest Approach

– Add edges between call sites & targets– Perform data flow on this larger graph

Example from Stephen Chong

def main(): x = 7 r = p(x) x = r z = p(x+10)

def p(a): if a < 9: y = 0 else: y = 1

x = 7call p(x)

if a < 9

r = return p(x)x = rcall p(x+10)

z = return p(x+10)

main()

p(a)

y = 1y = 0

return a

a=7 a=7

a=7

r=7

Page 141: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

141

Context Sensitivity

● Simplest Approach

– Add edges between call sites & targets– Perform data flow on this larger graph

Example from Stephen Chong

def main(): x = 7 r = p(x) x = r z = p(x+10)

def p(a): if a < 9: y = 0 else: y = 1

x = 7call p(x)

if a < 9

r = return p(x)x = rcall p(x+10)

z = return p(x+10)

main()

p(a)

y = 1y = 0

return a

a=7 a=7

a=7

r=7a=17

Page 142: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

142

Context Sensitivity

● Simplest Approach

– Add edges between call sites & targets– Perform data flow on this larger graph

Example from Stephen Chong

def main(): x = 7 r = p(x) x = r z = p(x+10)

def p(a): if a < 9: y = 0 else: y = 1

x = 7call p(x)

if a < 9

r = return p(x)x = rcall p(x+10)

z = return p(x+10)

main()

p(a)

y = 1y = 0

return a

a=7 a=⊤

a=7

r=7a=17

Page 143: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

143

Context Sensitivity

● Simplest Approach

– Add edges between call sites & targets– Perform data flow on this larger graph

Example from Stephen Chong

def main(): x = 7 r = p(x) x = r z = p(x+10)

def p(a): if a < 9: y = 0 else: y = 1

x = 7call p(x)

if a < 9

r = return p(x)x = rcall p(x+10)

z = return p(x+10)

main()

p(a)

y = 1y = 0

return a

a=7 a=⊤

a=⊤

r=7a=17

Page 144: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

144

Context Sensitivity

● Simplest Approach

– Add edges between call sites & targets– Perform data flow on this larger graph

Example from Stephen Chong

def main(): x = 7 r = p(x) x = r z = p(x+10)

def p(a): if a < 9: y = 0 else: y = 1

x = 7call p(x)

if a < 9

r = return p(x)x = rcall p(x+10)

z = return p(x+10)

main()

p(a)

y = 1y = 0

return a

a=7 a=⊤

a=⊤

r=⊤a=⊤

z=⊤

Page 145: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

145

Context Sensitivity

● Information from one call site can flow to a mismatched return site!

Page 146: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

146

Context Sensitivity

● Information from one call site can flow to a mismatched return site!

● How could we address it?

Page 147: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

147

Context Sensitivity

● Solution 2: Inlining

– Make a copy of the function at each call site

Page 148: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

148

Context Sensitivity

● Solution 2: Inlining

– Make a copy of the function at each call site

● What problems arise?

Page 149: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

149

Context Sensitivity

● Solution 2: Inlining

– Make a copy of the function at each call site

● What problems arise?

● What other strategies can we use?

Page 150: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

150

Context Sensitivity

● Solution 3: Make a Copy

– Make one copy of each function per call site

Page 151: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

151

Context Sensitivity

● Solution 3: Make a Copy

– Make one copy of each function per call site

1) def main():2) a()3) a()

4) def a():5) b()

6) def b():7) pass

Page 152: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

152

Context Sensitivity

● Solution 3: Make a Copy

– Make one copy of each function per call site

1) def main():2) a()3) a()

4) def a():5) b()

6) def b():7) pass

call a()

return a()call a()

return a()

main()call b()

return b()

a()##2

call b()

return b()

a()##3

pass

b()##5

Page 153: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

153

Context Sensitivity

● Solution 3: Make a Copy

– Make one copy of each function per call site

1) def main():2) a()3) a()

4) def a():5) b()

6) def b():7) pass

call a()

return a()call a()

return a()

main()call b()

return b()

a()##2

call b()

return b()

a()##3

pass

b()##5

So far,so good

Page 154: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

154

Context Sensitivity

● Solution 3: Make a Copy

– Make one copy of each function per call site

1) def main():2) a()3) a()

4) def a():5) b()

6) def b():7) pass

call a()

return a()call a()

return a()

main()call b()

return b()

a()##2

call b()

return b()

a()##3

pass

b()##5

better, butnot perfect

Page 155: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

155

Context Sensitivity

● Solution 3: Make a Copy

– Make one copy of each function per call site

1) def main():2) a()3) a()

4) def a():5) b()

6) def b():7) pass

call a()

return a()call a()

return a()

main()call b()

return b()

a()##2

call b()

return b()

a()##3

pass

b()##5

How can we improve it?

Page 156: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

156

Context Sensitivity

Generalized:

● Make a bounded number of copies

Page 157: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

157

Context Sensitivity

Generalized:

● Make a bounded number of copies

● Choose a key/feature that determines which copy to use

– Bounded calling context/call stack (call site sensitivity)

– Allocation sites of objects (object sensitivity)

Page 158: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

158

Context Sensitivity

● Solution 4: Make a logical copy

Page 159: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

159

Context Sensitivity

● Solution 4: Make a logical copy

– Instead of actually making a copy, just keep track of the context information (the key) during analysis

Page 160: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

160

Context Sensitivity

● Solution 4: Make a logical copy

– Instead of actually making a copy, just keep track of the context information (the key) during analysis

– Compute results (called procedure summaries) for each logical copy of a function.

Page 161: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

161

Context Sensitivity

● Solution 4: Make a logical copy

– Instead of actually making a copy, just keep track of the context information (the key) during analysis

– Compute results (called procedure summaries) for each logical copy of a function.

– Modify the treatment of calls slightly:

On foo(in) with context C:

Page 162: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

162

Context Sensitivity

● Solution 4: Make a logical copy

– Instead of actually making a copy, just keep track of the context information (the key) during analysis

– Compute results (called procedure summaries) for each logical copy of a function.

– Modify the treatment of calls slightly:

On foo(in) with context C:If (foo,C) doesn't have a summary, process foo(in) in C and save the result to S.

Page 163: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

163

Context Sensitivity

● Solution 4: Make a logical copy

– Instead of actually making a copy, just keep track of the context information (the key) during analysis

– Compute results (called procedure summaries) for each logical copy of a function.

– Modify the treatment of calls slightly:

On foo(in) with context C:If (foo,C) doesn't have a summary, process foo(in) in C and save the result to S.

If the summary S already approximates foo(in), use S

Page 164: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

164

Context Sensitivity

● Solution 4: Make a logical copy

– Instead of actually making a copy, just keep track of the context information (the key) during analysis

– Compute results (called procedure summaries) for each logical copy of a function.

– Modify the treatment of calls slightly:

On foo(in) with context C:If (foo,C) doesn't have a summary, process foo(in) in C and save the result to S.

If the summary S already approximates foo(in), use S

Otherwise, process foo(in) in C and update S with (in⨅S.in).

Page 165: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

165

Context Sensitivity

● Solution 4: Make a logical copy

– Instead of actually making a copy, just keep track of the context information (the key) during analysis

– Compute results (called procedure summaries) for each logical copy of a function.

– Modify the treatment of calls slightly:

On foo(in) with context C:If (foo,C) doesn't have a summary, process foo(in) in C and save the result to S.

If the summary S already approximates foo(in), use S

Otherwise, process foo(in) in C and update S with (in⨅S.in).

If the result changes, reprocess all callers of (foo,C)

Page 166: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

166

Context Sensitivity - IFDS

● In some cases, context sensitive analysis can be reduced to special forms of graph reachability.

Page 167: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

167

Context Sensitivity - IFDS

● In some cases, context sensitive analysis can be reduced to special forms of graph reachability.

– Set of dataflow facts D is finite

– Transfer functions are distributive [f(x⨅y)=f(x)⨅f(y)]

– Domain and range of transfer functions is (D) �– Lattice ordering is set containment

Page 168: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

168

Context Sensitivity - IFDS

● In some cases, context sensitive analysis can be reduced to special forms of graph reachability.

– Set of dataflow facts D is finite

– Transfer functions are distributive [f(x⨅y)=f(x)⨅f(y)]

– Domain and range of transfer functions is (D) �– Lattice ordering is set containment

(Interprocedural Finite Distributive Subsets)

Page 169: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

169

Context Sensitivity - IFDS

● Consider an undefined variable analysis...

a = 7

b = a

c = d

Page 170: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

170

Context Sensitivity - IFDS

● Consider an undefined variable analysis...

a = 7

b = a

c = d

a b c d

Page 171: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

171

Context Sensitivity - IFDS

● Consider an undefined variable analysis...

a = 7

b = a

c = d

a b c d

Page 172: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

172

Context Sensitivity - IFDS

● Consider an undefined variable analysis...

a = 7

b = a

c = d

a b c d

Page 173: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

173

Context Sensitivity - IFDS

● Consider an undefined variable analysis...

a = 7

b = a

c = d

a b c d

a is defined,so make it unreachable

Page 174: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

174

Context Sensitivity - IFDS

● Consider an undefined variable analysis...

a = 7

b = a

c = d

a b c d

c is unchanged,so propagate its reachability

Page 175: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

175

Context Sensitivity - IFDS

● Consider an undefined variable analysis...

a = 7

b = a

c = d

a b c d

Page 176: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

176

Context Sensitivity - IFDS

● Consider an undefined variable analysis...

a = 7

b = a

c = d

a b c d

Page 177: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

177

Context Sensitivity - IFDS

● Consider an undefined variable analysis...

a = 7

b = a

c = d

a b c d

c and d are reachable here.They are undefined at this point.

Page 178: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

178

Context Sensitivity - IFDS

● Consider an undefined variable analysis…

def main(): x = 7 r = p(x) x = r z = p(x+10)

def p(a): if a < 9: y = 0 else: y = 1

x = 7call p(x)

if a < 9

r = return p(x)x = rcall p(x+10)

z = return p(x+10)

p(a)

y = 1y = 0

return a

Page 179: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

179

Context Sensitivity - IFDS

● Consider an undefined variable analysis…

def main(): x = 7 r = p(x) x = r z = p(x+10)

def p(a): if a < 9: y = 0 else: y = 1

x = 7call p(x)

if a < 9

r = return p(x)x = rcall p(x+10)

z = return p(x+10)

p(a)

y = 1y = 0

return a

(1

)1

(2

)2

Page 180: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

180

Context Sensitivity - IFDS

● Consider an undefined variable analysis…

def main(): x = 7 r = p(x) x = r z = p(x+10)

def p(a): if a < 9: y = 0 else: y = 1

x = 7call p(x)

if a < 9

r = return p(x)x = rcall p(x+10)

z = return p(x+10)

p(a)

y = 1y = 0

return a

(1

)1

(2

)2

string: (1 )

2

Page 181: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

181

Context Sensitivity - IFDS

● Consider an undefined variable analysis…

def main(): x = 7 r = p(x) x = r z = p(x+10)

def p(a): if a < 9: y = 0 else: y = 1

x = 7call p(x)

if a < 9

r = return p(x)x = rcall p(x+10)

z = return p(x+10)

p(a)

y = 1y = 0

return a

(1

)1

(2

)2

string: (1 )

2unreachable

Page 182: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

182

Context Sensitivity - IFDS

● Consider an undefined variable analysis…

def main(): x = 7 r = p(x) x = r z = p(x+10)

def p(a): if a < 9: y = 0 else: y = 1

x = 7call p(x)

if a < 9

r = return p(x)x = rcall p(x+10)

z = return p(x+10)

p(a)

y = 1y = 0

return a

(1

)1

(2

)2

string: (1 )

1

Page 183: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

183

Context Sensitivity - IFDS

● Consider an undefined variable analysis…

def main(): x = 7 r = p(x) x = r z = p(x+10)

def p(a): if a < 9: y = 0 else: y = 1

x = 7call p(x)

if a < 9

r = return p(x)x = rcall p(x+10)

z = return p(x+10)

p(a)

y = 1y = 0

return a

(1

)1

(2

)2

string: (1 )

1(

2)

2

Page 184: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

184

Context Sensitivity - IFDS

● Consider an undefined variable analysis…

def main(): x = 7 r = p(x) x = r z = p(x+10)

def p(a): if a < 9: y = 0 else: y = 1

x = 7call p(x)

if a < 9

r = return p(x)x = rcall p(x+10)

z = return p(x+10)

p(a)

y = 1y = 0

return a

(1

)1

(2

)2

string: (1 )

1(

2)

2reachable

Page 185: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

185

Context Sensitivity - IFDS

● Consider an undefined variable analysis…

● A fact f holds before a node if f is CFL-Reachable in a language of matched parentheses

def main(): x = 7 r = p(x) x = r z = p(x+10)

def p(a): if a < 9: y = 0 else: y = 1

x = 7call p(x)

if a < 9

r = return p(x)x = rcall p(x+10)

z = return p(x+10)

p(a)

y = 1y = 0

return a

(1

)1

(2

)2

Page 186: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

186

Context Sensitivity - IFDS

[Reps, POPL 1995]

Page 187: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

187

Context Sensitivity - IFDS

● Does constant propagation fit our definition of IFDS?

Page 188: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

188

Context Sensitivity - IFDS

● Does constant propagation fit our definition of IFDS?

● Can you think of ways that it could be made to fit into IFDS?

Page 189: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

189

Dataflow Configurations

Can be configured in many ways:

Page 190: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

190

Dataflow Configurations

Can be configured in many ways:

● Forward / Backward (e.g. reaching vs liveness)

Page 191: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

191

Dataflow Configurations

Can be configured in many ways:

● Forward / Backward (e.g. reaching vs liveness)

● May / Must ( vs ∩ in lattice when paths ∏)

Page 192: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

192

Dataflow Configurations

Can be configured in many ways:

● Forward / Backward (e.g. reaching vs liveness)

● May / Must ( vs ∩ in lattice when paths ∏)

● Sensitivity {Path? Flow? Context?}

Page 193: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

193

Dataflow Configurations

Can be configured in many ways:

● Forward / Backward (e.g. reaching vs liveness)

● May / Must ( vs ∩ in lattice when paths ∏)

● Sensitivity {Path? Flow? Context?}

The configuration is ultimately driven by the property/problem of interest

Page 194: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

194

Static Analysis

● We've already seen a few static analyses:

– Call graph construction

– Points-to graph construction (What are MAY/MUST?)

– Static slicing

Page 195: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

195

Static Analysis

● We've already seen a few static analyses:

– Call graph construction

– Points-to graph construction (What are MAY/MUST?)

– Static slicing

● The choices for approximation are why these analyses are imprecise.

Page 196: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

196

Other (Traditionally) Static Approaches

● Type based analyses

● Bounded state exploration

● Symbolic execution

● Model checking

Many of these have been integrated into dynamic analyses, as we shall see over the semester.

Page 197: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

197

Static Analysis Summary

● Considers all possible executions

Page 198: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

198

Static Analysis Summary

● Considers all possible executions

● Approximates program behavior to fight undecidability

Page 199: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

199

Static Analysis Summary

● Considers all possible executions

● Approximates program behavior to fight undecidability

● Can answer queries like:– Must my program always …?– May my program ever …?

Page 200: Static Analysis and Dataflow Analysiswsumner/teaching/886/04-static.pdf · 2 Static Analysis Static analyses consider all possible behaviors of a program without running it. 3 Static

200

Static Analysis Summary

● Considers all possible executions

● Approximates program behavior to fight undecidability

● Can answer queries like:– Must my program always …?– May my program ever …?

● Dataflow analysis is one common form of static analysis