NoReturnFunctionChecker.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. //=== NoReturnFunctionChecker.cpp -------------------------------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This defines NoReturnFunctionChecker, which evaluates functions that do not
  11. // return to the caller.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "ClangSACheckers.h"
  15. #include "clang/StaticAnalyzer/Core/Checker.h"
  16. #include "clang/StaticAnalyzer/Core/CheckerManager.h"
  17. #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
  18. #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
  19. #include "clang/AST/Attr.h"
  20. #include "llvm/ADT/StringSwitch.h"
  21. #include <cstdarg>
  22. using namespace clang;
  23. using namespace ento;
  24. namespace {
  25. class NoReturnFunctionChecker : public Checker< check::PostStmt<CallExpr>,
  26. check::PostObjCMessage > {
  27. public:
  28. void checkPostStmt(const CallExpr *CE, CheckerContext &C) const;
  29. void checkPostObjCMessage(const ObjCMethodCall &msg, CheckerContext &C) const;
  30. };
  31. }
  32. void NoReturnFunctionChecker::checkPostStmt(const CallExpr *CE,
  33. CheckerContext &C) const {
  34. ProgramStateRef state = C.getState();
  35. const Expr *Callee = CE->getCallee();
  36. bool BuildSinks = getFunctionExtInfo(Callee->getType()).getNoReturn();
  37. if (!BuildSinks) {
  38. SVal L = state->getSVal(Callee, C.getLocationContext());
  39. const FunctionDecl *FD = L.getAsFunctionDecl();
  40. if (!FD)
  41. return;
  42. if (FD->getAttr<AnalyzerNoReturnAttr>())
  43. BuildSinks = true;
  44. else if (const IdentifierInfo *II = FD->getIdentifier()) {
  45. // HACK: Some functions are not marked noreturn, and don't return.
  46. // Here are a few hardwired ones. If this takes too long, we can
  47. // potentially cache these results.
  48. BuildSinks
  49. = llvm::StringSwitch<bool>(StringRef(II->getName()))
  50. .Case("exit", true)
  51. .Case("panic", true)
  52. .Case("error", true)
  53. .Case("Assert", true)
  54. // FIXME: This is just a wrapper around throwing an exception.
  55. // Eventually inter-procedural analysis should handle this easily.
  56. .Case("ziperr", true)
  57. .Case("assfail", true)
  58. .Case("db_error", true)
  59. .Case("__assert", true)
  60. .Case("__assert_rtn", true)
  61. .Case("__assert_fail", true)
  62. .Case("dtrace_assfail", true)
  63. .Case("yy_fatal_error", true)
  64. .Case("_XCAssertionFailureHandler", true)
  65. .Case("_DTAssertionFailureHandler", true)
  66. .Case("_TSAssertionFailureHandler", true)
  67. .Default(false);
  68. }
  69. }
  70. if (BuildSinks)
  71. C.generateSink();
  72. }
  73. static bool END_WITH_NULL isMultiArgSelector(const Selector *Sel, ...) {
  74. va_list argp;
  75. va_start(argp, Sel);
  76. unsigned Slot = 0;
  77. const char *Arg;
  78. while ((Arg = va_arg(argp, const char *))) {
  79. if (!Sel->getNameForSlot(Slot).equals(Arg))
  80. break; // still need to va_end!
  81. ++Slot;
  82. }
  83. va_end(argp);
  84. // We only succeeded if we made it to the end of the argument list.
  85. return (Arg == NULL);
  86. }
  87. void NoReturnFunctionChecker::checkPostObjCMessage(const ObjCMethodCall &Msg,
  88. CheckerContext &C) const {
  89. // HACK: This entire check is to handle two messages in the Cocoa frameworks:
  90. // -[NSAssertionHandler
  91. // handleFailureInMethod:object:file:lineNumber:description:]
  92. // -[NSAssertionHandler
  93. // handleFailureInFunction:file:lineNumber:description:]
  94. // Eventually these should be annotated with __attribute__((noreturn)).
  95. // Because ObjC messages use dynamic dispatch, it is not generally safe to
  96. // assume certain methods can't return. In cases where it is definitely valid,
  97. // see if you can mark the methods noreturn or analyzer_noreturn instead of
  98. // adding more explicit checks to this method.
  99. if (!Msg.isInstanceMessage())
  100. return;
  101. const ObjCInterfaceDecl *Receiver = Msg.getReceiverInterface();
  102. if (!Receiver)
  103. return;
  104. if (!Receiver->getIdentifier()->isStr("NSAssertionHandler"))
  105. return;
  106. Selector Sel = Msg.getSelector();
  107. switch (Sel.getNumArgs()) {
  108. default:
  109. return;
  110. case 4:
  111. if (!isMultiArgSelector(&Sel, "handleFailureInFunction", "file",
  112. "lineNumber", "description", NULL))
  113. return;
  114. break;
  115. case 5:
  116. if (!isMultiArgSelector(&Sel, "handleFailureInMethod", "object", "file",
  117. "lineNumber", "description", NULL))
  118. return;
  119. break;
  120. }
  121. // If we got here, it's one of the messages we care about.
  122. C.generateSink();
  123. }
  124. void ento::registerNoReturnFunctionChecker(CheckerManager &mgr) {
  125. mgr.registerChecker<NoReturnFunctionChecker>();
  126. }