2016-04-10 14:32:26 -07:00
|
|
|
// Copyright 2011 The Go Authors. All rights reserved.
|
2011-05-02 13:55:51 -04:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
2023-05-05 22:29:11 -04:00
|
|
|
//go:build cgo
|
|
|
|
|
|
runtime: scheduler, cgo reorganization
* Change use of m->g0 stack (aka scheduler stack).
* Provide runtime.mcall(f) to invoke f() on m->g0 stack.
* Replace scheduler loop entry with runtime.mcall(schedule).
Runtime.mcall eliminates the need for fake scheduler states that
exist just to run a bit of code on the m->g0 stack
(Grecovery, Gstackalloc).
The elimination of the scheduler as a loop that stops and
starts using gosave and gogo fixes a bad interaction with the
way cgo uses the m->g0 stack. Cgo runs external (gcc-compiled)
C functions on that stack, and then when calling back into Go,
it sets m->g0->sched.sp below the added call frames, so that
other uses of m->g0's stack will not interfere with those frames.
Unfortunately, gogo (longjmp) back to the scheduler loop at
this point would end up running scheduler with the lower
sp, which no longer points at a valid stack frame for
a call to scheduler. If scheduler then wrote any function call
arguments or local variables to where it expected the stack
frame to be, it would overwrite other data on the stack.
I realized this possibility while debugging a problem with
calling complex Go code in a Go -> C -> Go cgo callback.
This wasn't the bug I was looking for, it turns out, but I believe
it is a real bug nonetheless. Switching to runtime.mcall, which
only adds new frames to the stack and never jumps into
functions running in existing ones, fixes this bug.
* Move cgo-related code out of proc.c into cgocall.c.
* Add very large comment describing cgo call sequences.
* Simpilify, regularize cgo function implementations and names.
* Add test suite as misc/cgo/test.
Now the Go -> C path calls cgocall, which calls asmcgocall,
and the C -> Go path calls cgocallback, which calls cgocallbackg.
The shuffling, which affects mainly the callback case, moves
most of the callback implementation to cgocallback running
on the m->curg stack (not the m->g0 scheduler stack) and
only while accounted for with $GOMAXPROCS (between calls
to exitsyscall and entersyscall).
The previous callback code did not block in startcgocallback's
approximation to exitsyscall, so if, say, the garbage collector
were running, it would still barge in and start doing things
like call malloc. Similarly endcgocallback's approximation of
entersyscall did not call matchmg to kick off new OS threads
when necessary, which caused the bug in issue 1560.
Fixes #1560.
R=iant
CC=golang-dev
https://golang.org/cl/4253054
2011-03-07 10:37:42 -05:00
|
|
|
package cgotest
|
|
|
|
|
|
2011-05-02 13:55:51 -04:00
|
|
|
import "testing"
|
|
|
|
|
|
2012-10-30 13:38:01 -07:00
|
|
|
// The actual test functions are in non-_test.go files
|
2011-05-02 13:55:51 -04:00
|
|
|
// so that they can use cgo (import "C").
|
|
|
|
|
// These wrappers are here for gotest to find.
|
|
|
|
|
|
2014-09-25 07:59:01 -07:00
|
|
|
func Test1328(t *testing.T) { test1328(t) }
|
2022-04-13 14:35:15 -07:00
|
|
|
func Test1560(t *testing.T) { test1560(t) }
|
2014-09-25 07:59:01 -07:00
|
|
|
func Test1635(t *testing.T) { test1635(t) }
|
2019-02-22 10:17:22 -05:00
|
|
|
func Test3250(t *testing.T) { test3250(t) }
|
2014-09-25 07:59:01 -07:00
|
|
|
func Test3729(t *testing.T) { test3729(t) }
|
|
|
|
|
func Test3775(t *testing.T) { test3775(t) }
|
2019-02-22 10:17:22 -05:00
|
|
|
func Test4029(t *testing.T) { test4029(t) }
|
|
|
|
|
func Test4339(t *testing.T) { test4339(t) }
|
2014-09-25 07:59:01 -07:00
|
|
|
func Test5227(t *testing.T) { test5227(t) }
|
2019-02-22 10:17:22 -05:00
|
|
|
func Test5242(t *testing.T) { test5242(t) }
|
2014-09-25 07:59:01 -07:00
|
|
|
func Test5337(t *testing.T) { test5337(t) }
|
|
|
|
|
func Test5548(t *testing.T) { test5548(t) }
|
|
|
|
|
func Test5603(t *testing.T) { test5603(t) }
|
|
|
|
|
func Test5986(t *testing.T) { test5986(t) }
|
2019-02-22 10:17:22 -05:00
|
|
|
func Test6390(t *testing.T) { test6390(t) }
|
|
|
|
|
func Test6833(t *testing.T) { test6833(t) }
|
|
|
|
|
func Test6907(t *testing.T) { test6907(t) }
|
|
|
|
|
func Test6907Go(t *testing.T) { test6907Go(t) }
|
2014-09-25 07:59:01 -07:00
|
|
|
func Test7560(t *testing.T) { test7560(t) }
|
2019-02-22 10:17:22 -05:00
|
|
|
func Test7665(t *testing.T) { test7665(t) }
|
2014-09-25 07:59:01 -07:00
|
|
|
func Test7978(t *testing.T) { test7978(t) }
|
2019-02-22 10:17:22 -05:00
|
|
|
func Test8092(t *testing.T) { test8092(t) }
|
2014-10-09 16:10:51 +11:00
|
|
|
func Test8517(t *testing.T) { test8517(t) }
|
2019-02-22 10:17:22 -05:00
|
|
|
func Test8694(t *testing.T) { test8694(t) }
|
2022-10-21 15:18:14 -04:00
|
|
|
func Test8756(t *testing.T) { test8756(t) }
|
cmd/ld: do not assume that only pe section names start with '.'
Our current pe object reader assumes that every symbol starting with
'.' is section. It appeared to be true, until now gcc 4.9.1 generates
some symbols with '.' at the front. Change that logic to check other
symbol fields in addition to checking for '.'. I am not an expert
here, but it seems reasonable to me.
Added test, but it is only good, if tested with gcc 4.9.1. Otherwise
the test PASSes regardless.
Fixes #8811.
Fixes #8856.
LGTM=jfrederich, iant, stephen.gutekanst
R=golang-codereviews, jfrederich, stephen.gutekanst, iant
CC=alex.brainman, golang-codereviews
https://golang.org/cl/152410043
2014-10-11 22:01:04 +11:00
|
|
|
func Test8811(t *testing.T) { test8811(t) }
|
2022-10-21 15:18:14 -04:00
|
|
|
func Test9026(t *testing.T) { test9026(t) }
|
|
|
|
|
func Test9510(t *testing.T) { test9510(t) }
|
2015-01-12 00:12:59 -05:00
|
|
|
func Test9557(t *testing.T) { test9557(t) }
|
2015-06-07 22:14:04 -04:00
|
|
|
func Test10303(t *testing.T) { test10303(t, 10) }
|
2015-07-29 22:04:09 -07:00
|
|
|
func Test11925(t *testing.T) { test11925(t) }
|
2015-09-10 02:32:12 -04:00
|
|
|
func Test12030(t *testing.T) { test12030(t) }
|
2016-03-16 13:53:53 -04:00
|
|
|
func Test14838(t *testing.T) { test14838(t) }
|
2016-09-11 12:34:58 -07:00
|
|
|
func Test17065(t *testing.T) { test17065(t) }
|
2016-11-10 14:34:32 -08:00
|
|
|
func Test17537(t *testing.T) { test17537(t) }
|
2016-11-30 15:46:37 -08:00
|
|
|
func Test18126(t *testing.T) { test18126(t) }
|
2017-04-21 19:18:36 +09:00
|
|
|
func Test18720(t *testing.T) { test18720(t) }
|
2017-06-04 12:11:19 +09:00
|
|
|
func Test20129(t *testing.T) { test20129(t) }
|
2022-10-21 15:18:14 -04:00
|
|
|
func Test20266(t *testing.T) { test20266(t) }
|
2019-02-22 10:17:22 -05:00
|
|
|
func Test20369(t *testing.T) { test20369(t) }
|
2017-07-17 11:14:23 -05:00
|
|
|
func Test20910(t *testing.T) { test20910(t) }
|
2017-08-31 13:49:43 +09:00
|
|
|
func Test21708(t *testing.T) { test21708(t) }
|
2017-09-09 21:38:51 -07:00
|
|
|
func Test21809(t *testing.T) { test21809(t) }
|
2017-09-26 15:14:50 -07:00
|
|
|
func Test21897(t *testing.T) { test21897(t) }
|
2017-12-04 21:29:38 -08:00
|
|
|
func Test22906(t *testing.T) { test22906(t) }
|
2019-02-22 10:17:22 -05:00
|
|
|
func Test23356(t *testing.T) { test23356(t) }
|
2018-03-01 16:52:27 -06:00
|
|
|
func Test24206(t *testing.T) { test24206(t) }
|
2018-04-30 17:36:23 +09:00
|
|
|
func Test25143(t *testing.T) { test25143(t) }
|
2018-06-26 14:40:51 -07:00
|
|
|
func Test26066(t *testing.T) { test26066(t) }
|
2022-10-21 15:18:14 -04:00
|
|
|
func Test26213(t *testing.T) { test26213(t) }
|
2018-11-09 00:55:13 -05:00
|
|
|
func Test27660(t *testing.T) { test27660(t) }
|
2018-11-20 15:55:02 -08:00
|
|
|
func Test28896(t *testing.T) { test28896(t) }
|
2019-02-02 16:03:28 -08:00
|
|
|
func Test30065(t *testing.T) { test30065(t) }
|
2019-06-23 05:30:24 +09:00
|
|
|
func Test32579(t *testing.T) { test32579(t) }
|
2019-06-12 04:57:02 +08:00
|
|
|
func Test31891(t *testing.T) { test31891(t) }
|
2021-09-15 14:36:54 +01:00
|
|
|
func Test42018(t *testing.T) { test42018(t) }
|
2021-04-09 11:20:35 -07:00
|
|
|
func Test45451(t *testing.T) { test45451(t) }
|
2021-11-17 12:54:22 -05:00
|
|
|
func Test49633(t *testing.T) { test49633(t) }
|
2024-08-27 10:19:17 -07:00
|
|
|
func Test69086(t *testing.T) { test69086(t) }
|
2019-02-22 10:17:22 -05:00
|
|
|
func TestAlign(t *testing.T) { testAlign(t) }
|
|
|
|
|
func TestAtol(t *testing.T) { testAtol(t) }
|
|
|
|
|
func TestBlocking(t *testing.T) { testBlocking(t) }
|
|
|
|
|
func TestBoolAlign(t *testing.T) { testBoolAlign(t) }
|
|
|
|
|
func TestCallGoWithString(t *testing.T) { testCallGoWithString(t) }
|
|
|
|
|
func TestCallback(t *testing.T) { testCallback(t) }
|
|
|
|
|
func TestCallbackCallers(t *testing.T) { testCallbackCallers(t) }
|
|
|
|
|
func TestCallbackGC(t *testing.T) { testCallbackGC(t) }
|
|
|
|
|
func TestCallbackPanic(t *testing.T) { testCallbackPanic(t) }
|
|
|
|
|
func TestCallbackPanicLocked(t *testing.T) { testCallbackPanicLocked(t) }
|
|
|
|
|
func TestCallbackPanicLoop(t *testing.T) { testCallbackPanicLoop(t) }
|
|
|
|
|
func TestCallbackStack(t *testing.T) { testCallbackStack(t) }
|
|
|
|
|
func TestCflags(t *testing.T) { testCflags(t) }
|
|
|
|
|
func TestCheckConst(t *testing.T) { testCheckConst(t) }
|
|
|
|
|
func TestConst(t *testing.T) { testConst(t) }
|
|
|
|
|
func TestCthread(t *testing.T) { testCthread(t) }
|
|
|
|
|
func TestEnum(t *testing.T) { testEnum(t) }
|
2020-10-27 17:03:48 +00:00
|
|
|
func TestNamedEnum(t *testing.T) { testNamedEnum(t) }
|
|
|
|
|
func TestCastToEnum(t *testing.T) { testCastToEnum(t) }
|
2019-02-22 10:17:22 -05:00
|
|
|
func TestErrno(t *testing.T) { testErrno(t) }
|
|
|
|
|
func TestFpVar(t *testing.T) { testFpVar(t) }
|
2022-10-21 15:18:14 -04:00
|
|
|
func TestGCC68255(t *testing.T) { testGCC68255(t) }
|
2021-02-23 09:58:14 +01:00
|
|
|
func TestHandle(t *testing.T) { testHandle(t) }
|
2019-02-22 10:17:22 -05:00
|
|
|
func TestHelpers(t *testing.T) { testHelpers(t) }
|
|
|
|
|
func TestLibgcc(t *testing.T) { testLibgcc(t) }
|
|
|
|
|
func TestMultipleAssign(t *testing.T) { testMultipleAssign(t) }
|
|
|
|
|
func TestNaming(t *testing.T) { testNaming(t) }
|
|
|
|
|
func TestPanicFromC(t *testing.T) { testPanicFromC(t) }
|
|
|
|
|
func TestPrintf(t *testing.T) { testPrintf(t) }
|
|
|
|
|
func TestReturnAfterGrow(t *testing.T) { testReturnAfterGrow(t) }
|
|
|
|
|
func TestReturnAfterGrowFromGo(t *testing.T) { testReturnAfterGrowFromGo(t) }
|
|
|
|
|
func TestSetEnv(t *testing.T) { testSetEnv(t) }
|
|
|
|
|
func TestThreadLock(t *testing.T) { testThreadLockFunc(t) }
|
|
|
|
|
func TestUnsignedInt(t *testing.T) { testUnsignedInt(t) }
|
|
|
|
|
func TestZeroArgCallback(t *testing.T) { testZeroArgCallback(t) }
|
2025-11-24 08:14:18 +00:00
|
|
|
func Test76340(t *testing.T) { test76340(t) }
|
2011-08-18 12:17:09 -04:00
|
|
|
|
runtime/cgo: store M for C-created thread in pthread key
This reapplies CL 485500, with a fix drafted in CL 492987 incorporated.
CL 485500 is reverted due to #60004 and #60007. #60004 is fixed in
CL 492743. #60007 is fixed in CL 492987 (incorporated in this CL).
[Original CL 485500 description]
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.
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.
[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 492987 description]
On the first call into Go from a C thread, currently we set the g0
stack's high bound imprecisely based on the SP. With CL 485500, we
keep the M and don't recompute the stack bounds when it calls into
Go again. If the first call is made when the C thread uses some
deep stack, but a subsequent call is made with a shallower stack,
the SP may be above g0.stack.hi.
This is usually okay as we don't check usually stack.hi. One place
where we do check for stack.hi is in the signal handler, in
adjustSignalStack. In particular, C TSAN delivers signals on the
g0 stack (instead of the usual signal stack). If the SP is above
g0.stack.hi, we don't see it is on the g0 stack, and throws.
This CL makes it get an accurate stack upper bound with the
pthread API (on the platforms where it is available).
Also add some debug print for the "handler not on signal stack"
throw.
Fixes #51676.
Fixes #59294.
Fixes #59678.
Fixes #60007.
Change-Id: Ie51c8e81ade34ec81d69fd7bce1fe0039a470776
Reviewed-on: https://go-review.googlesource.com/c/go/+/495855
Run-TryBot: Cherry Mui <cherryyz@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Michael Pratt <mpratt@google.com>
2023-05-17 12:01:15 -04:00
|
|
|
func BenchmarkCgoCall(b *testing.B) { benchCgoCall(b) }
|
|
|
|
|
func BenchmarkGoString(b *testing.B) { benchGoString(b) }
|
|
|
|
|
func BenchmarkCGoCallback(b *testing.B) { benchCallback(b) }
|
|
|
|
|
func BenchmarkCGoInCThread(b *testing.B) { benchCGoInCthread(b) }
|