Assembly 堆栈大小的最大使用限制是否有任何限制?
我正在用x86指令集编写一个程序。当我使用存储在40kb大小堆栈中的本地数组时,为什么会崩溃Assembly 堆栈大小的最大使用限制是否有任何限制?,assembly,x86,Assembly,X86,我正在用x86指令集编写一个程序。当我使用存储在40kb大小堆栈中的本地数组时,为什么会崩溃 使用Windows 5操作系统与I5处理器,编译在Visual C++快照版2008 < P>我认为你是在捕捉到“强”守护页< /强>的形式。 为了在实际使用之前不浪费实际内存,Windows最初保留完整的堆栈空间(默认为1MB;可以通过编辑PE头进行更改),但只提交两个页面,并将第二个页面作为保护页面。保护页是内存中的一页(4KB),它在任何对它的访问时都会触发一个特殊异常(STATUS\u gua
使用Windows 5操作系统与I5处理器,编译在Visual C++快照版2008
< P>我认为你是在捕捉到“强”守护页< /强>的形式。 为了在实际使用之前不浪费实际内存,Windows最初保留完整的堆栈空间(默认为1MB;可以通过编辑PE头进行更改),但只提交两个页面,并将第二个页面作为保护页面。保护页是内存中的一页(4KB),它在任何对它的访问时都会触发一个特殊异常(STATUS\u guard\u page\u invalization)。当内核检测到一个保护页异常时,它提交被触摸的页面并在其之后添加另一个保护页。这样,如果您的函数将小变量推送到堆栈上,它会“自行”增长 但是,如果您试图分配一个大小超过4K(4096字节)的局部变量,则会出现问题。通常,堆栈分配是通过从ESP中减去来完成的。如果从ESP中减去4K以上,然后尝试写入堆栈,则有可能跳过保护页,然后访问保留内存。这一个不会被内核捕获,但会被传递到您的程序,通常会导致崩溃 解决方案很简单——以4K(=4096=0x1000字节)的块进行堆栈分配,并在每个块之后触摸堆栈以触发保护页。MSVC编译器通过在使用超过4K的局部变量的函数开始处调用\uu 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
有关堆栈和保护页的更多详细信息,请参阅。对于堆栈上的一个本地数组来说,这不是一件好事,如果您想成为一名男子,请将其存储到其他地方,两个可以为您的问题提供一些附加信息(最重要的是平台)第三,没有默认限制,但有几种技术可以确定您有多少内存。IIRC大多数VC版本默认使用2MB堆栈大小。
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