blob: 38d2aa6d8f9deb252da05b4f0f611350f98d3580 [file] [log] [blame]
Zhongxing Xuc6123a12009-11-24 08:24:26 +00001//=== 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//
Ted Kremenek3a0678e2015-09-08 03:50:52 +000010// This defines UndefResultChecker, a builtin check in ExprEngine that
Zhongxing Xuc6123a12009-11-24 08:24:26 +000011// performs checks for undefined results of non-assignment binary operators.
12//
13//===----------------------------------------------------------------------===//
14
Argyrios Kyrtzidisd4d3cee2011-02-28 01:27:22 +000015#include "ClangSACheckers.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000016#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
Argyrios Kyrtzidis6a5674f2011-03-01 01:16:21 +000017#include "clang/StaticAnalyzer/Core/Checker.h"
Argyrios Kyrtzidisd4d3cee2011-02-28 01:27:22 +000018#include "clang/StaticAnalyzer/Core/CheckerManager.h"
19#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
Ted Kremenekf8cbac42011-02-10 01:03:03 +000020#include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
Benjamin Kramer49038022012-02-04 13:45:25 +000021#include "llvm/ADT/SmallString.h"
Benjamin Kramer444a1302012-12-01 17:12:56 +000022#include "llvm/Support/raw_ostream.h"
Zhongxing Xuc6123a12009-11-24 08:24:26 +000023
24using namespace clang;
Ted Kremenek98857c92010-12-23 07:20:52 +000025using namespace ento;
Zhongxing Xuc6123a12009-11-24 08:24:26 +000026
27namespace {
Ted Kremenek3a0678e2015-09-08 03:50:52 +000028class UndefResultChecker
Argyrios Kyrtzidis6a5674f2011-03-01 01:16:21 +000029 : public Checker< check::PostStmt<BinaryOperator> > {
Zhongxing Xuc6123a12009-11-24 08:24:26 +000030
Ahmed Charlesb8984322014-03-07 20:03:18 +000031 mutable std::unique_ptr<BugType> BT;
32
Zhongxing Xuc6123a12009-11-24 08:24:26 +000033public:
Argyrios Kyrtzidisd4d3cee2011-02-28 01:27:22 +000034 void checkPostStmt(const BinaryOperator *B, CheckerContext &C) const;
Zhongxing Xuc6123a12009-11-24 08:24:26 +000035};
36} // end anonymous namespace
37
Argyrios Kyrtzidisd4d3cee2011-02-28 01:27:22 +000038void UndefResultChecker::checkPostStmt(const BinaryOperator *B,
39 CheckerContext &C) const {
Ted Kremenek49b1e382012-01-26 21:29:00 +000040 ProgramStateRef state = C.getState();
Ted Kremenek632e3b72012-01-06 22:09:28 +000041 const LocationContext *LCtx = C.getLocationContext();
42 if (state->getSVal(B, LCtx).isUndef()) {
Anna Zaks03256462013-06-18 23:16:15 +000043
44 // Do not report assignments of uninitialized values inside swap functions.
45 // This should allow to swap partially uninitialized structs
46 // (radar://14129997)
47 if (const FunctionDecl *EnclosingFunctionDecl =
48 dyn_cast<FunctionDecl>(C.getStackFrame()->getDecl()))
49 if (C.getCalleeName(EnclosingFunctionDecl) == "swap")
50 return;
51
Zhongxing Xuc6123a12009-11-24 08:24:26 +000052 // Generate an error node.
Devin Coughline39bd402015-09-16 22:03:05 +000053 ExplodedNode *N = C.generateErrorNode();
Zhongxing Xuc6123a12009-11-24 08:24:26 +000054 if (!N)
55 return;
Ted Kremenek3a0678e2015-09-08 03:50:52 +000056
Zhongxing Xuc6123a12009-11-24 08:24:26 +000057 if (!BT)
Alexander Kornienko4aca9b12014-02-11 21:49:21 +000058 BT.reset(
59 new BuiltinBug(this, "Result of operation is garbage or undefined"));
Zhongxing Xuc6123a12009-11-24 08:24:26 +000060
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +000061 SmallString<256> sbuf;
Zhongxing Xuc6123a12009-11-24 08:24:26 +000062 llvm::raw_svector_ostream OS(sbuf);
Craig Topper0dbb7832014-05-27 02:45:47 +000063 const Expr *Ex = nullptr;
Zhongxing Xuc6123a12009-11-24 08:24:26 +000064 bool isLeft = true;
Ted Kremenek3a0678e2015-09-08 03:50:52 +000065
Ted Kremenek632e3b72012-01-06 22:09:28 +000066 if (state->getSVal(B->getLHS(), LCtx).isUndef()) {
Zhongxing Xuc6123a12009-11-24 08:24:26 +000067 Ex = B->getLHS()->IgnoreParenCasts();
68 isLeft = true;
69 }
Ted Kremenek632e3b72012-01-06 22:09:28 +000070 else if (state->getSVal(B->getRHS(), LCtx).isUndef()) {
Zhongxing Xuc6123a12009-11-24 08:24:26 +000071 Ex = B->getRHS()->IgnoreParenCasts();
72 isLeft = false;
73 }
Ted Kremenek3a0678e2015-09-08 03:50:52 +000074
Zhongxing Xuc6123a12009-11-24 08:24:26 +000075 if (Ex) {
76 OS << "The " << (isLeft ? "left" : "right")
77 << " operand of '"
78 << BinaryOperator::getOpcodeStr(B->getOpcode())
79 << "' is a garbage value";
Ted Kremenek3a0678e2015-09-08 03:50:52 +000080 }
Zhongxing Xuc6123a12009-11-24 08:24:26 +000081 else {
82 // Neither operand was undefined, but the result is undefined.
83 OS << "The result of the '"
84 << BinaryOperator::getOpcodeStr(B->getOpcode())
85 << "' expression is undefined";
86 }
Aaron Ballman8d3a7a52015-06-23 13:15:32 +000087 auto report = llvm::make_unique<BugReport>(*BT, OS.str(), N);
Ted Kremenek33e88a72009-11-29 06:37:44 +000088 if (Ex) {
89 report->addRange(Ex->getSourceRange());
Jordan Rosea0f7d352012-08-28 00:50:51 +000090 bugreporter::trackNullOrUndefValue(N, Ex, *report);
Ted Kremenek33e88a72009-11-29 06:37:44 +000091 }
Zhongxing Xuc6123a12009-11-24 08:24:26 +000092 else
Jordan Rosea0f7d352012-08-28 00:50:51 +000093 bugreporter::trackNullOrUndefValue(N, B, *report);
Ted Kremenek3a0678e2015-09-08 03:50:52 +000094
Aaron Ballman8d3a7a52015-06-23 13:15:32 +000095 C.emitReport(std::move(report));
Zhongxing Xuc6123a12009-11-24 08:24:26 +000096 }
97}
Argyrios Kyrtzidisd4d3cee2011-02-28 01:27:22 +000098
99void ento::registerUndefResultChecker(CheckerManager &mgr) {
100 mgr.registerChecker<UndefResultChecker>();
101}