xref: /aosp_15_r20/external/clang/lib/StaticAnalyzer/Checkers/UndefResultChecker.cpp (revision 67e74705e28f6214e480b399dd47ea732279e315)
1*67e74705SXin Li //=== UndefResultChecker.cpp ------------------------------------*- C++ -*-===//
2*67e74705SXin Li //
3*67e74705SXin Li //                     The LLVM Compiler Infrastructure
4*67e74705SXin Li //
5*67e74705SXin Li // This file is distributed under the University of Illinois Open Source
6*67e74705SXin Li // License. See LICENSE.TXT for details.
7*67e74705SXin Li //
8*67e74705SXin Li //===----------------------------------------------------------------------===//
9*67e74705SXin Li //
10*67e74705SXin Li // This defines UndefResultChecker, a builtin check in ExprEngine that
11*67e74705SXin Li // performs checks for undefined results of non-assignment binary operators.
12*67e74705SXin Li //
13*67e74705SXin Li //===----------------------------------------------------------------------===//
14*67e74705SXin Li 
15*67e74705SXin Li #include "ClangSACheckers.h"
16*67e74705SXin Li #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
17*67e74705SXin Li #include "clang/StaticAnalyzer/Core/Checker.h"
18*67e74705SXin Li #include "clang/StaticAnalyzer/Core/CheckerManager.h"
19*67e74705SXin Li #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
20*67e74705SXin Li #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
21*67e74705SXin Li #include "llvm/ADT/SmallString.h"
22*67e74705SXin Li #include "llvm/Support/raw_ostream.h"
23*67e74705SXin Li 
24*67e74705SXin Li using namespace clang;
25*67e74705SXin Li using namespace ento;
26*67e74705SXin Li 
27*67e74705SXin Li namespace {
28*67e74705SXin Li class UndefResultChecker
29*67e74705SXin Li   : public Checker< check::PostStmt<BinaryOperator> > {
30*67e74705SXin Li 
31*67e74705SXin Li   mutable std::unique_ptr<BugType> BT;
32*67e74705SXin Li 
33*67e74705SXin Li public:
34*67e74705SXin Li   void checkPostStmt(const BinaryOperator *B, CheckerContext &C) const;
35*67e74705SXin Li };
36*67e74705SXin Li } // end anonymous namespace
37*67e74705SXin Li 
checkPostStmt(const BinaryOperator * B,CheckerContext & C) const38*67e74705SXin Li void UndefResultChecker::checkPostStmt(const BinaryOperator *B,
39*67e74705SXin Li                                        CheckerContext &C) const {
40*67e74705SXin Li   ProgramStateRef state = C.getState();
41*67e74705SXin Li   const LocationContext *LCtx = C.getLocationContext();
42*67e74705SXin Li   if (state->getSVal(B, LCtx).isUndef()) {
43*67e74705SXin Li 
44*67e74705SXin Li     // Do not report assignments of uninitialized values inside swap functions.
45*67e74705SXin Li     // This should allow to swap partially uninitialized structs
46*67e74705SXin Li     // (radar://14129997)
47*67e74705SXin Li     if (const FunctionDecl *EnclosingFunctionDecl =
48*67e74705SXin Li         dyn_cast<FunctionDecl>(C.getStackFrame()->getDecl()))
49*67e74705SXin Li       if (C.getCalleeName(EnclosingFunctionDecl) == "swap")
50*67e74705SXin Li         return;
51*67e74705SXin Li 
52*67e74705SXin Li     // Generate an error node.
53*67e74705SXin Li     ExplodedNode *N = C.generateErrorNode();
54*67e74705SXin Li     if (!N)
55*67e74705SXin Li       return;
56*67e74705SXin Li 
57*67e74705SXin Li     if (!BT)
58*67e74705SXin Li       BT.reset(
59*67e74705SXin Li           new BuiltinBug(this, "Result of operation is garbage or undefined"));
60*67e74705SXin Li 
61*67e74705SXin Li     SmallString<256> sbuf;
62*67e74705SXin Li     llvm::raw_svector_ostream OS(sbuf);
63*67e74705SXin Li     const Expr *Ex = nullptr;
64*67e74705SXin Li     bool isLeft = true;
65*67e74705SXin Li 
66*67e74705SXin Li     if (state->getSVal(B->getLHS(), LCtx).isUndef()) {
67*67e74705SXin Li       Ex = B->getLHS()->IgnoreParenCasts();
68*67e74705SXin Li       isLeft = true;
69*67e74705SXin Li     }
70*67e74705SXin Li     else if (state->getSVal(B->getRHS(), LCtx).isUndef()) {
71*67e74705SXin Li       Ex = B->getRHS()->IgnoreParenCasts();
72*67e74705SXin Li       isLeft = false;
73*67e74705SXin Li     }
74*67e74705SXin Li 
75*67e74705SXin Li     if (Ex) {
76*67e74705SXin Li       OS << "The " << (isLeft ? "left" : "right")
77*67e74705SXin Li          << " operand of '"
78*67e74705SXin Li          << BinaryOperator::getOpcodeStr(B->getOpcode())
79*67e74705SXin Li          << "' is a garbage value";
80*67e74705SXin Li     }
81*67e74705SXin Li     else {
82*67e74705SXin Li       // Neither operand was undefined, but the result is undefined.
83*67e74705SXin Li       OS << "The result of the '"
84*67e74705SXin Li          << BinaryOperator::getOpcodeStr(B->getOpcode())
85*67e74705SXin Li          << "' expression is undefined";
86*67e74705SXin Li     }
87*67e74705SXin Li     auto report = llvm::make_unique<BugReport>(*BT, OS.str(), N);
88*67e74705SXin Li     if (Ex) {
89*67e74705SXin Li       report->addRange(Ex->getSourceRange());
90*67e74705SXin Li       bugreporter::trackNullOrUndefValue(N, Ex, *report);
91*67e74705SXin Li     }
92*67e74705SXin Li     else
93*67e74705SXin Li       bugreporter::trackNullOrUndefValue(N, B, *report);
94*67e74705SXin Li 
95*67e74705SXin Li     C.emitReport(std::move(report));
96*67e74705SXin Li   }
97*67e74705SXin Li }
98*67e74705SXin Li 
registerUndefResultChecker(CheckerManager & mgr)99*67e74705SXin Li void ento::registerUndefResultChecker(CheckerManager &mgr) {
100*67e74705SXin Li   mgr.registerChecker<UndefResultChecker>();
101*67e74705SXin Li }
102