Semantics: Application to C Programs Lecture Slides by Dr. - - PowerPoint PPT Presentation

semantics application to c programs
SMART_READER_LITE
LIVE PREVIEW

Semantics: Application to C Programs Lecture Slides by Dr. - - PowerPoint PPT Presentation

Semantics: Application to C Programs Lecture Slides by Dr. Marie-Christine Jakobs Prof. Dr. Dirk Beyer Dirk.Beyer@sosy-lab.org SoSy-Lab, LMU Munich, Germany Organization Prof. Dr. Dirk Beyer SoSy-Lab, LMU Munich, Germany 2 / 100 Lecture


slide-1
SLIDE 1

Semantics: Application to C Programs

Lecture Slides by Dr. Marie-Christine Jakobs

  • Prof. Dr. Dirk Beyer

Dirk.Beyer@sosy-lab.org

SoSy-Lab, LMU Munich, Germany

slide-2
SLIDE 2

Organization

  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 2 / 100

slide-3
SLIDE 3

Lecture and Tutorial

Lecture Feb 27, 2019, 10:00 – 14:00 Munich, Oettingenstr. 67, C003 Tutorial Feb 27, 2019, 14:00 – 15:30 Munich, Oettingenstr. 67, C003

  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 3 / 100

slide-4
SLIDE 4

Course Material

https: //www.sosy-lab.org/Teaching/2019-SS-Semantik/

  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 4 / 100

slide-5
SLIDE 5

Introduction

  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 5 / 100

slide-6
SLIDE 6

Software Analysis

Computes an (over-)approximation of a program’s behavior. Applications ◮ Optimization ◮ Correctness (i.e. whether program satisfies a given property)

  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 6 / 100

slide-7
SLIDE 7

Software Verification

Formally proves whether a program P satisfies a property ϕ. ◮ Requires program semantics, i.e., meaning of program ◮ Relies on mathematical methods,

◮ logic ◮ induction ◮ . . .

  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 7 / 100

slide-8
SLIDE 8

Software Verification

Formally proves whether a program P satisfies a property ϕ.

Program P Property ϕ Verifier

TRUE FALSE ×

Disprove (×) Find a program execution (counterexample) that violates the property ϕ Prove () Show that every execution of the program satisfies the property ϕ.

  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 8 / 100

slide-9
SLIDE 9

What Could an Analysis Find out?

double divTwiceCons(double y) { int cons = 5; int d = 2∗cons; if (cons != 0) return y/(2∗cons); else return 0; }

  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 9 / 100

slide-10
SLIDE 10

Some Analysis Results

double divTwiceCons(double y) { int cons = 5; // expression 2*cons has value 10 // variable d not used int d = 2∗cons; if (cons != 0) // expression 2*cons evaluated before return y/(2∗cons); else // dead code return 0; }

  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 10 / 100

slide-11
SLIDE 11

One Resulting Code Optimization

double divTwiceCons(double y) { int cons = 5; // expression 2*cons has value 10 // variable d not used int d = 2∗cons; if (cons != 0) // expression 2*cons evaluated before return y/(2∗cons); else // dead code return 0; }

double divTwiceConsOptimized(double y) { return y/10; }

  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 11 / 100

slide-12
SLIDE 12

Does This Code Work?

double avgUpTo(int[] numbers, int length) { double sum = 0; for(int i=0;i<length;i++) sum += numbers[i]; return sum/(double)length; }

  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 12 / 100

slide-13
SLIDE 13

Problems With This Code

double avgUpTo(int[] numbers, int length) { double sum = 0; for(int i=0;i<length;i++) // possible null pointer access (numbers==null) // index out of bounds (length>numbers.length) // integer overflow sum += numbers[i]; // division by zero (length==0) return sum/(double) length; }

  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 13 / 100

slide-14
SLIDE 14

Why Should One Care for Bugs?

Costs

Ariane V88 Intel Pentium FDIV bug . . . Mars Polar Lander

Safety-criticality

endanger human lives Therac-25 Uber autonomous car . . .

  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 14 / 100

slide-15
SLIDE 15

Analysis and Verification Tools

SpotBugs Lint Error Prone Klee PeX Sapienz CPAchecker Infer CBMC SLAM UltimateAutomizer . . .

  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 15 / 100

slide-16
SLIDE 16

Overview on Analysis and Verification Techniques

Dataflow Analysis Abstract Interpretation Program Analysis Model Checking Automatic Interactive Theorem Proving Static Type Systems Dynamic Testing Runtime Verification

This lecture

  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 16 / 100

slide-17
SLIDE 17

Why Different Static, Automatic Techniques?

Theorem of Rice

Any non-trivial, semantic property of programs is undecidable.

Consequences

Techniques are ◮ incomplete, e.g. answer UNKNOWN, or ◮ unsound, i.e., report

◮ false alarms (non-existing bugs), ◮ false proofs (miss bugs).

  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 17 / 100

slide-18
SLIDE 18

Verifier Design Space

Program P Property ϕ Verifier Ideal verifier

TRUE UNKNOWN FALSE ×

Program P Property ϕ Verifier Unreliable verifier

TRUE UNKNOWN FALSE ×

false alarm violation false proof correct

  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 18 / 100

slide-19
SLIDE 19

Verifier Design Space

◮ Overapproximating verifier (superset of program behavior)

without precise counterexample check

Program P Property ϕ Verifier

TRUE UNKNOWN FALSE ×

false alarm violation

◮ Underapproximating verifier (subset of program behavior)

Program P Property ϕ Verifier

TRUE UNKNOWN FALSE ×

false proof correct

  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 19 / 100

slide-20
SLIDE 20

Other Reasons to Use Different Static Techniques

◮ State space grows exponentially with number of variables ◮ (Syntactic) paths grow exponentially with number of branches ⇒ Precise techniques may require too many resources (memory, time,. . . ) ⇒ Trade-off between precision and costs

  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 20 / 100

slide-21
SLIDE 21

Flow-Insensitivity

Order of statements not considered E.g., does not distinguish between these two programs x=0; y=x; x=x+1; x=0; x=x+1; y=x; ⇒ very imprecise

  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 21 / 100

slide-22
SLIDE 22

Flow-Sensitivity Plus Path-Insensitivity

◮ Takes order of statements into account ◮ Mostly, ignores infeasibility of syntactical paths ◮ Ignores branch correlations E.g., does not distinguish between these two programs

if (x>0) y=1; else y=0; if (x>0) y=y+1; else y=y+2; if (x>0) y=1; else y=0; if (x>0) y=y+2; else y=y+1;

  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 22 / 100

slide-23
SLIDE 23

Path-Sensitivity

◮ Takes (execution) paths into account ◮ Excludes infeasible, syntactic paths (not necessarily all infeasible ones) ◮ Covers flow-sensitivity

if (x>0) y=1; else y=0; if (x>0) y=y+2; else y=y+1;

To detect that y has value 0, 1, or 3 ◮ must exclude infeasible, syntactic path along first else-branch and second if-branch ◮ need to detect correlation between the if-conditions ◮ requires path-sensitivity ⇒ very precise

  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 23 / 100

slide-24
SLIDE 24

Precision vs. Costs

Program Analysis Model Checking Abstract Interpretation Dataflow Analysis Flow-insensitive Flow-sensitive Path-sensitive imprecise precise cheap expensive

  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 24 / 100

slide-25
SLIDE 25

Program Syntax and Semantics

  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 25 / 100

slide-26
SLIDE 26

Programs

Theory: simple while-programs ◮ Restriction to integer constants and variables ◮ Minimal set of statements (assignment, if, while) ◮ Techniques easier to teach/understand Practice: C programs ◮ Widely-used language ◮ Tool support

  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 26 / 100

slide-27
SLIDE 27

While-Programs

◮ Arithmetic expressions aexpr := Z | var | -aexpr | aexpr opa aexpr

  • pa standard arithmetic operation like +, −, /, %, . . .

◮ Boolean expressions bexpr := aexpr | aexpr opc aexpr | !bexpr | bexpr opb bexpr

◮ integer value 0 ≡false, remaining values represent true ◮ opc comparison operator like <, <=, >=, >, ==, ! = ◮ opb logic connective like &&(∧), || (∨), ˆ (xor), . . .

◮ Program S:= var=aexpr; | while bexpr S | if bexpr S else S | if bexpr S | S;S

  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 27 / 100

slide-28
SLIDE 28

Syntax vs. Semantics

Syntax

Representation of a program

Semantics

Meaning of a program

  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 28 / 100

slide-29
SLIDE 29

How to Represent a Program?

  • 1. Source code

if (x>0) abs = x; else abs = −x; i = 1; while(i<abs) i = 2∗i;

◮ Basically sequence of characters ◮ No explicit information about the structure or paths of programs

  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 29 / 100

slide-30
SLIDE 30

How to Represent a Program?

  • 2. Abstract-syntax tree (AST)

Program Sequence if if-Block Condition else-Block x>0 Assignement Assignement abs=x; abs=-x; Sequence Assignment i=1; while while-Block Condition i<abs Assignement i=2*i;

◮ Hierarchical representation ◮ Flow, paths hard to detect

  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 30 / 100

slide-31
SLIDE 31

How to Represent a Program?

  • 3. Control-flow graph

x>0 abs=x; abs=-x; i=1; i<abs i=2*i;

TRUE FALSE TRUE FALSE

  • 4. Control-flow automaton

l0 l1 l2 l3 l4 l5 l6 x>0 !(x>0) abs=x; abs=-x; i=1; i<abs !(i<abs) i=2*i;

  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 31 / 100

slide-32
SLIDE 32

Control-Flow Automaton

Definition

A control-flow automaton (CFA) is a three-tuple P = (L, l0, G) consisting of ◮ the set L of program locations (domain of program counter) ◮ the initial program location l0 ∈ L, and ◮ the control-flow edges G ⊆ L × Ops × L.

  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 32 / 100

slide-33
SLIDE 33

Operations Ops

Two types ◮ Assumes (boolean expressions) ◮ Assignments (var=aexpr;)

  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 33 / 100

slide-34
SLIDE 34

From Source Code to Control-Flow Automaton

Assignment var=expr;

v=expr;

While-Statement while (C) S

S S C ¬ C

If-Statement if (C) S1 else S2

S1 S2 S1 S2 C ¬C

If-Statement if (C) S

S S C ¬C

Sequential Composition S1; S2

S1 S2 S1 S2

  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 34 / 100

slide-35
SLIDE 35

Semantics

Remember: defines meaning of programs Different types ◮ Axiomatic semantics: based on pre- and postconditions, e.g. {true}x=0;{x=0} ◮ Denotational semantics: function from inputs to outputs ◮ Operational semantics (): defines execution of program

  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 35 / 100

slide-36
SLIDE 36

Operational Semantics

Defines program meaning by fixing program execution ◮ Transitions describe single execution steps

◮ Level of assignment or assume ◮ Change states ◮ Evaluate semantics of expressions in a state

◮ Execution: sequence of transitions

  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 36 / 100

slide-37
SLIDE 37

Concrete States

Pair of program counter and data state (C = L × Σ) ◮ Program counter

◮ Where am I? ◮ Location in CFA ◮ c(pc) refers to program counter of concrete state

◮ Data state σ : V → Z

◮ Fixes variable values ◮ c(d) refers to data state of concrete state

  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 37 / 100

slide-38
SLIDE 38

Semantics of Arithmetic Expressions

Evaluation function Sa : aexpr × Σ → Z Defined recursively on structure ◮ const ∈ Z : Sa(const, σ) = const ◮ variable var: Sa(var, σ) = σ(var) ◮ unary operation: Sa(−t, σ) = −Sa(t, σ) ◮ binary operation: Sa(t1 opa t2, σ) = Sa(t1, σ) opa Sa(t2, σ)

  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 38 / 100

slide-39
SLIDE 39

Semantics of Boolean Expressions

Evaluation function Sb : bexpr × Σ → {true, false} Defined recursively on structure ◮ arithmetic expression: Sb(t, σ) =

  • true

if Sa(t, σ) = 0 false else ◮ comparison: Sb(t1 opc t2, σ) = Sa(t1, σ) opc Sa(t2, σ) ◮ logic connection: Sb(b1 opb b2, σ) = Sb(b1, σ) opb Sb(b2, σ)

  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 39 / 100

slide-40
SLIDE 40

Examples for Expression Evaluation

Consider σ : abs → 2; i → 0; x → −2 Derivation of the values of ◮ Sa(−x, σ) ◮ Sa(2 ∗ i, σ) ◮ Sb(x > 0, σ) ◮ Sb(i < abs, σ)

  • n the board.
  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 40 / 100

slide-41
SLIDE 41

State Update

Σ × Opsassignment → Σ σ[var = aexpr; ] = σ′ with σ′(v) =

  • σ(v)

if v = var Sa(aexpr, σ) else

  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 41 / 100

slide-42
SLIDE 42

Examples for State Update

Consider σ : abs → 2; i → 0; x → −2 Computation of the state updates ◮ σ[i = 1; ] ◮ σ[abs = −x; ] ◮ σ[i = 2 ∗ i; ]

  • n the board.
  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 42 / 100

slide-43
SLIDE 43

Transitions – Single Execution Steps

Transitions T ⊆ C × G × C with (c, (l, op, l′), c′) ∈ T if

  • 1. Respects control-flow, i.e.,

c(pc) = l ∧ c′(pc) = l′

  • 2. Valid data behavior

◮ op assignment var=aexpr; ∧c′(d) = c(d)[var = aexpr; ] ◮ op assume bexpr ∧Sb(bexpr, c(d)) = true ∧ c(d) = c′(d)

  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 43 / 100

slide-44
SLIDE 44

Program Paths

Defined inductively ◮ every concrete state c with c(pc) = l0 ◮ c0

g1

→ c1 · · ·

gn

→ cn program path and (cn, gn+1, cn+1) ∈ T , then c0

g1

→ c1 · · ·

gn

→ cn

gn+1

→ cn+1 program path Set of all program paths of program P = (L, G, l0) denoted by paths(P).

  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 44 / 100

slide-45
SLIDE 45

Examples for Program Paths

l0 l1 l2 l3 l4 l5 l6 x>0 !(x>0) abs=x; abs=-x; i=1; i<abs !(i<abs) i=2*i; On the board: Shortest and longest program path starting in state (l0, σ) with σ : abs → 2; i → 0; x → −2

  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 45 / 100

slide-46
SLIDE 46

Reachable States

reach(P) := {c | ∃c0

g1

→ c1 · · ·

gn

→ cn ∈ paths(P) : cn = c}

  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 46 / 100

slide-47
SLIDE 47

Program Properties and Program Correctness

  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 47 / 100

slide-48
SLIDE 48

Program Properties

Trace Property Hyper Property Safety Liveness . . .

Termination Responsiveness

. . .

Reachability Type State Information-Flow Security

  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 48 / 100

slide-49
SLIDE 49

Reachability Property ϕR

Defines which concrete states ϕR ⊆ C must not be reached In this lecture: ◮ Certain program locations must not be reached ◮ Denoted by ϕLsub := {c ∈ C | c(pc) ∈ Lsub}

  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 49 / 100

slide-50
SLIDE 50

Correctness

Definition

Program P is correct wrt. reachability property ϕR if reach(P) ∩ ϕR = ∅.

  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 50 / 100

slide-51
SLIDE 51

Formalizing Verification Terms

◮ False alarm: v(P, ϕR) = FALSE ∧ reach(P) ∩ ϕR = ∅ ◮ False proof: v(P, ϕR) = TRUE ∧ reach(P) ∩ ϕR = ∅ ◮ Verifier v is sound and complete if v does not produce false proofs and false alarms, respectively.

  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 51 / 100

slide-52
SLIDE 52

Abstract Domains

  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 52 / 100

slide-53
SLIDE 53

Problem With Program Semantics

◮ Infinitely many data states σ ⇒ infinitely many reachable states ◮ Cannot analyze program paths individually

  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 53 / 100

slide-54
SLIDE 54

How to deal with infinite state space?

Idea: analyze set of program paths together ◮ Group concrete states ⇒ abstract states ◮ Define (abstract) semantics for abstract states ⇒ Abstract domain

  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 54 / 100

slide-55
SLIDE 55

Partial Order (Recap)

Definition

Let E be a set and ⊑ ⊆ E × E a binary relation on E. The structure (E, ⊑) is a partial order if ⊑ is ◮ reflexive ∀e ∈ E : e ⊑ e, ◮ transitive ∀e1, e2, e3 ∈ E : (e1 ⊑ e2 ∧ e2 ⊑ e3) ⇒ e1 ⊑ e3, ◮ antisymmetric ∀e1, e2 ∈ E : (e1 ⊑ e2 ∧ e2 ⊑ e1) ⇒ e1 = e2.

  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 55 / 100

slide-56
SLIDE 56

Examples for Partial Orders

◮ (Z, ≤) ◮ (2Q, ⊆) ◮ (Σ∗, lexicographic order) ◮ (Σ∗, suffix)

  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 56 / 100

slide-57
SLIDE 57

Chains

Let (E, ⊑) be a partial order.

Definition (Chain)

A subset Esubset ⊆ E is a chain if it is totally ordered, i.e. ∀e, e′ ∈ Esub : e ⊑ e′ ∨ e′ ⊑ e. A chain Esubset is finite if the subset Esubset is finite.

  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 57 / 100

slide-58
SLIDE 58

Ascending Chains

Let (E, ⊑) be a partial order.

Definition (Ascending Chain)

A sequence (ei)n∈N ∈ Eω is an ascending chain if ∀m, m′ ∈ N : m ≤ m′ ⇒ em ⊑ em′.

Definition (Stabilization)

A sequence (ei)n∈N ∈ Eω eventually stabilizes if ∃n0 ∈ N : ∀n ∈ N : n ≥ n0 : en = en0

Definition (Stabilizing Ascending Chain)

A stabilizing ascending chain eventually stabilizes.

  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 58 / 100

slide-59
SLIDE 59

Examples for Chains

Consider (Z, =) ◮ Set {1,2} not a chain ◮ (a1, a2, . . . ) with ai = 1 ascending and stabilizing ◮ Is a stabilizing ascending chain. Consider (Z, ≤) ◮ Every subset of Z is a chain. ◮ (a1, a2, . . . ) with ai =

  • if i even

1 else not ascending ◮ (a1, a2, . . . ) with ai = i ascending, but not stabilizing ◮ (a1, a2, . . . ) with ai = min(i, 10) ascending and stabilizing ◮ Is not a stabilizing ascending chain.

  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 59 / 100

slide-60
SLIDE 60

Height of Partial Order

Let (E, ⊑) be a partial order. ◮ (E, ⊑) has finite height if all chains are finite. ◮ (E, ⊑) has height h if all chains contain at most h + 1 elements and one chain contains h + 1 elements. Note: If E is finite than (E, ⊑) has finite height, but not vice versa. For example, (Z, =)

  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 60 / 100

slide-61
SLIDE 61

Heights of Example Partial Orders

PO finite height height (Z, ≤) × (Z, ≥) (Z, =)

  • (2Q, ⊆), Q finite
  • |Q|

(Σ∗, lexicographic order) (Σ∗, suffix)

  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 61 / 100

slide-62
SLIDE 62

Upper Bound (Join)

Let (E, ⊑) be a partial order.

Definition (Upper Bound)

An element e ∈ E is an upper bound of a subset Esub ⊆ E if ∀e′ ∈ Esub : e′ ⊑ e.

Definition (Least Upper Bound (lub))

An element e ∈ E is a least upper bound ⊔ of a subset Esub ⊆ E if ◮ e is an upper bound of Esub and ◮ for all upper bounds e′ of Esub it yields that e ⊑ e′.

  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 62 / 100

slide-63
SLIDE 63

Lower Bound (Meet)

Let (E, ⊑) be a partial order.

Definition (Lower Bound)

An element e ∈ E is an lower bound of a subset Esub ⊆ E if ∀e′ ∈ Esub : e ⊑ e′.

Definition (Greatest Lower Bound (glb))

An element e ∈ E is a greatest lower bound ⊓ of a subset Esub ⊆ E if ◮ e is a lower bound of Esub and ◮ for all lower bounds e′ of Esub it yields that e′ ⊑ e.

  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 63 / 100

slide-64
SLIDE 64

Computing Upper Bounds

PO subset ⊔ (lub) ⊓ (glb) (Z, ≤) {1, 4, 7} 7 1 (Z, ≤) Z × × (N, ≤) ∅ × (2Q, ⊆) 2Q (2Q, ⊆) {∅} (2Q, ⊆) Y ⊆ 2Q

  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 64 / 100

slide-65
SLIDE 65

Facts About Upper and Lower Bounds

  • 1. Least upper bounds and greatest lower bound do not

always exist. For example,

◮ (Z, ≤) ◮ (N, ≤) ◮ (N, ≥)

  • 2. The least upper bound and the greatest lower bound are

unique if they exists. (Proof on the board)

  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 65 / 100

slide-66
SLIDE 66

Lattice

Definition

A structure E = (E, ⊑, ⊔, ⊓, ⊤, ⊥) is a lattice if ◮ (E, ⊑) is a partial order ◮ least upper bound ⊔ and greater lower bound ⊓ exist for all subsets Esub ⊆ E ◮ ⊤ = ⊔E = ⊓∅ and ⊥ = ⊓E = ⊔∅ Note: For any set Q the structure (2Q, ⊆, ∪, ∩, Q, ∅) is a lattice.

  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 66 / 100

slide-67
SLIDE 67

Which Partial Orders Are Lattices?

(a) . . . (b) . . . (c) (d) (e) (f)

  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 67 / 100

slide-68
SLIDE 68

Flat-Lattice

Definition

A flat lattice of set Q consists of ◮ Extended set Q⊤

⊥ = Q ∪ {⊤, ⊥}

◮ Flat ordering ⊔, i.e. ∀q ∈ Q : ⊥ ⊑ q ⊑ ⊤ and ⊥ ⊑ ⊤ ◮ ⊔ =

    

⊥ X = ∅ ∨ X = {⊥} q X = {q} ∨ X = {⊥, q} ⊤ else ◮ ⊓ =

    

⊤ X = ∅ ∨ X = {⊤} q X = {q} ∨ X = {⊤, q} ⊥ else

⊤ . . . Q ⊥

  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 68 / 100

slide-69
SLIDE 69

Product Lattice

Let E1 = (E1, ⊑1, ⊔1, ⊓1, ⊤1, ⊥1) and E2 = (E2, ⊑2, ⊔2, ⊓2, ⊤2, ⊥2) be lattices. The product lattice E× = (E1 × E2, ⊑×, ⊔×, ⊓×, ⊤×, ⊥×) with

◮ (e1, e2) ⊑× (e′

1, e′ 2) if e1 ⊑1 e′ 1 ∧ e2 ⊑2 e′ 2

◮ ⊔×Esub = (⊔1{e1 | (e1, ·) ∈ Esub}, ⊔2{e2 | (·, e2) ∈ Esub}) ◮ ⊓×Esub = (⊓1{e1 | (e1, ·) ∈ Esub}, ⊓2{e2 | (·, e2) ∈ Esub}) ◮ ⊤× = (⊤1, ⊤2) and ⊥× = (⊥1, ⊥2)

is a lattice. Proof on the board.

  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 69 / 100

slide-70
SLIDE 70

Join-Semi-Lattice

Complete lattice always not required ⇒ remove unused elements

Definition

Join-Semi-Lattice A structure E = (E, ⊑, ⊔, ⊤) is a lattice if ◮ (E, ⊑) is a partial order ◮ least upper bound ⊔ exists for all subsets Esub ⊆ E ◮ ⊤ = ⊔E

  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 70 / 100

slide-71
SLIDE 71

Abstract Domain

Join-semi-lattice on set of abstract states + meaning of abstract states

Definition

An abstract domain D = (C, E, [ [ · ] ]) consists of ◮ a set C of concrete states ◮ a join-semi-lattice E = (E, ⊑, ⊔, ⊤) ◮ a concretization function [ [ · ] ] : E → 2C (assigns meaning of abstract states)

◮ [ [⊤] ] = C ◮ ∀Esub ⊆ E :

e∈Esub [

[e] ] ⊆ [ [ ⊔ Esub] ] (join operator overapproximates)

  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 71 / 100

slide-72
SLIDE 72

Abstraction

α : 2C → E Here: ◮ Not defined separately ◮ Returns smallest abstract state that covers set of concrete states

  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 72 / 100

slide-73
SLIDE 73

Galois Connection

Abstraction and concretization function fulfill the following connection

  • 1. ∀Csub ⊆ C : Csub ⊆ [

[α(Csub)] ] (abstraction safe approximation, but may loose information/precision)

  • 2. ∀e ∈ E : α([

[e] ]) ⊑ e (no loss in safety)

  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 73 / 100

slide-74
SLIDE 74

Abstract Semantics

Abstract interpretation of program, i.e., evaluation on abstract states Transfer relation ⊆ E × G × E ◮ ∀e ∈ E, g ∈ G :

  • c∈[

[e] ]{c′ | (c, g, c′) ∈ T } ⊆

  • (e,g,e′)∈ [

[e′] ] (safe over-approximation) ◮ Depends on abstract domain ◮ In this lecture: restricted to functions

  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 74 / 100

slide-75
SLIDE 75

Properties of Transfer Functions

◮ Monotony ∀e, e′ ∈ E, g ∈ G : e ⊑ e′ ⇒ (e, g) ⊑ (e′, g) ◮ Distributivity ∀e, e′ ∈ E, g ∈ G : (e, g)⊔ (e′, g) = (e ⊔ e′, g)

  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 75 / 100

slide-76
SLIDE 76

Elements of Abstraction (Recap.)

  • 1. Abstract domain

◮ Join-semi lattice E on set of abstract states E ◮ Meaning of abstract states [ [] ]

  • 2. Abstract semantics
  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 76 / 100

slide-77
SLIDE 77

Properties of Abstraction (Recap.)

◮ Join operator overapproximates ∀Esub ⊆ E :

  • e∈Esub

[ [e] ] ⊆ [ [ ⊔ Esub] ] ◮ Monotony of transfer relation ∀e, e′ ∈ E, g ∈ G : e ⊑ e′ ⇒ (e, g) ⊑ (e′, g) ◮ Distributivity of transfer relation ∀e, e′ ∈ E, g ∈ G : (e, g)⊔ (e′, g) = (e ⊔ e′, g)

  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 77 / 100

slide-78
SLIDE 78

Location Abstraction L

Tracks control-flow of program ◮ Uses flat lattice of set L of location states ◮ [ [ℓ] ] :=

    

C if ℓ = ⊤ ∅ if ℓ = ⊥ {c ∈ C | c(pc) = ℓ} else (guarantees that join overapproximates) ◮ (ℓ, (l, op, l′), ℓ′) ∈L if (ℓ = l ∨ ℓ = ⊤) and ℓ′ = l′

  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 78 / 100

slide-79
SLIDE 79

Properties of Location Abstraction

Transfer relation L ◮ overapproximates, i.e., ∀e ∈ EL, g ∈ G :

  • c∈[

[e] ]

{c′ | (c, g, c′) ∈ T } ⊆

  • (e,g,e′)∈L

[ [e′] ] ◮ monotone ◮ distributive

  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 79 / 100

slide-80
SLIDE 80

Property Encoding

Automaton observing violation of reachability property ϕLsub

qsafe qunsafe (·, ·, l) ∈ G ∧ l / ∈ Lsub g ∈ G (·, ·, l) ∈ G ∧ l ∈ Lsub

  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 80 / 100

slide-81
SLIDE 81

Property Abstraction R

Represent automaton encoding of property ϕLsub as abstraction ◮ Uses join-semilattice on set {qsafe, qunsafe} with qsafe ⊑ qunsafe ◮ [ [q] ] :=

  • C

if q = qunsafe {c ∈ C | c(pc) / ∈ Lsub} else ◮ (q, (l, op, l′), q′) ∈R if q′ = qunsafe ∧ l′ ∈ Lsub or q = q ∧ l′ / ∈ Lsub

  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 81 / 100

slide-82
SLIDE 82

Properties of Property Abstraction

Transfer relation R ◮ overapproximates ◮ monotone ◮ distributive

  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 82 / 100

slide-83
SLIDE 83

Variable Separate Domain

Assigns to each variable an abstract value from base domain B = (EB, ⊑B, ⊔B, ⊤B) ◮ E = {f : V ar → B} ◮ f ⊑ f ′ if ∀v ∈ V ar : f(v) ⊑B f ′(v) ◮ ⊔F = f ′′ with ∀v ∈ V ar : f ′′(v) = ⊔f∈Ff(v) ◮ [ [f] ] = {c | ∀v ∈ V ar : c(d)(v) ∈ [ [f(v)] ]B}

  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 83 / 100

slide-84
SLIDE 84

Value Abstraction V

Uses variable separate domain ◮ Base domain flat lattice of Z ◮ Abstract value ⊤ any value ◮ Transfer relation

◮ Assignment (f, (l, v := expr; , l′), f′) ∈V if ∀w ∈ V ar : v = w ⇒ f(w) = f′(w) and f′(v) =

    

⊤ if ∃w ∈ var(expr) : f(w) = ⊤ ⊥ if ∃w ∈ var(expr) : f(w) = ⊥ Sa(expr, f) else ◮ Assume (f, (l, expr, l′), f) ∈V if ∃w ∈ var(expr) : f(w) = ⊤ or ∀w ∈ var(expr) : f(w) ∈ Z ∧ Sb(expr, f)

  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 84 / 100

slide-85
SLIDE 85

Properties of Value Abstraction V

Transfer relation ◮ overapproximates ◮ monotone ◮ not distributive, e.g., f : x → 3; y → 2 f ′ : x → 2; y → 3 (f, x = x + y; )⊔ (f ′, x = x + y; ) : x → 5; y → ⊤, but (f ⊔ f ′, x = x + y; ) : x → ⊤; y → ⊤

  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 85 / 100

slide-86
SLIDE 86

Example Abstract Transitions

On the board: ◮ (i → ⊤; x → 3, (l, i = 1; , l′)) ◮ (i → ⊤; x → ⊤, (l, i = i ∗ 2; , l′)) ◮ (i → ⊤; x → 5, (l, i = i ∗ 2; , l′)) ◮ (i → 0; x → ⊤, (l, i&&x > 0, l′)) ◮ (i → ⊤; x → 10, (l, x > 10, l′))

  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 86 / 100

slide-87
SLIDE 87

Sign Abstraction

Variable separate domain using base domain ⊤ +- 0+

  • +

⊥ [ [⊤] ] = Z [ [ + ] ] = N+ [ [ − ] ] = Z \ N+ [ [0] ] = {0} [ [ + −] ] = Z \ {0} [ [0 + ] ] = N+ [ [ − 0] ] = Z \ N+ [ [⊥] ] = ∅

  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 87 / 100

slide-88
SLIDE 88

Transfer Relation of Sign Abstraction

Suggestion 1: ◮ (f, g) = f ′ with ∀v ∈ V ar : f ′(v) = ⊤ ◮ sound, but not useful

  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 88 / 100

slide-89
SLIDE 89

Transfer Relation of Sign Abstraction

Suggestion 2: ◮ Assume: (f, expr) = f ◮ Assignment: (f, expr) = f ′ v=const; f ′(v) =

    

+ const ∈ N+ const = 0 − else v=w; f ′(v) = f(v) v=expr; f ′(v) = ⊤ and ∀u ∈ V ar : u = v ⇒ f ′(u) = f(u) sound, but could be more precise

  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 89 / 100

slide-90
SLIDE 90

Transfer Relation of Sign Abstraction (Incomplete)

More precise for special boolean expression like var>0, var==0, var<0, var>=0, var<=0 ◮ can be decided ◮ used to restrict successor of assume expressions Abstract evaluation of arithmetic expressions, e.g. ◮ e + e = e, for any abstract value e except +- ◮ e + 0 = e ◮ e - 0 = e ◮ e*0 = 0 ◮ . . .

  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 90 / 100

slide-91
SLIDE 91

Interval Abstraction I

Variable separate domain based on interval domain ◮ E = Z2 ∪ {⊤, ⊥} ◮ ⊥ ⊑ e, e ⊑ ⊤ and [a, b] ⊑ [c, d] if c ≤ a ∧ b ≤ d ◮ ⊔Esub =

    

⊤ if ⊤ ∈ Esub ⊥ if Esub ⊆ {⊥} [min[a,b]∈Esuba, max[a,b]∈Esubb] else

◮ [ [[a, b]] ] = {x ∈ Z | a ≤ x ≤ b} [ [⊤] ] = Z [ [⊥] ] = ∅ Violates ascending chain condition.

  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 91 / 100

slide-92
SLIDE 92

Transfer Relation of Interval Abstraction

Relies on abstract evaluation of expressions in state f Arithmetic expressions ◮ const: [const,const] ◮ var: f(var) ◮ -[a,b]=[-b,-a] ◮

[a,b] opa [c,d] = [min(a opa c, b opa d), max(a opa c, b opa d)]

◮ special treatment of values ⊥, ⊤

  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 92 / 100

slide-93
SLIDE 93

Transfer Relation of Interval Abstraction

Relies on abstract evaluation of expressions in state f Boolean expression ◮

[a,b]=

    

{true} a > 0 ∨ b < 0 {false} a = b = 0 {true, false} else

[a,b]<[c,d]=

    

{true} b < c {false} a ≥ d {true, false} else

◮ other comparison operators similar ◮ . . . Define transfer relation analogous to transition

  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 93 / 100

slide-94
SLIDE 94

Cartesian Predicate Abstraction

Represent states by first order logic formulae ◮ Restricted to a set of predicates Pred (subset of boolean expressions without boolean connectors) ◮ Conjunction of predicates

  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 94 / 100

slide-95
SLIDE 95

Cartesian Predicate Abstraction

◮ Power set lattice on predicates (2Pred, ⊇, ∩, ∪, ∅, Pred) ◮ [ [⊤] ] = [ [∅] ] = C for p = ⊥: [ [p] ] = {c ∈ C | ∀pred ∈ p : Sb(pred, c(d)) = true} (guarantees that join overapproximates) ◮ Transfer relation

◮ Assignment (p, v = expr, p′) with

p′ =

  • t ∈ Pred
  • ∃v′ :

t′∈p t′[v → v′] ∧ v = expr[v → v′]

  • ⇒ t
  • ◮ Assume

(p, expr, p′) if

t∈p t ∧ expr is satisfiable and

p′ = {t ∈ Pred | (

t′∈p t′ ∧ expr) ⇒ t}

  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 95 / 100

slide-96
SLIDE 96

Properties of Cartesian Predicate Abstraction

Transfer relation ◮ overapproximates ◮ monotone ◮ not distributive (e.g., use value abstraction example and value assignments as predicates)

  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 96 / 100

slide-97
SLIDE 97

Example Abstract Transitions

Consider set of predicates {i>0, x=10} On the board: ◮ ({x = 10}, (l, i = 1; , l′)) ◮ ({i > 0}, (l, i = i ∗ 2; , l′)) ◮ ({i > 0}, (l, i < abs, l′)) ◮ ({x = 10, i > 0}, (l, x > 10, l′))

  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 97 / 100

slide-98
SLIDE 98

Composite Abstraction

Combines two abstractions ◮ Product (join-semi) lattice E1 × E2 ◮ [ [(e1, e2)] ] = [ [e1] ]1 ∩ [ [e2] ]2 ◮ Product transfer relation ((e1, e2), g, (e′

1, e′ 2)) ∈

if (e1, g, e′

1) ∈1 and (e2, g, e′ 2) ∈2

◮ More precise transfer relations possible

  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 98 / 100

slide-99
SLIDE 99

Properties of Composite Abstraction

Properties inherited from components Transfer relation ◮ overapproximates ◮ monotone ◮ distributive if respective property is fulfilled by both components. Proof on the board

  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 99 / 100

slide-100
SLIDE 100

Two Prominent Combination

◮ Value analysis L × V × R ◮ Predicate analysis L × P × R

  • Prof. Dr. Dirk Beyer

SoSy-Lab, LMU Munich, Germany 100 / 100