Coverage Report

Created: 2019-07-24 05:18

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