131
Nanjing University Tian Tan 2020 Pointer Analysis Static Program Analysis

Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

  • Upload
    others

  • View
    8

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Nanjing University

Tian Tan

2020

Pointer Analysis

Static Program Analysis

Page 2: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Contents

1. Motivation2. Introduction to Pointer Analysis3. Key Factors of Pointer Analysis4. Concerned Statements

2Tian Tan @ Nanjing University

Page 3: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Contents

1. Motivation2. Introduction to Pointer Analysis3. Key Factors of Pointer Analysis4. Concerned Statements

3Tian Tan @ Nanjing University

Page 4: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Problem of CHA

4

void foo() {Number n = new One();int x = n.get();

}

interface Number {int get();

}class Zero implements Number {

public int get() { return 0; }}class One implements Number {

public int get() { return 1; }}class Two implements Number {

public int get() { return 2; }}

Tian Tan @ Nanjing University

Page 5: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Problem of CHA

5

void foo() {Number n = new One();int x = n.get();

}

interface Number {int get();

}class Zero implements Number {

public int get() { return 0; }}class One implements Number {

public int get() { return 1; }}class Two implements Number {

public int get() { return 2; }}

CHA:

• call targets

Tian Tan @ Nanjing University

Page 6: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Problem of CHA

6

void foo() {Number n = new One();int x = n.get();

}

interface Number {int get();

}class Zero implements Number {

public int get() { return 0; }}class One implements Number {

public int get() { return 1; }}class Two implements Number {

public int get() { return 2; }}

CHA: based onclass hierarchy• 3 call targets

Tian Tan @ Nanjing University

Page 7: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Problem of CHA

7

void foo() {Number n = new One();int x = n.get();

}

interface Number {int get();

}class Zero implements Number {

public int get() { return 0; }}class One implements Number {

public int get() { return 1; }}class Two implements Number {

public int get() { return 2; }}

CHA: based onclass hierarchy• 3 call targets

Constant propagation• x =

Tian Tan @ Nanjing University

Page 8: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Problem of CHA

8

void foo() {Number n = new One();int x = n.get();

}

interface Number {int get();

}class Zero implements Number {

public int get() { return 0; }}class One implements Number {

public int get() { return 1; }}class Two implements Number {

public int get() { return 2; }}

CHA: based onclass hierarchy• 3 call targets

Constant propagation• x = NAC

Tian Tan @ Nanjing University

Page 9: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Problem of CHA

9

void foo() {Number n = new One();int x = n.get();

}

interface Number {int get();

}class Zero implements Number {

public int get() { return 0; }}class One implements Number {

public int get() { return 1; }}class Two implements Number {

public int get() { return 2; }}

CHA: based on only considersclass hierarchy• 3 call targets• 2 false positives

Constant propagation• x = NACX X

Tian Tan @ Nanjing University

imprecise

Page 10: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Via Pointer Analysis

10

void foo() {Number n = new One();int x = n.get();

}

interface Number {int get();

}class Zero implements Number {

public int get() { return 0; }}class One implements Number {

public int get() { return 1; }}class Two implements Number {

public int get() { return 2; }}

CHA: based on only considersclass hierarchy• 3 call targets• 2 false positives

Constant propagation• x = NAC

Pointer analysis: based on points-to relation• 1 call target

Tian Tan @ Nanjing University

imprecise

n points to new One

Page 11: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Via Pointer Analysis

11

void foo() {Number n = new One();int x = n.get();

}

interface Number {int get();

}class Zero implements Number {

public int get() { return 0; }}class One implements Number {

public int get() { return 1; }}class Two implements Number {

public int get() { return 2; }}

CHA: based on only considersclass hierarchy• 3 call targets• 2 false positives

Constant propagation• x = NAC

Pointer analysis: based on points-to relation• 1 call target

Constant propagation• x = 1

Tian Tan @ Nanjing University

imprecise

n points to new One

Page 12: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Via Pointer Analysis

12

void foo() {Number n = new One();int x = n.get();

}

interface Number {int get();

}class Zero implements Number {

public int get() { return 0; }}class One implements Number {

public int get() { return 1; }}class Two implements Number {

public int get() { return 2; }}

CHA: based on only considersclass hierarchy• 3 call targets• 2 false positives

Constant propagation• x = NAC

Pointer analysis: based on points-to relation• 1 call target• 0 false positive

Constant propagation• x = 1

Tian Tan @ Nanjing University

imprecise

precise

n points to new One

Page 13: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Contents

13Tian Tan @ Nanjing University

1. Motivation2. Introduction to Pointer Analysis3. Key Factors of Pointer Analysis4. Concerned Statements

Page 14: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Pointer Analysis• A fundamental static analysis

• Computes which memory locations a pointer can point to

14Tian Tan @ Nanjing University

Page 15: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Pointer Analysis• A fundamental static analysis

• Computes which memory locations a pointer can point to

• For object-oriented programs (focus on Java)• Computes which objects a pointer (variable or field) can point to

15Tian Tan @ Nanjing University

Page 16: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Pointer Analysis• A fundamental static analysis

• Computes which memory locations a pointer can point to

• For object-oriented programs (focus on Java)• Computes which objects a pointer (variable or field) can point to

• Regarded as a may-analysis• Computes an over-approximation of the set of objects that a pointer

can point to, i.e., we ask “a pointer may point to which objects?”

16Tian Tan @ Nanjing University

Page 17: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Pointer Analysis• A fundamental static analysis

• Computes which memory locations a pointer can point to

• For object-oriented programs (focus on Java)• Computes which objects a pointer (variable or field) can point to

• Regarded as a may-analysis• Computes an over-approximation of the set of objects that a pointer

can point to, i.e., we ask “a pointer may point to which objects?”

17

A research area with 40+ years of history William E. Weihl, “Interprocedural Data Flow Analysis in the Presence

of Pointers, Procedure Variables, and Label Variables”. POPL 1980.Still an active area today

OOPSLA’18, FSE’18, TOPLAS’19, OOPSLA’19, TOPLAS’20, …Tian Tan @ Nanjing University

Page 18: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Example

18

void foo() {A a = new A();B x = new B();a.setB(x);B y = a.getB();

}

class A {B b;void setB(B b) { this.b = b; }B getB() { return this.b; }

}

“Which objects a pointer can point to?”

Program Points-to relations

Tian Tan @ Nanjing University

Page 19: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Example

19

void foo() {A a = new A();B x = new B();a.setB(x);B y = a.getB();

}

class A {B b;void setB(B b) { this.b = b; }B getB() { return this.b; }

}

Variable Object

a new A

x new B

Program Points-to relations

“Which objects a pointer can point to?”

Tian Tan @ Nanjing University

Page 20: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Example

Variable Object

a new A

x new B

this

b

20

void foo() {A a = new A();B x = new B();a.setB(x);B y = a.getB();

}

class A {B b;void setB(B b) { this.b = b; }B getB() { return this.b; }

}

Program Points-to relations

“Which objects a pointer can point to?”

Tian Tan @ Nanjing University

Page 21: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Example

Variable Object

a new A

x new B

this new A

b new B

21

void foo() {A a = new A();B x = new B();a.setB(x);B y = a.getB();

}

class A {B b;void setB(B b) { this.b = b; }B getB() { return this.b; }

}

Program Points-to relations

“Which objects a pointer can point to?”

Tian Tan @ Nanjing University

Page 22: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Example

Variable Object

a new A

x new B

this new A

b new B

22

void foo() {A a = new A();B x = new B();a.setB(x);B y = a.getB();

}

class A {B b;void setB(B b) { this.b = b; }B getB() { return this.b; }

}

Field Object

new A.b new B

Program

“Which objects a pointer can point to?”

Points-to relations

Tian Tan @ Nanjing University

Page 23: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Example

23

void foo() {A a = new A();B x = new B();a.setB(x);B y = a.getB();

}

class A {B b;void setB(B b) { this.b = b; }B getB() { return this.b; }

}

Field Object

new A.b new B

Variable Object

a new A

x new B

this new A

b new B

y

Program Points-to relations

“Which objects a pointer can point to?”

Tian Tan @ Nanjing University

Page 24: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Example

24

void foo() {A a = new A();B x = new B();a.setB(x);B y = a.getB();

}

class A {B b;void setB(B b) { this.b = b; }B getB() { return this.b; }

}

Field Object

new A.b new B

Variable Object

a new A

x new B

this new A

b new B

y new B

Program Points-to relations

“Which objects a pointer can point to?”

Tian Tan @ Nanjing University

Page 25: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Example

Program Points-to relations

25

void foo() {A a = new A();B x = new B();a.setB(x);B y = a.getB();

}

class A {B b;void setB(B b) { this.b = b; }B getB() { return this.b; }

}

Field Object

new A.b new B

Variable Object

a new A

x new B

this new A

b new B

y new B

Pointer Analysis

input output

“Which objects a pointer can point to?”

Tian Tan @ Nanjing University

Page 26: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Pointer Analysis and Alias Analysis

Two closely related but different concepts• Pointer analysis: which objects a pointer can point to?• Alias analysis: can two pointers point to the same object?

Tian Tan @ Nanjing University 26

Page 27: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Pointer Analysis and Alias Analysis

Two closely related but different concepts• Pointer analysis: which objects a pointer can point to?• Alias analysis: can two pointers point to the same object?

If two pointers, say p and q, refer to the same object, then pand q are aliases

Tian Tan @ Nanjing University 27

p = new C();q = p;x = new X();y = new Y();

p and q are aliasesx and y are not aliases

Page 28: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Pointer Analysis and Alias Analysis

Two closely related but different concepts• Pointer analysis: which objects a pointer can point to?• Alias analysis: can two pointers point to the same object?

If two pointers, say p and q, refer to the same object, then pand q are aliases

Tian Tan @ Nanjing University 28

Alias information can be derived from points-to relations

p = new C();q = p;x = new X();y = new Y();

p and q are aliasesx and y are not aliases

Page 29: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Applications of Pointer Analysis

• Fundamental informationoCall graph, aliases, …

• Compiler optimizationoVirtual call inlining, …

• Bug detectionoNull pointer detection, …

• Security analysiso Information flow analysis, …

• And many more …

29Tian Tan @ Nanjing University

“Pointer analysis is one of the most fundamental static program analyses,

on which virtually all others are built.”*

Page 30: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Applications of Pointer Analysis

• Fundamental informationoCall graph, aliases, …

• Compiler optimizationoVirtual call inlining, …

• Bug detectionoNull pointer detection, …

• Security analysiso Information flow analysis, …

• And many more …

30

“Pointer analysis is one of the most fundamental static program analyses,

on which virtually all others are built.”*

*Pointer Analysis - Report from Dagstuhl Seminar 13162. 2013.Tian Tan @ Nanjing University

Page 31: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Contents

31Tian Tan @ Nanjing University

1. Motivation2. Introduction to Pointer Analysis3. Key Factors of Pointer Analysis4. Concerned Statements

Page 32: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Key Factors in Pointer Analysis

32

• Pointer analysis is a complex system• Multiple factors affect the precision and efficiency of the system

Tian Tan @ Nanjing University

Page 33: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Key Factors in Pointer Analysis

Factor Problem ChoiceHeap abstraction

How to model heap memory?

• Allocation-site• Storeless

Context sensitivity

How to model calling contexts?

• Context-sensitive• Context-insensitive

Flow sensitivity How to model control flow?

• Flow-sensitive• Flow-insensitive

Analysis scope Which parts of program should be analyzed?

• Whole-program• Demand-driven

33

• Pointer analysis is a complex system• Multiple factors affect the precision and efficiency of the system

Tian Tan @ Nanjing University

Page 34: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Key Factors in Pointer Analysis

Factor Problem ChoiceHeap abstraction

How to model heap memory?

• Allocation-site• Storeless

Context sensitivity

How to model calling contexts?

• Context-sensitive• Context-insensitive

Flow sensitivity How to model control flow?

• Flow-sensitive• Flow-insensitive

Analysis scope Which parts of program should be analyzed?

• Whole-program• Demand-driven

34

• Pointer analysis is a complex system• Multiple factors affect the precision and efficiency of the system

Tian Tan @ Nanjing University

Page 35: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Key Factors in Pointer Analysis

Factor Problem ChoiceHeap abstraction

How to model heap memory?

• Allocation-site• Storeless

Context sensitivity

How to model calling contexts?

• Context-sensitive• Context-insensitive

Flow sensitivity How to model control flow?

• Flow-sensitive• Flow-insensitive

Analysis scope Which parts of program should be analyzed?

• Whole-program• Demand-driven

35

• Pointer analysis is a complex system• Multiple factors affect the precision and efficiency of the system

Tian Tan @ Nanjing University

Page 36: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Key Factors in Pointer Analysis

Factor Problem ChoiceHeap abstraction

How to model heap memory?

• Allocation-site• Storeless

Context sensitivity

How to model calling contexts?

• Context-sensitive• Context-insensitive

Flow sensitivity How to model control flow?

• Flow-sensitive• Flow-insensitive

Analysis scope Which parts of program should be analyzed?

• Whole-program• Demand-driven

36

• Pointer analysis is a complex system• Multiple factors affect the precision and efficiency of the system

Tian Tan @ Nanjing University

Page 37: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Key Factors in Pointer Analysis

Factor Problem ChoiceHeap abstraction

How to model heap memory?

• Allocation-site• Storeless

Context sensitivity

How to model calling contexts?

• Context-sensitive• Context-insensitive

Flow sensitivity How to model control flow?

• Flow-sensitive• Flow-insensitive

Analysis scope Which parts of program should be analyzed?

• Whole-program• Demand-driven

37

• Pointer analysis is a complex system• Multiple factors affect the precision and efficiency of the system

Tian Tan @ Nanjing University

Page 38: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Heap Abstraction

How to model heap memory?• In dynamic execution, the number of heap objects can be unbounded

due to loops and recursion

38

for (…) {A a = new A();

}

Tian Tan @ Nanjing University

Page 39: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Heap Abstraction

How to model heap memory?• In dynamic execution, the number of heap objects can be unbounded

due to loops and recursion

• To ensure termination, heap abstraction models dynamically allocated, unbounded concrete objects as finite abstract objects for static analysis

39

for (…) {A a = new A();

}

Tian Tan @ Nanjing University

Page 40: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Heap Abstraction

How to model heap memory?• In dynamic execution, the number of heap objects can be unbounded

due to loops and recursion

• To ensure termination, heap abstraction models dynamically allocated, unbounded concrete objects as finite abstract objects for static analysis

40

Dynamic execution Static analysis

abstracted

Bounded abstract objectsUnbounded concrete objects

for (…) {A a = new A();

}

Tian Tan @ Nanjing University

Page 41: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Heap Abstraction

41Tian Tan @ Nanjing UniversityVini Kanvar, Uday P. Khedker, “Heap Abstractions for Static Analysis”. ACM CSUR 2016

Page 42: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Heap Abstraction

42Tian Tan @ Nanjing UniversityVini Kanvar, Uday P. Khedker, “Heap Abstractions for Static Analysis”. ACM CSUR 2016

Page 43: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Allocation-Site Abstraction

• Model concrete objects by their allocation sites• One abstract object per allocation site to represent

all its allocated concrete objects

43

The most commonly-used heap abstraction

Tian Tan @ Nanjing University

Page 44: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Allocation-Site Abstraction

• Model concrete objects by their allocation sites• One abstract object per allocation site to represent

all its allocated concrete objects

44

1 for (i = 0; i < 3; ++i) {2 a = new A();3 …4 }

Dynamic execution

𝑜𝑜2, iteration i = 0𝑜𝑜2, iteration i = 1𝑜𝑜2, iteration i = 2

Tian Tan @ Nanjing University

The most commonly-used heap abstraction

Page 45: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Allocation-Site Abstraction

• Model concrete objects by their allocation sites• One abstract object per allocation site to represent

all its allocated concrete objects

45

1 for (i = 0; i < 3; ++i) {2 a = new A();3 …4 }

𝑜𝑜2

Dynamic executionAllocation-site

abstraction

𝑜𝑜2, iteration i = 0𝑜𝑜2, iteration i = 1𝑜𝑜2, iteration i = 2

abstracted

Tian Tan @ Nanjing University

The most commonly-used heap abstraction

Page 46: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Allocation-Site Abstraction

• Model concrete objects by their allocation sites• One abstract object per allocation site to represent

all its allocated concrete objects

46

1 for (i = 0; i < 3; ++i) {2 a = new A();3 …4 }

𝑜𝑜2

Dynamic execution

𝑜𝑜2, iteration i = 0𝑜𝑜2, iteration i = 1𝑜𝑜2, iteration i = 2

abstracted

Tian Tan @ Nanjing University

The number of allocation sites in a program is bounded,

thus the abstract objects must be finite.

The most commonly-used heap abstraction

Allocation-site abstraction

Page 47: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Key Factors in Pointer Analysis

Factor Problem ChoiceHeap abstraction

How to model heap memory?

• Allocation-site• Storeless

Context sensitivity

How to model calling contexts?

• Context-sensitive• Context-insensitive

Flow sensitivity How to model control flow?

• Flow-sensitive• Flow-insensitive

Analysis scope Which parts of program should be analyzed?

• Whole-program• Demand-driven

47

• Pointer analysis is a complex system• Multiple factors affect the precision and efficiency of the system

Tian Tan @ Nanjing University

Page 48: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Context SensitivityHow to model calling contexts?

48

Context-sensitive Context-insensitive

Distinguish different calling contexts of a method

Merge all calling contexts of a method

Analyze each method multiple times, once for each context

Analyze each method once

Tian Tan @ Nanjing University

Page 49: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Context SensitivityHow to model calling contexts?

49

a.foo(x); b.foo(y);

Context 1:void foo(T p) {

…}

Context-sensitive Context-insensitive

Distinguish different calling contexts of a method

Merge all calling contexts of a method

Analyze each method multiple times, once for each context

Analyze each method once

Context 2:void foo(T p) {

…}

Tian Tan @ Nanjing University

Page 50: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Context SensitivityHow to model calling contexts?

50

a.foo(x); b.foo(y);

Context 1:void foo(T p) {

…}

Context-sensitive Context-insensitive

Distinguish different calling contexts of a method

Merge all calling contexts of a method

Analyze each method multiple times, once for each context

Analyze each method once

Context 2:void foo(T p) {

…}

Tian Tan @ Nanjing University

a.foo(x); b.foo(y);

void foo(T p) {…

}

Page 51: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Context SensitivityHow to model calling contexts?

51

a.foo(x); b.foo(y);

Context 1:void foo(T p) {

…}

Context-sensitive Context-insensitive

Distinguish different calling contexts of a method

Merge all calling contexts of a method

Analyze each method multiple times, once for each context

Analyze each method once

Context 2:void foo(T p) {

…}

Tian Tan @ Nanjing University

a.foo(x); b.foo(y);

void foo(T p) {…

}

Page 52: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Context SensitivityHow to model calling contexts?

52

a.foo(x); b.foo(y);

Context 1:void foo(T p) {

…}

Context-sensitive Context-insensitive

Distinguish different calling contexts of a method

Merge all calling contexts of a method

Analyze each method multiple times, once for each context

Analyze each method once

Context 2:void foo(T p) {

…}

Tian Tan @ Nanjing University

a.foo(x); b.foo(y);

void foo(T p) {…

}

Very useful technique Significantly improve precision More details in later lectures

We start with this

Page 53: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Key Factors in Pointer Analysis

Factor Problem ChoiceHeap abstraction

How to model heap memory?

• Allocation-site• Storeless

Context sensitivity

How to model calling contexts?

• Context-sensitive• Context-insensitive

Flow sensitivity How to model control flow?

• Flow-sensitive• Flow-insensitive

Analysis scope Which parts of program should be analyzed?

• Whole-program• Demand-driven

53

• Pointer analysis is a complex system• Multiple factors affect the precision and efficiency of the system

Tian Tan @ Nanjing University

Page 54: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Flow SensitivityHow to model control flow?

54

Flow-sensitive Flow-insensitive

Respect the execution order of the statements

Ignore the control-flow order, treat the program as a set of unordered statements

Maintain a map of points-to relations at each program location

Maintain one map of points-to relations for the whole program

Tian Tan @ Nanjing University

Page 55: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Flow SensitivityHow to model control flow?

55

Flow-sensitive Flow-insensitive

Respect the execution order of the statements

Ignore the control-flow order, treat the program as a set of unordered statements

Maintain a map of points-to relations at each program location

Maintain one map of points-to relations for the whole program

Tian Tan @ Nanjing University

So far, all data-flow analyseswe have learnt are flow-sensitive

Page 56: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Flow SensitivityHow to model control flow?

56

Flow-sensitive Flow-insensitive

Respect the execution order of the statements

Ignore the control-flow order, treat the program as a set of unordered statements

Maintain a map of points-to relations at each program location

Maintain one map of points-to relations for the whole program

1 c = new C();2 c.f = "x";3 s = c.f;4 c.f = "y";

Tian Tan @ Nanjing University

Page 57: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Flow SensitivityHow to model control flow?

57

Flow-sensitive Flow-insensitive

Respect the execution order of the statements

Ignore the control-flow order, treat the program as a set of unordered statements

Maintain a map of points-to relations at each program location

Maintain one map of points-to relations for the whole program

1 c = new C();2 c.f = "x";3 s = c.f;4 c.f = "y";

c ➝ {o1}

Tian Tan @ Nanjing University

Page 58: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Flow SensitivityHow to model control flow?

58

Flow-sensitive Flow-insensitive

Respect the execution order of the statements

Ignore the control-flow order, treat the program as a set of unordered statements

Maintain a map of points-to relations at each program location

Maintain one map of points-to relations for the whole program

1 c = new C();2 c.f = "x";3 s = c.f;4 c.f = "y";

c ➝ {o1}

c ➝ {o1}o1.f ➝ {"x"}

Tian Tan @ Nanjing University

Page 59: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Flow SensitivityHow to model control flow?

59

Flow-sensitive Flow-insensitive

Respect the execution order of the statements

Ignore the control-flow order, treat the program as a set of unordered statements

Maintain a map of points-to relations at each program location

Maintain one map of points-to relations for the whole program

1 c = new C();2 c.f = "x";3 s = c.f;4 c.f = "y";

c ➝ {o1}o1.f ➝ {"x"}s ➝

c ➝ {o1}

c ➝ {o1}o1.f ➝ {"x"}

Tian Tan @ Nanjing University

Page 60: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Flow SensitivityHow to model control flow?

60

Flow-sensitive Flow-insensitive

Respect the execution order of the statements

Ignore the control-flow order, treat the program as a set of unordered statements

Maintain a map of points-to relations at each program location

Maintain one map of points-to relations for the whole program

1 c = new C();2 c.f = "x";3 s = c.f;4 c.f = "y";

c ➝ {o1}o1.f ➝ {"x"}s ➝ {"x"}

c ➝ {o1}

c ➝ {o1}o1.f ➝ {"x"}

Tian Tan @ Nanjing University

Page 61: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Flow SensitivityHow to model control flow?

61

Flow-sensitive Flow-insensitive

Respect the execution order of the statements

Ignore the control-flow order, treat the program as a set of unordered statements

Maintain a map of points-to relations at each program location

Maintain one map of points-to relations for the whole program

1 c = new C();2 c.f = "x";3 s = c.f;4 c.f = "y";

c ➝ {o1}o1.f ➝ {"x"}s ➝ {"x"}

c ➝ {o1}

c ➝ {o1}o1.f ➝ {"x"}

Page 62: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Flow SensitivityHow to model control flow?

62

Flow-sensitive Flow-insensitive

Respect the execution order of the statements

Ignore the control-flow order, treat the program as a set of unordered statements

Maintain a map of points-to relations at each program location

Maintain one map of points-to relations for the whole program

1 c = new C();2 c.f = "x";3 s = c.f;4 c.f = "y";

c ➝ {o1}o1.f ➝ {"x"}s ➝ {"x"}

c ➝ {o1}

c ➝ {o1}o1.f ➝ {"x"}

c ➝ {o1}o1.f ➝ {"y"}s ➝ {"x"}

Page 63: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Flow SensitivityHow to model control flow?

63

Flow-sensitive Flow-insensitive

Respect the execution order of the statements

Ignore the control-flow order, treat the program as a set of unordered statements

Maintain a map of points-to relations at each program location

Maintain one map of points-to relations for the whole program

1 c = new C();2 c.f = "x";3 s = c.f;4 c.f = "y";

c ➝ {o1}o1.f ➝ {"x"}s ➝ {"x"}

c ➝ {o1}c ➝ {o1}

c ➝ {o1}o1.f ➝ {"x"}

c ➝ {o1}o1.f ➝ {"y"}s ➝ {"x"}

Page 64: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Flow SensitivityHow to model control flow?

64

Flow-sensitive Flow-insensitive

Respect the execution order of the statements

Ignore the control-flow order, treat the program as a set of unordered statements

Maintain a map of points-to relations at each program location

Maintain one map of points-to relations for the whole program

1 c = new C();2 c.f = "x";3 s = c.f;4 c.f = "y";

c ➝ {o1}o1.f ➝ {"x"}s ➝ {"x"}

c ➝ {o1}o1.f ➝

c ➝ {o1}

c ➝ {o1}o1.f ➝ {"x"}

c ➝ {o1}o1.f ➝ {"y"}s ➝ {"x"}

Page 65: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Flow SensitivityHow to model control flow?

65

Flow-sensitive Flow-insensitive

Respect the execution order of the statements

Ignore the control-flow order, treat the program as a set of unordered statements

Maintain a map of points-to relations at each program location

Maintain one map of points-to relations for the whole program

1 c = new C();2 c.f = "x";3 s = c.f;4 c.f = "y";

c ➝ {o1}o1.f ➝ {"x"}s ➝ {"x"}

c ➝ {o1}o1.f ➝ {"x", "y"}

c ➝ {o1}

c ➝ {o1}o1.f ➝ {"x"}

c ➝ {o1}o1.f ➝ {"y"}s ➝ {"x"}

Page 66: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Flow SensitivityHow to model control flow?

66

Flow-sensitive Flow-insensitive

Respect the execution order of the statements

Ignore the control-flow order, treat the program as a set of unordered statements

Maintain a map of points-to relations at each program location

Maintain one map of points-to relations for the whole program

1 c = new C();2 c.f = "x";3 s = c.f;4 c.f = "y";

c ➝ {o1}o1.f ➝ {"x"}s ➝ {"x"}

c ➝ {o1}o1.f ➝ {"x", "y"}s ➝

c ➝ {o1}

c ➝ {o1}o1.f ➝ {"x"}

c ➝ {o1}o1.f ➝ {"y"}s ➝ {"x"}

Page 67: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Flow SensitivityHow to model control flow?

67

Flow-sensitive Flow-insensitive

Respect the execution order of the statements

Ignore the control-flow order, treat the program as a set of unordered statements

Maintain a map of points-to relations at each program location

Maintain one map of points-to relations for the whole program

1 c = new C();2 c.f = "x";3 s = c.f;4 c.f = "y";

c ➝ {o1}o1.f ➝ {"x"}s ➝ {"x"}

c ➝ {o1}o1.f ➝ {"x", "y"}s ➝ {"x", "y"}

c ➝ {o1}

c ➝ {o1}o1.f ➝ {"x"}

c ➝ {o1}o1.f ➝ {"y"}s ➝ {"x"}

Page 68: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Flow SensitivityHow to model control flow?

68

Flow-sensitive Flow-insensitive

Respect the execution order of the statements

Ignore the control-flow order, treat the program as a set of unordered statements

Maintain a map of points-to relations at each program location

Maintain one map of points-to relations for the whole program

1 c = new C();2 c.f = "x";3 s = c.f;4 c.f = "y";

c ➝ {o1}o1.f ➝ {"x"}s ➝ {"x"}

c ➝ {o1}o1.f ➝ {"x", "y"}s ➝ {"x", "y"}

c ➝ {o1}

c ➝ {o1}o1.f ➝ {"x"}

c ➝ {o1}o1.f ➝ {"y"}s ➝ {"x"}

false positive

Page 69: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Flow SensitivityHow to model control flow?

69

Flow-sensitive Flow-insensitive

Respect the execution order of the statements

Ignore the control-flow order, treat the program as a set of unordered statements

Maintain a map of points-to relations at each program location

Maintain one map of points-to relations for the whole program

1 c = new C();2 c.f = "x";3 s = c.f;4 c.f = "y";

c ➝ {o1}o1.f ➝ {"x"}s ➝ {"x"}

c ➝ {o1}o1.f ➝ {"x", "y"}s ➝ {"x", "y"}

c ➝ {o1}

c ➝ {o1}o1.f ➝ {"x"}

c ➝ {o1}o1.f ➝ {"y"}s ➝ {"x"}

Chosen in this course

Page 70: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Key Factors in Pointer Analysis

Factor Problem ChoiceHeap abstraction

How to model heap memory?

• Allocation-site• Storeless

Context sensitivity

How to model calling contexts?

• Context-sensitive• Context-insensitive

Flow sensitivity How to model control flow?

• Flow-sensitive• Flow-insensitive

Analysis scope Which parts of program should be analyzed?

• Whole-program• Demand-driven

70

• Pointer analysis is a complex system• Multiple factors affect the precision and efficiency of the system

Tian Tan @ Nanjing University

Page 71: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Analysis ScopeWhich parts of program should be analyzed?

71

Whole-program Demand-driven

Compute points-to information for all pointers in the program

Only compute points-to information for the pointers that may affect specific sites of interest (on demand)

Provide information for all possible clients Provide information for specific clients

Tian Tan @ Nanjing University

Page 72: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Analysis ScopeWhich parts of program should be analyzed?

72

Whole-program Demand-driven

Compute points-to information for all pointers in the program

Only compute points-to information for the pointers that may affect specific sites of interest (on demand)

Provide information for all possible clients Provide information for specific clients

1 x = new A();2 y = x;3 …4 z = new T();5 z.bar();

Tian Tan @ Nanjing University

Page 73: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Analysis ScopeWhich parts of program should be analyzed?

73

Whole-program Demand-driven

Compute points-to information for all pointers in the program

Only compute points-to information for the pointers that may affect specific sites of interest (on demand)

Provide information for all possible clients Provide information for specific clients

1 x = new A();2 y = x;3 … 4 z = new T();5 z.bar();

x ➝ {o1}y ➝ {o1}z ➝ {o4}

Tian Tan @ Nanjing University

Page 74: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Analysis ScopeWhich parts of program should be analyzed?

74

Whole-program Demand-driven

Compute points-to information for all pointers in the program

Only compute points-to information for the pointers that may affect specific sites of interest (on demand)

Provide information for all possible clients Provide information for specific clients

1 x = new A();2 y = x;3 …4 z = new T();5 z.bar();

x ➝ {o1}y ➝ {o1}z ➝ {o4}

Client: call graph constructionSite of interest: line 5

What points-to information do we need

Tian Tan @ Nanjing University

Page 75: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Analysis ScopeWhich parts of program should be analyzed?

75

Whole-program Demand-driven

Compute points-to information for all pointers in the program

Only compute points-to information for the pointers that may affect specific sites of interest (on demand)

Provide information for all possible clients Provide information for specific clients

1 x = new A();2 y = x;3 …4 z = new T();5 z.bar();

x ➝ {o1}y ➝ {o1}z ➝ {o4}

Client: call graph constructionSite of interest: line 5

z ➝ {o4}

Tian Tan @ Nanjing University

Page 76: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Analysis ScopeWhich parts of program should be analyzed?

76

Whole-program Demand-driven

Compute points-to information for all pointers in the program

Only compute points-to information for the pointers that may affect specific sites of interest (on demand)

Provide information for all possible clients Provide information for specific clients

1 x = new A();2 y = x;3 … 4 z = new T();5 z.bar();

Chosen in this course

Client: call graph constructionSite of interest: line 5

z ➝ {o4}

x ➝ {o1}y ➝ {o1}z ➝ {o4}

Tian Tan @ Nanjing University

Page 77: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Pointer Analysis in This CourseFactor Problem ChoiceHeap abstraction

How to model heap memory?

• Allocation-site• Storeless

Context sensitivity

How to model calling contexts?

• Context-sensitive• Context-insensitive

Flow sensitivity

How to model control flow?

• Flow-sensitive• Flow-insensitive

Analysis scope Which parts of program should be analyzed?

• Whole-program• Demand-driven

77Tian Tan @ Nanjing University

Page 78: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Contents

78Tian Tan @ Nanjing University

1. Motivation2. Introduction to Pointer Analysis3. Key Factors of Pointer Analysis4. Concerned Statements

Page 79: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

What Do We Analyze?• Modern languages typically have many kinds of statements

• if-else• switch-case• for/while/do-while• break/continue• …

79Tian Tan @ Nanjing University

Page 80: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

What Do We Analyze?• Modern languages typically have many kinds of statements

• if-else• switch-case• for/while/do-while• break/continue• …

• We only focus on pointer-affecting statements

80

Do not directly affect pointers Ignored in pointer analysis

Tian Tan @ Nanjing University

Page 81: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Pointers in Java

• Local variable: x

• Static field: C.f

• Instance field: x.f

• Array element: array[i]

Tian Tan @ Nanjing University 81

Page 82: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Pointers in Java

• Local variable: x

• Static field: C.f

• Instance field: x.f

• Array element: array[i]

Tian Tan @ Nanjing University 82

Page 83: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Pointers in Java

• Local variable: x

• Static field: C.f

• Instance field: x.f

• Array element: array[i]

Tian Tan @ Nanjing University 83

Sometimes referred as global variable

Page 84: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Pointers in Java

• Local variable: x

• Static field: C.f

• Instance field: x.f

• Array element: array[i]

Tian Tan @ Nanjing University 84

Modeled as an object (pointed by x) with a field f

Page 85: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Pointers in Java

• Local variable: x

• Static field: C.f

• Instance field: x.f

• Array element: array[i]

Tian Tan @ Nanjing University 85

Ignore indexes. Modeled as an object (pointed by array)

with a single field, say arr, which may point to any value

stored in array

array = new String[10];array[0] = "x";array[1] = "y";s = array[0];

array = new String[];array.arr = "x";array.arr = "y";s = array.arr;

Real code Perspective of pointer analysis

Page 86: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Pointers in Java

• Local variable: x

• Static field: C.f

• Instance field: x.f

• Array element: array[i]

Tian Tan @ Nanjing University 86

Page 87: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Pointer-Affecting Statements

Tian Tan @ Nanjing University 87

New x = new T()

Assign x = y

Store x.f = y

Load y = x.f

Call r = x.k(a, …)

Page 88: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Pointer-Affecting Statements

Tian Tan @ Nanjing University 88

New x = new T()

Assign x = y

Store x.f = y

Load y = x.f

Call r = x.k(a, …)

x.f.g.h = y;

t1 = x.ft2 = t1.gt2.h = y;

Complex memory-accesses will be converted to three-address code by

introducing temporary variables

Page 89: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Pointer-Affecting Statements

Tian Tan @ Nanjing University 89

New x = new T()

Assign x = y

Store x.f = y

Load y = x.f

Call r = x.k(a, …)

• Static call C.foo()

• Special call super.foo()/x.<init>()/this.privateFoo()

• Virtual call x.foo()

Page 90: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

• Static call C.foo()

• Special call super.foo()/x.<init>()/this.privateFoo()

• Virtual call x.foo()

Pointer-Affecting Statements

Tian Tan @ Nanjing University 90

New x = new T()

Assign x = y

Store x.f = y

Load y = x.f

Call r = x.k(a, …)

focus

Page 91: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

The X You Need To Understand in This Lecture

• What is pointer analysis?

• Understand the key factors of pointer analysis

• Understand what we analyze in pointer analysis

Tian Tan @ Nanjing University

Page 92: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Nanjing University

Tian Tan

2020

Pointer Analysis

Static Program Analysis

Foundations (I)

Page 93: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Contents

1. Pointer Analysis: Rules2. How to Implement Pointer Analysis3. Pointer Analysis: Algorithms4. Pointer Analysis with Method Calls

93Tian Tan @ Nanjing University

Page 94: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Contents

1. Pointer Analysis: Rules2. How to Implement Pointer Analysis3. Pointer Analysis: Algorithms4. Pointer Analysis with Method Calls

94Tian Tan @ Nanjing University

Page 95: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Pointer-Affecting Statements

Tian Tan @ Nanjing University 95

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 inpointer analysis with method calls

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

Page 96: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Domain and Notations

96

Variables: x, y ∈ VFields: f, g ∈ FObjects: oi, oj ∈ OInstance fields: oi.f, oj.g ∈ O × FPointers: 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 97: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Rules

Kind Statement Rule

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

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

Store x.f = y 𝑜𝑜𝑖𝑖 ∈ 𝑝𝑝𝑝𝑝 𝑥𝑥 , 𝑜𝑜𝑗𝑗 ∈ 𝑝𝑝𝑝𝑝 𝑦𝑦𝑜𝑜𝑗𝑗 ∈ 𝑝𝑝𝑝𝑝(𝑜𝑜𝑖𝑖 . 𝑓𝑓)

Load y = x.f 𝑜𝑜𝑖𝑖 ∈ 𝑝𝑝𝑝𝑝 𝑥𝑥 , 𝑜𝑜𝑗𝑗 ∈ 𝑝𝑝𝑝𝑝 𝑜𝑜𝑖𝑖 . 𝑓𝑓𝑜𝑜𝑗𝑗 ∈ 𝑝𝑝𝑝𝑝(𝑦𝑦)

97Tian Tan @ Nanjing University

Page 98: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Rules

Kind Statement Rule

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

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

Store x.f = y 𝑜𝑜𝑖𝑖 ∈ 𝑝𝑝𝑝𝑝 𝑥𝑥 , 𝑜𝑜𝑗𝑗 ∈ 𝑝𝑝𝑝𝑝 𝑦𝑦𝑜𝑜𝑗𝑗 ∈ 𝑝𝑝𝑝𝑝(𝑜𝑜𝑖𝑖 . 𝑓𝑓)

Load y = x.f 𝑜𝑜𝑖𝑖 ∈ 𝑝𝑝𝑝𝑝 𝑥𝑥 , 𝑜𝑜𝑗𝑗 ∈ 𝑝𝑝𝑝𝑝 𝑜𝑜𝑖𝑖 . 𝑓𝑓𝑜𝑜𝑗𝑗 ∈ 𝑝𝑝𝑝𝑝(𝑦𝑦)

98Tian Tan @ Nanjing University

← premises

← conclusion

← unconditional

Page 99: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Rule: New

99

i: x = new T()

𝑜𝑜𝑖𝑖Conclusion

𝑜𝑜𝑖𝑖 ∈ 𝑝𝑝𝑝𝑝(𝑥𝑥)

Tian Tan @ Nanjing University

Page 100: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Rule: Assign

100

𝑜𝑜𝑖𝑖Conclusion

x = y

Premises

𝑜𝑜𝑖𝑖 ∈ 𝑝𝑝𝑝𝑝(𝑦𝑦)𝑜𝑜𝑖𝑖 ∈ 𝑝𝑝𝑝𝑝(𝑥𝑥)

Tian Tan @ Nanjing University

Page 101: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Rule: Store

101

𝑜𝑜𝑖𝑖

x.f = y

𝑜𝑜𝑖𝑖 ∈ 𝑝𝑝𝑝𝑝 𝑥𝑥 , 𝑜𝑜𝑗𝑗 ∈ 𝑝𝑝𝑝𝑝 𝑦𝑦𝑜𝑜𝑗𝑗 ∈ 𝑝𝑝𝑝𝑝(𝑜𝑜𝑖𝑖 .𝑓𝑓)

𝑜𝑜𝑗𝑗𝑓𝑓

Tian Tan @ Nanjing University

ConclusionPremises

Page 102: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Rule: Load

102

𝑜𝑜𝑗𝑗

y = x.f

𝑜𝑜𝑖𝑖 ∈ 𝑝𝑝𝑝𝑝 𝑥𝑥 , 𝑜𝑜𝑗𝑗 ∈ 𝑝𝑝𝑝𝑝 𝑜𝑜𝑖𝑖 .𝑓𝑓𝑜𝑜𝑗𝑗 ∈ 𝑝𝑝𝑝𝑝(𝑦𝑦)

𝑜𝑜𝑖𝑖𝑓𝑓

Tian Tan @ Nanjing University

ConclusionPremises

Page 103: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

RulesKind Rule Illustration

New 𝑜𝑜𝑖𝑖 ∈ 𝑝𝑝𝑝𝑝 𝑥𝑥

Assign 𝑜𝑜𝑖𝑖 ∈ 𝑝𝑝𝑝𝑝(𝑦𝑦)𝑜𝑜𝑖𝑖 ∈ 𝑝𝑝𝑝𝑝(𝑥𝑥)

Store 𝑜𝑜𝑖𝑖 ∈ 𝑝𝑝𝑝𝑝 𝑥𝑥 , 𝑜𝑜𝑗𝑗 ∈ 𝑝𝑝𝑝𝑝 𝑦𝑦𝑜𝑜𝑗𝑗 ∈ 𝑝𝑝𝑝𝑝(𝑜𝑜𝑖𝑖 . 𝑓𝑓)

Load 𝑜𝑜𝑖𝑖 ∈ 𝑝𝑝𝑝𝑝 𝑥𝑥 , 𝑜𝑜𝑗𝑗 ∈ 𝑝𝑝𝑝𝑝 𝑜𝑜𝑖𝑖 . 𝑓𝑓𝑜𝑜𝑗𝑗 ∈ 𝑝𝑝𝑝𝑝(𝑦𝑦)

103

ConclusionPremises

𝑜𝑜𝑗𝑗

y = x.f

𝑜𝑜𝑖𝑖𝑓𝑓

𝑜𝑜𝑖𝑖

x.f = y

𝑜𝑜𝑗𝑗𝑓𝑓

x = y

𝑜𝑜𝑖𝑖

i: x = new T()

𝑜𝑜𝑖𝑖

Tian Tan @ Nanjing University

Page 104: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Contents

104

1. Pointer Analysis: Rules2. How to Implement Pointer Analysis3. Pointer Analysis: Algorithms4. Pointer Analysis with Method Calls

Tian Tan @ Nanjing University

Page 105: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Our Pointer Analysis Algorithms

• A complete whole-program pointer analysis

• Carefully designed for understandability

• Easy to follow and implement

105Tian Tan @ Nanjing University

Page 106: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

How to Implement Pointer Analysis?

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

106

Kind Statement Rule

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

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

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

𝑜𝑜𝑗𝑗 ∈ 𝑝𝑝𝑝𝑝(𝒐𝒐𝒊𝒊.𝒇𝒇)

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

𝑜𝑜𝑗𝑗 ∈ 𝑝𝑝𝑝𝑝(𝒚𝒚)

Tian Tan @ Nanjing University

Page 107: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

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)

107

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 108: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

How to Implement Pointer Analysis?

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

108

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 109: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

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)

109

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 110: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

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.

110Tian Tan @ Nanjing University

Page 111: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

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 × PointerAn edge 𝑥𝑥 → 𝑦𝑦 means that the objects pointed by pointer 𝑥𝑥may flow to (and also be pointed to by) pointer 𝑦𝑦

111Tian Tan @ Nanjing University

Page 112: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Pointer Flow Graph: Edges

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

112

Kind Statement Rule

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

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

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

𝑜𝑜𝑗𝑗 ∈ 𝑝𝑝𝑝𝑝(𝒐𝒐𝒊𝒊.𝒇𝒇)

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

𝑜𝑜𝑗𝑗 ∈ 𝑝𝑝𝑝𝑝(𝒚𝒚)

Tian Tan @ Nanjing University

Page 113: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Pointer Flow Graph: Edges

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

113

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 114: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Pointer Flow Graph: An Example

114

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 115: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Pointer Flow Graph: An Example

115

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 116: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Pointer Flow Graph: An Example

116

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 117: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Pointer Flow Graph: An Example

117

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 118: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Pointer Flow Graph: An Example

118

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 119: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Pointer Flow Graph: An Example

119

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 120: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

③④

Pointer Flow Graph: An Example

120

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 121: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Pointer Flow Graph: An Example

121

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 122: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Pointer Flow Graph: An Example

122

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 123: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Pointer Flow Graph: An Example

123

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 124: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Pointer Flow Graph: An Example

124

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 125: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Pointer Flow Graph: An Example

125

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 126: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Pointer Flow Graph: An Example

126

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 127: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Pointer Flow Graph: An Example

127

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 128: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Implementing Pointer Analysis

128

1. Build pointer flow graph (PFG)

2. Propagate points-to information on PFG

Tian Tan @ Nanjing University

Page 129: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

Implementing Pointer Analysis

129

1. Build pointer flow graph (PFG)

2. Propagate points-to information on PFG

Mutually dependent

Tian Tan @ Nanjing University

Page 130: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

③④

Implementing Pointer Analysis

130

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 131: Pointer Analysis - Bitbucket · 2021. 1. 4. · Pointer Analysis • A fundamental static analysis • Computes which memory locations a pointer can point to • For object-oriented

③④

⑤ PFG is dynamically updated during pointer analysis

Implementing Pointer Analysis

131

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