EDA045F: Program Analysis LECTURE 2: DATAFLOW BONUS EXAMPLE - - PowerPoint PPT Presentation

eda045f program analysis
SMART_READER_LITE
LIVE PREVIEW

EDA045F: Program Analysis LECTURE 2: DATAFLOW BONUS EXAMPLE - - PowerPoint PPT Presentation

EDA045F: Program Analysis LECTURE 2: DATAFLOW BONUS EXAMPLE Christoph Reichenbach Example: Reaching Definitions x = 0 x = 0 y = 0 y = 0 z = 1 z = 1 while ( x < 5) { x = x + 1 x = x + 1 i f ( x >= 2) { y = 7 } e l s e { y =


slide-1
SLIDE 1

EDA045F: Program Analysis

LECTURE 2: DATAFLOW BONUS EXAMPLE

Christoph Reichenbach

slide-2
SLIDE 2

Example: Reaching Definitions

x = 0 y = 0 z = 1 while ( x < 5) { x = x + 1 i f ( x >= 2) { y = 7 } e l s e { z = y } } r e t u r n x , y , z x = 0 y = 0 z = 1 x = x + 1 y = 7 z = y return x, y, z

2 / 1

slide-3
SLIDE 3

Example: Reaching Definitions

x = 0 y = 0 z = 1 while ( x < 5) { x = x + 1 i f ( x >= 2) { y = 7 } e l s e { z = y } } r e t u r n x , y , z x = 0 y = 0 z = 1 x = x + 1 y = 7 z = y return x, y, z Reaching Definitions: What values are possible?

2 / 1

slide-4
SLIDE 4

Example: Reaching Definitions

Designing our abstract domain:

⊥ = Z {−3, 0} {0, 3} · · · · · · {−3} {−2} {−1} {0} {1} {2} {3} ⊤ = ∅ {−3, 0, 1}

◮ Capture sets of up to 3 possible numbers ◮ ⊤: ∅ (no possible numbers seen yet) ◮ ⊥: More than 3 possible numbers ◮ Infinitely many elements, but finite height! 3 / 1

slide-5
SLIDE 5

Example: Control-Flow Graph

x = 0 y = 0 z = 1 x = x + 1 y = 7 z = y return x, y, z b0 b1 b2 b3 b4 transb b inputs x y z b0 ∅ 1 b1 {b0, b2, b3} x + 1 y z b2 {b1} x 7 z b3 {b1} x y y b4 {b0, b2, b3} x y z mergeb =

  • s∈inputsb

s

4 / 1

slide-6
SLIDE 6

Example: Computing the Fixpoint

x y z

in:

⊤ ⊤ ⊤

trans:

1

  • ut:

⊤ ⊤ ⊤ x y z

in:

⊤ ⊤ ⊤

trans:

x+1 ↓ ↓

  • ut:

⊤ ⊤ ⊤ x y z

in:

⊤ ⊤ ⊤

trans:

↓ 7 ↓

  • ut:

⊤ ⊤ ⊤ x y z

in:

⊤ ⊤ ⊤

trans:

↓ ↓ y

  • ut:

⊤ ⊤ ⊤ x y z

in:

⊤ ⊤ ⊤

trans:

↓ ↓ ↓

  • ut:

⊤ ⊤ ⊤ b0 b1 b2 b3 b4

Initialise

5 / 1

slide-7
SLIDE 7

Example: Computing the Fixpoint

x y z

in:

⊤ ⊤ ⊤

trans:

1

  • ut:

1 x y z

in:

⊤ ⊤ ⊤

trans:

x+1 ↓ ↓

  • ut:

⊤ ⊤ ⊤ x y z

in:

⊤ ⊤ ⊤

trans:

↓ 7 ↓

  • ut:

⊤ 7 ⊤ x y z

in:

⊤ ⊤ ⊤

trans:

↓ ↓ y

  • ut:

⊤ ⊤ ⊤ x y z

in:

⊤ ⊤ ⊤

trans:

↓ ↓ ↓

  • ut:

⊤ ⊤ ⊤ b0 b1 b2 b3 b4

Transfer (1st)

6 / 1

slide-8
SLIDE 8

Example: Computing the Fixpoint

x y z

in:

⊤ ⊤ ⊤

trans:

1

  • ut:

1 x y z

in:

0,7 1

trans:

x+1 ↓ ↓

  • ut:

⊤ ⊤ ⊤ x y z

in:

⊤ ⊤ ⊤

trans:

↓ 7 ↓

  • ut:

⊤ 7 ⊤ x y z

in:

⊤ ⊤ ⊤

trans:

↓ ↓ y

  • ut:

⊤ ⊤ ⊤ x y z

in:

0,7 1

trans:

↓ ↓ ↓

  • ut:

⊤ ⊤ ⊤ b0 b1 b2 b3 b4

Join (1st)

7 / 1

slide-9
SLIDE 9

Example: Computing the Fixpoint

x y z

in:

⊤ ⊤ ⊤

trans:

1

  • ut:

1 x y z

in:

0,7 1

trans:

x+1 ↓ ↓

  • ut:

1 0,7 1 x y z

in:

⊤ ⊤ ⊤

trans:

↓ 7 ↓

  • ut:

⊤ 7 ⊤ x y z

in:

⊤ ⊤ ⊤

trans:

↓ ↓ y

  • ut:

⊤ ⊤ ⊤ x y z

in:

0,7 1

trans:

↓ ↓ ↓

  • ut:

0,7 1 b0 b1 b2 b3 b4

Transfer (2nd)

8 / 1

slide-10
SLIDE 10

Example: Computing the Fixpoint

x y z

in:

⊤ ⊤ ⊤

trans:

1

  • ut:

1 x y z

in:

0,7 1

trans:

x+1 ↓ ↓

  • ut:

1 0,7 1 x y z

in:

1 0,7 1

trans:

↓ 7 ↓

  • ut:

⊤ 7 ⊤ x y z

in:

1 0,7 1

trans:

↓ ↓ y

  • ut:

⊤ ⊤ ⊤ x y z

in:

0,7 1

trans:

↓ ↓ ↓

  • ut:

0,7 1 b0 b1 b2 b3 b4

Join (2nd)

9 / 1

slide-11
SLIDE 11

Example: Computing the Fixpoint

x y z

in:

⊤ ⊤ ⊤

trans:

1

  • ut:

1 x y z

in:

0,7 1

trans:

x+1 ↓ ↓

  • ut:

1 0,7 1 x y z

in:

1 0,7 1

trans:

↓ 7 ↓

  • ut:

1 7 1 x y z

in:

1 0,7 1

trans:

↓ ↓ y

  • ut:

1 0,7 0,7 x y z

in:

0,7 1

trans:

↓ ↓ ↓

  • ut:

0,7 1 b0 b1 b2 b3 b4

Transfer (3rd)

10 / 1

slide-12
SLIDE 12

Example: Computing the Fixpoint

x y z

in:

⊤ ⊤ ⊤

trans:

1

  • ut:

1 x y z

in:

0,1 0,7 0,1,7

trans:

x+1 ↓ ↓

  • ut:

1 0,7 1 x y z

in:

1 0,7 1

trans:

↓ 7 ↓

  • ut:

1 7 1 x y z

in:

1 0,7 1

trans:

↓ ↓ y

  • ut:

1 0,7 0,7 x y z

in:

0,1 0,7 0,1,7

trans:

↓ ↓ ↓

  • ut:

0,7 1 b0 b1 b2 b3 b4

Join (3rd)

11 / 1

slide-13
SLIDE 13

Example: Computing the Fixpoint

x y z

in:

⊤ ⊤ ⊤

trans:

1

  • ut:

1 x y z

in:

0,1 0,7 0,1,7

trans:

x+1 ↓ ↓

  • ut:

1,2 0,7 0,1,7 x y z

in:

1 0,7 1

trans:

↓ 7 ↓

  • ut:

1 7 1 x y z

in:

1 0,7 1

trans:

↓ ↓ y

  • ut:

1 0,7 0,7 x y z

in:

0,1 0,7 0,1,7

trans:

↓ ↓ ↓

  • ut:

0,1 0,7 0,1,7 b0 b1 b2 b3 b4

Transfer (4th)

12 / 1

slide-14
SLIDE 14

Example: Computing the Fixpoint

x y z

in:

⊤ ⊤ ⊤

trans:

1

  • ut:

1 x y z

in:

0,1 0,7 0,1,7

trans:

x+1 ↓ ↓

  • ut:

1,2 0,7 0,1,7 x y z

in:

1,2 0,7 0,1,7

trans:

↓ 7 ↓

  • ut:

1 7 1 x y z

in:

1,2 0,7 0,1,7

trans:

↓ ↓ y

  • ut:

1 0,7 0,7 x y z

in:

0,1 0,7 0,1,7

trans:

↓ ↓ ↓

  • ut:

0,1 0,7 0,1,7 b0 b1 b2 b3 b4

Join (4th)

13 / 1

slide-15
SLIDE 15

Example: Computing the Fixpoint

x y z

in:

⊤ ⊤ ⊤

trans:

1

  • ut:

1 x y z

in:

0,1 0,7 0,1,7

trans:

x+1 ↓ ↓

  • ut:

1,2 0,7 0,1,7 x y z

in:

1,2 0,7 0,1,7

trans:

↓ 7 ↓

  • ut:

1,2 7 0,1,7 x y z

in:

1,2 0,7 0,1,7

trans:

↓ ↓ y

  • ut:

1,2 0,7 0,7 x y z

in:

0,1 0,7 0,1,7

trans:

↓ ↓ ↓

  • ut:

0,1 0,7 0,1,7 b0 b1 b2 b3 b4

Transfer (5th)

14 / 1

slide-16
SLIDE 16

Example: Computing the Fixpoint

x y z

in:

⊤ ⊤ ⊤

trans:

1

  • ut:

1 x y z

in:

0,1,2 0,7 0,1,7

trans:

x+1 ↓ ↓

  • ut:

1,2 0,7 0,1,7 x y z

in:

1,2 0,7 0,1,7

trans:

↓ 7 ↓

  • ut:

1,2 7 0,1,7 x y z

in:

1,2 0,7 0,1,7

trans:

↓ ↓ y

  • ut:

1,2 0,7 0,7 x y z

in:

0,1,2 0,7 0,1,7

trans:

↓ ↓ ↓

  • ut:

0,1 0,7 0,1,7 b0 b1 b2 b3 b4

Join (5th)

15 / 1

slide-17
SLIDE 17

Example: Computing the Fixpoint

x y z

in:

⊤ ⊤ ⊤

trans:

1

  • ut:

1 x y z

in:

0,1,2 0,7 0,1,7

trans:

x+1 ↓ ↓

  • ut:

1,2,3 0,7 0,1,7 x y z

in:

1,2 0,7 0,1,7

trans:

↓ 7 ↓

  • ut:

1,2 7 0,1,7 x y z

in:

1,2 0,7 0,1,7

trans:

↓ ↓ y

  • ut:

1,2 0,7 0,7 x y z

in:

0,1,2 0,7 0,1,7

trans:

↓ ↓ ↓

  • ut:

0,1,2 0,7 0,1,7 b0 b1 b2 b3 b4

Transfer (6th)

16 / 1

slide-18
SLIDE 18

Example: Computing the Fixpoint

x y z

in:

⊤ ⊤ ⊤

trans:

1

  • ut:

1 x y z

in:

0,1,2 0,7 0,1,7

trans:

x+1 ↓ ↓

  • ut:

1,2,3 0,7 0,1,7 x y z

in:

1,2,3 0,7 0,1,7

trans:

↓ 7 ↓

  • ut:

1,2 7 0,1,7 x y z

in:

1,2,3 0,7 0,1,7

trans:

↓ ↓ y

  • ut:

1,2 0,7 0,7 x y z

in:

0,1,2 0,7 0,1,7

trans:

↓ ↓ ↓

  • ut:

0,1,2 0,7 0,1,7 b0 b1 b2 b3 b4

Join (6th)

17 / 1

slide-19
SLIDE 19

Example: Computing the Fixpoint

x y z

in:

⊤ ⊤ ⊤

trans:

1

  • ut:

1 x y z

in:

0,1,2 0,7 0,1,7

trans:

x+1 ↓ ↓

  • ut:

1,2,3 0,7 0,1,7 x y z

in:

1,2,3 0,7 0,1,7

trans:

↓ 7 ↓

  • ut:

1,2,3 7 0,1,7 x y z

in:

1,2,3 0,7 0,1,7

trans:

↓ ↓ y

  • ut:

1,2,3 0,7 0,7 x y z

in:

0,1,2 0,7 0,1,7

trans:

↓ ↓ ↓

  • ut:

0,1,2 0,7 0,1,7 b0 b1 b2 b3 b4

Transfer (7th)

18 / 1

slide-20
SLIDE 20

Example: Computing the Fixpoint

x y z

in:

⊤ ⊤ ⊤

trans:

1

  • ut:

1 x y z

in:

⊥ 0,7 0,1,7

trans:

x+1 ↓ ↓

  • ut:

1,2,3 0,7 0,1,7 x y z

in:

1,2,3 0,7 0,1,7

trans:

↓ 7 ↓

  • ut:

1,2,3 7 0,1,7 x y z

in:

1,2,3 0,7 0,1,7

trans:

↓ ↓ y

  • ut:

1,2,3 0,7 0,7 x y z

in:

⊥ 0,7 0,1,7

trans:

↓ ↓ ↓

  • ut:

0,1,2 0,7 0,1,7 b0 b1 b2 b3 b4

Join (7th)

19 / 1

slide-21
SLIDE 21

Example: Computing the Fixpoint

x y z

in:

⊤ ⊤ ⊤

trans:

1

  • ut:

1 x y z

in:

⊥ 0,7 0,1,7

trans:

x+1 ↓ ↓

  • ut:

⊥ 0,7 0,1,7 x y z

in:

1,2,3 0,7 0,1,7

trans:

↓ 7 ↓

  • ut:

1,2,3 7 0,1,7 x y z

in:

1,2,3 0,7 0,1,7

trans:

↓ ↓ y

  • ut:

1,2,3 0,7 0,7 x y z

in:

⊥ 0,7 0,1,7

trans:

↓ ↓ ↓

  • ut:

⊥ 0,7 0,1,7 b0 b1 b2 b3 b4

Transfer (8th)

20 / 1

slide-22
SLIDE 22

Example: Computing the Fixpoint

x y z

in:

⊤ ⊤ ⊤

trans:

1

  • ut:

1 x y z

in:

⊥ 0,7 0,1,7

trans:

x+1 ↓ ↓

  • ut:

⊥ 0,7 0,1,7 x y z

in:

⊥ 0,7 0,1,7

trans:

↓ 7 ↓

  • ut:

1,2,3 7 0,1,7 x y z

in:

⊥ 0,7 0,1,7

trans:

↓ ↓ y

  • ut:

1,2,3 0,7 0,7 x y z

in:

⊥ 0,7 0,1,7

trans:

↓ ↓ ↓

  • ut:

⊥ 0,7 0,1,7 b0 b1 b2 b3 b4

Join (8th)

21 / 1

slide-23
SLIDE 23

Example: Computing the Fixpoint

x y z

in:

⊤ ⊤ ⊤

trans:

1

  • ut:

1 x y z

in:

⊥ 0,7 0,1,7

trans:

x+1 ↓ ↓

  • ut:

⊥ 0,7 0,1,7 x y z

in:

⊥ 0,7 0,1,7

trans:

↓ 7 ↓

  • ut:

⊥ 7 0,1,7 x y z

in:

⊥ 0,7 0,1,7

trans:

↓ ↓ y

  • ut:

⊥ 0,7 0,7 x y z

in:

⊥ 0,7 0,1,7

trans:

↓ ↓ ↓

  • ut:

⊥ 0,7 0,1,7 b0 b1 b2 b3 b4

Transfer (9th)

22 / 1

slide-24
SLIDE 24

Example: Computing the Fixpoint

x y z

in:

⊤ ⊤ ⊤

trans:

1

  • ut:

1 x y z

in:

⊥ 0,7 0,1,7

trans:

x+1 ↓ ↓

  • ut:

⊥ 0,7 0,1,7 x y z

in:

⊥ 0,7 0,1,7

trans:

↓ 7 ↓

  • ut:

⊥ 7 0,1,7 x y z

in:

⊥ 0,7 0,1,7

trans:

↓ ↓ y

  • ut:

⊥ 0,7 0,7 x y z

in:

⊥ 0,7 0,1,7

trans:

↓ ↓ ↓

  • ut:

⊥ 0,7 0,1,7 b0 b1 b2 b3 b4

Join (9th)

Fixpoint reached

23 / 1

slide-25
SLIDE 25

Example: Conclusion

x = 0 y = 0 z = 1 while ( x < 5) { x = x + 1 i f ( x >= 2) { y = 7 } e l s e { z = y } } r e t u r n x , y , z

◮ Reached fixpoint after 9 iterations ◮ Return values:

x : ⊥ (unknown/any) y : 0 or 7 z : 0 or 1 or 7

◮ Conservative approximation of reality ◮ Once x reached more than 3 values,

algorithm gave up and went to ⊥

Alternatives are possible:

◮ Faster x + 1: always ⊥ ◮ Bigger/smaller domain 24 / 1