cgo: adjust return value location to account for stack copies.
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 issue 8771
Re SWIG, what if instead of the changes here to pass a second argument we ...
10 years, 7 months ago
(2014-09-19 00:26:28 UTC)
#5
Re SWIG, what if instead of the changes here to pass a second argument we
provided in the Go library a function like char *_go_topofstack(void),
callable from gcc. Then the two instances of *stktopptr would instead be
_go_topofstack() in the cgo code, and SWIG could call it too. There must be
some magic linker trick with weak symbols that SWIG could use to supply a
dummy version (return 0 would be fine) for older Go versions. Or we could
provide an initialized char* (*_go_topofstack)(void), and SWIG could
declare a common symbol for that too, and it would just be zero if you had
an older Go.
Russ
On Thu, Sep 18, 2014 at 5:26 PM, Russ Cox <rsc@golang.org> wrote: > > Re ...
10 years, 7 months ago
(2014-09-19 00:34:24 UTC)
#6
On Thu, Sep 18, 2014 at 5:26 PM, Russ Cox <rsc@golang.org> wrote:
>
> Re SWIG, what if instead of the changes here to pass a second argument we
> provided in the Go library a function like char *_go_topofstack(void),
> callable from gcc. Then the two instances of *stktopptr would instead be
> _go_topofstack() in the cgo code, and SWIG could call it too. There must be
> some magic linker trick with weak symbols that SWIG could use to supply a
> dummy version (return 0 would be fine) for older Go versions. Or we could
> provide an initialized char* (*_go_topofstack)(void), and SWIG could declare
> a common symbol for that too, and it would just be zero if you had an older
> Go.
SGTM
Just the function should be sufficient. I was actually thinking of
calling a function but was getting hung up on the fact that old
versions wouldn't have that function. I can't believe I didn't think
of using a stupid linker trick.
Ian
This __go_topofstack function has to be written in gcc code, but it needs access to ...
10 years, 7 months ago
(2014-09-19 02:33:34 UTC)
#7
This __go_topofstack function has to be written in gcc code, but it needs
access to the G register so it can get the top of stack. Is there any way
to do that inside gcc, or do we need to use assembly? Would inline asm
work or might the G register be clobbered?
On Thu, Sep 18, 2014 at 5:34 PM, Ian Lance Taylor <iant@golang.org> wrote:
> On Thu, Sep 18, 2014 at 5:26 PM, Russ Cox <rsc@golang.org> wrote:
> >
> > Re SWIG, what if instead of the changes here to pass a second argument we
> > provided in the Go library a function like char *_go_topofstack(void),
> > callable from gcc. Then the two instances of *stktopptr would instead be
> > _go_topofstack() in the cgo code, and SWIG could call it too. There must
> be
> > some magic linker trick with weak symbols that SWIG could use to supply a
> > dummy version (return 0 would be fine) for older Go versions. Or we could
> > provide an initialized char* (*_go_topofstack)(void), and SWIG could
> declare
> > a common symbol for that too, and it would just be zero if you had an
> older
> > Go.
>
> SGTM
>
> Just the function should be sufficient. I was actually thinking of
> calling a function but was getting hung up on the fact that old
> versions wouldn't have that function. I can't believe I didn't think
> of using a stupid linker trick.
>
> Ian
>
On Thu, Sep 18, 2014 at 10:33 PM, Keith Randall <khr@google.com> wrote: > This __go_topofstack ...
10 years, 7 months ago
(2014-09-19 02:38:11 UTC)
#8
On Thu, Sep 18, 2014 at 10:33 PM, Keith Randall <khr@google.com> wrote:
> This __go_topofstack function has to be written in gcc code, but it needs
> access to the G register so it can get the top of stack. Is there any way
> to do that inside gcc, or do we need to use assembly? Would inline asm
> work or might the G register be clobbered?
>
It can be written in Go assembly as long as it is NOSPLIT to turn off the
prologue and leaves the result in the right place and doesn't have a
center-dot in its name. It pretty much has to be Go assembly, or else it
won't find g the right way.
Russ
On Thu, Sep 18, 2014 at 7:38 PM, Russ Cox <rsc@golang.org> wrote: > On Thu, ...
10 years, 7 months ago
(2014-09-19 14:26:23 UTC)
#9
On Thu, Sep 18, 2014 at 7:38 PM, Russ Cox <rsc@golang.org> wrote:
> On Thu, Sep 18, 2014 at 10:33 PM, Keith Randall <khr@google.com> wrote:
>>
>> This __go_topofstack function has to be written in gcc code, but it needs
>> access to the G register so it can get the top of stack. Is there any way
>> to do that inside gcc, or do we need to use assembly? Would inline asm work
>> or might the G register be clobbered?
>
>
> It can be written in Go assembly as long as it is NOSPLIT to turn off the
> prologue and leaves the result in the right place and doesn't have a
> center-dot in its name. It pretty much has to be Go assembly, or else it
> won't find g the right way.
I think it can go in runtime/cgo/asm_*.s, along with crosscall2.
Ian
Yes, I'm just fighting with the linker to make my asm stub link correctly. On ...
10 years, 7 months ago
(2014-09-24 20:26:37 UTC)
#11
Yes, I'm just fighting with the linker to make my asm stub link correctly.
On Wed, Sep 24, 2014 at 11:20 AM, Russ Cox <rsc@golang.org> wrote:
> Ping. Keith, are you planning to revise this CL?
>
>
On 2014/09/24 20:26:37, khr1 wrote: > Yes, I'm just fighting with the linker to make ...
10 years, 7 months ago
(2014-09-24 23:07:40 UTC)
#12
On 2014/09/24 20:26:37, khr1 wrote:
> Yes, I'm just fighting with the linker to make my asm stub link correctly.
>
> On Wed, Sep 24, 2014 at 11:20 AM, Russ Cox <mailto:rsc@golang.org> wrote:
>
> > Ping. Keith, are you planning to revise this CL?
> >
> >
PTAL.
Is cgo_topofstack a good name? Does it need some extra underscores?
*** Submitted as https://code.google.com/p/go/source/detail?r=b4a82354d400 *** cgo: adjust return value location to account for stack copies. ...
10 years, 7 months ago
(2014-09-25 14:59:08 UTC)
#15
*** Submitted as https://code.google.com/p/go/source/detail?r=b4a82354d400 ***
cgo: adjust return value location to account for stack copies.
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 issue 8771
LGTM=iant, rsc
R=iant, rsc, khr
CC=golang-codereviews
https://codereview.appspot.com/144130043
Issue 144130043: code review 144130043: cgo: adjust return value location to account for stack ...
(Closed)
Created 10 years, 7 months ago by khr
Modified 10 years, 6 months ago
Reviewers:
Base URL:
Comments: 11