News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Retrieve available stack space

Started by Eddy, May 04, 2005, 02:10:18 PM

Previous topic - Next topic

pbrennick

Eddy,
Looks like you got it right, have fun and keep us informed as you are doing some very boundary type stuff and it will be very informative to know what you are learning.  :clap:

Paul

MichaelW

Quote from: roticv on May 05, 2005, 03:56:08 PM
fs:[4h] and fs:[8h] are just the limits of the stack - ie if the stack address goes above or below it, there would be a nasty error (or rather autoexitingorprocess).

Would that be the reason why my test ran 8 recursions with an allocation size of 1024 bytes before fs:[8h] changed? Am I correct in assuming that an allocation size of 8192 bytes triggered termination on the first recursion because it caused an access beyond the 'guard' page?



eschew obfuscation

Eddy

Michael,

What you see when running your program is the following: When your app starts, Windows apparently has committed 2  4kB stack pages for your app, of a maximum of 1 MB.
Since your routine needs about 1kB (1032 bytes) for every recursion, you can do 8 recursions using up the initial stack size of 8kB. After that, Windows has to increase the currently committed stack size and adds another page of 4kB. You can see this because fs:[8h] is decremented by 4kB.
After that, fs:[8h] is decremented every 4 recursions because in 4 recursions you use up the newly added stack page of 4kB.
This goes on and on until the totally committed stack size has reached the (default) limit of 1MB. When this happens and your app still demands more stack space, Windows terminates your app..

You can change the default stack size of 1MB at compile time (or during linking).

One interesting thing: When I run your app on Windows 98, fs:[8h] already decreases after the first 4 recursions! Apparently, Win98 already increases the committed stack space when the last stack page is beginning to being used, while XP first uses up that entire last page.

Kind regards
Eddy


Eddy
www.devotechs.com -- HIME : Huge Integer Math and Encryption library--

QvasiModo

Quote from: Eddy on May 06, 2005, 07:32:57 PM
One interesting thing: When I run your app on Windows 98, fs:[8h] already decreases after the first 4 recursions! Apparently, Win98 already increases the committed stack space when the last stack page is beginning to being used, while XP first uses up that entire last page.

Interesting indeed! :eek I wonder how's that done with guard pages?

Or maybe some API is using more stack on Win98 than on XP, so it "touches" the next page before the app does? (just guessing here).

MichaelW

I corrected some problems in my app and modified it to take a command line argument that specifies the amount of stack space to consume per recursion (in bytes). With no command line it defaults to 1024 bytes per recursion. After an hour of head scratching and experimenting I could not set the stack reserve or commit values using LINK. Regardless of what I tried the reserve and commit values remained at the defaults, and when I tried running the result Windows 2000 would terminate it, after displaying a non-specific error message (I did not bother to determine exactly what the error was). I finally decided to try EDITBIN, and it worked just as described in the documentation.

; ««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    .486                       ; create 32 bit code
    .model flat, stdcall       ; 32 bit memory model
    option casemap :none       ; case sensitive

    include \masm32\include\windows.inc
    include \masm32\include\masm32.inc
    include \masm32\include\kernel32.inc
    includelib \masm32\lib\masm32.lib
    includelib \masm32\lib\kernel32.lib
    include \masm32\macros\macros.asm

; ««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    .data
        start_esp dd 0
        bite_size dd 0
        argBuffer db 128 dup (0)
    .code
; ««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
start:
; ««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    invoke GetCL,1,ADDR argBuffer
    invoke atodw,ADDR argBuffer

    ; GetCL returns 0 for no command line ??
    .IF eax == 0
      mov   bite_size,1024
    .ELSE
      mov   bite_size,eax
    .ENDIF
    print chr$("bite_size = ")
    print ustr$(bite_size)
    print chr$(" bytes",13,10)

    mov   start_esp,esp
    print chr$("start_esp = ")
    print ustr$(start_esp)
    print chr$(13,10,13,10)

    jmp   eatstack

    mov   eax,input(13,10,"Press enter to exit...")
    exit

OPTION PROLOGUE:NONE
OPTION EPILOGUE:NONE

eatstack proc
    sub   esp,bite_size

    assume fs:nothing

    mov   ebx,start_esp
    sub   ebx,esp
    print chr$("start_esp - esp = ")
    print ustr$(ebx)
    print chr$(" bytes",13,10)
    mov   ebx,4
    mov   ebx,fs:[ebx]
    print chr$("fs:[4] = ")
    print ustr$(ebx)
    print chr$(", fs:[8] = ")
    mov   esi,8
    mov   esi,fs:[esi]
    print ustr$(esi)
    sub   ebx,esi
    print chr$(", fs:[4] - fs:[8] = ")
    print ustr$(ebx)
    mov   eax,input(" bytes",13,10,"Press enter to continue...",13,10)
    jmp   eatstack
eatstack endp

OPTION PROLOGUE:PrologueDef
OPTION EPILOGUE:EpilogueDef
; ««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
end start



[attachment deleted by admin]
eschew obfuscation