https://github.com/Prabhuk updated https://github.com/llvm/llvm-project/pull/230731
>From f305f07c52c08782ee2510984fc206ac584e85de Mon Sep 17 00:00:00 2001 From: prabhukr <[email protected]> Date: Sat, 10 Oct 2026 10:40:25 -0700 Subject: [PATCH] [clang][Driver] Honor integer normalization with -fexperimental-call-graph-section The type identifiers emitted for the call graph section honor -fsanitize-cfi-icall-experimental-normalize-integers in CodeGen (CreateMetadataIdentifierForCallGraphType), but the driver only forwarded the option to cc1 when -fsanitize=cfi or -fsanitize=kcfi was enabled, and otherwise reported it as unused. Forward it when -fexperimental-call-graph-section is enabled as well. Integer normalization is what allows LLVM CFI type identifiers to match across languages: Rust, for example, always encodes integers by their width and signedness, so cross-language CFI with Rust requires -fsanitize-cfi-icall-experimental-normalize-integers on the C/C++ side. Honoring the option for the call graph section without a sanitizer makes it possible to emit call graph type identifiers for C/C++ code that other front ends using the normalized encoding could match, should they emit the call graph section as well. --- clang/lib/Driver/SanitizerArgs.cpp | 17 ++++++++++++++--- clang/test/Driver/call-graph-section.c | 10 ++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/clang/lib/Driver/SanitizerArgs.cpp b/clang/lib/Driver/SanitizerArgs.cpp index a1bfa90e5dceb..4da96662b8909 100644 --- a/clang/lib/Driver/SanitizerArgs.cpp +++ b/clang/lib/Driver/SanitizerArgs.cpp @@ -1003,6 +1003,14 @@ SanitizerArgs::SanitizerArgs(const ToolChain &TC, options::OPT_fno_sanitize_cfi_canonical_jump_tables, true); } + // The call graph section (-fexperimental-call-graph-section) encodes the + // type identifiers of indirect calls and their potential targets like CFI + // does, so integer normalization applies to it as well, with or without CFI. + if (Args.hasFlag(options::OPT_fexperimental_call_graph_section, + options::OPT_fno_experimental_call_graph_section, false)) + CfiICallNormalizeIntegers |= + Args.hasArg(options::OPT_fsanitize_cfi_icall_normalize_integers); + if (AllAddedKinds & SanitizerKind::KCFI) { CfiICallGeneralizePointers = Args.hasArg(options::OPT_fsanitize_cfi_icall_generalize_pointers); @@ -1537,6 +1545,12 @@ void SanitizerArgs::addArgs(const ToolChain &TC, const llvm::opt::ArgList &Args, addIncludeLinkerOption(TC, Args, CmdArgs, "__sanitizer_stats_register"); } + // Integer normalization also applies to the type identifiers of the call + // graph section (-fexperimental-call-graph-section), which does not require + // any sanitizer to be enabled. + if (CfiICallNormalizeIntegers) + CmdArgs.push_back("-fsanitize-cfi-icall-experimental-normalize-integers"); + if (Sanitizers.empty()) return; CmdArgs.push_back(Args.MakeArgString("-fsanitize=" + toString(Sanitizers))); @@ -1615,9 +1629,6 @@ void SanitizerArgs::addArgs(const ToolChain &TC, const llvm::opt::ArgList &Args, if (CfiICallGeneralizePointers) CmdArgs.push_back("-fsanitize-cfi-icall-generalize-pointers"); - if (CfiICallNormalizeIntegers) - CmdArgs.push_back("-fsanitize-cfi-icall-experimental-normalize-integers"); - if (KcfiArity) { if (!TC.getTriple().isOSLinux() || !TC.getTriple().isArch64Bit()) { TC.getDriver().Diag(clang::diag::err_drv_kcfi_arity_unsupported_target) diff --git a/clang/test/Driver/call-graph-section.c b/clang/test/Driver/call-graph-section.c index 00fa896dffb87..a3996088af32a 100644 --- a/clang/test/Driver/call-graph-section.c +++ b/clang/test/Driver/call-graph-section.c @@ -3,3 +3,13 @@ // CALL-GRAPH-SECTION: "-fexperimental-call-graph-section" // NO-CALL-GRAPH-SECTION-NOT: "-fexperimental-call-graph-section" + +// The call graph section type identifiers honor integer normalization like CFI +// does, so the option is forwarded even when no sanitizer is enabled. +// RUN: %clang -### -fexperimental-call-graph-section -fsanitize-cfi-icall-experimental-normalize-integers %s 2>&1 | FileCheck --check-prefix=NORMALIZE-INTEGERS %s +// RUN: %clang -### -fsanitize-cfi-icall-experimental-normalize-integers %s 2>&1 | FileCheck --check-prefix=NO-NORMALIZE-INTEGERS %s + +// NORMALIZE-INTEGERS-NOT: warning: argument unused during compilation +// NORMALIZE-INTEGERS: "-fexperimental-call-graph-section" +// NORMALIZE-INTEGERS-SAME: "-fsanitize-cfi-icall-experimental-normalize-integers" +// NO-NORMALIZE-INTEGERS-NOT: "-fsanitize-cfi-icall-experimental-normalize-integers" _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
