clang 22.0.0git
MemoryUnsafeCastChecker.cpp
Go to the documentation of this file.
1//=======- MemoryUnsafeCastChecker.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// This file defines MemoryUnsafeCast checker, which checks for casts from a
10// base type to a derived type.
11//===----------------------------------------------------------------------===//
12
19
20using namespace clang;
21using namespace ento;
22using namespace ast_matchers;
23
24namespace {
25static constexpr const char *const BaseNode = "BaseNode";
26static constexpr const char *const DerivedNode = "DerivedNode";
27static constexpr const char *const FromCastNode = "FromCast";
28static constexpr const char *const ToCastNode = "ToCast";
29static constexpr const char *const WarnRecordDecl = "WarnRecordDecl";
30
31class MemoryUnsafeCastChecker : public Checker<check::ASTCodeBody> {
32 BugType BT{this, "Unsafe cast", "WebKit coding guidelines"};
33
34public:
35 void checkASTCodeBody(const Decl *D, AnalysisManager &Mgr,
36 BugReporter &BR) const;
37};
38} // end namespace
39
40static void emitDiagnostics(const BoundNodes &Nodes, BugReporter &BR,
42 const MemoryUnsafeCastChecker *Checker,
43 const BugType &BT) {
44 const auto *CE = Nodes.getNodeAs<CastExpr>(WarnRecordDecl);
45 const NamedDecl *Base = Nodes.getNodeAs<NamedDecl>(BaseNode);
46 const NamedDecl *Derived = Nodes.getNodeAs<NamedDecl>(DerivedNode);
47 assert(CE && Base && Derived);
48
49 std::string Diagnostics;
50 llvm::raw_string_ostream OS(Diagnostics);
51 OS << "Unsafe cast from base type '" << Base->getNameAsString()
52 << "' to derived type '" << Derived->getNameAsString() << "'";
53 PathDiagnosticLocation BSLoc(CE->getSourceRange().getBegin(),
54 BR.getSourceManager());
55 auto Report = std::make_unique<BasicBugReport>(BT, OS.str(), BSLoc);
56 Report->addRange(CE->getSourceRange());
57 BR.emitReport(std::move(Report));
58}
59
60static void emitDiagnosticsUnrelated(const BoundNodes &Nodes, BugReporter &BR,
62 const MemoryUnsafeCastChecker *Checker,
63 const BugType &BT) {
64 const auto *CE = Nodes.getNodeAs<CastExpr>(WarnRecordDecl);
65 const NamedDecl *FromCast = Nodes.getNodeAs<NamedDecl>(FromCastNode);
66 const NamedDecl *ToCast = Nodes.getNodeAs<NamedDecl>(ToCastNode);
67 assert(CE && FromCast && ToCast);
68
69 std::string Diagnostics;
70 llvm::raw_string_ostream OS(Diagnostics);
71 OS << "Unsafe cast from type '" << FromCast->getNameAsString()
72 << "' to an unrelated type '" << ToCast->getNameAsString() << "'";
73 PathDiagnosticLocation BSLoc(CE->getSourceRange().getBegin(),
74 BR.getSourceManager());
75 auto Report = std::make_unique<BasicBugReport>(BT, OS.str(), BSLoc);
76 Report->addRange(CE->getSourceRange());
77 BR.emitReport(std::move(Report));
78}
79
80namespace clang {
81namespace ast_matchers {
82AST_MATCHER_P(StringLiteral, mentionsBoundType, std::string, BindingID) {
83 return Builder->removeBindings([this, &Node](const BoundNodesMap &Nodes) {
84 const auto &BN = Nodes.getNode(this->BindingID);
85 if (const auto *ND = BN.get<NamedDecl>()) {
86 return ND->getName() != Node.getString();
87 }
88 return true;
89 });
90}
91} // end namespace ast_matchers
92} // end namespace clang
93
94static decltype(auto) hasTypePointingTo(DeclarationMatcher DeclM) {
95 return hasType(pointerType(pointee(hasDeclaration(DeclM))));
96}
97
98void MemoryUnsafeCastChecker::checkASTCodeBody(const Decl *D,
100 BugReporter &BR) const {
101
102 AnalysisDeclContext *ADC = AM.getAnalysisDeclContext(D);
103
104 // Match downcasts from base type to derived type and warn
105 auto MatchExprPtr = allOf(
106 hasSourceExpression(hasTypePointingTo(cxxRecordDecl().bind(BaseNode))),
107 hasTypePointingTo(cxxRecordDecl(isDerivedFrom(equalsBoundNode(BaseNode)))
108 .bind(DerivedNode)),
109 unless(anyOf(hasSourceExpression(cxxThisExpr()),
111 auto MatchExprPtrObjC = allOf(
112 hasSourceExpression(ignoringImpCasts(hasType(objcObjectPointerType(
113 pointee(hasDeclaration(objcInterfaceDecl().bind(BaseNode))))))),
114 ignoringImpCasts(hasType(objcObjectPointerType(pointee(hasDeclaration(
115 objcInterfaceDecl(isDerivedFrom(equalsBoundNode(BaseNode)))
116 .bind(DerivedNode)))))));
117 auto MatchExprRefTypeDef =
118 allOf(hasSourceExpression(hasType(hasUnqualifiedDesugaredType(recordType(
119 hasDeclaration(decl(cxxRecordDecl().bind(BaseNode))))))),
120 hasType(hasUnqualifiedDesugaredType(recordType(hasDeclaration(
121 decl(cxxRecordDecl(isDerivedFrom(equalsBoundNode(BaseNode)))
122 .bind(DerivedNode)))))),
123 unless(anyOf(hasSourceExpression(hasDescendant(cxxThisExpr())),
124 hasType(templateTypeParmDecl()))));
125
126 auto ExplicitCast = explicitCastExpr(anyOf(MatchExprPtr, MatchExprRefTypeDef,
127 MatchExprPtrObjC))
128 .bind(WarnRecordDecl);
129 auto Cast = stmt(ExplicitCast);
130
131 auto Matches =
132 match(stmt(forEachDescendant(Cast)), *D->getBody(), AM.getASTContext());
133 for (BoundNodes Match : Matches)
134 emitDiagnostics(Match, BR, ADC, this, BT);
135
136 // Match casts between unrelated types and warn
137 auto MatchExprPtrUnrelatedTypes = allOf(
138 hasSourceExpression(
139 hasTypePointingTo(cxxRecordDecl().bind(FromCastNode))),
140 hasTypePointingTo(cxxRecordDecl().bind(ToCastNode)),
142 isSameOrDerivedFrom(equalsBoundNode(FromCastNode)))),
143 hasSourceExpression(hasTypePointingTo(cxxRecordDecl(
144 isSameOrDerivedFrom(equalsBoundNode(ToCastNode))))))));
145 auto MatchExprPtrObjCUnrelatedTypes = allOf(
146 hasSourceExpression(ignoringImpCasts(hasType(objcObjectPointerType(
147 pointee(hasDeclaration(objcInterfaceDecl().bind(FromCastNode))))))),
148 ignoringImpCasts(hasType(objcObjectPointerType(
149 pointee(hasDeclaration(objcInterfaceDecl().bind(ToCastNode)))))),
151 ignoringImpCasts(hasType(
153 isSameOrDerivedFrom(equalsBoundNode(FromCastNode)))))))),
154 hasSourceExpression(ignoringImpCasts(hasType(
156 isSameOrDerivedFrom(equalsBoundNode(ToCastNode))))))))))));
157 auto MatchExprRefTypeDefUnrelated = allOf(
158 hasSourceExpression(hasType(hasUnqualifiedDesugaredType(recordType(
159 hasDeclaration(decl(cxxRecordDecl().bind(FromCastNode))))))),
160 hasType(hasUnqualifiedDesugaredType(
161 recordType(hasDeclaration(decl(cxxRecordDecl().bind(ToCastNode)))))),
163 hasType(hasUnqualifiedDesugaredType(
165 isSameOrDerivedFrom(equalsBoundNode(FromCastNode)))))))),
166 hasSourceExpression(hasType(hasUnqualifiedDesugaredType(
168 isSameOrDerivedFrom(equalsBoundNode(ToCastNode))))))))))));
169
170 auto ExplicitCastUnrelated =
171 explicitCastExpr(anyOf(MatchExprPtrUnrelatedTypes,
172 MatchExprPtrObjCUnrelatedTypes,
173 MatchExprRefTypeDefUnrelated))
174 .bind(WarnRecordDecl);
175 auto CastUnrelated = stmt(ExplicitCastUnrelated);
176 auto MatchesUnrelatedTypes = match(stmt(forEachDescendant(CastUnrelated)),
177 *D->getBody(), AM.getASTContext());
178 for (BoundNodes Match : MatchesUnrelatedTypes)
179 emitDiagnosticsUnrelated(Match, BR, ADC, this, BT);
180}
181
182void ento::registerMemoryUnsafeCastChecker(CheckerManager &Mgr) {
183 Mgr.registerChecker<MemoryUnsafeCastChecker>();
184}
185
186bool ento::shouldRegisterMemoryUnsafeCastChecker(const CheckerManager &mgr) {
187 return true;
188}
#define AST_MATCHER_P(Type, DefineMatcher, ParamType, Param)
AST_MATCHER_P(Type, DefineMatcher, ParamType, Param) { ... } defines a single-parameter function name...
static void emitDiagnostics(const BoundNodes &Nodes, BugReporter &BR, AnalysisDeclContext *ADC, const MemoryUnsafeCastChecker *Checker, const BugType &BT)
static decltype(auto) hasTypePointingTo(DeclarationMatcher DeclM)
static void emitDiagnosticsUnrelated(const BoundNodes &Nodes, BugReporter &BR, AnalysisDeclContext *ADC, const MemoryUnsafeCastChecker *Checker, const BugType &BT)
static decltype(auto) hasTypePointingTo(DeclarationMatcher DeclM)
static void emitDiagnostics(BoundNodes &Match, const Decl *D, BugReporter &BR, AnalysisManager &AM, const ObjCAutoreleaseWriteChecker *Checker)
AnalysisDeclContext contains the context data for the function, method or block under analysis.
CastExpr - Base class for type casts, including both implicit casts (ImplicitCastExpr) and explicit c...
Definition Expr.h:3610
Decl - This represents one declaration (or definition), e.g.
Definition DeclBase.h:86
virtual Stmt * getBody() const
getBody - If this Decl represents a declaration for a body of code, such as a function or method defi...
Definition DeclBase.h:1087
This represents a decl that may have a name.
Definition Decl.h:273
Maps string IDs to AST nodes matched by parts of a matcher.
const T * getNodeAs(StringRef ID) const
Returns the AST node bound to ID.
ASTContext & getASTContext() override
AnalysisDeclContext * getAnalysisDeclContext(const Decl *D)
BugReporter is a utility class for generating PathDiagnostics for analysis.
const SourceManager & getSourceManager()
virtual void emitReport(std::unique_ptr< BugReport > R)
Add the given report to the set of reports tracked by BugReporter.
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::VariadicOperatorMatcherFunc< 1, 1 > unless
Matches if the provided matcher does not match.
const AstTypeMatcher< ObjCObjectPointerType > objcObjectPointerType
internal::Matcher< Decl > DeclarationMatcher
Types of matchers for the top-level classes in the AST class hierarchy.
const internal::ArgumentAdaptingMatcherFunc< internal::HasDescendantMatcher > hasDescendant
Matches AST nodes that have descendant AST nodes that match the provided matcher.
const internal::ArgumentAdaptingMatcherFunc< internal::ForEachDescendantMatcher > forEachDescendant
Matches AST nodes that have descendant AST nodes that match the provided matcher.
SmallVector< BoundNodes, 1 > match(MatcherT Matcher, const NodeT &Node, ASTContext &Context)
Returns the results of matching Matcher on Node.
const internal::VariadicDynCastAllOfMatcher< Decl, TemplateTypeParmDecl > templateTypeParmDecl
Matches template type parameter declarations.
const internal::VariadicDynCastAllOfMatcher< Stmt, ExplicitCastExpr > explicitCastExpr
Matches explicit cast expressions.
const internal::VariadicDynCastAllOfMatcher< Decl, ObjCInterfaceDecl > objcInterfaceDecl
Matches Objective-C interface declarations.
const AstTypeMatcher< PointerType > pointerType
const internal::VariadicOperatorMatcherFunc< 2, std::numeric_limits< unsigned >::max()> allOf
Matches if all given matchers match.
const AstTypeMatcher< RecordType > recordType
const internal::VariadicDynCastAllOfMatcher< Decl, CXXRecordDecl > cxxRecordDecl
Matches C++ class declarations.
const internal::VariadicAllOfMatcher< Decl > decl
Matches declarations.
internal::PolymorphicMatcher< internal::HasDeclarationMatcher, void(internal::HasDeclarationSupportedTypes), internal::Matcher< Decl > > hasDeclaration(const internal::Matcher< Decl > &InnerMatcher)
Matches a node if the declaration associated with that node matches the given matcher.
const internal::VariadicAllOfMatcher< Stmt > stmt
Matches statements.
const internal::VariadicOperatorMatcherFunc< 2, std::numeric_limits< unsigned >::max()> anyOf
Matches if any of the given matchers matches.
const internal::VariadicDynCastAllOfMatcher< Stmt, CXXThisExpr > cxxThisExpr
Matches implicit and explicit this expressions.
@ OS
Indicates that the tracking object is a descendant of a referenced-counted OSObject,...
bool Cast(InterpState &S, CodePtr OpPC)
Definition Interp.h:2500
The JSON file list parser is used to communicate input to InstallAPI.
@ Match
This is not an overload because the signature exactly matches an existing declaration.
Definition Sema.h:816