堆栈大小的最大使用限制是否有限制?

时间:2013-03-11 09:59:21

标签: assembly x86

我正在使用x86指令集编写程序。当我使用存储在40kb大小的堆栈中的本地数组时,为什么会崩溃。

我正在使用带有i5处理器的windows7 os并在visual c ++ express edition 2008中进行编译

1 个答案:

答案 0 :(得分:8)

我认为你正在以警卫页面的形式击中捕获。

为了在实际使用之前不浪费实际内存,Windows最初会保留完整的堆栈空间(默认为1MB;可以通过编辑PE头来更改),但只提交两个页面,并使第二个页面成为保护页面。保护页面是内存页面(4KB),在对其进行任何访问时会触发特殊异常(STATUS_GUARD_PAGE_VIOLATION)。当内核检测到保护页面异常时,它会提交触摸的页面并在其后添加另一个保护页面。这样,如果你的函数在堆栈上推送小变量,它就会“不断”增长。

但是,如果尝试分配大小超过4K(4096字节)的局部变量,则会出现问题。通常,堆栈分配是通过简单地从ESP中减去来完成的。如果从中减去超过4K,然后尝试写入堆栈,则有可能在保护页面上进行拍摄并在其后访问保留的内存。这个不会被内核捕获,但会被传递给你的程序,并且通常会导致崩溃。

解决方案很简单 - 以4K块(= 4096 = 0x1000字节)堆叠分配并在每个块之后触摸堆栈以触发防护页面。 MSVC编译器通过在使用超过4K局部变量的函数的开头调用__chkstk()函数来自动执行此操作。以下是CRT来源的功能列表:

;***
;_chkstk - check stack upon procedure entry
;
;Purpose:
;       Provide stack checking on procedure entry. Method is to simply probe
;       each page of memory required for the stack in descending order. This
;       causes the necessary pages of memory to be allocated via the guard
;       page scheme, if possible. In the event of failure, the OS raises the
;       _XCPT_UNABLE_TO_GROW_STACK exception.
;
;       NOTE:  Currently, the (EAX < _PAGESIZE_) code path falls through
;       to the "lastpage" label of the (EAX >= _PAGESIZE_) code path.  This
;       is small; a minor speed optimization would be to special case
;       this up top.  This would avoid the painful save/restore of
;       ecx and would shorten the code path by 4-6 instructions.
;
;Entry:
;       EAX = size of local frame
;
;Exit:
;       ESP = new stackframe, if successful
;
;Uses:
;       EAX
;
;Exceptions:
;       _XCPT_GUARD_PAGE_VIOLATION - May be raised on a page probe. NEVER TRAP
;                                    THIS!!!! It is used by the OS to grow the
;                                    stack on demand.
;       _XCPT_UNABLE_TO_GROW_STACK - The stack cannot be grown. More precisely,
;                                    the attempt by the OS memory manager to
;                                    allocate another guard page in response
;                                    to a _XCPT_GUARD_PAGE_VIOLATION has
;                                    failed.
;
;*******************************************************************************

public  _alloca_probe

_chkstk proc

_alloca_probe    =  _chkstk

        push    ecx

; Calculate new TOS.

        lea     ecx, [esp] + 8 - 4      ; TOS before entering function + size for ret value
        sub     ecx, eax                ; new TOS

; Handle allocation size that results in wraparound.
; Wraparound will result in StackOverflow exception.

        sbb     eax, eax                ; 0 if CF==0, ~0 if CF==1
        not     eax                     ; ~0 if TOS did not wrapped around, 0 otherwise
        and     ecx, eax                ; set to 0 if wraparound

        mov     eax, esp                ; current TOS
        and     eax, not ( _PAGESIZE_ - 1) ; Round down to current page boundary

cs10:
        cmp     ecx, eax                ; Is new TOS
        jb      short cs20              ; in probed page?
        mov     eax, ecx                ; yes.
        pop     ecx
        xchg    esp, eax                ; update esp
        mov     eax, dword ptr [eax]    ; get return address
        mov     dword ptr [esp], eax    ; and put it at new TOS
        ret

; Find next lower page and probe
cs20:
        sub     eax, _PAGESIZE_         ; decrease by PAGESIZE
        test    dword ptr [eax],eax     ; probe page.
        jmp     short cs10

_chkstk endp

在你的情况下,你可能不需要这种复杂的逻辑,像这样的事情会这样做:

    xor eax, eax
    mov ecx, 40     ; alloc 40 pages
l1:
    sub esp, 1000h  ; move esp one page
    mov [esp], eax  ; touch the guard page
    loop l1         ; keep looping
    sub esp, xxxh   ; alloc the remaining variables

有关堆栈和防护页面的更多详细信息,请参阅here