MacOSXAPIChecker.cpp revision 8bef8238181a30e52dea380789a7e2d760eac532
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/ProgramStateTrait.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
29using namespace clang;
30using namespace ento;
31
32namespace {
33class MacOSXAPIChecker : public Checker< check::PreStmt<CallExpr> > {
34  mutable llvm::OwningPtr<BugType> BT_dispatchOnce;
35
36public:
37  void checkPreStmt(const CallExpr *CE, CheckerContext &C) const;
38
39  void CheckDispatchOnce(CheckerContext &C, const CallExpr *CE,
40                         StringRef FName) const;
41
42  typedef void (MacOSXAPIChecker::*SubChecker)(CheckerContext &,
43                                               const CallExpr *,
44                                               StringRef FName) const;
45};
46} //end anonymous namespace
47
48//===----------------------------------------------------------------------===//
49// dispatch_once and dispatch_once_f
50//===----------------------------------------------------------------------===//
51
52void MacOSXAPIChecker::CheckDispatchOnce(CheckerContext &C, const CallExpr *CE,
53                                         StringRef FName) 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  ProgramStateRef state = C.getState();
60  const MemRegion *R =
61    state->getSVal(CE->getArg(0), C.getLocationContext()).getAsRegion();
62  if (!R || !isa<StackSpaceRegion>(R->getMemorySpace()))
63    return;
64
65  ExplodedNode *N = C.generateSink(state);
66  if (!N)
67    return;
68
69  if (!BT_dispatchOnce)
70    BT_dispatchOnce.reset(new BugType("Improper use of 'dispatch_once'",
71                                      "Mac OS X API"));
72
73  llvm::SmallString<256> S;
74  llvm::raw_svector_ostream os(S);
75  os << "Call to '" << FName << "' uses";
76  if (const VarRegion *VR = dyn_cast<VarRegion>(R))
77    os << " the local variable '" << VR->getDecl()->getName() << '\'';
78  else
79    os << " stack allocated memory";
80  os << " for the predicate value.  Using such transient memory for "
81        "the predicate is potentially dangerous.";
82  if (isa<VarRegion>(R) && isa<StackLocalsSpaceRegion>(R->getMemorySpace()))
83    os << "  Perhaps you intended to declare the variable as 'static'?";
84
85  BugReport *report = new BugReport(*BT_dispatchOnce, os.str(), N);
86  report->addRange(CE->getArg(0)->getSourceRange());
87  C.EmitReport(report);
88}
89
90//===----------------------------------------------------------------------===//
91// Central dispatch function.
92//===----------------------------------------------------------------------===//
93
94void MacOSXAPIChecker::checkPreStmt(const CallExpr *CE,
95                                    CheckerContext &C) const {
96  StringRef Name = C.getCalleeName(CE);
97  if (Name.empty())
98    return;
99
100  SubChecker SC =
101    llvm::StringSwitch<SubChecker>(Name)
102      .Cases("dispatch_once", "dispatch_once_f",
103             &MacOSXAPIChecker::CheckDispatchOnce)
104      .Default(NULL);
105
106  if (SC)
107    (this->*SC)(C, CE, Name);
108}
109
110//===----------------------------------------------------------------------===//
111// Registration.
112//===----------------------------------------------------------------------===//
113
114void ento::registerMacOSXAPIChecker(CheckerManager &mgr) {
115  mgr.registerChecker<MacOSXAPIChecker>();
116}
117