Professional Documents
Culture Documents
4 Chaotic Iterations
4 Chaotic Iterations
spring 2005
slides by Mooly Sagiv
2
z=3
x=1
x=1
while (x > 0) (
while (x > 0) (
z=3
if (x = 1) then y = 7
else y = z + 4
x=3
print y
z=3
z=3
x=1
x=1
while (x > 0) (
while (x > 0) (
if (x = 1) then y = 7
else y = z + 4
x=3
print y
)
if (x = 1) then y = 7
x=3
print y
)
5
z=3
z=3
x=1
x=1
while (x > 0) (
print y
)
while (x > 0) (
if (x = 1) then y = 7
else y =z + 4
[xxS, yx7,zx3]
[xxS, yx7,zx3]
[xx3, yx7, zx3]
x=3
print y
)
7
Computing Constants
Constructing CFG
z =3
z=3
x =1
x=1
while (x > 0) (
while (x>0)
if (x = 1) then y = 7
if (x=1)
else y =z + 4
x=3
y =7
print y
y =z+4
x=3
)
print y
Iterative Computation
[xx0, yx0, zx0]
z =3
z =3
Pe.e[zx3]
Pe.e[zx3]
x =1
x =1
while (x>0)
Pe.e[xx1]
while (x>0)
Pe.e[xx1]
Pe. if x 0 then e
Pe. if x >0 then e
else z
else z
if (x=1)
Pe. e 6[xx1, y xS, zxS]
Pe.e y =7
Pe.e[yx7]
x=3
print y
Pe. if x 0 then e
10
else z
Pe.e[xx3]
11
Pe.e y =7
Pe.e[yx7]
z x=3
z
else z
else z
if (x=1)
y =z+4
Pe.e[yxe(z)+4]
Pe. if x 0 then e
print y
Pe. if x 0 then e
else z
y =z+4 z
Pe.e[yxe(z)+4]
Pe.e[xx3]
12
Iterative Computation
[xx0, yx0, zx0]
Iterative Computation
[xx0, yx0, zx0]
z =3
Pe.e[zx3]
z =3
Pe.e[zx3]
x =1
x =1
Pe.e[xx1]
z
while (x>0)
z
else z
Pe. if x 0 then e
Pe.e y =7
Pe.e[yx7]
z x=3
z
else z
else z
Pe.e[xx3]
print y
Pe.e y =7
Pe.e[yx7]
z x=3
else z
else z
else z
Pe.e[zx3]
Pe.e[xx3]
print y
else z
else z
else z
y =z+4 z
Pe.e[yxe(z)+4]
Pe.e[xx3]
print y
16
Iterative Computation
[xx0, yx0, zx0]
z =3
Pe.e[zx3]
x =1
x =1
Pe.e[xx1]
Pe.e[xx1]
Pe. if x 0 then e
Pe. if x >0 then e
else z
else z
if (x=1)
Pe. if x 0 then e
Pe.e y =7
Pe.e[yx7]
z x=3
15
Pe. if x 0 then e
if (x=1)
y =z+4 z
Pe.e[yxe(z)+4]
Pe.e[zx3]
14
x =1
z =3
Pe.e[xx3]
Iterative Computation
[xx0, yx0, zx0]
else z
Pe.e[xx1]
Pe. if x 0 then e
if (x=1)
Pe. if x 0 then e
y =z+4 z
Pe.e[yxe(z)+4]
print y
Pe.e[xx1]
else z
z =3
x =1
else z
Iterative Computation
z =3
Pe.e y =7
Pe.e[yx7]
z x=3
13
Pe. if x 0 then e
if (x=1)
y =z+4 z
Pe.e[yxe(z)+4]
Iterative Computation
[xx0, yx0, zx0]
while (x>0)
if (x=1)
Pe.e[xx1]
Pe. if x 0 then e
print y
Pe. if x 0 then e
else z
Pe.e[xx3]
17
else z
else z
if (x=1)
y =z+4 z
Pe.e[yxe(z)+4]
Pe. if x 0 then e
print y
Pe. if x 0 then e
else z
y =z+4 z
Pe.e[yxe(z)+4]
Pe.e[xx3]
18
Iterative Computation
[xx0, yx0, zx0]
[xx0, yx0, zx3]
Iterative Computation
[xx0, yx0, zx0]
z =3
Pe.e[zx3]
x =1
z =3
Pe.e[zx3]
x =1
Pe.e[xx1]
Pe.e[xx1]
Pe. if x 0 then e
Pe. if x >0 then e
else z
else z
if (x=1)
Pe. if x 0 then e
Pe.e[yx7]
else z
Pe.e[yx7]
19
Iterative Computation
[xx0, yx0, zx3]
z =3
Pe.e[zx3]
x =1
else z
z =3
Pe.e[zx3]
x =1
else z
else z
Pe.e[yx7]
Pe.e[xx3]
21
Iterative Computation
else z
else z
else z
y =z+4 z
Pe.e[yxe(z)+4]
Pe. if x 0 then e
if (x=1)
y =z+4 z
Pe.e[yxe(z)+4]
Pe.e[yx7]
Pe.e[xx3]
22
Iterative Computation
[xx0, yx0, zx0]
z =3
Pe.e[zx3]
x =1
z =3
Pe.e[zx3]
x =1
Pe.e[xx1]
20
Pe.e[xx1]
Pe. if x 0 then e
if (x=1)
Pe.e[xx3]
Pe.e[xx1]
else z
Iterative Computation
Pe. if x 0 then e
Pe.e[xx3]
else z
y =z+4 z
Pe.e[yxe(z)+4]
else z
if (x=1)
y =z+4 z
Pe.e[yxe(z)+4]
Pe. if x 0 then e
Pe.e[xx1]
Pe. if x 0 then e
Pe. if x >0 then e
else z
else z
if (x=1)
Pe. if x 0 then e
else z
Pe.e[xx3]
23
else z
else z
if (x=1)
Pe. if x 0 then e
Pe. if x 0 then e
else z
Posets
Mathematical Background
Declaratively define
b : L % L {false, true}
25
26
Posets
Denoted by (L, b )
In program analysis:
l1 b l2
l1 is more precise than l2
l1 represents fewer concrete states than l2
Examples
28
Complete Lattices
Bounds
A greatest lower bound of a subset L ` L is a lower
bound l0 cL such that l b l0 for any lower bound l of L
A lowest upper bound of a subset L ` L is an upper
bound u0 cL such that u0 b u for any upper bound u of L
For every subset L ` L:
6L (meet) a 6b
Constant propagation
29
30
Complete Lattices
Cartesian Products
A complete lattice
(L1, b1) = (L1, b, 71, 61, z1, S1)
A complete lattice
(L2, b2) = (, b, 72, 62, z2, S2)
Define a Poset L = (L1 % L2 ,b ) where
L is a complete lattice
31
32
Finite Maps
Chains
A subset Y ` L in a poset (L, b ) is a chain if every two
elements in Y are ordered
A complete lattice
(L1, b1) = (L1, b, 71, 61, z1, S1)
A finite set V
Define a Poset L = (V L1 ,b ) where
e1 b e2 if for all v V
l1 a l2 a l3a
e1v b e2v
L is a complete lattice
34
Galois Connection
Monotone Functions
A poset (L, b )
A function f: L L is monotone if for every
l1, l2 c L:
l1 b l2 f(l1 ) b f(l2 )
Alternatively if:
c C . a A
(c) b a iff c b (a)
and uniquely determine each other
35
36
S
f(S)
Fixed Points
Chaotic Iterations
f2(S)
A monotone function f: L L where
Red(f)
(L, b, 7, 6, z, S) is a complete lattice
Fix(f) = { l: l c L, f(l) = l}
Red(f) = {l: l c L, f(l) b l}
Fix(f)
Ext(f) = {l: l c L, l b f(l)}
l1 b l2 f(l1 ) b f(l2 )
Tarskis Theorem 1955: if f is monotone then:
lfp(f) = 6 Fix(f) = 6 Red(f) c Fix(f)
gfp(f) = 7 Fix(f) = 7 Ext(f) c Fix(f)
Ext(f)
gfp(f)
lfp(f)
x := (z, z, , z)
while (f(x) x ) do
f2(z)
x := f(x)
f(z)
z
38
Precision
Chaotic Iterations
WL = {1, 2, , n}
while (WL ) do
select and remove an element i c WL
new := fi(x)
if (new x[i]) then
x[i] := new;
Add all the indexes that directly depends on i to WL
39
40
WL
z =3
Pe.e[zx3]
df[v] =
Pe. if x 0 then e
while (x>0)
while (WL ) do
select and remove an element u c WL
temp = f(e)(dfentry[u])
new := dfentry(v)7 temp
Pe.e[yx7]
7
41
else z
else z
Pe. if x 0 then e
y =z+4
Pe.e[yxe(z)+4]
x=3
dfentry[v] := new;
WL := WL {v}
{2}
{3}
x =1
Pe.e[xx1]
WL = {s}
dfentry[v]
{1}
Pe.e[xx3]
{4}
{5}
{7}
{8}
else z {3}
{4}
{5,6}
{6,7}
{7}
print y
Parameters:
S=
dfentry[s] =
dfentry[v] = 7{f(u, v) (dfentry[u]) | (u, v) E }
FS:Ln Ln
FS (X)[s] =
FS(X)[v] = 7{f(u, v)(X[u]) | (u, v) E }
Complexity: O(n * h * c * k)
lfp(S) = lfp(FS)
43
44
Soundness, Completeness
Soundness:
Completeness:
Soundness in
Constant Propagation
46
Proof of Soundness
Define an appropriate structural operational semantics
Define collecting structural operational semantics
Establish a Galois connection between collecting states and
reaching definitions
(Local correctness) Show that the abstract interpretation of every
atomic statement is sound
w.r.t. the collecting semantics
(Global correctness) Conclude that the analysis is sound CC1976
48
axioms
rules
if Bbs=tt
if Bbs=ff
49
50
Collecting Semantics
51
52
Another Example
while (true) do
x = x +1
if (x = 1) then y = 7
else y = z + 4
x=3
print y
54
An Iterative Definition
[skip]
CSexit(1) = CSentry(l)
[b]
CSexit(1) = {: CSentry(l), b=tt}
[x := a]
CSexit(1) = {(s[x hAas]) | s CSentry(l)}
55
S=
CSentry[s] ={0}
CSentry[v] = {f(e)(CSentry[u]) | (u, v) E }
CS = Fcs (CS)
lfp(S) = lfp(FS)
57
58
Operational
semantics
Set of states
statement s
concretization
abstract
representation
statement s
Abstract
semantics
56
Operational
semantics
Set of states
` Set of states
Set of states
statement s
abstraction
concretization
abstract
representation
abstract
representation
59
Set of states
abstraction
statement s
Abstract
semantics
abstract
abstract
representation t representation
60
10
61
Galois Connection
62
CP is monotone
CP is monotone
df [Var* Z{z, S}]
CP( CP (df)) b df
c P([Var* Z])
c CP b CP ( CP(C))
63
64
Local Soundness
CP({S | CS }b S# ( CP(CS))
{S | CP (df)} CP (S# (df))
({S | CP (df)}) b S# (df)
For Booleans
65
66
11
Lemma 1
Consider a lattice L.
f: L L is monotone iff
for all X L: 7{f(z) | z X } b f(7{z | z X })
Local Soundness
({ x :=e | CS }b x :=e # ((CS))
67
Soundness Theorem(1)
68
Soundness Theorem(2)
1.
1.
2.
f: C C be a monotone function
2.
f: C C be a monotone function
3.
f# : A A be a monotone function
3.
f# : A A be a monotone function
4.
4.
aA: f((a)) b
(f#(a))
lfp(f) b (lfp(f#))
(lfp(f)) b lfp(f#)
(lfp(f)) b
lfp(f) b (lfp(f#))
lfp(f#)
69
Soundness Theorem(3)
1.
2.
f: C C be a monotone function
3.
f# : A A be a monotone function
4.
70
(lfp(f)) b lfp(f#)
lfp(f) b (lfp(f#))
71
72
12
Operational
semantics
Set of states
statement s
concretization
abstract
representation
statement s
Abstract
semantics
Set of states `
Set of states
abstraction
concretization
abstract
representation
Define
S# (df) = ({S| (df)})
But this S# may not be computable
Derive (at compiler-generation time) an alternative form
for S#
A useful measure to decide if the abstraction must lead
to overly imprecise results
73
74
Example: May-Be-Garbage
Lattice
Galois connection
Basic statements
Soundness
75
76
Points-To Analysis
Determine if a pointer variable p may point to
q on some path leading to a program point
a := x | *x | &x | n | a1 opa a2
Constant propagation
x = 5; *p = 7 ; x
S := x := a | *x := a | skip | S1 ; S2 |
if b then S1 else S2 | while b do S
Pointer aliases
Variables p and q are may-aliases at v if the points-to set
at v contains entries (p, x) and (q, x)
Side-effect analysis
*p = *q + * * t
77
78
13
Points-To Analysis
State1= [LocLocZ]
Lattice Lpt =
Galois connection
x := &y ()
x := *y ()
x := y ()
*x := y ()
79
80
Example
t := &a;
y := &b;
x := &y #
z := &c;
For
x := *y
x := y
if x> 0;
then p:= &y;
else p:= &z;
*x := y #
*p := t;
81
Example
82
else p:= &z; /* {(t, a), (y, b), (z, c), (p, z)}*/
/* {(t, a), (y, b), (z, c), (p, y), (p, z)}*/
*p := t;
/* {(t, a), (y, b), (y, c), (p, y), (p, z), (y, a), (z, a)}*/
83
84
14
Example
Precision
t := &a;
y := &b;
z := &c;
(CS) = DF
on all programs
if x> 0;
then p:= &y;
else p:= &z;
*p := t;
85
86
87
88
Conclusions
15