Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/clang/lib/StaticAnalyzer/Checkers/FixedAddressChecker.cpp
Line
Count
Source
1
//=== FixedAddressChecker.cpp - Fixed address usage 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 files defines FixedAddressChecker, a builtin checker that checks for
10
// assignment of a fixed address to a pointer.
11
// This check corresponds to CWE-587.
12
//
13
//===----------------------------------------------------------------------===//
14
15
#include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
16
#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
17
#include "clang/StaticAnalyzer/Core/Checker.h"
18
#include "clang/StaticAnalyzer/Core/CheckerManager.h"
19
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
20
21
using namespace clang;
22
using namespace ento;
23
24
namespace {
25
class FixedAddressChecker
26
  : public Checker< check::PreStmt<BinaryOperator> > {
27
  mutable std::unique_ptr<BuiltinBug> BT;
28
29
public:
30
  void checkPreStmt(const BinaryOperator *B, CheckerContext &C) const;
31
};
32
}
33
34
void FixedAddressChecker::checkPreStmt(const BinaryOperator *B,
35
2.31k
                                       CheckerContext &C) const {
36
2.31k
  // Using a fixed address is not portable because that address will probably
37
2.31k
  // not be valid in all environments or platforms.
38
2.31k
39
2.31k
  if (B->getOpcode() != BO_Assign)
40
1.52k
    return;
41
791
42
791
  QualType T = B->getType();
43
791
  if (!T->isPointerType())
44
527
    return;
45
264
46
264
  SVal RV = C.getSVal(B->getRHS());
47
264
48
264
  if (!RV.isConstant() || 
RV.isZeroConstant()23
)
49
262
    return;
50
2
51
2
  if (ExplodedNode *N = C.generateNonFatalErrorNode()) {
52
2
    if (!BT)
53
2
      BT.reset(
54
2
          new BuiltinBug(this, "Use fixed address",
55
2
                         "Using a fixed address is not portable because that "
56
2
                         "address will probably not be valid in all "
57
2
                         "environments or platforms."));
58
2
    auto R = llvm::make_unique<BugReport>(*BT, BT->getDescription(), N);
59
2
    R->addRange(B->getRHS()->getSourceRange());
60
2
    C.emitReport(std::move(R));
61
2
  }
62
2
}
63
64
71
void ento::registerFixedAddressChecker(CheckerManager &mgr) {
65
71
  mgr.registerChecker<FixedAddressChecker>();
66
71
}
67
68
71
bool ento::shouldRegisterFixedAddressChecker(const LangOptions &LO) {
69
71
  return true;
70
71
}