From 1b6807bb069c528447270c3d6c66c5c7597f388f Mon Sep 17 00:00:00 2001 From: Keith Randall Date: Thu, 25 Sep 2014 07:59:01 -0700 Subject: 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 #8771 LGTM=iant, rsc R=iant, rsc, khr CC=golang-codereviews https://golang.org/cl/144130043 --- src/runtime/stack.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'src/runtime/stack.c') diff --git a/src/runtime/stack.c b/src/runtime/stack.c index 0d8814731c..2d23c717bd 100644 --- a/src/runtime/stack.c +++ b/src/runtime/stack.c @@ -827,7 +827,9 @@ runtime·shrinkstack(G *gp) if(used >= oldsize / 4) return; // still using at least 1/4 of the segment. - if(gp->syscallsp != 0) // TODO: can we handle this case? + // We can't copy the stack if we're in a syscall. + // The syscall might have pointers into the stack. + if(gp->syscallsp != 0) return; #ifdef GOOS_windows -- cgit v1.3-5-g9baa