]> Cypherpunks.ru repositories - gostls13.git/commitdiff
cmd/cgo: disable #cgo noescape/nocallback until Go 1.23
authorRuss Cox <rsc@golang.org>
Thu, 2 Nov 2023 12:15:44 +0000 (08:15 -0400)
committerRuss Cox <rsc@golang.org>
Thu, 2 Nov 2023 16:43:23 +0000 (16:43 +0000)
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 <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
Reviewed-by: Bryan Mills <bcmills@google.com>
doc/go1.22.html
src/cmd/cgo/doc.go
src/cmd/cgo/gcc.go
src/cmd/cgo/internal/test/test.go
src/cmd/cgo/internal/testerrors/testdata/notmatchedcfunction.go
src/runtime/crash_cgo_test.go
src/runtime/testdata/testprogcgo/cgonocallback.go
src/runtime/testdata/testprogcgo/cgonoescape.go

index 86529512741ab215140c78bac29b1d59b6aef59b..287ee77bb567083224702df5cbc8a482863dc374 100644 (file)
@@ -39,24 +39,7 @@ Do not send CLs removing the interior tags from such phrases.
 
 <h3 id="cgo">Cgo</h3>
 
-<p><!-- CL 497837 --> The special comment that precedes
-  <code>import</code> <code>"C"</code> may now include two
-  new <code>#cgo</code> directives.
-  <ul>
-    <li>
-      <code>#cgo</code> <code>noescape</code> <code>cFunctionName</code>
-      tells cgo that Go pointers passed to the C function
-      <code>cFunctionName</code> do not escape.
-    </li>
-    <li>
-      <code>#cgo</code> <code>nocallback</code> <code>cFunctionName</code>
-      tells cgo that the C function <code>cFunctionName</code> does
-      not call any Go functions.
-    </li>
-  </ul>
-  See <a href="/cmd/cgo#hdr-Optimizing_calls_of_C_code">the <code>cgo</code>
-  documentation</a> for more details.
-</p>
+<!-- CL 497837 reverted -->
 
 <h2 id="runtime">Runtime</h2>
 
index 1f635d7c098bca174708bf204d7830d6d9f642b4..c2e375165c6731903225624d293a9beb52e4ee95 100644 (file)
@@ -420,30 +420,6 @@ passing uninitialized C memory to Go code if the Go code is going to
 store pointer values in it. Zero out the memory in C before passing it
 to Go.
 
-# Optimizing calls of C code
-
-When passing a Go pointer to a C function the compiler normally ensures
-that the Go object lives on the heap. If the C function does not keep
-a copy of the Go pointer, and never passes the Go pointer back to Go code,
-then this is unnecessary. The #cgo noescape directive may be used to tell
-the compiler that no Go pointers escape via the named C function.
-If the noescape directive is used and the C function does not handle the
-pointer safely, the program may crash or see memory corruption.
-
-For example:
-
-       // #cgo noescape cFunctionName
-
-When a Go function calls a C function, it prepares for the C function to
-call back to a Go function. the #cgo nocallback directive may be used to
-tell the compiler that these preparations are not necessary.
-If the nocallback directive is used and the C function does call back into
-Go code, the program will panic.
-
-For example:
-
-       // #cgo nocallback cFunctionName
-
 # Special cases
 
 A few special C types which would normally be represented by a pointer
index d30056ec840033c6bd2188a2e0ab6d7ce87b6365..6e7556de9606281571b1955bfa400d0fb6ca5c86 100644 (file)
@@ -94,8 +94,10 @@ func (f *File) ProcessCgoDirectives() {
                                directive := fields[1]
                                funcName := fields[2]
                                if directive == "nocallback" {
+                                       fatalf("#cgo nocallback disabled until Go 1.23")
                                        f.NoCallbacks[funcName] = true
                                } else if directive == "noescape" {
+                                       fatalf("#cgo noescape disabled until Go 1.23")
                                        f.NoEscapes[funcName] = true
                                }
                        }
index 9a6c6d82cefa1abfde6c3a2dcc03d1ed08a6f479..9b3790eb11e2d85dde3c4f23cf06f42210969fa3 100644 (file)
@@ -117,7 +117,8 @@ int add(int x, int y) {
 
 // escape vs noescape
 
-#cgo noescape handleGoStringPointerNoescape
+// TODO(#56378): enable in Go 1.23:
+// #cgo noescape handleGoStringPointerNoescape
 void handleGoStringPointerNoescape(void *s) {}
 
 void handleGoStringPointerEscape(void *s) {}
index 46afeefcc0f65c3e83dab4e5cfd9a43a003bb415..5ec9ec5d4a9790916b955a69e512bc047756b8b7 100644 (file)
@@ -5,7 +5,8 @@
 package main
 
 /*
-// ERROR MESSAGE: #cgo noescape noMatchedCFunction: no matched C function
+// TODO(#56378): change back to "#cgo noescape noMatchedCFunction: no matched C function" in Go 1.23
+// ERROR MESSAGE: #cgo noescape disabled until Go 1.23
 #cgo noescape noMatchedCFunction
 */
 import "C"
index 20e3b75d79eaf208e61e7db36074ef9c43503c15..5d0750e8f45c82a6757771e64a6ddc3e06d3a778 100644 (file)
@@ -754,6 +754,7 @@ func TestNeedmDeadlock(t *testing.T) {
 }
 
 func TestCgoNoCallback(t *testing.T) {
+       t.Skip("TODO(#56378): enable in Go 1.23")
        got := runTestProg(t, "testprogcgo", "CgoNoCallback")
        want := "function marked with #cgo nocallback called back into Go"
        if !strings.Contains(got, want) {
@@ -762,6 +763,7 @@ func TestCgoNoCallback(t *testing.T) {
 }
 
 func TestCgoNoEscape(t *testing.T) {
+       t.Skip("TODO(#56378): enable in Go 1.23")
        got := runTestProg(t, "testprogcgo", "CgoNoEscape")
        want := "OK\n"
        if got != want {
index 8cbbfd1957d668abe4dd7902bc1c0b9fee8053c5..c13bf271a4aa3364eda3e0ad7a49c0e911723807 100644 (file)
@@ -8,8 +8,7 @@ package main
 // But it do callback to go in this test, Go should crash here.
 
 /*
-#cgo nocallback runCShouldNotCallback
-
+// TODO(#56378): #cgo nocallback runCShouldNotCallback
 extern void runCShouldNotCallback();
 */
 import "C"
index 056be44889b26405c3fd3dfd32fdc7e99d87b53a..f5eebac677ad1c9a669b9c3fd3a328928a4d30cb 100644 (file)
@@ -13,7 +13,7 @@ package main
 // 2. less than 100 new allocated heap objects after invoking withoutNoEscape 100 times.
 
 /*
-#cgo noescape runCWithNoEscape
+// TODO(#56378): #cgo noescape runCWithNoEscape
 
 void runCWithNoEscape(void *p) {
 }