Introduction to Program Analysis Uday Khedker - - PowerPoint PPT Presentation

introduction to program analysis
SMART_READER_LITE
LIVE PREVIEW

Introduction to Program Analysis Uday Khedker - - PowerPoint PPT Presentation

Introduction to Program Analysis Uday Khedker (www.cse.iitb.ac.in/uday) Department of Computer Science and Engineering, Indian Institute of Technology, Bombay Dec 2017 WSSE Pune Intro to PA: Outline 1/1 Introduction to Program Analysis:


slide-1
SLIDE 1

Introduction to Program Analysis

Uday Khedker

(www.cse.iitb.ac.in/˜uday) Department of Computer Science and Engineering, Indian Institute of Technology, Bombay

Dec 2017

slide-2
SLIDE 2

WSSE Pune Intro to PA: Outline 1/1

Introduction to Program Analysis: An Outline

  • Motivating example of improving garbage collection
  • Soundness and precision of program analysis

Dec 2017 IIT Bombay

slide-3
SLIDE 3

WSSE Pune Intro to PA: Outline 2/1

What is Program Analysis?

Discovering information about a given program

Dec 2017 IIT Bombay

slide-4
SLIDE 4

WSSE Pune Intro to PA: Outline 2/1

What is Program Analysis?

Discovering information about a given program

  • Representing the dynamic behaviour of the program

Dec 2017 IIT Bombay

slide-5
SLIDE 5

WSSE Pune Intro to PA: Outline 2/1

What is Program Analysis?

Discovering information about a given program

  • Representing the dynamic behaviour of the program
  • Most often obtained without executing the program

◮ Static analysis Vs. Dynamic Analysis ◮ Example of loop tiling for parallelization

Dec 2017 IIT Bombay

slide-6
SLIDE 6

WSSE Pune Intro to PA: Outline 2/1

What is Program Analysis?

Discovering information about a given program

  • Representing the dynamic behaviour of the program
  • Most often obtained without executing the program

◮ Static analysis Vs. Dynamic Analysis ◮ Example of loop tiling for parallelization

  • Must represent all execution instances of the program

Dec 2017 IIT Bombay

slide-7
SLIDE 7

WSSE Pune Intro to PA: Outline 3/1

Why is it Useful?

  • Code optimization

◮ Improving time, space, energy, or power efficiency ◮ Compilation for special architecture (eg. multi-core)

Dec 2017 IIT Bombay

slide-8
SLIDE 8

WSSE Pune Intro to PA: Outline 3/1

Why is it Useful?

  • Code optimization

◮ Improving time, space, energy, or power efficiency ◮ Compilation for special architecture (eg. multi-core)

  • Verification and validation

Giving guarantees such as: The program will

◮ never divide a number by zero ◮ never dereference a NULL pointer ◮ close all opened files, all opened socket connections ◮ not allow buffer overflow security violation

Dec 2017 IIT Bombay

slide-9
SLIDE 9

WSSE Pune Intro to PA: Outline 3/1

Why is it Useful?

  • Code optimization

◮ Improving time, space, energy, or power efficiency ◮ Compilation for special architecture (eg. multi-core)

  • Verification and validation

Giving guarantees such as: The program will

◮ never divide a number by zero ◮ never dereference a NULL pointer ◮ close all opened files, all opened socket connections ◮ not allow buffer overflow security violation

  • Software engineering

◮ Maintenance, bug fixes, enhancements, migration ◮ Example: Y2K problem

Dec 2017 IIT Bombay

slide-10
SLIDE 10

WSSE Pune Intro to PA: Outline 3/1

Why is it Useful?

  • Code optimization

◮ Improving time, space, energy, or power efficiency ◮ Compilation for special architecture (eg. multi-core)

  • Verification and validation

Giving guarantees such as: The program will

◮ never divide a number by zero ◮ never dereference a NULL pointer ◮ close all opened files, all opened socket connections ◮ not allow buffer overflow security violation

  • Software engineering

◮ Maintenance, bug fixes, enhancements, migration ◮ Example: Y2K problem

  • Reverse engineering

To understand the program

Dec 2017 IIT Bombay

slide-11
SLIDE 11

Part 1

Program Analysis for Improving Garbage Collection

slide-12
SLIDE 12

WSSE Pune Intro to PA: Improving Garbage Collection 4/1

Garbage Collection ≡ Automatic Deallocation

  • Retain active data structure.

Deallocate inactive data structure.

  • What is an Active Data Structure?

Dec 2017 IIT Bombay

slide-13
SLIDE 13

WSSE Pune Intro to PA: Improving Garbage Collection 4/1

Garbage Collection ≡ Automatic Deallocation

  • Retain active data structure.

Deallocate inactive data structure.

  • What is an Active Data Structure?

If an object does not have an access path, (i.e. it is unreachable) then its memory can be reclaimed.

Dec 2017 IIT Bombay

slide-14
SLIDE 14

WSSE Pune Intro to PA: Improving Garbage Collection 4/1

Garbage Collection ≡ Automatic Deallocation

  • Retain active data structure.

Deallocate inactive data structure.

  • What is an Active Data Structure?

If an object does not have an access path, (i.e. it is unreachable) then its memory can be reclaimed. What if an object has an access path, but is not accessed after the given program point?

Dec 2017 IIT Bombay

slide-15
SLIDE 15

WSSE Pune Intro to PA: Improving Garbage Collection 5/1

What is Garbage?

1 w = x // x points to ma 2 if (x.data < max) 3 x = x.rptr 4 y = x.lptr 5 z = New class of z 6 y = y.lptr 7 z.sum = x.data + y.data

We use Java style statements for convenience Read “x.lptr” as “x→lptr Heap Stack x z w y a p q b i c f g h d e j m k l n

  • Garbage

Garbage

lptr r p t r r p t r l p t r rptr lptr rptr lptr l p t r rptr rptr lptr rptr l p t r

Dec 2017 IIT Bombay

slide-16
SLIDE 16

WSSE Pune Intro to PA: Improving Garbage Collection 5/1

What is Garbage?

1 w = x // x points to ma 2 if (x.data < max) 3 x = x.rptr 4 y = x.lptr 5 z = New class of z 6 y = y.lptr 7 z.sum = x.data + y.data

The blue nodes will be used after statement 4 Heap Stack x z w y a p q b i c f g h d e j m k l n

  • Garbage

Garbage

lptr r p t r r p t r l p t r rptr lptr rptr lptr l p t r rptr rptr lptr rptr l p t r

(x.data < max)

False a i m x y

Dec 2017 IIT Bombay

slide-17
SLIDE 17

WSSE Pune Intro to PA: Improving Garbage Collection 5/1

What is Garbage?

1 w = x // x points to ma 2 if (x.data < max) 3 x = x.rptr 4 y = x.lptr 5 z = New class of z 6 y = y.lptr 7 z.sum = x.data + y.data

The blue nodes will be used after statement 4 Heap Stack x z w y a p q b i c f g h d e j m k l n

  • Garbage

Garbage

lptr r p t r r p t r l p t r rptr lptr rptr lptr l p t r rptr rptr lptr rptr l p t r

(x.data < max)

True b f h x y

Dec 2017 IIT Bombay

slide-18
SLIDE 18

WSSE Pune Intro to PA: Improving Garbage Collection 5/1

What is Garbage?

1 w = x // x points to ma 2 if (x.data < max) 3 x = x.rptr 4 y = x.lptr 5 z = New class of z 6 y = y.lptr 7 z.sum = x.data + y.data

The blue nodes will be used after statement 4 Heap Stack x z w y a p q b i c f g h d e j m k l n

  • Garbage

Garbage

lptr r p t r r p t r l p t r rptr lptr rptr lptr l p t r rptr rptr lptr rptr l p t r

a i m b f h x y All white nodes are unused and should be considered garbage

Dec 2017 IIT Bombay

slide-19
SLIDE 19

WSSE Pune Intro to PA: Improving Garbage Collection 6/1

Cedar Mesa Folk Wisdom

Make the unused memory unreachable by setting references to NULL. (GC FAQ: http://www.iecc.com/gclist/GC-harder.html) Heap Stack x z w y a p q b i c f g h d e j m k l n

  • lptr

rptr l p t r r p t r lptr rptr lptr lptr rptr lptr rptr l p t r

a i m b f h

X X

Dec 2017 IIT Bombay

slide-20
SLIDE 20

WSSE Pune Intro to PA: Improving Garbage Collection 6/1

Cedar Mesa Folk Wisdom

Make the unused memory unreachable by setting references to NULL. (GC FAQ: http://www.iecc.com/gclist/GC-harder.html) Heap Stack x z w y a p q b i c f g h d e j m k l n

  • lptr

rptr l p t r r p t r lptr rptr lptr lptr rptr lptr rptr l p t r

a i m b f h

Dec 2017 IIT Bombay

slide-21
SLIDE 21

WSSE Pune Intro to PA: Improving Garbage Collection 7/1

Liveness of Stack Data: An Informal Introduction

1 w = x // x points to ma 2 while (x.data < max) 3 x = x.rptr 4 y = x.lptr 5 z = New class of z 6 y = y.lptr 7 z.sum = x.data + y.data

We use Java style statements for convenience Read “x.lptr” as “x→lptr if changed to while Stack Heap w x y z

Dec 2017 IIT Bombay

slide-22
SLIDE 22

WSSE Pune Intro to PA: Improving Garbage Collection 7/1

Liveness of Stack Data: An Informal Introduction

1 w = x // x points to ma 2 while (x.data < max) 3 x = x.rptr 4 y = x.lptr 5 z = New class of z 6 y = y.lptr 7 z.sum = x.data + y.data

Stack Heap w x y z What is the meaning of the use

  • f data?

Dec 2017 IIT Bombay

slide-23
SLIDE 23

WSSE Pune Intro to PA: Improving Garbage Collection 7/1

Liveness of Stack Data: An Informal Introduction

1 w = x // x points to ma 2 while (x.data < max) 3 x = x.rptr 4 y = x.lptr 5 z = New class of z 6 y = y.lptr 7 z.sum = x.data + y.data

Stack Heap w x y z lptr rptr data rptr rptr What is the meaning of the use

  • f data?

Dec 2017 IIT Bombay

slide-24
SLIDE 24

WSSE Pune Intro to PA: Improving Garbage Collection 7/1

Liveness of Stack Data: An Informal Introduction

1 w = x // x points to ma 2 while (x.data < max) 3 x = x.rptr 4 y = x.lptr 5 z = New class of z 6 y = y.lptr 7 z.sum = x.data + y.data

Stack Heap w x y z lptr rptr data rptr rptr What is the meaning of the use

  • f data?

Accessing the location and reading its contents

Dec 2017 IIT Bombay

slide-25
SLIDE 25

WSSE Pune Intro to PA: Improving Garbage Collection 7/1

Liveness of Stack Data: An Informal Introduction

1 w = x // x points to ma 2 while (x.data < max) 3 x = x.rptr 4 y = x.lptr 5 z = New class of z 6 y = y.lptr 7 z.sum = x.data + y.data

Stack Heap w x y z lptr rptr data rptr rptr Accessing the location and reading its contents Reading x (Stack data)

Dec 2017 IIT Bombay

slide-26
SLIDE 26

WSSE Pune Intro to PA: Improving Garbage Collection 7/1

Liveness of Stack Data: An Informal Introduction

1 w = x // x points to ma 2 while (x.data < max) 3 x = x.rptr 4 y = x.lptr 5 z = New class of z 6 y = y.lptr 7 z.sum = x.data + y.data

Stack Heap w x y z lptr rptr data rptr rptr Accessing the location and reading its contents Reading x.data (Heap data)

Dec 2017 IIT Bombay

slide-27
SLIDE 27

WSSE Pune Intro to PA: Improving Garbage Collection 7/1

Liveness of Stack Data: An Informal Introduction

1 w = x // x points to ma 2 while (x.data < max) 3 x = x.rptr 4 y = x.lptr 5 z = New class of z 6 y = y.lptr 7 z.sum = x.data + y.data

Stack Heap w x y z lptr rptr data rptr rptr Accessing the location and reading its contents Reading x.rptr (Heap data)

Dec 2017 IIT Bombay

slide-28
SLIDE 28

WSSE Pune Intro to PA: Improving Garbage Collection 8/1

Liveness of Stack Data: An Informal Introduction

w = x while (x.data < max) x = x.rptr y = x.lptr z = New class of z y = y.lptr z.sum = x.data + y.data w x y z No variable is used beyond this program point

Dec 2017 IIT Bombay

slide-29
SLIDE 29

WSSE Pune Intro to PA: Improving Garbage Collection 8/1

Liveness of Stack Data: An Informal Introduction

w = x while (x.data < max) x = x.rptr y = x.lptr z = New class of z y = y.lptr z.sum = x.data + y.data w x y z Live Dead Current values of x, y, and z are used beyond this program point

Dec 2017 IIT Bombay

slide-30
SLIDE 30

WSSE Pune Intro to PA: Improving Garbage Collection 8/1

Liveness of Stack Data: An Informal Introduction

w = x while (x.data < max) x = x.rptr y = x.lptr z = New class of z y = y.lptr z.sum = x.data + y.data w x y z

  • Current values of x, y, and z are

used beyond this program point

  • The value of y is different before

and after the assignment to y

Dec 2017 IIT Bombay

slide-31
SLIDE 31

WSSE Pune Intro to PA: Improving Garbage Collection 8/1

Liveness of Stack Data: An Informal Introduction

w = x while (x.data < max) x = x.rptr y = x.lptr z = New class of z y = y.lptr z.sum = x.data + y.data w x y z

  • The current values of x and y are

used beyond this program point

  • The current value of z is not used

beyond this program point

Dec 2017 IIT Bombay

slide-32
SLIDE 32

WSSE Pune Intro to PA: Improving Garbage Collection 8/1

Liveness of Stack Data: An Informal Introduction

w = x while (x.data < max) x = x.rptr y = x.lptr z = New class of z y = y.lptr z.sum = x.data + y.data w x y z

  • The current values of x is used

beyond this program point

  • Current values of y and z are not

used beyond this program point

Dec 2017 IIT Bombay

slide-33
SLIDE 33

WSSE Pune Intro to PA: Improving Garbage Collection 8/1

Liveness of Stack Data: An Informal Introduction

w = x while (x.data < max) x = x.rptr y = x.lptr z = New class of z y = y.lptr z.sum = x.data + y.data w x y z

  • Nothing is known as of now
  • Some information will be available

in the next iteration point

Dec 2017 IIT Bombay

slide-34
SLIDE 34

WSSE Pune Intro to PA: Improving Garbage Collection 8/1

Liveness of Stack Data: An Informal Introduction

w = x while (x.data < max) x = x.rptr y = x.lptr z = New class of z y = y.lptr z.sum = x.data + y.data w x y z

  • Current value of x is used beyond

this program point

  • However its value is different before

and after the assignment

Dec 2017 IIT Bombay

slide-35
SLIDE 35

WSSE Pune Intro to PA: Improving Garbage Collection 8/1

Liveness of Stack Data: An Informal Introduction

w = x while (x.data < max) x = x.rptr y = x.lptr z = New class of z y = y.lptr z.sum = x.data + y.data w x y z

  • Current value of x is used beyond

this program point

  • There are two control flow paths

beyond this program point

Dec 2017 IIT Bombay

slide-36
SLIDE 36

WSSE Pune Intro to PA: Improving Garbage Collection 8/1

Liveness of Stack Data: An Informal Introduction

w = x while (x.data < max) x = x.rptr y = x.lptr z = New class of z y = y.lptr z.sum = x.data + y.data w x y z Current value of x is used be- yond this program point

Dec 2017 IIT Bombay

slide-37
SLIDE 37

WSSE Pune Intro to PA: Improving Garbage Collection 8/1

Liveness of Stack Data: An Informal Introduction

w = x while (x.data < max) x = x.rptr y = x.lptr z = New class of z y = y.lptr z.sum = x.data + y.data w x y z Current value of x is used be- yond this program point

Dec 2017 IIT Bombay

slide-38
SLIDE 38

WSSE Pune Intro to PA: Improving Garbage Collection 8/1

Liveness of Stack Data: An Informal Introduction

w = x while (x.data < max) x = x.rptr y = x.lptr z = New class of z y = y.lptr z.sum = x.data + y.data w x y z w x y z Live Dead w x y z w x y z w x y z w x y z w x y z w x y z w x y z w x y z End of iteration #1

Dec 2017 IIT Bombay

slide-39
SLIDE 39

WSSE Pune Intro to PA: Improving Garbage Collection 8/1

Liveness of Stack Data: An Informal Introduction

w = x while (x.data < max) x = x.rptr y = x.lptr z = New class of z y = y.lptr z.sum = x.data + y.data w x y z w x y z Live Dead w x y z w x y z w x y z w x y z w x y z w x y z w x y z w x y z End of iteration #2

Dec 2017 IIT Bombay

slide-40
SLIDE 40

WSSE Pune Intro to PA: Improving Garbage Collection 9/1

Applying Cedar Mesa Folk Wisdom to Heap Data Liveness Analysis of Heap Data

If the while loop is not executed even once.

1 w = x // x points to ma 2 while (x.data < max) 3 x = x.rptr 4 y = x.lptr 5 z = New class of z 6 y = y.lptr 7 z.sum = x.data + y.data

Heap Stack x z w y a p q b i c f g h d e j m k l n

  • lptr

rptr rptr l p t r r p t r lptr rptr lptr l p t r rptr rptr lptr rptr l p t r

a i m

Dec 2017 IIT Bombay

slide-41
SLIDE 41

WSSE Pune Intro to PA: Improving Garbage Collection 9/1

Applying Cedar Mesa Folk Wisdom to Heap Data Liveness Analysis of Heap Data

If the while loop is executed once.

1 w = x // x points to ma 2 while (x.data < max) 3 x = x.rptr 4 y = x.lptr 5 z = New class of z 6 y = y.lptr 7 z.sum = x.data + y.data

Heap Stack x z w y a p q b i c f g h d e j m k l n

  • lptr

rptr rptr l p t r r p t r lptr rptr lptr l p t r rptr rptr lptr rptr l p t r

b f h

Dec 2017 IIT Bombay

slide-42
SLIDE 42

WSSE Pune Intro to PA: Improving Garbage Collection 9/1

Applying Cedar Mesa Folk Wisdom to Heap Data Liveness Analysis of Heap Data

If the while loop is executed twice.

1 w = x // x points to ma 2 while (x.data < max) 3 x = x.rptr 4 y = x.lptr 5 z = New class of z 6 y = y.lptr 7 z.sum = x.data + y.data

Heap Stack x z w y a p q b i c f g h d e j m k l n

  • lptr

rptr rptr l p t r r p t r lptr rptr lptr l p t r rptr rptr lptr rptr l p t r

c e

Dec 2017 IIT Bombay

slide-43
SLIDE 43

WSSE Pune Intro to PA: Improving Garbage Collection 10/1

The Moral of the Story

  • Mappings between access expressions and l-values keep changing
  • This is a rule for heap data

For stack and static data, it is an exception!

  • Static analysis of programs has made significant progress for stack and

static data. What about heap data?

◮ Given two access expressions at a program point, do they have the

same l-value?

◮ Given the same access expression at two program points, does it have

the same l-value?

Dec 2017 IIT Bombay

slide-44
SLIDE 44

WSSE Pune Intro to PA: Improving Garbage Collection 11/1

Our Solution

y = z = null 1 w = x w = null 2 while (x.data < max) { x.lptr = null 3 x = x.rptr } x.rptr = x.lptr.rptr = null x.lptr.lptr.lptr = null x.lptr.lptr.rptr = null 4 y = x.lptr x.lptr = y.rptr = null y.lptr.lptr = y.lptr.rptr = null 5 z = New class of z z.lptr = z.rptr = null 6 y = y.lptr y.lptr = y.rptr = null 7 z.sum = x.data + y.data x = y = z = null

Dec 2017 IIT Bombay

slide-45
SLIDE 45

WSSE Pune Intro to PA: Improving Garbage Collection 12/1

Our Solution

y = z = null 1 w = x w = null 2 while (x.data < max) { x.lptr = null 3 x = x.rptr } x.rptr = x.lptr.rptr = null x.lptr.lptr.lptr = null x.lptr.lptr.rptr = null 4 y = x.lptr x.lptr = y.rptr = null y.lptr.lptr = y.lptr.rptr = null 5 z = New class of z z.lptr = z.rptr = null 6 y = y.lptr y.lptr = y.rptr = null 7 z.sum = x.data + y.data x = y = z = null

Heap Stack x z w y a p q b i c f g h d e j m k l n

  • r

p t r lptr

While loop is not executed even once

a i m

lptr rptr lptr l p t r rptr lptr rptr rptr r p t r l p t r r p t r lptr

Dec 2017 IIT Bombay

slide-46
SLIDE 46

WSSE Pune Intro to PA: Improving Garbage Collection 12/1

Our Solution

y = z = null 1 w = x w = null 2 while (x.data < max) { x.lptr = null 3 x = x.rptr } x.rptr = x.lptr.rptr = null x.lptr.lptr.lptr = null x.lptr.lptr.rptr = null 4 y = x.lptr x.lptr = y.rptr = null y.lptr.lptr = y.lptr.rptr = null 5 z = New class of z z.lptr = z.rptr = null 6 y = y.lptr y.lptr = y.rptr = null 7 z.sum = x.data + y.data x = y = z = null

Heap Stack x z w y a p q b i c f g h d e j m k l n

  • r

p t r lptr

While loop is not executed even once

a i m

lptr rptr lptr l p t r rptr lptr rptr rptr r p t r l p t r r p t r lptr

Dec 2017 IIT Bombay

slide-47
SLIDE 47

WSSE Pune Intro to PA: Improving Garbage Collection 12/1

Our Solution

y = z = null 1 w = x w = null 2 while (x.data < max) { x.lptr = null 3 x = x.rptr } x.rptr = x.lptr.rptr = null x.lptr.lptr.lptr = null x.lptr.lptr.rptr = null 4 y = x.lptr x.lptr = y.rptr = null y.lptr.lptr = y.lptr.rptr = null 5 z = New class of z z.lptr = z.rptr = null 6 y = y.lptr y.lptr = y.rptr = null 7 z.sum = x.data + y.data x = y = z = null

Heap Stack x z w y a p q b i c f g h d e j m k l n

  • r

p t r lptr

While loop is not executed even once

a i m

lptr rptr lptr l p t r rptr lptr rptr rptr r p t r l p t r r p t r lptr

Dec 2017 IIT Bombay

slide-48
SLIDE 48

WSSE Pune Intro to PA: Improving Garbage Collection 12/1

Our Solution

y = z = null 1 w = x w = null 2 while (x.data < max) { x.lptr = null 3 x = x.rptr } x.rptr = x.lptr.rptr = null x.lptr.lptr.lptr = null x.lptr.lptr.rptr = null 4 y = x.lptr x.lptr = y.rptr = null y.lptr.lptr = y.lptr.rptr = null 5 z = New class of z z.lptr = z.rptr = null 6 y = y.lptr y.lptr = y.rptr = null 7 z.sum = x.data + y.data x = y = z = null

Heap Stack x z w y a p q b i c f g h d e j m k l n

  • r

p t r lptr

While loop is not executed even once

a i m

lptr rptr lptr l p t r rptr lptr rptr rptr r p t r l p t r r p t r lptr

Dec 2017 IIT Bombay

slide-49
SLIDE 49

WSSE Pune Intro to PA: Improving Garbage Collection 12/1

Our Solution

y = z = null 1 w = x w = null 2 while (x.data < max) { x.lptr = null 3 x = x.rptr } x.rptr = x.lptr.rptr = null x.lptr.lptr.lptr = null x.lptr.lptr.rptr = null 4 y = x.lptr x.lptr = y.rptr = null y.lptr.lptr = y.lptr.rptr = null 5 z = New class of z z.lptr = z.rptr = null 6 y = y.lptr y.lptr = y.rptr = null 7 z.sum = x.data + y.data x = y = z = null

Heap Stack x z w y a p q b i c f g h d e j m k l n

  • r

p t r lptr

While loop is not executed even once

a i m

lptr rptr lptr l p t r rptr lptr rptr rptr r p t r l p t r r p t r lptr

Dec 2017 IIT Bombay

slide-50
SLIDE 50

WSSE Pune Intro to PA: Improving Garbage Collection 12/1

Our Solution

y = z = null 1 w = x w = null 2 while (x.data < max) { x.lptr = null 3 x = x.rptr } x.rptr = x.lptr.rptr = null x.lptr.lptr.lptr = null x.lptr.lptr.rptr = null 4 y = x.lptr x.lptr = y.rptr = null y.lptr.lptr = y.lptr.rptr = null 5 z = New class of z z.lptr = z.rptr = null 6 y = y.lptr y.lptr = y.rptr = null 7 z.sum = x.data + y.data x = y = z = null

Heap Stack x z w y a p q b i c f g h d e j m k l n

  • r

p t r lptr

While loop is not executed even once

a i m

lptr rptr lptr l p t r rptr lptr rptr rptr r p t r l p t r r p t r lptr

Dec 2017 IIT Bombay

slide-51
SLIDE 51

WSSE Pune Intro to PA: Improving Garbage Collection 12/1

Our Solution

y = z = null 1 w = x w = null 2 while (x.data < max) { x.lptr = null 3 x = x.rptr } x.rptr = x.lptr.rptr = null x.lptr.lptr.lptr = null x.lptr.lptr.rptr = null 4 y = x.lptr x.lptr = y.rptr = null y.lptr.lptr = y.lptr.rptr = null 5 z = New class of z z.lptr = z.rptr = null 6 y = y.lptr y.lptr = y.rptr = null 7 z.sum = x.data + y.data x = y = z = null

Heap Stack x z w y a p q b i c f g h d e j m k l n

  • r

p t r lptr

While loop is not executed even once

a i m

lptr rptr lptr l p t r rptr lptr rptr

Dec 2017 IIT Bombay

slide-52
SLIDE 52

WSSE Pune Intro to PA: Improving Garbage Collection 12/1

Our Solution

y = z = null 1 w = x w = null 2 while (x.data < max) { x.lptr = null 3 x = x.rptr } x.rptr = x.lptr.rptr = null x.lptr.lptr.lptr = null x.lptr.lptr.rptr = null 4 y = x.lptr x.lptr = y.rptr = null y.lptr.lptr = y.lptr.rptr = null 5 z = New class of z z.lptr = z.rptr = null 6 y = y.lptr y.lptr = y.rptr = null 7 z.sum = x.data + y.data x = y = z = null

Heap Stack x z w y a p q b i c f g h d e j m k l n

  • r

p t r lptr

While loop is executed once

a i m b f h

lptr rptr rptr l p t r rptr l p t r rptr lptr

Dec 2017 IIT Bombay

slide-53
SLIDE 53

WSSE Pune Intro to PA: Improving Garbage Collection 12/1

Our Solution

y = z = null 1 w = x w = null 2 while (x.data < max) { x.lptr = null 3 x = x.rptr } x.rptr = x.lptr.rptr = null x.lptr.lptr.lptr = null x.lptr.lptr.rptr = null 4 y = x.lptr x.lptr = y.rptr = null y.lptr.lptr = y.lptr.rptr = null 5 z = New class of z z.lptr = z.rptr = null 6 y = y.lptr y.lptr = y.rptr = null 7 z.sum = x.data + y.data x = y = z = null

Heap Stack x z w y a p q b i c f g h d e j m k l n

  • r

p t r lptr

While loop is executed twice

a i m b f h c e

l p t r rptr rptr l p t r rptr l p t r rptr rptr

Dec 2017 IIT Bombay

slide-54
SLIDE 54

WSSE Pune Intro to PA: Improving Garbage Collection 13/1

Some Observations

y = z = null 1 w = x w = null 2 while (x.data < max) { x.lptr = null 3 x = x.rptr } x.rptr = x.lptr.rptr = null x.lptr.lptr.lptr = null x.lptr.lptr.rptr = null 4 y = x.lptr x.lptr = y.rptr = null y.lptr.lptr = y.lptr.rptr = null 5 z = New class of z z.lptr = z.rptr = null 6 y = y.lptr y.lptr = y.rptr = null 7 z.sum = x.data + y.data x = y = z = null

Heap Stack x z w y a p q b i c f g h d e j m k l n

  • r

p t r lptr

a i m

lptr r p t r lptr l p t r rptr lptr rptr rptr r p t r l p t r rptr

Node i is live but link a → i is nullified

Dec 2017 IIT Bombay

slide-55
SLIDE 55

WSSE Pune Intro to PA: Improving Garbage Collection 13/1

Some Observations

y = z = null 1 w = x w = null 2 while (x.data < max) { x.lptr = null 3 x = x.rptr } x.rptr = x.lptr.rptr = null x.lptr.lptr.lptr = null x.lptr.lptr.rptr = null 4 y = x.lptr x.lptr = y.rptr = null y.lptr.lptr = y.lptr.rptr = null 5 z = New class of z z.lptr = z.rptr = null 6 y = y.lptr y.lptr = y.rptr = null 7 z.sum = x.data + y.data x = y = z = null

Heap Stack x z w y a p q b i c f g h d e j m k l n

  • r

p t r lptr

a i m

lptr r p t r lptr l p t r rptr lptr rptr rptr r p t r l p t r rptr

  • The memory address that x holds when the

execution reaches a given program point is not an invariant of program execution

Dec 2017 IIT Bombay

slide-56
SLIDE 56

WSSE Pune Intro to PA: Improving Garbage Collection 13/1

Some Observations

y = z = null 1 w = x w = null 2 while (x.data < max) { x.lptr = null 3 x = x.rptr } x.rptr = x.lptr.rptr = null x.lptr.lptr.lptr = null x.lptr.lptr.rptr = null 4 y = x.lptr x.lptr = y.rptr = null y.lptr.lptr = y.lptr.rptr = null 5 z = New class of z z.lptr = z.rptr = null 6 y = y.lptr y.lptr = y.rptr = null 7 z.sum = x.data + y.data x = y = z = null

Heap Stack x z w y a p q b i c f g h d e j m k l n

  • r

p t r lptr

a i m

lptr r p t r lptr l p t r rptr lptr rptr rptr r p t r l p t r rptr

  • The memory address that x holds when the

execution reaches a given program point is not an invariant of program execution

  • Whether we dereference lptr out of x or

rptr out of x at a given program point is an invariant of program execution

Dec 2017 IIT Bombay

slide-57
SLIDE 57

WSSE Pune Intro to PA: Improving Garbage Collection 13/1

Some Observations

y = z = null 1 w = x w = null 2 while (x.data < max) { x.lptr = null 3 x = x.rptr } x.rptr = x.lptr.rptr = null x.lptr.lptr.lptr = null x.lptr.lptr.rptr = null 4 y = x.lptr x.lptr = y.rptr = null y.lptr.lptr = y.lptr.rptr = null 5 z = New class of z z.lptr = z.rptr = null 6 y = y.lptr y.lptr = y.rptr = null 7 z.sum = x.data + y.data x = y = z = null

Heap Stack x z w y a p q b i c f g h d e j m k l n

  • r

p t r lptr

a i m

lptr r p t r lptr l p t r rptr lptr rptr rptr r p t r l p t r rptr

  • The memory address that x holds when the

execution reaches a given program point is not an invariant of program execution

  • Whether we dereference lptr out of x or

rptr out of x at a given program point is an invariant of program execution

  • A static analysis can discover only

invariants

Dec 2017 IIT Bombay

slide-58
SLIDE 58

WSSE Pune Intro to PA: Improving Garbage Collection 13/1

Some Observations

y = z = null 1 w = x w = null 2 while (x.data < max) { x.lptr = null 3 x = x.rptr } x.rptr = x.lptr.rptr = null x.lptr.lptr.lptr = null x.lptr.lptr.rptr = null 4 y = x.lptr x.lptr = y.rptr = null y.lptr.lptr = y.lptr.rptr = null 5 z = New class of z z.lptr = z.rptr = null 6 y = y.lptr y.lptr = y.rptr = null 7 z.sum = x.data + y.data x = y = z = null

Heap Stack x z w y a p q b i c f g h d e j m k l n

  • r

p t r lptr

a i m

lptr r p t r lptr l p t r rptr lptr rptr rptr r p t r l p t r rptr

New access expressions are created. Can they cause exceptions?

  • The memory address that x holds when the

execution reaches a given program point is not an invariant of program execution

  • Whether we dereference lptr out of x or

rptr out of x at a given program point is an invariant of program execution

  • A static analysis can discover only some

invariants

Dec 2017 IIT Bombay

slide-59
SLIDE 59

WSSE Pune Intro to PA: Improving Garbage Collection 14/1

The Main Theme of (Static) Program Analysis

Constructing suitable abstractions for sound & precise modelling of runtime behaviour of programs efficiently

Dec 2017 IIT Bombay

slide-60
SLIDE 60

WSSE Pune Intro to PA: Improving Garbage Collection 14/1

The Main Theme of (Static) Program Analysis

Constructing suitable abstractions for sound & precise modelling of runtime behaviour of programs efficiently

Abstract, Bounded, Single Instance Concrete, Unbounded, Infinitely Many Static Dynamic Program Code Program Execution Program Execution Program Execution Program Execution Program Execution Program Execution Summary Information Memory Memory Memory Memory Memory Memory Memory Memory Memory Memory Memory Memory Memory Memory Memory Memory Memory Memory Memory Memory Memory Memory Memory Memory Static Analysis

Dec 2017 IIT Bombay

slide-61
SLIDE 61

Part 2

Soundness and Precision

slide-62
SLIDE 62

WSSE Pune Intro to PA: Soundness and Precision 15/1

Program Representation

  • Three address code statements

◮ Result, operator, operand1, operand2 ◮ Assignments, expressions, conditional jumps ◮ Pointer expressions (including structure accesses)

Features will be introduced as and when needed

  • Control flow graph representation

◮ Nodes represent maximal groups of statements

devoid of any control transfer except fall through

◮ Edges represent control transfers across basic blocks ◮ A unique Start node and a unique End node

Every node reachable from Start, and End reachable from every node

  • Initially only intraprocedural programs

Function calls brought in later

Dec 2017 IIT Bombay

slide-63
SLIDE 63

WSSE Pune Intro to PA: Soundness and Precision 16/1

Motivating Example for Introducing Soundness and Precision

Example Program Control Flow Graph int a; int f(int b) { int c; c = a%2; b = - abs(b); while (b < c) b = b+1; if (b > 0) b = 0; return b; }

Dec 2017 IIT Bombay

slide-64
SLIDE 64

WSSE Pune Intro to PA: Soundness and Precision 16/1

Motivating Example for Introducing Soundness and Precision

Example Program Control Flow Graph int a; int f(int b) { int c; c = a%2; b = - abs(b); while (b < c) b = b+1; if (b > 0) b = 0; return b; } Absolute

Dec 2017 IIT Bombay

slide-65
SLIDE 65

WSSE Pune Intro to PA: Soundness and Precision 16/1

Motivating Example for Introducing Soundness and Precision

Example Program Control Flow Graph int a; int f(int b) { int c; c = a%2; b = - abs(b); while (b < c) b = b+1; if (b > 0) b = 0; return b; } Absolute c = a%2 b = - abs(b) 1 if (b<c) 2 b = b+1 3 if (b>0) 4 b = 0 5 return b 6 T F T F

Dec 2017 IIT Bombay

slide-66
SLIDE 66

WSSE Pune Intro to PA: Soundness and Precision 17/1

Execution Traces for Concrete Semantics (1)

  • States

◮ A data state: Variables → Values ◮ A program state: (Program Point, A data state)

  • Execution traces (or traces, for short)

◮ Valid sequences of program states starting with a given initial state

Dec 2017 IIT Bombay

slide-67
SLIDE 67

WSSE Pune Intro to PA: Soundness and Precision 18/1

Execution Traces for Concrete Semantics (2)

c = a%2 b = - abs(b) 1 if (b<c) 2 b = b+1 3 if (b>0) 4 b = 0 5 return b 6 T F T F

Dec 2017 IIT Bombay

slide-68
SLIDE 68

WSSE Pune Intro to PA: Soundness and Precision 18/1

Execution Traces for Concrete Semantics (2)

c = a%2 b = - abs(b) 1 if (b<c) 2 b = b+1 3 if (b>0) 4 b = 0 5 return b 6 T F T F Trace 1 a b c Entry1, (5, 2, 7) Entry2, (5, −2, 1) Entry3, (5, −2, 1) Entry2, (5, −1, 1) Entry3, (5, −1, 1) Entry2, (5, 0, 1) Entry3, (5, 0, 1) Entry2, (5, 1, 1) Entry4, (5, 1, 1) Entry5, (5, 1, 1) Entry6, (5, 0, 1)

Dec 2017 IIT Bombay

slide-69
SLIDE 69

WSSE Pune Intro to PA: Soundness and Precision 18/1

Execution Traces for Concrete Semantics (2)

c = a%2 b = - abs(b) 1 if (b<c) 2 b = b+1 3 if (b>0) 4 b = 0 5 return b 6 T F T F Trace 1 a b c Entry1, (5, 2, 7) Entry2, (5, −2, 1) Entry3, (5, −2, 1) Entry2, (5, −1, 1) Entry3, (5, −1, 1) Entry2, (5, 0, 1) Entry3, (5, 0, 1) Entry2, (5, 1, 1) Entry4, (5, 1, 1) Entry5, (5, 1, 1) Entry6, (5, 0, 1) Trace 2 a b c Entry1, (−5, −2, 8) Entry2, (−5, −2, −1) Entry3, (−5, −2, −1) Entry2, (−5, −1, −1) Entry4, (−5, −1, −1) Entry6, (−5, −1, −1)

Dec 2017 IIT Bombay

slide-70
SLIDE 70

WSSE Pune Intro to PA: Soundness and Precision 18/1

Execution Traces for Concrete Semantics (2)

c = a%2 b = - abs(b) 1 if (b<c) 2 b = b+1 3 if (b>0) 4 b = 0 5 return b 6 T F T F Trace 1 a b c Entry1, (5, 2, 7) Entry2, (5, −2, 1) Entry3, (5, −2, 1) Entry2, (5, −1, 1) Entry3, (5, −1, 1) Entry2, (5, 0, 1) Entry3, (5, 0, 1) Entry2, (5, 1, 1) Entry4, (5, 1, 1) Entry5, (5, 1, 1) Entry6, (5, 0, 1) Trace 2 a b c Entry1, (−5, −2, 8) Entry2, (−5, −2, −1) Entry3, (−5, −2, −1) Entry2, (−5, −1, −1) Entry4, (−5, −1, −1) Entry6, (−5, −1, −1)

  • A separate trace for each combination of inputs

◮ The number of traces is potentially infinite

  • Program points may repeat in the traces

◮ Traces may be very long ◮ Non-terminating traces: Infinitely long

Dec 2017 IIT Bombay

slide-71
SLIDE 71

WSSE Pune Intro to PA: Soundness and Precision 19/1

Abstract States

A static analysis computes abstract states

  • The values are abstract values and are decided by the analysis
  • An analysis may record values for other program entities such as

expressions, statements, procedures etc.

Dec 2017 IIT Bombay

slide-72
SLIDE 72

WSSE Pune Intro to PA: Soundness and Precision 20/1

Static Analysis Computes Abstractions of Traces (1)

Execution Time Traces

Dec 2017 IIT Bombay

slide-73
SLIDE 73

WSSE Pune Intro to PA: Soundness and Precision 20/1

Static Analysis Computes Abstractions of Traces (1)

Execution Time Traces An Abstraction of Traces

Dec 2017 IIT Bombay

slide-74
SLIDE 74

WSSE Pune Intro to PA: Soundness and Precision 20/1

Static Analysis Computes Abstractions of Traces (1)

Execution Time Traces An Abstraction of Traces

Dec 2017 IIT Bombay

slide-75
SLIDE 75

WSSE Pune Intro to PA: Soundness and Precision 20/1

Static Analysis Computes Abstractions of Traces (1)

Execution Time Traces An Abstraction of Traces For compile time modelling of possible runtime behaviours of a program

  • compute a set of states

that cover all traces

  • associate the sets with

appropriate program points States may be defined in terms

  • f properties derived from values
  • f variables

Dec 2017 IIT Bombay

slide-76
SLIDE 76

WSSE Pune Intro to PA: Soundness and Precision 21/1

Static Analysis Computes Abstractions of Traces (2)

Trace 1 a b c Entry1, (5, 2, 7) Entry2, (5, −2, 1) Entry3, (5, −2, 1) Entry2, (5, −1, 1) Entry3, (5, −1, 1) Entry2, (5, 0, 1) Entry3, (5, 0, 1) Entry2, (5, 1, 1) Entry4, (5, 1, 1) Entry5, (5, 1, 1) Entry6, (5, 0, 1) Trace 2 a b c Entry1, (−5, −2, 8) Entry2, (−5, −2, −1) Entry3, (−5, −2, −1) Entry2, (−5, −1, −1) Entry4, (−5, −1, −1) Entry6, (−5, −1, −1) c = a%2 b = - abs(b) 1 if (b<c) 2 b = b+1 3 if (b>0) 4 b = 0 5 return b 6 T F T F A possible static abstraction using sets

Dec 2017 IIT Bombay

slide-77
SLIDE 77

WSSE Pune Intro to PA: Soundness and Precision 21/1

Static Analysis Computes Abstractions of Traces (2)

Trace 1 a b c Entry1, (5, 2, 7) Entry2, (5, −2, 1) Entry3, (5, −2, 1) Entry2, (5, −1, 1) Entry3, (5, −1, 1) Entry2, (5, 0, 1) Entry3, (5, 0, 1) Entry2, (5, 1, 1) Entry4, (5, 1, 1) Entry5, (5, 1, 1) Entry6, (5, 0, 1) Trace 2 a b c Entry1, (−5, −2, 8) Entry2, (−5, −2, −1) Entry3, (−5, −2, −1) Entry2, (−5, −1, −1) Entry4, (−5, −1, −1) Entry6, (−5, −1, −1) c = a%2 b = - abs(b) 1 if (b<c) 2 b = b+1 3 if (b>0) 4 b = 0 5 return b 6 T F T F {(5, 2, 7), (−5, −2, 8)} A possible static abstraction using sets

Dec 2017 IIT Bombay

slide-78
SLIDE 78

WSSE Pune Intro to PA: Soundness and Precision 21/1

Static Analysis Computes Abstractions of Traces (2)

Trace 1 a b c Entry1, (5, 2, 7) Entry2, (5, −2, 1) Entry3, (5, −2, 1) Entry2, (5, −1, 1) Entry3, (5, −1, 1) Entry2, (5, 0, 1) Entry3, (5, 0, 1) Entry2, (5, 1, 1) Entry4, (5, 1, 1) Entry5, (5, 1, 1) Entry6, (5, 0, 1) Trace 2 a b c Entry1, (−5, −2, 8) Entry2, (−5, −2, −1) Entry3, (−5, −2, −1) Entry2, (−5, −1, −1) Entry4, (−5, −1, −1) Entry6, (−5, −1, −1) c = a%2 b = - abs(b) 1 if (b<c) 2 b = b+1 3 if (b>0) 4 b = 0 5 return b 6 T F T F a={−5, 5}, b={−2, 2}, c={7, 8} A possible static abstraction using sets

Dec 2017 IIT Bombay

slide-79
SLIDE 79

WSSE Pune Intro to PA: Soundness and Precision 21/1

Static Analysis Computes Abstractions of Traces (2)

Trace 1 a b c Entry1, (5, 2, 7) Entry2, (5, −2, 1) Entry3, (5, −2, 1) Entry2, (5, −1, 1) Entry3, (5, −1, 1) Entry2, (5, 0, 1) Entry3, (5, 0, 1) Entry2, (5, 1, 1) Entry4, (5, 1, 1) Entry5, (5, 1, 1) Entry6, (5, 0, 1) Trace 2 a b c Entry1, (−5, −2, 8) Entry2, (−5, −2, −1) Entry3, (−5, −2, −1) Entry2, (−5, −1, −1) Entry4, (−5, −1, −1) Entry6, (−5, −1, −1) c = a%2 b = - abs(b) 1 if (b<c) 2 b = b+1 3 if (b>0) 4 b = 0 5 return b 6 T F T F a={−5, 5}, b={−2, 2}, c={7, 8} b={−2, −1, 0, 1} We only show the values of b Combine the values across all occurrences

  • f a program point

A possible static abstraction using sets

Dec 2017 IIT Bombay

slide-80
SLIDE 80

WSSE Pune Intro to PA: Soundness and Precision 21/1

Static Analysis Computes Abstractions of Traces (2)

Trace 1 a b c Entry1, (5, 2, 7) Entry2, (5, −2, 1) Entry3, (5, −2, 1) Entry2, (5, −1, 1) Entry3, (5, −1, 1) Entry2, (5, 0, 1) Entry3, (5, 0, 1) Entry2, (5, 1, 1) Entry4, (5, 1, 1) Entry5, (5, 1, 1) Entry6, (5, 0, 1) Trace 2 a b c Entry1, (−5, −2, 8) Entry2, (−5, −2, −1) Entry3, (−5, −2, −1) Entry2, (−5, −1, −1) Entry4, (−5, −1, −1) Entry6, (−5, −1, −1) c = a%2 b = - abs(b) 1 if (b<c) 2 b = b+1 3 if (b>0) 4 b = 0 5 return b 6 T F T F a={−5, 5}, b={−2, 2}, c={7, 8} b={−2, −1, 0, 1} We only show the values of b Combine the values across all occurrences

  • f a program point

b={−2, −1, 0} A possible static abstraction using sets

Dec 2017 IIT Bombay

slide-81
SLIDE 81

WSSE Pune Intro to PA: Soundness and Precision 21/1

Static Analysis Computes Abstractions of Traces (2)

Trace 1 a b c Entry1, (5, 2, 7) Entry2, (5, −2, 1) Entry3, (5, −2, 1) Entry2, (5, −1, 1) Entry3, (5, −1, 1) Entry2, (5, 0, 1) Entry3, (5, 0, 1) Entry2, (5, 1, 1) Entry4, (5, 1, 1) Entry5, (5, 1, 1) Entry6, (5, 0, 1) Trace 2 a b c Entry1, (−5, −2, 8) Entry2, (−5, −2, −1) Entry3, (−5, −2, −1) Entry2, (−5, −1, −1) Entry4, (−5, −1, −1) Entry6, (−5, −1, −1) c = a%2 b = - abs(b) 1 if (b<c) 2 b = b+1 3 if (b>0) 4 b = 0 5 return b 6 T F T F a={−5, 5}, b={−2, 2}, c={7, 8} b={−2, −1, 0, 1} We only show the values of b Combine the values across all occurrences

  • f a program point

b={−2, −1, 0} b={−1, 1} A possible static abstraction using sets

Dec 2017 IIT Bombay

slide-82
SLIDE 82

WSSE Pune Intro to PA: Soundness and Precision 21/1

Static Analysis Computes Abstractions of Traces (2)

Trace 1 a b c Entry1, (5, 2, 7) Entry2, (5, −2, 1) Entry3, (5, −2, 1) Entry2, (5, −1, 1) Entry3, (5, −1, 1) Entry2, (5, 0, 1) Entry3, (5, 0, 1) Entry2, (5, 1, 1) Entry4, (5, 1, 1) Entry5, (5, 1, 1) Entry6, (5, 0, 1) Trace 2 a b c Entry1, (−5, −2, 8) Entry2, (−5, −2, −1) Entry3, (−5, −2, −1) Entry2, (−5, −1, −1) Entry4, (−5, −1, −1) Entry6, (−5, −1, −1) c = a%2 b = - abs(b) 1 if (b<c) 2 b = b+1 3 if (b>0) 4 b = 0 5 return b 6 T F T F a={−5, 5}, b={−2, 2}, c={7, 8} b={−2, −1, 0, 1} We only show the values of b Combine the values across all occurrences

  • f a program point

b={−2, −1, 0} b={−1, 1} b={1} A possible static abstraction using sets

Dec 2017 IIT Bombay

slide-83
SLIDE 83

WSSE Pune Intro to PA: Soundness and Precision 21/1

Static Analysis Computes Abstractions of Traces (2)

Trace 1 a b c Entry1, (5, 2, 7) Entry2, (5, −2, 1) Entry3, (5, −2, 1) Entry2, (5, −1, 1) Entry3, (5, −1, 1) Entry2, (5, 0, 1) Entry3, (5, 0, 1) Entry2, (5, 1, 1) Entry4, (5, 1, 1) Entry5, (5, 1, 1) Entry6, (5, 0, 1) Trace 2 a b c Entry1, (−5, −2, 8) Entry2, (−5, −2, −1) Entry3, (−5, −2, −1) Entry2, (−5, −1, −1) Entry4, (−5, −1, −1) Entry6, (−5, −1, −1) c = a%2 b = - abs(b) 1 if (b<c) 2 b = b+1 3 if (b>0) 4 b = 0 5 return b 6 T F T F a={−5, 5}, b={−2, 2}, c={7, 8} b={−2, −1, 0, 1} We only show the values of b Combine the values across all occurrences

  • f a program point

b={−2, −1, 0} b={−1, 1} b={1} b={−1, 0} A possible static abstraction using sets

Dec 2017 IIT Bombay

slide-84
SLIDE 84

WSSE Pune Intro to PA: Soundness and Precision 22/1

Computing Static Abstraction for Liveness of Variables

Trace 1 a b c Entry1, (1, 1, 0) Entry2, (0, 1, 1) Entry3, (0, 1, 1) Entry2, (0, 1, 1) Entry3, (0, 1, 1) Entry2, (0, 1, 1) Entry3, (0, 1, 1) Entry2, (0, 1, 1) Entry4, (0, 1, 0) Entry5, (0, 0, 0) Entry6, (0, 1, 0) Trace 2 a b c Entry1, (1, 1, 0) Entry2, (0, 1, 1) Entry3, (0, 0, 1) Entry2, (0, 1, 1) Entry4, (0, 1, 0) Entry6, (0, 1, 0) c = a%2 b = - abs(b) 1 if (b<c) 2 b = b+1 3 if (b>0) 4 b = 0 5 return b 6 T F T F At a program point p a → 1 ⇒ a is live at p a → 0 ⇒ a is not live at p

Dec 2017 IIT Bombay

slide-85
SLIDE 85

WSSE Pune Intro to PA: Soundness and Precision 22/1

Computing Static Abstraction for Liveness of Variables

Trace 1 a b c Entry1, (1, 1, 0) Entry2, (0, 1, 1) Entry3, (0, 1, 1) Entry2, (0, 1, 1) Entry3, (0, 1, 1) Entry2, (0, 1, 1) Entry3, (0, 1, 1) Entry2, (0, 1, 1) Entry4, (0, 1, 0) Entry5, (0, 0, 0) Entry6, (0, 1, 0) Trace 2 a b c Entry1, (1, 1, 0) Entry2, (0, 1, 1) Entry3, (0, 0, 1) Entry2, (0, 1, 1) Entry4, (0, 1, 0) Entry6, (0, 1, 0) c = a%2 b = - abs(b) 1 if (b<c) 2 b = b+1 3 if (b>0) 4 b = 0 5 return b 6 T F T F 110 or {a, b} At a program point p a → 1 ⇒ a is live at p a → 0 ⇒ a is not live at p

Dec 2017 IIT Bombay

slide-86
SLIDE 86

WSSE Pune Intro to PA: Soundness and Precision 22/1

Computing Static Abstraction for Liveness of Variables

Trace 1 a b c Entry1, (1, 1, 0) Entry2, (0, 1, 1) Entry3, (0, 1, 1) Entry2, (0, 1, 1) Entry3, (0, 1, 1) Entry2, (0, 1, 1) Entry3, (0, 1, 1) Entry2, (0, 1, 1) Entry4, (0, 1, 0) Entry5, (0, 0, 0) Entry6, (0, 1, 0) Trace 2 a b c Entry1, (1, 1, 0) Entry2, (0, 1, 1) Entry3, (0, 0, 1) Entry2, (0, 1, 1) Entry4, (0, 1, 0) Entry6, (0, 1, 0) c = a%2 b = - abs(b) 1 if (b<c) 2 b = b+1 3 if (b>0) 4 b = 0 5 return b 6 T F T F 110 or {a, b} 011 or {b, c} At a program point p a → 1 ⇒ a is live at p a → 0 ⇒ a is not live at p

Dec 2017 IIT Bombay

slide-87
SLIDE 87

WSSE Pune Intro to PA: Soundness and Precision 22/1

Computing Static Abstraction for Liveness of Variables

Trace 1 a b c Entry1, (1, 1, 0) Entry2, (0, 1, 1) Entry3, (0, 1, 1) Entry2, (0, 1, 1) Entry3, (0, 1, 1) Entry2, (0, 1, 1) Entry3, (0, 1, 1) Entry2, (0, 1, 1) Entry4, (0, 1, 0) Entry5, (0, 0, 0) Entry6, (0, 1, 0) Trace 2 a b c Entry1, (1, 1, 0) Entry2, (0, 1, 1) Entry3, (0, 0, 1) Entry2, (0, 1, 1) Entry4, (0, 1, 0) Entry6, (0, 1, 0) c = a%2 b = - abs(b) 1 if (b<c) 2 b = b+1 3 if (b>0) 4 b = 0 5 return b 6 T F T F 110 or {a, b} 011 or {b, c} 011 or {b, c} At a program point p a → 1 ⇒ a is live at p a → 0 ⇒ a is not live at p

Dec 2017 IIT Bombay

slide-88
SLIDE 88

WSSE Pune Intro to PA: Soundness and Precision 22/1

Computing Static Abstraction for Liveness of Variables

Trace 1 a b c Entry1, (1, 1, 0) Entry2, (0, 1, 1) Entry3, (0, 1, 1) Entry2, (0, 1, 1) Entry3, (0, 1, 1) Entry2, (0, 1, 1) Entry3, (0, 1, 1) Entry2, (0, 1, 1) Entry4, (0, 1, 0) Entry5, (0, 0, 0) Entry6, (0, 1, 0) Trace 2 a b c Entry1, (1, 1, 0) Entry2, (0, 1, 1) Entry3, (0, 0, 1) Entry2, (0, 1, 1) Entry4, (0, 1, 0) Entry6, (0, 1, 0) c = a%2 b = - abs(b) 1 if (b<c) 2 b = b+1 3 if (b>0) 4 b = 0 5 return b 6 T F T F 110 or {a, b} 011 or {b, c} 011 or {b, c} 010 or {b} At a program point p a → 1 ⇒ a is live at p a → 0 ⇒ a is not live at p

Dec 2017 IIT Bombay

slide-89
SLIDE 89

WSSE Pune Intro to PA: Soundness and Precision 22/1

Computing Static Abstraction for Liveness of Variables

Trace 1 a b c Entry1, (1, 1, 0) Entry2, (0, 1, 1) Entry3, (0, 1, 1) Entry2, (0, 1, 1) Entry3, (0, 1, 1) Entry2, (0, 1, 1) Entry3, (0, 1, 1) Entry2, (0, 1, 1) Entry4, (0, 1, 0) Entry5, (0, 0, 0) Entry6, (0, 1, 0) Trace 2 a b c Entry1, (1, 1, 0) Entry2, (0, 1, 1) Entry3, (0, 0, 1) Entry2, (0, 1, 1) Entry4, (0, 1, 0) Entry6, (0, 1, 0) c = a%2 b = - abs(b) 1 if (b<c) 2 b = b+1 3 if (b>0) 4 b = 0 5 return b 6 T F T F 110 or {a, b} 011 or {b, c} 011 or {b, c} 010 or {b} 000 or ∅ At a program point p a → 1 ⇒ a is live at p a → 0 ⇒ a is not live at p

Dec 2017 IIT Bombay

slide-90
SLIDE 90

WSSE Pune Intro to PA: Soundness and Precision 22/1

Computing Static Abstraction for Liveness of Variables

Trace 1 a b c Entry1, (1, 1, 0) Entry2, (0, 1, 1) Entry3, (0, 1, 1) Entry2, (0, 1, 1) Entry3, (0, 1, 1) Entry2, (0, 1, 1) Entry3, (0, 1, 1) Entry2, (0, 1, 1) Entry4, (0, 1, 0) Entry5, (0, 0, 0) Entry6, (0, 1, 0) Trace 2 a b c Entry1, (1, 1, 0) Entry2, (0, 1, 1) Entry3, (0, 0, 1) Entry2, (0, 1, 1) Entry4, (0, 1, 0) Entry6, (0, 1, 0) c = a%2 b = - abs(b) 1 if (b<c) 2 b = b+1 3 if (b>0) 4 b = 0 5 return b 6 T F T F 110 or {a, b} 011 or {b, c} 011 or {b, c} 010 or {b} 000 or ∅ 010 or {b} At a program point p a → 1 ⇒ a is live at p a → 0 ⇒ a is not live at p

Dec 2017 IIT Bombay

slide-91
SLIDE 91

WSSE Pune Intro to PA: Soundness and Precision 22/1

Computing Static Abstraction for Liveness of Variables

Trace 1 a b c Entry1, (1, 1, 0) Entry2, (0, 1, 1) Entry3, (0, 1, 1) Entry2, (0, 1, 1) Entry3, (0, 1, 1) Entry2, (0, 1, 1) Entry3, (0, 1, 1) Entry2, (0, 1, 1) Entry4, (0, 1, 0) Entry5, (0, 0, 0) Entry6, (0, 1, 0) Trace 2 a b c Entry1, (1, 1, 0) Entry2, (0, 1, 1) Entry3, (0, 0, 1) Entry2, (0, 1, 1) Entry4, (0, 1, 0) Entry6, (0, 1, 0) c = a%2 b = - abs(b) 1 if (b<c) 2 b = b+1 3 if (b>0) 4 b = 0 5 return b 6 T F T F 110 or {a, b} 011 or {b, c} 011 or {b, c} 010 or {b} 000 or ∅ 010 or {b} At a program point p a → 1 ⇒ a is live at p a → 0 ⇒ a is not live at p Trace 2 does not add anything to the abstraction

Dec 2017 IIT Bombay

slide-92
SLIDE 92

WSSE Pune Intro to PA: Soundness and Precision 23/1

Soundness of Abstractions (1)

Sound

  • An over-approximation
  • f traces is sound

Dec 2017 IIT Bombay

slide-93
SLIDE 93

WSSE Pune Intro to PA: Soundness and Precision 23/1

Soundness of Abstractions (1)

Sound

  • An over-approximation
  • f traces is sound
  • Missing any state in

any trace causes unsoundness Unsound

Dec 2017 IIT Bombay

slide-94
SLIDE 94

WSSE Pune Intro to PA: Soundness and Precision 23/1

Soundness of Abstractions (1)

Sound

  • An over-approximation
  • f traces is sound
  • Missing any state in

any trace causes unsoundness Unsound

Dec 2017 IIT Bombay

slide-95
SLIDE 95

WSSE Pune Intro to PA: Soundness and Precision 24/1

Soundness of Abstractions (2)

c = a%2 b = - abs(b) 1 if (b<c) 2 b = b+1 3 if (b>0) 4 b = 0 5 return b 6 T F T F a={−5, 5}, b={−2, 2}, c={7, 8} b={−2, −1, 0, 1} b={−2, −1, 0} b={−1, 1} b={1} b={−1, 0} An unsound abstraction All variables can have arbitrary values at the start. b can have many more values at the entry of

  • blocks 2 and 3 (e.g. -3,
  • 8, . . . )
  • block 4 (e.g. 0)

Dec 2017 IIT Bombay

slide-96
SLIDE 96

WSSE Pune Intro to PA: Soundness and Precision 24/1

Soundness of Abstractions (2)

c = a%2 b = - abs(b) 1 if (b<c) 2 b = b+1 3 if (b>0) 4 b = 0 5 return b 6 T F T F a={−5, 5}, b={−2, 2}, c={7, 8} b={−2, −1, 0, 1} b={−2, −1, 0} b={−1, 1} b={1} b={−1, 0} An unsound abstraction

  • Over-approximated range of

values denoted by

  • low limit, high limit
  • Inclusive limits with

low limit ≤ high limit

  • One contiguous range per

variable with no “holes” A sound abstraction using intervals

Dec 2017 IIT Bombay

slide-97
SLIDE 97

WSSE Pune Intro to PA: Soundness and Precision 24/1

Soundness of Abstractions (2)

c = a%2 b = - abs(b) 1 if (b<c) 2 b = b+1 3 if (b>0) 4 b = 0 5 return b 6 T F T F a={−5, 5}, b={−2, 2}, c={7, 8} b={−2, −1, 0, 1} b={−2, −1, 0} b={−1, 1} b={1} b={−1, 0} An unsound abstraction c = a%2 b = - abs(b) 1 if (b<c) 2 b = b+1 3 if (b>0) 4 b = 0 5 return b 6 T F T F a=[−∞, ∞] , b=[−∞, ∞] , c =[−∞, ∞] b=[−∞, 1] b=[−∞, 0] b=[−1, 1] b=[1, 1] b=[−1, 0] A sound abstraction using intervals

Dec 2017 IIT Bombay

slide-98
SLIDE 98

WSSE Pune Intro to PA: Soundness and Precision 24/1

Soundness of Abstractions (2)

c = a%2 b = - abs(b) 1 if (b<c) 2 b = b+1 3 if (b>0) 4 b = 0 5 return b 6 T F T F a={−5, 5}, b={−2, 2}, c={7, 8} b={−2, −1, 0, 1} b={−2, −1, 0} b={−1, 1} b={1} b={−1, 0} An unsound abstraction c = a%2 b = - abs(b) 1 if (b<c) 2 b = b+1 3 if (b>0) 4 b = 0 5 return b 6 T F T F a=[−∞, ∞] , b=[−∞, ∞] , c =[−∞, ∞] b=[−∞, 1] b=[−∞, 0] b=[−1, 1] b=[1, 1] b=[−1, 0] A sound abstraction using intervals b can be 1 because of the increment in basic block 3

Dec 2017 IIT Bombay

slide-99
SLIDE 99

WSSE Pune Intro to PA: Soundness and Precision 25/1

Soundness of Abstractions for Liveness Analysis

A sound abstraction An unsound abstraction c = a%2 b = - abs(b) 1 if (b<c) 2 b = b+1 3 if (b>0) 4 b = 0 5 return b 6 T F T F {a, b} {b, c} {b, c} {b} ∅ {b} c = a%2 b = - abs(b) 1 if (b<c) 2 b = b+1 3 if (b>0) 4 b = 0 5 return b 6 T F T F ∅ ∅ ∅ ∅ ∅ ∅

Dec 2017 IIT Bombay

slide-100
SLIDE 100

WSSE Pune Intro to PA: Soundness and Precision 26/1

Precision of Sound Abstractions(1)

Sound but imprecise

Dec 2017 IIT Bombay

slide-101
SLIDE 101

WSSE Pune Intro to PA: Soundness and Precision 26/1

Precision of Sound Abstractions(1)

Sound but imprecise Sound and more precise

Dec 2017 IIT Bombay

slide-102
SLIDE 102

WSSE Pune Intro to PA: Soundness and Precision 26/1

Precision of Sound Abstractions(1)

Sound but imprecise Sound and more precise Sound and even more precise

Dec 2017 IIT Bombay

slide-103
SLIDE 103

WSSE Pune Intro to PA: Soundness and Precision 26/1

Precision of Sound Abstractions(1)

Sound but imprecise Sound and more precise Sound and even more precise

  • Precision is relative, soundness is absolute
  • Qualifiers “more” precise and “less” precise

are meaningful

  • Qualifiers “more” sound and “less” sound

are not meaningful

Dec 2017 IIT Bombay

slide-104
SLIDE 104

WSSE Pune Intro to PA: Soundness and Precision 27/1

Precision of Sound Abstractions(2)

c = a%2 b = - abs(b) 1 if (b<c) 2 b = b+1 3 if (b>0) 4 b = 0 5 return b 6 T F T F a=[−∞, ∞] , b=[−∞, ∞] , c =[−∞, ∞] b=[−∞, 1] b=[−∞, 0] b=[−1, 1] b=[1, 1] b=[−1, 0] A precise abstraction using intervals c = a%2 b = - abs(b) 1 if (b<c) 2 b = b+1 3 if (b>0) 4 b = 0 5 return b 6 T F T F a=[−∞, ∞] , b=[−∞, ∞] , c =[−∞, ∞] b=[−∞, ∞] b=[−∞, ∞] b=[−∞, ∞] b=[−∞, ∞] b=[−∞, ∞] An imprecise abstraction using intervals

Dec 2017 IIT Bombay

slide-105
SLIDE 105

WSSE Pune Intro to PA: Soundness and Precision 28/1

Precision of Abstractions for Liveness Analysis

A precise abstraction An imprecise abstraction c = a%2 b = - abs(b) 1 if (b<c) 2 b = b+1 3 if (b>0) 4 b = 0 5 return b 6 T F T F {a, b} {b, c} {b, c} {b} ∅ {b} c = a%2 b = - abs(b) 1 if (b<c) 2 b = b+1 3 if (b>0) 4 b = 0 5 return b 6 T F T F {a, b, c} {a, b, c} {a, b, c} {a, b, c} ∅ {a, b, c}

Dec 2017 IIT Bombay

slide-106
SLIDE 106

WSSE Pune Intro to PA: Soundness and Precision 29/1

Limitations of Static Analysis

  • In general, the computation of exact static abstraction is undecidable

◮ Possible reasons

  • Values of variables not known
  • Branch outcomes not known
  • Infinitely many paths in the presence of loops or recursion
  • Infinitely many values

◮ We have to settle for some imprecision ◮ How are data states compared to distinguish between a sound and

unsound (or a precise or an imprecise result)?

  • We have introduced the concepts intuitively
  • Formally, the comparison is made by defining a partial order

Dec 2017 IIT Bombay

slide-107
SLIDE 107

WSSE Pune Intro to PA: Soundness and Precision 30/1

Practical Static Analysis

  • The goodness of a static analysis lies in minimizing imprecision without

compromising on soundness Additional expectations: Efficiency and scalability

  • Some applications (e.g. debugging) do not need to cover all traces
  • We have not talked about completeness

◮ Some features of a programming language may not be covered

(e.g. “eval” in JavaScript, aliasing of array indices, effect of libraries)

◮ Accept a “soundy” analysis [Livshits et. al. CACM 2015]

OR Tolerate imprecision for complete soundness

Dec 2017 IIT Bombay