Coverage Report

Created: 2020-02-25 14:32

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/StaticAnalyzer/Checkers/NoReturnFunctionChecker.cpp
Line
Count
Source (jump to first uncovered line)
1
//=== NoReturnFunctionChecker.cpp -------------------------------*- C++ -*-===//
2
//
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
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
// This defines NoReturnFunctionChecker, which evaluates functions that do not
10
// return to the caller.
11
//
12
//===----------------------------------------------------------------------===//
13
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"
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
  mutable Selector HandleFailureInFunctionSel;
32
  mutable Selector HandleFailureInMethodSel;
33
public:
34
  void checkPostCall(const CallEvent &CE, CheckerContext &C) const;
35
  void checkPostObjCMessage(const ObjCMethodCall &msg, CheckerContext &C) const;
36
};
37
38
}
39
40
void NoReturnFunctionChecker::checkPostCall(const CallEvent &CE,
41
139k
                                            CheckerContext &C) const {
42
139k
  bool BuildSinks = false;
43
139k
44
139k
  if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(CE.getDecl()))
45
136k
    BuildSinks = FD->hasAttr<AnalyzerNoReturnAttr>() || 
FD->isNoReturn()136k
;
46
139k
47
139k
  const Expr *Callee = CE.getOriginExpr();
48
139k
  if (!BuildSinks && 
Callee139k
)
49
137k
    BuildSinks = getFunctionExtInfo(Callee->getType()).getNoReturn();
50
139k
51
139k
  if (!BuildSinks && 
CE.isGlobalCFunction()139k
) {
52
44.2k
    if (const IdentifierInfo *II = CE.getCalleeIdentifier()) {
53
44.2k
      // HACK: Some functions are not marked noreturn, and don't return.
54
44.2k
      //  Here are a few hardwired ones.  If this takes too long, we can
55
44.2k
      //  potentially cache these results.
56
44.2k
      BuildSinks
57
44.2k
        = llvm::StringSwitch<bool>(StringRef(II->getName()))
58
44.2k
            .Case("exit", true)
59
44.2k
            .Case("panic", true)
60
44.2k
            .Case("error", true)
61
44.2k
            .Case("Assert", true)
62
44.2k
            // FIXME: This is just a wrapper around throwing an exception.
63
44.2k
            //  Eventually inter-procedural analysis should handle this easily.
64
44.2k
            .Case("ziperr", true)
65
44.2k
            .Case("assfail", true)
66
44.2k
            .Case("db_error", true)
67
44.2k
            .Case("__assert", true)
68
44.2k
            .Case("__assert2", true)
69
44.2k
            // For the purpose of static analysis, we do not care that
70
44.2k
            //  this MSVC function will return if the user decides to continue.
71
44.2k
            .Case("_wassert", true)
72
44.2k
            .Case("__assert_rtn", true)
73
44.2k
            .Case("__assert_fail", true)
74
44.2k
            .Case("dtrace_assfail", true)
75
44.2k
            .Case("yy_fatal_error", true)
76
44.2k
            .Case("_XCAssertionFailureHandler", true)
77
44.2k
            .Case("_DTAssertionFailureHandler", true)
78
44.2k
            .Case("_TSAssertionFailureHandler", true)
79
44.2k
            .Default(false);
80
44.2k
    }
81
44.2k
  }
82
139k
83
139k
  if (BuildSinks)
84
573
    C.generateSink(C.getState(), C.getPredecessor());
85
139k
}
86
87
void NoReturnFunctionChecker::checkPostObjCMessage(const ObjCMethodCall &Msg,
88
3.38k
                                                   CheckerContext &C) const {
89
3.38k
  // Check if the method is annotated with analyzer_noreturn.
90
3.38k
  if (const ObjCMethodDecl *MD = Msg.getDecl()) {
91
3.36k
    MD = MD->getCanonicalDecl();
92
3.36k
    if (MD->hasAttr<AnalyzerNoReturnAttr>()) {
93
3
      C.generateSink(C.getState(), C.getPredecessor());
94
3
      return;
95
3
    }
96
3.38k
  }
97
3.38k
98
3.38k
  // HACK: This entire check is to handle two messages in the Cocoa frameworks:
99
3.38k
  // -[NSAssertionHandler
100
3.38k
  //    handleFailureInMethod:object:file:lineNumber:description:]
101
3.38k
  // -[NSAssertionHandler
102
3.38k
  //    handleFailureInFunction:file:lineNumber:description:]
103
3.38k
  // Eventually these should be annotated with __attribute__((noreturn)).
104
3.38k
  // Because ObjC messages use dynamic dispatch, it is not generally safe to
105
3.38k
  // assume certain methods can't return. In cases where it is definitely valid,
106
3.38k
  // see if you can mark the methods noreturn or analyzer_noreturn instead of
107
3.38k
  // adding more explicit checks to this method.
108
3.38k
109
3.38k
  if (!Msg.isInstanceMessage())
110
857
    return;
111
2.52k
112
2.52k
  const ObjCInterfaceDecl *Receiver = Msg.getReceiverInterface();
113
2.52k
  if (!Receiver)
114
424
    return;
115
2.10k
  if (!Receiver->getIdentifier()->isStr("NSAssertionHandler"))
116
2.09k
    return;
117
3
118
3
  Selector Sel = Msg.getSelector();
119
3
  switch (Sel.getNumArgs()) {
120
0
  default:
121
0
    return;
122
1
  case 4:
123
1
    lazyInitKeywordSelector(HandleFailureInFunctionSel, C.getASTContext(),
124
1
                            "handleFailureInFunction", "file", "lineNumber",
125
1
                            "description");
126
1
    if (Sel != HandleFailureInFunctionSel)
127
0
      return;
128
1
    break;
129
2
  case 5:
130
2
    lazyInitKeywordSelector(HandleFailureInMethodSel, C.getASTContext(),
131
2
                            "handleFailureInMethod", "object", "file",
132
2
                            "lineNumber", "description");
133
2
    if (Sel != HandleFailureInMethodSel)
134
0
      return;
135
2
    break;
136
3
  }
137
3
138
3
  // If we got here, it's one of the messages we care about.
139
3
  C.generateSink(C.getState(), C.getPredecessor());
140
3
}
141
142
1.01k
void ento::registerNoReturnFunctionChecker(CheckerManager &mgr) {
143
1.01k
  mgr.registerChecker<NoReturnFunctionChecker>();
144
1.01k
}
145
146
1.01k
bool ento::shouldRegisterNoReturnFunctionChecker(const LangOptions &LO) {
147
1.01k
  return true;
148
1.01k
}