Skip to content

Commit 8cdeab3

Browse files
committed
update cgo src files to 1.23.5
1 parent a034c30 commit 8cdeab3

File tree

4 files changed

+72
-41
lines changed

4 files changed

+72
-41
lines changed

src/gcc_libinit.c

Lines changed: 63 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@
44

55
//go:build unix
66

7+
// When cross-compiling with clang to linux/armv5, atomics are emulated
8+
// and cause a compiler warning. This results in a build failure since
9+
// cgo uses -Werror. See #65290.
10+
#pragma GCC diagnostic ignored "-Wpragmas"
11+
#pragma GCC diagnostic ignored "-Wunknown-warning-option"
12+
#pragma GCC diagnostic ignored "-Watomic-alignment"
13+
714
#include <pthread.h>
815
#include <errno.h>
916
#include <stdio.h>
@@ -42,31 +49,38 @@ x_cgo_sys_thread_create(void* (*func)(void*), void* arg) {
4249
uintptr_t
4350
_cgo_wait_runtime_init_done(void) {
4451
void (*pfn)(struct context_arg*);
52+
pfn = __atomic_load_n(&cgo_context_function, __ATOMIC_CONSUME);
4553

46-
pthread_mutex_lock(&runtime_init_mu);
47-
while (runtime_init_done == 0) {
48-
pthread_cond_wait(&runtime_init_cond, &runtime_init_mu);
49-
}
54+
int done = 2;
55+
if (__atomic_load_n(&runtime_init_done, __ATOMIC_CONSUME) != done) {
56+
pthread_mutex_lock(&runtime_init_mu);
57+
while (__atomic_load_n(&runtime_init_done, __ATOMIC_CONSUME) == 0) {
58+
pthread_cond_wait(&runtime_init_cond, &runtime_init_mu);
59+
}
5060

51-
// The key and x_cgo_pthread_key_created are for the whole program,
52-
// whereas the specific and destructor is per thread.
53-
(void)pthread_key_destructor;
54-
// if (x_cgo_pthread_key_created == 0 && pthread_key_create(&pthread_g, pthread_key_destructor) == 0) {
55-
// x_cgo_pthread_key_created = 1;
56-
// }
57-
58-
// TODO(iant): For the case of a new C thread calling into Go, such
59-
// as when using -buildmode=c-archive, we know that Go runtime
60-
// initialization is complete but we do not know that all Go init
61-
// functions have been run. We should not fetch cgo_context_function
62-
// until they have been, because that is where a call to
63-
// SetCgoTraceback is likely to occur. We are going to wait for Go
64-
// initialization to be complete anyhow, later, by waiting for
65-
// main_init_done to be closed in cgocallbackg1. We should wait here
66-
// instead. See also issue #15943.
67-
pfn = cgo_context_function;
61+
// The key and x_cgo_pthread_key_created are for the whole program,
62+
// whereas the specific and destructor is per thread.
63+
(void)pthread_key_destructor;
64+
// if (x_cgo_pthread_key_created == 0 && pthread_key_create(&pthread_g, pthread_key_destructor) == 0) {
65+
// x_cgo_pthread_key_created = 1;
66+
// }
67+
68+
69+
// TODO(iant): For the case of a new C thread calling into Go, such
70+
// as when using -buildmode=c-archive, we know that Go runtime
71+
// initialization is complete but we do not know that all Go init
72+
// functions have been run. We should not fetch cgo_context_function
73+
// until they have been, because that is where a call to
74+
// SetCgoTraceback is likely to occur. We are going to wait for Go
75+
// initialization to be complete anyhow, later, by waiting for
76+
// main_init_done to be closed in cgocallbackg1. We should wait here
77+
// instead. See also issue #15943.
78+
pfn = __atomic_load_n(&cgo_context_function, __ATOMIC_CONSUME);
79+
80+
__atomic_store_n(&runtime_init_done, done, __ATOMIC_RELEASE);
81+
pthread_mutex_unlock(&runtime_init_mu);
82+
}
6883

69-
pthread_mutex_unlock(&runtime_init_mu);
7084
if (pfn != nil) {
7185
struct context_arg arg;
7286

@@ -77,6 +91,30 @@ _cgo_wait_runtime_init_done(void) {
7791
return 0;
7892
}
7993

94+
// _cgo_set_stacklo sets g->stacklo based on the stack size.
95+
// This is common code called from x_cgo_init, which is itself
96+
// called by rt0_go in the runtime package.
97+
void _cgo_set_stacklo(G *g, uintptr *pbounds)
98+
{
99+
uintptr bounds[2];
100+
101+
// pbounds can be passed in by the caller; see gcc_linux_amd64.c.
102+
if (pbounds == NULL) {
103+
pbounds = &bounds[0];
104+
}
105+
106+
x_cgo_getstackbound(pbounds);
107+
108+
g->stacklo = *pbounds;
109+
110+
// Sanity check the results now, rather than getting a
111+
// morestack on g0 crash.
112+
if (g->stacklo >= g->stackhi) {
113+
fprintf(stderr, "runtime/cgo: bad stack bounds: lo=%p hi=%p\n", (void*)(g->stacklo), (void*)(g->stackhi));
114+
abort();
115+
}
116+
}
117+
80118
// Store the g into a thread-specific value associated with the pthread key pthread_g.
81119
// And pthread_key_destructor will dropm when the thread is exiting.
82120
void x_cgo_bindm(void* g) {
@@ -90,27 +128,20 @@ void x_cgo_bindm(void* g) {
90128
void
91129
x_cgo_notify_runtime_init_done(void* dummy __attribute__ ((unused))) {
92130
pthread_mutex_lock(&runtime_init_mu);
93-
runtime_init_done = 1;
131+
__atomic_store_n(&runtime_init_done, 1, __ATOMIC_RELEASE);
94132
pthread_cond_broadcast(&runtime_init_cond);
95133
pthread_mutex_unlock(&runtime_init_mu);
96134
}
97135

98136
// Sets the context function to call to record the traceback context
99137
// when calling a Go function from C code. Called from runtime.SetCgoTraceback.
100138
void x_cgo_set_context_function(void (*context)(struct context_arg*)) {
101-
pthread_mutex_lock(&runtime_init_mu);
102-
cgo_context_function = context;
103-
pthread_mutex_unlock(&runtime_init_mu);
139+
__atomic_store_n(&cgo_context_function, context, __ATOMIC_RELEASE);
104140
}
105141

106142
// Gets the context function.
107143
void (*(_cgo_get_context_function(void)))(struct context_arg*) {
108-
void (*ret)(struct context_arg*);
109-
110-
pthread_mutex_lock(&runtime_init_mu);
111-
ret = cgo_context_function;
112-
pthread_mutex_unlock(&runtime_init_mu);
113-
return ret;
144+
return __atomic_load_n(&cgo_context_function, __ATOMIC_CONSUME);
114145
}
115146

116147
// _cgo_try_pthread_create retries pthread_create if it fails with

src/gcc_mmap.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
//go:build (linux && (amd64 || arm64 || ppc64le)) || (freebsd && amd64)
5+
//go:build (linux && (amd64 || arm64 || loong64 || ppc64le)) || (freebsd && amd64)
66

77
#include <errno.h>
88
#include <stdint.h>

src/libcgo.h

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,19 +69,14 @@ void _cgo_sys_thread_start(ThreadStart *ts);
6969
uintptr_t _cgo_wait_runtime_init_done(void);
7070

7171
/*
72-
* Call fn in the 6c world.
72+
* Get the low and high boundaries of the stack.
7373
*/
74-
void crosscall_amd64(void (*fn)(void), void (*setg_gcc)(void*), void *g);
75-
76-
/*
77-
* Call fn in the 8c world.
78-
*/
79-
void crosscall_386(void (*fn)(void));
74+
void x_cgo_getstackbound(uintptr bounds[2]);
8075

8176
/*
8277
* Prints error then calls abort. For linux and android.
8378
*/
84-
void fatalf(const char* format, ...);
79+
void fatalf(const char* format, ...) __attribute__ ((noreturn));
8580

8681
/*
8782
* Registers the current mach thread port for EXC_BAD_ACCESS processing.

src/libcgo_unix.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5+
/*
6+
* Initialize g->stacklo.
7+
*/
8+
extern void _cgo_set_stacklo(G *, uintptr *);
9+
510
/*
611
* Call pthread_create, retrying on EAGAIN.
712
*/

0 commit comments

Comments
 (0)