1 // MacOSXAPIChecker.h - Checks proper use of various MacOS X APIs --*- C++ -*-//
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
7 //===----------------------------------------------------------------------===//
9 // This defines MacOSXAPIChecker, which is an assortment of checks on calls
10 // to various, widely used Apple APIs.
12 // FIXME: What's currently in BasicObjCFoundationChecks.cpp should be migrated
13 // to here, using the new Checker interface.
15 //===----------------------------------------------------------------------===//
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/BugReporter/CommonBugCategories.h"
22 #include "clang/StaticAnalyzer/Core/Checker.h"
23 #include "clang/StaticAnalyzer/Core/CheckerManager.h"
24 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
25 #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h"
26 #include "llvm/ADT/SmallString.h"
27 #include "llvm/ADT/StringSwitch.h"
28 #include "llvm/Support/raw_ostream.h"
30 using namespace clang
;
34 class MacOSXAPIChecker
: public Checker
< check::PreStmt
<CallExpr
> > {
35 const BugType BT_dispatchOnce
{this, "Improper use of 'dispatch_once'",
36 categories::AppleAPIMisuse
};
38 static const ObjCIvarRegion
*getParentIvarRegion(const MemRegion
*R
);
41 void checkPreStmt(const CallExpr
*CE
, CheckerContext
&C
) const;
43 void CheckDispatchOnce(CheckerContext
&C
, const CallExpr
*CE
,
44 StringRef FName
) const;
46 typedef void (MacOSXAPIChecker::*SubChecker
)(CheckerContext
&,
48 StringRef FName
) const;
50 } //end anonymous namespace
52 //===----------------------------------------------------------------------===//
53 // dispatch_once and dispatch_once_f
54 //===----------------------------------------------------------------------===//
56 const ObjCIvarRegion
*
57 MacOSXAPIChecker::getParentIvarRegion(const MemRegion
*R
) {
58 const SubRegion
*SR
= dyn_cast
<SubRegion
>(R
);
60 if (const ObjCIvarRegion
*IR
= dyn_cast
<ObjCIvarRegion
>(SR
))
62 SR
= dyn_cast
<SubRegion
>(SR
->getSuperRegion());
67 void MacOSXAPIChecker::CheckDispatchOnce(CheckerContext
&C
, const CallExpr
*CE
,
68 StringRef FName
) const {
69 if (CE
->getNumArgs() < 1)
72 // Check if the first argument is improperly allocated. If so, issue a
73 // warning because that's likely to be bad news.
74 const MemRegion
*R
= C
.getSVal(CE
->getArg(0)).getAsRegion();
78 // Global variables are fine.
79 const MemRegion
*RB
= R
->getBaseRegion();
80 const MemSpaceRegion
*RS
= RB
->getMemorySpace();
81 if (isa
<GlobalsSpaceRegion
>(RS
))
84 // Handle _dispatch_once. In some versions of the OS X SDK we have the case
85 // that dispatch_once is a macro that wraps a call to _dispatch_once.
86 // _dispatch_once is then a function which then calls the real dispatch_once.
87 // Users do not care; they just want the warning at the top-level call.
88 if (CE
->getBeginLoc().isMacroID()) {
89 StringRef TrimmedFName
= FName
.ltrim('_');
90 if (TrimmedFName
!= FName
)
95 llvm::raw_svector_ostream
os(S
);
96 bool SuggestStatic
= false;
97 os
<< "Call to '" << FName
<< "' uses";
98 if (const VarRegion
*VR
= dyn_cast
<VarRegion
>(RB
)) {
99 const VarDecl
*VD
= VR
->getDecl();
100 // FIXME: These should have correct memory space and thus should be filtered
101 // out earlier. This branch only fires when we're looking from a block,
102 // which we analyze as a top-level declaration, onto a static local
103 // in a function that contains the block.
104 if (VD
->isStaticLocal())
106 // We filtered out globals earlier, so it must be a local variable
107 // or a block variable which is under UnknownSpaceRegion.
109 os
<< " memory within";
110 if (VD
->hasAttr
<BlocksAttr
>())
111 os
<< " the block variable '";
113 os
<< " the local variable '";
114 os
<< VR
->getDecl()->getName() << '\'';
115 SuggestStatic
= true;
116 } else if (const ObjCIvarRegion
*IVR
= getParentIvarRegion(R
)) {
118 os
<< " memory within";
119 os
<< " the instance variable '" << IVR
->getDecl()->getName() << '\'';
120 } else if (isa
<HeapSpaceRegion
>(RS
)) {
121 os
<< " heap-allocated memory";
122 } else if (isa
<UnknownSpaceRegion
>(RS
)) {
123 // Presence of an IVar superregion has priority over this branch, because
124 // ObjC objects are on the heap even if the core doesn't realize this.
125 // Presence of a block variable base region has priority over this branch,
126 // because block variables are known to be either on stack or on heap
127 // (might actually move between the two, hence UnknownSpace).
130 os
<< " stack allocated memory";
132 os
<< " for the predicate value. Using such transient memory for "
133 "the predicate is potentially dangerous.";
135 os
<< " Perhaps you intended to declare the variable as 'static'?";
137 ExplodedNode
*N
= C
.generateErrorNode();
142 std::make_unique
<PathSensitiveBugReport
>(BT_dispatchOnce
, os
.str(), N
);
143 report
->addRange(CE
->getArg(0)->getSourceRange());
144 C
.emitReport(std::move(report
));
147 //===----------------------------------------------------------------------===//
148 // Central dispatch function.
149 //===----------------------------------------------------------------------===//
151 void MacOSXAPIChecker::checkPreStmt(const CallExpr
*CE
,
152 CheckerContext
&C
) const {
153 StringRef Name
= C
.getCalleeName(CE
);
158 llvm::StringSwitch
<SubChecker
>(Name
)
159 .Cases("dispatch_once",
162 &MacOSXAPIChecker::CheckDispatchOnce
)
166 (this->*SC
)(C
, CE
, Name
);
169 //===----------------------------------------------------------------------===//
171 //===----------------------------------------------------------------------===//
173 void ento::registerMacOSXAPIChecker(CheckerManager
&mgr
) {
174 mgr
.registerChecker
<MacOSXAPIChecker
>();
177 bool ento::shouldRegisterMacOSXAPIChecker(const CheckerManager
&mgr
) {