xref: /freebsd/contrib/llvm-project/clang/lib/StaticAnalyzer/Checkers/ObjCContainersChecker.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
10b57cec5SDimitry Andric //== ObjCContainersChecker.cpp - Path sensitive checker for CFArray *- C++ -*=//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // Performs path sensitive checks of Core Foundation static containers like
100b57cec5SDimitry Andric // CFArray.
110b57cec5SDimitry Andric // 1) Check for buffer overflows:
120b57cec5SDimitry Andric //      In CFArrayGetArrayAtIndex( myArray, index), if the index is outside the
130b57cec5SDimitry Andric //      index space of theArray (0 to N-1 inclusive (where N is the count of
140b57cec5SDimitry Andric //      theArray), the behavior is undefined.
150b57cec5SDimitry Andric //
160b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
170b57cec5SDimitry Andric 
180b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
190b57cec5SDimitry Andric #include "clang/AST/ParentMap.h"
200b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
210b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/Checker.h"
220b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/CheckerManager.h"
230b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
240b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h"
250b57cec5SDimitry Andric 
260b57cec5SDimitry Andric using namespace clang;
270b57cec5SDimitry Andric using namespace ento;
280b57cec5SDimitry Andric 
290b57cec5SDimitry Andric namespace {
300b57cec5SDimitry Andric class ObjCContainersChecker : public Checker< check::PreStmt<CallExpr>,
310b57cec5SDimitry Andric                                              check::PostStmt<CallExpr>,
320b57cec5SDimitry Andric                                              check::PointerEscape> {
33647cbc5dSDimitry Andric   const BugType BT{this, "CFArray API", categories::CoreFoundationObjectiveC};
340b57cec5SDimitry Andric 
getArraySym(const Expr * E,CheckerContext & C) const350b57cec5SDimitry Andric   inline SymbolRef getArraySym(const Expr *E, CheckerContext &C) const {
360b57cec5SDimitry Andric     SVal ArrayRef = C.getSVal(E);
370b57cec5SDimitry Andric     SymbolRef ArraySym = ArrayRef.getAsSymbol();
380b57cec5SDimitry Andric     return ArraySym;
390b57cec5SDimitry Andric   }
400b57cec5SDimitry Andric 
410b57cec5SDimitry Andric   void addSizeInfo(const Expr *Array, const Expr *Size,
420b57cec5SDimitry Andric                    CheckerContext &C) const;
430b57cec5SDimitry Andric 
440b57cec5SDimitry Andric public:
450b57cec5SDimitry Andric   void checkPostStmt(const CallExpr *CE, CheckerContext &C) const;
460b57cec5SDimitry Andric   void checkPreStmt(const CallExpr *CE, CheckerContext &C) const;
470b57cec5SDimitry Andric   ProgramStateRef checkPointerEscape(ProgramStateRef State,
480b57cec5SDimitry Andric                                      const InvalidatedSymbols &Escaped,
490b57cec5SDimitry Andric                                      const CallEvent *Call,
500b57cec5SDimitry Andric                                      PointerEscapeKind Kind) const;
510b57cec5SDimitry Andric 
520b57cec5SDimitry Andric   void printState(raw_ostream &OS, ProgramStateRef State,
535ffd83dbSDimitry Andric                   const char *NL, const char *Sep) const override;
540b57cec5SDimitry Andric };
550b57cec5SDimitry Andric } // end anonymous namespace
560b57cec5SDimitry Andric 
570b57cec5SDimitry Andric // ProgramState trait - a map from array symbol to its state.
REGISTER_MAP_WITH_PROGRAMSTATE(ArraySizeMap,SymbolRef,DefinedSVal)580b57cec5SDimitry Andric REGISTER_MAP_WITH_PROGRAMSTATE(ArraySizeMap, SymbolRef, DefinedSVal)
590b57cec5SDimitry Andric 
600b57cec5SDimitry Andric void ObjCContainersChecker::addSizeInfo(const Expr *Array, const Expr *Size,
610b57cec5SDimitry Andric                                         CheckerContext &C) const {
620b57cec5SDimitry Andric   ProgramStateRef State = C.getState();
630b57cec5SDimitry Andric   SVal SizeV = C.getSVal(Size);
640b57cec5SDimitry Andric   // Undefined is reported by another checker.
650b57cec5SDimitry Andric   if (SizeV.isUnknownOrUndef())
660b57cec5SDimitry Andric     return;
670b57cec5SDimitry Andric 
680b57cec5SDimitry Andric   // Get the ArrayRef symbol.
690b57cec5SDimitry Andric   SVal ArrayRef = C.getSVal(Array);
700b57cec5SDimitry Andric   SymbolRef ArraySym = ArrayRef.getAsSymbol();
710b57cec5SDimitry Andric   if (!ArraySym)
720b57cec5SDimitry Andric     return;
730b57cec5SDimitry Andric 
740b57cec5SDimitry Andric   C.addTransition(
750b57cec5SDimitry Andric       State->set<ArraySizeMap>(ArraySym, SizeV.castAs<DefinedSVal>()));
760b57cec5SDimitry Andric }
770b57cec5SDimitry Andric 
checkPostStmt(const CallExpr * CE,CheckerContext & C) const780b57cec5SDimitry Andric void ObjCContainersChecker::checkPostStmt(const CallExpr *CE,
790b57cec5SDimitry Andric                                           CheckerContext &C) const {
800b57cec5SDimitry Andric   StringRef Name = C.getCalleeName(CE);
810b57cec5SDimitry Andric   if (Name.empty() || CE->getNumArgs() < 1)
820b57cec5SDimitry Andric     return;
830b57cec5SDimitry Andric 
840b57cec5SDimitry Andric   // Add array size information to the state.
85*0fca6ea1SDimitry Andric   if (Name == "CFArrayCreate") {
860b57cec5SDimitry Andric     if (CE->getNumArgs() < 3)
870b57cec5SDimitry Andric       return;
880b57cec5SDimitry Andric     // Note, we can visit the Create method in the post-visit because
890b57cec5SDimitry Andric     // the CFIndex parameter is passed in by value and will not be invalidated
900b57cec5SDimitry Andric     // by the call.
910b57cec5SDimitry Andric     addSizeInfo(CE, CE->getArg(2), C);
920b57cec5SDimitry Andric     return;
930b57cec5SDimitry Andric   }
940b57cec5SDimitry Andric 
95*0fca6ea1SDimitry Andric   if (Name == "CFArrayGetCount") {
960b57cec5SDimitry Andric     addSizeInfo(CE->getArg(0), CE, C);
970b57cec5SDimitry Andric     return;
980b57cec5SDimitry Andric   }
990b57cec5SDimitry Andric }
1000b57cec5SDimitry Andric 
checkPreStmt(const CallExpr * CE,CheckerContext & C) const1010b57cec5SDimitry Andric void ObjCContainersChecker::checkPreStmt(const CallExpr *CE,
1020b57cec5SDimitry Andric                                          CheckerContext &C) const {
1030b57cec5SDimitry Andric   StringRef Name = C.getCalleeName(CE);
1040b57cec5SDimitry Andric   if (Name.empty() || CE->getNumArgs() < 2)
1050b57cec5SDimitry Andric     return;
1060b57cec5SDimitry Andric 
1070b57cec5SDimitry Andric   // Check the array access.
108*0fca6ea1SDimitry Andric   if (Name == "CFArrayGetValueAtIndex") {
1090b57cec5SDimitry Andric     ProgramStateRef State = C.getState();
1100b57cec5SDimitry Andric     // Retrieve the size.
1110b57cec5SDimitry Andric     // Find out if we saw this array symbol before and have information about
1120b57cec5SDimitry Andric     // it.
1130b57cec5SDimitry Andric     const Expr *ArrayExpr = CE->getArg(0);
1140b57cec5SDimitry Andric     SymbolRef ArraySym = getArraySym(ArrayExpr, C);
1150b57cec5SDimitry Andric     if (!ArraySym)
1160b57cec5SDimitry Andric       return;
1170b57cec5SDimitry Andric 
1180b57cec5SDimitry Andric     const DefinedSVal *Size = State->get<ArraySizeMap>(ArraySym);
1190b57cec5SDimitry Andric 
1200b57cec5SDimitry Andric     if (!Size)
1210b57cec5SDimitry Andric       return;
1220b57cec5SDimitry Andric 
1230b57cec5SDimitry Andric     // Get the index.
1240b57cec5SDimitry Andric     const Expr *IdxExpr = CE->getArg(1);
1250b57cec5SDimitry Andric     SVal IdxVal = C.getSVal(IdxExpr);
1260b57cec5SDimitry Andric     if (IdxVal.isUnknownOrUndef())
1270b57cec5SDimitry Andric       return;
1280b57cec5SDimitry Andric     DefinedSVal Idx = IdxVal.castAs<DefinedSVal>();
1290b57cec5SDimitry Andric 
1300b57cec5SDimitry Andric     // Now, check if 'Idx in [0, Size-1]'.
1310b57cec5SDimitry Andric     const QualType T = IdxExpr->getType();
13281ad6265SDimitry Andric     ProgramStateRef StInBound, StOutBound;
13381ad6265SDimitry Andric     std::tie(StInBound, StOutBound) = State->assumeInBoundDual(Idx, *Size, T);
1340b57cec5SDimitry Andric     if (StOutBound && !StInBound) {
1350b57cec5SDimitry Andric       ExplodedNode *N = C.generateErrorNode(StOutBound);
1360b57cec5SDimitry Andric       if (!N)
1370b57cec5SDimitry Andric         return;
138647cbc5dSDimitry Andric 
139a7dea167SDimitry Andric       auto R = std::make_unique<PathSensitiveBugReport>(
140647cbc5dSDimitry Andric           BT, "Index is out of bounds", N);
1410b57cec5SDimitry Andric       R->addRange(IdxExpr->getSourceRange());
142fe6060f1SDimitry Andric       bugreporter::trackExpressionValue(N, IdxExpr, *R,
143fe6060f1SDimitry Andric                                         {bugreporter::TrackingKind::Thorough,
144fe6060f1SDimitry Andric                                          /*EnableNullFPSuppression=*/false});
1450b57cec5SDimitry Andric       C.emitReport(std::move(R));
1460b57cec5SDimitry Andric       return;
1470b57cec5SDimitry Andric     }
1480b57cec5SDimitry Andric   }
1490b57cec5SDimitry Andric }
1500b57cec5SDimitry Andric 
1510b57cec5SDimitry Andric ProgramStateRef
checkPointerEscape(ProgramStateRef State,const InvalidatedSymbols & Escaped,const CallEvent * Call,PointerEscapeKind Kind) const1520b57cec5SDimitry Andric ObjCContainersChecker::checkPointerEscape(ProgramStateRef State,
1530b57cec5SDimitry Andric                                           const InvalidatedSymbols &Escaped,
1540b57cec5SDimitry Andric                                           const CallEvent *Call,
1550b57cec5SDimitry Andric                                           PointerEscapeKind Kind) const {
1560b57cec5SDimitry Andric   for (const auto &Sym : Escaped) {
1570b57cec5SDimitry Andric     // When a symbol for a mutable array escapes, we can't reason precisely
1580b57cec5SDimitry Andric     // about its size any more -- so remove it from the map.
1590b57cec5SDimitry Andric     // Note that we aren't notified here when a CFMutableArrayRef escapes as a
1600b57cec5SDimitry Andric     // CFArrayRef. This is because CFArrayRef is typedef'd as a pointer to a
1610b57cec5SDimitry Andric     // const-qualified type.
1620b57cec5SDimitry Andric     State = State->remove<ArraySizeMap>(Sym);
1630b57cec5SDimitry Andric   }
1640b57cec5SDimitry Andric   return State;
1650b57cec5SDimitry Andric }
1660b57cec5SDimitry Andric 
printState(raw_ostream & OS,ProgramStateRef State,const char * NL,const char * Sep) const1670b57cec5SDimitry Andric void ObjCContainersChecker::printState(raw_ostream &OS, ProgramStateRef State,
1680b57cec5SDimitry Andric                                        const char *NL, const char *Sep) const {
1690b57cec5SDimitry Andric   ArraySizeMapTy Map = State->get<ArraySizeMap>();
1700b57cec5SDimitry Andric   if (Map.isEmpty())
1710b57cec5SDimitry Andric     return;
1720b57cec5SDimitry Andric 
1730b57cec5SDimitry Andric   OS << Sep << "ObjC container sizes :" << NL;
1740b57cec5SDimitry Andric   for (auto I : Map) {
1750b57cec5SDimitry Andric     OS << I.first << " : " << I.second << NL;
1760b57cec5SDimitry Andric   }
1770b57cec5SDimitry Andric }
1780b57cec5SDimitry Andric 
1790b57cec5SDimitry Andric /// Register checker.
registerObjCContainersChecker(CheckerManager & mgr)1800b57cec5SDimitry Andric void ento::registerObjCContainersChecker(CheckerManager &mgr) {
1810b57cec5SDimitry Andric   mgr.registerChecker<ObjCContainersChecker>();
1820b57cec5SDimitry Andric }
1830b57cec5SDimitry Andric 
shouldRegisterObjCContainersChecker(const CheckerManager & mgr)1845ffd83dbSDimitry Andric bool ento::shouldRegisterObjCContainersChecker(const CheckerManager &mgr) {
1850b57cec5SDimitry Andric   return true;
1860b57cec5SDimitry Andric }
187