static program analysis · 2020-06-12 · static program analysis 2020 spring. nanjing university...

153
Yue Li and Tian Tan Static Program Analysis 2020 Spring

Upload: others

Post on 24-Jul-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

Yue Li and Tian Tan

Static Program Analysis

2020 Spring

Page 2: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

Nanjing University

Tian Tan

2020

Pointer Analysis

Static Program Analysis

Foundations

Page 3: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

Contents

1. Pointer Analysis: Rules

2. How to Implement Pointer Analysis

3. Pointer Analysis: Algorithms

4. Pointer Analysis with Method Calls

3Tian Tan @ Nanjing University

Page 4: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

Contents

1. Pointer Analysis: Rules

2. How to Implement Pointer Analysis

3. Pointer Analysis: Algorithms

4. Pointer Analysis with Method Calls

4Tian Tan @ Nanjing University

Page 5: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

Pointer-Affecting Statements

Tian Tan @ Nanjing University 5

New x = new T()

Assign x = y

Store x.f = y

Load y = x.f

Call r = x.k(a, …)Will come back to this in

pointer analysis with method calls

First focus on these statements(suppose the program has just one method)

Page 6: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

Domains and Notations

6

Variables: x, y ∈ V

Fields: f, g ∈ F

Objects: oi, oj ∈ O

Instance fields: oi.f, oj.g ∈ O × F

Pointers: Pointer = V ⋃ (O × F)

Points-to relations: pt : Pointer → 𝒫(O)

• 𝒫(O) denotes the powerset of O• pt(p) denotes the points-to set of p

Tian Tan @ Nanjing University

Page 7: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

Rules

Kind Statement Rule

New i: x = new T() 𝑜𝑖 ∈ 𝑝𝑡(𝑥)

Assign x = y𝑜𝑖 ∈ 𝑝𝑡(𝑦)

𝑜𝑖 ∈ 𝑝𝑡(𝑥)

Store x.f = y 𝑜𝑖 ∈ 𝑝𝑡 𝑥 , 𝑜𝑗 ∈ 𝑝𝑡 𝑦

𝑜𝑗 ∈ 𝑝𝑡(𝑜𝑖 . 𝑓)

Load y = x.f 𝑜𝑖 ∈ 𝑝𝑡 𝑥 , 𝑜𝑗 ∈ 𝑝𝑡 𝑜𝑖 . 𝑓

𝑜𝑗 ∈ 𝑝𝑡(𝑦)

7Tian Tan @ Nanjing University

Page 8: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

Rules

Kind Statement Rule

New i: x = new T() 𝑜𝑖 ∈ 𝑝𝑡(𝑥)

Assign x = y𝑜𝑖 ∈ 𝑝𝑡(𝑦)

𝑜𝑖 ∈ 𝑝𝑡(𝑥)

Store x.f = y 𝑜𝑖 ∈ 𝑝𝑡 𝑥 , 𝑜𝑗 ∈ 𝑝𝑡 𝑦

𝑜𝑗 ∈ 𝑝𝑡(𝑜𝑖 . 𝑓)

Load y = x.f 𝑜𝑖 ∈ 𝑝𝑡 𝑥 , 𝑜𝑗 ∈ 𝑝𝑡 𝑜𝑖 . 𝑓

𝑜𝑗 ∈ 𝑝𝑡(𝑦)

8Tian Tan @ Nanjing University

← premises

← conclusion

← unconditional

Page 9: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

Rule: New

9

i: x = new T()

𝑜𝑖Conclusion

𝑜𝑖 ∈ 𝑝𝑡(𝑥)

Tian Tan @ Nanjing University

Page 10: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

Rule: Assign

10

𝑜𝑖Conclusion

x = y

Premises

𝑜𝑖 ∈ 𝑝𝑡(𝑦)

𝑜𝑖 ∈ 𝑝𝑡(𝑥)

Tian Tan @ Nanjing University

Page 11: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

Rule: Store

11

𝑜𝑖

x.f = y

𝑜𝑖 ∈ 𝑝𝑡 𝑥 , 𝑜𝑗 ∈ 𝑝𝑡 𝑦

𝑜𝑗 ∈ 𝑝𝑡(𝑜𝑖 . 𝑓)

𝑜𝑗𝑓

Tian Tan @ Nanjing University

Conclusion

Premises

Page 12: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

Rule: Load

12

𝑜𝑗

y = x.f

𝑜𝑖 ∈ 𝑝𝑡 𝑥 , 𝑜𝑗 ∈ 𝑝𝑡 𝑜𝑖 . 𝑓

𝑜𝑗 ∈ 𝑝𝑡(𝑦)

𝑜𝑖𝑓

Tian Tan @ Nanjing University

Conclusion

Premises

Page 13: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

RulesKind Rule Illustration

New𝑜𝑖 ∈ 𝑝𝑡 𝑥

Assign𝑜𝑖 ∈ 𝑝𝑡(𝑦)

𝑜𝑖 ∈ 𝑝𝑡(𝑥)

Store𝑜𝑖 ∈ 𝑝𝑡 𝑥 , 𝑜𝑗 ∈ 𝑝𝑡 𝑦

𝑜𝑗 ∈ 𝑝𝑡(𝑜𝑖 . 𝑓)

Load𝑜𝑖 ∈ 𝑝𝑡 𝑥 , 𝑜𝑗 ∈ 𝑝𝑡 𝑜𝑖 . 𝑓

𝑜𝑗 ∈ 𝑝𝑡(𝑦)

13

Conclusion

Premises

𝑜𝑗

y = x.f

𝑜𝑖𝑓

𝑜𝑖

x.f = y

𝑜𝑗𝑓

x = y

𝑜𝑖

i: x = new T()

𝑜𝑖

Tian Tan @ Nanjing University

Page 14: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

Contents

14

1. Pointer Analysis: Rules

2. How to Implement Pointer Analysis

3. Pointer Analysis: Algorithms

4. Pointer Analysis with Method Calls

Tian Tan @ Nanjing University

Page 15: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

Our Pointer Analysis Algorithms

• A complete whole-program pointer analysis

• Carefully designed for understandability

• Easy to follow and implement

15Tian Tan @ Nanjing University

Page 16: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

How to Implement Pointer Analysis?

• Essentially, pointer analysis is to propagate points-to information among pointers (variables & fields)

16

Kind Statement Rule

New i: x = new T() 𝑜𝑖 ∈ 𝑝𝑡(𝑥)

Assign x = y𝑜𝑖 ∈ 𝑝𝑡(𝒚)

𝑜𝑖 ∈ 𝑝𝑡(𝒙)

Store x.f = y𝑜𝑖 ∈ 𝑝𝑡 𝑥 , 𝑜𝑗 ∈ 𝑝𝑡 𝒚

𝑜𝑗 ∈ 𝑝𝑡(𝒐𝒊. 𝒇)

Load y = x.f𝑜𝑖 ∈ 𝑝𝑡 𝑥 , 𝑜𝑗 ∈ 𝑝𝑡 𝒐𝒊. 𝒇

𝑜𝑗 ∈ 𝑝𝑡(𝒚)

Tian Tan @ Nanjing University

Page 17: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

Pointer analysis as solving a system of inclusion constraints for pointers

Referred as Andersen-style analysis*

How to Implement Pointer Analysis?

• Essentially, pointer analysis is to propagate points-to information among pointers (variables & fields)

17

Kind Statement Rule

New i: x = new T() 𝑜𝑖 ∈ 𝑝𝑡(𝑥)

Assign x = y𝑜𝑖 ∈ 𝑝𝑡(𝒚)

𝑜𝑖 ∈ 𝑝𝑡(𝒙)

Store x.f = y𝑜𝑖 ∈ 𝑝𝑡 𝑥 , 𝑜𝑗 ∈ 𝑝𝑡 𝒚

𝑜𝑗 ∈ 𝑝𝑡(𝒐𝒊. 𝒇)

Load y = x.f𝑜𝑖 ∈ 𝑝𝑡 𝑥 , 𝑜𝑗 ∈ 𝑝𝑡 𝒐𝒊. 𝒇

𝑜𝑗 ∈ 𝑝𝑡(𝒚)

Tian Tan @ Nanjing University

Lars Ole Andersen, 1994. “Program Analysis and Specialization for the C Programming Language”. Ph.D. Thesis. University of Copenhagen.

*

Page 18: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

How to Implement Pointer Analysis?

• Essentially, pointer analysis is to propagate points-to information among pointers (variables & fields)

18

Kind Statement Rule

New i: x = new T() 𝑜𝑖 ∈ 𝑝𝑡(𝑥)

Assign x = y𝑜𝑖 ∈ 𝑝𝑡(𝒚)

𝑜𝑖 ∈ 𝑝𝑡(𝒙)

Store x.f = y𝑜𝑖 ∈ 𝑝𝑡 𝑥 , 𝑜𝑗 ∈ 𝑝𝑡 𝒚

𝑜𝑗 ∈ 𝑝𝑡(𝒐𝒊. 𝒇)

Load y = x.f𝑜𝑖 ∈ 𝑝𝑡 𝑥 , 𝑜𝑗 ∈ 𝑝𝑡 𝒐𝒊. 𝒇

𝑜𝑗 ∈ 𝑝𝑡(𝒚)

Key to implementation: when 𝑝𝑡 𝑥 is changed, propagate the changed part to the related pointers of 𝑥

Tian Tan @ Nanjing University

Page 19: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

Solution• We use a graph to connect

related pointers• When 𝑝𝑡 𝑥 changes,

propagate the changed part to 𝑥’s successors

How to Implement Pointer Analysis?

• Essentially, pointer analysis is to propagate points-to information among pointers (variables & fields)

19

Kind Statement Rule

New i: x = new T() 𝑜𝑖 ∈ 𝑝𝑡(𝑥)

Assign x = y𝑜𝑖 ∈ 𝑝𝑡(𝒚)

𝑜𝑖 ∈ 𝑝𝑡(𝒙)

Store x.f = y𝑜𝑖 ∈ 𝑝𝑡 𝑥 , 𝑜𝑗 ∈ 𝑝𝑡 𝒚

𝑜𝑗 ∈ 𝑝𝑡(𝒐𝒊. 𝒇)

Load y = x.f𝑜𝑖 ∈ 𝑝𝑡 𝑥 , 𝑜𝑗 ∈ 𝑝𝑡 𝒐𝒊. 𝒇

𝑜𝑗 ∈ 𝑝𝑡(𝒚)

Key to implementation: when 𝑝𝑡 𝑥 is changed, propagate the changed part to the related pointers of 𝑥

Tian Tan @ Nanjing University

Page 20: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

Pointer Flow Graph (PFG)

Pointer flow graph of a program is a directed graph that expresses how objects flow among the pointers in the program.

20Tian Tan @ Nanjing University

Page 21: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

Pointer Flow Graph (PFG)

Pointer flow graph of a program is a directed graph that expresses how objects flow among the pointers in the program.

• Nodes: Pointer = V ⋃ (O × F)

A node n represents a variable or a field of an abstract object

• Edges: Pointer × Pointer

An edge 𝑥 → 𝑦 means that the objects pointed by pointer 𝑥may flow to (and also be pointed to by) pointer 𝑦

21Tian Tan @ Nanjing University

Page 22: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

Pointer Flow Graph: Edges

• PFG edges are added according to the statements of the program and the corresponding rules

22

Kind Statement Rule

New i: x = new T() 𝑜𝑖 ∈ 𝑝𝑡(𝑥)

Assign x = y𝑜𝑖 ∈ 𝑝𝑡(𝒚)

𝑜𝑖 ∈ 𝑝𝑡(𝒙)

Store x.f = y𝑜𝑖 ∈ 𝑝𝑡 𝑥 , 𝑜𝑗 ∈ 𝑝𝑡 𝒚

𝑜𝑗 ∈ 𝑝𝑡(𝒐𝒊. 𝒇)

Load y = x.f𝑜𝑖 ∈ 𝑝𝑡 𝑥 , 𝑜𝑗 ∈ 𝑝𝑡 𝒐𝒊. 𝒇

𝑜𝑗 ∈ 𝑝𝑡(𝒚)

Tian Tan @ Nanjing University

Page 23: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

Pointer Flow Graph: Edges

• PFG edges are added according to the statements of the program and the corresponding rules

23

Kind Statement Rule PFG Edge

New i: x = new T() 𝑜𝑖 ∈ 𝑝𝑡(𝑥) N/A

Assign x = y𝑜𝑖 ∈ 𝑝𝑡(𝒚)

𝑜𝑖 ∈ 𝑝𝑡(𝒙)𝑥 ← 𝑦

Store x.f = y𝑜𝑖 ∈ 𝑝𝑡 𝑥 , 𝑜𝑗 ∈ 𝑝𝑡 𝒚

𝑜𝑗 ∈ 𝑝𝑡(𝒐𝒊. 𝒇)𝑜𝑖 . 𝑓 ← 𝑦

Load y = x.f𝑜𝑖 ∈ 𝑝𝑡 𝑥 , 𝑜𝑗 ∈ 𝑝𝑡 𝒐𝒊. 𝒇

𝑜𝑗 ∈ 𝑝𝑡(𝒚)𝑦 ← 𝑜𝑖 . 𝑓

Tian Tan @ Nanjing University

Page 24: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

Pointer Flow Graph: An Example

24

Program

(𝑜𝑖 ∈ 𝑝𝑡 𝑐 , 𝑜𝑖 ∈ 𝑝𝑡 𝑑 )

Pointer flow graph

a = b; ①

c.f = a; ②

d = c; ③

c.f = d; ④

e = d.f; ⑤

➢ Variable node

➢ Instance field node

v

Oi.f

Tian Tan @ Nanjing University

Page 25: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

Pointer Flow Graph: An Example

25

Program

(𝑜𝑖 ∈ 𝑝𝑡 𝑐 , 𝑜𝑖 ∈ 𝑝𝑡 𝑑 )

Pointer flow graph

a = b; ①

c.f = a; ②

d = c; ③

c.f = d; ④

e = d.f; ⑤

➢ Variable node

➢ Instance field node

v

Oi.f

Tian Tan @ Nanjing University

Page 26: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

Pointer Flow Graph: An Example

26

Program

(𝑜𝑖 ∈ 𝑝𝑡 𝑐 , 𝑜𝑖 ∈ 𝑝𝑡 𝑑 )

Pointer flow graph

a = b; ①

c.f = a; ②

d = c; ③

c.f = d; ④

e = d.f; ⑤

ba

➢ Variable node

➢ Instance field node

v

Oi.f

Tian Tan @ Nanjing University

Page 27: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

Pointer Flow Graph: An Example

27

Program

(𝑜𝑖 ∈ 𝑝𝑡 𝑐 , 𝑜𝑖 ∈ 𝑝𝑡 𝑑 )

Pointer flow graph

a = b; ①

c.f = a; ②

d = c; ③

c.f = d; ④

e = d.f; ⑤

ba

➢ Variable node

➢ Instance field node

v

Oi.f

Tian Tan @ Nanjing University

Page 28: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

Pointer Flow Graph: An Example

28

Program

(𝑜𝑖 ∈ 𝑝𝑡 𝑐 , 𝑜𝑖 ∈ 𝑝𝑡 𝑑 )

Pointer flow graph

a = b; ①

c.f = a; ②

d = c; ③

c.f = d; ④

e = d.f; ⑤

ba

Oi.f

➢ Variable node

➢ Instance field node

v

Oi.f

Tian Tan @ Nanjing University

Page 29: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

Pointer Flow Graph: An Example

29

Program

(𝑜𝑖 ∈ 𝑝𝑡 𝑐 , 𝑜𝑖 ∈ 𝑝𝑡 𝑑 )

Pointer flow graph

a = b; ①

c.f = a; ②

d = c; ③

c.f = d; ④

e = d.f; ⑤

ba

d

Oi.f

c

➢ Variable node

➢ Instance field node

v

Oi.f

Tian Tan @ Nanjing University

Page 30: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

③④

Pointer Flow Graph: An Example

30

Program

(𝑜𝑖 ∈ 𝑝𝑡 𝑐 , 𝑜𝑖 ∈ 𝑝𝑡 𝑑 )

Pointer flow graph

a = b; ①

c.f = a; ②

d = c; ③

c.f = d; ④

e = d.f; ⑤

ba

d

Oi.f

c

➢ Variable node

➢ Instance field node

v

Oi.f

Tian Tan @ Nanjing University

Page 31: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

Pointer Flow Graph: An Example

31

Program

(𝑜𝑖 ∈ 𝑝𝑡 𝑐 , 𝑜𝑖 ∈ 𝑝𝑡 𝑑 )

Pointer flow graph

a = b; ①

c.f = a; ②

d = c; ③

c.f = d; ④

e = d.f; ⑤

ba

d

Oi.f

c

③④

➢ Variable node

➢ Instance field node

v

Oi.f

Tian Tan @ Nanjing University

Page 32: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

Pointer Flow Graph: An Example

32

Program

(𝑜𝑖 ∈ 𝑝𝑡 𝑐 , 𝑜𝑖 ∈ 𝑝𝑡 𝑑 )

Pointer flow graph

a = b; ①

c.f = a; ②

d = c; ③

c.f = d; ④

e = d.f; ⑤

ba

d

Oi.fe

c

③④

➢ Variable node

➢ Instance field node

v

Oi.f

Tian Tan @ Nanjing University

Page 33: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

Pointer Flow Graph: An Example

33

Program

(𝑜𝑖 ∈ 𝑝𝑡 𝑐 , 𝑜𝑖 ∈ 𝑝𝑡 𝑑 )

Pointer flow graph

a = b; ①

c.f = a; ②

d = c; ③

c.f = d; ④

e = d.f; ⑤

ba

d

Oi.fe

c

③④

➢ Variable node

➢ Instance field node

v

Oi.f

Tian Tan @ Nanjing University

Page 34: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

Pointer Flow Graph: An Example

34

Program

(𝑜𝑖 ∈ 𝑝𝑡 𝑐 , 𝑜𝑖 ∈ 𝑝𝑡 𝑑 )

Pointer flow graph

a = b; ①

c.f = a; ②

d = c; ③

c.f = d; ④

e = d.f; ⑤

ba

d

Oi.fe

c

③④

➢ Variable node

➢ Instance field node

v

Oi.f

With PFG, pointer analysis can be solved by computing transitive closure of the PFG

Tian Tan @ Nanjing University

Page 35: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

Pointer Flow Graph: An Example

35

Program

(𝑜𝑖 ∈ 𝑝𝑡 𝑐 , 𝑜𝑖 ∈ 𝑝𝑡 𝑑 )

Pointer flow graph

a = b; ①

c.f = a; ②

d = c; ③

c.f = d; ④

e = d.f; ⑤

ba

d

Oi.fe

c

③④

➢ Variable node

➢ Instance field node

v

Oi.f

With PFG, pointer analysis can be solved by computing transitive closure of the PFG

E.g, e is reachable from b on the PFG, which means that the objects pointed by b may flow to and also be pointed by e

Tian Tan @ Nanjing University

Page 36: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

Pointer Flow Graph: An Example

36

Program

(𝑜𝑖 ∈ 𝑝𝑡 𝑐 , 𝑜𝑖 ∈ 𝑝𝑡 𝑑 )

Pointer flow graph

a = b; ①

c.f = a; ②

d = c; ③

c.f = d; ④

e = d.f; ⑤

ba

d

Oi.fe

c

③④

➢ Variable node

➢ Instance field node

v

Oi.f

With PFG, pointer analysis can be solved by computing transitive closure of the PFG

E.g, e is reachable from b on the PFG, which means that the objects pointed by b may flow to and also be pointed by e

Tian Tan @ Nanjing University

j: b = new T();

𝑝𝑡 𝑏 = {𝑜𝑗}

Page 37: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

Pointer Flow Graph: An Example

37

Program

(𝑜𝑖 ∈ 𝑝𝑡 𝑐 , 𝑜𝑖 ∈ 𝑝𝑡 𝑑 )

Pointer flow graph

a = b; ①

c.f = a; ②

d = c; ③

c.f = d; ④

e = d.f; ⑤

ba

d

Oi.fe

c

③④

➢ Variable node

➢ Instance field node

v

Oi.f

With PFG, pointer analysis can be solved by computing transitive closure of the PFG

E.g, e is reachable from b on the PFG, which means that the objects pointed by b may flow to and also be pointed by e

Tian Tan @ Nanjing University

j: b = new T();

𝑝𝑡 𝑏 = {𝑜𝑗}𝑝𝑡 𝑎 = {𝑜𝑗}

𝑝𝑡 𝑒 = {𝑜𝑗} 𝑝𝑡 𝑜𝑖 . 𝑓 = {𝑜𝑗}

Page 38: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

Implementing Pointer Analysis

38

1. Build pointer flow graph (PFG)

2. Propagate points-to information on PFG

Tian Tan @ Nanjing University

Page 39: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

Implementing Pointer Analysis

39

1. Build pointer flow graph (PFG)

2. Propagate points-to information on PFG

Mutually dependent

Tian Tan @ Nanjing University

Page 40: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

③④

Implementing Pointer Analysis

40

1. Build pointer flow graph (PFG)

2. Propagate points-to information on PFG

Program(𝑜𝑖 ∈ 𝑝𝑡 𝑐 , 𝑜𝑖 ∈ 𝑝𝑡 𝑑 )

Pointer flow graph

a = b; ①

c.f = a; ②

d = c; ③

c.f = d; ④

e = d.f; ⑤

ba

d

Oi.fe

c

Mutually dependent

Tian Tan @ Nanjing University

Page 41: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

③④

⑤ PFG is dynamically updated during pointer analysis

Implementing Pointer Analysis

41

1. Build pointer flow graph (PFG)

2. Propagate points-to information on PFG

Program(𝑜𝑖 ∈ 𝑝𝑡 𝑐 , 𝑜𝑖 ∈ 𝑝𝑡 𝑑 )

Pointer flow graph

a = b; ①

c.f = a; ②

d = c; ③

c.f = d; ④

e = d.f; ⑤

ba

d

Oi.fe

c

Mutually dependent

Tian Tan @ Nanjing University

Page 42: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

Contents

42

1. Pointer Analysis: Rules

2. How to Implement Pointer Analysis

3. Pointer Analysis: Algorithms

4. Pointer Analysis with Method Calls

Tian Tan @ Nanjing University

Page 43: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

Pointer Analysis: Algorithms

43

Solve(𝑆)WL = [ ], PFG = {}foreach i: x = new T() ∈ 𝑆 do

add 𝑥, {𝑜𝑖} to WL

foreach x = y ∈ 𝑆 doAddEdge(y, x)

while WL is not empty doremove 𝑛, 𝑝𝑡𝑠 from WLΔ = pts – pt(n)Propagate(n, Δ)if n represents a variable x then

foreach 𝑜𝑖 ∈ Δ doforeach x.f = y ∈ 𝑆 do

AddEdge(y, 𝑜𝑖 . 𝑓)foreach y = x.f ∈ 𝑆 do

AddEdge(𝑜𝑖 . 𝑓, y)

Propagate(n, pts)

if pts is not empty then

pt(n) ⋃= pts

foreach n → s ∈ PFG do

add 𝑠, pts to WL

AddEdge(s, t)

if s → t ∉ PFG then

add s → t to PFG

if 𝑝𝑡(𝑠) is not empty then

add 𝑡, 𝑝𝑡(𝑠) to WL

SSet of statements of the input program

WL Work list

PFG Pointer flow graphTian Tan @ Nanjing University

Page 44: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

Pointer Analysis: Algorithms

44

Solve(𝑆)WL = [ ], PFG = {}foreach i: x = new T() ∈ 𝑆 do

add 𝑥, {𝑜𝑖} to WL

foreach x = y ∈ 𝑆 doAddEdge(y, x)

while WL is not empty doremove 𝑛, 𝑝𝑡𝑠 from WLΔ = pts – pt(n)Propagate(n, Δ)if n represents a variable x then

foreach 𝑜𝑖 ∈ Δ doforeach x.f = y ∈ 𝑆 do

AddEdge(y, 𝑜𝑖 . 𝑓)foreach y = x.f ∈ 𝑆 do

AddEdge(𝑜𝑖 . 𝑓, y)

Propagate(n, pts)

if pts is not empty then

pt(n) ⋃= pts

foreach n → s ∈ PFG do

add 𝑠, pts to WL

AddEdge(s, t)

if s → t ∉ PFG then

add s → t to PFG

if 𝑝𝑡(𝑠) is not empty then

add 𝑡, 𝑝𝑡(𝑠) to WL

SSet of statements of the input program

WL Work list

PFG Pointer flow graphTian Tan @ Nanjing University

Page 45: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

Pointer Analysis: Algorithms

45

Solve(𝑆)WL = [ ], PFG = {}foreach i: x = new T() ∈ 𝑆 do

add 𝑥, {𝑜𝑖} to WL

foreach x = y ∈ 𝑆 doAddEdge(y, x)

while WL is not empty doremove 𝑛, 𝑝𝑡𝑠 from WLΔ = pts – pt(n)Propagate(n, Δ)if n represents a variable x then

foreach 𝑜𝑖 ∈ Δ doforeach x.f = y ∈ 𝑆 do

AddEdge(y, 𝑜𝑖 . 𝑓)foreach y = x.f ∈ 𝑆 do

AddEdge(𝑜𝑖 . 𝑓, y)

Propagate(n, pts)

if pts is not empty then

pt(n) ⋃= pts

foreach n → s ∈ PFG do

add 𝑠, pts to WL

AddEdge(s, t)

if s → t ∉ PFG then

add s → t to PFG

if 𝑝𝑡(𝑠) is not empty then

add 𝑡, 𝑝𝑡(𝑠) to WL

SSet of statements of the input program

WL Work list

PFG Pointer flow graphTian Tan @ Nanjing University

Page 46: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

Worklist (WL)

• Worklist contains the points-to information to be processed• 𝑊𝐿 ⊆ Pointer, 𝒫(O) ∗

• Each worklist entry 𝑛, 𝑝𝑡𝑠 is a pair of pointer 𝑛 and points-to set 𝑝𝑡𝑠, which means that 𝑝𝑡𝑠 should be propagated to 𝑝𝑡(𝑛)• E.g., 𝑥, {𝑜𝑖} , 𝑦, {𝑜𝑗 , 𝑜𝑘} , 𝑜𝑗 . 𝑓, {𝑜𝑙} …

46Tian Tan @ Nanjing University

Page 47: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

Main Algorithm

47

Solve(𝑆)WL = [ ], PFG = {}foreach i: x = new T() ∈ 𝑆 do

add 𝑥, {𝑜𝑖} to WL

foreach x = y ∈ 𝑆 doAddEdge(y, x)

while WL is not empty doremove 𝑛, 𝑝𝑡𝑠 from WLΔ = pts – pt(n)Propagate(n, Δ)if n represents a variable x then

foreach 𝑜𝑖 ∈ Δ doforeach x.f = y ∈ 𝑆 do

AddEdge(y, 𝑜𝑖 . 𝑓)foreach y = x.f ∈ 𝑆 do

AddEdge(𝑜𝑖 . 𝑓, y)Tian Tan @ Nanjing University

Page 48: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

Solve(𝑆)WL = [ ], PFG = {}foreach i: x = new T() ∈ 𝑆 do

add 𝑥, {𝑜𝑖} to WL

foreach x = y ∈ 𝑆 doAddEdge(y, x)

while WL is not empty doremove 𝑛, 𝑝𝑡𝑠 from WLΔ = pts – pt(n)Propagate(n, Δ)if n represents a variable x then

foreach 𝑜𝑖 ∈ Δ doforeach x.f = y ∈ 𝑆 do

AddEdge(y, 𝑜𝑖 . 𝑓)foreach y = x.f ∈ 𝑆 do

AddEdge(𝑜𝑖 . 𝑓, y)

Handling of New and Assign

48

Kind Statement Rule PFG Edge

New i: x = new T() 𝑜𝑖 ∈ 𝑝𝑡(𝑥) N/A

Assign x = y𝑜𝑖 ∈ 𝑝𝑡(𝒚)

𝑜𝑖 ∈ 𝑝𝑡(𝒙)𝑥 ← 𝑦

Initialize the analysis

Tian Tan @ Nanjing University

Page 49: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

Solve(𝑆)WL = [ ], PFG = {}foreach i: x = new T() ∈ 𝑆 do

add 𝑥, {𝑜𝑖} to WL

foreach x = y ∈ 𝑆 doAddEdge(y, x)

while WL is not empty doremove 𝑛, 𝑝𝑡𝑠 from WLΔ = pts – pt(n)Propagate(n, Δ)if n represents a variable x then

foreach 𝑜𝑖 ∈ Δ doforeach x.f = y ∈ 𝑆 do

AddEdge(y, 𝑜𝑖 . 𝑓)foreach y = x.f ∈ 𝑆 do

AddEdge(𝑜𝑖 . 𝑓, y)

Handling of New and Assign

49

Kind Statement Rule PFG Edge

New i: x = new T() 𝑜𝑖 ∈ 𝑝𝑡(𝑥) N/A

Assign x = y𝑜𝑖 ∈ 𝑝𝑡(𝒚)

𝑜𝑖 ∈ 𝑝𝑡(𝒙)𝑥 ← 𝑦

Initialize the analysis

Add assign edges to PFG

Tian Tan @ Nanjing University

Page 50: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

Solve(𝑆)WL = [ ], PFG = {}foreach i: x = new T() ∈ 𝑆 do

add 𝑥, {𝑜𝑖} to WL

foreach x = y ∈ 𝑆 doAddEdge(y, x)

while WL is not empty doremove 𝑛, 𝑝𝑡𝑠 from WLΔ = pts – pt(n)Propagate(n, Δ)if n represents a variable x then

foreach 𝑜𝑖 ∈ Δ doforeach x.f = y ∈ 𝑆 do

AddEdge(y, 𝑜𝑖 . 𝑓)foreach y = x.f ∈ 𝑆 do

AddEdge(𝑜𝑖 . 𝑓, y)

Handling of New and Assign

50

① Do nothing if s → t is already in PFG

Kind Statement Rule PFG Edge

New i: x = new T() 𝑜𝑖 ∈ 𝑝𝑡(𝑥) N/A

Assign x = y𝑜𝑖 ∈ 𝑝𝑡(𝒚)

𝑜𝑖 ∈ 𝑝𝑡(𝒙)𝑥 ← 𝑦

AddEdge(s, t)

if s → t ∉ PFG then

add s → t to PFG

if 𝑝𝑡(𝑠) is not empty then

add 𝑡, 𝑝𝑡(𝑠) to WL

Tian Tan @ Nanjing University

Page 51: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

Solve(𝑆)WL = [ ], PFG = {}foreach i: x = new T() ∈ 𝑆 do

add 𝑥, {𝑜𝑖} to WL

foreach x = y ∈ 𝑆 doAddEdge(y, x)

while WL is not empty doremove 𝑛, 𝑝𝑡𝑠 from WLΔ = pts – pt(n)Propagate(n, Δ)if n represents a variable x then

foreach 𝑜𝑖 ∈ Δ doforeach x.f = y ∈ 𝑆 do

AddEdge(y, 𝑜𝑖 . 𝑓)foreach y = x.f ∈ 𝑆 do

AddEdge(𝑜𝑖 . 𝑓, y)

Handling of New and Assign

51

① Do nothing if s → t is already in PFG

② Add PFG edge

①②

Kind Statement Rule PFG Edge

New i: x = new T() 𝑜𝑖 ∈ 𝑝𝑡(𝑥) N/A

Assign x = y𝑜𝑖 ∈ 𝑝𝑡(𝒚)

𝑜𝑖 ∈ 𝑝𝑡(𝒙)𝑥 ← 𝑦

AddEdge(s, t)

if s → t ∉ PFG then

add s → t to PFG

if 𝑝𝑡(𝑠) is not empty then

add 𝑡, 𝑝𝑡(𝑠) to WL

Tian Tan @ Nanjing University

Page 52: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

Solve(𝑆)WL = [ ], PFG = {}foreach i: x = new T() ∈ 𝑆 do

add 𝑥, {𝑜𝑖} to WL

foreach x = y ∈ 𝑆 doAddEdge(y, x)

while WL is not empty doremove 𝑛, 𝑝𝑡𝑠 from WLΔ = pts – pt(n)Propagate(n, Δ)if n represents a variable x then

foreach 𝑜𝑖 ∈ Δ doforeach x.f = y ∈ 𝑆 do

AddEdge(y, 𝑜𝑖 . 𝑓)foreach y = x.f ∈ 𝑆 do

AddEdge(𝑜𝑖 . 𝑓, y)

Handling of New and Assign

52

① Do nothing if s → t is already in PFG

② Add PFG edge

③ Ensure every object pointed by 𝑠 is also pointed by 𝑡

①②

Kind Statement Rule PFG Edge

New i: x = new T() 𝑜𝑖 ∈ 𝑝𝑡(𝑥) N/A

Assign x = y𝑜𝑖 ∈ 𝑝𝑡(𝒚)

𝑜𝑖 ∈ 𝑝𝑡(𝒙)𝑥 ← 𝑦

AddEdge(s, t)

if s → t ∉ PFG then

add s → t to PFG

if 𝑝𝑡(𝑠) is not empty then

add 𝑡, 𝑝𝑡(𝑠) to WL

Tian Tan @ Nanjing University

Page 53: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

Solve(𝑆)WL = [ ], PFG = {}foreach i: x = new T() ∈ 𝑆 do

add 𝑥, {𝑜𝑖} to WL

foreach x = y ∈ 𝑆 doAddEdge(y, x)

while WL is not empty doremove 𝑛, 𝑝𝑡𝑠 from WLΔ = pts – pt(n)Propagate(n, Δ)if n represents a variable x then

foreach 𝑜𝑖 ∈ Δ doforeach x.f = y ∈ 𝑆 do

AddEdge(y, 𝑜𝑖 . 𝑓)foreach y = x.f ∈ 𝑆 do

AddEdge(𝑜𝑖 . 𝑓, y)

Handling of New and Assign

53

Kind Statement Rule PFG Edge

New i: x = new T() 𝑜𝑖 ∈ 𝑝𝑡(𝑥) N/A

Assign x = y𝑜𝑖 ∈ 𝑝𝑡(𝒚)

𝑜𝑖 ∈ 𝑝𝑡(𝒙)𝑥 ← 𝑦

Tian Tan @ Nanjing University

Process the entries in WL

Page 54: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

remove from WLSolve(𝑆)

WL = [ ], PFG = {}foreach i: x = new T() ∈ 𝑆 do

add 𝑥, {𝑜𝑖} to WL

foreach x = y ∈ 𝑆 doAddEdge(y, x)

while WL is not empty doremove 𝑛, 𝑝𝑡𝑠 from WLΔ = pts – pt(n)Propagate(n, Δ)if n represents a variable x then

foreach 𝑜𝑖 ∈ Δ doforeach x.f = y ∈ 𝑆 do

AddEdge(y, 𝑜𝑖 . 𝑓)foreach y = x.f ∈ 𝑆 do

AddEdge(𝑜𝑖 . 𝑓, y)

Handling of New and Assign

54

Kind Statement Rule PFG Edge

New i: x = new T() 𝑜𝑖 ∈ 𝑝𝑡(𝑥) N/A

Assign x = y𝑜𝑖 ∈ 𝑝𝑡(𝒚)

𝑜𝑖 ∈ 𝑝𝑡(𝒙)𝑥 ← 𝑦

Tian Tan @ Nanjing University

Process the entries in WL

𝑥, {𝑜1, 𝑜3}𝑝𝑡 𝑥 = {𝑜1, 𝑜2}

Δ = 𝑝𝑡𝑠 – 𝑝𝑡 𝑥= {𝑜1, 𝑜3} - {𝑜1, 𝑜2}= 𝑜3

Propagate(𝑥, 𝑜3 )

Page 55: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

Solve(𝑆)WL = [ ], PFG = {}foreach i: x = new T() ∈ 𝑆 do

add 𝑥, {𝑜𝑖} to WL

foreach x = y ∈ 𝑆 doAddEdge(y, x)

while WL is not empty doremove 𝑛, 𝑝𝑡𝑠 from WLΔ = pts – pt(n)Propagate(n, Δ)if n represents a variable x then

foreach 𝑜𝑖 ∈ Δ doforeach x.f = y ∈ 𝑆 do

AddEdge(y, 𝑜𝑖 . 𝑓)foreach y = x.f ∈ 𝑆 do

AddEdge(𝑜𝑖 . 𝑓, y)

Handling of New and Assign

55

Propagate(n, pts)

if pts is not empty then

pt(n) ⋃= pts

foreach n → s ∈ PFG do

add 𝑠, pts to WL

① Do nothing if 𝑝𝑡𝑠 is empty

Kind Statement Rule PFG Edge

New i: x = new T() 𝑜𝑖 ∈ 𝑝𝑡(𝑥) N/A

Assign x = y𝑜𝑖 ∈ 𝑝𝑡(𝒚)

𝑜𝑖 ∈ 𝑝𝑡(𝒙)𝑥 ← 𝑦

Tian Tan @ Nanjing University

Page 56: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

Solve(𝑆)WL = [ ], PFG = {}foreach i: x = new T() ∈ 𝑆 do

add 𝑥, {𝑜𝑖} to WL

foreach x = y ∈ 𝑆 doAddEdge(y, x)

while WL is not empty doremove 𝑛, 𝑝𝑡𝑠 from WLΔ = pts – pt(n)Propagate(n, Δ)if n represents a variable x then

foreach 𝑜𝑖 ∈ Δ doforeach x.f = y ∈ 𝑆 do

AddEdge(y, 𝑜𝑖 . 𝑓)foreach y = x.f ∈ 𝑆 do

AddEdge(𝑜𝑖 . 𝑓, y)

Handling of New and Assign

56

Propagate(n, pts)

if pts is not empty then

pt(n) ⋃= pts

foreach n → s ∈ PFG do

add 𝑠, pts to WL

① Do nothing if 𝑝𝑡𝑠 is empty

② Propagate 𝒑𝒕𝒔 to points-to set of 𝒏

①②

Kind Statement Rule PFG Edge

New i: x = new T() 𝑜𝑖 ∈ 𝑝𝑡(𝑥) N/A

Assign x = y𝑜𝑖 ∈ 𝑝𝑡(𝒚)

𝑜𝑖 ∈ 𝑝𝑡(𝒙)𝑥 ← 𝑦

Tian Tan @ Nanjing University

Page 57: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

Solve(𝑆)WL = [ ], PFG = {}foreach i: x = new T() ∈ 𝑆 do

add 𝑥, {𝑜𝑖} to WL

foreach x = y ∈ 𝑆 doAddEdge(y, x)

while WL is not empty doremove 𝑛, 𝑝𝑡𝑠 from WLΔ = pts – pt(n)Propagate(n, Δ)if n represents a variable x then

foreach 𝑜𝑖 ∈ Δ doforeach x.f = y ∈ 𝑆 do

AddEdge(y, 𝑜𝑖 . 𝑓)foreach y = x.f ∈ 𝑆 do

AddEdge(𝑜𝑖 . 𝑓, y)

Handling of New and Assign

57

Propagate(n, pts)

if pts is not empty then

pt(n) ⋃= pts

foreach n → s ∈ PFG do

add 𝑠, pts to WL

① Do nothing if 𝑝𝑡𝑠 is empty

② Propagate 𝒑𝒕𝒔 to points-to set of 𝒏

③ Propagate 𝑝𝑡𝑠 (the changed part) to 𝑛’s successors on PFG

①②

Kind Statement Rule PFG Edge

New i: x = new T() 𝑜𝑖 ∈ 𝑝𝑡(𝑥) N/A

Assign x = y𝑜𝑖 ∈ 𝑝𝑡(𝒚)

𝑜𝑖 ∈ 𝑝𝑡(𝒙)𝑥 ← 𝑦

Tian Tan @ Nanjing University

Page 58: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

Differential Propagation

Why?

58

Solve(𝑆)…while WL is not empty do

remove 𝑛, 𝑝𝑡𝑠 from WLΔ = pts – pt(n)Propagate(n, Δ)…

Tian Tan @ Nanjing University

Page 59: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

Differential Propagation• Differential propagation is employed to avoid propagation and

processing of redundant points-to information

• Insight: existing points-to information in 𝑝𝑡(𝑛) have already been propagated to 𝑛’s successors, and no need to be propagated again

59

Solve(𝑆)…while WL is not empty do

remove 𝑛, 𝑝𝑡𝑠 from WLΔ = pts – pt(n)Propagate(n, Δ)…

Tian Tan @ Nanjing University

Page 60: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

Differential Propagation• Differential propagation is employed to avoid propagation and

processing of redundant points-to information

• Insight: existing points-to information in 𝑝𝑡(𝑛) have already been propagated to 𝑛’s successors, and no need to be propagated again

60

Solve(𝑆)…while WL is not empty do

remove 𝑛, 𝑝𝑡𝑠 from WLΔ = pts – pt(n)Propagate(n, Δ)…

ba

d

c

𝑝𝑡 𝑎 = {𝑜1, 𝑜2, 𝑜3} 𝑝𝑡 𝑏 = {𝑜1, 𝑜3, 𝑜5}

PFG

𝑝𝑡 𝑑 = {}

𝑝𝑡 𝑐 = {}

Tian Tan @ Nanjing University

Page 61: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

Differential Propagation• Differential propagation is employed to avoid propagation and

processing of redundant points-to information

• Insight: existing points-to information in 𝑝𝑡(𝑛) have already been propagated to 𝑛’s successors, and no need to be propagated again

61

Solve(𝑆)…while WL is not empty do

remove 𝑛, 𝑝𝑡𝑠 from WLΔ = pts – pt(n)Propagate(n, Δ)…

ba

d

c

𝑝𝑡 𝑎 = {𝑜1, 𝑜2, 𝑜3} 𝑝𝑡 𝑏 = {𝑜1, 𝑜3, 𝑜5}

{𝑜1, 𝑜2, 𝑜3}

PFG

𝑝𝑡 𝑑 = {𝑜1, 𝑜2, 𝑜3}

𝑝𝑡 𝑐 = {𝑜1, 𝑜2, 𝑜3}

{𝑜1, 𝑜2, 𝑜3}

Tian Tan @ Nanjing University

Page 62: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

Differential Propagation• Differential propagation is employed to avoid propagation and

processing of redundant points-to information

• Insight: existing points-to information in 𝑝𝑡(𝑛) have already been propagated to 𝑛’s successors, and no need to be propagated again

62

Solve(𝑆)…while WL is not empty do

remove 𝑛, 𝑝𝑡𝑠 from WLΔ = pts – pt(n)Propagate(n, Δ)…

ba

d

c

𝑝𝑡 𝑎 = {𝑜1, 𝑜2, 𝑜3} 𝑝𝑡 𝑏 = {𝑜1, 𝑜3, 𝑜5}

{𝑜1, 𝑜3, 𝑜5}

{𝑜1, 𝑜3, 𝑜5}

PFG

𝑝𝑡 𝑑 = {𝑜1, 𝑜2, 𝑜3, 𝑜5}

𝑝𝑡 𝑐 = {𝑜1, 𝑜2, 𝑜3, 𝑜5}

Direct propagation:

Tian Tan @ Nanjing University

Page 63: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

Differential Propagation• Differential propagation is employed to avoid propagation and

processing of redundant points-to information

• Insight: existing points-to information in 𝑝𝑡(𝑛) have already been propagated to 𝑛’s successors, and no need to be propagated again

63

Solve(𝑆)…while WL is not empty do

remove 𝑛, 𝑝𝑡𝑠 from WLΔ = pts – pt(n)Propagate(n, Δ)…

ba

d

c

𝑝𝑡 𝑎 = {𝑜1, 𝑜2, 𝑜3} 𝑝𝑡 𝑏 = {𝑜1, 𝑜3, 𝑜5}

{𝑜1, 𝑜3, 𝑜5}

{𝑜1, 𝑜3, 𝑜5}

PFG

𝑝𝑡 𝑑 = {𝑜1, 𝑜2, 𝑜3, 𝑜5}

𝑝𝑡 𝑐 = {𝑜1, 𝑜2, 𝑜3, 𝑜5}

Direct propagation:redundant

Tian Tan @ Nanjing University

Page 64: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

Differential Propagation• Differential propagation is employed to avoid propagation and

processing of redundant points-to information

• Insight: existing points-to information in 𝑝𝑡(𝑛) have already been propagated to 𝑛’s successors, and no need to be propagated again

64

Solve(𝑆)…while WL is not empty do

remove 𝑛, 𝑝𝑡𝑠 from WLΔ = pts – pt(n)Propagate(n, Δ)…

ba

d

c

𝑝𝑡 𝑎 = {𝑜1, 𝑜2, 𝑜3} 𝑝𝑡 𝑏 = {𝑜1, 𝑜3, 𝑜5}

{𝑜1, 𝑜3, 𝑜5}

{𝑜5}

PFG

𝑝𝑡 𝑑 = {𝑜1, 𝑜2, 𝑜3, 𝑜5}

𝑝𝑡 𝑐 = {𝑜1, 𝑜2, 𝑜3, 𝑜5}

Differential propagation: Δ = 𝑝𝑡𝑠 − 𝑝𝑡 𝑐= {𝑜1, 𝑜3, 𝑜5} − {𝑜1, 𝑜2, 𝑜3}= {𝑜5}

In practice, Δ is usually small compared with the original set, so propagating only the new points-to information (Δ) improves efficiency

Page 65: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

Differential Propagation• Differential propagation is employed to avoid propagation and

processing of redundant points-to information

• Insight: existing points-to information in 𝑝𝑡(𝑛) have already been propagated to 𝑛’s successors, and no need to be propagated again

65

Solve(𝑆)…while WL is not empty do

remove 𝑛, 𝑝𝑡𝑠 from WLΔ = pts – pt(n)Propagate(n, Δ)…

ba

d

c

𝑝𝑡 𝑎 = {𝑜1, 𝑜2, 𝑜3} 𝑝𝑡 𝑏 = {𝑜1, 𝑜3, 𝑜5}

{𝑜1, 𝑜3, 𝑜5}

{𝑜5}

PFG

𝑝𝑡 𝑑 = {𝑜1, 𝑜2, 𝑜3, 𝑜5}

𝑝𝑡 𝑐 = {𝑜1, 𝑜2, 𝑜3, 𝑜5}

Besides, Δ is also important for efficiency when handling stores, loads, and method calls, as explained later

Differential propagation: Δ = 𝑝𝑡𝑠 − 𝑝𝑡 𝑐= {𝑜1, 𝑜3, 𝑜5} − {𝑜1, 𝑜2, 𝑜3}= {𝑜5}

Page 66: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

Solve(𝑆)WL = [ ], PFG = {}foreach i: x = new T() ∈ 𝑆 do

add 𝑥, {𝑜𝑖} to WL

foreach x = y ∈ 𝑆 doAddEdge(y, x)

while WL is not empty doremove 𝑛, 𝑝𝑡𝑠 from WLΔ = pts – pt(n)Propagate(n, Δ)if n represents a variable x then

foreach 𝑜𝑖 ∈ Δ doforeach x.f = y ∈ 𝑆 do

AddEdge(y, 𝑜𝑖 . 𝑓)foreach y = x.f ∈ 𝑆 do

AddEdge(𝑜𝑖 . 𝑓, y)

Handling of New and Assign

66

Kind Statement Rule PFG Edge

New i: x = new T() 𝑜𝑖 ∈ 𝑝𝑡(𝑥) N/A

Assign x = y𝑜𝑖 ∈ 𝑝𝑡(𝒚)

𝑜𝑖 ∈ 𝑝𝑡(𝒙)𝑥 ← 𝑦

Tian Tan @ Nanjing University

Page 67: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

Handling of Store and Load

67

Solve(𝑆)…while WL is not empty do

remove 𝑛, 𝑝𝑡𝑠 from WLΔ = pts – pt(n)Propagate(n, Δ)if n represents a variable x then

foreach 𝑜𝑖 ∈ Δ doforeach x.f = y ∈ 𝑆 do

AddEdge(y, 𝑜𝑖 . 𝑓)foreach y = x.f ∈ 𝑆 do

AddEdge(𝑜𝑖 . 𝑓, y)

Kind Statement Rule PFG Edge

Store x.f = y𝑜𝑖 ∈ 𝑝𝑡 𝒙 , 𝑜𝑗 ∈ 𝑝𝑡 𝒚

𝑜𝑗 ∈ 𝑝𝑡(𝒐𝒊. 𝒇)𝑜𝑖 . 𝑓 ← 𝑦

Load y = x.f𝑜𝑖 ∈ 𝑝𝑡 𝒙 , 𝑜𝑗 ∈ 𝑝𝑡 𝒐𝒊. 𝒇

𝑜𝑗 ∈ 𝑝𝑡(𝒚)𝑦 ← 𝑜𝑖 . 𝑓

Page 68: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

Handling of Store and Load

68

Solve(𝑆)…while WL is not empty do

remove 𝑛, 𝑝𝑡𝑠 from WLΔ = pts – pt(n)Propagate(n, Δ)if n represents a variable x then

foreach 𝑜𝑖 ∈ Δ doforeach x.f = y ∈ 𝑆 do

AddEdge(y, 𝑜𝑖 . 𝑓)foreach y = x.f ∈ 𝑆 do

AddEdge(𝑜𝑖 . 𝑓, y)

Kind Statement Rule PFG Edge

Store x.f = y𝑜𝑖 ∈ 𝑝𝑡 𝒙 , 𝑜𝑗 ∈ 𝑝𝑡 𝒚

𝑜𝑗 ∈ 𝑝𝑡(𝒐𝒊. 𝒇)𝑜𝑖 . 𝑓 ← 𝑦

Load y = x.f𝑜𝑖 ∈ 𝑝𝑡 𝒙 , 𝑜𝑗 ∈ 𝑝𝑡 𝒐𝒊. 𝒇

𝑜𝑗 ∈ 𝑝𝑡(𝒚)𝑦 ← 𝑜𝑖 . 𝑓

New points-to information may introduce new PFG edges

AddEdge(s, t)

if s → t ∉ PFG then

add s → t to PFG

if 𝑝𝑡(𝑠) is not empty then

add 𝑡, 𝑝𝑡(𝑠) to WL

Page 69: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

Algorithms: Review

69

Solve(𝑆)WL = [ ], PFG = {}foreach i: x = new T() ∈ 𝑆 do

add 𝑥, {𝑜𝑖} to WL

foreach x = y ∈ 𝑆 doAddEdge(y, x)

while WL is not empty doremove 𝑛, 𝑝𝑡𝑠 from WLΔ = pts – pt(n)Propagate(n, Δ)if n represents a variable x then

foreach 𝑜𝑖 ∈ Δ doforeach x.f = y ∈ 𝑆 do

AddEdge(y, 𝑜𝑖 . 𝑓)foreach y = x.f ∈ 𝑆 do

AddEdge(𝑜𝑖 . 𝑓, y)

Propagate(n, pts)

if pts is not empty then

pt(n) ⋃= pts

foreach n → s ∈ PFG do

add 𝑠, pts to WL

AddEdge(s, t)

if s → t ∉ PFG then

add s → t to PFG

if 𝑝𝑡(𝑠) is not empty then

add 𝑡, 𝑝𝑡(𝑠) to WL

SSet of statements of the input program

WL Work list

PFG Pointer flow graphTian Tan @ Nanjing University

Page 70: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

Solve(𝑆)WL = [ ], PFG = {}foreach i: x = new T() ∈ 𝑆 do

add 𝑥, {𝑜𝑖} to WL

foreach x = y ∈ 𝑆 doAddEdge(y, x)

while WL is not empty doremove 𝑛, 𝑝𝑡𝑠 from WLΔ = pts – pt(n)Propagate(n, Δ)if n represents a variable x then

foreach 𝑜𝑖 ∈ Δ doforeach x.f = y ∈ 𝑆 do

AddEdge(y, 𝑜𝑖 . 𝑓)foreach y = x.f ∈ 𝑆 do

AddEdge(𝑜𝑖 . 𝑓, y)

An Example

70

1 b = new C();2 a = b;3 c = new C();4 c.f = a;5 d = c;6 c.f = d;7 e = d.f;

WL:

PFG:

S:

Tian Tan @ Nanjing University

Page 71: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

Solve(𝑆)WL = [ ], PFG = {}foreach i: x = new T() ∈ 𝑆 do

add 𝑥, {𝑜𝑖} to WL

foreach x = y ∈ 𝑆 doAddEdge(y, x)

while WL is not empty doremove 𝑛, 𝑝𝑡𝑠 from WLΔ = pts – pt(n)Propagate(n, Δ)if n represents a variable x then

foreach 𝑜𝑖 ∈ Δ doforeach x.f = y ∈ 𝑆 do

AddEdge(y, 𝑜𝑖 . 𝑓)foreach y = x.f ∈ 𝑆 do

AddEdge(𝑜𝑖 . 𝑓, y)

An Example

71

1 b = new C();2 a = b;3 c = new C();4 c.f = a;5 d = c;6 c.f = d;7 e = d.f;

𝑏, {𝑜1} , 𝑐, {𝑜3}WL:

PFG:

S:

Tian Tan @ Nanjing University

Page 72: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

Solve(𝑆)WL = [ ], PFG = {}foreach i: x = new T() ∈ 𝑆 do

add 𝑥, {𝑜𝑖} to WL

foreach x = y ∈ 𝑆 doAddEdge(y, x)

while WL is not empty doremove 𝑛, 𝑝𝑡𝑠 from WLΔ = pts – pt(n)Propagate(n, Δ)if n represents a variable x then

foreach 𝑜𝑖 ∈ Δ doforeach x.f = y ∈ 𝑆 do

AddEdge(y, 𝑜𝑖 . 𝑓)foreach y = x.f ∈ 𝑆 do

AddEdge(𝑜𝑖 . 𝑓, y)

An Example

72

1 b = new C();2 a = b;3 c = new C();4 c.f = a;5 d = c;6 c.f = d;7 e = d.f;

𝑏, {𝑜1} , 𝑐, {𝑜3}WL:

PFG:

S:

𝑎 𝑏

𝑐𝑑{ }{ }

{ }{ }

Tian Tan @ Nanjing University

Page 73: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

Solve(𝑆)…while WL is not empty do

remove 𝑛, 𝑝𝑡𝑠 from WLΔ = pts – pt(n)Propagate(n, Δ)if n represents a variable x then

foreach 𝑜𝑖 ∈ Δ doforeach x.f = y ∈ 𝑆 do

AddEdge(y, 𝑜𝑖 . 𝑓)foreach y = x.f ∈ 𝑆 do

AddEdge(𝑜𝑖 . 𝑓, y)

An Example

73

1 b = new C();2 a = b;3 c = new C();4 c.f = a;5 d = c;6 c.f = d;7 e = d.f;

𝑐, {𝑜3}WL:

PFG:

S:

𝑎 𝑏

𝑐𝑑{ }{ }

{}{ }

Processing: 𝒃, {𝒐𝟏}

Tian Tan @ Nanjing University

Page 74: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

Solve(𝑆)…while WL is not empty do

remove 𝑛, 𝑝𝑡𝑠 from WLΔ = pts – pt(n)Propagate(n, Δ)if n represents a variable x then

foreach 𝑜𝑖 ∈ Δ doforeach x.f = y ∈ 𝑆 do

AddEdge(y, 𝑜𝑖 . 𝑓)foreach y = x.f ∈ 𝑆 do

AddEdge(𝑜𝑖 . 𝑓, y)

An Example

74

1 b = new C();2 a = b;3 c = new C();4 c.f = a;5 d = c;6 c.f = d;7 e = d.f;

𝑐, {𝑜3}WL:

PFG:

S:

𝑎 𝑏

𝑐𝑑{ }{ }

{𝑜1}{ }

Processing: 𝒃, {𝒐𝟏}

Propagate(n, pts)

if pts is not empty then

pt(n) ⋃= pts

foreach n → s ∈ PFG do

add 𝑠, pts to WL Tian Tan @ Nanjing University

Page 75: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

Solve(𝑆)…while WL is not empty do

remove 𝑛, 𝑝𝑡𝑠 from WLΔ = pts – pt(n)Propagate(n, Δ)if n represents a variable x then

foreach 𝑜𝑖 ∈ Δ doforeach x.f = y ∈ 𝑆 do

AddEdge(y, 𝑜𝑖 . 𝑓)foreach y = x.f ∈ 𝑆 do

AddEdge(𝑜𝑖 . 𝑓, y)

An Example

75

1 b = new C();2 a = b;3 c = new C();4 c.f = a;5 d = c;6 c.f = d;7 e = d.f;

𝑐, {𝑜3} , 𝑎, {𝑜1}WL:

PFG:

S:

𝑎 𝑏

𝑐𝑑{ }{ }

{𝑜1}{ }

Processing: 𝒃, {𝒐𝟏}

Propagate(n, pts)

if pts is not empty then

pt(n) ⋃= pts

foreach n → s ∈ PFG do

add 𝑠, pts to WL Tian Tan @ Nanjing University

Page 76: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

Solve(𝑆)…while WL is not empty do

remove 𝑛, 𝑝𝑡𝑠 from WLΔ = pts – pt(n)Propagate(n, Δ)if n represents a variable x then

foreach 𝑜𝑖 ∈ Δ doforeach x.f = y ∈ 𝑆 do

AddEdge(y, 𝑜𝑖 . 𝑓)foreach y = x.f ∈ 𝑆 do

AddEdge(𝑜𝑖 . 𝑓, y)

An Example

76

1 b = new C();2 a = b;3 c = new C();4 c.f = a;5 d = c;6 c.f = d;7 e = d.f;

𝑎, {𝑜1}WL:

PFG:

S:

{ }

{𝑜1}{ }

Processing: 𝒄, {𝒐𝟑}

𝑎 𝑏

𝑐𝑑

Propagate(n, pts)

if pts is not empty then

pt(n) ⋃= pts

foreach n → s ∈ PFG do

add 𝑠, pts to WL Tian Tan @ Nanjing University{ }

Page 77: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

Solve(𝑆)…while WL is not empty do

remove 𝑛, 𝑝𝑡𝑠 from WLΔ = pts – pt(n)Propagate(n, Δ)if n represents a variable x then

foreach 𝑜𝑖 ∈ Δ doforeach x.f = y ∈ 𝑆 do

AddEdge(y, 𝑜𝑖 . 𝑓)foreach y = x.f ∈ 𝑆 do

AddEdge(𝑜𝑖 . 𝑓, y)

An Example

77

1 b = new C();2 a = b;3 c = new C();4 c.f = a;5 d = c;6 c.f = d;7 e = d.f;

𝑎, {𝑜1}WL:

PFG:

S:

{ }

{𝑜1}{ }

Processing: 𝒄, {𝒐𝟑}

𝑎 𝑏

𝑐𝑑

Propagate(n, pts)

if pts is not empty then

pt(n) ⋃= pts

foreach n → s ∈ PFG do

add 𝑠, pts to WL Tian Tan @ Nanjing University{𝑜3}

Page 78: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

Solve(𝑆)…while WL is not empty do

remove 𝑛, 𝑝𝑡𝑠 from WLΔ = pts – pt(n)Propagate(n, Δ)if n represents a variable x then

foreach 𝑜𝑖 ∈ Δ doforeach x.f = y ∈ 𝑆 do

AddEdge(y, 𝑜𝑖 . 𝑓)foreach y = x.f ∈ 𝑆 do

AddEdge(𝑜𝑖 . 𝑓, y)

An Example

78

1 b = new C();2 a = b;3 c = new C();4 c.f = a;5 d = c;6 c.f = d;7 e = d.f;

𝑎, {𝑜1} , 𝑑, {𝑜3}WL:

PFG:

S:

{𝑜3}{ }

{𝑜1}{ }

Processing: 𝒄, {𝒐𝟑}

𝑎 𝑏

𝑐𝑑

Propagate(n, pts)

if pts is not empty then

pt(n) ⋃= pts

foreach n → s ∈ PFG do

add 𝑠, pts to WL Tian Tan @ Nanjing University

Page 79: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

Solve(𝑆)…while WL is not empty do

remove 𝑛, 𝑝𝑡𝑠 from WLΔ = pts – pt(n)Propagate(n, Δ)if n represents a variable x then

foreach 𝑜𝑖 ∈ Δ doforeach x.f = y ∈ 𝑆 do

AddEdge(y, 𝑜𝑖 . 𝑓)foreach y = x.f ∈ 𝑆 do

AddEdge(𝑜𝑖 . 𝑓, y)

An Example

79

1 b = new C();2 a = b;3 c = new C();4 c.f = a;5 d = c;6 c.f = d;7 e = d.f;

𝑎, {𝑜1} , 𝑑, {𝑜3}WL:

PFG:

S:

{𝑜3}{ }

{𝑜1}{ }

Processing: 𝒄, {𝒐𝟑}

𝑎 𝑏

𝑐𝑑

Propagate(n, pts)

if pts is not empty then

pt(n) ⋃= pts

foreach n → s ∈ PFG do

add 𝑠, pts to WL Tian Tan @ Nanjing University

What next?

Page 80: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

Solve(𝑆)…while WL is not empty do

remove 𝑛, 𝑝𝑡𝑠 from WLΔ = pts – pt(n)Propagate(n, Δ)if n represents a variable x then

foreach 𝑜𝑖 ∈ Δ doforeach x.f = y ∈ 𝑆 do

AddEdge(y, 𝑜𝑖 . 𝑓)foreach y = x.f ∈ 𝑆 do

AddEdge(𝑜𝑖 . 𝑓, y)

An Example

80

1 b = new C();2 a = b;3 c = new C();4 c.f = a;5 d = c;6 c.f = d;7 e = d.f;

𝑎, {𝑜1} , 𝑑, {𝑜3}WL:

PFG:

S:

{𝑜3}{ }

{𝑜1}{ }

Processing: 𝒄, {𝒐𝟑}

𝑜3. 𝑓

𝑎 𝑏

𝑐𝑑

{ }

Tian Tan @ Nanjing University

AddEdge(s, t)

if s → t ∉ PFG then

add s → t to PFG

if 𝑝𝑡(𝑠) is not empty then

add 𝑡, 𝑝𝑡(𝑠) to WL

Page 81: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

An Example

81

1 b = new C();2 a = b;3 c = new C();4 c.f = a;5 d = c;6 c.f = d;7 e = d.f;

𝑑, {𝑜3}WL:

PFG:

S:

{𝑜3}{ }

{𝑜1}{𝑜1}

Processing: 𝒂, {𝒐𝟏}

𝑜3. 𝑓

𝑎 𝑏

𝑐𝑑

{ }

Solve(𝑆)…while WL is not empty do

remove 𝑛, 𝑝𝑡𝑠 from WLΔ = pts – pt(n)Propagate(n, Δ)if n represents a variable x then

foreach 𝑜𝑖 ∈ Δ doforeach x.f = y ∈ 𝑆 do

AddEdge(y, 𝑜𝑖 . 𝑓)foreach y = x.f ∈ 𝑆 do

AddEdge(𝑜𝑖 . 𝑓, y)

Propagate(n, pts)

if pts is not empty then

pt(n) ⋃= pts

foreach n → s ∈ PFG do

add 𝑠, pts to WL Tian Tan @ Nanjing University

Page 82: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

An Example

82

1 b = new C();2 a = b;3 c = new C();4 c.f = a;5 d = c;6 c.f = d;7 e = d.f;

𝑑, {𝑜3} , 𝑜3. 𝑓, {𝑜1}WL:

PFG:

S:

{𝑜3}{ }

{𝑜1}{𝑜1}

Processing: 𝒂, {𝒐𝟏}

𝑜3. 𝑓

𝑎 𝑏

𝑐𝑑

{ }

Solve(𝑆)…while WL is not empty do

remove 𝑛, 𝑝𝑡𝑠 from WLΔ = pts – pt(n)Propagate(n, Δ)if n represents a variable x then

foreach 𝑜𝑖 ∈ Δ doforeach x.f = y ∈ 𝑆 do

AddEdge(y, 𝑜𝑖 . 𝑓)foreach y = x.f ∈ 𝑆 do

AddEdge(𝑜𝑖 . 𝑓, y)

Propagate(n, pts)

if pts is not empty then

pt(n) ⋃= pts

foreach n → s ∈ PFG do

add 𝑠, pts to WL Tian Tan @ Nanjing University

Page 83: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

An Example

83

1 b = new C();2 a = b;3 c = new C();4 c.f = a;5 d = c;6 c.f = d;7 e = d.f;

𝑜3. 𝑓, {𝑜1}WL:

PFG:

S:

{𝑜3}{𝑜3}

{𝑜1}{𝑜1}

Processing: 𝒅, {𝒐𝟑}

𝑜3. 𝑓

𝑎 𝑏

𝑐𝑑

{ }

Solve(𝑆)…while WL is not empty do

remove 𝑛, 𝑝𝑡𝑠 from WLΔ = pts – pt(n)Propagate(n, Δ)if n represents a variable x then

foreach 𝑜𝑖 ∈ Δ doforeach x.f = y ∈ 𝑆 do

AddEdge(y, 𝑜𝑖 . 𝑓)foreach y = x.f ∈ 𝑆 do

AddEdge(𝑜𝑖 . 𝑓, y)

Propagate(n, pts)

if pts is not empty then

pt(n) ⋃= pts

foreach n → s ∈ PFG do

add 𝑠, pts to WL Tian Tan @ Nanjing University

Page 84: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

An Example

84

1 b = new C();2 a = b;3 c = new C();4 c.f = a;5 d = c;6 c.f = d;7 e = d.f;

𝑜3. 𝑓, {𝑜1}WL:

PFG:

S:

{𝑜3}{𝑜3}

{𝑜1}{𝑜1}

Processing: 𝒅, {𝒐𝟑}

𝑜3. 𝑓

𝑎 𝑏

𝑐𝑑

{ }

Solve(𝑆)…while WL is not empty do

remove 𝑛, 𝑝𝑡𝑠 from WLΔ = pts – pt(n)Propagate(n, Δ)if n represents a variable x then

foreach 𝑜𝑖 ∈ Δ doforeach x.f = y ∈ 𝑆 do

AddEdge(y, 𝑜𝑖 . 𝑓)foreach y = x.f ∈ 𝑆 do

AddEdge(𝑜𝑖 . 𝑓, y)

Propagate(n, pts)

if pts is not empty then

pt(n) ⋃= pts

foreach n → s ∈ PFG do

add 𝑠, pts to WL Tian Tan @ Nanjing University

What next?

Page 85: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

An Example

85

1 b = new C();2 a = b;3 c = new C();4 c.f = a;5 d = c;6 c.f = d;7 e = d.f;

𝑜3. 𝑓, {𝑜1} , 𝑜3. 𝑓, {𝑜3}WL:

PFG:

S:

{𝑜3}{𝑜3}

{𝑜1}{𝑜1}

Processing: 𝒅, {𝒐𝟑}

𝑜3. 𝑓

𝑎 𝑏

𝑐𝑑

{ }

Solve(𝑆)…while WL is not empty do

remove 𝑛, 𝑝𝑡𝑠 from WLΔ = pts – pt(n)Propagate(n, Δ)if n represents a variable x then

foreach 𝑜𝑖 ∈ Δ doforeach x.f = y ∈ 𝑆 do

AddEdge(y, 𝑜𝑖 . 𝑓)foreach y = x.f ∈ 𝑆 do

AddEdge(𝑜𝑖 . 𝑓, y)

Propagate(n, pts)

if pts is not empty then

pt(n) ⋃= pts

foreach n → s ∈ PFG do

add 𝑠, pts to WL Tian Tan @ Nanjing University

Page 86: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

An Example

86

1 b = new C();2 a = b;3 c = new C();4 c.f = a;5 d = c;6 c.f = d;7 e = d.f;

𝑜3. 𝑓, {𝑜1} , 𝑜3. 𝑓, {𝑜3}WL:

PFG:

S:

{𝑜3}{𝑜3}

{𝑜1}{𝑜1}

Processing: 𝒅, {𝒐𝟑}

𝑜3. 𝑓

𝑎 𝑏

𝑐𝑑

{ }

Solve(𝑆)…while WL is not empty do

remove 𝑛, 𝑝𝑡𝑠 from WLΔ = pts – pt(n)Propagate(n, Δ)if n represents a variable x then

foreach 𝑜𝑖 ∈ Δ doforeach x.f = y ∈ 𝑆 do

AddEdge(y, 𝑜𝑖 . 𝑓)foreach y = x.f ∈ 𝑆 do

AddEdge(𝑜𝑖 . 𝑓, y)

Propagate(n, pts)

if pts is not empty then

pt(n) ⋃= pts

foreach n → s ∈ PFG do

add 𝑠, pts to WL Tian Tan @ Nanjing University

What next?

Page 87: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

An Example

87

1 b = new C();2 a = b;3 c = new C();4 c.f = a;5 d = c;6 c.f = d;7 e = d.f;

𝑜3. 𝑓, {𝑜1} , 𝑜3. 𝑓, {𝑜3}WL:

PFG:

S:

{𝑜3}{𝑜3}

{𝑜1}{𝑜1}

Processing: 𝒅, {𝒐𝟑}

𝑜3. 𝑓

𝑎 𝑏

𝑐𝑑

{ }𝑒

Solve(𝑆)…while WL is not empty do

remove 𝑛, 𝑝𝑡𝑠 from WLΔ = pts – pt(n)Propagate(n, Δ)if n represents a variable x then

foreach 𝑜𝑖 ∈ Δ doforeach x.f = y ∈ 𝑆 do

AddEdge(y, 𝑜𝑖 . 𝑓)foreach y = x.f ∈ 𝑆 do

AddEdge(𝑜𝑖 . 𝑓, y)

Tian Tan @ Nanjing University

AddEdge(s, t)

if s → t ∉ PFG then

add s → t to PFG

if 𝑝𝑡(𝑠) is not empty then

add 𝑡, 𝑝𝑡(𝑠) to WL

Page 88: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

An Example

88

1 b = new C();2 a = b;3 c = new C();4 c.f = a;5 d = c;6 c.f = d;7 e = d.f;

𝑜3. 𝑓, {𝑜3} , 𝑒, {𝑜1}WL:

PFG:

S:

{𝑜3}{𝑜3}

{𝑜1}{𝑜1}

Processing: 𝒐𝟑. 𝒇, {𝒐𝟏}

𝑜3. 𝑓

𝑎 𝑏

𝑐𝑑

{𝑜1}𝑒

Solve(𝑆)…while WL is not empty do

remove 𝑛, 𝑝𝑡𝑠 from WLΔ = pts – pt(n)Propagate(n, Δ)if n represents a variable x then

foreach 𝑜𝑖 ∈ Δ doforeach x.f = y ∈ 𝑆 do

AddEdge(y, 𝑜𝑖 . 𝑓)foreach y = x.f ∈ 𝑆 do

AddEdge(𝑜𝑖 . 𝑓, y)

Propagate(n, pts)

if pts is not empty then

pt(n) ⋃= pts

foreach n → s ∈ PFG do

add 𝑠, pts to WL Tian Tan @ Nanjing University

Page 89: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

An Example

89

1 b = new C();2 a = b;3 c = new C();4 c.f = a;5 d = c;6 c.f = d;7 e = d.f;

𝑒, {𝑜1} , 𝑒, {𝑜3}WL:

PFG:

S:

{𝑜3}{𝑜3}

{𝑜1}{𝑜1}

Processing: 𝒐𝟑. 𝒇, {𝒐𝟑}

𝑜3. 𝑓

𝑎 𝑏

𝑐𝑑

{𝑜1, 𝑜3}𝑒

Solve(𝑆)…while WL is not empty do

remove 𝑛, 𝑝𝑡𝑠 from WLΔ = pts – pt(n)Propagate(n, Δ)if n represents a variable x then

foreach 𝑜𝑖 ∈ Δ doforeach x.f = y ∈ 𝑆 do

AddEdge(y, 𝑜𝑖 . 𝑓)foreach y = x.f ∈ 𝑆 do

AddEdge(𝑜𝑖 . 𝑓, y)

Propagate(n, pts)

if pts is not empty then

pt(n) ⋃= pts

foreach n → s ∈ PFG do

add 𝑠, pts to WL Tian Tan @ Nanjing University

Page 90: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

An Example

90

1 b = new C();2 a = b;3 c = new C();4 c.f = a;5 d = c;6 c.f = d;7 e = d.f;

WL:

PFG:

S:

{𝑜3}{𝑜3}

{𝑜1}{𝑜1}

Processing: 𝒆, {𝒐𝟏} , 𝒆, {𝒐𝟑}

𝑜3. 𝑓

𝑎 𝑏

𝑐𝑑

{𝑜1, 𝑜3}𝑒

{𝑜1, 𝑜3}

Solve(𝑆)…while WL is not empty do

remove 𝑛, 𝑝𝑡𝑠 from WLΔ = pts – pt(n)Propagate(n, Δ)if n represents a variable x then

foreach 𝑜𝑖 ∈ Δ doforeach x.f = y ∈ 𝑆 do

AddEdge(y, 𝑜𝑖 . 𝑓)foreach y = x.f ∈ 𝑆 do

AddEdge(𝑜𝑖 . 𝑓, y)

Propagate(n, pts)

if pts is not empty then

pt(n) ⋃= pts

foreach n → s ∈ PFG do

add 𝑠, pts to WL Tian Tan @ Nanjing University

Page 91: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

An Example

91

1 b = new C();2 a = b;3 c = new C();4 c.f = a;5 d = c;6 c.f = d;7 e = d.f;

WL:

PFG:

S:

{𝑜3}{𝑜3}

{𝑜1}{𝑜1}

𝑜3. 𝑓

𝑎 𝑏

𝑐𝑑

{𝑜1, 𝑜3}𝑒

{𝑜1, 𝑜3}

Solve(𝑆)…while WL is not empty do

remove 𝑛, 𝑝𝑡𝑠 from WLΔ = pts – pt(n)Propagate(n, Δ)if n represents a variable x then

foreach 𝑜𝑖 ∈ Δ doforeach x.f = y ∈ 𝑆 do

AddEdge(y, 𝑜𝑖 . 𝑓)foreach y = x.f ∈ 𝑆 do

AddEdge(𝑜𝑖 . 𝑓, y)

Propagate(n, pts)

if pts is not empty then

pt(n) ⋃= pts

foreach n → s ∈ PFG do

add 𝑠, pts to WL Tian Tan @ Nanjing University

Page 92: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

Algorithms: Review

92

Solve(𝑆)WL = [ ], PFG = {}foreach i: x = new T() ∈ 𝑆 do

add 𝑥, {𝑜𝑖} to WL

foreach x = y ∈ 𝑆 doAddEdge(y, x)

while WL is not empty doremove 𝑛, 𝑝𝑡𝑠 from WLΔ = pts – pt(n)Propagate(n, Δ)if n represents a variable x then

foreach 𝑜𝑖 ∈ Δ doforeach x.f = y ∈ 𝑆 do

AddEdge(y, 𝑜𝑖 . 𝑓)foreach y = x.f ∈ 𝑆 do

AddEdge(𝑜𝑖 . 𝑓, y)

Propagate(n, pts)

if pts is not empty then

pt(n) ⋃= pts

foreach n → s ∈ PFG do

add 𝑠, pts to WL

AddEdge(s, t)

if s → t ∉ PFG then

add s → t to PFG

if 𝑝𝑡(𝑠) is not empty then

add 𝑡, 𝑝𝑡(𝑠) to WL

SSet of statements of the input program

WL Work list

PFG Pointer flow graphTian Tan @ Nanjing University

Page 93: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

TheX You Need To Understand in This Lecture

• Understand pointer analysis rules

• Understand pointer flow graph

• Understand pointer analysis algorithms

Tian Tan @ Nanjing University

Page 94: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

Contents

94

1. Pointer Analysis: Rules

2. How to Implement Pointer Analysis

3. Pointer Analysis: Algorithms

4. Pointer Analysis with Method Calls

Tian Tan @ Nanjing University

Page 95: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

Pointer Analysis in the Presence of Method Invocations• Inter-procedural pointer analysis requires call graph

95

void foo(A a) {…b = a.bar();…

}

𝑝𝑡 𝑎 = ? ? ?

𝑝𝑡 𝑏 = ? ? ?

Tian Tan @ Nanjing University

Page 96: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

Pointer Analysis in the Presence of Method Invocations• Inter-procedural pointer analysis requires call graph

• Call graph construction➢CHA: imprecise, introduce spurious call graph edges and

points-to relations

96

𝑝𝑡 𝑎 = ? ? ?

𝑝𝑡 𝑏 = ? ? ?

CHA: resolve call targets based on declared type of a

Tian Tan @ Nanjing University

void foo(A a) {…b = a.bar();…

}

Page 97: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

Pointer Analysis in the Presence of Method Invocations• Inter-procedural pointer analysis requires call graph

• Call graph construction➢CHA: imprecise, introduce spurious call graph edges and

points-to relations

➢Pointer analysis: more precise than CHA, both for call graph and points-to relations

97

𝑝𝑡 𝑎 = ? ? ?

𝑝𝑡 𝑏 = ? ? ?

CHA: resolve call targets based on declared type of a

Pointer analysis: resolve call targets based on 𝑝𝑡(𝑎)

a.k.a on-the-flycall graph construction

Tian Tan @ Nanjing University

void foo(A a) {…b = a.bar();…

}

Page 98: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

Rule: Call

98

Kind Statement Rule

Call l: r = x.k(a1,…,an)

𝑜𝑖 ∈ 𝑝𝑡 𝑥 , 𝑚 = Dispatch(𝑜𝑖 , k)

𝑜𝑢 ∈ 𝑝𝑡 𝑎𝑗 , 1 ≤ 𝑗 ≤ 𝑛𝑜𝑣 ∈ 𝑝𝑡(𝑚𝑟𝑒𝑡)𝑜𝑖 ∈ 𝑝𝑡(𝑚𝑡ℎ𝑖𝑠)

𝑜𝑢 ∈ 𝑝𝑡 𝑚𝑝𝑗 , 1 ≤ 𝑗 ≤ 𝑛

𝑜𝑣 ∈ 𝑝𝑡(𝑟)

Tian Tan @ Nanjing University

Page 99: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

Rule: Call

99

• Dispatch 𝑜𝑖 , k : resolves the virtual dispatch of k on 𝑜𝑖 to a target method (based on type of 𝑜𝑖)

Kind Statement Rule

Call l: r = x.k(a1,…,an)

𝑜𝑖 ∈ 𝑝𝑡 𝒙 , 𝑚 = Dispatch(𝑜𝑖 , k)

𝑜𝑢 ∈ 𝑝𝑡 𝑎𝑗 , 1 ≤ 𝑗 ≤ 𝑛𝑜𝑣 ∈ 𝑝𝑡(𝑚𝑟𝑒𝑡)𝑜𝑖 ∈ 𝑝𝑡(𝑚𝑡ℎ𝑖𝑠)

𝑜𝑢 ∈ 𝑝𝑡 𝑚𝑝𝑗 , 1 ≤ 𝑗 ≤ 𝑛

𝑜𝑣 ∈ 𝑝𝑡(𝑟)

Tian Tan @ Nanjing University

Page 100: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

Rule: Call

100

• Dispatch 𝑜𝑖 , k : resolves the virtual dispatch of k on 𝑜𝑖 to a target method (based on type of 𝑜𝑖)

• 𝑚𝑡ℎ𝑖𝑠: this variable of 𝑚

Kind Statement Rule

Call l: r = x.k(a1,…,an)

𝑜𝑖 ∈ 𝑝𝑡 𝒙 , 𝑚 = Dispatch(𝑜𝑖 , k)

𝑜𝑢 ∈ 𝑝𝑡 𝑎𝑗 , 1 ≤ 𝑗 ≤ 𝑛𝑜𝑣 ∈ 𝑝𝑡(𝑚𝑟𝑒𝑡)𝑜𝑖 ∈ 𝑝𝑡(𝒎𝒕𝒉𝒊𝒔)

𝑜𝑢 ∈ 𝑝𝑡 𝑚𝑝𝑗 , 1 ≤ 𝑗 ≤ 𝑛

𝑜𝑣 ∈ 𝑝𝑡(𝑟)

Tian Tan @ Nanjing University

Page 101: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

Rule: Call

101

• Dispatch 𝑜𝑖 , k : resolves the virtual dispatch of k on 𝑜𝑖 to a target method (based on type of 𝑜𝑖)

• 𝑚𝑡ℎ𝑖𝑠: this variable of 𝑚

Kind Statement Rule

Call l: r = x.k(a1,…,an)

𝑜𝑖 ∈ 𝑝𝑡 𝒙 , 𝑚 = Dispatch(𝑜𝑖 , k)

𝑜𝑢 ∈ 𝑝𝑡 𝑎𝑗 , 1 ≤ 𝑗 ≤ 𝑛𝑜𝑣 ∈ 𝑝𝑡(𝑚𝑟𝑒𝑡)𝑜𝑖 ∈ 𝑝𝑡(𝒎𝒕𝒉𝒊𝒔)

𝑜𝑢 ∈ 𝑝𝑡 𝑚𝑝𝑗 , 1 ≤ 𝑗 ≤ 𝑛

𝑜𝑣 ∈ 𝑝𝑡(𝑟)

Tian Tan @ Nanjing University

class T … { …B foo(A p1, A p2) {this…return ret;

}}

C x = new T();…r = x.foo(a1, a2);

Page 102: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

class T … { …B foo(A p1, A p2) {this…return ret;

}}

Rule: Call

102

Kind Statement Rule

Call l: r = x.k(a1,…,an)

𝑜𝑖 ∈ 𝑝𝑡 𝑥 , 𝑚 = Dispatch(𝑜𝑖 , k)

𝑜𝑢 ∈ 𝑝𝑡 𝒂𝒋 , 1 ≤ 𝑗 ≤ 𝑛𝑜𝑣 ∈ 𝑝𝑡(𝑚𝑟𝑒𝑡)𝑜𝑖 ∈ 𝑝𝑡(𝑚𝑡ℎ𝑖𝑠)

𝑜𝑢 ∈ 𝑝𝑡 𝒎𝒑𝒋 , 1 ≤ 𝑗 ≤ 𝑛

𝑜𝑣 ∈ 𝑝𝑡(𝑟)

• Dispatch 𝑜𝑖 , k : resolves the virtual dispatch of k on 𝑜𝑖 to a target method (based on type of 𝑜𝑖)

• 𝑚𝑡ℎ𝑖𝑠: this variable of 𝑚• 𝑚𝑝𝑗: the j-th parameter of 𝑚

C x = new T();…r = x.foo(a1, a2);

Tian Tan @ Nanjing University

Page 103: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

class T … { …B foo(A p1, A p2) {this…return ret;

}}

Rule: Call

103

Kind Statement Rule PFG Edge

Call l: r = x.k(a1,…,an)

𝑜𝑖 ∈ 𝑝𝑡 𝑥 , 𝑚 = Dispatch(𝑜𝑖 , k)

𝑜𝑢 ∈ 𝑝𝑡 𝒂𝒋 , 1 ≤ 𝑗 ≤ 𝑛𝑜𝑣 ∈ 𝑝𝑡(𝑚𝑟𝑒𝑡)𝑜𝑖 ∈ 𝑝𝑡(𝑚𝑡ℎ𝑖𝑠)

𝑜𝑢 ∈ 𝑝𝑡 𝒎𝒑𝒋 , 1 ≤ 𝑗 ≤ 𝑛

𝑜𝑣 ∈ 𝑝𝑡(𝑟)

𝑎1 → 𝑚𝑝1

…𝑎𝑛 → 𝑚𝑝𝑛

• Dispatch 𝑜𝑖 , k : resolves the virtual dispatch of k on 𝑜𝑖 to a target method (based on type of 𝑜𝑖)

• 𝑚𝑡ℎ𝑖𝑠: this variable of 𝑚• 𝑚𝑝𝑗: the j-th parameter of 𝑚

C x = new T();…r = x.foo(a1, a2);

Tian Tan @ Nanjing University

Page 104: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

class T … { …B foo(A p1, A p2) {this…return ret;

}}

Rule: Call

104

Kind Statement Rule PFG Edge

Call l: r = x.k(a1,…,an)

𝑜𝑖 ∈ 𝑝𝑡 𝑥 , 𝑚 = Dispatch(𝑜𝑖 , k)

𝑜𝑢 ∈ 𝑝𝑡 𝑎𝑗 , 1 ≤ 𝑗 ≤ 𝑛𝑜𝑣 ∈ 𝑝𝑡(𝒎𝒓𝒆𝒕)𝑜𝑖 ∈ 𝑝𝑡(𝑚𝑡ℎ𝑖𝑠)

𝑜𝑢 ∈ 𝑝𝑡 𝑚𝑝𝑗 , 1 ≤ 𝑗 ≤ 𝑛

𝑜𝑣 ∈ 𝑝𝑡(𝒓)

𝑎1 → 𝑚𝑝1

…𝑎𝑛 → 𝑚𝑝𝑛

• Dispatch 𝑜𝑖 , k : resolves the virtual dispatch of k on 𝑜𝑖 to a target method (based on type of 𝑜𝑖)

• 𝑚𝑡ℎ𝑖𝑠: this variable of 𝑚• 𝑚𝑝𝑗: the j-th parameter of 𝑚

• 𝑚𝑟𝑒𝑡: the variable that holds the return value of 𝑚

C x = new T();…r = x.foo(a1, a2);

Tian Tan @ Nanjing University

Page 105: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

class T … { …B foo(A p1, A p2) {this…return ret;

}}

Rule: Call

105

Kind Statement Rule PFG Edge

Call l: r = x.k(a1,…,an)

𝑜𝑖 ∈ 𝑝𝑡 𝑥 , 𝑚 = Dispatch(𝑜𝑖 , k)

𝑜𝑢 ∈ 𝑝𝑡 𝑎𝑗 , 1 ≤ 𝑗 ≤ 𝑛𝑜𝑣 ∈ 𝑝𝑡(𝒎𝒓𝒆𝒕)𝑜𝑖 ∈ 𝑝𝑡(𝑚𝑡ℎ𝑖𝑠)

𝑜𝑢 ∈ 𝑝𝑡 𝑚𝑝𝑗 , 1 ≤ 𝑗 ≤ 𝑛

𝑜𝑣 ∈ 𝑝𝑡(𝒓)

𝑎1 → 𝑚𝑝1

…𝑎𝑛 → 𝑚𝑝𝑛

𝑟 ← 𝑚𝑟𝑒𝑡

• Dispatch 𝑜𝑖 , k : resolves the virtual dispatch of k on 𝑜𝑖 to a target method (based on type of 𝑜𝑖)

• 𝑚𝑡ℎ𝑖𝑠: this variable of 𝑚• 𝑚𝑝𝑗: the j-th parameter of 𝑚

• 𝑚𝑟𝑒𝑡: the variable that holds the return value of 𝑚

C x = new T();…r = x.foo(a1, a2);

Tian Tan @ Nanjing University

Page 106: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

class T … { …B foo(A p1, A p2) {this…return ret;

}}

Rule: Call

106

Kind Statement Rule PFG Edge

Call l: r = x.k(a1,…,an)

𝑜𝑖 ∈ 𝑝𝑡 𝑥 , 𝑚 = Dispatch(𝑜𝑖 , k)

𝑜𝑢 ∈ 𝑝𝑡 𝑎𝑗 , 1 ≤ 𝑗 ≤ 𝑛𝑜𝑣 ∈ 𝑝𝑡(𝑚𝑟𝑒𝑡)𝑜𝑖 ∈ 𝑝𝑡(𝑚𝑡ℎ𝑖𝑠)

𝑜𝑢 ∈ 𝑝𝑡 𝑚𝑝𝑗 , 1 ≤ 𝑗 ≤ 𝑛

𝑜𝑣 ∈ 𝑝𝑡(𝑟)

𝑎1 → 𝑚𝑝1

…𝑎𝑛 → 𝑚𝑝𝑛

𝑟 ← 𝑚𝑟𝑒𝑡

• Dispatch 𝑜𝑖 , k : resolves the virtual dispatch of k on 𝑜𝑖 to a target method (based on type of 𝑜𝑖)

• 𝑚𝑡ℎ𝑖𝑠: this variable of 𝑚• 𝑚𝑝𝑗: the j-th parameter of 𝑚

• 𝑚𝑟𝑒𝑡: the variable that holds the return value of 𝑚

C x = new T();…r = x.foo(a1, a2);

Why not add PFG edge 𝑥 → 𝑚𝑡ℎ𝑖𝑠

Tian Tan @ Nanjing University

Page 107: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

class B extends A {T foo() {this…

}}

Rule: Call

107

Kind Statement Rule PFG Edge

Call l: r = x.k(a1,…,an)

𝑜𝑖 ∈ 𝑝𝑡 𝑥 , 𝑚 = Dispatch(𝑜𝑖 , k)

𝑜𝑢 ∈ 𝑝𝑡 𝑎𝑗 , 1 ≤ 𝑗 ≤ 𝑛𝑜𝑣 ∈ 𝑝𝑡(𝑚𝑟𝑒𝑡)𝑜𝑖 ∈ 𝑝𝑡(𝑚𝑡ℎ𝑖𝑠)

𝑜𝑢 ∈ 𝑝𝑡 𝑚𝑝𝑗 , 1 ≤ 𝑗 ≤ 𝑛

𝑜𝑣 ∈ 𝑝𝑡(𝑟)

𝑎1 → 𝑚𝑝1

…𝑎𝑛 → 𝑚𝑝𝑛

𝑟 ← 𝑚𝑟𝑒𝑡

x.foo();

class C extends A {T foo() {this…

}}

class A {T foo() {this…

}}

𝑝𝑡 𝑥 ={ new A,

new B,

new C }

Why not add PFG edge 𝑥 → 𝑚𝑡ℎ𝑖𝑠

Page 108: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

class B extends A {T foo() {this…

}}

Rule: Call

108

Kind Statement Rule PFG Edge

Call l: r = x.k(a1,…,an)

𝑜𝑖 ∈ 𝑝𝑡 𝑥 , 𝑚 = Dispatch(𝑜𝑖 , k)

𝑜𝑢 ∈ 𝑝𝑡 𝑎𝑗 , 1 ≤ 𝑗 ≤ 𝑛𝑜𝑣 ∈ 𝑝𝑡(𝑚𝑟𝑒𝑡)𝑜𝑖 ∈ 𝑝𝑡(𝑚𝑡ℎ𝑖𝑠)

𝑜𝑢 ∈ 𝑝𝑡 𝑚𝑝𝑗 , 1 ≤ 𝑗 ≤ 𝑛

𝑜𝑣 ∈ 𝑝𝑡(𝑟)

𝑎1 → 𝑚𝑝1

…𝑎𝑛 → 𝑚𝑝𝑛

𝑟 ← 𝑚𝑟𝑒𝑡

x.foo();

class C extends A {T foo() {this…

}}

class A {T foo() {this…

}}

𝑝𝑡 𝑥 ={ new A,

new B,

new C }

Why not add PFG edge 𝑥 → 𝑚𝑡ℎ𝑖𝑠

Page 109: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

class B extends A {T foo() {this…

}}

Rule: Call

109

Kind Statement Rule PFG Edge

Call l: r = x.k(a1,…,an)

𝑜𝑖 ∈ 𝑝𝑡 𝑥 , 𝑚 = Dispatch(𝑜𝑖 , k)

𝑜𝑢 ∈ 𝑝𝑡 𝑎𝑗 , 1 ≤ 𝑗 ≤ 𝑛𝑜𝑣 ∈ 𝑝𝑡(𝑚𝑟𝑒𝑡)𝑜𝑖 ∈ 𝑝𝑡(𝑚𝑡ℎ𝑖𝑠)

𝑜𝑢 ∈ 𝑝𝑡 𝑚𝑝𝑗 , 1 ≤ 𝑗 ≤ 𝑛

𝑜𝑣 ∈ 𝑝𝑡(𝑟)

𝑎1 → 𝑚𝑝1

…𝑎𝑛 → 𝑚𝑝𝑛

𝑟 ← 𝑚𝑟𝑒𝑡

x.foo();

class C extends A {T foo() {this…

}}

class A {T foo() {this…

}}

𝑝𝑡 𝑥 ={ new A,

new B,

new C }

Why not add PFG edge 𝑥 → 𝑚𝑡ℎ𝑖𝑠

Receiver object should only flow to this variable of the corresponding target method

PFG edge 𝑥 → 𝑚𝑡ℎ𝑖𝑠 would introduce spurious points-to relations for this variables

Page 110: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

class B extends A {T foo() {this…

}}

Rule: Call

110

Kind Statement Rule PFG Edge

Call l: r = x.k(a1,…,an)

𝑜𝑖 ∈ 𝑝𝑡 𝑥 , 𝑚 = Dispatch(𝑜𝑖 , k)

𝑜𝑢 ∈ 𝑝𝑡 𝑎𝑗 , 1 ≤ 𝑗 ≤ 𝑛𝑜𝑣 ∈ 𝑝𝑡(𝑚𝑟𝑒𝑡)𝑜𝑖 ∈ 𝑝𝑡(𝑚𝑡ℎ𝑖𝑠)

𝑜𝑢 ∈ 𝑝𝑡 𝑚𝑝𝑗 , 1 ≤ 𝑗 ≤ 𝑛

𝑜𝑣 ∈ 𝑝𝑡(𝑟)

𝑎1 → 𝑚𝑝1

…𝑎𝑛 → 𝑚𝑝𝑛

𝑟 ← 𝑚𝑟𝑒𝑡

x.foo();

class C extends A {T foo() {this…

}}

class A {T foo() {this…

}}

𝑝𝑡 𝑥 =

With 𝑥 → 𝑚𝑡ℎ𝑖𝑠

{ new A,

new B,

new C }

Why not add PFG edge 𝑥 → 𝑚𝑡ℎ𝑖𝑠

Receiver object should only flow to this variable of the corresponding target method

PFG edge 𝑥 → 𝑚𝑡ℎ𝑖𝑠 would introduce spurious points-to relations for this variables

Page 111: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

class B extends A {T foo() {this…

}}

{ new A,

new B,

new C }

Rule: Call

111

Kind Statement Rule PFG Edge

Call l: r = x.k(a1,…,an)

𝑜𝑖 ∈ 𝑝𝑡 𝑥 , 𝑚 = Dispatch(𝑜𝑖 , k)

𝑜𝑢 ∈ 𝑝𝑡 𝑎𝑗 , 1 ≤ 𝑗 ≤ 𝑛𝑜𝑣 ∈ 𝑝𝑡(𝑚𝑟𝑒𝑡)𝑜𝑖 ∈ 𝑝𝑡(𝑚𝑡ℎ𝑖𝑠)

𝑜𝑢 ∈ 𝑝𝑡 𝑚𝑝𝑗 , 1 ≤ 𝑗 ≤ 𝑛

𝑜𝑣 ∈ 𝑝𝑡(𝑟)

𝑎1 → 𝑚𝑝1

…𝑎𝑛 → 𝑚𝑝𝑛

𝑟 ← 𝑚𝑟𝑒𝑡

x.foo();

class C extends A {T foo() {this…

}}

class A {T foo() {this…

}}

𝑝𝑡 𝑥 =

{ new A,

new B,

new C }

{ new A,

new B,

new C }

With 𝑥 → 𝑚𝑡ℎ𝑖𝑠

{ new A,

new B,

new C }

Why not add PFG edge 𝑥 → 𝑚𝑡ℎ𝑖𝑠

Receiver object should only flow to this variable of the corresponding target method

PFG edge 𝑥 → 𝑚𝑡ℎ𝑖𝑠 would introduce spurious points-to relations for this variables

Page 112: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

class B extends A {T foo() {this…

}}

Rule: Call

112

Kind Statement Rule PFG Edge

Call l: r = x.k(a1,…,an)

𝑜𝑖 ∈ 𝑝𝑡 𝑥 , 𝑚 = Dispatch(𝑜𝑖 , k)

𝑜𝑢 ∈ 𝑝𝑡 𝑎𝑗 , 1 ≤ 𝑗 ≤ 𝑛𝑜𝑣 ∈ 𝑝𝑡(𝑚𝑟𝑒𝑡)𝑜𝑖 ∈ 𝑝𝑡(𝑚𝑡ℎ𝑖𝑠)

𝑜𝑢 ∈ 𝑝𝑡 𝑚𝑝𝑗 , 1 ≤ 𝑗 ≤ 𝑛

𝑜𝑣 ∈ 𝑝𝑡(𝑟)

𝑎1 → 𝑚𝑝1

…𝑎𝑛 → 𝑚𝑝𝑛

𝑟 ← 𝑚𝑟𝑒𝑡

x.foo();

class C extends A {T foo() {this…

}}

class A {T foo() {this…

}}

𝑝𝑡 𝑥 =

{ new C }{ new A } { new B }

{ new A,

new B,

new C }

Without 𝑥 → 𝑚𝑡ℎ𝑖𝑠

Why not add PFG edge 𝑥 → 𝑚𝑡ℎ𝑖𝑠

Receiver object should only flow to this variable of the corresponding target method

PFG edge 𝑥 → 𝑚𝑡ℎ𝑖𝑠 would introduce spurious points-to relations for this variables

Page 113: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

Interprocedural Pointer Analysis

• Run together with call graph construction

113Tian Tan @ Nanjing University

void foo(A a) {…b = a.bar();…

}

Pointer analysis

Call graph construction

Mutually dependent

Page 114: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

Interprocedural Pointer Analysis

• Run together with call graph construction

• Call graph forms a “reachable world”• Entry methods (e.g., the main method) are

reachable from the beginning

• The other reachable methods are gradually discovered during analysis

• Only reachable methods and statements are analyzed

114

Pointer analysis

Call graph construction

Mutually dependent

Tian Tan @ Nanjing University

void foo(A a) {…b = a.bar();…

}

main

m2 m3

m4

m5 m7

m6

m8m9

Page 115: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

Solve(𝑚𝑒𝑛𝑡𝑟𝑦)WL=[ ],PFG={},S={},RM={},CG={}

AddReachable(𝑚𝑒𝑛𝑡𝑟𝑦)while WL is not empty do

remove 𝑛, 𝑝𝑡𝑠 from WLΔ = pts – pt(n)Propagate(n, Δ)if n represents a variable x then

foreach 𝑜𝑖 ∈ Δ doforeach x.f = y ∈ 𝑆 do

AddEdge(y, 𝑜𝑖 . 𝑓)foreach y = x.f ∈ 𝑆 do

AddEdge(𝑜𝑖 . 𝑓, y)ProcessCall(x, 𝑜𝑖)

ProcessCall(x, 𝑜𝑖)foreach l: r = x.k(a1,…,an) ∈ 𝑆 do

𝑚 = Dispatch(𝑜𝑖, k)

add 𝑚𝑡ℎ𝑖𝑠, {𝑜𝑖} to WL

if l → 𝑚 ∉ CG then

add l → 𝑚 to CG

AddReachable(𝑚)

foreach parameter 𝑝𝑖 of 𝑚 do

AddEdge(𝑎𝑖, 𝑝𝑖)AddEdge(𝑚𝑟𝑒𝑡, 𝑟)

Algorithms

115

AddReachable(m)

if m ∉ RM then

add m to RM

𝑆 ∪= 𝑆𝑚foreach i: x = new T() ∈ 𝑆𝑚 do

add 𝑥, {𝑜𝑖} to WL

foreach x = y ∈ 𝑆𝑚 do

AddEdge(y, x)

New part

S Set of reachable statements

𝑆𝑚 Set of statements in method 𝑚

RM Set of reachable methods

CG Call graph edges

Page 116: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

AddReachable(𝑚)• Expand the “reachable world”, called

➢at the beginning for entry methods

➢when new call graph edge is discovered

116

AddReachable(m)

if m ∉ RM then

add m to RM

𝑆 ∪= 𝑆𝑚foreach i: x = new T() ∈ 𝑆𝑚 do

add 𝑥, {𝑜𝑖} to WL

foreach x = y ∈ 𝑆𝑚 do

AddEdge(y, x)

S Set of reachable statements

𝑆𝑚 Set of statements in method 𝑚

RM Set of reachable methods

Page 117: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

AddReachable(𝑚)• Expand the “reachable world”, called

➢at the beginning for entry methods

➢when new call graph edge is discovered

117

AddReachable(m)

if m ∉ RM then

add m to RM

𝑆 ∪= 𝑆𝑚foreach i: x = new T() ∈ 𝑆𝑚 do

add 𝑥, {𝑜𝑖} to WL

foreach x = y ∈ 𝑆𝑚 do

AddEdge(y, x)

Add new reachable method and statements

S Set of reachable statements

𝑆𝑚 Set of statements in method 𝑚

RM Set of reachable methods

Page 118: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

AddReachable(𝑚)• Expand the “reachable world”, called

➢at the beginning for entry methods

➢when new call graph edge is discovered

118

AddReachable(m)

if m ∉ RM then

add m to RM

𝑆 ∪= 𝑆𝑚foreach i: x = new T() ∈ 𝑆𝑚 do

add 𝑥, {𝑜𝑖} to WL

foreach x = y ∈ 𝑆𝑚 do

AddEdge(y, x)

Add new reachable method and statements

Update worklist and PFG for new discovered statements

S Set of reachable statements

𝑆𝑚 Set of statements in method 𝑚

RM Set of reachable methods

AddEdge(s, t)

if s → t ∉ PFG then

add s → t to PFG

if 𝑝𝑡(𝑠) is not empty then

add 𝑡, 𝑝𝑡(𝑠) to WL

(Same as before)

Page 119: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

Solve(𝑚𝑒𝑛𝑡𝑟𝑦)WL=[ ],PFG={},S={},RM={},CG={}

AddReachable(𝑚𝑒𝑛𝑡𝑟𝑦)while WL is not empty do

remove 𝑛, 𝑝𝑡𝑠 from WLΔ = pts – pt(n)Propagate(n, Δ)if n represents a variable x then

foreach 𝑜𝑖 ∈ Δ doforeach x.f = y ∈ 𝑆 do

AddEdge(y, 𝑜𝑖 . 𝑓)foreach y = x.f ∈ 𝑆 do

AddEdge(𝑜𝑖 . 𝑓, y)ProcessCall(x, 𝑜𝑖)

ProcessCall(x, 𝑜𝑖)foreach l: r = x.k(a1,…,an) ∈ 𝑆 do

𝑚 = Dispatch(𝑜𝑖, k)

add 𝑚𝑡ℎ𝑖𝑠, {𝑜𝑖} to WL

if l → 𝑚 ∉ CG then

add l → 𝑚 to CG

AddReachable(𝑚)

foreach parameter 𝑝𝑖 of 𝑚 do

AddEdge(𝑎𝑖, 𝑝𝑖)AddEdge(𝑚𝑟𝑒𝑡, 𝑟)

Algorithms

119

AddReachable(m)

if m ∉ RM then

add m to RM

𝑆 ∪= 𝑆𝑚foreach i: x = new T() ∈ 𝑆𝑚 do

add 𝑥, {𝑜𝑖} to WL

foreach x = y ∈ 𝑆𝑚 do

AddEdge(y, x)

New part

S Set of reachable statements

𝑆𝑚 Set of statements in method 𝑚

RM Set of reachable methods

CG Call graph edges

Page 120: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

ProcessCall(𝑥, 𝑜𝑖)

120

ProcessCall(x, 𝑜𝑖)foreach l: r = x.k(a1,…,an) ∈ 𝑆 do

𝑚 = Dispatch(𝑜𝑖, k)

add 𝑚𝑡ℎ𝑖𝑠, {𝑜𝑖} to WL

if l → 𝑚 ∉ CG then

add l → 𝑚 to CG

AddReachable(𝑚)

foreach parameter 𝑝𝑖 of 𝑚 do

AddEdge(𝑎𝑖, 𝑝𝑖)AddEdge(𝑚𝑟𝑒𝑡, 𝑟)

Kind Statement Rule PFG Edge

Call l: r = x.k(a1,…,an)

𝑜𝑖 ∈ 𝑝𝑡 𝑥 , 𝑚 = Dispatch(𝑜𝑖 , k)

𝑜𝑢 ∈ 𝑝𝑡 𝑎𝑗 , 1 ≤ 𝑗 ≤ 𝑛𝑜𝑣 ∈ 𝑝𝑡(𝑚𝑟𝑒𝑡)𝑜𝑖 ∈ 𝑝𝑡(𝑚𝑡ℎ𝑖𝑠)

𝑜𝑢 ∈ 𝑝𝑡 𝑚𝑝𝑗 , 1 ≤ 𝑗 ≤ 𝑛

𝑜𝑣 ∈ 𝑝𝑡(𝑟)

𝑎1 → 𝑚𝑝1

…𝑎𝑛 → 𝑚𝑝𝑛

𝑟 ← 𝑚𝑟𝑒𝑡

Tian Tan @ Nanjing University

Page 121: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

ProcessCall(𝑥, 𝑜𝑖)

121

ProcessCall(x, 𝑜𝑖)foreach l: r = x.k(a1,…,an) ∈ 𝑆 do

𝑚 = Dispatch(𝑜𝑖, k)

add 𝑚𝑡ℎ𝑖𝑠, {𝑜𝑖} to WL

if l → 𝑚 ∉ CG then

add l → 𝑚 to CG

AddReachable(𝑚)

foreach parameter 𝑝𝑖 of 𝑚 do

AddEdge(𝑎𝑖, 𝑝𝑖)AddEdge(𝑚𝑟𝑒𝑡, 𝑟)

Kind Statement Rule PFG Edge

Call l: r = x.k(a1,…,an)

𝑜𝑖 ∈ 𝑝𝑡 𝑥 , 𝑚 = Dispatch(𝑜𝑖 , k)

𝑜𝑢 ∈ 𝑝𝑡 𝑎𝑗 , 1 ≤ 𝑗 ≤ 𝑛𝑜𝑣 ∈ 𝑝𝑡(𝑚𝑟𝑒𝑡)𝑜𝑖 ∈ 𝑝𝑡(𝑚𝑡ℎ𝑖𝑠)

𝑜𝑢 ∈ 𝑝𝑡 𝑚𝑝𝑗 , 1 ≤ 𝑗 ≤ 𝑛

𝑜𝑣 ∈ 𝑝𝑡(𝑟)

𝑎1 → 𝑚𝑝1

…𝑎𝑛 → 𝑚𝑝𝑛

𝑟 ← 𝑚𝑟𝑒𝑡

Pass receiver object to this variable

Tian Tan @ Nanjing University

Page 122: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

ProcessCall(𝑥, 𝑜𝑖)

122

ProcessCall(x, 𝑜𝑖)foreach l: r = x.k(a1,…,an) ∈ 𝑆 do

𝑚 = Dispatch(𝑜𝑖, k)

add 𝑚𝑡ℎ𝑖𝑠, {𝑜𝑖} to WL

if l → 𝑚 ∉ CG then

add l → 𝑚 to CG

AddReachable(𝑚)

foreach parameter 𝑝𝑖 of 𝑚 do

AddEdge(𝑎𝑖, 𝑝𝑖)AddEdge(𝑚𝑟𝑒𝑡, 𝑟)

Kind Statement Rule PFG Edge

Call l: r = x.k(a1,…,an)

𝑜𝑖 ∈ 𝑝𝑡 𝑥 , 𝑚 = Dispatch(𝑜𝑖 , k)

𝑜𝑢 ∈ 𝑝𝑡 𝑎𝑗 , 1 ≤ 𝑗 ≤ 𝑛𝑜𝑣 ∈ 𝑝𝑡(𝑚𝑟𝑒𝑡)𝑜𝑖 ∈ 𝑝𝑡(𝑚𝑡ℎ𝑖𝑠)

𝑜𝑢 ∈ 𝑝𝑡 𝑚𝑝𝑗 , 1 ≤ 𝑗 ≤ 𝑛

𝑜𝑣 ∈ 𝑝𝑡(𝑟)

𝑎1 → 𝑚𝑝1

…𝑎𝑛 → 𝑚𝑝𝑛

𝑟 ← 𝑚𝑟𝑒𝑡

Pass receiver object to this variable

Construct call graph on the fly

Tian Tan @ Nanjing University

Page 123: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

ProcessCall(𝑥, 𝑜𝑖)

123

ProcessCall(x, 𝑜𝑖)foreach l: r = x.k(a1,…,an) ∈ 𝑆 do

𝑚 = Dispatch(𝑜𝑖, k)

add 𝑚𝑡ℎ𝑖𝑠, {𝑜𝑖} to WL

if l → 𝑚 ∉ CG then

add l → 𝑚 to CG

AddReachable(𝑚)

foreach parameter 𝑝𝑖 of 𝑚 do

AddEdge(𝑎𝑖, 𝑝𝑖)AddEdge(𝑚𝑟𝑒𝑡, 𝑟)

Kind Statement Rule PFG Edge

Call l: r = x.k(a1,…,an)

𝑜𝑖 ∈ 𝑝𝑡 𝑥 , 𝑚 = Dispatch(𝑜𝑖 , k)

𝑜𝑢 ∈ 𝑝𝑡 𝑎𝑗 , 1 ≤ 𝑗 ≤ 𝑛𝑜𝑣 ∈ 𝑝𝑡(𝑚𝑟𝑒𝑡)𝑜𝑖 ∈ 𝑝𝑡(𝑚𝑡ℎ𝑖𝑠)

𝑜𝑢 ∈ 𝑝𝑡 𝑚𝑝𝑗 , 1 ≤ 𝑗 ≤ 𝑛

𝑜𝑣 ∈ 𝑝𝑡(𝑟)

𝑎1 → 𝑚𝑝1

…𝑎𝑛 → 𝑚𝑝𝑛

𝑟 ← 𝑚𝑟𝑒𝑡

Pass receiver object to this variable

Pass arguments

Pass return values

Tian Tan @ Nanjing University

Construct call graph on the fly

Page 124: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

Solve(𝑚𝑒𝑛𝑡𝑟𝑦)WL=[ ],PFG={},S={},RM={},CG={}

AddReachable(𝑚𝑒𝑛𝑡𝑟𝑦)while WL is not empty do

remove 𝑛, 𝑝𝑡𝑠 from WLΔ = pts – pt(n)Propagate(n, Δ)if n represents a variable x then

foreach 𝑜𝑖 ∈ Δ doforeach x.f = y ∈ 𝑆 do

AddEdge(y, 𝑜𝑖 . 𝑓)foreach y = x.f ∈ 𝑆 do

AddEdge(𝑜𝑖 . 𝑓, y)ProcessCall(x, 𝑜𝑖)

ProcessCall(x, 𝑜𝑖)foreach l: r = x.k(a1,…,an) ∈ 𝑆 do

𝑚 = Dispatch(𝑜𝑖, k)

add 𝑚𝑡ℎ𝑖𝑠, {𝑜𝑖} to WL

if l → 𝑚 ∉ CG then

add l → 𝑚 to CG

AddReachable(𝑚)

foreach parameter 𝑝𝑖 of 𝑚 do

AddEdge(𝑎𝑖, 𝑝𝑖)AddEdge(𝑚𝑟𝑒𝑡, 𝑟)

124

AddReachable(m)

if m ∉ RM then

add m to RM

𝑆 ∪= 𝑆𝑚foreach i: x = new T() ∈ 𝑆𝑚 do

add 𝑥, {𝑜𝑖} to WL

foreach x = y ∈ 𝑆𝑚 do

AddEdge(y, x)

S Set of reachable statements

𝑆𝑚 Set of statements in method 𝑚

RM Set of reachable methods

CG Call graph edges

Algorithms Output: Points-to Relations (pt)Call Graph (CG)

Page 125: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

An Example

Tian Tan @ Nanjing University 125

Solve(𝑚𝑒𝑛𝑡𝑟𝑦)WL=[ ],PFG={},S={},RM={},CG={}

AddReachable(𝑚𝑒𝑛𝑡𝑟𝑦)while WL is not empty do

remove 𝑛, 𝑝𝑡𝑠 from WLΔ = pts – pt(n)Propagate(n, Δ)if n represents a variable x then

foreach 𝑜𝑖 ∈ Δ doforeach x.f = y ∈ 𝑆 do

AddEdge(y, 𝑜𝑖 . 𝑓)foreach y = x.f ∈ 𝑆 do

AddEdge(𝑜𝑖 . 𝑓, y)ProcessCall(x, 𝑜𝑖)

1 class A {2 static void main() {3 A a = new A();4 A b = new B();5 A c = b.foo(a);6 }7 A foo(A x) { … }8 }9 class B extends A {10 A foo(A y) {11 A r = new A();12 return r;13 }14 }

Page 126: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

An Example

Tian Tan @ Nanjing University 126

Solve(𝑚𝑒𝑛𝑡𝑟𝑦)WL=[ ],PFG={},S={},RM={},CG={}

AddReachable(𝑚𝑒𝑛𝑡𝑟𝑦)while WL is not empty do

remove 𝑛, 𝑝𝑡𝑠 from WLΔ = pts – pt(n)Propagate(n, Δ)if n represents a variable x then

foreach 𝑜𝑖 ∈ Δ doforeach x.f = y ∈ 𝑆 do

AddEdge(y, 𝑜𝑖 . 𝑓)foreach y = x.f ∈ 𝑆 do

AddEdge(𝑜𝑖 . 𝑓, y)ProcessCall(x, 𝑜𝑖)

[ ]WL:

CG:

RM: { }

1 class A {2 static void main() {3 A a = new A();4 A b = new B();5 A c = b.foo(a);6 }7 A foo(A x) { … }8 }9 class B extends A {10 A foo(A y) {11 A r = new A();12 return r;13 }14 }

PFG:{ }

Page 127: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

1 class A {2 static void main() {3 A a = new A();4 A b = new B();5 A c = b.foo(a);6 }7 A foo(A x) { … }8 }9 class B extends A {10 A foo(A y) {11 A r = new A();12 return r;13 }14 }

An Example

Tian Tan @ Nanjing University 127

Solve(𝑚𝑒𝑛𝑡𝑟𝑦)WL=[ ],PFG={},S={},RM={},CG={}

AddReachable(𝑚𝑒𝑛𝑡𝑟𝑦)while WL is not empty do

remove 𝑛, 𝑝𝑡𝑠 from WLΔ = pts – pt(n)Propagate(n, Δ)if n represents a variable x then

foreach 𝑜𝑖 ∈ Δ doforeach x.f = y ∈ 𝑆 do

AddEdge(y, 𝑜𝑖 . 𝑓)foreach y = x.f ∈ 𝑆 do

AddEdge(𝑜𝑖 . 𝑓, y)ProcessCall(x, 𝑜𝑖)

[ ]WL:

CG:

RM:

AddReachable(m)

if m ∉ RM then

add m to RM

𝑆 ∪= 𝑆𝑚foreach i: x = new T() ∈ 𝑆𝑚 do

add 𝑥, {𝑜𝑖} to WL

foreach x = y ∈ 𝑆𝑚 do

AddEdge(y, x)

PFG:

{ }

{ }

Page 128: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

1 class A {2 static void main() {3 A a = new A();4 A b = new B();5 A c = b.foo(a);6 }7 A foo(A x) { … }8 }9 class B extends A {10 A foo(A y) {11 A r = new A();12 return r;13 }14 }

An Example

Tian Tan @ Nanjing University 128

Solve(𝑚𝑒𝑛𝑡𝑟𝑦)WL=[ ],PFG={},S={},RM={},CG={}

AddReachable(𝑚𝑒𝑛𝑡𝑟𝑦)while WL is not empty do

remove 𝑛, 𝑝𝑡𝑠 from WLΔ = pts – pt(n)Propagate(n, Δ)if n represents a variable x then

foreach 𝑜𝑖 ∈ Δ doforeach x.f = y ∈ 𝑆 do

AddEdge(y, 𝑜𝑖 . 𝑓)foreach y = x.f ∈ 𝑆 do

AddEdge(𝑜𝑖 . 𝑓, y)ProcessCall(x, 𝑜𝑖)

[ ]WL:

CG:

RM:

AddReachable(m)

if m ∉ RM then

add m to RM

𝑆 ∪= 𝑆𝑚foreach i: x = new T() ∈ 𝑆𝑚 do

add 𝑥, {𝑜𝑖} to WL

foreach x = y ∈ 𝑆𝑚 do

AddEdge(y, x)

PFG:

{ A.main() }

{ }

Page 129: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

1 class A {2 static void main() {3 A a = new A();4 A b = new B();5 A c = b.foo(a);6 }7 A foo(A x) { … }8 }9 class B extends A {10 A foo(A y) {11 A r = new A();12 return r;13 }14 }

An Example

Tian Tan @ Nanjing University 129

Solve(𝑚𝑒𝑛𝑡𝑟𝑦)WL=[ ],PFG={},S={},RM={},CG={}

AddReachable(𝑚𝑒𝑛𝑡𝑟𝑦)while WL is not empty do

remove 𝑛, 𝑝𝑡𝑠 from WLΔ = pts – pt(n)Propagate(n, Δ)if n represents a variable x then

foreach 𝑜𝑖 ∈ Δ doforeach x.f = y ∈ 𝑆 do

AddEdge(y, 𝑜𝑖 . 𝑓)foreach y = x.f ∈ 𝑆 do

AddEdge(𝑜𝑖 . 𝑓, y)ProcessCall(x, 𝑜𝑖)

[ 𝑎, {𝑜3} , 𝑏, {𝑜4} ]WL:

CG:

RM:

AddReachable(m)

if m ∉ RM then

add m to RM

𝑆 ∪= 𝑆𝑚foreach i: x = new T() ∈ 𝑆𝑚 do

add 𝑥, {𝑜𝑖} to WL

foreach x = y ∈ 𝑆𝑚 do

AddEdge(y, x)

PFG:

{ A.main() }

{ }

Page 130: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

An Example

Tian Tan @ Nanjing University 130

Solve(𝑚𝑒𝑛𝑡𝑟𝑦)WL=[ ],PFG={},S={},RM={},CG={}

AddReachable(𝑚𝑒𝑛𝑡𝑟𝑦)while WL is not empty do

remove 𝑛, 𝑝𝑡𝑠 from WLΔ = pts – pt(n)Propagate(n, Δ)if n represents a variable x then

foreach 𝑜𝑖 ∈ Δ doforeach x.f = y ∈ 𝑆 do

AddEdge(y, 𝑜𝑖 . 𝑓)foreach y = x.f ∈ 𝑆 do

AddEdge(𝑜𝑖 . 𝑓, y)ProcessCall(x, 𝑜𝑖)

[ 𝑏, {𝑜4} ]WL:

CG:

RM:

Processing:

𝑎, {𝑜3}

1 class A {2 static void main() {3 A a = new A();4 A b = new B();5 A c = b.foo(a);6 }7 A foo(A x) { … }8 }9 class B extends A {10 A foo(A y) {11 A r = new A();12 return r;13 }14 }

PFG:

{ A.main() }

{ }

Page 131: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

An Example

Tian Tan @ Nanjing University 131

Solve(𝑚𝑒𝑛𝑡𝑟𝑦)WL=[ ],PFG={},S={},RM={},CG={}

AddReachable(𝑚𝑒𝑛𝑡𝑟𝑦)while WL is not empty do

remove 𝑛, 𝑝𝑡𝑠 from WLΔ = pts – pt(n)Propagate(n, Δ)if n represents a variable x then

foreach 𝑜𝑖 ∈ Δ doforeach x.f = y ∈ 𝑆 do

AddEdge(y, 𝑜𝑖 . 𝑓)foreach y = x.f ∈ 𝑆 do

AddEdge(𝑜𝑖 . 𝑓, y)ProcessCall(x, 𝑜𝑖)

[ 𝑏, {𝑜4} ]WL:

CG:

RM:

Processing:

𝑎, {𝑜3}

1 class A {2 static void main() {3 A a = new A();4 A b = new B();5 A c = b.foo(a);6 }7 A foo(A x) { … }8 }9 class B extends A {10 A foo(A y) {11 A r = new A();12 return r;13 }14 }

PFG:

{𝑜3}

𝑎

{ A.main() }

{ }

Page 132: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

An Example

Tian Tan @ Nanjing University 132

Solve(𝑚𝑒𝑛𝑡𝑟𝑦)WL=[ ],PFG={},S={},RM={},CG={}

AddReachable(𝑚𝑒𝑛𝑡𝑟𝑦)while WL is not empty do

remove 𝑛, 𝑝𝑡𝑠 from WLΔ = pts – pt(n)Propagate(n, Δ)if n represents a variable x then

foreach 𝑜𝑖 ∈ Δ doforeach x.f = y ∈ 𝑆 do

AddEdge(y, 𝑜𝑖 . 𝑓)foreach y = x.f ∈ 𝑆 do

AddEdge(𝑜𝑖 . 𝑓, y)ProcessCall(x, 𝑜𝑖)

[ ]WL:

CG:

RM:

Processing:

𝑏, {𝑜4}

1 class A {2 static void main() {3 A a = new A();4 A b = new B();5 A c = b.foo(a);6 }7 A foo(A x) { … }8 }9 class B extends A {10 A foo(A y) {11 A r = new A();12 return r;13 }14 }

{𝑜3}

𝑎

PFG:

{ A.main() }

{ }

Page 133: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

An Example

Tian Tan @ Nanjing University 133

Solve(𝑚𝑒𝑛𝑡𝑟𝑦)WL=[ ],PFG={},S={},RM={},CG={}

AddReachable(𝑚𝑒𝑛𝑡𝑟𝑦)while WL is not empty do

remove 𝑛, 𝑝𝑡𝑠 from WLΔ = pts – pt(n)Propagate(n, Δ)if n represents a variable x then

foreach 𝑜𝑖 ∈ Δ doforeach x.f = y ∈ 𝑆 do

AddEdge(y, 𝑜𝑖 . 𝑓)foreach y = x.f ∈ 𝑆 do

AddEdge(𝑜𝑖 . 𝑓, y)ProcessCall(x, 𝑜𝑖)

[ ]WL:

CG:

RM:

Processing:

𝑏, {𝑜4}

1 class A {2 static void main() {3 A a = new A();4 A b = new B();5 A c = b.foo(a);6 }7 A foo(A x) { … }8 }9 class B extends A {10 A foo(A y) {11 A r = new A();12 return r;13 }14 }

{𝑜3}

𝑎

{𝑜4}

PFG:

𝑏

{ A.main() }

{ }

Page 134: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

An Example

Tian Tan @ Nanjing University 134

Solve(𝑚𝑒𝑛𝑡𝑟𝑦)WL=[ ],PFG={},S={},RM={},CG={}

AddReachable(𝑚𝑒𝑛𝑡𝑟𝑦)while WL is not empty do

remove 𝑛, 𝑝𝑡𝑠 from WLΔ = pts – pt(n)Propagate(n, Δ)if n represents a variable x then

foreach 𝑜𝑖 ∈ Δ doforeach x.f = y ∈ 𝑆 do

AddEdge(y, 𝑜𝑖 . 𝑓)foreach y = x.f ∈ 𝑆 do

AddEdge(𝑜𝑖 . 𝑓, y)ProcessCall(x, 𝑜𝑖)

[ ]WL:

CG:

RM:

Processing:

𝑏, {𝑜4}

1 class A {2 static void main() {3 A a = new A();4 A b = new B();5 A c = b.foo(a);6 }7 A foo(A x) { … }8 }9 class B extends A {10 A foo(A y) {11 A r = new A();12 return r;13 }14 }

{𝑜3}

𝑎

{𝑜4}

PFG:

What next?

𝑏

{ A.main() }

{ }

Page 135: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

An Example

Tian Tan @ Nanjing University 135

1 class A {2 static void main() {3 A a = new A();4 A b = new B();5 A c = b.foo(a);6 }7 A foo(A x) { … }8 }9 class B extends A {10 A foo(A y) {11 A r = new A();12 return r;13 }14 }

Solve(𝑚𝑒𝑛𝑡𝑟𝑦)WL=[ ],PFG={},S={},RM={},CG={}

AddReachable(𝑚𝑒𝑛𝑡𝑟𝑦)while WL is not empty do

remove 𝑛, 𝑝𝑡𝑠 from WLΔ = pts – pt(n)Propagate(n, Δ)if n represents a variable x then

foreach 𝑜𝑖 ∈ Δ doforeach x.f = y ∈ 𝑆 do

AddEdge(y, 𝑜𝑖 . 𝑓)foreach y = x.f ∈ 𝑆 do

AddEdge(𝑜𝑖 . 𝑓, y)ProcessCall(x, 𝑜𝑖)

[ ]WL:

CG:

RM:

Processing:

𝑏, {𝑜4}

ProcessCall(x, 𝑜𝑖)foreach l: r = x.k(a1,…,an) ∈ 𝑆 do

𝑚 = Dispatch(𝑜𝑖, k)

add 𝑚𝑡ℎ𝑖𝑠, {𝑜𝑖} to WL

if l → 𝑚 ∉ CG then

add l → 𝑚 to CG

AddReachable(𝑚)

foreach parameter 𝑝𝑖 of 𝑚 do

AddEdge(𝑎𝑖, 𝑝𝑖)AddEdge(𝑚𝑟𝑒𝑡, 𝑟) {𝑜3}

𝑎

{𝑜4}

𝑏

PFG:

{ A.main() }

{ }

Page 136: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

An Example

Tian Tan @ Nanjing University 136

1 class A {2 static void main() {3 A a = new A();4 A b = new B();5 A c = b.foo(a);6 }7 A foo(A x) { … }8 }9 class B extends A {10 A foo(A y) {11 A r = new A();12 return r;13 }14 }

Solve(𝑚𝑒𝑛𝑡𝑟𝑦)WL=[ ],PFG={},S={},RM={},CG={}

AddReachable(𝑚𝑒𝑛𝑡𝑟𝑦)while WL is not empty do

remove 𝑛, 𝑝𝑡𝑠 from WLΔ = pts – pt(n)Propagate(n, Δ)if n represents a variable x then

foreach 𝑜𝑖 ∈ Δ doforeach x.f = y ∈ 𝑆 do

AddEdge(y, 𝑜𝑖 . 𝑓)foreach y = x.f ∈ 𝑆 do

AddEdge(𝑜𝑖 . 𝑓, y)ProcessCall(x, 𝑜𝑖)

[ ]WL:

CG:

RM:

Processing:

𝑏, {𝑜4}

ProcessCall(x, 𝑜𝑖)foreach l: r = x.k(a1,…,an) ∈ 𝑆 do

𝑚 = Dispatch(𝑜𝑖, k)

add 𝑚𝑡ℎ𝑖𝑠, {𝑜𝑖} to WL

if l → 𝑚 ∉ CG then

add l → 𝑚 to CG

AddReachable(𝑚)

foreach parameter 𝑝𝑖 of 𝑚 do

AddEdge(𝑎𝑖, 𝑝𝑖)AddEdge(𝑚𝑟𝑒𝑡, 𝑟) {𝑜3}

𝑎

{𝑜4}

𝑏

PFG:

𝑚 =

{ A.main() }

{ }

Page 137: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

An Example

Tian Tan @ Nanjing University 137

1 class A {2 static void main() {3 A a = new A();4 A b = new B();5 A c = b.foo(a);6 }7 A foo(A x) { … }8 }9 class B extends A {10 A foo(A y) {11 A r = new A();12 return r;13 }14 }

Solve(𝑚𝑒𝑛𝑡𝑟𝑦)WL=[ ],PFG={},S={},RM={},CG={}

AddReachable(𝑚𝑒𝑛𝑡𝑟𝑦)while WL is not empty do

remove 𝑛, 𝑝𝑡𝑠 from WLΔ = pts – pt(n)Propagate(n, Δ)if n represents a variable x then

foreach 𝑜𝑖 ∈ Δ doforeach x.f = y ∈ 𝑆 do

AddEdge(y, 𝑜𝑖 . 𝑓)foreach y = x.f ∈ 𝑆 do

AddEdge(𝑜𝑖 . 𝑓, y)ProcessCall(x, 𝑜𝑖)

[ ]WL:

CG:

RM:

Processing:

𝑏, {𝑜4}

ProcessCall(x, 𝑜𝑖)foreach l: r = x.k(a1,…,an) ∈ 𝑆 do

𝑚 = Dispatch(𝑜𝑖, k)

add 𝑚𝑡ℎ𝑖𝑠, {𝑜𝑖} to WL

if l → 𝑚 ∉ CG then

add l → 𝑚 to CG

AddReachable(𝑚)

foreach parameter 𝑝𝑖 of 𝑚 do

AddEdge(𝑎𝑖, 𝑝𝑖)AddEdge(𝑚𝑟𝑒𝑡, 𝑟) {𝑜3}

𝑎

{𝑜4}

𝑏

PFG:

𝑚 = B.foo(A)

{ A.main() }

{ }

Page 138: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

An Example

Tian Tan @ Nanjing University 138

1 class A {2 static void main() {3 A a = new A();4 A b = new B();5 A c = b.foo(a);6 }7 A foo(A x) { … }8 }9 class B extends A {10 A foo(A y) {11 A r = new A();12 return r;13 }14 }

Solve(𝑚𝑒𝑛𝑡𝑟𝑦)WL=[ ],PFG={},S={},RM={},CG={}

AddReachable(𝑚𝑒𝑛𝑡𝑟𝑦)while WL is not empty do

remove 𝑛, 𝑝𝑡𝑠 from WLΔ = pts – pt(n)Propagate(n, Δ)if n represents a variable x then

foreach 𝑜𝑖 ∈ Δ doforeach x.f = y ∈ 𝑆 do

AddEdge(y, 𝑜𝑖 . 𝑓)foreach y = x.f ∈ 𝑆 do

AddEdge(𝑜𝑖 . 𝑓, y)ProcessCall(x, 𝑜𝑖)

[ B. foo/𝑡ℎ𝑖𝑠, {𝑜4} ]WL:

CG:

RM:

Processing:

𝑏, {𝑜4}

ProcessCall(x, 𝑜𝑖)foreach l: r = x.k(a1,…,an) ∈ 𝑆 do

𝑚 = Dispatch(𝑜𝑖, k)

add 𝑚𝑡ℎ𝑖𝑠, {𝑜𝑖} to WL

if l → 𝑚 ∉ CG then

add l → 𝑚 to CG

AddReachable(𝑚)

foreach parameter 𝑝𝑖 of 𝑚 do

AddEdge(𝑎𝑖, 𝑝𝑖)AddEdge(𝑚𝑟𝑒𝑡, 𝑟) {𝑜3}

𝑎

{𝑜4}

𝑏

PFG:

{ A.main() }

{ }

Page 139: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

An Example

Tian Tan @ Nanjing University 139

1 class A {2 static void main() {3 A a = new A();4 A b = new B();5 A c = b.foo(a);6 }7 A foo(A x) { … }8 }9 class B extends A {10 A foo(A y) {11 A r = new A();12 return r;13 }14 }

Solve(𝑚𝑒𝑛𝑡𝑟𝑦)WL=[ ],PFG={},S={},RM={},CG={}

AddReachable(𝑚𝑒𝑛𝑡𝑟𝑦)while WL is not empty do

remove 𝑛, 𝑝𝑡𝑠 from WLΔ = pts – pt(n)Propagate(n, Δ)if n represents a variable x then

foreach 𝑜𝑖 ∈ Δ doforeach x.f = y ∈ 𝑆 do

AddEdge(y, 𝑜𝑖 . 𝑓)foreach y = x.f ∈ 𝑆 do

AddEdge(𝑜𝑖 . 𝑓, y)ProcessCall(x, 𝑜𝑖)

[ B. foo/𝑡ℎ𝑖𝑠, {𝑜4} ]WL:

CG:

RM:

Processing:

𝑏, {𝑜4}

ProcessCall(x, 𝑜𝑖)foreach l: r = x.k(a1,…,an) ∈ 𝑆 do

𝑚 = Dispatch(𝑜𝑖, k)

add 𝑚𝑡ℎ𝑖𝑠, {𝑜𝑖} to WL

if l → 𝑚 ∉ CG then

add l → 𝑚 to CG

AddReachable(𝑚)

foreach parameter 𝑝𝑖 of 𝑚 do

AddEdge(𝑎𝑖, 𝑝𝑖)AddEdge(𝑚𝑟𝑒𝑡, 𝑟) {𝑜3}

𝑎

{𝑜4}

𝑏

PFG:

{ A.main() }

{ 5 → B.foo(A) }

Page 140: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

An Example

Tian Tan @ Nanjing University 140

1 class A {2 static void main() {3 A a = new A();4 A b = new B();5 A c = b.foo(a);6 }7 A foo(A x) { … }8 }9 class B extends A {10 A foo(A y) {11 A r = new A();12 return r;13 }14 }

Solve(𝑚𝑒𝑛𝑡𝑟𝑦)WL=[ ],PFG={},S={},RM={},CG={}

AddReachable(𝑚𝑒𝑛𝑡𝑟𝑦)while WL is not empty do

remove 𝑛, 𝑝𝑡𝑠 from WLΔ = pts – pt(n)Propagate(n, Δ)if n represents a variable x then

foreach 𝑜𝑖 ∈ Δ doforeach x.f = y ∈ 𝑆 do

AddEdge(y, 𝑜𝑖 . 𝑓)foreach y = x.f ∈ 𝑆 do

AddEdge(𝑜𝑖 . 𝑓, y)ProcessCall(x, 𝑜𝑖)

[ B. foo/𝑡ℎ𝑖𝑠, {𝑜4} ]WL:

CG:

RM:

Processing:

𝑏, {𝑜4}

ProcessCall(x, 𝑜𝑖)foreach l: r = x.k(a1,…,an) ∈ 𝑆 do

𝑚 = Dispatch(𝑜𝑖, k)

add 𝑚𝑡ℎ𝑖𝑠, {𝑜𝑖} to WL

if l → 𝑚 ∉ CG then

add l → 𝑚 to CG

AddReachable(𝑚)

foreach parameter 𝑝𝑖 of 𝑚 do

AddEdge(𝑎𝑖, 𝑝𝑖)AddEdge(𝑚𝑟𝑒𝑡, 𝑟) {𝑜3}

𝑎

{𝑜4}

𝑏

PFG:

{ A.main() }

{ 5 → B.foo(A) }

CHA: { 5 → B.foo(A) , 5 → A.foo(A) }

Spurious call edge

Page 141: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

An Example

Tian Tan @ Nanjing University 141

1 class A {2 static void main() {3 A a = new A();4 A b = new B();5 A c = b.foo(a);6 }7 A foo(A x) { … }8 }9 class B extends A {10 A foo(A y) {11 A r = new A();12 return r;13 }14 }

Solve(𝑚𝑒𝑛𝑡𝑟𝑦)WL=[ ],PFG={},S={},RM={},CG={}

AddReachable(𝑚𝑒𝑛𝑡𝑟𝑦)while WL is not empty do

remove 𝑛, 𝑝𝑡𝑠 from WLΔ = pts – pt(n)Propagate(n, Δ)if n represents a variable x then

foreach 𝑜𝑖 ∈ Δ doforeach x.f = y ∈ 𝑆 do

AddEdge(y, 𝑜𝑖 . 𝑓)foreach y = x.f ∈ 𝑆 do

AddEdge(𝑜𝑖 . 𝑓, y)ProcessCall(x, 𝑜𝑖)

[ B. foo/𝑡ℎ𝑖𝑠, {𝑜4} ]WL:

CG:

RM:

Processing:

𝑏, {𝑜4}

ProcessCall(x, 𝑜𝑖)foreach l: r = x.k(a1,…,an) ∈ 𝑆 do

𝑚 = Dispatch(𝑜𝑖, k)

add 𝑚𝑡ℎ𝑖𝑠, {𝑜𝑖} to WL

if l → 𝑚 ∉ CG then

add l → 𝑚 to CG

AddReachable(𝑚)

foreach parameter 𝑝𝑖 of 𝑚 do

AddEdge(𝑎𝑖, 𝑝𝑖)AddEdge(𝑚𝑟𝑒𝑡, 𝑟) {𝑜3}

𝑎

{𝑜4}

𝑏

PFG:

What change?

{ A.main }

{ 5 → B.foo(A) }

Page 142: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

{ A.main }

An Example

Tian Tan @ Nanjing University 142

1 class A {2 static void main() {3 A a = new A();4 A b = new B();5 A c = b.foo(a);6 }7 A foo(A x) { … }8 }9 class B extends A {10 A foo(A y) {11 A r = new A();12 return r;13 }14 }

Solve(𝑚𝑒𝑛𝑡𝑟𝑦)WL=[ ],PFG={},S={},RM={},CG={}

AddReachable(𝑚𝑒𝑛𝑡𝑟𝑦)while WL is not empty do

remove 𝑛, 𝑝𝑡𝑠 from WLΔ = pts – pt(n)Propagate(n, Δ)if n represents a variable x then

foreach 𝑜𝑖 ∈ Δ doforeach x.f = y ∈ 𝑆 do

AddEdge(y, 𝑜𝑖 . 𝑓)foreach y = x.f ∈ 𝑆 do

AddEdge(𝑜𝑖 . 𝑓, y)ProcessCall(x, 𝑜𝑖)

[ B. foo/𝑡ℎ𝑖𝑠, {𝑜4} ]WL:

CG:

RM:

Processing:

𝑏, {𝑜4}

ProcessCall(x, 𝑜𝑖)foreach l: r = x.k(a1,…,an) ∈ 𝑆 do

𝑚 = Dispatch(𝑜𝑖, k)

add 𝑚𝑡ℎ𝑖𝑠, {𝑜𝑖} to WL

if l → 𝑚 ∉ CG then

add l → 𝑚 to CG

AddReachable(𝑚)

foreach parameter 𝑝𝑖 of 𝑚 do

AddEdge(𝑎𝑖, 𝑝𝑖)AddEdge(𝑚𝑟𝑒𝑡, 𝑟) {𝑜3}

𝑎

{𝑜4}

𝑏

PFG:

AddReachable(m)

if m ∉ RM then

add m to RM

𝑆 ∪= 𝑆𝑚foreach i: x = new T() ∈ 𝑆𝑚 do

add 𝑥, {𝑜𝑖} to WL

foreach x = y ∈ 𝑆𝑚 do

AddEdge(y, x){ 5 → B.foo(A) }

Page 143: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

An Example

Tian Tan @ Nanjing University 143

1 class A {2 static void main() {3 A a = new A();4 A b = new B();5 A c = b.foo(a);6 }7 A foo(A x) { … }8 }9 class B extends A {10 A foo(A y) {11 A r = new A();12 return r;13 }14 }

Solve(𝑚𝑒𝑛𝑡𝑟𝑦)WL=[ ],PFG={},S={},RM={},CG={}

AddReachable(𝑚𝑒𝑛𝑡𝑟𝑦)while WL is not empty do

remove 𝑛, 𝑝𝑡𝑠 from WLΔ = pts – pt(n)Propagate(n, Δ)if n represents a variable x then

foreach 𝑜𝑖 ∈ Δ doforeach x.f = y ∈ 𝑆 do

AddEdge(y, 𝑜𝑖 . 𝑓)foreach y = x.f ∈ 𝑆 do

AddEdge(𝑜𝑖 . 𝑓, y)ProcessCall(x, 𝑜𝑖)

[ B. foo/𝑡ℎ𝑖𝑠, {𝑜4} ]WL:

CG:

RM:

Processing:

𝑏, {𝑜4}

ProcessCall(x, 𝑜𝑖)foreach l: r = x.k(a1,…,an) ∈ 𝑆 do

𝑚 = Dispatch(𝑜𝑖, k)

add 𝑚𝑡ℎ𝑖𝑠, {𝑜𝑖} to WL

if l → 𝑚 ∉ CG then

add l → 𝑚 to CG

AddReachable(𝑚)

foreach parameter 𝑝𝑖 of 𝑚 do

AddEdge(𝑎𝑖, 𝑝𝑖)AddEdge(𝑚𝑟𝑒𝑡, 𝑟) {𝑜3}

𝑎

{𝑜4}

𝑏

PFG:

AddReachable(m)

if m ∉ RM then

add m to RM

𝑆 ∪= 𝑆𝑚foreach i: x = new T() ∈ 𝑆𝑚 do

add 𝑥, {𝑜𝑖} to WL

foreach x = y ∈ 𝑆𝑚 do

AddEdge(y, x){ 5 → B.foo(A) }

{ A.main() B.foo(A) }

Page 144: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

An Example

Tian Tan @ Nanjing University 144

1 class A {2 static void main() {3 A a = new A();4 A b = new B();5 A c = b.foo(a);6 }7 A foo(A x) { … }8 }9 class B extends A {10 A foo(A y) {11 A r = new A();12 return r;13 }14 }

Solve(𝑚𝑒𝑛𝑡𝑟𝑦)WL=[ ],PFG={},S={},RM={},CG={}

AddReachable(𝑚𝑒𝑛𝑡𝑟𝑦)while WL is not empty do

remove 𝑛, 𝑝𝑡𝑠 from WLΔ = pts – pt(n)Propagate(n, Δ)if n represents a variable x then

foreach 𝑜𝑖 ∈ Δ doforeach x.f = y ∈ 𝑆 do

AddEdge(y, 𝑜𝑖 . 𝑓)foreach y = x.f ∈ 𝑆 do

AddEdge(𝑜𝑖 . 𝑓, y)ProcessCall(x, 𝑜𝑖)

[ B. foo/𝑡ℎ𝑖𝑠, {𝑜4} ,

𝑟, {𝑜11} ]WL:

CG:

RM:

Processing:

𝑏, {𝑜4}

ProcessCall(x, 𝑜𝑖)foreach l: r = x.k(a1,…,an) ∈ 𝑆 do

𝑚 = Dispatch(𝑜𝑖, k)

add 𝑚𝑡ℎ𝑖𝑠, {𝑜𝑖} to WL

if l → 𝑚 ∉ CG then

add l → 𝑚 to CG

AddReachable(𝑚)

foreach parameter 𝑝𝑖 of 𝑚 do

AddEdge(𝑎𝑖, 𝑝𝑖)AddEdge(𝑚𝑟𝑒𝑡, 𝑟) {𝑜3}

𝑎

{𝑜4}

𝑏

PFG:

AddReachable(m)

if m ∉ RM then

add m to RM

𝑆 ∪= 𝑆𝑚foreach i: x = new T() ∈ 𝑆𝑚 do

add 𝑥, {𝑜𝑖} to WL

foreach x = y ∈ 𝑆𝑚 do

AddEdge(y, x){ 5 → B.foo(A) }

{ A.main() B.foo(A) }

Page 145: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

An Example

Tian Tan @ Nanjing University 145

1 class A {2 static void main() {3 A a = new A();4 A b = new B();5 A c = b.foo(a);6 }7 A foo(A x) { … }8 }9 class B extends A {10 A foo(A y) {11 A r = new A();12 return r;13 }14 }

Solve(𝑚𝑒𝑛𝑡𝑟𝑦)WL=[ ],PFG={},S={},RM={},CG={}

AddReachable(𝑚𝑒𝑛𝑡𝑟𝑦)while WL is not empty do

remove 𝑛, 𝑝𝑡𝑠 from WLΔ = pts – pt(n)Propagate(n, Δ)if n represents a variable x then

foreach 𝑜𝑖 ∈ Δ doforeach x.f = y ∈ 𝑆 do

AddEdge(y, 𝑜𝑖 . 𝑓)foreach y = x.f ∈ 𝑆 do

AddEdge(𝑜𝑖 . 𝑓, y)ProcessCall(x, 𝑜𝑖)

[ B. foo/𝑡ℎ𝑖𝑠, {𝑜4} ,

𝑟, {𝑜11} , 𝑦, {𝑜3} ]WL:

CG:

RM:

ProcessCall(x, 𝑜𝑖)foreach l: r = x.k(a1,…,an) ∈ 𝑆 do

𝑚 = Dispatch(𝑜𝑖, k)

add 𝑚𝑡ℎ𝑖𝑠, {𝑜𝑖} to WL

if l → 𝑚 ∉ CG then

add l → 𝑚 to CG

AddReachable(𝑚)

foreach parameter 𝑝𝑖 of 𝑚 do

AddEdge(𝑎𝑖, 𝑝𝑖)AddEdge(𝑚𝑟𝑒𝑡, 𝑟) {𝑜3}

𝑎

{𝑜4}

𝑏

PFG:

{ }

𝑦

Processing:

𝑏, {𝑜4}

{ A.main() B.foo(A) }

{ 5 → B.foo(A) }

Page 146: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

An Example

Tian Tan @ Nanjing University 146

1 class A {2 static void main() {3 A a = new A();4 A b = new B();5 A c = b.foo(a);6 }7 A foo(A x) { … }8 }9 class B extends A {10 A foo(A y) {11 A r = new A();12 return r;13 }14 }

Solve(𝑚𝑒𝑛𝑡𝑟𝑦)WL=[ ],PFG={},S={},RM={},CG={}

AddReachable(𝑚𝑒𝑛𝑡𝑟𝑦)while WL is not empty do

remove 𝑛, 𝑝𝑡𝑠 from WLΔ = pts – pt(n)Propagate(n, Δ)if n represents a variable x then

foreach 𝑜𝑖 ∈ Δ doforeach x.f = y ∈ 𝑆 do

AddEdge(y, 𝑜𝑖 . 𝑓)foreach y = x.f ∈ 𝑆 do

AddEdge(𝑜𝑖 . 𝑓, y)ProcessCall(x, 𝑜𝑖)

[ B. foo/𝑡ℎ𝑖𝑠, {𝑜4} ,

𝑟, {𝑜11} , 𝑦, {𝑜3} ]WL:

CG:

RM:

ProcessCall(x, 𝑜𝑖)foreach l: r = x.k(a1,…,an) ∈ 𝑆 do

𝑚 = Dispatch(𝑜𝑖, k)

add 𝑚𝑡ℎ𝑖𝑠, {𝑜𝑖} to WL

if l → 𝑚 ∉ CG then

add l → 𝑚 to CG

AddReachable(𝑚)

foreach parameter 𝑝𝑖 of 𝑚 do

AddEdge(𝑎𝑖, 𝑝𝑖)AddEdge(𝑚𝑟𝑒𝑡, 𝑟) {𝑜3}

𝑎

{𝑜4}

𝑏

PFG:

{ }

𝑦

{ }{ }

𝑟𝑐Processing:

𝑏, {𝑜4}

{ A.main() B.foo(A) }

{ 5 → B.foo(A) }

Page 147: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

An Example

Tian Tan @ Nanjing University 147

1 class A {2 static void main() {3 A a = new A();4 A b = new B();5 A c = b.foo(a);6 }7 A foo(A x) { … }8 }9 class B extends A {10 A foo(A y) {11 A r = new A();12 return r;13 }14 }

Solve(𝑚𝑒𝑛𝑡𝑟𝑦)WL=[ ],PFG={},S={},RM={},CG={}

AddReachable(𝑚𝑒𝑛𝑡𝑟𝑦)while WL is not empty do

remove 𝑛, 𝑝𝑡𝑠 from WLΔ = pts – pt(n)Propagate(n, Δ)if n represents a variable x then

foreach 𝑜𝑖 ∈ Δ doforeach x.f = y ∈ 𝑆 do

AddEdge(y, 𝑜𝑖 . 𝑓)foreach y = x.f ∈ 𝑆 do

AddEdge(𝑜𝑖 . 𝑓, y)ProcessCall(x, 𝑜𝑖)

[ 𝑟, {𝑜11} , 𝑦, {𝑜3} ]WL:

CG:

RM:

Processing:

B. foo/𝑡ℎ𝑖𝑠, {𝑜4}

{𝑜3}

𝑎

{𝑜4}

𝑏

PFG:

{ }

𝑦

{ }{ }

𝑟𝑐

B.foo/𝑡ℎ𝑖𝑠

{𝑜4}{ A.main() B.foo(A) }

{ 5 → B.foo(A) }

Page 148: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

An Example

Tian Tan @ Nanjing University 148

1 class A {2 static void main() {3 A a = new A();4 A b = new B();5 A c = b.foo(a);6 }7 A foo(A x) { … }8 }9 class B extends A {10 A foo(A y) {11 A r = new A();12 return r;13 }14 }

Solve(𝑚𝑒𝑛𝑡𝑟𝑦)WL=[ ],PFG={},S={},RM={},CG={}

AddReachable(𝑚𝑒𝑛𝑡𝑟𝑦)while WL is not empty do

remove 𝑛, 𝑝𝑡𝑠 from WLΔ = pts – pt(n)Propagate(n, Δ)if n represents a variable x then

foreach 𝑜𝑖 ∈ Δ doforeach x.f = y ∈ 𝑆 do

AddEdge(y, 𝑜𝑖 . 𝑓)foreach y = x.f ∈ 𝑆 do

AddEdge(𝑜𝑖 . 𝑓, y)ProcessCall(x, 𝑜𝑖)

[ 𝑦, {𝑜3} , 𝑐, {𝑜11} ]WL:

CG:

RM:

Processing:

𝑟, {𝑜11}

{𝑜3}

𝑎

{𝑜4}

𝑏

PFG:

{ }

𝑦

{𝑜11}{ }

𝑟𝑐

B.foo/𝑡ℎ𝑖𝑠

{𝑜4}{ A.main() B.foo(A) }

{ 5 → B.foo(A) }

Page 149: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

An Example

Tian Tan @ Nanjing University 149

1 class A {2 static void main() {3 A a = new A();4 A b = new B();5 A c = b.foo(a);6 }7 A foo(A x) { … }8 }9 class B extends A {10 A foo(A y) {11 A r = new A();12 return r;13 }14 }

Solve(𝑚𝑒𝑛𝑡𝑟𝑦)WL=[ ],PFG={},S={},RM={},CG={}

AddReachable(𝑚𝑒𝑛𝑡𝑟𝑦)while WL is not empty do

remove 𝑛, 𝑝𝑡𝑠 from WLΔ = pts – pt(n)Propagate(n, Δ)if n represents a variable x then

foreach 𝑜𝑖 ∈ Δ doforeach x.f = y ∈ 𝑆 do

AddEdge(y, 𝑜𝑖 . 𝑓)foreach y = x.f ∈ 𝑆 do

AddEdge(𝑜𝑖 . 𝑓, y)ProcessCall(x, 𝑜𝑖)

WL:

CG:

RM:

Processing:

𝑦, {𝑜3}

{𝑜3}

𝑎

{𝑜4}

𝑏

PFG:

{𝑜3}

𝑦

{𝑜11}

𝑟𝑐

B.foo/𝑡ℎ𝑖𝑠

{𝑜4}{ A.main() B.foo(A) }

{ 5 → B.foo(A) }

[ 𝑐, {𝑜11} ]

{ }

Page 150: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

An Example

Tian Tan @ Nanjing University 150

1 class A {2 static void main() {3 A a = new A();4 A b = new B();5 A c = b.foo(a);6 }7 A foo(A x) { … }8 }9 class B extends A {10 A foo(A y) {11 A r = new A();12 return r;13 }14 }

Solve(𝑚𝑒𝑛𝑡𝑟𝑦)WL=[ ],PFG={},S={},RM={},CG={}

AddReachable(𝑚𝑒𝑛𝑡𝑟𝑦)while WL is not empty do

remove 𝑛, 𝑝𝑡𝑠 from WLΔ = pts – pt(n)Propagate(n, Δ)if n represents a variable x then

foreach 𝑜𝑖 ∈ Δ doforeach x.f = y ∈ 𝑆 do

AddEdge(y, 𝑜𝑖 . 𝑓)foreach y = x.f ∈ 𝑆 do

AddEdge(𝑜𝑖 . 𝑓, y)ProcessCall(x, 𝑜𝑖)

[ ]WL:

CG:

RM:

Processing:

𝑐, {𝑜11}

{𝑜3}

𝑎

{𝑜4}

𝑏

PFG:

{𝑜3}

𝑦

{𝑜11}{𝑜11}

𝑟𝑐

B.foo/𝑡ℎ𝑖𝑠

{𝑜4}{ A.main() B.foo(A) }

{ 5 → B.foo(A) }

Page 151: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

An Example

Tian Tan @ Nanjing University 151

1 class A {2 static void main() {3 A a = new A();4 A b = new B();5 A c = b.foo(a);6 }7 A foo(A x) { … }8 }9 class B extends A {10 A foo(A y) {11 A r = new A();12 return r;13 }14 }

Solve(𝑚𝑒𝑛𝑡𝑟𝑦)WL=[ ],PFG={},S={},RM={},CG={}

AddReachable(𝑚𝑒𝑛𝑡𝑟𝑦)while WL is not empty do

remove 𝑛, 𝑝𝑡𝑠 from WLΔ = pts – pt(n)Propagate(n, Δ)if n represents a variable x then

foreach 𝑜𝑖 ∈ Δ doforeach x.f = y ∈ 𝑆 do

AddEdge(y, 𝑜𝑖 . 𝑓)foreach y = x.f ∈ 𝑆 do

AddEdge(𝑜𝑖 . 𝑓, y)ProcessCall(x, 𝑜𝑖)

[ ]WL:

CG:

RM:

Processing:

{𝑜3}

𝑎

{𝑜4}

𝑏

PFG:

{𝑜3}

𝑦

{𝑜11}{𝑜11}

𝑟𝑐

B.foo/𝑡ℎ𝑖𝑠

{𝑜4}{ A.main() B.foo(A) }

{ 5 → B.foo(A) }

Algorithm finishes

Final results

Page 152: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

TheX You Need To Understand in This Lecture

• Understand pointer analysis rule for method call

• Understand inter-procedural pointer analysis algorithm

• Understand on-the-fly call graph construction

Tian Tan @ Nanjing University

Page 153: Static Program Analysis · 2020-06-12 · Static Program Analysis 2020 Spring. Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis Foundations. Contents 1. Pointer

南京大学

李樾

谭添

计算机科学与技术系

程序设计语言

静态分析研究组

软件分析