]> Cypherpunks repositories - gostls13.git/commitdiff
runtime/cgo: add tsan acquire/release around setenv/unsetenv
authorIan Lance Taylor <iant@golang.org>
Tue, 2 Aug 2016 18:42:53 +0000 (11:42 -0700)
committerIan Lance Taylor <iant@golang.org>
Tue, 23 Aug 2016 14:07:58 +0000 (14:07 +0000)
Change-Id: Iabb25e97714d070c31c657559a97a3bfc979da18
Reviewed-on: https://go-review.googlesource.com/25403
Reviewed-by: Dmitry Vyukov <dvyukov@google.com>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>

misc/cgo/testsanitizers/test.bash
misc/cgo/testsanitizers/tsan7.go [new file with mode: 0644]
src/runtime/cgo/gcc_setenv.c

index 6e6347ce298884c1fc5ae8bf7b144613ccd31c4e..d1d2dc6ae24ea068162b863ef212520d3c10ab32 100755 (executable)
@@ -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 (file)
index 0000000..2fb9e45
--- /dev/null
@@ -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()
+}
index 8708d400eff8e2bbc854086744568d9937a86242..ed5d203fb04904993dd558eec40a8e47a41ea2e4 100644 (file)
 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();
 }