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 12, 2024
1 parent 9623a35 commit 70c56b5
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
33 changes: 27 additions & 6 deletions src/runtime/cgo/gcc_stack_unix.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,45 @@ x_cgo_getstackbound(uintptr bounds[2])
pthread_attr_t attr;
void *addr;
size_t size;
bool noinit;
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
pthread_attr_getstack(&attr, &addr, &size); // low address
err = pthread_getattr_np(pthread_self(), &attr); // GNU extension
if (err != 0) {
noinit = true;
goto failback;
}
err = pthread_attr_getstack(&attr, &addr, &size); // low address
if (err != 0) {
goto failback;
}
goto ret;
#elif defined(__illumos__)
pthread_attr_init(&attr);
pthread_attr_get_np(pthread_self(), &attr);
pthread_attr_getstack(&attr, &addr, &size); // low address
#else
err = pthread_attr_get_np(pthread_self(), &attr);
if (err != 0) {
goto failback;
}
err = pthread_attr_getstack(&attr, &addr, &size); // low address
if (err != 0) {
goto failback;
}
goto ret;
#endif
failback:
// We don't know how to get the current stacks, so assume they are the
// same as the default stack bounds.
if (!noinit) {
pthread_attr_destroy(&attr);
}
pthread_attr_init(&attr);
pthread_attr_getstacksize(&attr, &size);
addr = __builtin_frame_address(0) + 4096 - size;
#endif
ret:
pthread_attr_destroy(&attr);

bounds[0] = (uintptr)addr;
Expand Down
1 change: 1 addition & 0 deletions src/runtime/cgo/libcgo.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
Expand Down

0 comments on commit 70c56b5

Please sign in to comment.