Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/clang/lib/StaticAnalyzer/Checkers/CastSizeChecker.cpp
Line
Count
Source (jump to first uncovered line)
1
//=== CastSizeChecker.cpp ---------------------------------------*- 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
// CastSizeChecker checks when casting a malloc'ed symbolic region to type T,
10
// whether the size of the symbolic region is a multiple of the size of T.
11
//
12
//===----------------------------------------------------------------------===//
13
#include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
14
#include "clang/AST/CharUnits.h"
15
#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
16
#include "clang/StaticAnalyzer/Core/Checker.h"
17
#include "clang/StaticAnalyzer/Core/CheckerManager.h"
18
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
19
20
using namespace clang;
21
using namespace ento;
22
23
namespace {
24
class CastSizeChecker : public Checker< check::PreStmt<CastExpr> > {
25
  mutable std::unique_ptr<BuiltinBug> BT;
26
27
public:
28
  void checkPreStmt(const CastExpr *CE, CheckerContext &C) const;
29
};
30
}
31
32
/// Check if we are casting to a struct with a flexible array at the end.
33
/// \code
34
/// struct foo {
35
///   size_t len;
36
///   struct bar data[];
37
/// };
38
/// \endcode
39
/// or
40
/// \code
41
/// struct foo {
42
///   size_t len;
43
///   struct bar data[0];
44
/// }
45
/// \endcode
46
/// In these cases it is also valid to allocate size of struct foo + a multiple
47
/// of struct bar.
48
static bool evenFlexibleArraySize(ASTContext &Ctx, CharUnits RegionSize,
49
19
                                  CharUnits TypeSize, QualType ToPointeeTy) {
50
19
  const RecordType *RT = ToPointeeTy->getAs<RecordType>();
51
19
  if (!RT)
52
4
    return false;
53
15
54
15
  const RecordDecl *RD = RT->getDecl();
55
15
  RecordDecl::field_iterator Iter(RD->field_begin());
56
15
  RecordDecl::field_iterator End(RD->field_end());
57
15
  const FieldDecl *Last = nullptr;
58
45
  for (; Iter != End; 
++Iter30
)
59
30
    Last = *Iter;
60
15
  assert(Last && "empty structs should already be handled");
61
15
62
15
  const Type *ElemType = Last->getType()->getArrayElementTypeNoTypeQual();
63
15
  CharUnits FlexSize;
64
15
  if (const ConstantArrayType *ArrayTy =
65
11
        Ctx.getAsConstantArrayType(Last->getType())) {
66
11
    FlexSize = Ctx.getTypeSizeInChars(ElemType);
67
11
    if (ArrayTy->getSize() == 1 && 
TypeSize > FlexSize5
)
68
5
      TypeSize -= FlexSize;
69
6
    else if (ArrayTy->getSize() != 0)
70
2
      return false;
71
4
  } else if (RD->hasFlexibleArrayMember()) {
72
4
    FlexSize = Ctx.getTypeSizeInChars(ElemType);
73
4
  } else {
74
0
    return false;
75
0
  }
76
13
77
13
  if (FlexSize.isZero())
78
0
    return false;
79
13
80
13
  CharUnits Left = RegionSize - TypeSize;
81
13
  if (Left.isNegative())
82
0
    return false;
83
13
84
13
  return Left % FlexSize == 0;
85
13
}
86
87
7.85k
void CastSizeChecker::checkPreStmt(const CastExpr *CE,CheckerContext &C) const {
88
7.85k
  const Expr *E = CE->getSubExpr();
89
7.85k
  ASTContext &Ctx = C.getASTContext();
90
7.85k
  QualType ToTy = Ctx.getCanonicalType(CE->getType());
91
7.85k
  const PointerType *ToPTy = dyn_cast<PointerType>(ToTy.getTypePtr());
92
7.85k
93
7.85k
  if (!ToPTy)
94
3.56k
    return;
95
4.29k
96
4.29k
  QualType ToPointeeTy = ToPTy->getPointeeType();
97
4.29k
98
4.29k
  // Only perform the check if 'ToPointeeTy' is a complete type.
99
4.29k
  if (ToPointeeTy->isIncompleteType())
100
645
    return;
101
3.64k
102
3.64k
  ProgramStateRef state = C.getState();
103
3.64k
  const MemRegion *R = C.getSVal(E).getAsRegion();
104
3.64k
  if (!R)
105
180
    return;
106
3.46k
107
3.46k
  const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R);
108
3.46k
  if (!SR)
109
3.15k
    return;
110
313
111
313
  SValBuilder &svalBuilder = C.getSValBuilder();
112
313
  SVal extent = SR->getExtent(svalBuilder);
113
313
  const llvm::APSInt *extentInt = svalBuilder.getKnownValue(state, extent);
114
313
  if (!extentInt)
115
89
    return;
116
224
117
224
  CharUnits regionSize = CharUnits::fromQuantity(extentInt->getSExtValue());
118
224
  CharUnits typeSize = C.getASTContext().getTypeSizeInChars(ToPointeeTy);
119
224
120
224
  // Ignore void, and a few other un-sizeable types.
121
224
  if (typeSize.isZero())
122
3
    return;
123
221
124
221
  if (regionSize % typeSize == 0)
125
202
    return;
126
19
127
19
  if (evenFlexibleArraySize(Ctx, regionSize, typeSize, ToPointeeTy))
128
7
    return;
129
12
130
12
  if (ExplodedNode *errorNode = C.generateErrorNode()) {
131
12
    if (!BT)
132
2
      BT.reset(new BuiltinBug(this, "Cast region with wrong size.",
133
2
                                    "Cast a region whose size is not a multiple"
134
2
                                    " of the destination type size."));
135
12
    auto R = llvm::make_unique<BugReport>(*BT, BT->getDescription(), errorNode);
136
12
    R->addRange(CE->getSourceRange());
137
12
    C.emitReport(std::move(R));
138
12
  }
139
12
}
140
141
60
void ento::registerCastSizeChecker(CheckerManager &mgr) {
142
60
  mgr.registerChecker<CastSizeChecker>();
143
60
}
144
145
76
bool ento::shouldRegisterCastSizeChecker(const LangOptions &LO) {
146
76
  // PR31226: C++ is more complicated than what this checker currently supports.
147
76
  // There are derived-to-base casts, there are different rules for 0-size
148
76
  // structures, no flexible arrays, etc.
149
76
  // FIXME: Disabled on C++ for now.
150
76
  return !LO.CPlusPlus;
151
76
}