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" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame^] | 19 | #include "clang/Basic/TargetInfo.h" |
| 20 | #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h" |
Argyrios Kyrtzidis | ec8605f | 2011-03-01 01:16:21 +0000 | [diff] [blame] | 21 | #include "clang/StaticAnalyzer/Core/Checker.h" |
Argyrios Kyrtzidis | 695fb50 | 2011-02-17 21:39:17 +0000 | [diff] [blame] | 22 | #include "clang/StaticAnalyzer/Core/CheckerManager.h" |
Argyrios Kyrtzidis | 983326f | 2011-02-23 01:05:36 +0000 | [diff] [blame] | 23 | #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" |
Ted Kremenek | 18c66fd | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 24 | #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.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> > { |
Dylan Noblesmith | 6f42b62 | 2012-02-05 02:12:40 +0000 | [diff] [blame] | 34 | mutable 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 | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 59 | ProgramStateRef state = C.getState(); |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 60 | const MemRegion *R = |
| 61 | state->getSVal(CE->getArg(0), C.getLocationContext()).getAsRegion(); |
Ted Kremenek | df61b58 | 2010-02-25 05:44:09 +0000 | [diff] [blame] | 62 | if (!R || !isa<StackSpaceRegion>(R->getMemorySpace())) |
| 63 | return; |
| 64 | |
Ted Kremenek | d048c6e | 2010-12-20 21:19:09 +0000 | [diff] [blame] | 65 | ExplodedNode *N = C.generateSink(state); |
Ted Kremenek | df61b58 | 2010-02-25 05:44:09 +0000 | [diff] [blame] | 66 | if (!N) |
| 67 | return; |
| 68 | |
Jordy Rose | 57964bd | 2011-07-15 06:02:19 +0000 | [diff] [blame] | 69 | if (!BT_dispatchOnce) |
| 70 | BT_dispatchOnce.reset(new BugType("Improper use of 'dispatch_once'", |
| 71 | "Mac OS X API")); |
| 72 | |
Ted Kremenek | 45b76ba | 2012-09-13 19:48:51 +0000 | [diff] [blame] | 73 | // 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 Kremenek | be87972 | 2012-09-13 18:18:37 +0000 | [diff] [blame] | 77 | if (CE->getLocStart().isMacroID()) { |
| 78 | StringRef TrimmedFName = FName.ltrim("_"); |
| 79 | if (TrimmedFName != FName) |
| 80 | FName = TrimmedFName; |
| 81 | } |
| 82 | |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 83 | SmallString<256> S; |
Ted Kremenek | df61b58 | 2010-02-25 05:44:09 +0000 | [diff] [blame] | 84 | llvm::raw_svector_ostream os(S); |
Anna Zaks | b805c8f | 2011-12-01 05:57:37 +0000 | [diff] [blame] | 85 | os << "Call to '" << FName << "' uses"; |
Ted Kremenek | df61b58 | 2010-02-25 05:44:09 +0000 | [diff] [blame] | 86 | 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 Zaks | e172e8b | 2011-08-17 23:00:25 +0000 | [diff] [blame] | 95 | BugReport *report = new BugReport(*BT_dispatchOnce, os.str(), N); |
Ted Kremenek | df61b58 | 2010-02-25 05:44:09 +0000 | [diff] [blame] | 96 | report->addRange(CE->getArg(0)->getSourceRange()); |
Jordan Rose | 785950e | 2012-11-02 01:53:40 +0000 | [diff] [blame] | 97 | C.emitReport(report); |
Ted Kremenek | df61b58 | 2010-02-25 05:44:09 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | //===----------------------------------------------------------------------===// |
| 101 | // Central dispatch function. |
| 102 | //===----------------------------------------------------------------------===// |
| 103 | |
Argyrios Kyrtzidis | 983326f | 2011-02-23 01:05:36 +0000 | [diff] [blame] | 104 | void MacOSXAPIChecker::checkPreStmt(const CallExpr *CE, |
| 105 | CheckerContext &C) const { |
Anna Zaks | b805c8f | 2011-12-01 05:57:37 +0000 | [diff] [blame] | 106 | StringRef Name = C.getCalleeName(CE); |
| 107 | if (Name.empty()) |
Ted Kremenek | df61b58 | 2010-02-25 05:44:09 +0000 | [diff] [blame] | 108 | return; |
| 109 | |
Jordy Rose | 57964bd | 2011-07-15 06:02:19 +0000 | [diff] [blame] | 110 | SubChecker SC = |
Anna Zaks | b805c8f | 2011-12-01 05:57:37 +0000 | [diff] [blame] | 111 | llvm::StringSwitch<SubChecker>(Name) |
Ted Kremenek | be87972 | 2012-09-13 18:18:37 +0000 | [diff] [blame] | 112 | .Cases("dispatch_once", |
| 113 | "_dispatch_once", |
| 114 | "dispatch_once_f", |
Jordy Rose | 57964bd | 2011-07-15 06:02:19 +0000 | [diff] [blame] | 115 | &MacOSXAPIChecker::CheckDispatchOnce) |
| 116 | .Default(NULL); |
Ted Kremenek | df61b58 | 2010-02-25 05:44:09 +0000 | [diff] [blame] | 117 | |
Jordy Rose | 57964bd | 2011-07-15 06:02:19 +0000 | [diff] [blame] | 118 | if (SC) |
Anna Zaks | b805c8f | 2011-12-01 05:57:37 +0000 | [diff] [blame] | 119 | (this->*SC)(C, CE, Name); |
Ted Kremenek | df61b58 | 2010-02-25 05:44:09 +0000 | [diff] [blame] | 120 | } |
Argyrios Kyrtzidis | 983326f | 2011-02-23 01:05:36 +0000 | [diff] [blame] | 121 | |
| 122 | //===----------------------------------------------------------------------===// |
| 123 | // Registration. |
| 124 | //===----------------------------------------------------------------------===// |
| 125 | |
| 126 | void ento::registerMacOSXAPIChecker(CheckerManager &mgr) { |
| 127 | mgr.registerChecker<MacOSXAPIChecker>(); |
| 128 | } |