]> Cypherpunks repositories - gostls13.git/commitdiff
[release-branch.go1.22] runtime: x_cgo_getstackbound: initialize pthread attr
authorKir Kolyshkin <kolyshkin@gmail.com>
Thu, 23 May 2024 21:12:10 +0000 (14:12 -0700)
committerGopher Robot <gobot@golang.org>
Wed, 29 May 2024 23:45:12 +0000 (23:45 +0000)
In glibc versions older than 2.32 (before commit 4721f95058),
pthread_getattr_np does not always initialize the `attr` argument,
and when it fails, it results in a NULL pointer dereference in
pthread_attr_destroy down the road.

This is the simplest way to avoid this, and an alternative to CL 585019.

Fixes #67650.

Change-Id: If490fd37020b03eb084ebbdbf9ae0248916426d0
Reviewed-on: https://go-review.googlesource.com/c/go/+/587919
Auto-Submit: Ian Lance Taylor <iant@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Cherry Mui <cherryyz@google.com>
Reviewed-on: https://go-review.googlesource.com/c/go/+/587979
Reviewed-by: Carlos Amedee <carlos@golang.org>
Auto-Submit: Matthew Dempsky <mdempsky@google.com>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
Commit-Queue: Matthew Dempsky <mdempsky@google.com>

src/runtime/cgo/gcc_stack_unix.c

index f3fead9c9eca6a2f9845e4decf875cd926d52880..98a75a19679547c4fceffea833311535299ddba1 100644 (file)
@@ -18,6 +18,9 @@ x_cgo_getstackbound(uintptr bounds[2])
        void *addr;
        size_t size;
 
+       // Needed before pthread_getattr_np, too, since before glibc 2.32
+       // it did not call pthread_attr_init in all cases (see #65625).
+       pthread_attr_init(&attr);
 #if defined(__GLIBC__) || (defined(__sun) && !defined(__illumos__))
        // pthread_getattr_np is a GNU extension supported in glibc.
        // Solaris is not glibc but does support pthread_getattr_np
@@ -25,11 +28,9 @@ x_cgo_getstackbound(uintptr bounds[2])
        pthread_getattr_np(pthread_self(), &attr);  // GNU extension
        pthread_attr_getstack(&attr, &addr, &size); // low address
 #elif defined(__illumos__)
-       pthread_attr_init(&attr);
        pthread_attr_get_np(pthread_self(), &attr);
        pthread_attr_getstack(&attr, &addr, &size); // low address
 #else
-       pthread_attr_init(&attr);
        pthread_attr_getstacksize(&attr, &size);
        addr = __builtin_frame_address(0) + 4096 - size;
 #endif