Skip to content

Commit

Permalink
update cgo src files to 1.23.5
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasten committed Jan 21, 2025
1 parent a034c30 commit 8cdeab3
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 41 deletions.
95 changes: 63 additions & 32 deletions src/gcc_libinit.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@

//go:build unix

// When cross-compiling with clang to linux/armv5, atomics are emulated
// and cause a compiler warning. This results in a build failure since
// cgo uses -Werror. See #65290.
#pragma GCC diagnostic ignored "-Wpragmas"
#pragma GCC diagnostic ignored "-Wunknown-warning-option"
#pragma GCC diagnostic ignored "-Watomic-alignment"

#include <pthread.h>
#include <errno.h>
#include <stdio.h>
Expand Down Expand Up @@ -42,31 +49,38 @@ x_cgo_sys_thread_create(void* (*func)(void*), void* arg) {
uintptr_t
_cgo_wait_runtime_init_done(void) {
void (*pfn)(struct context_arg*);
pfn = __atomic_load_n(&cgo_context_function, __ATOMIC_CONSUME);

pthread_mutex_lock(&runtime_init_mu);
while (runtime_init_done == 0) {
pthread_cond_wait(&runtime_init_cond, &runtime_init_mu);
}
int done = 2;
if (__atomic_load_n(&runtime_init_done, __ATOMIC_CONSUME) != done) {
pthread_mutex_lock(&runtime_init_mu);
while (__atomic_load_n(&runtime_init_done, __ATOMIC_CONSUME) == 0) {
pthread_cond_wait(&runtime_init_cond, &runtime_init_mu);
}

// The key and x_cgo_pthread_key_created are for the whole program,
// whereas the specific and destructor is per thread.
(void)pthread_key_destructor;
// if (x_cgo_pthread_key_created == 0 && pthread_key_create(&pthread_g, pthread_key_destructor) == 0) {
// x_cgo_pthread_key_created = 1;
// }

// TODO(iant): For the case of a new C thread calling into Go, such
// as when using -buildmode=c-archive, we know that Go runtime
// initialization is complete but we do not know that all Go init
// functions have been run. We should not fetch cgo_context_function
// until they have been, because that is where a call to
// SetCgoTraceback is likely to occur. We are going to wait for Go
// initialization to be complete anyhow, later, by waiting for
// main_init_done to be closed in cgocallbackg1. We should wait here
// instead. See also issue #15943.
pfn = cgo_context_function;
// The key and x_cgo_pthread_key_created are for the whole program,
// whereas the specific and destructor is per thread.
(void)pthread_key_destructor;
// if (x_cgo_pthread_key_created == 0 && pthread_key_create(&pthread_g, pthread_key_destructor) == 0) {
// x_cgo_pthread_key_created = 1;
// }


// TODO(iant): For the case of a new C thread calling into Go, such
// as when using -buildmode=c-archive, we know that Go runtime
// initialization is complete but we do not know that all Go init
// functions have been run. We should not fetch cgo_context_function
// until they have been, because that is where a call to
// SetCgoTraceback is likely to occur. We are going to wait for Go
// initialization to be complete anyhow, later, by waiting for
// main_init_done to be closed in cgocallbackg1. We should wait here
// instead. See also issue #15943.
pfn = __atomic_load_n(&cgo_context_function, __ATOMIC_CONSUME);

__atomic_store_n(&runtime_init_done, done, __ATOMIC_RELEASE);
pthread_mutex_unlock(&runtime_init_mu);
}

pthread_mutex_unlock(&runtime_init_mu);
if (pfn != nil) {
struct context_arg arg;

Expand All @@ -77,6 +91,30 @@ _cgo_wait_runtime_init_done(void) {
return 0;
}

// _cgo_set_stacklo sets g->stacklo based on the stack size.
// This is common code called from x_cgo_init, which is itself
// called by rt0_go in the runtime package.
void _cgo_set_stacklo(G *g, uintptr *pbounds)
{
uintptr bounds[2];

// pbounds can be passed in by the caller; see gcc_linux_amd64.c.
if (pbounds == NULL) {
pbounds = &bounds[0];
}

x_cgo_getstackbound(pbounds);

g->stacklo = *pbounds;

// Sanity check the results now, rather than getting a
// morestack on g0 crash.
if (g->stacklo >= g->stackhi) {
fprintf(stderr, "runtime/cgo: bad stack bounds: lo=%p hi=%p\n", (void*)(g->stacklo), (void*)(g->stackhi));
abort();
}
}

// Store the g into a thread-specific value associated with the pthread key pthread_g.
// And pthread_key_destructor will dropm when the thread is exiting.
void x_cgo_bindm(void* g) {
Expand All @@ -90,27 +128,20 @@ void x_cgo_bindm(void* g) {
void
x_cgo_notify_runtime_init_done(void* dummy __attribute__ ((unused))) {
pthread_mutex_lock(&runtime_init_mu);
runtime_init_done = 1;
__atomic_store_n(&runtime_init_done, 1, __ATOMIC_RELEASE);
pthread_cond_broadcast(&runtime_init_cond);
pthread_mutex_unlock(&runtime_init_mu);
}

// Sets the context function to call to record the traceback context
// when calling a Go function from C code. Called from runtime.SetCgoTraceback.
void x_cgo_set_context_function(void (*context)(struct context_arg*)) {
pthread_mutex_lock(&runtime_init_mu);
cgo_context_function = context;
pthread_mutex_unlock(&runtime_init_mu);
__atomic_store_n(&cgo_context_function, context, __ATOMIC_RELEASE);
}

// Gets the context function.
void (*(_cgo_get_context_function(void)))(struct context_arg*) {
void (*ret)(struct context_arg*);

pthread_mutex_lock(&runtime_init_mu);
ret = cgo_context_function;
pthread_mutex_unlock(&runtime_init_mu);
return ret;
return __atomic_load_n(&cgo_context_function, __ATOMIC_CONSUME);
}

// _cgo_try_pthread_create retries pthread_create if it fails with
Expand Down
2 changes: 1 addition & 1 deletion src/gcc_mmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build (linux && (amd64 || arm64 || ppc64le)) || (freebsd && amd64)
//go:build (linux && (amd64 || arm64 || loong64 || ppc64le)) || (freebsd && amd64)

#include <errno.h>
#include <stdint.h>
Expand Down
11 changes: 3 additions & 8 deletions src/libcgo.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,19 +69,14 @@ void _cgo_sys_thread_start(ThreadStart *ts);
uintptr_t _cgo_wait_runtime_init_done(void);

/*
* Call fn in the 6c world.
* Get the low and high boundaries of the stack.
*/
void crosscall_amd64(void (*fn)(void), void (*setg_gcc)(void*), void *g);

/*
* Call fn in the 8c world.
*/
void crosscall_386(void (*fn)(void));
void x_cgo_getstackbound(uintptr bounds[2]);

/*
* Prints error then calls abort. For linux and android.
*/
void fatalf(const char* format, ...);
void fatalf(const char* format, ...) __attribute__ ((noreturn));

/*
* Registers the current mach thread port for EXC_BAD_ACCESS processing.
Expand Down
5 changes: 5 additions & 0 deletions src/libcgo_unix.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

/*
* Initialize g->stacklo.
*/
extern void _cgo_set_stacklo(G *, uintptr *);

/*
* Call pthread_create, retrying on EAGAIN.
*/
Expand Down

0 comments on commit 8cdeab3

Please sign in to comment.