Skip to content

Commit

Permalink
runtime/cgo: provide backward compatibility with old glibc for cgo
Browse files Browse the repository at this point in the history
Fixes: golang#65625
Before go1.22, the example in golang#65625 can be run successfully, though
the core issue is in old version glibc, but it will be better to
provide this backward compatibility to let people can upgrade to
go 1.22+.

Signed-off-by: lifubang <[email protected]>
  • Loading branch information
lifubang committed May 23, 2024
1 parent a5339da commit 27655c7
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions src/runtime/cgo/gcc_stack_unix.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#endif

#include <pthread.h>
#include <string.h>
#include "libcgo.h"

void
Expand All @@ -17,12 +18,33 @@ x_cgo_getstackbound(uintptr bounds[2])
pthread_attr_t attr;
void *addr;
size_t size;
int err;

#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
// (and the fallback doesn't work...). Illumos does not.
pthread_getattr_np(pthread_self(), &attr); // GNU extension

// After glibc 2.31, there is a `__pthread_attr_init` call in
// `pthread_getattr_np`, but if there is no init for `attr`, it
// will cause `pthread_attr_destroy` free some unknown memory,
// which will cause golang crash, so we need to call
// `pthread_attr_init` firstly to have a backward compatibility
// with glibc(<= 2.31).
pthread_attr_init(&attr);

err = pthread_getattr_np(pthread_self(), &attr); // GNU extension

// As we all know, when using clone(2), there is a tid dirty cache
// bug in all versions of glibc(>=2.25), which is introduced by:
// https://sourceware.org/git/?p=glibc.git;a=commitdiff;h=c579f48e
// But we can ignore this bug because we only need the stack's addr
// and size here, and the error is from `__pthread_getaffinity_np`,
// which is unrelated to the stack info.
if (err != 0 && err != 3) {
fatalf("pthread_getattr_np failed: %s", strerror(err));
}

pthread_attr_getstack(&attr, &addr, &size); // low address
#elif defined(__illumos__)
pthread_attr_init(&attr);
Expand All @@ -37,10 +59,6 @@ x_cgo_getstackbound(uintptr bounds[2])
#endif
pthread_attr_destroy(&attr);

// bounds points into the Go stack. TSAN can't see the synchronization
// in Go around stack reuse.
_cgo_tsan_acquire();
bounds[0] = (uintptr)addr;
bounds[1] = (uintptr)addr + size;
_cgo_tsan_release();
}

0 comments on commit 27655c7

Please sign in to comment.