[Ecls-list] Bug in code detecting stack growth direction

Daniel Herring dherring at tentpost.com
Fri Feb 10 02:24:45 UTC 2012


On Fri, 3 Feb 2012, Juan Jose Garcia-Ripoll wrote:

> I just noticed that some cleverly optimizing compilers broke the code I used to detect whether the stack grows upwards or downwards. I will upload a patch tonight.

Wouldn't two calls to alloca() in a single function give a reliable answer 
on pretty much all platforms?

#include <alloca.h>
#include <stdio.h>
int main()
{
   void *x;
   void *y;
   x=alloca(100);
   y=alloca(100);
   printf("delta: %d\n", (int)(y-x));
   return 0;
}


The following was another idea; but it looks no better than what you have 
now.

#include <stdio.h>
#include <stdlib.h>
void * start;
void * end;
void recurse(int n, void *y)
{
   int x;
   if(n==0)
     end=&x;
   else
     recurse(n-1, &x);
}
int main(int argc, char **argv)
{
    int x;
    int times;
    start=&x;
    times=(random()%10)+1;
    recurse(times, start);
    printf("delta: %d\n", (int)(end-start));
    return 0;
}


This link indicates Sparc could violate some common assumptions about 
stack behavior (though it looks to me like he was confusing the register 
window with the stack).
http://stackoverflow.com/questions/664744/what-is-the-direction-of-stack-growth-in-most-modern-systems

- Daniel




More information about the ecl-devel mailing list