clang 22.0.0git
UndefResultChecker.cpp
Go to the documentation of this file.
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
21#include "llvm/Support/raw_ostream.h"
22
23using namespace clang;
24using namespace ento;
25
26namespace {
27class UndefResultChecker
28 : public Checker< check::PostStmt<BinaryOperator> > {
29
30 const BugType BT{this, "Result of operation is garbage or undefined"};
31
32public:
33 void checkPostStmt(const BinaryOperator *B, CheckerContext &C) const;
34};
35} // end anonymous namespace
36
37static bool isArrayIndexOutOfBounds(CheckerContext &C, const Expr *Ex) {
38 ProgramStateRef state = C.getState();
39
41 return false;
42
43 SVal Loc = C.getSVal(Ex);
44 if (!Loc.isValid())
45 return false;
46
48 const ElementRegion *ER = dyn_cast<ElementRegion>(MR);
49 if (!ER)
50 return false;
51
54 state, ER->getSuperRegion(), C.getSValBuilder(), ER->getValueType());
55 ProgramStateRef StInBound, StOutBound;
56 std::tie(StInBound, StOutBound) = state->assumeInBoundDual(Idx, ElementCount);
57 return StOutBound && !StInBound;
58}
59
60void UndefResultChecker::checkPostStmt(const BinaryOperator *B,
61 CheckerContext &C) const {
62 if (C.getSVal(B).isUndef()) {
63
64 // Do not report assignments of uninitialized values inside swap functions.
65 // This should allow to swap partially uninitialized structs
66 if (const FunctionDecl *EnclosingFunctionDecl =
67 dyn_cast<FunctionDecl>(C.getStackFrame()->getDecl()))
68 if (C.getCalleeName(EnclosingFunctionDecl) == "swap")
69 return;
70
71 // Generate an error node.
72 ExplodedNode *N = C.generateErrorNode();
73 if (!N)
74 return;
75
76 SmallString<256> sbuf;
77 llvm::raw_svector_ostream OS(sbuf);
78 const Expr *Ex = nullptr;
79 bool isLeft = true;
80
81 if (C.getSVal(B->getLHS()).isUndef()) {
82 Ex = B->getLHS()->IgnoreParenCasts();
83 isLeft = true;
84 }
85 else if (C.getSVal(B->getRHS()).isUndef()) {
86 Ex = B->getRHS()->IgnoreParenCasts();
87 isLeft = false;
88 }
89
90 if (Ex) {
91 OS << "The " << (isLeft ? "left" : "right") << " operand of '"
93 << "' is a garbage value";
95 OS << " due to array index out of bounds";
96 } else {
97 // Neither operand was undefined, but the result is undefined.
98 OS << "The result of the '"
100 << "' expression is undefined";
101 }
102 auto report = std::make_unique<PathSensitiveBugReport>(BT, OS.str(), N);
103 if (Ex) {
104 report->addRange(Ex->getSourceRange());
105 bugreporter::trackExpressionValue(N, Ex, *report);
106 }
107 else
109
110 C.emitReport(std::move(report));
111 }
112}
113
114void ento::registerUndefResultChecker(CheckerManager &mgr) {
115 mgr.registerChecker<UndefResultChecker>();
116}
117
118bool ento::shouldRegisterUndefResultChecker(const CheckerManager &mgr) {
119 return true;
120}
static const MemRegion * getRegion(const CallEvent &Call, const MutexDescriptor &Descriptor, bool IsLock)
static bool isArrayIndexOutOfBounds(CheckerContext &C, const Expr *Ex)
A builtin binary operation expression such as "x + y" or "x <= y".
Definition Expr.h:3974
Expr * getLHS() const
Definition Expr.h:4024
StringRef getOpcodeStr() const
Definition Expr.h:4040
Expr * getRHS() const
Definition Expr.h:4026
Opcode getOpcode() const
Definition Expr.h:4019
This represents one expression.
Definition Expr.h:112
Expr * IgnoreParenCasts() LLVM_READONLY
Skip past any parentheses and casts which might surround this expression until reaching a fixed point...
Definition Expr.cpp:3078
SourceRange getSourceRange() const LLVM_READONLY
SourceLocation tokens are not useful in isolation - they are low level value objects created/interpre...
Definition Stmt.cpp:334
CHECKER * registerChecker(AT &&...Args)
Register a single-part checker (derived from Checker): construct its singleton instance,...
Simple checker classes that implement one frontend (i.e.
Definition Checker.h:553
bool isValid() const =delete
ElementRegion is used to represent both array elements and casts.
Definition MemRegion.h:1227
QualType getValueType() const override
Definition MemRegion.h:1249
MemRegion - The root abstract class for all memory regions.
Definition MemRegion.h:98
SVal - This represents a symbolic expression, which can be either an L-value or an R-value.
Definition SVals.h:56
T castAs() const
Convert to the specified SVal type, asserting that this SVal is of the desired type.
Definition SVals.h:83
LLVM_ATTRIBUTE_RETURNS_NONNULL const MemRegion * getSuperRegion() const
Definition MemRegion.h:487
bool trackExpressionValue(const ExplodedNode *N, const Expr *E, PathSensitiveBugReport &R, TrackingOptions Opts={})
Attempts to add visitors to track expression value back to its point of origin.
DefinedOrUnknownSVal getDynamicElementCount(ProgramStateRef State, const MemRegion *MR, SValBuilder &SVB, QualType Ty)
IntrusiveRefCntPtr< const ProgramState > ProgramStateRef
@ OS
Indicates that the tracking object is a descendant of a referenced-counted OSObject,...
The JSON file list parser is used to communicate input to InstallAPI.
bool isa(CodeGen::Address addr)
Definition Address.h:330