rules_go icon indicating copy to clipboard operation
rules_go copied to clipboard

Filter LTO-dependent CFI sanitizer flags from cgo compiler options

Open granatam opened this issue 2 weeks ago • 2 comments

Clang CFI sanitizer options are incompatible with the compiler invocations performed internally by cgo.

cgo appends -fno-lto to its compiler command: https://github.com/golang/go/blob/224934336832d84eedcf168ae633e376a5decb22/src/cmd/cgo/gcc.go#L1840-L1841

However, Clang's CFI sanitizers require LTO (-flto or -flto=thin), with the exception of KCFI: https://clang.llvm.org/docs/ControlFlowIntegrity.html

As a result, when CFI is enabled through the C/C++ toolchain, rules_go forwards the sanitizer option to cgo together with options that ultimately conflict with cgo's -fno-lto and breaks the build.

rules_go already filters compiler options that are incompatible with cgo through _COMPILER_OPTIONS_DENYLIST:

https://github.com/bazel-contrib/rules_go/blob/caa2ca58cb8f1305a77eff41540441cdaf954e48/go/private/context.bzl#L129-L150

It seems that CFI sanitizer options should be filtered there as well.

If this sounds reasonable, I can follow up with a PR.

granatam avatar Nov 13 '26 13:11 granatam

PR welcome!

fmeum avatar Nov 13 '26 13:11 fmeum

Hi, I'd like to work on this issue. I'll look into the existing compiler options denylist, add the necessary CFI flag filtering, and include tests to verify the behavior. Thanks!

anshikagrover15 avatar Nov 14 '26 07:11 anshikagrover15