Skip to content

Commit c1fc209

Browse files
randall77gopherbot
authored andcommitted
runtime: use precise bounds of Go data/bss for race detector
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 <[email protected]> Auto-Submit: Keith Randall <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Dmitry Vyukov <[email protected]>
1 parent 9d0320d commit c1fc209

File tree

4 files changed

+28
-6
lines changed

4 files changed

+28
-6
lines changed

src/runtime/race.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,6 @@ func raceinit() (gctx, pctx uintptr) {
455455

456456
racecall(&__tsan_init, uintptr(unsafe.Pointer(&gctx)), uintptr(unsafe.Pointer(&pctx)), abi.FuncPCABI0(racecallbackthunk), 0)
457457

458-
// Round data segment to page boundaries, because it's used in mmap().
459458
start := ^uintptr(0)
460459
end := uintptr(0)
461460
if start > firstmoduledata.noptrdata {
@@ -482,10 +481,13 @@ func raceinit() (gctx, pctx uintptr) {
482481
if end < firstmoduledata.ebss {
483482
end = firstmoduledata.ebss
484483
}
485-
size := alignUp(end-start, _PageSize)
486-
racecall(&__tsan_map_shadow, start, size, 0, 0)
484+
// Use exact bounds for boundary check in racecalladdr. See issue 73483.
487485
racedatastart = start
488-
racedataend = start + size
486+
racedataend = end
487+
// Round data segment to page boundaries for race detector (TODO: still needed?)
488+
start = alignDown(start, _PageSize)
489+
end = alignUp(end, _PageSize)
490+
racecall(&__tsan_map_shadow, start, end-start, 0, 0)
489491

490492
return
491493
}

src/runtime/race_arm64.s

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ data:
163163
BLT ret
164164
MOVD runtime·racedataend(SB), R10
165165
CMP R10, R1
166-
BGT ret
166+
BGE ret
167167
call:
168168
JMP racecall<>(SB)
169169
ret:

src/runtime/race_ppc64le.s

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ data:
153153
BLT ret
154154
MOVD runtime·racedataend(SB), R9
155155
CMP R4, R9
156-
BGT ret
156+
BGE ret
157157
call:
158158
// Careful!! racecall will save LR on its
159159
// stack, which is OK as long as racecalladdr

test/fixedbugs/issue73483.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// run -race
2+
3+
//go:build race && cgo
4+
5+
// Copyright 2025 The Go Authors. All rights reserved.
6+
// Use of this source code is governed by a BSD-style
7+
// license that can be found in the LICENSE file.
8+
9+
package main
10+
11+
/*
12+
int v[8192];
13+
*/
14+
import "C"
15+
16+
var x [8192]C.int
17+
18+
func main() {
19+
copy(C.v[:], x[:])
20+
}

0 commit comments

Comments
 (0)