2 ms·
The main problem there is that local non-static variables get placed onto stack. Stack space is allocated by generated code by just decrementing stack pointer w
by dfox 11y ago
The main problem there is that local non-static variables get placed onto stack. Stack space is allocated by generated code by just decrementing stack pointer without any explicit calls to OS. On typical modern unix, only few pages of stack are actually mapped and kernel handles page faults on neighboring pages by allocating more stack pages. Because it is possible that function needs more than one page of stack space, there is more than one such "magic" stack page, but still there is some finite number of them (othervise there would be no way to distinguish between accesses beyond the end of stack that should grow stack and accesses to random unmapped memory). Thus if you allocate some ridiculously large things on stack and access them in the direction opposite of stack growth, you may get segfault (accessing these structures in "the right order" is by no means sufficient for this to be safe because there are things like red-zones, signals, other local variables...).
This is true only for first thread, other threads have fixed stack size specified on thread creation (on Linux it's 8MB by default), but usually even thread's stack pages are really allocated only on first access.
On UNIX if you really want to bump stack by arbitrary amounts the most portable way is to preallocate your own stack of sufficient size and then use that (either by abusing sigaltstack() or via makecontext()/setcontext() or possibly by creating new thread). But generally, having large local variables is not exactly good idea.
- bogomipz 11y agoCould you elaborate on "Stack space is allocated by generated code" Are you talking about the C runtime in the binary? Thanks.
- dfox 11y agoNo. Typical C function looks something like this: push %rbp mov %rsp, %rbp sub $something, %rsp ... actual code ... mov %rbp, %rsp ret The sub $something, %rsp instruction is everything that user-space does to allocate stack memory. Actual allocation of stack pages happens in kernel in a way that is completely transparent as long as the $something does not get unreasonably large.