2012-02-09 4 views
2

스택 크기 제한에 도달하면 프로세서가 운영 체제에 트랩을 발생시키지 않습니다 (따라서 스택 오버 플로우가 발생하지 않음 : P)프로그램 스택이 실제로 오버플로됩니까?

+0

어떤 프로세서 :

이 코드는 내가 여기를 게시 할 수있는 오픈 소스이기 때문에

? 그 OS? OS가 있다면? 이것과 같은 일반적인 질문은 답이 될 수 있습니다. 예, 스택이 오버플로 될 수 있습니다. – Ernstsson

답변

1

Windows가 끝날 때마다 커지기 시작합니다.

Visual Studio 컴파일러에서이 문제를 담당하는 코드는 chkstk.obj 모듈에 있습니다.

;*** 
;_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 

     end 
+0

감사합니다. 보호 페이지는 스택 오버플로가 발생할 때 SP가 유입되는 곳입니다. OS가이를 감지하고 더 많은 페이지를 할당합니까? 또한 스택에 필요한 메모리의 각 페이지를 내림차순으로 검색합니다. 음 ... 내림차순으로? –

관련 문제