MacOSXAPIChecker.cpp revision e172e8b9e7fc67d7d03589af7e92fe777afcf33a
1179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// MacOSXAPIChecker.h - Checks proper use of various MacOS X APIs --*- C++ -*-//
2179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org//
3179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org//                     The LLVM Compiler Infrastructure
4179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org//
5179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// This file is distributed under the University of Illinois Open Source
6179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// License. See LICENSE.TXT for details.
7179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org//
8179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org//===----------------------------------------------------------------------===//
9179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org//
10179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// This defines MacOSXAPIChecker, which is an assortment of checks on calls
11179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// to various, widely used Mac OS X functions.
12179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org//
13179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// FIXME: What's currently in BasicObjCFoundationChecks.cpp should be migrated
14179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// to here, using the new Checker interface.
15179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org//
16179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org//===----------------------------------------------------------------------===//
17179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
18179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org#include "ClangSACheckers.h"
19179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org#include "clang/StaticAnalyzer/Core/Checker.h"
20179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org#include "clang/StaticAnalyzer/Core/CheckerManager.h"
21179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
22179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
23179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h"
24179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org#include "clang/Basic/TargetInfo.h"
25179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org#include "llvm/ADT/SmallString.h"
26179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org#include "llvm/ADT/StringSwitch.h"
27179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org#include "llvm/Support/raw_ostream.h"
28179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
29179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.orgusing namespace clang;
30179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.orgusing namespace ento;
31179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
32179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.orgnamespace {
33179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.orgclass MacOSXAPIChecker : public Checker< check::PreStmt<CallExpr> > {
34179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  mutable llvm::OwningPtr<BugType> BT_dispatchOnce;
35179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
36179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.orgpublic:
37179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  void checkPreStmt(const CallExpr *CE, CheckerContext &C) const;
38179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
39179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  void CheckDispatchOnce(CheckerContext &C, const CallExpr *CE,
40179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org                         const IdentifierInfo *FI) const;
41179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
42179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  typedef void (MacOSXAPIChecker::*SubChecker)(CheckerContext &,
43179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org                                               const CallExpr *,
44179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org                                               const IdentifierInfo *) const;
4545b9940be332834440bd5299419f396e38085ebehans@chromium.org};
46179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org} //end anonymous namespace
47179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
48//===----------------------------------------------------------------------===//
49// dispatch_once and dispatch_once_f
50//===----------------------------------------------------------------------===//
51
52void 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 ProgramState *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  BugReport *report = new BugReport(*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
93void 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 ProgramState *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
122void ento::registerMacOSXAPIChecker(CheckerManager &mgr) {
123  mgr.registerChecker<MacOSXAPIChecker>();
124}
125