ObjCContainersChecker.cpp revision 166d502d5367ceacd1313a33cac43b1048b8524d
1//== ObjCContainersChecker.cpp - Path sensitive checker for CFArray *- 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// Performs path sensitive checks of Core Foundation static containers like 11// CFArray. 12// 1) Check for buffer overflows: 13// In CFArrayGetArrayAtIndex( myArray, index), if the index is outside the 14// index space of theArray (0 to N-1 inclusive (where N is the count of 15// theArray), the behavior is undefined. 16// 17//===----------------------------------------------------------------------===// 18 19#include "ClangSACheckers.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 "clang/StaticAnalyzer/Core/BugReporter/BugType.h" 25#include "clang/AST/ParentMap.h" 26 27using namespace clang; 28using namespace ento; 29 30namespace { 31class ObjCContainersChecker : public Checker< check::PreStmt<CallExpr>, 32 check::PostStmt<CallExpr> > { 33 mutable OwningPtr<BugType> BT; 34 inline void initBugType() const { 35 if (!BT) 36 BT.reset(new BugType("CFArray API", 37 categories::CoreFoundationObjectiveC)); 38 } 39 40 inline SymbolRef getArraySym(const Expr *E, CheckerContext &C) const { 41 SVal ArrayRef = C.getState()->getSVal(E, C.getLocationContext()); 42 SymbolRef ArraySym = ArrayRef.getAsSymbol(); 43 return ArraySym; 44 } 45 46 void addSizeInfo(const Expr *Array, const Expr *Size, 47 CheckerContext &C) const; 48 49public: 50 /// A tag to id this checker. 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}; 56} // end anonymous namespace 57 58// ProgramState trait - a map from array symbol to its state. 59REGISTER_MAP_WITH_PROGRAMSTATE(ArraySizeMap, SymbolRef, DefinedSVal) 60 61void ObjCContainersChecker::addSizeInfo(const Expr *Array, const Expr *Size, 62 CheckerContext &C) const { 63 ProgramStateRef State = C.getState(); 64 SVal SizeV = State->getSVal(Size, C.getLocationContext()); 65 // Undefined is reported by another checker. 66 if (SizeV.isUnknownOrUndef()) 67 return; 68 69 // Get the ArrayRef symbol. 70 SVal ArrayRef = State->getSVal(Array, C.getLocationContext()); 71 SymbolRef ArraySym = ArrayRef.getAsSymbol(); 72 if (!ArraySym) 73 return; 74 75 C.addTransition(State->set<ArraySizeMap>(ArraySym, cast<DefinedSVal>(SizeV))); 76 return; 77} 78 79void ObjCContainersChecker::checkPostStmt(const CallExpr *CE, 80 CheckerContext &C) const { 81 StringRef Name = C.getCalleeName(CE); 82 if (Name.empty() || CE->getNumArgs() < 1) 83 return; 84 85 // Add array size information to the state. 86 if (Name.equals("CFArrayCreate")) { 87 if (CE->getNumArgs() < 3) 88 return; 89 // Note, we can visit the Create method in the post-visit because 90 // the CFIndex parameter is passed in by value and will not be invalidated 91 // by the call. 92 addSizeInfo(CE, CE->getArg(2), C); 93 return; 94 } 95 96 if (Name.equals("CFArrayGetCount")) { 97 addSizeInfo(CE->getArg(0), CE, C); 98 return; 99 } 100} 101 102void ObjCContainersChecker::checkPreStmt(const CallExpr *CE, 103 CheckerContext &C) const { 104 StringRef Name = C.getCalleeName(CE); 105 if (Name.empty() || CE->getNumArgs() < 2) 106 return; 107 108 // Check the array access. 109 if (Name.equals("CFArrayGetValueAtIndex")) { 110 ProgramStateRef State = C.getState(); 111 // Retrieve the size. 112 // Find out if we saw this array symbol before and have information about it. 113 const Expr *ArrayExpr = CE->getArg(0); 114 SymbolRef ArraySym = getArraySym(ArrayExpr, C); 115 if (!ArraySym) 116 return; 117 118 const DefinedSVal *Size = State->get<ArraySizeMap>(ArraySym); 119 120 if (!Size) 121 return; 122 123 // Get the index. 124 const Expr *IdxExpr = CE->getArg(1); 125 SVal IdxVal = State->getSVal(IdxExpr, C.getLocationContext()); 126 if (IdxVal.isUnknownOrUndef()) 127 return; 128 DefinedSVal Idx = cast<DefinedSVal>(IdxVal); 129 130 // Now, check if 'Idx in [0, Size-1]'. 131 const QualType T = IdxExpr->getType(); 132 ProgramStateRef StInBound = State->assumeInBound(Idx, *Size, true, T); 133 ProgramStateRef StOutBound = State->assumeInBound(Idx, *Size, false, T); 134 if (StOutBound && !StInBound) { 135 ExplodedNode *N = C.generateSink(StOutBound); 136 if (!N) 137 return; 138 initBugType(); 139 BugReport *R = new BugReport(*BT, "Index is out of bounds", N); 140 R->addRange(IdxExpr->getSourceRange()); 141 C.emitReport(R); 142 return; 143 } 144 } 145} 146 147/// Register checker. 148void ento::registerObjCContainersChecker(CheckerManager &mgr) { 149 mgr.registerChecker<ObjCContainersChecker>(); 150} 151