From: Russ Cox Date: Fri, 28 Apr 2017 16:45:46 +0000 (-0400) Subject: runtime: fix profile handling of labels for race detector X-Git-Tag: go1.9beta1~409 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=c82efb1fa370ea83a7e9ff89d85598bb0fdc3fb5;p=gostls13.git runtime: fix profile handling of labels for race detector If g1 sets its labels and then they are copied into a profile buffer and then g2 reads the profile buffer and inspects the labels, the race detector must understand that g1's recording of the labels happens before g2's use of the labels. Make that so. Fixes race test failure in CL 39613. Change-Id: Id7cda1c2aac6f8eef49213b5ca414f7154b4acfa Reviewed-on: https://go-review.googlesource.com/42111 Run-TryBot: Russ Cox TryBot-Result: Gobot Gobot Reviewed-by: Michael Matloob --- diff --git a/src/runtime/profbuf.go b/src/runtime/profbuf.go index 605d8d34a4..2719238bc1 100644 --- a/src/runtime/profbuf.go +++ b/src/runtime/profbuf.go @@ -544,5 +544,18 @@ Read: // Remember how much we returned, to commit read on next call. b.rNext = br.addCountsAndClearFlags(skip+di, ti) + if raceenabled { + // Match racewritepc in runtime_setProfLabel, + // so that the setting of the labels in runtime_setProfLabel + // is treated as happening before any use of the labels + // by our caller. The synchronization on labelSync itself is a fiction + // for the race detector. The actual synchronization is handled + // by the fact that the signal handler only reads from the current + // goroutine and uses atomics to write the updated queue indices, + // and then the read-out from the signal handler buffer uses + // atomics to read those queue indices. + raceacquire(unsafe.Pointer(&labelSync)) + } + return data[:di], tags[:ti], false } diff --git a/src/runtime/proflabel.go b/src/runtime/proflabel.go index 9742afafd7..1b41a8a16e 100644 --- a/src/runtime/proflabel.go +++ b/src/runtime/proflabel.go @@ -6,8 +6,16 @@ package runtime import "unsafe" +var labelSync uintptr + //go:linkname runtime_setProfLabel runtime/pprof.runtime_setProfLabel func runtime_setProfLabel(labels unsafe.Pointer) { + // Introduce race edge for read-back via profile. + // This would more properly use &getg().labels as the sync address, + // but we do the read in a signal handler and can't call the race runtime then. + if raceenabled { + racerelease(unsafe.Pointer(&labelSync)) + } getg().labels = labels }