31bool isRefcountedStringsHack(
const VarDecl *
V) {
33 auto safeClass = [](
const std::string &className) {
34 return className ==
"String" || className ==
"AtomString" ||
35 className ==
"UniquedString" || className ==
"Identifier";
39 if (
auto *CXXRD =
T->getAsCXXRecordDecl()) {
43 if (
T->isPointerType() ||
T->isReferenceType()) {
44 if (
auto *CXXRD =
T->getPointeeCXXRecordDecl()) {
53 const VarDecl *Guardian{
nullptr};
55 explicit GuardianVisitor(
const VarDecl *Guardian) : Guardian(Guardian) {
59 bool VisitBinaryOperator(BinaryOperator *BO)
override {
61 if (
auto *VarRef = dyn_cast<DeclRefExpr>(BO->
getLHS())) {
62 if (VarRef->getDecl() == Guardian)
69 bool VisitCXXConstructExpr(CXXConstructExpr *CE)
override {
71 if (Ctor->isMoveConstructor() && CE->
getNumArgs() == 1) {
73 if (
auto *VarRef = dyn_cast<DeclRefExpr>(Arg)) {
74 if (VarRef->getDecl() == Guardian)
82 bool VisitCXXMemberCallExpr(CXXMemberCallExpr *MCE)
override {
84 if (MethodName ==
"swap" || MethodName ==
"leakRef" ||
85 MethodName ==
"releaseNonNull" || MethodName ==
"clear") {
87 if (
auto *VarRef = dyn_cast<DeclRefExpr>(ThisArg)) {
88 if (VarRef->getDecl() == Guardian)
95 bool VisitCXXOperatorCallExpr(CXXOperatorCallExpr *OCE)
override {
99 if (
auto *VarRef = dyn_cast<DeclRefExpr>(ThisArg)) {
100 if (VarRef->getDecl() == Guardian)
108bool isGuardedScopeEmbeddedInGuardianScope(
const VarDecl *Guarded,
109 const VarDecl *MaybeGuardian) {
111 assert(MaybeGuardian);
116 const CompoundStmt *guardiansClosestCompStmtAncestor =
nullptr;
121 !guardianAncestors.
empty();
126 for (
auto &guardianAncestor : guardianAncestors) {
127 if (
auto *CStmtParentAncestor = guardianAncestor.get<
CompoundStmt>()) {
128 guardiansClosestCompStmtAncestor = CStmtParentAncestor;
132 if (guardiansClosestCompStmtAncestor)
136 if (!guardiansClosestCompStmtAncestor)
143 !guardedVarAncestors.
empty();
148 for (
auto &guardedVarAncestor : guardedVarAncestors) {
149 if (
auto *CStmtAncestor = guardedVarAncestor.get<
CompoundStmt>()) {
150 if (!FirstCompondStmt) {
151 FirstCompondStmt = CStmtAncestor;
154 if (CStmtAncestor == guardiansClosestCompStmtAncestor) {
155 GuardianVisitor guardianVisitor(MaybeGuardian);
156 auto *GuardedScope =
const_cast<CompoundStmt *
>(FirstCompondStmt);
157 return guardianVisitor.TraverseCompoundStmt(GuardedScope);
166class RawPtrRefLocalVarsChecker
167 :
public Checker<check::ASTDecl<TranslationUnitDecl>> {
169 mutable BugReporter *BR;
170 EnsureFunctionAnalysis EFA;
173 mutable std::optional<RetainTypeChecker> RTC;
176 RawPtrRefLocalVarsChecker(
const char *description)
177 : Bug(this, description,
"WebKit coding guidelines") {}
179 virtual std::optional<bool>
isUnsafePtr(
const QualType
T)
const = 0;
180 virtual bool isSafePtr(
const CXXRecordDecl *)
const = 0;
181 virtual bool isSafePtrType(
const QualType)
const = 0;
182 virtual bool isSafeExpr(
const Expr *)
const {
return false; }
183 virtual const char *ptrKind()
const = 0;
185 void checkASTDecl(
const TranslationUnitDecl *TUD, AnalysisManager &MGR,
186 BugReporter &BRArg)
const {
193 const RawPtrRefLocalVarsChecker *Checker;
194 Decl *DeclWithIssue{
nullptr};
196 TrivialFunctionAnalysis TFA;
198 explicit LocalVisitor(
const RawPtrRefLocalVarsChecker *Checker)
201 ShouldVisitTemplateInstantiations =
true;
202 ShouldVisitImplicitCode =
false;
205 bool TraverseDecl(Decl *D)
override {
206 llvm::SaveAndRestore SavedDecl(DeclWithIssue);
212 bool VisitTypedefDecl(TypedefDecl *TD)
override {
214 Checker->RTC->visitTypedef(TD);
218 bool VisitVarDecl(VarDecl *
V)
override {
219 auto *
Init =
V->getInit();
220 if (
Init &&
V->isLocalVarDecl())
221 Checker->visitVarDecl(
V,
Init, DeclWithIssue);
225 bool VisitBinaryOperator(BinaryOperator *BO)
override {
227 if (
auto *VarRef = dyn_cast<DeclRefExpr>(BO->
getLHS())) {
228 if (
auto *
V = dyn_cast<VarDecl>(VarRef->getDecl()))
229 Checker->visitVarDecl(
V, BO->
getRHS(), DeclWithIssue);
235 bool TraverseIfStmt(IfStmt *IS)
override {
237 return DynamicRecursiveASTVisitor::TraverseIfStmt(IS);
241 bool TraverseForStmt(ForStmt *FS)
override {
243 return DynamicRecursiveASTVisitor::TraverseForStmt(FS);
247 bool TraverseCXXForRangeStmt(CXXForRangeStmt *FRS)
override {
249 return DynamicRecursiveASTVisitor::TraverseCXXForRangeStmt(FRS);
253 bool TraverseWhileStmt(WhileStmt *WS)
override {
255 return DynamicRecursiveASTVisitor::TraverseWhileStmt(WS);
261 return DynamicRecursiveASTVisitor::TraverseCompoundStmt(CS);
265 bool TraverseClassTemplateDecl(ClassTemplateDecl *Decl)
override {
268 return DynamicRecursiveASTVisitor::TraverseClassTemplateDecl(Decl);
272 LocalVisitor visitor(
this);
274 RTC->visitTranslationUnitDecl(TUD);
275 visitor.TraverseDecl(
const_cast<TranslationUnitDecl *
>(TUD));
278 void visitVarDecl(
const VarDecl *
V,
const Expr *
Value,
279 const Decl *DeclWithIssue)
const {
280 if (shouldSkipVarDecl(
V))
283 std::optional<bool> IsUncountedPtr =
isUnsafePtr(
V->getType());
284 if (IsUncountedPtr && *IsUncountedPtr) {
287 [&](
const clang::CXXRecordDecl *
Record) {
290 [&](
const clang::QualType
Type) {
return isSafePtrType(
Type); },
291 [&](
const clang::Expr *InitArgOrigin,
bool IsSafe) {
292 if (!InitArgOrigin || IsSafe)
307 if (EFA.isACallToEnsureFn(InitArgOrigin))
310 if (isSafeExpr(InitArgOrigin))
313 if (
auto *Ref = llvm::dyn_cast<DeclRefExpr>(InitArgOrigin)) {
314 if (
auto *MaybeGuardian =
315 dyn_cast_or_null<VarDecl>(Ref->getFoundDecl())) {
316 const auto *MaybeGuardianArgType =
318 if (MaybeGuardianArgType) {
319 const CXXRecordDecl *
const MaybeGuardianArgCXXRecord =
320 MaybeGuardianArgType->getAsCXXRecordDecl();
321 if (MaybeGuardianArgCXXRecord) {
324 isRefcountedStringsHack(MaybeGuardian)) &&
325 isGuardedScopeEmbeddedInGuardianScope(
342 reportBug(
V,
Value, DeclWithIssue);
346 bool shouldSkipVarDecl(
const VarDecl *
V)
const {
350 return BR->getSourceManager().isInSystemHeader(
V->getLocation());
353 void reportBug(
const VarDecl *
V,
const Expr *
Value,
354 const Decl *DeclWithIssue)
const {
356 SmallString<100> Buf;
357 llvm::raw_svector_ostream Os(Buf);
360 Os <<
"Assignment to an " << ptrKind() <<
" parameter ";
364 PathDiagnosticLocation BSLoc(
Value->getExprLoc(), BR->getSourceManager());
365 auto Report = std::make_unique<BasicBugReport>(Bug, Os.str(), BSLoc);
367 BR->emitReport(std::move(
Report));
369 if (
V->hasLocalStorage())
370 Os <<
"Local variable ";
371 else if (
V->isStaticLocal())
372 Os <<
"Static local variable ";
373 else if (
V->hasGlobalStorage())
374 Os <<
"Global variable ";
378 Os <<
" is " << ptrKind() <<
" and unsafe.";
380 PathDiagnosticLocation BSLoc(
V->getLocation(), BR->getSourceManager());
381 auto Report = std::make_unique<BasicBugReport>(Bug, Os.str(), BSLoc);
382 Report->addRange(
V->getSourceRange());
383 Report->setDeclWithIssue(DeclWithIssue);
384 BR->emitReport(std::move(
Report));
389class UncountedLocalVarsChecker final :
public RawPtrRefLocalVarsChecker {
391 UncountedLocalVarsChecker()
392 : RawPtrRefLocalVarsChecker(
"Uncounted raw pointer or reference not "
393 "provably backed by ref-counted variable") {}
394 std::optional<bool>
isUnsafePtr(
const QualType
T)
const final {
400 bool isSafePtrType(
const QualType
type)
const final {
403 const char *ptrKind() const final {
return "uncounted"; }
406class UncheckedLocalVarsChecker final :
public RawPtrRefLocalVarsChecker {
408 UncheckedLocalVarsChecker()
409 : RawPtrRefLocalVarsChecker(
"Unchecked raw pointer or reference not "
410 "provably backed by checked variable") {}
411 std::optional<bool>
isUnsafePtr(
const QualType
T)
const final {
417 bool isSafePtrType(
const QualType
type)
const final {
420 bool isSafeExpr(
const Expr *E)
const final {
423 const char *ptrKind() const final {
return "unchecked"; }
426class UnretainedLocalVarsChecker final :
public RawPtrRefLocalVarsChecker {
428 UnretainedLocalVarsChecker()
429 : RawPtrRefLocalVarsChecker(
"Unretained raw pointer or reference not "
430 "provably backed by a RetainPtr") {
431 RTC = RetainTypeChecker();
433 std::optional<bool>
isUnsafePtr(
const QualType
T)
const final {
434 return RTC->isUnretained(
T);
439 bool isSafePtrType(
const QualType
type)
const final {
442 bool isSafeExpr(
const Expr *E)
const final {
446 const char *ptrKind() const final {
return "unretained"; }
451void ento::registerUncountedLocalVarsChecker(
CheckerManager &Mgr) {
455bool ento::shouldRegisterUncountedLocalVarsChecker(
const CheckerManager &) {
459void ento::registerUncheckedLocalVarsChecker(
CheckerManager &Mgr) {
463bool ento::shouldRegisterUncheckedLocalVarsChecker(
const CheckerManager &) {
467void ento::registerUnretainedLocalVarsChecker(
CheckerManager &Mgr) {
471bool ento::shouldRegisterUnretainedLocalVarsChecker(
const CheckerManager &) {
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
llvm::MachO::Record Record
Defines the clang::SourceLocation class and associated facilities.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
DynTypedNodeList getParents(const NodeT &Node)
Forwards to get node parents from the ParentMapContext.
static bool isAssignmentOp(Opcode Opc)
Expr * getArg(unsigned Arg)
Return the specified argument.
CXXConstructorDecl * getConstructor() const
Get the constructor that this expression will (ultimately) call.
unsigned getNumArgs() const
Return the number of arguments to the constructor call.
CXXMethodDecl * getMethodDecl() const
Retrieve the declaration of the called method.
Expr * getImplicitObjectArgument() const
Retrieve the implicit object argument for the member call.
static bool isAssignmentOp(OverloadedOperatorKind Opc)
Expr * getArg(unsigned Arg)
getArg - Return the specified argument.
unsigned getNumArgs() const
getNumArgs - Return the number of actual arguments to this call.
CompoundStmt - This represents a group of statements like { stmt stmt }.
ASTContext & getASTContext() const LLVM_READONLY
Container for either a single DynTypedNode or for an ArrayRef to DynTypedNode.
virtual bool TraverseDecl(MaybeConst< Decl > *D)
Expr * IgnoreParenCasts() LLVM_READONLY
Skip past any parentheses and casts which might surround this expression until reaching a fixed point...
A (possibly-)qualified type.
const Type * getTypePtr() const
Retrieves a pointer to the underlying (unqualified) type.
bool isTrivial(const Decl *D) const
Represents a variable declaration or definition.
bool isLocalVarDecl() const
Returns true for local variable declarations other than parameters.
CHECKER * registerChecker(AT &&...Args)
Register a single-part checker (derived from Checker): construct its singleton instance,...
Simple checker classes that implement one frontend (i.e.
const internal::VariadicAllOfMatcher< Type > type
Matches Types in the clang AST.
bool isCocoaObjectRef(QualType T)
std::variant< struct RequiresDecl, struct HeaderDecl, struct UmbrellaDirDecl, struct ModuleDecl, struct ExcludeDecl, struct ExportDecl, struct ExportAsDecl, struct ExternModuleDecl, struct UseDecl, struct LinkDecl, struct ConfigMacrosDecl, struct ConflictDecl > Decl
All declarations that can appear in a module declaration.
The JSON file list parser is used to communicate input to InstallAPI.
bool isa(CodeGen::Address addr)
bool isExprToGetCheckedPtrCapableMember(const clang::Expr *E)
bool tryToFindPtrOrigin(const Expr *E, bool StopAtFirstRefCountedObj, std::function< bool(const clang::CXXRecordDecl *)> isSafePtr, std::function< bool(const clang::QualType)> isSafePtrType, std::function< bool(const clang::Expr *, bool)> callback)
This function de-facto defines a set of transformations that we consider safe (in heuristical sense).
bool isRefOrCheckedPtrType(const clang::QualType T)
void printQuotedQualifiedName(llvm::raw_ostream &Os, const NamedDeclDerivedT &D)
bool isRetainPtrOrOSPtrType(const clang::QualType T)
std::optional< bool > isUnsafePtr(const QualType T, bool IsArcEnabled)
const FunctionProtoType * T
bool isSmartPtrClass(const std::string &Name)
bool isRefCounted(const CXXRecordDecl *R)
@ Type
The name was classified as a type.
bool isRetainPtrOrOSPtr(const std::string &Name)
std::optional< bool > isUncountedPtr(const QualType T)
bool isSafePtr(clang::CXXRecordDecl *Decl)
std::string safeGetName(const T *ASTNode)
bool isNullPtr(const clang::Expr *E)
bool isCheckedPtr(const std::string &Name)
DynamicRecursiveASTVisitorBase< false > DynamicRecursiveASTVisitor
bool isConstOwnerPtrMemberExpr(const clang::Expr *E)
std::optional< bool > isUncheckedPtr(const QualType T)