]> Cypherpunks repositories - gostls13.git/commit
cmd/cgo: add #cgo noescape/nocallback annotations
authordoujiang24 <doujiang24@gmail.com>
Fri, 25 Aug 2023 17:06:31 +0000 (17:06 +0000)
committerGopher Robot <gobot@golang.org>
Fri, 25 Aug 2023 17:39:23 +0000 (17:39 +0000)
commit24b9ef1a7366fe751880ab2098cff630144b8ac8
tree0f1de5cac722c84485bdfd9c1cc80f6422d7a037
parent1a01cb22f9ab07d55ee61c95a34e1e18e49596c0
cmd/cgo: add #cgo noescape/nocallback annotations

When passing pointers of Go objects from Go to C, the cgo command generate _Cgo_use(pN) for the unsafe.Pointer type arguments, so that the Go compiler will escape these object to heap.

Since the C function may callback to Go, then the Go stack might grow/shrink, that means the pointers that the C function have will be invalid.

After adding the #cgo noescape annotation for a C function, the cgo command won't generate _Cgo_use(pN), and the Go compiler won't force the object escape to heap.

After adding the #cgo nocallback annotation for a C function, which means the C function won't callback to Go, if it do callback to Go, the Go process will crash.

Fixes #56378

Change-Id: Ifdca070584e0d349c7b12276270e50089e481f7a
GitHub-Last-Rev: f1a17b08b0590eca2670e404bbfedad5461df72f
GitHub-Pull-Request: golang/go#60399
Reviewed-on: https://go-review.googlesource.com/c/go/+/497837
Reviewed-by: Ian Lance Taylor <iant@google.com>
Reviewed-by: Bryan Mills <bcmills@google.com>
Run-TryBot: Bryan Mills <bcmills@google.com>
Auto-Submit: Bryan Mills <bcmills@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
15 files changed:
src/cmd/cgo/doc.go
src/cmd/cgo/gcc.go
src/cmd/cgo/internal/testerrors/errors_test.go
src/cmd/cgo/internal/testerrors/testdata/notmatchedcfunction.go [new file with mode: 0644]
src/cmd/cgo/main.go
src/cmd/cgo/out.go
src/cmd/go/internal/modindex/build.go
src/go/build/build.go
src/runtime/cgo.go
src/runtime/cgocall.go
src/runtime/crash_cgo_test.go
src/runtime/runtime2.go
src/runtime/testdata/testprogcgo/cgonocallback.c [new file with mode: 0644]
src/runtime/testdata/testprogcgo/cgonocallback.go [new file with mode: 0644]
src/runtime/testdata/testprogcgo/cgonoescape.go [new file with mode: 0644]