Commit 499cd337 authored by Michael Hudson-Doyle's avatar Michael Hudson-Doyle

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 <michael.hudson@canonical.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarIan Lance Taylor <iant@golang.org>
parent f459660c
...@@ -227,6 +227,12 @@ func main() { ...@@ -227,6 +227,12 @@ func main() {
goFiles := args[i:] goFiles := args[i:]
for _, arg := range args[:i] {
if arg == "-fsanitize=thread" {
tsanProlog = yesTsanProlog
}
}
p := newPackage(args[:i]) p := newPackage(args[:i])
// Record CGO_LDFLAGS from the environment for external linking. // Record CGO_LDFLAGS from the environment for external linking.
......
...@@ -1303,14 +1303,12 @@ extern char* _cgo_topofstack(void); ...@@ -1303,14 +1303,12 @@ extern char* _cgo_topofstack(void);
` `
// Prologue defining TSAN functions in C. // Prologue defining TSAN functions in C.
const tsanProlog = ` const noTsanProlog = `
#define _cgo_tsan_acquire() #define _cgo_tsan_acquire()
#define _cgo_tsan_release() #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)); long long _cgo_sync __attribute__ ((common));
extern void __tsan_acquire(void*); extern void __tsan_acquire(void*);
...@@ -1323,10 +1321,11 @@ static void _cgo_tsan_acquire() { ...@@ -1323,10 +1321,11 @@ static void _cgo_tsan_acquire() {
static void _cgo_tsan_release() { static void _cgo_tsan_release() {
__tsan_release(&_cgo_sync); __tsan_release(&_cgo_sync);
} }
#endif
#endif
` `
// Set to yesTsanProlog if we see -fsanitize=thread in the flags for gcc.
var tsanProlog = noTsanProlog
const builtinProlog = ` const builtinProlog = `
#include <stddef.h> /* for ptrdiff_t and size_t below */ #include <stddef.h> /* for ptrdiff_t and size_t below */
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment