[clang] 3c0a5b4 - [Clang][CFI] Don't let -fdevirtualize-speculatively force llvm.type.checked.load (#241643)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Nov 12 18:54:42 PST 2026
Author: Vitaly Buka
Date: 2026-11-13T02:54:33Z
New Revision: 3c0a5b4d4d68794d920edef9d30598700cc6cc1d
URL: https://github.com/llvm/llvm-project/commit/3c0a5b4d4d68794d920edef9d30598700cc6cc1d
DIFF: https://github.com/llvm/llvm-project/commit/3c0a5b4d4d68794d920edef9d30598700cc6cc1d.diff
LOG: [Clang][CFI] Don't let -fdevirtualize-speculatively force llvm.type.checked.load (#241643)
9e7ce77573b2 (#159685) made `-fdevirtualize-speculatively` override both
conditions for emitting `llvm.type.checked.load`:
`-fwhole-program-vtables`
and hidden LTO visibility of the class. The override never helped
speculative devirtualization, because that pass only runs without LTO,
and CFI requires LTO.
With LTO and trapping `cfi-vcall`, the override adds a local CFI check
to
classes with public LTO visibility. That check only knows the vtables in
the current LTO unit, so it traps on valid objects created elsewhere,
such as `std::exception` subclasses from the shared C++ runtime or
classes from other DSOs. With `-fsanitize-cfi-cross-dso`, these calls
also skip `__cfi_slowpath`.
Remove the override.
Needed by #241647, which uses the same path for non-trapping
`cfi-vcall`.
Assisted-by: Gemini
Added:
Modified:
clang/lib/CodeGen/CGClass.cpp
clang/test/CodeGenCXX/cfi-vtable-check.cpp
compiler-rt/test/cfi/devirtualize-speculatively.cpp
Removed:
################################################################################
diff --git a/clang/lib/CodeGen/CGClass.cpp b/clang/lib/CodeGen/CGClass.cpp
index 00bc1eb0848d4..a1b61394571d4 100644
--- a/clang/lib/CodeGen/CGClass.cpp
+++ b/clang/lib/CodeGen/CGClass.cpp
@@ -2980,9 +2980,8 @@ void CodeGenFunction::EmitVTableCheck(CFITypeCheckKind TCK, CanQualType T,
}
bool CodeGenFunction::ShouldEmitVTableTypeCheckedLoad(const CXXRecordDecl *RD) {
- if ((!CGM.getCodeGenOpts().WholeProgramVTables ||
- !CGM.HasHiddenLTOVisibility(RD)) &&
- !CGM.getCodeGenOpts().DevirtualizeSpeculatively)
+ if (!CGM.getCodeGenOpts().WholeProgramVTables ||
+ !CGM.HasHiddenLTOVisibility(RD))
return false;
if (CGM.getCodeGenOpts().VirtualFunctionElimination)
diff --git a/clang/test/CodeGenCXX/cfi-vtable-check.cpp b/clang/test/CodeGenCXX/cfi-vtable-check.cpp
index 46c4b2f935044..47e8d7130e7ba 100644
--- a/clang/test/CodeGenCXX/cfi-vtable-check.cpp
+++ b/clang/test/CodeGenCXX/cfi-vtable-check.cpp
@@ -80,15 +80,15 @@ struct S {
// SPEC_TRAP-NEXT: store ptr [[S]], ptr [[S_ADDR]], align 8
// SPEC_TRAP-NEXT: [[TMP0:%.*]] = load ptr, ptr [[S_ADDR]], align 8
// SPEC_TRAP-NEXT: [[VTABLE:%.*]] = load ptr, ptr [[TMP0]], align 8
-// SPEC_TRAP-NEXT: [[TMP1:%.*]] = call { ptr, i1 } @llvm.type.checked.load(ptr [[VTABLE]], i32 0, metadata !"_ZTS1S"), !nosanitize [[META1:![0-9]+]]
-// SPEC_TRAP-NEXT: [[TMP2:%.*]] = extractvalue { ptr, i1 } [[TMP1]], 1, !nosanitize [[META1]]
-// SPEC_TRAP-NEXT: br i1 [[TMP2]], label %[[CONT:.*]], label %[[TRAP:.*]], !prof [[PROF2:![0-9]+]], !nosanitize [[META1]]
+// SPEC_TRAP-NEXT: [[TMP1:%.*]] = call i1 @llvm.type.test(ptr [[VTABLE]], metadata !"_ZTS1S"), !nosanitize [[META1:![0-9]+]]
+// SPEC_TRAP-NEXT: br i1 [[TMP1]], label %[[CONT:.*]], label %[[TRAP:.*]], !prof [[PROF2:![0-9]+]], !nosanitize [[META1]]
// SPEC_TRAP: [[TRAP]]:
// SPEC_TRAP-NEXT: call void @llvm.ubsantrap(i8 2) #[[ATTR3:[0-9]+]], !nosanitize [[META1]]
// SPEC_TRAP-NEXT: unreachable, !nosanitize [[META1]]
// SPEC_TRAP: [[CONT]]:
-// SPEC_TRAP-NEXT: [[TMP3:%.*]] = extractvalue { ptr, i1 } [[TMP1]], 0, !nosanitize [[META1]]
-// SPEC_TRAP-NEXT: call void [[TMP3]](ptr noundef nonnull align 8 dereferenceable(8) [[TMP0]])
+// SPEC_TRAP-NEXT: [[VFN:%.*]] = getelementptr inbounds ptr, ptr [[VTABLE]], i64 0
+// SPEC_TRAP-NEXT: [[TMP2:%.*]] = load ptr, ptr [[VFN]], align 8
+// SPEC_TRAP-NEXT: call void [[TMP2]](ptr noundef nonnull align 8 dereferenceable(8) [[TMP0]])
// SPEC_TRAP-NEXT: ret void
//
extern "C" void test(S *s) {
@@ -115,7 +115,7 @@ extern "C" void test(S *s) {
// XDSO_TRAP: attributes #[[ATTR6:[0-9]+]] = { nomerge noreturn nounwind }
//.
// SPEC_TRAP: attributes #[[ATTR0]] = { mustprogress noinline nounwind optnone "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-features"="+cx8,+mmx,+sse,+sse2,+x87" }
-// SPEC_TRAP: attributes #[[ATTR1:[0-9]+]] = { nocallback nofree nosync nounwind willreturn memory(none) }
+// SPEC_TRAP: attributes #[[ATTR1:[0-9]+]] = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
// SPEC_TRAP: attributes #[[ATTR2:[0-9]+]] = { cold noreturn nounwind memory(inaccessiblemem: write) }
// SPEC_TRAP: attributes #[[ATTR3]] = { nomerge noreturn nounwind }
//.
diff --git a/compiler-rt/test/cfi/devirtualize-speculatively.cpp b/compiler-rt/test/cfi/devirtualize-speculatively.cpp
index 92fd71ee7494b..7cc258f07751d 100644
--- a/compiler-rt/test/cfi/devirtualize-speculatively.cpp
+++ b/compiler-rt/test/cfi/devirtualize-speculatively.cpp
@@ -6,9 +6,6 @@
// public LTO visibility: their vtables may come from outside of the LTO unit,
// like the one defined in a non-LTO object here.
-// FIXME: The checked load traps on the vtable from the non-LTO object.
-// XFAIL: *
-
struct [[clang::lto_visibility_public]] A {
virtual void f();
};
More information about the cfe-commits
mailing list