[clang] [AST] Avoid base-to-derived cast in Redeclarable constructor (PR #238335)

Romaric Jodin via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 3 05:53:38 PST 2026


https://github.com/rjodinchr updated https://github.com/llvm/llvm-project/pull/238335

>From b38d3702a231ebc7591e4c04a95514f04b3ca2ea Mon Sep 17 00:00:00 2001
From: Romaric Jodin <rjodin at google.com>
Date: Tue, 3 Nov 2026 11:53:06 +0100
Subject: [PATCH] [AST] Avoid base-to-derived cast in Redeclarable constructor

Redeclarable<decl_type>::Redeclarable previously initialized First by
evaluating static_cast<decl_type *>(this) inside the Redeclarable base
class constructor.

At the point where the Redeclarable base subobject constructor runs, the
derived decl_type constructor has not yet begun execution and the object
vtable pointer has not yet been updated to decl_type. When compiled with
Control Flow Integrity enabled for derived casts
(-fsanitize=cfi-derived-cast), downcasting this from Redeclarable<T> *
to decl_type * during base class construction fails the CFI vtable check
at runtime.

Pass the derived this pointer (which already has static type
decl_type *) directly from each CRTP subclass constructor to
Redeclarable(const ASTContext &Ctx, decl_type *First). This avoids any
base-to-derived downcast during construction while keeping
getFirstDecl() a branchless load. Add a unit test in DeclTest verifying
getFirstDecl(), isFirstDecl(), and getPreviousDecl() across
redeclarations.
---
 clang/include/clang/AST/Decl.h         |  2 +-
 clang/include/clang/AST/DeclCXX.h      |  4 ++--
 clang/include/clang/AST/DeclTemplate.h |  3 ++-
 clang/include/clang/AST/Redeclarable.h |  5 ++---
 clang/lib/AST/Decl.cpp                 |  9 +++++----
 clang/lib/AST/DeclCXX.cpp              |  6 +++---
 clang/lib/AST/DeclObjC.cpp             |  4 ++--
 clang/unittests/AST/DeclTest.cpp       | 14 ++++++++++++++
 8 files changed, 31 insertions(+), 16 deletions(-)

diff --git a/clang/include/clang/AST/Decl.h b/clang/include/clang/AST/Decl.h
index 87418ac804c43b..f9d3ee7f4067b0 100644
--- a/clang/include/clang/AST/Decl.h
+++ b/clang/include/clang/AST/Decl.h
@@ -3728,7 +3728,7 @@ class TypedefNameDecl : public TypeDecl, public Redeclarable<TypedefNameDecl> {
   TypedefNameDecl(Kind DK, ASTContext &C, DeclContext *DC,
                   SourceLocation StartLoc, SourceLocation IdLoc,
                   const IdentifierInfo *Id, TypeSourceInfo *TInfo)
-      : TypeDecl(DK, DC, IdLoc, Id, StartLoc), redeclarable_base(C),
+      : TypeDecl(DK, DC, IdLoc, Id, StartLoc), redeclarable_base(C, this),
         MaybeModedTInfo(TInfo, 0) {}
 
   using redeclarable_base = Redeclarable<TypedefNameDecl>;
diff --git a/clang/include/clang/AST/DeclCXX.h b/clang/include/clang/AST/DeclCXX.h
index cf61653728cb65..a71a6b8e4102db 100644
--- a/clang/include/clang/AST/DeclCXX.h
+++ b/clang/include/clang/AST/DeclCXX.h
@@ -3251,8 +3251,8 @@ class NamespaceAliasDecl : public NamespaceBaseDecl,
                      IdentifierInfo *Alias, NestedNameSpecifierLoc QualifierLoc,
                      SourceLocation IdentLoc, NamespaceBaseDecl *Namespace)
       : NamespaceBaseDecl(NamespaceAlias, DC, AliasLoc, Alias),
-        redeclarable_base(C), NamespaceLoc(NamespaceLoc), IdentLoc(IdentLoc),
-        QualifierLoc(QualifierLoc), Namespace(Namespace) {}
+        redeclarable_base(C, this), NamespaceLoc(NamespaceLoc),
+        IdentLoc(IdentLoc), QualifierLoc(QualifierLoc), Namespace(Namespace) {}
 
   void anchor() override;
 
diff --git a/clang/include/clang/AST/DeclTemplate.h b/clang/include/clang/AST/DeclTemplate.h
index 1a5ed48874dddb..4523c02ee89eca 100644
--- a/clang/include/clang/AST/DeclTemplate.h
+++ b/clang/include/clang/AST/DeclTemplate.h
@@ -819,7 +819,8 @@ class RedeclarableTemplateDecl : public TemplateDecl,
   RedeclarableTemplateDecl(Kind DK, ASTContext &C, DeclContext *DC,
                            SourceLocation L, DeclarationName Name,
                            TemplateParameterList *Params, NamedDecl *Decl)
-      : TemplateDecl(DK, DC, L, Name, Params, Decl), redeclarable_base(C) {}
+      : TemplateDecl(DK, DC, L, Name, Params, Decl),
+        redeclarable_base(C, this) {}
 
 public:
   friend class ASTDeclReader;
diff --git a/clang/include/clang/AST/Redeclarable.h b/clang/include/clang/AST/Redeclarable.h
index fe2648aa58301a..2792ae36e83bbf 100644
--- a/clang/include/clang/AST/Redeclarable.h
+++ b/clang/include/clang/AST/Redeclarable.h
@@ -191,9 +191,8 @@ class Redeclarable {
   friend class ASTDeclWriter;
   friend class IncrementalParser;
 
-  Redeclarable(const ASTContext &Ctx)
-      : RedeclLink(LatestDeclLink(Ctx)),
-        First(static_cast<decl_type *>(this)) {}
+  Redeclarable(const ASTContext &Ctx, decl_type *First)
+      : RedeclLink(LatestDeclLink(Ctx)), First(First) {}
 
   /// Return the previous declaration of this declaration or NULL if this
   /// is the first declaration.
diff --git a/clang/lib/AST/Decl.cpp b/clang/lib/AST/Decl.cpp
index c6c2043614aa90..f652a39d6bd4d0 100644
--- a/clang/lib/AST/Decl.cpp
+++ b/clang/lib/AST/Decl.cpp
@@ -104,7 +104,7 @@ bool Decl::isOutOfLine() const {
 
 TranslationUnitDecl::TranslationUnitDecl(ASTContext &ctx)
     : Decl(TranslationUnit, nullptr, SourceLocation()),
-      DeclContext(TranslationUnit), redeclarable_base(ctx), Ctx(ctx) {}
+      DeclContext(TranslationUnit), redeclarable_base(ctx, this), Ctx(ctx) {}
 
 //===----------------------------------------------------------------------===//
 // NamedDecl Implementation
@@ -2117,7 +2117,7 @@ VarDecl::VarDecl(Kind DK, ASTContext &C, DeclContext *DC,
                  const IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
                  StorageClass SC)
     : DeclaratorDecl(DK, DC, IdLoc, Id, T, TInfo, StartLoc),
-      redeclarable_base(C) {
+      redeclarable_base(C, this) {
   static_assert(sizeof(VarDeclBitfields) <= sizeof(unsigned),
                 "VarDeclBitfields too large!");
   static_assert(sizeof(ParmVarDeclBitfields) <= sizeof(unsigned),
@@ -3091,7 +3091,7 @@ FunctionDecl::FunctionDecl(Kind DK, ASTContext &C, DeclContext *DC,
                            const AssociatedConstraint &TrailingRequiresClause)
     : DeclaratorDecl(DK, DC, NameInfo.getLoc(), NameInfo.getName(), T, TInfo,
                      StartLoc),
-      DeclContext(DK), redeclarable_base(C), Body(), ODRHash(0),
+      DeclContext(DK), redeclarable_base(C, this), Body(), ODRHash(0),
       EndRangeLoc(NameInfo.getEndLoc()), DNLoc(NameInfo.getInfo()) {
   assert(T.isNull() || T->isFunctionType());
   FunctionDeclBits.SClass = S;
@@ -4969,7 +4969,8 @@ const FieldDecl *FieldDecl::findCountedByField() const {
 TagDecl::TagDecl(Kind DK, TagKind TK, const ASTContext &C, DeclContext *DC,
                  SourceLocation L, IdentifierInfo *Id, TagDecl *PrevDecl,
                  SourceLocation StartL)
-    : TypeDecl(DK, DC, L, Id, StartL), DeclContext(DK), redeclarable_base(C),
+    : TypeDecl(DK, DC, L, Id, StartL), DeclContext(DK),
+      redeclarable_base(C, this),
       TypedefNameDeclOrQualifier((TypedefNameDecl *)nullptr) {
   assert((DK != Enum || TK == TagTypeKind::Enum) &&
          "EnumDecl not matched with TagTypeKind::Enum");
diff --git a/clang/lib/AST/DeclCXX.cpp b/clang/lib/AST/DeclCXX.cpp
index ac468f6879f7b0..b60a348f3e6874 100644
--- a/clang/lib/AST/DeclCXX.cpp
+++ b/clang/lib/AST/DeclCXX.cpp
@@ -3365,7 +3365,7 @@ NamespaceDecl::NamespaceDecl(ASTContext &C, DeclContext *DC, bool Inline,
                              IdentifierInfo *Id, NamespaceDecl *PrevDecl,
                              bool Nested)
     : NamespaceBaseDecl(Namespace, DC, IdLoc, Id), DeclContext(Namespace),
-      redeclarable_base(C), LocStart(StartLoc) {
+      redeclarable_base(C, this), LocStart(StartLoc) {
   setInline(Inline);
   setNested(Nested);
   setPreviousDecl(PrevDecl);
@@ -3466,7 +3466,7 @@ void UsingShadowDecl::anchor() {}
 UsingShadowDecl::UsingShadowDecl(Kind K, ASTContext &C, DeclContext *DC,
                                  SourceLocation Loc, DeclarationName Name,
                                  BaseUsingDecl *Introducer, NamedDecl *Target)
-    : NamedDecl(K, DC, Loc, Name), redeclarable_base(C),
+    : NamedDecl(K, DC, Loc, Name), redeclarable_base(C, this),
       UsingOrNextShadow(Introducer) {
   if (Target) {
     assert(!isa<UsingShadowDecl>(Target));
@@ -3477,7 +3477,7 @@ UsingShadowDecl::UsingShadowDecl(Kind K, ASTContext &C, DeclContext *DC,
 
 UsingShadowDecl::UsingShadowDecl(Kind K, ASTContext &C, EmptyShell Empty)
     : NamedDecl(K, nullptr, SourceLocation(), DeclarationName()),
-      redeclarable_base(C) {}
+      redeclarable_base(C, this) {}
 
 UsingShadowDecl *UsingShadowDecl::CreateDeserialized(ASTContext &C,
                                                      GlobalDeclID ID) {
diff --git a/clang/lib/AST/DeclObjC.cpp b/clang/lib/AST/DeclObjC.cpp
index 1adf6c9e048a97..5d76bca871e1e2 100644
--- a/clang/lib/AST/DeclObjC.cpp
+++ b/clang/lib/AST/DeclObjC.cpp
@@ -1584,7 +1584,7 @@ ObjCInterfaceDecl::ObjCInterfaceDecl(
     const IdentifierInfo *Id, ObjCTypeParamList *typeParamList,
     SourceLocation CLoc, ObjCInterfaceDecl *PrevDecl, bool IsInternal)
     : ObjCContainerDecl(ObjCInterface, DC, Id, CLoc, AtLoc),
-      redeclarable_base(C) {
+      redeclarable_base(C, this) {
   setPreviousDecl(PrevDecl);
 
   // Copy the 'data' pointer over.
@@ -1951,7 +1951,7 @@ ObjCProtocolDecl::ObjCProtocolDecl(ASTContext &C, DeclContext *DC,
                                    SourceLocation atStartLoc,
                                    ObjCProtocolDecl *PrevDecl)
     : ObjCContainerDecl(ObjCProtocol, DC, Id, nameLoc, atStartLoc),
-      redeclarable_base(C) {
+      redeclarable_base(C, this) {
   setPreviousDecl(PrevDecl);
   if (PrevDecl)
     Data = PrevDecl->Data;
diff --git a/clang/unittests/AST/DeclTest.cpp b/clang/unittests/AST/DeclTest.cpp
index 195b8ab4c4e661..6539c4e8fcbccf 100644
--- a/clang/unittests/AST/DeclTest.cpp
+++ b/clang/unittests/AST/DeclTest.cpp
@@ -1029,3 +1029,17 @@ TEST(Decl, ObjCPropertyDeclNameForDiagnostic) {
                              /*Qualified=*/true);
   EXPECT_EQ(ExtPQualifiedOS.str(), "-[MyClass extensionProp]");
 }
+
+TEST(Decl, RedeclarableFirstDecl) {
+  auto AST = tooling::buildASTFromCode("void f(); void f() {}");
+  ASTContext &Ctx = AST->getASTContext();
+
+  auto *FD = const_cast<FunctionDecl *>(selectFirst<FunctionDecl>(
+      "f", match(functionDecl(hasName("f"), isDefinition()).bind("f"), Ctx)));
+  ASSERT_NE(FD, nullptr);
+  FunctionDecl *First = FD->getFirstDecl();
+  ASSERT_NE(First, nullptr);
+  EXPECT_TRUE(First->isFirstDecl());
+  EXPECT_FALSE(FD->isFirstDecl());
+  EXPECT_EQ(FD->getPreviousDecl(), First);
+}



More information about the cfe-commits mailing list