clang API Documentation
00001 //=== NoReturnFunctionChecker.cpp -------------------------------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This defines NoReturnFunctionChecker, which evaluates functions that do not 00011 // return to the caller. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #include "ClangSACheckers.h" 00016 #include "SelectorExtras.h" 00017 #include "clang/AST/Attr.h" 00018 #include "clang/StaticAnalyzer/Core/Checker.h" 00019 #include "clang/StaticAnalyzer/Core/CheckerManager.h" 00020 #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h" 00021 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" 00022 #include "llvm/ADT/StringSwitch.h" 00023 #include <cstdarg> 00024 00025 using namespace clang; 00026 using namespace ento; 00027 00028 namespace { 00029 00030 class NoReturnFunctionChecker : public Checker< check::PostCall, 00031 check::PostObjCMessage > { 00032 mutable Selector HandleFailureInFunctionSel; 00033 mutable Selector HandleFailureInMethodSel; 00034 public: 00035 void checkPostCall(const CallEvent &CE, CheckerContext &C) const; 00036 void checkPostObjCMessage(const ObjCMethodCall &msg, CheckerContext &C) const; 00037 }; 00038 00039 } 00040 00041 void NoReturnFunctionChecker::checkPostCall(const CallEvent &CE, 00042 CheckerContext &C) const { 00043 bool BuildSinks = false; 00044 00045 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(CE.getDecl())) 00046 BuildSinks = FD->hasAttr<AnalyzerNoReturnAttr>() || FD->isNoReturn(); 00047 00048 const Expr *Callee = CE.getOriginExpr(); 00049 if (!BuildSinks && Callee) 00050 BuildSinks = getFunctionExtInfo(Callee->getType()).getNoReturn(); 00051 00052 if (!BuildSinks && CE.isGlobalCFunction()) { 00053 if (const IdentifierInfo *II = CE.getCalleeIdentifier()) { 00054 // HACK: Some functions are not marked noreturn, and don't return. 00055 // Here are a few hardwired ones. If this takes too long, we can 00056 // potentially cache these results. 00057 BuildSinks 00058 = llvm::StringSwitch<bool>(StringRef(II->getName())) 00059 .Case("exit", true) 00060 .Case("panic", true) 00061 .Case("error", true) 00062 .Case("Assert", true) 00063 // FIXME: This is just a wrapper around throwing an exception. 00064 // Eventually inter-procedural analysis should handle this easily. 00065 .Case("ziperr", true) 00066 .Case("assfail", true) 00067 .Case("db_error", true) 00068 .Case("__assert", true) 00069 // For the purpose of static analysis, we do not care that 00070 // this MSVC function will return if the user decides to continue. 00071 .Case("_wassert", true) 00072 .Case("__assert_rtn", true) 00073 .Case("__assert_fail", true) 00074 .Case("dtrace_assfail", true) 00075 .Case("yy_fatal_error", true) 00076 .Case("_XCAssertionFailureHandler", true) 00077 .Case("_DTAssertionFailureHandler", true) 00078 .Case("_TSAssertionFailureHandler", true) 00079 .Default(false); 00080 } 00081 } 00082 00083 if (BuildSinks) 00084 C.generateSink(); 00085 } 00086 00087 void NoReturnFunctionChecker::checkPostObjCMessage(const ObjCMethodCall &Msg, 00088 CheckerContext &C) const { 00089 // Check if the method is annotated with analyzer_noreturn. 00090 if (const ObjCMethodDecl *MD = Msg.getDecl()) { 00091 MD = MD->getCanonicalDecl(); 00092 if (MD->hasAttr<AnalyzerNoReturnAttr>()) { 00093 C.generateSink(); 00094 return; 00095 } 00096 } 00097 00098 // HACK: This entire check is to handle two messages in the Cocoa frameworks: 00099 // -[NSAssertionHandler 00100 // handleFailureInMethod:object:file:lineNumber:description:] 00101 // -[NSAssertionHandler 00102 // handleFailureInFunction:file:lineNumber:description:] 00103 // Eventually these should be annotated with __attribute__((noreturn)). 00104 // Because ObjC messages use dynamic dispatch, it is not generally safe to 00105 // assume certain methods can't return. In cases where it is definitely valid, 00106 // see if you can mark the methods noreturn or analyzer_noreturn instead of 00107 // adding more explicit checks to this method. 00108 00109 if (!Msg.isInstanceMessage()) 00110 return; 00111 00112 const ObjCInterfaceDecl *Receiver = Msg.getReceiverInterface(); 00113 if (!Receiver) 00114 return; 00115 if (!Receiver->getIdentifier()->isStr("NSAssertionHandler")) 00116 return; 00117 00118 Selector Sel = Msg.getSelector(); 00119 switch (Sel.getNumArgs()) { 00120 default: 00121 return; 00122 case 4: 00123 lazyInitKeywordSelector(HandleFailureInFunctionSel, C.getASTContext(), 00124 "handleFailureInFunction", "file", "lineNumber", 00125 "description", nullptr); 00126 if (Sel != HandleFailureInFunctionSel) 00127 return; 00128 break; 00129 case 5: 00130 lazyInitKeywordSelector(HandleFailureInMethodSel, C.getASTContext(), 00131 "handleFailureInMethod", "object", "file", 00132 "lineNumber", "description", nullptr); 00133 if (Sel != HandleFailureInMethodSel) 00134 return; 00135 break; 00136 } 00137 00138 // If we got here, it's one of the messages we care about. 00139 C.generateSink(); 00140 } 00141 00142 void ento::registerNoReturnFunctionChecker(CheckerManager &mgr) { 00143 mgr.registerChecker<NoReturnFunctionChecker>(); 00144 }