SlideShare a Scribd company logo
Myths about static analysis. The third
myth - dynamic analysis is better than
static analysis
Author: Andrey Karpov
Date: 03.11.2011
While communicating with people on forums, I noticed there are a few lasting misconceptions
concerning the static analysis methodology. I decided to write a series of brief articles where I want to
show you the real state of things.
The third myth is: "Dynamic analysis performed by tools like valgrind for C/C++ is much better than
static code analysis".
The statement is rather strange. Dynamic and static analyses are just two different methodologies which
supplement each other. Programmers seem to understand it, but I hear it again and again that dynamic
analysis is better than static analysis.
Let me list advantages of static code analysis.
Diagnostics of all the branches in a program
Dynamic analysis in practice cannot cover all the branches of a program. After these words, fans of
valgrind tell me that one should create appropriate tests. They are right in theory. But anyone who tried
to create them understands how complicated and long it is. In practice, even good tests cover not more
than 80% of program code.
It is especially noticeable in code fragments handling non-standard/emergency situations. If you take an
old project and check it with a static analyzer, most errors will be detected in these very places. The
reason is that even if the project is old, these fragments stay almost untested. Here is a brief example to
show you what I mean (FCE Ultra project):
fp = fopen(name,"wb");
int x = 0;
if (!fp)
int x = 1;
The 'x' flag will not be equal to one if the file wasn't opened. It is because of such errors that something
goes wrong in programs: they crash or generate meaningless messages instead of adequate error
messages.
Scalability
To be able to check large projects through dynamic methods regularly, you have to create a special
infrastructure. You need special tests. You need to launch several instances of an application in parallel
with different input data.
Static analysis is scaled several times easier. Usually you need only a multi-core computer to run a tool
performing static analysis.
Analysis at a higher level
One of the advantages of dynamic analysis is that it knows what function and with what arguments is
being called. Consequently, it can check if the call is correct. Static analysis can't know it and can't check
arguments' values in most cases. This is a disadvantage of this method. But static analysis performs
analysis at a higher level than dynamic analysis. This feature allows a static analyzer to detect issues
which are correct from the viewpoint of dynamic analysis. Here is a simple example (ReactOS project):
void Mapdesc::identify( REAL dest[MAXCOORDS][MAXCOORDS] )
{
memset( dest, 0, sizeof( dest ) );
for( int i=0; i != hcoords; i++ )
dest[i][i] = 1.0;
}
Everything is good here from the viewpoint of dynamic analysis, while static analysis gives the alarm
because it is very suspicious that the number of bytes being cleared in an array coincides with the
number of bytes the pointer consists of.
Here you are another example from the Clang project:
MapTy PerPtrTopDown;
MapTy PerPtrBottomUp;
void clearBottomUpPointers() {
PerPtrTopDown.clear();
}
void clearTopDownPointers() {
PerPtrTopDown.clear();
}
Is there anything here dynamic analysis may find suspicious? Nothing. But a static analyzer can suspect
there is something wrong. The error is this: inside clearBottomUpPointers() there must be this code:
"PerPtrBottomUp.clear();".

More Related Content

Viewers also liked

Mobile vs. Desktop Browsers
Mobile vs. Desktop BrowsersMobile vs. Desktop Browsers
Mobile vs. Desktop Browsers
Climax
 
公共圖書館建設規劃工作的幾點思考
公共圖書館建設規劃工作的幾點思考公共圖書館建設規劃工作的幾點思考
公共圖書館建設規劃工作的幾點思考
Ophelia Tang
 
Future of Testing, Test Automation and The Quality Analyst
Future of Testing, Test Automation and The Quality AnalystFuture of Testing, Test Automation and The Quality Analyst
Future of Testing, Test Automation and The Quality Analyst
Anand Bagmar
 
Renaissance
RenaissanceRenaissance
Deciding what to build without killing each other
Deciding what to build without killing each otherDeciding what to build without killing each other
Deciding what to build without killing each other
Philip Likens
 
Employee performance review phrases
Employee performance review phrasesEmployee performance review phrases
Employee performance review phrases
origincooper
 
Get Internet Number Resources from ARIN (IPv4, IPv6, ASNs)
Get Internet Number Resources from ARIN (IPv4, IPv6, ASNs)Get Internet Number Resources from ARIN (IPv4, IPv6, ASNs)
Get Internet Number Resources from ARIN (IPv4, IPv6, ASNs)
ARIN
 
Snap: The SME's guide to the social media landscape
Snap: The SME's guide to the social media landscapeSnap: The SME's guide to the social media landscape
Snap: The SME's guide to the social media landscape
Snap
 
Questionnaire on Work Culture
Questionnaire on Work CultureQuestionnaire on Work Culture
Questionnaire on Work Culture
Venkata Vihari K
 
Tata Motors & JLR Merger
Tata Motors & JLR MergerTata Motors & JLR Merger
Tata Motors & JLR Merger
Rohit Dhoundiyal
 
Permendiknas Nomor 20 Tahun 2007: Standar Penilaian Pendidikan
Permendiknas Nomor 20 Tahun 2007: Standar Penilaian PendidikanPermendiknas Nomor 20 Tahun 2007: Standar Penilaian Pendidikan
Permendiknas Nomor 20 Tahun 2007: Standar Penilaian Pendidikan
Yani Pieter Pitoy
 
Ratio Analysis No. 1 CMD
Ratio Analysis No. 1 CMDRatio Analysis No. 1 CMD
Ratio Analysis No. 1 CMDknoxbusiness
 
InMobi Insights : The State of Mobile App Monetization, Q3 - 2014
InMobi Insights : The State of Mobile App Monetization, Q3 - 2014InMobi Insights : The State of Mobile App Monetization, Q3 - 2014
InMobi Insights : The State of Mobile App Monetization, Q3 - 2014
InMobi
 
Pmp 簡易快速記憶圖法
Pmp 簡易快速記憶圖法Pmp 簡易快速記憶圖法
Pmp 簡易快速記憶圖法Jack Liao
 
100 energizers
100 energizers100 energizers
100 energizersvickthorr
 
Say Cheese! Using Pictures to Teach Reading
Say Cheese! Using Pictures to Teach Reading Say Cheese! Using Pictures to Teach Reading
Say Cheese! Using Pictures to Teach Reading
Jennifer Jones
 
100 images for visual brainstorming
100 images for visual brainstorming100 images for visual brainstorming
100 images for visual brainstorming
Marc Heleven
 
Apps for the Connected World: Supercharge Customer Data with Code Halos
Apps for the Connected World: Supercharge Customer Data with Code HalosApps for the Connected World: Supercharge Customer Data with Code Halos
Apps for the Connected World: Supercharge Customer Data with Code Halos
Cognizant
 
Hardware trends 2015
Hardware trends 2015Hardware trends 2015
Hardware trends 2015
HAX
 

Viewers also liked (20)

Mobile vs. Desktop Browsers
Mobile vs. Desktop BrowsersMobile vs. Desktop Browsers
Mobile vs. Desktop Browsers
 
公共圖書館建設規劃工作的幾點思考
公共圖書館建設規劃工作的幾點思考公共圖書館建設規劃工作的幾點思考
公共圖書館建設規劃工作的幾點思考
 
Future of Testing, Test Automation and The Quality Analyst
Future of Testing, Test Automation and The Quality AnalystFuture of Testing, Test Automation and The Quality Analyst
Future of Testing, Test Automation and The Quality Analyst
 
Renaissance
RenaissanceRenaissance
Renaissance
 
Deciding what to build without killing each other
Deciding what to build without killing each otherDeciding what to build without killing each other
Deciding what to build without killing each other
 
Employee performance review phrases
Employee performance review phrasesEmployee performance review phrases
Employee performance review phrases
 
Get Internet Number Resources from ARIN (IPv4, IPv6, ASNs)
Get Internet Number Resources from ARIN (IPv4, IPv6, ASNs)Get Internet Number Resources from ARIN (IPv4, IPv6, ASNs)
Get Internet Number Resources from ARIN (IPv4, IPv6, ASNs)
 
Snap: The SME's guide to the social media landscape
Snap: The SME's guide to the social media landscapeSnap: The SME's guide to the social media landscape
Snap: The SME's guide to the social media landscape
 
Questionnaire on Work Culture
Questionnaire on Work CultureQuestionnaire on Work Culture
Questionnaire on Work Culture
 
Tata Motors & JLR Merger
Tata Motors & JLR MergerTata Motors & JLR Merger
Tata Motors & JLR Merger
 
Permendiknas Nomor 20 Tahun 2007: Standar Penilaian Pendidikan
Permendiknas Nomor 20 Tahun 2007: Standar Penilaian PendidikanPermendiknas Nomor 20 Tahun 2007: Standar Penilaian Pendidikan
Permendiknas Nomor 20 Tahun 2007: Standar Penilaian Pendidikan
 
Ratio Analysis No. 1 CMD
Ratio Analysis No. 1 CMDRatio Analysis No. 1 CMD
Ratio Analysis No. 1 CMD
 
InMobi Insights : The State of Mobile App Monetization, Q3 - 2014
InMobi Insights : The State of Mobile App Monetization, Q3 - 2014InMobi Insights : The State of Mobile App Monetization, Q3 - 2014
InMobi Insights : The State of Mobile App Monetization, Q3 - 2014
 
Pmp 簡易快速記憶圖法
Pmp 簡易快速記憶圖法Pmp 簡易快速記憶圖法
Pmp 簡易快速記憶圖法
 
100 energizers
100 energizers100 energizers
100 energizers
 
Domain Event - The Hidden Gem of DDD
Domain Event - The Hidden Gem of DDDDomain Event - The Hidden Gem of DDD
Domain Event - The Hidden Gem of DDD
 
Say Cheese! Using Pictures to Teach Reading
Say Cheese! Using Pictures to Teach Reading Say Cheese! Using Pictures to Teach Reading
Say Cheese! Using Pictures to Teach Reading
 
100 images for visual brainstorming
100 images for visual brainstorming100 images for visual brainstorming
100 images for visual brainstorming
 
Apps for the Connected World: Supercharge Customer Data with Code Halos
Apps for the Connected World: Supercharge Customer Data with Code HalosApps for the Connected World: Supercharge Customer Data with Code Halos
Apps for the Connected World: Supercharge Customer Data with Code Halos
 
Hardware trends 2015
Hardware trends 2015Hardware trends 2015
Hardware trends 2015
 

Similar to Myths about static analysis. The third myth - dynamic analysis is better than static analysis

Difficulties of comparing code analyzers, or don't forget about usability
Difficulties of comparing code analyzers, or don't forget about usabilityDifficulties of comparing code analyzers, or don't forget about usability
Difficulties of comparing code analyzers, or don't forget about usability
PVS-Studio
 
Difficulties of comparing code analyzers, or don't forget about usability
Difficulties of comparing code analyzers, or don't forget about usabilityDifficulties of comparing code analyzers, or don't forget about usability
Difficulties of comparing code analyzers, or don't forget about usability
Andrey Karpov
 
Difficulties of comparing code analyzers, or don't forget about usability
Difficulties of comparing code analyzers, or don't forget about usabilityDifficulties of comparing code analyzers, or don't forget about usability
Difficulties of comparing code analyzers, or don't forget about usability
PVS-Studio
 
Regular use of static code analysis in team development
Regular use of static code analysis in team developmentRegular use of static code analysis in team development
Regular use of static code analysis in team development
PVS-Studio
 
If the coding bug is banal, it doesn't meant it's not crucial
If the coding bug is banal, it doesn't meant it's not crucialIf the coding bug is banal, it doesn't meant it's not crucial
If the coding bug is banal, it doesn't meant it's not crucial
PVS-Studio
 
Static analysis as part of the development process in Unreal Engine
Static analysis as part of the development process in Unreal EngineStatic analysis as part of the development process in Unreal Engine
Static analysis as part of the development process in Unreal Engine
PVS-Studio
 
Regular use of static code analysis in team development
Regular use of static code analysis in team developmentRegular use of static code analysis in team development
Regular use of static code analysis in team development
PVS-Studio
 
Regular use of static code analysis in team development
Regular use of static code analysis in team developmentRegular use of static code analysis in team development
Regular use of static code analysis in team development
Andrey Karpov
 
Static analysis is most efficient when being used regularly. We'll tell you w...
Static analysis is most efficient when being used regularly. We'll tell you w...Static analysis is most efficient when being used regularly. We'll tell you w...
Static analysis is most efficient when being used regularly. We'll tell you w...
Andrey Karpov
 
An ideal static analyzer, or why ideals are unachievable
An ideal static analyzer, or why ideals are unachievableAn ideal static analyzer, or why ideals are unachievable
An ideal static analyzer, or why ideals are unachievable
PVS-Studio
 
0136 ideal static_analyzer
0136 ideal static_analyzer0136 ideal static_analyzer
0136 ideal static_analyzerPVS-Studio
 
Searching for bugs in Mono: there are hundreds of them!
Searching for bugs in Mono: there are hundreds of them!Searching for bugs in Mono: there are hundreds of them!
Searching for bugs in Mono: there are hundreds of them!
PVS-Studio
 
Static analysis is most efficient when being used regularly. We'll tell you w...
Static analysis is most efficient when being used regularly. We'll tell you w...Static analysis is most efficient when being used regularly. We'll tell you w...
Static analysis is most efficient when being used regularly. We'll tell you w...
PVS-Studio
 
We continue checking Microsoft projects: analysis of PowerShell
We continue checking Microsoft projects: analysis of PowerShellWe continue checking Microsoft projects: analysis of PowerShell
We continue checking Microsoft projects: analysis of PowerShell
PVS-Studio
 
Testing parallel programs
Testing parallel programsTesting parallel programs
Testing parallel programs
PVS-Studio
 
PVS-Studio for Visual C++
PVS-Studio for Visual C++PVS-Studio for Visual C++
PVS-Studio for Visual C++
Andrey Karpov
 
Three Interviews About Static Code Analyzers
Three Interviews About Static Code AnalyzersThree Interviews About Static Code Analyzers
Three Interviews About Static Code Analyzers
Andrey Karpov
 
Adaptation of the technology of the static code analyzer for developing paral...
Adaptation of the technology of the static code analyzer for developing paral...Adaptation of the technology of the static code analyzer for developing paral...
Adaptation of the technology of the static code analyzer for developing paral...
PVS-Studio
 
What do static analysis and search engines have in common? A good "top"!
What do static analysis and search engines have in common? A good "top"!What do static analysis and search engines have in common? A good "top"!
What do static analysis and search engines have in common? A good "top"!
PVS-Studio
 
PVS-Studio for Visual C++
PVS-Studio for Visual C++PVS-Studio for Visual C++
PVS-Studio for Visual C++
PVS-Studio
 

Similar to Myths about static analysis. The third myth - dynamic analysis is better than static analysis (20)

Difficulties of comparing code analyzers, or don't forget about usability
Difficulties of comparing code analyzers, or don't forget about usabilityDifficulties of comparing code analyzers, or don't forget about usability
Difficulties of comparing code analyzers, or don't forget about usability
 
Difficulties of comparing code analyzers, or don't forget about usability
Difficulties of comparing code analyzers, or don't forget about usabilityDifficulties of comparing code analyzers, or don't forget about usability
Difficulties of comparing code analyzers, or don't forget about usability
 
Difficulties of comparing code analyzers, or don't forget about usability
Difficulties of comparing code analyzers, or don't forget about usabilityDifficulties of comparing code analyzers, or don't forget about usability
Difficulties of comparing code analyzers, or don't forget about usability
 
Regular use of static code analysis in team development
Regular use of static code analysis in team developmentRegular use of static code analysis in team development
Regular use of static code analysis in team development
 
If the coding bug is banal, it doesn't meant it's not crucial
If the coding bug is banal, it doesn't meant it's not crucialIf the coding bug is banal, it doesn't meant it's not crucial
If the coding bug is banal, it doesn't meant it's not crucial
 
Static analysis as part of the development process in Unreal Engine
Static analysis as part of the development process in Unreal EngineStatic analysis as part of the development process in Unreal Engine
Static analysis as part of the development process in Unreal Engine
 
Regular use of static code analysis in team development
Regular use of static code analysis in team developmentRegular use of static code analysis in team development
Regular use of static code analysis in team development
 
Regular use of static code analysis in team development
Regular use of static code analysis in team developmentRegular use of static code analysis in team development
Regular use of static code analysis in team development
 
Static analysis is most efficient when being used regularly. We'll tell you w...
Static analysis is most efficient when being used regularly. We'll tell you w...Static analysis is most efficient when being used regularly. We'll tell you w...
Static analysis is most efficient when being used regularly. We'll tell you w...
 
An ideal static analyzer, or why ideals are unachievable
An ideal static analyzer, or why ideals are unachievableAn ideal static analyzer, or why ideals are unachievable
An ideal static analyzer, or why ideals are unachievable
 
0136 ideal static_analyzer
0136 ideal static_analyzer0136 ideal static_analyzer
0136 ideal static_analyzer
 
Searching for bugs in Mono: there are hundreds of them!
Searching for bugs in Mono: there are hundreds of them!Searching for bugs in Mono: there are hundreds of them!
Searching for bugs in Mono: there are hundreds of them!
 
Static analysis is most efficient when being used regularly. We'll tell you w...
Static analysis is most efficient when being used regularly. We'll tell you w...Static analysis is most efficient when being used regularly. We'll tell you w...
Static analysis is most efficient when being used regularly. We'll tell you w...
 
We continue checking Microsoft projects: analysis of PowerShell
We continue checking Microsoft projects: analysis of PowerShellWe continue checking Microsoft projects: analysis of PowerShell
We continue checking Microsoft projects: analysis of PowerShell
 
Testing parallel programs
Testing parallel programsTesting parallel programs
Testing parallel programs
 
PVS-Studio for Visual C++
PVS-Studio for Visual C++PVS-Studio for Visual C++
PVS-Studio for Visual C++
 
Three Interviews About Static Code Analyzers
Three Interviews About Static Code AnalyzersThree Interviews About Static Code Analyzers
Three Interviews About Static Code Analyzers
 
Adaptation of the technology of the static code analyzer for developing paral...
Adaptation of the technology of the static code analyzer for developing paral...Adaptation of the technology of the static code analyzer for developing paral...
Adaptation of the technology of the static code analyzer for developing paral...
 
What do static analysis and search engines have in common? A good "top"!
What do static analysis and search engines have in common? A good "top"!What do static analysis and search engines have in common? A good "top"!
What do static analysis and search engines have in common? A good "top"!
 
PVS-Studio for Visual C++
PVS-Studio for Visual C++PVS-Studio for Visual C++
PVS-Studio for Visual C++
 

Myths about static analysis. The third myth - dynamic analysis is better than static analysis

  • 1. Myths about static analysis. The third myth - dynamic analysis is better than static analysis Author: Andrey Karpov Date: 03.11.2011 While communicating with people on forums, I noticed there are a few lasting misconceptions concerning the static analysis methodology. I decided to write a series of brief articles where I want to show you the real state of things. The third myth is: "Dynamic analysis performed by tools like valgrind for C/C++ is much better than static code analysis". The statement is rather strange. Dynamic and static analyses are just two different methodologies which supplement each other. Programmers seem to understand it, but I hear it again and again that dynamic analysis is better than static analysis. Let me list advantages of static code analysis. Diagnostics of all the branches in a program Dynamic analysis in practice cannot cover all the branches of a program. After these words, fans of valgrind tell me that one should create appropriate tests. They are right in theory. But anyone who tried to create them understands how complicated and long it is. In practice, even good tests cover not more than 80% of program code. It is especially noticeable in code fragments handling non-standard/emergency situations. If you take an old project and check it with a static analyzer, most errors will be detected in these very places. The reason is that even if the project is old, these fragments stay almost untested. Here is a brief example to show you what I mean (FCE Ultra project): fp = fopen(name,"wb"); int x = 0; if (!fp) int x = 1; The 'x' flag will not be equal to one if the file wasn't opened. It is because of such errors that something goes wrong in programs: they crash or generate meaningless messages instead of adequate error messages.
  • 2. Scalability To be able to check large projects through dynamic methods regularly, you have to create a special infrastructure. You need special tests. You need to launch several instances of an application in parallel with different input data. Static analysis is scaled several times easier. Usually you need only a multi-core computer to run a tool performing static analysis. Analysis at a higher level One of the advantages of dynamic analysis is that it knows what function and with what arguments is being called. Consequently, it can check if the call is correct. Static analysis can't know it and can't check arguments' values in most cases. This is a disadvantage of this method. But static analysis performs analysis at a higher level than dynamic analysis. This feature allows a static analyzer to detect issues which are correct from the viewpoint of dynamic analysis. Here is a simple example (ReactOS project): void Mapdesc::identify( REAL dest[MAXCOORDS][MAXCOORDS] ) { memset( dest, 0, sizeof( dest ) ); for( int i=0; i != hcoords; i++ ) dest[i][i] = 1.0; } Everything is good here from the viewpoint of dynamic analysis, while static analysis gives the alarm because it is very suspicious that the number of bytes being cleared in an array coincides with the number of bytes the pointer consists of. Here you are another example from the Clang project: MapTy PerPtrTopDown; MapTy PerPtrBottomUp; void clearBottomUpPointers() { PerPtrTopDown.clear(); } void clearTopDownPointers() { PerPtrTopDown.clear(); } Is there anything here dynamic analysis may find suspicious? Nothing. But a static analyzer can suspect there is something wrong. The error is this: inside clearBottomUpPointers() there must be this code: "PerPtrBottomUp.clear();".