1 //=== FixedAddressChecker.cpp - Fixed address usage checker ----*- C++ -*--===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This files defines FixedAddressChecker, a builtin checker that checks for
11 // assignment of a fixed address to a pointer.
12 // This check corresponds to CWE-587.
14 //===----------------------------------------------------------------------===//
16 #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
17 #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
18 #include "clang/StaticAnalyzer/Core/Checker.h"
19 #include "clang/StaticAnalyzer/Core/CheckerManager.h"
20 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
22 using namespace clang;
26 class FixedAddressChecker
27 : public Checker< check::PreStmt<BinaryOperator> > {
28 mutable std::unique_ptr<BuiltinBug> BT;
31 void checkPreStmt(const BinaryOperator *B, CheckerContext &C) const;
35 void FixedAddressChecker::checkPreStmt(const BinaryOperator *B,
36 CheckerContext &C) const {
37 // Using a fixed address is not portable because that address will probably
38 // not be valid in all environments or platforms.
40 if (B->getOpcode() != BO_Assign)
43 QualType T = B->getType();
44 if (!T->isPointerType())
47 SVal RV = C.getSVal(B->getRHS());
49 if (!RV.isConstant() || RV.isZeroConstant())
52 if (ExplodedNode *N = C.generateNonFatalErrorNode()) {
55 new BuiltinBug(this, "Use fixed address",
56 "Using a fixed address is not portable because that "
57 "address will probably not be valid in all "
58 "environments or platforms."));
59 auto R = llvm::make_unique<BugReport>(*BT, BT->getDescription(), N);
60 R->addRange(B->getRHS()->getSourceRange());
61 C.emitReport(std::move(R));
65 void ento::registerFixedAddressChecker(CheckerManager &mgr) {
66 mgr.registerChecker<FixedAddressChecker>();