1e5dd7070Spatrick //=== UndefResultChecker.cpp ------------------------------------*- C++ -*-===//
2e5dd7070Spatrick //
3e5dd7070Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e5dd7070Spatrick // See https://llvm.org/LICENSE.txt for license information.
5e5dd7070Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e5dd7070Spatrick //
7e5dd7070Spatrick //===----------------------------------------------------------------------===//
8e5dd7070Spatrick //
9e5dd7070Spatrick // This defines UndefResultChecker, a builtin check in ExprEngine that
10e5dd7070Spatrick // performs checks for undefined results of non-assignment binary operators.
11e5dd7070Spatrick //
12e5dd7070Spatrick //===----------------------------------------------------------------------===//
13e5dd7070Spatrick
14e5dd7070Spatrick #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
15e5dd7070Spatrick #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
16e5dd7070Spatrick #include "clang/StaticAnalyzer/Core/Checker.h"
17e5dd7070Spatrick #include "clang/StaticAnalyzer/Core/CheckerManager.h"
18e5dd7070Spatrick #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
19a9ac8606Spatrick #include "clang/StaticAnalyzer/Core/PathSensitive/DynamicExtent.h"
20e5dd7070Spatrick #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
21e5dd7070Spatrick #include "llvm/ADT/SmallString.h"
22e5dd7070Spatrick #include "llvm/Support/raw_ostream.h"
23e5dd7070Spatrick
24e5dd7070Spatrick using namespace clang;
25e5dd7070Spatrick using namespace ento;
26e5dd7070Spatrick
27e5dd7070Spatrick namespace {
28e5dd7070Spatrick class UndefResultChecker
29e5dd7070Spatrick : public Checker< check::PostStmt<BinaryOperator> > {
30e5dd7070Spatrick
31e5dd7070Spatrick mutable std::unique_ptr<BugType> BT;
32e5dd7070Spatrick
33e5dd7070Spatrick public:
34e5dd7070Spatrick void checkPostStmt(const BinaryOperator *B, CheckerContext &C) const;
35e5dd7070Spatrick };
36e5dd7070Spatrick } // end anonymous namespace
37e5dd7070Spatrick
isArrayIndexOutOfBounds(CheckerContext & C,const Expr * Ex)38e5dd7070Spatrick static bool isArrayIndexOutOfBounds(CheckerContext &C, const Expr *Ex) {
39e5dd7070Spatrick ProgramStateRef state = C.getState();
40e5dd7070Spatrick
41e5dd7070Spatrick if (!isa<ArraySubscriptExpr>(Ex))
42e5dd7070Spatrick return false;
43e5dd7070Spatrick
44e5dd7070Spatrick SVal Loc = C.getSVal(Ex);
45e5dd7070Spatrick if (!Loc.isValid())
46e5dd7070Spatrick return false;
47e5dd7070Spatrick
48e5dd7070Spatrick const MemRegion *MR = Loc.castAs<loc::MemRegionVal>().getRegion();
49e5dd7070Spatrick const ElementRegion *ER = dyn_cast<ElementRegion>(MR);
50e5dd7070Spatrick if (!ER)
51e5dd7070Spatrick return false;
52e5dd7070Spatrick
53e5dd7070Spatrick DefinedOrUnknownSVal Idx = ER->getIndex().castAs<DefinedOrUnknownSVal>();
54ec727ea7Spatrick DefinedOrUnknownSVal ElementCount = getDynamicElementCount(
55ec727ea7Spatrick state, ER->getSuperRegion(), C.getSValBuilder(), ER->getValueType());
56*12c85518Srobert ProgramStateRef StInBound, StOutBound;
57*12c85518Srobert std::tie(StInBound, StOutBound) = state->assumeInBoundDual(Idx, ElementCount);
58e5dd7070Spatrick return StOutBound && !StInBound;
59e5dd7070Spatrick }
60e5dd7070Spatrick
isShiftOverflow(const BinaryOperator * B,CheckerContext & C)61e5dd7070Spatrick static bool isShiftOverflow(const BinaryOperator *B, CheckerContext &C) {
62e5dd7070Spatrick return C.isGreaterOrEqual(
63e5dd7070Spatrick B->getRHS(), C.getASTContext().getIntWidth(B->getLHS()->getType()));
64e5dd7070Spatrick }
65e5dd7070Spatrick
isLeftShiftResultUnrepresentable(const BinaryOperator * B,CheckerContext & C)66e5dd7070Spatrick static bool isLeftShiftResultUnrepresentable(const BinaryOperator *B,
67e5dd7070Spatrick CheckerContext &C) {
68e5dd7070Spatrick SValBuilder &SB = C.getSValBuilder();
69e5dd7070Spatrick ProgramStateRef State = C.getState();
70e5dd7070Spatrick const llvm::APSInt *LHS = SB.getKnownValue(State, C.getSVal(B->getLHS()));
71e5dd7070Spatrick const llvm::APSInt *RHS = SB.getKnownValue(State, C.getSVal(B->getRHS()));
72e5dd7070Spatrick assert(LHS && RHS && "Values unknown, inconsistent state");
73e5dd7070Spatrick return (unsigned)RHS->getZExtValue() > LHS->countLeadingZeros();
74e5dd7070Spatrick }
75e5dd7070Spatrick
checkPostStmt(const BinaryOperator * B,CheckerContext & C) const76e5dd7070Spatrick void UndefResultChecker::checkPostStmt(const BinaryOperator *B,
77e5dd7070Spatrick CheckerContext &C) const {
78e5dd7070Spatrick if (C.getSVal(B).isUndef()) {
79e5dd7070Spatrick
80e5dd7070Spatrick // Do not report assignments of uninitialized values inside swap functions.
81e5dd7070Spatrick // This should allow to swap partially uninitialized structs
82e5dd7070Spatrick // (radar://14129997)
83e5dd7070Spatrick if (const FunctionDecl *EnclosingFunctionDecl =
84e5dd7070Spatrick dyn_cast<FunctionDecl>(C.getStackFrame()->getDecl()))
85e5dd7070Spatrick if (C.getCalleeName(EnclosingFunctionDecl) == "swap")
86e5dd7070Spatrick return;
87e5dd7070Spatrick
88e5dd7070Spatrick // Generate an error node.
89e5dd7070Spatrick ExplodedNode *N = C.generateErrorNode();
90e5dd7070Spatrick if (!N)
91e5dd7070Spatrick return;
92e5dd7070Spatrick
93e5dd7070Spatrick if (!BT)
94e5dd7070Spatrick BT.reset(
95e5dd7070Spatrick new BuiltinBug(this, "Result of operation is garbage or undefined"));
96e5dd7070Spatrick
97e5dd7070Spatrick SmallString<256> sbuf;
98e5dd7070Spatrick llvm::raw_svector_ostream OS(sbuf);
99e5dd7070Spatrick const Expr *Ex = nullptr;
100e5dd7070Spatrick bool isLeft = true;
101e5dd7070Spatrick
102e5dd7070Spatrick if (C.getSVal(B->getLHS()).isUndef()) {
103e5dd7070Spatrick Ex = B->getLHS()->IgnoreParenCasts();
104e5dd7070Spatrick isLeft = true;
105e5dd7070Spatrick }
106e5dd7070Spatrick else if (C.getSVal(B->getRHS()).isUndef()) {
107e5dd7070Spatrick Ex = B->getRHS()->IgnoreParenCasts();
108e5dd7070Spatrick isLeft = false;
109e5dd7070Spatrick }
110e5dd7070Spatrick
111e5dd7070Spatrick if (Ex) {
112e5dd7070Spatrick OS << "The " << (isLeft ? "left" : "right") << " operand of '"
113e5dd7070Spatrick << BinaryOperator::getOpcodeStr(B->getOpcode())
114e5dd7070Spatrick << "' is a garbage value";
115e5dd7070Spatrick if (isArrayIndexOutOfBounds(C, Ex))
116e5dd7070Spatrick OS << " due to array index out of bounds";
117e5dd7070Spatrick } else {
118e5dd7070Spatrick // Neither operand was undefined, but the result is undefined.
119e5dd7070Spatrick if ((B->getOpcode() == BinaryOperatorKind::BO_Shl ||
120e5dd7070Spatrick B->getOpcode() == BinaryOperatorKind::BO_Shr) &&
121e5dd7070Spatrick C.isNegative(B->getRHS())) {
122e5dd7070Spatrick OS << "The result of the "
123e5dd7070Spatrick << ((B->getOpcode() == BinaryOperatorKind::BO_Shl) ? "left"
124e5dd7070Spatrick : "right")
125e5dd7070Spatrick << " shift is undefined because the right operand is negative";
126e5dd7070Spatrick Ex = B->getRHS();
127e5dd7070Spatrick } else if ((B->getOpcode() == BinaryOperatorKind::BO_Shl ||
128e5dd7070Spatrick B->getOpcode() == BinaryOperatorKind::BO_Shr) &&
129e5dd7070Spatrick isShiftOverflow(B, C)) {
130e5dd7070Spatrick
131e5dd7070Spatrick OS << "The result of the "
132e5dd7070Spatrick << ((B->getOpcode() == BinaryOperatorKind::BO_Shl) ? "left"
133e5dd7070Spatrick : "right")
134e5dd7070Spatrick << " shift is undefined due to shifting by ";
135e5dd7070Spatrick Ex = B->getRHS();
136e5dd7070Spatrick
137e5dd7070Spatrick SValBuilder &SB = C.getSValBuilder();
138e5dd7070Spatrick const llvm::APSInt *I =
139e5dd7070Spatrick SB.getKnownValue(C.getState(), C.getSVal(B->getRHS()));
140e5dd7070Spatrick if (!I)
141e5dd7070Spatrick OS << "a value that is";
142e5dd7070Spatrick else if (I->isUnsigned())
143e5dd7070Spatrick OS << '\'' << I->getZExtValue() << "\', which is";
144e5dd7070Spatrick else
145e5dd7070Spatrick OS << '\'' << I->getSExtValue() << "\', which is";
146e5dd7070Spatrick
147e5dd7070Spatrick OS << " greater or equal to the width of type '"
148*12c85518Srobert << B->getLHS()->getType() << "'.";
149e5dd7070Spatrick } else if (B->getOpcode() == BinaryOperatorKind::BO_Shl &&
150e5dd7070Spatrick C.isNegative(B->getLHS())) {
151e5dd7070Spatrick OS << "The result of the left shift is undefined because the left "
152e5dd7070Spatrick "operand is negative";
153e5dd7070Spatrick Ex = B->getLHS();
154e5dd7070Spatrick } else if (B->getOpcode() == BinaryOperatorKind::BO_Shl &&
155e5dd7070Spatrick isLeftShiftResultUnrepresentable(B, C)) {
156e5dd7070Spatrick ProgramStateRef State = C.getState();
157e5dd7070Spatrick SValBuilder &SB = C.getSValBuilder();
158e5dd7070Spatrick const llvm::APSInt *LHS =
159e5dd7070Spatrick SB.getKnownValue(State, C.getSVal(B->getLHS()));
160e5dd7070Spatrick const llvm::APSInt *RHS =
161e5dd7070Spatrick SB.getKnownValue(State, C.getSVal(B->getRHS()));
162e5dd7070Spatrick OS << "The result of the left shift is undefined due to shifting \'"
163e5dd7070Spatrick << LHS->getSExtValue() << "\' by \'" << RHS->getZExtValue()
164e5dd7070Spatrick << "\', which is unrepresentable in the unsigned version of "
165*12c85518Srobert << "the return type \'" << B->getLHS()->getType() << "\'";
166e5dd7070Spatrick Ex = B->getLHS();
167e5dd7070Spatrick } else {
168e5dd7070Spatrick OS << "The result of the '"
169e5dd7070Spatrick << BinaryOperator::getOpcodeStr(B->getOpcode())
170e5dd7070Spatrick << "' expression is undefined";
171e5dd7070Spatrick }
172e5dd7070Spatrick }
173e5dd7070Spatrick auto report = std::make_unique<PathSensitiveBugReport>(*BT, OS.str(), N);
174e5dd7070Spatrick if (Ex) {
175e5dd7070Spatrick report->addRange(Ex->getSourceRange());
176e5dd7070Spatrick bugreporter::trackExpressionValue(N, Ex, *report);
177e5dd7070Spatrick }
178e5dd7070Spatrick else
179e5dd7070Spatrick bugreporter::trackExpressionValue(N, B, *report);
180e5dd7070Spatrick
181e5dd7070Spatrick C.emitReport(std::move(report));
182e5dd7070Spatrick }
183e5dd7070Spatrick }
184e5dd7070Spatrick
registerUndefResultChecker(CheckerManager & mgr)185e5dd7070Spatrick void ento::registerUndefResultChecker(CheckerManager &mgr) {
186e5dd7070Spatrick mgr.registerChecker<UndefResultChecker>();
187e5dd7070Spatrick }
188e5dd7070Spatrick
shouldRegisterUndefResultChecker(const CheckerManager & mgr)189ec727ea7Spatrick bool ento::shouldRegisterUndefResultChecker(const CheckerManager &mgr) {
190e5dd7070Spatrick return true;
191e5dd7070Spatrick }
192