Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/clang/lib/StaticAnalyzer/Checkers/ArrayBoundChecker.cpp
Line
Count
Source (jump to first uncovered line)
1
//== ArrayBoundChecker.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
// This file defines ArrayBoundChecker, which is a path-sensitive check
10
// which looks for an out-of-bound array element access.
11
//
12
//===----------------------------------------------------------------------===//
13
14
#include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.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
#include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
20
21
using namespace clang;
22
using namespace ento;
23
24
namespace {
25
class ArrayBoundChecker :
26
    public Checker<check::Location> {
27
  mutable std::unique_ptr<BuiltinBug> BT;
28
29
public:
30
  void checkLocation(SVal l, bool isLoad, const Stmt* S,
31
                     CheckerContext &C) const;
32
};
33
}
34
35
void ArrayBoundChecker::checkLocation(SVal l, bool isLoad, const Stmt* LoadS,
36
3.57k
                                      CheckerContext &C) const {
37
3.57k
  // Check for out of bound array element access.
38
3.57k
  const MemRegion *R = l.getAsRegion();
39
3.57k
  if (!R)
40
21
    return;
41
3.55k
42
3.55k
  const ElementRegion *ER = dyn_cast<ElementRegion>(R);
43
3.55k
  if (!ER)
44
3.03k
    return;
45
522
46
522
  // Get the index of the accessed element.
47
522
  DefinedOrUnknownSVal Idx = ER->getIndex().castAs<DefinedOrUnknownSVal>();
48
522
49
522
  // Zero index is always in bound, this also passes ElementRegions created for
50
522
  // pointer casts.
51
522
  if (Idx.isZeroConstant())
52
112
    return;
53
410
54
410
  ProgramStateRef state = C.getState();
55
410
56
410
  // Get the size of the array.
57
410
  DefinedOrUnknownSVal NumElements
58
410
    = C.getStoreManager().getSizeInElements(state, ER->getSuperRegion(),
59
410
                                            ER->getValueType());
60
410
61
410
  ProgramStateRef StInBound = state->assumeInBound(Idx, NumElements, true);
62
410
  ProgramStateRef StOutBound = state->assumeInBound(Idx, NumElements, false);
63
410
  if (StOutBound && 
!StInBound83
) {
64
16
    ExplodedNode *N = C.generateErrorNode(StOutBound);
65
16
    if (!N)
66
0
      return;
67
16
68
16
    if (!BT)
69
4
      BT.reset(new BuiltinBug(
70
4
          this, "Out-of-bound array access",
71
4
          "Access out-of-bound array element (buffer overflow)"));
72
16
73
16
    // FIXME: It would be nice to eventually make this diagnostic more clear,
74
16
    // e.g., by referencing the original declaration or by saying *why* this
75
16
    // reference is outside the range.
76
16
77
16
    // Generate a report for this bug.
78
16
    auto report = llvm::make_unique<BugReport>(*BT, BT->getDescription(), N);
79
16
80
16
    report->addRange(LoadS->getSourceRange());
81
16
    C.emitReport(std::move(report));
82
16
    return;
83
16
  }
84
394
85
394
  // Array bound check succeeded.  From this point forward the array bound
86
394
  // should always succeed.
87
394
  C.addTransition(StInBound);
88
394
}
89
90
7
void ento::registerArrayBoundChecker(CheckerManager &mgr) {
91
7
  mgr.registerChecker<ArrayBoundChecker>();
92
7
}
93
94
7
bool ento::shouldRegisterArrayBoundChecker(const LangOptions &LO) {
95
7
  return true;
96
7
}