Ras Bodik WF 11-12:30 slides adapted from 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

Runtime Techniques for Efficient and Reliable Program Execution Harry Xu CS 295 Winter 2012.
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.
Abstraction and Modular Reasoning for the Verification of Software Corina Pasareanu NASA Ames Research Center.
3-Valued Logic Analyzer (TVP) Tal Lev-Ami and Mooly Sagiv.
Rigorous Software Development CSCI-GA Instructor: Thomas Wies Spring 2012 Lecture 11.
Background for “KISS: Keep It Simple and Sequential” cs264 Ras Bodik spring 2005.
Optimization Compiler Baojian Hua
Introduction to Advanced Topics Chapter 1 Mooly Sagiv Schrierber
1 Operational Semantics Mooly Sagiv Tel Aviv University Textbook: Semantics with Applications.
Establishing Local Temporal Heap Safety Properties with Applications to Compile-Time Memory Management Ran Shaham Eran Yahav Elliot Kolodner Mooly Sagiv.
Program analysis Mooly Sagiv html://
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.
4/25/08Prof. Hilfinger CS164 Lecture 371 Global Optimization Lecture 37 (From notes by R. Bodik & G. Necula)
Program analysis Mooly Sagiv html://
Programming Language Semantics Mooly SagivEran Yahav Schrirber 317Open space html://
Abstract Interpretation Part I Mooly Sagiv Textbook: Chapter 4.
1 Program Analysis Mooly Sagiv Tel Aviv University Textbook: Principles of Program Analysis.
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://
Prof. Bodik CS 164 Lecture 16, Fall Global Optimization Lecture 16.
272: Software Engineering Fall 2012 Instructor: Tevfik Bultan Lecture 4: SMT-based Bounded Model Checking of Concurrent Software.
An Overview on Static Program Analysis Mooly Sagiv.
Topic #10: Optimization EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
An Overview on Program Analysis Mooly Sagiv Tel Aviv University Textbook: Principles of Program.
Chapter 25 Formal Methods Formal methods Specify program using math Develop program using math Prove program matches specification using.
Problem Solving Techniques. Compiler n Is a computer program whose purpose is to take a description of a desired program coded in a programming language.
Compiler Principles Fall Compiler Principles Lecture 0: Local Optimizations Roman Manevich Ben-Gurion University.
Programming Languages and Design Lecture 3 Semantic Specifications of Programming Languages Instructor: Li Ma Department of Computer Science Texas Southern.
Semantics In Text: Chapter 3.
Program Analysis Mooly Sagiv Tel Aviv University Sunday Scrieber 8 Monday Schrieber.
An Undergraduate Course on Software Bug Detection Tools and Techniques Eric Larson Seattle University March 3, 2006.
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.
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:
Operational Semantics Mooly Sagiv Tel Aviv University Sunday Scrieber 8 Monday Schrieber.
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
Welcome! Simone Campanoni
Compiler Design (40-414) Main Text Book:
Programming Languages and Compilers (CS 421)
Chapter 1 Introduction.
Textbook: Principles of Program Analysis
Compiler Construction (CS-636)
Seminar in automatic tools for analyzing programs with dynamic memory
Chapter 1 Introduction.
State your reasons or how to keep proofs while optimizing code
G. Ramalingam Microsoft Research, India & K. V. Raghavan
Symbolic Implementation of the Best Transformer
Iterative Program Analysis Abstract Interpretation
Lecture 5 Floyd-Hoare Style Verification
Objective of This Course
Software analysis SET seminar.
CSE401 Introduction to Compiler Construction
Languages and Compilers (SProg og Oversættere) Compiler Optimizations
Advanced Compiler Design
CUTE: A Concolic Unit Testing Engine for C
Program correctness Axiomatic semantics
Program Verification with Hoare Logic
Programming Languages and Compilers (CS 421)
Presentation transcript:

Ras Bodik WF 11-12:30 slides adapted from Mooly Sagiv cs264: Program Analysis catalog name: Implementation of Programming Languages Ras Bodik WF 11-12:30 slides adapted from Mooly Sagiv

Topics static program analysis advanced topics, time permitting principles: key ideas and connections techniques: efficient algorithms applications: from compilation to software engineering advanced topics, time permitting dynamic program analysis ex.: run-time bug-finding program representations ex.: Static Single Assignment form

Course Requirements Prerequisites Useful but not mandatory a compiler course (cs164) Useful but not mandatory semantics of programming languages (cs263) algorithms discrete math

Course structure Source Format Grade Nielsen, Nielsen, Hankin, Principles of Program Analysis research papers Format lectures (roughly following the textbook) discussions of research papers you’ll read the paper before lecture and send me a “mini-review” 4 paragraphs Grade 4-5 homeworks take-home exam project

Guest lectures Lectures may include several “guest lecturers” expert’s view on a more advanced topic Guest lecture time usually not during class; instead PS Seminar (Mondays 4pm in 320 Soda) Faculty candidate talks (TBD) CHESS Seminar (Tuesdays, 4pm, 540 Cory) First speaker Shaz Qadeer, Monday 4pm 320 Soda paper: KISS: Keep it Simple and Sequential you’ll write a mini-review (instructions to come)

Outline What is static analysis usage in compilers and other clients Why is it called abstract interpretation? handling undecidability soundness of abstract interpretation Relation to program verification Complementary approaches

Static Analysis Goal: Usage: automatic derivation of properties that hold on every execution leading to a program location (label) (without knowing program input) Usage: compiler optimizations code quality tools Identify bugs Prove absence of certain bugs

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

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

Another example static analysis problem Find variables which are live at a given program location Definition: variable x is live at a program location p if x’s R-value can be used before x is set Corresponding property: there exists an x-definition-free execution path from p to a use of x

A simple liveness example /* c */ L0: a := 0 /* ac */ L1: b := a + 1 /* bc */ c := c + b a := b * 2 if c < N goto L1 return c a b c register interference graph

Typical compiler source program Scanner tokens Parser AST Semantic Analysis AST + type information Code Generator IR Static analysis IR + analysis info Transformations

Some Static Analysis Problems Live variables Reaching definitions Available expressions Dead code Pointer variables that never point to same location Points in the program in which it is safe to free an object A virtual method call whose target method is unique Statements that can be executed in parallel An access to a variable that must reside in the cache Integer intervals

The Need for Static Analysis Compilers Advanced computer architectures (Superscalar pipelined, VLIW, prefetching) High-level programming languages (functional, OO, garbage collected, concurrent) Software Productivity Tools Compile time debugging Strengthen type checking for C Detect Array-bound violations Identify dangling pointers Generate test cases Prove absence of runtime exceptions Prove pre- and post-conditions

Software Quality Tools. Detecting Hazards Uninitialized variables: a = malloc() ; b = a; cfree (a); c = malloc (); if (b == c) // unexpected equality References outside array bounds Memory leaks

leakage of address pointed to by head Memory leakage example List* reverse(List head) { List *rev, *n; rev = NULL; while (head != NULL) { n = headnext; headnext = rev; head = n; rev = head; } return rev; } typedef struct List { int d; struct List* next; } List; leakage of address pointed to by head

Challenges in Static Analysis Correctness Precision Efficiency Scaling

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

Example Abstract Interpretation. Casting Out Nines A (weak) sanity check of decimal arithmetic using 9 values 0, 1, 2, 3, 4, 5, 6, 7, 8 The casting-out-nine rule: whenever an intermediate result exceeds 8, replace by the sum of its digits (recursively) Example “123 * 457 + 76543 = 132654?” 123*457 + 76543 = 132654? 6 * 7 + 7 = 21? 6 + 7  = 3? 4 = 3? NO. Report an error. Why this rule produces no false alarms: (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 Determine if an integer variable is even or odd at a given program point

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

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

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

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

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

Odd/Even Abstract Interpretation (X) = if X=  return  ⊥ else if for all z in X (z%2 == 0) return E else if for all z in X (z%2 != 0) return O else return ? (a) = if a=⊥ return  else if a = E return Even else if a = O return Odd else return Natural

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

Concrete and Abstract Interpretation

Abstract interpretation cannot be always precise Operational semantics x := x/2 {8, 16} {16, 32}   E Abstract semantics x := x /# 2 ? E  a ⊒

Abstract (Conservative) interpretation Operational semantics statement s Set of states Set of states  abstract representation abstract representation  Abstract semantics statement s abstract representation ⊒

Abstract (Conservative) interpretation Operational semantics statement s Set of states ⊆ Set of states  Set of states  abstract representation Abstract semantics statement s abstract representation

Challenges in Abstract Interpretation Finding appropriate program semantics (runtime) Designing abstract representations What to forget What to remember Summarize crucial information Handling loops Handling procedures Scalability Large programs Missing source code Precise enough

Runtime vs. Abstract Interpretation (Software Quality Tools) Effectiveness Missed Errors False alarms Locate rare errors Cost Proportional to program’s execution Proportional to program’s size

Example Constant Propagation Abstract representation set of integer values and and extra value “?” denoting variables not known to be constants Conservative interpretation of + 4 3 2 ? … 1 + #

Example Constant Propagation (Cont) Conservative interpretation of * *# ? 1 2 *# ? 1 2 ? ? ? ? ? ? ? ? 1 ? 1 2 1 ? 1 2 2 ? 2 4 2 ? 2 4 … ? … … … ? … …

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 It is undecidable if a program point is reachable in some execution Some static analysis problems are undecidable even if the program conditions are ignored

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 + p2(x_1, x_2, ..., x_n)) /* Is y=0 here? */

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

Analogies with Numerical Analysis Approximate the exact semantics More precision can be obtained at greater computational costs But sometimes more precise can also be more efficient

Violation of soundness Loop invariant code motion Dead code elimination Overflow ((x+y)+z) != (x + (y+z)) Quality checking tools may decide to ignore certain kinds of errors Sound w.r.t different concrete semantics

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

Program Verification Mathematically prove the correctness of the program Requires formal specification Example. Hoare Logic {P} S {Q} {x = 1} x++ ; {x = 2} {x =1} {true} if (y >0) x = 1 else x = 2 {?} {y=n} z = 1 while (y>0) {z = z * y-- ; } {?}

Relation to Program Verification Program Analysis Program Verification Fully automatic But can benefit from specification Applicable to a programming language Can be very imprecise May yield false alarms Identify interesting bugs Establish non-trivial properties using effective algorithms Requires specification and loop invariants Not decidable Program specific Relative complete Must provide counter examples Provide useful documentation

Complementary Approaches Finite state model checking Unsound approaches Compute underapproximation Better programming language design Type checking Proof carrying code Just in time and dynamic compilation Profiling Runtime tests