Coverage Report

Created: 2023-09-21 18:56

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