Skip to content

Commit

Permalink
Revert "cmd/cgo: disable #cgo noescape/nocallback until Go 1.23"
Browse files Browse the repository at this point in the history
This reverts commit 607e020.

Reason for revert: Go1.22 is released.

It's aggressive to introdcue #cgo noescape/nocallback in Go1.22, as in golang#63739
And it won't be a problem again while upgrading from Go1.22 to Go1.23.

fix golang#56378

Signed-off-by: doujiang24 <doujiang24@gmail.com>
  • Loading branch information
doujiang24 committed Apr 18, 2024
1 parent 334ce51 commit cdc4e9c
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 10 deletions.
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

0 comments on commit cdc4e9c

Please sign in to comment.