News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

More Advanced Prologue and Epilogue MACROses for PROCs

Started by Antariy, June 07, 2013, 11:00:04 PM

Previous topic - Next topic

jj2007

Quote from: ToutEnMasm on June 09, 2013, 04:57:16 AM
Reply to the question and you win.

Yves, the system reserves a megabyte or so anyway. It's just not committed, so you have to probe it (that's the main issue of this thread).

Of course, you can commit a gigabyte with /Stack, but for good reasons it's not considered good programming practice.

Gunther

Hi Alex,

the program crashed under Windows 7. Here is the rest of the output:


c:\scratch03>CrashStack.exe
=== Dump info for growing stack, 3 steps ===


============== Step #1 ==============

In printinfo1, ESP: 0018DFC8, stack bottom: 0018D000
VirtualQuery for ESP:
        BaseAddress:            0018D000
        AllocationBase:         00090000
        AllocationProtect:      00000004
        RegionSize:             00003000
        State:                  00001000
        Protect:                00000004
        Type:                   00020000

In printinfo1, ESP: 0018DFC8, stack bottom: 0018D000
VirtualQuery for the stack bottom:
        BaseAddress:            0018D000
        AllocationBase:         00090000
        AllocationProtect:      00000004
        RegionSize:             00003000
        State:                  00001000
        Protect:                00000004
        Type:                   00020000

In printinfo1, ESP: 0018DFC8, stack bottom: 0018D000
VirtualQuery for page step below bottom...:
        BaseAddress:            0018C000
        AllocationBase:         00090000
        AllocationProtect:      00000004
        RegionSize:             00001000
        State:                  00001000
        Protect:                00000104
        Type:                   00020000

In printinfo1, ESP: 0018DFC8, stack bottom: 0018D000
VirtualQuery for page step below bottom...:
        BaseAddress:            0018B000
        AllocationBase:         00090000
        AllocationProtect:      00000004
        RegionSize:             00001000
        State:                  00002000
        Protect:                00000000
        Type:                   00020000

In printinfo1, ESP: 0018DFC8, stack bottom: 0018D000
VirtualQuery for page step below bottom...:
        BaseAddress:            0018A000
        AllocationBase:         00090000
        AllocationProtect:      00000004
        RegionSize:             00002000
        State:                  00002000
        Protect:                00000000
        Type:                   00020000


============== Step #2 ==============

In printinfo1, ESP: 0018CFC8, stack bottom: 0018C000
VirtualQuery for ESP:
        BaseAddress:            0018C000
        AllocationBase:         00090000
        AllocationProtect:      00000004
        RegionSize:             00004000
        State:                  00001000
        Protect:                00000004
        Type:                   00020000

In printinfo1, ESP: 0018CFC8, stack bottom: 0018C000
VirtualQuery for the stack bottom:
        BaseAddress:            0018C000
        AllocationBase:         00090000
        AllocationProtect:      00000004
        RegionSize:             00004000
        State:                  00001000
        Protect:                00000004
        Type:                   00020000

In printinfo1, ESP: 0018CFC8, stack bottom: 0018C000
VirtualQuery for page step below bottom...:
        BaseAddress:            0018B000
        AllocationBase:         00090000
        AllocationProtect:      00000004
        RegionSize:             00001000
        State:                  00001000
        Protect:                00000104
        Type:                   00020000

In printinfo1, ESP: 0018CFC8, stack bottom: 0018C000
VirtualQuery for page step below bottom...:
        BaseAddress:            0018A000
        AllocationBase:         00090000
        AllocationProtect:      00000004
        RegionSize:             00001000
        State:                  00002000
        Protect:                00000000
        Type:                   00020000

In printinfo1, ESP: 0018CFC8, stack bottom: 0018C000
VirtualQuery for page step below bottom...:
        BaseAddress:            00189000
        AllocationBase:         00090000
        AllocationProtect:      00000004
        RegionSize:             00002000
        State:                  00002000
        Protect:                00000000
        Type:                   00020000


============== Step #3 ==============

In printinfo1, ESP: 0018BFC8, stack bottom: 0018B000
VirtualQuery for ESP:
        BaseAddress:            0018B000
        AllocationBase:         00090000
        AllocationProtect:      00000004
        RegionSize:             00005000
        State:                  00001000
        Protect:                00000004
        Type:                   00020000

In printinfo1, ESP: 0018BFC8, stack bottom: 0018B000
VirtualQuery for the stack bottom:
        BaseAddress:            0018B000
        AllocationBase:         00090000
        AllocationProtect:      00000004
        RegionSize:             00005000
        State:                  00001000
        Protect:                00000004
        Type:                   00020000

In printinfo1, ESP: 0018BFC8, stack bottom: 0018B000
VirtualQuery for page step below bottom...:
        BaseAddress:            0018A000
        AllocationBase:         00090000
        AllocationProtect:      00000004
        RegionSize:             00001000
        State:                  00001000
        Protect:                00000104
        Type:                   00020000

In printinfo1, ESP: 0018BFC8, stack bottom: 0018B000
VirtualQuery for page step below bottom...:
        BaseAddress:            00189000
        AllocationBase:         00090000
        AllocationProtect:      00000004
        RegionSize:             00001000
        State:                  00002000
        Protect:                00000000
        Type:                   00020000

In printinfo1, ESP: 0018BFC8, stack bottom: 0018B000
VirtualQuery for page step below bottom...:
        BaseAddress:            00188000
        AllocationBase:         00090000
        AllocationProtect:      00000004
        RegionSize:             00002000
        State:                  00002000
        Protect:                00000000
        Type:                   00020000



=== Crash - continuously access below guard page with increasing the step ===

Step size: -4096, ESP: 0018BFFC, Stack bottom: 0018B000


I hope that helps.

Gunther
You have to know the facts before you can distort them.

Antariy

Hi Gunther :t

Yes, the crash is intentional - after it dumps info about bottom of growing stack and its near area, the program intentionally causes a crash acessing a bit below over guard page. Your results shows that on Win7 the system strategy is the same - commited data area, then guard page, then uncommited area with no access.
Thank you very much :biggrin:

It would be also interesting to see what will it show on Win8, can I please ask some body to test it on that system?
It's curious if they had enlarged the guard area from one page to more pages in size on Win8.

sinsi

Windows 8 Pro x64

=== Dump info for growing stack, 3 steps ===


============== Step #1 ==============

In printinfo1, ESP: 0018CFC8, stack bottom: 0018C000
VirtualQuery for ESP:
        BaseAddress:            0018C000
        AllocationBase:         00090000
        AllocationProtect:      00000004
        RegionSize:             00004000
        State:                  00001000
        Protect:                00000004
        Type:                   00020000

In printinfo1, ESP: 0018CFC8, stack bottom: 0018B000
VirtualQuery for the stack bottom:
        BaseAddress:            0018B000
        AllocationBase:         00090000
        AllocationProtect:      00000004
        RegionSize:             00005000
        State:                  00001000
        Protect:                00000004
        Type:                   00020000

In printinfo1, ESP: 0018CFC8, stack bottom: 0018B000
VirtualQuery for page step below bottom...:
        BaseAddress:            0018A000
        AllocationBase:         00090000
        AllocationProtect:      00000004
        RegionSize:             00001000
        State:                  00001000
        Protect:                00000104
        Type:                   00020000

In printinfo1, ESP: 0018CFC8, stack bottom: 0018B000
VirtualQuery for page step below bottom...:
        BaseAddress:            00189000
        AllocationBase:         00090000
        AllocationProtect:      00000004
        RegionSize:             00002000
        State:                  00001000
        Protect:                00000104
        Type:                   00020000

In printinfo1, ESP: 0018CFC8, stack bottom: 0018B000
VirtualQuery for page step below bottom...:
        BaseAddress:            00188000
        AllocationBase:         00090000
        AllocationProtect:      00000004
        RegionSize:             00001000
        State:                  00002000
        Protect:                00000000
        Type:                   00020000


============== Step #2 ==============

In printinfo1, ESP: 0018AFC8, stack bottom: 0018A000
VirtualQuery for ESP:
        BaseAddress:            0018A000
        AllocationBase:         00090000
        AllocationProtect:      00000004
        RegionSize:             00006000
        State:                  00001000
        Protect:                00000004
        Type:                   00020000

In printinfo1, ESP: 0018AFC8, stack bottom: 00189000
VirtualQuery for the stack bottom:
        BaseAddress:            00189000
        AllocationBase:         00090000
        AllocationProtect:      00000004
        RegionSize:             00007000
        State:                  00001000
        Protect:                00000004
        Type:                   00020000

In printinfo1, ESP: 0018AFC8, stack bottom: 00189000
VirtualQuery for page step below bottom...:
        BaseAddress:            00188000
        AllocationBase:         00090000
        AllocationProtect:      00000004
        RegionSize:             00001000
        State:                  00001000
        Protect:                00000104
        Type:                   00020000

In printinfo1, ESP: 0018AFC8, stack bottom: 00189000
VirtualQuery for page step below bottom...:
        BaseAddress:            00187000
        AllocationBase:         00090000
        AllocationProtect:      00000004
        RegionSize:             00002000
        State:                  00001000
        Protect:                00000104
        Type:                   00020000

In printinfo1, ESP: 0018AFC8, stack bottom: 00189000
VirtualQuery for page step below bottom...:
        BaseAddress:            00186000
        AllocationBase:         00090000
        AllocationProtect:      00000004
        RegionSize:             00001000
        State:                  00002000
        Protect:                00000000
        Type:                   00020000


============== Step #3 ==============

In printinfo1, ESP: 00188FC8, stack bottom: 00188000
VirtualQuery for ESP:
        BaseAddress:            00188000
        AllocationBase:         00090000
        AllocationProtect:      00000004
        RegionSize:             00008000
        State:                  00001000
        Protect:                00000004
        Type:                   00020000

In printinfo1, ESP: 00188FC8, stack bottom: 00187000
VirtualQuery for the stack bottom:
        BaseAddress:            00187000
        AllocationBase:         00090000
        AllocationProtect:      00000004
        RegionSize:             00009000
        State:                  00001000
        Protect:                00000004
        Type:                   00020000

In printinfo1, ESP: 00188FC8, stack bottom: 00187000
VirtualQuery for page step below bottom...:
        BaseAddress:            00186000
        AllocationBase:         00090000
        AllocationProtect:      00000004
        RegionSize:             00001000
        State:                  00001000
        Protect:                00000104
        Type:                   00020000

In printinfo1, ESP: 00188FC8, stack bottom: 00187000
VirtualQuery for page step below bottom...:
        BaseAddress:            00185000
        AllocationBase:         00090000
        AllocationProtect:      00000004
        RegionSize:             00002000
        State:                  00001000
        Protect:                00000104
        Type:                   00020000

In printinfo1, ESP: 00188FC8, stack bottom: 00187000
VirtualQuery for page step below bottom...:
        BaseAddress:            00184000
        AllocationBase:         00090000
        AllocationProtect:      00000004
        RegionSize:             00001000
        State:                  00002000
        Protect:                00000000
        Type:                   00020000



=== Crash - continuously access below guard page with increasing the step ===

Step size: -4096, ESP: 00188FFC, Stack bottom: 00187000
Step size: -8192, ESP: 00188FFC, Stack bottom: 00185000
🍺🍺🍺

Antariy

Hi John :t

Thank you very much!
So, it seems they really had enlarged the guard area - probably for some compatibility reason (actually even on not so modern systems, like WinXP is, there was some problems with "old software", and compatibility fix related to a stack; but it seems that there are probably exist much of handcrafted proggies of early era that have not cared about stack probing - so probably MS decided to increase the compatibility with older software in that extent extending the guard area).

Antariy

...and now anyone may merry roll nice procs with up to 8 KB of locals and don't bother with anything (under Win8). Well, that's how the conniving of programmer's lazyness and careless growing on your eyes :lol: Well, these days the "common" RAM size increasing with such speed, that it seems they will soon commit full stack size - less underlying details required from programmer to know, less problems to an OS vendor :greensml:

This is post of humor. Humor??? :biggrin:

TouEnMasm


Here a piece of code to made controls in access memory.The sample is adapted to the stack need.


.const
SAFEMEM MACRO adress:REQ
mov keepesp,esp ;preserve esp
mov esp,SafeStackPlace ;usable stack
invoke Good_Adress,addr adress
.if eax == 1
mov esp,keepesp
.else
mov eax,@Line   ;return the line of the error (GetExitCodeProcess)
invoke ExitProcess,eax
.endif
ENDM

.data
SafeStackPlace  dd 0
keepesp dd 0

.code
;--------- at the start of the executable --------------------
mov SafeStackPlace,esp
sub esp,100h                           ;reserve good stack to allow the use of VirtualQuery
;---------- the proc ------------------------------------------
;0 inaccessible
;1 accès autorisé
;################################################################
Good_Adress PROC pchain:DWORD
Local mem_basic_inf:MEMORY_BASIC_INFORMATION
Local  retour:DWORD
      mov retour,0
mov edx,pchain
invoke VirtualQuery,pchain,addr mem_basic_inf,sizeof mem_basic_inf
.if eax != 0
mov eax,mem_basic_inf.AllocationProtect
.if eax == 0
jmp fin
.endif
;------- general access conditions ---------
test eax,PAGE_NOACCESS
jnz fin
test eax,PAGE_EXECUTE
jnz fin
test eax,PAGE_GUARD
jnz fin
;------------ verify for stack ------------
mov eax,mem_basic_inf.State
test eax,MEM_COMMIT
jz fin
mov retour,1
.endif 
fin:
FindeGood_Adress:
         mov eax,retour
         ret
Good_Adress endp

;--------------------- and now use it --------------------------------------------
;################################################################
Big_stack_proc PROC
Local bigone[0ff2h]:DWORD ;The system need to add more than one page to the stack
Local retour:DWORD
SAFEMEM retour
invoke MessageBox,NULL,ADDR All_is_good,addr titre,MB_OK
FindeBig_stack_proc:
         mov eax,retour
         ret
Big_stack_proc endp


Fa is a musical note to play with CL

farrier

This was first experience with a large set of local variables that caused a GPF.  The best solution from all the suggestions was:

MainDlgProc proc hWin:HWND,uMsg:UINT,wParam:WPARAM,lPar am:LPARAM
  LOCAL buffer[4096]:byte
  LOCAL buffer2[256]:byte
  LOCAL buffer3[256]:byte
  LOCAL printout[4096]:byte
   
  LOCAL pos:dword
  LOCAL hdi:HD_ITEM

  ;Touching the stack frame
  mov eax,ebp
  .while eax>esp
     mov dword ptr [eax],0
     sub eax, 4096
  .endw
  push edx
  push esi
  push edi


The following links describe the problem:

http://www.masmforum.com/board/index.php?topic=2011.0

http://www.asmcommunity.net/board/index.php?topic=19497.msg150610#msg150610

http://www.asmcommunity.net/board/index.php?topic=13525.0

My problem showed in WIN95 but not in most others.

hth,

farrier

TouEnMasm


I agree with you,1121_StackProbe.zip (archive 2) answer the question  :t
Fa is a musical note to play with CL

Antariy

Hi farrier, yes, your example is good and your solution is right - and is in the stack probing.
As for not crashing on other systems - that depends on much of things, and if that program will have more or less complex recursion - it may crash. As a recursion here maybe SendMessage from this dialog proc or any proc it calls, or other type of deep callback (like MessageBox displaying or creating the child dialog etc etc). So, the probing is the best solution to make robust code.

But the one of points of this thread is that the macroses set - the thing this thread is about - implements the stack probing automatically, if the size of locals is indicating need in this.

But Yves' messages here, especially after the one that is just above this post, looks just as pure paradox.


And, remember that the thread "main point" was not about stack probing, but about stack balancing - that's where it started from :biggrin: This macroses set, even in the state as it currently is, may be used as a simple tool to detect a stack imbalancing when program is developed. Besides of that the same feature may be used to detect buffer overruns. Though it all was described better in the first post of the thread.

Gunther

Hi Alex,

Quote from: Antariy on June 09, 2013, 08:42:30 PM
And, remember that the thread "main point" was not about stack probing, but about stack balancing - that's where it started from :biggrin: This macroses set, even in the state as it currently is, may be used as a simple tool to detect a stack imbalancing when program is developed. Besides of that the same feature may be used to detect buffer overruns. Though it all was described better in the first post of the thread.

that's very clear and your goals are well described in the first post.  :t

Gunther
You have to know the facts before you can distort them.