When a function created by cgo or SWIG returns an interface type and at runtime returns a value that requires allocation when converting to interface, it will call the memory allocation routine while appearing to be in a system call. In particular there will be no memory cache allocated. This patch permits this case to work. Bootstrapped and ran Go testsuite on x86_64-unknown-linux-gnu. Committed to mainline and 4.8 branch.
Ian
diff -r bc94be12533f libgo/runtime/malloc.goc --- a/libgo/runtime/malloc.goc Wed Jul 24 10:36:05 2013 -0700 +++ b/libgo/runtime/malloc.goc Wed Jul 24 15:22:39 2013 -0700 @@ -41,11 +41,24 @@ uintptr npages; MSpan *s; void *v; + bool incallback; m = runtime_m(); g = runtime_g(); - if(g->status == Gsyscall) - dogc = 0; + + incallback = false; + if(m->mcache == nil && g->ncgo > 0) { + // For gccgo this case can occur when a cgo or SWIG function + // has an interface return type and the function + // returns a non-pointer, so memory allocation occurs + // after syscall.Cgocall but before syscall.CgocallDone. + // We treat it as a callback. + runtime_exitsyscall(); + m = runtime_m(); + incallback = true; + dogc = false; + } + if(runtime_gcwaiting && g != m->g0 && m->locks == 0 && dogc) { runtime_gosched(); m = runtime_m(); @@ -129,6 +142,10 @@ runtime_racemalloc(v, size, m->racepc); m->racepc = nil; } + + if(incallback) + runtime_entersyscall(); + return v; }