clang API Documentation

NoReturnFunctionChecker.cpp
Go to the documentation of this file.
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 "clang/StaticAnalyzer/Core/Checker.h"
00017 #include "clang/StaticAnalyzer/Core/CheckerManager.h"
00018 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
00019 #include "clang/StaticAnalyzer/Core/PathSensitive/ObjCMessage.h"
00020 #include "llvm/ADT/StringSwitch.h"
00021 #include <cstdarg>
00022 
00023 using namespace clang;
00024 using namespace ento;
00025 
00026 namespace {
00027 
00028 class NoReturnFunctionChecker : public Checker< check::PostStmt<CallExpr>,
00029                                                 check::PostObjCMessage > {
00030 public:
00031   void checkPostStmt(const CallExpr *CE, CheckerContext &C) const;
00032   void checkPostObjCMessage(const ObjCMessage &msg, CheckerContext &C) const;
00033 };
00034 
00035 }
00036 
00037 void NoReturnFunctionChecker::checkPostStmt(const CallExpr *CE,
00038                                             CheckerContext &C) const {
00039   ProgramStateRef state = C.getState();
00040   const Expr *Callee = CE->getCallee();
00041 
00042   bool BuildSinks = getFunctionExtInfo(Callee->getType()).getNoReturn();
00043 
00044   if (!BuildSinks) {
00045     SVal L = state->getSVal(Callee, C.getLocationContext());
00046     const FunctionDecl *FD = L.getAsFunctionDecl();
00047     if (!FD)
00048       return;
00049 
00050     if (FD->getAttr<AnalyzerNoReturnAttr>())
00051       BuildSinks = true;
00052     else if (const IdentifierInfo *II = FD->getIdentifier()) {
00053       // HACK: Some functions are not marked noreturn, and don't return.
00054       //  Here are a few hardwired ones.  If this takes too long, we can
00055       //  potentially cache these results.
00056       BuildSinks
00057         = llvm::StringSwitch<bool>(StringRef(II->getName()))
00058             .Case("exit", true)
00059             .Case("panic", true)
00060             .Case("error", true)
00061             .Case("Assert", true)
00062             // FIXME: This is just a wrapper around throwing an exception.
00063             //  Eventually inter-procedural analysis should handle this easily.
00064             .Case("ziperr", true)
00065             .Case("assfail", true)
00066             .Case("db_error", true)
00067             .Case("__assert", true)
00068             .Case("__assert_rtn", true)
00069             .Case("__assert_fail", true)
00070             .Case("dtrace_assfail", true)
00071             .Case("yy_fatal_error", true)
00072             .Case("_XCAssertionFailureHandler", true)
00073             .Case("_DTAssertionFailureHandler", true)
00074             .Case("_TSAssertionFailureHandler", true)
00075             .Default(false);
00076     }
00077   }
00078 
00079   if (BuildSinks)
00080     C.generateSink();
00081 }
00082 
00083 static bool END_WITH_NULL isMultiArgSelector(const Selector *Sel, ...) {
00084   va_list argp;
00085   va_start(argp, Sel);
00086 
00087   unsigned Slot = 0;
00088   const char *Arg;
00089   while ((Arg = va_arg(argp, const char *))) {
00090     if (!Sel->getNameForSlot(Slot).equals(Arg))
00091       break; // still need to va_end!
00092     ++Slot;
00093   }
00094 
00095   va_end(argp);
00096 
00097   // We only succeeded if we made it to the end of the argument list.
00098   return (Arg == NULL);
00099 }
00100 
00101 void NoReturnFunctionChecker::checkPostObjCMessage(const ObjCMessage &Msg,
00102                                                    CheckerContext &C) const {
00103   // HACK: This entire check is to handle two messages in the Cocoa frameworks:
00104   // -[NSAssertionHandler
00105   //    handleFailureInMethod:object:file:lineNumber:description:]
00106   // -[NSAssertionHandler
00107   //    handleFailureInFunction:file:lineNumber:description:]
00108   // Eventually these should be annotated with __attribute__((noreturn)).
00109   // Because ObjC messages use dynamic dispatch, it is not generally safe to
00110   // assume certain methods can't return. In cases where it is definitely valid,
00111   // see if you can mark the methods noreturn or analyzer_noreturn instead of
00112   // adding more explicit checks to this method.
00113 
00114   if (!Msg.isInstanceMessage())
00115     return;
00116 
00117   const ObjCInterfaceDecl *Receiver = Msg.getReceiverInterface();
00118   if (!Receiver)
00119     return;
00120   if (!Receiver->getIdentifier()->isStr("NSAssertionHandler"))
00121     return;
00122 
00123   Selector Sel = Msg.getSelector();
00124   switch (Sel.getNumArgs()) {
00125   default:
00126     return;
00127   case 4:
00128     if (!isMultiArgSelector(&Sel, "handleFailureInFunction", "file",
00129                             "lineNumber", "description", NULL))
00130       return;
00131     break;
00132   case 5:
00133     if (!isMultiArgSelector(&Sel, "handleFailureInMethod", "object", "file",
00134                             "lineNumber", "description", NULL))
00135       return;
00136     break;
00137   }
00138 
00139   // If we got here, it's one of the messages we care about.
00140   C.generateSink();
00141 }
00142 
00143 
00144 void ento::registerNoReturnFunctionChecker(CheckerManager &mgr) {
00145   mgr.registerChecker<NoReturnFunctionChecker>();
00146 }