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 · 12 comments
Labels
help wanted NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. OS-Darwin
Milestone

Comments

@p8a
Copy link

p8a 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
Copy link
Author

p8a 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
Copy link
Contributor

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

@ianlancetaylor ianlancetaylor added OS-Darwin WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. labels Oct 4, 2018
@ianlancetaylor ianlancetaylor added this to the Unplanned milestone Oct 4, 2018
@p8a
Copy link
Author

p8a 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
Copy link
Contributor

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

@p8a
Copy link
Author

p8a 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 removed the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Oct 5, 2018
@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
@tankbusta
Copy link

tankbusta commented Jun 13, 2019

I'm running into this same issue with my code that calls into cgo that requires a larger stack size. @p8a gcc_darwin_amd64.c workaround was a solution to get it working.

Edit: @ianlancetaylor's suggestion of go build -extldflags=-stack_size=0x200000 did not work either

@ianlancetaylor
Copy link
Contributor

It would help if somebody could provide a small self-contained test case that demonstrates the problem.

@aykevl
Copy link

aykevl commented Jun 14, 2019

Just to note if someone else runs into it: I thought I needed this because I was getting a weird panic ("fatal: morestack on g0"). Turns out I had a stack overflow in complicated CGo code that was traversing a cyclic tree going back and forth between C and Go with a callback, but I didn't expect it to be cyclic so I ended up with a stack overflow due to endless recursion.

@p8a
Copy link
Author

p8a commented Jun 16, 2019

It would help if somebody could provide a small self-contained test case that demonstrates the problem.

@ianlancetaylor my last comment (#28024 (comment)) has a test case

@ianlancetaylor
Copy link
Contributor

@p8a My apologies. Thanks for sending that. Although I'm surprised to see __cdecl in repro.h, as this issue was reported against Darwin. What does __cdecl mean on Darwin?

@elagergren-spideroak
Copy link

elagergren-spideroak commented May 1, 2020

So, partially to bump this, because I'm debugging something similar, this can still be reproduced as of Go 1.14.2, and even without __cdecl.

@AskAlexSharov
Copy link

TG has minimal requirement of Go1.5, so better switch to 1.5.4 fir debugging.

AskAlexSharov pushed a commit to ledgerwatch/erigon that referenced this issue Oct 5, 2023
This introduces _experimental_ block execution run by embedded Silkworm
API library:

- new command-line option `silkworm.path` to enable the feature by
specifying the path to the Silkworm library
- the Silkworm API shared library is dynamically loaded on-demand
- currently requires to build Silkworm library on the target machine
- available only on Linux at the moment: macOS has issue with [stack
size](golang/go#28024) and Windows would
require [TDM-GCC-64](https://jmeubank.github.io/tdm-gcc/), both need
dedicated effort for an assessment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. OS-Darwin
Projects
None yet
Development

No branches or pull requests

6 participants