1
0
mirror of https://github.com/golang/go synced 2024-11-12 02:00:23 -07:00

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: Ian Lance Taylor <iant@golang.org>
This commit is contained in:
Michael Hudson-Doyle 2016-05-02 14:46:40 +12:00
parent f459660cb8
commit 499cd33719
2 changed files with 12 additions and 7 deletions

View 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.

View 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 */