From 2d85e87f08c325f8be869718c4ac0d7c069161c0 Mon Sep 17 00:00:00 2001 From: Ian Lance Taylor Date: Tue, 2 Aug 2016 11:42:53 -0700 Subject: [PATCH] runtime/cgo: add tsan acquire/release around setenv/unsetenv Change-Id: Iabb25e97714d070c31c657559a97a3bfc979da18 Reviewed-on: https://go-review.googlesource.com/25403 Reviewed-by: Dmitry Vyukov Run-TryBot: Ian Lance Taylor TryBot-Result: Gobot Gobot --- misc/cgo/testsanitizers/test.bash | 3 +++ misc/cgo/testsanitizers/tsan7.go | 40 +++++++++++++++++++++++++++++++ src/runtime/cgo/gcc_setenv.c | 4 ++++ 3 files changed, 47 insertions(+) create mode 100644 misc/cgo/testsanitizers/tsan7.go diff --git a/misc/cgo/testsanitizers/test.bash b/misc/cgo/testsanitizers/test.bash index 6e6347ce298..d1d2dc6ae24 100755 --- a/misc/cgo/testsanitizers/test.bash +++ b/misc/cgo/testsanitizers/test.bash @@ -161,6 +161,9 @@ if test "$tsan" = "yes"; then # This test requires rebuilding runtime/cgo with -fsanitize=thread. testtsan tsan6.go "CGO_CFLAGS=-fsanitize=thread CGO_LDFLAGS=-fsanitize=thread" "-installsuffix=tsan" + + # This test requires rebuilding runtime/cgo with -fsanitize=thread. + testtsan tsan7.go "CGO_CFLAGS=-fsanitize=thread CGO_LDFLAGS=-fsanitize=thread" "-installsuffix=tsan" fi fi diff --git a/misc/cgo/testsanitizers/tsan7.go b/misc/cgo/testsanitizers/tsan7.go new file mode 100644 index 00000000000..2fb9e45ee2d --- /dev/null +++ b/misc/cgo/testsanitizers/tsan7.go @@ -0,0 +1,40 @@ +// Copyright 2016 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package main + +// Setting an environment variable in a cgo program changes the C +// environment. Test that this does not confuse the race detector. + +/* +#cgo CFLAGS: -fsanitize=thread +#cgo LDFLAGS: -fsanitize=thread +*/ +import "C" + +import ( + "fmt" + "os" + "sync" + "time" +) + +func main() { + var wg sync.WaitGroup + var mu sync.Mutex + f := func() { + defer wg.Done() + for i := 0; i < 100; i++ { + time.Sleep(time.Microsecond) + mu.Lock() + s := fmt.Sprint(i) + os.Setenv("TSAN_TEST"+s, s) + mu.Unlock() + } + } + wg.Add(2) + go f() + go f() + wg.Wait() +} diff --git a/src/runtime/cgo/gcc_setenv.c b/src/runtime/cgo/gcc_setenv.c index 8708d400eff..ed5d203fb04 100644 --- a/src/runtime/cgo/gcc_setenv.c +++ b/src/runtime/cgo/gcc_setenv.c @@ -13,12 +13,16 @@ void x_cgo_setenv(char **arg) { + _cgo_tsan_acquire(); setenv(arg[0], arg[1], 1); + _cgo_tsan_release(); } /* Stub for calling unsetenv */ void x_cgo_unsetenv(char *arg) { + _cgo_tsan_acquire(); unsetenv(arg); + _cgo_tsan_release(); }