]> Cypherpunks repositories - gostls13.git/commitdiff
runtime/cgo: get getstackbound for set_stacklo
authorIan Lance Taylor <iant@golang.org>
Wed, 16 Aug 2023 00:29:40 +0000 (17:29 -0700)
committerGopher Robot <gobot@golang.org>
Tue, 22 Aug 2023 22:06:46 +0000 (22:06 +0000)
Change-Id: Ia63a4604449b5e460e6f54c962fb7d6db2bc6a43
Reviewed-on: https://go-review.googlesource.com/c/go/+/519457
Run-TryBot: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@google.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Cherry Mui <cherryyz@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
src/runtime/cgo/gcc_freebsd_amd64.c
src/runtime/cgo/gcc_libinit.c
src/runtime/cgo/gcc_linux_amd64.c
src/runtime/cgo/gcc_linux_arm64.c
src/runtime/cgo/libcgo.h
src/runtime/cgo/libcgo_unix.h

index 81943881c254f5fc9c03eff4d2bcbf846fd72583..31905f2aa3c4ef7985010bc017cbf2115ddd4675 100644 (file)
@@ -17,17 +17,17 @@ static void (*setg_gcc)(void*);
 void
 x_cgo_init(G *g, void (*setg)(void*))
 {
-       pthread_attr_t *attr;
+       uintptr *pbounds;
 
        // Deal with memory sanitizer/clang interaction.
        // See gcc_linux_amd64.c for details.
        setg_gcc = setg;
-       attr = (pthread_attr_t*)malloc(sizeof *attr);
-       if (attr == NULL) {
+       pbounds = (uintptr*)malloc(2 * sizeof(uintptr));
+       if (pbounds == NULL) {
                fatalf("malloc failed: %s", strerror(errno));
        }
-       _cgo_set_stacklo(g, attr);
-       free(attr);
+       _cgo_set_stacklo(g, pbounds);
+       free(pbounds);
 }
 
 void
index f6c930209e1d526c73f8bb1615f0a533b3272223..68f4a023795db1ba9ce6866ec22d76e05a1e3eb9 100644 (file)
@@ -85,20 +85,18 @@ _cgo_wait_runtime_init_done(void) {
 // _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, pthread_attr_t *pattr)
+void _cgo_set_stacklo(G *g, uintptr *pbounds)
 {
-       pthread_attr_t attr;
-       size_t size;
+       uintptr bounds[2];
 
-       // pattr can be passed in by the caller; see gcc_linux_amd64.c.
-       if (pattr == NULL) {
-               pattr = &attr;
+       // pbounds can be passed in by the caller; see gcc_linux_amd64.c.
+       if (pbounds == NULL) {
+               pbounds = &bounds[0];
        }
 
-       pthread_attr_init(pattr);
-       pthread_attr_getstacksize(pattr, &size);
+       x_cgo_getstackbound(pbounds);
 
-       g->stacklo = (uintptr)(__builtin_frame_address(0)) - size + 4096;
+       g->stacklo = *pbounds;
 
        // Sanity check the results now, rather than getting a
        // morestack on g0 crash.
@@ -106,8 +104,6 @@ void _cgo_set_stacklo(G *g, pthread_attr_t *pattr)
                fprintf(stderr, "runtime/cgo: bad stack bounds: lo=%p hi=%p\n", (void*)(g->stacklo), (void*)(g->stackhi));
                abort();
        }
-
-       pthread_attr_destroy(pattr);
 }
 
 // Store the g into a thread-specific value associated with the pthread key pthread_g.
index 405f3e1d5e752731ccc726793edb2817ce478ae0..dcb596e213750c8d2190c9e8f058231be5f46a27 100644 (file)
@@ -19,7 +19,7 @@ void (*x_cgo_inittls)(void **tlsg, void **tlsbase) __attribute__((common));
 void
 x_cgo_init(G *g, void (*setg)(void*), void **tlsg, void **tlsbase)
 {
-       pthread_attr_t *attr;
+       uintptr *pbounds;
 
        /* The memory sanitizer distributed with versions of clang
           before 3.8 has a bug: if you call mmap before malloc, mmap
@@ -37,12 +37,12 @@ x_cgo_init(G *g, void (*setg)(void*), void **tlsg, void **tlsbase)
           malloc, so we actually use the memory we allocate.  */
 
        setg_gcc = setg;
-       attr = (pthread_attr_t*)malloc(sizeof *attr);
-       if (attr == NULL) {
+       pbounds = (uintptr*)malloc(2 * sizeof(uintptr));
+       if (pbounds == NULL) {
                fatalf("malloc failed: %s", strerror(errno));
        }
-       _cgo_set_stacklo(g, attr);
-       free(attr);
+       _cgo_set_stacklo(g, pbounds);
+       free(pbounds);
 
        if (x_cgo_inittls) {
                x_cgo_inittls(tlsg, tlsbase);
index 8b822fe8a31129899c46d4cbe820ae39f8824abe..0dcff2c0901406d967fd92a6a947eddb44e40158 100644 (file)
@@ -56,7 +56,7 @@ threadentry(void *v)
 void
 x_cgo_init(G *g, void (*setg)(void*), void **tlsg, void **tlsbase)
 {
-       pthread_attr_t *attr;
+       uintptr *pbounds;
 
        /* The memory sanitizer distributed with versions of clang
           before 3.8 has a bug: if you call mmap before malloc, mmap
@@ -74,12 +74,12 @@ x_cgo_init(G *g, void (*setg)(void*), void **tlsg, void **tlsbase)
           malloc, so we actually use the memory we allocate.  */
 
        setg_gcc = setg;
-       attr = (pthread_attr_t*)malloc(sizeof *attr);
-       if (attr == NULL) {
+       pbounds = (uintptr*)malloc(2 * sizeof(uintptr));
+       if (pbounds == NULL) {
                fatalf("malloc failed: %s", strerror(errno));
        }
-       _cgo_set_stacklo(g, attr);
-       free(attr);
+       _cgo_set_stacklo(g, pbounds);
+       free(pbounds);
 
        if (x_cgo_inittls) {
                x_cgo_inittls(tlsg, tlsbase);
index 443aa2696d49349b91881e3d3c507910971a8c74..295c12c53c113a532b69944594ee68513432cd1b 100644 (file)
@@ -68,6 +68,11 @@ void _cgo_sys_thread_start(ThreadStart *ts);
  */
 uintptr_t _cgo_wait_runtime_init_done(void);
 
+/*
+ * Get the low and high boundaries of the stack.
+ */
+void x_cgo_getstackbound(uintptr bounds[2]);
+
 /*
  * Prints error then calls abort. For linux and android.
  */
index 6d30769542a17964ed4725417b6a5882d23b255a..b8f8d30d307233cca522dca9f6dff06cd2393bc3 100644 (file)
@@ -5,7 +5,7 @@
 /*
  * Initialize g->stacklo.
  */
-extern void _cgo_set_stacklo(G *, pthread_attr_t*);
+extern void _cgo_set_stacklo(G *, uintptr *);
 
 /*
  * Call pthread_create, retrying on EAGAIN.