]> Cypherpunks repositories - gostls13.git/commitdiff
runtime: use precise bounds of Go data/bss for race detector
authorKeith Randall <khr@golang.org>
Wed, 23 Apr 2025 21:15:51 +0000 (14:15 -0700)
committerGopher Robot <gobot@golang.org>
Thu, 24 Apr 2025 06:22:12 +0000 (23:22 -0700)
We only want to call into the race detector for Go global variables.
By rounding up the region bounds, we can include some C globals.
Even worse, we can include only *part* of a C global, leading to
race{read,write}range calls which straddle the end of shadow memory.
That causes the race detector to barf.

Fix some off-by-one errors in the assembly comparisons. We want to
skip calling the race detector when addr == racedataend.

Fixes #73483

Change-Id: I436b0f588d6165b61f30cb7653016ba9b7cbf585
Reviewed-on: https://go-review.googlesource.com/c/go/+/667655
Reviewed-by: Keith Randall <khr@google.com>
Auto-Submit: Keith Randall <khr@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Dmitry Vyukov <dvyukov@google.com>
src/runtime/race.go
src/runtime/race_arm64.s
src/runtime/race_ppc64le.s
test/fixedbugs/issue73483.go [new file with mode: 0644]

index 6b7bbe52458b80f2693e879c6ad7088f10f91e40..fa781a3ccc04ca6e9f781a69184ae56cc3661b19 100644 (file)
@@ -455,7 +455,6 @@ func raceinit() (gctx, pctx uintptr) {
 
        racecall(&__tsan_init, uintptr(unsafe.Pointer(&gctx)), uintptr(unsafe.Pointer(&pctx)), abi.FuncPCABI0(racecallbackthunk), 0)
 
-       // Round data segment to page boundaries, because it's used in mmap().
        start := ^uintptr(0)
        end := uintptr(0)
        if start > firstmoduledata.noptrdata {
@@ -482,10 +481,13 @@ func raceinit() (gctx, pctx uintptr) {
        if end < firstmoduledata.ebss {
                end = firstmoduledata.ebss
        }
-       size := alignUp(end-start, _PageSize)
-       racecall(&__tsan_map_shadow, start, size, 0, 0)
+       // Use exact bounds for boundary check in racecalladdr. See issue 73483.
        racedatastart = start
-       racedataend = start + size
+       racedataend = end
+       // Round data segment to page boundaries for race detector (TODO: still needed?)
+       start = alignDown(start, _PageSize)
+       end = alignUp(end, _PageSize)
+       racecall(&__tsan_map_shadow, start, end-start, 0, 0)
 
        return
 }
index c42a6c1377528ea621ed709b160ab26432552b9f..83dfdef2e50c0ca3af52f5e215e6a48aff85e375 100644 (file)
@@ -163,7 +163,7 @@ data:
        BLT     ret
        MOVD    runtime·racedataend(SB), R10
        CMP     R10, R1
-       BGT     ret
+       BGE     ret
 call:
        JMP     racecall<>(SB)
 ret:
index 43829479bde86841f889b673bfa1086a5e45ee22..d3cac03ff48d687db556b1666da4b71d60111819 100644 (file)
@@ -153,7 +153,7 @@ data:
        BLT     ret
        MOVD    runtime·racedataend(SB), R9
        CMP     R4, R9
-       BGT     ret
+       BGE     ret
 call:
        // Careful!! racecall will save LR on its
        // stack, which is OK as long as racecalladdr
diff --git a/test/fixedbugs/issue73483.go b/test/fixedbugs/issue73483.go
new file mode 100644 (file)
index 0000000..8cd0c34
--- /dev/null
@@ -0,0 +1,20 @@
+// run -race
+
+//go:build race && cgo
+
+// Copyright 2025 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
+
+/*
+   int v[8192];
+*/
+import "C"
+
+var x [8192]C.int
+
+func main() {
+       copy(C.v[:], x[:])
+}