• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // 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 
18 #include "ClangSACheckers.h"
19 #include "clang/StaticAnalyzer/Core/Checker.h"
20 #include "clang/StaticAnalyzer/Core/CheckerManager.h"
21 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
22 #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
23 #include "clang/StaticAnalyzer/Core/PathSensitive/GRStateTrait.h"
24 #include "clang/Basic/TargetInfo.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 llvm::OwningPtr<BugType> BT_dispatchOnce;
35 
36 public:
37   void checkPreStmt(const CallExpr *CE, CheckerContext &C) const;
38 
39   void CheckDispatchOnce(CheckerContext &C, const CallExpr *CE,
40                          const IdentifierInfo *FI) const;
41 
42   typedef void (MacOSXAPIChecker::*SubChecker)(CheckerContext &,
43                                                const CallExpr *,
44                                                const IdentifierInfo *) const;
45 };
46 } //end anonymous namespace
47 
48 //===----------------------------------------------------------------------===//
49 // dispatch_once and dispatch_once_f
50 //===----------------------------------------------------------------------===//
51 
CheckDispatchOnce(CheckerContext & C,const CallExpr * CE,const IdentifierInfo * FI) const52 void MacOSXAPIChecker::CheckDispatchOnce(CheckerContext &C, const CallExpr *CE,
53                                          const IdentifierInfo *FI) const {
54   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.
59   const GRState *state = C.getState();
60   const MemRegion *R = state->getSVal(CE->getArg(0)).getAsRegion();
61   if (!R || !isa<StackSpaceRegion>(R->getMemorySpace()))
62     return;
63 
64   ExplodedNode *N = C.generateSink(state);
65   if (!N)
66     return;
67 
68   if (!BT_dispatchOnce)
69     BT_dispatchOnce.reset(new BugType("Improper use of 'dispatch_once'",
70                                       "Mac OS X API"));
71 
72   llvm::SmallString<256> S;
73   llvm::raw_svector_ostream os(S);
74   os << "Call to '" << FI->getName() << "' uses";
75   if (const VarRegion *VR = dyn_cast<VarRegion>(R))
76     os << " the local variable '" << VR->getDecl()->getName() << '\'';
77   else
78     os << " stack allocated memory";
79   os << " for the predicate value.  Using such transient memory for "
80         "the predicate is potentially dangerous.";
81   if (isa<VarRegion>(R) && isa<StackLocalsSpaceRegion>(R->getMemorySpace()))
82     os << "  Perhaps you intended to declare the variable as 'static'?";
83 
84   RangedBugReport *report = new RangedBugReport(*BT_dispatchOnce, os.str(), N);
85   report->addRange(CE->getArg(0)->getSourceRange());
86   C.EmitReport(report);
87 }
88 
89 //===----------------------------------------------------------------------===//
90 // Central dispatch function.
91 //===----------------------------------------------------------------------===//
92 
checkPreStmt(const CallExpr * CE,CheckerContext & C) const93 void MacOSXAPIChecker::checkPreStmt(const CallExpr *CE,
94                                     CheckerContext &C) const {
95   // FIXME: This sort of logic is common to several checkers, including
96   // UnixAPIChecker, PthreadLockChecker, and CStringChecker.  Should refactor.
97   const GRState *state = C.getState();
98   const Expr *Callee = CE->getCallee();
99   const FunctionDecl *Fn = state->getSVal(Callee).getAsFunctionDecl();
100 
101   if (!Fn)
102     return;
103 
104   const IdentifierInfo *FI = Fn->getIdentifier();
105   if (!FI)
106     return;
107 
108   SubChecker SC =
109     llvm::StringSwitch<SubChecker>(FI->getName())
110       .Cases("dispatch_once", "dispatch_once_f",
111              &MacOSXAPIChecker::CheckDispatchOnce)
112       .Default(NULL);
113 
114   if (SC)
115     (this->*SC)(C, CE, FI);
116 }
117 
118 //===----------------------------------------------------------------------===//
119 // Registration.
120 //===----------------------------------------------------------------------===//
121 
registerMacOSXAPIChecker(CheckerManager & mgr)122 void ento::registerMacOSXAPIChecker(CheckerManager &mgr) {
123   mgr.registerChecker<MacOSXAPIChecker>();
124 }
125