Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Stack Alignment Fault with VC6

Status
Not open for further replies.

iScientist

Programmer
Jan 24, 2003
13
GB
I am receiving a WSAEFAULT error from a socket send. When i trace into the socket call a sysenter instruction (fast kernel call) is returning C00002C5 (DATA ALIGNMENT FAULT).
From this I figure the problem is with alignment of stack.
The stack backtrace to the socket call involves some of our assembler code. If I remove a single push instruction the problem goes away (more confirmation of stack aligment problem). Question is how do I align the stack to prevent this from happening.

many thanks for any help given
 
Were you trying to push a byte, a word, or a long?

> Question is how do I align the stack to prevent this from happening.
All through C and C++ code, the compiler will generate code which preserves the alignment of the stack. Look at the generated asm for a function prologue to see how its done, then do something similar.


--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 
Yes but the socket send() call that fails is called from a C function which is called from assembler, so why isnt the stack aligned by the C function that calls it? Manually aligning the stack by anding esp with 0xffffff8 solves the problem but I dont understand why exactly. Why isnt the stack aligned by the C functions the host the socket call ?

any ideas
 
C preserves the alignment, it doesn't fix it if you break it.


--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 
So remains the question, if the stack is not align before the C calls the how do you align the stack ?
 
Add some void push'es inside of asm{} group before the call to send() and the same number of pop's after it. Number of them could be calculated depending on value of eps. Or do it as you have done - clear lower bits of esp and restore them after return. Clearing lower bits decrements the value of the pointer - so everything seems ok. What are your suspicions about it actually?
 
Thanks mingis, I was hopefully looking for an assembler directive that would do this for me. Failing that can any one point me to an example in assembler that I can intergrate into our code. I need a generic solution as there are quite a few places in our asm code where this problem occours. The asm is Masm 6.11, Thanks all.
 
Thanks to all who contrib'ed, problem solved. Yes it was a stack alignment problem, someone had added a push and pop of a word sized variable around a call to a C function, like this

PUSH Word Ptr ERROR
CALL FUNC
POP Word Ptr Error

this word-sized push mis-aligned the stack. The solution was to replace the above code with 32Bit push/pops ie

MOVZX EAX, Word Ptr ERROR
PUSH EAX
CALL FUNC
POP EAX
MOV Word Ptr Error, AX

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top