Skip to content

runtime: use separate stack for cgo callbacks #11089

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

Closed
randall77 opened this issue Jun 5, 2015 · 4 comments
Closed

runtime: use separate stack for cgo callbacks #11089

randall77 opened this issue Jun 5, 2015 · 4 comments
Milestone

Comments

@randall77
Copy link
Contributor

Currently there is a problem combining cgo callbacks and stack copying. Imagine we pass a pointer to a Go-stack-allocated object into cgo. That in itself is fine, as the Go stack will not be grown (because no one is running on it) or shrunk (because we forbid it, just like syscalls) during the cgo call. However, if cgo does a callback into Go, we resume running on the same Go stack. Additional stack use might force us to grow the Go stack, which involves copying it. After the callback returns, the pointers that the C code had are now bogus.

There are no easy solutions here. I propose to fix this problem by allocating a new stack for the callback. That way we can leave the previous Go stack frozen so pointers into it remain valid. It will be somewhat expensive, but cgo callbacks are hopefully uncommon. I have not seen any reports of this bug happening in the wild.

Allocating a new stack for the callback will complicate stack tracebacks, panic, and the like. There will now potentially be multiple stacks per G, which I'm sure will get tricky. (I've also considered using a whole new G for the callback, but that has its own issues).

Related to issue 10303.

@randall77
Copy link
Contributor Author

Also see issue 8771, https://codereview.appspot.com/144130043/

@minux
Copy link
Member

minux commented Jun 5, 2015

It seems we removed segmented stack support too soon?
It's a perfect case to use a new segmented stack for growing
callback stacks.

Please don't assume cgo callbacks are rare. (They are the
primary entry points for c-shared/c-archive build modes.)

@alexbrainman
Copy link
Member

This also applies to windows (non-cgo) callbacks.

Alex

@ianlancetaylor ianlancetaylor added this to the Go1.6 milestone Jul 11, 2015
@randall77
Copy link
Contributor Author

Russ fixed this with the (much easier) change to just disallow passing pointers into the stack to cgo.

https://go-review.googlesource.com/#/c/10814/

@mikioh mikioh modified the milestones: Go1.5, Go1.6 Jul 11, 2015
@golang golang locked and limited conversation to collaborators Jul 11, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants