]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/cgo: an approach to tsan that works with gcc
authorMichael Hudson-Doyle <michael.hudson@canonical.com>
Mon, 2 May 2016 02:46:40 +0000 (14:46 +1200)
committerMichael Hudson-Doyle <michael.hudson@canonical.com>
Tue, 3 May 2016 00:49:46 +0000 (00:49 +0000)
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: Ian Lance Taylor <iant@golang.org>
src/cmd/cgo/main.go
src/cmd/cgo/out.go

index 2dc36c20db91e2e9073d5b19be6b452d9c9be797..cbdeb0f9ca6169be71777161e26ad9a93dca5f95 100644 (file)
@@ -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.
index e91abe6e9d276fd3224e5b7b08bf6045797bd86f..5eab3a71b49e291938ef724ee15f93da5508f764 100644 (file)
@@ -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 <stddef.h> /* for ptrdiff_t and size_t below */