[clang] [compiler-rt] [Clang][CFI] Only normalize integer types `cfi-icall` metadata (PR #242466)

Chandler Carruth via cfe-commits cfe-commits at lists.llvm.org
Fri Nov 13 18:52:02 PST 2026


https://github.com/chandlerc updated https://github.com/llvm/llvm-project/pull/242466

>From 423c41494f51c6f434f541332e76629ca4c40672 Mon Sep 17 00:00:00 2001
From: Chandler Carruth <chandlerc at gmail.com>
Date: Fri, 13 Nov 2026 18:35:17 +0000
Subject: [PATCH 1/3] [Clang][CFI] Only normalize integer types on function
 type metadata

When using `-fsanitize-cfi-icall-experimental-normalize-integers`, its
normalization was applied unconditionally when creating metadata,
appending `.normalized` and normalizing parts of C++ class vtable
`!type` metadata, virtual member-pointer metadata, and virtual call
checks.

This would cause ThinLTO to see vtable `!type` identifiers that mismatch
RTTI type-infos. Best case, this blocked whole-program devirtualization,
but in the worst case (mixed use of normalization) it would cause WPD to
make unsound transforms. =[

Restricting the normalization and `.normalized` suffix to function type
metadata fixes these issues.

Added both a direct test and a CFI test that actually uses ThinLTO and
would break without this.

Assisted-by: Antigravity with Gemini
---
 clang/lib/CodeGen/CodeGenModule.cpp           |   8 +-
 .../CodeGenCXX/cfi-icall-normalize-vtable.cpp |  77 +++++++++
 .../test/cfi/normalize-integers-wpd.cpp       | 157 ++++++++++++++++++
 3 files changed, 239 insertions(+), 3 deletions(-)
 create mode 100644 clang/test/CodeGenCXX/cfi-icall-normalize-vtable.cpp
 create mode 100644 compiler-rt/test/cfi/normalize-integers-wpd.cpp

diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp
index 3b8ec02a94e54..c3dbdfeace4fe 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -8743,10 +8743,12 @@ llvm::Metadata *CodeGenModule::CreateMetadataIdentifierImpl(
   if (ForceString || isExternallyVisible(T->getLinkage())) {
     std::string OutName;
     llvm::raw_string_ostream Out(OutName);
-    getCXXABI().getMangleContext().mangleCanonicalTypeName(
-        T, Out, getCodeGenOpts().SanitizeCfiICallNormalizeIntegers);
+    bool NormalizeIntegers = T->isFunctionType() &&
+                             getCodeGenOpts().SanitizeCfiICallNormalizeIntegers;
+    getCXXABI().getMangleContext().mangleCanonicalTypeName(T, Out,
+                                                           NormalizeIntegers);
 
-    if (getCodeGenOpts().SanitizeCfiICallNormalizeIntegers)
+    if (NormalizeIntegers)
       Out << ".normalized";
 
     Out << Suffix;
diff --git a/clang/test/CodeGenCXX/cfi-icall-normalize-vtable.cpp b/clang/test/CodeGenCXX/cfi-icall-normalize-vtable.cpp
new file mode 100644
index 0000000000000..2225e067f35ad
--- /dev/null
+++ b/clang/test/CodeGenCXX/cfi-icall-normalize-vtable.cpp
@@ -0,0 +1,77 @@
+// RUN: %clang_cc1 -triple x86_64-unknown-linux -fsanitize=kcfi -fwhole-program-vtables -flto=thin -flto-unit -fsanitize-cfi-icall-experimental-normalize-integers -emit-llvm -o - %s | FileCheck %s --check-prefixes=CHECK,KCFI --implicit-check-not=.normalized.virtual
+// RUN: %clang_cc1 -triple x86_64-unknown-linux -fsanitize=cfi-icall,cfi-vcall,cfi-mfcall -fsanitize-trap=cfi-icall,cfi-vcall,cfi-mfcall -fwhole-program-vtables -flto=thin -flto-unit -fsanitize-cfi-icall-experimental-normalize-integers -emit-llvm -o - %s | FileCheck %s --check-prefixes=CHECK,CFI,CFI-PUBLIC --implicit-check-not=.normalized.virtual
+// RUN: %clang_cc1 -triple x86_64-unknown-linux -fvisibility=hidden -fsanitize=cfi-icall,cfi-vcall,cfi-mfcall -fsanitize-trap=cfi-icall,cfi-vcall,cfi-mfcall -fwhole-program-vtables -flto=thin -flto-unit -fsanitize-cfi-icall-experimental-normalize-integers -emit-llvm -o - %s | FileCheck %s --check-prefixes=CHECK,CFI,CFI-HIDDEN --implicit-check-not=.normalized.virtual
+
+// Verify that -fsanitize-cfi-icall-experimental-normalize-integers only
+// normalizes FunctionType metadata and does not alter class vtable !type
+// metadata or virtual member-pointer .virtual metadata.
+
+struct Base {
+  virtual int get(int x);
+};
+
+struct Derived : Base {
+  int get(int x) override;
+};
+
+template <typename T>
+struct Box {
+  virtual T val(T x);
+};
+
+int Base::get(int x) { return x; }
+int Derived::get(int x) { return x + 1; }
+template <typename T>
+T Box<T>::val(T x) { return x; }
+template struct Box<int>;
+
+void icall_target(int x) {}
+
+int test_calls(Base *b, int (Base::*mp)(int), Box<int> *box, void (*fp)(int), int x) {
+  int r = b->get(x);
+  r += (b->*mp)(x);
+  r += box->val(x);
+  fp(x);
+  return r;
+}
+
+void take_addr() {
+  void (*fp)(int) = &icall_target;
+  fp(1);
+}
+
+// CHECK-NOT: .normalized.virtual
+// CHECK-NOT: _ZTS4Base.normalized
+// CHECK-NOT: _ZTS7Derived.normalized
+// CHECK-NOT: _ZTS3BoxIu3i32E
+// CHECK-NOT: _ZTS3BoxIiE.normalized
+// CHECK-NOT: _ZTSM4BaseFuu3i32E
+// CHECK-NOT: _ZTSM4BaseFiiE.normalized.virtual
+
+// CHECK-DAG: @_ZTV4Base = {{.*}}!type ![[BASE_TYPE:[0-9]+]], !type ![[BASE_VMEMPTR:[0-9]+]]
+// CHECK-DAG: @_ZTV7Derived = {{.*}}!type ![[BASE_TYPE]], !type ![[BASE_VMEMPTR]], !type ![[DERIVED_TYPE:[0-9]+]], !type ![[DERIVED_VMEMPTR:[0-9]+]]
+// CHECK-DAG: @_ZTV3BoxIiE = {{.*}}!type ![[BOX_TYPE:[0-9]+]], !type ![[BOX_VMEMPTR:[0-9]+]]
+
+// CFI-DAG: define{{.*}} void @_Z12icall_targeti(i32 {{.*}}){{.*}} !type ![[FN_TYPE:[0-9]+]] !type ![[FN_GEN_TYPE:[0-9]+]]
+// KCFI-DAG: define{{.*}} void @_Z12icall_targeti(i32 {{.*}}){{.*}} !kcfi_type ![[KCFI_FN_TYPE:[0-9]+]]
+
+// CHECK-LABEL: define{{.*}} i32 @_Z10test_callsP4BaseMS_FiiEP3BoxIiEPFviEi(
+// KCFI-DAG: call i1 @llvm.public.type.test(ptr {{.*}}, metadata !"_ZTS4Base")
+// KCFI-DAG: call i1 @llvm.public.type.test(ptr {{.*}}, metadata !"_ZTS3BoxIiE")
+// KCFI-DAG: call void %{{.*}}(i32 {{.*}}) [ "kcfi"(i32 1162514891) ]
+// CFI-PUBLIC-DAG: call i1 @llvm.public.type.test(ptr {{.*}}, metadata !"_ZTSM4BaseFiiE.virtual")
+// CFI-HIDDEN-DAG: call { ptr, i1 } @llvm.type.checked.load(ptr {{.*}}, i32 0, metadata !"_ZTS4Base")
+// CFI-HIDDEN-DAG: call i1 @llvm.type.test(ptr {{.*}}, metadata !"_ZTSM4BaseFiiE.virtual")
+// CFI-HIDDEN-DAG: call i1 @llvm.type.test(ptr {{.*}}, metadata !"_ZTSM4BaseFiiE")
+// CFI-HIDDEN-DAG: call { ptr, i1 } @llvm.type.checked.load(ptr {{.*}}, i32 0, metadata !"_ZTS3BoxIiE")
+// CFI-DAG: call i1 @llvm.type.test(ptr {{.*}}, metadata !"_ZTSFvu3i32E.normalized")
+
+// CHECK-DAG: ![[BASE_TYPE]] = !{i64 16, !"_ZTS4Base"}
+// CHECK-DAG: ![[BASE_VMEMPTR]] = !{i64 16, !"_ZTSM4BaseFiiE.virtual"}
+// CHECK-DAG: ![[DERIVED_TYPE]] = !{i64 16, !"_ZTS7Derived"}
+// CHECK-DAG: ![[DERIVED_VMEMPTR]] = !{i64 16, !"_ZTSM7DerivedFiiE.virtual"}
+// CHECK-DAG: ![[BOX_TYPE]] = !{i64 16, !"_ZTS3BoxIiE"}
+// CHECK-DAG: ![[BOX_VMEMPTR]] = !{i64 16, !"_ZTSM3BoxIiEFiiE.virtual"}
+// CFI-DAG: ![[FN_TYPE]] = !{i64 0, !"_ZTSFvu3i32E.normalized"}
+// CFI-DAG: ![[FN_GEN_TYPE]] = !{i64 0, !"_ZTSFvu3i32E.normalized.generalized"}
+// KCFI-DAG: ![[KCFI_FN_TYPE]] = !{i32 1162514891}
diff --git a/compiler-rt/test/cfi/normalize-integers-wpd.cpp b/compiler-rt/test/cfi/normalize-integers-wpd.cpp
new file mode 100644
index 0000000000000..ff09a7d156262
--- /dev/null
+++ b/compiler-rt/test/cfi/normalize-integers-wpd.cpp
@@ -0,0 +1,157 @@
+// Test that -fsanitize-cfi-icall-experimental-normalize-integers does not
+// normalize C++ class vtable !type metadata and break WholeProgramDevirt:
+//   1. Uniform ThinLTO with --lto-validate-all-vtables-have-type-infos still
+//      matches vtable !type metadata against RTTI _ZTI symbols and
+//      devirtualizes single-implementation virtual calls.
+//   2. Mixed-TU ThinLTO (where only some TUs pass
+//      -fsanitize-cfi-icall-experimental-normalize-integers) still sees
+//      overriding subclasses across TUs and does not unsoundly devirtualize.
+
+// REQUIRES: linux, lld-available, thinlto, cxxabi, (x86_64-target-arch || aarch64-target-arch)
+
+// --- KCFI: uniform + --lto-validate-all-vtables-have-type-infos ---
+// RUN: %clangxx -O2 -flto=thin -fno-split-lto-unit -fwhole-program-vtables \
+// RUN:   -fsanitize=kcfi -fsanitize-cfi-icall-experimental-normalize-integers \
+// RUN:   -DTU_A -c %s -o %t.kcfi.a.o
+// RUN: %clangxx -O2 -flto=thin -fno-split-lto-unit -fwhole-program-vtables \
+// RUN:   -fsanitize=kcfi -fsanitize-cfi-icall-experimental-normalize-integers \
+// RUN:   -DTU_MAIN -c %s -o %t.kcfi.main.o
+// RUN: %clangxx -O2 -flto=thin -fuse-ld=lld %t.kcfi.a.o %t.kcfi.main.o \
+// RUN:   -o %t.kcfi.uniform -Wl,--lto-whole-program-visibility \
+// RUN:   -Wl,--lto-validate-all-vtables-have-type-infos \
+// RUN:   -Wl,-mllvm,-pass-remarks=wholeprogramdevirt 2>&1 | \
+// RUN:   FileCheck %s --check-prefix=DEVIRT
+// RUN: %run %t.kcfi.uniform
+
+// --- KCFI: mixed-TU (TU_B without normalize-integers defines overrides) ---
+// RUN: %clangxx -O2 -flto=thin -fno-split-lto-unit -fwhole-program-vtables \
+// RUN:   -DTU_B -c %s -o %t.plain.b.o
+// RUN: %clangxx -O2 -flto=thin -fno-split-lto-unit -fwhole-program-vtables \
+// RUN:   -fsanitize=kcfi -fsanitize-cfi-icall-experimental-normalize-integers \
+// RUN:   -DTU_MAIN -DWITH_ALT -c %s -o %t.kcfi.main_alt.o
+// RUN: %clangxx -O2 -flto=thin -fuse-ld=lld %t.kcfi.a.o %t.plain.b.o \
+// RUN:   %t.kcfi.main_alt.o -o %t.kcfi.mixed -Wl,--lto-whole-program-visibility
+// RUN: %run %t.kcfi.mixed
+
+// --- cfi-icall: uniform + --lto-validate-all-vtables-have-type-infos ---
+// RUN: %clangxx -O2 -flto=thin -fwhole-program-vtables \
+// RUN:   -fsanitize=cfi-icall -fsanitize-trap=cfi-icall \
+// RUN:   -fsanitize-cfi-icall-experimental-normalize-integers \
+// RUN:   -DTU_A -c %s -o %t.cfi.a.o
+// RUN: %clangxx -O2 -flto=thin -fwhole-program-vtables \
+// RUN:   -fsanitize=cfi-icall -fsanitize-trap=cfi-icall \
+// RUN:   -fsanitize-cfi-icall-experimental-normalize-integers \
+// RUN:   -DTU_MAIN -c %s -o %t.cfi.main.o
+// RUN: %clangxx -O2 -flto=thin -fuse-ld=lld -fsanitize=cfi-icall \
+// RUN:   -fsanitize-trap=cfi-icall %t.cfi.a.o %t.cfi.main.o \
+// RUN:   -o %t.cfi.uniform -Wl,--lto-whole-program-visibility \
+// RUN:   -Wl,--lto-validate-all-vtables-have-type-infos \
+// RUN:   -Wl,-mllvm,-pass-remarks=wholeprogramdevirt 2>&1 | \
+// RUN:   FileCheck %s --check-prefix=DEVIRT
+// RUN: %run %t.cfi.uniform
+
+// --- cfi-icall: mixed-TU (TU_B without normalize-integers defines overrides) ---
+// RUN: %clangxx -O2 -flto=thin -fwhole-program-vtables \
+// RUN:   -DTU_B -c %s -o %t.plain_split.b.o
+// RUN: %clangxx -O2 -flto=thin -fwhole-program-vtables \
+// RUN:   -fsanitize=cfi-icall -fsanitize-trap=cfi-icall \
+// RUN:   -fsanitize-cfi-icall-experimental-normalize-integers \
+// RUN:   -DTU_MAIN -DWITH_ALT -c %s -o %t.cfi.main_alt.o
+// RUN: %clangxx -O2 -flto=thin -fuse-ld=lld -fsanitize=cfi-icall \
+// RUN:   -fsanitize-trap=cfi-icall %t.cfi.a.o %t.plain_split.b.o \
+// RUN:   %t.cfi.main_alt.o -o %t.cfi.mixed -Wl,--lto-whole-program-visibility
+// RUN: %run %t.cfi.mixed
+
+// DEVIRT-DAG: single-impl: devirtualized a call to _ZNK12_GLOBAL__N_17Primary3tagEv
+// DEVIRT-DAG: single-impl: devirtualized a call to _ZNK12_GLOBAL__N_17Primary3valEi
+// DEVIRT-DAG: single-impl: devirtualized a call to _ZNK3BoxIiE3getEi
+
+struct Shape {
+  virtual int tag() const = 0;
+  virtual int val(int x) const = 0;
+  virtual ~Shape() = default;
+};
+
+template <typename T> struct Box {
+  virtual T get(T x) const { return x + 1; }
+  virtual ~Box() = default;
+};
+
+Shape *make_primary();
+Box<int> *make_primary_box();
+Shape *make_alt();
+Box<int> *make_alt_box();
+int add_ten(int x);
+int apply_icall(int (*fn)(int), int x);
+int eval_shape(const Shape *s, const Box<int> *b, int x);
+
+#if defined(TU_A)
+
+namespace {
+struct Primary : Shape {
+  int tag() const override { return 10; }
+  int val(int x) const override { return x + 100; }
+};
+} // namespace
+
+template struct Box<int>;
+
+Shape *make_primary() {
+  static Primary p;
+  return &p;
+}
+
+Box<int> *make_primary_box() {
+  static Box<int> b;
+  return &b;
+}
+
+int add_ten(int x) { return x + 10; }
+
+__attribute__((noinline)) int apply_icall(int (*fn)(int), int x) {
+  return fn(x);
+}
+
+__attribute__((noinline)) int eval_shape(const Shape *s, const Box<int> *b,
+                                         int x) {
+  return s->tag() + s->val(x) + b->get(x);
+}
+
+#elif defined(TU_B)
+
+namespace {
+struct Alt : Shape {
+  int tag() const override { return 20; }
+  int val(int x) const override { return x + 200; }
+};
+
+struct AltBox : Box<int> {
+  int get(int x) const override { return x + 50; }
+};
+} // namespace
+
+Shape *make_alt() {
+  static Alt a;
+  return &a;
+}
+
+Box<int> *make_alt_box() {
+  static AltBox ab;
+  return &ab;
+}
+
+#elif defined(TU_MAIN)
+
+int main() {
+  if (apply_icall(&add_ten, 5) != 15)
+    return 1;
+  if (eval_shape(make_primary(), make_primary_box(), 5) != 121)
+    return 2;
+#  if defined(WITH_ALT)
+  if (eval_shape(make_alt(), make_alt_box(), 5) != 280)
+    return 3;
+#  endif
+  return 0;
+}
+
+#endif

>From 290da44184a39de368ab8ac6b29af2df9b48de3f Mon Sep 17 00:00:00 2001
From: Chandler Carruth <chandlerc at gmail.com>
Date: Fri, 13 Nov 2026 21:28:40 +0000
Subject: [PATCH 2/3] [Clang][CFI] Scope integer normalization specifically to
 cfi-icall metadata

Rather than checking `T->isFunctionType()` inside
`CreateMetadataIdentifierImpl` (which also normalized
`-fexperimental-call-graph-section` metadata), pass `NormalizeIntegers`
explicitly from `CreateMetadataIdentifierForFnType` and give `cfi-icall`
its own metadata map.

Assisted-by: Antigravity with Gemini
---
 clang/lib/CodeGen/CodeGenModule.cpp           | 45 ++++++++++---------
 clang/lib/CodeGen/CodeGenModule.h             | 13 +++---
 .../CodeGenCXX/cfi-icall-normalize-vtable.cpp | 20 ++++++---
 3 files changed, 45 insertions(+), 33 deletions(-)

diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp
index c3dbdfeace4fe..5a41182a0d33c 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -3757,14 +3757,12 @@ void CodeGenModule::createFunctionTypeMetadataForIcall(const FunctionDecl *FD,
   if (isa<CXXMethodDecl>(FD) && !cast<CXXMethodDecl>(FD)->isStatic())
     return;
 
-  QualType FnType = GeneralizeFunctionType(getContext(), FD->getType(),
-                                           /*GeneralizePointers=*/false);
-  llvm::Metadata *MD = CreateMetadataIdentifierForType(FnType);
+  llvm::Metadata *MD = CreateMetadataIdentifierForFnType(
+      FD->getType(), /*GeneralizePointers=*/false);
   F->addTypeMetadata(0, MD);
 
-  QualType GenPtrFnType = GeneralizeFunctionType(getContext(), FD->getType(),
-                                                 /*GeneralizePointers=*/true);
-  F->addTypeMetadata(0, CreateMetadataIdentifierGeneralized(GenPtrFnType));
+  F->addTypeMetadata(0, CreateMetadataIdentifierForFnType(
+                            FD->getType(), /*GeneralizePointers=*/true));
 
   // Emit a hash-based bit set entry for cross-DSO calls.
   if (CodeGenOpts.SanitizeCfiCrossDso)
@@ -8729,8 +8727,10 @@ void CodeGenModule::EmitOMPThreadPrivateDecl(const OMPThreadPrivateDecl *D) {
   }
 }
 
-llvm::Metadata *CodeGenModule::CreateMetadataIdentifierImpl(
-    QualType T, MetadataTypeMap &Map, StringRef Suffix, bool ForceString) {
+llvm::Metadata *
+CodeGenModule::CreateMetadataIdentifierImpl(QualType T, MetadataTypeMap &Map,
+                                            StringRef Suffix, bool ForceString,
+                                            bool NormalizeIntegers) {
   if (auto *FnType = T->getAs<FunctionProtoType>())
     T = getContext().getFunctionType(
         FnType->getReturnType(), FnType->getParamTypes(),
@@ -8743,8 +8743,6 @@ llvm::Metadata *CodeGenModule::CreateMetadataIdentifierImpl(
   if (ForceString || isExternallyVisible(T->getLinkage())) {
     std::string OutName;
     llvm::raw_string_ostream Out(OutName);
-    bool NormalizeIntegers = T->isFunctionType() &&
-                             getCodeGenOpts().SanitizeCfiICallNormalizeIntegers;
     getCXXABI().getMangleContext().mangleCanonicalTypeName(T, Out,
                                                            NormalizeIntegers);
 
@@ -8762,13 +8760,23 @@ llvm::Metadata *CodeGenModule::CreateMetadataIdentifierImpl(
   return InternalId;
 }
 
+llvm::Metadata *
+CodeGenModule::CreateMetadataIdentifierForFnType(QualType T,
+                                                 bool GeneralizePointers) {
+  assert(T->isFunctionType());
+  T = GeneralizeFunctionType(getContext(), T, GeneralizePointers);
+  bool NormalizeIntegers = getCodeGenOpts().SanitizeCfiICallNormalizeIntegers;
+  if (GeneralizePointers)
+    return CreateMetadataIdentifierImpl(T, GeneralizedMetadataIdMap,
+                                        ".generalized", /*ForceString=*/false,
+                                        NormalizeIntegers);
+  return CreateMetadataIdentifierImpl(T, CfiICallMetadataIdMap, "",
+                                      /*ForceString=*/false, NormalizeIntegers);
+}
+
 llvm::Metadata *CodeGenModule::CreateMetadataIdentifierForFnType(QualType T) {
-  assert(isa<FunctionType>(T));
-  T = GeneralizeFunctionType(
-      getContext(), T, getCodeGenOpts().SanitizeCfiICallGeneralizePointers);
-  if (getCodeGenOpts().SanitizeCfiICallGeneralizePointers)
-    return CreateMetadataIdentifierGeneralized(T);
-  return CreateMetadataIdentifierForType(T);
+  return CreateMetadataIdentifierForFnType(
+      T, getCodeGenOpts().SanitizeCfiICallGeneralizePointers);
 }
 
 llvm::Metadata *CodeGenModule::CreateMetadataIdentifierForType(QualType T) {
@@ -8780,11 +8788,6 @@ CodeGenModule::CreateMetadataIdentifierForVirtualMemPtrType(QualType T) {
   return CreateMetadataIdentifierImpl(T, VirtualMetadataIdMap, ".virtual");
 }
 
-llvm::Metadata *CodeGenModule::CreateMetadataIdentifierGeneralized(QualType T) {
-  return CreateMetadataIdentifierImpl(T, GeneralizedMetadataIdMap,
-                                      ".generalized", /*ForceString=*/false);
-}
-
 // Applies C default argument promotions to a parameter type.
 //
 // FIXME: The canonical source of truth for C default argument promotion is
diff --git a/clang/lib/CodeGen/CodeGenModule.h b/clang/lib/CodeGen/CodeGenModule.h
index 92afa8e708e2a..b4c718e77c349 100644
--- a/clang/lib/CodeGen/CodeGenModule.h
+++ b/clang/lib/CodeGen/CodeGenModule.h
@@ -708,6 +708,7 @@ class CodeGenModule : public CodeGenTypeCache {
   typedef llvm::DenseMap<QualType, llvm::Metadata *> MetadataTypeMap;
   MetadataTypeMap MetadataIdMap;
   MetadataTypeMap VirtualMetadataIdMap;
+  MetadataTypeMap CfiICallMetadataIdMap;
   MetadataTypeMap GeneralizedMetadataIdMap;
   MetadataTypeMap CallGraphMetadataIdMap;
 
@@ -1753,7 +1754,9 @@ class CodeGenModule : public CodeGenTypeCache {
   /// Generate a KCFI type identifier for T.
   llvm::ConstantInt *CreateKCFITypeId(QualType T, StringRef Salt);
 
-  /// Create a metadata identifier for the given function type.
+  /// Create a metadata identifier for the given function type for CFI icall.
+  llvm::Metadata *CreateMetadataIdentifierForFnType(QualType T,
+                                                    bool GeneralizePointers);
   llvm::Metadata *CreateMetadataIdentifierForFnType(QualType T);
 
   /// Create a metadata identifier for the given type. This may either be an
@@ -1781,11 +1784,6 @@ class CodeGenModule : public CodeGenTypeCache {
   /// calls via a member function pointer.
   llvm::Metadata *CreateMetadataIdentifierForVirtualMemPtrType(QualType T);
 
-  /// Create a metadata identifier for the generalization of the given type.
-  /// This may either be an MDString (for external identifiers) or a distinct
-  /// unnamed MDNode (for internal identifiers).
-  llvm::Metadata *CreateMetadataIdentifierGeneralized(QualType T);
-
   /// Create and attach type metadata to the given function.
   void createFunctionTypeMetadataForIcall(const FunctionDecl *FD,
                                           llvm::Function *F);
@@ -2223,7 +2221,8 @@ class CodeGenModule : public CodeGenTypeCache {
 
   llvm::Metadata *CreateMetadataIdentifierImpl(QualType T, MetadataTypeMap &Map,
                                                StringRef Suffix,
-                                               bool ForceString = false);
+                                               bool ForceString = false,
+                                               bool NormalizeIntegers = false);
 
   /// Emit deactivation symbols for any PFP fields whose offset is taken with
   /// offsetof.
diff --git a/clang/test/CodeGenCXX/cfi-icall-normalize-vtable.cpp b/clang/test/CodeGenCXX/cfi-icall-normalize-vtable.cpp
index 2225e067f35ad..53ac0ebd481b5 100644
--- a/clang/test/CodeGenCXX/cfi-icall-normalize-vtable.cpp
+++ b/clang/test/CodeGenCXX/cfi-icall-normalize-vtable.cpp
@@ -1,10 +1,12 @@
 // RUN: %clang_cc1 -triple x86_64-unknown-linux -fsanitize=kcfi -fwhole-program-vtables -flto=thin -flto-unit -fsanitize-cfi-icall-experimental-normalize-integers -emit-llvm -o - %s | FileCheck %s --check-prefixes=CHECK,KCFI --implicit-check-not=.normalized.virtual
-// RUN: %clang_cc1 -triple x86_64-unknown-linux -fsanitize=cfi-icall,cfi-vcall,cfi-mfcall -fsanitize-trap=cfi-icall,cfi-vcall,cfi-mfcall -fwhole-program-vtables -flto=thin -flto-unit -fsanitize-cfi-icall-experimental-normalize-integers -emit-llvm -o - %s | FileCheck %s --check-prefixes=CHECK,CFI,CFI-PUBLIC --implicit-check-not=.normalized.virtual
-// RUN: %clang_cc1 -triple x86_64-unknown-linux -fvisibility=hidden -fsanitize=cfi-icall,cfi-vcall,cfi-mfcall -fsanitize-trap=cfi-icall,cfi-vcall,cfi-mfcall -fwhole-program-vtables -flto=thin -flto-unit -fsanitize-cfi-icall-experimental-normalize-integers -emit-llvm -o - %s | FileCheck %s --check-prefixes=CHECK,CFI,CFI-HIDDEN --implicit-check-not=.normalized.virtual
+// RUN: %clang_cc1 -triple x86_64-unknown-linux -fsanitize=cfi-icall,cfi-vcall,cfi-mfcall -fsanitize-trap=cfi-icall,cfi-vcall,cfi-mfcall -fwhole-program-vtables -flto=thin -flto-unit -fsanitize-cfi-icall-experimental-normalize-integers -emit-llvm -o - %s | FileCheck %s --check-prefixes=CHECK,CFI,CFI-NOCG,CFI-PUBLIC --implicit-check-not=.normalized.virtual
+// RUN: %clang_cc1 -triple x86_64-unknown-linux -fvisibility=hidden -fsanitize=cfi-icall,cfi-vcall,cfi-mfcall -fsanitize-trap=cfi-icall,cfi-vcall,cfi-mfcall -fwhole-program-vtables -flto=thin -flto-unit -fsanitize-cfi-icall-experimental-normalize-integers -emit-llvm -o - %s | FileCheck %s --check-prefixes=CHECK,CFI,CFI-NOCG,CFI-HIDDEN --implicit-check-not=.normalized.virtual
+// RUN: %clang_cc1 -triple x86_64-unknown-linux -fexperimental-call-graph-section -fsanitize=cfi-icall,cfi-vcall,cfi-mfcall -fsanitize-trap=cfi-icall,cfi-vcall,cfi-mfcall -fwhole-program-vtables -flto=thin -flto-unit -fsanitize-cfi-icall-experimental-normalize-integers -emit-llvm -o - %s | FileCheck %s --check-prefixes=CHECK,CFI,CFI-PUBLIC,CALLGRAPH --implicit-check-not=.normalized.virtual
 
 // Verify that -fsanitize-cfi-icall-experimental-normalize-integers only
-// normalizes FunctionType metadata and does not alter class vtable !type
-// metadata or virtual member-pointer .virtual metadata.
+// normalizes cfi-icall/kcfi function metadata and does not alter class vtable
+// !type metadata, member-pointer metadata, or -fexperimental-call-graph-section
+// metadata.
 
 struct Base {
   virtual int get(int x);
@@ -52,7 +54,9 @@ void take_addr() {
 // CHECK-DAG: @_ZTV7Derived = {{.*}}!type ![[BASE_TYPE]], !type ![[BASE_VMEMPTR]], !type ![[DERIVED_TYPE:[0-9]+]], !type ![[DERIVED_VMEMPTR:[0-9]+]]
 // CHECK-DAG: @_ZTV3BoxIiE = {{.*}}!type ![[BOX_TYPE:[0-9]+]], !type ![[BOX_VMEMPTR:[0-9]+]]
 
-// CFI-DAG: define{{.*}} void @_Z12icall_targeti(i32 {{.*}}){{.*}} !type ![[FN_TYPE:[0-9]+]] !type ![[FN_GEN_TYPE:[0-9]+]]
+// CALLGRAPH-DAG: define{{.*}} i32 @_ZN4Base3getEi(ptr {{.*}}, i32 {{.*}}){{.*}} !callgraph ![[CG_METHOD_TYPE:[0-9]+]]
+// CFI-NOCG-DAG: define{{.*}} void @_Z12icall_targeti(i32 {{.*}}){{.*}} !type ![[FN_TYPE:[0-9]+]] !type ![[FN_GEN_TYPE:[0-9]+]]
+// CALLGRAPH-DAG: define{{.*}} void @_Z12icall_targeti(i32 {{.*}}){{.*}} !type ![[FN_TYPE:[0-9]+]] !type ![[FN_GEN_TYPE:[0-9]+]]{{.*}} !callgraph ![[CG_FN_TYPE:[0-9]+]]
 // KCFI-DAG: define{{.*}} void @_Z12icall_targeti(i32 {{.*}}){{.*}} !kcfi_type ![[KCFI_FN_TYPE:[0-9]+]]
 
 // CHECK-LABEL: define{{.*}} i32 @_Z10test_callsP4BaseMS_FiiEP3BoxIiEPFviEi(
@@ -65,6 +69,8 @@ void take_addr() {
 // CFI-HIDDEN-DAG: call i1 @llvm.type.test(ptr {{.*}}, metadata !"_ZTSM4BaseFiiE")
 // CFI-HIDDEN-DAG: call { ptr, i1 } @llvm.type.checked.load(ptr {{.*}}, i32 0, metadata !"_ZTS3BoxIiE")
 // CFI-DAG: call i1 @llvm.type.test(ptr {{.*}}, metadata !"_ZTSFvu3i32E.normalized")
+// CALLGRAPH-DAG: call noundef i32 %{{.*}}(ptr {{.*}}, i32 {{.*}}), !callee_type ![[CG_METHOD_CALLEE:[0-9]+]]
+// CALLGRAPH-DAG: call void %{{.*}}(i32 {{.*}}), !callee_type ![[CG_FN_CALLEE:[0-9]+]]
 
 // CHECK-DAG: ![[BASE_TYPE]] = !{i64 16, !"_ZTS4Base"}
 // CHECK-DAG: ![[BASE_VMEMPTR]] = !{i64 16, !"_ZTSM4BaseFiiE.virtual"}
@@ -75,3 +81,7 @@ void take_addr() {
 // CFI-DAG: ![[FN_TYPE]] = !{i64 0, !"_ZTSFvu3i32E.normalized"}
 // CFI-DAG: ![[FN_GEN_TYPE]] = !{i64 0, !"_ZTSFvu3i32E.normalized.generalized"}
 // KCFI-DAG: ![[KCFI_FN_TYPE]] = !{i32 1162514891}
+// CALLGRAPH-DAG: ![[CG_METHOD_TYPE]] = !{!"_ZTSFiiE"}
+// CALLGRAPH-DAG: ![[CG_METHOD_CALLEE]] = !{![[CG_METHOD_TYPE]]}
+// CALLGRAPH-DAG: ![[CG_FN_TYPE]] = !{!"_ZTSFviE"}
+// CALLGRAPH-DAG: ![[CG_FN_CALLEE]] = !{![[CG_FN_TYPE]]}

>From b3dbc6106fe87c1bd9695ebb84c0d7fd2f65051c Mon Sep 17 00:00:00 2001
From: Chandler Carruth <chandlerc at gmail.com>
Date: Sat, 14 Nov 2026 02:46:07 +0000
Subject: [PATCH 3/3] [Clang][CFI] Rename helper to
 CreateMetadataIdentifierForCFIFnType and add release note

Address review feedback by renaming `CreateMetadataIdentifierForFnType`
to `CreateMetadataIdentifierForCFIFnType` and documenting the fix in
`clang/docs/ReleaseNotes.md`.

Assisted-by: Antigravity with Gemini
---
 clang/docs/ReleaseNotes.md          |  4 ++++
 clang/lib/CodeGen/CGExpr.cpp        |  2 +-
 clang/lib/CodeGen/CodeGenModule.cpp | 13 +++++++------
 clang/lib/CodeGen/CodeGenModule.h   |  6 +++---
 4 files changed, 15 insertions(+), 10 deletions(-)

diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 14382a06db34c..8f74104a99b74 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -1575,6 +1575,10 @@ The `alpha.cplusplus.UseAfterLifetimeEnd` checker was renamed to `alpha.core.Use
   modes for `-fsanitize=alloc-token`, selected with
   `-Xclang -falloc-token-mode=<mode>`. These modes derive token IDs from both
   the allocated type and the name of the function containing the allocation.
+- Fixed `-fsanitize-cfi-icall-experimental-normalize-integers` to only
+  normalize `cfi-icall` and `kcfi` function type metadata, leaving C++ class
+  vtable, member-pointer, and `-fexperimental-call-graph-section` metadata
+  canonical so whole-program devirtualization works reliably. (#GH242466)
 
 ### Python Binding Changes
 
diff --git a/clang/lib/CodeGen/CGExpr.cpp b/clang/lib/CodeGen/CGExpr.cpp
index 5075292d3d45a..5574d4ecad120 100644
--- a/clang/lib/CodeGen/CGExpr.cpp
+++ b/clang/lib/CodeGen/CGExpr.cpp
@@ -7234,7 +7234,7 @@ RValue CodeGenFunction::EmitCall(QualType CalleeType,
     EmitSanitizerStatReport(llvm::SanStat_CFI_ICall);
 
     llvm::Metadata *MD =
-        CGM.CreateMetadataIdentifierForFnType(QualType(FnType, 0));
+        CGM.CreateMetadataIdentifierForCFIFnType(QualType(FnType, 0));
 
     llvm::Value *TypeId = llvm::MetadataAsValue::get(getLLVMContext(), MD);
 
diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp
index 5a41182a0d33c..853c472a8bdbf 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -3757,11 +3757,11 @@ void CodeGenModule::createFunctionTypeMetadataForIcall(const FunctionDecl *FD,
   if (isa<CXXMethodDecl>(FD) && !cast<CXXMethodDecl>(FD)->isStatic())
     return;
 
-  llvm::Metadata *MD = CreateMetadataIdentifierForFnType(
+  llvm::Metadata *MD = CreateMetadataIdentifierForCFIFnType(
       FD->getType(), /*GeneralizePointers=*/false);
   F->addTypeMetadata(0, MD);
 
-  F->addTypeMetadata(0, CreateMetadataIdentifierForFnType(
+  F->addTypeMetadata(0, CreateMetadataIdentifierForCFIFnType(
                             FD->getType(), /*GeneralizePointers=*/true));
 
   // Emit a hash-based bit set entry for cross-DSO calls.
@@ -8761,8 +8761,8 @@ CodeGenModule::CreateMetadataIdentifierImpl(QualType T, MetadataTypeMap &Map,
 }
 
 llvm::Metadata *
-CodeGenModule::CreateMetadataIdentifierForFnType(QualType T,
-                                                 bool GeneralizePointers) {
+CodeGenModule::CreateMetadataIdentifierForCFIFnType(QualType T,
+                                                    bool GeneralizePointers) {
   assert(T->isFunctionType());
   T = GeneralizeFunctionType(getContext(), T, GeneralizePointers);
   bool NormalizeIntegers = getCodeGenOpts().SanitizeCfiICallNormalizeIntegers;
@@ -8774,8 +8774,9 @@ CodeGenModule::CreateMetadataIdentifierForFnType(QualType T,
                                       /*ForceString=*/false, NormalizeIntegers);
 }
 
-llvm::Metadata *CodeGenModule::CreateMetadataIdentifierForFnType(QualType T) {
-  return CreateMetadataIdentifierForFnType(
+llvm::Metadata *
+CodeGenModule::CreateMetadataIdentifierForCFIFnType(QualType T) {
+  return CreateMetadataIdentifierForCFIFnType(
       T, getCodeGenOpts().SanitizeCfiICallGeneralizePointers);
 }
 
diff --git a/clang/lib/CodeGen/CodeGenModule.h b/clang/lib/CodeGen/CodeGenModule.h
index b4c718e77c349..c6b13f5881784 100644
--- a/clang/lib/CodeGen/CodeGenModule.h
+++ b/clang/lib/CodeGen/CodeGenModule.h
@@ -1755,9 +1755,9 @@ class CodeGenModule : public CodeGenTypeCache {
   llvm::ConstantInt *CreateKCFITypeId(QualType T, StringRef Salt);
 
   /// Create a metadata identifier for the given function type for CFI icall.
-  llvm::Metadata *CreateMetadataIdentifierForFnType(QualType T,
-                                                    bool GeneralizePointers);
-  llvm::Metadata *CreateMetadataIdentifierForFnType(QualType T);
+  llvm::Metadata *CreateMetadataIdentifierForCFIFnType(QualType T,
+                                                       bool GeneralizePointers);
+  llvm::Metadata *CreateMetadataIdentifierForCFIFnType(QualType T);
 
   /// Create a metadata identifier for the given type. This may either be an
   /// MDString (for external identifiers) or a distinct unnamed MDNode (for



More information about the cfe-commits mailing list