[cfe-dev] stack size issue

Yingshen Yu yingshen.yu at gmail.com
Mon Mar 5 02:40:47 PST 2012


Hi,

First of all, compiler version:
clang -v
Apple clang version 3.1 (tags/Apple/clang-318.0.45) (based on LLVM 3.1svn)
Target: x86_64-apple-darwin11.3.0
Thread model: posix


I am migrating an old C++ library to use clang, and it has some a stack
size checking code, the essential part is like below:

unsigned char* init_stack_base; //this is set before calling recurse()

void recurse() {
      unsigned char buf[0x100];
      unsigned long long bytes_used;
      bytes_used = (init_stack_base - buf);
      if (bytes_used > STACK_LIMIT + 0x100)
            my_assert(); // void my_assert();
      recurse();
}

The variable size for  one recurse() call is sizeof(buf) +
sizeof(bytes_used). which is 256+8= 264, of course I need consider the
stack is aligned with 16bytes, so it will be 0x110 (272).  This is exactly
the number I got from LLVM GCC 4.2.

The interesting thing is, when I switch to clang,  it reserves 0x120 bytes
for variables. As a result, the code now asserts because the STACK_LIMIT is
defined  with the assumption above.

Anyone can explain why clang needs alloc extra 16 bytes stack for the same
code?

If you need test it, I pasted the test program at:
http://snipt.org/ufDi0

thanks!

-- 
Yingshen(Jonny) Yu
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20120305/b00ba3bf/attachment.html>


More information about the cfe-dev mailing list