1 //===- EnumCastOutOfRangeChecker.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 // The EnumCastOutOfRangeChecker is responsible for checking integer to 10 // enumeration casts that could result in undefined values. This could happen 11 // if the value that we cast from is out of the value range of the enumeration. 12 // Reference: 13 // [ISO/IEC 14882-2014] ISO/IEC 14882-2014. 14 // Programming Languages — C++, Fourth Edition. 2014. 15 // C++ Standard, [dcl.enum], in paragraph 8, which defines the range of an enum 16 // C++ Standard, [expr.static.cast], paragraph 10, which defines the behaviour 17 // of casting an integer value that is out of range 18 // SEI CERT C++ Coding Standard, INT50-CPP. Do not cast to an out-of-range 19 // enumeration value 20 //===----------------------------------------------------------------------===// 21 22 #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h" 23 #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h" 24 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" 25 #include "llvm/Support/FormatVariadic.h" 26 #include <optional> 27 28 using namespace clang; 29 using namespace ento; 30 using llvm::formatv; 31 32 namespace { 33 // This evaluator checks two SVals for equality. The first SVal is provided via 34 // the constructor, the second is the parameter of the overloaded () operator. 35 // It uses the in-built ConstraintManager to resolve the equlity to possible or 36 // not possible ProgramStates. 37 class ConstraintBasedEQEvaluator { 38 const DefinedOrUnknownSVal CompareValue; 39 const ProgramStateRef PS; 40 SValBuilder &SVB; 41 42 public: 43 ConstraintBasedEQEvaluator(CheckerContext &C, 44 const DefinedOrUnknownSVal CompareValue) 45 : CompareValue(CompareValue), PS(C.getState()), SVB(C.getSValBuilder()) {} 46 47 bool operator()(const llvm::APSInt &EnumDeclInitValue) { 48 DefinedOrUnknownSVal EnumDeclValue = SVB.makeIntVal(EnumDeclInitValue); 49 DefinedOrUnknownSVal ElemEqualsValueToCast = 50 SVB.evalEQ(PS, EnumDeclValue, CompareValue); 51 52 return static_cast<bool>(PS->assume(ElemEqualsValueToCast, true)); 53 } 54 }; 55 56 // This checker checks CastExpr statements. 57 // If the value provided to the cast is one of the values the enumeration can 58 // represent, the said value matches the enumeration. If the checker can 59 // establish the impossibility of matching it gives a warning. 60 // Being conservative, it does not warn if there is slight possibility the 61 // value can be matching. 62 class EnumCastOutOfRangeChecker : public Checker<check::PreStmt<CastExpr>> { 63 mutable std::unique_ptr<BugType> EnumValueCastOutOfRange; 64 void reportWarning(CheckerContext &C, const CastExpr *CE, 65 const EnumDecl *E) const; 66 67 public: 68 void checkPreStmt(const CastExpr *CE, CheckerContext &C) const; 69 }; 70 71 using EnumValueVector = llvm::SmallVector<llvm::APSInt, 6>; 72 73 // Collects all of the values an enum can represent (as SVals). 74 EnumValueVector getDeclValuesForEnum(const EnumDecl *ED) { 75 EnumValueVector DeclValues( 76 std::distance(ED->enumerator_begin(), ED->enumerator_end())); 77 llvm::transform(ED->enumerators(), DeclValues.begin(), 78 [](const EnumConstantDecl *D) { return D->getInitVal(); }); 79 return DeclValues; 80 } 81 } // namespace 82 83 void EnumCastOutOfRangeChecker::reportWarning(CheckerContext &C, 84 const CastExpr *CE, 85 const EnumDecl *E) const { 86 assert(E && "valid EnumDecl* is expected"); 87 if (const ExplodedNode *N = C.generateNonFatalErrorNode()) { 88 if (!EnumValueCastOutOfRange) 89 EnumValueCastOutOfRange.reset( 90 new BugType(this, "Enum cast out of range")); 91 92 std::string ValueStr = "", NameStr = "the enum"; 93 94 // Try to add details to the message: 95 const auto ConcreteValue = 96 C.getSVal(CE->getSubExpr()).getAs<nonloc::ConcreteInt>(); 97 if (ConcreteValue) { 98 ValueStr = formatv(" '{0}'", ConcreteValue->getValue()); 99 } 100 if (StringRef EnumName{E->getName()}; !EnumName.empty()) { 101 NameStr = formatv("'{0}'", EnumName); 102 } 103 104 std::string Msg = formatv("The value{0} provided to the cast expression is " 105 "not in the valid range of values for {1}", 106 ValueStr, NameStr); 107 108 auto BR = std::make_unique<PathSensitiveBugReport>(*EnumValueCastOutOfRange, 109 Msg, N); 110 bugreporter::trackExpressionValue(N, CE->getSubExpr(), *BR); 111 BR->addNote("enum declared here", 112 PathDiagnosticLocation::create(E, C.getSourceManager()), 113 {E->getSourceRange()}); 114 C.emitReport(std::move(BR)); 115 } 116 } 117 118 void EnumCastOutOfRangeChecker::checkPreStmt(const CastExpr *CE, 119 CheckerContext &C) const { 120 121 // Only perform enum range check on casts where such checks are valid. For 122 // all other cast kinds (where enum range checks are unnecessary or invalid), 123 // just return immediately. TODO: The set of casts allowed for enum range 124 // checking may be incomplete. Better to add a missing cast kind to enable a 125 // missing check than to generate false negatives and have to remove those 126 // later. 127 switch (CE->getCastKind()) { 128 case CK_IntegralCast: 129 break; 130 131 default: 132 return; 133 break; 134 } 135 136 // Get the value of the expression to cast. 137 const std::optional<DefinedOrUnknownSVal> ValueToCast = 138 C.getSVal(CE->getSubExpr()).getAs<DefinedOrUnknownSVal>(); 139 140 // If the value cannot be reasoned about (not even a DefinedOrUnknownSVal), 141 // don't analyze further. 142 if (!ValueToCast) 143 return; 144 145 const QualType T = CE->getType(); 146 // Check whether the cast type is an enum. 147 if (!T->isEnumeralType()) 148 return; 149 150 // If the cast is an enum, get its declaration. 151 // If the isEnumeralType() returned true, then the declaration must exist 152 // even if it is a stub declaration. It is up to the getDeclValuesForEnum() 153 // function to handle this. 154 const EnumDecl *ED = T->castAs<EnumType>()->getDecl(); 155 156 EnumValueVector DeclValues = getDeclValuesForEnum(ED); 157 158 // If the declarator list is empty, bail out. 159 // Every initialization an enum with a fixed underlying type but without any 160 // enumerators would produce a warning if we were to continue at this point. 161 // The most notable example is std::byte in the C++17 standard library. 162 // TODO: Create heuristics to bail out when the enum type is intended to be 163 // used to store combinations of flag values (to mitigate the limitation 164 // described in the docs). 165 if (DeclValues.size() == 0) 166 return; 167 168 // Check if any of the enum values possibly match. 169 bool PossibleValueMatch = 170 llvm::any_of(DeclValues, ConstraintBasedEQEvaluator(C, *ValueToCast)); 171 172 // If there is no value that can possibly match any of the enum values, then 173 // warn. 174 if (!PossibleValueMatch) 175 reportWarning(C, CE, ED); 176 } 177 178 void ento::registerEnumCastOutOfRangeChecker(CheckerManager &mgr) { 179 mgr.registerChecker<EnumCastOutOfRangeChecker>(); 180 } 181 182 bool ento::shouldRegisterEnumCastOutOfRangeChecker(const CheckerManager &mgr) { 183 return true; 184 } 185