An Overview on Static Program Analysis 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

- Vasvi Kakkad.  Formal -  Tool for mathematical analysis of language  Method for precisely designing language  Well formed model for describing and.
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.
Context-Sensitive Interprocedural Points-to Analysis in the Presence of Function Pointers Presentation by Patrick Kaleem Justin.
Compilation 2011 Static Analysis Johnni Winther Michael I. Schwartzbach Aarhus University.
ECE 454 Computer Systems Programming Compiler and Optimization (I) Ding Yuan ECE Dept., University of Toronto
Abstraction and Modular Reasoning for the Verification of Software Corina Pasareanu NASA Ames Research Center.
Pointer Analysis – Part I Mayur Naik Intel Research, Berkeley CS294 Lecture March 17, 2009.
Data-Flow Analysis Framework Domain – What kind of solution is the analysis looking for? Ex. Variables have not yet been defined – Algorithm assigns a.
Programming Languages Marjan Sirjani 2 2. Language Design Issues Design to Run efficiently : early languages Easy to write correctly : new languages.
INF 212 ANALYSIS OF PROG. LANGS Type Systems Instructors: Crista Lopes Copyright © Instructors.
Introduction to Advanced Topics Chapter 1 Mooly Sagiv Schrierber
ISBN Chapter 3 Describing Syntax and Semantics.
CS 355 – Programming Languages
1 Operational Semantics Mooly Sagiv Tel Aviv University Textbook: Semantics with Applications.
Recap from last time We were trying to do Common Subexpression Elimination Compute expressions that are available at each program point.
Next Section: Pointer Analysis Outline: –What is pointer analysis –Intraprocedural pointer analysis –Interprocedural pointer analysis (Wilson & Lam) –Unification.
Program analysis Mooly Sagiv html://
Recap Mooly Sagiv. Outline Subjects Studied Questions & Answers.
An Overview on Program Analysis Mooly Sagiv Tel Aviv University Textbook: Principles of Program.
An Overview on Static Program Analysis Mooly Sagiv Tel Aviv University Textbook: Principles of.
Program analysis Mooly Sagiv html://
Programming Language Semantics Mooly SagivEran Yahav Schrirber 317Open space html://
Reference Book: Modern Compiler Design by Grune, Bal, Jacobs and Langendoen Wiley 2000.
Data Flow Analysis Compiler Design October 5, 2004 These slides live on the Web. I obtained them from Jeff Foster and he said that he obtained.
Abstract Interpretation Part I Mooly Sagiv Textbook: Chapter 4.
Interprocedural Analysis Noam Rinetzky Mooly Sagiv Tel Aviv University Textbook Chapter 2.5.
1 Program Analysis Mooly Sagiv Tel Aviv University Textbook: Principles of Program Analysis.
Compiler Summary Mooly Sagiv html://
Overview of program analysis Mooly Sagiv html://
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 Languages and Compilers (SProg og Oversættere) Lecture 15 (1) Compiler Optimizations Bent Thomsen Department of Computer Science Aalborg University With.
Describing Syntax and Semantics
Program Analysis Mooly Sagiv Tel Aviv University Sunday Scrieber 8 Monday Schrieber.
Overview of program analysis Mooly Sagiv html://
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.
Procedure Optimizations and Interprocedural Analysis Chapter 15, 19 Mooly Sagiv.
CUTE: A Concolic Unit Testing Engine for C Technical Report Koushik SenDarko MarinovGul Agha University of Illinois Urbana-Champaign.
CS 403: Programming Languages Lecture 2 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
CSC3315 (Spring 2009)1 CSC 3315 Programming Languages Hamid Harroud School of Science and Engineering, Akhawayn University
An Overview on Program Analysis Mooly Sagiv Tel Aviv University Textbook: Principles of Program.
ISBN Chapter 3 Describing Semantics -Attribute Grammars -Dynamic Semantics.
Static Program Analyses of DSP Software Systems Ramakrishnan Venkitaraman and Gopal Gupta.
Compiler Principles Fall Compiler Principles Lecture 0: Local Optimizations Roman Manevich Ben-Gurion University.
Page 1 5/2/2007  Kestrel Technology LLC A Tutorial on Abstract Interpretation as the Theoretical Foundation of CodeHawk  Arnaud Venet Kestrel Technology.
Semantics In Text: Chapter 3.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
Program Analysis Mooly Sagiv
Principle of Programming Lanugages 3: Compilation of statements Statements in C Assertion Hoare logic Department of Information Science and Engineering.
Static Techniques for V&V. Hierarchy of V&V techniques Static Analysis V&V Dynamic Techniques Model Checking Simulation Symbolic Execution Testing Informal.
1 Languages and Compilers (SProg og Oversættere) Compiler Optimizations Bent Thomsen Department of Computer Science Aalborg University With acknowledgement.
CUTE: A Concolic Unit Testing Engine for C Koushik SenDarko MarinovGul Agha University of Illinois Urbana-Champaign.
Introduction to Software Analysis CS Why Take This Course? Learn methods to improve software quality – reliability, security, performance, etc.
Operational Semantics Mooly Sagiv Tel Aviv University Textbook: Semantics with Applications Chapter.
1 Iterative Program Analysis Abstract Interpretation Mooly Sagiv Tel Aviv University Textbook:
CSC3315 (Spring 2009)1 CSC 3315 Languages & Compilers Hamid Harroud School of Science and Engineering, Akhawayn University
Operational Semantics Mooly Sagiv Tel Aviv University Sunday Scrieber 8 Monday Schrieber.
CS412/413 Introduction to Compilers and Translators April 2, 1999 Lecture 24: Introduction to Optimization.
Operational Semantics Mooly Sagiv Reference: Semantics with Applications Chapter 2 H. Nielson and F. Nielson
Operational Semantics Mooly Sagiv Reference: Semantics with Applications Chapter 2 H. Nielson and F. Nielson
Optimization Simone Campanoni
Data Flow Analysis Suman Jana
Symbolic Implementation of the Best Transformer
University Of Virginia
Semantics In Text: Chapter 3.
Languages and Compilers (SProg og Oversættere) Compiler Optimizations
CUTE: A Concolic Unit Testing Engine for C
Ras Bodik WF 11-12:30 slides adapted from Mooly Sagiv
Presentation transcript:

An Overview on Static Program Analysis Mooly Sagiv

Subjects u What is static analysis u Usage in compilers –Where does it fit? –Prospective –Challenges u Other clients u Why is it called ``abstract interpretation''? u Undecidability u Handling Undecidability u Soundness of abstract interpretation –Definition –Violation of soundness –Abstract interpretation cannot be always homomorphic u Relation to program verification u Origins u Complementary approaches u Tentative schedule

Static Analysis u Automatic derivation of static properties which hold on every execution leading to a program location

Example Static Analysis Problem u Find variables with constant value at a given program location u Example program int p(int x){ return x *x ; void main()} { int z; if (getc()) z = p(6) + 8; else z = p(-7) -5; printf (z); } 44

Recursive Program int x void p(a) { read (c); if c > 0 { a = a -2; p(a); a = a + 2; } x = -2 * a + 5; print (x); } void main { p(7); print(x); }

Iterative Approximation z =3 while (x>0) if (x=1) y =7y =z+4 assert y==7 [x  ?, y  ?, z  ? ] [x  ?, y  ?, z  3 ] [x  1, y  ?, z  3 ] [x  1, y  7, z  3 ] [x  ?, y  7, z  3 ] [x  ?, y  ?, z  3 ]

List reverse(Element  head) { List rev, n; rev = NULL; while (head != NULL) { n = head  next; head  next = rev; head = n; rev = head; } return rev; } Memory Leakage potential leakage of address pointed to by head

Memory Leakage Element  reverse(Element  head) { Element  rev,  n; rev = NULL; while (head != NULL) { n = head  next; head  next = rev; rev = head; head = n; } return rev; }  No memory leaks

A Simple Example void foo(char *s ) { while ( *s != ‘ ‘ ) s++; *s = 0; } Potential buffer overrun: offset(s)  alloc(base(s))

A Simple Example void foo(char string(s) { while ( *s != ‘ ‘&& *s != 0) s++; *s = 0; }  No buffer overruns

Example Static Analysis Problem u Find variables which are live at a given program location u Used before set on some execution paths from the current program point

A Simple Example /* c */ L0: a := 0 /* ac */ L1:b := a + 1 /* bc */ c := c + b /* bc */ a := b * 2 /* ac */ if c < N goto L1 /* c */ return c ab c

Compiler Scheme String Scanner Parser Semantic Analysis Code Generator Static analysis Transformations Tokens AST LIR source-program tokens AST IR IR +information

Other Example Program Analyses u Reaching definitions u Expressions that are ``available'' u Dead code u Pointer variables never point into the same location u Points in the program in which it is safe to free an object u An invocation of virtual method whose address is unique u Statements that can be executed in parallel u An access to a variable which must be in cache u Integer intervals

The Need for Static Analysis u Compilers –Advanced computer architectures –High level programming languages (functional, OO, dynamic) u Software Productivity Tools –Compile time debugging »Stronger type Checking for C »Array bound violations »Identify dangling pointers »Generate test cases »Generate certification proofs u Program Understanding

Challenges in Static Analysis u Non-trivial u Correctness u Precision u Efficiency of the analysis u Scaling

C Compilers u The language was designed to reduce the need for optimizations and static analysis u The programmer has control over performance (order of evaluation, storage, registers) u C compilers nowadays spend most of the compilation time in static analysis u Sometimes C compilers have to work harder!

Software Quality Tools u Detecting hazards (lint) –Uninitialized variables a = malloc() ; b = a; cfree (a); c = malloc (); if (b == c) printf(“unexpected equality”); u References outside array bounds u Memory leaks (occurs even in Java!)

Foundation of Static Analysis u Static analysis can be viewed as interpreting the program over an “abstract domain” u Execute the program over larger set of execution paths u Guarantee sound results –Every identified constant is indeed a constant –But not every constant is identified as such

Example Abstract Interpretation Casting Out Nines u Check soundness of arithmetic using 9 values 0, 1, 2, 3, 4, 5, 6, 7, 8 u Whenever an intermediate result exceeds 8, replace by the sum of its digits (recursively) u Report an error if the values do not match u Example query “123 * = $?” –Left 123* = 6 * =6 + 7 = 4 –Right 3 –Report an error u Soundness (10a + b) mod 9 = (a + b) mod 9 (a+b) mod 9 = (a mod 9) + (b mod 9) (a*b) mod 9 = (a mod 9) * (b mod 9)

Even/Odd Abstract Interpretation u Determine if an integer variable is even or odd at a given program point

Example Program while (x !=1) do { if (x %2) == 0 { x := x / 2; } else { x := x * 3 + 1; assert (x %2 ==0); } } {0, 1, 2, … } {0, 2, 3, … } {0, 2, 4, …} {3, 5, 7, …} {0, 1, 2, … } {10, 16, 22, …} {1 }

Example Program while (x !=1) do { if (x %2) == 0 { x := x / 2; } else { x := x * 3 + 1; assert (x %2 ==0); } } ? ? E O ? E O

  Abstract Abstract Interpretation Concrete  Sets of stores Descriptors of sets of stores 

Odd/Even Abstract Interpretation  {-2, 1, 5} {0,2} {2}{0}  EO ? All concrete states   {x: x  Even}

Odd/Even Abstract Interpretation  {-2, 1, 5} {0,2} {2}{0}  EO ?    All concrete states   {x: x  Even}

Odd/Even Abstract Interpretation  {-2, 1, 5} {0,2} {2}{0}  EO ?  All concrete states {x: x  Even}  

Example Program while (x !=1) do { if (x %2) == 0 { x := x / 2; } else { x := x * 3 + 1; assert (x %2 ==0); } } OE

(Best) Abstract Transformer Concrete Representation ConcretizationAbstraction Operational Semantics St Abstract Representation Abstract Semantics St

Concrete and Abstract Interpretation

Example Program [x !=1] 1 [x := x /2] 3 [exit] 5 [x %2=0] 2 [x := x * 3 +1] 4 x 1 = x’ 3  x’ 4  x' 1 = x 1 x 2 = x’ 1   x‘ 2 = x 2 x 3 = x’ 2  E x' 3 =  x 4 = x’ 2  O x‘ 4 = x 4 * # O + # O x 5 = x’ 1  O x‘ 5 = x 5 F T T F

Runtime vs. Static Testing RuntimeAbstract EffectivenessMissed ErrorsFalse alarms Locate rare errors CostProportional to program’s execution Proportional to program’s size

Abstract (Conservative) interpretation abstract representation Set of states concretization Abstract semantics statement s abstract representation abstraction Operational semantics statement s Set of states

Example rule of signs u Safely identify the sign of variables at every program location u Abstract representation {P, N, ?} u Abstract (conservative) semantics of * P N ?

Abstract (conservative) interpretation {…,,…} concretization Abstract semantics x := x*#y abstraction Operational semantics x := x*y {…, …}

Example rule of signs (cont) u Safely identify the sign of variables at every program location u Abstract representation {P, N, ?} u  (C) = if all elements in C are positive then return P else if all elements in C are negative then return N else return ? u  (a) = if (a==P) then return{0, 1, 2, … } else if (a==N) return {-1, -2, -3, …, } else return Z

Example Constant Propagation u Abstract representation set of integer values and and extra value “?” denoting variables not known to be constants u Conservative interpretation of +

Example Constant Propagation(Cont) u Conservative interpretation of *

Example Program {,, …,,, …} x = 5; {,, … } y = 7; {,, …} if (getc()) y = x + 2; {,, …} z = x +y ; { }

Example Program x = 5; y = 7; if (getc()) y = x + 2; z = x +y ;

Example Program (2) {,, …,,, …} if (getc()) x= 3 ; y = 2; {,, …} else x =2; y = 3; {,, …} z = x +y; {, }

Undecidability Issues u It is undecidable if a program point is reachable in some execution u Some static analysis problems are undecidable even if the program conditions are ignored u It may be undecidable to compute the best transformers u Computing the least abstract value may be undecidable

The Constant Propagation Example while (getc()) { if (getc()) x 1 = x 1 + 1; if (getc()) x 2 = x 2 + 1;... if (getc()) x n = x n + 1; } y = truncate (1/ (1 + p 2 (x 1, x 2,..., x n )) /* Is y=0 here? */

Coping with undecidabilty u Loop free programs u Simple static properties u Interactive solutions u Conservative estimations –Every enabled transformation cannot change the meaning of the code but some transformations are no enabled –Non optimal code –Every potential error is caught but some “false alarms” may be issued

Analogies with Numerical Analysis u Approximate the exact semantics u More precision can be obtained at greater u computational costs

Violation of soundness u Loop invariant code motion u Dead code elimination u Overflow ((x+y)+z) != (x + (y+z)) u Quality checking tools may decide to ignore certain kinds of errors

Abstract interpretation cannot be always homomorphic (rules of signs) abstraction abstraction Operational semantics x := x+y Abstract semantics x := x+#y

Local Soundness of Abstract Interpretation abstraction Operational semantics statement Abstract semantics statement# 

Optimality Criteria u Precise (with respect to a subset of the programs) u Precise under the assumption that all paths are executable (statically exact) u Relatively optimal with respect to the chosen abstract domain u Good enough

Relation to Program Verification u Fully automatic u Applicable to a programming language u Can be very imprecise u May yield false alarms u Requires specification and loop invariants u Program specific u Relative complete u Provide counter examples u Provide useful documentation u Can be mechanized using theorem provers Program AnalysisProgram Verification

Origins of Abstract Interpretation u [Naur 1965] The Gier Algol compiler “A process which combines the operators and operands of the source text in the manner in which an actual evaluation would have to do it, but which operates on descriptions of the operands, not their value” u [Reynolds 1969] Interesting analysis which includes infinite domains (context free grammars) u [Syntzoff 1972] Well foudedness of programs and termination u [Cousot and Cousot 1976,77,79,…] The foundation u [Graham and Wegman, 1975: Kamm and Ullman, Kildall 1977] Algorithmic foundations u [Tarjan 1981] Reductions to semi-ring problems u [Sharir and Pnueli 1981] Foundation of the interprocedural case u [Allen, Kennedy, Cock, Jones, Muchnick and Schwartz]

Complementary Approaches u Better programming language design u Type checking u Just in time and dynamic compilation u Profiling u Sophisticated hardware u Runtime tests u Concolic testing

Summary u Understanding concretization is essential –For dataflow analysis –For type inference/checking –For logicians u Abstract interpretation is not limited to a particular style of programming