clang  14.0.0git
FixedAddressChecker.cpp
Go to the documentation of this file.
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 
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  CheckerContext &C) const {
36  // Using a fixed address is not portable because that address will probably
37  // not be valid in all environments or platforms.
38 
39  if (B->getOpcode() != BO_Assign)
40  return;
41 
42  QualType T = B->getType();
43  if (!T->isPointerType())
44  return;
45 
46  SVal RV = C.getSVal(B->getRHS());
47 
48  if (!RV.isConstant() || RV.isZeroConstant())
49  return;
50 
51  if (ExplodedNode *N = C.generateNonFatalErrorNode()) {
52  if (!BT)
53  BT.reset(
54  new BuiltinBug(this, "Use fixed address",
55  "Using a fixed address is not portable because that "
56  "address will probably not be valid in all "
57  "environments or platforms."));
58  auto R =
59  std::make_unique<PathSensitiveBugReport>(*BT, BT->getDescription(), N);
60  R->addRange(B->getRHS()->getSourceRange());
61  C.emitReport(std::move(R));
62  }
63 }
64 
65 void ento::registerFixedAddressChecker(CheckerManager &mgr) {
66  mgr.registerChecker<FixedAddressChecker>();
67 }
68 
69 bool ento::shouldRegisterFixedAddressChecker(const CheckerManager &mgr) {
70  return true;
71 }
clang::Stmt::getSourceRange
SourceRange getSourceRange() const LLVM_READONLY
SourceLocation tokens are not useful in isolation - they are low level value objects created/interpre...
Definition: Stmt.cpp:324
clang::QualType
A (possibly-)qualified type.
Definition: Type.h:673
AttributeLangSupport::C
@ C
Definition: SemaDeclAttr.cpp:54
clang::BinaryOperator::getOpcode
Opcode getOpcode() const
Definition: Expr.h:3847
BuiltinCheckerRegistration.h
clang::BinaryOperator
A builtin binary operation expression such as "x + y" or "x <= y".
Definition: Expr.h:3803
CheckerManager.h
clang::Type::isPointerType
bool isPointerType() const
Definition: Type.h:6673
BugType.h
CheckerContext.h
Checker.h
clang
Definition: CalledOnceCheck.h:17
clang::BinaryOperator::getRHS
Expr * getRHS() const
Definition: Expr.h:3854
clang::Expr::getType
QualType getType() const
Definition: Expr.h:141