News:

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

Main Menu

Increasing stack size using linker option /STACK:RESERVE,COMMIT

Started by jj2007, October 07, 2012, 08:32:22 PM

Previous topic - Next topic

jj2007

include \masm32\include\masm32rt.inc
.stack 1000000   ; no effect

.code
start:
   MsgBox 0, hex$(esp), "Stack:", MB_OK   ; always 0012FFC0
   exit

end start

Linker option is /STACK:1000000,1000000 /verbose

Whatever I try, esp is stuck at 0012FFC4 on entry. Where is the problem?
:(

hutch--


MichaelW

The default size is 1000000 bytes. If I specify 1000000,1000000 then the ESP value on entry, in decimal, is 1245124 (12FFC4h). If I specify 2000000, 2000000, then the value is 2293700.
Well Microsoft, here's another nice mess you've gotten us into.

jj2007

Thanks, the hex notation does the job: with /STACK:0x200000, I get effectively esp=0022FFC0

Vortex

Tested with polink on XP Sp3 :



include     \masm32\include\masm32rt.inc

.data

format1     db 'esp = %X',0

.data?

buffer      db 32 dup(?)

.code

start:

    invoke  wsprintf,ADDR buffer,ADDR format1,esp
    invoke  StdOut,ADDR buffer

    invoke  ExitProcess,0

END start


esp = 22FFC4

hutch--

Just be careful with the assumptions that a stack start address will remain constant in later OS versions, one of the techniques to defeat the virus idiot fringe is to randomise the stack address so that many of the stack exploits will not work.

sinsi

Using ASLR, you can randomise things.
E:\masm32>stack
esp  = 3FFE2C
hmod = 810000

E:\masm32>stack
esp  = 26F794
hmod = D80000

E:\masm32>stack
esp  = 30F9D8
hmod = 1330000

E:\masm32>stack
esp  = 1EFE4C
hmod = 1330000

E:\masm32>stack
esp  = 42F7FC
hmod = 1330000

E:\masm32>stack
esp  = 32FE24
hmod = 140000

E:\masm32>stack
esp  = 23F8DC
hmod = 12F0000

Needs link version 9 to be able to use the /DYNAMICBASE switch.
All those people that assume hmod is always 400000, this is why there is an API call for it.
Tá fuinneoga a haon déag níos fearr :biggrin:

japheth


VirtualQuery() can be used to get the stack base and size:


mainCRTStartup PROC c

local mbi:MEMORY_BASIC_INFORMATION

    invoke VirtualQuery, addr mbi, addr mbi, sizeof MEMORY_BASIC_INFORMATION
    .if ( eax )
        ;invoke printf, CStr("BaseAddress=%X",lf), mbi.BaseAddress
        ;invoke printf, CStr("RegionSize=%X",lf), mbi.RegionSize
        mov esi, mbi.AllocationBase
        invoke printf, CStr("stack base=%X",lf), esi
        lea edi, mbi
        and di, 0F000h
        .repeat
            add edi, 1000h
            invoke VirtualQuery, edi, addr mbi, sizeof MEMORY_BASIC_INFORMATION
        .until eax == 0 || esi != mbi.AllocationBase
        sub edi, esi
        invoke printf, CStr("stack size=%X",lf), edi
    .else
        invoke GetLastError
        invoke printf, CStr("VirtualQuery(%X) failed [%X]",lf), addr mbi, eax
    .endif
    invoke ExitProcess, 0

mainCRTStartup endp


to set the stack size from inside the program, use the .drectve section:


    option dotname
.drectve segment info
    db "-stack:0x1000000,0x1000 "
.drectve ends



jj2007

Quote from: japheth on October 08, 2012, 12:52:14 AM

to set the stack size from inside the program, use the .drectve section:


    option dotname
.drectve segment info
    db "-stack:0x1000000,0x1000 "
.drectve ends


Works but you need at least Masm 8.0 or JWasm. By the way, is drectve documented anywhere?

qWord

MREAL macros - when you need floating point arithmetic while assembling!


jj2007

Normally my proggies need little stack, but I wanted to test, just for fun, if you can load Windows.inc into an in-memory dialog edit control. The answer is simple: You can't, at least not on XP SP3...

Even if there is plenty of stack, DialogBoxIndirectParamW fails silently at exactly 508*1024 bytes.

Thanks for your help with the linker option syntax. .drectve is also a nice option, although in general I like being compatible to ML 6.15.

Below is my test case.

include \masm32\MasmBasic\MasmBasic.inc        ; download
include DlgDefine.asm   ; slightly modified version of MasmBasic DlgDefine macro
  Init
  DlgDefine "WinInc - attention, truncated at 508 kBytes", 0, 0, 500, 200
  DlgControl dcEdit, wCat$(FileRead$("windowsUC.inc")), ES_MULTILINE, 1, 1, 100.0, 100.0   ; any Unicode text file will do
  DlgShow
  Exit
end start

P.S.:
Quote from: sinsi on October 07, 2012, 11:29:17 PM
Using ASLR, you can randomise things.

Wouldn't that imply that you get a random usable stack size??

TouEnMasm



      .686                                      ; create 32 bit code
      .model flat, stdcall

      option casemap :none                      ; case sensitive

option dotname
.drectve  segment info
   db "-stack:0x1000000,0x3000 "
.drectve ends

something wrong on the syntax ?

Quote
pile.asm(10) : error A2008: syntax error : info
pile.asm(11) : error A2034: must be in segment block
pile.asm(12) : fatal error A1010: unmatched block nesting : .drectve
and without info
Quote
pile.obj : warning LNK4078: multiple '.drectve' sections found with different attributes (00000A00)

Help !





Fa is a musical note to play with CL

jj2007

Quote from: ToutEnMasm on October 08, 2012, 04:55:06 PM
pile.asm(10) : error A2008: syntax error : info

Help !

It's explained above, Yves. Just read my posts.

By the way, line 28 in DlgDefine.asm attached above needs a correction:

  .Repeat
   push 0
  .Until esp<36000h   ; 34000h is ok for XP but Win7-32 needs 36000h