1 //=== UndefResultChecker.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 defines UndefResultChecker, a builtin check in ExprEngine that 10 // performs checks for undefined results of non-assignment binary operators. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h" 15 #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h" 16 #include "clang/StaticAnalyzer/Core/Checker.h" 17 #include "clang/StaticAnalyzer/Core/CheckerManager.h" 18 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" 19 #include "clang/StaticAnalyzer/Core/PathSensitive/DynamicExtent.h" 20 #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h" 21 #include "llvm/ADT/SmallString.h" 22 #include "llvm/Support/raw_ostream.h" 23 24 using namespace clang; 25 using namespace ento; 26 27 namespace { 28 class UndefResultChecker 29 : public Checker< check::PostStmt<BinaryOperator> > { 30 31 mutable std::unique_ptr<BugType> BT; 32 33 public: 34 void checkPostStmt(const BinaryOperator *B, CheckerContext &C) const; 35 }; 36 } // end anonymous namespace 37 38 static bool isArrayIndexOutOfBounds(CheckerContext &C, const Expr *Ex) { 39 ProgramStateRef state = C.getState(); 40 41 if (!isa<ArraySubscriptExpr>(Ex)) 42 return false; 43 44 SVal Loc = C.getSVal(Ex); 45 if (!Loc.isValid()) 46 return false; 47 48 const MemRegion *MR = Loc.castAs<loc::MemRegionVal>().getRegion(); 49 const ElementRegion *ER = dyn_cast<ElementRegion>(MR); 50 if (!ER) 51 return false; 52 53 DefinedOrUnknownSVal Idx = ER->getIndex().castAs<DefinedOrUnknownSVal>(); 54 DefinedOrUnknownSVal ElementCount = getDynamicElementCount( 55 state, ER->getSuperRegion(), C.getSValBuilder(), ER->getValueType()); 56 ProgramStateRef StInBound, StOutBound; 57 std::tie(StInBound, StOutBound) = state->assumeInBoundDual(Idx, ElementCount); 58 return StOutBound && !StInBound; 59 } 60 61 static bool isShiftOverflow(const BinaryOperator *B, CheckerContext &C) { 62 return C.isGreaterOrEqual( 63 B->getRHS(), C.getASTContext().getIntWidth(B->getLHS()->getType())); 64 } 65 66 static bool isLeftShiftResultUnrepresentable(const BinaryOperator *B, 67 CheckerContext &C) { 68 SValBuilder &SB = C.getSValBuilder(); 69 ProgramStateRef State = C.getState(); 70 const llvm::APSInt *LHS = SB.getKnownValue(State, C.getSVal(B->getLHS())); 71 const llvm::APSInt *RHS = SB.getKnownValue(State, C.getSVal(B->getRHS())); 72 assert(LHS && RHS && "Values unknown, inconsistent state"); 73 return (unsigned)RHS->getZExtValue() > LHS->countl_zero(); 74 } 75 76 void UndefResultChecker::checkPostStmt(const BinaryOperator *B, 77 CheckerContext &C) const { 78 if (C.getSVal(B).isUndef()) { 79 80 // Do not report assignments of uninitialized values inside swap functions. 81 // This should allow to swap partially uninitialized structs 82 if (const FunctionDecl *EnclosingFunctionDecl = 83 dyn_cast<FunctionDecl>(C.getStackFrame()->getDecl())) 84 if (C.getCalleeName(EnclosingFunctionDecl) == "swap") 85 return; 86 87 // Generate an error node. 88 ExplodedNode *N = C.generateErrorNode(); 89 if (!N) 90 return; 91 92 if (!BT) 93 BT.reset( 94 new BugType(this, "Result of operation is garbage or undefined")); 95 96 SmallString<256> sbuf; 97 llvm::raw_svector_ostream OS(sbuf); 98 const Expr *Ex = nullptr; 99 bool isLeft = true; 100 101 if (C.getSVal(B->getLHS()).isUndef()) { 102 Ex = B->getLHS()->IgnoreParenCasts(); 103 isLeft = true; 104 } 105 else if (C.getSVal(B->getRHS()).isUndef()) { 106 Ex = B->getRHS()->IgnoreParenCasts(); 107 isLeft = false; 108 } 109 110 if (Ex) { 111 OS << "The " << (isLeft ? "left" : "right") << " operand of '" 112 << BinaryOperator::getOpcodeStr(B->getOpcode()) 113 << "' is a garbage value"; 114 if (isArrayIndexOutOfBounds(C, Ex)) 115 OS << " due to array index out of bounds"; 116 } else { 117 // Neither operand was undefined, but the result is undefined. 118 OS << "The result of the '" 119 << BinaryOperator::getOpcodeStr(B->getOpcode()) 120 << "' expression is undefined"; 121 } 122 auto report = std::make_unique<PathSensitiveBugReport>(*BT, OS.str(), N); 123 if (Ex) { 124 report->addRange(Ex->getSourceRange()); 125 bugreporter::trackExpressionValue(N, Ex, *report); 126 } 127 else 128 bugreporter::trackExpressionValue(N, B, *report); 129 130 C.emitReport(std::move(report)); 131 } 132 } 133 134 void ento::registerUndefResultChecker(CheckerManager &mgr) { 135 mgr.registerChecker<UndefResultChecker>(); 136 } 137 138 bool ento::shouldRegisterUndefResultChecker(const CheckerManager &mgr) { 139 return true; 140 } 141