[clang] [analyzer] Add alpha.cplusplus.PlacementSelfCopy checker (PR #233525)
DonĂ¡t Nagy via cfe-commits
cfe-commits at lists.llvm.org
Thu Nov 26 01:17:02 PST 2026
================
@@ -0,0 +1,541 @@
+//===- PlacementSelfCopyChecker.cpp -----------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// Defines a checker that finds placement new expressions that access the
+// target storage through the old object while the new object is being
+// constructed, e.g. `::new (p) T(*p)`. A call that is not inlined is assumed
+// to access the old object passed to it; when it is the copy or move
+// constructor called by placement new itself, it is reported as a self-copy.
+//
+//===----------------------------------------------------------------------===//
+
+#include "clang/AST/ExprCXX.h"
+#include "clang/AST/ParentMap.h"
+#include "clang/Basic/Builtins.h"
+#include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
+#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
+#include "clang/StaticAnalyzer/Core/Checker.h"
+#include "clang/StaticAnalyzer/Core/CheckerManager.h"
+#include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
+#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
+
+using namespace clang;
+using namespace ento;
+
+using PlacementKey = std::pair<const CXXNewExpr *, const StackFrame *>;
+REGISTER_MAP_WITH_PROGRAMSTATE(PlacementTargets, PlacementKey,
+ const MemRegion *)
+// Variables that refer to the old storage, mapped to the placement target.
+REGISTER_MAP_WITH_PROGRAMSTATE(SourceVars, const MemRegion *, const MemRegion *)
+// Frames of methods called on the old storage, mapped to the placement target.
+REGISTER_MAP_WITH_PROGRAMSTATE(SourceFrames, const StackFrame *,
+ const MemRegion *)
+REGISTER_TRAIT_WITH_PROGRAMSTATE(PendingSelfCopy, const MemRegion *)
+
+namespace {
+class PlacementSelfCopyChecker
+ : public Checker<check::PostCall, check::PreCall, check::Bind,
+ check::Location, check::PostStmt<LambdaExpr>,
+ check::PostStmt<CXXNewExpr>> {
+ const BugType BT{this, "Placement new initialized from its own storage",
+ categories::MemoryError};
+ const BugType AccessBT{this, "Placement new accesses its own storage",
+ categories::MemoryError};
+
+ void reportAccess(const Expr *E, CheckerContext &C) const;
+ void reportSelfCopy(const Expr *Arg, CheckerContext &C) const;
+ void reportPendingSelfCopy(const CallEvent &Call, CheckerContext &C) const;
+
+public:
+ void checkPostCall(const CallEvent &Call, CheckerContext &C) const;
+ void checkPreCall(const CallEvent &Call, CheckerContext &C) const;
+ void checkBind(SVal Loc, SVal Val, const Stmt *S, bool AtDeclInit,
+ CheckerContext &C) const;
+ void checkLocation(SVal Loc, bool IsLoad, const Stmt *S,
+ CheckerContext &C) const;
+ void checkPostStmt(const LambdaExpr *LE, CheckerContext &C) const;
+ void checkPostStmt(const CXXNewExpr *NE, CheckerContext &C) const;
+};
+} // namespace
+
+namespace {
+struct Extent {
+ const MemRegion *Base;
+ int64_t Begin;
+ std::optional<int64_t> End;
+};
+} // namespace
+
+// Base class subobjects keep their own extent.
+static const MemRegion *stripCasts(const MemRegion *R) {
+ return R->StripCasts(/*StripBaseAndDerivedCasts=*/false);
+}
+
+static std::optional<Extent> getExtent(const MemRegion *R, ASTContext &Ctx) {
+ RegionOffset Offset = R->getAsOffset();
+ if (!Offset.isValid() || Offset.hasSymbolicOffset())
+ return std::nullopt;
+ Extent E{Offset.getRegion(), Offset.getOffset(), std::nullopt};
+ QualType Ty;
+ if (const auto *TR = dyn_cast<TypedValueRegion>(R))
+ Ty = TR->getValueType();
+ else if (const auto *SR = dyn_cast<SymbolicRegion>(R))
+ Ty = SR->getPointeeStaticType();
+ if (!Ty.isNull() && !Ty->isIncompleteType())
+ E.End = E.Begin + Ctx.getTypeSize(Ty);
+ return E;
+}
+
+// Returns the extents of A and B if both are known within the same base.
+static std::optional<std::pair<Extent, Extent>>
+getKnownExtents(const MemRegion *A, const MemRegion *B, ASTContext &Ctx) {
+ std::optional<Extent> EA = getExtent(A, Ctx), EB = getExtent(B, Ctx);
+ if (!EA || !EB || !EA->End || !EB->End || EA->Base != EB->Base)
+ return std::nullopt;
+ return std::make_pair(*EA, *EB);
+}
+
+static bool overlaps(const MemRegion *A, const MemRegion *B, ASTContext &Ctx) {
+ if (auto E = getKnownExtents(A, B, Ctx))
+ return E->first.Begin < *E->second.End && E->second.Begin < *E->first.End;
+ A = stripCasts(A);
+ return A == stripCasts(B) || A->isSubRegionOf(B) || B->isSubRegionOf(A);
----------------
NagyDonat wrote:
Rename the function to e.g. `overlapsTarget` or something similar to hint that it is intended for this special purpose. The comment and the parameter names are good at the definition (and they should stay), but at the call site this still appears to be a generic "do these two regions overlap?" function.
https://github.com/llvm/llvm-project/pull/233525
More information about the cfe-commits
mailing list