I am writing code for a real-time program running on an embedded Linux system. Since it is very important that we do not hang unpredictably on page errors, I would like a stack prefix so that the guaranteed coverage of the area we are using is called with mlockall() .
For the main thread, this is quite simple; just make some large alloca() and don't forget to write each page. This works because when the program starts, the stack limit is much larger than we need; we end up highlighting exactly how we prefault inside.
However, for pthread stacks, will they be distributed using MAP_GROWSDOWN ? If so, what is the best way to prefix them, given that:
- We do not know how much of the (known) stack size is consumed when starting libc
- We do not want to allocate more memory on the stack than necessary
I know that I can use pthread_attr_setstack to transmit on a manually allocated stack, but this complicates the cleanup after the thread, and therefore I would prefer to avoid this if possible.
As such, what is the best way to accomplish this prefix? It would be enough if there was an easy way to find out the bottom border of the stack (just above the guard page); at that moment I could just write on every page from there to the current stack pointer.
Note that tolerance is not a concern; we will be happy to have a solution that works only on x86-32 and Linux.
source share