Closed Thread
Results 1 to 4 of 4

Thread: Stack allocation and stack size

  1. #1
    mircan is offline Newbie
    Join Date
    Mar 2010
    Posts
    2
    Rep Power
    0

    Stack allocation and stack size

    Hi

    I've got the following question: what detrmines the size of the stack of a process in Linux?
    Below are two programs written in C and their disassembly (I know Prog1.c is not the way one should handle with pointers ). The assembly codes are very similiar but execution of the first program causes segmentation fault. The initial stack of the first program is about 12 KB while for the second it's about 980 KB. What determines this, if the assembly codes are so similiar?
    And even assumming that initial stack of the first program is 12 KB - why writing to address from within the stack limit (about 8 MB on my system) doesn't make the system expand the stack?
    Is there any stack allocation at the binary level? Is the stack size determined by the compiler?

    Prog1.c:
    Code:
    int main()
    {
            char a;        
            char* b = &a;
            b = b - 999999;
            *b = '\0';
    }
    Prog1.s:
    Code:
            .file        "prog1.c"
            .text
    .globl main
            .type        main, @function
    main:
            pushl        %ebp
            movl        %esp, %ebp
            subl        $16, %esp
            leal        -5(%ebp), %eax
            movl        %eax, -4(%ebp)
            subl        $999999, -4(%ebp)
            movl        -4(%ebp), %eax
            movb        $0, (%eax)
            leave
            ret
            .size        main, .-main
            .ident        "GCC: (GNU) 4.4.3"
            .section        .note.GNU-stack,"",@progbits
    Prog2.c:
    Code:
    int main()
    {
            char a[1000000];
            char* b = &a[0];
            *b = '\0';
    }
    Prog2.s:
    Code:
            .file        "prog2.c"
            .text
    .globl main
            .type        main, @function
    main:
            pushl        %ebp
            movl        %esp, %ebp
            subl        $1000016, %esp
            leal        -1000004(%ebp), %eax
            movl        %eax, -4(%ebp)
            movl        -4(%ebp), %eax
            movb        $0, (%eax)
            leave
            ret
            .size        main, .-main
            .ident        "GCC: (GNU) 4.4.3"
            .section        .note.GNU-stack,"",@progbits

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Sysop_fb is offline Programmer
    Join Date
    Apr 2009
    Location
    Missouri
    Posts
    150
    Blog Entries
    2
    Rep Power
    12

    Re: Stack allocation and stack size

    The programs are completely different.

    This one is allocating 1 variable a which is of size char and a pointer to char pointing at a.
    Then you move the pointer from a to another address 999999 bytes away in memory. Probably a place in read-only memory or atleast a place where a user-space program shouldn't be writing to.
    Prog1.c:
    Code:
    int main()
    {
            char a;        
            char* b = &a;
            b = b - 999999;
            *b = '\0';
    }
    Prog1.s:
    Code:
            .file        "prog1.c"
            .text
    .globl main
            .type        main, @function
    main:
            pushl        %ebp
            movl        %esp, %ebp
            subl        $16, %esp
            leal        -5(%ebp), %eax
            movl        %eax, -4(%ebp)
            subl        $999999, -4(%ebp)
            movl        -4(%ebp), %eax
            movb        $0, (%eax)
            leave
            ret
            .size        main, .-main
            .ident        "GCC: (GNU) 4.4.3"
            .section        .note.GNU-stack,"",@progbits

    This one allocates 1000000 chars in an char array and a pointer to char b which points at the first char in that char array then it sets the value of a[0] to '\0'
    Prog2.c:
    Code:
    int main()
    {
            char a[1000000];
            char* b = &a[0];
            *b = '\0';
    }
    Prog2.s:
    Code:
            .file        "prog2.c"
            .text
    .globl main
            .type        main, @function
    main:
            pushl        %ebp
            movl        %esp, %ebp
            subl        $1000016, %esp
            leal        -1000004(%ebp), %eax
            movl        %eax, -4(%ebp)
            movl        -4(%ebp), %eax
            movb        $0, (%eax)
            leave
            ret
            .size        main, .-main
            .ident        "GCC: (GNU) 4.4.3"
            .section        .note.GNU-stack,"",@progbits
    "The best optimizer is between your ears" - Michael Abrash
    Saying you can optimize a program is like saying you understand how a program works on every level of every facet on a specific machines configuration.

  4. #3
    mircan is offline Newbie
    Join Date
    Mar 2010
    Posts
    2
    Rep Power
    0

    Re: Stack allocation and stack size

    I finally found out that the thing is in a way Linux handles page faults. If a page fault related to an address beetwen stack top and stack limit occurs the kernel checks the value in stack pointer register. On x86 for instance stack size is expanded only if the address is not lower than stack pointer - 32.

  5. #4
    Sysop_fb is offline Programmer
    Join Date
    Apr 2009
    Location
    Missouri
    Posts
    150
    Blog Entries
    2
    Rep Power
    12

    Re: Stack allocation and stack size

    The stack size of the second program is expanded because C likes to put local variables on the stack.
    Code:
    subl        $1000016, %esp
    "The best optimizer is between your ears" - Michael Abrash
    Saying you can optimize a program is like saying you understand how a program works on every level of every facet on a specific machines configuration.

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Generics and the Stack
    By chili5 in forum Java Tutorials
    Replies: 4
    Last Post: 10-14-2011, 03:03 PM
  2. C++ How to set stack/heap size?
    By pancakethirsty in forum C and C++
    Replies: 10
    Last Post: 07-12-2011, 10:30 PM
  3. call stack.
    By javaman in forum Java Help
    Replies: 0
    Last Post: 05-10-2010, 02:20 AM
  4. Getting the size of a Stack in C
    By fantanoice in forum C and C++
    Replies: 4
    Last Post: 11-05-2009, 02:51 AM
  5. ASM 16 bit STACK TUTORIAL
    By JMC31337 in forum Classes and Code Snippets
    Replies: 3
    Last Post: 02-17-2009, 11:52 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts