flat assembler
Message board for the users of flat assembler.

Index > Windows > Misaligned Stack

Author
Thread Post new topic Reply to topic
comrade



Joined: 16 Jun 2003
Posts: 1150
Location: Russian Federation
comrade 25 Jun 2003, 05:20
This is not a FASM issue, but I was just wondering about this:
Code:
proc getMX,.szDomain,.lpServers,.dwDepth
        .pref   dw ?
        enter
        push    ebx esi edi
        stdcall [socket],PF_INET,SOCK_DGRAM,0
        cmp     eax,INVALID_SOCKET
        je      .err    


socket() will fail and return INVALID_SOCKET (-1), even though all parameters to it are correct. If we add:
Code:
proc getMX,.szDomain,.lpServers,.dwDepth
        .pref   dw ?
        rb 02h
        enter
        push    ebx esi edi
        stdcall [socket],PF_INET,SOCK_DGRAM,0
        cmp     eax,INVALID_SOCKET
        je      .err    

socket() will work just fine, and return a new valid socket handle.

I've noticed that first one allocates 2 bytes off the stack, causing it to misalign (not aligned on dword boundary anymore) and socket() to fail for some reason? Those extra 2 bytes helped.
Why does this happen?

_________________
comrade (comrade64@live.com; http://comrade.ownz.com/)
Post 25 Jun 2003, 05:20
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger ICQ Number Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8359
Location: Kraków, Poland
Tomasz Grysztar 25 Jun 2003, 08:24
Stack should be always aligned to 4 in Win32, and misalignment often causes some of system functions to fail - I had the same problem in the earlier versions of fasm, which were ported from DOS, where no stack doesn't need to be aligned.
Maybe we should add some alignment to the "enter" macro, so it'll be automatically avoided?
Post 25 Jun 2003, 08:24
View user's profile Send private message Visit poster's website Reply with quote
roticv



Joined: 19 Jun 2003
Posts: 374
Location: Singapore
roticv 25 Jun 2003, 09:09
like
Code:
add esp, 3
and esp, -4
    

?
Post 25 Jun 2003, 09:09
View user's profile Send private message Visit poster's website MSN Messenger Reply with quote
scientica
Retired moderator


Joined: 16 Jun 2003
Posts: 689
Location: Linköping, Sweden
scientica 25 Jun 2003, 14:50
privalov wrote:
Stack should be always aligned to 4 in Win32, and misalignment often causes some of system functions to fail - I had the same problem in the earlier versions of fasm, which were ported from DOS, where no stack doesn't need to be aligned.
Maybe we should add some alignment to the "enter" macro, so it'll be automatically avoided?

I think it should be implented by default in the enter macro, for win32, I don't if the Penguins get mad in linux if the plates are disaligned (=I don't know how linux treat the issue Rolling Eyes ), enven thought I think the stack should be aligned on a dword boundary (for all 32-OSes, qword aligned fo 64-bits, and 16-bit for those systems who only uses 16-bit), it's kinda like a pile of plates, I don't fancy the idea of a tilting high pile of plates Wink

btw, the WinNT systems are especially affected by disaligned stacks iirc.

_________________
... a professor saying: "use this proprietary software to learn computer science" is the same as English professor handing you a copy of Shakespeare and saying: "use this book to learn Shakespeare without opening the book itself.
- Bradley Kuhn
Post 25 Jun 2003, 14:50
View user's profile Send private message Visit poster's website Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8359
Location: Kraków, Poland
Tomasz Grysztar 25 Jun 2003, 15:02
It should be something like:
Code:
macro enter
 { rb (4 - ($-dynamic_data) and 11b) and 11b
   dynamic_size = $ - dynamic_data
   end virtual
   enter dynamic_size,0 }    
Post 25 Jun 2003, 15:02
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-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.