1
0
mirror of https://github.com/golang/go synced 2024-11-27 00:11:19 -07:00
go/misc/cgo/testsanitizers/testdata
Michael Pratt 7b874619be runtime/cgo: store M for C-created thread in pthread key
This reapplies CL 481061, with the followup fixes in CL 482975, CL 485315, and
CL 485316 incorporated.

CL 481061, by doujiang24 <doujiang24@gmail.com>, speed up C to Go
calls by binding the M to the C thread. See below for its
description.
CL 482975 is a followup fix to a C declaration in testprogcgo.
CL 485315 is a followup fix for x_cgo_getstackbound on Illumos.
CL 485316 is a followup cleanup for ppc64 assembly.

[Original CL 481061 description]

This reapplies CL 392854, with the followup fixes in CL 479255,
CL 479915, and CL 481057 incorporated.

CL 392854, by doujiang24 <doujiang24@gmail.com>, speed up C to Go
calls by binding the M to the C thread. See below for its
description.
CL 479255 is a followup fix for a small bug in ARM assembly code.
CL 479915 is another followup fix to address C to Go calls after
the C code uses some stack, but that CL is also buggy.
CL 481057, by Michael Knyszek, is a followup fix for a memory leak
bug of CL 479915.

[Original CL 392854 description]

In a C thread, it's necessary to acquire an extra M by using needm while invoking a Go function from C. But, needm and dropm are heavy costs due to the signal-related syscalls.
So, we change to not dropm while returning back to C, which means binding the extra M to the C thread until it exits, to avoid needm and dropm on each C to Go call.
Instead, we only dropm while the C thread exits, so the extra M won't leak.

When invoking a Go function from C:
Allocate a pthread variable using pthread_key_create, only once per shared object, and register a thread-exit-time destructor.
And store the g0 of the current m into the thread-specified value of the pthread key,  only once per C thread, so that the destructor will put the extra M back onto the extra M list while the C thread exits.

When returning back to C:
Skip dropm in cgocallback, when the pthread variable has been created, so that the extra M will be reused the next time invoke a Go function from C.

This is purely a performance optimization. The old version, in which needm & dropm happen on each cgo call, is still correct too, and we have to keep the old version on systems with cgo but without pthreads, like Windows.

This optimization is significant, and the specific value depends on the OS system and CPU, but in general, it can be considered as 10x faster, for a simple Go function call from a C thread.

For the newly added BenchmarkCGoInCThread, some benchmark results:
1. it's 28x faster, from 3395 ns/op to 121 ns/op, in darwin OS & Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
2. it's 6.5x faster, from 1495 ns/op to 230 ns/op, in Linux OS & Intel(R) Xeon(R) CPU E5-2630 0 @ 2.30GHz

[CL 479915 description]

Currently, when C calls into Go the first time, we grab an M
using needm, which sets m.g0's stack bounds using the SP. We don't
know how big the stack is, so we simply assume 32K. Previously,
when the Go function returns to C, we drop the M, and the next
time C calls into Go, we put a new stack bound on the g0 based on
the current SP. After CL 392854, we don't drop the M, and the next
time C calls into Go, we reuse the same g0, without recomputing
the stack bounds. If the C code uses quite a bit of stack space
before calling into Go, the SP may be well below the 32K stack
bound we assumed, so the runtime thinks the g0 stack overflows.

This CL makes needm get a more accurate stack bound from
pthread. (In some platforms this may still be a guess as we don't
know exactly where we are in the C stack), but it is probably
better than simply assuming 32K.

[CL 485500 description]

CL 479915 passed the G to _cgo_getstackbound for direct updates to
gp.stack.lo. A G can be reused on a new thread after the previous thread
exited. This could trigger the C TSAN race detector because it couldn't
see the synchronization in Go (lockextra) preventing the same G from
being used on multiple threads at the same time.

We work around this by passing the address of a stack variable to
_cgo_getstackbound rather than the G. The stack is generally unique per
thread, so TSAN won't see the same address from multiple threads. Even
if stacks are reused across threads by pthread, C TSAN should see the
synchonization in the stack allocator.

A regression test is added to misc/cgo/testsanitizer.

Fixes #51676.
Fixes #59294.
Fixes #59678.

Change-Id: Ic62be31a06ee83568215e875a891df37084e08ca
Reviewed-on: https://go-review.googlesource.com/c/go/+/485500
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Cherry Mui <cherryyz@google.com>
Run-TryBot: Michael Pratt <mpratt@google.com>
2023-04-26 19:25:46 +00:00
..
asan_linkerx cmd/link: don't reset variable size when handling -X flag 2022-10-13 18:47:30 +00:00
arena_fail.go misc/cgo/test: add asan and msan arena tests 2022-10-12 20:44:43 +00:00
asan1_fail.go runtime: fix the issue that the -asan option cannot print where the error occurred 2021-12-29 04:10:07 +00:00
asan2_fail.go runtime: fix the issue that the -asan option cannot print where the error occurred 2021-12-29 04:10:07 +00:00
asan3_fail.go cmd/dist: add asan tests in misc/cgo/testsanitizers package 2021-11-02 06:21:44 +00:00
asan4_fail.go cmd/dist: add asan tests in misc/cgo/testsanitizers package 2021-11-02 06:21:44 +00:00
asan5_fail.go src/runtime: mark asanread and asanwrite functions as NOSPLIT 2022-01-05 09:50:29 +00:00
asan_global1_fail.go cmd/dist: add asan tests for global objects in testsanitizers package 2022-05-05 04:05:43 +00:00
asan_global2_fail.go cmd/dist: add asan tests for global objects in testsanitizers package 2022-05-05 04:05:43 +00:00
asan_global3_fail.go cmd/dist: add asan tests for global objects in testsanitizers package 2022-05-05 04:05:43 +00:00
asan_global4_fail.go cmd/dist: add asan tests for global objects in testsanitizers package 2022-05-05 04:05:43 +00:00
asan_global5.go cmd/dist: add asan tests for global objects in testsanitizers package 2022-05-05 04:05:43 +00:00
asan_unsafe_fail1.go cmd/compile: set conversions to unsafe.Pointer as an escaping operation when -asan is enabled 2022-03-17 08:43:51 +00:00
asan_unsafe_fail2.go cmd/compile: set conversions to unsafe.Pointer as an escaping operation when -asan is enabled 2022-03-17 08:43:51 +00:00
asan_unsafe_fail3.go cmd/compile: set conversions to unsafe.Pointer as an escaping operation when -asan is enabled 2022-03-17 08:43:51 +00:00
asan_useAfterReturn.go cmd/dist: add asan tests in misc/cgo/testsanitizers package 2021-11-02 06:21:44 +00:00
libfuzzer1.go misc/cgo/testsanitizers: add libfuzzer tests 2022-12-27 21:10:04 +00:00
libfuzzer2.c misc/cgo/testsanitizers: add libfuzzer tests 2022-12-27 21:10:04 +00:00
libfuzzer2.go misc/cgo/testsanitizers: add libfuzzer tests 2022-12-27 21:10:04 +00:00
msan2_cmsan.go
msan2.go
msan3.go
msan4.go
msan5.go
msan6.go misc/cgo: gofmt 2023-04-10 22:32:33 +00:00
msan7.go
msan8.go runtime/cgo: when using msan explicitly unpoison cgoCallers 2021-08-09 14:48:39 +00:00
msan_fail.go
msan_shared.go
msan.go misc/cgo/testsanitizers: use fmt.Printf instead fmt.Println 2023-01-20 01:23:37 +00:00
tsan2.go
tsan3.go
tsan4.go
tsan5.go
tsan6.go
tsan7.go
tsan8.go
tsan9.go all: update references to symbols moved from io/ioutil to io 2021-04-05 17:51:15 +00:00
tsan10.go
tsan11.go misc/cgo/testsanitizers: buffer the signal channel in TestTSAN/tsan11 2022-05-27 15:58:52 +00:00
tsan12.go misc/cgo/testsanitizers: use buffered channel in tsan12.go 2022-05-24 14:30:46 +00:00
tsan13.go runtime/cgo: add tsan sync for traceback function 2023-03-08 20:11:59 +00:00
tsan14.go runtime/cgo: store M for C-created thread in pthread key 2023-04-26 19:25:46 +00:00
tsan_shared.go
tsan.go