From 5957ae48afd97f17cb0ee771595a21621bea23fb Mon Sep 17 00:00:00 2001 From: Thomas Tendyck Date: Sun, 10 Mar 2024 21:59:22 +0100 Subject: [PATCH] update go to 1.21.8 --- _ertgo | 2 +- dockerfiles/Dockerfile | 2 +- dockerfiles/Dockerfile.focal | 2 +- src/gcc_libinit.c | 202 +++++++++++++++++++---------------- src/gcc_mmap.c | 55 ++++------ src/integration_test.sh | 4 +- src/libcgo.h | 7 +- 7 files changed, 146 insertions(+), 128 deletions(-) diff --git a/_ertgo b/_ertgo index 4950e9b2..211aa319 160000 --- a/_ertgo +++ b/_ertgo @@ -1 +1 @@ -Subproject commit 4950e9b24c673b4a98d1a92d4819fb2c4cdbf5b0 +Subproject commit 211aa319e9c399e7ea0d59d9ade1386587ded4e9 diff --git a/dockerfiles/Dockerfile b/dockerfiles/Dockerfile index af8b76a9..b91bbc43 100644 --- a/dockerfiles/Dockerfile +++ b/dockerfiles/Dockerfile @@ -13,7 +13,7 @@ RUN apt-get install -y --no-install-recommends \ ARG erttag=v0.4.2 ARG egotag=v1.4.4 -RUN wget -qO- https://go.dev/dl/go1.20.6.linux-amd64.tar.gz | tar -C /usr/local -xz \ +RUN wget -qO- https://go.dev/dl/go1.21.8.linux-amd64.tar.gz | tar -C /usr/local -xz \ && git clone -b $erttag --depth=1 https://github.com/edgelesssys/edgelessrt \ && git clone -b $egotag --depth=1 https://github.com/edgelesssys/ego \ && mkdir ertbuild egobuild diff --git a/dockerfiles/Dockerfile.focal b/dockerfiles/Dockerfile.focal index 3bbceea6..2d352ae7 100644 --- a/dockerfiles/Dockerfile.focal +++ b/dockerfiles/Dockerfile.focal @@ -13,7 +13,7 @@ RUN DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \ ARG erttag=v0.4.2 ARG egotag=v1.4.4 -RUN wget -qO- https://go.dev/dl/go1.20.6.linux-amd64.tar.gz | tar -C /usr/local -xz \ +RUN wget -qO- https://go.dev/dl/go1.21.8.linux-amd64.tar.gz | tar -C /usr/local -xz \ && git clone -b $erttag --depth=1 https://github.com/edgelesssys/edgelessrt \ && git clone -b $egotag --depth=1 https://github.com/edgelesssys/ego \ && mkdir ertbuild egobuild diff --git a/src/gcc_libinit.c b/src/gcc_libinit.c index 2d3faff4..0113c780 100644 --- a/src/gcc_libinit.c +++ b/src/gcc_libinit.c @@ -2,125 +2,147 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -// +build cgo -// +build aix darwin dragonfly freebsd linux netbsd openbsd solaris +//go:build unix -#include #include +#include #include #include #include // strerror #include -#include "go_runtime_cleanup.h" #include "libcgo.h" #include "libcgo_unix.h" +#include "go_runtime_cleanup.h" static pthread_cond_t runtime_init_cond = PTHREAD_COND_INITIALIZER; static pthread_mutex_t runtime_init_mu = PTHREAD_MUTEX_INITIALIZER; static int runtime_init_done; +// pthread_g is a pthread specific key, for storing the g that binded to the C thread. +// The registered pthread_key_destructor will dropm, when the pthread-specified value g is not NULL, +// while a C thread is exiting. +static pthread_key_t pthread_g; +static void pthread_key_destructor(void* g); +uintptr_t x_cgo_pthread_key_created; +void (*x_crosscall2_ptr)(void (*fn)(void *), void *, int, size_t); + // The context function, used when tracing back C calls into Go. static void (*cgo_context_function)(struct context_arg*); -void x_cgo_sys_thread_create(void* (*func)(void*), void* arg) -{ - pthread_t p; - int err = _cgo_try_pthread_create(&p, NULL, func, arg); - if (err != 0) - { - fprintf(stderr, "pthread_create failed: %s", strerror(err)); - abort(); - } +void +x_cgo_sys_thread_create(void* (*func)(void*), void* arg) { + pthread_t p; + int err = _cgo_try_pthread_create(&p, NULL, func, arg); + if (err != 0) { + fprintf(stderr, "pthread_create failed: %s", strerror(err)); + abort(); + } } -uintptr_t _cgo_wait_runtime_init_done(void) -{ - void (*pfn)(struct context_arg*); - - pthread_mutex_lock(&runtime_init_mu); - while (runtime_init_done == 0) - { - pthread_cond_wait(&runtime_init_cond, &runtime_init_mu); - } - - // 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; - - pthread_mutex_unlock(&runtime_init_mu); - if (pfn != nil) - { - struct context_arg arg; - - arg.Context = 0; - (*pfn)(&arg); - return arg.Context; - } - return 0; +uintptr_t +_cgo_wait_runtime_init_done(void) { + void (*pfn)(struct context_arg*); + + pthread_mutex_lock(&runtime_init_mu); + while (runtime_init_done == 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. + 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; + + pthread_mutex_unlock(&runtime_init_mu); + if (pfn != nil) { + struct context_arg arg; + + arg.Context = 0; + (*pfn)(&arg); + return arg.Context; + } + return 0; } -void x_cgo_notify_runtime_init_done(void* dummy __attribute__((unused))) -{ - pthread_mutex_lock(&runtime_init_mu); - runtime_init_done = 1; - pthread_cond_broadcast(&runtime_init_cond); - pthread_mutex_unlock(&runtime_init_mu); +// 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) { + // We assume this will always succeed, otherwise, there might be extra M leaking, + // when a C thread exits after a cgo call. + // We only invoke this function once per thread in runtime.needAndBindM, + // and the next calls just reuse the bound m. + pthread_setspecific(pthread_g, g); +} + +void +x_cgo_notify_runtime_init_done(void* dummy __attribute__ ((unused))) { + pthread_mutex_lock(&runtime_init_mu); + runtime_init_done = 1; + 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); +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); } // 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; +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; } // _cgo_try_pthread_create retries pthread_create if it fails with // EAGAIN. -int _cgo_try_pthread_create( - pthread_t* thread, - const pthread_attr_t* attr, - void* (*pfn)(void*), - void* arg) -{ - int tries; - int err; - struct timespec ts; - - for (tries = 0; tries < 20; tries++) - { - err = go_rc_pthread_create(thread, attr, pfn, arg); - if (err == 0) - { - // pthread_detach(*thread); - return 0; - } - if (err != EAGAIN) - { - return err; - } - ts.tv_sec = 0; - ts.tv_nsec = (tries + 1) * 1000 * 1000; // Milliseconds. - nanosleep(&ts, nil); - } - return EAGAIN; +int +_cgo_try_pthread_create(pthread_t* thread, const pthread_attr_t* attr, void* (*pfn)(void*), void* arg) { + int tries; + int err; + struct timespec ts; + + for (tries = 0; tries < 20; tries++) { + err = go_rc_pthread_create(thread, attr, pfn, arg); + if (err == 0) { + // pthread_detach(*thread); + return 0; + } + if (err != EAGAIN) { + return err; + } + ts.tv_sec = 0; + ts.tv_nsec = (tries + 1) * 1000 * 1000; // Milliseconds. + nanosleep(&ts, nil); + } + return EAGAIN; +} + +static void +pthread_key_destructor(void* g) { + if (x_crosscall2_ptr != NULL) { + // fn == NULL means dropm. + // We restore g by using the stored g, before dropm in runtime.cgocallback, + // since the g stored in the TLS by Go might be cleared in some platforms, + // before this destructor invoked. + x_crosscall2_ptr(NULL, g, 0, 0); + } } diff --git a/src/gcc_mmap.c b/src/gcc_mmap.c index f9f53421..15536633 100644 --- a/src/gcc_mmap.c +++ b/src/gcc_mmap.c @@ -2,48 +2,39 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -// +build linux,amd64 linux,arm64 +//go:build (linux && (amd64 || arm64 || ppc64le)) || (freebsd && amd64) #include #include -#include #include #include -#include "go_runtime_cleanup.h" #include "libcgo.h" +#include "go_runtime_cleanup.h" -uintptr_t x_cgo_mmap( - void* addr, - uintptr_t length, - int32_t prot, - int32_t flags, - int32_t fd, - uint32_t offset) -{ - void* p; +uintptr_t +x_cgo_mmap(void *addr, uintptr_t length, int32_t prot, int32_t flags, int32_t fd, uint32_t offset) { + void *p; - _cgo_tsan_acquire(); - p = go_rc_mmap(addr, length, prot, flags, fd, offset); - _cgo_tsan_release(); - if (p == MAP_FAILED) - { - /* This is what the Go code expects on failure. */ - return (uintptr_t)errno; - } - return (uintptr_t)p; + _cgo_tsan_acquire(); + p = go_rc_mmap(addr, length, prot, flags, fd, offset); + _cgo_tsan_release(); + if (p == MAP_FAILED) { + /* This is what the Go code expects on failure. */ + return (uintptr_t)errno; + } + return (uintptr_t)p; } -void x_cgo_munmap(void* addr, uintptr_t length) -{ - int r; +void +x_cgo_munmap(void *addr, uintptr_t length) { + int r; - _cgo_tsan_acquire(); - r = go_rc_munmap(addr, length); - _cgo_tsan_release(); - if (r < 0) - { - /* The Go runtime is not prepared for munmap to fail. */ - abort(); - } + _cgo_tsan_acquire(); + r = go_rc_munmap(addr, length); + _cgo_tsan_release(); + if (r < 0) { + /* The Go runtime is not prepared for munmap to fail. */ + abort(); + } } diff --git a/src/integration_test.sh b/src/integration_test.sh index 244c4ad2..6da186d9 100755 --- a/src/integration_test.sh +++ b/src/integration_test.sh @@ -73,8 +73,8 @@ cd /tmp/ego-integration-test run ego sign # sign with 16385 heapSize should succeed now sed -i 's/"heapSize": 16385,/"heapSize": 511,/' enclave.json run ego sign |& grep "heapSize is set to less than" -# Run integration test built with ego_largeheap and heapSize of 512 MB -sed -i 's/"heapSize": 511,/"heapSize": 512,/' enclave.json +# Run integration test built with ego_largeheap and heapSize of 768 MB +sed -i 's/"heapSize": 511,/"heapSize": 768,/' enclave.json run ego sign run ego run integration-test diff --git a/src/libcgo.h b/src/libcgo.h index aba500a3..04755f0f 100644 --- a/src/libcgo.h +++ b/src/libcgo.h @@ -51,6 +51,11 @@ extern void (*_cgo_thread_start)(ThreadStart *ts); */ extern void (*_cgo_sys_thread_create)(void* (*func)(void*), void* arg); +/* + * Indicates whether a dummy pthread per-thread variable is allocated. + */ +extern uintptr_t *_cgo_pthread_key_created; + /* * Creates the new operating system thread (OS, arch dependent). */ @@ -66,7 +71,7 @@ uintptr_t _cgo_wait_runtime_init_done(void); /* * Call fn in the 6c world. */ -void crosscall_amd64(void (*fn)(void)); +void crosscall_amd64(void (*fn)(void), void (*setg_gcc)(void*), void *g); /* * Call fn in the 8c world.