DebugChecks.rst 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. ============
  2. Debug Checks
  3. ============
  4. .. contents::
  5. :local:
  6. The analyzer contains a number of checkers which can aid in debugging. Enable
  7. them by using the "-analyzer-checker=" flag, followed by the name of the
  8. checker.
  9. General Analysis Dumpers
  10. ========================
  11. These checkers are used to dump the results of various infrastructural analyses
  12. to stderr. Some checkers also have "view" variants, which will display a graph
  13. using a 'dot' format viewer (such as Graphviz on macOS) instead.
  14. - debug.DumpCallGraph, debug.ViewCallGraph: Show the call graph generated for
  15. the current translation unit. This is used to determine the order in which to
  16. analyze functions when inlining is enabled.
  17. - debug.DumpCFG, debug.ViewCFG: Show the CFG generated for each top-level
  18. function being analyzed.
  19. - debug.DumpDominators: Shows the dominance tree for the CFG of each top-level
  20. function.
  21. - debug.DumpLiveVars: Show the results of live variable analysis for each
  22. top-level function being analyzed.
  23. - debug.DumpLiveStmts: Show the results of live statement analysis for each
  24. top-level function being analyzed.
  25. - debug.ViewExplodedGraph: Show the Exploded Graphs generated for the
  26. analysis of different functions in the input translation unit. When there
  27. are several functions analyzed, display one graph per function. Beware
  28. that these graphs may grow very large, even for small functions.
  29. Path Tracking
  30. =============
  31. These checkers print information about the path taken by the analyzer engine.
  32. - debug.DumpCalls: Prints out every function or method call encountered during a
  33. path traversal. This is indented to show the call stack, but does NOT do any
  34. special handling of branches, meaning different paths could end up
  35. interleaved.
  36. - debug.DumpTraversal: Prints the name of each branch statement encountered
  37. during a path traversal ("IfStmt", "WhileStmt", etc). Currently used to check
  38. whether the analysis engine is doing BFS or DFS.
  39. State Checking
  40. ==============
  41. These checkers will print out information about the analyzer state in the form
  42. of analysis warnings. They are intended for use with the -verify functionality
  43. in regression tests.
  44. - debug.TaintTest: Prints out the word "tainted" for every expression that
  45. carries taint. At the time of this writing, taint was only introduced by the
  46. checks under experimental.security.taint.TaintPropagation; this checker may
  47. eventually move to the security.taint package.
  48. - debug.ExprInspection: Responds to certain function calls, which are modeled
  49. after builtins. These function calls should affect the program state other
  50. than the evaluation of their arguments; to use them, you will need to declare
  51. them within your test file. The available functions are described below.
  52. (FIXME: debug.ExprInspection should probably be renamed, since it no longer only
  53. inspects expressions.)
  54. ExprInspection checks
  55. ---------------------
  56. - ``void clang_analyzer_eval(bool);``
  57. Prints TRUE if the argument is known to have a non-zero value, FALSE if the
  58. argument is known to have a zero or null value, and UNKNOWN if the argument
  59. isn't sufficiently constrained on this path. You can use this to test other
  60. values by using expressions like "x == 5". Note that this functionality is
  61. currently DISABLED in inlined functions, since different calls to the same
  62. inlined function could provide different information, making it difficult to
  63. write proper -verify directives.
  64. In C, the argument can be typed as 'int' or as '_Bool'.
  65. Example usage::
  66. clang_analyzer_eval(x); // expected-warning{{UNKNOWN}}
  67. if (!x) return;
  68. clang_analyzer_eval(x); // expected-warning{{TRUE}}
  69. - ``void clang_analyzer_checkInlined(bool);``
  70. If a call occurs within an inlined function, prints TRUE or FALSE according to
  71. the value of its argument. If a call occurs outside an inlined function,
  72. nothing is printed.
  73. The intended use of this checker is to assert that a function is inlined at
  74. least once (by passing 'true' and expecting a warning), or to assert that a
  75. function is never inlined (by passing 'false' and expecting no warning). The
  76. argument is technically unnecessary but is intended to clarify intent.
  77. You might wonder why we can't print TRUE if a function is ever inlined and
  78. FALSE if it is not. The problem is that any inlined function could conceivably
  79. also be analyzed as a top-level function (in which case both TRUE and FALSE
  80. would be printed), depending on the value of the -analyzer-inlining option.
  81. In C, the argument can be typed as 'int' or as '_Bool'.
  82. Example usage::
  83. int inlined() {
  84. clang_analyzer_checkInlined(true); // expected-warning{{TRUE}}
  85. return 42;
  86. }
  87. void topLevel() {
  88. clang_analyzer_checkInlined(false); // no-warning (not inlined)
  89. int value = inlined();
  90. // This assertion will not be valid if the previous call was not inlined.
  91. clang_analyzer_eval(value == 42); // expected-warning{{TRUE}}
  92. }
  93. - ``void clang_analyzer_warnIfReached();``
  94. Generate a warning if this line of code gets reached by the analyzer.
  95. Example usage::
  96. if (true) {
  97. clang_analyzer_warnIfReached(); // expected-warning{{REACHABLE}}
  98. }
  99. else {
  100. clang_analyzer_warnIfReached(); // no-warning
  101. }
  102. - ``void clang_analyzer_numTimesReached();``
  103. Same as above, but include the number of times this call expression
  104. gets reached by the analyzer during the current analysis.
  105. Example usage::
  106. for (int x = 0; x < 3; ++x) {
  107. clang_analyzer_numTimesReached(); // expected-warning{{3}}
  108. }
  109. - ``void clang_analyzer_warnOnDeadSymbol(int);``
  110. Subscribe for a delayed warning when the symbol that represents the value of
  111. the argument is garbage-collected by the analyzer.
  112. When calling 'clang_analyzer_warnOnDeadSymbol(x)', if value of 'x' is a
  113. symbol, then this symbol is marked by the ExprInspection checker. Then,
  114. during each garbage collection run, the checker sees if the marked symbol is
  115. being collected and issues the 'SYMBOL DEAD' warning if it does.
  116. This way you know where exactly, up to the line of code, the symbol dies.
  117. It is unlikely that you call this function after the symbol is already dead,
  118. because the very reference to it as the function argument prevents it from
  119. dying. However, if the argument is not a symbol but a concrete value,
  120. no warning would be issued.
  121. Example usage::
  122. do {
  123. int x = generate_some_integer();
  124. clang_analyzer_warnOnDeadSymbol(x);
  125. } while(0); // expected-warning{{SYMBOL DEAD}}
  126. - ``void clang_analyzer_explain(a single argument of any type);``
  127. This function explains the value of its argument in a human-readable manner
  128. in the warning message. You can make as many overrides of its prototype
  129. in the test code as necessary to explain various integral, pointer,
  130. or even record-type values. To simplify usage in C code (where overloading
  131. the function declaration is not allowed), you may append an arbitrary suffix
  132. to the function name, without affecting functionality.
  133. Example usage::
  134. void clang_analyzer_explain(int);
  135. void clang_analyzer_explain(void *);
  136. // Useful in C code
  137. void clang_analyzer_explain_int(int);
  138. void foo(int param, void *ptr) {
  139. clang_analyzer_explain(param); // expected-warning{{argument 'param'}}
  140. clang_analyzer_explain_int(param); // expected-warning{{argument 'param'}}
  141. if (!ptr)
  142. clang_analyzer_explain(ptr); // expected-warning{{memory address '0'}}
  143. }
  144. - ``void clang_analyzer_dump( /* a single argument of any type */);``
  145. Similar to clang_analyzer_explain, but produces a raw dump of the value,
  146. same as SVal::dump().
  147. Example usage::
  148. void clang_analyzer_dump(int);
  149. void foo(int x) {
  150. clang_analyzer_dump(x); // expected-warning{{reg_$0<x>}}
  151. }
  152. - ``size_t clang_analyzer_getExtent(void *);``
  153. This function returns the value that represents the extent of a memory region
  154. pointed to by the argument. This value is often difficult to obtain otherwise,
  155. because no valid code that produces this value. However, it may be useful
  156. for testing purposes, to see how well does the analyzer model region extents.
  157. Example usage::
  158. void foo() {
  159. int x, *y;
  160. size_t xs = clang_analyzer_getExtent(&x);
  161. clang_analyzer_explain(xs); // expected-warning{{'4'}}
  162. size_t ys = clang_analyzer_getExtent(&y);
  163. clang_analyzer_explain(ys); // expected-warning{{'8'}}
  164. }
  165. - ``void clang_analyzer_printState();``
  166. Dumps the current ProgramState to the stderr. Quickly lookup the program state
  167. at any execution point without ViewExplodedGraph or re-compiling the program.
  168. This is not very useful for writing tests (apart from testing how ProgramState
  169. gets printed), but useful for debugging tests. Also, this method doesn't
  170. produce a warning, so it gets printed on the console before all other
  171. ExprInspection warnings.
  172. Example usage::
  173. void foo() {
  174. int x = 1;
  175. clang_analyzer_printState(); // Read the stderr!
  176. }
  177. - ``void clang_analyzer_hashDump(int);``
  178. The analyzer can generate a hash to identify reports. To debug what information
  179. is used to calculate this hash it is possible to dump the hashed string as a
  180. warning of an arbitrary expression using the function above.
  181. Example usage::
  182. void foo() {
  183. int x = 1;
  184. clang_analyzer_hashDump(x); // expected-warning{{hashed string for x}}
  185. }
  186. - ``void clang_analyzer_denote(int, const char *);``
  187. Denotes symbols with strings. A subsequent call to clang_analyzer_express()
  188. will expresses another symbol in terms of these string. Useful for testing
  189. relationships between different symbols.
  190. Example usage::
  191. void foo(int x) {
  192. clang_analyzer_denote(x, "$x");
  193. clang_analyzer_express(x + 1); // expected-warning{{$x + 1}}
  194. }
  195. - ``void clang_analyzer_express(int);``
  196. See clang_analyzer_denote().
  197. Statistics
  198. ==========
  199. The debug.Stats checker collects various information about the analysis of each
  200. function, such as how many blocks were reached and if the analyzer timed out.
  201. There is also an additional -analyzer-stats flag, which enables various
  202. statistics within the analyzer engine. Note the Stats checker (which produces at
  203. least one bug report per function) may actually change the values reported by
  204. -analyzer-stats.
  205. Output testing checkers
  206. =======================
  207. - debug.ReportStmts reports a warning at **every** statement, making it a very
  208. useful tool for testing thoroughly bug report construction and output
  209. emission.