<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>
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
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
}
}
// escape vs noescape
-#cgo noescape handleGoStringPointerNoescape
+// TODO(#56378): enable in Go 1.23:
+// #cgo noescape handleGoStringPointerNoescape
void handleGoStringPointerNoescape(void *s) {}
void handleGoStringPointerEscape(void *s) {}
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"
}
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) {
}
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 {
// 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"
// 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) {
}