The MASM Forum

General => The Campus => Topic started by: jj2007 on October 07, 2012, 08:32:22 PM

Title: Increasing stack size using linker option /STACK:RESERVE,COMMIT
Post by: jj2007 on October 07, 2012, 08:32:22 PM
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?
:(
Title: Re: Increasing stack size using linker option /STACK:RESERVE,COMMIT
Post by: hutch-- on October 07, 2012, 09:20:46 PM
JJ,

Do the size in C notation HEX. 0xDEADBEEF
Title: Re: Increasing stack size using linker option /STACK:RESERVE,COMMIT
Post by: MichaelW on October 07, 2012, 09:46:13 PM
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.
Title: Re: Increasing stack size using linker option /STACK:RESERVE,COMMIT
Post by: jj2007 on October 07, 2012, 09:55:47 PM
Thanks, the hex notation does the job: with /STACK:0x200000, I get effectively esp=0022FFC0
Title: Re: Increasing stack size using linker option /STACK:RESERVE,COMMIT
Post by: Vortex on October 07, 2012, 10:32:43 PM
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
Title: Re: Increasing stack size using linker option /STACK:RESERVE,COMMIT
Post by: hutch-- on October 07, 2012, 10:41:26 PM
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.
Title: Re: Increasing stack size using linker option /STACK:RESERVE,COMMIT
Post by: sinsi on October 07, 2012, 11:29:17 PM
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.
Title: Re: Increasing stack size using linker option /STACK:RESERVE,COMMIT
Post by: japheth on October 08, 2012, 12:52:14 AM

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


Title: Re: Increasing stack size using linker option /STACK:RESERVE,COMMIT
Post by: jj2007 on October 08, 2012, 03:19:08 AM
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?
Title: Re: Increasing stack size using linker option /STACK:RESERVE,COMMIT
Post by: qWord on October 08, 2012, 03:29:53 AM
Quote from: jj2007 on October 08, 2012, 03:19:08 AMBy the way, is drectve documented anywhere?
yes, in Microsoft's PE and COFF Specification.
Title: Re: Increasing stack size using linker option /STACK:RESERVE,COMMIT
Post by: TouEnMasm on October 08, 2012, 03:31:54 AM

You can also do it at run time:
http://www.asmfr.com/codes/ALLOCATION-DYNAMIQUE-MEMOIRE-PILE-DANS-MASM_21789.aspx (http://www.asmfr.com/codes/ALLOCATION-DYNAMIQUE-MEMOIRE-PILE-DANS-MASM_21789.aspx)
Title: Re: Increasing stack size using linker option /STACK:RESERVE,COMMIT
Post by: jj2007 on October 08, 2012, 09:45:26 AM
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 (http://masm32.com/board/index.php?topic=94.0)
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??
Title: Re: Increasing stack size using linker option /STACK:RESERVE,COMMIT
Post by: TouEnMasm on October 08, 2012, 04:55:06 PM


      .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 !





Title: Re: Increasing stack size using linker option /STACK:RESERVE,COMMIT
Post by: jj2007 on October 08, 2012, 05:31:24 PM
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