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