Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/clang/lib/StaticAnalyzer/Checkers/ReturnUndefChecker.cpp
Line
Count
Source (jump to first uncovered line)
1
//== ReturnUndefChecker.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 file defines ReturnUndefChecker, which is a path-sensitive
10
// check which looks for undefined or garbage values being returned to the
11
// caller.
12
//
13
//===----------------------------------------------------------------------===//
14
15
#include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.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/CallEvent.h"
20
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
21
22
using namespace clang;
23
using namespace ento;
24
25
namespace {
26
class ReturnUndefChecker : public Checker< check::PreStmt<ReturnStmt> > {
27
  mutable std::unique_ptr<BuiltinBug> BT_Undef;
28
  mutable std::unique_ptr<BuiltinBug> BT_NullReference;
29
30
  void emitUndef(CheckerContext &C, const Expr *RetE) const;
31
  void checkReference(CheckerContext &C, const Expr *RetE,
32
                      DefinedOrUnknownSVal RetVal) const;
33
public:
34
  void checkPreStmt(const ReturnStmt *RS, CheckerContext &C) const;
35
};
36
}
37
38
void ReturnUndefChecker::checkPreStmt(const ReturnStmt *RS,
39
20.7k
                                      CheckerContext &C) const {
40
20.7k
  const Expr *RetE = RS->getRetValue();
41
20.7k
  if (!RetE)
42
2.36k
    return;
43
18.3k
  SVal RetVal = C.getSVal(RetE);
44
18.3k
45
18.3k
  const StackFrameContext *SFC = C.getStackFrame();
46
18.3k
  QualType RT = CallEvent::getDeclaredResultType(SFC->getDecl());
47
18.3k
48
18.3k
  if (RetVal.isUndef()) {
49
53
    // "return;" is modeled to evaluate to an UndefinedVal. Allow UndefinedVal
50
53
    // to be returned in functions returning void to support this pattern:
51
53
    //   void foo() {
52
53
    //     return;
53
53
    //   }
54
53
    //   void test() {
55
53
    //     return foo();
56
53
    //   }
57
53
    if (!RT.isNull() && 
RT->isVoidType()52
)
58
2
      return;
59
51
60
51
    // Not all blocks have explicitly-specified return types; if the return type
61
51
    // is not available, but the return value expression has 'void' type, assume
62
51
    // Sema already checked it.
63
51
    if (RT.isNull() && 
isa<BlockDecl>(SFC->getDecl())1
&&
64
51
        
RetE->getType()->isVoidType()1
)
65
1
      return;
66
50
67
50
    emitUndef(C, RetE);
68
50
    return;
69
50
  }
70
18.3k
71
18.3k
  if (RT.isNull())
72
19
    return;
73
18.2k
74
18.2k
  if (RT->isReferenceType()) {
75
6.74k
    checkReference(C, RetE, RetVal.castAs<DefinedOrUnknownSVal>());
76
6.74k
    return;
77
6.74k
  }
78
18.2k
}
79
80
static void emitBug(CheckerContext &C, BuiltinBug &BT, const Expr *RetE,
81
55
                    const Expr *TrackingE = nullptr) {
82
55
  ExplodedNode *N = C.generateErrorNode();
83
55
  if (!N)
84
0
    return;
85
55
86
55
  auto Report = llvm::make_unique<BugReport>(BT, BT.getDescription(), N);
87
55
88
55
  Report->addRange(RetE->getSourceRange());
89
55
  bugreporter::trackExpressionValue(N, TrackingE ? 
TrackingE5
:
RetE50
, *Report);
90
55
91
55
  C.emitReport(std::move(Report));
92
55
}
93
94
50
void ReturnUndefChecker::emitUndef(CheckerContext &C, const Expr *RetE) const {
95
50
  if (!BT_Undef)
96
14
    BT_Undef.reset(
97
14
        new BuiltinBug(this, "Garbage return value",
98
14
                       "Undefined or garbage value returned to caller"));
99
50
  emitBug(C, *BT_Undef, RetE);
100
50
}
101
102
void ReturnUndefChecker::checkReference(CheckerContext &C, const Expr *RetE,
103
6.74k
                                        DefinedOrUnknownSVal RetVal) const {
104
6.74k
  ProgramStateRef StNonNull, StNull;
105
6.74k
  std::tie(StNonNull, StNull) = C.getState()->assume(RetVal);
106
6.74k
107
6.74k
  if (StNonNull) {
108
6.73k
    // Going forward, assume the location is non-null.
109
6.73k
    C.addTransition(StNonNull);
110
6.73k
    return;
111
6.73k
  }
112
5
113
5
  // The return value is known to be null. Emit a bug report.
114
5
  if (!BT_NullReference)
115
5
    BT_NullReference.reset(new BuiltinBug(this, "Returning null reference"));
116
5
117
5
  emitBug(C, *BT_NullReference, RetE, bugreporter::getDerefExpr(RetE));
118
5
}
119
120
676
void ento::registerReturnUndefChecker(CheckerManager &mgr) {
121
676
  mgr.registerChecker<ReturnUndefChecker>();
122
676
}
123
124
677
bool ento::shouldRegisterReturnUndefChecker(const LangOptions &LO) {
125
677
  return true;
126
677
}