Detecting Assumptions on Deterministic Implementations of Non-deterministic Specifications August Shi, Alex Gyori, Owolabi Legunsen, Darko Marinov 4/12/2016.

Slides:



Advertisements
Similar presentations
Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
Advertisements

Sets and Maps Part of the Collections Framework. 2 The Set interface A Set is unordered and has no duplicates Operations are exactly those for Collection.
Abstraction and Modular Reasoning for the Verification of Software Corina Pasareanu NASA Ames Research Center.
Problem Solving 5 Using Java API for Searching and Sorting Applications ICS-201 Introduction to Computing II Semester 071.
1 Various Methods of Populating Arrays Randomly generated integers.
Singleton vs utility class  at first glance, the singleton pattern does not seem to offer any advantages to using a utility class  i.e., a utility class.
Collections Framework A very brief look at Java’s Collection Framework David Davenport May 2010.
Collections Sets - no duplicates Lists - duplicates allowed Maps - key / value pairs A collection is an Object which contains other Objects. There are.
Generic Programming David Rabinowitz. March 3rd, 2004 Object Oriented Design Course 2 The problem Assume we have a nice Stack implementation. Our stack.
Sets and Maps Chapter 9. Chapter 9: Sets and Maps2 Chapter Objectives To understand the Java Map and Set interfaces and how to use them To learn about.
© The McGraw-Hill Companies, 2006 Chapter 17 The Java Collections Framework.
Generic Programming Amit Shabtay. March 3rd, 2004 Object Oriented Design Course 2 The Problem Assume we have a nice Stack implementation. Our stack receives.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L15 (Chapter 22) Java Collections.
05 - Containers DRAFT COPY © S. Uchitel, 2004 Container OrderedDuplicates BagsNOYES SetsNONO ListsYESYES MapsNO.
Lists in Java Part of the Collections Framework. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection.
Generic Programming David Rabinowitz. June 14, 2006 Object Oriented Design Course 2 The problem Assume we have a nice Stack implementation. Our stack.
AP Computer Science.  Not necessary but good programming practice in Java  When you override a super class method notation.
12-Jul-15 Lists in Java Part of the Collections Framework.
Chapter 19 Java Data Structures
272: Software Engineering Fall 2012 Instructor: Tevfik Bultan Lecture 8: Semi-automated test generation via UDITA.
Iterator COMP 401, Spring 2013 Lecture 07 1/31/2013.
Describing algorithms in pseudo code To describe algorithms we need a language which is: – less formal than programming languages (implementation details.
Sets and Maps Part of the Collections Framework. The Set interface A Set is unordered and has no duplicates Operations are exactly those for Collection.
(c) University of Washingtonhashing-1 CSC 143 Java Hashing Set Implementation via Hashing.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Java Classes, Interfaces, and Types 1.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
 2006 Pearson Education, Inc. All rights reserved Arrays.
Collections in Java. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection with no duplicates SortedSet.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
ISBN Chapter 7 Expressions and Assignment Statements.
The Java Collections Framework (Part 2) By the end of this lecture you should be able to: Use the HashMap class to store objects in a map; Create objects.
EECE 310: Software Engineering Iteration Abstraction.
CIS 280 Hashing and Hash Tables. Calendar Today: Hashing and Hash Tables Wednesday: Calculus due, work Friday: Cuckoo hashing and linear hashing Monday:
1 TCSS 143, Autumn 2004 Lecture Notes Java Collection Framework: Maps and Sets.
Lecture 131 CS110 Lecture 13 Thursday, March 11, 2004 Announcements –hw5 due tonight –Spring break next week –hw6 due two weeks from tonight Agenda –questions.
תוכנה 1 תרגול 8 – מבני נתונים גנריים. 2 Java Collections Framework Collection: a group of elements Interface Based Design: Java Collections Framework.
CS Data Structures I Chapter 2 Principles of Programming & Software Engineering.
3-February-2003cse Collections © 2003 University of Washington1 Java Collections CSE 403, Winter 2003 Software Engineering
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 13 Implementing.
Hashing as a Dictionary Implementation Chapter 19.
RED-BLACK TREE SEARCH THE FOLLOWING METHOD IS IN TreeMap.java:
Sadegh Aliakbary Sharif University of Technology Fall 2012.
1 Interfaces in Java’s Collection Framework Rick Mercer.
Chapter 3 Collections. Objectives  Define the concepts and terminology related to collections  Explore the basic structures of the Java Collections.
CSE 143 Lecture 2 More ArrayList ; classes and objects reading: 10.1; slides created by Marty Stepp and Hélène Martin
More Java: Static and Final, Abstract Class and Interface, Exceptions, Collections Framework 1 CS300.
Sets and Maps Part of the Collections Framework. 2 The Set interface A Set is unordered and has no duplicates Operations are exactly those for Collection.
Iteration Abstraction SWE Software Construction Fall 2009.
CMSC 202 Containers and Iterators. Container Definition A “container” is a data structure whose purpose is to hold objects. Most languages support several.
1 Collections. 2 Concept A collection is a data structure – actually, an object – to hold other objects, which let you store and organize objects in useful.
Sets and Maps Chapter 9. Chapter Objectives  To understand the Java Map and Set interfaces and how to use them  To learn about hash coding and its use.
Collections Dwight Deugo Nesa Matic
3-1 Java's Collection Framework Another use of polymorphism and interfaces Rick Mercer.
Part 1: Composition, Aggregation, and Delegation Part 2: Iterator COMP 401 Fall 2014 Lecture 10 9/18/2014.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
JAVA GENERICS Lecture 16 CS2110 – Spring 2016 Photo credit: Andrew Kennedy.
CS 5150 Software Engineering Lecture 21 Reliability 2.
Introduction to Java Collection. Java Collections What are they? –A number of pre-packaged implementations of common ‘container’ classes, such as LinkedLists,
Collections ABCD ABCD Head Node Tail Node array doubly linked list Traditional Arrays and linked list: Below is memory representation of traditional.
Sets and Maps Chapter 9.
Software Construction Lab 10 Unit Testing with JUnit
Mutation Testing Meets Approximate Computing
Part of the Collections Framework
Abstract Class As per dictionary, abstraction is the quality of dealing with ideas rather than events. For example, when you consider the case of ,
Containers and Iterators
Sets and Maps Chapter 9.
Mitigating the Effects of Flaky Tests on Mutation Testing
Presentation transcript:

Detecting Assumptions on Deterministic Implementations of Non-deterministic Specifications August Shi, Alex Gyori, Owolabi Legunsen, Darko Marinov 4/12/2016 ICST 2016 Chicago, Illinois CCF , CCF , CCF , CCF

Example Code and Test 2 public class Book { String author; String title; public Book(String author, String title) { this.author = author; this.title = title; } public String getStringRep() { JSONObject j = new JSONObject(); // JSONObject extends java.util.HashMap j.put("author", this.author); j.put("title", this.title); return j.toString(); // toString() iterates through entries } public class BookTest public void testGetStringRep() { Book b = new Book("A", "T"); assertEquals("{\"author\":\"A\",\"title\":\"T\"}", b.getStringRep()); } /** … This class makes no guarantees as to the order of the map … */ public class java.util.HashMap …

Non-deterministic Specifications A specification (spec) that allows multiple implementations with different outputs for a given input Good: Allow freedom of implementation Although specs are non-deterministic, underlying implementations are often deterministic Bad: Code that Assumes a Deterministic Implementation of a Non- deterministic Specification (ADINS) Such code can behave unexpectedly when run using a different underlying implementation that still satisfies the spec Such code can be a cause of flaky tests 3

Library NonDex (Non-Deterministic Explorer) A simple technique for detecting ADINS code 4 Test Method with Deterministic Spec calls outputs Method with Non-deterministic Spec calls outputs …

Library NonDex (Non-Deterministic Explorer) A simple technique for detecting ADINS code 5 NonDex Model calls … Method with Deterministic Spec calls Implementation 1 Implementation 2 Implementation n … outputs NonDex outputs Test

Library NonDex (Non-Deterministic Explorer) A simple technique for detecting ADINS code 6 … Method with Deterministic Spec Explore: Implementation 1 … Implementation n Test NonDex Model Implementation 1 Implementation 2 Implementation n … NonDex outputs calls outputs

Finding Non-deterministic Specs Searched Java Standard Library for method specs that are non-deterministic 1.Searched JavaDocs for these keywords: “order”, “deterministic”, “not specified” 2.Searched for methods that return an array Examined JavaDocs for all found methods Identified 31 method specs that are non-deterministic 7

Some Non-deterministic Specs java.util.Set.iterator(): “Returns an iterator over the elements in this set. The elements are returned in no particular order…” java.util.HashMap.entrySet(): “Returns a Set view of the mappings contained in this map…” “This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time.” java.lang.Class.getDeclaredMethods(): “Returns an array containing Method objects reflecting all the declared methods of the class … The elements in the returned array are not sorted and are not in any particular order.” 8

9 ClassMethodsCategory java.lang.ObjecthashCodeRandom java.util.HashMap*keySet, values, entrySetPermute java.util.ConcurrentHashMap*keySet, values, entrySet, keys, elementsPermute java.io.Filelist, listFiles, listRootsPermute java.lang.ClassgetClasses, getDeclaredMethods, …Permute java.lang.reflect.MethodgetParameterAnnotationsPermute java.lang.reflect.FieldgetDeclaredAnnotationsPermute java.text.DateFormatSymbolsgetAvailableLocales, getZoneStrings Permute, Extend java.text.BreakIteratorgetAvailableLocalesPermute java.text.DecimalFormatSymbolsgetAvailableLocalesPermute java.text.NumberFormatgetAvailableLocalesPermute java.text.DateFormatgetAvailableLocalesPermute * Non-determinism is actually in internal iterator, exposed to outside by these methods NonDex Models for Specs Permute: method returns array/collection whose order is not specified NonDex model explores different permutations of elements

Different Levels of Non-determinism Some assumptions of determinism may be “acceptable” For example, if two calls of iterator() are made on the same, unmodified Set, should the iteration orders be the same? We introduce four “levels” for NonDex to “Permute” ONE: assumes deterministic implementation, but shuffles once, potentially different than underlying implementation EQ: shuffles differently only when objects are not “equal” ID: shuffles differently only when object address is different, or object has been modified FULL: shuffles differently with every call 10

01. Set s = new HashSet (); 02. s.add(1); s.add(2); 03. Integer[] a = s.toArray(); 04. // assertArrayEquals(a, new Integer[]{1, 2}); // FULL, ID, EQ, ONE can fail // assertArrayEquals(a, s.toArray()); // FULL can fail s.contains(1); 09. // assertArrayEquals(a, s.toArray()); // FULL can fail s.add(3); s.remove(3); 12. // assertArrayEquals(a, s.toArray()); // FULL, ID can fail Set t = new HashSet (); 15. t.add(1); t.add(2); 16. // assertArrayEquals(a, t.toArray()); // FULL, ID can fail Set u = new HashSet (); 19. u.add(3); u.add(4); 20. Integer[] b = u.toArray(); 21. // assertEquals(a[0] < a[1], b[0] < b[1]); // FULL, ID, EQ can fail 11

NonDex Model Exploration In our prototype, NonDex models use seeds to control which implementations to use Models use seeds for different java.util.Random calls that represent the different implementations We execute code with NonDex multiple times, each using different seeds, to explore different behaviors 12

Research Questions How many flaky tests are due to ADINS code? In open-source projects? In student submissions? How many seeds should be used to likely find all flaky tests due to ADINS code? How often are developers making “acceptable” assumptions about non-determinism? 13

Experimental Setup Tool (also called NonDex) is implemented for OpenJDK 8 Models implemented by modifying code in OpenJDK 8 Use modified NonDex JVM in place of the OpenJDK 8 JVM Evaluation projects: 195 open-source GitHub projects that build with Maven First run each project’s tests with 10 different seeds If a test with different behavior is found, run project’s tests with 100 different seeds 72 student submissions from Software Engineering I course Run each student submission’s tests with 100 different seeds 14

Open-source Project Results 15 FULLIDEQONE Flaky Tests Detected6054 Total Seeds Detecting4362/ / / /6000 Min Seeds Detecting8000 Max Seeds Detecting100

16 ProjectTestClass#TestNameFULLIDEQONE reflectasmFieldAccessTest#testIndexSetAndGet48000 joda-timeTestDateTimeZone#testGetShortName3553 oryxTextUtilsTest#testJSONMap visualeeJPAExaminerTest#testFindAndSetAttributes…8556 commons-cliOptionGroupTest#testToString42000 commons-langMultilineRecursiveToStringStyleTest#boolArray100 easy-batchPrinterTest#testPrettyPrinting scribe-javaMapUtilsTest#shouldPrettyPrintMap geoserver-managerGSLLayerEncoder21Test#testMetadata handlebars.javaTagTypeTest#collectSectionAndVars100 jscepDefaultCertStoreInspectorTest#example junitMethodSorterTest#testJvmMethodSorter org-jsonTestSuite#testJSONStringerObject slf4jEventLoggerTest#testEventLogger wsdoc CollectionTests#testBasicSets Subset of Flaky Tests Detected

Example Flaky Test (from commons-cli) 17 public class OptionGroupTest { public void testToString() { OptionGroup g1 = new OptionGroup(); g1.addOption(new Option(null, "foo", false, "Foo")); g1.addOption(new Option(null, "bar", false, "Bar")); if (!"[--bar Bar, --foo Foo]".equals(g1.toString())) { assertEquals("[--foo Foo, --bar Bar]", g1.toString()); }...}} public class OptionGroup... { Map om = new HashMap(); public OptionGroup addOption(Option option) { om.put(option.getKey(), option); return this;} public String toString() { StringBuilder buff = new StringBuilder(); Iterator iter = getOptions().iterator(); buff.append("["); while (iter.hasNext()) { /*... populate buff with the values in iter... */ } return buff.toString();}}

Student Submission Results Flaky tests mostly due to asserting result of method call is exactly equal to some String (similar beginning example) We also used Java PathFinder (JPF) to do systematic exploration Replace java.util.Random calls with JPF choice points Using JPF choice points did not detect any more flaky tests than using java.util.Random (with 100 seeds) 18 FULLIDEQONE Flaky Tests Found Total Seeds Detected8159/ / / /11000 Min Seeds Detected37000 Max Seeds Detected

Conclusion Non-deterministic specs give freedom to implementers, but code that assumes deterministic implementations (ADINS) leads to undesirable behavior We develop a technique NonDex to detect ADINS code We detected 60 flaky tests in open-source projects, and 110 flaky tests in student submissions We plan to further research how to better detect, debug, and repair ADINS code We plan on releasing NonDex, but are currently having issues with OpenJDK/Oracle licensing 19 August Shi:

BACKUP 20

21 class HashMap {... class HashIterator {... /* Rename hasNext() -> original_hasNext() */ /* Rename nextNode() -> original_nextNode() */ Iterator NonDex_iter; HashIterator() {... List original = new ArrayList<>(); while (original_hasNext()) original.add(original_nextNode()); NonDex.shuffle(original, (NonDex.level == ID) ? System.identityHashCode(HashMap.this) + modCount : (NonDex.level == EQ) ? HashMap.this.hashCode() : 0); NonDex_iter = original.iterator(); } public final boolean hasNext() { return NonDex_iter.hasNext(); } final Node nextNode() { if (modCount != expectedModCount) throw new ConcurrentModificationException(); current = NonDex_iter.next(); return current; }

22 class NonDex { static int level; // FULL, ID, EQ, or ONE int seed =...; static Random full = new Random(seed); public static List shuffle(List l, int v) { int size = l.size(); Random rand = (level == FULL) ? full : // Full (level == ID) ? new Random(seed + v) : // Same object (level == EQ) ? new Random(seed + v) : // Equal object (level == ONE) ? new Random(seed); // Once for (int i = 0; i < size - 1; i++) { int s = rand.getNext(i, size); if (s == i) continue; T obj = l.get(i); l.set(i, l.get(s)); l.set(s, obj); } return l; }

Example Flaky Test (from scribe- java) 23 public class MapUtilsTest public void shouldPrettyPrintMap() { Map map = new HashMap<>(); map.put(1, "one"); map.put(2, "two"); map.put(3, "three"); map.put(4, "four"); assertEquals( "{ 1 -> one, 2 -> two, 3 -> three, 4 -> four }", MapUtils.toString(map)); } public class MapUtils { public static String toString(Map map) {... StringBuilder result = new StringBuilder(); for (Map.Entry entry : map.entrySet()) { result.append(String.format(", %s -> %s ", entry.getKey().toString(), entry.getValue().toString())); } return "{" + result.substring(1) + "}"; }