From e25b913127ac8ba26c4ecc39288c7f8781f4ef5d Mon Sep 17 00:00:00 2001 From: Michael Pratt Date: Mon, 16 Sep 2024 11:22:02 -0400 Subject: [PATCH] internal/race,runtime: linkname contents of internal/race Rather than importing runtime directly, linkname the functions from runtime. This allows importing internal/race from internal/runtime/* packages, similar to internal/asan and internal/msan. For #54766. Cq-Include-Trybots: luci.golang.try:gotip-linux-amd64-longtest-swissmap Change-Id: Ibd9644557782076e3cee7927c8a6e6d2909f0a6e Reviewed-on: https://go-review.googlesource.com/c/go/+/616458 Reviewed-by: Keith Randall Auto-Submit: Michael Pratt LUCI-TryBot-Result: Go LUCI Reviewed-by: Keith Randall --- src/go/build/deps_test.go | 4 +-- src/internal/race/race.go | 53 ++++++++++++++------------------ src/runtime/race.go | 63 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 87 insertions(+), 33 deletions(-) diff --git a/src/go/build/deps_test.go b/src/go/build/deps_test.go index da2ab30a3b..c71d82f178 100644 --- a/src/go/build/deps_test.go +++ b/src/go/build/deps_test.go @@ -83,6 +83,7 @@ var depsRules = ` < internal/stringslite < internal/itoa < internal/unsafeheader + < internal/race < internal/msan < internal/asan < internal/runtime/sys @@ -92,7 +93,6 @@ var depsRules = ` < internal/runtime/math < internal/runtime/maps < runtime - < internal/race < sync/atomic < internal/weak < sync @@ -102,7 +102,7 @@ var depsRules = ` < errors < internal/oserror; - cmp, internal/race, runtime, math/bits + cmp, runtime, math/bits < iter < maps, slices; diff --git a/src/internal/race/race.go b/src/internal/race/race.go index d2c7e53e41..58198559a9 100644 --- a/src/internal/race/race.go +++ b/src/internal/race/race.go @@ -7,48 +7,39 @@ package race import ( - "runtime" "unsafe" ) const Enabled = true -func Acquire(addr unsafe.Pointer) { - runtime.RaceAcquire(addr) -} +// Functions below pushed from runtime. -func Release(addr unsafe.Pointer) { - runtime.RaceRelease(addr) -} +//go:linkname Acquire +func Acquire(addr unsafe.Pointer) -func ReleaseMerge(addr unsafe.Pointer) { - runtime.RaceReleaseMerge(addr) -} +//go:linkname Release +func Release(addr unsafe.Pointer) -func Disable() { - runtime.RaceDisable() -} +//go:linkname ReleaseMerge +func ReleaseMerge(addr unsafe.Pointer) -func Enable() { - runtime.RaceEnable() -} +//go:linkname Disable +func Disable() -func Read(addr unsafe.Pointer) { - runtime.RaceRead(addr) -} +//go:linkname Enable +func Enable() -func Write(addr unsafe.Pointer) { - runtime.RaceWrite(addr) -} +//go:linkname Read +func Read(addr unsafe.Pointer) -func ReadRange(addr unsafe.Pointer, len int) { - runtime.RaceReadRange(addr, len) -} +//go:linkname Write +func Write(addr unsafe.Pointer) -func WriteRange(addr unsafe.Pointer, len int) { - runtime.RaceWriteRange(addr, len) -} +//go:linkname ReadRange +func ReadRange(addr unsafe.Pointer, len int) -func Errors() int { - return runtime.RaceErrors() -} +//go:linkname WriteRange +func WriteRange(addr unsafe.Pointer, len int) + +//go:linkname Errors +func Errors() int diff --git a/src/runtime/race.go b/src/runtime/race.go index 7d5cbce49e..75ca4f86d5 100644 --- a/src/runtime/race.go +++ b/src/runtime/race.go @@ -14,16 +14,49 @@ import ( // Public race detection API, present iff build with -race. func RaceRead(addr unsafe.Pointer) + +//go:linkname race_Read internal/race.Read +//go:nosplit +func race_Read(addr unsafe.Pointer) { + RaceRead(addr) +} + func RaceWrite(addr unsafe.Pointer) + +//go:linkname race_Write internal/race.Write +//go:nosplit +func race_Write(addr unsafe.Pointer) { + RaceWrite(addr) +} + func RaceReadRange(addr unsafe.Pointer, len int) + +//go:linkname race_ReadRange internal/race.ReadRange +//go:nosplit +func race_ReadRange(addr unsafe.Pointer, len int) { + RaceReadRange(addr, len) +} + func RaceWriteRange(addr unsafe.Pointer, len int) +//go:linkname race_WriteRange internal/race.WriteRange +//go:nosplit +func race_WriteRange(addr unsafe.Pointer, len int) { + RaceWriteRange(addr, len) +} + func RaceErrors() int { var n uint64 racecall(&__tsan_report_count, uintptr(unsafe.Pointer(&n)), 0, 0, 0) return int(n) } +//go:linkname race_Errors internal/race.Errors +//go:nosplit +func race_Errors() int { + return RaceErrors() +} + // RaceAcquire/RaceRelease/RaceReleaseMerge establish happens-before relations // between goroutines. These inform the race detector about actual synchronization // that it can't see for some reason (e.g. synchronization within RaceDisable/RaceEnable @@ -38,6 +71,12 @@ func RaceAcquire(addr unsafe.Pointer) { raceacquire(addr) } +//go:linkname race_Acquire internal/race.Acquire +//go:nosplit +func race_Acquire(addr unsafe.Pointer) { + RaceAcquire(addr) +} + // RaceRelease performs a release operation on addr that // can synchronize with a later RaceAcquire on addr. // @@ -49,6 +88,12 @@ func RaceRelease(addr unsafe.Pointer) { racerelease(addr) } +//go:linkname race_Release internal/race.Release +//go:nosplit +func race_Release(addr unsafe.Pointer) { + RaceRelease(addr) +} + // RaceReleaseMerge is like RaceRelease, but also establishes a happens-before // relation with the preceding RaceRelease or RaceReleaseMerge on addr. // @@ -60,6 +105,12 @@ func RaceReleaseMerge(addr unsafe.Pointer) { racereleasemerge(addr) } +//go:linkname race_ReleaseMerge internal/race.ReleaseMerge +//go:nosplit +func race_ReleaseMerge(addr unsafe.Pointer) { + RaceReleaseMerge(addr) +} + // RaceDisable disables handling of race synchronization events in the current goroutine. // Handling is re-enabled with RaceEnable. RaceDisable/RaceEnable can be nested. // Non-synchronization events (memory accesses, function entry/exit) still affect @@ -74,6 +125,12 @@ func RaceDisable() { gp.raceignore++ } +//go:linkname race_Disable internal/race.Disable +//go:nosplit +func race_Disable() { + RaceDisable() +} + // RaceEnable re-enables handling of race events in the current goroutine. // //go:nosplit @@ -85,6 +142,12 @@ func RaceEnable() { } } +//go:linkname race_Enable internal/race.Enable +//go:nosplit +func race_Enable() { + RaceEnable() +} + // Private interface for the runtime. const raceenabled = true