xref: /freebsd/contrib/llvm-project/clang/lib/StaticAnalyzer/Checkers/OSObjectCStyleCast.cpp (revision 6be3386466ab79a84b48429ae66244f21526d3df)
1 //===- OSObjectCStyleCast.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 OSObjectCStyleCast checker, which checks for C-style casts
10 // of OSObjects. Such casts almost always indicate a code smell,
11 // as an explicit static or dynamic cast should be used instead.
12 //===----------------------------------------------------------------------===//
13 
14 #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
15 #include "clang/ASTMatchers/ASTMatchFinder.h"
16 #include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
17 #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
18 #include "clang/StaticAnalyzer/Core/Checker.h"
19 #include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
20 #include "llvm/Support/Debug.h"
21 
22 using namespace clang;
23 using namespace ento;
24 using namespace ast_matchers;
25 
26 namespace {
27 
28 const char *WarnAtNode = "OSObjCast";
29 
30 class OSObjectCStyleCastChecker : public Checker<check::ASTCodeBody> {
31 public:
32   void checkASTCodeBody(const Decl *D,
33                         AnalysisManager &AM,
34                         BugReporter &BR) const;
35 };
36 
37 static void emitDiagnostics(const BoundNodes &Nodes,
38                             BugReporter &BR,
39                             AnalysisDeclContext *ADC,
40                             const OSObjectCStyleCastChecker *Checker) {
41   const auto *CE = Nodes.getNodeAs<CastExpr>(WarnAtNode);
42   assert(CE);
43 
44   std::string Diagnostics;
45   llvm::raw_string_ostream OS(Diagnostics);
46   OS << "C-style cast of OSObject. Use OSDynamicCast instead.";
47 
48   BR.EmitBasicReport(
49     ADC->getDecl(),
50     Checker,
51     /*Name=*/"OSObject C-Style Cast",
52     /*BugCategory=*/"Security",
53     OS.str(),
54     PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), ADC),
55     CE->getSourceRange());
56 }
57 
58 static decltype(auto) hasTypePointingTo(DeclarationMatcher DeclM) {
59   return hasType(pointerType(pointee(hasDeclaration(DeclM))));
60 }
61 
62 void OSObjectCStyleCastChecker::checkASTCodeBody(const Decl *D, AnalysisManager &AM,
63                                                  BugReporter &BR) const {
64 
65   AnalysisDeclContext *ADC = AM.getAnalysisDeclContext(D);
66 
67   auto DynamicCastM = callExpr(callee(functionDecl(hasName("safeMetaCast"))));
68 
69   auto OSObjTypeM = hasTypePointingTo(cxxRecordDecl(isDerivedFrom("OSMetaClassBase")));
70   auto OSObjSubclassM = hasTypePointingTo(
71     cxxRecordDecl(isDerivedFrom("OSObject")));
72 
73   auto CastM = cStyleCastExpr(
74           allOf(hasSourceExpression(allOf(OSObjTypeM, unless(DynamicCastM))),
75           OSObjSubclassM)).bind(WarnAtNode);
76 
77   auto Matches = match(stmt(forEachDescendant(CastM)), *D->getBody(), AM.getASTContext());
78   for (BoundNodes Match : Matches)
79     emitDiagnostics(Match, BR, ADC, this);
80 }
81 }
82 
83 void ento::registerOSObjectCStyleCast(CheckerManager &Mgr) {
84   Mgr.registerChecker<OSObjectCStyleCastChecker>();
85 }
86 
87 bool ento::shouldRegisterOSObjectCStyleCast(const CheckerManager &mgr) {
88   return true;
89 }
90