https://github.com/Prabhuk created https://github.com/llvm/llvm-project/pull/230731
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, so that the call graph type identifiers of C/C++ functions can match the ones emitted for other languages that always use the normalized integer encoding (e.g. Rust's -Zcall-graph-section with -Zsanitizer-cfi-normalize-integers). >From 690d58c8ea3420ac219857e35fb527333b1d0279 Mon Sep 17 00:00:00 2001 From: prabhukr <[email protected]> Date: Fri, 9 Oct 2026 20:54:34 -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, so that the call graph type identifiers of C/C++ functions can match the ones emitted for other languages that always use the normalized integer encoding (e.g. Rust's -Zcall-graph-section with -Zsanitizer-cfi-normalize-integers). --- 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 a1bfa90e5dceb2..4da96662b8909a 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 00fa896dffb876..a3996088af32a3 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
