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