flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > Suggestion to add stack touching code to PROC macros

Author
Thread Post new topic Reply to topic
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17716
Location: In your JS exploiting you and your system
revolution
If you have a procedure that requires a large stack space for local data storage you may get access violation exceptions generated at runtime. The stack is dynamically grown in a downward direction by the OS with the use of guard pages. However if the stack pointer is adjusted by a procedure entry with "sub esp,somevalue" the mechanism can fail when the stack pointer "jumps over" the guard page to unmapped memory and creates havoc with memory exceptions.

So here is my susgestion. Just a simple addition of 3 lines to the PROC32.INC and PROC64.INC files:
Code:
macro prologuedef procname,flag,parmbytes,localbytes,reglist
 { if parmbytes | localbytes
    push ebp
    mov ebp,esp
    if localbytes
 ;add the next three lines
     repeat localbytes shr 12
      mov byte[esp-%*4096],0
     end repeat
 ;------------------------
     sub esp,localbytes
    end if
   end if
   irps reg, reglist \{ push reg \} }    
For PROC64.INC the registers become RBP and RSP but still the same idea.

I think in the majority of cases this is efficient and adequate. The "repeat" will only generate the minimum required extra instructions, zero instructions in most cases. Even for moderately large stack adjustments only a few instructions are needed. If you are generating very large local stack areas, and thus the "repeat" loop iterates many times, then perhaps it would be better to use a more appropriate memory allocation function (VirtualAlloc or LocalAlloc) for your purpose but the code will still work if you want to use the stack.
Post 17 Apr 2006, 09:38
View user's profile Send private message Visit poster's website Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 7802
Location: Kraków, Poland
Tomasz Grysztar
Instead of modifying the prologuedef macro, it'd be better to make the macro called stprologue or something like that, and then enable it this way:
Code:
prologue@proc equ stprologue    

or with MASM.INC included:
Code:
option prologue:stprologue    
Post 18 Apr 2006, 14:13
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.