NoReturnFunctionChecker.cpp revision 55fc873017f10f6f566b182b70f6fc22aefa3464
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::PostStmt<CallExpr>, 30 check::PostObjCMessage > { 31public: 32 void checkPostStmt(const CallExpr *CE, CheckerContext &C) const; 33 void checkPostObjCMessage(const ObjCMethodCall &msg, CheckerContext &C) const; 34}; 35 36} 37 38void NoReturnFunctionChecker::checkPostStmt(const CallExpr *CE, 39 CheckerContext &C) const { 40 ProgramStateRef state = C.getState(); 41 const Expr *Callee = CE->getCallee(); 42 43 bool BuildSinks = getFunctionExtInfo(Callee->getType()).getNoReturn(); 44 45 if (!BuildSinks) { 46 SVal L = state->getSVal(Callee, C.getLocationContext()); 47 const FunctionDecl *FD = L.getAsFunctionDecl(); 48 if (!FD) 49 return; 50 51 if (FD->getAttr<AnalyzerNoReturnAttr>()) 52 BuildSinks = true; 53 else if (const IdentifierInfo *II = FD->getIdentifier()) { 54 // HACK: Some functions are not marked noreturn, and don't return. 55 // Here are a few hardwired ones. If this takes too long, we can 56 // potentially cache these results. 57 BuildSinks 58 = llvm::StringSwitch<bool>(StringRef(II->getName())) 59 .Case("exit", true) 60 .Case("panic", true) 61 .Case("error", true) 62 .Case("Assert", true) 63 // FIXME: This is just a wrapper around throwing an exception. 64 // Eventually inter-procedural analysis should handle this easily. 65 .Case("ziperr", true) 66 .Case("assfail", true) 67 .Case("db_error", true) 68 .Case("__assert", 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 84static 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 102void NoReturnFunctionChecker::checkPostObjCMessage(const ObjCMethodCall &Msg, 103 CheckerContext &C) const { 104 // HACK: This entire check is to handle two messages in the Cocoa frameworks: 105 // -[NSAssertionHandler 106 // handleFailureInMethod:object:file:lineNumber:description:] 107 // -[NSAssertionHandler 108 // handleFailureInFunction:file:lineNumber:description:] 109 // Eventually these should be annotated with __attribute__((noreturn)). 110 // Because ObjC messages use dynamic dispatch, it is not generally safe to 111 // assume certain methods can't return. In cases where it is definitely valid, 112 // see if you can mark the methods noreturn or analyzer_noreturn instead of 113 // adding more explicit checks to this method. 114 115 if (!Msg.isInstanceMessage()) 116 return; 117 118 const ObjCInterfaceDecl *Receiver = Msg.getReceiverInterface(); 119 if (!Receiver) 120 return; 121 if (!Receiver->getIdentifier()->isStr("NSAssertionHandler")) 122 return; 123 124 Selector Sel = Msg.getSelector(); 125 switch (Sel.getNumArgs()) { 126 default: 127 return; 128 case 4: 129 if (!isMultiArgSelector(&Sel, "handleFailureInFunction", "file", 130 "lineNumber", "description", NULL)) 131 return; 132 break; 133 case 5: 134 if (!isMultiArgSelector(&Sel, "handleFailureInMethod", "object", "file", 135 "lineNumber", "description", NULL)) 136 return; 137 break; 138 } 139 140 // If we got here, it's one of the messages we care about. 141 C.generateSink(); 142} 143 144 145void ento::registerNoReturnFunctionChecker(CheckerManager &mgr) { 146 mgr.registerChecker<NoReturnFunctionChecker>(); 147} 148