/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/StaticAnalyzer/Checkers/UndefResultChecker.cpp
Line | Count | Source (jump to first uncovered line) |
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 | 46 | static bool isArrayIndexOutOfBounds(CheckerContext &C, const Expr *Ex) { |
39 | 46 | ProgramStateRef state = C.getState(); |
40 | | |
41 | 46 | if (!isa<ArraySubscriptExpr>(Ex)) |
42 | 38 | return false; |
43 | | |
44 | 8 | SVal Loc = C.getSVal(Ex); |
45 | 8 | if (!Loc.isValid()) |
46 | 0 | return false; |
47 | | |
48 | 8 | const MemRegion *MR = Loc.castAs<loc::MemRegionVal>().getRegion(); |
49 | 8 | const ElementRegion *ER = dyn_cast<ElementRegion>(MR); |
50 | 8 | if (!ER) |
51 | 0 | return false; |
52 | | |
53 | 8 | DefinedOrUnknownSVal Idx = ER->getIndex().castAs<DefinedOrUnknownSVal>(); |
54 | 8 | DefinedOrUnknownSVal ElementCount = getDynamicElementCount( |
55 | 8 | state, ER->getSuperRegion(), C.getSValBuilder(), ER->getValueType()); |
56 | 8 | ProgramStateRef StInBound, StOutBound; |
57 | 8 | std::tie(StInBound, StOutBound) = state->assumeInBoundDual(Idx, ElementCount); |
58 | 8 | return StOutBound && !StInBound4 ; |
59 | 8 | } |
60 | | |
61 | 9 | static bool isShiftOverflow(const BinaryOperator *B, CheckerContext &C) { |
62 | 9 | return C.isGreaterOrEqual( |
63 | 9 | B->getRHS(), C.getASTContext().getIntWidth(B->getLHS()->getType())); |
64 | 9 | } |
65 | | |
66 | | static bool isLeftShiftResultUnrepresentable(const BinaryOperator *B, |
67 | 3 | CheckerContext &C) { |
68 | 3 | SValBuilder &SB = C.getSValBuilder(); |
69 | 3 | ProgramStateRef State = C.getState(); |
70 | 3 | const llvm::APSInt *LHS = SB.getKnownValue(State, C.getSVal(B->getLHS())); |
71 | 3 | const llvm::APSInt *RHS = SB.getKnownValue(State, C.getSVal(B->getRHS())); |
72 | 3 | assert(LHS && RHS && "Values unknown, inconsistent state"); |
73 | 3 | return (unsigned)RHS->getZExtValue() > LHS->countl_zero(); |
74 | 3 | } |
75 | | |
76 | | void UndefResultChecker::checkPostStmt(const BinaryOperator *B, |
77 | 127k | CheckerContext &C) const { |
78 | 127k | 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 | 60 | if (const FunctionDecl *EnclosingFunctionDecl = |
83 | 60 | dyn_cast<FunctionDecl>(C.getStackFrame()->getDecl())) |
84 | 57 | if (C.getCalleeName(EnclosingFunctionDecl) == "swap") |
85 | 1 | return; |
86 | | |
87 | | // Generate an error node. |
88 | 59 | ExplodedNode *N = C.generateErrorNode(); |
89 | 59 | if (!N) |
90 | 0 | return; |
91 | | |
92 | 59 | if (!BT) |
93 | 34 | BT.reset( |
94 | 34 | new BugType(this, "Result of operation is garbage or undefined")); |
95 | | |
96 | 59 | SmallString<256> sbuf; |
97 | 59 | llvm::raw_svector_ostream OS(sbuf); |
98 | 59 | const Expr *Ex = nullptr; |
99 | 59 | bool isLeft = true; |
100 | | |
101 | 59 | if (C.getSVal(B->getLHS()).isUndef()) { |
102 | 37 | Ex = B->getLHS()->IgnoreParenCasts(); |
103 | 37 | isLeft = true; |
104 | 37 | } |
105 | 22 | else if (C.getSVal(B->getRHS()).isUndef()) { |
106 | 9 | Ex = B->getRHS()->IgnoreParenCasts(); |
107 | 9 | isLeft = false; |
108 | 9 | } |
109 | | |
110 | 59 | if (Ex) { |
111 | 46 | OS << "The " << (isLeft ? "left"37 : "right"9 ) << " operand of '" |
112 | 46 | << BinaryOperator::getOpcodeStr(B->getOpcode()) |
113 | 46 | << "' is a garbage value"; |
114 | 46 | if (isArrayIndexOutOfBounds(C, Ex)) |
115 | 4 | OS << " due to array index out of bounds"; |
116 | 46 | } else { |
117 | | // Neither operand was undefined, but the result is undefined. |
118 | 13 | if ((B->getOpcode() == BinaryOperatorKind::BO_Shl || |
119 | 13 | B->getOpcode() == BinaryOperatorKind::BO_Shr1 ) && |
120 | 13 | C.isNegative(B->getRHS())12 ) { |
121 | 3 | OS << "The result of the " |
122 | 3 | << ((B->getOpcode() == BinaryOperatorKind::BO_Shl) ? "left" |
123 | 3 | : "right"0 ) |
124 | 3 | << " shift is undefined because the right operand is negative"; |
125 | 3 | Ex = B->getRHS(); |
126 | 10 | } else if ((B->getOpcode() == BinaryOperatorKind::BO_Shl || |
127 | 10 | B->getOpcode() == BinaryOperatorKind::BO_Shr1 ) && |
128 | 10 | isShiftOverflow(B, C)9 ) { |
129 | | |
130 | 4 | OS << "The result of the " |
131 | 4 | << ((B->getOpcode() == BinaryOperatorKind::BO_Shl) ? "left" |
132 | 4 | : "right"0 ) |
133 | 4 | << " shift is undefined due to shifting by "; |
134 | 4 | Ex = B->getRHS(); |
135 | | |
136 | 4 | SValBuilder &SB = C.getSValBuilder(); |
137 | 4 | const llvm::APSInt *I = |
138 | 4 | SB.getKnownValue(C.getState(), C.getSVal(B->getRHS())); |
139 | 4 | if (!I) |
140 | 0 | OS << "a value that is"; |
141 | 4 | else if (I->isUnsigned()) |
142 | 1 | OS << '\'' << I->getZExtValue() << "\', which is"; |
143 | 3 | else |
144 | 3 | OS << '\'' << I->getSExtValue() << "\', which is"; |
145 | | |
146 | 4 | OS << " greater or equal to the width of type '" |
147 | 4 | << B->getLHS()->getType() << "'."; |
148 | 6 | } else if (B->getOpcode() == BinaryOperatorKind::BO_Shl && |
149 | 6 | C.isNegative(B->getLHS())5 ) { |
150 | 2 | OS << "The result of the left shift is undefined because the left " |
151 | 2 | "operand is negative"; |
152 | 2 | Ex = B->getLHS(); |
153 | 4 | } else if (B->getOpcode() == BinaryOperatorKind::BO_Shl && |
154 | 4 | isLeftShiftResultUnrepresentable(B, C)3 ) { |
155 | 2 | ProgramStateRef State = C.getState(); |
156 | 2 | SValBuilder &SB = C.getSValBuilder(); |
157 | 2 | const llvm::APSInt *LHS = |
158 | 2 | SB.getKnownValue(State, C.getSVal(B->getLHS())); |
159 | 2 | const llvm::APSInt *RHS = |
160 | 2 | SB.getKnownValue(State, C.getSVal(B->getRHS())); |
161 | 2 | OS << "The result of the left shift is undefined due to shifting \'" |
162 | 2 | << LHS->getSExtValue() << "\' by \'" << RHS->getZExtValue() |
163 | 2 | << "\', which is unrepresentable in the unsigned version of " |
164 | 2 | << "the return type \'" << B->getLHS()->getType() << "\'"; |
165 | 2 | Ex = B->getLHS(); |
166 | 2 | } else { |
167 | 2 | OS << "The result of the '" |
168 | 2 | << BinaryOperator::getOpcodeStr(B->getOpcode()) |
169 | 2 | << "' expression is undefined"; |
170 | 2 | } |
171 | 13 | } |
172 | 59 | auto report = std::make_unique<PathSensitiveBugReport>(*BT, OS.str(), N); |
173 | 59 | if (Ex) { |
174 | 57 | report->addRange(Ex->getSourceRange()); |
175 | 57 | bugreporter::trackExpressionValue(N, Ex, *report); |
176 | 57 | } |
177 | 2 | else |
178 | 2 | bugreporter::trackExpressionValue(N, B, *report); |
179 | | |
180 | 59 | C.emitReport(std::move(report)); |
181 | 59 | } |
182 | 127k | } |
183 | | |
184 | 1.27k | void ento::registerUndefResultChecker(CheckerManager &mgr) { |
185 | 1.27k | mgr.registerChecker<UndefResultChecker>(); |
186 | 1.27k | } |
187 | | |
188 | 2.55k | bool ento::shouldRegisterUndefResultChecker(const CheckerManager &mgr) { |
189 | 2.55k | return true; |
190 | 2.55k | } |