Clang Project

clang_source_code/lib/StaticAnalyzer/Checkers/ReturnUndefChecker.cpp
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
22using namespace clang;
23using namespace ento;
24
25namespace {
26class ReturnUndefChecker : public Checker< check::PreStmt<ReturnStmt> > {
27  mutable std::unique_ptr<BuiltinBugBT_Undef;
28  mutable std::unique_ptr<BuiltinBugBT_NullReference;
29
30  void emitUndef(CheckerContext &Cconst Expr *RetEconst;
31  void checkReference(CheckerContext &Cconst Expr *RetE,
32                      DefinedOrUnknownSVal RetValconst;
33public:
34  void checkPreStmt(const ReturnStmt *RSCheckerContext &Cconst;
35};
36}
37
38void ReturnUndefChecker::checkPreStmt(const ReturnStmt *RS,
39                                      CheckerContext &Cconst {
40  const Expr *RetE = RS->getRetValue();
41  if (!RetE)
42    return;
43  SVal RetVal = C.getSVal(RetE);
44
45  const StackFrameContext *SFC = C.getStackFrame();
46  QualType RT = CallEvent::getDeclaredResultType(SFC->getDecl());
47
48  if (RetVal.isUndef()) {
49    // "return;" is modeled to evaluate to an UndefinedVal. Allow UndefinedVal
50    // to be returned in functions returning void to support this pattern:
51    //   void foo() {
52    //     return;
53    //   }
54    //   void test() {
55    //     return foo();
56    //   }
57    if (!RT.isNull() && RT->isVoidType())
58      return;
59
60    // Not all blocks have explicitly-specified return types; if the return type
61    // is not available, but the return value expression has 'void' type, assume
62    // Sema already checked it.
63    if (RT.isNull() && isa<BlockDecl>(SFC->getDecl()) &&
64        RetE->getType()->isVoidType())
65      return;
66
67    emitUndef(CRetE);
68    return;
69  }
70
71  if (RT.isNull())
72    return;
73
74  if (RT->isReferenceType()) {
75    checkReference(CRetERetVal.castAs<DefinedOrUnknownSVal>());
76    return;
77  }
78}
79
80static void emitBug(CheckerContext &CBuiltinBug &BTconst Expr *RetE,
81                    const Expr *TrackingE = nullptr) {
82  ExplodedNode *N = C.generateErrorNode();
83  if (!N)
84    return;
85
86  auto Report = llvm::make_unique<BugReport>(BT, BT.getDescription(), N);
87
88  Report->addRange(RetE->getSourceRange());
89  bugreporter::trackExpressionValue(N, TrackingE ? TrackingE : RetE, *Report);
90
91  C.emitReport(std::move(Report));
92}
93
94void ReturnUndefChecker::emitUndef(CheckerContext &Cconst Expr *RetEconst {
95  if (!BT_Undef)
96    BT_Undef.reset(
97        new BuiltinBug(this"Garbage return value",
98                       "Undefined or garbage value returned to caller"));
99  emitBug(C*BT_UndefRetE);
100}
101
102void ReturnUndefChecker::checkReference(CheckerContext &Cconst Expr *RetE,
103                                        DefinedOrUnknownSVal RetValconst {
104  ProgramStateRef StNonNullStNull;
105  std::tie(StNonNull, StNull) = C.getState()->assume(RetVal);
106
107  if (StNonNull) {
108    // Going forward, assume the location is non-null.
109    C.addTransition(StNonNull);
110    return;
111  }
112
113  // The return value is known to be null. Emit a bug report.
114  if (!BT_NullReference)
115    BT_NullReference.reset(new BuiltinBug(this"Returning null reference"));
116
117  emitBug(C*BT_NullReferenceRetEbugreporter::getDerefExpr(RetE));
118}
119
120void ento::registerReturnUndefChecker(CheckerManager &mgr) {
121  mgr.registerChecker<ReturnUndefChecker>();
122}
123
124bool ento::shouldRegisterReturnUndefChecker(const LangOptions &LO) {
125  return true;
126}
127