Home | History | Annotate | Download | only in tests
      1 
      2 /* Check basic stack overflow detection.
      3 
      4    It's difficult to get consistent behaviour across all platforms.
      5    For example, x86 w/ gcc-4.3.1 gives
      6 
      7      Expected: stack array "a" in frame 2 back from here
      8      Actual:   stack array "beforea" in frame 2 back from here
      9 
     10    whereas amd64 w/ gcc-4.3.1 gives
     11 
     12      Expected: stack array "a" in frame 2 back from here
     13      Actual:   unknown
     14 
     15    This happens because on x86 the arrays are placed on the
     16    stack without holes in between, but not so for amd64.  I don't
     17    know why.
     18 */
     19 
     20 
     21 #include <stdio.h>
     22 
     23 __attribute__((noinline)) void foo ( long* sa, int n )
     24 {
     25   int i;
     26   for (i = 0; i < n; i++)
     27     sa[i] = 0;
     28 }
     29 
     30 __attribute__((noinline)) void bar ( long* sa, int n )
     31 {
     32    foo(sa, n);
     33 }
     34 
     35 int main ( void )
     36 {
     37   int i;
     38   long beforea[3];
     39   long a[7];
     40   long aftera[3];
     41   bar(a, 7+1);     /* generates error */
     42   bar(a, 7+0);     /* generates no error */
     43   for (i = 0; i < 7+1; i++) {
     44      a[i] = 0;
     45   }
     46  {char beforebuf[8];
     47   char buf[8];
     48   char afterbuf[8];
     49   sprintf(buf, "%d", 123456789);
     50   return 1 & ((a[4] + beforea[1] + aftera[1] + beforebuf[1]
     51                     + buf[2] + afterbuf[3]) / 100000) ;
     52  }
     53 }
     54