4

Will the stack of a C program be retracted?

 3 years ago
source link: https://www.codesd.com/item/will-the-stack-of-a-c-program-be-retracted.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Will the stack of a C program be retracted?

advertisements

I've noticed that every running C program has a private mapping called [stack] that is initially quite small (128k on my machine), but will grow to accomodate any automatic variables (up to the stack size limit). I assume this is where the call stack of my program is located.

However, it doesn't seem to ever shrink back to its original size. Is there any way to free up that memory without terminating the process?

How is the C stack implemented internally; what increases the size of the [stack] mapping on demand? Some compiler generated code, the C library or the operating system? Where is the increase triggered?

Update: I'm using Linux 3.0.0, gcc 4.6.1 and glibc6 on x86-64; as this is probably pretty implementation specific, any information on how it works there would be fine.


In Linux/MMU (in !MMU you cannot grow the stack), the stack is grown in the page fault handler. For x86, whether to grow the stack is decided by the following code from arch/x86/mm/fault.c:do_page_fault():

        if (error_code & PF_USER) {
            /*
             * Accessing the stack below %sp is always a bug.
             * The large cushion allows instructions like enter
             * and pusha to work. ("enter $65535, $31" pushes
             * 32 pointers and then decrements %sp by 65535.)
             */
            if (unlikely(address + 65536 + 32 * sizeof(unsigned long) < regs->sp)) {
                    bad_area(regs, error_code, address);
                    return;
            }
    }
    if (unlikely(expand_stack(vma, address))) {
            bad_area(regs, error_code, address);
            return;
    }

expand_stack() checks the usual RLIMITS (RLIMIT_AS, RLIMIT_STACK, RLIMIT_MEMLOCK), whether LSMs will allow to grow the stack, whether there's too much overcommit, etc..., and finally grows the stack.

Tags stack

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK