Skip to content

Commit 75d7795

Browse files
committed
runtime/cgo: make compatible with race detector
Some routines run without and m or g and cannot invoke the race detector runtime. They must be opaque to the runtime. That used to be true because they were written in C. Now that they are written in Go, disable the race detector annotations for those functions explicitly. Add test. Fixes #10874. Change-Id: Ia8cc28d51e7051528f9f9594b75634e6bb66a785 Reviewed-on: https://go-review.googlesource.com/12534 Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent 0acecb7 commit 75d7795

File tree

10 files changed

+140
-117
lines changed

10 files changed

+140
-117
lines changed

src/cmd/cgo/out.go

+1
Original file line numberDiff line numberDiff line change
@@ -809,6 +809,7 @@ func (p *Package) writeExports(fgo2, fm, fgcc, fgcch io.Writer) {
809809
fmt.Fprintf(fgo2, "//go:linkname _cgoexp%s_%s _cgoexp%s_%s\n", cPrefix, exp.ExpName, cPrefix, exp.ExpName)
810810
fmt.Fprintf(fgo2, "//go:cgo_export_static _cgoexp%s_%s\n", cPrefix, exp.ExpName)
811811
fmt.Fprintf(fgo2, "//go:nosplit\n") // no split stack, so no use of m or g
812+
fmt.Fprintf(fgo2, "//go:norace\n") // must not have race detector calls inserted
812813
fmt.Fprintf(fgo2, "func _cgoexp%s_%s(a unsafe.Pointer, n int32) {", cPrefix, exp.ExpName)
813814
fmt.Fprintf(fgo2, "\tfn := %s\n", goname)
814815
// The indirect here is converting from a Go function pointer to a C function pointer.

src/cmd/compile/internal/gc/go.go

+1
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,7 @@ var (
660660
nosplit bool
661661
nowritebarrier bool
662662
systemstack bool
663+
norace bool
663664
)
664665

665666
var debuglive int

src/cmd/compile/internal/gc/go.y

+2
Original file line numberDiff line numberDiff line change
@@ -1390,6 +1390,7 @@ xfndcl:
13901390
$$.Nbody = $3;
13911391
$$.Func.Endlineno = lineno;
13921392
$$.Noescape = noescape;
1393+
$$.Func.Norace = norace;
13931394
$$.Func.Nosplit = nosplit;
13941395
$$.Func.Nowritebarrier = nowritebarrier;
13951396
$$.Func.Systemstack = systemstack;
@@ -1579,6 +1580,7 @@ xdcl_list:
15791580
}
15801581
nointerface = false
15811582
noescape = false
1583+
norace = false
15821584
nosplit = false
15831585
nowritebarrier = false
15841586
systemstack = false

src/cmd/compile/internal/gc/lex.go

+5
Original file line numberDiff line numberDiff line change
@@ -1612,6 +1612,11 @@ func getlinepragma() int {
16121612
return c
16131613
}
16141614

1615+
if verb == "go:norace" {
1616+
norace = true
1617+
return c
1618+
}
1619+
16151620
if verb == "go:nosplit" {
16161621
nosplit = true
16171622
return c

src/cmd/compile/internal/gc/racewalk.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ func ispkgin(pkgs []string) bool {
4242
return false
4343
}
4444

45+
// TODO(rsc): Remove. Put //go:norace on forkAndExecInChild instead.
4546
func isforkfunc(fn *Node) bool {
4647
// Special case for syscall.forkAndExecInChild.
4748
// In the child, this function must not acquire any locks, because
@@ -52,7 +53,7 @@ func isforkfunc(fn *Node) bool {
5253
}
5354

5455
func racewalk(fn *Node) {
55-
if ispkgin(omit_pkgs) || isforkfunc(fn) {
56+
if ispkgin(omit_pkgs) || isforkfunc(fn) || fn.Func.Norace {
5657
return
5758
}
5859

src/cmd/compile/internal/gc/syntax.go

+1
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ type Func struct {
169169

170170
Endlineno int32
171171

172+
Norace bool // func must not have race detector annotations
172173
Nosplit bool // func should not execute on separate stack
173174
Nowritebarrier bool // emit compiler error instead of write barrier
174175
Dupok bool // duplicate definitions ok

0 commit comments

Comments
 (0)