mirror of https://github.com/golang/go.git
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:
parent
b4c7f6280e
commit
cf862478c8
|
|
@ -111,61 +111,35 @@ if test "$tsan" = "yes"; then
|
||||||
rm -f ${TMPDIR}/testsanitizers$$*
|
rm -f ${TMPDIR}/testsanitizers$$*
|
||||||
fi
|
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
|
err=${TMPDIR}/tsanerr$$.out
|
||||||
|
if ! env $2 go run $3 $1 2>$err; then
|
||||||
if ! go run tsan.go 2>$err; then
|
|
||||||
cat $err
|
cat $err
|
||||||
echo "FAIL: tsan"
|
echo "FAIL: $1"
|
||||||
status=1
|
status=1
|
||||||
elif grep -i warning $err >/dev/null 2>&1; then
|
elif grep -i warning $err >/dev/null 2>&1; then
|
||||||
cat $err
|
cat $err
|
||||||
echo "FAIL: tsan"
|
echo "FAIL: $1"
|
||||||
status=1
|
status=1
|
||||||
fi
|
fi
|
||||||
|
rm -f $err
|
||||||
|
}
|
||||||
|
|
||||||
if ! go run tsan2.go 2>$err; then
|
if test "$tsan" = "yes"; then
|
||||||
cat $err
|
testtsan tsan.go
|
||||||
echo "FAIL: tsan2"
|
testtsan tsan2.go
|
||||||
status=1
|
testtsan tsan3.go
|
||||||
elif grep -i warning $err >/dev/null 2>&1; then
|
testtsan tsan4.go
|
||||||
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
|
|
||||||
|
|
||||||
# This test requires rebuilding os/user with -fsanitize=thread.
|
# 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
|
testtsan tsan5.go "CGO_CFLAGS=-fsanitize=thread CGO_LDFLAGS=-fsanitize=thread" "-installsuffix=tsan"
|
||||||
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
|
|
||||||
|
|
||||||
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
|
fi
|
||||||
|
|
||||||
exit $status
|
exit $status
|
||||||
|
|
|
||||||
|
|
@ -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()
|
||||||
|
}
|
||||||
|
|
@ -8,11 +8,15 @@
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <sys/mman.h>
|
#include <sys/mman.h>
|
||||||
|
|
||||||
|
#include "libcgo.h"
|
||||||
|
|
||||||
void *
|
void *
|
||||||
x_cgo_mmap(void *addr, uintptr_t length, int32_t prot, int32_t flags, int32_t fd, uint32_t offset) {
|
x_cgo_mmap(void *addr, uintptr_t length, int32_t prot, int32_t flags, int32_t fd, uint32_t offset) {
|
||||||
void *p;
|
void *p;
|
||||||
|
|
||||||
|
_cgo_tsan_acquire();
|
||||||
p = mmap(addr, length, prot, flags, fd, offset);
|
p = mmap(addr, length, prot, flags, fd, offset);
|
||||||
|
_cgo_tsan_release();
|
||||||
if (p == MAP_FAILED) {
|
if (p == MAP_FAILED) {
|
||||||
/* This is what the Go code expects on failure. */
|
/* This is what the Go code expects on failure. */
|
||||||
p = (void *) (uintptr_t) errno;
|
p = (void *) (uintptr_t) errno;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue