blob: a76b3d7a7e9950bc1c5266256715c93e116b9cbb [file] [log] [blame]
Ted Kremenekdf61b582010-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
11// to various, widely used Mac OS X functions.
12//
13// FIXME: What's currently in BasicObjCFoundationChecks.cpp should be migrated
14// to here, using the new Checker interface.
15//
16//===----------------------------------------------------------------------===//
17
Argyrios Kyrtzidis027a6ab2011-02-15 07:42:33 +000018#include "ClangSACheckers.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000019#include "clang/Basic/TargetInfo.h"
20#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
Argyrios Kyrtzidisec8605f2011-03-01 01:16:21 +000021#include "clang/StaticAnalyzer/Core/Checker.h"
Argyrios Kyrtzidis695fb502011-02-17 21:39:17 +000022#include "clang/StaticAnalyzer/Core/CheckerManager.h"
Argyrios Kyrtzidis983326f2011-02-23 01:05:36 +000023#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
Ted Kremenek18c66fd2011-08-15 22:09:50 +000024#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h"
Ted Kremenekdf61b582010-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 Kremenek9ef65372010-12-23 07:20:52 +000030using namespace ento;
Ted Kremenekdf61b582010-02-25 05:44:09 +000031
32namespace {
Argyrios Kyrtzidisec8605f2011-03-01 01:16:21 +000033class MacOSXAPIChecker : public Checker< check::PreStmt<CallExpr> > {
Dylan Noblesmith6f42b622012-02-05 02:12:40 +000034 mutable OwningPtr<BugType> BT_dispatchOnce;
Ted Kremenekdf61b582010-02-25 05:44:09 +000035
36public:
Argyrios Kyrtzidis983326f2011-02-23 01:05:36 +000037 void checkPreStmt(const CallExpr *CE, CheckerContext &C) const;
Jordy Rose57964bd2011-07-15 06:02:19 +000038
39 void CheckDispatchOnce(CheckerContext &C, const CallExpr *CE,
Anna Zaksb805c8f2011-12-01 05:57:37 +000040 StringRef FName) const;
Jordy Rose57964bd2011-07-15 06:02:19 +000041
42 typedef void (MacOSXAPIChecker::*SubChecker)(CheckerContext &,
43 const CallExpr *,
Anna Zaksb805c8f2011-12-01 05:57:37 +000044 StringRef FName) const;
Ted Kremenekdf61b582010-02-25 05:44:09 +000045};
46} //end anonymous namespace
47
Ted Kremenekdf61b582010-02-25 05:44:09 +000048//===----------------------------------------------------------------------===//
49// dispatch_once and dispatch_once_f
50//===----------------------------------------------------------------------===//
51
Jordy Rose57964bd2011-07-15 06:02:19 +000052void MacOSXAPIChecker::CheckDispatchOnce(CheckerContext &C, const CallExpr *CE,
Anna Zaksb805c8f2011-12-01 05:57:37 +000053 StringRef FName) const {
Ted Kremenekdf61b582010-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 Kremenek8bef8232012-01-26 21:29:00 +000059 ProgramStateRef state = C.getState();
Ted Kremenek5eca4822012-01-06 22:09:28 +000060 const MemRegion *R =
61 state->getSVal(CE->getArg(0), C.getLocationContext()).getAsRegion();
Ted Kremenekdf61b582010-02-25 05:44:09 +000062 if (!R || !isa<StackSpaceRegion>(R->getMemorySpace()))
63 return;
64
Ted Kremenekd048c6e2010-12-20 21:19:09 +000065 ExplodedNode *N = C.generateSink(state);
Ted Kremenekdf61b582010-02-25 05:44:09 +000066 if (!N)
67 return;
68
Jordy Rose57964bd2011-07-15 06:02:19 +000069 if (!BT_dispatchOnce)
70 BT_dispatchOnce.reset(new BugType("Improper use of 'dispatch_once'",
71 "Mac OS X API"));
72
Ted Kremenek45b76ba2012-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 Kremenekbe879722012-09-13 18:18:37 +000077 if (CE->getLocStart().isMacroID()) {
78 StringRef TrimmedFName = FName.ltrim("_");
79 if (TrimmedFName != FName)
80 FName = TrimmedFName;
81 }
82
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +000083 SmallString<256> S;
Ted Kremenekdf61b582010-02-25 05:44:09 +000084 llvm::raw_svector_ostream os(S);
Anna Zaksb805c8f2011-12-01 05:57:37 +000085 os << "Call to '" << FName << "' uses";
Ted Kremenekdf61b582010-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
Anna Zakse172e8b2011-08-17 23:00:25 +000095 BugReport *report = new BugReport(*BT_dispatchOnce, os.str(), N);
Ted Kremenekdf61b582010-02-25 05:44:09 +000096 report->addRange(CE->getArg(0)->getSourceRange());
Jordan Rose785950e2012-11-02 01:53:40 +000097 C.emitReport(report);
Ted Kremenekdf61b582010-02-25 05:44:09 +000098}
99
100//===----------------------------------------------------------------------===//
101// Central dispatch function.
102//===----------------------------------------------------------------------===//
103
Argyrios Kyrtzidis983326f2011-02-23 01:05:36 +0000104void MacOSXAPIChecker::checkPreStmt(const CallExpr *CE,
105 CheckerContext &C) const {
Anna Zaksb805c8f2011-12-01 05:57:37 +0000106 StringRef Name = C.getCalleeName(CE);
107 if (Name.empty())
Ted Kremenekdf61b582010-02-25 05:44:09 +0000108 return;
109
Jordy Rose57964bd2011-07-15 06:02:19 +0000110 SubChecker SC =
Anna Zaksb805c8f2011-12-01 05:57:37 +0000111 llvm::StringSwitch<SubChecker>(Name)
Ted Kremenekbe879722012-09-13 18:18:37 +0000112 .Cases("dispatch_once",
113 "_dispatch_once",
114 "dispatch_once_f",
Jordy Rose57964bd2011-07-15 06:02:19 +0000115 &MacOSXAPIChecker::CheckDispatchOnce)
116 .Default(NULL);
Ted Kremenekdf61b582010-02-25 05:44:09 +0000117
Jordy Rose57964bd2011-07-15 06:02:19 +0000118 if (SC)
Anna Zaksb805c8f2011-12-01 05:57:37 +0000119 (this->*SC)(C, CE, Name);
Ted Kremenekdf61b582010-02-25 05:44:09 +0000120}
Argyrios Kyrtzidis983326f2011-02-23 01:05:36 +0000121
122//===----------------------------------------------------------------------===//
123// Registration.
124//===----------------------------------------------------------------------===//
125
126void ento::registerMacOSXAPIChecker(CheckerManager &mgr) {
127 mgr.registerChecker<MacOSXAPIChecker>();
128}