Coverage Report

Created: 2023-09-30 09:22

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/StaticAnalyzer/Checkers/MacOSXAPIChecker.cpp
Line
Count
Source (jump to first uncovered line)
1
// MacOSXAPIChecker.h - Checks proper use of various MacOS X APIs --*- 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 MacOSXAPIChecker, which is an assortment of checks on calls
10
// to various, widely used Apple APIs.
11
//
12
// FIXME: What's currently in BasicObjCFoundationChecks.cpp should be migrated
13
// to here, using the new Checker interface.
14
//
15
//===----------------------------------------------------------------------===//
16
17
#include "clang/AST/Attr.h"
18
#include "clang/Basic/TargetInfo.h"
19
#include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
20
#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
21
#include "clang/StaticAnalyzer/Core/Checker.h"
22
#include "clang/StaticAnalyzer/Core/CheckerManager.h"
23
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
24
#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h"
25
#include "llvm/ADT/SmallString.h"
26
#include "llvm/ADT/StringSwitch.h"
27
#include "llvm/Support/raw_ostream.h"
28
29
using namespace clang;
30
using namespace ento;
31
32
namespace {
33
class MacOSXAPIChecker : public Checker< check::PreStmt<CallExpr> > {
34
  mutable std::unique_ptr<BugType> BT_dispatchOnce;
35
36
  static const ObjCIvarRegion *getParentIvarRegion(const MemRegion *R);
37
38
public:
39
  void checkPreStmt(const CallExpr *CE, CheckerContext &C) const;
40
41
  void CheckDispatchOnce(CheckerContext &C, const CallExpr *CE,
42
                         StringRef FName) const;
43
44
  typedef void (MacOSXAPIChecker::*SubChecker)(CheckerContext &,
45
                                               const CallExpr *,
46
                                               StringRef FName) const;
47
};
48
} //end anonymous namespace
49
50
//===----------------------------------------------------------------------===//
51
// dispatch_once and dispatch_once_f
52
//===----------------------------------------------------------------------===//
53
54
const ObjCIvarRegion *
55
24
MacOSXAPIChecker::getParentIvarRegion(const MemRegion *R) {
56
24
  const SubRegion *SR = dyn_cast<SubRegion>(R);
57
48
  while (SR) {
58
42
    if (const ObjCIvarRegion *IR = dyn_cast<ObjCIvarRegion>(SR))
59
18
      return IR;
60
24
    SR = dyn_cast<SubRegion>(SR->getSuperRegion());
61
24
  }
62
6
  return nullptr;
63
24
}
64
65
void MacOSXAPIChecker::CheckDispatchOnce(CheckerContext &C, const CallExpr *CE,
66
54
                                         StringRef FName) const {
67
54
  if (CE->getNumArgs() < 1)
68
0
    return;
69
70
  // Check if the first argument is improperly allocated.  If so, issue a
71
  // warning because that's likely to be bad news.
72
54
  const MemRegion *R = C.getSVal(CE->getArg(0)).getAsRegion();
73
54
  if (!R)
74
0
    return;
75
76
  // Global variables are fine.
77
54
  const MemRegion *RB = R->getBaseRegion();
78
54
  const MemSpaceRegion *RS = RB->getMemorySpace();
79
54
  if (isa<GlobalsSpaceRegion>(RS))
80
15
    return;
81
82
  // Handle _dispatch_once.  In some versions of the OS X SDK we have the case
83
  // that dispatch_once is a macro that wraps a call to _dispatch_once.
84
  // _dispatch_once is then a function which then calls the real dispatch_once.
85
  // Users do not care; they just want the warning at the top-level call.
86
39
  if (CE->getBeginLoc().isMacroID()) {
87
4
    StringRef TrimmedFName = FName.ltrim('_');
88
4
    if (TrimmedFName != FName)
89
4
      FName = TrimmedFName;
90
4
  }
91
92
39
  SmallString<256> S;
93
39
  llvm::raw_svector_ostream os(S);
94
39
  bool SuggestStatic = false;
95
39
  os << "Call to '" << FName << "' uses";
96
39
  if (const VarRegion *VR = dyn_cast<VarRegion>(RB)) {
97
15
    const VarDecl *VD = VR->getDecl();
98
    // FIXME: These should have correct memory space and thus should be filtered
99
    // out earlier. This branch only fires when we're looking from a block,
100
    // which we analyze as a top-level declaration, onto a static local
101
    // in a function that contains the block.
102
15
    if (VD->isStaticLocal())
103
2
      return;
104
    // We filtered out globals earlier, so it must be a local variable
105
    // or a block variable which is under UnknownSpaceRegion.
106
13
    if (VR != R)
107
2
      os << " memory within";
108
13
    if (VD->hasAttr<BlocksAttr>())
109
4
      os << " the block variable '";
110
9
    else
111
9
      os << " the local variable '";
112
13
    os << VR->getDecl()->getName() << '\'';
113
13
    SuggestStatic = true;
114
24
  } else if (const ObjCIvarRegion *IVR = getParentIvarRegion(R)) {
115
18
    if (IVR != R)
116
12
      os << " memory within";
117
18
    os << " the instance variable '" << IVR->getDecl()->getName() << '\'';
118
18
  } else 
if (6
isa<HeapSpaceRegion>(RS)6
) {
119
4
    os << " heap-allocated memory";
120
4
  } else 
if (2
isa<UnknownSpaceRegion>(RS)2
) {
121
    // Presence of an IVar superregion has priority over this branch, because
122
    // ObjC objects are on the heap even if the core doesn't realize this.
123
    // Presence of a block variable base region has priority over this branch,
124
    // because block variables are known to be either on stack or on heap
125
    // (might actually move between the two, hence UnknownSpace).
126
2
    return;
127
2
  } else {
128
0
    os << " stack allocated memory";
129
0
  }
130
35
  os << " for the predicate value.  Using such transient memory for "
131
35
        "the predicate is potentially dangerous.";
132
35
  if (SuggestStatic)
133
13
    os << "  Perhaps you intended to declare the variable as 'static'?";
134
135
35
  ExplodedNode *N = C.generateErrorNode();
136
35
  if (!N)
137
0
    return;
138
139
35
  if (!BT_dispatchOnce)
140
5
    BT_dispatchOnce.reset(new BugType(this, "Improper use of 'dispatch_once'",
141
5
                                      "API Misuse (Apple)"));
142
143
35
  auto report =
144
35
      std::make_unique<PathSensitiveBugReport>(*BT_dispatchOnce, os.str(), N);
145
35
  report->addRange(CE->getArg(0)->getSourceRange());
146
35
  C.emitReport(std::move(report));
147
35
}
148
149
//===----------------------------------------------------------------------===//
150
// Central dispatch function.
151
//===----------------------------------------------------------------------===//
152
153
void MacOSXAPIChecker::checkPreStmt(const CallExpr *CE,
154
509
                                    CheckerContext &C) const {
155
509
  StringRef Name = C.getCalleeName(CE);
156
509
  if (Name.empty())
157
20
    return;
158
159
489
  SubChecker SC =
160
489
    llvm::StringSwitch<SubChecker>(Name)
161
489
      .Cases("dispatch_once",
162
489
             "_dispatch_once",
163
489
             "dispatch_once_f",
164
489
             &MacOSXAPIChecker::CheckDispatchOnce)
165
489
      .Default(nullptr);
166
167
489
  if (SC)
168
54
    (this->*SC)(C, CE, Name);
169
489
}
170
171
//===----------------------------------------------------------------------===//
172
// Registration.
173
//===----------------------------------------------------------------------===//
174
175
51
void ento::registerMacOSXAPIChecker(CheckerManager &mgr) {
176
51
  mgr.registerChecker<MacOSXAPIChecker>();
177
51
}
178
179
102
bool ento::shouldRegisterMacOSXAPIChecker(const CheckerManager &mgr) {
180
102
  return true;
181
102
}