Preventing bugs with pluggable type-checking Michael Ernst MIT

Slides:



Advertisements
Similar presentations
Optional Static Typing Guido van Rossum (with Paul Prescod, Greg Stein, and the types-SIG)
Advertisements

Rigorous Software Development CSCI-GA Instructor: Thomas Wies Spring 2012 Lecture 8.
Introducing JavaScript
Identity and Equality Based on material by Michael Ernst, University of Washington.
Data-Flow Analysis Framework Domain – What kind of solution is the analysis looking for? Ex. Variables have not yet been defined – Algorithm assigns a.
Object and Reference Immutability using Java Generics Yoav Zibin, Alex Potanin(*), Mahmood Ali, Shay Artzi, Adam Kiezun, and Michael D. Ernst MIT Computer.
Preventing bugs with pluggable type checking Michael D. Ernst University of Washington Object x)
GUI Threading Explained and Verified Michael Ernst U. of Washington, Seattle, USA IMDEA Software Institute, Madrid, Spain joint work with Colin Gordon,
Java Annotations. Annotations  Annotations are metadata or data about data. An annotation indicates that the declared element should be processed in.
Static code check – Klocwork
Detecting and preventing bugs with pluggable type-checking Michael D. Ernst University of Washington Joint work with Mahmood Ali, Werner Dietl, …
An overview of JML tools and applications Lilian Burdy Gemplus Yoonsik Cheon, Gary Leavens Iowa Univ. David Cok Kodak Michael Ernst MIT Rustan Leino Microsoft.
Working with JavaScript. 2 Objectives Introducing JavaScript Inserting JavaScript into a Web Page File Writing Output to the Web Page Working with Variables.
Object and Reference Immutability using Java Generics Yoav Zibin(1), Alex Potanin(2), Shay Artzi(1), Adam Kiezun(1), and Michael D. Ernst(1) 1) MIT Computer.
Visualizing Type Qualifier Inference with Eclipse David Greenfieldboyce Jeffrey S. Foster University of Maryland.
XP 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties Tutorial 10.
1 Chapter 2 Introductory Programs. 2 Getting started To create and run a Java program –Create a text file with a.java extension for the source code. For.
Javari: Adding Reference Immutability to Java Matthew Tschantz and Michael Ernst MIT CSAIL.
Usable code verification: balancing costs and benefits Two nuggets: User-defined verifiers Crowd-sourced verification Michael D. Ernst University of Washington.
Unit Testing & Defensive Programming. F-22 Raptor Fighter.
11 Values and References Chapter Objectives You will be able to: Describe and compare value types and reference types. Write programs that use variables.
Software Engineering Prof. Dr. Bertrand Meyer March 2007 – June 2007 Chair of Software Engineering Static program checking and verification Slides: Based.
Types for Programs and Proofs Lecture 1. What are types? int, float, char, …, arrays types of procedures, functions, references, records, objects,...
Inference and Checking of Object Ownership Wei Huang 1, Werner Dietl 2, Ana Milanova 1, Michael D. Ernst 2 1 Rensselaer Polytechnic Institute 2 University.
07 Coding Conventions. 2 Demonstrate Developing Local Variables Describe Separating Public and Private Members during Declaration Explore Using System.exit.
Object x) { List lst; … } Detecting and preventing null pointer errors with pluggable type-checking CSE 331 University of Washington.
CSE 131 Computer Science 1 Module 1: (basics of Java)
Preventing bugs with pluggable type checking Michael Ernst and Mahmood Ali University of Washington and MIT OOPSLA 2009 tutorial Object.
C# D1 CSC 298 Elements of C# code (part 2). C# D2 Writing a class (or a struct)  Similarly to Java or C++  Fields: to hold the class data  Methods:
Java is an object oriented programming language In this model of programming all entities are objects that have methods and fields Methods perform tasks.
Ownership and Immutability in Generic Java (OIGJ) Yoav Zibin +, Alex Potanin * Paley Li *, Mahmood Ali ^, and Michael Ernst $ Presenter: Yossi Gil + +
Refactoring1 Improving the structure of existing code.
User-defined type checkers for error detection and prevention in Java Michael D. Ernst MIT Computer Science & AI Lab
An Undergraduate Course on Software Bug Detection Tools and Techniques Eric Larson Seattle University March 3, 2006.
Advanced C# Types Tom Roeder CS fa. From last time out parameters difference is that the callee is required to assign it before returning not the.
M1G Introduction to Programming 2 5. Completing the program.
Demo of Scalable Pluggable Types Michael Ernst MIT Dagstuhl Seminar “Scalable Program Analysis” April 17, 2008.
How analysis can hinder source code manipulaton And what to do about it Michael Ernst University of Washington.
JSR 308: Type Annotations Making Java annotations more general and more useful Spec leads: Michael Ernst & Alex Buckley Implementation lead: Werner Dietl.
Object and Reference Immutability using Java Generics Yoav Zibin(1), Alex Potanin(2), Shay Artzi(1), Adam Kiezun(1), and Michael D. Ernst(1) 1) MIT Computer.
More About Data Types & Functions. General Program Structure #include statements for I/O, etc. #include's for class headers – function prototype statements.
Java Annotations. Annotations  Annotations are metadata or data about data. An annotation indicates that the declared element should be processed in.
Java Annotations for Types and Expressions Mathias Ricken October 24, 2008 COMP 617 Seminar.
Detecting and preventing bugs with pluggable type-checking Michael D. Ernst University of Washington Joint work with Werner Dietl, and many others
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Using Member Functions and Data Members.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
ReIm & ReImInfer: Checking and Inference of Reference Immutability and Method Purity Wei Huang 1, Ana Milanova 1, Werner Dietl 2, Michael D. Ernst 2 1.
Preventing bugs with pluggable type checking Michael Ernst University of Washington Joint work with Mahmood Ali and Matthew Papi Object.
Tracking Bad Apples: Reporting the Origin of Null & Undefined Value Errors Michael D. Bond UT Austin Nicholas Nethercote National ICT Australia Stephen.
Zach Tatlock / Winter 2016 CSE 331 Software Design and Implementation Lecture 16 Checker Framework.
Jeremy Nimmer, page 1 Automatic Generation of Program Specifications Jeremy Nimmer MIT Lab for Computer Science Joint work with.
CSE 331 Software Design and Implementation
The need for Programming Languages
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
Javarifier: inference of reference immutability
Types for Programs and Proofs
CSE 374 Programming Concepts & Tools
Introduction to Classes and Objects
Testing and Debugging.
Accessible Formal Methods A Study of the Java Modeling Language
C Basics.
Lightweight Verification of Array Indexing
Embracing Java 9 and beyond with Eclipse JDT
Object and Reference Immutability using Java Generics
16 Strings.
CSE 331 Annotations slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
CMPE212 – Reminders Assignment 2 due next Friday.
Java Annotations for Invariant Specification
Presentation transcript:

Preventing bugs with pluggable type-checking Michael Ernst MIT

Problem: Java’s type checking is too weak Type checking prevents many bugs int i = “42”; // type error Type checking doesn't prevent enough bugs // possible NullPointerException! getValue().toString(); Java types cannot express important properties –Non-null, interned, immutable, encrypted, tainted,... Solution: pluggable type systems –Design a type system to solve a specific problem –Annotate your code with type qualifiers –Type checker warns about violations (bugs)

Code example: Neighbors of a graph node class Graph { Set edges; //... List getNeighbors( Node v) { List neighbors = new LinkedList (); for (Edge e : edges) if (e.from() == v) neighbors.add(e.to()); return neighbors; }

Prevent null pointer type class Graph { Set edges; //... List getNeighbors( Node v) { List neighbors = new LinkedList (); for (Edge e : edges) if (e.from() == v) neighbors.add(e.to()); return neighbors; }

Prevent null pointer type class Graph Set edges; List Node v) { List neighbors = new LinkedList (); for (Edge e : edges) if (e.from() == v) // OK: no null ptr neighbors.add(e.to()); // OK: no null ptr return neighbors; }

Prevent incorrect equality type qualifier class Graph { Set edges; //... List Node v) { List neighbors = new LinkedList (); for (Edge e : edges) if (e.from() == v) // OK neighbors.add(e.to()); return neighbors; }

Prevent incorrect side type qualifier class Graph { Set edges; //... List Node { List neighbors = new LinkedList (); for (Edge e : edges) if (e.from() == v) neighbors.add(e.to()); return neighbors; } // body is OK: no side effects }

Using a checker Write annotations on the code –Or use an inference tool Compiler plugin for javac javac -processor NonnullChecker MyFile.java –Produces additional errors/warnings –Can use it from an IDE (e.g., Eclipse)

Outline NonNull checker Interned checker IGJ (immutability) checker Creating your own checker

Null dereference checker. Problem: NullPointerExceptions Object obj; // might be Object nnobj; // never null nnobj.toString(); // OK obj.toString(); // possible NPE obj =...; // OK nnobj = obj; // nnobj may become null Type system:

Demo of null dereference checker

Comparison with other tools Checking a 4KLOC program Type checking finds all the NPE bugs The other tools also find non-NPE bugs Null pointer errorsFalse warnings Annotations written FoundMissed JSR FindBugs071 0 Jlint078 0 PMD070 0

Good defaults reduce user effort Nullable default: –Dictated by backward compatibility –Many instances – too verbose –Flow-sensitive analysis inferred many NonNull types for local variables, reducing annotation burden NonNull default –Less verbose in signatures Draws attention to exceptions rather than the rule –More annotations in method bodies Our system: Nullable only for local variables –Not for generics on local variables –An alternative to type inference; effect may be similar

Local type inference (flow-sensitivity) Date d1, d2, d3; d1 = new Date(); d1.getMonth(); // OK: d1 is non-null assert d2 != null; d2.getMonth(); // OK if (d3 != null) { d3.getMonth(); // OK } Often, no need for annotations on local vars

Interning (= canonicalization, hash-consing) Reuse an existing object instead of creating a new one A space optimization: savings can be significant A time optimization: use == for comparisons Built into java.lang.String : intern() method Users can add interning for their own classes s1 s2 s3 “hello”

Interning (= canonicalization, hash-consing) Reuse an existing object instead of creating a new one A space optimization: savings can be significant A time optimization: use == for comparisons Built into java.lang.String : intern() method Users can add interning for their own classes s1 s2 s3 “hello”

Interning (= canonicalization, hash-consing) Reuse an existing object instead of creating a new one A space optimization: savings can be significant A time optimization: use == for comparisons Built into java.lang.String : intern() method Users can add interning for their own classes s1 s2 s3 “hello” s1 = s1.intern();

Interning (= canonicalization, hash-consing) Reuse an existing object instead of creating a new one A space optimization: savings can be significant A time optimization: use == for comparisons Built into java.lang.String : intern() method Users can add interning for their own classes s1 s2 s3 “hello” s1 = s1.intern(); s2 = s2.intern();

Interning (= canonicalization, hash-consing) Reuse an existing object instead of creating a new one A space optimization: savings can be significant A time optimization: use == for comparisons Built into java.lang.String : intern() method Users can add interning for their own classes s1 s2 s3 “hello” s1 = s1.intern(); s2 = s2.intern(); s3 = s3.intern();

Interning (= canonicalization, hash-consing) Reuse an existing object instead of creating a new one A space optimization: savings can be significant A time optimization: use == for comparisons Built into java.lang.String : intern() method Users can add interning for their own classes s1 s2 s3 “hello” s1 = s1.intern(); s2 = s2.intern(); s3 = s3.intern();

Interned checker. Problem: incorrect equality checks String String is, is2;... is = myString.intern()... // OK if (is == is2) {... } // OK if (s == is) {... } // unsafe equality is = s; // unsafe assignment Type system:

Demo of Interned checker

Interned case study Program: Daikon (250 KLOC) Interning is important –Daikon’s key scalability problem is memory –1170 lines of code/comment refer to interning 72% of files have none, 87% have 0, 1, or 2 –200 run-time assertions –Emacs plug-in for String equality checking 124 annotations in 11 files (12KLOC)

Interned results 9 errors –Missing calls to intern 2 performance bugs –Unnecessary interning in inner loop of file reading 1 design flaw –VarInfoName is not interned within its implementation, but all escaping objects are –Too hard to understand complex interning 14 false positives )

Observer methods can expose state class Class { private Object[] signers; Object[] getSigners() { return signers; // JDK 1.1 bug } myClass.getSigners()[0] = “ Sun ” ;

Observer methods can expose state class Class { private Object[] Object[] getSigners() { return signers; // Fixes JDK 1.1 bug } myClass.getSigners()[0] = “ Sun ” ; // Error

IGJ (Immutability Generic Java). Problem: unintended side effects Type system: –ReadOnly : Reference immutability (aliases may modify) –Immutable : Object immutability (object cannot change) Inspired by generics Syntax uses annotations, not generics

Demo of IGJ checker

IGJ case study Programs (128KLOC) –JOlden benchmarks –htmlparser library –tinySQL library –SVNKit subversion client –IGJ checker 4760 annotations (62133 possible locations)

IGJ case study results Representation exposure errors Constructors that left object in inconsistent state In SVNKit: –some getters have side effects –some setters have no side effects Used both reference and object immutability More opportunities for immutability in new code than old

Java syntax for Annotations on types Permits annotations in new locations List myList; myGraph = Graph) tmpGraph; class UnmodifiableList List { … } Planned for inclusion in Java 7 (“JSR 308”) Publicly-available implementation Implementation is backwards compatible –Optionally write annotations in comments List myList; –Compiles with any Java compiler

Case studies 4 checkers 360KLOC 75 bugs verified by a human and fixed See technical report from September 2007 (better today!)

Usability of pluggable checkers Scales to >200,000 LOC Found bugs in every codebase –More important: prevents bugs! Few false positives Programmers found the checkers easy to use Is it too verbose? 1 per 75 lines 124 annotations in 220KLOC revealed 11 bugs –Possible to annotate part of program –Fewer annotations in new code

Creating your own checker Most users do not need to Basic functionality: mention annotation on javac command line Simple rules: declarative syntax (meta-annotations) Special rules: override a few methods –For NonNull: dereferences –For Interned: equality Examples of properties you can check: –General-purpose (tainting) –Project-specific (string formatting)

Your turn to use pluggable type checkers Webpage: Search for “JSR 308” or “Annotations on Java types” – –Inference tools: NonNull, Readonly –Checkers Framework for writing new checkers –JSR 308 compiler (patch to OpenJDK)‏ Go forth and prevent bugs!