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