Menu

#14 Stacks not aligned on non-Linux i386

open
nobody
None
5
2012-03-26
2012-03-26
No

I came across an issue where st would segfault when built with gcc 4.5.0 using -O3
on Solaris 10 i386. The issue turned out to be that the stack was not properly
aligned for the use of MMX/SSE instructions, which require their arguments to be
16-byte aligned.

The problem does not occur under linux because of the custom assembly used
in saving and restoring the context, which has an .align 16.

At least for GCC, the attached patch addresses the problem; it enables the fix
on the earliest version of GCC it is possible on (4.2.0).

Some more details:

On i386, GCC will ensure the stack is 16-byte aligned if necessary, but will only
check and re-align in main(), instead of in each function that uses those instructions.
The stacks created for st threads are not, then, covered by this, and will only be
word-aligned, which is the default for the i386 ABI.

GCC provides a force_align_arg_pointer function attribute, which will cause the
decorated function to ensure that the stack is aligned to the largest degree necessary.
Decorating st_thread_main() suffices to address the problem, as GCC will automatically
maintain alignment from there on in.

This is not required on x86-64, because that ABI explicitly requires that any array of
at least 16 bytes, or any C99 variable length array, be 16-byte aligned, specifically for
SSE instructions. Indeed, the force_align_arg_pointer attribute is not available for
that architecture.

References:
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=40838
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47842
http://mail.python.org/pipermail/python-dev/2010-May/100050.html
http://www.sco.com/developers/devspecs/abi386-4.pdf
http://www.x86-64.org/documentation/abi.pdf

Discussion

  • Matthew Ryan

    Matthew Ryan - 2012-03-26
     
  • Mike Abbott

    Mike Abbott - 2012-04-12

    Rather than rely on a feature specific to your compiler, could you supply a patch which simply adjusts MD_STACK_PAD_SIZE to an appropriate value for your platform?

     

Log in to post a comment.