Coverage Report

Created: 2023-09-30 09:22

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/StaticAnalyzer/Checkers/ExprInspectionChecker.cpp
Line
Count
Source (jump to first uncovered line)
1
//==- ExprInspectionChecker.cpp - Used for regression tests ------*- 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
#include "clang/Analysis/IssueHash.h"
10
#include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
11
#include "clang/StaticAnalyzer/Checkers/SValExplainer.h"
12
#include "clang/StaticAnalyzer/Checkers/Taint.h"
13
#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
14
#include "clang/StaticAnalyzer/Core/Checker.h"
15
#include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
16
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
17
#include "clang/StaticAnalyzer/Core/PathSensitive/DynamicExtent.h"
18
#include "llvm/ADT/StringSwitch.h"
19
#include "llvm/Support/ScopedPrinter.h"
20
#include <optional>
21
22
using namespace clang;
23
using namespace ento;
24
25
namespace {
26
class ExprInspectionChecker
27
    : public Checker<eval::Call, check::DeadSymbols, check::EndAnalysis> {
28
  mutable std::unique_ptr<BugType> BT;
29
30
  // These stats are per-analysis, not per-branch, hence they shouldn't
31
  // stay inside the program state.
32
  struct ReachedStat {
33
    ExplodedNode *ExampleNode;
34
    unsigned NumTimesReached;
35
  };
36
  mutable llvm::DenseMap<const CallExpr *, ReachedStat> ReachedStats;
37
38
  void analyzerEval(const CallExpr *CE, CheckerContext &C) const;
39
  void analyzerCheckInlined(const CallExpr *CE, CheckerContext &C) const;
40
  void analyzerWarnIfReached(const CallExpr *CE, CheckerContext &C) const;
41
  void analyzerNumTimesReached(const CallExpr *CE, CheckerContext &C) const;
42
  void analyzerCrash(const CallExpr *CE, CheckerContext &C) const;
43
  void analyzerWarnOnDeadSymbol(const CallExpr *CE, CheckerContext &C) const;
44
  void analyzerValue(const CallExpr *CE, CheckerContext &C) const;
45
  void analyzerDumpSValType(const CallExpr *CE, CheckerContext &C) const;
46
  void analyzerDump(const CallExpr *CE, CheckerContext &C) const;
47
  void analyzerExplain(const CallExpr *CE, CheckerContext &C) const;
48
  void analyzerPrintState(const CallExpr *CE, CheckerContext &C) const;
49
  void analyzerGetExtent(const CallExpr *CE, CheckerContext &C) const;
50
  void analyzerDumpExtent(const CallExpr *CE, CheckerContext &C) const;
51
  void analyzerDumpElementCount(const CallExpr *CE, CheckerContext &C) const;
52
  void analyzerHashDump(const CallExpr *CE, CheckerContext &C) const;
53
  void analyzerDenote(const CallExpr *CE, CheckerContext &C) const;
54
  void analyzerExpress(const CallExpr *CE, CheckerContext &C) const;
55
  void analyzerIsTainted(const CallExpr *CE, CheckerContext &C) const;
56
57
  typedef void (ExprInspectionChecker::*FnCheck)(const CallExpr *,
58
                                                 CheckerContext &C) const;
59
60
  // Optional parameter `ExprVal` for expression value to be marked interesting.
61
  ExplodedNode *reportBug(llvm::StringRef Msg, CheckerContext &C,
62
                          std::optional<SVal> ExprVal = std::nullopt) const;
63
  ExplodedNode *reportBug(llvm::StringRef Msg, BugReporter &BR, ExplodedNode *N,
64
                          std::optional<SVal> ExprVal = std::nullopt) const;
65
  template <typename T> void printAndReport(CheckerContext &C, T What) const;
66
67
  const Expr *getArgExpr(const CallExpr *CE, CheckerContext &C) const;
68
  const MemRegion *getArgRegion(const CallExpr *CE, CheckerContext &C) const;
69
70
public:
71
  bool evalCall(const CallEvent &Call, CheckerContext &C) const;
72
  void checkDeadSymbols(SymbolReaper &SymReaper, CheckerContext &C) const;
73
  void checkEndAnalysis(ExplodedGraph &G, BugReporter &BR,
74
                        ExprEngine &Eng) const;
75
};
76
} // namespace
77
78
REGISTER_SET_WITH_PROGRAMSTATE(MarkedSymbols, SymbolRef)
79
REGISTER_MAP_WITH_PROGRAMSTATE(DenotedSymbols, SymbolRef, const StringLiteral *)
80
81
bool ExprInspectionChecker::evalCall(const CallEvent &Call,
82
63.2k
                                     CheckerContext &C) const {
83
63.2k
  const auto *CE = dyn_cast_or_null<CallExpr>(Call.getOriginExpr());
84
63.2k
  if (!CE)
85
8.97k
    return false;
86
87
  // These checks should have no effect on the surrounding environment
88
  // (globals should not be invalidated, etc), hence the use of evalCall.
89
54.2k
  FnCheck Handler =
90
54.2k
      llvm::StringSwitch<FnCheck>(C.getCalleeName(CE))
91
54.2k
          .Case("clang_analyzer_eval", &ExprInspectionChecker::analyzerEval)
92
54.2k
          .Case("clang_analyzer_checkInlined",
93
54.2k
                &ExprInspectionChecker::analyzerCheckInlined)
94
54.2k
          .Case("clang_analyzer_crash", &ExprInspectionChecker::analyzerCrash)
95
54.2k
          .Case("clang_analyzer_warnIfReached",
96
54.2k
                &ExprInspectionChecker::analyzerWarnIfReached)
97
54.2k
          .Case("clang_analyzer_warnOnDeadSymbol",
98
54.2k
                &ExprInspectionChecker::analyzerWarnOnDeadSymbol)
99
54.2k
          .StartsWith("clang_analyzer_explain",
100
54.2k
                      &ExprInspectionChecker::analyzerExplain)
101
54.2k
          .Case("clang_analyzer_dumpExtent",
102
54.2k
                &ExprInspectionChecker::analyzerDumpExtent)
103
54.2k
          .Case("clang_analyzer_dumpElementCount",
104
54.2k
                &ExprInspectionChecker::analyzerDumpElementCount)
105
54.2k
          .Case("clang_analyzer_value", &ExprInspectionChecker::analyzerValue)
106
54.2k
          .StartsWith("clang_analyzer_dumpSvalType",
107
54.2k
                      &ExprInspectionChecker::analyzerDumpSValType)
108
54.2k
          .StartsWith("clang_analyzer_dump",
109
54.2k
                      &ExprInspectionChecker::analyzerDump)
110
54.2k
          .Case("clang_analyzer_getExtent",
111
54.2k
                &ExprInspectionChecker::analyzerGetExtent)
112
54.2k
          .Case("clang_analyzer_printState",
113
54.2k
                &ExprInspectionChecker::analyzerPrintState)
114
54.2k
          .Case("clang_analyzer_numTimesReached",
115
54.2k
                &ExprInspectionChecker::analyzerNumTimesReached)
116
54.2k
          .Case("clang_analyzer_hashDump",
117
54.2k
                &ExprInspectionChecker::analyzerHashDump)
118
54.2k
          .Case("clang_analyzer_denote", &ExprInspectionChecker::analyzerDenote)
119
54.2k
          .Case("clang_analyzer_express", // This also marks the argument as
120
                                          // interesting.
121
54.2k
                &ExprInspectionChecker::analyzerExpress)
122
54.2k
          .StartsWith("clang_analyzer_isTainted",
123
54.2k
                      &ExprInspectionChecker::analyzerIsTainted)
124
54.2k
          .Default(nullptr);
125
126
54.2k
  if (!Handler)
127
34.3k
    return false;
128
129
19.8k
  (this->*Handler)(CE, C);
130
19.8k
  return true;
131
54.2k
}
132
133
static const char *getArgumentValueString(const CallExpr *CE,
134
10.3k
                                          CheckerContext &C) {
135
10.3k
  if (CE->getNumArgs() == 0)
136
0
    return "Missing assertion argument";
137
138
10.3k
  ExplodedNode *N = C.getPredecessor();
139
10.3k
  const LocationContext *LC = N->getLocationContext();
140
10.3k
  ProgramStateRef State = N->getState();
141
142
10.3k
  const Expr *Assertion = CE->getArg(0);
143
10.3k
  SVal AssertionVal = State->getSVal(Assertion, LC);
144
145
10.3k
  if (AssertionVal.isUndef())
146
11
    return "UNDEFINED";
147
148
10.3k
  ProgramStateRef StTrue, StFalse;
149
10.3k
  std::tie(StTrue, StFalse) =
150
10.3k
      State->assume(AssertionVal.castAs<DefinedOrUnknownSVal>());
151
152
10.3k
  if (StTrue) {
153
8.59k
    if (StFalse)
154
2.11k
      return "UNKNOWN";
155
6.47k
    else
156
6.47k
      return "TRUE";
157
8.59k
  } else {
158
1.75k
    if (StFalse)
159
1.75k
      return "FALSE";
160
0
    else
161
0
      llvm_unreachable("Invalid constraint; neither true or false.");
162
1.75k
  }
163
10.3k
}
164
165
ExplodedNode *
166
ExprInspectionChecker::reportBug(llvm::StringRef Msg, CheckerContext &C,
167
16.1k
                                 std::optional<SVal> ExprVal) const {
168
16.1k
  ExplodedNode *N = C.generateNonFatalErrorNode();
169
16.1k
  reportBug(Msg, C.getBugReporter(), N, ExprVal);
170
16.1k
  return N;
171
16.1k
}
172
173
ExplodedNode *
174
ExprInspectionChecker::reportBug(llvm::StringRef Msg, BugReporter &BR,
175
                                 ExplodedNode *N,
176
16.3k
                                 std::optional<SVal> ExprVal) const {
177
16.3k
  if (!N)
178
0
    return nullptr;
179
180
16.3k
  if (!BT)
181
357
    BT.reset(new BugType(this, "Checking analyzer assumptions", "debug"));
182
183
16.3k
  auto R = std::make_unique<PathSensitiveBugReport>(*BT, Msg, N);
184
16.3k
  if (ExprVal) {
185
1.46k
    R->markInteresting(*ExprVal);
186
1.46k
  }
187
16.3k
  BR.emitReport(std::move(R));
188
16.3k
  return N;
189
16.3k
}
190
191
const Expr *ExprInspectionChecker::getArgExpr(const CallExpr *CE,
192
5.47k
                                              CheckerContext &C) const {
193
5.47k
  if (CE->getNumArgs() == 0) {
194
3
    reportBug("Missing argument", C);
195
3
    return nullptr;
196
3
  }
197
5.47k
  return CE->getArg(0);
198
5.47k
}
199
200
const MemRegion *ExprInspectionChecker::getArgRegion(const CallExpr *CE,
201
23
                                                     CheckerContext &C) const {
202
23
  const Expr *Arg = getArgExpr(CE, C);
203
23
  if (!Arg)
204
0
    return nullptr;
205
206
23
  const MemRegion *MR = C.getSVal(Arg).getAsRegion();
207
23
  if (!MR) {
208
0
    reportBug("Cannot obtain the region", C);
209
0
    return nullptr;
210
0
  }
211
212
23
  return MR;
213
23
}
214
215
void ExprInspectionChecker::analyzerEval(const CallExpr *CE,
216
10.2k
                                         CheckerContext &C) const {
217
10.2k
  const LocationContext *LC = C.getPredecessor()->getLocationContext();
218
219
  // A specific instantiation of an inlined function may have more constrained
220
  // values than can generally be assumed. Skip the check.
221
10.2k
  if (LC->getStackFrame()->getParent() != nullptr)
222
7
    return;
223
224
10.2k
  reportBug(getArgumentValueString(CE, C), C);
225
10.2k
}
226
227
void ExprInspectionChecker::analyzerWarnIfReached(const CallExpr *CE,
228
216
                                                  CheckerContext &C) const {
229
216
  reportBug("REACHABLE", C);
230
216
}
231
232
void ExprInspectionChecker::analyzerNumTimesReached(const CallExpr *CE,
233
1.84k
                                                    CheckerContext &C) const {
234
1.84k
  ++ReachedStats[CE].NumTimesReached;
235
1.84k
  if (!ReachedStats[CE].ExampleNode) {
236
    // Later, in checkEndAnalysis, we'd throw a report against it.
237
147
    ReachedStats[CE].ExampleNode = C.generateNonFatalErrorNode();
238
147
  }
239
1.84k
}
240
241
void ExprInspectionChecker::analyzerCheckInlined(const CallExpr *CE,
242
137
                                                 CheckerContext &C) const {
243
137
  const LocationContext *LC = C.getPredecessor()->getLocationContext();
244
245
  // An inlined function could conceivably also be analyzed as a top-level
246
  // function. We ignore this case and only emit a message (TRUE or FALSE)
247
  // when we are analyzing it as an inlined function. This means that
248
  // clang_analyzer_checkInlined(true) should always print TRUE, but
249
  // clang_analyzer_checkInlined(false) should never actually print anything.
250
137
  if (LC->getStackFrame()->getParent() == nullptr)
251
14
    return;
252
253
123
  reportBug(getArgumentValueString(CE, C), C);
254
123
}
255
256
void ExprInspectionChecker::analyzerExplain(const CallExpr *CE,
257
62
                                            CheckerContext &C) const {
258
62
  const Expr *Arg = getArgExpr(CE, C);
259
62
  if (!Arg)
260
0
    return;
261
262
62
  SVal V = C.getSVal(Arg);
263
62
  SValExplainer Ex(C.getASTContext());
264
62
  reportBug(Ex.Visit(V), C);
265
62
}
266
267
static void printHelper(llvm::raw_svector_ostream &Out, CheckerContext &C,
268
5
                        const llvm::APSInt &I) {
269
5
  Out << I.getBitWidth() << (I.isUnsigned() ? 
"u:"1
:
"s:"4
);
270
5
  Out << I;
271
5
}
272
273
static void printHelper(llvm::raw_svector_ostream &Out, CheckerContext &C,
274
19
                        SymbolRef Sym) {
275
19
  C.getConstraintManager().printValue(Out, C.getState(), Sym);
276
19
}
277
278
static void printHelper(llvm::raw_svector_ostream &Out, CheckerContext &C,
279
3.59k
                        SVal V) {
280
3.59k
  Out << V;
281
3.59k
}
282
283
template <typename T>
284
3.62k
void ExprInspectionChecker::printAndReport(CheckerContext &C, T What) const {
285
3.62k
  llvm::SmallString<64> Str;
286
3.62k
  llvm::raw_svector_ostream OS(Str);
287
3.62k
  printHelper(OS, C, What);
288
3.62k
  reportBug(OS.str(), C);
289
3.62k
}
ExprInspectionChecker.cpp:void (anonymous namespace)::ExprInspectionChecker::printAndReport<clang::ento::SVal>(clang::ento::CheckerContext&, clang::ento::SVal) const
Line
Count
Source
284
3.57k
void ExprInspectionChecker::printAndReport(CheckerContext &C, T What) const {
285
3.57k
  llvm::SmallString<64> Str;
286
3.57k
  llvm::raw_svector_ostream OS(Str);
287
3.57k
  printHelper(OS, C, What);
288
3.57k
  reportBug(OS.str(), C);
289
3.57k
}
ExprInspectionChecker.cpp:void (anonymous namespace)::ExprInspectionChecker::printAndReport<clang::ento::DefinedOrUnknownSVal>(clang::ento::CheckerContext&, clang::ento::DefinedOrUnknownSVal) const
Line
Count
Source
284
23
void ExprInspectionChecker::printAndReport(CheckerContext &C, T What) const {
285
23
  llvm::SmallString<64> Str;
286
23
  llvm::raw_svector_ostream OS(Str);
287
23
  printHelper(OS, C, What);
288
23
  reportBug(OS.str(), C);
289
23
}
ExprInspectionChecker.cpp:void (anonymous namespace)::ExprInspectionChecker::printAndReport<clang::ento::SymExpr const*>(clang::ento::CheckerContext&, clang::ento::SymExpr const*) const
Line
Count
Source
284
19
void ExprInspectionChecker::printAndReport(CheckerContext &C, T What) const {
285
19
  llvm::SmallString<64> Str;
286
19
  llvm::raw_svector_ostream OS(Str);
287
19
  printHelper(OS, C, What);
288
19
  reportBug(OS.str(), C);
289
19
}
ExprInspectionChecker.cpp:void (anonymous namespace)::ExprInspectionChecker::printAndReport<llvm::APSInt>(clang::ento::CheckerContext&, llvm::APSInt) const
Line
Count
Source
284
5
void ExprInspectionChecker::printAndReport(CheckerContext &C, T What) const {
285
5
  llvm::SmallString<64> Str;
286
5
  llvm::raw_svector_ostream OS(Str);
287
5
  printHelper(OS, C, What);
288
5
  reportBug(OS.str(), C);
289
5
}
290
291
void ExprInspectionChecker::analyzerValue(const CallExpr *CE,
292
26
                                          CheckerContext &C) const {
293
26
  const Expr *Arg = getArgExpr(CE, C);
294
26
  if (!Arg)
295
1
    return;
296
297
25
  SVal V = C.getSVal(Arg);
298
25
  if (const SymbolRef Sym = V.getAsSymbol())
299
19
    printAndReport(C, Sym);
300
6
  else if (const llvm::APSInt *I = V.getAsInteger())
301
5
    printAndReport(C, *I);
302
1
  else
303
1
    reportBug("n/a", C);
304
25
}
305
306
void ExprInspectionChecker::analyzerDumpSValType(const CallExpr *CE,
307
2
                                                 CheckerContext &C) const {
308
2
  const Expr *Arg = getArgExpr(CE, C);
309
2
  if (!Arg)
310
0
    return;
311
312
2
  QualType Ty = C.getSVal(Arg).getType(C.getASTContext());
313
2
  reportBug(Ty.getAsString(), C);
314
2
}
315
316
void ExprInspectionChecker::analyzerDump(const CallExpr *CE,
317
3.55k
                                         CheckerContext &C) const {
318
3.55k
  const Expr *Arg = getArgExpr(CE, C);
319
3.55k
  if (!Arg)
320
0
    return;
321
322
3.55k
  SVal V = C.getSVal(Arg);
323
3.55k
  printAndReport(C, V);
324
3.55k
}
325
326
void ExprInspectionChecker::analyzerGetExtent(const CallExpr *CE,
327
278
                                              CheckerContext &C) const {
328
278
  const Expr *Arg = getArgExpr(CE, C);
329
278
  if (!Arg)
330
0
    return;
331
332
278
  ProgramStateRef State = C.getState();
333
278
  SVal Size = getDynamicExtentWithOffset(State, C.getSVal(Arg));
334
335
278
  State = State->BindExpr(CE, C.getLocationContext(), Size);
336
278
  C.addTransition(State);
337
278
}
338
339
void ExprInspectionChecker::analyzerDumpExtent(const CallExpr *CE,
340
22
                                               CheckerContext &C) const {
341
22
  const Expr *Arg = getArgExpr(CE, C);
342
22
  if (!Arg)
343
0
    return;
344
345
22
  ProgramStateRef State = C.getState();
346
22
  SVal Size = getDynamicExtentWithOffset(State, C.getSVal(Arg));
347
22
  printAndReport(C, Size);
348
22
}
349
350
void ExprInspectionChecker::analyzerDumpElementCount(const CallExpr *CE,
351
23
                                                     CheckerContext &C) const {
352
23
  const MemRegion *MR = getArgRegion(CE, C);
353
23
  if (!MR)
354
0
    return;
355
356
23
  QualType ElementTy;
357
23
  if (const auto *TVR = MR->getAs<TypedValueRegion>()) {
358
21
    ElementTy = TVR->getValueType();
359
21
  } else {
360
2
    ElementTy = MR->castAs<SymbolicRegion>()->getPointeeStaticType();
361
2
  }
362
363
23
  assert(!ElementTy->isPointerType());
364
365
23
  DefinedOrUnknownSVal ElementCount = getDynamicElementCountWithOffset(
366
23
      C.getState(), C.getSVal(getArgExpr(CE, C)), ElementTy);
367
23
  printAndReport(C, ElementCount);
368
23
}
369
370
void ExprInspectionChecker::analyzerPrintState(const CallExpr *CE,
371
58
                                               CheckerContext &C) const {
372
58
  C.getState()->dump();
373
58
}
374
375
void ExprInspectionChecker::analyzerWarnOnDeadSymbol(const CallExpr *CE,
376
21
                                                     CheckerContext &C) const {
377
21
  const Expr *Arg = getArgExpr(CE, C);
378
21
  if (!Arg)
379
0
    return;
380
381
21
  SVal Val = C.getSVal(Arg);
382
21
  SymbolRef Sym = Val.getAsSymbol();
383
21
  if (!Sym)
384
0
    return;
385
386
21
  ProgramStateRef State = C.getState();
387
21
  State = State->add<MarkedSymbols>(Sym);
388
21
  C.addTransition(State);
389
21
}
390
391
void ExprInspectionChecker::checkDeadSymbols(SymbolReaper &SymReaper,
392
238k
                                             CheckerContext &C) const {
393
238k
  ProgramStateRef State = C.getState();
394
238k
  const MarkedSymbolsTy &Syms = State->get<MarkedSymbols>();
395
238k
  ExplodedNode *N = C.getPredecessor();
396
238k
  for (SymbolRef Sym : Syms) {
397
96
    if (!SymReaper.isDead(Sym))
398
81
      continue;
399
400
    // The non-fatal error node should be the same for all reports.
401
15
    if (ExplodedNode *BugNode = reportBug("SYMBOL DEAD", C))
402
15
      N = BugNode;
403
15
    State = State->remove<MarkedSymbols>(Sym);
404
15
  }
405
406
238k
  for (auto I : State->get<DenotedSymbols>()) {
407
14.5k
    SymbolRef Sym = I.first;
408
14.5k
    if (!SymReaper.isLive(Sym))
409
712
      State = State->remove<DenotedSymbols>(Sym);
410
14.5k
  }
411
412
238k
  C.addTransition(State, N);
413
238k
}
414
415
void ExprInspectionChecker::checkEndAnalysis(ExplodedGraph &G, BugReporter &BR,
416
8.95k
                                             ExprEngine &Eng) const {
417
8.95k
  for (auto Item : ReachedStats) {
418
147
    unsigned NumTimesReached = Item.second.NumTimesReached;
419
147
    ExplodedNode *N = Item.second.ExampleNode;
420
421
147
    reportBug(llvm::to_string(NumTimesReached), BR, N);
422
147
  }
423
8.95k
  ReachedStats.clear();
424
8.95k
}
425
426
void ExprInspectionChecker::analyzerCrash(const CallExpr *CE,
427
0
                                          CheckerContext &C) const {
428
0
  LLVM_BUILTIN_TRAP;
429
0
}
430
431
void ExprInspectionChecker::analyzerHashDump(const CallExpr *CE,
432
21
                                             CheckerContext &C) const {
433
21
  const LangOptions &Opts = C.getLangOpts();
434
21
  const SourceManager &SM = C.getSourceManager();
435
21
  FullSourceLoc FL(CE->getArg(0)->getBeginLoc(), SM);
436
21
  std::string HashContent =
437
21
      getIssueString(FL, getCheckerName().getName(), "Category",
438
21
                     C.getLocationContext()->getDecl(), Opts);
439
440
21
  reportBug(HashContent, C);
441
21
}
442
443
void ExprInspectionChecker::analyzerDenote(const CallExpr *CE,
444
1.47k
                                           CheckerContext &C) const {
445
1.47k
  if (CE->getNumArgs() < 2) {
446
4
    reportBug("clang_analyzer_denote() requires a symbol and a string literal",
447
4
              C);
448
4
    return;
449
4
  }
450
451
1.47k
  SymbolRef Sym = C.getSVal(CE->getArg(0)).getAsSymbol();
452
1.47k
  if (!Sym) {
453
2
    reportBug("Not a symbol", C);
454
2
    return;
455
2
  }
456
457
1.46k
  const auto *E = dyn_cast<StringLiteral>(CE->getArg(1)->IgnoreParenCasts());
458
1.46k
  if (!E) {
459
0
    reportBug("Not a string literal", C);
460
0
    return;
461
0
  }
462
463
1.46k
  ProgramStateRef State = C.getState();
464
465
1.46k
  C.addTransition(C.getState()->set<DenotedSymbols>(Sym, E));
466
1.46k
}
467
468
namespace {
469
class SymbolExpressor
470
    : public SymExprVisitor<SymbolExpressor, std::optional<std::string>> {
471
  ProgramStateRef State;
472
473
public:
474
1.46k
  SymbolExpressor(ProgramStateRef State) : State(State) {}
475
476
2.32k
  std::optional<std::string> lookup(const SymExpr *S) {
477
2.32k
    if (const StringLiteral *const *SLPtr = State->get<DenotedSymbols>(S)) {
478
1.35k
      const StringLiteral *SL = *SLPtr;
479
1.35k
      return std::string(SL->getBytes());
480
1.35k
    }
481
973
    return std::nullopt;
482
2.32k
  }
483
484
1.54k
  std::optional<std::string> VisitSymExpr(const SymExpr *S) {
485
1.54k
    return lookup(S);
486
1.54k
  }
487
488
646
  std::optional<std::string> VisitSymIntExpr(const SymIntExpr *S) {
489
646
    if (std::optional<std::string> Str = lookup(S))
490
2
      return Str;
491
644
    if (std::optional<std::string> Str = Visit(S->getLHS()))
492
644
      return (*Str + " " + BinaryOperator::getOpcodeStr(S->getOpcode()) + " " +
493
644
              std::to_string(S->getRHS().getLimitedValue()) +
494
644
              (S->getRHS().isUnsigned() ? 
"U"88
:
""556
))
495
644
          .str();
496
0
    return std::nullopt;
497
644
  }
498
499
109
  std::optional<std::string> VisitSymSymExpr(const SymSymExpr *S) {
500
109
    if (std::optional<std::string> Str = lookup(S))
501
0
      return Str;
502
109
    if (std::optional<std::string> Str1 = Visit(S->getLHS()))
503
109
      if (std::optional<std::string> Str2 = Visit(S->getRHS()))
504
109
        return (*Str1 + " " + BinaryOperator::getOpcodeStr(S->getOpcode()) +
505
109
                " " + *Str2)
506
109
            .str();
507
0
    return std::nullopt;
508
109
  }
509
510
2
  std::optional<std::string> VisitUnarySymExpr(const UnarySymExpr *S) {
511
2
    if (std::optional<std::string> Str = lookup(S))
512
0
      return Str;
513
2
    if (std::optional<std::string> Str = Visit(S->getOperand()))
514
2
      return (UnaryOperator::getOpcodeStr(S->getOpcode()) + *Str).str();
515
0
    return std::nullopt;
516
2
  }
517
518
28
  std::optional<std::string> VisitSymbolCast(const SymbolCast *S) {
519
28
    if (std::optional<std::string> Str = lookup(S))
520
27
      return Str;
521
1
    if (std::optional<std::string> Str = Visit(S->getOperand()))
522
1
      return (Twine("(") + S->getType().getAsString() + ")" + *Str).str();
523
0
    return std::nullopt;
524
1
  }
525
};
526
} // namespace
527
528
void ExprInspectionChecker::analyzerExpress(const CallExpr *CE,
529
1.46k
                                            CheckerContext &C) const {
530
1.46k
  const Expr *Arg = getArgExpr(CE, C);
531
1.46k
  if (!Arg)
532
2
    return;
533
534
1.46k
  SVal ArgVal = C.getSVal(CE->getArg(0));
535
1.46k
  SymbolRef Sym = ArgVal.getAsSymbol();
536
1.46k
  if (!Sym) {
537
2
    reportBug("Not a symbol", C, ArgVal);
538
2
    return;
539
2
  }
540
541
1.46k
  SymbolExpressor V(C.getState());
542
1.46k
  auto Str = V.Visit(Sym);
543
1.46k
  if (!Str) {
544
217
    reportBug("Unable to express", C, ArgVal);
545
217
    return;
546
217
  }
547
548
1.24k
  reportBug(*Str, C, ArgVal);
549
1.24k
}
550
551
void ExprInspectionChecker::analyzerIsTainted(const CallExpr *CE,
552
430
                                              CheckerContext &C) const {
553
430
  if (CE->getNumArgs() != 1) {
554
1
    reportBug("clang_analyzer_isTainted() requires exactly one argument", C);
555
1
    return;
556
1
  }
557
429
  const bool IsTainted =
558
429
      taint::isTainted(C.getState(), CE->getArg(0), C.getLocationContext());
559
429
  reportBug(IsTainted ? 
"YES"397
:
"NO"32
, C);
560
429
}
561
562
442
void ento::registerExprInspectionChecker(CheckerManager &Mgr) {
563
442
  Mgr.registerChecker<ExprInspectionChecker>();
564
442
}
565
566
884
bool ento::shouldRegisterExprInspectionChecker(const CheckerManager &mgr) {
567
884
  return true;
568
884
}