1 //=== FixedAddressChecker.cpp - Fixed address usage checker ----*- C++ -*--===//
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
7 //===----------------------------------------------------------------------===//
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.
13 //===----------------------------------------------------------------------===//
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"
21 using namespace clang;
25 class FixedAddressChecker
26 : public Checker< check::PreStmt<BinaryOperator> > {
27 mutable std::unique_ptr<BuiltinBug> BT;
30 void checkPreStmt(const BinaryOperator *B, CheckerContext &C) const;
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.
39 if (B->getOpcode() != BO_Assign)
42 QualType T = B->getType();
43 if (!T->isPointerType())
46 SVal RV = C.getSVal(B->getRHS());
48 if (!RV.isConstant() || RV.isZeroConstant())
51 if (ExplodedNode *N = C.generateNonFatalErrorNode()) {
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."));
59 std::make_unique<PathSensitiveBugReport>(*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>();
69 bool ento::shouldRegisterFixedAddressChecker(const LangOptions &LO) {