blob: c038a2649e15d04bb499129c4a06b9a5d4285e71 [file] [log] [blame]
Ted Kremenekb663ffe2010-02-25 05:44:09 +00001// MacOSXAPIChecker.h - Checks proper use of various MacOS X APIs --*- C++ -*-//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This defines MacOSXAPIChecker, which is an assortment of checks on calls
Anna Zaks8ef07e52013-04-03 19:28:22 +000011// to various, widely used Apple APIs.
Ted Kremenekb663ffe2010-02-25 05:44:09 +000012//
13// FIXME: What's currently in BasicObjCFoundationChecks.cpp should be migrated
14// to here, using the new Checker interface.
15//
16//===----------------------------------------------------------------------===//
17
Argyrios Kyrtzidisa6d04d52011-02-15 07:42:33 +000018#include "ClangSACheckers.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000019#include "clang/Basic/TargetInfo.h"
20#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
Argyrios Kyrtzidis6a5674f2011-03-01 01:16:21 +000021#include "clang/StaticAnalyzer/Core/Checker.h"
Argyrios Kyrtzidis507ff532011-02-17 21:39:17 +000022#include "clang/StaticAnalyzer/Core/CheckerManager.h"
Argyrios Kyrtzidisdff865d2011-02-23 01:05:36 +000023#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
Ted Kremenek001fd5b2011-08-15 22:09:50 +000024#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h"
Ted Kremenekb663ffe2010-02-25 05:44:09 +000025#include "llvm/ADT/SmallString.h"
26#include "llvm/ADT/StringSwitch.h"
27#include "llvm/Support/raw_ostream.h"
28
29using namespace clang;
Ted Kremenek98857c92010-12-23 07:20:52 +000030using namespace ento;
Ted Kremenekb663ffe2010-02-25 05:44:09 +000031
32namespace {
Argyrios Kyrtzidis6a5674f2011-03-01 01:16:21 +000033class MacOSXAPIChecker : public Checker< check::PreStmt<CallExpr> > {
Ahmed Charlesb8984322014-03-07 20:03:18 +000034 mutable std::unique_ptr<BugType> BT_dispatchOnce;
Ted Kremenekb663ffe2010-02-25 05:44:09 +000035
36public:
Argyrios Kyrtzidisdff865d2011-02-23 01:05:36 +000037 void checkPreStmt(const CallExpr *CE, CheckerContext &C) const;
Jordy Roseede26952011-07-15 06:02:19 +000038
39 void CheckDispatchOnce(CheckerContext &C, const CallExpr *CE,
Anna Zaksc6aa5312011-12-01 05:57:37 +000040 StringRef FName) const;
Jordy Roseede26952011-07-15 06:02:19 +000041
42 typedef void (MacOSXAPIChecker::*SubChecker)(CheckerContext &,
43 const CallExpr *,
Anna Zaksc6aa5312011-12-01 05:57:37 +000044 StringRef FName) const;
Ted Kremenekb663ffe2010-02-25 05:44:09 +000045};
46} //end anonymous namespace
47
Ted Kremenekb663ffe2010-02-25 05:44:09 +000048//===----------------------------------------------------------------------===//
49// dispatch_once and dispatch_once_f
50//===----------------------------------------------------------------------===//
51
Jordy Roseede26952011-07-15 06:02:19 +000052void MacOSXAPIChecker::CheckDispatchOnce(CheckerContext &C, const CallExpr *CE,
Anna Zaksc6aa5312011-12-01 05:57:37 +000053 StringRef FName) const {
Ted Kremenekb663ffe2010-02-25 05:44:09 +000054 if (CE->getNumArgs() < 1)
55 return;
56
57 // Check if the first argument is stack allocated. If so, issue a warning
58 // because that's likely to be bad news.
Ted Kremenek49b1e382012-01-26 21:29:00 +000059 ProgramStateRef state = C.getState();
Ted Kremenek632e3b72012-01-06 22:09:28 +000060 const MemRegion *R =
61 state->getSVal(CE->getArg(0), C.getLocationContext()).getAsRegion();
Ted Kremenekb663ffe2010-02-25 05:44:09 +000062 if (!R || !isa<StackSpaceRegion>(R->getMemorySpace()))
63 return;
64
Devin Coughline39bd402015-09-16 22:03:05 +000065 ExplodedNode *N = C.generateErrorNode(state);
Ted Kremenekb663ffe2010-02-25 05:44:09 +000066 if (!N)
67 return;
68
Jordy Roseede26952011-07-15 06:02:19 +000069 if (!BT_dispatchOnce)
Alexander Kornienko4aca9b12014-02-11 21:49:21 +000070 BT_dispatchOnce.reset(new BugType(this, "Improper use of 'dispatch_once'",
Anna Zaks8ef07e52013-04-03 19:28:22 +000071 "API Misuse (Apple)"));
Jordy Roseede26952011-07-15 06:02:19 +000072
Ted Kremenek5371c732012-09-13 19:48:51 +000073 // Handle _dispatch_once. In some versions of the OS X SDK we have the case
74 // that dispatch_once is a macro that wraps a call to _dispatch_once.
75 // _dispatch_once is then a function which then calls the real dispatch_once.
76 // Users do not care; they just want the warning at the top-level call.
Ted Kremeneke5c0a9b2012-09-13 18:18:37 +000077 if (CE->getLocStart().isMacroID()) {
Vedant Kumar409506e2016-02-16 02:14:44 +000078 StringRef TrimmedFName = FName.ltrim('_');
Ted Kremeneke5c0a9b2012-09-13 18:18:37 +000079 if (TrimmedFName != FName)
80 FName = TrimmedFName;
81 }
Ted Kremenek3a0678e2015-09-08 03:50:52 +000082
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +000083 SmallString<256> S;
Ted Kremenekb663ffe2010-02-25 05:44:09 +000084 llvm::raw_svector_ostream os(S);
Anna Zaksc6aa5312011-12-01 05:57:37 +000085 os << "Call to '" << FName << "' uses";
Ted Kremenekb663ffe2010-02-25 05:44:09 +000086 if (const VarRegion *VR = dyn_cast<VarRegion>(R))
87 os << " the local variable '" << VR->getDecl()->getName() << '\'';
88 else
89 os << " stack allocated memory";
90 os << " for the predicate value. Using such transient memory for "
91 "the predicate is potentially dangerous.";
92 if (isa<VarRegion>(R) && isa<StackLocalsSpaceRegion>(R->getMemorySpace()))
93 os << " Perhaps you intended to declare the variable as 'static'?";
94
Aaron Ballman8d3a7a52015-06-23 13:15:32 +000095 auto report = llvm::make_unique<BugReport>(*BT_dispatchOnce, os.str(), N);
Ted Kremenekb663ffe2010-02-25 05:44:09 +000096 report->addRange(CE->getArg(0)->getSourceRange());
Aaron Ballman8d3a7a52015-06-23 13:15:32 +000097 C.emitReport(std::move(report));
Ted Kremenekb663ffe2010-02-25 05:44:09 +000098}
99
100//===----------------------------------------------------------------------===//
101// Central dispatch function.
102//===----------------------------------------------------------------------===//
103
Argyrios Kyrtzidisdff865d2011-02-23 01:05:36 +0000104void MacOSXAPIChecker::checkPreStmt(const CallExpr *CE,
105 CheckerContext &C) const {
Anna Zaksc6aa5312011-12-01 05:57:37 +0000106 StringRef Name = C.getCalleeName(CE);
107 if (Name.empty())
Ted Kremenekb663ffe2010-02-25 05:44:09 +0000108 return;
109
Jordy Roseede26952011-07-15 06:02:19 +0000110 SubChecker SC =
Anna Zaksc6aa5312011-12-01 05:57:37 +0000111 llvm::StringSwitch<SubChecker>(Name)
Ted Kremeneke5c0a9b2012-09-13 18:18:37 +0000112 .Cases("dispatch_once",
113 "_dispatch_once",
114 "dispatch_once_f",
Jordy Roseede26952011-07-15 06:02:19 +0000115 &MacOSXAPIChecker::CheckDispatchOnce)
Craig Topper0dbb7832014-05-27 02:45:47 +0000116 .Default(nullptr);
Ted Kremenekb663ffe2010-02-25 05:44:09 +0000117
Jordy Roseede26952011-07-15 06:02:19 +0000118 if (SC)
Anna Zaksc6aa5312011-12-01 05:57:37 +0000119 (this->*SC)(C, CE, Name);
Ted Kremenekb663ffe2010-02-25 05:44:09 +0000120}
Argyrios Kyrtzidisdff865d2011-02-23 01:05:36 +0000121
122//===----------------------------------------------------------------------===//
123// Registration.
124//===----------------------------------------------------------------------===//
125
126void ento::registerMacOSXAPIChecker(CheckerManager &mgr) {
127 mgr.registerChecker<MacOSXAPIChecker>();
128}