blob: 4f0b7e51da109ecc7743b2fa617418d8f057f2f3 [file] [log] [blame]
Anna Zaks4f870e62012-01-30 06:42:48 +00001//== 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"
Chandler Carruth3a022472012-12-04 09:13:33 +000020#include "clang/AST/ParentMap.h"
21#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
Anna Zaks4f870e62012-01-30 06:42:48 +000022#include "clang/StaticAnalyzer/Core/Checker.h"
23#include "clang/StaticAnalyzer/Core/CheckerManager.h"
24#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
25#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h"
Anna Zaks4f870e62012-01-30 06:42:48 +000026
27using namespace clang;
28using namespace ento;
29
30namespace {
31class ObjCContainersChecker : public Checker< check::PreStmt<CallExpr>,
Devin Coughlin0bee1d72015-06-15 01:00:42 +000032 check::PostStmt<CallExpr>,
33 check::PointerEscape> {
Ahmed Charlesb8984322014-03-07 20:03:18 +000034 mutable std::unique_ptr<BugType> BT;
Anna Zaks4f870e62012-01-30 06:42:48 +000035 inline void initBugType() const {
36 if (!BT)
Alexander Kornienko4aca9b12014-02-11 21:49:21 +000037 BT.reset(new BugType(this, "CFArray API",
Ted Kremenekb45d1982012-04-05 20:43:28 +000038 categories::CoreFoundationObjectiveC));
Anna Zaks4f870e62012-01-30 06:42:48 +000039 }
40
41 inline SymbolRef getArraySym(const Expr *E, CheckerContext &C) const {
42 SVal ArrayRef = C.getState()->getSVal(E, C.getLocationContext());
43 SymbolRef ArraySym = ArrayRef.getAsSymbol();
Anna Zaks4f870e62012-01-30 06:42:48 +000044 return ArraySym;
45 }
46
47 void addSizeInfo(const Expr *Array, const Expr *Size,
48 CheckerContext &C) const;
49
50public:
51 /// A tag to id this checker.
52 static void *getTag() { static int Tag; return &Tag; }
53
54 void checkPostStmt(const CallExpr *CE, CheckerContext &C) const;
55 void checkPreStmt(const CallExpr *CE, CheckerContext &C) const;
Devin Coughlin0bee1d72015-06-15 01:00:42 +000056 ProgramStateRef checkPointerEscape(ProgramStateRef State,
57 const InvalidatedSymbols &Escaped,
58 const CallEvent *Call,
59 PointerEscapeKind Kind) const;
Anna Zaks4f870e62012-01-30 06:42:48 +000060};
61} // end anonymous namespace
62
Jordan Rose0c153cb2012-11-02 01:54:06 +000063// ProgramState trait - a map from array symbol to its state.
64REGISTER_MAP_WITH_PROGRAMSTATE(ArraySizeMap, SymbolRef, DefinedSVal)
Anna Zaks4f870e62012-01-30 06:42:48 +000065
66void ObjCContainersChecker::addSizeInfo(const Expr *Array, const Expr *Size,
67 CheckerContext &C) const {
Ted Kremenekf8c36e02012-01-31 01:19:57 +000068 ProgramStateRef State = C.getState();
Anna Zaks4f870e62012-01-30 06:42:48 +000069 SVal SizeV = State->getSVal(Size, C.getLocationContext());
70 // Undefined is reported by another checker.
71 if (SizeV.isUnknownOrUndef())
72 return;
73
74 // Get the ArrayRef symbol.
75 SVal ArrayRef = State->getSVal(Array, C.getLocationContext());
76 SymbolRef ArraySym = ArrayRef.getAsSymbol();
77 if (!ArraySym)
78 return;
79
David Blaikie2fdacbc2013-02-20 05:52:05 +000080 C.addTransition(
81 State->set<ArraySizeMap>(ArraySym, SizeV.castAs<DefinedSVal>()));
Anna Zaks4f870e62012-01-30 06:42:48 +000082 return;
83}
84
85void ObjCContainersChecker::checkPostStmt(const CallExpr *CE,
86 CheckerContext &C) const {
87 StringRef Name = C.getCalleeName(CE);
Anna Zaks88598242012-02-04 06:40:52 +000088 if (Name.empty() || CE->getNumArgs() < 1)
Anna Zaks4f870e62012-01-30 06:42:48 +000089 return;
90
91 // Add array size information to the state.
92 if (Name.equals("CFArrayCreate")) {
Anna Zaks88598242012-02-04 06:40:52 +000093 if (CE->getNumArgs() < 3)
94 return;
Anna Zaks4f870e62012-01-30 06:42:48 +000095 // Note, we can visit the Create method in the post-visit because
96 // the CFIndex parameter is passed in by value and will not be invalidated
97 // by the call.
98 addSizeInfo(CE, CE->getArg(2), C);
99 return;
100 }
101
102 if (Name.equals("CFArrayGetCount")) {
103 addSizeInfo(CE->getArg(0), CE, C);
104 return;
105 }
106}
107
108void ObjCContainersChecker::checkPreStmt(const CallExpr *CE,
109 CheckerContext &C) const {
110 StringRef Name = C.getCalleeName(CE);
Anna Zaks88598242012-02-04 06:40:52 +0000111 if (Name.empty() || CE->getNumArgs() < 2)
Anna Zaks4f870e62012-01-30 06:42:48 +0000112 return;
113
114 // Check the array access.
115 if (Name.equals("CFArrayGetValueAtIndex")) {
Ted Kremenekf8c36e02012-01-31 01:19:57 +0000116 ProgramStateRef State = C.getState();
Anna Zaks4f870e62012-01-30 06:42:48 +0000117 // Retrieve the size.
Devin Coughlind7112c32015-06-04 00:18:10 +0000118 // Find out if we saw this array symbol before and have information about
119 // it.
Anna Zaks4f870e62012-01-30 06:42:48 +0000120 const Expr *ArrayExpr = CE->getArg(0);
Ted Kremenek00fa5962012-04-05 05:18:05 +0000121 SymbolRef ArraySym = getArraySym(ArrayExpr, C);
122 if (!ArraySym)
123 return;
124
125 const DefinedSVal *Size = State->get<ArraySizeMap>(ArraySym);
126
Anna Zaks88598242012-02-04 06:40:52 +0000127 if (!Size)
Anna Zaks4f870e62012-01-30 06:42:48 +0000128 return;
Anna Zaks4f870e62012-01-30 06:42:48 +0000129
130 // Get the index.
131 const Expr *IdxExpr = CE->getArg(1);
132 SVal IdxVal = State->getSVal(IdxExpr, C.getLocationContext());
Anna Zaks88598242012-02-04 06:40:52 +0000133 if (IdxVal.isUnknownOrUndef())
134 return;
David Blaikie2fdacbc2013-02-20 05:52:05 +0000135 DefinedSVal Idx = IdxVal.castAs<DefinedSVal>();
Anna Zaks4f870e62012-01-30 06:42:48 +0000136
137 // Now, check if 'Idx in [0, Size-1]'.
138 const QualType T = IdxExpr->getType();
Anna Zaks88598242012-02-04 06:40:52 +0000139 ProgramStateRef StInBound = State->assumeInBound(Idx, *Size, true, T);
140 ProgramStateRef StOutBound = State->assumeInBound(Idx, *Size, false, T);
Anna Zaks4f870e62012-01-30 06:42:48 +0000141 if (StOutBound && !StInBound) {
142 ExplodedNode *N = C.generateSink(StOutBound);
143 if (!N)
144 return;
145 initBugType();
146 BugReport *R = new BugReport(*BT, "Index is out of bounds", N);
147 R->addRange(IdxExpr->getSourceRange());
Jordan Rosee10d5a72012-11-02 01:53:40 +0000148 C.emitReport(R);
Anna Zaks4f870e62012-01-30 06:42:48 +0000149 return;
150 }
151 }
152}
153
Devin Coughlin0bee1d72015-06-15 01:00:42 +0000154ProgramStateRef
155ObjCContainersChecker::checkPointerEscape(ProgramStateRef State,
156 const InvalidatedSymbols &Escaped,
157 const CallEvent *Call,
158 PointerEscapeKind Kind) const {
159 for (InvalidatedSymbols::const_iterator I = Escaped.begin(),
160 E = Escaped.end();
161 I != E; ++I) {
162 SymbolRef Sym = *I;
163 // When a symbol for a mutable array escapes, we can't reason precisely
164 // about its size any more -- so remove it from the map.
165 // Note that we aren't notified here when a CFMutableArrayRef escapes as a
166 // CFArrayRef. This is because CFArrayRef is typedef'd as a pointer to a
167 // const-qualified type.
168 State = State->remove<ArraySizeMap>(Sym);
169 }
170 return State;
171}
Anna Zaks4f870e62012-01-30 06:42:48 +0000172/// Register checker.
173void ento::registerObjCContainersChecker(CheckerManager &mgr) {
174 mgr.registerChecker<ObjCContainersChecker>();
175}