+ All Categories
Home > Documents > 1 Iterative Program Analysis Part I Mooly Sagiv msagiv/courses/pa.html Tel Aviv University 640-6706...

1 Iterative Program Analysis Part I Mooly Sagiv msagiv/courses/pa.html Tel Aviv University 640-6706...

Date post: 20-Dec-2015
Category:
View: 217 times
Download: 0 times
Share this document with a friend
Popular Tags:
88
1 Iterative Program Analysis Part I Mooly Sagiv http://www.cs.tau.ac.il/~msagiv/courses/pa.html Tel Aviv University 640-6706 Textbook: Principles of Program Analysis Chapter 2.1 +6 (modified) Appendix A
Transcript
Page 1: 1 Iterative Program Analysis Part I Mooly Sagiv msagiv/courses/pa.html Tel Aviv University 640-6706 Textbook: Principles of Program.

1

Iterative Program AnalysisPart I

Mooly Sagivhttp://www.cs.tau.ac.il/~msagiv/courses/pa.html

Tel Aviv University

640-6706

Textbook: Principles of Program Analysis

Chapter 2.1 +6 (modified)

Appendix A

Page 2: 1 Iterative Program Analysis Part I Mooly Sagiv msagiv/courses/pa.html Tel Aviv University 640-6706 Textbook: Principles of Program.

Outline A gentle introduction constant propagation Mathematical background Chaotic iterations Abstract interpretation More examples

– Kill/Gen Problems– Garbage variables– Pointer analysis

» Stack» Heap (later)

– Array bound (next week) Precision/Completeness

Page 3: 1 Iterative Program Analysis Part I Mooly Sagiv msagiv/courses/pa.html Tel Aviv University 640-6706 Textbook: Principles of Program.

A Simple Example Program

z = 3

x = 1

while (x > 0) (

if (x = 1) then y = 7

else y = z + 4

x = 3

print y

)

[x0, y0, z0]

[x1, y0, z3]

[x1, y0, z3][x1, y7, z3]

[x0, y0, z3]

[x3, y7, z3]

[x3, y7, z3]

[x1, y7, z3]

Page 4: 1 Iterative Program Analysis Part I Mooly Sagiv msagiv/courses/pa.html Tel Aviv University 640-6706 Textbook: Principles of Program.

A Simple Example Program

z = 3

x = 1

while (x > 0) (

if (x = 1) then y = 7

else y = z + 4

x = 3

print y

)

[x0, y0, z0]

[x1, y0, z3]

[x, y0, z3][x1, y7, z3]

[x1, y7, z3]

[x0, y0, z3]

[x3, y7, z3]

[x3, y7, z3]

Page 5: 1 Iterative Program Analysis Part I Mooly Sagiv msagiv/courses/pa.html Tel Aviv University 640-6706 Textbook: Principles of Program.

A Simple Example Program

z = 3

x = 1

while (x > 0) (

if (x = 1) then y = 7

else y = z + 4

x = 3

print y

)

[x0, y0, z0]

[x1, y0, z3]

[x, y0, z3][x, y7, z3]

[x1, y7, z3]

[x0, y0, z3]

[x3, y7, z3]

[x3, y7, z3]

Page 6: 1 Iterative Program Analysis Part I Mooly Sagiv msagiv/courses/pa.html Tel Aviv University 640-6706 Textbook: Principles of Program.

A Simple Example Program

z = 3

x = 1

while (x > 0) (

if (x = 1) then y = 7

else y =z + 4

x = 3

print y

)

[x0, y0, z0]

[x1, y0, z3]

[x, y0, z3][x, y7, z3]

[x, y7,z3]

[x0, y0, z3]

[x3, y7, z3]

[x3, y7, z3]

[x1, y7,z3]

Page 7: 1 Iterative Program Analysis Part I Mooly Sagiv msagiv/courses/pa.html Tel Aviv University 640-6706 Textbook: Principles of Program.

A Simple Example Program

z = 3

x = 1

while (x > 0) (

if (x = 1) then y = 7

else y =z + 4

x = 3

print y

)

[x0, y0, z0]

[x1, y0, z3]

[x, y0, z3][x1, y7, z3]

[x, y7,z3]

[x0, y0, z3]

[x3, y7, z3]

[x3, y7, z3]

[x, y7,z3][x1, y7,z3]

Page 8: 1 Iterative Program Analysis Part I Mooly Sagiv msagiv/courses/pa.html Tel Aviv University 640-6706 Textbook: Principles of Program.

A Simple Example Program

z = 3

x = 1

while (x > 0) (

if (x = 1) then y = 7

else y =z + 4

x = 3

print y

)

[x0, y0, z0]

[x1, y0, z3]

[x, y0, z3][x1, y7, z3]

[x0, y0, z3]

[x3, y7, z3]

[x3, y7, z3]

[x, y7,z3][x, y7,z3]

Page 9: 1 Iterative Program Analysis Part I Mooly Sagiv msagiv/courses/pa.html Tel Aviv University 640-6706 Textbook: Principles of Program.

Computing Constants

Construct a control flow graph (CFG) Associate transfer functions with control flow

graph edges Iterate until a solution is found The solution is unique

– But order of evaluation may affect the number of iterations

Page 10: 1 Iterative Program Analysis Part I Mooly Sagiv msagiv/courses/pa.html Tel Aviv University 640-6706 Textbook: Principles of Program.

Constructing CFG

z = 3

x = 1

while (x > 0) (

if (x = 1) then y = 7

else y =z + 4

x = 3

print y

)

z =3

x =1

while (x>0)

if (x=1)

y =7 y =z+4

x=3

print y

Page 11: 1 Iterative Program Analysis Part I Mooly Sagiv msagiv/courses/pa.html Tel Aviv University 640-6706 Textbook: Principles of Program.

Associating Transfer Functionsz =3

x =1

while (x>0)

if (x=1)

y =7 y =z+4

x=3

print y

e.e[z3]

e.e[x1]

e. if x >0 then e else

e. if x 0 then e else

e. e [x1, y , z] e. if x 0 then e else

e.e[y7] e.e[ye(z)+4]

e.e[x3]

e.e

Page 12: 1 Iterative Program Analysis Part I Mooly Sagiv msagiv/courses/pa.html Tel Aviv University 640-6706 Textbook: Principles of Program.

Iterative Computationz =3

x =1

while (x>0)

if (x=1)

y =7 y =z+4

x=3

print y

e.e[z3]

e.e[x1]

e. if x >0 then e else

e. if x 0 then e else

e. e [x1, y , z] e. if x 0 then e else

e.e[y7] e.e[ye(z)+4]

e.e[x3]

e.e

[x0, y0, z0]

Page 13: 1 Iterative Program Analysis Part I Mooly Sagiv msagiv/courses/pa.html Tel Aviv University 640-6706 Textbook: Principles of Program.

Iterative Computationz =3

x =1

while (x>0)

if (x=1)

y =7 y =z+4

x=3

print y

e.e[z3]

e.e[x1]

e. if x >0 then e else

e. if x 0 then e else

e. e [x1, y , z] e. if x 0 then e else

e.e[y7] e.e[ye(z)+4]

e.e[x3]

e.e

[x0, y0, z0]

[x1, y0, z0]

Page 14: 1 Iterative Program Analysis Part I Mooly Sagiv msagiv/courses/pa.html Tel Aviv University 640-6706 Textbook: Principles of Program.

Iterative Computationz =3

x =1

while (x>0)

if (x=1)

y =7 y =z+4

x=3

print y

e.e[z3]

e.e[x1]

e. if x >0 then e else

e. if x 0 then e else

e. e [x1, y , z] e. if x 0 then e else

e.e[y7] e.e[ye(z)+4]

e.e[x3]

e.e

[x0, y0, z0]

[x0, y0, z3]

Page 15: 1 Iterative Program Analysis Part I Mooly Sagiv msagiv/courses/pa.html Tel Aviv University 640-6706 Textbook: Principles of Program.

Iterative Computationz =3

x =1

while (x>0)

if (x=1)

y =7 y =z+4

x=3

print y

e.e[z3]

e.e[x1]

e. if x >0 then e else

e. if x 0 then e else

e. e [x1, y , z] e. if x 0 then e else

e.e[y7] e.e[ye(z)+4]

e.e[x3]

e.e

[x0, y0, z0]

[x0, y0, z3]

[x1, y0, z3]

Page 16: 1 Iterative Program Analysis Part I Mooly Sagiv msagiv/courses/pa.html Tel Aviv University 640-6706 Textbook: Principles of Program.

Iterative Computationz =3

x =1

while (x>0)

if (x=1)

y =7 y =z+4

x=3

print y

e.e[z3]

e.e[x1]

e. if x >0 then e else

e. if x 0 then e else

e. e [x1, y , z] e. if x 0 then e else

e.e[y7] e.e[ye(z)+4]

e.e[x3]

e.e

[x0, y0, z0]

[x0, y0, z3]

[x1, y0, z3]

[x1, y0, z3]

Page 17: 1 Iterative Program Analysis Part I Mooly Sagiv msagiv/courses/pa.html Tel Aviv University 640-6706 Textbook: Principles of Program.

Iterative Computationz =3

x =1

while (x>0)

if (x=1)

y =7 y =z+4

x=3

print y

e.e[z3]

e.e[x1]

e. if x >0 then e else

e. if x 0 then e else

e. e [x1, y , z] e. if x 0 then e else

e.e[y7] e.e[ye(z)+4]

e.e[x3]

e.e

[x0, y0, z0]

[x0, y0, z3]

[x1, y0, z3]

[x1, y0, z3]

[x1, y0, z3]

Page 18: 1 Iterative Program Analysis Part I Mooly Sagiv msagiv/courses/pa.html Tel Aviv University 640-6706 Textbook: Principles of Program.

Iterative Computationz =3

x =1

while (x>0)

if (x=1)

y =7 y =z+4

x=3

print y

e.e[z3]

e.e[x1]

e. if x >0 then e else

e. if x 0 then e else

e. e [x1, y , z] e. if x 0 then e else

e.e[y7] e.e[ye(z)+4]

e.e[x3]

e.e

[x0, y0, z0]

[x0, y0, z3]

[x1, y0, z3]

[x1, y0, z3]

[x1, y0, z3]

[x1, y7, z3]

Page 19: 1 Iterative Program Analysis Part I Mooly Sagiv msagiv/courses/pa.html Tel Aviv University 640-6706 Textbook: Principles of Program.

Iterative Computationz =3

x =1

while (x>0)

if (x=1)

y =7 y =z+4

x=3

print y

e.e[z3]

e.e[x1]

e. if x >0 then e else

e. if x 0 then e else

e. e [x1, y , z] e. if x 0 then e else

e.e[y7] e.e[ye(z)+4]

e.e[x3]

e.e

[x0, y0, z0]

[x0, y0, z3]

[x1, y0, z3]

[x1, y0, z3]

[x1, y0, z3]

[x1, y7, z3]

[x3, y7, z3]

Page 20: 1 Iterative Program Analysis Part I Mooly Sagiv msagiv/courses/pa.html Tel Aviv University 640-6706 Textbook: Principles of Program.

Iterative Computationz =3

x =1

while (x>0)

if (x=1)

y =7 y =z+4

x=3

print y

e.e[z3]

e.e[x1]

e. if x >0 then e else

e. if x 0 then e else

e. e [x1, y , z] e. if x 0 then e else

e.e[y7] e.e[ye(z)+4]

e.e[x3]

e.e

[x0, y0, z0]

[x0, y0, z3]

[x, y0, z3]

[x1, y0, z3]

[x1, y0, z3]

[x1, y7, z3]

[x3, y7, z3]

Page 21: 1 Iterative Program Analysis Part I Mooly Sagiv msagiv/courses/pa.html Tel Aviv University 640-6706 Textbook: Principles of Program.

Iterative Computationz =3

x =1

while (x>0)

if (x=1)

y =7 y =z+4

x=3

print y

e.e[z3]

e.e[x1]

e. if x >0 then e else

e. if x 0 then e else

e. e [x1, y , z] e. if x 0 then e else

e.e[y7] e.e[ye(z)+4]

e.e[x3]

e.e

[x0, y0, z0]

[x0, y0, z3]

[x, y0, z3]

[x, y0, z3]

[x1, y0, z3]

[x1, y7, z3]

[x3, y7, z3]

Page 22: 1 Iterative Program Analysis Part I Mooly Sagiv msagiv/courses/pa.html Tel Aviv University 640-6706 Textbook: Principles of Program.

Iterative Computationz =3

x =1

while (x>0)

if (x=1)

y =7 y =z+4

x=3

print y

e.e[z3]

e.e[x1]

e. if x >0 then e else

e. if x 0 then e else

e. e [x1, y , z] e. if x 0 then e else

e.e[y7] e.e[ye(z)+4]

e.e[x3]

e.e

[x0, y0, z0]

[x0, y0, z3]

[x, y0, z3]

[x, y0, z3]

[x, y0, z3]

[x1, y7, z3]

[x3, y7, z3]

Page 23: 1 Iterative Program Analysis Part I Mooly Sagiv msagiv/courses/pa.html Tel Aviv University 640-6706 Textbook: Principles of Program.

Iterative Computationz =3

x =1

while (x>0)

if (x=1)

y =7 y =z+4

x=3

print y

e.e[z3]

e.e[x1]

e. if x >0 then e else

e. if x 0 then e else

e. e [x1, y , z] e. if x 0 then e else

e.e[y7] e.e[ye(z)+4]

e.e[x3]

e.e

[x0, y0, z0]

[x0, y0, z3]

[x, y0, z3]

[x, y0, z3]

[x, y0, z3]

[x1, y7, z3]

[x3, y7, z3]

[x, y0, z3]

Page 24: 1 Iterative Program Analysis Part I Mooly Sagiv msagiv/courses/pa.html Tel Aviv University 640-6706 Textbook: Principles of Program.

Iterative Computationz =3

x =1

while (x>0)

if (x=1)

y =7 y =z+4

x=3

print y

e.e[z3]

e.e[x1]

e. if x >0 then e else

e. if x 0 then e else

e. e [x1, y , z] e. if x 0 then e else

e.e[y7] e.e[ye(z)+4]

e.e[x3]

e.e

[x0, y0, z0]

[x0, y0, z3]

[x, y0, z3]

[x, y0, z3]

[x1, y0, z3]

[x, y7, z3]

[x3, y7, z3]

[x, y0, z3]

Page 25: 1 Iterative Program Analysis Part I Mooly Sagiv msagiv/courses/pa.html Tel Aviv University 640-6706 Textbook: Principles of Program.

Mathematical Background Declaratively define

– The result of the analysis

– The exact solution

– Allow comparison

Page 26: 1 Iterative Program Analysis Part I Mooly Sagiv msagiv/courses/pa.html Tel Aviv University 640-6706 Textbook: Principles of Program.

Posets A partial ordering is a binary relation

: L L {false, true} – For all l L : l l (Reflexive)– For all l1, l2, l3 L : l1 l2, l2 l3 l1 l3 (Transitive)– For all l1, l2 L : l1 l2, l2 l1 l1 = l2

(Anti-Symmetric) Denoted by (L, ) In program analysis

– l1 l2 l1 is more precise than l2 l1 represents fewer concrete states than l2

Examples– Total orders (N, )– Powersets (P(S), )– Powersets (P(S), ) – Constant propagation

Page 27: 1 Iterative Program Analysis Part I Mooly Sagiv msagiv/courses/pa.html Tel Aviv University 640-6706 Textbook: Principles of Program.

Posets More notations

– l1 l2 l2 l1

– l1 l2 l1 l2 l1 l2

– l1 l2 l2 l1

Page 28: 1 Iterative Program Analysis Part I Mooly Sagiv msagiv/courses/pa.html Tel Aviv University 640-6706 Textbook: Principles of Program.

Upper and Lower Bounds Consider a poset (L, ) A subset L’ L has a lower bound l L if for all l’ L’ : l

l’ A subset L’ L has an upper bound u L if for all l’ L’ :

l’ u A greatest lower bound of a subset L’ L is a lower bound

l0 L such that l l0 for any lower bound l of L’ A lowest upper bound of a subset L’ L is an upper bound

u0 L such that u0 u for any upper bound u of L’ For every subset L’ L:

– The greatest lower bound of L’ is unique if at all exists L’ (meet) a b

– The lowest upper bound of L’ is unique if at all exists» L’ (join) ab

Page 29: 1 Iterative Program Analysis Part I Mooly Sagiv msagiv/courses/pa.html Tel Aviv University 640-6706 Textbook: Principles of Program.

Complete Lattices A poset (L, ) is a complete lattice if

every subset has least and upper bounds L = (L, ) = (L, , , , , )

= = L = L =

Examples– Total orders (N, )– Powersets (P(S), )– Powersets (P(S), ) – Constant propagation

Page 30: 1 Iterative Program Analysis Part I Mooly Sagiv msagiv/courses/pa.html Tel Aviv University 640-6706 Textbook: Principles of Program.

Complete Lattices Lemma For every poset (L, ) the

following conditions are equivalent– L is a complete lattice– Every subset of L has a least upper bound– Every subset of L has a greatest lower bound

Page 31: 1 Iterative Program Analysis Part I Mooly Sagiv msagiv/courses/pa.html Tel Aviv University 640-6706 Textbook: Principles of Program.

Cartesian Products A complete lattice

(L1, 1) = (L1, , 1, 1, 1, 1) A complete lattice

(L2, 2) = (, , 2, 2, 2, 2) Define a Poset L = (L1 L2 , ) where

– (x1, x2) (y1, y2) if » x1 y1 and» x2 y2

L is a complete lattice

Page 32: 1 Iterative Program Analysis Part I Mooly Sagiv msagiv/courses/pa.html Tel Aviv University 640-6706 Textbook: Principles of Program.

Finite Maps A complete lattice

(L1, 1) = (L1, , 1, 1, 1, 1) A finite set V Define a Poset L = (V L1 , ) where

– e1 e2 if for all v V » e1v e2v

L is a complete lattice

Page 33: 1 Iterative Program Analysis Part I Mooly Sagiv msagiv/courses/pa.html Tel Aviv University 640-6706 Textbook: Principles of Program.

Chains A subset Y L in a poset (L, ) is a chain if every two

elements in Y are ordered– For all l1, l2 Y: l1 l2 or l2 l1

An ascending chain is a sequence of values– l1 l2 l3 …

A strictly ascending chain is a sequence of values– l1 l2 l3…

A descending chain is a sequence of values– l1 l2 l3 …

A strictly descending chain is a sequence of values– l1 l2 l3 …

L has a finite height if every chain in L is finite Lemma A poset (L, ) has finite height if and only if

every strictly decreasing and strictly increasing chains are finite

Page 34: 1 Iterative Program Analysis Part I Mooly Sagiv msagiv/courses/pa.html Tel Aviv University 640-6706 Textbook: Principles of Program.

Monotone Functions

A poset (L, ) A function f: L L is monotone if for every

l1, l2 L:

– l1 l2 f(l1 ) f(l2 )

Page 35: 1 Iterative Program Analysis Part I Mooly Sagiv msagiv/courses/pa.html Tel Aviv University 640-6706 Textbook: Principles of Program.

Galois Connections Lattices C and A and functions : C A and : A C

The pair of functions (, ) form Galois connection if and are monotone a A

( (a)) a c C

» c ((C))

Alternatively if: c C a A (c) a iff c (a)

and uniquely determine each other

Page 36: 1 Iterative Program Analysis Part I Mooly Sagiv msagiv/courses/pa.html Tel Aviv University 640-6706 Textbook: Principles of Program.

Fixed Points A monotone function f: L L where

(L, , , , , ) is a complete lattice Fix(f) = { l: l L, f(l) = l} Red(f) = {l: l L, f(l) l} Ext(f) = {l: l L, l f(l)}

– l1 l2 f(l1 ) f(l2 ) Tarski’s Theorem 1955: if f is monotone

then:– lfp(f) = Fix(f) = Red(f) Fix(f)– gfp(f) = Fix(f) = Ext(f) Fix(f)

f()

f()

f2()

f2()

Fix(f)

Ext(f)

Red(f)

gfp(f)

lfp(f)

Page 37: 1 Iterative Program Analysis Part I Mooly Sagiv msagiv/courses/pa.html Tel Aviv University 640-6706 Textbook: Principles of Program.

Chaotic Iterations A lattice L = (L, , , , , ) with finite strictly increasing chains Ln = L L … L A monotone function f: Ln Ln

Compute lfp(f) The simultaneous least fixed of the system {x[i] = fi(x) : 1 i n }

x := (, , …, )

while (f(x) x ) do

x := f(x)

for i :=1 to n do x[i] =

WL = {1, 2, …, n}

while (WL ) do

select and remove an element i WL

new := fi(x)

if (new x[i]) then

x[i] := new;

Add all the indexes that directly depends on i to WL

Page 38: 1 Iterative Program Analysis Part I Mooly Sagiv msagiv/courses/pa.html Tel Aviv University 640-6706 Textbook: Principles of Program.

Chaotic Iterations Ln = L L … L A monotone function f: Ln Ln

Compute lfp(f) The simultaneous least fixed of the system {x[i] = fi(x) : 1 i n } Minimum number of non-constant Maximum number of

for i :=1 to n do x[i] =

WL = {1, 2, …, n}

while (WL ) do

select and remove an element i WL

new := fi(x)

if (new x[i]) then

x[i] := new;

Add all the indexes that directly depends on i to WL

Page 39: 1 Iterative Program Analysis Part I Mooly Sagiv msagiv/courses/pa.html Tel Aviv University 640-6706 Textbook: Principles of Program.

Specialized Chaotic Iterations

Chaotic(G(V, E): Graph, s: Node, L: Lattice, : L, f: E (L L) ){

for each v in V to n do dfentry[v] :=

df[v] =

WL = {s}

while (WL ) do

select and remove an element u WL

for each v, such that. (u, v) E do

temp = f(e)(dfentry[u])

new := dfentry(v) temp

if (new dfentry[v]) then

dfentry[v] := new;

WL := WL {v}

Page 40: 1 Iterative Program Analysis Part I Mooly Sagiv msagiv/courses/pa.html Tel Aviv University 640-6706 Textbook: Principles of Program.

z =3

x =1

while (x>0)

if (x=1)

y =7 y =z+4

x=3

print y

e.e[z3]

e.e[x1]

e. if x >0 then e else

e. if x 0 then e else

e. e [x1, y , z] e. if x 0 then e else

e.e[y7] e.e[ye(z)+4]

e.e[x3]

e.e

1

2

3

4

5 6

7

8

[x0, y0, z0]

WLdfentry]v]

{1}

{2}df[2]:=[x0, y0, z3]

{3}df[3]:=[x1, y0, z3]

{4}df[4]:=[x1, y0, z3]

{5}df[5]:=[x1, y0, z3]

{7}df[7]:=[x1, y7, z3]

{8}df[8]:=[x3, y7, z3]

{3}df[3]:=[x, y, z3]

{4}df[4]:=[x, y, z3]

{5,6}df[5]:=[x1, y, z3]

{6,7}df[6]:=[x, y, z3]

{7}df[7]:=[x, y7, z3]

Page 41: 1 Iterative Program Analysis Part I Mooly Sagiv msagiv/courses/pa.html Tel Aviv University 640-6706 Textbook: Principles of Program.

Specialized Chaotic IterationsSystem of Equations

S =

dfentry[s] =

dfentry[v] = {f(u, v) (dfentry[u]) | (u, v) E }

FS:Ln Ln

FS (X)[s] =

FS(X)[v] = {f(u, v)(X[u]) | (u, v) E }

lfp(S) = lfp(FS)

Page 42: 1 Iterative Program Analysis Part I Mooly Sagiv msagiv/courses/pa.html Tel Aviv University 640-6706 Textbook: Principles of Program.

Complexity of Chaotic Iterations

Parameters:– n the number of CFG nodes

– k is the maximum outdegree of edges

– A lattice of height h

– c is the maximum cost of» applying f(e)

» L comparisons

ComplexityO(n * h * c * k)

Page 43: 1 Iterative Program Analysis Part I Mooly Sagiv msagiv/courses/pa.html Tel Aviv University 640-6706 Textbook: Principles of Program.

Soundness

Every detected constant is indeed such Every error will be detected The least fixed points represents all occurring

runtime states

Page 44: 1 Iterative Program Analysis Part I Mooly Sagiv msagiv/courses/pa.html Tel Aviv University 640-6706 Textbook: Principles of Program.

Completeness

Every constant is indeed detected as such Every detected error is real Every state represented by the least fixed is

reachable for some input

Page 45: 1 Iterative Program Analysis Part I Mooly Sagiv msagiv/courses/pa.html Tel Aviv University 640-6706 Textbook: Principles of Program.

The Abstract Interpretation Technique

The foundation of program analysis Goals

– Establish soundness of (find faults in) a given program analysis algorithm

– Design new program analysis algorithms

The main ideas:– Relate each step in the algorithm to a step in a

structural operational semantics

– Establish global correctness using a general theorem

Not limited to a particular form of analysis

Page 46: 1 Iterative Program Analysis Part I Mooly Sagiv msagiv/courses/pa.html Tel Aviv University 640-6706 Textbook: Principles of Program.

Soundness in Constant Propagation

Every detected constant is indeed such May include fewer constants May miss At every CFG node l

All constants in dfentry(l) are indeed constants

At every CFG node l dfentry(l) “represents” all the possible concrete states arising when the structural operational semantics reaches l

Page 47: 1 Iterative Program Analysis Part I Mooly Sagiv msagiv/courses/pa.html Tel Aviv University 640-6706 Textbook: Principles of Program.

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 soundw.r.t. the collecting semantics

(Global correctness) Conclude that the analysis is sound CC1976

(lfp(S)[v, entry]) REACH[v, entry]

Page 48: 1 Iterative Program Analysis Part I Mooly Sagiv msagiv/courses/pa.html Tel Aviv University 640-6706 Textbook: Principles of Program.

Structural Semantics for While[asssos] <x := a, s> s[x Aas]

[skipsos] <skip, s> s

[comp1sos] <S1 , s> <S’1, s’>

<S1; S2, s> < S’1; S2, s’>

axioms

rules

[comp2sos] <S1 , s> s’

<S1; S2, s> < S2, s’>

Page 49: 1 Iterative Program Analysis Part I Mooly Sagiv msagiv/courses/pa.html Tel Aviv University 640-6706 Textbook: Principles of Program.

Structural Semantics for Whileif construct

[ifttsos] <if b then S1 else S2, s> <S1, s> if Bbs=tt

[ifffos] <if b then S1 else S2, s> <S2, s> if Bbs=ff

Page 50: 1 Iterative Program Analysis Part I Mooly Sagiv msagiv/courses/pa.html Tel Aviv University 640-6706 Textbook: Principles of Program.

Structural Semantics for Whilewhile construct

[whilesos] <while b do S, s> <if b then (S; while b do S) else skip, s>

Page 51: 1 Iterative Program Analysis Part I Mooly Sagiv msagiv/courses/pa.html Tel Aviv University 640-6706 Textbook: Principles of Program.

Collecting Semantics

The input state is not known at compile-time

“Collect” all the states for all possible inputs to the program

No lost of precision

Page 52: 1 Iterative Program Analysis Part I Mooly Sagiv msagiv/courses/pa.html Tel Aviv University 640-6706 Textbook: Principles of Program.

A Simple Example Program

z = 3

x = 1

while (x > 0) (

if (x = 1) then y = 7

else y = z + 4

x = 3

print y

)

{[x0, y0, z0]}

{[x1, y0, z3]}

{[x1, y0, z3], [x3, y0, z3],}

{[x0, y0, z3]}

{[x1, y7, z3], [x3, y7, z3]}

{[x1, y7, z3], [x3, y7, z3]}

{[x3, y7, z3]}

{[x3, y7, z3]}

Page 53: 1 Iterative Program Analysis Part I Mooly Sagiv msagiv/courses/pa.html Tel Aviv University 640-6706 Textbook: Principles of Program.

Another Example

x= 0

while (true) do

x = x +1

Page 54: 1 Iterative Program Analysis Part I Mooly Sagiv msagiv/courses/pa.html Tel Aviv University 640-6706 Textbook: Principles of Program.

An “Iterative” Definition

Generate a system of monotone equations The least solution is well-defined The least solution is the collecting interpretation But may not be computable

Page 55: 1 Iterative Program Analysis Part I Mooly Sagiv msagiv/courses/pa.html Tel Aviv University 640-6706 Textbook: Principles of Program.

Equations Generated for Collecting Interpretation

Equations for elementary statements– [skip]

CSexit(1) = CSentry(l) – [b]

CSexit(1) = {: CSentry(l), b=tt} – [x := a]

CSexit(1) = {(s[x Aas]) | s CSentry(l)} Equations for control flow constructs

CSentry(l) = CSexit(l’) l’ immediately precedes l in the control flow graph

An equation for the entryCSentry(1) = { | Var* Z}

Page 56: 1 Iterative Program Analysis Part I Mooly Sagiv msagiv/courses/pa.html Tel Aviv University 640-6706 Textbook: Principles of Program.

Specialized Chaotic IterationsSystem of Equations (Collecting Semantics)

S =

CSentry[s] ={0}

CSentry[v] = {f(e)(CSentry[u]) | (u, v) E }

where f(e) = X. {st(e) | X} for atomic statements

f(e) = X.{ | b(e) =tt }

FS:Ln Ln

Fs(X)[v] = {f(e)[u] | (u, v) E }

lfp(S) = lfp(FS)

Page 57: 1 Iterative Program Analysis Part I Mooly Sagiv msagiv/courses/pa.html Tel Aviv University 640-6706 Textbook: Principles of Program.

The Least Solution

2n sets of equationsCSentry(1), …, CSentry (n), CSexit(1), …, CSexit (n)

Can be written in vectorial form

The least solution lfp(Fcs) is well-defined

Every component is minimal Since Fcs is monotone such a solution always exists

CSentry(v) = {s|s0| <P, s0 > * (S’, s)), init(S’)=v}

Simplify the soundness criteria

)CS(CS csF

Page 58: 1 Iterative Program Analysis Part I Mooly Sagiv msagiv/courses/pa.html Tel Aviv University 640-6706 Textbook: Principles of Program.

Abstract (Conservative) interpretation

abstract representation

Set of states

concretization

Abstractsemantics

statement s abstract representation

concretization

Operational semantics

statement sSet of states Set of states

Page 59: 1 Iterative Program Analysis Part I Mooly Sagiv msagiv/courses/pa.html Tel Aviv University 640-6706 Textbook: Principles of Program.

Abstract (Conservative) interpretation

abstract representation

Set of states

abstraction

Abstractsemantics

statement s abstract representation

abstraction

Operational semantics

statement sSet of states

abstract representation

Page 60: 1 Iterative Program Analysis Part I Mooly Sagiv msagiv/courses/pa.html Tel Aviv University 640-6706 Textbook: Principles of Program.

The Abstraction Function Map collecting states into constants The abstraction of an individual state

CP:[Var* Z] [Var* Z{, }]CP() =

The abstraction of set of states CP:P([Var* Z]) [Var* Z{, }] CP (CS) = { CP () | CS} = {| CS}

Soundness CP (CSentry (v)) dfentry(v)

Completeness

Page 61: 1 Iterative Program Analysis Part I Mooly Sagiv msagiv/courses/pa.html Tel Aviv University 640-6706 Textbook: Principles of Program.

The Concretization Function Map constants into collecting states The formal meaning of constants The concretization

CP: [Var* Z{, }] P([Var* Z]) CP (df) = {| CP () df} = { | df}

Soundness CSentry (v) CP (dfentry(v))

Optimality

Page 62: 1 Iterative Program Analysis Part I Mooly Sagiv msagiv/courses/pa.html Tel Aviv University 640-6706 Textbook: Principles of Program.

Galois Connection

CP is monotone

CP is monotone

df [Var* Z{, }] CP( CP (df)) df

c P([Var* Z])

– c CP CP ( CP(C))

Page 63: 1 Iterative Program Analysis Part I Mooly Sagiv msagiv/courses/pa.html Tel Aviv University 640-6706 Textbook: Principles of Program.

Local Concrete Semantics

For every atomic statement S S : [Var* Z] [Var* Z]

x := a] s = s[x Aas] skip] s = s

For Boolean conditions …

Page 64: 1 Iterative Program Analysis Part I Mooly Sagiv msagiv/courses/pa.html Tel Aviv University 640-6706 Textbook: Principles of Program.

Local Abstract Semantics

For every atomic statement S S # :Var* L Var* L

x := a #(e) = e [x a #(e) skip # (e) = e

For Booleans …

Page 65: 1 Iterative Program Analysis Part I Mooly Sagiv msagiv/courses/pa.html Tel Aviv University 640-6706 Textbook: Principles of Program.

Local Soundness For every atomic statement S show one of the

following CP({S | CS } S# ( CP(CS))

– {S | CP (df)} CP (S# (df))

({S | CP (df)}) S# (df)

The above condition implies global soundness [Cousot & Cousot 1976] (CSentry (l)) dfentry(l) CSentry (l) (dfentry(l))

Page 66: 1 Iterative Program Analysis Part I Mooly Sagiv msagiv/courses/pa.html Tel Aviv University 640-6706 Textbook: Principles of Program.

Lemma 1

Consider a lattice L.

f: L L is monotone iff for all X L: {f(z) | z X } f({z | z X })

Page 67: 1 Iterative Program Analysis Part I Mooly Sagiv msagiv/courses/pa.html Tel Aviv University 640-6706 Textbook: Principles of Program.

Assignments in constant propagation

Monotone– df1 df2 x :=e)#df1 ) x :=e)#df2(

Local Soundness ({ x :=e | CS } x :=e # ((CS))

Page 68: 1 Iterative Program Analysis Part I Mooly Sagiv msagiv/courses/pa.html Tel Aviv University 640-6706 Textbook: Principles of Program.

Soundness Theorem(1)

1. Let (, ) form Galois connection from C to A

2. f: C C be a monotone function

3. f# : A A be a monotone function

4. aA: f((a)) (f#(a))

lfp(f) (lfp(f#))

(lfp(f)) lfp(f#)

Page 69: 1 Iterative Program Analysis Part I Mooly Sagiv msagiv/courses/pa.html Tel Aviv University 640-6706 Textbook: Principles of Program.

Soundness Theorem(2)

1. Let (, ) form Galois connection from C to A

2. f: C C be a monotone function

3. f# : A A be a monotone function

4. cC: (f(c)) f#((c))

(lfp(f)) lfp(f#)

lfp(f) (lfp(f#))

Page 70: 1 Iterative Program Analysis Part I Mooly Sagiv msagiv/courses/pa.html Tel Aviv University 640-6706 Textbook: Principles of Program.

Soundness Theorem(3)

1. Let (, ) form Galois connection from C to A

2. f: C C be a monotone function

3. f# : A A be a monotone function

4. aA: (f((a))) f#(a)

(lfp(f)) lfp(f#)

lfp(f) (lfp(f#))

Page 71: 1 Iterative Program Analysis Part I Mooly Sagiv msagiv/courses/pa.html Tel Aviv University 640-6706 Textbook: Principles of Program.

Proof of Soundness (Summary)

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 soundw.r.t. the collecting semantics

(Global correctness) Conclude that the analysis is sound

Page 72: 1 Iterative Program Analysis Part I Mooly Sagiv msagiv/courses/pa.html Tel Aviv University 640-6706 Textbook: Principles of Program.

Best (Conservative) interpretation

abstract representation

Set of states

concretization

Abstractsemantics

statement s abstract representation

abstraction

Operational semantics

statement sSet of states

concretization

Set of states

Page 73: 1 Iterative Program Analysis Part I Mooly Sagiv msagiv/courses/pa.html Tel Aviv University 640-6706 Textbook: Principles of Program.

Induced Analysis (Relatively Optimal)

It is sometimes possible to show that a given analysis is not only sound but optimal w.r.t. the chosen abstraction – but not necessarily optimal!

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

Page 74: 1 Iterative Program Analysis Part I Mooly Sagiv msagiv/courses/pa.html Tel Aviv University 640-6706 Textbook: Principles of Program.

Example Dataflow Problem Formal available expression analysis Find out which expressions are available at a given

program point Example program

x = y + t z = y + r while (…) { t = t + (y + r) }

Lattice Galois connection Basic statements Soundness

Page 75: 1 Iterative Program Analysis Part I Mooly Sagiv msagiv/courses/pa.html Tel Aviv University 640-6706 Textbook: Principles of Program.

Example: May-Be-Garbage A variable x may-be-garbage at a program point v

if there exists a execution path leading to v in which x’s value is unpredictable:– Was not assigned

– Was assigned using an unpredictable expression

Lattice Galois connection Basic statements Soundness

Page 76: 1 Iterative Program Analysis Part I Mooly Sagiv msagiv/courses/pa.html Tel Aviv University 640-6706 Textbook: Principles of Program.

Points-To Analysis Determine if a pointer variable p may point

to q on some path leading to a program point

“Adapt” other optimizations – Constant propagation

x = 5;*p = 7 ;… x …

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

Page 77: 1 Iterative Program Analysis Part I Mooly Sagiv msagiv/courses/pa.html Tel Aviv University 640-6706 Textbook: Principles of Program.

The PWhile Programming Language Abstract Syntax

a := x | *x | &x | n | a1 opa a2

b := true | false | not b | b1 opb b2 | a1 opr a2

S := x := a | *x := a | skip | S1 ; S2 | if b then S1 else S2 | while b do S

Page 78: 1 Iterative Program Analysis Part I Mooly Sagiv msagiv/courses/pa.html Tel Aviv University 640-6706 Textbook: Principles of Program.

Concrete Semantics 1 for PWhile

For every atomic statement S

S : States1 States1

x := a ()=[loc(x) Aa ]

x := &y ()

x := *y ()

x := y ()

*x := y ()

State1= [LocLocZ]

Page 79: 1 Iterative Program Analysis Part I Mooly Sagiv msagiv/courses/pa.html Tel Aviv University 640-6706 Textbook: Principles of Program.

Points-To Analysis Lattice Lpt =

Galois connection

Page 80: 1 Iterative Program Analysis Part I Mooly Sagiv msagiv/courses/pa.html Tel Aviv University 640-6706 Textbook: Principles of Program.

Abstract Semantics for PWhile

•For every atomic statement S

S #: P(Var* Var*) P(Var* Var*)

x := &y #

x := *y #

x := y #

*x := y #

Page 81: 1 Iterative Program Analysis Part I Mooly Sagiv msagiv/courses/pa.html Tel Aviv University 640-6706 Textbook: Principles of Program.

t := &a; y := &b;

z := &c;

if x> 0; then p:= &y;

else p:= &z;

*p := t;

Page 82: 1 Iterative Program Analysis Part I Mooly Sagiv msagiv/courses/pa.html Tel Aviv University 640-6706 Textbook: Principles of Program.

/* */ t := &a; /* {(t, a)}*/ /* {(t, a)}*/ y := &b; /* {(t, a), (y, b) }*/

/* {(t, a), (y, b)}*/ z := &c; /* {(t, a), (y, b), (z, c) }*/

if x> 0; then p:= &y; /* {(t, a), (y, b), (z, c), (p, y)}*/

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)}*/

Page 83: 1 Iterative Program Analysis Part I Mooly Sagiv msagiv/courses/pa.html Tel Aviv University 640-6706 Textbook: Principles of Program.

Flow insensitive points-to-analysisSteengard 1996

Ignore control flow One set of points-to per program Can be represented as a directed graph Conservative approximation

– Accumulate pointers

Can be computed in almost linear time

Page 84: 1 Iterative Program Analysis Part I Mooly Sagiv msagiv/courses/pa.html Tel Aviv University 640-6706 Textbook: Principles of Program.

t := &a; y := &b;

z := &c;

if x> 0; then p:= &y;

else p:= &z;

*p := t;

Page 85: 1 Iterative Program Analysis Part I Mooly Sagiv msagiv/courses/pa.html Tel Aviv University 640-6706 Textbook: Principles of Program.

Precision

We cannot usually have (CS) = DF

on all programs

But can we say something about precision in all programs?

Page 86: 1 Iterative Program Analysis Part I Mooly Sagiv msagiv/courses/pa.html Tel Aviv University 640-6706 Textbook: Principles of Program.

The Join-Over-All-Paths (JOP)

Let paths(v) denote the potentially infinite set paths from start to v (written as sequences of labels)

For a sequence of edges [e1, e2, …, en] definef [e1, e2, …, en]: L L by composing the effects of basic blocksf [e1, e2, …, en](l) = f(en) (… (f(e2) (f(e1) (l)) …)

JOP[v] = {f[e1, e2, …,en]() [e1, e2, …, en] paths(v)}

Page 87: 1 Iterative Program Analysis Part I Mooly Sagiv msagiv/courses/pa.html Tel Aviv University 640-6706 Textbook: Principles of Program.

JOP vs. Least Solution

The DF solution obtained by Chaotic iteration satisfies for every l: – JOP[v] DFentry(v)

A function f is additive (distributive) if – f({x| x X}) = {f(x) | X}

If every fl is additive (distributive) for all the nodes v– JOP[v] = DFentry(v)

Page 88: 1 Iterative Program Analysis Part I Mooly Sagiv msagiv/courses/pa.html Tel Aviv University 640-6706 Textbook: Principles of Program.

Conclusions

Chaotic iterations is a powerful technique Easy to implement Rather precise But expensive

– More efficient methods exist for structured programs

Abstract interpretation relates runtime semantics and static information

The concrete semantics serves as a tool in designing abstractions– More intuition will be given in the sequel


Recommended