1 //=== NoReturnFunctionChecker.cpp -------------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This defines NoReturnFunctionChecker, which evaluates functions that do not
10 // return to the caller.
12 //===----------------------------------------------------------------------===//
14 #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
15 #include "clang/AST/Attr.h"
16 #include "clang/Analysis/SelectorExtras.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"
24 using namespace clang;
29 class NoReturnFunctionChecker : public Checker< check::PostCall,
30 check::PostObjCMessage > {
31 mutable Selector HandleFailureInFunctionSel;
32 mutable Selector HandleFailureInMethodSel;
34 void checkPostCall(const CallEvent &CE, CheckerContext &C) const;
35 void checkPostObjCMessage(const ObjCMethodCall &msg, CheckerContext &C) const;
40 void NoReturnFunctionChecker::checkPostCall(const CallEvent &CE,
41 CheckerContext &C) const {
42 bool BuildSinks = false;
44 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(CE.getDecl()))
45 BuildSinks = FD->hasAttr<AnalyzerNoReturnAttr>() || FD->isNoReturn();
47 const Expr *Callee = CE.getOriginExpr();
48 if (!BuildSinks && Callee)
49 BuildSinks = getFunctionExtInfo(Callee->getType()).getNoReturn();
51 if (!BuildSinks && CE.isGlobalCFunction()) {
52 if (const IdentifierInfo *II = CE.getCalleeIdentifier()) {
53 // HACK: Some functions are not marked noreturn, and don't return.
54 // Here are a few hardwired ones. If this takes too long, we can
55 // potentially cache these results.
57 = llvm::StringSwitch<bool>(StringRef(II->getName()))
62 // FIXME: This is just a wrapper around throwing an exception.
63 // Eventually inter-procedural analysis should handle this easily.
65 .Case("assfail", true)
66 .Case("db_error", true)
67 .Case("__assert", true)
68 .Case("__assert2", true)
69 // For the purpose of static analysis, we do not care that
70 // this MSVC function will return if the user decides to continue.
71 .Case("_wassert", true)
72 .Case("__assert_rtn", true)
73 .Case("__assert_fail", true)
74 .Case("dtrace_assfail", true)
75 .Case("yy_fatal_error", true)
76 .Case("_XCAssertionFailureHandler", true)
77 .Case("_DTAssertionFailureHandler", true)
78 .Case("_TSAssertionFailureHandler", true)
84 C.generateSink(C.getState(), C.getPredecessor());
87 void NoReturnFunctionChecker::checkPostObjCMessage(const ObjCMethodCall &Msg,
88 CheckerContext &C) const {
89 // Check if the method is annotated with analyzer_noreturn.
90 if (const ObjCMethodDecl *MD = Msg.getDecl()) {
91 MD = MD->getCanonicalDecl();
92 if (MD->hasAttr<AnalyzerNoReturnAttr>()) {
93 C.generateSink(C.getState(), C.getPredecessor());
98 // HACK: This entire check is to handle two messages in the Cocoa frameworks:
99 // -[NSAssertionHandler
100 // handleFailureInMethod:object:file:lineNumber:description:]
101 // -[NSAssertionHandler
102 // handleFailureInFunction:file:lineNumber:description:]
103 // Eventually these should be annotated with __attribute__((noreturn)).
104 // Because ObjC messages use dynamic dispatch, it is not generally safe to
105 // assume certain methods can't return. In cases where it is definitely valid,
106 // see if you can mark the methods noreturn or analyzer_noreturn instead of
107 // adding more explicit checks to this method.
109 if (!Msg.isInstanceMessage())
112 const ObjCInterfaceDecl *Receiver = Msg.getReceiverInterface();
115 if (!Receiver->getIdentifier()->isStr("NSAssertionHandler"))
118 Selector Sel = Msg.getSelector();
119 switch (Sel.getNumArgs()) {
123 lazyInitKeywordSelector(HandleFailureInFunctionSel, C.getASTContext(),
124 "handleFailureInFunction", "file", "lineNumber",
126 if (Sel != HandleFailureInFunctionSel)
130 lazyInitKeywordSelector(HandleFailureInMethodSel, C.getASTContext(),
131 "handleFailureInMethod", "object", "file",
132 "lineNumber", "description");
133 if (Sel != HandleFailureInMethodSel)
138 // If we got here, it's one of the messages we care about.
139 C.generateSink(C.getState(), C.getPredecessor());
142 void ento::registerNoReturnFunctionChecker(CheckerManager &mgr) {
143 mgr.registerChecker<NoReturnFunctionChecker>();
146 bool ento::shouldRegisterNoReturnFunctionChecker(const LangOptions &LO) {