Author Topic: Increasing stack size using linker option /STACK:RESERVE,COMMIT  (Read 8926 times)

jj2007

  • Member
  • *****
  • Posts: 9462
  • Assembler is fun ;-)
    • MasmBasic
Increasing stack size using linker option /STACK:RESERVE,COMMIT
« 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?
 :(

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 6331
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: Increasing stack size using linker option /STACK:RESERVE,COMMIT
« Reply #1 on: October 07, 2012, 09:20:46 PM »
JJ,

Do the size in C notation HEX. 0xDEADBEEF
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy:

MichaelW

  • Global Moderator
  • Member
  • *****
  • Posts: 1209
Re: Increasing stack size using linker option /STACK:RESERVE,COMMIT
« Reply #2 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.
Well Microsoft, here’s another nice mess you’ve gotten us into.

jj2007

  • Member
  • *****
  • Posts: 9462
  • Assembler is fun ;-)
    • MasmBasic
Re: Increasing stack size using linker option /STACK:RESERVE,COMMIT
« Reply #3 on: October 07, 2012, 09:55:47 PM »
Thanks, the hex notation does the job: with /STACK:0x200000, I get effectively esp=0022FFC0

Vortex

  • Member
  • *****
  • Posts: 1962
Re: Increasing stack size using linker option /STACK:RESERVE,COMMIT
« Reply #4 on: October 07, 2012, 10:32:43 PM »
Tested with polink on XP Sp3 :

Code: [Select]

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

Code: [Select]
esp = 22FFC4

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 6331
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: Increasing stack size using linker option /STACK:RESERVE,COMMIT
« Reply #5 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.
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy:

sinsi

  • Member
  • *****
  • Posts: 1148
Re: Increasing stack size using linker option /STACK:RESERVE,COMMIT
« Reply #6 on: October 07, 2012, 11:29:17 PM »
Using ASLR, you can randomise things.
Code: [Select]
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.
I can walk on water but stagger on beer bourbon.

japheth

  • Guest
Re: Increasing stack size using linker option /STACK:RESERVE,COMMIT
« Reply #7 on: October 08, 2012, 12:52:14 AM »

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

Code: [Select]
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:

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


jj2007

  • Member
  • *****
  • Posts: 9462
  • Assembler is fun ;-)
    • MasmBasic
Re: Increasing stack size using linker option /STACK:RESERVE,COMMIT
« Reply #8 on: October 08, 2012, 03:19:08 AM »

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

Code: [Select]
    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

  • Member
  • *****
  • Posts: 1473
  • The base type of a type is the type itself
    • SmplMath macros
Re: Increasing stack size using linker option /STACK:RESERVE,COMMIT
« Reply #9 on: October 08, 2012, 03:29:53 AM »
By the way, is drectve documented anywhere?
yes, in Microsoft's PE and COFF Specification.
MREAL macros - when you need floating point arithmetic while assembling!

ToutEnMasm

  • Member
  • *****
  • Posts: 1189
    • EditMasm
Fa is a musical note to play with CL

jj2007

  • Member
  • *****
  • Posts: 9462
  • Assembler is fun ;-)
    • MasmBasic
Re: Increasing stack size using linker option /STACK:RESERVE,COMMIT
« Reply #11 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
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.:
Using ASLR, you can randomise things.

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

ToutEnMasm

  • Member
  • *****
  • Posts: 1189
    • EditMasm
Re: Increasing stack size using linker option /STACK:RESERVE,COMMIT
« Reply #12 on: October 08, 2012, 04:55:06 PM »

Code: [Select]
      .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

  • Member
  • *****
  • Posts: 9462
  • Assembler is fun ;-)
    • MasmBasic
Re: Increasing stack size using linker option /STACK:RESERVE,COMMIT
« Reply #13 on: October 08, 2012, 05:31:24 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