Commit c00ff65d authored by Russ Cox's avatar Russ Cox

runtime: use default system stack size, not 64 kB, on non-cgo macOS

At least one libc call we make
(res_search, which calls _mdns_query and then mdns_item_call)
pushes a 64 kB stack frame onto the stack.
Then it faults on the guard page.

Use the default system stack size, under the assumption
that the C code being called is compatible with that stack size.

For #31705.

Change-Id: I1b0bfc2e54043c49f0709255988ef920ce30ee82
Reviewed-on: https://go-review.googlesource.com/c/go/+/180779
Run-TryBot: Russ Cox <rsc@golang.org>
Reviewed-by: default avatarIan Lance Taylor <iant@golang.org>
parent e9073ef8
...@@ -145,14 +145,14 @@ func newosproc(mp *m) { ...@@ -145,14 +145,14 @@ func newosproc(mp *m) {
exit(1) exit(1)
} }
// Set the stack size we want to use. 64KB for now. // Find out OS stack size for our own stack guard.
// TODO: just use OS default size? var stacksize uintptr
const stackSize = 1 << 16 if pthread_attr_getstacksize(&attr, &stacksize) != 0 {
if pthread_attr_setstacksize(&attr, stackSize) != 0 {
write(2, unsafe.Pointer(&failthreadcreate[0]), int32(len(failthreadcreate))) write(2, unsafe.Pointer(&failthreadcreate[0]), int32(len(failthreadcreate)))
exit(1) exit(1)
} }
//mSysStatInc(&memstats.stacks_sys, stackSize) //TODO: do this? mp.g0.stack.hi = stacksize // for mstart
//mSysStatInc(&memstats.stacks_sys, stacksize) //TODO: do this?
// Tell the pthread library we won't join with this thread. // Tell the pthread library we won't join with this thread.
if pthread_attr_setdetachstate(&attr, _PTHREAD_CREATE_DETACHED) != 0 { if pthread_attr_setdetachstate(&attr, _PTHREAD_CREATE_DETACHED) != 0 {
...@@ -191,11 +191,16 @@ func newosproc0(stacksize uintptr, fn uintptr) { ...@@ -191,11 +191,16 @@ func newosproc0(stacksize uintptr, fn uintptr) {
exit(1) exit(1)
} }
// Set the stack we want to use. // The caller passes in a suggested stack size,
if pthread_attr_setstacksize(&attr, stacksize) != 0 { // from when we allocated the stack and thread ourselves,
// without libpthread. Now that we're using libpthread,
// we use the OS default stack size instead of the suggestion.
// Find out that stack size for our own stack guard.
if pthread_attr_getstacksize(&attr, &stacksize) != 0 {
write(2, unsafe.Pointer(&failthreadcreate[0]), int32(len(failthreadcreate))) write(2, unsafe.Pointer(&failthreadcreate[0]), int32(len(failthreadcreate)))
exit(1) exit(1)
} }
g0.stack.hi = stacksize // for mstart
mSysStatInc(&memstats.stacks_sys, stacksize) mSysStatInc(&memstats.stacks_sys, stacksize)
// Tell the pthread library we won't join with this thread. // Tell the pthread library we won't join with this thread.
......
...@@ -128,10 +128,10 @@ func pthread_attr_init_trampoline() ...@@ -128,10 +128,10 @@ func pthread_attr_init_trampoline()
//go:nosplit //go:nosplit
//go:cgo_unsafe_args //go:cgo_unsafe_args
func pthread_attr_setstacksize(attr *pthreadattr, size uintptr) int32 { func pthread_attr_getstacksize(attr *pthreadattr, size *uintptr) int32 {
return libcCall(unsafe.Pointer(funcPC(pthread_attr_setstacksize_trampoline)), unsafe.Pointer(&attr)) return libcCall(unsafe.Pointer(funcPC(pthread_attr_getstacksize_trampoline)), unsafe.Pointer(&attr))
} }
func pthread_attr_setstacksize_trampoline() func pthread_attr_getstacksize_trampoline()
//go:nosplit //go:nosplit
//go:cgo_unsafe_args //go:cgo_unsafe_args
...@@ -396,7 +396,7 @@ func closeonexec(fd int32) { ...@@ -396,7 +396,7 @@ func closeonexec(fd int32) {
// in a system library, with the libc_ prefix missing. // in a system library, with the libc_ prefix missing.
//go:cgo_import_dynamic libc_pthread_attr_init pthread_attr_init "/usr/lib/libSystem.B.dylib" //go:cgo_import_dynamic libc_pthread_attr_init pthread_attr_init "/usr/lib/libSystem.B.dylib"
//go:cgo_import_dynamic libc_pthread_attr_setstacksize pthread_attr_setstacksize "/usr/lib/libSystem.B.dylib" //go:cgo_import_dynamic libc_pthread_attr_getstacksize pthread_attr_getstacksize "/usr/lib/libSystem.B.dylib"
//go:cgo_import_dynamic libc_pthread_attr_setdetachstate pthread_attr_setdetachstate "/usr/lib/libSystem.B.dylib" //go:cgo_import_dynamic libc_pthread_attr_setdetachstate pthread_attr_setdetachstate "/usr/lib/libSystem.B.dylib"
//go:cgo_import_dynamic libc_pthread_create pthread_create "/usr/lib/libSystem.B.dylib" //go:cgo_import_dynamic libc_pthread_create pthread_create "/usr/lib/libSystem.B.dylib"
//go:cgo_import_dynamic libc_exit exit "/usr/lib/libSystem.B.dylib" //go:cgo_import_dynamic libc_exit exit "/usr/lib/libSystem.B.dylib"
......
...@@ -474,7 +474,7 @@ TEXT runtime·pthread_attr_init_trampoline(SB),NOSPLIT,$0 ...@@ -474,7 +474,7 @@ TEXT runtime·pthread_attr_init_trampoline(SB),NOSPLIT,$0
POPL BP POPL BP
RET RET
TEXT runtime·pthread_attr_setstacksize_trampoline(SB),NOSPLIT,$0 TEXT runtime·pthread_attr_getstacksize_trampoline(SB),NOSPLIT,$0
PUSHL BP PUSHL BP
MOVL SP, BP MOVL SP, BP
SUBL $8, SP SUBL $8, SP
...@@ -483,7 +483,7 @@ TEXT runtime·pthread_attr_setstacksize_trampoline(SB),NOSPLIT,$0 ...@@ -483,7 +483,7 @@ TEXT runtime·pthread_attr_setstacksize_trampoline(SB),NOSPLIT,$0
MOVL AX, 0(SP) MOVL AX, 0(SP)
MOVL 4(CX), AX // arg 2 size MOVL 4(CX), AX // arg 2 size
MOVL AX, 4(SP) MOVL AX, 4(SP)
CALL libc_pthread_attr_setstacksize(SB) CALL libc_pthread_attr_getstacksize(SB)
MOVL BP, SP MOVL BP, SP
POPL BP POPL BP
RET RET
......
...@@ -443,12 +443,12 @@ TEXT runtime·pthread_attr_init_trampoline(SB),NOSPLIT,$0 ...@@ -443,12 +443,12 @@ TEXT runtime·pthread_attr_init_trampoline(SB),NOSPLIT,$0
POPQ BP POPQ BP
RET RET
TEXT runtime·pthread_attr_setstacksize_trampoline(SB),NOSPLIT,$0 TEXT runtime·pthread_attr_getstacksize_trampoline(SB),NOSPLIT,$0
PUSHQ BP PUSHQ BP
MOVQ SP, BP MOVQ SP, BP
MOVQ 8(DI), SI // arg 2 size MOVQ 8(DI), SI // arg 2 size
MOVQ 0(DI), DI // arg 1 attr MOVQ 0(DI), DI // arg 1 attr
CALL libc_pthread_attr_setstacksize(SB) CALL libc_pthread_attr_getstacksize(SB)
POPQ BP POPQ BP
RET RET
......
...@@ -323,7 +323,7 @@ TEXT runtime·pthread_attr_init_trampoline(SB),NOSPLIT,$0 ...@@ -323,7 +323,7 @@ TEXT runtime·pthread_attr_init_trampoline(SB),NOSPLIT,$0
BL libc_exit(SB) BL libc_exit(SB)
RET RET
TEXT runtime·pthread_attr_setstacksize_trampoline(SB),NOSPLIT,$0 TEXT runtime·pthread_attr_getstacksize_trampoline(SB),NOSPLIT,$0
MOVW $46, R0 MOVW $46, R0
BL libc_exit(SB) BL libc_exit(SB)
RET RET
......
...@@ -382,10 +382,10 @@ TEXT runtime·pthread_attr_init_trampoline(SB),NOSPLIT,$0 ...@@ -382,10 +382,10 @@ TEXT runtime·pthread_attr_init_trampoline(SB),NOSPLIT,$0
BL libc_pthread_attr_init(SB) BL libc_pthread_attr_init(SB)
RET RET
TEXT runtime·pthread_attr_setstacksize_trampoline(SB),NOSPLIT,$0 TEXT runtime·pthread_attr_getstacksize_trampoline(SB),NOSPLIT,$0
MOVD 8(R0), R1 // arg 2 size MOVD 8(R0), R1 // arg 2 size
MOVD 0(R0), R0 // arg 1 attr MOVD 0(R0), R0 // arg 1 attr
BL libc_pthread_attr_setstacksize(SB) BL libc_pthread_attr_getstacksize(SB)
RET RET
TEXT runtime·pthread_attr_setdetachstate_trampoline(SB),NOSPLIT,$0 TEXT runtime·pthread_attr_setdetachstate_trampoline(SB),NOSPLIT,$0
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment