AMD64: stack alignment

Claus Assmann <[email protected]> Tue, 17 Dec 2013 20:29:22 -0800
Newsgroups gmane.comp.lib.state-threads.devel
Message-ID <[email protected]>
--HcAYCG3uE/tztfnV
Content-Type: text/plain; charset=unknown-8bit
Content-Disposition: inline
Content-Transfer-Encoding: 8bit

It seems the stack alignment on AMD64 (BSD) is broken.  My program
crashes when linked with OpenSSL 1.0.1e due to a misaligned stack
(see below).
According to the AMD64 ABI %rsp must be congruent 8 modulo 16
on function entry.

I'm attaching a hack to deal with this, but it needs to be reviewed
and maybe fixed? It fixes my test program, but that's of course not
sufficient as "proof" that this is correct.

Breakpoint 5, sha1_block_data_order_ssse3 () at sha1-x86_64.s:1295
1295    pushq   %rbx
Current language:  auto; currently asm
(gdb) p $rsp
$15 = (void *) 0x8007360a0

This is wrong and causes a crash here:

1328    movdqa  %xmm0,0(%rsp)
(gdb) n

Program received signal SIGBUS, Bus error.
sha1_block_data_order_ssse3 () at sha1-x86_64.s:1328

(gdb) where
#0  sha1_block_data_order_ssse3 () at sha1-x86_64.s:1295
#1  0x000000000045bc68 in SHA1_Final (md=0x8007361d8 "Øu÷¸;¤¿Pqs\224­½0sÜ¢·ñþ", c=0x800c39240) at md32_common.h:372
#2  0x0000000000487bb4 in final (ctx=0x8007361a8, md=0x8007361d8 "Øu÷¸;¤¿Pqs\224­½0sÜ¢·ñþ") at m_sha1.c:81
#3  0x00000000004800ec in EVP_DigestFinal_ex (ctx=0x8007361a8, md=0x8007361d8 "Øu÷¸;¤¿Pqs\224­½0sÜ¢·ñþ", size=0x0)
    at digest.c:272
#4  0x000000000050e089 in ssleay_rand_add (buf=0x8007362a0, num=8, add=0) at md_rand.c:289
#5  0x000000000047da97 in RAND_add (buf=0x8007362a0, num=8, entropy=0) at rand_lib.c:157
#6  0x000000000040de2b in ssl23_accept (s=0x800c49c00) at s23_srvr.c:155
#7  0x000000000041a384 in SSL_accept (s=0x800c49c00) at ssl_lib.c:940
#8  0x000000000040b2dd in do_tls_operation (fp=<value optimized out>, hsfunc=0x41a350 <SSL_accept>, rfunc=0, wfunc=0, 
    buf=0x0, num=0, bytes=0x8007384a8) at ../../mta/libmta/tlsbio.c:218
#9  0x0000000000405394 in handle_session (srv_socket_index=<value optimized out>, cli_nfd=0x800c06ac0)
    at ../../../mta/statethreads/examples/smtps2.c:2904
#10 0x0000000000408c43 in handle_connections (arg=0x0) at ../../../mta/statethreads/examples/smtps2.c:1778
#11 0x000000000056e38f in _st_thread_main () at ../../mta/statethreads/sched.c:329
#12 0x000000000056e473 in st_thread_create (start=0x4089e0 <handle_connections>, arg=0x0, joinable=0, 
    stk_size=<value optimized out>) at ../../mta/statethreads/sched.c:593
#13 0x0000000000000000 in ?? ()


--HcAYCG3uE/tztfnV
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="st.amd64"

Index: statethreads/md.h
===================================================================
RCS file: /home/ca/cvs/mta/statethreads/md.h,v
retrieving revision 1.18
diff -u -r1.18 md.h
--- statethreads/md.h	23 Nov 2013 13:51:02 -0000	1.18
+++ statethreads/md.h	18 Dec 2013 04:00:05 -0000
@@ -165,6 +165,7 @@
 
 #if defined(__i386__) || defined(__amd64__)
 #define MD_JB_SP   2
+#define MD_SP_OFF  8
 #elif defined(__sparc__)
 #define MD_JB_SP   0
 #elif defined(__alpha__)
@@ -562,6 +563,7 @@
 #define MD_JB_SP   0
 #elif defined(__amd64__)
 #define MD_JB_SP   6
+#define MD_SP_OFF  8
 #else
 #error Unknown CPU architecture
 #endif
@@ -659,6 +661,10 @@
 
 #ifndef MD_CAP_STACK
 #define MD_CAP_STACK(var_addr)
+#endif
+
+#ifndef MD_SP_OFF
+#define MD_SP_OFF  0
 #endif
 
 #endif /* !__ST_MD_H__ */
Index: statethreads/sched.c
===================================================================
RCS file: /home/ca/cvs/mta/statethreads/sched.c,v
retrieving revision 1.8
diff -u -r1.8 sched.c
--- statethreads/sched.c	27 Apr 2013 04:31:41 -0000	1.8
+++ statethreads/sched.c	18 Dec 2013 04:00:29 -0000
@@ -563,7 +563,7 @@
 
   /* Make stack 64-byte aligned */
   if ((unsigned long)sp & 0x3f)
-    sp = sp - ((unsigned long)sp & 0x3f);
+    sp = sp - ((unsigned long)sp & 0x3f) - MD_SP_OFF;
   stack->sp = sp - _ST_STACK_PAD_SIZE;
 #elif defined (MD_STACK_GROWS_UP)
   sp = stack->stk_bottom;

--HcAYCG3uE/tztfnV
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

------------------------------------------------------------------------------
Rapidly troubleshoot problems before they affect your business. Most IT 
organizations don't have a clear picture of how application performance 
affects their revenue. With AppDynamics, you get 100% visibility into your 
Java,.NET, & PHP application. Start your 15-day FREE TRIAL of AppDynamics Pro!
http://pubads.g.doubleclick.net/gampad/clk?id=84349831&iu=/4140/ostg.clktrk
--HcAYCG3uE/tztfnV
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
State-threads-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/state-threads-devel

--HcAYCG3uE/tztfnV--