How the thread stack is growing

Source: Internet
Author: User

We know that the default space for each thread's initial stack is 1M, and we can set it in the VC-compiled linker, which will be compiled into the final PE executable file. The thread stack memory includes the commit and reserver parts, and we said 1M actually refers to the reserve section, the system in order to save memory, does not put all the reserve 1M commit physical memory (commit), so the initial only commit part of memory.   We can easily find a program to verify through WinDbg:!address-f:stack  baseaddr endaddr+1 rgnsize Type State Protect Usage---------------------------------------------------------------------------------------------    90000 184000 f4000 mem_private mem_reserve Stack [~0; 16d8.13ec]  184000 185000-mem_private mem_commit PAGE_READ write| Page_guard stack [~0; 16d8.13ec]  185000 190000 b000 mem_private mem_commit page_readwrite Stack [~0; 16d8.13ec]&nbsp You can see a thread's stack divided by 3 parts: 0xb000 bytes of mem_commit memory, 0x1000 bytes of Mem_commit & Page_guard memory, and 0xf4000 bytes of mem_reserve memory, The total is 0xb000+0x1000+0xf4000 = 0x100000 = 1m  through experiments, we can see that the thread stack commits only part of the memory, most of the memory is reserve, and now the problem is that the stack is in the process of growing, How does it commit (commit) memory? We know that we declare a local variable of N-byte size in the function, which is the space requested in the stack of the thread (in fact it only needs to be esp-n). If we look at the disassembly code of the function, we will notice that it has no commit memory-related code. So how in the end did it commit those reserve memory?  &nBSP, who had been interviewed for this question, because did not read the relevant books, did not answer out ...  recently thought about this problem, finally in Zhang Banque << software debugging >> found the answer:

when the system submits the stack space, it will intentionally submit a page, which is called the Stack Guard page, which we can verify in the WinDbg experiment above. The Stack protection page has a special Page_guard property that, when a memory page with such a property is accessed, the CPU generates a page fault and begins execution of the system's memory management function, which clears the Page_guard property of the corresponding page when the Page_guard property is detected by the memory management function. It then calls a system function called Micheckforuserstackoverflow, which reads the basic information of the user-state stack from the teb of the current thread and checks the address that caused the exception, and returns the Status_ if the access address that caused the exception is not in the stack space range. Guard_page_violation, otherwise the Micheckforuserstackoverflow function calculates if there is enough space left in the stack to create a new stack protection page. If so, call Zwallocatevirtualmemory to commit a memory page with the Page_guard property from the reserved space. The new Stack protection page and the original adjacent, after such operations, the stack of the protection of the page to the lower address direction of the translation of a bit, the stack of free space to increase the size of a page, this is called the stack Space auto-growth.   stack overflow means that when the stack space of the submission is exhausted and the Stack Protection page is accessed again, the system repeats the process until the stack protects the page from the last page of the reserved space with only one page of space left. The Micheckforuserstackoverflow function submits the second-to-last page, but no longer sets the Page_guard property because the final page is never accessible, so the stack grows to its maximum limit, in order for the application to know that the stack will run out, The Micheckforuserstackoverflow function returns Status_stack_overflow, triggering a stack overflow exception. Finally the sense of technology deep can be deep, from the C + + compiler to the CRT runtime, then to the operating system, from the user state to the kernel and the driver, and finally to the hardware, the principle behind the principle, really can master all the details of how many people?

How the thread stack is growing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.