EDA045F: Program Analysis
LECTURE 2: DATAFLOW BONUS EXAMPLE
Christoph Reichenbach
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 =
LECTURE 2: DATAFLOW BONUS EXAMPLE
Christoph Reichenbach
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
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
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
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
4 / 1
x y z
in:
⊤ ⊤ ⊤
trans:
1
⊤ ⊤ ⊤ x y z
in:
⊤ ⊤ ⊤
trans:
x+1 ↓ ↓
⊤ ⊤ ⊤ x y z
in:
⊤ ⊤ ⊤
trans:
↓ 7 ↓
⊤ ⊤ ⊤ x y z
in:
⊤ ⊤ ⊤
trans:
↓ ↓ y
⊤ ⊤ ⊤ x y z
in:
⊤ ⊤ ⊤
trans:
↓ ↓ ↓
⊤ ⊤ ⊤ b0 b1 b2 b3 b4
5 / 1
x y z
in:
⊤ ⊤ ⊤
trans:
1
1 x y z
in:
⊤ ⊤ ⊤
trans:
x+1 ↓ ↓
⊤ ⊤ ⊤ x y z
in:
⊤ ⊤ ⊤
trans:
↓ 7 ↓
⊤ 7 ⊤ x y z
in:
⊤ ⊤ ⊤
trans:
↓ ↓ y
⊤ ⊤ ⊤ x y z
in:
⊤ ⊤ ⊤
trans:
↓ ↓ ↓
⊤ ⊤ ⊤ b0 b1 b2 b3 b4
6 / 1
x y z
in:
⊤ ⊤ ⊤
trans:
1
1 x y z
in:
0,7 1
trans:
x+1 ↓ ↓
⊤ ⊤ ⊤ x y z
in:
⊤ ⊤ ⊤
trans:
↓ 7 ↓
⊤ 7 ⊤ x y z
in:
⊤ ⊤ ⊤
trans:
↓ ↓ y
⊤ ⊤ ⊤ x y z
in:
0,7 1
trans:
↓ ↓ ↓
⊤ ⊤ ⊤ b0 b1 b2 b3 b4
7 / 1
x y z
in:
⊤ ⊤ ⊤
trans:
1
1 x y z
in:
0,7 1
trans:
x+1 ↓ ↓
1 0,7 1 x y z
in:
⊤ ⊤ ⊤
trans:
↓ 7 ↓
⊤ 7 ⊤ x y z
in:
⊤ ⊤ ⊤
trans:
↓ ↓ y
⊤ ⊤ ⊤ x y z
in:
0,7 1
trans:
↓ ↓ ↓
0,7 1 b0 b1 b2 b3 b4
8 / 1
x y z
in:
⊤ ⊤ ⊤
trans:
1
1 x y z
in:
0,7 1
trans:
x+1 ↓ ↓
1 0,7 1 x y z
in:
1 0,7 1
trans:
↓ 7 ↓
⊤ 7 ⊤ x y z
in:
1 0,7 1
trans:
↓ ↓ y
⊤ ⊤ ⊤ x y z
in:
0,7 1
trans:
↓ ↓ ↓
0,7 1 b0 b1 b2 b3 b4
9 / 1
x y z
in:
⊤ ⊤ ⊤
trans:
1
1 x y z
in:
0,7 1
trans:
x+1 ↓ ↓
1 0,7 1 x y z
in:
1 0,7 1
trans:
↓ 7 ↓
1 7 1 x y z
in:
1 0,7 1
trans:
↓ ↓ y
1 0,7 0,7 x y z
in:
0,7 1
trans:
↓ ↓ ↓
0,7 1 b0 b1 b2 b3 b4
10 / 1
x y z
in:
⊤ ⊤ ⊤
trans:
1
1 x y z
in:
0,1 0,7 0,1,7
trans:
x+1 ↓ ↓
1 0,7 1 x y z
in:
1 0,7 1
trans:
↓ 7 ↓
1 7 1 x y z
in:
1 0,7 1
trans:
↓ ↓ y
1 0,7 0,7 x y z
in:
0,1 0,7 0,1,7
trans:
↓ ↓ ↓
0,7 1 b0 b1 b2 b3 b4
11 / 1
x y z
in:
⊤ ⊤ ⊤
trans:
1
1 x y z
in:
0,1 0,7 0,1,7
trans:
x+1 ↓ ↓
1,2 0,7 0,1,7 x y z
in:
1 0,7 1
trans:
↓ 7 ↓
1 7 1 x y z
in:
1 0,7 1
trans:
↓ ↓ y
1 0,7 0,7 x y z
in:
0,1 0,7 0,1,7
trans:
↓ ↓ ↓
0,1 0,7 0,1,7 b0 b1 b2 b3 b4
12 / 1
x y z
in:
⊤ ⊤ ⊤
trans:
1
1 x y z
in:
0,1 0,7 0,1,7
trans:
x+1 ↓ ↓
1,2 0,7 0,1,7 x y z
in:
1,2 0,7 0,1,7
trans:
↓ 7 ↓
1 7 1 x y z
in:
1,2 0,7 0,1,7
trans:
↓ ↓ y
1 0,7 0,7 x y z
in:
0,1 0,7 0,1,7
trans:
↓ ↓ ↓
0,1 0,7 0,1,7 b0 b1 b2 b3 b4
13 / 1
x y z
in:
⊤ ⊤ ⊤
trans:
1
1 x y z
in:
0,1 0,7 0,1,7
trans:
x+1 ↓ ↓
1,2 0,7 0,1,7 x y z
in:
1,2 0,7 0,1,7
trans:
↓ 7 ↓
1,2 7 0,1,7 x y z
in:
1,2 0,7 0,1,7
trans:
↓ ↓ y
1,2 0,7 0,7 x y z
in:
0,1 0,7 0,1,7
trans:
↓ ↓ ↓
0,1 0,7 0,1,7 b0 b1 b2 b3 b4
14 / 1
x y z
in:
⊤ ⊤ ⊤
trans:
1
1 x y z
in:
0,1,2 0,7 0,1,7
trans:
x+1 ↓ ↓
1,2 0,7 0,1,7 x y z
in:
1,2 0,7 0,1,7
trans:
↓ 7 ↓
1,2 7 0,1,7 x y z
in:
1,2 0,7 0,1,7
trans:
↓ ↓ y
1,2 0,7 0,7 x y z
in:
0,1,2 0,7 0,1,7
trans:
↓ ↓ ↓
0,1 0,7 0,1,7 b0 b1 b2 b3 b4
15 / 1
x y z
in:
⊤ ⊤ ⊤
trans:
1
1 x y z
in:
0,1,2 0,7 0,1,7
trans:
x+1 ↓ ↓
1,2,3 0,7 0,1,7 x y z
in:
1,2 0,7 0,1,7
trans:
↓ 7 ↓
1,2 7 0,1,7 x y z
in:
1,2 0,7 0,1,7
trans:
↓ ↓ y
1,2 0,7 0,7 x y z
in:
0,1,2 0,7 0,1,7
trans:
↓ ↓ ↓
0,1,2 0,7 0,1,7 b0 b1 b2 b3 b4
16 / 1
x y z
in:
⊤ ⊤ ⊤
trans:
1
1 x y z
in:
0,1,2 0,7 0,1,7
trans:
x+1 ↓ ↓
1,2,3 0,7 0,1,7 x y z
in:
1,2,3 0,7 0,1,7
trans:
↓ 7 ↓
1,2 7 0,1,7 x y z
in:
1,2,3 0,7 0,1,7
trans:
↓ ↓ y
1,2 0,7 0,7 x y z
in:
0,1,2 0,7 0,1,7
trans:
↓ ↓ ↓
0,1,2 0,7 0,1,7 b0 b1 b2 b3 b4
17 / 1
x y z
in:
⊤ ⊤ ⊤
trans:
1
1 x y z
in:
0,1,2 0,7 0,1,7
trans:
x+1 ↓ ↓
1,2,3 0,7 0,1,7 x y z
in:
1,2,3 0,7 0,1,7
trans:
↓ 7 ↓
1,2,3 7 0,1,7 x y z
in:
1,2,3 0,7 0,1,7
trans:
↓ ↓ y
1,2,3 0,7 0,7 x y z
in:
0,1,2 0,7 0,1,7
trans:
↓ ↓ ↓
0,1,2 0,7 0,1,7 b0 b1 b2 b3 b4
18 / 1
x y z
in:
⊤ ⊤ ⊤
trans:
1
1 x y z
in:
⊥ 0,7 0,1,7
trans:
x+1 ↓ ↓
1,2,3 0,7 0,1,7 x y z
in:
1,2,3 0,7 0,1,7
trans:
↓ 7 ↓
1,2,3 7 0,1,7 x y z
in:
1,2,3 0,7 0,1,7
trans:
↓ ↓ y
1,2,3 0,7 0,7 x y z
in:
⊥ 0,7 0,1,7
trans:
↓ ↓ ↓
0,1,2 0,7 0,1,7 b0 b1 b2 b3 b4
19 / 1
x y z
in:
⊤ ⊤ ⊤
trans:
1
1 x y z
in:
⊥ 0,7 0,1,7
trans:
x+1 ↓ ↓
⊥ 0,7 0,1,7 x y z
in:
1,2,3 0,7 0,1,7
trans:
↓ 7 ↓
1,2,3 7 0,1,7 x y z
in:
1,2,3 0,7 0,1,7
trans:
↓ ↓ y
1,2,3 0,7 0,7 x y z
in:
⊥ 0,7 0,1,7
trans:
↓ ↓ ↓
⊥ 0,7 0,1,7 b0 b1 b2 b3 b4
20 / 1
x y z
in:
⊤ ⊤ ⊤
trans:
1
1 x y z
in:
⊥ 0,7 0,1,7
trans:
x+1 ↓ ↓
⊥ 0,7 0,1,7 x y z
in:
⊥ 0,7 0,1,7
trans:
↓ 7 ↓
1,2,3 7 0,1,7 x y z
in:
⊥ 0,7 0,1,7
trans:
↓ ↓ y
1,2,3 0,7 0,7 x y z
in:
⊥ 0,7 0,1,7
trans:
↓ ↓ ↓
⊥ 0,7 0,1,7 b0 b1 b2 b3 b4
21 / 1
x y z
in:
⊤ ⊤ ⊤
trans:
1
1 x y z
in:
⊥ 0,7 0,1,7
trans:
x+1 ↓ ↓
⊥ 0,7 0,1,7 x y z
in:
⊥ 0,7 0,1,7
trans:
↓ 7 ↓
⊥ 7 0,1,7 x y z
in:
⊥ 0,7 0,1,7
trans:
↓ ↓ y
⊥ 0,7 0,7 x y z
in:
⊥ 0,7 0,1,7
trans:
↓ ↓ ↓
⊥ 0,7 0,1,7 b0 b1 b2 b3 b4
22 / 1
x y z
in:
⊤ ⊤ ⊤
trans:
1
1 x y z
in:
⊥ 0,7 0,1,7
trans:
x+1 ↓ ↓
⊥ 0,7 0,1,7 x y z
in:
⊥ 0,7 0,1,7
trans:
↓ 7 ↓
⊥ 7 0,1,7 x y z
in:
⊥ 0,7 0,1,7
trans:
↓ ↓ y
⊥ 0,7 0,7 x y z
in:
⊥ 0,7 0,1,7
trans:
↓ ↓ ↓
⊥ 0,7 0,1,7 b0 b1 b2 b3 b4
23 / 1
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:
◮ 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