Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use correct thread stack limits in jl_init_stack_limits #54639

Merged
merged 1 commit into from
Jun 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 7 additions & 11 deletions src/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,21 +71,17 @@ void jl_init_stack_limits(int ismaster, void **stack_lo, void **stack_hi)
size_t stacksize;
pthread_attr_getstack(&attr, &stackaddr, &stacksize);
pthread_attr_destroy(&attr);
*stack_lo = (void*)stackaddr;
#pragma GCC diagnostic push
#if defined(_COMPILER_GCC_) && __GNUC__ >= 12
#pragma GCC diagnostic ignored "-Wdangling-pointer"
#endif
*stack_hi = (void*)__builtin_frame_address(0);
#pragma GCC diagnostic pop
*stack_hi = stackaddr;
*stack_lo = (char*)stackaddr - stacksize;
return;
# elif defined(_OS_DARWIN_)
extern void *pthread_get_stackaddr_np(pthread_t thread);
extern size_t pthread_get_stacksize_np(pthread_t thread);
pthread_t thread = pthread_self();
void *stackaddr = pthread_get_stackaddr_np(thread);
*stack_lo = (void*)stackaddr;
*stack_hi = (void*)__builtin_frame_address(0);
size_t stacksize = pthread_get_stacksize_np(thread);
*stack_hi = stackaddr;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't you meant to switch these, since the malloc region is stackaddr to stackaddr+stacksize?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe I am misunderstanding you (sorry), but: stacks on all OS/CPU combos supported by Julia grow downwards, the stack starts at the highest address stackaddr and grows downwards towards stackaddr-stacksize (right after which the guard pages comes by default, at least for pthread created stacks with default settings).

I double checked this in darwin_libpthread, glibc and muslc and via real-world experiments on darwin/macos and linux+glibc (no muslc for me

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vtjnash does my explanation make sense to you? Any other questions?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, yeah, it is confusing that it is inverted from the values returned by pthread_attr_getstack, but apparently that is the legacy behavior

*stack_lo = (char*)stackaddr - stacksize;
return;
# elif defined(_OS_FREEBSD_)
pthread_attr_t attr;
Expand All @@ -95,8 +91,8 @@ void jl_init_stack_limits(int ismaster, void **stack_lo, void **stack_hi)
size_t stacksize;
pthread_attr_getstack(&attr, &stackaddr, &stacksize);
pthread_attr_destroy(&attr);
*stack_lo = (void*)stackaddr;
*stack_hi = (void*)__builtin_frame_address(0);
*stack_hi = stackaddr;
*stack_lo = (char*)stackaddr - stacksize;
return;
# else
# warning "Getting precise stack size for thread is not supported."
Expand Down