18#include "llvm/ADT/StringSwitch.h"
19#include "llvm/Support/ScopedPrinter.h"
26class ExprInspectionChecker
27 :
public Checker<eval::Call, check::DeadSymbols, check::EndAnalysis> {
28 const BugType BT{
this,
"Checking analyzer assumptions",
"debug"};
34 unsigned NumTimesReached;
36 mutable llvm::DenseMap<const CallExpr *, ReachedStat> ReachedStats;
57 typedef void (ExprInspectionChecker::*FnCheck)(
const CallExpr *,
62 std::optional<SVal> ExprVal = std::nullopt)
const;
64 std::optional<SVal> ExprVal = std::nullopt)
const;
83 const auto *CE = dyn_cast_or_null<CallExpr>(
Call.getOriginExpr());
90 llvm::StringSwitch<FnCheck>(
C.getCalleeName(CE))
91 .Case(
"clang_analyzer_eval", &ExprInspectionChecker::analyzerEval)
92 .Case(
"clang_analyzer_checkInlined",
93 &ExprInspectionChecker::analyzerCheckInlined)
94 .Case(
"clang_analyzer_crash", &ExprInspectionChecker::analyzerCrash)
95 .Case(
"clang_analyzer_warnIfReached",
96 &ExprInspectionChecker::analyzerWarnIfReached)
97 .Case(
"clang_analyzer_warnOnDeadSymbol",
98 &ExprInspectionChecker::analyzerWarnOnDeadSymbol)
99 .StartsWith(
"clang_analyzer_explain",
100 &ExprInspectionChecker::analyzerExplain)
101 .Case(
"clang_analyzer_dumpExtent",
102 &ExprInspectionChecker::analyzerDumpExtent)
103 .Case(
"clang_analyzer_dumpElementCount",
104 &ExprInspectionChecker::analyzerDumpElementCount)
105 .Case(
"clang_analyzer_value", &ExprInspectionChecker::analyzerValue)
106 .StartsWith(
"clang_analyzer_dumpSvalType",
107 &ExprInspectionChecker::analyzerDumpSValType)
108 .StartsWith(
"clang_analyzer_dump",
109 &ExprInspectionChecker::analyzerDump)
110 .Case(
"clang_analyzer_getExtent",
111 &ExprInspectionChecker::analyzerGetExtent)
112 .Case(
"clang_analyzer_printState",
113 &ExprInspectionChecker::analyzerPrintState)
114 .Case(
"clang_analyzer_numTimesReached",
115 &ExprInspectionChecker::analyzerNumTimesReached)
116 .Case(
"clang_analyzer_hashDump",
117 &ExprInspectionChecker::analyzerHashDump)
118 .Case(
"clang_analyzer_denote", &ExprInspectionChecker::analyzerDenote)
119 .Case(
"clang_analyzer_express",
121 &ExprInspectionChecker::analyzerExpress)
122 .StartsWith(
"clang_analyzer_isTainted",
123 &ExprInspectionChecker::analyzerIsTainted)
129 (this->*Handler)(CE,
C);
136 return "Missing assertion argument";
143 SVal AssertionVal = State->getSVal(Assertion, LC);
149 std::tie(StTrue, StFalse) =
161 llvm_unreachable(
"Invalid constraint; neither true or false.");
166ExprInspectionChecker::reportBug(llvm::StringRef Msg,
CheckerContext &
C,
167 std::optional<SVal> ExprVal)
const {
169 reportBug(Msg,
C.getBugReporter(), N, ExprVal);
174ExprInspectionChecker::reportBug(llvm::StringRef Msg,
BugReporter &BR,
176 std::optional<SVal> ExprVal)
const {
179 auto R = std::make_unique<PathSensitiveBugReport>(BT, Msg, N);
181 R->markInteresting(*ExprVal);
187const Expr *ExprInspectionChecker::getArgExpr(
const CallExpr *CE,
190 reportBug(
"Missing argument",
C);
198 const Expr *Arg = getArgExpr(CE,
C);
202 const MemRegion *MR =
C.getSVal(Arg).getAsRegion();
204 reportBug(
"Cannot obtain the region",
C);
211void ExprInspectionChecker::analyzerEval(
const CallExpr *CE,
223void ExprInspectionChecker::analyzerWarnIfReached(
const CallExpr *CE,
225 reportBug(
"REACHABLE",
C);
228void ExprInspectionChecker::analyzerNumTimesReached(
const CallExpr *CE,
230 ++ReachedStats[CE].NumTimesReached;
231 if (!ReachedStats[CE].ExampleNode) {
233 ReachedStats[CE].ExampleNode =
C.generateNonFatalErrorNode();
237void ExprInspectionChecker::analyzerCheckInlined(
const CallExpr *CE,
252void ExprInspectionChecker::analyzerExplain(
const CallExpr *CE,
254 const Expr *Arg = getArgExpr(CE,
C);
260 reportBug(Ex.Visit(
V),
C);
264 const llvm::APSInt &I) {
265 Out << I.getBitWidth() << (I.isUnsigned() ?
"u:" :
"s:");
271 C.getConstraintManager().printValue(Out,
C.getState(), Sym);
280void ExprInspectionChecker::printAndReport(
CheckerContext &
C,
T What)
const {
282 llvm::raw_svector_ostream OS(Str);
284 reportBug(OS.str(),
C);
287void ExprInspectionChecker::analyzerValue(
const CallExpr *CE,
289 const Expr *Arg = getArgExpr(CE,
C);
295 printAndReport(
C, Sym);
296 else if (
const llvm::APSInt *I =
V.getAsInteger())
297 printAndReport(
C, *I);
302void ExprInspectionChecker::analyzerDumpSValType(
const CallExpr *CE,
304 const Expr *Arg = getArgExpr(CE,
C);
308 QualType Ty =
C.getSVal(Arg).getType(
C.getASTContext());
312void ExprInspectionChecker::analyzerDump(
const CallExpr *CE,
314 const Expr *Arg = getArgExpr(CE,
C);
319 printAndReport(
C,
V);
322void ExprInspectionChecker::analyzerGetExtent(
const CallExpr *CE,
324 const Expr *Arg = getArgExpr(CE,
C);
331 State = State->BindExpr(CE,
C.getLocationContext(), Size);
332 C.addTransition(State);
335void ExprInspectionChecker::analyzerDumpExtent(
const CallExpr *CE,
337 const Expr *Arg = getArgExpr(CE,
C);
343 printAndReport(
C, Size);
346void ExprInspectionChecker::analyzerDumpElementCount(
const CallExpr *CE,
354 ElementTy = TVR->getValueType();
362 C.getState(),
C.getSVal(getArgExpr(CE,
C)), ElementTy);
363 printAndReport(
C, ElementCount);
366void ExprInspectionChecker::analyzerPrintState(
const CallExpr *CE,
368 C.getState()->dump();
371void ExprInspectionChecker::analyzerWarnOnDeadSymbol(
const CallExpr *CE,
373 const Expr *Arg = getArgExpr(CE,
C);
377 SVal Val =
C.getSVal(Arg);
383 State = State->add<MarkedSymbols>(Sym);
384 C.addTransition(State);
387void ExprInspectionChecker::checkDeadSymbols(
SymbolReaper &SymReaper,
390 const MarkedSymbolsTy &Syms = State->get<MarkedSymbols>();
393 if (!SymReaper.
isDead(Sym))
399 State = State->remove<MarkedSymbols>(Sym);
402 for (
auto I : State->get<DenotedSymbols>()) {
404 if (!SymReaper.
isLive(Sym))
405 State = State->remove<DenotedSymbols>(Sym);
408 C.addTransition(State, N);
413 for (
auto Item : ReachedStats) {
414 unsigned NumTimesReached = Item.second.NumTimesReached;
417 reportBug(llvm::to_string(NumTimesReached), BR, N);
419 ReachedStats.clear();
422void ExprInspectionChecker::analyzerCrash(
const CallExpr *CE,
427void ExprInspectionChecker::analyzerHashDump(
const CallExpr *CE,
432 std::string HashContent =
434 C.getLocationContext()->getDecl(), Opts);
436 reportBug(HashContent,
C);
439void ExprInspectionChecker::analyzerDenote(
const CallExpr *CE,
442 reportBug(
"clang_analyzer_denote() requires a symbol and a string literal",
449 reportBug(
"Not a symbol",
C);
455 reportBug(
"Not a string literal",
C);
461 C.addTransition(
C.getState()->set<DenotedSymbols>(Sym,
E));
466 :
public SymExprVisitor<SymbolExpressor, std::optional<std::string>> {
472 std::optional<std::string> lookup(
const SymExpr *S) {
473 if (
const StringLiteral *
const *SLPtr = State->get<DenotedSymbols>(S)) {
484 std::optional<std::string> VisitSymIntExpr(
const SymIntExpr *S) {
485 if (std::optional<std::string> Str = lookup(S))
487 if (std::optional<std::string> Str =
Visit(S->getLHS()))
489 std::to_string(S->getRHS().getLimitedValue()) +
490 (S->getRHS().isUnsigned() ?
"U" :
""))
495 std::optional<std::string> VisitSymSymExpr(
const SymSymExpr *S) {
496 if (std::optional<std::string> Str = lookup(S))
498 if (std::optional<std::string> Str1 =
Visit(S->getLHS()))
499 if (std::optional<std::string> Str2 =
Visit(S->getRHS()))
506 std::optional<std::string> VisitUnarySymExpr(
const UnarySymExpr *S) {
507 if (std::optional<std::string> Str = lookup(S))
509 if (std::optional<std::string> Str =
Visit(S->getOperand()))
514 std::optional<std::string> VisitSymbolCast(
const SymbolCast *S) {
515 if (std::optional<std::string> Str = lookup(S))
517 if (std::optional<std::string> Str =
Visit(S->getOperand()))
518 return (Twine(
"(") + S->getType().getAsString() +
")" + *Str).str();
524void ExprInspectionChecker::analyzerExpress(
const CallExpr *CE,
526 const Expr *Arg = getArgExpr(CE,
C);
533 reportBug(
"Not a symbol",
C, ArgVal);
537 SymbolExpressor
V(
C.getState());
538 auto Str =
V.Visit(Sym);
540 reportBug(
"Unable to express",
C, ArgVal);
544 reportBug(*Str,
C, ArgVal);
547void ExprInspectionChecker::analyzerIsTainted(
const CallExpr *CE,
550 reportBug(
"clang_analyzer_isTainted() requires exactly one argument",
C);
553 const bool IsTainted =
555 reportBug(IsTainted ?
"YES" :
"NO",
C);
562bool ento::shouldRegisterExprInspectionChecker(
const CheckerManager &mgr) {
static void printHelper(llvm::raw_svector_ostream &Out, CheckerContext &C, const llvm::APSInt &I)
static const char * getArgumentValueString(const CallExpr *CE, CheckerContext &C)
#define REGISTER_MAP_WITH_PROGRAMSTATE(Name, Key, Value)
Declares an immutable map of type NameTy, suitable for placement into the ProgramState.
#define REGISTER_SET_WITH_PROGRAMSTATE(Name, Elem)
Declares an immutable set of type NameTy, suitable for placement into the ProgramState.
static std::string getName(const CallEvent &Call)
StringRef getOpcodeStr() const
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Expr * getArg(unsigned Arg)
getArg - Return the specified argument.
unsigned getNumArgs() const
getNumArgs - Return the number of actual arguments to this call.
This represents one expression.
Expr * IgnoreParenCasts() LLVM_READONLY
Skip past any parentheses and casts which might surround this expression until reaching a fixed point...
A SourceLocation and its associated SourceManager.
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
It wraps the AnalysisDeclContext to represent both the call stack with the help of StackFrameContext ...
const LocationContext * getParent() const
It might return null.
const StackFrameContext * getStackFrame() const
A (possibly-)qualified type.
static std::string getAsString(SplitQualType split, const PrintingPolicy &Policy)
This class handles loading and caching of source files into memory.
SourceLocation getBeginLoc() const LLVM_READONLY
StringLiteral - This represents a string literal expression, e.g.
StringRef getBytes() const
Allow access to clients that need the byte representation, such as ASTWriterStmt::VisitStringLiteral(...
bool isPointerType() const
static StringRef getOpcodeStr(Opcode Op)
getOpcodeStr - Turn an Opcode enum value into the punctuation char it corresponds to,...
Template implementation for all binary symbolic expressions.
BugReporter is a utility class for generating PathDiagnostics for analysis.
virtual void emitReport(std::unique_ptr< BugReport > R)
Add the given report to the set of reports tracked by BugReporter.
Represents an abstract call to a function or method along a particular path.
CHECKER * registerChecker(AT &&... Args)
Used to register checkers.
const ProgramStateRef & getState() const
const LocationContext * getLocationContext() const
MemRegion - The root abstract class for all memory regions.
LLVM_ATTRIBUTE_RETURNS_NONNULL const RegionTy * castAs() const
const RegionTy * getAs() const
SVal - This represents a symbolic expression, which can be either an L-value or an R-value.
SymbolRef getAsSymbol(bool IncludeBaseRegions=false) const
If this SVal wraps a symbol return that SymbolRef.
T castAs() const
Convert to the specified SVal type, asserting that this SVal is of the desired type.
SymExprVisitor - this class implements a simple visitor for SymExpr subclasses.
RetTy VisitSymExpr(SymbolRef S)
Represents a cast expression.
A class responsible for cleaning up unused symbols.
bool isDead(SymbolRef sym)
Returns whether or not a symbol has been confirmed dead.
bool isLive(SymbolRef sym)
SymbolicRegion - A special, "non-concrete" region.
TypedValueRegion - An abstract class representing regions having a typed value.
Represents a symbolic expression involving a unary operator.
bool isTainted(ProgramStateRef State, const Stmt *S, const LocationContext *LCtx, TaintTagType Kind=TaintTagGeneric)
Check if the statement has a tainted value in the given state.
SVal getDynamicExtentWithOffset(ProgramStateRef State, SVal BufV)
Get the dynamic extent for a symbolic value that represents a buffer.
DefinedOrUnknownSVal getDynamicElementCountWithOffset(ProgramStateRef State, SVal BufV, QualType Ty)
The JSON file list parser is used to communicate input to InstallAPI.
const FunctionProtoType * T
std::string getIssueString(const FullSourceLoc &IssueLoc, llvm::StringRef CheckerName, llvm::StringRef WarningMessage, const Decl *IssueDecl, const LangOptions &LangOpts)
Get the unhashed string representation of the V1 issue hash.