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

Fix #1429, adjust pthread stack to account for TCB+TLS #1430

Merged
merged 1 commit into from
Dec 12, 2023
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
37 changes: 21 additions & 16 deletions src/os/posix/src/os-impl-tasks.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,26 @@
#include "os-shared-idmap.h"

/*
* Defines
* Extra Stack Space for overhead -
*
* glibc pthreads implicitly puts its TCB/TLS structures at the base of the stack.
* The actual size of these structures is highly system and configuration dependent.
* Experimentation/measurement on an x64-64 system with glibc shows this to consume
* between 9-10kB of stack space off the top.
*
* There does not seem to be a reliable/standardized way of determining how large this
* is going to be, but PTHREAD_STACK_MIN (if defined) should include adequate space for it.
* If this is not defined, then just assume 1 page.
*
* Importantly - when the user passes a stack size to OS_TaskCreate() - the expectation is
* that there will be at least this amount of real _usable_ space for the task. If 10kB
* is used before the entry point is even called, this needs to be accounted for, or else
* the stack might end up being too small.
*/
#ifndef PTHREAD_STACK_MIN
#define PTHREAD_STACK_MIN (8 * 1024)
#ifdef PTHREAD_STACK_MIN

Check notice

Code scanning / CodeQL

Conditional compilation Note

Use of conditional compilation must be kept to a minimum.
#define OS_IMPL_STACK_EXTRA PTHREAD_STACK_MIN
#else
#define OS_IMPL_STACK_EXTRA POSIX_GlobalVars.PageSize
#endif

/* Tables where the OS object information is stored */
Expand Down Expand Up @@ -451,20 +467,9 @@
}

/*
* Adjust the stack size parameter.
*
* POSIX has additional restrictions/limitations on the stack size of tasks that
* other RTOS environments may not have. Specifically POSIX says that the stack
* size must be at least PTHREAD_STACK_MIN and may also need to be a multiple of the
* system page size.
*
* Rounding up means the user might get a bigger stack than they requested, but
* that should not break anything aside from consuming extra memory.
* Adjust the stack size parameter, add budget for TCB/TLS overhead.
*/
if (stacksz < PTHREAD_STACK_MIN)
{
stacksz = PTHREAD_STACK_MIN;
}
stacksz += OS_IMPL_STACK_EXTRA;

stacksz += POSIX_GlobalVars.PageSize - 1;
stacksz -= stacksz % POSIX_GlobalVars.PageSize;
Expand Down