Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/clang/lib/StaticAnalyzer/Checkers/ReturnValueChecker.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- ReturnValueChecker - Applies guaranteed return values ----*- 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 defines ReturnValueChecker, which checks for calls with guaranteed
10
// boolean return value. It ensures the return value of each function call.
11
//
12
//===----------------------------------------------------------------------===//
13
14
#include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
15
#include "clang/StaticAnalyzer/Core/Checker.h"
16
#include "clang/StaticAnalyzer/Core/CheckerManager.h"
17
#include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
18
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
19
#include "llvm/ADT/Optional.h"
20
#include "llvm/ADT/SmallVector.h"
21
22
using namespace clang;
23
using namespace ento;
24
25
namespace {
26
class ReturnValueChecker : public Checker<check::PostCall, check::EndFunction> {
27
public:
28
  // It sets the predefined invariant ('CDM') if the current call not break it.
29
  void checkPostCall(const CallEvent &Call, CheckerContext &C) const;
30
31
  // It reports whether a predefined invariant ('CDM') is broken.
32
  void checkEndFunction(const ReturnStmt *RS, CheckerContext &C) const;
33
34
private:
35
  // The pairs are in the following form: {{{class, call}}, return value}
36
  const CallDescriptionMap<bool> CDM = {
37
      // These are known in the LLVM project: 'Error()'
38
      {{{"ARMAsmParser", "Error"}}, true},
39
      {{{"HexagonAsmParser", "Error"}}, true},
40
      {{{"LLLexer", "Error"}}, true},
41
      {{{"LLParser", "Error"}}, true},
42
      {{{"MCAsmParser", "Error"}}, true},
43
      {{{"MCAsmParserExtension", "Error"}}, true},
44
      {{{"TGParser", "Error"}}, true},
45
      {{{"X86AsmParser", "Error"}}, true},
46
      // 'TokError()'
47
      {{{"LLParser", "TokError"}}, true},
48
      {{{"MCAsmParser", "TokError"}}, true},
49
      {{{"MCAsmParserExtension", "TokError"}}, true},
50
      {{{"TGParser", "TokError"}}, true},
51
      // 'error()'
52
      {{{"MIParser", "error"}}, true},
53
      {{{"WasmAsmParser", "error"}}, true},
54
      {{{"WebAssemblyAsmParser", "error"}}, true},
55
      // Other
56
      {{{"AsmParser", "printError"}}, true}};
57
};
58
} // namespace
59
60
4
static std::string getName(const CallEvent &Call) {
61
4
  std::string Name = "";
62
4
  if (const auto *MD = dyn_cast<CXXMethodDecl>(Call.getDecl()))
63
4
    if (const CXXRecordDecl *RD = MD->getParent())
64
4
      Name += RD->getNameAsString() + "::";
65
4
66
4
  Name += Call.getCalleeIdentifier()->getName();
67
4
  return Name;
68
4
}
69
70
// The predefinitions ('CDM') could break due to the ever growing code base.
71
// Check for the expected invariants and see whether they apply.
72
static Optional<bool> isInvariantBreak(bool ExpectedValue, SVal ReturnV,
73
6
                                       CheckerContext &C) {
74
6
  auto ReturnDV = ReturnV.getAs<DefinedOrUnknownSVal>();
75
6
  if (!ReturnDV)
76
0
    return None;
77
6
78
6
  if (ExpectedValue)
79
6
    return C.getState()->isNull(*ReturnDV).isConstrainedTrue();
80
0
81
0
  return C.getState()->isNull(*ReturnDV).isConstrainedFalse();
82
0
}
83
84
void ReturnValueChecker::checkPostCall(const CallEvent &Call,
85
162
                                       CheckerContext &C) const {
86
162
  const bool *RawExpectedValue = CDM.lookup(Call);
87
162
  if (!RawExpectedValue)
88
158
    return;
89
4
90
4
  SVal ReturnV = Call.getReturnValue();
91
4
  bool ExpectedValue = *RawExpectedValue;
92
4
  Optional<bool> IsInvariantBreak = isInvariantBreak(ExpectedValue, ReturnV, C);
93
4
  if (!IsInvariantBreak)
94
0
    return;
95
4
96
4
  // If the invariant is broken it is reported by 'checkEndFunction()'.
97
4
  if (*IsInvariantBreak)
98
2
    return;
99
2
100
2
  std::string Name = getName(Call);
101
2
  const NoteTag *CallTag = C.getNoteTag(
102
2
      [Name, ExpectedValue](BugReport &) -> std::string {
103
1
        SmallString<128> Msg;
104
1
        llvm::raw_svector_ostream Out(Msg);
105
1
106
1
        Out << '\'' << Name << "' returns "
107
1
            << (ExpectedValue ? "true" : 
"false"0
);
108
1
        return Out.str();
109
1
      },
110
2
      /*IsPrunable=*/true);
111
2
112
2
  ProgramStateRef State = C.getState();
113
2
  State = State->assume(ReturnV.castAs<DefinedOrUnknownSVal>(), ExpectedValue);
114
2
  C.addTransition(State, CallTag);
115
2
}
116
117
void ReturnValueChecker::checkEndFunction(const ReturnStmt *RS,
118
62
                                          CheckerContext &C) const {
119
62
  if (!RS || 
!RS->getRetValue()19
)
120
43
    return;
121
19
122
19
  // We cannot get the caller in the top-frame.
123
19
  const StackFrameContext *SFC = C.getStackFrame();
124
19
  if (C.getStackFrame()->inTopFrame())
125
13
    return;
126
6
127
6
  ProgramStateRef State = C.getState();
128
6
  CallEventManager &CMgr = C.getStateManager().getCallEventManager();
129
6
  CallEventRef<> Call = CMgr.getCaller(SFC, State);
130
6
  if (!Call)
131
0
    return;
132
6
133
6
  const bool *RawExpectedValue = CDM.lookup(*Call);
134
6
  if (!RawExpectedValue)
135
4
    return;
136
2
137
2
  SVal ReturnV = State->getSVal(RS->getRetValue(), C.getLocationContext());
138
2
  bool ExpectedValue = *RawExpectedValue;
139
2
  Optional<bool> IsInvariantBreak = isInvariantBreak(ExpectedValue, ReturnV, C);
140
2
  if (!IsInvariantBreak)
141
0
    return;
142
2
143
2
  // If the invariant is appropriate it is reported by 'checkPostCall()'.
144
2
  if (!*IsInvariantBreak)
145
0
    return;
146
2
147
2
  std::string Name = getName(*Call);
148
2
  const NoteTag *CallTag = C.getNoteTag(
149
2
      [Name, ExpectedValue](BugReport &BR) -> std::string {
150
1
        SmallString<128> Msg;
151
1
        llvm::raw_svector_ostream Out(Msg);
152
1
153
1
        // The following is swapped because the invariant is broken.
154
1
        Out << '\'' << Name << "' returns "
155
1
            << (ExpectedValue ? "false" : 
"true"0
);
156
1
157
1
        return Out.str();
158
1
      },
159
2
      /*IsPrunable=*/false);
160
2
161
2
  C.addTransition(State, CallTag);
162
2
}
163
164
11
void ento::registerReturnValueChecker(CheckerManager &Mgr) {
165
11
  Mgr.registerChecker<ReturnValueChecker>();
166
11
}
167
168
11
bool ento::shouldRegisterReturnValueChecker(const LangOptions &LO) {
169
11
  return true;
170
11
}