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

Gabriel Dos Reis gdr at integrable-solutions.net
Fri Feb 10 03:27:48 UTC 2012


On Thu, Feb 9, 2012 at 8:24 PM, Daniel Herring <dherring at tentpost.com> wrote:
> 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));

You probably want to cast x and y to intptr_t and compare instead of the
substraction: you can't subtract two pointers that are not related to the same
object.  You also probably want to check for availability of alloca.

>   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
>
> ------------------------------------------------------------------------------
> Virtualization & Cloud Management Using Capacity Planning
> Cloud computing makes use of virtualization - but cloud computing
> also focuses on allowing computing to be delivered as a service.
> http://www.accelacomm.com/jaw/sfnl/114/51521223/
> _______________________________________________
> Ecls-list mailing list
> Ecls-list at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/ecls-list




More information about the ecl-devel mailing list