| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h" |
| 19 | #include "clang/AST/ParentMap.h" |
| 20 | #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h" |
| 21 | #include "clang/StaticAnalyzer/Core/Checker.h" |
| 22 | #include "clang/StaticAnalyzer/Core/CheckerManager.h" |
| 23 | #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" |
| 24 | #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h" |
| 25 | |
| 26 | using namespace clang; |
| 27 | using namespace ento; |
| 28 | |
| 29 | namespace { |
| 30 | class ObjCContainersChecker : public Checker< check::PreStmt<CallExpr>, |
| 31 | check::PostStmt<CallExpr>, |
| 32 | check::PointerEscape> { |
| 33 | mutable std::unique_ptr<BugType> BT; |
| 34 | inline void initBugType() const { |
| 35 | if (!BT) |
| 36 | BT.reset(new BugType(this, "CFArray API", |
| 37 | categories::CoreFoundationObjectiveC)); |
| 38 | } |
| 39 | |
| 40 | inline SymbolRef getArraySym(const Expr *E, CheckerContext &C) const { |
| 41 | SVal ArrayRef = C.getSVal(E); |
| 42 | SymbolRef ArraySym = ArrayRef.getAsSymbol(); |
| 43 | return ArraySym; |
| 44 | } |
| 45 | |
| 46 | void addSizeInfo(const Expr *Array, const Expr *Size, |
| 47 | CheckerContext &C) const; |
| 48 | |
| 49 | public: |
| 50 | |
| 51 | static void *getTag() { static int Tag; return &Tag; } |
| 52 | |
| 53 | void checkPostStmt(const CallExpr *CE, CheckerContext &C) const; |
| 54 | void checkPreStmt(const CallExpr *CE, CheckerContext &C) const; |
| 55 | ProgramStateRef checkPointerEscape(ProgramStateRef State, |
| 56 | const InvalidatedSymbols &Escaped, |
| 57 | const CallEvent *Call, |
| 58 | PointerEscapeKind Kind) const; |
| 59 | |
| 60 | void printState(raw_ostream &OS, ProgramStateRef State, |
| 61 | const char *NL, const char *Sep) const; |
| 62 | }; |
| 63 | } |
| 64 | |
| 65 | |
| 66 | REGISTER_MAP_WITH_PROGRAMSTATE(ArraySizeMap, SymbolRef, DefinedSVal) |
| 67 | |
| 68 | void ObjCContainersChecker::addSizeInfo(const Expr *Array, const Expr *Size, |
| 69 | CheckerContext &C) const { |
| 70 | ProgramStateRef State = C.getState(); |
| 71 | SVal SizeV = C.getSVal(Size); |
| 72 | |
| 73 | if (SizeV.isUnknownOrUndef()) |
| 74 | return; |
| 75 | |
| 76 | |
| 77 | SVal ArrayRef = C.getSVal(Array); |
| 78 | SymbolRef ArraySym = ArrayRef.getAsSymbol(); |
| 79 | if (!ArraySym) |
| 80 | return; |
| 81 | |
| 82 | C.addTransition( |
| 83 | State->set<ArraySizeMap>(ArraySym, SizeV.castAs<DefinedSVal>())); |
| 84 | } |
| 85 | |
| 86 | void ObjCContainersChecker::checkPostStmt(const CallExpr *CE, |
| 87 | CheckerContext &C) const { |
| 88 | StringRef Name = C.getCalleeName(CE); |
| 89 | if (Name.empty() || CE->getNumArgs() < 1) |
| 90 | return; |
| 91 | |
| 92 | |
| 93 | if (Name.equals("CFArrayCreate")) { |
| 94 | if (CE->getNumArgs() < 3) |
| 95 | return; |
| 96 | |
| 97 | |
| 98 | |
| 99 | addSizeInfo(CE, CE->getArg(2), C); |
| 100 | return; |
| 101 | } |
| 102 | |
| 103 | if (Name.equals("CFArrayGetCount")) { |
| 104 | addSizeInfo(CE->getArg(0), CE, C); |
| 105 | return; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | void ObjCContainersChecker::checkPreStmt(const CallExpr *CE, |
| 110 | CheckerContext &C) const { |
| 111 | StringRef Name = C.getCalleeName(CE); |
| 112 | if (Name.empty() || CE->getNumArgs() < 2) |
| 113 | return; |
| 114 | |
| 115 | |
| 116 | if (Name.equals("CFArrayGetValueAtIndex")) { |
| 117 | ProgramStateRef State = C.getState(); |
| 118 | |
| 119 | |
| 120 | |
| 121 | const Expr *ArrayExpr = CE->getArg(0); |
| 122 | SymbolRef ArraySym = getArraySym(ArrayExpr, C); |
| 123 | if (!ArraySym) |
| 124 | return; |
| 125 | |
| 126 | const DefinedSVal *Size = State->get<ArraySizeMap>(ArraySym); |
| 127 | |
| 128 | if (!Size) |
| 129 | return; |
| 130 | |
| 131 | |
| 132 | const Expr *IdxExpr = CE->getArg(1); |
| 133 | SVal IdxVal = C.getSVal(IdxExpr); |
| 134 | if (IdxVal.isUnknownOrUndef()) |
| 135 | return; |
| 136 | DefinedSVal Idx = IdxVal.castAs<DefinedSVal>(); |
| 137 | |
| 138 | |
| 139 | const QualType T = IdxExpr->getType(); |
| 140 | ProgramStateRef StInBound = State->assumeInBound(Idx, *Size, true, T); |
| 141 | ProgramStateRef StOutBound = State->assumeInBound(Idx, *Size, false, T); |
| 142 | if (StOutBound && !StInBound) { |
| 143 | ExplodedNode *N = C.generateErrorNode(StOutBound); |
| 144 | if (!N) |
| 145 | return; |
| 146 | initBugType(); |
| 147 | auto R = llvm::make_unique<BugReport>(*BT, "Index is out of bounds", N); |
| 148 | R->addRange(IdxExpr->getSourceRange()); |
| 149 | bugreporter::trackExpressionValue(N, IdxExpr, *R, |
| 150 | ); |
| 151 | C.emitReport(std::move(R)); |
| 152 | return; |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | ProgramStateRef |
| 158 | ObjCContainersChecker::checkPointerEscape(ProgramStateRef State, |
| 159 | const InvalidatedSymbols &Escaped, |
| 160 | const CallEvent *Call, |
| 161 | PointerEscapeKind Kind) const { |
| 162 | for (const auto &Sym : Escaped) { |
| 163 | |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | State = State->remove<ArraySizeMap>(Sym); |
| 169 | } |
| 170 | return State; |
| 171 | } |
| 172 | |
| 173 | void ObjCContainersChecker::printState(raw_ostream &OS, ProgramStateRef State, |
| 174 | const char *NL, const char *Sep) const { |
| 175 | ArraySizeMapTy Map = State->get<ArraySizeMap>(); |
| 176 | if (Map.isEmpty()) |
| 177 | return; |
| 178 | |
| 179 | OS << Sep << "ObjC container sizes :" << NL; |
| 180 | for (auto I : Map) { |
| 181 | OS << I.first << " : " << I.second << NL; |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | |
| 186 | void ento::registerObjCContainersChecker(CheckerManager &mgr) { |
| 187 | mgr.registerChecker<ObjCContainersChecker>(); |
| 188 | } |
| 189 | |
| 190 | bool ento::shouldRegisterObjCContainersChecker(const LangOptions &LO) { |
| 191 | return true; |
| 192 | } |
| 193 | |