static program analysis - universität des saarlandes · static program analysis foundations of...

33
Static Program Analysis Foundations of Abstract Interpretation Sebastian Hack, Christian Hammer, Jan Reineke Advanced Lecture, Winter 2014/15

Upload: others

Post on 27-Sep-2020

8 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Static Program Analysis - Universität des Saarlandes · Static Program Analysis Foundations of Abstract Interpretation Sebastian Hack, Christian Hammer, Jan Reineke Advanced Lecture,

Static Program Analysis Foundations of Abstract Interpretation

Sebastian Hack, Christian Hammer, Jan Reineke

Advanced Lecture, Winter 2014/15

Page 2: Static Program Analysis - Universität des Saarlandes · Static Program Analysis Foundations of Abstract Interpretation Sebastian Hack, Christian Hammer, Jan Reineke Advanced Lecture,

Abstract Interpretation

Semantics-based approach to program analysis

Framework to develop provably correct and terminating

analyses

Ingredients:

Concrete semantics: Formalizes meaning of a program

Abstract semantics

Both semantics defined as fixpoints of monotone

functions over some domain

Relation between the two semantics establishing

correctness

Page 3: Static Program Analysis - Universität des Saarlandes · Static Program Analysis Foundations of Abstract Interpretation Sebastian Hack, Christian Hammer, Jan Reineke Advanced Lecture,

Concrete Semantics

Different semantics are required for

different properties:

“Is there an execution in which

the value of x alternates between

3 and 5?” Trace Semantics

“Is the final value of x always the

same as the initial value of x?”

“Input/Output” Semantics

“May x ever assume the value 45

at program point 7?”

Reachability Semantics

start

1

2

5

6 7

8

x = x % 5

y = 42

Pos(x < y)

Pos(a<b) Neg(a<b)

Neg(x < y)

x = x+2 x = x+1

3

4

a = M[x]

b = M[x+1]

Page 4: Static Program Analysis - Universität des Saarlandes · Static Program Analysis Foundations of Abstract Interpretation Sebastian Hack, Christian Hammer, Jan Reineke Advanced Lecture,

Concrete Semantics

Trace Semantics: Captures set of traces of

states that the program may execute.

Input/Output Semantics: Captures the pairs of

initial and final states of execution traces.

Abstraction of Trace Semantics

Reachability Semantics: Captures the set of

reachable states at each program point

Abstraction of Trace Semantics

Page 5: Static Program Analysis - Universität des Saarlandes · Static Program Analysis Foundations of Abstract Interpretation Sebastian Hack, Christian Hammer, Jan Reineke Advanced Lecture,

Reachability Semantics

Captures the set of reachable states at each

program point. Formally:

Example: start

1 3

x = 0

Pos(x < 100)

Neg(x < 100)

x = x+1

2

x \in {…, -2, -1, 0, 1, 2, …}

x \in {0, …, 100} x \in {101}

Page 6: Static Program Analysis - Universität des Saarlandes · Static Program Analysis Foundations of Abstract Interpretation Sebastian Hack, Christian Hammer, Jan Reineke Advanced Lecture,

Reachability Semantics

Can be captured as the least solution of:

start

1 3

x = 0

Pos(x < 100)

Neg(x < 100)

x = x+1

2

Page 7: Static Program Analysis - Universität des Saarlandes · Static Program Analysis Foundations of Abstract Interpretation Sebastian Hack, Christian Hammer, Jan Reineke Advanced Lecture,

Questions

Why the least solution?

Is there more than one solution?

Is there a unique least solution?

Can we systematically compute it?

start

1 3

x = 0

Pos(x < 100)

Neg(x < 100)

x = x+1

2

Page 8: Static Program Analysis - Universität des Saarlandes · Static Program Analysis Foundations of Abstract Interpretation Sebastian Hack, Christian Hammer, Jan Reineke Advanced Lecture,

Answers

Is there more than one solution? Often

Is there a unique least solution? Yes

Can we systematically compute it? Yes and No

start

1 3

x = 0

Pos(x < 100)

Neg(x < 100)

x = x+1

2

Page 9: Static Program Analysis - Universität des Saarlandes · Static Program Analysis Foundations of Abstract Interpretation Sebastian Hack, Christian Hammer, Jan Reineke Advanced Lecture,

Why? Knaster-Tarski Fixpoint Theorem

Raises more questions:

What is a complete lattice?

What is a monotonic function?

What is a fixed point?

Page 10: Static Program Analysis - Universität des Saarlandes · Static Program Analysis Foundations of Abstract Interpretation Sebastian Hack, Christian Hammer, Jan Reineke Advanced Lecture,

Monotone Functions

Examples:

Which of these are monotone?

Need to know what the order is.

Page 11: Static Program Analysis - Universität des Saarlandes · Static Program Analysis Foundations of Abstract Interpretation Sebastian Hack, Christian Hammer, Jan Reineke Advanced Lecture,

Partial Orders

Page 12: Static Program Analysis - Universität des Saarlandes · Static Program Analysis Foundations of Abstract Interpretation Sebastian Hack, Christian Hammer, Jan Reineke Advanced Lecture,

Partial Orders: Examples I

Page 13: Static Program Analysis - Universität des Saarlandes · Static Program Analysis Foundations of Abstract Interpretation Sebastian Hack, Christian Hammer, Jan Reineke Advanced Lecture,

Partial Orders: Examples II

What about ?

Page 14: Static Program Analysis - Universität des Saarlandes · Static Program Analysis Foundations of Abstract Interpretation Sebastian Hack, Christian Hammer, Jan Reineke Advanced Lecture,

Complete Lattices

What is an upper bound of a set A?

What is the least upper bound (also: join, supremum) of a set A?

Page 15: Static Program Analysis - Universität des Saarlandes · Static Program Analysis Foundations of Abstract Interpretation Sebastian Hack, Christian Hammer, Jan Reineke Advanced Lecture,

Least Upper Bounds: Examples I

Which of these are complete lattices?

Page 16: Static Program Analysis - Universität des Saarlandes · Static Program Analysis Foundations of Abstract Interpretation Sebastian Hack, Christian Hammer, Jan Reineke Advanced Lecture,

Least Upper Bounds: Examples II

Which of these are complete lattices?

Page 17: Static Program Analysis - Universität des Saarlandes · Static Program Analysis Foundations of Abstract Interpretation Sebastian Hack, Christian Hammer, Jan Reineke Advanced Lecture,

Properties of Complete Lattices

Page 18: Static Program Analysis - Universität des Saarlandes · Static Program Analysis Foundations of Abstract Interpretation Sebastian Hack, Christian Hammer, Jan Reineke Advanced Lecture,

Generic Lattice Constructions:

Power-set Lattice

Graphical representation (Hasse diagram):

Page 19: Static Program Analysis - Universität des Saarlandes · Static Program Analysis Foundations of Abstract Interpretation Sebastian Hack, Christian Hammer, Jan Reineke Advanced Lecture,

Generic Lattice Constructions:

Total Function Space

What about ?

Page 20: Static Program Analysis - Universität des Saarlandes · Static Program Analysis Foundations of Abstract Interpretation Sebastian Hack, Christian Hammer, Jan Reineke Advanced Lecture,

Generic Lattice Constructions: Flat Lattice

Graphical representation (Hasse diagram) with :

… -3 -2 -1 0 1 2 3 …

Page 21: Static Program Analysis - Universität des Saarlandes · Static Program Analysis Foundations of Abstract Interpretation Sebastian Hack, Christian Hammer, Jan Reineke Advanced Lecture,

Fixed Points

Example:

Has multiple fixed points: But a unique least fixed point.

Page 22: Static Program Analysis - Universität des Saarlandes · Static Program Analysis Foundations of Abstract Interpretation Sebastian Hack, Christian Hammer, Jan Reineke Advanced Lecture,

Knaster-Tarski Fixpoint Theorem

Raises more questions:

What is a complete lattice? ✓

What is a monotonic function? ✓

What is a fixed point? ✓

Page 23: Static Program Analysis - Universität des Saarlandes · Static Program Analysis Foundations of Abstract Interpretation Sebastian Hack, Christian Hammer, Jan Reineke Advanced Lecture,

Back to the Reachability Semantics

Can be captured as the least fixed point of:

start

1 3

x = 0

Pos(x < 100)

Neg(x < 100)

x = x+1

2

Monotone?

Page 24: Static Program Analysis - Universität des Saarlandes · Static Program Analysis Foundations of Abstract Interpretation Sebastian Hack, Christian Hammer, Jan Reineke Advanced Lecture,

How to Compute the Least Fixed Point

Kleene Iteration:

Why is this increasing?

Will this reach the fixed point?

It will here:

But in general?

start

1 3

x = 0

Pos(x < 100)

Neg(x < 100)

x = x+1

2

start

1 3

x = 0

Pos(true)

Neg(true)

x = x+1

2

No!

Lattice has infinite ascending chains.

Page 25: Static Program Analysis - Universität des Saarlandes · Static Program Analysis Foundations of Abstract Interpretation Sebastian Hack, Christian Hammer, Jan Reineke Advanced Lecture,

Ascending Chain Condition

Length of longest ascending chain determines worst-case complexity

of Kleene Iteration.

… -1 0 1 …

Power set lattice Flat lattice

How about total function space lattice?

Page 26: Static Program Analysis - Universität des Saarlandes · Static Program Analysis Foundations of Abstract Interpretation Sebastian Hack, Christian Hammer, Jan Reineke Advanced Lecture,

Recap: Abstract Interpretation

Semantics-based approach to program analysis

Framework to develop provably correct and terminating

analyses

Ingredients:

Concrete semantics: Formalizes meaning of a program

Abstract semantics

Both semantics defined as fixpoints of monotone

functions over some domain

Relation between the two semantics establishing

correctness

(✓)

Page 27: Static Program Analysis - Universität des Saarlandes · Static Program Analysis Foundations of Abstract Interpretation Sebastian Hack, Christian Hammer, Jan Reineke Advanced Lecture,

Abstract Semantics

Similar to concrete semantics:

A complete lattice (L#, ≤) as the domain for

abstract elements

A monotone function F# corresponding to the

concrete function F

Then the abstract semantics is the least fixed

point of F#, lfp F#

If F# “correctly approximates” F,

then lfp F# “correctly approximates” lfp F.

Page 28: Static Program Analysis - Universität des Saarlandes · Static Program Analysis Foundations of Abstract Interpretation Sebastian Hack, Christian Hammer, Jan Reineke Advanced Lecture,

An Example Abstract Domain

for Values of Variables

How to relate the two?

Concretization function, specifying “meaning” of abstract values.

Abstraction function: determines best representation concrete values.

Page 29: Static Program Analysis - Universität des Saarlandes · Static Program Analysis Foundations of Abstract Interpretation Sebastian Hack, Christian Hammer, Jan Reineke Advanced Lecture,

Relation between Abstract and Concrete

Are these functions monotone?

Why should they be?

What is the meaning of the partial order in the abstract domain?

What if we first abstract and then concretize?

Page 30: Static Program Analysis - Universität des Saarlandes · Static Program Analysis Foundations of Abstract Interpretation Sebastian Hack, Christian Hammer, Jan Reineke Advanced Lecture,

How to Compute in the Abstract Domain

Example: Multiplication on Flat Lattice

0 a

0

b

* #

Denotes abstract

version of operator

Page 31: Static Program Analysis - Universität des Saarlandes · Static Program Analysis Foundations of Abstract Interpretation Sebastian Hack, Christian Hammer, Jan Reineke Advanced Lecture,

How to Compute in the Abstract Domain?

Formally

Local Correctness Condition:

Correct by construction

(if concretization and abstraction have certain properties):

Page 32: Static Program Analysis - Universität des Saarlandes · Static Program Analysis Foundations of Abstract Interpretation Sebastian Hack, Christian Hammer, Jan Reineke Advanced Lecture,

From Local to Global Correctness

Page 33: Static Program Analysis - Universität des Saarlandes · Static Program Analysis Foundations of Abstract Interpretation Sebastian Hack, Christian Hammer, Jan Reineke Advanced Lecture,

Fixpoint Transfer Theorem