summaryrefslogtreecommitdiff
path: root/src/cmd/cgo
diff options
context:
space:
mode:
authorKeith Randall <khr@golang.org>2014-09-25 07:59:01 -0700
committerKeith Randall <khr@golang.org>2014-09-25 07:59:01 -0700
commit0c34008d4e1b07c77820b855e95971ca0e2d9a8b (patch)
tree312a3e1102bf3e1b30dadcc1c9a1676377c9189f /src/cmd/cgo
parent29ea6456c72c8b6b7f1b5902441ea8ff4c50e65a (diff)
downloadgo-0c34008d4e1b07c77820b855e95971ca0e2d9a8b.tar.gz
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
Diffstat (limited to 'src/cmd/cgo')
-rw-r--r--src/cmd/cgo/out.go16
1 files changed, 15 insertions, 1 deletions
diff --git a/src/cmd/cgo/out.go b/src/cmd/cgo/out.go
index 2d14f766f..4e5b3a245 100644
--- a/src/cmd/cgo/out.go
+++ b/src/cmd/cgo/out.go
@@ -44,6 +44,7 @@ func (p *Package) writeDefs() {
fmt.Fprintf(fm, "int main() { return 0; }\n")
if *importRuntimeCgo {
fmt.Fprintf(fm, "void crosscall2(void(*fn)(void*, int), void *a, int c) { }\n")
+ fmt.Fprintf(fm, "char* cgo_topofstack(void) { return (char*)0; }\n")
} else {
// If we're not importing runtime/cgo, we *are* runtime/cgo,
// which provides crosscall2. We just need a prototype.
@@ -519,9 +520,13 @@ func (p *Package) writeOutputFunc(fgcc *os.File, n *Name) {
// Use packed attribute to force no padding in this struct in case
// gcc has different packing requirements.
fmt.Fprintf(fgcc, "\t%s %v *a = v;\n", ctype, p.packedAttribute())
+ if n.FuncType.Result != nil {
+ // Save the stack top for use below.
+ fmt.Fprintf(fgcc, "\tchar *stktop = cgo_topofstack();\n")
+ }
fmt.Fprintf(fgcc, "\t")
if t := n.FuncType.Result; t != nil {
- fmt.Fprintf(fgcc, "a->r = ")
+ fmt.Fprintf(fgcc, "__typeof__(a->r) r = ")
if c := t.C.String(); c[len(c)-1] == '*' {
fmt.Fprint(fgcc, "(__typeof__(a->r)) ")
}
@@ -544,6 +549,13 @@ func (p *Package) writeOutputFunc(fgcc *os.File, n *Name) {
fmt.Fprintf(fgcc, "a->p%d", i)
}
fmt.Fprintf(fgcc, ");\n")
+ if n.FuncType.Result != nil {
+ // The cgo call may have caused a stack copy (via a callback).
+ // Adjust the return value pointer appropriately.
+ fmt.Fprintf(fgcc, "\ta = (void*)((char*)a + (cgo_topofstack() - stktop));\n")
+ // Save the return value.
+ fmt.Fprintf(fgcc, "\ta->r = r;\n")
+ }
if n.AddError {
fmt.Fprintf(fgcc, "\treturn errno;\n")
}
@@ -1131,6 +1143,8 @@ __cgo_size_assert(__cgo_long_long, 8)
__cgo_size_assert(float, 4)
__cgo_size_assert(double, 8)
+extern char* cgo_topofstack(void);
+
#include <errno.h>
#include <string.h>
`