From 0f5d86c5a01fbb70ffa1a59049b0a7cf4fcbcc0a Mon Sep 17 00:00:00 2001 From: Holger Hans Peter Freyther Date: Sun, 9 Jul 2023 22:24:12 +0800 Subject: [PATCH] cmd/go: permit additional cflags when compiling In CL 475375 the Go command started to generate the "preferlinkext" token file for "strange/dangerous" compiler flags. This serves as a hint to the Go linker whether to call the external linker or not. Permit compiler flag used by the hermetic_cc_toolchain bzlmod. As a side effect, it also allows these flags to appear in #cgo directives in source code. We don't know of any cases where that is actually useful, but it appears to be harmless and simplifies the implementation of the internal linking change. Fixes #72842 Change-Id: Ic6de29b535a4e2c0720f383567ea6b3c7ca4f541 Reviewed-on: https://go-review.googlesource.com/c/go/+/657575 Auto-Submit: Ian Lance Taylor LUCI-TryBot-Result: Go LUCI Reviewed-by: Ian Lance Taylor Reviewed-by: Michael Matloob --- src/cmd/go/internal/work/security.go | 1 + .../testdata/script/cgo_suspect_flag_force_external.txt | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/src/cmd/go/internal/work/security.go b/src/cmd/go/internal/work/security.go index c3d62ddc23..3b3eba536c 100644 --- a/src/cmd/go/internal/work/security.go +++ b/src/cmd/go/internal/work/security.go @@ -90,6 +90,7 @@ var validCompilerFlags = []*lazyregexp.Regexp{ re(`-f(no-)?use-linker-plugin`), // safe if -B is not used; we don't permit -B re(`-f(no-)?visibility-inlines-hidden`), re(`-fsanitize=(.+)`), + re(`-fsanitize-undefined-strip-path-components=(-)?[0-9]+`), re(`-ftemplate-depth-(.+)`), re(`-ftls-model=(global-dynamic|local-dynamic|initial-exec|local-exec)`), re(`-fvisibility=(.+)`), diff --git a/src/cmd/go/testdata/script/cgo_suspect_flag_force_external.txt b/src/cmd/go/testdata/script/cgo_suspect_flag_force_external.txt index 6dc30bedb9..b4b2d14b11 100644 --- a/src/cmd/go/testdata/script/cgo_suspect_flag_force_external.txt +++ b/src/cmd/go/testdata/script/cgo_suspect_flag_force_external.txt @@ -56,6 +56,14 @@ env CGO_CFLAGS=-fprofile-instr-generate go build -x -n -o dummy.exe ./usesInternalCgo stderr preferlinkext +# Trimming file information for the UndefinedBehaviorSanitizer is permitted for internal linking. +env CGO_CFLAGS=-fsanitize-undefined-strip-path-components=-1 +go build -x -n -o dummy.exe ./usesInternalCgo +! stderr preferlinkext +env CGO_CFLAGS=-fsanitize-undefined-strip-path-components=2 +go build -x -n -o dummy.exe ./usesInternalCgo +! stderr preferlinkext + # The -fdebug-prefix-map=path is permitted for internal linking. env CGO_CFLAGS=-fdebug-prefix-map=/some/sandbox/execroot/workspace=/tmp/new go build -x -n -o dummy.exe ./usesInternalCgo -- 2.51.0