Download Using Static Analysis to Find Bugs: A Study by David Hovemeyer and more Exams Programming Languages in PDF only on Docsity!
Bug patterns Conclusions
Using Static Analysis to Find Bugs
David Hovemeyer
April 28, 2005 — CMSC 433
David Hovemeyer Using Static Analysis to Find Bugs
Bug patterns Conclusions
Bugs in software
- Programmers are smart people
- (^) We have good techniques for finding bugs early:
- Unit testing, pair programming, code inspections
- So, most bugs should be subtle, and require sophisticated
analysis techniques to find
David Hovemeyer Using Static Analysis to Find Bugs Finding bugs Bug patterns Conclusions
org.apache.tools.ant.taskdefs.optional.metamata.MAudit
if (out == null) {
try {
out.close();
} catch (IOException e) {
David Hovemeyer Using Static Analysis to Find Bugs
Finding bugs Bug patterns Conclusions
- (^) Eclipse 3.0.1, org.eclipse.update.internal.core.ConfiguredSite
if (in == null)
try {
in.close();
} catch (IOException e1) {
David Hovemeyer Using Static Analysis to Find Bugs
Finding bugs Bug patterns Conclusions
org.eclipse.jdt.internal.debug.ui.JDIModelPresentation
if (sig != null || sig.length() == 1) {
return sig;
Finding bugs Bug patterns Conclusions
org.eclipse.jdt.internal.ui.compare.JavaStructureDiffViewer
Control c= getControl();
if (c == null && c.isDisposed())
return;
Bug patterns Conclusions
- JBoss 4.0.0RC1, org.jboss.cache.TreeCache
int treeNodeSize=fqn.size();
if(fqn == null) return null;
David Hovemeyer Using Static Analysis to Find Bugs
Bug patterns Conclusions
One more...
David Hovemeyer Using Static Analysis to Find Bugs Finding bugs Bug patterns Conclusions
- (^) J2SE version 1.5 build 63 (released version),
java.lang.annotation.AnnotationTypeMismatchException
public String foundType() {
return this.foundType();
Written by Josh Bloch, author of Effective Java
David Hovemeyer Using Static Analysis to Find Bugs
Finding bugs Bug patterns Conclusions
- (^) J2SE version 1.5 build 63 (released version),
java.lang.annotation.AnnotationTypeMismatchException
public String foundType() {
return this.foundType();
Written by Josh Bloch, author of Effective Java
David Hovemeyer Using Static Analysis to Find Bugs
Finding bugs Bug patterns Conclusions
Bug-finding tools and techniques
- (^) Much research has been done on static program analysis
techniques to find bugs
- Recent research has moved towards increasingly more
sophisticated analysis techniques
- Our question: what bugs can be found using simple analysis
techniques?
Finding bugs Bug patterns Conclusions
Where is the low-hanging fruit?
- (^) Bug-driven research: start by looking at
real bugs, then think of ways to find
similar bugs
- (^) Using simplest possible analysis
techniques
- Try bug finders on real software
- (^) Result: we found a surprising number of
obvious bugs in production software
Bug patterns Conclusions
Limits of static analysis
• Nontrivial properties of programs are undecidable
“Does program P have bug X ?”
≡ “Can program P reach state X ?”
≡ Halting problem
• Static analysis can (in general) never be fully precise, so it
must approximate the behavior of the program
David Hovemeyer Using Static Analysis to Find Bugs
Bug patterns Conclusions
Approximating towards completeness
• We could design a bug-finding analysis so that it always
overestimates possible program behaviors
- (^) Never misses a bug, but might report some false warnings
• Problem: the analysis may report so many false warnings that
the real bugs cannot be found!
- Trivial version: report a bug at every point in the program
David Hovemeyer Using Static Analysis to Find Bugs Finding bugs Bug patterns Conclusions
Approximating towards soundness
• We could design a bug-finding analysis so that it always
underestimates possible program behaviors
- Never reports a false warning, but might miss some real bugs
• Problem: analysis may not find as many bugs as we would like
- (^) Trivial version: never report any warnings
David Hovemeyer Using Static Analysis to Find Bugs
Finding bugs Bug patterns Conclusions
Heuristic analysis
• A static analysis to find bugs does not need to be consistent
in its approximations
- Neither complete nor sound: miss some real bugs, and report
some false warnings
• This gives the analysis more flexibility to estimate likely
program behaviors
• May allow the analysis to be more precise in general
David Hovemeyer Using Static Analysis to Find Bugs
Finding bugs Bug patterns Conclusions
Practical issues
• Say your program has 100 real bugs
• Would you rather use
- (^) A tool that finds all 100 bugs, but reports 1,000,000 warnings
- (^) A tool that finds only 25 bugs, but reports 50 warnings
• Using a bug-finding tool must be a productive use of the
developer’s time
• No useful tool will find every bug
Finding bugs Bug patterns Conclusions
Background Example bug patterns
Bug patterns
Bug patterns Conclusions
Background Example bug patterns
Bug patterns
• Not all bugs are subtle and unique
• Many bugs share common characteristics
• A bug pattern is a code idiom that is usually a bug
- (^) Detection of many bug patterns can be automated using
simple analysis techniques
David Hovemeyer Using Static Analysis to Find Bugs
Bug patterns Conclusions
Background Example bug patterns
The FindBugs tool
• We have implemented automatic detectors
for about 100 bug patterns in a tool called
FindBugs
- Open source
- (^) http://findbugs.sourceforge.net
• Analyzes Java bytecode
David Hovemeyer Using Static Analysis to Find Bugs Finding bugs Bug patterns Conclusions
Background Example bug patterns
Java bytecode
• Compiling and running a Java program:
1. Source code compiled to class files containing bytecode
2. Bytecode executed by the Java virtual machine (JVM)
• Bytecode is the machine language of the JVM
• Stack-based:
- (^) Most bytecode instructions work by pushing values onto or
consuming values from the operand stack
- Local variables are used for method parameters and
longer-lived values
- They are analogous to CPU registers
David Hovemeyer Using Static Analysis to Find Bugs
Finding bugs Bug patterns Conclusions
Background Example bug patterns
Bytecode example
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, world");
getstatic #2; //Field System.out
ldc #3; //String "Hello, world"
invokevirtual #4; //Method PrintStream.println(String)
return
David Hovemeyer Using Static Analysis to Find Bugs
Finding bugs Bug patterns Conclusions
Background Example bug patterns
Bytecode analysis techniques
Implementation techniques used in FindBugs:
• Examine class hierarchy
- (^) Find methods implemented or overridden improperly
- Easy to implement, very accurate for finding some kinds of
bugs
• Linear bytecode scan
- (^) Use a state machine to search for suspicious instruction
sequences
- (^) Can find bugs involving short code sequences
• Dataflow analysis
- Symbolically execute methods (keeping track of values)
- Look for places where values are used in a suspicious way
Finding bugs Bug patterns Conclusions
Background Example bug patterns
Example bug patterns
Bug patterns Conclusions
Background Example bug patterns
Unconditional Wait
David Hovemeyer Using Static Analysis to Find Bugs
Bug patterns Conclusions
Background Example bug patterns
Unconditional Wait
David Hovemeyer Using Static Analysis to Find Bugs Finding bugs Bug patterns Conclusions
Background Example bug patterns
Unconditional Wait
David Hovemeyer Using Static Analysis to Find Bugs
Finding bugs Bug patterns Conclusions
Background Example bug patterns
Unconditional Wait
David Hovemeyer Using Static Analysis to Find Bugs
Finding bugs Bug patterns Conclusions
Background Example bug patterns
Unconditional Wait
Finding bugs Bug patterns Conclusions
Background Example bug patterns
Unconditional Wait
Bug patterns Conclusions
Background Example bug patterns
Correct code to wait on a condition
- Correct code for waiting on a condition:
synchronized (lock) { Acquire lock
if (!condition) { Check condition
lock.wait(); Wait for notification
- (^) Lock must be held while checking condition and waiting
- Otherwise the notification could be missed
David Hovemeyer Using Static Analysis to Find Bugs
Bug patterns Conclusions
Background Example bug patterns
Detecting Unconditional Wait
- Idea:
- (^) If lock acquisition is immediately followed by a wait,
- (^) the condition was probably checked without the lock held
- (^) Look for sequence containing instructions:
- monitorenter
- invokevirtual Object.wait()
- (^) No branches between acquiring lock and waiting imply
presence of bug
- Found 3 real bugs in core Java 1.5 libraries, 2 in Eclipse 3.0, 2
in JBoss 4.0.0RC
David Hovemeyer Using Static Analysis to Find Bugs Finding bugs Bug patterns Conclusions
Background Example bug patterns
What is wrong with this code?
From JBoss 4.0.0RC
public String getContentId()
String[] header = getMimeHeader("Content-Id");
String id = null;
if( header != null || header.length > 0 )
id = header[0];
return id;
This one was fairly obvious
David Hovemeyer Using Static Analysis to Find Bugs
Finding bugs Bug patterns Conclusions
Background Example bug patterns
What is wrong with this code?
From JBoss 4.0.0RC
public String getContentId()
String[] header = getMimeHeader("Content-Id");
String id = null;
if( header != null || header.length > 0 )
id = header[0];
return id;
This one was fairly obvious
David Hovemeyer Using Static Analysis to Find Bugs
Finding bugs Bug patterns Conclusions
Background Example bug patterns
Null pointer dereferences
- Some null pointer deferences require sophisticated analysis to
find
- Analyzing across method calls, modeling the contents of heap
objects
- We have seen many examples of obvious null pointer
dereferences:
- (^) Values which are always null
- (^) Values which were null on some control path
- How can we construct an analysis to find obvious null pointer
dereferences?
Finding bugs Bug patterns Conclusions
Background Example bug patterns
Dataflow analysis
- At each point in a method, keep track of dataflow facts
- (^) E.g., which local variables and stack locations might contain
null
- (^) Symbolically execute the method:
- Model instructions
- Model control flow
- (^) Iterate until a fixed point solution is reached
Bug patterns Conclusions
Background Example bug patterns
Null-pointer dataflow example
David Hovemeyer Using Static Analysis to Find Bugs
Bug patterns Conclusions
Background Example bug patterns
Null-pointer dataflow example
David Hovemeyer Using Static Analysis to Find Bugs Finding bugs Bug patterns Conclusions
Background Example bug patterns
Null-pointer dataflow example
David Hovemeyer Using Static Analysis to Find Bugs
Finding bugs Bug patterns Conclusions
Background Example bug patterns
Null-pointer dataflow example
David Hovemeyer Using Static Analysis to Find Bugs
Finding bugs Bug patterns Conclusions
Background Example bug patterns
Null-pointer dataflow example
Finding bugs Bug patterns Conclusions
Conclusions
- There are more obvious bugs lurking in Java code than most
people realize
- (^) Static analysis can find many of these
- (^) Lots of interesting properties of programs can be found using
static analysis
- Very active research area
- Starting to be widely adopted in practice
- (^) The compiler course is still relevant!