NoReturnFunctionChecker.cpp revision 263508
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
15#include "ClangSACheckers.h"
16#include "clang/AST/Attr.h"
17#include "clang/StaticAnalyzer/Core/Checker.h"
18#include "clang/StaticAnalyzer/Core/CheckerManager.h"
19#include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
20#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
21#include "llvm/ADT/StringSwitch.h"
22#include <cstdarg>
23
24using namespace clang;
25using namespace ento;
26
27namespace {
28
29class NoReturnFunctionChecker : public Checker< check::PostCall,
30                                                check::PostObjCMessage > {
31public:
32  void checkPostCall(const CallEvent &CE, CheckerContext &C) const;
33  void checkPostObjCMessage(const ObjCMethodCall &msg, CheckerContext &C) const;
34};
35
36}
37
38void NoReturnFunctionChecker::checkPostCall(const CallEvent &CE,
39                                            CheckerContext &C) const {
40  ProgramStateRef state = C.getState();
41  bool BuildSinks = false;
42
43  if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(CE.getDecl()))
44    BuildSinks = FD->getAttr<AnalyzerNoReturnAttr>() || FD->isNoReturn();
45
46  const Expr *Callee = CE.getOriginExpr();
47  if (!BuildSinks && Callee)
48    BuildSinks = getFunctionExtInfo(Callee->getType()).getNoReturn();
49
50  if (!BuildSinks && CE.isGlobalCFunction()) {
51    if (const IdentifierInfo *II = CE.getCalleeIdentifier()) {
52      // HACK: Some functions are not marked noreturn, and don't return.
53      //  Here are a few hardwired ones.  If this takes too long, we can
54      //  potentially cache these results.
55      BuildSinks
56        = llvm::StringSwitch<bool>(StringRef(II->getName()))
57            .Case("exit", true)
58            .Case("panic", true)
59            .Case("error", true)
60            .Case("Assert", true)
61            // FIXME: This is just a wrapper around throwing an exception.
62            //  Eventually inter-procedural analysis should handle this easily.
63            .Case("ziperr", true)
64            .Case("assfail", true)
65            .Case("db_error", true)
66            .Case("__assert", true)
67            // For the purpose of static analysis, we do not care that
68            //  this MSVC function will return if the user decides to continue.
69            .Case("_wassert", true)
70            .Case("__assert_rtn", true)
71            .Case("__assert_fail", true)
72            .Case("dtrace_assfail", true)
73            .Case("yy_fatal_error", true)
74            .Case("_XCAssertionFailureHandler", true)
75            .Case("_DTAssertionFailureHandler", true)
76            .Case("_TSAssertionFailureHandler", true)
77            .Default(false);
78    }
79  }
80
81  if (BuildSinks)
82    C.generateSink();
83}
84
85static bool END_WITH_NULL isMultiArgSelector(const Selector *Sel, ...) {
86  va_list argp;
87  va_start(argp, Sel);
88
89  unsigned Slot = 0;
90  const char *Arg;
91  while ((Arg = va_arg(argp, const char *))) {
92    if (!Sel->getNameForSlot(Slot).equals(Arg))
93      break; // still need to va_end!
94    ++Slot;
95  }
96
97  va_end(argp);
98
99  // We only succeeded if we made it to the end of the argument list.
100  return (Arg == NULL);
101}
102
103void NoReturnFunctionChecker::checkPostObjCMessage(const ObjCMethodCall &Msg,
104                                                   CheckerContext &C) const {
105  // Check if the method is annotated with analyzer_noreturn.
106  if (const ObjCMethodDecl *MD = Msg.getDecl()) {
107    MD = MD->getCanonicalDecl();
108    if (MD->hasAttr<AnalyzerNoReturnAttr>()) {
109      C.generateSink();
110      return;
111    }
112  }
113
114  // HACK: This entire check is to handle two messages in the Cocoa frameworks:
115  // -[NSAssertionHandler
116  //    handleFailureInMethod:object:file:lineNumber:description:]
117  // -[NSAssertionHandler
118  //    handleFailureInFunction:file:lineNumber:description:]
119  // Eventually these should be annotated with __attribute__((noreturn)).
120  // Because ObjC messages use dynamic dispatch, it is not generally safe to
121  // assume certain methods can't return. In cases where it is definitely valid,
122  // see if you can mark the methods noreturn or analyzer_noreturn instead of
123  // adding more explicit checks to this method.
124
125  if (!Msg.isInstanceMessage())
126    return;
127
128  const ObjCInterfaceDecl *Receiver = Msg.getReceiverInterface();
129  if (!Receiver)
130    return;
131  if (!Receiver->getIdentifier()->isStr("NSAssertionHandler"))
132    return;
133
134  Selector Sel = Msg.getSelector();
135  switch (Sel.getNumArgs()) {
136  default:
137    return;
138  case 4:
139    if (!isMultiArgSelector(&Sel, "handleFailureInFunction", "file",
140                            "lineNumber", "description", NULL))
141      return;
142    break;
143  case 5:
144    if (!isMultiArgSelector(&Sel, "handleFailureInMethod", "object", "file",
145                            "lineNumber", "description", NULL))
146      return;
147    break;
148  }
149
150  // If we got here, it's one of the messages we care about.
151  C.generateSink();
152}
153
154
155void ento::registerNoReturnFunctionChecker(CheckerManager &mgr) {
156  mgr.registerChecker<NoReturnFunctionChecker>();
157}
158