|
| 1 | +//===-- ImplicitAtomicsChecker.cpp - Null dereference checker -----------------===// |
| 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 defines NullDerefChecker, a builtin check in ExprEngine that performs |
| 10 | +// checks for null pointers at loads and stores. |
| 11 | +// |
| 12 | +//===----------------------------------------------------------------------===// |
| 13 | + |
| 14 | +#include "clang/AST/ExprObjC.h" |
| 15 | +#include "clang/AST/ExprOpenMP.h" |
| 16 | +#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h" |
| 17 | +#include "clang/StaticAnalyzer/Core/Checker.h" |
| 18 | +#include "clang/StaticAnalyzer/Core/CheckerManager.h" |
| 19 | +#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" |
| 20 | +#include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h" |
| 21 | +#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerHelpers.h" |
| 22 | +#include "llvm/ADT/SmallString.h" |
| 23 | +#include "llvm/Support/raw_ostream.h" |
| 24 | +#include "clang/StaticAnalyzer/Frontend/CheckerRegistry.h" |
| 25 | + |
| 26 | + |
| 27 | +using namespace clang; |
| 28 | +using namespace ento; |
| 29 | + |
| 30 | +namespace { |
| 31 | +class ImplicitAtomicsChecker |
| 32 | + : public Checker< check::PreStmt<CastExpr>, |
| 33 | + check::PreStmt<BinaryOperator>, |
| 34 | + check::PreStmt<UnaryOperator>, |
| 35 | + check::PreCall> { |
| 36 | + //check::Bind |
| 37 | + //check::Location |
| 38 | + BugType ImplicitAtomicsBugType{this, "Implicit Atomic seq_cst synchronization", "Atomics"}; |
| 39 | + |
| 40 | + void reportBug(const Stmt *S, CheckerContext &C) const; |
| 41 | + void reportBug(const Stmt *S, CheckerContext &C, StringRef desc) const; |
| 42 | + void reportBug(const CallEvent &S, CheckerContext &C, StringRef desc="") const; |
| 43 | + |
| 44 | +public: |
| 45 | + //void checkLocation(SVal location, bool isLoad, const Stmt* S, |
| 46 | + // CheckerContext &C) const; |
| 47 | + //void checkBind(SVal L, SVal V, const Stmt *S, CheckerContext &C) const; |
| 48 | + void checkPreStmt(const CastExpr *CE, CheckerContext &C) const; |
| 49 | + void checkPreStmt(const UnaryOperator *UOp, CheckerContext &C) const; |
| 50 | + void checkPreStmt(const BinaryOperator *BOp, CheckerContext &C) const; |
| 51 | + void checkPreCall(const CallEvent &Call, CheckerContext &C) const; |
| 52 | +}; |
| 53 | +} // end anonymous namespace |
| 54 | + |
| 55 | +// Checks if RD has name in Names and is in std namespace |
| 56 | +static bool hasStdClassWithName(const CXXRecordDecl *RD, |
| 57 | + ArrayRef<llvm::StringLiteral> Names) { |
| 58 | + // or could check ASTContext::getQualifiedTemplateName()->isDerivedFrom() ? |
| 59 | + if (!RD || !RD->getDeclContext()->isStdNamespace()) |
| 60 | + return false; |
| 61 | + if (RD->getDeclName().isIdentifier()) { |
| 62 | + StringRef Name = RD->getName(); |
| 63 | + return llvm::any_of(Names, [&Name](StringRef GivenName) -> bool { |
| 64 | + return Name == GivenName; |
| 65 | + }); |
| 66 | + } |
| 67 | + return false; |
| 68 | +} |
| 69 | + |
| 70 | +constexpr llvm::StringLiteral STD_PTR_NAMES[] = {"atomic", "atomic_ref"}; |
| 71 | + |
| 72 | +static bool isStdAtomic(const CXXRecordDecl *RD) { |
| 73 | + return hasStdClassWithName(RD, STD_PTR_NAMES); |
| 74 | +} |
| 75 | + |
| 76 | +static bool isStdAtomicCall(const Expr *E) { |
| 77 | + return E && isStdAtomic(E->IgnoreImplicit()->getType()->getAsCXXRecordDecl()); |
| 78 | +} |
| 79 | + |
| 80 | +static bool isStdAtomic(const Expr *E) { |
| 81 | + return E->getType()->isAtomicType(); |
| 82 | +} |
| 83 | + |
| 84 | +void ImplicitAtomicsChecker::reportBug(const CallEvent &S, CheckerContext &C, StringRef desc) const { |
| 85 | + reportBug(S.getOriginExpr(), C, desc); |
| 86 | +} |
| 87 | + |
| 88 | +// try to find the "best" node to attach this to, so we generate fewer duplicate reports |
| 89 | +void ImplicitAtomicsChecker::reportBug(const Stmt *S, CheckerContext &C) const { |
| 90 | + while (1) { |
| 91 | + const auto *expr = dyn_cast<Expr>(S); |
| 92 | + if (!expr) |
| 93 | + break; |
| 94 | + expr = expr->IgnoreParenCasts(); |
| 95 | + if (const auto *UO = dyn_cast<UnaryOperator>(expr)) |
| 96 | + S = UO->getSubExpr(); |
| 97 | + else if (const auto *BO = dyn_cast<BinaryOperator>(expr)) |
| 98 | + S = isStdAtomic(BO->getLHS()) ? BO->getLHS() : |
| 99 | + isStdAtomic(BO->getRHS()) ? BO->getRHS() : |
| 100 | + BO->getLHS(); |
| 101 | + else |
| 102 | + break; |
| 103 | + } |
| 104 | + reportBug(S, C, ""); |
| 105 | +} |
| 106 | + |
| 107 | +void ImplicitAtomicsChecker::reportBug(const Stmt *S, CheckerContext &C, StringRef desc) const { |
| 108 | + SmallString<100> buf; |
| 109 | + llvm::raw_svector_ostream os(buf); |
| 110 | + os << ImplicitAtomicsBugType.getDescription() << desc; |
| 111 | + PathDiagnosticLocation N = PathDiagnosticLocation::createBegin( |
| 112 | + S, C.getSourceManager(), C.getLocationContext()); |
| 113 | + auto report = std::make_unique<BasicBugReport>(ImplicitAtomicsBugType, buf.str(), N); |
| 114 | + C.emitReport(std::move(report)); |
| 115 | +} |
| 116 | + |
| 117 | +void ImplicitAtomicsChecker::checkPreStmt(const CastExpr *CE, CheckerContext &C) const { |
| 118 | + //if (isStdAtomic(CE) != isStdAtomic(CE->getSubExpr())) { // AtomicToNonAtomic or NonAtomicToAtomic CastExpr |
| 119 | + if (CE->getCastKind() == CK_AtomicToNonAtomic) { |
| 120 | + reportBug(CE, C); |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +void ImplicitAtomicsChecker::checkPreStmt(const UnaryOperator *UOp, |
| 125 | + CheckerContext &C) const { |
| 126 | + if (UOp->getOpcode() == UO_AddrOf) |
| 127 | + return; |
| 128 | + const Expr *Sub = UOp->getSubExpr(); |
| 129 | + if (isStdAtomic(UOp) || isStdAtomic(Sub)) |
| 130 | + reportBug(UOp, C); |
| 131 | +} |
| 132 | + |
| 133 | +void ImplicitAtomicsChecker::checkPreStmt(const BinaryOperator *BOp, |
| 134 | + CheckerContext &C) const { |
| 135 | + const Expr *Lhs = BOp->getLHS(); |
| 136 | + const Expr *Rhs = BOp->getRHS(); |
| 137 | + if (isStdAtomic(Lhs) || isStdAtomic(Rhs) || isStdAtomic(BOp)) |
| 138 | + reportBug(BOp, C); |
| 139 | +} |
| 140 | + |
| 141 | +void ImplicitAtomicsChecker::checkPreCall(const CallEvent &Call, |
| 142 | + CheckerContext &C) const { |
| 143 | + const auto *MC = dyn_cast<CXXInstanceCall>(&Call); |
| 144 | + if (!MC || !isStdAtomicCall(MC->getCXXThisExpr())) |
| 145 | + return; |
| 146 | + if (const auto *OC = dyn_cast<CXXMemberOperatorCall>(&Call)) { |
| 147 | + OverloadedOperatorKind OOK = OC->getOverloadedOperator(); |
| 148 | + if (CXXOperatorCallExpr::isAssignmentOp(OOK) || OOK == OO_PlusPlus || OOK == OO_MinusMinus) { |
| 149 | + reportBug(Call, C, " (std::atomic)"); |
| 150 | + } |
| 151 | + } |
| 152 | + else if (const auto *Convert = dyn_cast<CXXConversionDecl>(MC->getDecl())) { |
| 153 | + reportBug(Call, C, " (std::atomic)"); |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | + |
| 158 | +//// These seem probably unnecessary: |
| 159 | +// |
| 160 | +//static const Expr *getDereferenceExpr(const Stmt *S, bool IsBind=false) { |
| 161 | +// const Expr *E = nullptr; |
| 162 | +// |
| 163 | +// // Walk through lvalue casts to get the original expression |
| 164 | +// // that syntactically caused the load. |
| 165 | +// if (const Expr *expr = dyn_cast<Expr>(S)) |
| 166 | +// E = expr->IgnoreParenLValueCasts(); |
| 167 | +// |
| 168 | +// if (IsBind) { |
| 169 | +// const VarDecl *VD; |
| 170 | +// const Expr *Init; |
| 171 | +// std::tie(VD, Init) = parseAssignment(S); |
| 172 | +// if (VD && Init) |
| 173 | +// E = Init; |
| 174 | +// } |
| 175 | +// return E; |
| 176 | +//} |
| 177 | +// |
| 178 | +//// load or bare symbol |
| 179 | +//void ImplicitAtomicsChecker::checkLocation(SVal l, bool isLoad, const Stmt* S, |
| 180 | +// CheckerContext &C) const { |
| 181 | +// const Expr *expr = getDereferenceExpr(S); |
| 182 | +// assert(expr); |
| 183 | +// if (isStdAtomic(expr)) |
| 184 | +// reportBug(S, C); |
| 185 | +//} |
| 186 | +// |
| 187 | +//// auto &r = *l, or store |
| 188 | +//void ImplicitAtomicsChecker::checkBind(SVal L, SVal V, const Stmt *S, |
| 189 | +// CheckerContext &C) const { |
| 190 | +// const Expr *expr = getDereferenceExpr(S, /*IsBind=*/true); |
| 191 | +// assert(expr); |
| 192 | +// if (isStdAtomic(expr)) |
| 193 | +// reportBug(S, C, " (bind)"); |
| 194 | +//} |
| 195 | + |
| 196 | +namespace clang { |
| 197 | +namespace ento { |
| 198 | +void registerImplicitAtomicsChecker(CheckerManager &mgr) { |
| 199 | + mgr.registerChecker<ImplicitAtomicsChecker>(); |
| 200 | +} |
| 201 | +bool shouldRegisterImplicitAtomicsChecker(const CheckerManager &mgr) { |
| 202 | + return true; |
| 203 | +} |
| 204 | +} // namespace ento |
| 205 | +} // namespace clang |
| 206 | + |
| 207 | +#ifdef CLANG_PLUGIN |
| 208 | +extern "C" const char clang_analyzerAPIVersionString[] = |
| 209 | + CLANG_ANALYZER_API_VERSION_STRING; |
| 210 | +extern "C" void clang_registerCheckers(CheckerRegistry ®istry) { |
| 211 | + registry.addChecker<ImplicitAtomicsChecker>( |
| 212 | + "julia.ImplicitAtomics", "Flags implicit atomic operations", "" |
| 213 | + ); |
| 214 | +} |
| 215 | +#endif |
0 commit comments