1 //== ObjCContainersChecker.cpp - Path sensitive checker for CFArray *- C++ -*=//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Performs path sensitive checks of Core Foundation static containers like
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.
17 //===----------------------------------------------------------------------===//
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/PathSensitive/ObjCMessage.h"
25 #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
26 #include "clang/AST/ParentMap.h"
28 using namespace clang;
32 class ObjCContainersChecker : public Checker< check::PreStmt<CallExpr>,
33 check::PostStmt<CallExpr> > {
34 mutable OwningPtr<BugType> BT;
35 inline void initBugType() const {
37 BT.reset(new BugType("CFArray API",
38 categories::CoreFoundationObjectiveC));
41 inline SymbolRef getArraySym(const Expr *E, CheckerContext &C) const {
42 SVal ArrayRef = C.getState()->getSVal(E, C.getLocationContext());
43 SymbolRef ArraySym = ArrayRef.getAsSymbol();
47 void addSizeInfo(const Expr *Array, const Expr *Size,
48 CheckerContext &C) const;
51 /// A tag to id this checker.
52 static void *getTag() { static int Tag; return &Tag; }
54 void checkPostStmt(const CallExpr *CE, CheckerContext &C) const;
55 void checkPreStmt(const CallExpr *CE, CheckerContext &C) const;
57 } // end anonymous namespace
59 // ProgramState trait - a map from array symbol to it's state.
60 typedef llvm::ImmutableMap<SymbolRef, DefinedSVal> ArraySizeM;
62 namespace { struct ArraySizeMap {}; }
63 namespace clang { namespace ento {
64 template<> struct ProgramStateTrait<ArraySizeMap>
65 : public ProgramStatePartialTrait<ArraySizeM > {
66 static void *GDMIndex() { return ObjCContainersChecker::getTag(); }
70 void ObjCContainersChecker::addSizeInfo(const Expr *Array, const Expr *Size,
71 CheckerContext &C) const {
72 ProgramStateRef State = C.getState();
73 SVal SizeV = State->getSVal(Size, C.getLocationContext());
74 // Undefined is reported by another checker.
75 if (SizeV.isUnknownOrUndef())
78 // Get the ArrayRef symbol.
79 SVal ArrayRef = State->getSVal(Array, C.getLocationContext());
80 SymbolRef ArraySym = ArrayRef.getAsSymbol();
84 C.addTransition(State->set<ArraySizeMap>(ArraySym, cast<DefinedSVal>(SizeV)));
88 void ObjCContainersChecker::checkPostStmt(const CallExpr *CE,
89 CheckerContext &C) const {
90 StringRef Name = C.getCalleeName(CE);
91 if (Name.empty() || CE->getNumArgs() < 1)
94 // Add array size information to the state.
95 if (Name.equals("CFArrayCreate")) {
96 if (CE->getNumArgs() < 3)
98 // Note, we can visit the Create method in the post-visit because
99 // the CFIndex parameter is passed in by value and will not be invalidated
101 addSizeInfo(CE, CE->getArg(2), C);
105 if (Name.equals("CFArrayGetCount")) {
106 addSizeInfo(CE->getArg(0), CE, C);
111 void ObjCContainersChecker::checkPreStmt(const CallExpr *CE,
112 CheckerContext &C) const {
113 StringRef Name = C.getCalleeName(CE);
114 if (Name.empty() || CE->getNumArgs() < 2)
117 // Check the array access.
118 if (Name.equals("CFArrayGetValueAtIndex")) {
119 ProgramStateRef State = C.getState();
120 // Retrieve the size.
121 // Find out if we saw this array symbol before and have information about it.
122 const Expr *ArrayExpr = CE->getArg(0);
123 SymbolRef ArraySym = getArraySym(ArrayExpr, C);
127 const DefinedSVal *Size = State->get<ArraySizeMap>(ArraySym);
133 const Expr *IdxExpr = CE->getArg(1);
134 SVal IdxVal = State->getSVal(IdxExpr, C.getLocationContext());
135 if (IdxVal.isUnknownOrUndef())
137 DefinedSVal Idx = cast<DefinedSVal>(IdxVal);
139 // Now, check if 'Idx in [0, Size-1]'.
140 const QualType T = IdxExpr->getType();
141 ProgramStateRef StInBound = State->assumeInBound(Idx, *Size, true, T);
142 ProgramStateRef StOutBound = State->assumeInBound(Idx, *Size, false, T);
143 if (StOutBound && !StInBound) {
144 ExplodedNode *N = C.generateSink(StOutBound);
148 BugReport *R = new BugReport(*BT, "Index is out of bounds", N);
149 R->addRange(IdxExpr->getSourceRange());
156 /// Register checker.
157 void ento::registerObjCContainersChecker(CheckerManager &mgr) {
158 mgr.registerChecker<ObjCContainersChecker>();