Coverage Report

Created: 2020-09-19 12:23

/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
100k
                                            CheckerContext &C) const {
42
100k
  bool BuildSinks = false;
43
100k
44
100k
  if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(CE.getDecl()))
45
97.1k
    BuildSinks = FD->hasAttr<AnalyzerNoReturnAttr>() || 
FD->isNoReturn()97.1k
;
46
100k
47
100k
  const Expr *Callee = CE.getOriginExpr();
48
100k
  if (!BuildSinks && 
Callee100k
)
49
98.8k
    BuildSinks = getFunctionExtInfo(Callee->getType()).getNoReturn();
50
100k
51
100k
  if (!BuildSinks && 
CE.isGlobalCFunction()100k
) {
52
48.4k
    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.
56
48.4k
      BuildSinks
57
48.4k
        = llvm::StringSwitch<bool>(StringRef(II->getName()))
58
48.4k
            .Case("exit", true)
59
48.4k
            .Case("panic", true)
60
48.4k
            .Case("error", true)
61
48.4k
            .Case("Assert", true)
62
            // FIXME: This is just a wrapper around throwing an exception.
63
            //  Eventually inter-procedural analysis should handle this easily.
64
48.4k
            .Case("ziperr", true)
65
48.4k
            .Case("assfail", true)
66
48.4k
            .Case("db_error", true)
67
48.4k
            .Case("__assert", true)
68
48.4k
            .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
48.4k
            .Case("_wassert", true)
72
48.4k
            .Case("__assert_rtn", true)
73
48.4k
            .Case("__assert_fail", true)
74
48.4k
            .Case("dtrace_assfail", true)
75
48.4k
            .Case("yy_fatal_error", true)
76
48.4k
            .Case("_XCAssertionFailureHandler", true)
77
48.4k
            .Case("_DTAssertionFailureHandler", true)
78
48.4k
            .Case("_TSAssertionFailureHandler", true)
79
48.4k
            .Default(false);
80
48.4k
    }
81
48.4k
  }
82
100k
83
100k
  if (BuildSinks)
84
579
    C.generateSink(C.getState(), C.getPredecessor());
85
100k
}
86
87
void NoReturnFunctionChecker::checkPostObjCMessage(const ObjCMethodCall &Msg,
88
3.43k
                                                   CheckerContext &C) const {
89
  // Check if the method is annotated with analyzer_noreturn.
90
3.43k
  if (const ObjCMethodDecl *MD = Msg.getDecl()) {
91
3.40k
    MD = MD->getCanonicalDecl();
92
3.40k
    if (MD->hasAttr<AnalyzerNoReturnAttr>()) {
93
3
      C.generateSink(C.getState(), C.getPredecessor());
94
3
      return;
95
3
    }
96
3.42k
  }
97
3.42k
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.
108
3.42k
109
3.42k
  if (!Msg.isInstanceMessage())
110
876
    return;
111
2.55k
112
2.55k
  const ObjCInterfaceDecl *Receiver = Msg.getReceiverInterface();
113
2.55k
  if (!Receiver)
114
448
    return;
115
2.10k
  if (!Receiver->getIdentifier()->isStr("NSAssertionHandler"))
116
2.10k
    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
  // 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.08k
void ento::registerNoReturnFunctionChecker(CheckerManager &mgr) {
143
1.08k
  mgr.registerChecker<NoReturnFunctionChecker>();
144
1.08k
}
145
146
2.17k
bool ento::shouldRegisterNoReturnFunctionChecker(const CheckerManager &mgr) {
147
2.17k
  return true;
148
2.17k
}