D-Programming-Language/druntime — pull request #244 by alexrp (2012-06-19 16:05:02+00:00)
Title: Sanitize calculation of thread stack bottom on Linux.

This fixes an issue with druntime's calculation of the stack bottom for non-main threads on Linux that has been around for a long time.

The issue is that `__libc_stack_end` only returns the stack end for the main thread. Any other threads that attempt to get their stack bottom via this global will cause the runtime to blow up since their stack top value will be correct, but their stack bottom value will be completely bogus (this results in the GC scanning an insanely large area of memory that doesn't even belong to the process). This is why the comment in `core.thread` was in place, and an inline assembly solution was used. The inline assembly solution works fine for D threads, but note that the code in `thread_attachThis` does not use this method (and for good reasons). This meant that threads attaching to the runtime and then doing a GC cycle would almost always cause the GC to explode.

This patch fixes `rt_stackBottom` on Linux by using the Linux-specific `pthread_getattr_np` function along with `pthread_attr_getstack` to fetch the stack bottom value. This will always work for threads attaching to the runtime and for D threads.

I've also properly documented `thread_stackBottom` now that it will work properly on all currently supported platforms.

Note that this still leaves the old, broken method around for Solaris. I just don't know how to achieve the same results on that platform, so someone else will have to fix that. I've added a FIXME note so it's not forgotten. Besides, I don't believe we currently *support* Solaris anyway.

Please merge this for 2.060 - it is essential for proper C interop in multithreaded applications on Linux.