3-Valued Logic Analyzer (TVP) Part II Tal Lev-Ami and Mooly Sagiv.

Slides:



Advertisements
Similar presentations
Automated Theorem Proving Lecture 1. Program verification is undecidable! Given program P and specification S, does P satisfy S?
Advertisements

Singly linked lists Doubly linked lists
Abstract Interpretation Part II
1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson.
Semantics Static semantics Dynamic semantics attribute grammars
Interprocedural Shape Analysis for Recursive Programs Noam Rinetzky Mooly Sagiv.
3-Valued Logic Analyzer (TVP) Tal Lev-Ami and Mooly Sagiv.
1/20 Generalized Symbolic Execution for Model Checking and Testing Charngki PSWLAB Generalized Symbolic Execution for Model Checking and Testing.
1 Lecture 07 – Shape Analysis Eran Yahav. Previously  LFP computation and join-over-all-paths  Inter-procedural analysis  call-string approach  functional.
1 Lecture 08(a) – Shape Analysis – continued Lecture 08(b) – Typestate Verification Lecture 08(c) – Predicate Abstraction Eran Yahav.
1 Operational Semantics Mooly Sagiv Tel Aviv University Textbook: Semantics with Applications.
Shape Analysis via 3-Valued Logic Mooly Sagiv Tel Aviv University
Programming Language Semantics Mooly SagivEran Yahav Schrirber 317Open space html://
Finite Differencing of Logical Formulas for Static Analysis Thomas Reps University of Wisconsin Joint work with M. Sagiv and A. Loginov.
Denotational Semantics Syntax-directed approach, generalization of attribute grammars: –Define context-free abstract syntax –Specify syntactic categories.
Control Flow Analysis Mooly Sagiv Tel Aviv University Sunday Scrieber 8 Monday Schrieber.
1 Motivation Dynamically allocated storage and pointers are an essential programming tools –Object oriented –Modularity –Data structure But –Error prone.
1 Iterative Program Analysis Part I Mooly Sagiv Tel Aviv University Textbook: Principles of Program.
1 Control Flow Analysis Mooly Sagiv Tel Aviv University Textbook Chapter 3
1 Eran Yahav and Mooly Sagiv School of Computer Science Tel-Aviv University Verifying Safety Properties.
Programming Language Semantics Mooly SagivEran Yahav Schrirber 317Open space html://
Abstract Interpretation Part I Mooly Sagiv Textbook: Chapter 4.
Finding the Weakest Characterization of Erroneous Inputs Dzintars Avots and Benjamin Livshits.
Semantics with Applications Mooly Sagiv Schrirber html:// Textbooks:Winskel The.
Detecting Memory Errors using Compile Time Techniques Nurit Dor Mooly Sagiv Tel-Aviv University.
1 Program Analysis Systematic Domain Design Mooly Sagiv Tel Aviv University Textbook: Principles.
Operational Semantics Semantics with Applications Chapter 2 H. Nielson and F. Nielson
1 Shape Analysis via 3-Valued Logic Mooly Sagiv Tel Aviv University Shape analysis with applications Chapter 4.6
1 ES 314 Advanced Programming Lec 2 Sept 3 Goals: Complete the discussion of problem Review of C++ Object-oriented design Arrays and pointers.
Program Analysis Mooly Sagiv Tel Aviv University Sunday Scrieber 8 Monday Schrieber.
Static Program Analysis via Three-Valued Logic Thomas Reps University of Wisconsin Joint work with M. Sagiv (Tel Aviv) and R. Wilhelm (U. Saarlandes)
1 Program Analysis Mooly Sagiv Tel Aviv University Textbook: Principles of Program Analysis.
1 Tentative Schedule u Today: Theory of abstract interpretation u May 5 Procedures u May 15, Orna Grumberg u May 12 Yom Hatzamaut u May.
1/25 Pointer Logic Changki PSWLAB Pointer Logic Daniel Kroening and Ofer Strichman Decision Procedure.
Dagstuhl Seminar "Applied Deductive Verification" November Symbolically Computing Most-Precise Abstract Operations for Shape.
Program Analysis and Verification Noam Rinetzky Lecture 10: Shape Analysis 1 Slides credit: Roman Manevich, Mooly Sagiv, Eran Yahav.
1 Iterative Program Analysis Abstract Interpretation Mooly Sagiv Tel Aviv University Textbook:
T. Lev-Ami, R. Manevich, M. Sagiv TVLA: A System for Generating Abstract Interpreters A. Loginov, G. Ramalingam, E. Yahav.
1 Automatic Refinement and Vacuity Detection for Symbolic Trajectory Evaluation Orna Grumberg Technion Haifa, Israel Joint work with Rachel Tzoref.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
Shape Analysis via 3-Valued Logic Mooly Sagiv Thomas Reps Reinhard Wilhelm
(1 - 1) Introduction to C Data Structures & Abstract Data Types Instructor - Andrew S. O’Fallon CptS 122 (August 26, 2015) Washington State University.
Symbolically Computing Most-Precise Abstract Operations for Shape Analysis Greta Yorsh Thomas Reps Mooly Sagiv Tel Aviv University University of Wisconsin.
Symbolic Execution with Abstract Subsumption Checking Saswat Anand College of Computing, Georgia Institute of Technology Corina Păsăreanu QSS, NASA Ames.
1 Shape Analysis via 3-Valued Logic Mooly Sagiv Tel Aviv University Shape analysis with applications Chapter 4.6
Nikolaj Bjørner Microsoft Research DTU Winter course January 2 nd 2012 Organized by Flemming Nielson & Hanne Riis Nielson.
1 First order theories (Chapter 1, Sections 1.4 – 1.5) From the slides for the book “Decision procedures” by D.Kroening and O.Strichman.
Data Structures and Algorithms for Efficient Shape Analysis by Roman Manevich Prepared under the supervision of Dr. Shmuel (Mooly) Sagiv.
Operational Semantics Mooly Sagiv Tel Aviv University Textbook: Semantics with Applications Chapter.
1 Program Analysis via 3-Valued Logic Mooly Sagiv, Tal Lev-Ami, Roman Manevich Tel Aviv University Thomas Reps, University of Wisconsin, Madison Reinhard.
Program Analysis via 3-Valued Logic Thomas Reps University of Wisconsin Joint work with Mooly Sagiv and Reinhard Wilhelm.
1 Iterative Program Analysis Abstract Interpretation Mooly Sagiv Tel Aviv University Textbook:
1 Numeric Abstract Domains Mooly Sagiv Tel Aviv University Adapted from Antoine Mine.
Operational Semantics Mooly Sagiv Tel Aviv University Sunday Scrieber 8 Monday Schrieber.
CS5205Semantics1 CS5205: Foundation in Programming Languages Semantics Static Semantics Dynamic Semantics Operational Semantics Big-step Small-Step Denotational.
Operational Semantics Mooly Sagiv Reference: Semantics with Applications Chapter 2 H. Nielson and F. Nielson
Putting Static Analysis to Work for Verification A Case Study Tal Lev-Ami Thomas Reps Mooly Sagiv Reinhard Wilhelm.
Operational Semantics Mooly Sagiv Reference: Semantics with Applications Chapter 2 H. Nielson and F. Nielson
Records type city is record -- Ada Name: String (1..10); Country : String (1..20); Population: integer; Capital : Boolean; end record; struct city { --
Textbook: Principles of Program Analysis
Partially Disjunctive Heap Abstraction
Compactly Representing First-Order Structures for Static Analysis
Spring 2016 Program Analysis and Verification
Program Analysis and Verification
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Graph-Based Operational Semantics
Parametric Shape Analysis via 3-Valued Logic
Parametric Shape Analysis via 3-Valued Logic
Symbolic Characterization of Heap Abstractions
(1 - 2) Introduction to C Data Structures & Abstract Data Types
Presentation transcript:

3-Valued Logic Analyzer (TVP) Part II Tal Lev-Ami and Mooly Sagiv

Outline u The Shape Analysis Problem u Solving Shape Analysis with TVLA –Structural Operational Semantics –Predicate logic –Embedding –(Imprecise) Abstract Interpretation –Instrumentation Predicates –Focus –Coerce u Bibliography

Shape Analysis u Determine the possible shapes of a dynamically allocated data structure at given program point u Relevant questions: –Does a variable point to an acyclic list? –Does a variable point to a doubly-linked list? –Does a variable point p to an allocated element every time p is dereferenced? –Can a procedure create a memory-leak

Dereference of NULL pointers typedef struct element { int value; struct element *next; } Elements bool search(int value, Elements *c) { Elements *elem; for ( elem = c; c != NULL;elem = elem->next;) if (elem->val == value) return TRUE; return FALSE NULL dereference

Memory leakage Elements* reverse(Elements *c) { Elements *h,*g; h = NULL; while (c!= NULL) { g = c->next; h = c; c->next = h; c = g; } return h; leakage of address pointed-by h

The SWhile Programming Language Abstract Syntax a := x | x.sel | null | n | a 1 op a a 2 b := true | false | not b | b 1 op b b 2 | a 1 op r a 2 S := [x := a] l | [x.sel := a] l | [x := malloc()] l | [skip] l | S 1 ; S 2 | if [b] l then S 1 else S 2 | while [b] l do S sel:= car | cdr

Dereference of NULL pointers [elem := c;] 1 [found := false;] 2 while ([c != null] 3 && [!found] 4 ) ( if ([elem->car= value] 5 ) then [found := true] 6 else [elem = elem->cdr] 7 ) NULL dereference

Structural Operational Semantics for languages with dynamically allocated objects u The program state consists of: –current allocated objects –a mapping from variables into atoms, objects, and null –a car mapping from objects into atoms, objects, and null –a cdr mapping from objects into atoms, objects, and null –…–… u malloc() allocates more objects u assignments update the state

Structural Operational Semantics u The program state S=(O, env, car, cdr): –current allocated objects O –atoms (integers, Booleans) A –env: Var *  A  O  {null} –car: A  A  O  {null} –cdr: A  A  O  {null} u The meaning of expressions A  a  : S  A  O  {null} –A  at  (s) = at –A  x  ((O, env, car, cdr)) = env(x) –A  x.car  ((O, env, car, cdr)) = car(env(x)) –A  x.cdr  ((O, env, car, cdr)) = cdr(env(x))

Structural Semantics for SWhile axioms [ass v sos ]  (O, e[x  A  a  s], car, cdr) [ass car sos ]  (O, e, car[e(x)  A  a  s], cdr) [ass cdr sos ]  (O, e, car, cdr[e(x)  A  a  s]) [skip sos ]  s [ass m sos ]  (O  {n}, e[x  n], car, cdr) where n  O

Structural Semantics for SWhile rules [comp 1 sos ]   [comp 2 sos ]  s’  [if tt sos ]  if B  b  s=tt [if ff sos ]  if B  b  s=ff

Summary u The SOS is natural u Can handle: –errors, e.g., null dereferences –free –garbage collection u But does not lead to an analysis –The set of potential objects is unbound u Solution: Three-Valued Kleene Predicate Logic

Predicate Logic u Vocabulary –A finite set of predicate symbols P each with a fixed arity –A finite set of function symbols u Logical Structures S provide meaning for predicates –A set of individuals (nodes) U –P S : U S  {0, 1} u First-Order Formulas over  express logical structure properties

Using Predicate Logic to describe states in SOS u U=O u For a Boolean variable x define a nullary predicate (proposition) b[x] –b[x] = 1 when env(x)=1 u For a pointer variable x define a unary predicate –p[x](u)=1 when env(x)=u and u is an object u Two binary predicates: –s[car](u1, u2) = 1 when car(u1)=u2 and u2 is object –s[cdr](u1, u2) = 1 when cdr(u1)=u2 and u2 is object

Running Example [elem := c;] 1 [found := false;] 2 while ([c != null] 3 && [!found] 4 ) ( if ([elem->car= value] 5 ) then [found := true] 6 else [elem = elem->cdr] 7 )

%s Pvar {elem, c} %s Bvar {found} %s Sel {car, cdr} #include "pred.tvp" % #include "cond.tvp" #include "stat.tvp" % /* [elem := c;] 1 */ l_1 Copy_Var(elem, c) l_2 /* [found := false;] 2 */ l_2 Set_False(found) l_3 /* while ([c != null] 3 && [!found] 4 ) ( */ l_3 Is_Not_Null_Var (c) l_4 l_3 Is_Null_Var (c) l_end l_4 Is_False(found) l_5 l_4 Is_True(found) l_end /*if ([elem->car= value] 5 ) */ l_5 Uninterpreted_Cond() l_6 l_5 Uninterpreted_Cond() l_7 /*then [found := true] 6 */l_6 Set_True(found) l_3 /*else [elem = elem->cdr] 7 */ l_7 Get_Sel(cdr, elem, elem) l_3 /* ) */ % l_1, l_end

foreach (z in Bvar) { %p b[z]() } pred.tvp foreach (z in Pvar) { %p p[z](v) unique box } foreach (sel in Sel) { %p s[sel](v1, v2) function }

Actions u Use first order formulae over  to express the SOS u Every action can have: –title %t –focus formula %f –precondition formula %p –error messages %message –new formula %new –predicate-update formulas {} –retain formula

cond.tvp (part 1) %action Uninterpreted_Cond() { %t "uninterpreted-Condition" } %action Is_True(x1) { %t x1 %p b[x1]() { b[x1]() = 1 } %action Is_False(x1) { %t "!" + x1 %p !b[x1]() { b[x1]() = 0 }

cond.tvp (part 2) %action Is_Not_Null_Var(x1) { %t x1 + " != null" %p E(v) p[x1](v) } %action Is_Null_Var(x1) { %t x1 + " = null" %p !(E(v) p[x1](v)) }

stat.tvp (part 1) %action Skip() { %t "Skip" } %action Set_True(x1) { %t x1 + " := true" { b[x1]() = 1 } %action Set_False(x1) { %t x1 + " := false" { b[x1]() = 0 }

stat.tvp (part 2) %action Copy_Var(x1, x2) { %t x1 + " := " + x2 { p[x1](v) = p[x2](v) }

stat.tvp (part 3) %action Get_Sel(sel, x1, x2) { %t x1 + " := " + x2 + “.” + sel %message (!E(v) p[x2](v)) -> "an illegal dereference to" + sel + " component of " + x2 { p[x1](v) = E(v_1) p[x2](v_1) & s[sel](v_1, v) }

stat.tvp (part 4) %action Set_Sel_Null(x1, sel) { %t x1 + "." + sel + " := null" %message (!E(v) p[x1](v)) -> "an illegal dereference to" + sel + " component of " + x1 { s[sel](v_1, v_2) = s[sel](v_1, v_2) & !p[x1](v_1) }

stat.tvp (part 5) %action Set_Sel(x1, sel, x2) { %t x1 + “.” + sel + " := " + x2 %message (E(v, v1) p[x1](v) & s[sel](v, v1)) -> "Internal Error! assume that " + x1 + "." + sel + ==NULL" %message (!E(v) p[x1](v)) -> "an illegal dereference to" + sel + " component of " + x1 { s[sel](v_1, v_2) = s[sel](v_1, v_2) | p[x1](v_1) & p[x2](v_2) }

stat.tvp (part 6) %action Malloc(x1) { %t x1 + " := malloc()" %new { p[x1](v) = isNew(v) }

3-Valued Kleene Logic u A logic with 3-values –0 -false –1 - true –1/2 - don’t know u Operators are conservatively interpreted –1/2 means either true or false 01 1/2 Logical order information order 0  1=1/2

Kleene Interpretation of Operators (logical-and)

Kleene Interpretation of Operators (logical-or)

Kleene Interpretation of Operators (logical-negation)

Kleene Interpretation of Operators (logical-implication)

3-Valued Predicate Logic u Vocabulary –A finite set of predicate symbols P –A special unary predicate sm »sm(u)=0 when u represents a unique concrete node »sm(u)=1/2 when u may represent more than one concrete node u 3-valued Logical Structures S provide meaning for predicates –A (bounded) set of individuals (nodes) U –P S : U S  {0, 1/2, 1} u First-Order Formulas over  express logical structure properties u Interpret  as maximum on logical order

The Blur Operation u Abstract an arbitrary structure into a structure of bounded size u Select a set of unary predicates as abstraction-predicates u Map all the nodes with the same value of abstraction predicates into a single summary node u Join the values of other predicates

The Embedding Theorem u If a big structure B can be embedded in a structure S via a surjective (onto) function f such that all predicate values are preserved, i.e., p B (u 1,.., u k )  p S (f(u 1 ),..., f(u k )) u Then, every formula  is preserved  is preserved –  = 1 in S   = 1 in B –  =0 in S   =0 in B –  = 1/2 in S  don’t know

Naive Program Analysis via 3-valued predicate logic u Chaotic iterations u Start with the initial 3-valued structure u Execute every action in three phases: –check if precondition is satisfied –execute update formulas –execute blur –Command line tvla prgm prgm -action pub

prgm.tvs %n = {u, u0} %p = { sm = {u:1/2} s[cdr] = {u->u:1/2, u0->u:1/2} p[c] = {u0} }

More Precise Shape Analysis u Distinguish between cyclic and acyclic lists u Use Focus to guarantee that important formulas do not evaluate to 1/2 u Use Coerce to maintain global invariants u It all works –Singly linked lists (reverse, insert, delete, del_all) –Sortedness (bubble-sort, insetion-sort, reverse) –Doubly linked lists (insert, delete –Mobile code (router) –Java multithreading (interference, concurrent-queue)

The Instrumentation Principle u Increase precision by storing the truth- value of some designated formulae u Introduce predicate-update formulae to update the extra predicates

is = 0 Example: Heap Sharing  x is[cdr](v) =  v1,v2: cdr(v1,v)  cdr(v2,v)  v1  v2 u1u1 u x u1u1 u x is = 0

Example: Heap Sharing  x is[cdr](v) =  v1,v2: cdr(v1,v)  cdr(v2,v)  v1  v2 u1u1 u x u1u1 u x is = 0 is = 1

foreach (z in Bvar) { %p b[z]() } pred.tvp foreach (z in Pvar) { %p p[z](v) unique box } foreach (sel in Sel) { %p s[sel](v1, v2) function } foreach (sel in Sel) { %i is[sel](v) = E(v1, v2) sel(v_1) & sel(v2, v) & v_1 != v_2 }

stat.tvp (part 4) %action Set_Sel_Null(x1, sel) { %t x1 + "." + sel + " := null" %message (!E(v) p[x1](v)) -> "an illegal dereference to" + sel + " component of " + x1 { s[sel](v_1, v_2) = s[sel](v_1, v_2) & !p[x1](v_1) is[sel](v) = is(v) & (!(E(v_1) x1(v_1) & sel(v_1, v)) | E(v_1, v_2) v_1 != v_2 & (sel(v_1, v) & !x1(v_1)) & (sel(v_2, v) & !x1(v_2))) }

stat.tvp (part 5) %action Set_Sel(x1, sel, x2) { %t x1 + “.” + sel + " := " + x2 %message (E(v, v1) p[x1](v) & s[sel](v, v1)) -> "Internal Error! assume that " + x1 + "." + sel + ==NULL" %message (!E(v) p[x1](v)) -> "an illegal dereference to" + sel + " component of " + x1 { s[sel](v_1, v_2) = s[sel](v_1, v_2) | p[x1](v_1) & p[x2](v_2) is[sel](v) = is[sel](v) | E(v_1) x2(v) & sel(v_1, v) }

u reachable-from-variable-x(v)  v1:x(v1)  cdr*(v1,v) u cyclic-along-dimension-d(v) cdr+(v, v) u ordered element inOrder(v)  v1:cdr(v, v_1)  v->d d u doubly linked lists Additional Instrumentation Predicates

The Focusing Principle u To increase precision –“Bring the predicate-update formula into focus” (Force 1/2 to 0 or 1) –Then apply the predicate-update formulas

(1) Focus on  v 1 : x(v 1 )  cdr(v 1,v) u1u1 x y u xy u1u1 u xy y u1u1 u.1 x  u1u1 u.0 u

x(v) =  v 1 : x(v 1 )  cdr(v 1,v) (2) Evaluate Predicate-Update Formulae xy u1u1 u xy y u1u1 u.1 x  u1u1 u.0 u u1u1 u x u1u1 u.1 x u.0 y x y u1u1 u

The Coercion Principle u Increase precision by exploiting some structural properties possessed by all stores (Global invariants) u Structural properties captured by constraints u Apply a constraint solver

(3) Apply Constraint Solver u1u1 u x u1u1 u.1 x u.0 y x y u1u1 u u1u1 u x x y u1u1 u u1u1 u.1 x u.0 y

Conclusion u TVLA allows construction of non trivial analyses u But it is no panacea –Expressing operational semantics using logical formulas is not always easy –Need instrumentation to be reasonably precise (sometimes help efficiency as well) u Open problems: –A debugger for TVLA –Frontends –Algorithmic problems: »Space optimizations

Bibliography u Chapter 2.6 u (Invited talk CC’2000) u Parametric Shape Analysis based on 3-valued logics (the general theory) u The system and its applications