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