| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h" |
| 18 | #include "clang/Basic/TargetInfo.h" |
| 19 | #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h" |
| 20 | #include "clang/StaticAnalyzer/Core/Checker.h" |
| 21 | #include "clang/StaticAnalyzer/Core/CheckerManager.h" |
| 22 | #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" |
| 23 | #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h" |
| 24 | #include "llvm/ADT/SmallString.h" |
| 25 | #include "llvm/ADT/StringSwitch.h" |
| 26 | #include "llvm/Support/raw_ostream.h" |
| 27 | |
| 28 | using namespace clang; |
| 29 | using namespace ento; |
| 30 | |
| 31 | namespace { |
| 32 | class MacOSXAPIChecker : public Checker< check::PreStmt<CallExpr> > { |
| 33 | mutable std::unique_ptr<BugType> BT_dispatchOnce; |
| 34 | |
| 35 | static const ObjCIvarRegion *getParentIvarRegion(const MemRegion *R); |
| 36 | |
| 37 | public: |
| 38 | void checkPreStmt(const CallExpr *CE, CheckerContext &C) const; |
| 39 | |
| 40 | void CheckDispatchOnce(CheckerContext &C, const CallExpr *CE, |
| 41 | StringRef FName) const; |
| 42 | |
| 43 | typedef void (MacOSXAPIChecker::*SubChecker)(CheckerContext &, |
| 44 | const CallExpr *, |
| 45 | StringRef FName) const; |
| 46 | }; |
| 47 | } |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | const ObjCIvarRegion * |
| 54 | MacOSXAPIChecker::getParentIvarRegion(const MemRegion *R) { |
| 55 | const SubRegion *SR = dyn_cast<SubRegion>(R); |
| 56 | while (SR) { |
| 57 | if (const ObjCIvarRegion *IR = dyn_cast<ObjCIvarRegion>(SR)) |
| 58 | return IR; |
| 59 | SR = dyn_cast<SubRegion>(SR->getSuperRegion()); |
| 60 | } |
| 61 | return nullptr; |
| 62 | } |
| 63 | |
| 64 | void MacOSXAPIChecker::CheckDispatchOnce(CheckerContext &C, const CallExpr *CE, |
| 65 | StringRef FName) const { |
| 66 | if (CE->getNumArgs() < 1) |
| 67 | return; |
| 68 | |
| 69 | |
| 70 | |
| 71 | const MemRegion *R = C.getSVal(CE->getArg(0)).getAsRegion(); |
| 72 | if (!R) |
| 73 | return; |
| 74 | |
| 75 | |
| 76 | const MemRegion *RB = R->getBaseRegion(); |
| 77 | const MemSpaceRegion *RS = RB->getMemorySpace(); |
| 78 | if (isa<GlobalsSpaceRegion>(RS)) |
| 79 | return; |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | if (CE->getBeginLoc().isMacroID()) { |
| 86 | StringRef TrimmedFName = FName.ltrim('_'); |
| 87 | if (TrimmedFName != FName) |
| 88 | FName = TrimmedFName; |
| 89 | } |
| 90 | |
| 91 | SmallString<256> S; |
| 92 | llvm::raw_svector_ostream os(S); |
| 93 | bool SuggestStatic = false; |
| 94 | os << "Call to '" << FName << "' uses"; |
| 95 | if (const VarRegion *VR = dyn_cast<VarRegion>(RB)) { |
| 96 | const VarDecl *VD = VR->getDecl(); |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | if (VD->isStaticLocal()) |
| 102 | return; |
| 103 | |
| 104 | |
| 105 | if (VR != R) |
| 106 | os << " memory within"; |
| 107 | if (VD->hasAttr<BlocksAttr>()) |
| 108 | os << " the block variable '"; |
| 109 | else |
| 110 | os << " the local variable '"; |
| 111 | os << VR->getDecl()->getName() << '\''; |
| 112 | SuggestStatic = true; |
| 113 | } else if (const ObjCIvarRegion *IVR = getParentIvarRegion(R)) { |
| 114 | if (IVR != R) |
| 115 | os << " memory within"; |
| 116 | os << " the instance variable '" << IVR->getDecl()->getName() << '\''; |
| 117 | } else if (isa<HeapSpaceRegion>(RS)) { |
| 118 | os << " heap-allocated memory"; |
| 119 | } else if (isa<UnknownSpaceRegion>(RS)) { |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | return; |
| 126 | } else { |
| 127 | os << " stack allocated memory"; |
| 128 | } |
| 129 | os << " for the predicate value. Using such transient memory for " |
| 130 | "the predicate is potentially dangerous."; |
| 131 | if (SuggestStatic) |
| 132 | os << " Perhaps you intended to declare the variable as 'static'?"; |
| 133 | |
| 134 | ExplodedNode *N = C.generateErrorNode(); |
| 135 | if (!N) |
| 136 | return; |
| 137 | |
| 138 | if (!BT_dispatchOnce) |
| 139 | BT_dispatchOnce.reset(new BugType(this, "Improper use of 'dispatch_once'", |
| 140 | "API Misuse (Apple)")); |
| 141 | |
| 142 | auto report = llvm::make_unique<BugReport>(*BT_dispatchOnce, os.str(), N); |
| 143 | report->addRange(CE->getArg(0)->getSourceRange()); |
| 144 | C.emitReport(std::move(report)); |
| 145 | } |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | void MacOSXAPIChecker::checkPreStmt(const CallExpr *CE, |
| 152 | CheckerContext &C) const { |
| 153 | StringRef Name = C.getCalleeName(CE); |
| 154 | if (Name.empty()) |
| 155 | return; |
| 156 | |
| 157 | SubChecker SC = |
| 158 | llvm::StringSwitch<SubChecker>(Name) |
| 159 | .Cases("dispatch_once", |
| 160 | "_dispatch_once", |
| 161 | "dispatch_once_f", |
| 162 | &MacOSXAPIChecker::CheckDispatchOnce) |
| 163 | .Default(nullptr); |
| 164 | |
| 165 | if (SC) |
| 166 | (this->*SC)(C, CE, Name); |
| 167 | } |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | void ento::registerMacOSXAPIChecker(CheckerManager &mgr) { |
| 174 | mgr.registerChecker<MacOSXAPIChecker>(); |
| 175 | } |
| 176 | |
| 177 | bool ento::shouldRegisterMacOSXAPIChecker(const LangOptions &LO) { |
| 178 | return true; |
| 179 | } |
| 180 | |