33
Find Bugs is Easy http://findbugs.sourceforge.net/ umdFindbugs.png By- David H. Hovemeyer & William Pugh Summary by- Sahul Peermohammed

Find Bugs is Easy By- David H. Hovemeyer & William Pugh Summary by- Sahul Peermohammed

Embed Size (px)

Citation preview

Page 1: Find Bugs is Easy  By- David H. Hovemeyer & William Pugh Summary by- Sahul Peermohammed

Find Bugs is Easy

http://findbugs.sourceforge.net/umdFindbugs.png

By-

David H. Hovemeyer & William Pugh

Summary by-

Sahul Peermohammed

Page 2: Find Bugs is Easy  By- David H. Hovemeyer & William Pugh Summary by- Sahul Peermohammed

Introduction

• Bugs are a serious problem• Tools offer tremendous promise for improving software

quality.• Lots of research done in this field.• Some of the techniques proposed in this research

require sophisticated program analysis.• Find Bugs is a simple static analysis technique based on

the notion of Bug patterns.• A Bug pattern is a code idiom that is likely to be an error

Page 3: Find Bugs is Easy  By- David H. Hovemeyer & William Pugh Summary by- Sahul Peermohammed

Motivation• “US companies alone are spending almost £40 billion pound

annually on defective software- one third of software market”. [SourceNinja] 1

• “Smart ship USS Yorktown- US Navy ship was left dead in the water in 1997 for nearly 3 hours after a divide by zero error” [Wikipedia] 2

• Raise awareness of the large number of easily-detectable bugs that are not caught by traditional quality assurance techniques

• Suggest possibilities for future research Ways to integrate automatic bug finding techniques into the development

process

Page 4: Find Bugs is Easy  By- David H. Hovemeyer & William Pugh Summary by- Sahul Peermohammed

Techniques for finding bugs• Manual Code inspections

Requires intensive manual effort Influenced by what a piece of code is intended to do

• Automatic Code inspections1. Dynamic techniques

• Advantage is that they do not consider infeasible paths in a program.• Disadvantage being it is significant complex to achieve high statement or

branch coverage.

2. Static techniques• Formal proof of correctness.• Some techniques may be complete or incomplete.• Unsound techniques can identify “probable” bugs, but may miss some real

bugs and also may emit some inaccurate warnings.

Page 5: Find Bugs is Easy  By- David H. Hovemeyer & William Pugh Summary by- Sahul Peermohammed

Bug checkers vs. Style checkers• Bug Checker

Bug checker checks for violation of a specific correctness property, and which may cause misbehavior at runtime.

Affects users of the software. Requires judgment in order to understand the cause of the

bug, and to fix it without introducing new bugs.

• Style Checker Style checker determines if code violates a particular coding

style rule. Affects the developers working on the software. Fixed easily by changing code to adhere to style guidelines

and improving the understandability of the code

Page 6: Find Bugs is Easy  By- David H. Hovemeyer & William Pugh Summary by- Sahul Peermohammed

FindBugs Tool

• Open source.

• Originally contained 50 bug detectors. Now has more than 200 bug detectors.

• Pattern implemented using BCEL(Byte Code Engineering Library), an open source bytecode analysis and instrumentation library.

• Implementation strategies used by the detectors can be divided into several rough categories:

Class structure and inheritance hierarchy only Linear code scan Control sensitive Dataflow

Page 7: Find Bugs is Easy  By- David H. Hovemeyer & William Pugh Summary by- Sahul Peermohammed
Page 8: Find Bugs is Easy  By- David H. Hovemeyer & William Pugh Summary by- Sahul Peermohammed

Bug Patterns and detectors

• Authors have described a few bug patterns• Each detector falls

under following categories:

Single-threaded correctness

issue

Thread/synchronization

correctness issue Performance issue Security and

vulnerability to malicious untrusted code

Page 9: Find Bugs is Easy  By- David H. Hovemeyer & William Pugh Summary by- Sahul Peermohammed

Open Stream (OS)

• Good practice to close stream when it becomes unreachable.

• Although Finalizers ensure I/O streams are automatically closed when garbage collected.

• But no guarantee that this will happen in a timely manner.

• Why to close the stream after use?• Operating system has limited resource, and running out of them may

cause the program to misbehave.• The data stored in the stream’s buffer may never be written to the file.

Page 10: Find Bugs is Easy  By- David H. Hovemeyer & William Pugh Summary by- Sahul Peermohammed

• Uses dataflow analysis to determine all of the instructions reached by the definitions.

• Checks if a stream in the open state reaches the exit block of the control flow graph for a method.

private static File _parsePackagesFile(File packages, File destDir) {try {FileReader fr =new FileReader(packages);BufferedReader br =new BufferedReader(fr);...// fr/br are never closed

Open Stream (OS)

Page 11: Find Bugs is Easy  By- David H. Hovemeyer & William Pugh Summary by- Sahul Peermohammed

Uninitialized Read In Constructor (UR)

• Each field set to its default value for its type when constructed.

• Not useful to read a field before a value is written to it.

• This detector checks in Constructor whether any field is read before it is written.

public SnapshotRecordingMonitor(){log = Logger.getLogger(monitorName);history = new ArrayList(100);}

Page 12: Find Bugs is Easy  By- David H. Hovemeyer & William Pugh Summary by- Sahul Peermohammed

Non-Short-Circuit Boolean Operator (NS)

• Java’s && and || operators follows short-circuit evaluation.

• Often used to test a reference value against null, and call a method if the reference is found not to be null.

• Non-short-circuiting & and | operators are also defined for boolean values.

• Programmer’s unintentionally use one of these operators where they intended to use a short-circuiting boolean operator.

• Because both boolean expressions are evaluated unconditionally, a null pointer exception may result.

if(cheatSheet != null &

cheatSheet.getTitle() != null)

return cheatSheet.getTitle();

Page 13: Find Bugs is Easy  By- David H. Hovemeyer & William Pugh Summary by- Sahul Peermohammed

Non-serializable Serializable class (SE)

• Looks for classes that implement Serializable interface but cannot be serialized.

• Transient field: Objects will not be included when the object is serialized.

• Detector checks for two main factors: It contains a non-transient instance field of a type that does

not implement Serializable, or The superclass of the class is not serializable.

Page 14: Find Bugs is Easy  By- David H. Hovemeyer & William Pugh Summary by- Sahul Peermohammed

Static Field Modifiable By Untrusted Code(MS)

• Untrusted code allowed to modify static fields, thereby modifying the behavior of the library for all users.

• Detector looks for following factors:

A static non-final field has public or protected access. A static final field has public or protected access and references

a mutable structure such as an Array or Hashtable. A method returns a reference to a static mutable structure such

as an array or Hashtable.

Page 15: Find Bugs is Easy  By- David H. Hovemeyer & William Pugh Summary by- Sahul Peermohammed

Null Pointer Dereference (NP), RedundantComparison to Null (RCN)

• Detector uses data flow control to check for these types of issues.

• Null pointer dereference:Control c= getControl();

if (c == null && c.isDisposed())

return;

• Redundant null comparisonif (m.parent != this) {

add(m);

}

helpMenu = m;

if (m != null) {

…….

Page 16: Find Bugs is Easy  By- David H. Hovemeyer & William Pugh Summary by- Sahul Peermohammed

Cloneable Not Implemented Correctly (CN)

• Checks whether a class implements Cloneable interface correctly.

• Violation is to not call super.clone(), but rather allocate a new object by invoking a constructor.

• super.clone() ensure that they all delegate object creation to Object.clone(), which automatically creates a new object of the correct class.

• For e.g.• Suppose class ("A") does not call super.clone(), and class ("A") is extended by a

subclass ("B"), and the subclass B calls super.clone(), then it is likely that B's clone() method will return an object of type A, which violates the standard contract for clone().

Page 17: Find Bugs is Easy  By- David H. Hovemeyer & William Pugh Summary by- Sahul Peermohammed

Suspicious Equals Comparison (EC)

• Uses intra-procedural dataflow analysis to determine objects of types known to be incomparable are compared.

• According to the contract of equals(), objects of different classes should always compare as unequal.

• Result of this comparison will always be false at runtime.

Page 18: Find Bugs is Easy  By- David H. Hovemeyer & William Pugh Summary by- Sahul Peermohammed

Dropped Exception

• Looks for a try-catch block where the catch block is empty and the exception is slightly discarded.

• Programmers believe the exception cannot occur.

• Ignoring the exception can create incorrect anomalous behavior that could be very hard to track down.

• Exceptions should be handled or reported in some way, or they should be thrown out of the method.

Page 19: Find Bugs is Easy  By- David H. Hovemeyer & William Pugh Summary by- Sahul Peermohammed

Return Value Should Be Checked (RV)

• Java libraries have a number of immutable classes. For e.g. once constructed, Java String objects do not change

value.

• Methods that transform a String value do so by returning a new object

Any String method returning a String StringBuffer.toString() Any method of InetAddress, BigInteger, or BigDecimal MessageDigest.digest(byte[])

Page 20: Find Bugs is Easy  By- David H. Hovemeyer & William Pugh Summary by- Sahul Peermohammed

Read Return Should Be Checked (RR)

• java.io.InputStream class has two read() methods which read multiple bytes into a buffer.

• These methods return an integer indicating how many bytes were read.

• Programmers sometimes assume these methods always return the requested number of bytes. However, some input streams (e.g. sockets) can return short reads.

• If return value from these methods is ignored, the program may read uninitialized/stale elements in the buffer.

• The detector uses dataflow analysis & determines whether or not the location where the return value of a call to read() is stored is ever used by another instruction

Page 21: Find Bugs is Easy  By- David H. Hovemeyer & William Pugh Summary by- Sahul Peermohammed

int length = input.readInt();byte[] byteArray = new byte[length];input.read(byteArray, 0, length);if (length >= 4){...

Read Return Should Be Checked (RR)

Page 22: Find Bugs is Easy  By- David H. Hovemeyer & William Pugh Summary by- Sahul Peermohammed

Unconditional Wait (UW)

• Threads using wait() and notify() is a frequent source of errors in multithreaded programs.

• Looks for code where a wait is performed unconditionally in a synchronized block.

• Indicates that the condition associated with the wait was checked without a lock held and notification performed by another thread could have been missed.

• Detector for this bug pattern uses a linear scan over method’s bytecode.

// If we are not enabled, then waitif (!enabled) {try {synchronized (lock) {lock.wait();...

Page 23: Find Bugs is Easy  By- David H. Hovemeyer & William Pugh Summary by- Sahul Peermohammed

Wait Not In Loop (Wa)• Similar to the previous problem.

synchronized (object) { if (<condition does not hold>) { object.wait(); } // Proceed when condition holds} Should be:synchronized (object) { while (<condition does not hold>) { object.wait(); } // Proceed when condition holds} https://weblogs.java.net/blog/caroljmcdonald/archive/2011/12/21/finding-bugs-matter-findbugs

Page 24: Find Bugs is Easy  By- David H. Hovemeyer & William Pugh Summary by- Sahul Peermohammed

Equal Objects Must Have Equal Hashcodes(HE)

• For Java objects to be stored in HashMaps and HashSets, they must implement both the equals(Object) and hashCode() methods if either of them is overridden.

• Objects which compare as equal must have the same hashcode.

• Consider a case where a class overrides equals() but not hashCode(). The default implementation of hashCode() in the Object class returns an arbitrary value assigned by the virtual machine.

• It might result in objects of this class to be equal without having the same hashcode.

• Detector checks for the easy cases such as: Classes which redefine equals(Object) but inherit the default implementation of hashCode() Classes which redefine hashCode() but do not redefine equals(Object)

• Detector uses simple analysis of method signatures and the class hierarchy.

Page 25: Find Bugs is Easy  By- David H. Hovemeyer & William Pugh Summary by- Sahul Peermohammed

Bad Covariant Definition of Equals (Eq)

• Classes may override the equals(Object) method.

• Programmers sometimes mistakenly use the type of their class Foo as the type of the parameter to equals():

public boolean equals(Foo obj) {...}

• Does not override the version in the Object class, which may lead to unexpected behavior at runtime.

• If the class is used with one of the standard collection classes which expect that the standard equals(Object) method is overridden.

• Detector simply examines the method signatures of a class and its superclasses.

Page 26: Find Bugs is Easy  By- David H. Hovemeyer & William Pugh Summary by- Sahul Peermohammed

Classification of Bug Patterns

• Fix is a judgment call • Some bug pattern detectors are very accurate, but determining whether to fix is

up to the programmer.

• False positives.• Some the bug detectors admit false positives, and report warnings in cases

where the situation described by the warning does not, in fact occur

• Mostly harmless bugs• The warning may reflect a violation of good programming practice but be

unlikely to cause problems in practice.

• Serious bugs• warning is accurate and in our judgment reflects a serious bug that warrants

fixing

Page 27: Find Bugs is Easy  By- David H. Hovemeyer & William Pugh Summary by- Sahul Peermohammed

Evaluation• Authors ran FIndBugs(v 0.8.4) on a few applications and classified the bugs

reported under several categories as shown in the following table :

• GNU Classpath, version 0.08• an open source implementation of the core Java runtime libraries

• rt.jar from Sun JDK 1.5.0, build 59• Sun’s implementation of the APIs for J2SE

• Eclipse, version 3.0• IDE

• DrJava, version stable-20040326• IDE

• JBoss, version 4.0.0RC1• Java application server

• jEdit, version 4.2pre15• programmer’s text editor

Page 28: Find Bugs is Easy  By- David H. Hovemeyer & William Pugh Summary by- Sahul Peermohammed
Page 29: Find Bugs is Easy  By- David H. Hovemeyer & William Pugh Summary by- Sahul Peermohammed
Page 30: Find Bugs is Easy  By- David H. Hovemeyer & William Pugh Summary by- Sahul Peermohammed
Page 31: Find Bugs is Easy  By- David H. Hovemeyer & William Pugh Summary by- Sahul Peermohammed

Why Bugs Occur• Everyone makes dumb mistakes

• Programmer intended to use the && operator, but mistakenly used the || operator.

• Java offers many opportunities for latent bugs• Number of patterns and requirements that are not checked by the

compiler but simply result in runtime errors when violated.

• Programming with threads is harder than people think.• Programmers are not as scared of using threads as they should be• Concurrency bugs are especially problematic because they can be

extremely difficult to reproduce.

Page 32: Find Bugs is Easy  By- David H. Hovemeyer & William Pugh Summary by- Sahul Peermohammed

Questions & Feedback?

Thank You!

32

Page 33: Find Bugs is Easy  By- David H. Hovemeyer & William Pugh Summary by- Sahul Peermohammed

References• 1 => http

://www.itproportal.com/2012/01/26/software-bugs-arent-cheap-so-why-arent-more-companies-doing-more-stop-them/• 2 => http://web.archive.org/web/20071213201037/http://

www.gcn.com/print/17_17/33727-1.html?topic=news• Finding bug is easy: http://

dl.acm.org/ft_gateway.cfm?id=1052895&type=pdf&CFID=298767656&CFTOKEN=18408623• Bad Covariant Definition of Equals (Eq):

https://weblogs.java.net/blog/caroljmcdonald/archive/2011/12/21/finding-bugs-matter-findbugs