De necessariis pre condiciones consequentia sine machina P. Consobrinus, R. Consobrinus M. Aquilifer, F. Oratio.

Slides:



Advertisements
Similar presentations
Advanced programming tools at Microsoft
Advertisements

Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 18 Program Correctness To treat programming.
Technologies for finding errors in object-oriented software K. Rustan M. Leino Microsoft Research, Redmond, WA Lecture 1 Summer school on Formal Models.
Automated Theorem Proving Lecture 1. Program verification is undecidable! Given program P and specification S, does P satisfy S?
Copyright W. Howden1 Programming by Contract CSE 111 6/4/2014.
Modular and Verified Automatic Program Repair Francesco Logozzo, Thomas Ball RiSE - Microsoft Research Redmond.
An Abstract Interpretation Framework for Refactoring P. Cousot, NYU, ENS, CNRS, INRIA R. Cousot, ENS, CNRS, INRIA F. Logozzo, M. Barnett, Microsoft Research.
CodeContracts & Clousot Francesco Logozzo - Microsoft Mehdi Bouaziz – ENS.
Reasoning About Code; Hoare Logic, continued
Hoare’s Correctness Triplets Dijkstra’s Predicate Transformers
Rigorous Software Development CSCI-GA Instructor: Thomas Wies Spring 2012 Lecture 13.
David Evans CS655: Programming Languages University of Virginia Computer Science Lecture 19: Minding Ps & Qs: Axiomatic.
Copyright © 2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages Chapter 3 : Describing Syntax and Semantics Axiomatic Semantics.
ISBN Chapter 3 Describing Syntax and Semantics.
Simple Example {i = 0} j := i * i {j < 100} Can we ‘verify’ this triple? Only if we know the semantics of assignment.
Copyright © 2006 Addison-Wesley. All rights reserved. 3.5 Dynamic Semantics Meanings of expressions, statements, and program units Static semantics – type.
1 Design by Contract Building Reliable Software. 2 Software Correctness Correctness is a relative notion  A program is correct with respect to its specification.
CS 355 – Programming Languages
ESC Java. Static Analysis Spectrum Power Cost Type checking Data-flow analysis Model checking Program verification AutomatedManual ESC.
Program analysis Mooly Sagiv html://
1 Specifying Object Interfaces. 2 Major tasks in this stage: --are there any missing attributes or operations? --how can we reduce coupling, make interface.
Static and Dynamic Contract Verifiers For Java Hongming Liu.
Program analysis Mooly Sagiv html://
Houdini: An Annotation Assistant for ESC/Java Cormac Flanagan and K. Rustan M. Leino Compaq Systems Research Center.
OOP #10: Correctness Fritz Henglein. Wrap-up: Types A type is a collection of objects with common behavior (operations and properties). (Abstract) types.
Abstract Interpretation Part I Mooly Sagiv Textbook: Chapter 4.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 18 Program Correctness To treat programming.
ESC Java. Static Analysis Spectrum Power Cost Type checking Data-flow analysis Model checking Program verification AutomatedManual ESC.
Overview of program analysis Mooly Sagiv html://
Describing Syntax and Semantics
Program Analysis Mooly Sagiv Tel Aviv University Sunday Scrieber 8 Monday Schrieber.
Procedure specifications CSE 331. Outline Satisfying a specification; substitutability Stronger and weaker specifications - Comparing by hand - Comparing.
Pre/Post Condition Logic 03/06/2013. Agenda Hoare’s Logic Overview Application to Pre/Post Conditions.
Software Engineering Prof. Dr. Bertrand Meyer March 2007 – June 2007 Chair of Software Engineering Static program checking and verification Slides: Based.
Reading and Writing Mathematical Proofs
A Parametric Segmentation Functor for Fully Automatic and Scalable Array Content Analysis Patrick Cousot, NYU & ENS Radhia Cousot, CNRS & ENS & MSR Francesco.
Design by Contract in Java Concept and Comparison.
SWE 619 © Paul Ammann Procedural Abstraction and Design by Contract Paul Ammann Information & Software Engineering SWE 619 Software Construction cs.gmu.edu/~pammann/
Chapter 25 Formal Methods Formal methods Specify program using math Develop program using math Prove program matches specification using.
CS 261 – Data Structures Preconditions, Postconditions & Assert.
CS 363 Comparative Programming Languages Semantics.
Reasoning about programs March CSE 403, Winter 2011, Brun.
Chapter 3 Part II Describing Syntax and Semantics.
Semantics In Text: Chapter 3.
COP4020 Programming Languages Introduction to Axiomatic Semantics Prof. Robert van Engelen.
ANU COMP2110 Software Design in 2003 Lecture 10Slide 1 COMP2110 Software Design in 2004 Lecture 12 Documenting Detailed Design How to write down detailed.
Principle of Programming Lanugages 3: Compilation of statements Statements in C Assertion Hoare logic Department of Information Science and Engineering.
Defensive Programming CNS 3370 Copyright 2003, Fresh Sources, Inc.
PROGRAMMING PRE- AND POSTCONDITIONS, INVARIANTS AND METHOD CONTRACTS B MODULE 2: SOFTWARE SYSTEMS 13 NOVEMBER 2013.
Loop Invariants and Binary Search Chapter 4.4, 5.1.
CSC3315 (Spring 2009)1 CSC 3315 Languages & Compilers Hamid Harroud School of Science and Engineering, Akhawayn University
© Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd edition, by Bertrand Meyer (Prentice.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 17 – Specifications, error checking & assert.
Jeremy Nimmer, page 1 Automatic Generation of Program Specifications Jeremy Nimmer MIT Lab for Computer Science Joint work with.
11/22/2016IT 3271 A formal system:Axioms and Rules, for inferring valid specification x := m; y := n; while ¬(x=y) do if x>y then x := x-y else y := y-x.
Chapter 3 of Programming Languages by Ravi Sethi
Chapter 6 CS 3370 – C++ Functions.
Reasoning About Code.
Reasoning about code CSE 331 University of Washington.
CSE 143 Error Handling [Section 2.8] 3/30/98 CSE 143.
Specifying Object Interfaces
Lecture 5 Floyd-Hoare Style Verification
Programming Languages 2nd edition Tucker and Noonan
CSC 143 Error Handling Kinds of errors: invalid input vs programming bugs How to handle: Bugs: use assert to trap during testing Bad data: should never.
Hoare-style program verification
Semantics In Text: Chapter 3.
Program correctness Axiomatic semantics
Programming Languages 2nd edition Tucker and Noonan
COP4020 Programming Languages
Presentation transcript:

De necessariis pre condiciones consequentia sine machina P. Consobrinus, R. Consobrinus M. Aquilifer, F. Oratio

Automatic inference of necessary preconditions P. Cousot, R. Cousot M. Fahndrich, F. Logozzo

The paper in one slide Problem: Automatic inference of preconditions Define: What is a precondition? Sufficient precondition: if it holds, the function is correct Necessary precondition: if it does not hold, the function is definitely wrong When automatic inference is considered, only necessary preconditions make sense Sufficient preconditions impose too large a burden to callers Necessary preconditions are easy to explain to users Implementation in Clousot Precision improvements 9% to 21% Extremely low false positive ratio

Example int Example1(int x, object[] a) { if (x >= 0) { return a.Length; } return -1; } Sufficient precondition: a != null Too strong for the caller No runtime errors when x < 0 and a == null Clousot users complained about it “wrong preconditions”

Example Sufficient precondition: false It may fail, so eliminate all runs Necessary precondition: 0 < a.Length If a.Length == 0 it will always fail Necessary precondition is weaker than the weakest precondition!!! void Example2(object[] a) { Contract.Requires(a != null); for (var i = 0; i <= a.Length; i++) { a[i] = F(a[i]); if (NonDet()) return; }

Semantics

Program semantics Program traces: T = G ∪ B ∪ I G = good traces, terminating in a good state B = bad traces, terminating in an assertion violation Assertions: Language-induced: division by zero, null pointers, buffer overrun … User-supplied annotations: assertions, preconditions, postconditions, object invariants I = infinite traces, non-termination Notation: X(s) are the traces starting with s

Necessary and sufficient In S N we say that S in a sufficient condition for N N is a necessary condition for S For a program P A condition S is sufficient if its truth ensures that P is correct A condition N is necessary if its falsehood ensures P is incorrect

Sufficient Preconditions

Weakest (liberal) preconditions Provide sufficient preconditions guaranteeing partial correctness: wlp( P, true)(s 0 ) ≝ (B(s 0 ) = ∅ ) Drawbacks of wlp for the automatic inference of preconditions: 1.With loops, there is no algorithm to compute wlp( P, true) Solution in deductive verification: Use loop invariant 2.Inferred preconditions are sufficient but not the weakest anymore Under-approximation of loops 3.Sufficient preconditions rule out good runs Callers should satisfy a too strong condition

Example Overflows are not an error Ex. Sum([ , , -10]) = 19 In deductive verification, provide loop invariant Which is the weakest precondition? The method itself Sufficient preconditions: ∀ i ∈ [0, xs.Length], 0 ≤ xs[i] < MaxInt/xs.Length or xs.Length == 3 ∧ xs[0] + xs[1] == 0 ∧ xs[2] >= 0 or …. int Sum(int[] xs) { Contract.Requires(xs != null); int sum = 0; for (var i = 0; i < xs.Length; i++) sum += xs[i]; Contract.Assert(sum >=0); return sum; }

Under-approximation of wlp Formally, with loop invariants, we compute a sufficient condition S: S(s 0 ) wlp( P, true)(s 0 ) Which is equivalent to [I(s 0 ) = ∅ ] [S(s 0 ) G(s 0 ) ≠ ∅ ] So that it may exists some initial state s such that ¬ S(s) ∧ G(s) ≠ ∅ i.e., s does not satisfy S, but it does not lead to a bad state

Consequences Sufficient preconditions impose too large a burden to the caller They just ensure the correctness of the callee Not practical in a realistic setting Users complained about “wrong” preconditions “wrong preconditions” = sufficient preconditions

Necessary preconditions

Strongest necessary preconditions If the program terminates in a good state for s 0 then N(s 0 ) should hold: [I(s 0 ) = ∅ ] [G(s 0 ) ≠ ∅ N(s 0 )] Equivalently [I(s 0 ) = ∅ ] [¬N(s 0 ) (G(s 0 ) = ∅ ∧ B(s 0 ) ≠ ∅ )] i.e., if N does not hold, either The program diverges, or The program reaches a bad state Strongest (liberal) necessary precondition: snp( P, true)(s 0 ) ≝ ¬[G(s 0 ) = ∅ ∧ B(s 0 ) ≠ ∅ ]= [G(s 0 ) ≠ ∅ ∨ B(s 0 ) = ∅ ]

Comparison, ignoring non- termination G(s 0 ) S(s 0 ) ∅ ≠ ∅ B(s 0 ) ∅ true ≠ ∅ false G(s 0 ) N(s 0 ) ∅ ≠ ∅ B(s 0 ) ∅ true ≠ ∅ falsetrue Weakest sufficient preconditionsStrongest necessary preconditions

Approximation of necessary conditions Static analyses to infer an error condition E such that E(s 0 ) [G(s 0 ) = ∅ ∧ B(s 0 ) ≠ ∅ ] i.e., E is sufficient to guarantee the presence of definite errors or non- termination E is an under-approximation of the error semantics The negation, ¬E = N is weaker than the strongest (liberal) necessary precondition: G(s 0 ) ≠ ∅ ∨ B(s 0 ) = ∅ ¬E(s 0 )

Inference

Main Algorithm Iterate until stabilization For each method m Analyze m using the underlying static analysis Collect proof obligations Use the analysis to prove the assertions in Let ⊆ be the set of warnings If ≠ ∅ then Infer necessary preconditions for assertions in Simplify the inferred preconditions Propagate the necessary preconditions to the callers of m

Static analyses for the inference All-Paths precondition analysis Hoists unmodified assertions to the code entry Conditional-path precondition analysis Hoist assertions by taking into account assignments and tests Use dual-widening for loops Dual-widening under-approximates its arguments Quantified precondition analysis Deal with unbounded data structures

Examples All-paths infers a != null Conditional-paths also infers a.Length > 0 ∧ (a[0] != 3 a.Length >1) Quantified infers ∃ j ∈ [0, a.Length]. a[j] == 3 Details in the paper int FirstOccurence(int[] a) { int i = 0; while (a[i] != 3) i++; return i; }

Simplification We can infer many preconditions for a given method Simplification allows reducing them Key to scalability Pretty print preconditions for the user Simplification is a set of rewriting rules to iterate to fixpoint Examples P, [b ⇒ a], [¬b ⇒ a] → P, [true ⇒ a] P, [true ⇒ a] → P, a

Implementation

Code Contracts static checker Clousot/cccheck static analyzer for.NET Downloaded more than 80,000 times Use preconditions/postconditions to reason on method calls Suggest and propagates inferred preconditions and postconditions Users complained about sufficient preconditions Starting point for this work

User experience

Experimental results Un-annotated code (.net base libraries) All paths analysis Infer 18,643 preconditions Simplification removes >32% Conditional path analysis Infers 28,623 preconditions Simplification removes >24% Similar results for partially annotated code (Facebook C# SDK) Conditional path analysis is more precise but up to 4x slower than all-paths analysis Because of inferred disjunctions

Precision Number of inferred preconditions is not a good measure We are interested in the precision, i.e., fewer methods with warnings Precision gain is between 9% (framework libraries) and 21% (facebook C# SDK) Missing preconditions public surface are errors The library does not defend against “bad inputs” On mscorlib, the core library of.Net, we found 129 new bugs Only one false positive Because of exception handling in clousot

Conclusions

Sic transit gloria mundi The violation of a necessary precondition guarantee a definite error When automatically inferring preconditions, only necessary preconditions make sense Sufficient preconditions are too strict for callers Advantages Easy to explain to the users Provide chain leading to errors No false positives Implemented, and used in a widely downloaded tool (Clousot/cccheck)