Closed
Description
This is for better performance, which will avoid escaping.
example:
//go:cgo_unsafe_stack_pointer str
void strpointer(void *str) {
// must not callback to go
}
Now, the str
pointer must be allocated on the heap, for safety.
AFAIK, the unsafe cases:
- C code calls back into Go code, and the Go code triggers a stack copy, the
str
pointer might move. from: cmd/cgo: replace _Cgo_use with runtime.KeepAlive? #20281 (comment) - GC may trigger
shrinkstack
, thestr
pointer might move, before C returns.
So, when people use //go:cgo_unsafe_stack_pointer
,
they must make sure the C code will not call back to go, it's an unsafe usage.
And, the go compiler needs these changes:
- skip generating
_Cgo_use
forstr
pointer. - skip
shrinkstack
when the goroutine is invoking such an unsafe C function (we could set a flag underg
, before invoking the C function).