This is the mail archive of the mailing list for the GCC project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

RFC: __attribute__ ((forced_stack_boundary))


There is a problem with some versions of pthread library on linux (RH 8.0 for example), where the stack of subthreads is not aligned correctly. This problem is covered in PR target/10395 (, and as 10 duplicates show, the problem is quite serious.

The problem will be exposed, when SSE code is used in subthread. Here is the example from PR target/10395:

/* *** start ***/
#include <pthread.h>
#include <stdio.h>
#include <assert.h>

#include <xmmintrin.h>
#include <mmintrin.h>

#ifdef __ICC
#include <emmintrin.h>

void * f(void *p)
 int x = (p == NULL) ? 0 : * (int *) p;
 __m128i s;
 printf("&x = %p &s= %p\n", &x, &s);
 return NULL;

int main(int argc, char ** argv)
 pthread_t th;

 f(& argc);
 assert(pthread_create(& th, NULL, f, &argc)==0);
 assert(pthread_join(th, NULL)==0);
 return 0;

/* ***end *** /

$ /usr/lib/gcc-snapshot/bin/gcc -pthread -msse2 gcc_test.c -o gcc_test.LINUX
$ ./gcc_test.LINUX
&x = 0xbffffb6c &s= 0xbffffb50
&x = 0xbf7ffae8 &s= 0xbf7ffacc <---- error

Accesing variable 's' on unaligned stack will cause a segfault.

To solve this problem, I would like to propose a function attribute, named "forced_stack_boundary". This attribute would force stack alignment in the same way as it is forced in the main() function:

       pushl   %ebp
       movl %esp, %ebp
       subl $8, %esp
       andl $-16, %esp     <- this
       subl $16, %esp

The use of __attribute__ keyword is backward compatible. When __attribute__ ((forced_stack_boundary)) is added to the prototype of the function, older gccs will just display a warning message:

pr10395.c:13: warning: 'forced_stack_boundary' attribute directive ignored

The produced object code would eliminate the effects of pthread stack alignment bug, it would be compatible with all versions of pthread library, and would have the overhead of a couple of instructions.

If this attribute is mentioned in the gcc release notes, perhaps affected application source code will be changed to make it gcc-4.0 'compatible'. In above example, the only change would be the addition of one line:

void * f(void *p) __attribute__ ((forced_stack_boundary));

Finally, this line is from comment #1:

The code works as expected with Intel's compiler (build 7.1.011), I guess it uses the same libpthread as gcc does.

This means that a lot of bugreports will be filled with "gcc segfaults where icc doesn't".


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]