Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/clang/lib/StaticAnalyzer/Checkers/VLASizeChecker.cpp
Line
Count
Source (jump to first uncovered line)
1
//=== VLASizeChecker.cpp - Undefined dereference checker --------*- 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
// This defines VLASizeChecker, a builtin check in ExprEngine that
10
// performs checks for declaration of VLA of undefined or zero size.
11
// In addition, VLASizeChecker is responsible for defining the extent
12
// of the MemRegion that represents a VLA.
13
//
14
//===----------------------------------------------------------------------===//
15
16
#include "Taint.h"
17
#include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
18
#include "clang/AST/CharUnits.h"
19
#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
20
#include "clang/StaticAnalyzer/Core/Checker.h"
21
#include "clang/StaticAnalyzer/Core/CheckerManager.h"
22
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
23
#include "llvm/ADT/STLExtras.h"
24
#include "llvm/ADT/SmallString.h"
25
#include "llvm/Support/raw_ostream.h"
26
27
using namespace clang;
28
using namespace ento;
29
using namespace taint;
30
31
namespace {
32
class VLASizeChecker : public Checker< check::PreStmt<DeclStmt> > {
33
  mutable std::unique_ptr<BugType> BT;
34
  enum VLASize_Kind { VLA_Garbage, VLA_Zero, VLA_Tainted, VLA_Negative };
35
36
  void reportBug(VLASize_Kind Kind, const Expr *SizeE, ProgramStateRef State,
37
                 CheckerContext &C,
38
                 std::unique_ptr<BugReporterVisitor> Visitor = nullptr) const;
39
40
public:
41
  void checkPreStmt(const DeclStmt *DS, CheckerContext &C) const;
42
};
43
} // end anonymous namespace
44
45
void VLASizeChecker::reportBug(
46
    VLASize_Kind Kind, const Expr *SizeE, ProgramStateRef State,
47
10
    CheckerContext &C, std::unique_ptr<BugReporterVisitor> Visitor) const {
48
10
  // Generate an error node.
49
10
  ExplodedNode *N = C.generateErrorNode(State);
50
10
  if (!N)
51
0
    return;
52
10
53
10
  if (!BT)
54
4
    BT.reset(new BuiltinBug(
55
4
        this, "Dangerous variable-length array (VLA) declaration"));
56
10
57
10
  SmallString<256> buf;
58
10
  llvm::raw_svector_ostream os(buf);
59
10
  os << "Declared variable-length array (VLA) ";
60
10
  switch (Kind) {
61
10
  case VLA_Garbage:
62
1
    os << "uses a garbage value as its size";
63
1
    break;
64
10
  case VLA_Zero:
65
1
    os << "has zero size";
66
1
    break;
67
10
  case VLA_Tainted:
68
3
    os << "has tainted size";
69
3
    break;
70
10
  case VLA_Negative:
71
5
    os << "has negative size";
72
5
    break;
73
10
  }
74
10
75
10
  auto report = llvm::make_unique<BugReport>(*BT, os.str(), N);
76
10
  report->addVisitor(std::move(Visitor));
77
10
  report->addRange(SizeE->getSourceRange());
78
10
  bugreporter::trackExpressionValue(N, SizeE, *report);
79
10
  C.emitReport(std::move(report));
80
10
}
81
82
25.8k
void VLASizeChecker::checkPreStmt(const DeclStmt *DS, CheckerContext &C) const {
83
25.8k
  if (!DS->isSingleDecl())
84
0
    return;
85
25.8k
86
25.8k
  const VarDecl *VD = dyn_cast<VarDecl>(DS->getSingleDecl());
87
25.8k
  if (!VD)
88
0
    return;
89
25.8k
90
25.8k
  ASTContext &Ctx = C.getASTContext();
91
25.8k
  const VariableArrayType *VLA = Ctx.getAsVariableArrayType(VD->getType());
92
25.8k
  if (!VLA)
93
25.8k
    return;
94
43
95
43
  // FIXME: Handle multi-dimensional VLAs.
96
43
  const Expr *SE = VLA->getSizeExpr();
97
43
  ProgramStateRef state = C.getState();
98
43
  SVal sizeV = C.getSVal(SE);
99
43
100
43
  if (sizeV.isUndef()) {
101
1
    reportBug(VLA_Garbage, SE, state, C);
102
1
    return;
103
1
  }
104
42
105
42
  // See if the size value is known. It can't be undefined because we would have
106
42
  // warned about that already.
107
42
  if (sizeV.isUnknown())
108
3
    return;
109
39
110
39
  // Check if the size is tainted.
111
39
  if (isTainted(state, sizeV)) {
112
3
    reportBug(VLA_Tainted, SE, nullptr, C,
113
3
              llvm::make_unique<TaintBugVisitor>(sizeV));
114
3
    return;
115
3
  }
116
36
117
36
  // Check if the size is zero.
118
36
  DefinedSVal sizeD = sizeV.castAs<DefinedSVal>();
119
36
120
36
  ProgramStateRef stateNotZero, stateZero;
121
36
  std::tie(stateNotZero, stateZero) = state->assume(sizeD);
122
36
123
36
  if (stateZero && 
!stateNotZero7
) {
124
1
    reportBug(VLA_Zero, SE, stateZero, C);
125
1
    return;
126
1
  }
127
35
128
35
  // From this point on, assume that the size is not zero.
129
35
  state = stateNotZero;
130
35
131
35
  // VLASizeChecker is responsible for defining the extent of the array being
132
35
  // declared. We do this by multiplying the array length by the element size,
133
35
  // then matching that with the array region's extent symbol.
134
35
135
35
  // Check if the size is negative.
136
35
  SValBuilder &svalBuilder = C.getSValBuilder();
137
35
138
35
  QualType Ty = SE->getType();
139
35
  DefinedOrUnknownSVal Zero = svalBuilder.makeZeroVal(Ty);
140
35
141
35
  SVal LessThanZeroVal = svalBuilder.evalBinOp(state, BO_LT, sizeD, Zero, Ty);
142
35
  if (Optional<DefinedSVal> LessThanZeroDVal =
143
35
        LessThanZeroVal.getAs<DefinedSVal>()) {
144
35
    ConstraintManager &CM = C.getConstraintManager();
145
35
    ProgramStateRef StatePos, StateNeg;
146
35
147
35
    std::tie(StateNeg, StatePos) = CM.assumeDual(state, *LessThanZeroDVal);
148
35
    if (StateNeg && 
!StatePos11
) {
149
5
      reportBug(VLA_Negative, SE, state, C);
150
5
      return;
151
5
    }
152
30
    state = StatePos;
153
30
  }
154
35
155
35
  // Convert the array length to size_t.
156
35
  QualType SizeTy = Ctx.getSizeType();
157
30
  NonLoc ArrayLength =
158
30
      svalBuilder.evalCast(sizeD, SizeTy, SE->getType()).castAs<NonLoc>();
159
30
160
30
  // Get the element size.
161
30
  CharUnits EleSize = Ctx.getTypeSizeInChars(VLA->getElementType());
162
30
  SVal EleSizeVal = svalBuilder.makeIntVal(EleSize.getQuantity(), SizeTy);
163
30
164
30
  // Multiply the array length by the element size.
165
30
  SVal ArraySizeVal = svalBuilder.evalBinOpNN(
166
30
      state, BO_Mul, ArrayLength, EleSizeVal.castAs<NonLoc>(), SizeTy);
167
30
168
30
  // Finally, assume that the array's extent matches the given size.
169
30
  const LocationContext *LC = C.getLocationContext();
170
30
  DefinedOrUnknownSVal Extent =
171
30
    state->getRegion(VD, LC)->getExtent(svalBuilder);
172
30
  DefinedOrUnknownSVal ArraySize = ArraySizeVal.castAs<DefinedOrUnknownSVal>();
173
30
  DefinedOrUnknownSVal sizeIsKnown =
174
30
    svalBuilder.evalEQ(state, Extent, ArraySize);
175
30
  state = state->assume(sizeIsKnown, true);
176
30
177
30
  // Assume should not fail at this point.
178
30
  assert(state);
179
30
180
30
  // Remember our assumptions!
181
30
  C.addTransition(state);
182
30
}
183
184
675
void ento::registerVLASizeChecker(CheckerManager &mgr) {
185
675
  mgr.registerChecker<VLASizeChecker>();
186
675
}
187
188
676
bool ento::shouldRegisterVLASizeChecker(const LangOptions &LO) {
189
676
  return true;
190
676
}