https://github.com/chandlerc created https://github.com/llvm/llvm-project/pull/242466
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 >From 423c41494f51c6f434f541332e76629ca4c40672 Mon Sep 17 00:00:00 2001 From: Chandler Carruth <[email protected]> Date: Fri, 13 Nov 2026 18:35:17 +0000 Subject: [PATCH] [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 _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
