From 499cd3371997bdb6e33377266754d20782ef134d Mon Sep 17 00:00:00 2001 From: Michael Hudson-Doyle Date: Mon, 2 May 2016 14:46:40 +1200 Subject: [PATCH] cmd/cgo: an approach to tsan that works with gcc GCC, unlike clang, does not provide any way for code being compiled to tell if -fsanitize-thread was passed. But cgo can look to see if that flag is being passed and generate different code in that case. Fixes #14602 Change-Id: I86cb5318c2e35501ae399618c05af461d1252d2d Reviewed-on: https://go-review.googlesource.com/22688 Run-TryBot: Michael Hudson-Doyle TryBot-Result: Gobot Gobot Reviewed-by: Ian Lance Taylor --- src/cmd/cgo/main.go | 6 ++++++ src/cmd/cgo/out.go | 13 ++++++------- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/cmd/cgo/main.go b/src/cmd/cgo/main.go index 2dc36c20db..cbdeb0f9ca 100644 --- a/src/cmd/cgo/main.go +++ b/src/cmd/cgo/main.go @@ -227,6 +227,12 @@ func main() { goFiles := args[i:] + for _, arg := range args[:i] { + if arg == "-fsanitize=thread" { + tsanProlog = yesTsanProlog + } + } + p := newPackage(args[:i]) // Record CGO_LDFLAGS from the environment for external linking. diff --git a/src/cmd/cgo/out.go b/src/cmd/cgo/out.go index e91abe6e9d..5eab3a71b4 100644 --- a/src/cmd/cgo/out.go +++ b/src/cmd/cgo/out.go @@ -1303,14 +1303,12 @@ extern char* _cgo_topofstack(void); ` // Prologue defining TSAN functions in C. -const tsanProlog = ` +const noTsanProlog = ` #define _cgo_tsan_acquire() #define _cgo_tsan_release() -#if defined(__has_feature) -#if __has_feature(thread_sanitizer) -#undef _cgo_tsan_acquire -#undef _cgo_tsan_release +` +const yesTsanProlog = ` long long _cgo_sync __attribute__ ((common)); extern void __tsan_acquire(void*); @@ -1323,10 +1321,11 @@ static void _cgo_tsan_acquire() { static void _cgo_tsan_release() { __tsan_release(&_cgo_sync); } -#endif -#endif ` +// Set to yesTsanProlog if we see -fsanitize=thread in the flags for gcc. +var tsanProlog = noTsanProlog + const builtinProlog = ` #include /* for ptrdiff_t and size_t below */