Ted Kremenek | df61b58 | 2010-02-25 05:44:09 +0000 | [diff] [blame] | 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 | |
Argyrios Kyrtzidis | 027a6ab | 2011-02-15 07:42:33 +0000 | [diff] [blame] | 18 | #include "ClangSACheckers.h" |
Argyrios Kyrtzidis | ec8605f | 2011-03-01 01:16:21 +0000 | [diff] [blame] | 19 | #include "clang/StaticAnalyzer/Core/Checker.h" |
Argyrios Kyrtzidis | 695fb50 | 2011-02-17 21:39:17 +0000 | [diff] [blame] | 20 | #include "clang/StaticAnalyzer/Core/CheckerManager.h" |
Argyrios Kyrtzidis | 983326f | 2011-02-23 01:05:36 +0000 | [diff] [blame] | 21 | #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" |
Ted Kremenek | 9b66371 | 2011-02-10 01:03:03 +0000 | [diff] [blame] | 22 | #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h" |
Ted Kremenek | 18c66fd | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 23 | #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h" |
Argyrios Kyrtzidis | 983326f | 2011-02-23 01:05:36 +0000 | [diff] [blame] | 24 | #include "clang/Basic/TargetInfo.h" |
Ted Kremenek | df61b58 | 2010-02-25 05:44:09 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/SmallString.h" |
| 26 | #include "llvm/ADT/StringSwitch.h" |
| 27 | #include "llvm/Support/raw_ostream.h" |
| 28 | |
| 29 | using namespace clang; |
Ted Kremenek | 9ef6537 | 2010-12-23 07:20:52 +0000 | [diff] [blame] | 30 | using namespace ento; |
Ted Kremenek | df61b58 | 2010-02-25 05:44:09 +0000 | [diff] [blame] | 31 | |
| 32 | namespace { |
Argyrios Kyrtzidis | ec8605f | 2011-03-01 01:16:21 +0000 | [diff] [blame] | 33 | class MacOSXAPIChecker : public Checker< check::PreStmt<CallExpr> > { |
Jordy Rose | 57964bd | 2011-07-15 06:02:19 +0000 | [diff] [blame] | 34 | mutable llvm::OwningPtr<BugType> BT_dispatchOnce; |
Ted Kremenek | df61b58 | 2010-02-25 05:44:09 +0000 | [diff] [blame] | 35 | |
| 36 | public: |
Argyrios Kyrtzidis | 983326f | 2011-02-23 01:05:36 +0000 | [diff] [blame] | 37 | void checkPreStmt(const CallExpr *CE, CheckerContext &C) const; |
Jordy Rose | 57964bd | 2011-07-15 06:02:19 +0000 | [diff] [blame] | 38 | |
| 39 | void CheckDispatchOnce(CheckerContext &C, const CallExpr *CE, |
Anna Zaks | b805c8f | 2011-12-01 05:57:37 +0000 | [diff] [blame^] | 40 | StringRef FName) const; |
Jordy Rose | 57964bd | 2011-07-15 06:02:19 +0000 | [diff] [blame] | 41 | |
| 42 | typedef void (MacOSXAPIChecker::*SubChecker)(CheckerContext &, |
| 43 | const CallExpr *, |
Anna Zaks | b805c8f | 2011-12-01 05:57:37 +0000 | [diff] [blame^] | 44 | StringRef FName) const; |
Ted Kremenek | df61b58 | 2010-02-25 05:44:09 +0000 | [diff] [blame] | 45 | }; |
| 46 | } //end anonymous namespace |
| 47 | |
Ted Kremenek | df61b58 | 2010-02-25 05:44:09 +0000 | [diff] [blame] | 48 | //===----------------------------------------------------------------------===// |
| 49 | // dispatch_once and dispatch_once_f |
| 50 | //===----------------------------------------------------------------------===// |
| 51 | |
Jordy Rose | 57964bd | 2011-07-15 06:02:19 +0000 | [diff] [blame] | 52 | void MacOSXAPIChecker::CheckDispatchOnce(CheckerContext &C, const CallExpr *CE, |
Anna Zaks | b805c8f | 2011-12-01 05:57:37 +0000 | [diff] [blame^] | 53 | StringRef FName) const { |
Ted Kremenek | df61b58 | 2010-02-25 05:44:09 +0000 | [diff] [blame] | 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. |
Ted Kremenek | 18c66fd | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 59 | const ProgramState *state = C.getState(); |
Ted Kremenek | df61b58 | 2010-02-25 05:44:09 +0000 | [diff] [blame] | 60 | const MemRegion *R = state->getSVal(CE->getArg(0)).getAsRegion(); |
| 61 | if (!R || !isa<StackSpaceRegion>(R->getMemorySpace())) |
| 62 | return; |
| 63 | |
Ted Kremenek | d048c6e | 2010-12-20 21:19:09 +0000 | [diff] [blame] | 64 | ExplodedNode *N = C.generateSink(state); |
Ted Kremenek | df61b58 | 2010-02-25 05:44:09 +0000 | [diff] [blame] | 65 | if (!N) |
| 66 | return; |
| 67 | |
Jordy Rose | 57964bd | 2011-07-15 06:02:19 +0000 | [diff] [blame] | 68 | if (!BT_dispatchOnce) |
| 69 | BT_dispatchOnce.reset(new BugType("Improper use of 'dispatch_once'", |
| 70 | "Mac OS X API")); |
| 71 | |
Ted Kremenek | df61b58 | 2010-02-25 05:44:09 +0000 | [diff] [blame] | 72 | llvm::SmallString<256> S; |
| 73 | llvm::raw_svector_ostream os(S); |
Anna Zaks | b805c8f | 2011-12-01 05:57:37 +0000 | [diff] [blame^] | 74 | os << "Call to '" << FName << "' uses"; |
Ted Kremenek | df61b58 | 2010-02-25 05:44:09 +0000 | [diff] [blame] | 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 | |
Anna Zaks | e172e8b | 2011-08-17 23:00:25 +0000 | [diff] [blame] | 84 | BugReport *report = new BugReport(*BT_dispatchOnce, os.str(), N); |
Ted Kremenek | df61b58 | 2010-02-25 05:44:09 +0000 | [diff] [blame] | 85 | report->addRange(CE->getArg(0)->getSourceRange()); |
| 86 | C.EmitReport(report); |
| 87 | } |
| 88 | |
| 89 | //===----------------------------------------------------------------------===// |
| 90 | // Central dispatch function. |
| 91 | //===----------------------------------------------------------------------===// |
| 92 | |
Argyrios Kyrtzidis | 983326f | 2011-02-23 01:05:36 +0000 | [diff] [blame] | 93 | void MacOSXAPIChecker::checkPreStmt(const CallExpr *CE, |
| 94 | CheckerContext &C) const { |
Anna Zaks | b805c8f | 2011-12-01 05:57:37 +0000 | [diff] [blame^] | 95 | StringRef Name = C.getCalleeName(CE); |
| 96 | if (Name.empty()) |
Ted Kremenek | df61b58 | 2010-02-25 05:44:09 +0000 | [diff] [blame] | 97 | return; |
| 98 | |
Jordy Rose | 57964bd | 2011-07-15 06:02:19 +0000 | [diff] [blame] | 99 | SubChecker SC = |
Anna Zaks | b805c8f | 2011-12-01 05:57:37 +0000 | [diff] [blame^] | 100 | llvm::StringSwitch<SubChecker>(Name) |
Jordy Rose | 57964bd | 2011-07-15 06:02:19 +0000 | [diff] [blame] | 101 | .Cases("dispatch_once", "dispatch_once_f", |
| 102 | &MacOSXAPIChecker::CheckDispatchOnce) |
| 103 | .Default(NULL); |
Ted Kremenek | df61b58 | 2010-02-25 05:44:09 +0000 | [diff] [blame] | 104 | |
Jordy Rose | 57964bd | 2011-07-15 06:02:19 +0000 | [diff] [blame] | 105 | if (SC) |
Anna Zaks | b805c8f | 2011-12-01 05:57:37 +0000 | [diff] [blame^] | 106 | (this->*SC)(C, CE, Name); |
Ted Kremenek | df61b58 | 2010-02-25 05:44:09 +0000 | [diff] [blame] | 107 | } |
Argyrios Kyrtzidis | 983326f | 2011-02-23 01:05:36 +0000 | [diff] [blame] | 108 | |
| 109 | //===----------------------------------------------------------------------===// |
| 110 | // Registration. |
| 111 | //===----------------------------------------------------------------------===// |
| 112 | |
| 113 | void ento::registerMacOSXAPIChecker(CheckerManager &mgr) { |
| 114 | mgr.registerChecker<MacOSXAPIChecker>(); |
| 115 | } |