1
0
mirror of https://github.com/golang/go synced 2024-11-26 21:01:31 -07:00

runtime/cgo: add TSAN locks around mmap call

Change-Id: I806cc5523b7b5e3278d01074bc89900d78700e0c
Reviewed-on: https://go-review.googlesource.com/23736
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Dmitry Vyukov <dvyukov@google.com>
This commit is contained in:
Ian Lance Taylor 2016-06-03 10:49:24 -07:00
parent b4c7f6280e
commit cf862478c8
3 changed files with 71 additions and 44 deletions

View File

@ -111,61 +111,35 @@ if test "$tsan" = "yes"; then
rm -f ${TMPDIR}/testsanitizers$$*
fi
if test "$tsan" = "yes"; then
# Run a TSAN test.
# $1 test name
# $2 environment variables
# $3 go run args
testtsan() {
err=${TMPDIR}/tsanerr$$.out
if ! go run tsan.go 2>$err; then
if ! env $2 go run $3 $1 2>$err; then
cat $err
echo "FAIL: tsan"
echo "FAIL: $1"
status=1
elif grep -i warning $err >/dev/null 2>&1; then
cat $err
echo "FAIL: tsan"
echo "FAIL: $1"
status=1
fi
rm -f $err
}
if ! go run tsan2.go 2>$err; then
cat $err
echo "FAIL: tsan2"
status=1
elif grep -i warning $err >/dev/null 2>&1; then
cat $err
echo "FAIL: tsan2"
status=1
fi
if ! go run tsan3.go 2>$err; then
cat $err
echo "FAIL: tsan3"
status=1
elif grep -i warning $err >/dev/null 2>&1; then
cat $err
echo "FAIL: tsan3"
status=1
fi
if ! go run tsan4.go 2>$err; then
cat $err
echo "FAIL: tsan4"
status=1
elif grep -i warning $err >/dev/null 2>&1; then
cat $err
echo "FAIL: tsan4"
status=1
fi
if test "$tsan" = "yes"; then
testtsan tsan.go
testtsan tsan2.go
testtsan tsan3.go
testtsan tsan4.go
# This test requires rebuilding os/user with -fsanitize=thread.
if ! CGO_CFLAGS="-fsanitize=thread" CGO_LDFLAGS="-fsanitize=thread" go run -installsuffix=tsan tsan5.go 2>$err; then
cat $err
echo "FAIL: tsan5"
status=1
elif grep -i warning $err >/dev/null 2>&1; then
cat $err
echo "FAIL: tsan5"
status=1
fi
testtsan tsan5.go "CGO_CFLAGS=-fsanitize=thread CGO_LDFLAGS=-fsanitize=thread" "-installsuffix=tsan"
rm -f $err
# This test requires rebuilding runtime/cgo with -fsanitize=thread.
testtsan tsan6.go "CGO_CFLAGS=-fsanitize=thread CGO_LDFLAGS=-fsanitize=thread" "-installsuffix=tsan"
fi
exit $status

View File

@ -0,0 +1,49 @@
// 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
// Check that writes to Go allocated memory, with Go synchronization,
// do not look like a race.
/*
#cgo CFLAGS: -fsanitize=thread
#cgo LDFLAGS: -fsanitize=thread
void f(char *p) {
*p = 1;
}
*/
import "C"
import (
"runtime"
"sync"
)
func main() {
var wg sync.WaitGroup
var mu sync.Mutex
c := make(chan []C.char, 100)
for i := 0; i < 10; i++ {
wg.Add(2)
go func() {
defer wg.Done()
for i := 0; i < 100; i++ {
c <- make([]C.char, 4096)
runtime.Gosched()
}
}()
go func() {
defer wg.Done()
for i := 0; i < 100; i++ {
p := &(<-c)[0]
mu.Lock()
C.f(p)
mu.Unlock()
}
}()
}
wg.Wait()
}

View File

@ -8,11 +8,15 @@
#include <stdint.h>
#include <sys/mman.h>
#include "libcgo.h"
void *
x_cgo_mmap(void *addr, uintptr_t length, int32_t prot, int32_t flags, int32_t fd, uint32_t offset) {
void *p;
_cgo_tsan_acquire();
p = mmap(addr, length, prot, flags, fd, offset);
_cgo_tsan_release();
if (p == MAP_FAILED) {
/* This is what the Go code expects on failure. */
p = (void *) (uintptr_t) errno;