|
| 1 | +//=== PolymorphicPtrArithmetic.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 checker reports pointer arithmetic operations on arrays of |
| 10 | +// polymorphic objects, where the array has the type of its base class. |
| 11 | +// Corresponds to the CTR56-CPP. Do not use pointer arithmetic on |
| 12 | +// polymorphic objects |
| 13 | +// |
| 14 | +//===----------------------------------------------------------------------===// |
| 15 | + |
| 16 | +#include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h" |
| 17 | +#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h" |
| 18 | +#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h" |
| 19 | +#include "clang/StaticAnalyzer/Core/BugReporter/CommonBugCategories.h" |
| 20 | +#include "clang/StaticAnalyzer/Core/Checker.h" |
| 21 | +#include "clang/StaticAnalyzer/Core/CheckerManager.h" |
| 22 | +#include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h" |
| 23 | +#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" |
| 24 | +#include "clang/StaticAnalyzer/Core/PathSensitive/DynamicType.h" |
| 25 | +#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h" |
| 26 | + |
| 27 | +using namespace clang; |
| 28 | +using namespace ento; |
| 29 | + |
| 30 | +namespace { |
| 31 | +class PolymorphicPtrArithmeticChecker |
| 32 | + : public Checker<check::PreStmt<BinaryOperator>, |
| 33 | + check::PreStmt<ArraySubscriptExpr>> { |
| 34 | + const BugType BT{this, |
| 35 | + "Pointer arithmetic on polymorphic objects is undefined"}; |
| 36 | + |
| 37 | +protected: |
| 38 | + class PtrCastVisitor : public BugReporterVisitor { |
| 39 | + public: |
| 40 | + void Profile(llvm::FoldingSetNodeID &ID) const override { |
| 41 | + static int X = 0; |
| 42 | + ID.AddPointer(&X); |
| 43 | + } |
| 44 | + PathDiagnosticPieceRef VisitNode(const ExplodedNode *N, |
| 45 | + BugReporterContext &BRC, |
| 46 | + PathSensitiveBugReport &BR) override; |
| 47 | + }; |
| 48 | + |
| 49 | + void checkTypedExpr(const Expr *E, CheckerContext &C) const; |
| 50 | + |
| 51 | +public: |
| 52 | + void checkPreStmt(const BinaryOperator *B, CheckerContext &C) const; |
| 53 | + void checkPreStmt(const ArraySubscriptExpr *B, CheckerContext &C) const; |
| 54 | +}; |
| 55 | +} // namespace |
| 56 | + |
| 57 | +void PolymorphicPtrArithmeticChecker::checkPreStmt(const BinaryOperator *B, |
| 58 | + CheckerContext &C) const { |
| 59 | + if (!B->isAdditiveOp()) |
| 60 | + return; |
| 61 | + |
| 62 | + bool IsLHSPtr = B->getLHS()->getType()->isAnyPointerType(); |
| 63 | + bool IsRHSPtr = B->getRHS()->getType()->isAnyPointerType(); |
| 64 | + if (!IsLHSPtr && !IsRHSPtr) |
| 65 | + return; |
| 66 | + |
| 67 | + const Expr *E = IsLHSPtr ? B->getLHS() : B->getRHS(); |
| 68 | + |
| 69 | + checkTypedExpr(E, C); |
| 70 | +} |
| 71 | + |
| 72 | +void PolymorphicPtrArithmeticChecker::checkPreStmt(const ArraySubscriptExpr *B, |
| 73 | + CheckerContext &C) const { |
| 74 | + bool IsLHSPtr = B->getLHS()->getType()->isAnyPointerType(); |
| 75 | + bool IsRHSPtr = B->getRHS()->getType()->isAnyPointerType(); |
| 76 | + if (!IsLHSPtr && !IsRHSPtr) |
| 77 | + return; |
| 78 | + |
| 79 | + const Expr *E = IsLHSPtr ? B->getLHS() : B->getRHS(); |
| 80 | + |
| 81 | + checkTypedExpr(E, C); |
| 82 | +} |
| 83 | + |
| 84 | +void PolymorphicPtrArithmeticChecker::checkTypedExpr(const Expr *E, |
| 85 | + CheckerContext &C) const { |
| 86 | + const MemRegion *MR = C.getSVal(E).getAsRegion(); |
| 87 | + if (!MR) |
| 88 | + return; |
| 89 | + |
| 90 | + const auto *BaseClassRegion = MR->getAs<TypedValueRegion>(); |
| 91 | + const auto *DerivedClassRegion = MR->getBaseRegion()->getAs<SymbolicRegion>(); |
| 92 | + if (!BaseClassRegion || !DerivedClassRegion) |
| 93 | + return; |
| 94 | + |
| 95 | + const auto *BaseClass = BaseClassRegion->getValueType()->getAsCXXRecordDecl(); |
| 96 | + const auto *DerivedClass = |
| 97 | + DerivedClassRegion->getSymbol()->getType()->getPointeeCXXRecordDecl(); |
| 98 | + if (!BaseClass || !DerivedClass) |
| 99 | + return; |
| 100 | + |
| 101 | + if (!BaseClass->hasDefinition() || !DerivedClass->hasDefinition()) |
| 102 | + return; |
| 103 | + |
| 104 | + if (!DerivedClass->isDerivedFrom(BaseClass)) |
| 105 | + return; |
| 106 | + |
| 107 | + ExplodedNode *N = C.generateNonFatalErrorNode(); |
| 108 | + if (!N) |
| 109 | + return; |
| 110 | + |
| 111 | + SmallString<256> Buf; |
| 112 | + llvm::raw_svector_ostream OS(Buf); |
| 113 | + |
| 114 | + QualType SourceType = BaseClassRegion->getValueType(); |
| 115 | + QualType TargetType = |
| 116 | + DerivedClassRegion->getSymbol()->getType()->getPointeeType(); |
| 117 | + |
| 118 | + OS << "Doing pointer arithmetic with '" << TargetType.getAsString() |
| 119 | + << "' objects as their base class '" |
| 120 | + << SourceType.getAsString(C.getASTContext().getPrintingPolicy()) |
| 121 | + << "' is undefined"; |
| 122 | + |
| 123 | + auto R = std::make_unique<PathSensitiveBugReport>(BT, OS.str(), N); |
| 124 | + |
| 125 | + // Mark region of problematic base class for later use in the BugVisitor. |
| 126 | + R->markInteresting(BaseClassRegion); |
| 127 | + R->addVisitor<PtrCastVisitor>(); |
| 128 | + C.emitReport(std::move(R)); |
| 129 | +} |
| 130 | + |
| 131 | +PathDiagnosticPieceRef |
| 132 | +PolymorphicPtrArithmeticChecker::PtrCastVisitor::VisitNode( |
| 133 | + const ExplodedNode *N, BugReporterContext &BRC, |
| 134 | + PathSensitiveBugReport &BR) { |
| 135 | + const Stmt *S = N->getStmtForDiagnostics(); |
| 136 | + if (!S) |
| 137 | + return nullptr; |
| 138 | + |
| 139 | + const auto *CastE = dyn_cast<CastExpr>(S); |
| 140 | + if (!CastE) |
| 141 | + return nullptr; |
| 142 | + |
| 143 | + // FIXME: This way of getting base types does not support reference types. |
| 144 | + QualType SourceType = CastE->getSubExpr()->getType()->getPointeeType(); |
| 145 | + QualType TargetType = CastE->getType()->getPointeeType(); |
| 146 | + |
| 147 | + if (SourceType.isNull() || TargetType.isNull() || SourceType == TargetType) |
| 148 | + return nullptr; |
| 149 | + |
| 150 | + // Region associated with the current cast expression. |
| 151 | + const MemRegion *M = N->getSVal(CastE).getAsRegion(); |
| 152 | + if (!M) |
| 153 | + return nullptr; |
| 154 | + |
| 155 | + // Check if target region was marked as problematic previously. |
| 156 | + if (!BR.isInteresting(M)) |
| 157 | + return nullptr; |
| 158 | + |
| 159 | + SmallString<256> Buf; |
| 160 | + llvm::raw_svector_ostream OS(Buf); |
| 161 | + |
| 162 | + OS << "Casting from '" << SourceType.getAsString() << "' to '" |
| 163 | + << TargetType.getAsString() << "' here"; |
| 164 | + |
| 165 | + PathDiagnosticLocation Pos(S, BRC.getSourceManager(), |
| 166 | + N->getLocationContext()); |
| 167 | + return std::make_shared<PathDiagnosticEventPiece>(Pos, OS.str(), |
| 168 | + /*addPosRange=*/true); |
| 169 | +} |
| 170 | + |
| 171 | +void ento::registerPolymorphicPtrArithmeticChecker(CheckerManager &mgr) { |
| 172 | + mgr.registerChecker<PolymorphicPtrArithmeticChecker>(); |
| 173 | +} |
| 174 | + |
| 175 | +bool ento::shouldRegisterPolymorphicPtrArithmeticChecker( |
| 176 | + const CheckerManager &mgr) { |
| 177 | + return true; |
| 178 | +} |
0 commit comments