]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/lib/StaticAnalyzer/Checkers/UndefResultChecker.cpp
Merge clang trunk r300422 and resolve conflicts.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / clang / lib / StaticAnalyzer / Checkers / UndefResultChecker.cpp
1 //=== UndefResultChecker.cpp ------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This defines UndefResultChecker, a builtin check in ExprEngine that
11 // performs checks for undefined results of non-assignment binary operators.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "ClangSACheckers.h"
16 #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
17 #include "clang/StaticAnalyzer/Core/Checker.h"
18 #include "clang/StaticAnalyzer/Core/CheckerManager.h"
19 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.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   const LocationContext *LCtx = C.getLocationContext();
41
42   if (!isa<ArraySubscriptExpr>(Ex))
43     return false;
44
45   SVal Loc = state->getSVal(Ex, LCtx);
46   if (!Loc.isValid())
47     return false;
48
49   const MemRegion *MR = Loc.castAs<loc::MemRegionVal>().getRegion();
50   const ElementRegion *ER = dyn_cast<ElementRegion>(MR);
51   if (!ER)
52     return false;
53
54   DefinedOrUnknownSVal Idx = ER->getIndex().castAs<DefinedOrUnknownSVal>();
55   DefinedOrUnknownSVal NumElements = C.getStoreManager().getSizeInElements(
56       state, ER->getSuperRegion(), ER->getValueType());
57   ProgramStateRef StInBound = state->assumeInBound(Idx, NumElements, true);
58   ProgramStateRef StOutBound = state->assumeInBound(Idx, NumElements, false);
59   return StOutBound && !StInBound;
60 }
61
62 void UndefResultChecker::checkPostStmt(const BinaryOperator *B,
63                                        CheckerContext &C) const {
64   ProgramStateRef state = C.getState();
65   const LocationContext *LCtx = C.getLocationContext();
66   if (state->getSVal(B, LCtx).isUndef()) {
67
68     // Do not report assignments of uninitialized values inside swap functions.
69     // This should allow to swap partially uninitialized structs
70     // (radar://14129997)
71     if (const FunctionDecl *EnclosingFunctionDecl =
72         dyn_cast<FunctionDecl>(C.getStackFrame()->getDecl()))
73       if (C.getCalleeName(EnclosingFunctionDecl) == "swap")
74         return;
75
76     // Generate an error node.
77     ExplodedNode *N = C.generateErrorNode();
78     if (!N)
79       return;
80
81     if (!BT)
82       BT.reset(
83           new BuiltinBug(this, "Result of operation is garbage or undefined"));
84
85     SmallString<256> sbuf;
86     llvm::raw_svector_ostream OS(sbuf);
87     const Expr *Ex = nullptr;
88     bool isLeft = true;
89
90     if (state->getSVal(B->getLHS(), LCtx).isUndef()) {
91       Ex = B->getLHS()->IgnoreParenCasts();
92       isLeft = true;
93     }
94     else if (state->getSVal(B->getRHS(), LCtx).isUndef()) {
95       Ex = B->getRHS()->IgnoreParenCasts();
96       isLeft = false;
97     }
98
99     if (Ex) {
100       OS << "The " << (isLeft ? "left" : "right")
101          << " operand of '"
102          << BinaryOperator::getOpcodeStr(B->getOpcode())
103          << "' is a garbage value";
104       if (isArrayIndexOutOfBounds(C, Ex))
105         OS << " due to array index out of bounds";
106     }
107     else {
108       // Neither operand was undefined, but the result is undefined.
109       OS << "The result of the '"
110          << BinaryOperator::getOpcodeStr(B->getOpcode())
111          << "' expression is undefined";
112     }
113     auto report = llvm::make_unique<BugReport>(*BT, OS.str(), N);
114     if (Ex) {
115       report->addRange(Ex->getSourceRange());
116       bugreporter::trackNullOrUndefValue(N, Ex, *report);
117     }
118     else
119       bugreporter::trackNullOrUndefValue(N, B, *report);
120
121     C.emitReport(std::move(report));
122   }
123 }
124
125 void ento::registerUndefResultChecker(CheckerManager &mgr) {
126   mgr.registerChecker<UndefResultChecker>();
127 }