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

cmd/cgo: C functions that return values fail if they call a Go callback that copies the stack #8771

Closed
ianlancetaylor opened this issue Sep 18, 2014 · 2 comments
Milestone

Comments

@ianlancetaylor
Copy link
Contributor

Cgo uses a wrapper function that calls C code, passing the address of the stack frame.
 This wrapper function is compiled by GCC, and it calls the real function written by
the user.  The user's function is permitted to call Go callbacks.  Those Go callbacks
will run on the stack of the original caller.  They may cause a stack copy.

If the stack gets copied during a Go callback, then the caller of the GCC-compiled
wrapper is running in a different location.  The stack frame pointer used by the
GCC-compiled wrapper is not updated, since of course the stack copier knows nothing
about GCC-compiled code.  I don't think this is a problem for the arguments to the
function; they have already been copied out of the stack frame when the wrapper calls
the real function.

However, it is a problem for C functions that return a value.  The wrapper will take the
value returned by the C function, and store it using its pointer to the stack frame. 
That pointer will not have been updated if a stack copy occurs.  In other words, the
wrapper may store the return value on the old stack, not the new one.

There is a test case demonstrating the problem in
https://golang.org/cl/145130043.
@gopherbot
Copy link

Comment 1:

CL https://golang.org/cl/144130043 mentions this issue.

@randall77
Copy link
Contributor

Comment 2:

This issue was closed by revision 1b6807b.

Status changed to Fixed.

@rsc rsc added this to the Go1.4 milestone Apr 14, 2015
@rsc rsc removed the release-go1.4 label Apr 14, 2015
@golang golang locked and limited conversation to collaborators Jun 25, 2016
wheatman pushed a commit to wheatman/go-akaros that referenced this issue Jun 25, 2018
During a cgo call, the stack can be copied.  This copy invalidates
the pointer that cgo has into the return value area.  To fix this
problem, pass the address of the location containing the stack
top value (which is in the G struct).  For cgo functions which
return values, read the stktop before and after the cgo call to
compute the adjustment necessary to write the return value.

Fixes golang#8771

LGTM=iant, rsc
R=iant, rsc, khr
CC=golang-codereviews
https://golang.org/cl/144130043
wheatman pushed a commit to wheatman/go-akaros that referenced this issue Jun 26, 2018
During a cgo call, the stack can be copied.  This copy invalidates
the pointer that cgo has into the return value area.  To fix this
problem, pass the address of the location containing the stack
top value (which is in the G struct).  For cgo functions which
return values, read the stktop before and after the cgo call to
compute the adjustment necessary to write the return value.

Fixes golang#8771

LGTM=iant, rsc
R=iant, rsc, khr
CC=golang-codereviews
https://golang.org/cl/144130043
wheatman pushed a commit to wheatman/go-akaros that referenced this issue Jul 9, 2018
During a cgo call, the stack can be copied.  This copy invalidates
the pointer that cgo has into the return value area.  To fix this
problem, pass the address of the location containing the stack
top value (which is in the G struct).  For cgo functions which
return values, read the stktop before and after the cgo call to
compute the adjustment necessary to write the return value.

Fixes golang#8771

LGTM=iant, rsc
R=iant, rsc, khr
CC=golang-codereviews
https://golang.org/cl/144130043
wheatman pushed a commit to wheatman/go-akaros that referenced this issue Jul 30, 2018
During a cgo call, the stack can be copied.  This copy invalidates
the pointer that cgo has into the return value area.  To fix this
problem, pass the address of the location containing the stack
top value (which is in the G struct).  For cgo functions which
return values, read the stktop before and after the cgo call to
compute the adjustment necessary to write the return value.

Fixes golang#8771

LGTM=iant, rsc
R=iant, rsc, khr
CC=golang-codereviews
https://golang.org/cl/144130043
This issue was closed.
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

4 participants