Repository: go
Issue: cmd/cgo: C-Shared program crashes with "fatal: morestack g0"
Labels: NeedsInvestigation,compiler/runtime

<!-- Please answer these questions before submitting your issue. Thanks! -->

### What version of Go are you using (`go version`)?

<pre>
$ go version
go version go1.11.2 darwin/amd64
</pre>

### Does this issue reproduce with the latest release?

Yes

### What operating system and processor architecture are you using (`go env`)?

<details><summary><code>go env</code> Output</summary><br><pre>
$ go env
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/joshua/Library/Caches/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/joshua/go:/Users/joshua/src/repo004/GOPATH"
GOPROXY=""
GORACE=""
GOROOT="/usr/local/cross-rev005/go-1.11.2"
GOTMPDIR=""
GOTOOLDIR="/usr/local/cross-rev005/go-1.11.2/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/vx/tlkd5xy56h711dyw9xr3dzch000136/T/go-build534324521=/tmp/go-build -gno-record-gcc-switches -fno-common"
</pre></details>

### What did you do?

<!--
If possible, provide a recipe for reproducing the error.
A complete runnable program is good.
A link on play.golang.org is best.
-->

It's a convoluted situation...

I've create a `c-shared` dylib that under the hood is making use CGO to call into another dylib which does make calls back into Go. At the top level of this, I have a written a C program that calls the go library:

```
#include "stuff.h"
#include <stdio.h>

int main() {

    libbbtevi_error_t err;
    libbbtevi_add_archive_to_filesystem("<a path to a file on disk>", 3, 6, &err);
    printf("%s\n", err.msg);
 ```

`libbbtevi_add_archive_to_filesystem` is just a wrapper around a Go function `AddISYSArchiveToFilesystem`

```//export libbbtevi_add_archive_to_filesystem
func libbbtevi_add_archive_to_filesystem(
	bldb_path *C.char,
	fs_id C.int64_t,
	archive_id C.int64_t,
	e *C.libbbtevi_error_t,
) C.libbbtevi_return_t {
	// Set up error handling
	var err error
	defer func() {
		writeEVIError(e, err)
	}()

	// Validate parameters
	if bldb_path == nil {
		return C.LIBBBTEVI_RETURN_INVALID_PARAMETER
	}

	err = fs.AddISYSArchiveToFilesystem(C.GoString(bldb_path), int64(fs_id), int64(archive_id))
	if err != nil {
		return C.LIBBBTEVI_RETURN_FAILURE
	}

	return C.LIBBBTEVI_RETURN_SUCCESS
}
```

I also wrote a go program that just calls the `AddISYSArchiveToFilesystem` call directly:

```
package main

import (
	"bbgo_utils2/fs"
	"log"
)

func main() {
	path := "<a path to a file on disk>"
	err := fs.AddISYSArchiveToFilesystem(path, 3, 6)
	if err != nil {
		log.Fatal(err)
	}
	log.Println("Done")
}
```

### What did you expect to see?

Both programs run without error

### What did you see instead?

The Go program runs to completion, without issue

The C program crashes with: 

```
fatal: morestack on g0
Trace/BPT trap: 5
```

if run in `lldb` the following backtrace is available:

```
fatal: morestack on g0
Process 93693 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BREAKPOINT (code=EXC_I386_BPT, subcode=0x0)
    frame #0: 0x0000000100129bf2 libbbtgo.dylib`runtime.abort at asm_amd64.s:840
   837  TEXT runtime·abort(SB),NOSPLIT,$0-0
   838          INT     $3
   839  loop:
-> 840          JMP     loop
   841
   842  // check that SP is in range [g->stack.lo, g->stack.hi)
   843  TEXT runtime·stackcheck(SB), NOSPLIT, $0-0
Target 0: (isystester) stopped.
(lldb) bt
* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BREAKPOINT (code=EXC_I386_BPT, subcode=0x0)
  * frame #0: 0x0000000100129bf2 libbbtgo.dylib`runtime.abort at asm_amd64.s:840
    frame #1: 0x00000001001282a5 libbbtgo.dylib`runtime.morestack at asm_amd64.s:397
    frame #2: 0x0000000100127892 libbbtgo.dylib`runtime.exitsyscallfast.func1 at proc.go:3102
    frame #3: 0x0000000100128236 libbbtgo.dylib`runtime.systemstack at asm_amd64.s:351
    frame #4: 0x0000000100100a50 libbbtgo.dylib`runtime.startTheWorldWithSema + 608
    frame #5: 0x0000000100104e15 libbbtgo.dylib`runtime.exitsyscall at proc.go:3015
    frame #6: 0x00000001000d3878 libbbtgo.dylib`runtime.cgocallbackg at cgocall.go:191
    frame #7: 0x0000000100129b5b libbbtgo.dylib`runtime.cgocallback_gofunc at asm_amd64.s:775
    frame #8: 0x00000001001299f2 libbbtgo.dylib`runtime.asmcgocall at asm_amd64.s:622
    frame #9: 0x00000001000d3712 libbbtgo.dylib`runtime.cgocall at cgocall.go:131
    frame #10: 0x000000010056046b libbbtgo.dylib`isys._Cfunc_IGR_Extract_Subfile_Stream at _cgo_gotypes.go:198
    frame #11: 0x00000001005673f4 libbbtgo.dylib`isys.(*SubfileEntry).Reader.func2 at subfile.go:85
    frame #12: 0x0000000100565396 libbbtgo.dylib`isys.(*SubfileEntry).Reader at subfile.go:85
    frame #13: 0x0000000100568eb0 libbbtgo.dylib`go4n6/logical/isysarchive.(*Collection).Entry at isysarchive.go:53
    frame #14: 0x00000001005d3b91 libbbtgo.dylib`bbgo_utils2/fs.(*rdrRunBased).OpenID at reader.go:998
    frame #15: 0x00000001005c9655 libbbtgo.dylib`bbgo_utils2/fs.AddISYSArchiveToFilesystem at fs.go:456
    frame #16: 0x00000001005ede13 libbbtgo.dylib`main.libbbtevi_add_archive_to_filesystem at libbbtevi.go:785
    frame #17: 0x00000001005e6151 libbbtgo.dylib`main._cgoexpwrap_8a09112867c2_libbbtevi_add_archive_to_filesystem at _cgo_gotypes.go:691
    frame #18: 0x000000010012860b libbbtgo.dylib`runtime.call64 at asm_amd64.s:523
    frame #19: 0x00000001000d3adf libbbtgo.dylib`runtime.cgocallbackg1 at cgocall.go:316
    frame #20: 0x00000001000d3896 libbbtgo.dylib`runtime.cgocallbackg at cgocall.go:194
    frame #21: 0x0000000100129b5b libbbtgo.dylib`runtime.cgocallback_gofunc at asm_amd64.s:775
    frame #22: 0x000000010012a281 libbbtgo.dylib`runtime.goexit at asm_amd64.s:1333
```