xref: /llvm-project/clang/docs/analyzer/developer-docs/DebugChecks.rst (revision f6d557ee34b6bbdb1dc32f29e34b4a4a8ad35e81)
11a17032bSKristof Umann============
21a17032bSKristof UmannDebug Checks
31a17032bSKristof Umann============
41a17032bSKristof Umann
51a17032bSKristof Umann.. contents::
61a17032bSKristof Umann   :local:
71a17032bSKristof Umann
81a17032bSKristof UmannThe analyzer contains a number of checkers which can aid in debugging. Enable
91a17032bSKristof Umannthem by using the "-analyzer-checker=" flag, followed by the name of the
101a17032bSKristof Umannchecker.
111a17032bSKristof Umann
121a17032bSKristof Umann
131a17032bSKristof UmannGeneral Analysis Dumpers
141a17032bSKristof Umann========================
151a17032bSKristof Umann
161a17032bSKristof UmannThese checkers are used to dump the results of various infrastructural analyses
171a17032bSKristof Umannto stderr. Some checkers also have "view" variants, which will display a graph
18d45eaf94SJ. Ryan Stinnettusing a 'dot' format viewer (such as Graphviz on macOS) instead.
191a17032bSKristof Umann
201a17032bSKristof Umann- debug.DumpCallGraph, debug.ViewCallGraph: Show the call graph generated for
211a17032bSKristof Umann  the current translation unit. This is used to determine the order in which to
221a17032bSKristof Umann  analyze functions when inlining is enabled.
231a17032bSKristof Umann
241a17032bSKristof Umann- debug.DumpCFG, debug.ViewCFG: Show the CFG generated for each top-level
251a17032bSKristof Umann  function being analyzed.
261a17032bSKristof Umann
271a17032bSKristof Umann- debug.DumpDominators: Shows the dominance tree for the CFG of each top-level
281a17032bSKristof Umann  function.
291a17032bSKristof Umann
301a17032bSKristof Umann- debug.DumpLiveVars: Show the results of live variable analysis for each
311a17032bSKristof Umann  top-level function being analyzed.
321a17032bSKristof Umann
33dd1d5488SKristóf Umann- debug.DumpLiveExprs: Show the results of live expression analysis for each
341a17032bSKristof Umann  top-level function being analyzed.
351a17032bSKristof Umann
361a17032bSKristof Umann- debug.ViewExplodedGraph: Show the Exploded Graphs generated for the
371a17032bSKristof Umann  analysis of different functions in the input translation unit. When there
381a17032bSKristof Umann  are several functions analyzed, display one graph per function. Beware
391a17032bSKristof Umann  that these graphs may grow very large, even for small functions.
401a17032bSKristof Umann
411a17032bSKristof UmannPath Tracking
421a17032bSKristof Umann=============
431a17032bSKristof Umann
441a17032bSKristof UmannThese checkers print information about the path taken by the analyzer engine.
451a17032bSKristof Umann
461a17032bSKristof Umann- debug.DumpCalls: Prints out every function or method call encountered during a
471a17032bSKristof Umann  path traversal. This is indented to show the call stack, but does NOT do any
481a17032bSKristof Umann  special handling of branches, meaning different paths could end up
491a17032bSKristof Umann  interleaved.
501a17032bSKristof Umann
511a17032bSKristof Umann- debug.DumpTraversal: Prints the name of each branch statement encountered
521a17032bSKristof Umann  during a path traversal ("IfStmt", "WhileStmt", etc). Currently used to check
531a17032bSKristof Umann  whether the analysis engine is doing BFS or DFS.
541a17032bSKristof Umann
551a17032bSKristof Umann
561a17032bSKristof UmannState Checking
571a17032bSKristof Umann==============
581a17032bSKristof Umann
591a17032bSKristof UmannThese checkers will print out information about the analyzer state in the form
601a17032bSKristof Umannof analysis warnings. They are intended for use with the -verify functionality
611a17032bSKristof Umannin regression tests.
621a17032bSKristof Umann
631a17032bSKristof Umann- debug.TaintTest: Prints out the word "tainted" for every expression that
641a17032bSKristof Umann  carries taint. At the time of this writing, taint was only introduced by the
651a17032bSKristof Umann  checks under experimental.security.taint.TaintPropagation; this checker may
661a17032bSKristof Umann  eventually move to the security.taint package.
671a17032bSKristof Umann
681a17032bSKristof Umann- debug.ExprInspection: Responds to certain function calls, which are modeled
691a17032bSKristof Umann  after builtins. These function calls should affect the program state other
701a17032bSKristof Umann  than the evaluation of their arguments; to use them, you will need to declare
711a17032bSKristof Umann  them within your test file. The available functions are described below.
721a17032bSKristof Umann
731a17032bSKristof Umann(FIXME: debug.ExprInspection should probably be renamed, since it no longer only
741a17032bSKristof Umanninspects expressions.)
751a17032bSKristof Umann
761a17032bSKristof Umann
771a17032bSKristof UmannExprInspection checks
781a17032bSKristof Umann---------------------
791a17032bSKristof Umann
801a17032bSKristof Umann- ``void clang_analyzer_eval(bool);``
811a17032bSKristof Umann
821a17032bSKristof Umann  Prints TRUE if the argument is known to have a non-zero value, FALSE if the
831a17032bSKristof Umann  argument is known to have a zero or null value, and UNKNOWN if the argument
841a17032bSKristof Umann  isn't sufficiently constrained on this path.  You can use this to test other
851a17032bSKristof Umann  values by using expressions like "x == 5".  Note that this functionality is
861a17032bSKristof Umann  currently DISABLED in inlined functions, since different calls to the same
871a17032bSKristof Umann  inlined function could provide different information, making it difficult to
881a17032bSKristof Umann  write proper -verify directives.
891a17032bSKristof Umann
901a17032bSKristof Umann  In C, the argument can be typed as 'int' or as '_Bool'.
911a17032bSKristof Umann
921a17032bSKristof Umann  Example usage::
931a17032bSKristof Umann
941a17032bSKristof Umann    clang_analyzer_eval(x); // expected-warning{{UNKNOWN}}
951a17032bSKristof Umann    if (!x) return;
961a17032bSKristof Umann    clang_analyzer_eval(x); // expected-warning{{TRUE}}
971a17032bSKristof Umann
981a17032bSKristof Umann
991a17032bSKristof Umann- ``void clang_analyzer_checkInlined(bool);``
1001a17032bSKristof Umann
1011a17032bSKristof Umann  If a call occurs within an inlined function, prints TRUE or FALSE according to
1021a17032bSKristof Umann  the value of its argument. If a call occurs outside an inlined function,
1031a17032bSKristof Umann  nothing is printed.
1041a17032bSKristof Umann
1051a17032bSKristof Umann  The intended use of this checker is to assert that a function is inlined at
1061a17032bSKristof Umann  least once (by passing 'true' and expecting a warning), or to assert that a
1071a17032bSKristof Umann  function is never inlined (by passing 'false' and expecting no warning). The
1081a17032bSKristof Umann  argument is technically unnecessary but is intended to clarify intent.
1091a17032bSKristof Umann
1101a17032bSKristof Umann  You might wonder why we can't print TRUE if a function is ever inlined and
1111a17032bSKristof Umann  FALSE if it is not. The problem is that any inlined function could conceivably
1121a17032bSKristof Umann  also be analyzed as a top-level function (in which case both TRUE and FALSE
1131a17032bSKristof Umann  would be printed), depending on the value of the -analyzer-inlining option.
1141a17032bSKristof Umann
1151a17032bSKristof Umann  In C, the argument can be typed as 'int' or as '_Bool'.
1161a17032bSKristof Umann
1171a17032bSKristof Umann  Example usage::
1181a17032bSKristof Umann
1191a17032bSKristof Umann    int inlined() {
1201a17032bSKristof Umann      clang_analyzer_checkInlined(true); // expected-warning{{TRUE}}
1211a17032bSKristof Umann      return 42;
1221a17032bSKristof Umann    }
1231a17032bSKristof Umann
1241a17032bSKristof Umann    void topLevel() {
1251a17032bSKristof Umann      clang_analyzer_checkInlined(false); // no-warning (not inlined)
1261a17032bSKristof Umann      int value = inlined();
1271a17032bSKristof Umann      // This assertion will not be valid if the previous call was not inlined.
1281a17032bSKristof Umann      clang_analyzer_eval(value == 42); // expected-warning{{TRUE}}
1291a17032bSKristof Umann    }
1301a17032bSKristof Umann
1311a17032bSKristof Umann- ``void clang_analyzer_warnIfReached();``
1321a17032bSKristof Umann
1331a17032bSKristof Umann  Generate a warning if this line of code gets reached by the analyzer.
1341a17032bSKristof Umann
1351a17032bSKristof Umann  Example usage::
1361a17032bSKristof Umann
1371a17032bSKristof Umann    if (true) {
1381a17032bSKristof Umann      clang_analyzer_warnIfReached();  // expected-warning{{REACHABLE}}
1391a17032bSKristof Umann    }
1401a17032bSKristof Umann    else {
1411a17032bSKristof Umann      clang_analyzer_warnIfReached();  // no-warning
1421a17032bSKristof Umann    }
1431a17032bSKristof Umann
1441a17032bSKristof Umann- ``void clang_analyzer_numTimesReached();``
1451a17032bSKristof Umann
1461a17032bSKristof Umann  Same as above, but include the number of times this call expression
1471a17032bSKristof Umann  gets reached by the analyzer during the current analysis.
1481a17032bSKristof Umann
1491a17032bSKristof Umann  Example usage::
1501a17032bSKristof Umann
1511a17032bSKristof Umann    for (int x = 0; x < 3; ++x) {
1521a17032bSKristof Umann      clang_analyzer_numTimesReached(); // expected-warning{{3}}
1531a17032bSKristof Umann    }
1541a17032bSKristof Umann
1551a17032bSKristof Umann- ``void clang_analyzer_warnOnDeadSymbol(int);``
1561a17032bSKristof Umann
1571a17032bSKristof Umann  Subscribe for a delayed warning when the symbol that represents the value of
1581a17032bSKristof Umann  the argument is garbage-collected by the analyzer.
1591a17032bSKristof Umann
1601a17032bSKristof Umann  When calling 'clang_analyzer_warnOnDeadSymbol(x)', if value of 'x' is a
1611a17032bSKristof Umann  symbol, then this symbol is marked by the ExprInspection checker. Then,
1621a17032bSKristof Umann  during each garbage collection run, the checker sees if the marked symbol is
1631a17032bSKristof Umann  being collected and issues the 'SYMBOL DEAD' warning if it does.
1641a17032bSKristof Umann  This way you know where exactly, up to the line of code, the symbol dies.
1651a17032bSKristof Umann
1661a17032bSKristof Umann  It is unlikely that you call this function after the symbol is already dead,
1671a17032bSKristof Umann  because the very reference to it as the function argument prevents it from
1681a17032bSKristof Umann  dying. However, if the argument is not a symbol but a concrete value,
1691a17032bSKristof Umann  no warning would be issued.
1701a17032bSKristof Umann
1711a17032bSKristof Umann  Example usage::
1721a17032bSKristof Umann
1731a17032bSKristof Umann    do {
1741a17032bSKristof Umann      int x = generate_some_integer();
1751a17032bSKristof Umann      clang_analyzer_warnOnDeadSymbol(x);
1761a17032bSKristof Umann    } while(0);  // expected-warning{{SYMBOL DEAD}}
1771a17032bSKristof Umann
1781a17032bSKristof Umann
1791a17032bSKristof Umann- ``void clang_analyzer_explain(a single argument of any type);``
1801a17032bSKristof Umann
1811a17032bSKristof Umann  This function explains the value of its argument in a human-readable manner
1821a17032bSKristof Umann  in the warning message. You can make as many overrides of its prototype
1831a17032bSKristof Umann  in the test code as necessary to explain various integral, pointer,
1841a17032bSKristof Umann  or even record-type values. To simplify usage in C code (where overloading
1851a17032bSKristof Umann  the function declaration is not allowed), you may append an arbitrary suffix
1861a17032bSKristof Umann  to the function name, without affecting functionality.
1871a17032bSKristof Umann
1881a17032bSKristof Umann  Example usage::
1891a17032bSKristof Umann
1901a17032bSKristof Umann    void clang_analyzer_explain(int);
1911a17032bSKristof Umann    void clang_analyzer_explain(void *);
1921a17032bSKristof Umann
1931a17032bSKristof Umann    // Useful in C code
1941a17032bSKristof Umann    void clang_analyzer_explain_int(int);
1951a17032bSKristof Umann
1961a17032bSKristof Umann    void foo(int param, void *ptr) {
1971a17032bSKristof Umann      clang_analyzer_explain(param); // expected-warning{{argument 'param'}}
1981a17032bSKristof Umann      clang_analyzer_explain_int(param); // expected-warning{{argument 'param'}}
1991a17032bSKristof Umann      if (!ptr)
2001a17032bSKristof Umann        clang_analyzer_explain(ptr); // expected-warning{{memory address '0'}}
2011a17032bSKristof Umann    }
2021a17032bSKristof Umann
2031a17032bSKristof Umann- ``void clang_analyzer_dump( /* a single argument of any type */);``
2041a17032bSKristof Umann
2051a17032bSKristof Umann  Similar to clang_analyzer_explain, but produces a raw dump of the value,
2061a17032bSKristof Umann  same as SVal::dump().
2071a17032bSKristof Umann
2081a17032bSKristof Umann  Example usage::
2091a17032bSKristof Umann
2101a17032bSKristof Umann    void clang_analyzer_dump(int);
2111a17032bSKristof Umann    void foo(int x) {
2121a17032bSKristof Umann      clang_analyzer_dump(x); // expected-warning{{reg_$0<x>}}
2131a17032bSKristof Umann    }
2141a17032bSKristof Umann
2151a17032bSKristof Umann- ``size_t clang_analyzer_getExtent(void *);``
2161a17032bSKristof Umann
2171a17032bSKristof Umann  This function returns the value that represents the extent of a memory region
2181a17032bSKristof Umann  pointed to by the argument. This value is often difficult to obtain otherwise,
2191a17032bSKristof Umann  because no valid code that produces this value. However, it may be useful
2201a17032bSKristof Umann  for testing purposes, to see how well does the analyzer model region extents.
2211a17032bSKristof Umann
2221a17032bSKristof Umann  Example usage::
2231a17032bSKristof Umann
2241a17032bSKristof Umann    void foo() {
2251a17032bSKristof Umann      int x, *y;
2261a17032bSKristof Umann      size_t xs = clang_analyzer_getExtent(&x);
2271a17032bSKristof Umann      clang_analyzer_explain(xs); // expected-warning{{'4'}}
2281a17032bSKristof Umann      size_t ys = clang_analyzer_getExtent(&y);
2291a17032bSKristof Umann      clang_analyzer_explain(ys); // expected-warning{{'8'}}
2301a17032bSKristof Umann    }
2311a17032bSKristof Umann
2321a17032bSKristof Umann- ``void clang_analyzer_printState();``
2331a17032bSKristof Umann
2341a17032bSKristof Umann  Dumps the current ProgramState to the stderr. Quickly lookup the program state
2351a17032bSKristof Umann  at any execution point without ViewExplodedGraph or re-compiling the program.
2361a17032bSKristof Umann  This is not very useful for writing tests (apart from testing how ProgramState
2371a17032bSKristof Umann  gets printed), but useful for debugging tests. Also, this method doesn't
2381a17032bSKristof Umann  produce a warning, so it gets printed on the console before all other
2391a17032bSKristof Umann  ExprInspection warnings.
2401a17032bSKristof Umann
2411a17032bSKristof Umann  Example usage::
2421a17032bSKristof Umann
2431a17032bSKristof Umann    void foo() {
2441a17032bSKristof Umann      int x = 1;
2451a17032bSKristof Umann      clang_analyzer_printState(); // Read the stderr!
2461a17032bSKristof Umann    }
2471a17032bSKristof Umann
2481a17032bSKristof Umann- ``void clang_analyzer_hashDump(int);``
2491a17032bSKristof Umann
2501a17032bSKristof Umann  The analyzer can generate a hash to identify reports. To debug what information
2511a17032bSKristof Umann  is used to calculate this hash it is possible to dump the hashed string as a
2521a17032bSKristof Umann  warning of an arbitrary expression using the function above.
2531a17032bSKristof Umann
2541a17032bSKristof Umann  Example usage::
2551a17032bSKristof Umann
2561a17032bSKristof Umann    void foo() {
2571a17032bSKristof Umann      int x = 1;
2581a17032bSKristof Umann      clang_analyzer_hashDump(x); // expected-warning{{hashed string for x}}
2591a17032bSKristof Umann    }
2601a17032bSKristof Umann
2611a17032bSKristof Umann- ``void clang_analyzer_denote(int, const char *);``
2621a17032bSKristof Umann
2631a17032bSKristof Umann  Denotes symbols with strings. A subsequent call to clang_analyzer_express()
2641a17032bSKristof Umann  will expresses another symbol in terms of these string. Useful for testing
2651a17032bSKristof Umann  relationships between different symbols.
2661a17032bSKristof Umann
2671a17032bSKristof Umann  Example usage::
2681a17032bSKristof Umann
2691a17032bSKristof Umann    void foo(int x) {
2701a17032bSKristof Umann      clang_analyzer_denote(x, "$x");
2711a17032bSKristof Umann      clang_analyzer_express(x + 1); // expected-warning{{$x + 1}}
2721a17032bSKristof Umann    }
2731a17032bSKristof Umann
2741a17032bSKristof Umann- ``void clang_analyzer_express(int);``
2751a17032bSKristof Umann
2761a17032bSKristof Umann  See clang_analyzer_denote().
2771a17032bSKristof Umann
278859bcf4eSBalazs Benics- ``void clang_analyzer_isTainted(a single argument of any type);``
279859bcf4eSBalazs Benics
280859bcf4eSBalazs Benics  Queries the analyzer whether the expression used as argument is tainted or not.
281859bcf4eSBalazs Benics  This is useful in tests, where we don't want to issue warning for all tainted
282859bcf4eSBalazs Benics  expressions but only check for certain expressions.
283859bcf4eSBalazs Benics  This would help to reduce the *noise* that the `TaintTest` debug checker would
284684ee205SFlorian Hahn  introduce and let you focus on the `expected-warning`'s that you really care
285859bcf4eSBalazs Benics  about.
286859bcf4eSBalazs Benics
287859bcf4eSBalazs Benics  Example usage::
288859bcf4eSBalazs Benics
289859bcf4eSBalazs Benics    int read_integer() {
290859bcf4eSBalazs Benics      int n;
291859bcf4eSBalazs Benics      clang_analyzer_isTainted(n);     // expected-warning{{NO}}
292859bcf4eSBalazs Benics      scanf("%d", &n);
293859bcf4eSBalazs Benics      clang_analyzer_isTainted(n);     // expected-warning{{YES}}
294859bcf4eSBalazs Benics      clang_analyzer_isTainted(n + 2); // expected-warning{{YES}}
295859bcf4eSBalazs Benics      clang_analyzer_isTainted(n > 0); // expected-warning{{YES}}
296859bcf4eSBalazs Benics      int next_tainted_value = n; // no-warning
297859bcf4eSBalazs Benics      return n;
298859bcf4eSBalazs Benics    }
299859bcf4eSBalazs Benics
30089d210feSCharusso- ``clang_analyzer_dumpExtent(a single argument of any type)``
30189d210feSCharusso- ``clang_analyzer_dumpElementCount(a single argument of any type)``
30289d210feSCharusso
30389d210feSCharusso  Dumps out the extent and the element count of the argument.
30489d210feSCharusso
30589d210feSCharusso  Example usage::
30689d210feSCharusso
30789d210feSCharusso    void array() {
30889d210feSCharusso      int a[] = {1, 3};
30989d210feSCharusso      clang_analyzer_dumpExtent(a);       // expected-warning {{8 S64b}}
31089d210feSCharusso      clang_analyzer_dumpElementCount(a); // expected-warning {{2 S64b}}
31189d210feSCharusso    }
312*bc08c3cbSDenys Petrov
313*bc08c3cbSDenys Petrov- ``clang_analyzer_value(a single argument of integer or pointer type)``
314*bc08c3cbSDenys Petrov
315*bc08c3cbSDenys Petrov  Prints an associated value for the given argument.
316*bc08c3cbSDenys Petrov  Supported argument types are integers, enums and pointers.
317*bc08c3cbSDenys Petrov  The value can be represented either as a range set or as a concrete integer.
318*bc08c3cbSDenys Petrov  For the rest of the types function prints ``n/a`` (aka not available).
319*bc08c3cbSDenys Petrov
320*bc08c3cbSDenys Petrov  **Note:** This function will print nothing for clang built with Z3 constraint manager.
321*bc08c3cbSDenys Petrov  This may cause crashes of your tests. To manage this use one of the test constraining
322*bc08c3cbSDenys Petrov  techniques:
323*bc08c3cbSDenys Petrov
324*bc08c3cbSDenys Petrov  * llvm-lit commands ``REQUIRES no-z3`` or ``UNSUPPORTED z3`` `See for details. <https://llvm.org/docs/TestingGuide.html#constraining-test-execution>`_
325*bc08c3cbSDenys Petrov
326*bc08c3cbSDenys Petrov  * a preprocessor directive ``#ifndef ANALYZER_CM_Z3``
327*bc08c3cbSDenys Petrov
328*bc08c3cbSDenys Petrov  * a clang command argument ``-analyzer-constraints=range``
329*bc08c3cbSDenys Petrov
330*bc08c3cbSDenys Petrov  Example usage::
331*bc08c3cbSDenys Petrov
332*bc08c3cbSDenys Petrov    void print(char c, unsigned u) {
333*bc08c3cbSDenys Petrov      clang_analyzer_value(c); // expected-warning {{8s:{ [-128, 127] }}}
334*bc08c3cbSDenys Petrov      if(u != 42)
335*bc08c3cbSDenys Petrov         clang_analyzer_value(u); // expected-warning {{32u:{ [0, 41], [43, 4294967295] }}}
336*bc08c3cbSDenys Petrov      else
337*bc08c3cbSDenys Petrov         clang_analyzer_value(u); // expected-warning {{32u:42}}
338*bc08c3cbSDenys Petrov    }
33989d210feSCharusso
3401a17032bSKristof UmannStatistics
3411a17032bSKristof Umann==========
3421a17032bSKristof Umann
3431a17032bSKristof UmannThe debug.Stats checker collects various information about the analysis of each
3441a17032bSKristof Umannfunction, such as how many blocks were reached and if the analyzer timed out.
3451a17032bSKristof Umann
3461a17032bSKristof UmannThere is also an additional -analyzer-stats flag, which enables various
3471a17032bSKristof Umannstatistics within the analyzer engine. Note the Stats checker (which produces at
3481a17032bSKristof Umannleast one bug report per function) may actually change the values reported by
3491a17032bSKristof Umann-analyzer-stats.
3504962816eSKristof Umann
3514962816eSKristof UmannOutput testing checkers
3524962816eSKristof Umann=======================
3534962816eSKristof Umann
3544962816eSKristof Umann- debug.ReportStmts reports a warning at **every** statement, making it a very
3554962816eSKristof Umann  useful tool for testing thoroughly bug report construction and output
3564962816eSKristof Umann  emission.
357