18#include "llvm/ADT/StringSwitch.h"
19#include "llvm/Support/ScopedPrinter.h"
26class ExprInspectionChecker
27 :
public Checker<eval::Call, check::DeadSymbols, check::EndAnalysis> {
28 mutable std::unique_ptr<BugType> BT;
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;
65 template <
typename T>
void printAndReport(
CheckerContext &
C, T What)
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 {
181 BT.reset(
new BugType(
this,
"Checking analyzer assumptions",
"debug"));
183 auto R = std::make_unique<PathSensitiveBugReport>(*BT, Msg, N);
185 R->markInteresting(*ExprVal);
191const Expr *ExprInspectionChecker::getArgExpr(
const CallExpr *CE,
194 reportBug(
"Missing argument",
C);
202 const Expr *Arg = getArgExpr(CE,
C);
206 const MemRegion *MR =
C.getSVal(Arg).getAsRegion();
208 reportBug(
"Cannot obtain the region",
C);
215void ExprInspectionChecker::analyzerEval(
const CallExpr *CE,
227void ExprInspectionChecker::analyzerWarnIfReached(
const CallExpr *CE,
229 reportBug(
"REACHABLE",
C);
232void ExprInspectionChecker::analyzerNumTimesReached(
const CallExpr *CE,
234 ++ReachedStats[CE].NumTimesReached;
235 if (!ReachedStats[CE].ExampleNode) {
237 ReachedStats[CE].ExampleNode =
C.generateNonFatalErrorNode();
241void ExprInspectionChecker::analyzerCheckInlined(
const CallExpr *CE,
256void ExprInspectionChecker::analyzerExplain(
const CallExpr *CE,
258 const Expr *Arg = getArgExpr(CE,
C);
264 reportBug(Ex.Visit(
V),
C);
268 const llvm::APSInt &I) {
269 Out << I.getBitWidth() << (I.isUnsigned() ?
"u:" :
"s:");
275 C.getConstraintManager().printValue(Out,
C.getState(), Sym);
284void ExprInspectionChecker::printAndReport(
CheckerContext &
C, T What)
const {
286 llvm::raw_svector_ostream OS(Str);
288 reportBug(OS.str(),
C);
291void ExprInspectionChecker::analyzerValue(
const CallExpr *CE,
293 const Expr *Arg = getArgExpr(CE,
C);
299 printAndReport(
C, Sym);
300 else if (
const llvm::APSInt *I =
V.getAsInteger())
301 printAndReport(
C, *I);
306void ExprInspectionChecker::analyzerDumpSValType(
const CallExpr *CE,
308 const Expr *Arg = getArgExpr(CE,
C);
312 QualType Ty =
C.getSVal(Arg).getType(
C.getASTContext());
316void ExprInspectionChecker::analyzerDump(
const CallExpr *CE,
318 const Expr *Arg = getArgExpr(CE,
C);
323 printAndReport(
C,
V);
326void ExprInspectionChecker::analyzerGetExtent(
const CallExpr *CE,
328 const Expr *Arg = getArgExpr(CE,
C);
335 State = State->BindExpr(CE,
C.getLocationContext(), Size);
336 C.addTransition(State);
339void ExprInspectionChecker::analyzerDumpExtent(
const CallExpr *CE,
341 const Expr *Arg = getArgExpr(CE,
C);
347 printAndReport(
C, Size);
350void ExprInspectionChecker::analyzerDumpElementCount(
const CallExpr *CE,
358 ElementTy = TVR->getValueType();
366 C.getState(),
C.getSVal(getArgExpr(CE,
C)), ElementTy);
367 printAndReport(
C, ElementCount);
370void ExprInspectionChecker::analyzerPrintState(
const CallExpr *CE,
372 C.getState()->dump();
375void ExprInspectionChecker::analyzerWarnOnDeadSymbol(
const CallExpr *CE,
377 const Expr *Arg = getArgExpr(CE,
C);
381 SVal Val =
C.getSVal(Arg);
387 State = State->add<MarkedSymbols>(Sym);
388 C.addTransition(State);
391void ExprInspectionChecker::checkDeadSymbols(
SymbolReaper &SymReaper,
394 const MarkedSymbolsTy &Syms = State->get<MarkedSymbols>();
397 if (!SymReaper.
isDead(Sym))
403 State = State->remove<MarkedSymbols>(Sym);
406 for (
auto I : State->get<DenotedSymbols>()) {
408 if (!SymReaper.
isLive(Sym))
409 State = State->remove<DenotedSymbols>(Sym);
412 C.addTransition(State, N);
417 for (
auto Item : ReachedStats) {
418 unsigned NumTimesReached = Item.second.NumTimesReached;
421 reportBug(llvm::to_string(NumTimesReached), BR, N);
423 ReachedStats.clear();
426void ExprInspectionChecker::analyzerCrash(
const CallExpr *CE,
431void ExprInspectionChecker::analyzerHashDump(
const CallExpr *CE,
436 std::string HashContent =
438 C.getLocationContext()->getDecl(), Opts);
440 reportBug(HashContent,
C);
443void ExprInspectionChecker::analyzerDenote(
const CallExpr *CE,
446 reportBug(
"clang_analyzer_denote() requires a symbol and a string literal",
453 reportBug(
"Not a symbol",
C);
459 reportBug(
"Not a string literal",
C);
465 C.addTransition(
C.getState()->set<DenotedSymbols>(Sym, E));
470 :
public SymExprVisitor<SymbolExpressor, std::optional<std::string>> {
476 std::optional<std::string> lookup(
const SymExpr *S) {
477 if (
const StringLiteral *
const *SLPtr = State->get<DenotedSymbols>(S)) {
488 std::optional<std::string> VisitSymIntExpr(
const SymIntExpr *S) {
489 if (std::optional<std::string> Str = lookup(S))
491 if (std::optional<std::string> Str =
Visit(S->getLHS()))
493 std::to_string(S->getRHS().getLimitedValue()) +
494 (S->getRHS().isUnsigned() ?
"U" :
""))
499 std::optional<std::string> VisitSymSymExpr(
const SymSymExpr *S) {
500 if (std::optional<std::string> Str = lookup(S))
502 if (std::optional<std::string> Str1 =
Visit(S->getLHS()))
503 if (std::optional<std::string> Str2 =
Visit(S->getRHS()))
510 std::optional<std::string> VisitUnarySymExpr(
const UnarySymExpr *S) {
511 if (std::optional<std::string> Str = lookup(S))
513 if (std::optional<std::string> Str =
Visit(S->getOperand()))
518 std::optional<std::string> VisitSymbolCast(
const SymbolCast *S) {
519 if (std::optional<std::string> Str = lookup(S))
521 if (std::optional<std::string> Str =
Visit(S->getOperand()))
522 return (Twine(
"(") + S->getType().getAsString() +
")" + *Str).str();
528void ExprInspectionChecker::analyzerExpress(
const CallExpr *CE,
530 const Expr *Arg = getArgExpr(CE,
C);
537 reportBug(
"Not a symbol",
C, ArgVal);
541 SymbolExpressor
V(
C.getState());
542 auto Str =
V.Visit(Sym);
544 reportBug(
"Unable to express",
C, ArgVal);
548 reportBug(*Str,
C, ArgVal);
551void ExprInspectionChecker::analyzerIsTainted(
const CallExpr *CE,
554 reportBug(
"clang_analyzer_isTainted() requires exactly one argument",
C);
557 const bool IsTainted =
559 reportBug(IsTainted ?
"YES" :
"NO",
C);
566bool 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)
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.