108
Yue Li and Tian Tan Static Program Analysis 2020 Spring

Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

  • Upload
    others

  • View
    8

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

Yue Li and Tian Tan

Static Program Analysis

2020 Spring

Page 2: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

Nanjing University

Tian Tan

2020

Interprocedural Analysis

Static Program Analysis

Page 3: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

Contents

3

1. Motivation

2. Call Graph Construction (CHA)

3. Interprocedural Control-Flow Graph

4. Interprocedural Data-Flow Analysis

Tian Tan @ Nanjing University

Page 4: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

Contents

4

1. Motivation

2. Call Graph Construction (CHA)

3. Interprocedural Control-Flow Graph

4. Interprocedural Data-Flow Analysis

Tian Tan @ Nanjing University

Page 5: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

void foo() {int n = bar(42);

}

int bar(int x) {int y = x + 1;return 10;

}

Motivation of Interprocedural Analysis

5

So far, all analyses we learnt are intraprocedural. How to deal with method calls?

Constant Propagation

Tian Tan @ Nanjing University

Page 6: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

void foo() {int n = bar(42);

}

int bar(int x) {int y = x + 1;return 10;

}

Motivation of Interprocedural Analysis

6

So far, all analyses we learnt are intraprocedural. How to deal with method calls?• Make the most conservative assumption for

method calls, for safe-approximation

Constant Propagation

Tian Tan @ Nanjing University

Page 7: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

void foo() {int n = bar(42);

}

int bar(int x) {int y = x + 1;return 10;

}

Motivation of Interprocedural Analysis

7

So far, all analyses we learnt are intraprocedural. How to deal with method calls?• Make the most conservative assumption for

method calls, for safe-approximation

• Source of imprecision

➢ x = NAC, y = NAC➢ n = NAC

Constant Propagation

Tian Tan @ Nanjing University

Page 8: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

void foo() {int n = bar(42);

}

int bar(int x) {int y = x + 1;return 10;

}

Motivation of Interprocedural Analysis

8

So far, all analyses we learnt are intraprocedural. How to deal with method calls?• Make the most conservative assumption for

method calls, for safe-approximation

• Source of imprecision

➢ x = NAC, y = NAC➢ n = NAC

Constant Propagation

Ret

urn

ed

ges

For better precision, we need Interproceduralanalysis: propagate data-flow information along interprocedural control-flow edges(i.e., call and return edges)

Tian Tan @ Nanjing University

Page 9: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

void foo() {int n = bar(42);

}

int bar(int x) {int y = x + 1;return 10;

}

Motivation of Interprocedural Analysis

9

So far, all analyses we learnt are intraprocedural. How to deal with method calls?• Make the most conservative assumption for

method calls, for safe-approximation

• Source of imprecision

➢ x = NAC, y = NAC➢ n = NAC

Constant Propagation

Ret

urn

ed

ges

Tian Tan @ Nanjing University

For better precision, we need Interproceduralanalysis: propagate data-flow information along interprocedural control-flow edges(i.e., call and return edges)➢ x = 42, y = 43➢ n = 10

Page 10: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

To perform interprocedural analysis,we need call graph

void foo() {int n = bar(42);

}

int bar(int x) {int y = x + 1;return 10;

}

Motivation of Interprocedural Analysis

10

So far, all analyses we learnt are intraprocedural. How to deal with method calls?• Make the most conservative assumption for

method calls, for safe-approximation

• Source of imprecision

➢ x = NAC, y = NAC➢ n = NAC

Constant Propagation

Ret

urn

ed

ges

Tian Tan @ Nanjing University

For better precision, we need Interproceduralanalysis: propagate data-flow information along interprocedural control-flow edges(i.e., call and return edges)➢ x = 42, y = 43➢ n = 10

Page 11: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

Contents

11

1. Motivation

2. Call Graph Construction (CHA)

3. Interprocedural Control-Flow Graph

4. Interprocedural Data-Flow Analysis

Tian Tan @ Nanjing University

Page 12: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

Call Graph

12

A representation of calling relationships in the program

• Essentially, a call graph is a set of call edges from call-sites to their target methods (callees)

Tian Tan @ Nanjing University

Page 13: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

Call Graph

13

foo()

bar(); baz(123);

bar()

baz(666)baz(int)

A representation of calling relationships in the program

• Essentially, a call graph is a set of call edges from call-sites to their target methods (callees)

Tian Tan @ Nanjing University

void foo() {bar();baz(123);

}

void bar(int x) {baz(666);

}

void baz() { }

Page 14: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

Applications of Call Graph

• Foundation of all interprocedural analyses

• Program optimization

• Program understanding

• Program debugging

• Program testing

• And many more …14Tian Tan @ Nanjing University

Page 15: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

Call Graph Construction for OOPLs (focus on Java)

• Class hierarchy analysis (CHA)

• Rapid type analysis (RTA)

• Variable type analysis (VTA)

• Pointer analysis (k-CFA)

15Tian Tan @ Nanjing University

Page 16: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

Call Graph Construction for OOPLs (focus on Java)

• Class hierarchy analysis (CHA)

• Rapid type analysis (RTA)

• Variable type analysis (VTA)

• Pointer analysis (k-CFA)

16

More precise

More efficient

Tian Tan @ Nanjing University

Page 17: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

Call Graph Construction for OOPLs (focus on Java)

• Class hierarchy analysis (CHA)

• Rapid type analysis (RTA)

• Variable type analysis (VTA)

• Pointer analysis (k-CFA)

17

More precise

More efficient

nextlectures

this lecture

Tian Tan @ Nanjing University

Page 18: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

Method Calls (Invocations) in Java

18

Static call Special call Virtual call

Instruction invokestatic invokespecial invokeinterfaceinvokevirtual

Tian Tan @ Nanjing University

Page 19: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

Method Calls (Invocations) in Java

19

Static call Special call Virtual call

Instruction invokestatic invokespecial invokeinterfaceinvokevirtual

Receiver objects × ✓ ✓

Target methods • Static methods • Constructors• Private instance

methods• Superclass instance

methods

• Other instance methods

Tian Tan @ Nanjing University

Page 20: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

Method Calls (Invocations) in Java

20

Static call Special call Virtual call

Instruction invokestatic invokespecial invokeinterfaceinvokevirtual

Receiver objects × ✓ ✓

Target methods • Static methods • Constructors• Private instance

methods• Superclass instance

methods

• Other instance methods

#Target methods 1 1 ≥1 (polymorphism)

Determinacy Compile-time Compile-time Run-time

Tian Tan @ Nanjing University

Page 21: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

Method Calls (Invocations) in Java

21

Static call Special call Virtual call

Instruction invokestatic invokespecial invokeinterfaceinvokevirtual

Receiver objects × ✓ ✓

Target methods • Static methods • Constructors• Private instance

methods• Superclass instance

methods

• Other instance methods

#Target methods 1 1 ≥1 (polymorphism)

Determinacy Compile-time Compile-time Run-time

Key to call graph construction for OOPLs

Tian Tan @ Nanjing University

Page 22: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

Method Dispatch of Virtual Calls

22

During run-time, a virtual call is resolved based on1. type of the receiver object (pointed by o)2. method signature at the call site

o1.foo(…)2;

Tian Tan @ Nanjing University

Page 23: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

Method Dispatch of Virtual Calls

23

During run-time, a virtual call is resolved based on1. type of the receiver object (pointed by o)2. method signature at the call site

In this lecture, a signature acts as an identifier of a method• Signature = class type + method name + descriptor• Descriptor = return type + parameter types

o1.foo(…)2;

Tian Tan @ Nanjing University

Page 24: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

Method Dispatch of Virtual Calls

24

During run-time, a virtual call is resolved based on1. type of the receiver object (pointed by o)2. method signature at the call site

In this lecture, a signature acts as an identifier of a method

• Signature = class type + method name + descriptor• Descriptor = return type + parameter types

class C {T foo(P p, Q q, R r) { … }

}

<C: T foo(P,Q,R)>

o1.foo(…)2;

Tian Tan @ Nanjing University

Page 25: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

Method Dispatch of Virtual Calls

25

During run-time, a virtual call is resolved based on1. type of the receiver object (pointed by o)2. method signature at the call site

In this lecture, a signature acts as an identifier of a method

• Signature = class type + method name + descriptor• Descriptor = return type + parameter types

class C {T foo(P p, Q q, R r) { … }

}

<C: T foo(P,Q,R)>

o1.foo(…)2;

Tian Tan @ Nanjing University

C.foo(P,Q,R) for short

Page 26: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

Method Dispatch of Virtual Calls

26

During run-time, a virtual call is resolved based on1. type of the receiver object (pointed by o): 𝒄2. method signature at the call site: 𝒎

We define function Dispatch(𝑐,𝑚) to simulate the procedure of run-time method dispatch

<C: T foo(P,Q,R)>

Dispatch(𝑐,𝑚) =

𝑚′, if 𝑐 contains non-abstract method 𝑚’ that

has the same name and descriptor as 𝑚

Dispatch(𝑐′, 𝑚), otherwise

where 𝑐′ is superclass of 𝑐

o1.foo(…)2;

Tian Tan @ Nanjing University

Page 27: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

Dispatch: An Example

27

class A {void foo() {…}

}

class B extends A {}

class C extends B {void foo() {…}

}

void dispatch() {A x = new B();x.foo();

A y = new C();y.foo();

}

Dispatch(𝑐,𝑚) =

𝑚′, if 𝑐 contains non-abstract method 𝑚’ that

has the same name and descriptor as 𝑚

Dispatch(𝑐′,𝑚), otherwise

where 𝑐′ is superclass of 𝑐

A

foo()

B

C

foo()

Dispatch(B, A.foo()) =

Tian Tan @ Nanjing University

Page 28: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

Dispatch: An Example

28

class A {void foo() {…}

}

class B extends A {}

class C extends B {void foo() {…}

}

void dispatch() {A x = new B();x.foo();

A y = new C();y.foo();

}

Dispatch(B, A.foo()) = A.foo()

A

foo()

B

C

foo()

Dispatch(C, A.foo()) =

Dispatch(𝑐,𝑚) =

𝑚′, if 𝑐 contains non-abstract method 𝑚’ that

has the same name and descriptor as 𝑚

Dispatch(𝑐′,𝑚), otherwise

where 𝑐′ is superclass of 𝑐

Tian Tan @ Nanjing University

Page 29: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

Dispatch: An Example

29

class A {void foo() {…}

}

class B extends A {}

class C extends B {void foo() {…}

}

void dispatch() {A x = new B();x.foo();

A y = new C();y.foo();

}

Dispatch(B, A.foo()) = A.foo()

A

foo()

B

C

foo()

Dispatch(C, A.foo()) = C.foo()

Dispatch(𝑐,𝑚) =

𝑚′, if 𝑐 contains non-abstract method 𝑚’ that

has the same name and descriptor as 𝑚

Dispatch(𝑐′,𝑚), otherwise

where 𝑐′ is superclass of 𝑐

Tian Tan @ Nanjing University

Page 30: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

Class Hierarchy Analysis* (CHA)

• Require the class hierarchy information (inheritance structure) of the whole program

• Resolve a virtual call based on the declared type of receiver variable of the call site

30

Jeffrey Dean, David Grove, Craig Chambers, “Optimization of Object-Oriented Programs Using Static Class Hierarchy Analysis”. ECOOP 1995.

*

A a = …a.foo();

Tian Tan @ Nanjing University

Page 31: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

Class Hierarchy Analysis* (CHA)

• Require the class hierarchy information (inheritance structure) of the whole program

• Resolve a virtual call based on the declared type of receiver variable of the call site

• Assume the receiver variable a may point to objects of class Aor all subclasses of A• Resolve target methods by looking up the class hierarchy of class A

31

Jeffrey Dean, David Grove, Craig Chambers, “Optimization of Object-Oriented Programs Using Static Class Hierarchy Analysis”. ECOOP 1995.

*

A a = …a.foo();

Tian Tan @ Nanjing University

Page 32: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

Class Hierarchy Analysis* (CHA)

• Require the class hierarchy information (inheritance structure) of the whole program

• Resolve a virtual call based on the declared type of receiver variable of the call site

• Assume the receiver variable a may point to objects of class Aor all subclasses of A• Resolve target methods by looking up the class hierarchy of class A

32

Jeffrey Dean, David Grove, Craig Chambers, “Optimization of Object-Oriented Programs Using Static Class Hierarchy Analysis”. ECOOP 1995.

*

A a = …a.foo();

Tian Tan @ Nanjing University

Page 33: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

Call Resolution of CHA

Resolve(𝑐𝑠)T = {}𝑚 = method signature at 𝑐𝑠if 𝑐𝑠 is a static call then

T = { 𝑚 }if 𝑐𝑠 is special call then𝑐𝑚 = class type of 𝑚T = { Dispatch(𝑐𝑚, 𝑚) }

if 𝑐𝑠 is a virtual call then𝑐 = declared type of receiver variable at 𝑐𝑠foreach 𝑐′ that is a subclass of 𝑐 or 𝑐 itself do

add Dispatch(𝑐′, 𝑚) to Treturn T

33

We define function Resolve(𝑐𝑠) to resolve possible target methods of a call site 𝑐𝑠 by CHA

Tian Tan @ Nanjing University

Page 34: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

Call Resolution of CHA

Resolve(𝑐𝑠)T = {}𝑚 = method signature at 𝑐𝑠if 𝑐𝑠 is a static call then

T = { 𝑚 }if 𝑐𝑠 is special call then𝑐𝑚 = class type of 𝑚T = { Dispatch(𝑐𝑚, 𝑚) }

if 𝑐𝑠 is a virtual call then𝑐 = declared type of receiver variable at 𝑐𝑠foreach 𝑐′ that is a subclass of 𝑐 or 𝑐 itself do

add Dispatch(𝑐′, 𝑚) to Treturn T

34

We define function Resolve(𝑐𝑠) to resolve possible target methods of a call site 𝑐𝑠 by CHA

class C {static T foo(P p, Q q){…}

}

C.foo(x, y);

𝑐𝑠 C.foo(x, y);

𝑚 <C: T foo(P,Q)>

Tian Tan @ Nanjing University

Page 35: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

Call Resolution of CHA

Resolve(𝑐𝑠)T = {}𝑚 = method signature at 𝑐𝑠if 𝑐𝑠 is a static call then

T = { 𝑚 }if 𝑐𝑠 is special call then𝑐𝑚 = class type of 𝑚T = { Dispatch(𝑐𝑚, 𝑚) }

if 𝑐𝑠 is a virtual call then𝑐 = declared type of receiver variable at 𝑐𝑠foreach 𝑐′ that is a subclass of 𝑐 or 𝑐 itself do

add Dispatch(𝑐′, 𝑚) to Treturn T

35

We define function Resolve(𝑐𝑠) to resolve possible target methods of a call site 𝑐𝑠 by CHA

class C extends B {T foo(P p, Q q) {…super.foo(p, q);

}}

𝑐𝑠 super.foo(p, q);

𝑚 <B: T foo(P,Q)>

𝑐𝑚 B

Tian Tan @ Nanjing University

Page 36: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

Call Resolution of CHA

Resolve(𝑐𝑠)T = {}𝑚 = method signature at 𝑐𝑠if 𝑐𝑠 is a static call then

T = { 𝑚 }if 𝑐𝑠 is special call then𝑐𝑚 = class type of 𝑚T = { Dispatch(𝑐𝑚, 𝑚) }

if 𝑐𝑠 is a virtual call then𝑐 = declared type of receiver variable at 𝑐𝑠foreach 𝑐′ that is a subclass of 𝑐 or 𝑐 itself do

add Dispatch(𝑐′, 𝑚) to Treturn T

36

We define function Resolve(𝑐𝑠) to resolve possible target methods of a call site 𝑐𝑠 by CHA

class C extends B {T foo(P p, Q q) {…super.foo(p, q);

}}

𝑐𝑠 super.foo(p, q);

𝑚 <B: T foo(P,Q)>

𝑐𝑚 B

Tian Tan @ Nanjing University

A

foo()

B

C

foo()

Targetmethod

Page 37: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

Call Resolution of CHA

Resolve(𝑐𝑠)T = {}𝑚 = method signature at 𝑐𝑠if 𝑐𝑠 is a static call then

T = { 𝑚 }if 𝑐𝑠 is special call then𝑐𝑚 = class type of 𝑚T = { Dispatch(𝑐𝑚, 𝑚) }

if 𝑐𝑠 is a virtual call then𝑐 = declared type of receiver variable at 𝑐𝑠foreach 𝑐′ that is a subclass of 𝑐 or 𝑐 itself do

add Dispatch(𝑐′, 𝑚) to Treturn T

37

We define function Resolve(𝑐𝑠) to resolve possible target methods of a call site 𝑐𝑠 by CHA

class C extends B {T foo(P p, Q q) {…this.bar();

}private T bar()

}C c = new C();

Tian Tan @ Nanjing University

Special call• Private instance method• Constructor• Superclass instance method

Page 38: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

Call Resolution of CHA

Resolve(𝑐𝑠)T = {}𝑚 = method signature at 𝑐𝑠if 𝑐𝑠 is a static call then

T = { 𝑚 }if 𝑐𝑠 is special call then𝑐𝑚 = class type of 𝑚T = { Dispatch(𝑐𝑚, 𝑚) }

if 𝑐𝑠 is a virtual call then𝑐 = declared type of receiver variable at 𝑐𝑠foreach 𝑐′ that is a subclass of 𝑐 or 𝑐 itself do

add Dispatch(𝑐′, 𝑚) to Treturn T

38

We define function Resolve(𝑐𝑠) to resolve possible target methods of a call site 𝑐𝑠 by CHA

𝑐𝑠 c.foo(x, y);

𝑚 <C: T foo(P,Q)>

𝑐 C

class C {T foo(P p, Q q) {…}

}C c = …c.foo(x, y);

Tian Tan @ Nanjing University

Page 39: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

Call Resolution of CHA

Resolve(𝑐𝑠)T = {}𝑚 = method signature at 𝑐𝑠if 𝑐𝑠 is a static call then

T = { 𝑚 }if 𝑐𝑠 is special call then𝑐𝑚 = class type of 𝑚T = { Dispatch(𝑐𝑚, 𝑚) }

if 𝑐𝑠 is a virtual call then𝑐 = declared type of receiver variable at 𝑐𝑠foreach 𝑐′ that is a subclass of 𝑐 or 𝑐 itself do

add Dispatch(𝑐′, 𝑚) to Treturn T

39

We define function Resolve(𝑐𝑠) to resolve possible target methods of a call site 𝑐𝑠 by CHA

𝑐𝑠 c.foo(x, y);

𝑚 <C: T foo(P,Q)>

𝑐 C

class C {T foo(P p, Q q) {…}

}C c = …c.foo(x, y);

Tian Tan @ Nanjing University

Subclasses includes all direct

and indirect subclasses of 𝑐

Page 40: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

CHA: An Example

40

class A {void foo() {…}

}class B extends A {}

class C extends B {void foo() {…}

}class D extends B {void foo() {…}

}

void resolve() {C c = …c.foo();

A a = …a.foo();

B b = …b.foo();

}

A

foo()

B

D

foo()

C

foo()

Tian Tan @ Nanjing University

Page 41: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

CHA: An Example

41

class A {void foo() {…}

}class B extends A {}

class C extends B {void foo() {…}

}class D extends B {void foo() {…}

}

void resolve() {C c = …c.foo();

A a = …a.foo();

B b = …b.foo();

}

Resolve(c.foo()) =

A

foo()

B

D

foo()

C

foo()

Tian Tan @ Nanjing University

Page 42: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

CHA: An Example

42

class A {void foo() {…}

}class B extends A {}

class C extends B {void foo() {…}

}class D extends B {void foo() {…}

}

void resolve() {C c = …c.foo();

A a = …a.foo();

B b = …b.foo();

}

Resolve(a.foo()) =

Resolve(c.foo()) = {C.foo()}

A

foo()

B

D

foo()

C

foo()

Tian Tan @ Nanjing University

Page 43: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

CHA: An Example

43

class A {void foo() {…}

}class B extends A {}

class C extends B {void foo() {…}

}class D extends B {void foo() {…}

}

void resolve() {C c = …c.foo();

A a = …a.foo();

B b = …b.foo();

}

Resolve(a.foo()) = {A.foo(), C.foo(), D.foo()}

Resolve(c.foo()) = {C.foo()}

A

foo()

B

D

foo()

C

foo()

Resolve(b.foo()) =

Tian Tan @ Nanjing University

Page 44: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

CHA: An Example

44

class A {void foo() {…}

}class B extends A {}

class C extends B {void foo() {…}

}class D extends B {void foo() {…}

}

void resolve() {C c = …c.foo();

A a = …a.foo();

B b = …b.foo();

}

Resolve(a.foo()) = {A.foo(), C.foo(), D.foo()}

Resolve(c.foo()) = {C.foo()}

A

foo()

B

D

foo()

C

foo()

Resolve(b.foo()) = {A.foo(), C.foo(), D.foo()}

Tian Tan @ Nanjing University

Page 45: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

CHA: An Example

45

class A {void foo() {…}

}class B extends A {}

class C extends B {void foo() {…}

}class D extends B {void foo() {…}

}

void resolve() {C c = …c.foo();

A a = …a.foo();

B b = new B();b.foo();

}

Resolve(a.foo()) = {A.foo(), C.foo(), D.foo()}

Resolve(c.foo()) = {C.foo()}

A

foo()

B

D

foo()

C

foo()

Resolve(b.foo()) =

Tian Tan @ Nanjing University

Page 46: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

CHA: An Example

46

class A {void foo() {…}

}class B extends A {}

class C extends B {void foo() {…}

}class D extends B {void foo() {…}

}

void resolve() {C c = …c.foo();

A a = …a.foo();

B b = new B();b.foo();

}

Resolve(a.foo()) = {A.foo(), C.foo(), D.foo()}

Resolve(c.foo()) = {C.foo()}

A

foo()

B

D

foo()

C

foo()

Resolve(b.foo()) = {A.foo(), C.foo(), D.foo()}

Spurious call targetsTian Tan @ Nanjing University

Page 47: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

Features of CHA

• Advantage: fast• Only consider the declared type of receiver variable

at the call-site, and its inheritance hierarchy

• Ignore data- and control-flow information

47Tian Tan @ Nanjing University

Page 48: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

Features of CHA

• Advantage: fast• Only consider the declared type of receiver variable

at the call-site, and its inheritance hierarchy

• Ignore data- and control-flow information

• Disadvantage: imprecise• Easily introduce spurious target methods

• Addressed in next lectures

48Tian Tan @ Nanjing University

Page 49: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

Features of CHA

• Advantage: fast• Only consider the declared type of receiver variable

at the call-site, and its inheritance hierarchy

• Ignore data- and control-flow information

• Disadvantage: imprecise• Easily introduce spurious target methods

• Addressed in next lectures

49

Common usage: IDE

Tian Tan @ Nanjing University

Page 50: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

CHA in IDE (IntelliJ IDEA)

50Tian Tan @ Nanjing University

Page 51: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

Call Graph Construction

Build call graph for whole program via CHA

• Start from entry methods (focus on main method)

• For each reachable method 𝑚, resolve target methods for each call site 𝑐𝑠 in 𝑚 via CHA (Resolve(𝑐𝑠))

• Repeat until no new method is discovered

51

main

m2 m3

m4

m5m7

m6

m8m9

Tian Tan @ Nanjing University

Page 52: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

Call Graph Construction: Algorithm

52

WLWork list, containing the methods to be processed

CG Call graph, a set of call edges

RM A set of reachable methods

BuildCallGraph(𝑚𝑒𝑛𝑡𝑟𝑦)WL = [𝑚𝑒𝑛𝑡𝑟𝑦], CG = {}, RM = {}while WL is not empty do

remove 𝑚 from WLif 𝑚 ∉ RM then

add 𝑚 to RMforeach call site 𝑐𝑠 in 𝑚 do

T = Resolve(𝑐𝑠)foreach target method 𝑚’ in T do

add 𝑐𝑠 → 𝑚′ to CGadd 𝑚′ to WL

return CG

Tian Tan @ Nanjing University

Page 53: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

Call Graph Construction: Algorithm

53

WLWork list, containing the methods to be processed

CG Call graph, a set of call edges

RM A set of reachable methods

Initialize the algorithmBuildCallGraph(𝑚𝑒𝑛𝑡𝑟𝑦)

WL = [𝑚𝑒𝑛𝑡𝑟𝑦], CG = {}, RM = {}while WL is not empty do

remove 𝑚 from WLif 𝑚 ∉ RM then

add 𝑚 to RMforeach call site 𝑐𝑠 in 𝑚 do

T = Resolve(𝑐𝑠)foreach target method 𝑚’ in T do

add 𝑐𝑠 → 𝑚′ to CGadd 𝑚′ to WL

return CG

Tian Tan @ Nanjing University

Page 54: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

Call Graph Construction: Algorithm

54

WLWork list, containing the methods to be processed

CG Call graph, a set of call edges

RM A set of reachable methods

Initialize the algorithmBuildCallGraph(𝑚𝑒𝑛𝑡𝑟𝑦)

WL = [𝑚𝑒𝑛𝑡𝑟𝑦], CG = {}, RM = {}while WL is not empty do

remove 𝑚 from WLif 𝑚 ∉ RM then

add 𝑚 to RMforeach call site 𝑐𝑠 in 𝑚 do

T = Resolve(𝑐𝑠)foreach target method 𝑚’ in T do

add 𝑐𝑠 → 𝑚′ to CGadd 𝑚′ to WL

return CG

Resolve target methods via CHA

Tian Tan @ Nanjing University

Page 55: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

Call Graph Construction: Algorithm

55

WLWork list, containing the methods to be processed

CG Call graph, a set of call edges

RM A set of reachable methods

Initialize the algorithmBuildCallGraph(𝑚𝑒𝑛𝑡𝑟𝑦)

WL = [𝑚𝑒𝑛𝑡𝑟𝑦], CG = {}, RM = {}while WL is not empty do

remove 𝑚 from WLif 𝑚 ∉ RM then

add 𝑚 to RMforeach call site 𝑐𝑠 in 𝑚 do

T = Resolve(𝑐𝑠)foreach target method 𝑚’ in T do

add 𝑐𝑠 → 𝑚′ to CGadd 𝑚′ to WL

return CG

Resolve target methods via CHA

Add call edges to call graph

Tian Tan @ Nanjing University

Page 56: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

Call Graph Construction: Algorithm

56

WLWork list, containing the methods to be processed

CG Call graph, a set of call edges

RM A set of reachable methods

Initialize the algorithmBuildCallGraph(𝑚𝑒𝑛𝑡𝑟𝑦)

WL = [𝑚𝑒𝑛𝑡𝑟𝑦], CG = {}, RM = {}while WL is not empty do

remove 𝑚 from WLif 𝑚 ∉ RM then

add 𝑚 to RMforeach call site 𝑐𝑠 in 𝑚 do

T = Resolve(𝑐𝑠)foreach target method 𝑚’ in T do

add 𝑐𝑠 → 𝑚′ to CGadd 𝑚′ to WL

return CG

Resolve target methods via CHA

Add call edges to call graph

May discover new method, add it to work list

Tian Tan @ Nanjing University

Page 57: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

Call Graph Construction: An Example

57

A.main()

A.foo();

B.Bar()

A.foo()

a.bar();

A.bar()

c.bar();

C.m()

C.bar()

A.foo();

WL = [A.main()]

class A {static void main() {A.foo();

}static void foo() {A a = new A();a.bar();

}void bar() {C c = new C();c.bar();

}}class B extends A {void bar() {} }

class C extends A {void bar() {if (…) A.foo();

}void m() {}

}

Initialization with main method

Page 58: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

Call Graph Construction: An Example

58

A.main()

A.foo();

B.Bar()

A.foo()

a.bar();

A.bar()

c.bar();

C.m()

C.bar()

A.foo();

WL = [ ]

class A {static void main() {A.foo();

}static void foo() {A a = new A();a.bar();

}void bar() {C c = new C();c.bar();

}}class B extends A {void bar() {} }

class C extends A {void bar() {if (…) A.foo();

}void m() {}

}

Resolve(A.foo()) =

Page 59: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

Call Graph Construction: An Example

59

A.main()

A.foo();

B.Bar()

A.foo()

a.bar();

A.bar()

c.bar();

C.m()

C.bar()

A.foo();

WL = [A.foo()]

class A {static void main() {A.foo();

}static void foo() {A a = new A();a.bar();

}void bar() {C c = new C();c.bar();

}}class B extends A {void bar() {} }

class C extends A {void bar() {if (…) A.foo();

}void m() {}

}

Resolve(A.foo()) = { A.foo() }

Page 60: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

Call Graph Construction: An Example

60

A.main()

A.foo();

B.Bar()

A.foo()

a.bar();

A.bar()

c.bar();

C.m()

C.bar()

A.foo();

WL = [ ]

class A {static void main() {A.foo();

}static void foo() {A a = new A();a.bar();

}void bar() {C c = new C();c.bar();

}}class B extends A {void bar() {} }

class C extends A {void bar() {if (…) A.foo();

}void m() {}

}

Page 61: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

Call Graph Construction: An Example

61

A.main()

A.foo();

B.Bar()

A.foo()

a.bar();

A.bar()

c.bar();

C.m()

C.bar()

A.foo();

class A {static void main() {A.foo();

}static void foo() {A a = new A();a.bar();

}void bar() {C c = new C();c.bar();

}}class B extends A {void bar() {} }

class C extends A {void bar() {if (…) A.foo();

}void m() {}

}

Methods in RM

Methods not in RM

Page 62: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

Call Graph Construction: An Example

62

A.main()

A.foo();

B.Bar()

A.foo()

a.bar();

A.bar()

c.bar();

C.m()

C.bar()

A.foo();

WL = [ ]

class A {static void main() {A.foo();

}static void foo() {A a = new A();a.bar();

}void bar() {C c = new C();c.bar();

}}class B extends A {void bar() {} }

class C extends A {void bar() {if (…) A.foo();

}void m() {}

}

Resolve(a.bar()) =

Page 63: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

Call Graph Construction: An Example

63

A.main()

A.foo();

B.Bar()

A.foo()

a.bar();

A.bar()

c.bar();

C.m()

C.bar()

A.foo();

WL = [A.bar(), B.bar(), C.bar()]

class A {static void main() {A.foo();

}static void foo() {A a = new A();a.bar();

}void bar() {C c = new C();c.bar();

}}class B extends A {void bar() {} }

class C extends A {void bar() {if (…) A.foo();

}void m() {}

}

Resolve(a.bar()) = { A.bar(),

B.bar(),

C.bar() }

Page 64: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

Call Graph Construction: An Example

64

A.main()

A.foo();

B.Bar()

A.foo()

a.bar();

A.bar()

c.bar();

C.m()

C.bar()

A.foo();

WL = [B.bar(), C.bar()]

class A {static void main() {A.foo();

}static void foo() {A a = new A();a.bar();

}void bar() {C c = new C();c.bar();

}}class B extends A {void bar() {} }

class C extends A {void bar() {if (…) A.foo();

}void m() {}

}

Resolve(c.bar()) =

Page 65: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

class A {static void main() {A.foo();

}static void foo() {A a = new A();a.bar();

}void bar() {C c = new C();c.bar();

}}class B extends A {void bar() {} }

class C extends A {void bar() {if (…) A.foo();

}void m() {}

}

Call Graph Construction: An Example

65

A.main()

A.foo();

B.Bar()

A.foo()

a.bar();

A.bar()

c.bar();

C.m()

C.bar()

A.foo();

WL = [B.bar(), C.bar(),C.bar()]

Resolve(c.bar()) = { C.bar() }

Page 66: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

class A {static void main() {A.foo();

}static void foo() {A a = new A();a.bar();

}void bar() {C c = new C();c.bar();

}}class B extends A {void bar() {} }

class C extends A {void bar() {if (…) A.foo();

}void m() {}

}

Call Graph Construction: An Example

66

A.main()

A.foo();

B.Bar()

A.foo()

a.bar();

A.bar()

c.bar();

C.m()

C.bar()

A.foo();

WL = [C.bar(),C.bar()]

Page 67: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

Call Graph Construction: An Example

67

A.main()

A.foo();

B.Bar()

A.foo()

a.bar();

A.bar()

c.bar();

C.m()

C.bar()

A.foo();

WL = [C.bar()]

class A {static void main() {A.foo();

}static void foo() {A a = new A();a.bar();

}void bar() {C c = new C();c.bar();

}}class B extends A {void bar() {} }

class C extends A {void bar() {if (…) A.foo();

}void m() {}

}

Page 68: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

Call Graph Construction: An Example

68

class A {static void main() {A.foo();

}static void foo() {A a = new A();a.bar();

}void bar() {C c = new C();c.bar();

}}class B extends A {void bar() {} }

class C extends A {void bar() {if (…) A.foo();

}void m() {}

}

A.main()

A.foo();

B.Bar()

A.foo()

a.bar();

A.bar()

c.bar();

C.m()

C.bar()

A.foo();

WL = [C.bar(),A.foo()]

Resolve(A.foo()) = { A.foo() }

Page 69: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

Call Graph Construction: An Example

69

class A {static void main() {A.foo();

}static void foo() {A a = new A();a.bar();

}void bar() {C c = new C();c.bar();

}}class B extends A {void bar() {} }

class C extends A {void bar() {if (…) A.foo();

}void m() {}

}

A.main()

A.foo();

B.Bar()

A.foo()

a.bar();

A.bar()

c.bar();

C.m()

C.bar()

A.foo();

WL = [A.foo()]

Page 70: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

Call Graph Construction: An Example

70

class A {static void main() {A.foo();

}static void foo() {A a = new A();a.bar();

}void bar() {C c = new C();c.bar();

}}class B extends A {void bar() {} }

class C extends A {void bar() {if (…) A.foo();

}void m() {}

}

A.main()

A.foo();

B.Bar()

A.foo()

a.bar();

A.bar()

c.bar();

C.m()

C.bar()

A.foo();

WL = [ ]

Page 71: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

Call Graph Construction: An Example

71

A.main()

A.foo();

B.Bar()

A.foo()

a.bar();

A.bar()

c.bar();

C.m()

C.bar()

A.foo();

WL = [ ]

class A {static void main() {A.foo();

}static void foo() {A a = new A();a.bar();

}void bar() {C c = new C();c.bar();

}}class B extends A {void bar() {} }

class C extends A {void bar() {if (…) A.foo();

}void m() {}

}

Page 72: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

Call Graph Construction: An Example

72

A.main()

A.foo();

B.Bar()

A.foo()

a.bar();

A.bar()

c.bar();

C.m()

C.bar()

A.foo();

WL = [ ]

class A {static void main() {A.foo();

}static void foo() {A a = new A();a.bar();

}void bar() {C c = new C();c.bar();

}}class B extends A {void bar() {} }

class C extends A {void bar() {if (…) A.foo();

}void m() {}

}Unreachable (dead code)

Page 73: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

Contents

73

1. Motivation

2. Call Graph Construction (CHA)

3. Interprocedural Control-Flow Graph

4. Interprocedural Data-Flow Analysis

Tian Tan @ Nanjing University

Page 74: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

Interprocedural Control-Flow Graph• CFG represents structure of an individual method

• ICFG represents structure of the whole program• With ICFG, we can perform interprocedural analysis

74Tian Tan @ Nanjing University

Page 75: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

Interprocedural Control-Flow Graph• CFG represents structure of an individual method

• ICFG represents structure of the whole program• With ICFG, we can perform interprocedural analysis

• An ICFG of a program consists of CFGs of the methods in the program, plus two kinds of additional edges:➢Call edges: from call sites to the entry nodes of their callees

➢Return edges: from return statements of the callees to the statements following their call sites (i.e., return sites)

75Tian Tan @ Nanjing University

Page 76: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

Interprocedural Control-Flow Graph• CFG represents structure of an individual method

• ICFG represents structure of the whole program• With ICFG, we can perform interprocedural analysis

• An ICFG of a program consists of CFGs of the methods in the program, plus two kinds of additional edges:➢Call edges: from call sites to the entry nodes of their callees

➢Return edges: from return statements of the callees to the statements following their call sites (i.e., return sites)

76

void foo() {bar(…); // call siteint n = 3; // return site

}

Tian Tan @ Nanjing University

Page 77: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

Interprocedural Control-Flow Graph• CFG represents structure of an individual method

• ICFG represents structure of the whole program• With ICFG, we can perform interprocedural analysis

• An ICFG of a program consists of CFGs of the methods in the program, plus two kinds of additional edges:➢Call edges: from call sites to the entry nodes of their callees

➢Return edges: from return statements of the callees to the statements following their call sites (i.e., return sites)

77

The information for connecting these two kinds of edges comes from call graph

ICFG = CFGs + call & return edges

Tian Tan @ Nanjing University

Page 78: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

ICFG: An Example

78

a = 6

b = addOne(a)

c = b - 3

b = ten()

c = a * b

Enter addOne(int x)

y = x + 1

return y

Enter ten()

return 10

Enter main()

Tian Tan @ Nanjing University

static void main() {int a, b, c;a = 6;b = addOne(a);c = b - 3;b = ten();c = a * b;

}

int addOne(int x) {int y = x + 1;return y;

}

int ten() {return 10;

}

CFG edges

Page 79: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

ICFG: An Example

79

a = 6

b = addOne(a)

c = b - 3

b = ten()

c = a * b

Enter addOne(int x)

y = x + 1

return y

Enter ten()

return 10

Enter main()

Tian Tan @ Nanjing University

static void main() {int a, b, c;a = 6;b = addOne(a);c = b - 3;b = ten();c = a * b;

}

int addOne(int x) {int y = x + 1;return y;

}

int ten() {return 10;

}

CFG edges

Call edges

Return edges

ICFG = CFGs + call & return edges

Page 80: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

ICFG: An Example

80

static void main() {int a, b, c;a = 6;b = addOne(a);c = b - 3;b = ten();c = a * b;

}

int addOne(int x) {int y = x + 1;return y;

}

int ten() {return 10;

}

Enter main()

a = 6

b = addOne(a)

c = b - 3

b = ten()

c = a * b

Enter addOne(int x)

y = x + 1

return y

Enter ten()

return 10

CFG edges

Call edges

Return edges

ICFG = CFGs + call & return edgesTian Tan @ Nanjing University

Page 81: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

Contents

81

1. Motivation

2. Call Graph Construction (CHA)

3. Interprocedural Control-Flow Graph

4. Interprocedural Data-Flow Analysis

Tian Tan @ Nanjing University

Page 82: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

Interprocedural Data-Flow Analysis

82

Intraprocedural Interprocecdural

Program representation CFG ICFG = CFGs + call & return edges

Analyzing the whole program with method calls based on interprocedural control-flow graph (ICFG)

Tian Tan @ Nanjing University

Page 83: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

Interprocedural Data-Flow Analysis

83

Intraprocedural Interprocecdural

Program representation CFG ICFG = CFGs + call & return edges

Transfer functions Node transfer Node transfer + edge transfer

Analyzing the whole program with method calls based on interprocedural control-flow graph (ICFG)

Tian Tan @ Nanjing University

Page 84: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

Interprocedural Data-Flow AnalysisAnalyzing the whole program with method calls based on interprocedural control-flow graph (ICFG)

84

Intraprocedural Interprocecdural

Program representation CFG ICFG = CFGs + call & return edges

Transfer functions Node transfer Node transfer + edge transfer

Edge transfer• Call edge transfer: transfer data flow from call node to the

entry node of callee (along call edges)• Return edge transfer: transfer data flow from return node of

the callee to the return site (along return edges)Tian Tan @ Nanjing University

Page 85: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

Interprocedural Constant Propagation

• Call edge transfer: pass argument values

• Return edge transfer: pass return values

• Node transfer: same as intra-procedural constant propagation, plus that• For each call node, kill data-flow value for the LHS variable. Its value

will flow to return site along the return edges

85Tian Tan @ Nanjing University

b = addOne(a)a=…,b=…

a=…

Page 86: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

86

Enter main()

Interprocedural Constant Propagation: An Example

Enter addOne(int x)

a = 6

b = addOne(a)

c = b - 3

b = ten()

c = a * b

y = x + 1

return y

Enter ten()

return 10

IN Flow

OUT Flow

Tian Tan @ Nanjing University

static void main() {int a, b, c;a = 6;b = addOne(a);c = b - 3;b = ten();c = a * b;

}

staticint addOne(int x) {int y = x + 1;return y;

}

static int ten() {return 10;

}

Page 87: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

87

Enter main()

Interprocedural Constant Propagation: An Example

Enter addOne(int x)

a = 6

b = addOne(a)

c = b - 3

b = ten()

c = a * b

y = x + 1

return y

Enter ten()

return 10

a=6

a=6

a=6

IN Flow

OUT Flow

Tian Tan @ Nanjing University

static void main() {int a, b, c;a = 6;b = addOne(a);c = b - 3;b = ten();c = a * b;

}

staticint addOne(int x) {int y = x + 1;return y;

}

static int ten() {return 10;

}

Page 88: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

88

Enter main()

Interprocedural Constant Propagation: An Example

Enter addOne(int x)

a = 6

b = addOne(a)

c = b - 3

b = ten()

c = a * b

y = x + 1

return y

Enter ten()

return 10

IN Flow

OUT Flow

a=6

a=6

a=6

x=6

x=val(a)

Call edge transfer: pass argument values

Tian Tan @ Nanjing University

static void main() {int a, b, c;a = 6;b = addOne(a);c = b - 3;b = ten();c = a * b;

}

staticint addOne(int x) {int y = x + 1;return y;

}

static int ten() {return 10;

}

Page 89: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

89

Enter main()

Interprocedural Constant Propagation: An Example

Enter addOne(int x)

a = 6

b = addOne(a)

c = b - 3

b = ten()

c = a * b

y = x + 1

return y

Enter ten()

return 10

a=6

a=6

a=6

x=6

x=6

x=6

IN Flow

OUT Flow

Tian Tan @ Nanjing University

x=val(a)

static void main() {int a, b, c;a = 6;b = addOne(a);c = b - 3;b = ten();c = a * b;

}

staticint addOne(int x) {int y = x + 1;return y;

}

static int ten() {return 10;

}

Page 90: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

90

Enter main()

Interprocedural Constant Propagation: An Example

Enter addOne(int x)

a = 6

b = addOne(a)

c = b - 3

b = ten()

c = a * b

y = x + 1

return y

Enter ten()

return 10

a=6

a=6

a=6

x=6

x=6

x=6

x=6,y=7

IN Flow

OUT Flow

Tian Tan @ Nanjing University

x=val(a)

static void main() {int a, b, c;a = 6;b = addOne(a);c = b - 3;b = ten();c = a * b;

}

staticint addOne(int x) {int y = x + 1;return y;

}

static int ten() {return 10;

}

Page 91: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

91

Enter main()

Interprocedural Constant Propagation: An Example

Enter addOne(int x)

a = 6

b = addOne(a)

c = b - 3

b = ten()

c = a * b

y = x + 1

return y

Enter ten()

return 10

a=6

a=6

a=6

x=6

x=6

x=6

x=6,y=7

x=6,y=7

x=6,y=7

IN Flow

OUT Flow

Tian Tan @ Nanjing University

x=val(a)

static void main() {int a, b, c;a = 6;b = addOne(a);c = b - 3;b = ten();c = a * b;

}

staticint addOne(int x) {int y = x + 1;return y;

}

static int ten() {return 10;

}

Page 92: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

92

Enter main()

Interprocedural Constant Propagation: An Example

Enter addOne(int x)

a = 6

b = addOne(a)

c = b - 3

b = ten()

c = a * b

y = x + 1

return y

Enter ten()

return 10

a=6

a=6

a=6

x=6

x=6

x=6

x=6,y=7

x=6,y=7

x=6,y=7

return edge transfer: pass return values

IN Flow

OUT Flow

Tian Tan @ Nanjing University

x=val(a)

static void main() {int a, b, c;a = 6;b = addOne(a);c = b - 3;b = ten();c = a * b;

}

staticint addOne(int x) {int y = x + 1;return y;

}

static int ten() {return 10;

}

Page 93: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

93

Enter main()

Interprocedural Constant Propagation: An Example

Enter addOne(int x)

a = 6

b = addOne(a)

c = b - 3

b = ten()

c = a * b

y = x + 1

return y

Enter ten()

return 10

a=6

a=6

a=6

x=6

x=6

x=6

x=6,y=7

x=6,y=7

x=6,y=7

a=6,b=7

b=val(y)

return edge transfer: pass return values

IN Flow

OUT Flow

Tian Tan @ Nanjing University

x=val(a)

static void main() {int a, b, c;a = 6;b = addOne(a);c = b - 3;b = ten();c = a * b;

}

staticint addOne(int x) {int y = x + 1;return y;

}

static int ten() {return 10;

}

Page 94: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

94

Enter main()

Interprocedural Constant Propagation: An Example

Enter addOne(int x)

a = 6

b = addOne(a)

c = b - 3

b = ten()

c = a * b

y = x + 1

return y

Enter ten()

return 10

a=6

a=6

a=6

x=6

x=6

x=6

x=6,y=7

x=6,y=7

x=6,y=7

a=6,b=7

IN Flow

OUT Flow

Tian Tan @ Nanjing University

b=val(y)

x=val(a)

static void main() {int a, b, c;a = 6;b = addOne(a);c = b - 3;b = ten();c = a * b;

}

staticint addOne(int x) {int y = x + 1;return y;

}

static int ten() {return 10;

}

Page 95: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

static void main() {int a, b, c;a = 6;b = addOne(a);c = b - 3;b = ten();c = a * b;

}

staticint addOne(int x) {int y = x + 1;return y;

}

static int ten() {return 10;

}

95

Enter main()

Interprocedural Constant Propagation: An Example

Enter addOne(int x)

a = 6

b = addOne(a)

c = b - 3

b = ten()

c = a * b

y = x + 1

return y

Enter ten()

return 10

a=6

a=6

a=6

x=6

x=6

x=6

x=6,y=7

x=6,y=7

x=6,y=7

a=6,b=7

IN Flow

OUT Flow

Tian Tan @ Nanjing University

b=val(y)

x=val(a)

Such edge (from call site to return site) is named call-to-return edge. It allows the analysis to propagate local data-flow (a=6 in this case) on ICFG.

Page 96: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

static void main() {int a, b, c;a = 6;b = addOne(a);c = b - 3;b = ten();c = a * b;

}

staticint addOne(int x) {int y = x + 1;return y;

}

static int ten() {return 10;

}

a=6

a=6,b=7

96

Enter main()

Interprocedural Constant Propagation: An Example

Enter addOne(int x)

a = 6

b = addOne(a)

c = b - 3

b = ten()

c = a * b

y = x + 1

return y

Enter ten()

return 10

a=6

a=6

x=6,a=6

x=6,a=6

x=6,a=6

x=6,y=7,a=6

x=6,y=7,a=6

x=6,y=7,a=6

Such edge (from call site to return site) is named call-to-return edge. It allows the analysis to propagate local data-flow (a=6 in this case) on ICFG.

Without such edges, we have to propagate local data-flow across other methods, which is very inefficient.

IN Flow

OUT Flow

Tian Tan @ Nanjing University

b=val(y)

x=val(a)

Page 97: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

static void main() {int a, b, c;a = 6;b = addOne(a);c = b - 3;b = ten();c = a * b;

}

staticint addOne(int x) {int y = x + 1;return y;

}

static int ten() {return 10;

}

97

Enter main()

Interprocedural Constant Propagation: An Example

Enter addOne(int x)

a = 6

b = addOne(a)

c = b - 3

b = ten()

c = a * b

y = x + 1

return y

Enter ten()

return 10

a=6

a=6

a=6

x=6

x=6

x=6

x=6,y=7

x=6,y=7

x=6,y=7

a=6,b=7

IN Flow

OUT Flow

Tian Tan @ Nanjing University

b=val(y)

x=val(a)

Such edge (from call site to return site) is named call-to-return edge. It allows the analysis to propagate local data-flow (a=6 in this case) on ICFG.

Without such edges, we have to propagate local data-flow across other methods, which is very inefficient.

Page 98: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

98

static void main() {int a, b, c;a = 6;b = addOne(a);c = b - 3;b = ten();c = a * b;

}

staticint addOne(int x) {int y = x + 1;return y;

}

static int ten() {return 10;

}

Enter main()

Interprocedural Constant Propagation: An Example

Enter addOne(int x)

a = 6

b = addOne(a)

c = b - 3

b = ten()

c = a * b

y = x + 1

return y

Enter ten()

return 10

a=6

a=6

a=6

x=6

x=6

x=6

x=6,y=7

x=6,y=7

x=6,y=7

a=6,b=7

a=6,b=7,c=4

a=6,b=7,c=4

IN Flow

OUT Flow

Tian Tan @ Nanjing University

b=val(y)

x=val(a)

Page 99: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

99

Enter main()

Interprocedural Constant Propagation: An Example

Enter addOne(int x)

a = 6

b = addOne(a)

c = b - 3

b = ten()

c = a * b

y = x + 1

return y

Enter ten()

return 10

a=6

a=6

a=6

x=6

x=6

x=6

x=6,y=7

x=6,y=7

x=6,y=7

a=6,b=7

a=6,b=7,c=4

a=6,b=7,c=4

IN Flow

OUT Flow

Tian Tan @ Nanjing University

b=val(y)

x=val(a)

static void main() {int a, b, c;a = 6;b = addOne(a);c = b - 3;b = ten();c = a * b;

}

staticint addOne(int x) {int y = x + 1;return y;

}

static int ten() {return 10;

}

Page 100: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

100

Enter main()

Interprocedural Constant Propagation: An Example

Enter addOne(int x)

a = 6

b = addOne(a)

c = b - 3

b = ten()

c = a * b

y = x + 1

return y

Enter ten()

return 10

a=6

a=6

a=6

x=6

x=6

x=6

x=6,y=7

x=6,y=7

x=6,y=7

a=6,b=7

a=6,b=7,c=4

a=6,b=7,c=4

a=6,c=4

IN Flow

OUT Flow

Tian Tan @ Nanjing University

For each call node, kill data-flow value of the LHS variable. Its value will flow. to return site along the return edges.

b=val(y)

x=val(a)

static void main() {int a, b, c;a = 6;b = addOne(a);c = b - 3;b = ten();c = a * b;

}

staticint addOne(int x) {int y = x + 1;return y;

}

static int ten() {return 10;

}

Page 101: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

101

Enter main()

Interprocedural Constant Propagation: An Example

Enter addOne(int x)

a = 6

b = addOne(a)

c = b - 3

b = ten()

c = a * b

y = x + 1

return y

Enter ten()

return 10

a=6

a=6

a=6

x=6

x=6

x=6

x=6,y=7

x=6,y=7

x=6,y=7

a=6,b=7

a=6,b=7,c=4

a=6,b=7,c=4

a=6,c=4

b=10

IN Flow

OUT Flow

Tian Tan @ Nanjing UniversityReturn edge transfer:

pass return values

For each call node, kill data-flow value of the LHS variable. Its value will flow. to return site along the return edges.

b=val(y)

x=val(a)

static void main() {int a, b, c;a = 6;b = addOne(a);c = b - 3;b = ten();c = a * b;

}

staticint addOne(int x) {int y = x + 1;return y;

}

static int ten() {return 10;

}

Page 102: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

102

Enter main()

Interprocedural Constant Propagation: An Example

Enter addOne(int x)

a = 6

b = addOne(a)

c = b - 3

b = ten()

c = a * b

y = x + 1

return y

Enter ten()

return 10

a=6

a=6

a=6

x=6

x=6

x=6

x=6,y=7

x=6,y=7

x=6,y=7

a=6,b=7

a=6,b=7,c=4

a=6,b=7,c=4

a=6,c=4

b=10

a=6,b=10,c=4

IN Flow

OUT Flow

Tian Tan @ Nanjing UniversityReturn edge transfer:

pass return values

For each call node, kill data-flow value of the LHS variable. Its value will flow. to return site along the return edges.

b=val(y)

x=val(a)

static void main() {int a, b, c;a = 6;b = addOne(a);c = b - 3;b = ten();c = a * b;

}

staticint addOne(int x) {int y = x + 1;return y;

}

static int ten() {return 10;

}

Page 103: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

103

Enter main()

Interprocedural Constant Propagation: An Example

Enter addOne(int x)

a = 6

b = addOne(a)

c = b - 3

b = ten()

c = a * b

y = x + 1

return y

Enter ten()

return 10

a=6

a=6

a=6

x=6

x=6

x=6

x=6,y=7

x=6,y=7

x=6,y=7

a=6,b=7

a=6,b=7,c=4

a=6,b=7,c=4

b=10

Return edge transfer:pass return values

For each call node, kill data-flow value of the LHS variable. Its value will flow. to return site along the return edges. Otherwise, it may cause imprecision.

a=6,b=NAC,c=4

a=6,b=7,c=4

IN Flow

OUT Flow

Tian Tan @ Nanjing University

b=val(y)

x=val(a)

static void main() {int a, b, c;a = 6;b = addOne(a);c = b - 3;b = ten();c = a * b;

}

staticint addOne(int x) {int y = x + 1;return y;

}

static int ten() {return 10;

}

Page 104: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

104

Enter main()

Interprocedural Constant Propagation: An Example

Enter addOne(int x)

a = 6

b = addOne(a)

c = b - 3

b = ten()

c = a * b

y = x + 1

return y

Enter ten()

return 10

a=6

a=6

a=6,b=7

a=6

a=6,b=7,c=4

a=6,b=7,c=4

a=6,c=4

a=6,b=10,c=4

a=6,b=10,c=60

x=6

x=6

x=6

x=6,y=7

x=6,y=7

x=6,y=7

b=10

IN Flow

OUT Flow

Tian Tan @ Nanjing University

b=val(y)

x=val(a)

static void main() {int a, b, c;a = 6;b = addOne(a);c = b - 3;b = ten();c = a * b;

}

staticint addOne(int x) {int y = x + 1;return y;

}

static int ten() {return 10;

}

Page 105: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

static void main() {int a, b, c;a = 6;b = addOne(a);c = b - 3;b = ten();c = a * b;

}

staticint addOne(int x) {int y = x + 1;return y;

}

static int ten() {return 10;

}

105

Enter main()

Intraprocedural Constant Propagation: An Example

Enter addOne(int x)

a = 6

b = addOne(a)

c = b - 3

b = ten()

c = a * b

y = x + 1

return y

Enter ten()

return 10

a=6

a=6

a=6,b=NAC

a=6,b=NAC

a=6,b=NAC,c=NAC

a=6,b=NAC,c=NAC

x=NAC

x=NAC

x=NAC

x=NAC,y=NAC

x=NAC,y=NAC

x=NAC,y=NAC

a=6,b=NAC,c=NAC

a=6,b=NAC,c=NAC

a=6,b=NAC,c=NAC

IN Flow

OUT Flow

Tian Tan @ Nanjing University

Page 106: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

106

Enter main()

Interprocedural Constant Propagation: An Example

Enter addOne(int x)

a = 6

b = addOne(a)

c = b - 3

b = ten()

c = a * b

y = x + 1

return y

Enter ten()

return 10

a=6

a=6

a=6,b=7

a=6

a=6,b=7,c=4

a=6,b=7,c=4

a=6,c=4

a=6,b=10,c=4

a=6,b=10,c=60

x=6

x=6

x=6

x=6,y=7

x=6,y=7

x=6,y=7

b=10Interprocedural constant propagation

is more precise thanIntraprocedural constant propagation

IN Flow

OUT Flow

Tian Tan @ Nanjing University

b=val(y)

x=val(a)

static void main() {int a, b, c;a = 6;b = addOne(a);c = b - 3;b = ten();c = a * b;

}

staticint addOne(int x) {int y = x + 1;return y;

}

static int ten() {return 10;

}

Page 107: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

TheX You Need To Understand in This Lecture

• How to build call graph via class hierarchy analysis

• Concept of interprocedural control-flow graph

• Concept of interprocedural data-flow analysis

• Interprocedural constant propagation

Page 108: Static Program Analysis - Bitbucket · Class Hierarchy Analysis* (CHA) •Require the class hierarchy information (inheritance structure) of the whole program •Resolve a virtual

南京大学

李樾

谭添

计算机科学与技术系

程序设计语言

静态分析研究组

软件分析