Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/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
82.6k
                                            CheckerContext &C) const {
42
82.6k
  bool BuildSinks = false;
43
82.6k
44
82.6k
  if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(CE.getDecl()))
45
79.1k
    BuildSinks = FD->hasAttr<AnalyzerNoReturnAttr>() || 
FD->isNoReturn()79.1k
;
46
82.6k
47
82.6k
  const Expr *Callee = CE.getOriginExpr();
48
82.6k
  if (!BuildSinks && 
Callee82.1k
)
49
81.0k
    BuildSinks = getFunctionExtInfo(Callee->getType()).getNoReturn();
50
82.6k
51
82.6k
  if (!BuildSinks && 
CE.isGlobalCFunction()82.1k
) {
52
38.5k
    if (const IdentifierInfo *II = CE.getCalleeIdentifier()) {
53
38.5k
      // HACK: Some functions are not marked noreturn, and don't return.
54
38.5k
      //  Here are a few hardwired ones.  If this takes too long, we can
55
38.5k
      //  potentially cache these results.
56
38.5k
      BuildSinks
57
38.5k
        = llvm::StringSwitch<bool>(StringRef(II->getName()))
58
38.5k
            .Case("exit", true)
59
38.5k
            .Case("panic", true)
60
38.5k
            .Case("error", true)
61
38.5k
            .Case("Assert", true)
62
38.5k
            // FIXME: This is just a wrapper around throwing an exception.
63
38.5k
            //  Eventually inter-procedural analysis should handle this easily.
64
38.5k
            .Case("ziperr", true)
65
38.5k
            .Case("assfail", true)
66
38.5k
            .Case("db_error", true)
67
38.5k
            .Case("__assert", true)
68
38.5k
            .Case("__assert2", true)
69
38.5k
            // For the purpose of static analysis, we do not care that
70
38.5k
            //  this MSVC function will return if the user decides to continue.
71
38.5k
            .Case("_wassert", true)
72
38.5k
            .Case("__assert_rtn", true)
73
38.5k
            .Case("__assert_fail", true)
74
38.5k
            .Case("dtrace_assfail", true)
75
38.5k
            .Case("yy_fatal_error", true)
76
38.5k
            .Case("_XCAssertionFailureHandler", true)
77
38.5k
            .Case("_DTAssertionFailureHandler", true)
78
38.5k
            .Case("_TSAssertionFailureHandler", true)
79
38.5k
            .Default(false);
80
38.5k
    }
81
38.5k
  }
82
82.6k
83
82.6k
  if (BuildSinks)
84
524
    C.generateSink(C.getState(), C.getPredecessor());
85
82.6k
}
86
87
void NoReturnFunctionChecker::checkPostObjCMessage(const ObjCMethodCall &Msg,
88
3.32k
                                                   CheckerContext &C) const {
89
3.32k
  // Check if the method is annotated with analyzer_noreturn.
90
3.32k
  if (const ObjCMethodDecl *MD = Msg.getDecl()) {
91
3.30k
    MD = MD->getCanonicalDecl();
92
3.30k
    if (MD->hasAttr<AnalyzerNoReturnAttr>()) {
93
3
      C.generateSink(C.getState(), C.getPredecessor());
94
3
      return;
95
3
    }
96
3.31k
  }
97
3.31k
98
3.31k
  // HACK: This entire check is to handle two messages in the Cocoa frameworks:
99
3.31k
  // -[NSAssertionHandler
100
3.31k
  //    handleFailureInMethod:object:file:lineNumber:description:]
101
3.31k
  // -[NSAssertionHandler
102
3.31k
  //    handleFailureInFunction:file:lineNumber:description:]
103
3.31k
  // Eventually these should be annotated with __attribute__((noreturn)).
104
3.31k
  // Because ObjC messages use dynamic dispatch, it is not generally safe to
105
3.31k
  // assume certain methods can't return. In cases where it is definitely valid,
106
3.31k
  // see if you can mark the methods noreturn or analyzer_noreturn instead of
107
3.31k
  // adding more explicit checks to this method.
108
3.31k
109
3.31k
  if (!Msg.isInstanceMessage())
110
852
    return;
111
2.46k
112
2.46k
  const ObjCInterfaceDecl *Receiver = Msg.getReceiverInterface();
113
2.46k
  if (!Receiver)
114
422
    return;
115
2.04k
  if (!Receiver->getIdentifier()->isStr("NSAssertionHandler"))
116
2.04k
    return;
117
3
118
3
  Selector Sel = Msg.getSelector();
119
3
  switch (Sel.getNumArgs()) {
120
3
  default:
121
0
    return;
122
3
  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
686
void ento::registerNoReturnFunctionChecker(CheckerManager &mgr) {
143
686
  mgr.registerChecker<NoReturnFunctionChecker>();
144
686
}
145
146
687
bool ento::shouldRegisterNoReturnFunctionChecker(const LangOptions &LO) {
147
687
  return true;
148
687
}