Synthesis, Analysis, and Verification Lecture 05a Lectures: Viktor Kuncak Programs with Data Structures: Assertions for Accesses. Dynamic Allocation.

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Automated Theorem Proving Lecture 1. Program verification is undecidable! Given program P and specification S, does P satisfy S?
CS3012: Formal Languages and Compilers Static Analysis the last of the analysis phases of compilation type checking - is an operator applied to an incompatible.
Synthesis, Analysis, and Verification Lecture 13 Dynamic Allocation.
CPSC 388 – Compiler Design and Construction
Synthesis, Analysis, and Verification Lecture 04c Lectures: Viktor Kuncak VC Generation for Programs with Data Structures “Beyond Integers”
Automated Verification with HIP and SLEEK Asankhaya Sharma.
Semantics Static semantics Dynamic semantics attribute grammars
Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
ICE1341 Programming Languages Spring 2005 Lecture #6 Lecture #6 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
Programming Languages and Paradigms
Compilation 2011 Static Analysis Johnni Winther Michael I. Schwartzbach Aarhus University.
Programming Paradigms Introduction. 6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L1:
Rigorous Software Development CSCI-GA Instructor: Thomas Wies Spring 2012 Lecture 11.
1/20 Generalized Symbolic Execution for Model Checking and Testing Charngki PSWLAB Generalized Symbolic Execution for Model Checking and Testing.
PSUCS322 HM 1 Languages and Compiler Design II IR Code Generation I Material provided by Prof. Jingke Li Stolen with pride and modified by Herb Mayer PSU.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 7: References and Assignment.
This Week Finish relational semantics Hoare logic Interlude on one-point rule Building formulas from programs.
Lecture 16 Subroutine Calls and Parameter Passing Semantics Dragon: Sec. 7.5 Fischer: Sec Procedure declaration procedure p( a, b : integer, f :
Chapter 7:: Data Types Programming Language Pragmatics
David Evans CS655: Programming Languages University of Virginia Computer Science Lecture 19: Minding Ps & Qs: Axiomatic.
ISBN Chapter 3 Describing Syntax and Semantics.
1 Semantic Description of Programming languages. 2 Static versus Dynamic Semantics n Static Semantics represents legal forms of programs that cannot be.
CS 355 – Programming Languages
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
ALGOL 60 Design by committee of computer scientists: Naur, Backus, Bauer, McCarthy, van Wijngaarden, Landin, etc. Design by committee of computer scientists:
CS 61C L4 Structs (1) A Carle, Summer 2005 © UCB inst.eecs.berkeley.edu/~cs61c/su05 CS61C : Machine Structures Lecture #4: Strings & Structs
Using and Building an Automatic Program Verifier K. Rustan M. Leino Research in Software Engineering (RiSE) Microsoft Research, Redmond Lecture 5 LASER.
Describing Syntax and Semantics
Floyd Hoare Logic. Semantics A programming language specification consists of a syntactic description and a semantic description. Syntactic description:symbols.
Cs164 Prof. Bodik, Fall Symbol Tables and Static Checks Lecture 14.
1/25 Pointer Logic Changki PSWLAB Pointer Logic Daniel Kroening and Ofer Strichman Decision Procedure.
272: Software Engineering Fall 2012 Instructor: Tevfik Bultan Lecture 4: SMT-based Bounded Model Checking of Concurrent Software.
Symbol Table (  ) Contents Map identifiers to the symbol with relevant information about the identifier All information is derived from syntax tree -
Software Engineering Prof. Dr. Bertrand Meyer March 2007 – June 2007 Chair of Software Engineering Static program checking and verification Slides: Based.
1 Inference Rules and Proofs (Z); Program Specification and Verification Inference Rules and Proofs (Z); Program Specification and Verification.
Compiler Construction
Extended Static Checking for Java  ESC/Java finds common errors in Java programs: null dereferences, array index bounds errors, type cast errors, race.
ISBN Chapter 3 Describing Semantics -Attribute Grammars -Dynamic Semantics.
Synthesis, Analysis, and Verification Lecture 05b Lectures: Viktor Kuncak Dynamic Allocation Linked Structures and Their Properties WS1S.
Elements of Computing Systems, Nisan & Schocken, MIT Press, Chapter 1: Compiler II: Code Generation slide 1www.nand2tetris.org Building.
Elements of Computing Systems, Nisan & Schocken, MIT Press, Chapter 11: Compiler II: Code Generation slide 1www.nand2tetris.org Building.
CS 363 Comparative Programming Languages Semantics.
Lists II. List ADT When using an array-based implementation of the List ADT we encounter two problems; 1. Overflow 2. Wasted Space These limitations are.
Synthesis, Analysis, and Verification Lecture 12 Verifying Programs that have Data Structures.
Application: Correctness of Algorithms Lecture 22 Section 4.5 Fri, Feb 18, 2005.
Chapter 3 Part II Describing Syntax and Semantics.
Semantics In Text: Chapter 3.
Principle of Programming Lanugages 3: Compilation of statements Statements in C Assertion Hoare logic Department of Information Science and Engineering.
K. Rustan M. Leino Research in Software Engineering (RiSE) Microsoft Research, Redmond, WA part 2 International Summer School Marktoberdorf Marktoberdorf,
Static Techniques for V&V. Hierarchy of V&V techniques Static Analysis V&V Dynamic Techniques Model Checking Simulation Symbolic Execution Testing Informal.
This Week Lecture on relational semantics Exercises on logic and relations Labs on using Isabelle to do proofs.
Python Programming Lecture II. Data Data is the raw material that programs manipulate. Data comes in different flavours. The basic ones are called “primitive.
1 Proving program termination Lecture 5 · February 4 th, 2008 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: A.
Extended Static Checking for Java Cormac Flanagan Joint work with: Rustan Leino, Mark Lillibridge, Greg Nelson, Jim Saxe, and Raymie Stata Compaq Systems.
Pointers1 WHAT IS A POINTER? Simply stated, a pointer is an address. A running program consists of three parts: execution stack, code, and data. They are.
Java & C++ Comparisons How important are classes and objects?? What mechanisms exist for input and output?? Are references and pointers the same thing??
Type soundness In a more formal way. Proving Soundness of Type Systems Goal of a sound type system: –if the program type checks, then it never “crashes”
/ PSWLAB Evidence-Based Analysis and Inferring Preconditions for Bug Detection By D. Brand, M. Buss, V. C. Sreedhar published in ICSM 2007.
1/20 Arrays Changki PSWLAB Arrays Daniel Kroening and Ofer Strichman Decision Procedure.
C HAPTER 3 Describing Syntax and Semantics. D YNAMIC S EMANTICS Describing syntax is relatively simple There is no single widely acceptable notation or.
Prof. Necula CS 164 Lecture 171 Operational Semantics of Cool ICOM 4029 Lecture 10.
1 Compiler Construction Run-time Environments,. 2 Run-Time Environments (Chapter 7) Continued: Access to No-local Names.
Arrays An array is a sequence of objects all of which have the same type. The objects are called the elements of the array and are numbered consecutively.
Further with Hoare Logic Sections 6.12, 6.10, 6.13
CS 153: Concepts of Compiler Design October 5 Class Meeting
Reasoning About Code.
Reasoning about code CSE 331 University of Washington.
Program correctness Axiomatic semantics
SPL – PS2 C++ Memory Handling.
Presentation transcript:

Synthesis, Analysis, and Verification Lecture 05a Lectures: Viktor Kuncak Programs with Data Structures: Assertions for Accesses. Dynamic Allocation

VCG for Real Languages Programs that Manipulate Integers, Maps, Arrays, and Linked Data Structures Compute Formulas from Programs have more operations in expressions of x=E Formulas with Integer Variables and Operations, as well as variables and operations on functions Prover (Integer Constraint Solver) + provers for function symbols, mathematical arrays, term algebras,...

Subtlety of Array Assignment Rule for wp of assignment of expression E to variable x, for postcondition P: wp(x=E, P) = Example: wp(x=y+1,x > 5) = wp of assignment to an array cell: wp(a[i]=y+1, a[i]>5) = wp(a[i]=y+1, a[i]>5 && a[j]>3) =

Arrays as Mathematical Functions Suppose we have expressions that manipulate functions. Array update operator on functions: f(x:=v) = g means: 1) g(x)=v, and 2) g(y)=f(y) for y != x. How to represent assignments? x = a[i]  x = a(i) a[i]=v 

Now we can handle static arrays wp(x=E, P) =

Example with Static Arrays if (a[i] > 0) { b[k]= b[k] + a[i]; i= i + 1; k = k + 1; } else { b[k] = b[k] + a[j]; j= j + 1; k = k – 1; }

Example with Static Arrays (assume(a(i) > 0); b= b(k:= b(k)+ a(i)); i= i + 1; k = k + 1;) [] (assume(a(i)<=0); b= b(k:= b(k)+ a(j)); j= j + 1; k = k – 1; ) guarded commands: formula

Array Bounds Checks: Index >= 0 if (a[i] > 0) { b[k]= b[k] + a[i]; i= i + 1; k = k + 1; } else { b[k] = b[k] + a[j]; j= j + 1; k = k – 1; } assert(i >= 0) (assume(a(i) > 0); assert b= b(k:= b(k)+ a(i)); i= i + 1; k = k + 1;) [] (assume(a(i)<=0); assert b= b(k:= b(k)+ a(j)); j= j + 1; k = k – 1; )

How to model “index not too large”? (for statically allocated arrays) const M = 100 const N = 2*M int a[N], b[N];... if (a[i] > 0) { b[k]= b[k] + a[i]; i= i + 1; k = k + 1; } assert (assume(a(i) > 0); assert b= b(k:= b(k)+ a(i)); i= i + 1; k = k + 1;) [] (assume(a(i)<=0))

Guarded Command Translation of Array Manipulations - with Bounds Checks x= a[i]  a[i] = y  assert(0 <= i); assert(i < a_size); x = a(i); assert(0 <= i);...

Checking Assertions const M = 100; const N = 2*M; int a[N], b[N]; i = -1; while (i < N) { i= i + 1; if (a[i] > 0) { k = k + 1; b[k]= b[k] + a[i]; } 1.Translate to guarded commands 2.Find a loop invariant and prove it inductive 3.Show that the invariant implies assertions

Checking Assertions (II)

Semantics of Assert using Error

assume and assert assume(E); assert(E) - never crashes assert(E); assume(E) - crashes if E does not hold This makes semantics of assert subtle

Arrays in GC Language are Values We always update entire arrays Copy semantics! guarded commands: b= b(0:=100); assert(b(0)==100); original program b[0]=100; assert(b(0)==100);

Arrays in GC Language are Values We always update entire arrays Copy semantics! guarded commands: b= b(0:=100); assert(b(0)==100); ok a= b; // copy a= a(0:=200); assert(b(0)==100); ok corresponds to Scala maps var a = Map[Int,Int]() var b = Map[Int,Int]() b= b + (0 -> 100) assert(b(0)==100); ok a= b // f. updates will copy a= a + (0 -> 200) assert(b(0)==100); ok

Mutable Arrays are by Reference Java (also Scala arrays and mutable maps): b[0]= 100; assert(b[0]==100); a= b; // make references point to same array a[0]= 200; assert(b[0]==100); // fails, b[0]==a[0]==200

To model Java Arrays, we first examine how to model objects in general

Reference Fields class Node { Node next; } How to model ‘next’ field? y = x.next;  x.next = y; 

Each Field Becomes Function Each Field assignment becomes Function Update radius : Circle -> int center : Circle -> Point  this.radius = this.radius * 2  class Circle { int radius; Point center; void grow() { radius = radius * 2; } } radius= radius(this:= radius(this)*2)

Field Manipulations with Checks x=y.f  y.f = x  x.f.f= z.f + y.f.f.f ;  assert x= f(y) assert f=

All Arrays of Given Result Become One Class Array Assignment Updates Given Array at Given Index length : Array -> int data : Array -> (Int -> Int) or simply: Array x Int -> Int  a.data[i] = x  data= data( (a,i):= x) class Array { int length; data : int[] } a[i] = x

Assignments to Java arrays: Now including All Assertions (safety ensured, or your models back) length : Array -> int data : Array -> (Int -> Int) or simply: Array x Int -> Int  assert assert data= data( (a,i):= x)  class Array { int length; data : int[] } a[i] = x y = a[i]

A Detour into the Dark Side

Can this assertion fail in C++ (or Pascal)? x= 4; y= 5; assert(x==4); Variables in C and Assembly void funny(int& x, int& y) { } int z; funny(z, z);

Memory Model in C Just one global array of locations: mem : int  int// one big array each variable x has address in memory, xAddr, which is &x We map operations to operations on this array: int x; int y; int* p; y= x  mem[yAddr]= mem[xAddr] x=y+z  mem[xAddr]= mem[yAddr] + mem[zAddr] y = *p  mem[yAddr]= mem[mem[pAddr]] p = &x  mem[pAddr] = xAddr *p = x  mem[mem[pAddr]]= mem[xAddr]

Can this assertion fail in C++ (or Pascal)? Variables in C and Assembly void funny(int& x, int& y) { x= 4; y= 5; assert(x==4); } int z; funny(&z, &z); void funny(xAddr, yAddr) { mem[xAddr]= 4; mem[yAddr]= 5; assert(mem[xAddr]==4); } zAddr = someNiceLocation funny(zAddr, zAddr);

Disadvantage of Global Array In Java: wp(x=E, y > 0) = In C: wp(x=E, y > 0) =

Disadvantage of Global Array In Java: wp(x=E, y > 0) = y > 0 In C: wp(x=E, y > 0) = wp(mem[xAddr]=E’, mem[yAddr]>0) = wp(mem= mem(xAddr:=E’), mem(yAddr)>0) = (mem(yAddr)>0)[ mem:=mem(xAddr:=E’) ] = (mem(xAddr:=E’))(yAddr) > 0 Each assignment can interfere with each value! This is a problem with the language, not our model

And how to do array bounds checks? Switch to a decent programming language.

For More Information Now please install go ahead and install all your updates so that the attacker is less likely to use buffer overflows and other consequences of memory unsafety to compromise your machines.

Back to Memory Safety

Memory Allocation in Java x = new C(); y = new C(); assert(x != y); // two fresh objects distinct Why should this assertion hold? How to model this?

How to represent fresh objects? assume(N > 50); a = new Object[N]; i = 0; while (i < N) { a[i] = new Object(); i = i + 1; } assert(a[5] != a[15]);

Alloc Set (Function) alloc : Obj  Boolean i.e. alloc : Set[Obj] x = new C(); 

Allocating New Objects

Allocating New Arrays

Now we can model many programs We can represent any body of sequential code inside one procedure. Our loop invariants, pre/post conditions can become very complex – software verification is hard

Example assume P; if (first == null) { first = n; n.next = null; n.prev = null; } else { n.next = first; first.prev = n; n.prev = null; first = n; } assert Q;

assume P; if (first == null) { first = n; n.next = null; n.prev = null; } else { n.next = first; first.prev = n; n.prev = null; first = n; } assert Q;

Reachability assume P; if (first == null) { first = n; n.next = null; n.prev = null; } else { n.next = first; first.prev = n; n.prev = null; first = n; } assert Q;