]> Cypherpunks repositories - gostls13.git/commitdiff
runtime: fix map iterator concurrent map check
authorKeith Randall <khr@golang.org>
Wed, 6 Jul 2016 22:02:49 +0000 (15:02 -0700)
committerKeith Randall <khr@golang.org>
Tue, 16 Aug 2016 21:52:44 +0000 (21:52 +0000)
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>
src/runtime/crash_test.go
src/runtime/hashmap.go
src/runtime/testdata/testprog/map.go [new file with mode: 0644]

index a2f7ff7dec83462743c14ebeb61da87fec72bda1..2f6afb60f6d4f5abadbb23bcf762a67762591e25 100644 (file)
@@ -6,6 +6,7 @@ package runtime_test
 
 import (
        "bytes"
+       "flag"
        "fmt"
        "internal/testenv"
        "io/ioutil"
@@ -482,3 +483,39 @@ func TestMemPprof(t *testing.T) {
                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)
+       }
+}
index 509cab2f0f0565b683369216fa0f74bbb434af39..f756e7b6035392430d162f1b14b0f20c0232ad54 100644 (file)
@@ -382,9 +382,6 @@ func mapaccessK(t *maptype, h *hmap, key unsafe.Pointer) (unsafe.Pointer, unsafe
        if h == nil || h.count == 0 {
                return nil, nil
        }
-       if h.flags&hashWriting != 0 {
-               throw("concurrent map read and map write")
-       }
        alg := t.key.alg
        hash := alg.hash(key, uintptr(h.hash0))
        m := uintptr(1)<<h.B - 1
@@ -685,6 +682,9 @@ func mapiternext(it *hiter) {
                callerpc := getcallerpc(unsafe.Pointer(&it))
                racereadpc(unsafe.Pointer(h), callerpc, funcPC(mapiternext))
        }
+       if h.flags&hashWriting != 0 {
+               throw("concurrent map iteration and map write")
+       }
        t := it.t
        bucket := it.bucket
        b := it.bptr
diff --git a/src/runtime/testdata/testprog/map.go b/src/runtime/testdata/testprog/map.go
new file mode 100644 (file)
index 0000000..5524289
--- /dev/null
@@ -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
+}