clang 22.0.0git
RawPtrRefCallArgsChecker.cpp
Go to the documentation of this file.
1//=======- RawPtrRefCallArgsChecker.cpp --------------------------*- 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 "ASTUtils.h"
10#include "DiagOutputUtils.h"
11#include "PtrTypesSemantics.h"
12#include "clang/AST/Decl.h"
13#include "clang/AST/DeclCXX.h"
21#include "llvm/Support/SaveAndRestore.h"
22#include <optional>
23
24using namespace clang;
25using namespace ento;
26
27namespace {
28
29class RawPtrRefCallArgsChecker
30 : public Checker<check::ASTDecl<TranslationUnitDecl>> {
31 BugType Bug;
32
33 TrivialFunctionAnalysis TFA;
34 EnsureFunctionAnalysis EFA;
35
36protected:
37 mutable BugReporter *BR;
38 mutable std::optional<RetainTypeChecker> RTC;
39
40public:
41 RawPtrRefCallArgsChecker(const char *description)
42 : Bug(this, description, "WebKit coding guidelines") {}
43
44 virtual std::optional<bool> isUnsafeType(QualType) const = 0;
45 virtual std::optional<bool> isUnsafePtr(QualType) const = 0;
46 virtual bool isSafePtr(const CXXRecordDecl *Record) const = 0;
47 virtual bool isSafePtrType(const QualType type) const = 0;
48 virtual bool isSafeExpr(const Expr *) const { return false; }
49 virtual bool isSafeDecl(const Decl *) const { return false; }
50 virtual const char *ptrKind() const = 0;
51
52 void checkASTDecl(const TranslationUnitDecl *TUD, AnalysisManager &MGR,
53 BugReporter &BRArg) const {
54 BR = &BRArg;
55
56 // The calls to checkAST* from AnalysisConsumer don't
57 // visit template instantiations or lambda classes. We
58 // want to visit those, so we make our own RecursiveASTVisitor.
59 struct LocalVisitor : DynamicRecursiveASTVisitor {
60 const RawPtrRefCallArgsChecker *Checker;
61 Decl *DeclWithIssue{nullptr};
62
63 explicit LocalVisitor(const RawPtrRefCallArgsChecker *Checker)
64 : Checker(Checker) {
65 assert(Checker);
66 ShouldVisitTemplateInstantiations = true;
67 ShouldVisitImplicitCode = false;
68 }
69
70 bool TraverseClassTemplateDecl(ClassTemplateDecl *Decl) override {
72 return true;
73 return DynamicRecursiveASTVisitor::TraverseClassTemplateDecl(Decl);
74 }
75
76 bool TraverseDecl(Decl *D) override {
77 llvm::SaveAndRestore SavedDecl(DeclWithIssue);
78 if (D && (isa<FunctionDecl>(D) || isa<ObjCMethodDecl>(D)))
79 DeclWithIssue = D;
81 }
82
83 bool VisitCallExpr(CallExpr *CE) override {
84 Checker->visitCallExpr(CE, DeclWithIssue);
85 return true;
86 }
87
88 bool VisitTypedefDecl(TypedefDecl *TD) override {
89 if (Checker->RTC)
90 Checker->RTC->visitTypedef(TD);
91 return true;
92 }
93
94 bool VisitObjCMessageExpr(ObjCMessageExpr *ObjCMsgExpr) override {
95 Checker->visitObjCMessageExpr(ObjCMsgExpr, DeclWithIssue);
96 return true;
97 }
98 };
99
100 LocalVisitor visitor(this);
101 if (RTC)
102 RTC->visitTranslationUnitDecl(TUD);
103 visitor.TraverseDecl(const_cast<TranslationUnitDecl *>(TUD));
104 }
105
106 void visitCallExpr(const CallExpr *CE, const Decl *D) const {
107 if (shouldSkipCall(CE))
108 return;
109
110 if (auto *F = CE->getDirectCallee()) {
111 // Skip the first argument for overloaded member operators (e. g. lambda
112 // or std::function call operator).
113 unsigned ArgIdx =
114 isa<CXXOperatorCallExpr>(CE) && isa_and_nonnull<CXXMethodDecl>(F);
115
116 if (auto *MemberCallExpr = dyn_cast<CXXMemberCallExpr>(CE)) {
117 if (auto *MD = MemberCallExpr->getMethodDecl()) {
118 auto name = safeGetName(MD);
119 if (name == "ref" || name == "deref")
120 return;
121 if (name == "incrementCheckedPtrCount" ||
122 name == "decrementCheckedPtrCount")
123 return;
124 }
125 auto *E = MemberCallExpr->getImplicitObjectArgument();
126 QualType ArgType = MemberCallExpr->getObjectType().getCanonicalType();
127 std::optional<bool> IsUnsafe = isUnsafeType(ArgType);
128 if (IsUnsafe && *IsUnsafe && !isPtrOriginSafe(E))
129 reportBugOnThis(E, D);
130 }
131
132 for (auto P = F->param_begin();
133 // FIXME: Also check variadic function parameters.
134 // FIXME: Also check default function arguments. Probably a different
135 // checker. In case there are default arguments the call can have
136 // fewer arguments than the callee has parameters.
137 P < F->param_end() && ArgIdx < CE->getNumArgs(); ++P, ++ArgIdx) {
138 // TODO: attributes.
139 // if ((*P)->hasAttr<SafeRefCntblRawPtrAttr>())
140 // continue;
141
142 QualType ArgType = (*P)->getType();
143 // FIXME: more complex types (arrays, references to raw pointers, etc)
144 std::optional<bool> IsUncounted = isUnsafePtr(ArgType);
145 if (!IsUncounted || !(*IsUncounted))
146 continue;
147
148 const auto *Arg = CE->getArg(ArgIdx);
149
150 if (auto *defaultArg = dyn_cast<CXXDefaultArgExpr>(Arg))
151 Arg = defaultArg->getExpr();
152
153 if (isPtrOriginSafe(Arg))
154 continue;
155
156 reportBug(Arg, *P, D);
157 }
158 for (; ArgIdx < CE->getNumArgs(); ++ArgIdx) {
159 const auto *Arg = CE->getArg(ArgIdx);
160 auto ArgType = Arg->getType();
161 std::optional<bool> IsUncounted = isUnsafePtr(ArgType);
162 if (!IsUncounted || !(*IsUncounted))
163 continue;
164
165 if (auto *defaultArg = dyn_cast<CXXDefaultArgExpr>(Arg))
166 Arg = defaultArg->getExpr();
167
168 if (isPtrOriginSafe(Arg))
169 continue;
170
171 reportBug(Arg, nullptr, D);
172 }
173 }
174 }
175
176 void visitObjCMessageExpr(const ObjCMessageExpr *E, const Decl *D) const {
177 if (BR->getSourceManager().isInSystemHeader(E->getExprLoc()))
178 return;
179
180 if (auto *Receiver = E->getInstanceReceiver()) {
181 std::optional<bool> IsUnsafe = isUnsafePtr(E->getReceiverType());
182 if (IsUnsafe && *IsUnsafe && !isPtrOriginSafe(Receiver)) {
183 if (isAllocInit(E))
184 return;
185 reportBugOnReceiver(Receiver, D);
186 }
187 }
188
189 auto *MethodDecl = E->getMethodDecl();
190 if (!MethodDecl)
191 return;
192
193 auto ArgCount = E->getNumArgs();
194 for (unsigned i = 0; i < ArgCount; ++i) {
195 auto *Arg = E->getArg(i);
196 bool hasParam = i < MethodDecl->param_size();
197 auto *Param = hasParam ? MethodDecl->getParamDecl(i) : nullptr;
198 auto ArgType = Arg->getType();
199 std::optional<bool> IsUnsafe = isUnsafePtr(ArgType);
200 if (!IsUnsafe || !(*IsUnsafe))
201 continue;
202 if (isPtrOriginSafe(Arg))
203 continue;
204 reportBug(Arg, Param, D);
205 }
206 }
207
208 bool isPtrOriginSafe(const Expr *Arg) const {
209 return tryToFindPtrOrigin(
210 Arg, /*StopAtFirstRefCountedObj=*/true,
211 [&](const clang::CXXRecordDecl *Record) { return isSafePtr(Record); },
212 [&](const clang::QualType T) { return isSafePtrType(T); },
213 [&](const clang::Decl *D) { return isSafeDecl(D); },
214 [&](const clang::Expr *ArgOrigin, bool IsSafe) {
215 if (IsSafe)
216 return true;
217 if (isNullPtr(ArgOrigin))
218 return true;
219 if (isa<IntegerLiteral>(ArgOrigin)) {
220 // FIXME: Check the value.
221 // foo(123)
222 return true;
223 }
224 if (isa<CXXBoolLiteralExpr>(ArgOrigin))
225 return true;
226 if (isa<ObjCStringLiteral>(ArgOrigin))
227 return true;
228 if (isASafeCallArg(ArgOrigin))
229 return true;
230 if (EFA.isACallToEnsureFn(ArgOrigin))
231 return true;
232 if (isSafeExpr(ArgOrigin))
233 return true;
234 return false;
235 });
236 }
237
238 bool shouldSkipCall(const CallExpr *CE) const {
239 const auto *Callee = CE->getDirectCallee();
240
241 if (BR->getSourceManager().isInSystemHeader(CE->getExprLoc()))
242 return true;
243
244 if (Callee && TFA.isTrivial(Callee) && !Callee->isVirtualAsWritten())
245 return true;
246
247 if (isTrivialBuiltinFunction(Callee))
248 return true;
249
250 if (CE->getNumArgs() == 0)
251 return false;
252
253 // If an assignment is problematic we should warn about the sole existence
254 // of object on LHS.
255 if (auto *MemberOp = dyn_cast<CXXOperatorCallExpr>(CE)) {
256 // Note: assignemnt to built-in type isn't derived from CallExpr.
257 if (MemberOp->getOperator() ==
258 OO_Equal) { // Ignore assignment to Ref/RefPtr.
259 auto *callee = MemberOp->getDirectCallee();
260 if (auto *calleeDecl = dyn_cast<CXXMethodDecl>(callee)) {
261 if (const CXXRecordDecl *classDecl = calleeDecl->getParent()) {
262 if (isSafePtr(classDecl))
263 return true;
264 }
265 }
266 }
267 if (MemberOp->isAssignmentOp())
268 return false;
269 }
270
271 if (!Callee)
272 return false;
273
274 if (isMethodOnWTFContainerType(Callee))
275 return true;
276
277 auto overloadedOperatorType = Callee->getOverloadedOperator();
278 if (overloadedOperatorType == OO_EqualEqual ||
279 overloadedOperatorType == OO_ExclaimEqual ||
280 overloadedOperatorType == OO_LessEqual ||
281 overloadedOperatorType == OO_GreaterEqual ||
282 overloadedOperatorType == OO_Spaceship ||
283 overloadedOperatorType == OO_AmpAmp ||
284 overloadedOperatorType == OO_PipePipe)
285 return true;
286
287 if (isCtorOfSafePtr(Callee) || isPtrConversion(Callee))
288 return true;
289
290 auto name = safeGetName(Callee);
291 if (name == "adoptRef" || name == "getPtr" || name == "WeakPtr" ||
292 name == "is" || name == "equal" || name == "hash" || name == "isType" ||
293 // FIXME: Most/all of these should be implemented via attributes.
294 name == "CFEqual" || name == "equalIgnoringASCIICase" ||
295 name == "equalIgnoringASCIICaseCommon" ||
296 name == "equalIgnoringNullity" || name == "toString")
297 return true;
298
299 return false;
300 }
301
302 bool isMethodOnWTFContainerType(const FunctionDecl *Decl) const {
303 if (!isa<CXXMethodDecl>(Decl))
304 return false;
305 auto *ClassDecl = Decl->getParent();
306 if (!ClassDecl || !isa<CXXRecordDecl>(ClassDecl))
307 return false;
308
309 auto *NsDecl = ClassDecl->getParent();
310 if (!NsDecl || !isa<NamespaceDecl>(NsDecl))
311 return false;
312
313 auto MethodName = safeGetName(Decl);
314 auto ClsNameStr = safeGetName(ClassDecl);
315 StringRef ClsName = ClsNameStr; // FIXME: Make safeGetName return StringRef.
316 auto NamespaceName = safeGetName(NsDecl);
317 // FIXME: These should be implemented via attributes.
318 return NamespaceName == "WTF" &&
319 (MethodName == "find" || MethodName == "findIf" ||
320 MethodName == "reverseFind" || MethodName == "reverseFindIf" ||
321 MethodName == "findIgnoringASCIICase" || MethodName == "get" ||
322 MethodName == "inlineGet" || MethodName == "contains" ||
323 MethodName == "containsIf" ||
324 MethodName == "containsIgnoringASCIICase" ||
325 MethodName == "startsWith" || MethodName == "endsWith" ||
326 MethodName == "startsWithIgnoringASCIICase" ||
327 MethodName == "endsWithIgnoringASCIICase" ||
328 MethodName == "substring") &&
329 (ClsName.ends_with("Vector") || ClsName.ends_with("Set") ||
330 ClsName.ends_with("Map") || ClsName == "StringImpl" ||
331 ClsName.ends_with("String"));
332 }
333
334 void reportBug(const Expr *CallArg, const ParmVarDecl *Param,
335 const Decl *DeclWithIssue) const {
336 assert(CallArg);
337
338 SmallString<100> Buf;
339 llvm::raw_svector_ostream Os(Buf);
340
341 const std::string paramName = safeGetName(Param);
342 Os << "Call argument";
343 if (!paramName.empty()) {
344 Os << " for parameter ";
345 printQuotedQualifiedName(Os, Param);
346 }
347 Os << " is " << ptrKind() << " and unsafe.";
348
349 bool usesDefaultArgValue = isa<CXXDefaultArgExpr>(CallArg) && Param;
350 const SourceLocation SrcLocToReport =
351 usesDefaultArgValue ? Param->getDefaultArg()->getExprLoc()
352 : CallArg->getSourceRange().getBegin();
353
354 PathDiagnosticLocation BSLoc(SrcLocToReport, BR->getSourceManager());
355 auto Report = std::make_unique<BasicBugReport>(Bug, Os.str(), BSLoc);
356 Report->addRange(CallArg->getSourceRange());
357 Report->setDeclWithIssue(DeclWithIssue);
358 BR->emitReport(std::move(Report));
359 }
360
361 void reportBugOnThis(const Expr *CallArg, const Decl *DeclWithIssue) const {
362 assert(CallArg);
363
364 const SourceLocation SrcLocToReport = CallArg->getSourceRange().getBegin();
365
366 SmallString<100> Buf;
367 llvm::raw_svector_ostream Os(Buf);
368 Os << "Call argument for 'this' parameter is " << ptrKind();
369 Os << " and unsafe.";
370
371 PathDiagnosticLocation BSLoc(SrcLocToReport, BR->getSourceManager());
372 auto Report = std::make_unique<BasicBugReport>(Bug, Os.str(), BSLoc);
373 Report->addRange(CallArg->getSourceRange());
374 Report->setDeclWithIssue(DeclWithIssue);
375 BR->emitReport(std::move(Report));
376 }
377
378 void reportBugOnReceiver(const Expr *CallArg,
379 const Decl *DeclWithIssue) const {
380 assert(CallArg);
381
382 const SourceLocation SrcLocToReport = CallArg->getSourceRange().getBegin();
383
384 SmallString<100> Buf;
385 llvm::raw_svector_ostream Os(Buf);
386 Os << "Receiver is " << ptrKind() << " and unsafe.";
387
388 PathDiagnosticLocation BSLoc(SrcLocToReport, BR->getSourceManager());
389 auto Report = std::make_unique<BasicBugReport>(Bug, Os.str(), BSLoc);
390 Report->addRange(CallArg->getSourceRange());
391 Report->setDeclWithIssue(DeclWithIssue);
392 BR->emitReport(std::move(Report));
393 }
394};
395
396class UncountedCallArgsChecker final : public RawPtrRefCallArgsChecker {
397public:
398 UncountedCallArgsChecker()
399 : RawPtrRefCallArgsChecker("Uncounted call argument for a raw "
400 "pointer/reference parameter") {}
401
402 std::optional<bool> isUnsafeType(QualType QT) const final {
403 return isUncounted(QT);
404 }
405
406 std::optional<bool> isUnsafePtr(QualType QT) const final {
407 return isUncountedPtr(QT.getCanonicalType());
408 }
409
410 bool isSafePtr(const CXXRecordDecl *Record) const final {
412 }
413
414 bool isSafePtrType(const QualType type) const final {
416 }
417
418 const char *ptrKind() const final { return "uncounted"; }
419};
420
421class UncheckedCallArgsChecker final : public RawPtrRefCallArgsChecker {
422public:
423 UncheckedCallArgsChecker()
424 : RawPtrRefCallArgsChecker("Unchecked call argument for a raw "
425 "pointer/reference parameter") {}
426
427 std::optional<bool> isUnsafeType(QualType QT) const final {
428 return isUnchecked(QT);
429 }
430
431 std::optional<bool> isUnsafePtr(QualType QT) const final {
432 return isUncheckedPtr(QT.getCanonicalType());
433 }
434
435 bool isSafePtr(const CXXRecordDecl *Record) const final {
437 }
438
439 bool isSafePtrType(const QualType type) const final {
441 }
442
443 bool isSafeExpr(const Expr *E) const final {
445 }
446
447 const char *ptrKind() const final { return "unchecked"; }
448};
449
450class UnretainedCallArgsChecker final : public RawPtrRefCallArgsChecker {
451public:
452 UnretainedCallArgsChecker()
453 : RawPtrRefCallArgsChecker("Unretained call argument for a raw "
454 "pointer/reference parameter") {
455 RTC = RetainTypeChecker();
456 }
457
458 std::optional<bool> isUnsafeType(QualType QT) const final {
459 return RTC->isUnretained(QT);
460 }
461
462 std::optional<bool> isUnsafePtr(QualType QT) const final {
463 return RTC->isUnretained(QT);
464 }
465
466 bool isSafePtr(const CXXRecordDecl *Record) const final {
468 }
469
470 bool isSafePtrType(const QualType type) const final {
472 }
473
474 bool isSafeExpr(const Expr *E) const final {
477 }
478
479 bool isSafeDecl(const Decl *D) const final {
480 // Treat NS/CF globals in system header as immortal.
482 }
483
484 const char *ptrKind() const final { return "unretained"; }
485};
486
487} // namespace
488
489void ento::registerUncountedCallArgsChecker(CheckerManager &Mgr) {
490 Mgr.registerChecker<UncountedCallArgsChecker>();
491}
492
493bool ento::shouldRegisterUncountedCallArgsChecker(const CheckerManager &) {
494 return true;
495}
496
497void ento::registerUncheckedCallArgsChecker(CheckerManager &Mgr) {
498 Mgr.registerChecker<UncheckedCallArgsChecker>();
499}
500
501bool ento::shouldRegisterUncheckedCallArgsChecker(const CheckerManager &) {
502 return true;
503}
504
505void ento::registerUnretainedCallArgsChecker(CheckerManager &Mgr) {
506 Mgr.registerChecker<UnretainedCallArgsChecker>();
507}
508
509bool ento::shouldRegisterUnretainedCallArgsChecker(const CheckerManager &) {
510 return true;
511}
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
llvm::MachO::Record Record
Definition MachO.h:31
Defines the clang::SourceLocation class and associated facilities.
Expr * getArg(unsigned Arg)
getArg - Return the specified argument.
Definition Expr.h:3147
FunctionDecl * getDirectCallee()
If the callee is a FunctionDecl, return it. Otherwise return null.
Definition Expr.h:3126
unsigned getNumArgs() const
getNumArgs - Return the number of actual arguments to this call.
Definition Expr.h:3134
SourceLocation getLocation() const
Definition DeclBase.h:439
virtual bool TraverseDecl(MaybeConst< Decl > *D)
SourceLocation getExprLoc() const LLVM_READONLY
getExprLoc - Return the preferred location for the arrow when diagnosing a problem with a generic exp...
Definition Expr.cpp:276
QualType getType() const
Definition Expr.h:144
Expr * getArg(unsigned Arg)
getArg - Return the specified argument.
Definition ExprObjC.h:1400
Expr * getInstanceReceiver()
Returns the object expression (receiver) for an instance message, or null for a message that is not a...
Definition ExprObjC.h:1265
const ObjCMethodDecl * getMethodDecl() const
Definition ExprObjC.h:1361
QualType getReceiverType() const
Retrieve the receiver type to which this message is being directed.
Definition ExprObjC.cpp:296
unsigned getNumArgs() const
Return the number of actual arguments in this message, not counting the receiver.
Definition ExprObjC.h:1387
Expr * getDefaultArg()
Definition Decl.cpp:3006
bool isInSystemHeader(SourceLocation Loc) const
Returns if a SourceLocation is in a system header.
SourceLocation getBegin() const
SourceRange getSourceRange() const LLVM_READONLY
SourceLocation tokens are not useful in isolation - they are low level value objects created/interpre...
Definition Stmt.cpp:338
const SourceManager & getSourceManager()
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.
Definition Checker.h:553
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.
RangeSelector name(std::string ID)
Given a node with a "name", (like NamedDecl, DeclRefExpr, CxxCtorInitializer, and TypeLoc) selects th...
The JSON file list parser is used to communicate input to InstallAPI.
bool isCtorOfSafePtr(const clang::FunctionDecl *F)
bool isTrivialBuiltinFunction(const FunctionDecl *F)
bool isa(CodeGen::Address addr)
Definition Address.h:330
bool isExprToGetCheckedPtrCapableMember(const clang::Expr *E)
Definition ASTUtils.cpp:304
bool isPtrConversion(const FunctionDecl *F)
std::optional< bool > isUnchecked(const QualType T)
bool isRefOrCheckedPtrType(const clang::QualType T)
void printQuotedQualifiedName(llvm::raw_ostream &Os, const NamedDeclDerivedT &D)
bool isRetainPtrOrOSPtrType(const clang::QualType T)
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::Decl *)> isSafeGlobalDecl, std::function< bool(const clang::Expr *, bool)> callback)
This function de-facto defines a set of transformations that we consider safe (in heuristical sense).
Definition ASTUtils.cpp:25
bool isASafeCallArg(const Expr *E)
For E referring to a ref-countable/-counted pointer/reference we return whether it's a safe call argu...
Definition ASTUtils.cpp:240
const FunctionProtoType * T
bool isSmartPtrClass(const std::string &Name)
bool isRefCounted(const CXXRecordDecl *R)
bool isRetainPtrOrOSPtr(const std::string &Name)
std::optional< bool > isUncountedPtr(const QualType T)
bool isSafePtr(clang::CXXRecordDecl *Decl)
Definition ASTUtils.cpp:21
std::string safeGetName(const T *ASTNode)
Definition ASTUtils.h:95
bool isNullPtr(const clang::Expr *E)
Definition ASTUtils.cpp:270
bool isCheckedPtr(const std::string &Name)
DynamicRecursiveASTVisitorBase< false > DynamicRecursiveASTVisitor
bool isAllocInit(const Expr *E, const Expr **InnerExpr)
Definition ASTUtils.cpp:324
std::optional< bool > isUncounted(const QualType T)
std::optional< bool > isUncheckedPtr(const QualType T)