Coverage Report

Created: 2020-11-24 06:42

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