Lecture 4: Data-Flow Analysis (contd.)

23
Lecture 4: Data-Flow Analysis (contd.) 17-355/17-655/17-819: Program Analysis Rohan Padhye and Jonathan Aldrich February 11, 2021 * Course materials developed with Claire Le Goues 1 (c) 2021 J. Aldrich, C. Le Goues, R. Padhye

Transcript of Lecture 4: Data-Flow Analysis (contd.)

Lecture 4: Data-Flow Analysis(contd.)

17-355/17-655/17-819: Program AnalysisRohan Padhye and Jonathan Aldrich

February 11, 2021

* Course materials developed with Claire Le Goues

1(c) 2021 J. Aldrich, C. Le Goues, R. Padhye

Random Facts #1

2(c) 2021 J. Aldrich, C. Le Goues, R. Padhye

exec(s:='print("exec(s:=%r)"%s)')Python 3.8:

β€œYou are here” maps don’t lie

What mathematical concept is common to both these facts?

Example of Zero Analysis: Looping Code

3(c) 2021 J. Aldrich, C. Le Goues, R. Padhye

2:𝑦 ≔ 0

3:ifπ‘₯ = 0 goto 7

4: 𝑦 ≔ 1

6:goto 3

5: π‘₯ ≔ π‘₯ βˆ’ 1

1:π‘₯ ≔ 10P1

P2P3

P4

P5

P7

7:π‘₯ ≔ 𝑦

P6

P8

T

F

P9

P0

Example of Zero Analysis: Looping Code

4(c) 2021 J. Aldrich, C. Le Goues, R. Padhye

2:𝑦 ≔ 0

3:ifπ‘₯ = 0 goto 7

4: 𝑦 ≔ 1

6:goto 3

5: π‘₯ ≔ π‘₯ βˆ’ 1

1:π‘₯ ≔ 10P1

P2P3

P4

P5

P7

7:π‘₯ ≔ 𝑦

P6

P8

T

F

P9

P0

Example of Zero Analysis: Looping Code

5(c) 2021 J. Aldrich, C. Le Goues, R. Padhye

2:𝑦 ≔ 0

3:ifπ‘₯ = 0 goto 7

4: 𝑦 ≔ 1

6:goto 3

5: π‘₯ ≔ π‘₯ βˆ’ 1

1:π‘₯ ≔ 10P1

P2P3

P4

P5

P7

7:π‘₯ ≔ 𝑦

P6

P8

T

F

P9

P0

Fixed point of Flow Functions

6(c) 2021 J. Aldrich, C. Le Goues, R. Padhye

2:𝑦 ≔ 0

3:ifπ‘₯ = 0 goto 7

4: 𝑦 ≔ 1

6:goto 3

5: π‘₯ ≔ π‘₯ βˆ’ 1

1:π‘₯ ≔ 10P1

P2P3

P4

P5

P7

7:π‘₯ ≔ 𝑦

P6

P8

T

F

P9

(𝜎5, 𝜎6, 𝜎7, … , 𝜎8) β†’9! (πœŽβ€²5, πœŽβ€²6, πœŽβ€²7, … , πœŽβ€²8)

P0

πœŽβ€²6 = 𝑓: π‘₯ ≔ 10 (𝜎5)

πœŽβ€²7 = 𝑓: 𝑦 ≔ 0 (𝜎6)

πœŽβ€²; = 𝑓: π‘₯ ≔ 𝑦 (𝜎<)

πœŽβ€²5 = 𝜎5

πœŽβ€²= = 𝜎7 βŠ” 𝜎>

πœŽβ€²< = 𝑓: if π‘₯ = 10 goto 7 ?(𝜎=)

πœŽβ€²@ = 𝑓: if π‘₯ = 10 goto 7 A(𝜎=)

…

Fixed point of Flow Functions

7(c) 2021 J. Aldrich, C. Le Goues, R. Padhye

(𝜎5, 𝜎6, 𝜎7, … , 𝜎8) β†’9! (πœŽβ€²5, πœŽβ€²6, πœŽβ€²7, … , πœŽβ€²8)

πœŽβ€²6 = 𝑓: π‘₯ ≔ 10 (𝜎5)

πœŽβ€²7 = 𝑓: 𝑦 ≔ 0 (𝜎6)

πœŽβ€²; = 𝑓: π‘₯ ≔ 𝑦 (𝜎<)

πœŽβ€²5 = 𝜎5

πœŽβ€²= = 𝜎7 βŠ” 𝜎>

πœŽβ€²< = 𝑓: if π‘₯ = 10 goto 7 ?(𝜎=)

πœŽβ€²@ = 𝑓: if π‘₯ = 10 goto 7 A(𝜎=)

…

(𝜎!, 𝜎", 𝜎#, … , 𝜎$) = 𝑓%(𝜎!, 𝜎", 𝜎#, … , 𝜎$)Fixed point!

Correctness theorem:If data-flow analysis is well designed*, thenany fixed point of the analysis is sound.

* we will define these properties and prove this theoremin two weeks!

More on joins and lattices

8(c) 2021 J. Aldrich, C. Le Goues, R. Padhye

(𝜎5, 𝜎6, 𝜎7, … , 𝜎8) β†’9! (πœŽβ€²5, πœŽβ€²6, πœŽβ€²7, … , πœŽβ€²8)

πœŽβ€²6 = 𝑓: π‘₯ ≔ 10 (𝜎5)

πœŽβ€²7 = 𝑓: 𝑦 ≔ 0 (𝜎6)

πœŽβ€²; = 𝑓: π‘₯ ≔ 𝑦 (𝜎<)

πœŽβ€²5 = 𝜎5

πœŽβ€²= = 𝜎7 βŠ” 𝜎>

πœŽβ€²< = 𝑓: if π‘₯ = 10 goto 7 ?(𝜎=)

πœŽβ€²@ = 𝑓: if π‘₯ = 10 goto 7 A(𝜎=)

…

Hold up! How do you

More on joins and lattices

9(c) 2021 J. Aldrich, C. Le Goues, R. Padhye

πœŽβ€²= = 𝜎7 βŠ” 𝜎>

2:𝑦 ≔ 0

3:ifπ‘₯ = 0 goto 7

4: 𝑦 ≔ 1

6:goto 3

5: π‘₯ ≔ π‘₯ βˆ’ 1

1:π‘₯ ≔ 10P1

P2P3

P4

P5

P7

7:π‘₯ ≔ 𝑦

P6

P8

T

F

P9

What should be the initial value for 𝜎! ????

P0

More on joins and lattices

10(c) 2021 J. Aldrich, C. Le Goues, R. Padhye

Enter: βŠ₯ (β€œbottom”)

βŠ₯βŠ‘ 𝑙βŠ₯βŠ” 𝑙 = 𝑙

for all 𝑙 ∈ 𝐿:𝑙 βŠ‘ βŠ€π‘™ βŠ” ⊀ = ⊀

A lattice with both βŠ₯ and ⊀ defined is called a Complete Lattice

What would the complete lattice for Zero Analysis look like?

More on joins and lattices

11(c) 2021 J. Aldrich, C. Le Goues, R. Padhye

𝜎: π‘‰π‘Žπ‘Ÿ β†’ 𝐿 where 𝐿 = {𝑍,𝑁, βŠ₯, ⊀}

𝜎( βŠ” 𝜎) = { π‘₯ ↦ 𝜎( π‘₯ βŠ” 𝜎) π‘₯ , 𝑦 ↦ 𝜎( 𝑦 βŠ” 𝜎) 𝑦 }

𝜎( βŠ‘ 𝜎) = ???

Exercise: Define lifted βŠ‘ in terms of ordering on 𝐿

More on joins and lattices

12(c) 2021 J. Aldrich, C. Le Goues, R. Padhye

{x↦ ⊀, y↦ ⊀}

{x↦Z, y↦ ⊀} {x↦N, y↦ ⊀} {x↦ ⊀, y↦Z} {x↦ ⊀, y↦N}

… … … … … … … … … … … … …

{x↦ ⊀, y↦βŠ₯} {x↦Z, y↦Z} {x↦Z, y↦N} … … …

{x↦Z, y↦βŠ₯} {x↦N, y↦βŠ₯} {x↦βŠ₯, y↦Z} {x↦βŠ₯, y↦N}

{x↦βŠ₯, y↦βŠ₯}

Lifting a complete lattice gives another complete lattice

Running a Data Flow Analysis

13(c) 2021 J. Aldrich, C. Le Goues, R. Padhye

2:𝑦 ≔ 0

3:ifπ‘₯ = 0 goto 7

4: 𝑦 ≔ 1

6:goto 3

5: π‘₯ ≔ π‘₯ βˆ’ 1

1:π‘₯ ≔ 10P1

P2P3

P4

P5

P7

7:π‘₯ ≔ 𝑦

P6

P8

T

F

P9

βŠ₯ βŠ₯βŠ₯ βŠ₯βŠ₯ βŠ₯βŠ₯ βŠ₯βŠ₯ βŠ₯βŠ₯ βŠ₯

βŠ₯ βŠ₯βŠ₯ βŠ₯βŠ₯ βŠ₯

P0

Running a Data Flow Analysis

14(c) 2021 J. Aldrich, C. Le Goues, R. Padhye

πœŽβ€²= = 𝜎7 βŠ” 𝜎>

2:𝑦 ≔ 0

3:ifπ‘₯ = 0 goto 7

4: 𝑦 ≔ 1

6:goto 3

5: π‘₯ ≔ π‘₯ βˆ’ 1

1:π‘₯ ≔ 10P1

P2P3

P4

P5

P7

7:π‘₯ ≔ 𝑦

P6

P8

T

F

P9

βŠ₯ βŠ₯βŠ₯ βŠ₯βŠ₯ βŠ₯βŠ₯ βŠ₯βŠ₯ βŠ₯βŠ₯ βŠ₯

P0

Running a Data Flow Analysis

15(c) 2021 J. Aldrich, C. Le Goues, R. Padhye

πœŽβ€²= = 𝜎7 βŠ” 𝜎>

2:𝑦 ≔ 0

3:ifπ‘₯ = 0 goto 7

4: 𝑦 ≔ 1

6:goto 3

5: π‘₯ ≔ π‘₯ βˆ’ 1

1:π‘₯ ≔ 10P1

P2P3

P4

P5

P7

7:π‘₯ ≔ 𝑦

P6

P8

T

F

P9

P0

Running a Data Flow Analysis

16(c) 2021 J. Aldrich, C. Le Goues, R. Padhye

πœŽβ€²= = 𝜎7 βŠ” 𝜎>

2:𝑦 ≔ 0

3:ifπ‘₯ = 0 goto 7

4: 𝑦 ≔ 1

6:goto 3

5: π‘₯ ≔ π‘₯ βˆ’ 1

1:π‘₯ ≔ 10P1

P2P3

P4

P5

P7

7:π‘₯ ≔ 𝑦

P6

P8

T

F

P9

P0

Running a Data Flow Analysis

17(c) 2021 J. Aldrich, C. Le Goues, R. Padhye

2:𝑦 ≔ 0

3:ifπ‘₯ = 0 goto 7

4: 𝑦 ≔ 1

6:goto 3

5: π‘₯ ≔ π‘₯ βˆ’ 1

1:π‘₯ ≔ 10P1

P2P3

P4

P5

P7

7:π‘₯ ≔ 𝑦

P6

P8

T

F

P9

P0

WHAT’S THE ALGORITHM?

18(c) 2021 J. Aldrich, C. Le Goues, R. Padhye

Analysis Execution Strategy

20(c) 2021 J. Aldrich, C. Le Goues, R. Padhye

Kildall’s Algorithm

21(c) 2021 J. Aldrich, C. Le Goues, R. Padhye

What order to process worklist nodes in?β€’ Random? Queue? Stack?β€’ Any order is valid (!!)β€’ Some orders are better in practice

o Topological sorts are niceo Explore loops inside outo Reverse postorder!

22(c) 2021 J. Aldrich, C. Le Goues, R. Padhye

Exercise: Apply Kildall’s Worklist Algorithm for Zero Analysis

23(c) 2021 J. Aldrich, C. Le Goues, R. Padhye

2:𝑦 ≔ 0

3:ifπ‘₯ = 0 goto 7

4: 𝑦 ≔ 1

6:goto 3

5: π‘₯ ≔ π‘₯ βˆ’ 1

1:π‘₯ ≔ 10

7:π‘₯ ≔ 𝑦

T

F

Performance of Kildall’s Algorithmβ€’ Why is it guaranteed to terminate?β€’ What is its complexity?

24(c) 2021 J. Aldrich, C. Le Goues, R. Padhye