Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

runtime/cgo: -extldflags=-stack_size=NNN does not set stack size used by runtime/cgo #28024

Open
p8a opened this issue Oct 4, 2018 · 5 comments

Comments

Projects
None yet
2 participants
@p8a
Copy link

commented Oct 4, 2018

Please answer these questions before submitting your issue. Thanks!

What version of Go are you using (go version)?

go version go1.11 darwin/amd64
macOS Sierra (10.12.6)

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using (go env)?

GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/p8a/Library/Caches/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/p8a/data/go"
GOPROXY=""
GORACE=""
GOROOT="/usr/local/Cellar/go/1.11/libexec"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.11/libexec/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/5l/3qmpjt5s6_z0vhkj27l7v0p40000gr/T/go-build878210713=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

Integrating via cgo a 3rd-party library which requires 2mb stack space does not work because stack size is set to default PTHREAD_STACK_MIN (which is less).

The runtime behavior manifests as a deadlocked "panic" call with the following stack:

2359 Thread_1644947
    + 2359 _sigtramp  (in libsystem_platform.dylib) + 26  [0x7fff9656cb3a]
    +   2359 runtime.sigtramp  (in test) + 51  [0x4065fe3]
    +     2359 runtime.sigtrampgo  (in test) + 544  [0x4049000]
    +       2359 runtime.sighandler  (in test) + 1788  [0x404866c]
    +         2359 runtime.(*sigctxt).preparePanic  (in test) + 172  [0x40475fc]

What did you expect to see?

Panic being triggered and a way to set cgo stack size.

What did you see instead?

@p8a

This comment has been minimized.

Copy link
Author

commented Oct 4, 2018

I've currently modified gcc_darwin_amd64.c to allow setting stack size at compilation time:

40a41,45
> #ifdef CGO_STACK_SIZE
>     size = CGO_STACK_SIZE;
>     pthread_attr_setstacksize(&attr, size);
> #endif
>
$ CGO_CFLAGS="-DCGO_STACK_SIZE=0x200000" make
@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

commented Oct 4, 2018

Does it work if you use go build -extldflags=-stack_size=0x200000?

@p8a

This comment has been minimized.

Copy link
Author

commented Oct 4, 2018

No, I tried that and it still panics:

otool -lV test | grep stack
 stacksize 2097152

The program passes a callback function to the library, the crash is when returning from the go callback:

go -> call c_func (go_cbk)
c   -> call go_cbk
go_cbk returns -> panic
@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

commented Oct 4, 2018

I don't understand why that would be. Do you have a complete test case you can show us that recreates the problem?

@p8a

This comment has been minimized.

Copy link
Author

commented Oct 4, 2018

Not my exact problem (still trying to isolate it into a sample, cannot make "preparePanic" lock) but one showing setting the stack size does not help (note stack size returned by pthread):

repro.go.txt
repro.h.txt

First attempt: calling Go callback causes a crash

> go build -a -ldflags='-extldflags "-Wl,-stack_size -Wl,0x200000"' repro.go
> otool -lV repro | grep stacksize
 stacksize 2097152
> ./repro                                   
stack size: 524288
no_optimize: 4294705152
fatal: morestack on g0
SIGTRAP: trace trap
PC=0x4052032 m=0 sigcode=1
signal arrived during cgo execution

goroutine 4 [running, locked to thread]:
runtime.abort()
...

Second attempt with no callback showing a clean execution (comment out in repro.h last printf calling goCallback):

> ./repro
stack size: 524288
no_optimize: 4294705152
exit

@ianlancetaylor ianlancetaylor modified the milestones: Unplanned, Go1.12 Oct 5, 2018

@ianlancetaylor ianlancetaylor changed the title cgo on darwin - stack size runtime/cgo: -extldflags=-stack_size=NNN does not set stack size used by runtime/cgo Nov 28, 2018

@ianlancetaylor ianlancetaylor modified the milestones: Go1.12, Unplanned Nov 28, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.