1 // MacOSXAPIChecker.h - Checks proper use of various MacOS X APIs --*- C++ -*-//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This defines MacOSXAPIChecker, which is an assortment of checks on calls
11 // to various, widely used Apple APIs.
13 // FIXME: What's currently in BasicObjCFoundationChecks.cpp should be migrated
14 // to here, using the new Checker interface.
16 //===----------------------------------------------------------------------===//
18 #include "ClangSACheckers.h"
19 #include "clang/Basic/TargetInfo.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"
29 using namespace clang;
33 class MacOSXAPIChecker : public Checker< check::PreStmt<CallExpr> > {
34 mutable std::unique_ptr<BugType> BT_dispatchOnce;
36 static const ObjCIvarRegion *getParentIvarRegion(const MemRegion *R);
39 void checkPreStmt(const CallExpr *CE, CheckerContext &C) const;
41 void CheckDispatchOnce(CheckerContext &C, const CallExpr *CE,
42 StringRef FName) const;
44 typedef void (MacOSXAPIChecker::*SubChecker)(CheckerContext &,
46 StringRef FName) const;
48 } //end anonymous namespace
50 //===----------------------------------------------------------------------===//
51 // dispatch_once and dispatch_once_f
52 //===----------------------------------------------------------------------===//
54 const ObjCIvarRegion *
55 MacOSXAPIChecker::getParentIvarRegion(const MemRegion *R) {
56 const SubRegion *SR = dyn_cast<SubRegion>(R);
58 if (const ObjCIvarRegion *IR = dyn_cast<ObjCIvarRegion>(SR))
60 SR = dyn_cast<SubRegion>(SR->getSuperRegion());
65 void MacOSXAPIChecker::CheckDispatchOnce(CheckerContext &C, const CallExpr *CE,
66 StringRef FName) const {
67 if (CE->getNumArgs() < 1)
70 // Check if the first argument is improperly allocated. If so, issue a
71 // warning because that's likely to be bad news.
72 const MemRegion *R = C.getSVal(CE->getArg(0)).getAsRegion();
76 // Global variables are fine.
77 const MemRegion *RB = R->getBaseRegion();
78 const MemSpaceRegion *RS = RB->getMemorySpace();
79 if (isa<GlobalsSpaceRegion>(RS))
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 if (CE->getLocStart().isMacroID()) {
87 StringRef TrimmedFName = FName.ltrim('_');
88 if (TrimmedFName != FName)
93 llvm::raw_svector_ostream os(S);
94 bool SuggestStatic = false;
95 os << "Call to '" << FName << "' uses";
96 if (const VarRegion *VR = dyn_cast<VarRegion>(RB)) {
97 // We filtered out globals earlier, so it must be a local variable
98 // or a block variable which is under UnknownSpaceRegion.
100 os << " memory within";
101 if (VR->getDecl()->hasAttr<BlocksAttr>())
102 os << " the block variable '";
104 os << " the local variable '";
105 os << VR->getDecl()->getName() << '\'';
106 SuggestStatic = true;
107 } else if (const ObjCIvarRegion *IVR = getParentIvarRegion(R)) {
109 os << " memory within";
110 os << " the instance variable '" << IVR->getDecl()->getName() << '\'';
111 } else if (isa<HeapSpaceRegion>(RS)) {
112 os << " heap-allocated memory";
113 } else if (isa<UnknownSpaceRegion>(RS)) {
114 // Presence of an IVar superregion has priority over this branch, because
115 // ObjC objects are on the heap even if the core doesn't realize this.
116 // Presence of a block variable base region has priority over this branch,
117 // because block variables are known to be either on stack or on heap
118 // (might actually move between the two, hence UnknownSpace).
121 os << " stack allocated memory";
123 os << " for the predicate value. Using such transient memory for "
124 "the predicate is potentially dangerous.";
126 os << " Perhaps you intended to declare the variable as 'static'?";
128 ExplodedNode *N = C.generateErrorNode();
132 if (!BT_dispatchOnce)
133 BT_dispatchOnce.reset(new BugType(this, "Improper use of 'dispatch_once'",
134 "API Misuse (Apple)"));
136 auto report = llvm::make_unique<BugReport>(*BT_dispatchOnce, os.str(), N);
137 report->addRange(CE->getArg(0)->getSourceRange());
138 C.emitReport(std::move(report));
141 //===----------------------------------------------------------------------===//
142 // Central dispatch function.
143 //===----------------------------------------------------------------------===//
145 void MacOSXAPIChecker::checkPreStmt(const CallExpr *CE,
146 CheckerContext &C) const {
147 StringRef Name = C.getCalleeName(CE);
152 llvm::StringSwitch<SubChecker>(Name)
153 .Cases("dispatch_once",
156 &MacOSXAPIChecker::CheckDispatchOnce)
160 (this->*SC)(C, CE, Name);
163 //===----------------------------------------------------------------------===//
165 //===----------------------------------------------------------------------===//
167 void ento::registerMacOSXAPIChecker(CheckerManager &mgr) {
168 mgr.registerChecker<MacOSXAPIChecker>();