runtime: fix map iterator concurrent map check

We should check whether there is a concurrent writer at the
start of every mapiternext, not just in mapaccessK (which is
only called during certain map growth situations).

Tests turned off by default because they are inherently flaky.

Fixes #16278

Change-Id: I8b72cab1b8c59d1923bec6fa3eabc932e4e91542
Reviewed-on: https://go-review.googlesource.com/24749
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: Josh Bleecher Snyder <josharian@gmail.com>
This commit is contained in:
Keith Randall 2016-07-06 15:02:49 -07:00
parent a16a189fb9
commit e492d9f018
3 changed files with 117 additions and 3 deletions

View file

@ -6,6 +6,7 @@ package runtime_test
import ( import (
"bytes" "bytes"
"flag"
"fmt" "fmt"
"internal/testenv" "internal/testenv"
"io/ioutil" "io/ioutil"
@ -482,3 +483,39 @@ func TestMemPprof(t *testing.T) {
t.Error("missing MemProf in pprof output") t.Error("missing MemProf in pprof output")
} }
} }
var concurrentMapTest = flag.Bool("run_concurrent_map_tests", false, "also run flaky concurrent map tests")
func TestConcurrentMapWrites(t *testing.T) {
if !*concurrentMapTest {
t.Skip("skipping without -run_concurrent_map_tests")
}
testenv.MustHaveGoRun(t)
output := runTestProg(t, "testprog", "concurrentMapWrites")
want := "fatal error: concurrent map writes"
if !strings.HasPrefix(output, want) {
t.Fatalf("output does not start with %q:\n%s", want, output)
}
}
func TestConcurrentMapReadWrite(t *testing.T) {
if !*concurrentMapTest {
t.Skip("skipping without -run_concurrent_map_tests")
}
testenv.MustHaveGoRun(t)
output := runTestProg(t, "testprog", "concurrentMapReadWrite")
want := "fatal error: concurrent map read and map write"
if !strings.HasPrefix(output, want) {
t.Fatalf("output does not start with %q:\n%s", want, output)
}
}
func TestConcurrentMapIterateWrite(t *testing.T) {
if !*concurrentMapTest {
t.Skip("skipping without -run_concurrent_map_tests")
}
testenv.MustHaveGoRun(t)
output := runTestProg(t, "testprog", "concurrentMapIterateWrite")
want := "fatal error: concurrent map iteration and map write"
if !strings.HasPrefix(output, want) {
t.Fatalf("output does not start with %q:\n%s", want, output)
}
}

View file

@ -382,9 +382,6 @@ func mapaccessK(t *maptype, h *hmap, key unsafe.Pointer) (unsafe.Pointer, unsafe
if h == nil || h.count == 0 { if h == nil || h.count == 0 {
return nil, nil return nil, nil
} }
if h.flags&hashWriting != 0 {
throw("concurrent map read and map write")
}
alg := t.key.alg alg := t.key.alg
hash := alg.hash(key, uintptr(h.hash0)) hash := alg.hash(key, uintptr(h.hash0))
m := uintptr(1)<<h.B - 1 m := uintptr(1)<<h.B - 1
@ -685,6 +682,9 @@ func mapiternext(it *hiter) {
callerpc := getcallerpc(unsafe.Pointer(&it)) callerpc := getcallerpc(unsafe.Pointer(&it))
racereadpc(unsafe.Pointer(h), callerpc, funcPC(mapiternext)) racereadpc(unsafe.Pointer(h), callerpc, funcPC(mapiternext))
} }
if h.flags&hashWriting != 0 {
throw("concurrent map iteration and map write")
}
t := it.t t := it.t
bucket := it.bucket bucket := it.bucket
b := it.bptr b := it.bptr

77
src/runtime/testdata/testprog/map.go vendored Normal file
View file

@ -0,0 +1,77 @@
// 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
import "runtime"
func init() {
register("concurrentMapWrites", concurrentMapWrites)
register("concurrentMapReadWrite", concurrentMapReadWrite)
register("concurrentMapIterateWrite", concurrentMapIterateWrite)
}
func concurrentMapWrites() {
m := map[int]int{}
c := make(chan struct{})
go func() {
for i := 0; i < 10000; i++ {
m[5] = 0
runtime.Gosched()
}
c <- struct{}{}
}()
go func() {
for i := 0; i < 10000; i++ {
m[6] = 0
runtime.Gosched()
}
c <- struct{}{}
}()
<-c
<-c
}
func concurrentMapReadWrite() {
m := map[int]int{}
c := make(chan struct{})
go func() {
for i := 0; i < 10000; i++ {
m[5] = 0
runtime.Gosched()
}
c <- struct{}{}
}()
go func() {
for i := 0; i < 10000; i++ {
_ = m[6]
runtime.Gosched()
}
c <- struct{}{}
}()
<-c
<-c
}
func concurrentMapIterateWrite() {
m := map[int]int{}
c := make(chan struct{})
go func() {
for i := 0; i < 10000; i++ {
m[5] = 0
runtime.Gosched()
}
c <- struct{}{}
}()
go func() {
for i := 0; i < 10000; i++ {
for range m {
}
runtime.Gosched()
}
c <- struct{}{}
}()
<-c
<-c
}