Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "cmd/cgo: disable #cgo noescape/nocallback until Go 1.23" #66879

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/cmd/cgo/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,30 @@ 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
Expand Down
2 changes: 0 additions & 2 deletions src/cmd/cgo/gcc.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,8 @@ 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
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/cmd/cgo/internal/test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,7 @@ int add(int x, int y) {

// escape vs noescape

// TODO(#56378): enable in Go 1.23:
// #cgo noescape handleGoStringPointerNoescape
#cgo noescape handleGoStringPointerNoescape
void handleGoStringPointerNoescape(void *s) {}

void handleGoStringPointerEscape(void *s) {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
package main

/*
// 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
// ERROR MESSAGE: #cgo noescape noMatchedCFunction: no matched C function
#cgo noescape noMatchedCFunction
*/
import "C"
Expand Down
2 changes: 0 additions & 2 deletions src/runtime/crash_cgo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -754,7 +754,6 @@ 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) {
Expand All @@ -763,7 +762,6 @@ 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 {
Expand Down
3 changes: 2 additions & 1 deletion src/runtime/testdata/testprogcgo/cgonocallback.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ package main
// But it do callback to go in this test, Go should crash here.

/*
// TODO(#56378): #cgo nocallback runCShouldNotCallback
#cgo nocallback runCShouldNotCallback

extern void runCShouldNotCallback();
*/
import "C"
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/testdata/testprogcgo/cgonoescape.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ package main
// 2. less than 100 new allocated heap objects after invoking withoutNoEscape 100 times.

/*
// TODO(#56378): #cgo noescape runCWithNoEscape
#cgo noescape runCWithNoEscape

void runCWithNoEscape(void *p) {
}
Expand Down