Skip to content

Commit b9c30c7

Browse files
rscpull[bot]
authored andcommitted
cmd/cgo: disable #cgo noescape/nocallback until Go 1.23
Go 1.21 and earlier do not understand this line, causing "go mod vendor" of //go:build go1.22-tagged code that uses this feature to fail. The solution is to include the go/build change to skip over the line in Go 1.22 (making "go mod vendor" from Go 1.22 onward work with this change) and then wait to deploy the cgo change until Go 1.23, at which point Go 1.21 and earlier will be unsupported. For #56378. Fixes #63293. Change-Id: Ifa08b134eac5a6aa15d67dad0851f00e15e1e58b Reviewed-on: https://go-review.googlesource.com/c/go/+/539235 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Cherry Mui <[email protected]> Reviewed-by: Bryan Mills <[email protected]>
1 parent 1b05e91 commit b9c30c7

File tree

8 files changed

+11
-47
lines changed

8 files changed

+11
-47
lines changed

doc/go1.22.html

+1-18
Original file line numberDiff line numberDiff line change
@@ -39,24 +39,7 @@ <h3 id="go-command">Go command</h3>
3939

4040
<h3 id="cgo">Cgo</h3>
4141

42-
<p><!-- CL 497837 --> The special comment that precedes
43-
<code>import</code> <code>"C"</code> may now include two
44-
new <code>#cgo</code> directives.
45-
<ul>
46-
<li>
47-
<code>#cgo</code> <code>noescape</code> <code>cFunctionName</code>
48-
tells cgo that Go pointers passed to the C function
49-
<code>cFunctionName</code> do not escape.
50-
</li>
51-
<li>
52-
<code>#cgo</code> <code>nocallback</code> <code>cFunctionName</code>
53-
tells cgo that the C function <code>cFunctionName</code> does
54-
not call any Go functions.
55-
</li>
56-
</ul>
57-
See <a href="/cmd/cgo#hdr-Optimizing_calls_of_C_code">the <code>cgo</code>
58-
documentation</a> for more details.
59-
</p>
42+
<!-- CL 497837 reverted -->
6043

6144
<h2 id="runtime">Runtime</h2>
6245

src/cmd/cgo/doc.go

-24
Original file line numberDiff line numberDiff line change
@@ -420,30 +420,6 @@ passing uninitialized C memory to Go code if the Go code is going to
420420
store pointer values in it. Zero out the memory in C before passing it
421421
to Go.
422422
423-
# Optimizing calls of C code
424-
425-
When passing a Go pointer to a C function the compiler normally ensures
426-
that the Go object lives on the heap. If the C function does not keep
427-
a copy of the Go pointer, and never passes the Go pointer back to Go code,
428-
then this is unnecessary. The #cgo noescape directive may be used to tell
429-
the compiler that no Go pointers escape via the named C function.
430-
If the noescape directive is used and the C function does not handle the
431-
pointer safely, the program may crash or see memory corruption.
432-
433-
For example:
434-
435-
// #cgo noescape cFunctionName
436-
437-
When a Go function calls a C function, it prepares for the C function to
438-
call back to a Go function. the #cgo nocallback directive may be used to
439-
tell the compiler that these preparations are not necessary.
440-
If the nocallback directive is used and the C function does call back into
441-
Go code, the program will panic.
442-
443-
For example:
444-
445-
// #cgo nocallback cFunctionName
446-
447423
# Special cases
448424
449425
A few special C types which would normally be represented by a pointer

src/cmd/cgo/gcc.go

+2
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,10 @@ func (f *File) ProcessCgoDirectives() {
9494
directive := fields[1]
9595
funcName := fields[2]
9696
if directive == "nocallback" {
97+
fatalf("#cgo nocallback disabled until Go 1.23")
9798
f.NoCallbacks[funcName] = true
9899
} else if directive == "noescape" {
100+
fatalf("#cgo noescape disabled until Go 1.23")
99101
f.NoEscapes[funcName] = true
100102
}
101103
}

src/cmd/cgo/internal/test/test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,8 @@ int add(int x, int y) {
117117
118118
// escape vs noescape
119119
120-
#cgo noescape handleGoStringPointerNoescape
120+
// TODO(#56378): enable in Go 1.23:
121+
// #cgo noescape handleGoStringPointerNoescape
121122
void handleGoStringPointerNoescape(void *s) {}
122123
123124
void handleGoStringPointerEscape(void *s) {}

src/cmd/cgo/internal/testerrors/testdata/notmatchedcfunction.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
package main
66

77
/*
8-
// ERROR MESSAGE: #cgo noescape noMatchedCFunction: no matched C function
8+
// TODO(#56378): change back to "#cgo noescape noMatchedCFunction: no matched C function" in Go 1.23
9+
// ERROR MESSAGE: #cgo noescape disabled until Go 1.23
910
#cgo noescape noMatchedCFunction
1011
*/
1112
import "C"

src/runtime/crash_cgo_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,7 @@ func TestNeedmDeadlock(t *testing.T) {
754754
}
755755

756756
func TestCgoNoCallback(t *testing.T) {
757+
t.Skip("TODO(#56378): enable in Go 1.23")
757758
got := runTestProg(t, "testprogcgo", "CgoNoCallback")
758759
want := "function marked with #cgo nocallback called back into Go"
759760
if !strings.Contains(got, want) {
@@ -762,6 +763,7 @@ func TestCgoNoCallback(t *testing.T) {
762763
}
763764

764765
func TestCgoNoEscape(t *testing.T) {
766+
t.Skip("TODO(#56378): enable in Go 1.23")
765767
got := runTestProg(t, "testprogcgo", "CgoNoEscape")
766768
want := "OK\n"
767769
if got != want {

src/runtime/testdata/testprogcgo/cgonocallback.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ package main
88
// But it do callback to go in this test, Go should crash here.
99

1010
/*
11-
#cgo nocallback runCShouldNotCallback
12-
11+
// TODO(#56378): #cgo nocallback runCShouldNotCallback
1312
extern void runCShouldNotCallback();
1413
*/
1514
import "C"

src/runtime/testdata/testprogcgo/cgonoescape.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ package main
1313
// 2. less than 100 new allocated heap objects after invoking withoutNoEscape 100 times.
1414

1515
/*
16-
#cgo noescape runCWithNoEscape
16+
// TODO(#56378): #cgo noescape runCWithNoEscape
1717
1818
void runCWithNoEscape(void *p) {
1919
}

0 commit comments

Comments
 (0)