<p>What is Dynamic Code Analysis?</p>

<p>How is it different from <a href="https://stackoverflow.com/questions/49716/what-is-static-code-analysis">Static Code Analysis</a> (ie, what can it catch that can't be caught in static)?</p>

<p>I've heard of bounds checking and memory analysis - what are these?</p>

<p>What other things are checked using dynamic analysis?</p>

<p>-Adam</p>

[{'answer_id': 49954, 'author': 'David Schlosnagle', 'author_id': 1750, 'author_profile': 'https://Stackoverflow.com/users/1750', 'pm_score': 3, 'selected': False, 'text': '<p>Basically you instrument your code to analyze your software as it is running (dynamic) rather than just analyzing the software without running (static). Also see this <a href="http://developers.sun.com/learning/javaoneonline/j1sessn.jsp?sessn=TS-5711&amp;yr=2007&amp;track=4" rel="noreferrer">JavaOne presentation comparing the two</a>. <a href="http://valgrind.org/" rel="noreferrer">Valgrind</a> is one example dynamic analysis tool for C. You could also use code coverage tools like <a href="http://cobertura.sourceforge.net/" rel="noreferrer">Cobertura</a> or <a href="http://emma.sourceforge.net/" rel="noreferrer">EMMA</a> for Java analysis.</p>\n\n<p>From Wikipedia\'s <a href="http://en.wikipedia.org/wiki/Dynamic_program_analysis" rel="noreferrer">definition of dynamic program analysis</a>:</p>\n\n<blockquote>\n  <p>Dynamic program analysis is the\n  analysis of computer software that is\n  performed with executing programs\n  built from that software on a real or\n  virtual processor (analysis performed\n  without executing programs is known as\n  static code analysis). Dynamic program\n  analysis tools may require loading of\n  special libraries or even\n  recompilation of program code.</p>\n</blockquote>\n'}, {'answer_id': 49967, 'author': 'David Schmitt', 'author_id': 4918, 'author_profile': 'https://Stackoverflow.com/users/4918', 'pm_score': 2, 'selected': False, 'text': "<blockquote>\n  <p>Bounds checking</p>\n</blockquote>\n\n<p>This means runtime checks of array accesses. Contrary to C's laissez-faire approach to memory accesses and pointer arithmetic, other languages like Java or C# actually check whether or not a given array has the element one is trying to access.</p>\n"}, {'answer_id': 49984, 'author': 'Marcio Aguiar', 'author_id': 4213, 'author_profile': 'https://Stackoverflow.com/users/4213', 'pm_score': 6, 'selected': True, 'text': '<p>Simply put, static analysis collect information based on <strong>source code</strong> and dynamic analysis is based on the <strong>system execution</strong>, often using instrumentation.</p>\n<h2>Advantages of dynamic analysis</h2>\n<ul>\n<li>Is able to detect dependencies that are not possible to detect in static analysis. Ex.: dynamic dependencies using reflection, dependency injection, polymorphism.</li>\n<li>Can collect temporal information.</li>\n<li>Deals with real input data. During the static analysis it is difficult to impossible to know what files will be passed as input, what WEB requests will come, what user will click, etc.</li>\n</ul>\n<h2>Disadvantages of dynamic analysis</h2>\n<ul>\n<li>May negatively impact the performance of the application.</li>\n<li>Cannot guarantee the full coverage of the source code, as it\'s runs are based on user interaction or automatic tests.</li>\n</ul>\n<h2>Resources</h2>\n<p>There\'s many dynamic analysis tools in the market, being debuggers the most notorious one. On the other hand, it\'s still an academic research field. There\'s many researchers studying how to use dynamic analysis for better understanding of software systems. There\'s an annual workshop dedicated to <a href="http://www.cs.wisc.edu/woda-2008/" rel="noreferrer">dependency analysis.</a></p>\n'}, {'answer_id': 5263547, 'author': 'Ira Baxter', 'author_id': 120163, 'author_profile': 'https://Stackoverflow.com/users/120163', 'pm_score': 2, 'selected': False, 'text': '<p>You asked for a good explanation of "bounds checking and memory analysis" issues. </p>\n\n<p>Our <a href="http://www.semanticdesigns.com/Products/MemorySafety" rel="nofollow noreferrer">Memory Safety Check</a> tool instruments your application to watch at runtime for memory access errors (buffer overruns, array subscript errors, bad pointers, alloc/free errors).    The link contains\na detailed explanation complete with examples.   This <a href="https://stackoverflow.com/a/52612757/120163">SO answer</a> shows two programs that have pointers into dead stack frame, and how CheckPointer detects and reports the point of error in the source code </p>\n\n<p>A briefer example: C (and C++) infamously do not check accesses to arrays, to see if the access is inside the bounds of the array.  The benefit: well-designed program don\'t pay the cost of such a check in production mode.  The downside: buggy programs can touch things outside the array, and this can cause behavior which is very hard to understand; thus the buggy program is difficult to debug.   </p>\n\n<p>What a dynamic instrumentation tool like the Memory Safety Checker does, is associate some metadata with every pointer (e.g., the type of the thing to which the pointer "points", and if it is an array, the array bounds), and then check at runtime, any accesses via pointers to arrays, whether the array bound is violated.  The tool modifies the original program to collect the metadata where it is generated (e.g., on entry to scopes in which arrays are declared, or as the result of a malloc operation, etc.) and modifies the program at every array reference (written both as x[y] where either x or y is an array pointer and the the value is some type of integral type, similarly for *(x+y)!) to check the access.   Now if the program runs, and performs an out-of-bounds access, the check catches the error and it reported at the first place where it could be detected. [If  you think about it, you\'ll realize the instrumentation for metadata collection and checking has to be pretty clever, to handle all the variant cases a language like C may have.  Its actually hard to make this work completely).</p>\n\n<p>The good news is that now such access is reported early where it is easier to detect the problem and fix the program.    Such a tool isn\'t intended production use; one uses during development and testing to help verify absence of errors.    If there are no errors discovered, then one does a normal compile and runs the programs without the checks.</p>\n\n<p>This is an extremely good example of a dynamic analysis tool: the testing happens at runtime.</p>\n'}]
['https://Stackoverflow.com/questions/49937', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2915/']