*BSD amd64 stack alignment issue revisited

Anton Berezin <[email protected]> Tue, 16 Dec 2014 23:24:36 +0100
Newsgroups gmane.comp.lib.state-threads.devel
Message-ID <[email protected]>
Hello,

According to this list archives, about a year ago, there was a discussion
about stack alignment issues on *BSD/amd64.

I can confirm that the issue is real, I was bitten by the same problem on
FreeBSD 10.1.

My own fix is somewhat simpler, but essentially accomplishes the same thing,
namely making sure that the alignment is on *odd* 8-byte boundary.  Perhaps
the comment I included with my patch would clarify the matter enough to lead
to the acceptance of the patch, since FreeBSD's own makecontext(3) function
ensures the same conditions.

http://www.freebsd.org/cgi/man.cgi?query=makecontext&sektion=3&manpath=FreeBSD+10.1-RELEASE

The patch is against release 1.9.

--- md.h	2009-10-01 20:46:43.000000000 +0200
+++ md.h	2014-12-16 22:52:34.000000000 +0100
@@ -161,6 +161,19 @@
 #define MD_JB_SP  34
 #elif defined(__amd64__)
 #define MD_JB_SP   2
+/*
+ * The following comment is taken from src/lib/libc/amd64/gen/makecontext.c
+ * It explains why we specifically break the alignment to 64 byte boundary
+ * that is unconditionally enforced by sched.c, by setting MD_STACK_PAD_SIZE
+ * to this funky value.
+ * Without this, anything using va_start() & friends fails unpredictably.
+ */
+        /*
+         * Account for arguments on stack and do the funky C entry alignment.
+         * This means that we need an 8-byte-odd alignment since the ABI expects
+         * the return address to be pushed, thus breaking the 16 byte alignment.
+         */
+#define MD_STACK_PAD_SIZE 8
 #else
 #error Unknown CPU architecture
 #endif

Cheers,
\Anton.
-- 
Our society can survive even a large amount of irrational regulation.
  -- John McCarthy

------------------------------------------------------------------------------
Download BIRT iHub F-Type - The Free Enterprise-Grade BIRT Server
from Actuate! Instantly Supercharge Your Business Reports and Dashboards
with Interactivity, Sharing, Native Excel Exports, App Integration & more
Get technology previously reserved for billion-dollar corporations, FREE
http://pubads.g.doubleclick.net/gampad/clk?id=164703151&iu=/4140/ostg.clktrk