Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/clang/lib/StaticAnalyzer/Checkers/ReturnPointerRangeChecker.cpp
Line
Count
Source (jump to first uncovered line)
1
//== ReturnPointerRangeChecker.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 ReturnPointerRangeChecker, which is a path-sensitive check
10
// which looks for an out-of-bound pointer being returned to callers.
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 ReturnPointerRangeChecker :
26
    public Checker< check::PreStmt<ReturnStmt> > {
27
  mutable std::unique_ptr<BuiltinBug> BT;
28
29
public:
30
    void checkPreStmt(const ReturnStmt *RS, CheckerContext &C) const;
31
};
32
}
33
34
void ReturnPointerRangeChecker::checkPreStmt(const ReturnStmt *RS,
35
283
                                             CheckerContext &C) const {
36
283
  ProgramStateRef state = C.getState();
37
283
38
283
  const Expr *RetE = RS->getRetValue();
39
283
  if (!RetE)
40
14
    return;
41
269
42
269
  SVal V = C.getSVal(RetE);
43
269
  const MemRegion *R = V.getAsRegion();
44
269
45
269
  const ElementRegion *ER = dyn_cast_or_null<ElementRegion>(R);
46
269
  if (!ER)
47
263
    return;
48
6
49
6
  DefinedOrUnknownSVal Idx = ER->getIndex().castAs<DefinedOrUnknownSVal>();
50
6
  // Zero index is always in bound, this also passes ElementRegions created for
51
6
  // pointer casts.
52
6
  if (Idx.isZeroConstant())
53
2
    return;
54
4
  // FIXME: All of this out-of-bounds checking should eventually be refactored
55
4
  // into a common place.
56
4
57
4
  DefinedOrUnknownSVal NumElements
58
4
    = C.getStoreManager().getSizeInElements(state, ER->getSuperRegion(),
59
4
                                           ER->getValueType());
60
4
61
4
  ProgramStateRef StInBound = state->assumeInBound(Idx, NumElements, true);
62
4
  ProgramStateRef StOutBound = state->assumeInBound(Idx, NumElements, false);
63
4
  if (StOutBound && !StInBound) {
64
4
    ExplodedNode *N = C.generateErrorNode(StOutBound);
65
4
66
4
    if (!N)
67
0
      return;
68
4
69
4
    // FIXME: This bug correspond to CWE-466.  Eventually we should have bug
70
4
    // types explicitly reference such exploit categories (when applicable).
71
4
    if (!BT)
72
3
      BT.reset(new BuiltinBug(
73
3
          this, "Return of pointer value outside of expected range",
74
3
          "Returned pointer value points outside the original object "
75
3
          "(potential buffer overflow)"));
76
4
77
4
    // FIXME: It would be nice to eventually make this diagnostic more clear,
78
4
    // e.g., by referencing the original declaration or by saying *why* this
79
4
    // reference is outside the range.
80
4
81
4
    // Generate a report for this bug.
82
4
    auto report = llvm::make_unique<BugReport>(*BT, BT->getDescription(), N);
83
4
84
4
    report->addRange(RetE->getSourceRange());
85
4
    C.emitReport(std::move(report));
86
4
  }
87
4
}
88
89
4
void ento::registerReturnPointerRangeChecker(CheckerManager &mgr) {
90
4
  mgr.registerChecker<ReturnPointerRangeChecker>();
91
4
}
92
93
4
bool ento::shouldRegisterReturnPointerRangeChecker(const LangOptions &LO) {
94
4
  return true;
95
4
}